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
;
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
904 static cp_declarator
*
905 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
906 special_function_kind sfk
)
908 cp_declarator
*declarator
;
910 /* It is valid to write:
912 class C { void f(); };
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
920 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
921 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
923 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
927 declarator
= make_declarator (cdk_id
);
928 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
929 declarator
->u
.id
.unqualified_name
= unqualified_name
;
930 declarator
->u
.id
.sfk
= sfk
;
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
940 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
942 cp_declarator
*declarator
;
944 declarator
= make_declarator (cdk_pointer
);
945 declarator
->declarator
= target
;
946 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
947 declarator
->u
.pointer
.class_type
= NULL_TREE
;
952 /* Like make_pointer_declarator -- but for references. */
955 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
957 cp_declarator
*declarator
;
959 declarator
= make_declarator (cdk_reference
);
960 declarator
->declarator
= target
;
961 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
962 declarator
->u
.pointer
.class_type
= NULL_TREE
;
967 /* Like make_pointer_declarator -- but for a pointer to a non-static
968 member of CLASS_TYPE. */
971 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
972 cp_declarator
*pointee
)
974 cp_declarator
*declarator
;
976 declarator
= make_declarator (cdk_ptrmem
);
977 declarator
->declarator
= pointee
;
978 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
979 declarator
->u
.pointer
.class_type
= class_type
;
984 /* Make a declarator for the function given by TARGET, with the
985 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
986 "const"-qualified member function. The EXCEPTION_SPECIFICATION
987 indicates what exceptions can be thrown. */
990 make_call_declarator (cp_declarator
*target
,
991 cp_parameter_declarator
*parms
,
992 cp_cv_quals cv_qualifiers
,
993 tree exception_specification
)
995 cp_declarator
*declarator
;
997 declarator
= make_declarator (cdk_function
);
998 declarator
->declarator
= target
;
999 declarator
->u
.function
.parameters
= parms
;
1000 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1001 declarator
->u
.function
.exception_specification
= exception_specification
;
1006 /* Make a declarator for an array of BOUNDS elements, each of which is
1007 defined by ELEMENT. */
1010 make_array_declarator (cp_declarator
*element
, tree bounds
)
1012 cp_declarator
*declarator
;
1014 declarator
= make_declarator (cdk_array
);
1015 declarator
->declarator
= element
;
1016 declarator
->u
.array
.bounds
= bounds
;
1021 cp_parameter_declarator
*no_parameters
;
1023 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1024 DECLARATOR and DEFAULT_ARGUMENT. */
1026 cp_parameter_declarator
*
1027 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1028 cp_declarator
*declarator
,
1029 tree default_argument
)
1031 cp_parameter_declarator
*parameter
;
1033 parameter
= ((cp_parameter_declarator
*)
1034 alloc_declarator (sizeof (cp_parameter_declarator
)));
1035 parameter
->next
= NULL
;
1036 if (decl_specifiers
)
1037 parameter
->decl_specifiers
= *decl_specifiers
;
1039 clear_decl_specs (¶meter
->decl_specifiers
);
1040 parameter
->declarator
= declarator
;
1041 parameter
->default_argument
= default_argument
;
1042 parameter
->ellipsis_p
= false;
1047 /* Returns true iff DECLARATOR is a declaration for a function. */
1050 function_declarator_p (const cp_declarator
*declarator
)
1054 if (declarator
->kind
== cdk_function
1055 && declarator
->declarator
->kind
== cdk_id
)
1057 if (declarator
->kind
== cdk_id
1058 || declarator
->kind
== cdk_error
)
1060 declarator
= declarator
->declarator
;
1070 A cp_parser parses the token stream as specified by the C++
1071 grammar. Its job is purely parsing, not semantic analysis. For
1072 example, the parser breaks the token stream into declarators,
1073 expressions, statements, and other similar syntactic constructs.
1074 It does not check that the types of the expressions on either side
1075 of an assignment-statement are compatible, or that a function is
1076 not declared with a parameter of type `void'.
1078 The parser invokes routines elsewhere in the compiler to perform
1079 semantic analysis and to build up the abstract syntax tree for the
1082 The parser (and the template instantiation code, which is, in a
1083 way, a close relative of parsing) are the only parts of the
1084 compiler that should be calling push_scope and pop_scope, or
1085 related functions. The parser (and template instantiation code)
1086 keeps track of what scope is presently active; everything else
1087 should simply honor that. (The code that generates static
1088 initializers may also need to set the scope, in order to check
1089 access control correctly when emitting the initializers.)
1094 The parser is of the standard recursive-descent variety. Upcoming
1095 tokens in the token stream are examined in order to determine which
1096 production to use when parsing a non-terminal. Some C++ constructs
1097 require arbitrary look ahead to disambiguate. For example, it is
1098 impossible, in the general case, to tell whether a statement is an
1099 expression or declaration without scanning the entire statement.
1100 Therefore, the parser is capable of "parsing tentatively." When the
1101 parser is not sure what construct comes next, it enters this mode.
1102 Then, while we attempt to parse the construct, the parser queues up
1103 error messages, rather than issuing them immediately, and saves the
1104 tokens it consumes. If the construct is parsed successfully, the
1105 parser "commits", i.e., it issues any queued error messages and
1106 the tokens that were being preserved are permanently discarded.
1107 If, however, the construct is not parsed successfully, the parser
1108 rolls back its state completely so that it can resume parsing using
1109 a different alternative.
1114 The performance of the parser could probably be improved substantially.
1115 We could often eliminate the need to parse tentatively by looking ahead
1116 a little bit. In some places, this approach might not entirely eliminate
1117 the need to parse tentatively, but it might still speed up the average
1120 /* Flags that are passed to some parsing functions. These values can
1121 be bitwise-ored together. */
1123 typedef enum cp_parser_flags
1126 CP_PARSER_FLAGS_NONE
= 0x0,
1127 /* The construct is optional. If it is not present, then no error
1128 should be issued. */
1129 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1130 /* When parsing a type-specifier, do not allow user-defined types. */
1131 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1134 /* The different kinds of declarators we want to parse. */
1136 typedef enum cp_parser_declarator_kind
1138 /* We want an abstract declarator. */
1139 CP_PARSER_DECLARATOR_ABSTRACT
,
1140 /* We want a named declarator. */
1141 CP_PARSER_DECLARATOR_NAMED
,
1142 /* We don't mind, but the name must be an unqualified-id. */
1143 CP_PARSER_DECLARATOR_EITHER
1144 } cp_parser_declarator_kind
;
1146 /* The precedence values used to parse binary expressions. The minimum value
1147 of PREC must be 1, because zero is reserved to quickly discriminate
1148 binary operators from other tokens. */
1153 PREC_LOGICAL_OR_EXPRESSION
,
1154 PREC_LOGICAL_AND_EXPRESSION
,
1155 PREC_INCLUSIVE_OR_EXPRESSION
,
1156 PREC_EXCLUSIVE_OR_EXPRESSION
,
1157 PREC_AND_EXPRESSION
,
1158 PREC_EQUALITY_EXPRESSION
,
1159 PREC_RELATIONAL_EXPRESSION
,
1160 PREC_SHIFT_EXPRESSION
,
1161 PREC_ADDITIVE_EXPRESSION
,
1162 PREC_MULTIPLICATIVE_EXPRESSION
,
1164 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1167 /* A mapping from a token type to a corresponding tree node type, with a
1168 precedence value. */
1170 typedef struct cp_parser_binary_operations_map_node
1172 /* The token type. */
1173 enum cpp_ttype token_type
;
1174 /* The corresponding tree code. */
1175 enum tree_code tree_type
;
1176 /* The precedence of this operator. */
1177 enum cp_parser_prec prec
;
1178 } cp_parser_binary_operations_map_node
;
1180 /* The status of a tentative parse. */
1182 typedef enum cp_parser_status_kind
1184 /* No errors have occurred. */
1185 CP_PARSER_STATUS_KIND_NO_ERROR
,
1186 /* An error has occurred. */
1187 CP_PARSER_STATUS_KIND_ERROR
,
1188 /* We are committed to this tentative parse, whether or not an error
1190 CP_PARSER_STATUS_KIND_COMMITTED
1191 } cp_parser_status_kind
;
1193 typedef struct cp_parser_expression_stack_entry
1195 /* Left hand side of the binary operation we are currently
1198 /* Original tree code for left hand side, if it was a binary
1199 expression itself (used for -Wparentheses). */
1200 enum tree_code lhs_type
;
1201 /* Tree code for the binary operation we are parsing. */
1202 enum tree_code tree_type
;
1203 /* Precedence of the binary operation we are parsing. */
1205 } cp_parser_expression_stack_entry
;
1207 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1208 entries because precedence levels on the stack are monotonically
1210 typedef struct cp_parser_expression_stack_entry
1211 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1213 /* Context that is saved and restored when parsing tentatively. */
1214 typedef struct cp_parser_context
GTY (())
1216 /* If this is a tentative parsing context, the status of the
1218 enum cp_parser_status_kind status
;
1219 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1220 that are looked up in this context must be looked up both in the
1221 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1222 the context of the containing expression. */
1225 /* The next parsing context in the stack. */
1226 struct cp_parser_context
*next
;
1227 } cp_parser_context
;
1231 /* Constructors and destructors. */
1233 static cp_parser_context
*cp_parser_context_new
1234 (cp_parser_context
*);
1236 /* Class variables. */
1238 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1240 /* The operator-precedence table used by cp_parser_binary_expression.
1241 Transformed into an associative array (binops_by_token) by
1244 static const cp_parser_binary_operations_map_node binops
[] = {
1245 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1246 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1248 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1249 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1250 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1252 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1253 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1255 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1256 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1258 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1259 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1260 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1261 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1263 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1264 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1266 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1268 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1270 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1272 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1274 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1277 /* The same as binops, but initialized by cp_parser_new so that
1278 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1280 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1282 /* Constructors and destructors. */
1284 /* Construct a new context. The context below this one on the stack
1285 is given by NEXT. */
1287 static cp_parser_context
*
1288 cp_parser_context_new (cp_parser_context
* next
)
1290 cp_parser_context
*context
;
1292 /* Allocate the storage. */
1293 if (cp_parser_context_free_list
!= NULL
)
1295 /* Pull the first entry from the free list. */
1296 context
= cp_parser_context_free_list
;
1297 cp_parser_context_free_list
= context
->next
;
1298 memset (context
, 0, sizeof (*context
));
1301 context
= GGC_CNEW (cp_parser_context
);
1303 /* No errors have occurred yet in this context. */
1304 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1305 /* If this is not the bottomost context, copy information that we
1306 need from the previous context. */
1309 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1310 expression, then we are parsing one in this context, too. */
1311 context
->object_type
= next
->object_type
;
1312 /* Thread the stack. */
1313 context
->next
= next
;
1319 /* The cp_parser structure represents the C++ parser. */
1321 typedef struct cp_parser
GTY(())
1323 /* The lexer from which we are obtaining tokens. */
1326 /* The scope in which names should be looked up. If NULL_TREE, then
1327 we look up names in the scope that is currently open in the
1328 source program. If non-NULL, this is either a TYPE or
1329 NAMESPACE_DECL for the scope in which we should look. It can
1330 also be ERROR_MARK, when we've parsed a bogus scope.
1332 This value is not cleared automatically after a name is looked
1333 up, so we must be careful to clear it before starting a new look
1334 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1335 will look up `Z' in the scope of `X', rather than the current
1336 scope.) Unfortunately, it is difficult to tell when name lookup
1337 is complete, because we sometimes peek at a token, look it up,
1338 and then decide not to consume it. */
1341 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1342 last lookup took place. OBJECT_SCOPE is used if an expression
1343 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1344 respectively. QUALIFYING_SCOPE is used for an expression of the
1345 form "X::Y"; it refers to X. */
1347 tree qualifying_scope
;
1349 /* A stack of parsing contexts. All but the bottom entry on the
1350 stack will be tentative contexts.
1352 We parse tentatively in order to determine which construct is in
1353 use in some situations. For example, in order to determine
1354 whether a statement is an expression-statement or a
1355 declaration-statement we parse it tentatively as a
1356 declaration-statement. If that fails, we then reparse the same
1357 token stream as an expression-statement. */
1358 cp_parser_context
*context
;
1360 /* True if we are parsing GNU C++. If this flag is not set, then
1361 GNU extensions are not recognized. */
1362 bool allow_gnu_extensions_p
;
1364 /* TRUE if the `>' token should be interpreted as the greater-than
1365 operator. FALSE if it is the end of a template-id or
1366 template-parameter-list. */
1367 bool greater_than_is_operator_p
;
1369 /* TRUE if default arguments are allowed within a parameter list
1370 that starts at this point. FALSE if only a gnu extension makes
1371 them permissible. */
1372 bool default_arg_ok_p
;
1374 /* TRUE if we are parsing an integral constant-expression. See
1375 [expr.const] for a precise definition. */
1376 bool integral_constant_expression_p
;
1378 /* TRUE if we are parsing an integral constant-expression -- but a
1379 non-constant expression should be permitted as well. This flag
1380 is used when parsing an array bound so that GNU variable-length
1381 arrays are tolerated. */
1382 bool allow_non_integral_constant_expression_p
;
1384 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1385 been seen that makes the expression non-constant. */
1386 bool non_integral_constant_expression_p
;
1388 /* TRUE if local variable names and `this' are forbidden in the
1390 bool local_variables_forbidden_p
;
1392 /* TRUE if the declaration we are parsing is part of a
1393 linkage-specification of the form `extern string-literal
1395 bool in_unbraced_linkage_specification_p
;
1397 /* TRUE if we are presently parsing a declarator, after the
1398 direct-declarator. */
1399 bool in_declarator_p
;
1401 /* TRUE if we are presently parsing a template-argument-list. */
1402 bool in_template_argument_list_p
;
1404 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1405 to IN_OMP_BLOCK if parsing OpenMP structured block and
1406 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1407 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1408 iteration-statement, OpenMP block or loop within that switch. */
1409 #define IN_SWITCH_STMT 1
1410 #define IN_ITERATION_STMT 2
1411 #define IN_OMP_BLOCK 4
1412 #define IN_OMP_FOR 8
1413 unsigned char in_statement
;
1415 /* TRUE if we are presently parsing the body of a switch statement.
1416 Note that this doesn't quite overlap with in_statement above.
1417 The difference relates to giving the right sets of error messages:
1418 "case not in switch" vs "break statement used with OpenMP...". */
1419 bool in_switch_statement_p
;
1421 /* TRUE if we are parsing a type-id in an expression context. In
1422 such a situation, both "type (expr)" and "type (type)" are valid
1424 bool in_type_id_in_expr_p
;
1426 /* TRUE if we are currently in a header file where declarations are
1427 implicitly extern "C". */
1428 bool implicit_extern_c
;
1430 /* TRUE if strings in expressions should be translated to the execution
1432 bool translate_strings_p
;
1434 /* TRUE if we are presently parsing the body of a function, but not
1436 bool in_function_body
;
1438 /* If non-NULL, then we are parsing a construct where new type
1439 definitions are not permitted. The string stored here will be
1440 issued as an error message if a type is defined. */
1441 const char *type_definition_forbidden_message
;
1443 /* A list of lists. The outer list is a stack, used for member
1444 functions of local classes. At each level there are two sub-list,
1445 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1446 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1447 TREE_VALUE's. The functions are chained in reverse declaration
1450 The TREE_PURPOSE sublist contains those functions with default
1451 arguments that need post processing, and the TREE_VALUE sublist
1452 contains those functions with definitions that need post
1455 These lists can only be processed once the outermost class being
1456 defined is complete. */
1457 tree unparsed_functions_queues
;
1459 /* The number of classes whose definitions are currently in
1461 unsigned num_classes_being_defined
;
1463 /* The number of template parameter lists that apply directly to the
1464 current declaration. */
1465 unsigned num_template_parameter_lists
;
1470 /* Constructors and destructors. */
1472 static cp_parser
*cp_parser_new
1475 /* Routines to parse various constructs.
1477 Those that return `tree' will return the error_mark_node (rather
1478 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1479 Sometimes, they will return an ordinary node if error-recovery was
1480 attempted, even though a parse error occurred. So, to check
1481 whether or not a parse error occurred, you should always use
1482 cp_parser_error_occurred. If the construct is optional (indicated
1483 either by an `_opt' in the name of the function that does the
1484 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1485 the construct is not present. */
1487 /* Lexical conventions [gram.lex] */
1489 static tree cp_parser_identifier
1491 static tree cp_parser_string_literal
1492 (cp_parser
*, bool, bool);
1494 /* Basic concepts [gram.basic] */
1496 static bool cp_parser_translation_unit
1499 /* Expressions [gram.expr] */
1501 static tree cp_parser_primary_expression
1502 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1503 static tree cp_parser_id_expression
1504 (cp_parser
*, bool, bool, bool *, bool, bool);
1505 static tree cp_parser_unqualified_id
1506 (cp_parser
*, bool, bool, bool, bool);
1507 static tree cp_parser_nested_name_specifier_opt
1508 (cp_parser
*, bool, bool, bool, bool);
1509 static tree cp_parser_nested_name_specifier
1510 (cp_parser
*, bool, bool, bool, bool);
1511 static tree cp_parser_class_or_namespace_name
1512 (cp_parser
*, bool, bool, bool, bool, bool);
1513 static tree cp_parser_postfix_expression
1514 (cp_parser
*, bool, bool);
1515 static tree cp_parser_postfix_open_square_expression
1516 (cp_parser
*, tree
, bool);
1517 static tree cp_parser_postfix_dot_deref_expression
1518 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1519 static tree cp_parser_parenthesized_expression_list
1520 (cp_parser
*, bool, bool, bool *);
1521 static void cp_parser_pseudo_destructor_name
1522 (cp_parser
*, tree
*, tree
*);
1523 static tree cp_parser_unary_expression
1524 (cp_parser
*, bool, bool);
1525 static enum tree_code cp_parser_unary_operator
1527 static tree cp_parser_new_expression
1529 static tree cp_parser_new_placement
1531 static tree cp_parser_new_type_id
1532 (cp_parser
*, tree
*);
1533 static cp_declarator
*cp_parser_new_declarator_opt
1535 static cp_declarator
*cp_parser_direct_new_declarator
1537 static tree cp_parser_new_initializer
1539 static tree cp_parser_delete_expression
1541 static tree cp_parser_cast_expression
1542 (cp_parser
*, bool, bool);
1543 static tree cp_parser_binary_expression
1544 (cp_parser
*, bool);
1545 static tree cp_parser_question_colon_clause
1546 (cp_parser
*, tree
);
1547 static tree cp_parser_assignment_expression
1548 (cp_parser
*, bool);
1549 static enum tree_code cp_parser_assignment_operator_opt
1551 static tree cp_parser_expression
1552 (cp_parser
*, bool);
1553 static tree cp_parser_constant_expression
1554 (cp_parser
*, bool, bool *);
1555 static tree cp_parser_builtin_offsetof
1558 /* Statements [gram.stmt.stmt] */
1560 static void cp_parser_statement
1561 (cp_parser
*, tree
, bool, bool *);
1562 static void cp_parser_label_for_labeled_statement
1564 static tree cp_parser_expression_statement
1565 (cp_parser
*, tree
);
1566 static tree cp_parser_compound_statement
1567 (cp_parser
*, tree
, bool);
1568 static void cp_parser_statement_seq_opt
1569 (cp_parser
*, tree
);
1570 static tree cp_parser_selection_statement
1571 (cp_parser
*, bool *);
1572 static tree cp_parser_condition
1574 static tree cp_parser_iteration_statement
1576 static void cp_parser_for_init_statement
1578 static tree cp_parser_jump_statement
1580 static void cp_parser_declaration_statement
1583 static tree cp_parser_implicitly_scoped_statement
1584 (cp_parser
*, bool *);
1585 static void cp_parser_already_scoped_statement
1588 /* Declarations [gram.dcl.dcl] */
1590 static void cp_parser_declaration_seq_opt
1592 static void cp_parser_declaration
1594 static void cp_parser_block_declaration
1595 (cp_parser
*, bool);
1596 static void cp_parser_simple_declaration
1597 (cp_parser
*, bool);
1598 static void cp_parser_decl_specifier_seq
1599 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1600 static tree cp_parser_storage_class_specifier_opt
1602 static tree cp_parser_function_specifier_opt
1603 (cp_parser
*, cp_decl_specifier_seq
*);
1604 static tree cp_parser_type_specifier
1605 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1607 static tree cp_parser_simple_type_specifier
1608 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1609 static tree cp_parser_type_name
1611 static tree cp_parser_elaborated_type_specifier
1612 (cp_parser
*, bool, bool);
1613 static tree cp_parser_enum_specifier
1615 static void cp_parser_enumerator_list
1616 (cp_parser
*, tree
);
1617 static void cp_parser_enumerator_definition
1618 (cp_parser
*, tree
);
1619 static tree cp_parser_namespace_name
1621 static void cp_parser_namespace_definition
1623 static void cp_parser_namespace_body
1625 static tree cp_parser_qualified_namespace_specifier
1627 static void cp_parser_namespace_alias_definition
1629 static bool cp_parser_using_declaration
1630 (cp_parser
*, bool);
1631 static void cp_parser_using_directive
1633 static void cp_parser_asm_definition
1635 static void cp_parser_linkage_specification
1637 static void cp_parser_static_assert
1638 (cp_parser
*, bool);
1640 /* Declarators [gram.dcl.decl] */
1642 static tree cp_parser_init_declarator
1643 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1644 static cp_declarator
*cp_parser_declarator
1645 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1646 static cp_declarator
*cp_parser_direct_declarator
1647 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1648 static enum tree_code cp_parser_ptr_operator
1649 (cp_parser
*, tree
*, cp_cv_quals
*);
1650 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1652 static tree cp_parser_declarator_id
1653 (cp_parser
*, bool);
1654 static tree cp_parser_type_id
1656 static void cp_parser_type_specifier_seq
1657 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1658 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1660 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1661 (cp_parser
*, bool *);
1662 static cp_parameter_declarator
*cp_parser_parameter_declaration
1663 (cp_parser
*, bool, bool *);
1664 static void cp_parser_function_body
1666 static tree cp_parser_initializer
1667 (cp_parser
*, bool *, bool *);
1668 static tree cp_parser_initializer_clause
1669 (cp_parser
*, bool *);
1670 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1671 (cp_parser
*, bool *);
1673 static bool cp_parser_ctor_initializer_opt_and_function_body
1676 /* Classes [gram.class] */
1678 static tree cp_parser_class_name
1679 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1680 static tree cp_parser_class_specifier
1682 static tree cp_parser_class_head
1683 (cp_parser
*, bool *, tree
*, tree
*);
1684 static enum tag_types cp_parser_class_key
1686 static void cp_parser_member_specification_opt
1688 static void cp_parser_member_declaration
1690 static tree cp_parser_pure_specifier
1692 static tree cp_parser_constant_initializer
1695 /* Derived classes [gram.class.derived] */
1697 static tree cp_parser_base_clause
1699 static tree cp_parser_base_specifier
1702 /* Special member functions [gram.special] */
1704 static tree cp_parser_conversion_function_id
1706 static tree cp_parser_conversion_type_id
1708 static cp_declarator
*cp_parser_conversion_declarator_opt
1710 static bool cp_parser_ctor_initializer_opt
1712 static void cp_parser_mem_initializer_list
1714 static tree cp_parser_mem_initializer
1716 static tree cp_parser_mem_initializer_id
1719 /* Overloading [gram.over] */
1721 static tree cp_parser_operator_function_id
1723 static tree cp_parser_operator
1726 /* Templates [gram.temp] */
1728 static void cp_parser_template_declaration
1729 (cp_parser
*, bool);
1730 static tree cp_parser_template_parameter_list
1732 static tree cp_parser_template_parameter
1733 (cp_parser
*, bool *);
1734 static tree cp_parser_type_parameter
1736 static tree cp_parser_template_id
1737 (cp_parser
*, bool, bool, bool);
1738 static tree cp_parser_template_name
1739 (cp_parser
*, bool, bool, bool, bool *);
1740 static tree cp_parser_template_argument_list
1742 static tree cp_parser_template_argument
1744 static void cp_parser_explicit_instantiation
1746 static void cp_parser_explicit_specialization
1749 /* Exception handling [gram.exception] */
1751 static tree cp_parser_try_block
1753 static bool cp_parser_function_try_block
1755 static void cp_parser_handler_seq
1757 static void cp_parser_handler
1759 static tree cp_parser_exception_declaration
1761 static tree cp_parser_throw_expression
1763 static tree cp_parser_exception_specification_opt
1765 static tree cp_parser_type_id_list
1768 /* GNU Extensions */
1770 static tree cp_parser_asm_specification_opt
1772 static tree cp_parser_asm_operand_list
1774 static tree cp_parser_asm_clobber_list
1776 static tree cp_parser_attributes_opt
1778 static tree cp_parser_attribute_list
1780 static bool cp_parser_extension_opt
1781 (cp_parser
*, int *);
1782 static void cp_parser_label_declaration
1785 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1786 static bool cp_parser_pragma
1787 (cp_parser
*, enum pragma_context
);
1789 /* Objective-C++ Productions */
1791 static tree cp_parser_objc_message_receiver
1793 static tree cp_parser_objc_message_args
1795 static tree cp_parser_objc_message_expression
1797 static tree cp_parser_objc_encode_expression
1799 static tree cp_parser_objc_defs_expression
1801 static tree cp_parser_objc_protocol_expression
1803 static tree cp_parser_objc_selector_expression
1805 static tree cp_parser_objc_expression
1807 static bool cp_parser_objc_selector_p
1809 static tree cp_parser_objc_selector
1811 static tree cp_parser_objc_protocol_refs_opt
1813 static void cp_parser_objc_declaration
1815 static tree cp_parser_objc_statement
1818 /* Utility Routines */
1820 static tree cp_parser_lookup_name
1821 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1822 static tree cp_parser_lookup_name_simple
1823 (cp_parser
*, tree
);
1824 static tree cp_parser_maybe_treat_template_as_class
1826 static bool cp_parser_check_declarator_template_parameters
1827 (cp_parser
*, cp_declarator
*);
1828 static bool cp_parser_check_template_parameters
1829 (cp_parser
*, unsigned);
1830 static tree cp_parser_simple_cast_expression
1832 static tree cp_parser_global_scope_opt
1833 (cp_parser
*, bool);
1834 static bool cp_parser_constructor_declarator_p
1835 (cp_parser
*, bool);
1836 static tree cp_parser_function_definition_from_specifiers_and_declarator
1837 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1838 static tree cp_parser_function_definition_after_declarator
1839 (cp_parser
*, bool);
1840 static void cp_parser_template_declaration_after_export
1841 (cp_parser
*, bool);
1842 static void cp_parser_perform_template_parameter_access_checks
1843 (VEC (deferred_access_check
,gc
)*);
1844 static tree cp_parser_single_declaration
1845 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool *);
1846 static tree cp_parser_functional_cast
1847 (cp_parser
*, tree
);
1848 static tree cp_parser_save_member_function_body
1849 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1850 static tree cp_parser_enclosed_template_argument_list
1852 static void cp_parser_save_default_args
1853 (cp_parser
*, tree
);
1854 static void cp_parser_late_parsing_for_member
1855 (cp_parser
*, tree
);
1856 static void cp_parser_late_parsing_default_args
1857 (cp_parser
*, tree
);
1858 static tree cp_parser_sizeof_operand
1859 (cp_parser
*, enum rid
);
1860 static bool cp_parser_declares_only_class_p
1862 static void cp_parser_set_storage_class
1863 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1864 static void cp_parser_set_decl_spec_type
1865 (cp_decl_specifier_seq
*, tree
, bool);
1866 static bool cp_parser_friend_p
1867 (const cp_decl_specifier_seq
*);
1868 static cp_token
*cp_parser_require
1869 (cp_parser
*, enum cpp_ttype
, const char *);
1870 static cp_token
*cp_parser_require_keyword
1871 (cp_parser
*, enum rid
, const char *);
1872 static bool cp_parser_token_starts_function_definition_p
1874 static bool cp_parser_next_token_starts_class_definition_p
1876 static bool cp_parser_next_token_ends_template_argument_p
1878 static bool cp_parser_nth_token_starts_template_argument_list_p
1879 (cp_parser
*, size_t);
1880 static enum tag_types cp_parser_token_is_class_key
1882 static void cp_parser_check_class_key
1883 (enum tag_types
, tree type
);
1884 static void cp_parser_check_access_in_redeclaration
1886 static bool cp_parser_optional_template_keyword
1888 static void cp_parser_pre_parsed_nested_name_specifier
1890 static void cp_parser_cache_group
1891 (cp_parser
*, enum cpp_ttype
, unsigned);
1892 static void cp_parser_parse_tentatively
1894 static void cp_parser_commit_to_tentative_parse
1896 static void cp_parser_abort_tentative_parse
1898 static bool cp_parser_parse_definitely
1900 static inline bool cp_parser_parsing_tentatively
1902 static bool cp_parser_uncommitted_to_tentative_parse_p
1904 static void cp_parser_error
1905 (cp_parser
*, const char *);
1906 static void cp_parser_name_lookup_error
1907 (cp_parser
*, tree
, tree
, const char *);
1908 static bool cp_parser_simulate_error
1910 static bool cp_parser_check_type_definition
1912 static void cp_parser_check_for_definition_in_return_type
1913 (cp_declarator
*, tree
);
1914 static void cp_parser_check_for_invalid_template_id
1915 (cp_parser
*, tree
);
1916 static bool cp_parser_non_integral_constant_expression
1917 (cp_parser
*, const char *);
1918 static void cp_parser_diagnose_invalid_type_name
1919 (cp_parser
*, tree
, tree
);
1920 static bool cp_parser_parse_and_diagnose_invalid_type_name
1922 static int cp_parser_skip_to_closing_parenthesis
1923 (cp_parser
*, bool, bool, bool);
1924 static void cp_parser_skip_to_end_of_statement
1926 static void cp_parser_consume_semicolon_at_end_of_statement
1928 static void cp_parser_skip_to_end_of_block_or_statement
1930 static void cp_parser_skip_to_closing_brace
1932 static void cp_parser_skip_to_end_of_template_parameter_list
1934 static void cp_parser_skip_to_pragma_eol
1935 (cp_parser
*, cp_token
*);
1936 static bool cp_parser_error_occurred
1938 static bool cp_parser_allow_gnu_extensions_p
1940 static bool cp_parser_is_string_literal
1942 static bool cp_parser_is_keyword
1943 (cp_token
*, enum rid
);
1944 static tree cp_parser_make_typename_type
1945 (cp_parser
*, tree
, tree
);
1947 /* Returns nonzero if we are parsing tentatively. */
1950 cp_parser_parsing_tentatively (cp_parser
* parser
)
1952 return parser
->context
->next
!= NULL
;
1955 /* Returns nonzero if TOKEN is a string literal. */
1958 cp_parser_is_string_literal (cp_token
* token
)
1960 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1963 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1966 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1968 return token
->keyword
== keyword
;
1971 /* If not parsing tentatively, issue a diagnostic of the form
1972 FILE:LINE: MESSAGE before TOKEN
1973 where TOKEN is the next token in the input stream. MESSAGE
1974 (specified by the caller) is usually of the form "expected
1978 cp_parser_error (cp_parser
* parser
, const char* message
)
1980 if (!cp_parser_simulate_error (parser
))
1982 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1983 /* This diagnostic makes more sense if it is tagged to the line
1984 of the token we just peeked at. */
1985 cp_lexer_set_source_position_from_token (token
);
1987 if (token
->type
== CPP_PRAGMA
)
1989 error ("%<#pragma%> is not allowed here");
1990 cp_parser_skip_to_pragma_eol (parser
, token
);
1994 c_parse_error (message
,
1995 /* Because c_parser_error does not understand
1996 CPP_KEYWORD, keywords are treated like
1998 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2003 /* Issue an error about name-lookup failing. NAME is the
2004 IDENTIFIER_NODE DECL is the result of
2005 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2006 the thing that we hoped to find. */
2009 cp_parser_name_lookup_error (cp_parser
* parser
,
2012 const char* desired
)
2014 /* If name lookup completely failed, tell the user that NAME was not
2016 if (decl
== error_mark_node
)
2018 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2019 error ("%<%D::%D%> has not been declared",
2020 parser
->scope
, name
);
2021 else if (parser
->scope
== global_namespace
)
2022 error ("%<::%D%> has not been declared", name
);
2023 else if (parser
->object_scope
2024 && !CLASS_TYPE_P (parser
->object_scope
))
2025 error ("request for member %qD in non-class type %qT",
2026 name
, parser
->object_scope
);
2027 else if (parser
->object_scope
)
2028 error ("%<%T::%D%> has not been declared",
2029 parser
->object_scope
, name
);
2031 error ("%qD has not been declared", name
);
2033 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2034 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
2035 else if (parser
->scope
== global_namespace
)
2036 error ("%<::%D%> %s", name
, desired
);
2038 error ("%qD %s", name
, desired
);
2041 /* If we are parsing tentatively, remember that an error has occurred
2042 during this tentative parse. Returns true if the error was
2043 simulated; false if a message should be issued by the caller. */
2046 cp_parser_simulate_error (cp_parser
* parser
)
2048 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2050 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2056 /* Check for repeated decl-specifiers. */
2059 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
2063 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2065 unsigned count
= decl_specs
->specs
[(int)ds
];
2068 /* The "long" specifier is a special case because of "long long". */
2072 error ("%<long long long%> is too long for GCC");
2073 else if (pedantic
&& !in_system_header
&& warn_long_long
)
2074 pedwarn ("ISO C++ does not support %<long long%>");
2078 static const char *const decl_spec_names
[] = {
2094 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2099 /* This function is called when a type is defined. If type
2100 definitions are forbidden at this point, an error message is
2104 cp_parser_check_type_definition (cp_parser
* parser
)
2106 /* If types are forbidden here, issue a message. */
2107 if (parser
->type_definition_forbidden_message
)
2109 /* Use `%s' to print the string in case there are any escape
2110 characters in the message. */
2111 error ("%s", parser
->type_definition_forbidden_message
);
2117 /* This function is called when the DECLARATOR is processed. The TYPE
2118 was a type defined in the decl-specifiers. If it is invalid to
2119 define a type in the decl-specifiers for DECLARATOR, an error is
2123 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2126 /* [dcl.fct] forbids type definitions in return types.
2127 Unfortunately, it's not easy to know whether or not we are
2128 processing a return type until after the fact. */
2130 && (declarator
->kind
== cdk_pointer
2131 || declarator
->kind
== cdk_reference
2132 || declarator
->kind
== cdk_ptrmem
))
2133 declarator
= declarator
->declarator
;
2135 && declarator
->kind
== cdk_function
)
2137 error ("new types may not be defined in a return type");
2138 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2143 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2144 "<" in any valid C++ program. If the next token is indeed "<",
2145 issue a message warning the user about what appears to be an
2146 invalid attempt to form a template-id. */
2149 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2152 cp_token_position start
= 0;
2154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2157 error ("%qT is not a template", type
);
2158 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2159 error ("%qE is not a template", type
);
2161 error ("invalid template-id");
2162 /* Remember the location of the invalid "<". */
2163 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2164 start
= cp_lexer_token_position (parser
->lexer
, true);
2165 /* Consume the "<". */
2166 cp_lexer_consume_token (parser
->lexer
);
2167 /* Parse the template arguments. */
2168 cp_parser_enclosed_template_argument_list (parser
);
2169 /* Permanently remove the invalid template arguments so that
2170 this error message is not issued again. */
2172 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2176 /* If parsing an integral constant-expression, issue an error message
2177 about the fact that THING appeared and return true. Otherwise,
2178 return false. In either case, set
2179 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2182 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2185 parser
->non_integral_constant_expression_p
= true;
2186 if (parser
->integral_constant_expression_p
)
2188 if (!parser
->allow_non_integral_constant_expression_p
)
2190 error ("%s cannot appear in a constant-expression", thing
);
2197 /* Emit a diagnostic for an invalid type name. SCOPE is the
2198 qualifying scope (or NULL, if none) for ID. This function commits
2199 to the current active tentative parse, if any. (Otherwise, the
2200 problematic construct might be encountered again later, resulting
2201 in duplicate error messages.) */
2204 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2206 tree decl
, old_scope
;
2207 /* Try to lookup the identifier. */
2208 old_scope
= parser
->scope
;
2209 parser
->scope
= scope
;
2210 decl
= cp_parser_lookup_name_simple (parser
, id
);
2211 parser
->scope
= old_scope
;
2212 /* If the lookup found a template-name, it means that the user forgot
2213 to specify an argument list. Emit a useful error message. */
2214 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2215 error ("invalid use of template-name %qE without an argument list", decl
);
2216 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2217 error ("invalid use of destructor %qD as a type", id
);
2218 else if (TREE_CODE (decl
) == TYPE_DECL
)
2219 /* Something like 'unsigned A a;' */
2220 error ("invalid combination of multiple type-specifiers");
2221 else if (!parser
->scope
)
2223 /* Issue an error message. */
2224 error ("%qE does not name a type", id
);
2225 /* If we're in a template class, it's possible that the user was
2226 referring to a type from a base class. For example:
2228 template <typename T> struct A { typedef T X; };
2229 template <typename T> struct B : public A<T> { X x; };
2231 The user should have said "typename A<T>::X". */
2232 if (processing_template_decl
&& current_class_type
2233 && TYPE_BINFO (current_class_type
))
2237 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2241 tree base_type
= BINFO_TYPE (b
);
2242 if (CLASS_TYPE_P (base_type
)
2243 && dependent_type_p (base_type
))
2246 /* Go from a particular instantiation of the
2247 template (which will have an empty TYPE_FIELDs),
2248 to the main version. */
2249 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2250 for (field
= TYPE_FIELDS (base_type
);
2252 field
= TREE_CHAIN (field
))
2253 if (TREE_CODE (field
) == TYPE_DECL
2254 && DECL_NAME (field
) == id
)
2256 inform ("(perhaps %<typename %T::%E%> was intended)",
2257 BINFO_TYPE (b
), id
);
2266 /* Here we diagnose qualified-ids where the scope is actually correct,
2267 but the identifier does not resolve to a valid type name. */
2268 else if (parser
->scope
!= error_mark_node
)
2270 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2271 error ("%qE in namespace %qE does not name a type",
2273 else if (TYPE_P (parser
->scope
))
2274 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2278 cp_parser_commit_to_tentative_parse (parser
);
2281 /* Check for a common situation where a type-name should be present,
2282 but is not, and issue a sensible error message. Returns true if an
2283 invalid type-name was detected.
2285 The situation handled by this function are variable declarations of the
2286 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2287 Usually, `ID' should name a type, but if we got here it means that it
2288 does not. We try to emit the best possible error message depending on
2289 how exactly the id-expression looks like. */
2292 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2296 cp_parser_parse_tentatively (parser
);
2297 id
= cp_parser_id_expression (parser
,
2298 /*template_keyword_p=*/false,
2299 /*check_dependency_p=*/true,
2300 /*template_p=*/NULL
,
2301 /*declarator_p=*/true,
2302 /*optional_p=*/false);
2303 /* After the id-expression, there should be a plain identifier,
2304 otherwise this is not a simple variable declaration. Also, if
2305 the scope is dependent, we cannot do much. */
2306 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2307 || (parser
->scope
&& TYPE_P (parser
->scope
)
2308 && dependent_type_p (parser
->scope
)))
2310 cp_parser_abort_tentative_parse (parser
);
2313 if (!cp_parser_parse_definitely (parser
) || TREE_CODE (id
) == TYPE_DECL
)
2316 /* Emit a diagnostic for the invalid type. */
2317 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2318 /* Skip to the end of the declaration; there's no point in
2319 trying to process it. */
2320 cp_parser_skip_to_end_of_block_or_statement (parser
);
2324 /* Consume tokens up to, and including, the next non-nested closing `)'.
2325 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2326 are doing error recovery. Returns -1 if OR_COMMA is true and we
2327 found an unnested comma. */
2330 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2335 unsigned paren_depth
= 0;
2336 unsigned brace_depth
= 0;
2338 if (recovering
&& !or_comma
2339 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2344 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2346 switch (token
->type
)
2349 case CPP_PRAGMA_EOL
:
2350 /* If we've run out of tokens, then there is no closing `)'. */
2354 /* This matches the processing in skip_to_end_of_statement. */
2359 case CPP_OPEN_BRACE
:
2362 case CPP_CLOSE_BRACE
:
2368 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2372 case CPP_OPEN_PAREN
:
2377 case CPP_CLOSE_PAREN
:
2378 if (!brace_depth
&& !paren_depth
--)
2381 cp_lexer_consume_token (parser
->lexer
);
2390 /* Consume the token. */
2391 cp_lexer_consume_token (parser
->lexer
);
2395 /* Consume tokens until we reach the end of the current statement.
2396 Normally, that will be just before consuming a `;'. However, if a
2397 non-nested `}' comes first, then we stop before consuming that. */
2400 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2402 unsigned nesting_depth
= 0;
2406 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2408 switch (token
->type
)
2411 case CPP_PRAGMA_EOL
:
2412 /* If we've run out of tokens, stop. */
2416 /* If the next token is a `;', we have reached the end of the
2422 case CPP_CLOSE_BRACE
:
2423 /* If this is a non-nested '}', stop before consuming it.
2424 That way, when confronted with something like:
2428 we stop before consuming the closing '}', even though we
2429 have not yet reached a `;'. */
2430 if (nesting_depth
== 0)
2433 /* If it is the closing '}' for a block that we have
2434 scanned, stop -- but only after consuming the token.
2440 we will stop after the body of the erroneously declared
2441 function, but before consuming the following `typedef'
2443 if (--nesting_depth
== 0)
2445 cp_lexer_consume_token (parser
->lexer
);
2449 case CPP_OPEN_BRACE
:
2457 /* Consume the token. */
2458 cp_lexer_consume_token (parser
->lexer
);
2462 /* This function is called at the end of a statement or declaration.
2463 If the next token is a semicolon, it is consumed; otherwise, error
2464 recovery is attempted. */
2467 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2469 /* Look for the trailing `;'. */
2470 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2472 /* If there is additional (erroneous) input, skip to the end of
2474 cp_parser_skip_to_end_of_statement (parser
);
2475 /* If the next token is now a `;', consume it. */
2476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2477 cp_lexer_consume_token (parser
->lexer
);
2481 /* Skip tokens until we have consumed an entire block, or until we
2482 have consumed a non-nested `;'. */
2485 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2487 int nesting_depth
= 0;
2489 while (nesting_depth
>= 0)
2491 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2493 switch (token
->type
)
2496 case CPP_PRAGMA_EOL
:
2497 /* If we've run out of tokens, stop. */
2501 /* Stop if this is an unnested ';'. */
2506 case CPP_CLOSE_BRACE
:
2507 /* Stop if this is an unnested '}', or closes the outermost
2514 case CPP_OPEN_BRACE
:
2523 /* Consume the token. */
2524 cp_lexer_consume_token (parser
->lexer
);
2528 /* Skip tokens until a non-nested closing curly brace is the next
2532 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2534 unsigned nesting_depth
= 0;
2538 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2540 switch (token
->type
)
2543 case CPP_PRAGMA_EOL
:
2544 /* If we've run out of tokens, stop. */
2547 case CPP_CLOSE_BRACE
:
2548 /* If the next token is a non-nested `}', then we have reached
2549 the end of the current block. */
2550 if (nesting_depth
-- == 0)
2554 case CPP_OPEN_BRACE
:
2555 /* If it the next token is a `{', then we are entering a new
2556 block. Consume the entire block. */
2564 /* Consume the token. */
2565 cp_lexer_consume_token (parser
->lexer
);
2569 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2570 parameter is the PRAGMA token, allowing us to purge the entire pragma
2574 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2578 parser
->lexer
->in_pragma
= false;
2581 token
= cp_lexer_consume_token (parser
->lexer
);
2582 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2584 /* Ensure that the pragma is not parsed again. */
2585 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2588 /* Require pragma end of line, resyncing with it as necessary. The
2589 arguments are as for cp_parser_skip_to_pragma_eol. */
2592 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2594 parser
->lexer
->in_pragma
= false;
2595 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2596 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2599 /* This is a simple wrapper around make_typename_type. When the id is
2600 an unresolved identifier node, we can provide a superior diagnostic
2601 using cp_parser_diagnose_invalid_type_name. */
2604 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2607 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2609 result
= make_typename_type (scope
, id
, typename_type
,
2610 /*complain=*/tf_none
);
2611 if (result
== error_mark_node
)
2612 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2615 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2619 /* Create a new C++ parser. */
2622 cp_parser_new (void)
2628 /* cp_lexer_new_main is called before calling ggc_alloc because
2629 cp_lexer_new_main might load a PCH file. */
2630 lexer
= cp_lexer_new_main ();
2632 /* Initialize the binops_by_token so that we can get the tree
2633 directly from the token. */
2634 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2635 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2637 parser
= GGC_CNEW (cp_parser
);
2638 parser
->lexer
= lexer
;
2639 parser
->context
= cp_parser_context_new (NULL
);
2641 /* For now, we always accept GNU extensions. */
2642 parser
->allow_gnu_extensions_p
= 1;
2644 /* The `>' token is a greater-than operator, not the end of a
2646 parser
->greater_than_is_operator_p
= true;
2648 parser
->default_arg_ok_p
= true;
2650 /* We are not parsing a constant-expression. */
2651 parser
->integral_constant_expression_p
= false;
2652 parser
->allow_non_integral_constant_expression_p
= false;
2653 parser
->non_integral_constant_expression_p
= false;
2655 /* Local variable names are not forbidden. */
2656 parser
->local_variables_forbidden_p
= false;
2658 /* We are not processing an `extern "C"' declaration. */
2659 parser
->in_unbraced_linkage_specification_p
= false;
2661 /* We are not processing a declarator. */
2662 parser
->in_declarator_p
= false;
2664 /* We are not processing a template-argument-list. */
2665 parser
->in_template_argument_list_p
= false;
2667 /* We are not in an iteration statement. */
2668 parser
->in_statement
= 0;
2670 /* We are not in a switch statement. */
2671 parser
->in_switch_statement_p
= false;
2673 /* We are not parsing a type-id inside an expression. */
2674 parser
->in_type_id_in_expr_p
= false;
2676 /* Declarations aren't implicitly extern "C". */
2677 parser
->implicit_extern_c
= false;
2679 /* String literals should be translated to the execution character set. */
2680 parser
->translate_strings_p
= true;
2682 /* We are not parsing a function body. */
2683 parser
->in_function_body
= false;
2685 /* The unparsed function queue is empty. */
2686 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2688 /* There are no classes being defined. */
2689 parser
->num_classes_being_defined
= 0;
2691 /* No template parameters apply. */
2692 parser
->num_template_parameter_lists
= 0;
2697 /* Create a cp_lexer structure which will emit the tokens in CACHE
2698 and push it onto the parser's lexer stack. This is used for delayed
2699 parsing of in-class method bodies and default arguments, and should
2700 not be confused with tentative parsing. */
2702 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2704 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2705 lexer
->next
= parser
->lexer
;
2706 parser
->lexer
= lexer
;
2708 /* Move the current source position to that of the first token in the
2710 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2713 /* Pop the top lexer off the parser stack. This is never used for the
2714 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2716 cp_parser_pop_lexer (cp_parser
*parser
)
2718 cp_lexer
*lexer
= parser
->lexer
;
2719 parser
->lexer
= lexer
->next
;
2720 cp_lexer_destroy (lexer
);
2722 /* Put the current source position back where it was before this
2723 lexer was pushed. */
2724 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2727 /* Lexical conventions [gram.lex] */
2729 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2733 cp_parser_identifier (cp_parser
* parser
)
2737 /* Look for the identifier. */
2738 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2739 /* Return the value. */
2740 return token
? token
->u
.value
: error_mark_node
;
2743 /* Parse a sequence of adjacent string constants. Returns a
2744 TREE_STRING representing the combined, nul-terminated string
2745 constant. If TRANSLATE is true, translate the string to the
2746 execution character set. If WIDE_OK is true, a wide string is
2749 C++98 [lex.string] says that if a narrow string literal token is
2750 adjacent to a wide string literal token, the behavior is undefined.
2751 However, C99 6.4.5p4 says that this results in a wide string literal.
2752 We follow C99 here, for consistency with the C front end.
2754 This code is largely lifted from lex_string() in c-lex.c.
2756 FUTURE: ObjC++ will need to handle @-strings here. */
2758 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2763 struct obstack str_ob
;
2764 cpp_string str
, istr
, *strs
;
2767 tok
= cp_lexer_peek_token (parser
->lexer
);
2768 if (!cp_parser_is_string_literal (tok
))
2770 cp_parser_error (parser
, "expected string-literal");
2771 return error_mark_node
;
2774 /* Try to avoid the overhead of creating and destroying an obstack
2775 for the common case of just one string. */
2776 if (!cp_parser_is_string_literal
2777 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2779 cp_lexer_consume_token (parser
->lexer
);
2781 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2782 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2784 if (tok
->type
== CPP_WSTRING
)
2791 gcc_obstack_init (&str_ob
);
2796 cp_lexer_consume_token (parser
->lexer
);
2798 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2799 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2800 if (tok
->type
== CPP_WSTRING
)
2803 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2805 tok
= cp_lexer_peek_token (parser
->lexer
);
2807 while (cp_parser_is_string_literal (tok
));
2809 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2812 if (wide
&& !wide_ok
)
2814 cp_parser_error (parser
, "a wide string is invalid in this context");
2818 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2819 (parse_in
, strs
, count
, &istr
, wide
))
2821 value
= build_string (istr
.len
, (char *)istr
.text
);
2822 free ((void *)istr
.text
);
2824 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2825 value
= fix_string_type (value
);
2828 /* cpp_interpret_string has issued an error. */
2829 value
= error_mark_node
;
2832 obstack_free (&str_ob
, 0);
2838 /* Basic concepts [gram.basic] */
2840 /* Parse a translation-unit.
2843 declaration-seq [opt]
2845 Returns TRUE if all went well. */
2848 cp_parser_translation_unit (cp_parser
* parser
)
2850 /* The address of the first non-permanent object on the declarator
2852 static void *declarator_obstack_base
;
2856 /* Create the declarator obstack, if necessary. */
2857 if (!cp_error_declarator
)
2859 gcc_obstack_init (&declarator_obstack
);
2860 /* Create the error declarator. */
2861 cp_error_declarator
= make_declarator (cdk_error
);
2862 /* Create the empty parameter list. */
2863 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2864 /* Remember where the base of the declarator obstack lies. */
2865 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2868 cp_parser_declaration_seq_opt (parser
);
2870 /* If there are no tokens left then all went well. */
2871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2873 /* Get rid of the token array; we don't need it any more. */
2874 cp_lexer_destroy (parser
->lexer
);
2875 parser
->lexer
= NULL
;
2877 /* This file might have been a context that's implicitly extern
2878 "C". If so, pop the lang context. (Only relevant for PCH.) */
2879 if (parser
->implicit_extern_c
)
2881 pop_lang_context ();
2882 parser
->implicit_extern_c
= false;
2886 finish_translation_unit ();
2892 cp_parser_error (parser
, "expected declaration");
2896 /* Make sure the declarator obstack was fully cleaned up. */
2897 gcc_assert (obstack_next_free (&declarator_obstack
)
2898 == declarator_obstack_base
);
2900 /* All went well. */
2904 /* Expressions [gram.expr] */
2906 /* Parse a primary-expression.
2917 ( compound-statement )
2918 __builtin_va_arg ( assignment-expression , type-id )
2919 __builtin_offsetof ( type-id , offsetof-expression )
2921 Objective-C++ Extension:
2929 ADDRESS_P is true iff this expression was immediately preceded by
2930 "&" and therefore might denote a pointer-to-member. CAST_P is true
2931 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2932 true iff this expression is a template argument.
2934 Returns a representation of the expression. Upon return, *IDK
2935 indicates what kind of id-expression (if any) was present. */
2938 cp_parser_primary_expression (cp_parser
*parser
,
2941 bool template_arg_p
,
2946 /* Assume the primary expression is not an id-expression. */
2947 *idk
= CP_ID_KIND_NONE
;
2949 /* Peek at the next token. */
2950 token
= cp_lexer_peek_token (parser
->lexer
);
2951 switch (token
->type
)
2962 token
= cp_lexer_consume_token (parser
->lexer
);
2963 /* Floating-point literals are only allowed in an integral
2964 constant expression if they are cast to an integral or
2965 enumeration type. */
2966 if (TREE_CODE (token
->u
.value
) == REAL_CST
2967 && parser
->integral_constant_expression_p
2970 /* CAST_P will be set even in invalid code like "int(2.7 +
2971 ...)". Therefore, we have to check that the next token
2972 is sure to end the cast. */
2975 cp_token
*next_token
;
2977 next_token
= cp_lexer_peek_token (parser
->lexer
);
2978 if (/* The comma at the end of an
2979 enumerator-definition. */
2980 next_token
->type
!= CPP_COMMA
2981 /* The curly brace at the end of an enum-specifier. */
2982 && next_token
->type
!= CPP_CLOSE_BRACE
2983 /* The end of a statement. */
2984 && next_token
->type
!= CPP_SEMICOLON
2985 /* The end of the cast-expression. */
2986 && next_token
->type
!= CPP_CLOSE_PAREN
2987 /* The end of an array bound. */
2988 && next_token
->type
!= CPP_CLOSE_SQUARE
2989 /* The closing ">" in a template-argument-list. */
2990 && (next_token
->type
!= CPP_GREATER
2991 || parser
->greater_than_is_operator_p
))
2995 /* If we are within a cast, then the constraint that the
2996 cast is to an integral or enumeration type will be
2997 checked at that point. If we are not within a cast, then
2998 this code is invalid. */
3000 cp_parser_non_integral_constant_expression
3001 (parser
, "floating-point literal");
3003 return token
->u
.value
;
3007 /* ??? Should wide strings be allowed when parser->translate_strings_p
3008 is false (i.e. in attributes)? If not, we can kill the third
3009 argument to cp_parser_string_literal. */
3010 return cp_parser_string_literal (parser
,
3011 parser
->translate_strings_p
,
3014 case CPP_OPEN_PAREN
:
3017 bool saved_greater_than_is_operator_p
;
3019 /* Consume the `('. */
3020 cp_lexer_consume_token (parser
->lexer
);
3021 /* Within a parenthesized expression, a `>' token is always
3022 the greater-than operator. */
3023 saved_greater_than_is_operator_p
3024 = parser
->greater_than_is_operator_p
;
3025 parser
->greater_than_is_operator_p
= true;
3026 /* If we see `( { ' then we are looking at the beginning of
3027 a GNU statement-expression. */
3028 if (cp_parser_allow_gnu_extensions_p (parser
)
3029 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3031 /* Statement-expressions are not allowed by the standard. */
3033 pedwarn ("ISO C++ forbids braced-groups within expressions");
3035 /* And they're not allowed outside of a function-body; you
3036 cannot, for example, write:
3038 int i = ({ int j = 3; j + 1; });
3040 at class or namespace scope. */
3041 if (!parser
->in_function_body
)
3043 error ("statement-expressions are allowed only inside functions");
3044 cp_parser_skip_to_end_of_block_or_statement (parser
);
3045 expr
= error_mark_node
;
3049 /* Start the statement-expression. */
3050 expr
= begin_stmt_expr ();
3051 /* Parse the compound-statement. */
3052 cp_parser_compound_statement (parser
, expr
, false);
3054 expr
= finish_stmt_expr (expr
, false);
3059 /* Parse the parenthesized expression. */
3060 expr
= cp_parser_expression (parser
, cast_p
);
3061 /* Let the front end know that this expression was
3062 enclosed in parentheses. This matters in case, for
3063 example, the expression is of the form `A::B', since
3064 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3066 finish_parenthesized_expr (expr
);
3068 /* The `>' token might be the end of a template-id or
3069 template-parameter-list now. */
3070 parser
->greater_than_is_operator_p
3071 = saved_greater_than_is_operator_p
;
3072 /* Consume the `)'. */
3073 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3074 cp_parser_skip_to_end_of_statement (parser
);
3080 switch (token
->keyword
)
3082 /* These two are the boolean literals. */
3084 cp_lexer_consume_token (parser
->lexer
);
3085 return boolean_true_node
;
3087 cp_lexer_consume_token (parser
->lexer
);
3088 return boolean_false_node
;
3090 /* The `__null' literal. */
3092 cp_lexer_consume_token (parser
->lexer
);
3095 /* Recognize the `this' keyword. */
3097 cp_lexer_consume_token (parser
->lexer
);
3098 if (parser
->local_variables_forbidden_p
)
3100 error ("%<this%> may not be used in this context");
3101 return error_mark_node
;
3103 /* Pointers cannot appear in constant-expressions. */
3104 if (cp_parser_non_integral_constant_expression (parser
,
3106 return error_mark_node
;
3107 return finish_this_expr ();
3109 /* The `operator' keyword can be the beginning of an
3114 case RID_FUNCTION_NAME
:
3115 case RID_PRETTY_FUNCTION_NAME
:
3116 case RID_C99_FUNCTION_NAME
:
3117 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3118 __func__ are the names of variables -- but they are
3119 treated specially. Therefore, they are handled here,
3120 rather than relying on the generic id-expression logic
3121 below. Grammatically, these names are id-expressions.
3123 Consume the token. */
3124 token
= cp_lexer_consume_token (parser
->lexer
);
3125 /* Look up the name. */
3126 return finish_fname (token
->u
.value
);
3133 /* The `__builtin_va_arg' construct is used to handle
3134 `va_arg'. Consume the `__builtin_va_arg' token. */
3135 cp_lexer_consume_token (parser
->lexer
);
3136 /* Look for the opening `('. */
3137 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3138 /* Now, parse the assignment-expression. */
3139 expression
= cp_parser_assignment_expression (parser
,
3141 /* Look for the `,'. */
3142 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3143 /* Parse the type-id. */
3144 type
= cp_parser_type_id (parser
);
3145 /* Look for the closing `)'. */
3146 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3147 /* Using `va_arg' in a constant-expression is not
3149 if (cp_parser_non_integral_constant_expression (parser
,
3151 return error_mark_node
;
3152 return build_x_va_arg (expression
, type
);
3156 return cp_parser_builtin_offsetof (parser
);
3158 /* Objective-C++ expressions. */
3160 case RID_AT_PROTOCOL
:
3161 case RID_AT_SELECTOR
:
3162 return cp_parser_objc_expression (parser
);
3165 cp_parser_error (parser
, "expected primary-expression");
3166 return error_mark_node
;
3169 /* An id-expression can start with either an identifier, a
3170 `::' as the beginning of a qualified-id, or the "operator"
3174 case CPP_TEMPLATE_ID
:
3175 case CPP_NESTED_NAME_SPECIFIER
:
3179 const char *error_msg
;
3184 /* Parse the id-expression. */
3186 = cp_parser_id_expression (parser
,
3187 /*template_keyword_p=*/false,
3188 /*check_dependency_p=*/true,
3190 /*declarator_p=*/false,
3191 /*optional_p=*/false);
3192 if (id_expression
== error_mark_node
)
3193 return error_mark_node
;
3194 token
= cp_lexer_peek_token (parser
->lexer
);
3195 done
= (token
->type
!= CPP_OPEN_SQUARE
3196 && token
->type
!= CPP_OPEN_PAREN
3197 && token
->type
!= CPP_DOT
3198 && token
->type
!= CPP_DEREF
3199 && token
->type
!= CPP_PLUS_PLUS
3200 && token
->type
!= CPP_MINUS_MINUS
);
3201 /* If we have a template-id, then no further lookup is
3202 required. If the template-id was for a template-class, we
3203 will sometimes have a TYPE_DECL at this point. */
3204 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3205 || TREE_CODE (id_expression
) == TYPE_DECL
)
3206 decl
= id_expression
;
3207 /* Look up the name. */
3210 tree ambiguous_decls
;
3212 decl
= cp_parser_lookup_name (parser
, id_expression
,
3215 /*is_namespace=*/false,
3216 /*check_dependency=*/true,
3218 /* If the lookup was ambiguous, an error will already have
3220 if (ambiguous_decls
)
3221 return error_mark_node
;
3223 /* In Objective-C++, an instance variable (ivar) may be preferred
3224 to whatever cp_parser_lookup_name() found. */
3225 decl
= objc_lookup_ivar (decl
, id_expression
);
3227 /* If name lookup gives us a SCOPE_REF, then the
3228 qualifying scope was dependent. */
3229 if (TREE_CODE (decl
) == SCOPE_REF
)
3231 /* Check to see if DECL is a local variable in a context
3232 where that is forbidden. */
3233 if (parser
->local_variables_forbidden_p
3234 && local_variable_p (decl
))
3236 /* It might be that we only found DECL because we are
3237 trying to be generous with pre-ISO scoping rules.
3238 For example, consider:
3242 for (int i = 0; i < 10; ++i) {}
3243 extern void f(int j = i);
3246 Here, name look up will originally find the out
3247 of scope `i'. We need to issue a warning message,
3248 but then use the global `i'. */
3249 decl
= check_for_out_of_scope_variable (decl
);
3250 if (local_variable_p (decl
))
3252 error ("local variable %qD may not appear in this context",
3254 return error_mark_node
;
3259 decl
= (finish_id_expression
3260 (id_expression
, decl
, parser
->scope
,
3262 parser
->integral_constant_expression_p
,
3263 parser
->allow_non_integral_constant_expression_p
,
3264 &parser
->non_integral_constant_expression_p
,
3265 template_p
, done
, address_p
,
3269 cp_parser_error (parser
, error_msg
);
3273 /* Anything else is an error. */
3275 /* ...unless we have an Objective-C++ message or string literal,
3277 if (c_dialect_objc ()
3278 && (token
->type
== CPP_OPEN_SQUARE
3279 || token
->type
== CPP_OBJC_STRING
))
3280 return cp_parser_objc_expression (parser
);
3282 cp_parser_error (parser
, "expected primary-expression");
3283 return error_mark_node
;
3287 /* Parse an id-expression.
3294 :: [opt] nested-name-specifier template [opt] unqualified-id
3296 :: operator-function-id
3299 Return a representation of the unqualified portion of the
3300 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3301 a `::' or nested-name-specifier.
3303 Often, if the id-expression was a qualified-id, the caller will
3304 want to make a SCOPE_REF to represent the qualified-id. This
3305 function does not do this in order to avoid wastefully creating
3306 SCOPE_REFs when they are not required.
3308 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3311 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3312 uninstantiated templates.
3314 If *TEMPLATE_P is non-NULL, it is set to true iff the
3315 `template' keyword is used to explicitly indicate that the entity
3316 named is a template.
3318 If DECLARATOR_P is true, the id-expression is appearing as part of
3319 a declarator, rather than as part of an expression. */
3322 cp_parser_id_expression (cp_parser
*parser
,
3323 bool template_keyword_p
,
3324 bool check_dependency_p
,
3329 bool global_scope_p
;
3330 bool nested_name_specifier_p
;
3332 /* Assume the `template' keyword was not used. */
3334 *template_p
= template_keyword_p
;
3336 /* Look for the optional `::' operator. */
3338 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3340 /* Look for the optional nested-name-specifier. */
3341 nested_name_specifier_p
3342 = (cp_parser_nested_name_specifier_opt (parser
,
3343 /*typename_keyword_p=*/false,
3348 /* If there is a nested-name-specifier, then we are looking at
3349 the first qualified-id production. */
3350 if (nested_name_specifier_p
)
3353 tree saved_object_scope
;
3354 tree saved_qualifying_scope
;
3355 tree unqualified_id
;
3358 /* See if the next token is the `template' keyword. */
3360 template_p
= &is_template
;
3361 *template_p
= cp_parser_optional_template_keyword (parser
);
3362 /* Name lookup we do during the processing of the
3363 unqualified-id might obliterate SCOPE. */
3364 saved_scope
= parser
->scope
;
3365 saved_object_scope
= parser
->object_scope
;
3366 saved_qualifying_scope
= parser
->qualifying_scope
;
3367 /* Process the final unqualified-id. */
3368 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3371 /*optional_p=*/false);
3372 /* Restore the SAVED_SCOPE for our caller. */
3373 parser
->scope
= saved_scope
;
3374 parser
->object_scope
= saved_object_scope
;
3375 parser
->qualifying_scope
= saved_qualifying_scope
;
3377 return unqualified_id
;
3379 /* Otherwise, if we are in global scope, then we are looking at one
3380 of the other qualified-id productions. */
3381 else if (global_scope_p
)
3386 /* Peek at the next token. */
3387 token
= cp_lexer_peek_token (parser
->lexer
);
3389 /* If it's an identifier, and the next token is not a "<", then
3390 we can avoid the template-id case. This is an optimization
3391 for this common case. */
3392 if (token
->type
== CPP_NAME
3393 && !cp_parser_nth_token_starts_template_argument_list_p
3395 return cp_parser_identifier (parser
);
3397 cp_parser_parse_tentatively (parser
);
3398 /* Try a template-id. */
3399 id
= cp_parser_template_id (parser
,
3400 /*template_keyword_p=*/false,
3401 /*check_dependency_p=*/true,
3403 /* If that worked, we're done. */
3404 if (cp_parser_parse_definitely (parser
))
3407 /* Peek at the next token. (Changes in the token buffer may
3408 have invalidated the pointer obtained above.) */
3409 token
= cp_lexer_peek_token (parser
->lexer
);
3411 switch (token
->type
)
3414 return cp_parser_identifier (parser
);
3417 if (token
->keyword
== RID_OPERATOR
)
3418 return cp_parser_operator_function_id (parser
);
3422 cp_parser_error (parser
, "expected id-expression");
3423 return error_mark_node
;
3427 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3428 /*check_dependency_p=*/true,
3433 /* Parse an unqualified-id.
3437 operator-function-id
3438 conversion-function-id
3442 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3443 keyword, in a construct like `A::template ...'.
3445 Returns a representation of unqualified-id. For the `identifier'
3446 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3447 production a BIT_NOT_EXPR is returned; the operand of the
3448 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3449 other productions, see the documentation accompanying the
3450 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3451 names are looked up in uninstantiated templates. If DECLARATOR_P
3452 is true, the unqualified-id is appearing as part of a declarator,
3453 rather than as part of an expression. */
3456 cp_parser_unqualified_id (cp_parser
* parser
,
3457 bool template_keyword_p
,
3458 bool check_dependency_p
,
3464 /* Peek at the next token. */
3465 token
= cp_lexer_peek_token (parser
->lexer
);
3467 switch (token
->type
)
3473 /* We don't know yet whether or not this will be a
3475 cp_parser_parse_tentatively (parser
);
3476 /* Try a template-id. */
3477 id
= cp_parser_template_id (parser
, template_keyword_p
,
3480 /* If it worked, we're done. */
3481 if (cp_parser_parse_definitely (parser
))
3483 /* Otherwise, it's an ordinary identifier. */
3484 return cp_parser_identifier (parser
);
3487 case CPP_TEMPLATE_ID
:
3488 return cp_parser_template_id (parser
, template_keyword_p
,
3495 tree qualifying_scope
;
3500 /* Consume the `~' token. */
3501 cp_lexer_consume_token (parser
->lexer
);
3502 /* Parse the class-name. The standard, as written, seems to
3505 template <typename T> struct S { ~S (); };
3506 template <typename T> S<T>::~S() {}
3508 is invalid, since `~' must be followed by a class-name, but
3509 `S<T>' is dependent, and so not known to be a class.
3510 That's not right; we need to look in uninstantiated
3511 templates. A further complication arises from:
3513 template <typename T> void f(T t) {
3517 Here, it is not possible to look up `T' in the scope of `T'
3518 itself. We must look in both the current scope, and the
3519 scope of the containing complete expression.
3521 Yet another issue is:
3530 The standard does not seem to say that the `S' in `~S'
3531 should refer to the type `S' and not the data member
3534 /* DR 244 says that we look up the name after the "~" in the
3535 same scope as we looked up the qualifying name. That idea
3536 isn't fully worked out; it's more complicated than that. */
3537 scope
= parser
->scope
;
3538 object_scope
= parser
->object_scope
;
3539 qualifying_scope
= parser
->qualifying_scope
;
3541 /* Check for invalid scopes. */
3542 if (scope
== error_mark_node
)
3544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3545 cp_lexer_consume_token (parser
->lexer
);
3546 return error_mark_node
;
3548 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3550 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3551 error ("scope %qT before %<~%> is not a class-name", scope
);
3552 cp_parser_simulate_error (parser
);
3553 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3554 cp_lexer_consume_token (parser
->lexer
);
3555 return error_mark_node
;
3557 gcc_assert (!scope
|| TYPE_P (scope
));
3559 /* If the name is of the form "X::~X" it's OK. */
3560 token
= cp_lexer_peek_token (parser
->lexer
);
3562 && token
->type
== CPP_NAME
3563 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3565 && constructor_name_p (token
->u
.value
, scope
))
3567 cp_lexer_consume_token (parser
->lexer
);
3568 return build_nt (BIT_NOT_EXPR
, scope
);
3571 /* If there was an explicit qualification (S::~T), first look
3572 in the scope given by the qualification (i.e., S). */
3574 type_decl
= NULL_TREE
;
3577 cp_parser_parse_tentatively (parser
);
3578 type_decl
= cp_parser_class_name (parser
,
3579 /*typename_keyword_p=*/false,
3580 /*template_keyword_p=*/false,
3582 /*check_dependency=*/false,
3583 /*class_head_p=*/false,
3585 if (cp_parser_parse_definitely (parser
))
3588 /* In "N::S::~S", look in "N" as well. */
3589 if (!done
&& scope
&& qualifying_scope
)
3591 cp_parser_parse_tentatively (parser
);
3592 parser
->scope
= qualifying_scope
;
3593 parser
->object_scope
= NULL_TREE
;
3594 parser
->qualifying_scope
= NULL_TREE
;
3596 = cp_parser_class_name (parser
,
3597 /*typename_keyword_p=*/false,
3598 /*template_keyword_p=*/false,
3600 /*check_dependency=*/false,
3601 /*class_head_p=*/false,
3603 if (cp_parser_parse_definitely (parser
))
3606 /* In "p->S::~T", look in the scope given by "*p" as well. */
3607 else if (!done
&& object_scope
)
3609 cp_parser_parse_tentatively (parser
);
3610 parser
->scope
= object_scope
;
3611 parser
->object_scope
= NULL_TREE
;
3612 parser
->qualifying_scope
= NULL_TREE
;
3614 = cp_parser_class_name (parser
,
3615 /*typename_keyword_p=*/false,
3616 /*template_keyword_p=*/false,
3618 /*check_dependency=*/false,
3619 /*class_head_p=*/false,
3621 if (cp_parser_parse_definitely (parser
))
3624 /* Look in the surrounding context. */
3627 parser
->scope
= NULL_TREE
;
3628 parser
->object_scope
= NULL_TREE
;
3629 parser
->qualifying_scope
= NULL_TREE
;
3631 = cp_parser_class_name (parser
,
3632 /*typename_keyword_p=*/false,
3633 /*template_keyword_p=*/false,
3635 /*check_dependency=*/false,
3636 /*class_head_p=*/false,
3639 /* If an error occurred, assume that the name of the
3640 destructor is the same as the name of the qualifying
3641 class. That allows us to keep parsing after running
3642 into ill-formed destructor names. */
3643 if (type_decl
== error_mark_node
&& scope
)
3644 return build_nt (BIT_NOT_EXPR
, scope
);
3645 else if (type_decl
== error_mark_node
)
3646 return error_mark_node
;
3648 /* Check that destructor name and scope match. */
3649 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3651 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3652 error ("declaration of %<~%T%> as member of %qT",
3654 cp_parser_simulate_error (parser
);
3655 return error_mark_node
;
3660 A typedef-name that names a class shall not be used as the
3661 identifier in the declarator for a destructor declaration. */
3663 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3664 && !DECL_SELF_REFERENCE_P (type_decl
)
3665 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3666 error ("typedef-name %qD used as destructor declarator",
3669 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3673 if (token
->keyword
== RID_OPERATOR
)
3677 /* This could be a template-id, so we try that first. */
3678 cp_parser_parse_tentatively (parser
);
3679 /* Try a template-id. */
3680 id
= cp_parser_template_id (parser
, template_keyword_p
,
3681 /*check_dependency_p=*/true,
3683 /* If that worked, we're done. */
3684 if (cp_parser_parse_definitely (parser
))
3686 /* We still don't know whether we're looking at an
3687 operator-function-id or a conversion-function-id. */
3688 cp_parser_parse_tentatively (parser
);
3689 /* Try an operator-function-id. */
3690 id
= cp_parser_operator_function_id (parser
);
3691 /* If that didn't work, try a conversion-function-id. */
3692 if (!cp_parser_parse_definitely (parser
))
3693 id
= cp_parser_conversion_function_id (parser
);
3702 cp_parser_error (parser
, "expected unqualified-id");
3703 return error_mark_node
;
3707 /* Parse an (optional) nested-name-specifier.
3709 nested-name-specifier:
3710 class-or-namespace-name :: nested-name-specifier [opt]
3711 class-or-namespace-name :: template nested-name-specifier [opt]
3713 PARSER->SCOPE should be set appropriately before this function is
3714 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3715 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3718 Sets PARSER->SCOPE to the class (TYPE) or namespace
3719 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3720 it unchanged if there is no nested-name-specifier. Returns the new
3721 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3723 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3724 part of a declaration and/or decl-specifier. */
3727 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3728 bool typename_keyword_p
,
3729 bool check_dependency_p
,
3731 bool is_declaration
)
3733 bool success
= false;
3734 cp_token_position start
= 0;
3737 /* Remember where the nested-name-specifier starts. */
3738 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3740 start
= cp_lexer_token_position (parser
->lexer
, false);
3741 push_deferring_access_checks (dk_deferred
);
3748 tree saved_qualifying_scope
;
3749 bool template_keyword_p
;
3751 /* Spot cases that cannot be the beginning of a
3752 nested-name-specifier. */
3753 token
= cp_lexer_peek_token (parser
->lexer
);
3755 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3756 the already parsed nested-name-specifier. */
3757 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3759 /* Grab the nested-name-specifier and continue the loop. */
3760 cp_parser_pre_parsed_nested_name_specifier (parser
);
3761 /* If we originally encountered this nested-name-specifier
3762 with IS_DECLARATION set to false, we will not have
3763 resolved TYPENAME_TYPEs, so we must do so here. */
3765 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3767 new_scope
= resolve_typename_type (parser
->scope
,
3768 /*only_current_p=*/false);
3769 if (new_scope
!= error_mark_node
)
3770 parser
->scope
= new_scope
;
3776 /* Spot cases that cannot be the beginning of a
3777 nested-name-specifier. On the second and subsequent times
3778 through the loop, we look for the `template' keyword. */
3779 if (success
&& token
->keyword
== RID_TEMPLATE
)
3781 /* A template-id can start a nested-name-specifier. */
3782 else if (token
->type
== CPP_TEMPLATE_ID
)
3786 /* If the next token is not an identifier, then it is
3787 definitely not a class-or-namespace-name. */
3788 if (token
->type
!= CPP_NAME
)
3790 /* If the following token is neither a `<' (to begin a
3791 template-id), nor a `::', then we are not looking at a
3792 nested-name-specifier. */
3793 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3794 if (token
->type
!= CPP_SCOPE
3795 && !cp_parser_nth_token_starts_template_argument_list_p
3800 /* The nested-name-specifier is optional, so we parse
3802 cp_parser_parse_tentatively (parser
);
3804 /* Look for the optional `template' keyword, if this isn't the
3805 first time through the loop. */
3807 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3809 template_keyword_p
= false;
3811 /* Save the old scope since the name lookup we are about to do
3812 might destroy it. */
3813 old_scope
= parser
->scope
;
3814 saved_qualifying_scope
= parser
->qualifying_scope
;
3815 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3816 look up names in "X<T>::I" in order to determine that "Y" is
3817 a template. So, if we have a typename at this point, we make
3818 an effort to look through it. */
3820 && !typename_keyword_p
3822 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3823 parser
->scope
= resolve_typename_type (parser
->scope
,
3824 /*only_current_p=*/false);
3825 /* Parse the qualifying entity. */
3827 = cp_parser_class_or_namespace_name (parser
,
3833 /* Look for the `::' token. */
3834 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3836 /* If we found what we wanted, we keep going; otherwise, we're
3838 if (!cp_parser_parse_definitely (parser
))
3840 bool error_p
= false;
3842 /* Restore the OLD_SCOPE since it was valid before the
3843 failed attempt at finding the last
3844 class-or-namespace-name. */
3845 parser
->scope
= old_scope
;
3846 parser
->qualifying_scope
= saved_qualifying_scope
;
3847 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3849 /* If the next token is an identifier, and the one after
3850 that is a `::', then any valid interpretation would have
3851 found a class-or-namespace-name. */
3852 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3853 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3855 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3858 token
= cp_lexer_consume_token (parser
->lexer
);
3861 if (!token
->ambiguous_p
)
3864 tree ambiguous_decls
;
3866 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
3868 /*is_template=*/false,
3869 /*is_namespace=*/false,
3870 /*check_dependency=*/true,
3872 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3873 error ("%qD used without template parameters", decl
);
3874 else if (ambiguous_decls
)
3876 error ("reference to %qD is ambiguous",
3878 print_candidates (ambiguous_decls
);
3879 decl
= error_mark_node
;
3882 cp_parser_name_lookup_error
3883 (parser
, token
->u
.value
, decl
,
3884 "is not a class or namespace");
3886 parser
->scope
= error_mark_node
;
3888 /* Treat this as a successful nested-name-specifier
3893 If the name found is not a class-name (clause
3894 _class_) or namespace-name (_namespace.def_), the
3895 program is ill-formed. */
3898 cp_lexer_consume_token (parser
->lexer
);
3902 /* We've found one valid nested-name-specifier. */
3904 /* Name lookup always gives us a DECL. */
3905 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3906 new_scope
= TREE_TYPE (new_scope
);
3907 /* Uses of "template" must be followed by actual templates. */
3908 if (template_keyword_p
3909 && !(CLASS_TYPE_P (new_scope
)
3910 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3911 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3912 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3913 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3914 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3915 == TEMPLATE_ID_EXPR
)))
3916 pedwarn (TYPE_P (new_scope
)
3917 ? "%qT is not a template"
3918 : "%qD is not a template",
3920 /* If it is a class scope, try to complete it; we are about to
3921 be looking up names inside the class. */
3922 if (TYPE_P (new_scope
)
3923 /* Since checking types for dependency can be expensive,
3924 avoid doing it if the type is already complete. */
3925 && !COMPLETE_TYPE_P (new_scope
)
3926 /* Do not try to complete dependent types. */
3927 && !dependent_type_p (new_scope
))
3928 new_scope
= complete_type (new_scope
);
3929 /* Make sure we look in the right scope the next time through
3931 parser
->scope
= new_scope
;
3934 /* If parsing tentatively, replace the sequence of tokens that makes
3935 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3936 token. That way, should we re-parse the token stream, we will
3937 not have to repeat the effort required to do the parse, nor will
3938 we issue duplicate error messages. */
3939 if (success
&& start
)
3943 token
= cp_lexer_token_at (parser
->lexer
, start
);
3944 /* Reset the contents of the START token. */
3945 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3946 /* Retrieve any deferred checks. Do not pop this access checks yet
3947 so the memory will not be reclaimed during token replacing below. */
3948 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
3949 token
->u
.tree_check_value
->value
= parser
->scope
;
3950 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
3951 token
->u
.tree_check_value
->qualifying_scope
=
3952 parser
->qualifying_scope
;
3953 token
->keyword
= RID_MAX
;
3955 /* Purge all subsequent tokens. */
3956 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3960 pop_to_parent_deferring_access_checks ();
3962 return success
? parser
->scope
: NULL_TREE
;
3965 /* Parse a nested-name-specifier. See
3966 cp_parser_nested_name_specifier_opt for details. This function
3967 behaves identically, except that it will an issue an error if no
3968 nested-name-specifier is present. */
3971 cp_parser_nested_name_specifier (cp_parser
*parser
,
3972 bool typename_keyword_p
,
3973 bool check_dependency_p
,
3975 bool is_declaration
)
3979 /* Look for the nested-name-specifier. */
3980 scope
= cp_parser_nested_name_specifier_opt (parser
,
3985 /* If it was not present, issue an error message. */
3988 cp_parser_error (parser
, "expected nested-name-specifier");
3989 parser
->scope
= NULL_TREE
;
3995 /* Parse a class-or-namespace-name.
3997 class-or-namespace-name:
4001 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4002 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4003 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4004 TYPE_P is TRUE iff the next name should be taken as a class-name,
4005 even the same name is declared to be another entity in the same
4008 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4009 specified by the class-or-namespace-name. If neither is found the
4010 ERROR_MARK_NODE is returned. */
4013 cp_parser_class_or_namespace_name (cp_parser
*parser
,
4014 bool typename_keyword_p
,
4015 bool template_keyword_p
,
4016 bool check_dependency_p
,
4018 bool is_declaration
)
4021 tree saved_qualifying_scope
;
4022 tree saved_object_scope
;
4026 /* Before we try to parse the class-name, we must save away the
4027 current PARSER->SCOPE since cp_parser_class_name will destroy
4029 saved_scope
= parser
->scope
;
4030 saved_qualifying_scope
= parser
->qualifying_scope
;
4031 saved_object_scope
= parser
->object_scope
;
4032 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4033 there is no need to look for a namespace-name. */
4034 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
4036 cp_parser_parse_tentatively (parser
);
4037 scope
= cp_parser_class_name (parser
,
4040 type_p
? class_type
: none_type
,
4042 /*class_head_p=*/false,
4044 /* If that didn't work, try for a namespace-name. */
4045 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
4047 /* Restore the saved scope. */
4048 parser
->scope
= saved_scope
;
4049 parser
->qualifying_scope
= saved_qualifying_scope
;
4050 parser
->object_scope
= saved_object_scope
;
4051 /* If we are not looking at an identifier followed by the scope
4052 resolution operator, then this is not part of a
4053 nested-name-specifier. (Note that this function is only used
4054 to parse the components of a nested-name-specifier.) */
4055 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4056 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4057 return error_mark_node
;
4058 scope
= cp_parser_namespace_name (parser
);
4064 /* Parse a postfix-expression.
4068 postfix-expression [ expression ]
4069 postfix-expression ( expression-list [opt] )
4070 simple-type-specifier ( expression-list [opt] )
4071 typename :: [opt] nested-name-specifier identifier
4072 ( expression-list [opt] )
4073 typename :: [opt] nested-name-specifier template [opt] template-id
4074 ( expression-list [opt] )
4075 postfix-expression . template [opt] id-expression
4076 postfix-expression -> template [opt] id-expression
4077 postfix-expression . pseudo-destructor-name
4078 postfix-expression -> pseudo-destructor-name
4079 postfix-expression ++
4080 postfix-expression --
4081 dynamic_cast < type-id > ( expression )
4082 static_cast < type-id > ( expression )
4083 reinterpret_cast < type-id > ( expression )
4084 const_cast < type-id > ( expression )
4085 typeid ( expression )
4091 ( type-id ) { initializer-list , [opt] }
4093 This extension is a GNU version of the C99 compound-literal
4094 construct. (The C99 grammar uses `type-name' instead of `type-id',
4095 but they are essentially the same concept.)
4097 If ADDRESS_P is true, the postfix expression is the operand of the
4098 `&' operator. CAST_P is true if this expression is the target of a
4101 Returns a representation of the expression. */
4104 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4108 cp_id_kind idk
= CP_ID_KIND_NONE
;
4109 tree postfix_expression
= NULL_TREE
;
4111 /* Peek at the next token. */
4112 token
= cp_lexer_peek_token (parser
->lexer
);
4113 /* Some of the productions are determined by keywords. */
4114 keyword
= token
->keyword
;
4124 const char *saved_message
;
4126 /* All of these can be handled in the same way from the point
4127 of view of parsing. Begin by consuming the token
4128 identifying the cast. */
4129 cp_lexer_consume_token (parser
->lexer
);
4131 /* New types cannot be defined in the cast. */
4132 saved_message
= parser
->type_definition_forbidden_message
;
4133 parser
->type_definition_forbidden_message
4134 = "types may not be defined in casts";
4136 /* Look for the opening `<'. */
4137 cp_parser_require (parser
, CPP_LESS
, "`<'");
4138 /* Parse the type to which we are casting. */
4139 type
= cp_parser_type_id (parser
);
4140 /* Look for the closing `>'. */
4141 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4142 /* Restore the old message. */
4143 parser
->type_definition_forbidden_message
= saved_message
;
4145 /* And the expression which is being cast. */
4146 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4147 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4148 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4150 /* Only type conversions to integral or enumeration types
4151 can be used in constant-expressions. */
4152 if (!cast_valid_in_integral_constant_expression_p (type
)
4153 && (cp_parser_non_integral_constant_expression
4155 "a cast to a type other than an integral or "
4156 "enumeration type")))
4157 return error_mark_node
;
4163 = build_dynamic_cast (type
, expression
);
4167 = build_static_cast (type
, expression
);
4171 = build_reinterpret_cast (type
, expression
);
4175 = build_const_cast (type
, expression
);
4186 const char *saved_message
;
4187 bool saved_in_type_id_in_expr_p
;
4189 /* Consume the `typeid' token. */
4190 cp_lexer_consume_token (parser
->lexer
);
4191 /* Look for the `(' token. */
4192 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4193 /* Types cannot be defined in a `typeid' expression. */
4194 saved_message
= parser
->type_definition_forbidden_message
;
4195 parser
->type_definition_forbidden_message
4196 = "types may not be defined in a `typeid\' expression";
4197 /* We can't be sure yet whether we're looking at a type-id or an
4199 cp_parser_parse_tentatively (parser
);
4200 /* Try a type-id first. */
4201 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4202 parser
->in_type_id_in_expr_p
= true;
4203 type
= cp_parser_type_id (parser
);
4204 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4205 /* Look for the `)' token. Otherwise, we can't be sure that
4206 we're not looking at an expression: consider `typeid (int
4207 (3))', for example. */
4208 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4209 /* If all went well, simply lookup the type-id. */
4210 if (cp_parser_parse_definitely (parser
))
4211 postfix_expression
= get_typeid (type
);
4212 /* Otherwise, fall back to the expression variant. */
4217 /* Look for an expression. */
4218 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4219 /* Compute its typeid. */
4220 postfix_expression
= build_typeid (expression
);
4221 /* Look for the `)' token. */
4222 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4224 /* Restore the saved message. */
4225 parser
->type_definition_forbidden_message
= saved_message
;
4226 /* `typeid' may not appear in an integral constant expression. */
4227 if (cp_parser_non_integral_constant_expression(parser
,
4228 "`typeid' operator"))
4229 return error_mark_node
;
4236 /* The syntax permitted here is the same permitted for an
4237 elaborated-type-specifier. */
4238 type
= cp_parser_elaborated_type_specifier (parser
,
4239 /*is_friend=*/false,
4240 /*is_declaration=*/false);
4241 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4249 /* If the next thing is a simple-type-specifier, we may be
4250 looking at a functional cast. We could also be looking at
4251 an id-expression. So, we try the functional cast, and if
4252 that doesn't work we fall back to the primary-expression. */
4253 cp_parser_parse_tentatively (parser
);
4254 /* Look for the simple-type-specifier. */
4255 type
= cp_parser_simple_type_specifier (parser
,
4256 /*decl_specs=*/NULL
,
4257 CP_PARSER_FLAGS_NONE
);
4258 /* Parse the cast itself. */
4259 if (!cp_parser_error_occurred (parser
))
4261 = cp_parser_functional_cast (parser
, type
);
4262 /* If that worked, we're done. */
4263 if (cp_parser_parse_definitely (parser
))
4266 /* If the functional-cast didn't work out, try a
4267 compound-literal. */
4268 if (cp_parser_allow_gnu_extensions_p (parser
)
4269 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4271 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4272 bool saved_in_type_id_in_expr_p
;
4274 cp_parser_parse_tentatively (parser
);
4275 /* Consume the `('. */
4276 cp_lexer_consume_token (parser
->lexer
);
4277 /* Parse the type. */
4278 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4279 parser
->in_type_id_in_expr_p
= true;
4280 type
= cp_parser_type_id (parser
);
4281 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4282 /* Look for the `)'. */
4283 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4284 /* Look for the `{'. */
4285 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4286 /* If things aren't going well, there's no need to
4288 if (!cp_parser_error_occurred (parser
))
4290 bool non_constant_p
;
4291 /* Parse the initializer-list. */
4293 = cp_parser_initializer_list (parser
, &non_constant_p
);
4294 /* Allow a trailing `,'. */
4295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4296 cp_lexer_consume_token (parser
->lexer
);
4297 /* Look for the final `}'. */
4298 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4300 /* If that worked, we're definitely looking at a
4301 compound-literal expression. */
4302 if (cp_parser_parse_definitely (parser
))
4304 /* Warn the user that a compound literal is not
4305 allowed in standard C++. */
4307 pedwarn ("ISO C++ forbids compound-literals");
4308 /* Form the representation of the compound-literal. */
4310 = finish_compound_literal (type
, initializer_list
);
4315 /* It must be a primary-expression. */
4317 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4318 /*template_arg_p=*/false,
4324 /* Keep looping until the postfix-expression is complete. */
4327 if (idk
== CP_ID_KIND_UNQUALIFIED
4328 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4329 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4330 /* It is not a Koenig lookup function call. */
4332 = unqualified_name_lookup_error (postfix_expression
);
4334 /* Peek at the next token. */
4335 token
= cp_lexer_peek_token (parser
->lexer
);
4337 switch (token
->type
)
4339 case CPP_OPEN_SQUARE
:
4341 = cp_parser_postfix_open_square_expression (parser
,
4344 idk
= CP_ID_KIND_NONE
;
4347 case CPP_OPEN_PAREN
:
4348 /* postfix-expression ( expression-list [opt] ) */
4351 bool is_builtin_constant_p
;
4352 bool saved_integral_constant_expression_p
= false;
4353 bool saved_non_integral_constant_expression_p
= false;
4356 is_builtin_constant_p
4357 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4358 if (is_builtin_constant_p
)
4360 /* The whole point of __builtin_constant_p is to allow
4361 non-constant expressions to appear as arguments. */
4362 saved_integral_constant_expression_p
4363 = parser
->integral_constant_expression_p
;
4364 saved_non_integral_constant_expression_p
4365 = parser
->non_integral_constant_expression_p
;
4366 parser
->integral_constant_expression_p
= false;
4368 args
= (cp_parser_parenthesized_expression_list
4369 (parser
, /*is_attribute_list=*/false,
4371 /*non_constant_p=*/NULL
));
4372 if (is_builtin_constant_p
)
4374 parser
->integral_constant_expression_p
4375 = saved_integral_constant_expression_p
;
4376 parser
->non_integral_constant_expression_p
4377 = saved_non_integral_constant_expression_p
;
4380 if (args
== error_mark_node
)
4382 postfix_expression
= error_mark_node
;
4386 /* Function calls are not permitted in
4387 constant-expressions. */
4388 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4389 && cp_parser_non_integral_constant_expression (parser
,
4392 postfix_expression
= error_mark_node
;
4397 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4399 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4405 = perform_koenig_lookup (postfix_expression
, args
);
4409 = unqualified_fn_lookup_error (postfix_expression
);
4411 /* We do not perform argument-dependent lookup if
4412 normal lookup finds a non-function, in accordance
4413 with the expected resolution of DR 218. */
4414 else if (args
&& is_overloaded_fn (postfix_expression
))
4416 tree fn
= get_first_fn (postfix_expression
);
4418 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4419 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4421 /* Only do argument dependent lookup if regular
4422 lookup does not find a set of member functions.
4423 [basic.lookup.koenig]/2a */
4424 if (!DECL_FUNCTION_MEMBER_P (fn
))
4428 = perform_koenig_lookup (postfix_expression
, args
);
4433 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4435 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4436 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4438 if (processing_template_decl
4439 && (type_dependent_expression_p (instance
)
4440 || (!BASELINK_P (fn
)
4441 && TREE_CODE (fn
) != FIELD_DECL
)
4442 || type_dependent_expression_p (fn
)
4443 || any_type_dependent_arguments_p (args
)))
4446 = build_min_nt (CALL_EXPR
, postfix_expression
,
4451 if (BASELINK_P (fn
))
4453 = (build_new_method_call
4454 (instance
, fn
, args
, NULL_TREE
,
4455 (idk
== CP_ID_KIND_QUALIFIED
4456 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4460 = finish_call_expr (postfix_expression
, args
,
4461 /*disallow_virtual=*/false,
4462 /*koenig_p=*/false);
4464 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4465 || TREE_CODE (postfix_expression
) == MEMBER_REF
4466 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4467 postfix_expression
= (build_offset_ref_call_from_tree
4468 (postfix_expression
, args
));
4469 else if (idk
== CP_ID_KIND_QUALIFIED
)
4470 /* A call to a static class member, or a namespace-scope
4473 = finish_call_expr (postfix_expression
, args
,
4474 /*disallow_virtual=*/true,
4477 /* All other function calls. */
4479 = finish_call_expr (postfix_expression
, args
,
4480 /*disallow_virtual=*/false,
4483 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4484 idk
= CP_ID_KIND_NONE
;
4490 /* postfix-expression . template [opt] id-expression
4491 postfix-expression . pseudo-destructor-name
4492 postfix-expression -> template [opt] id-expression
4493 postfix-expression -> pseudo-destructor-name */
4495 /* Consume the `.' or `->' operator. */
4496 cp_lexer_consume_token (parser
->lexer
);
4499 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4505 /* postfix-expression ++ */
4506 /* Consume the `++' token. */
4507 cp_lexer_consume_token (parser
->lexer
);
4508 /* Generate a representation for the complete expression. */
4510 = finish_increment_expr (postfix_expression
,
4511 POSTINCREMENT_EXPR
);
4512 /* Increments may not appear in constant-expressions. */
4513 if (cp_parser_non_integral_constant_expression (parser
,
4515 postfix_expression
= error_mark_node
;
4516 idk
= CP_ID_KIND_NONE
;
4519 case CPP_MINUS_MINUS
:
4520 /* postfix-expression -- */
4521 /* Consume the `--' token. */
4522 cp_lexer_consume_token (parser
->lexer
);
4523 /* Generate a representation for the complete expression. */
4525 = finish_increment_expr (postfix_expression
,
4526 POSTDECREMENT_EXPR
);
4527 /* Decrements may not appear in constant-expressions. */
4528 if (cp_parser_non_integral_constant_expression (parser
,
4530 postfix_expression
= error_mark_node
;
4531 idk
= CP_ID_KIND_NONE
;
4535 return postfix_expression
;
4539 /* We should never get here. */
4541 return error_mark_node
;
4544 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4545 by cp_parser_builtin_offsetof. We're looking for
4547 postfix-expression [ expression ]
4549 FOR_OFFSETOF is set if we're being called in that context, which
4550 changes how we deal with integer constant expressions. */
4553 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4554 tree postfix_expression
,
4559 /* Consume the `[' token. */
4560 cp_lexer_consume_token (parser
->lexer
);
4562 /* Parse the index expression. */
4563 /* ??? For offsetof, there is a question of what to allow here. If
4564 offsetof is not being used in an integral constant expression context,
4565 then we *could* get the right answer by computing the value at runtime.
4566 If we are in an integral constant expression context, then we might
4567 could accept any constant expression; hard to say without analysis.
4568 Rather than open the barn door too wide right away, allow only integer
4569 constant expressions here. */
4571 index
= cp_parser_constant_expression (parser
, false, NULL
);
4573 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4575 /* Look for the closing `]'. */
4576 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4578 /* Build the ARRAY_REF. */
4579 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4581 /* When not doing offsetof, array references are not permitted in
4582 constant-expressions. */
4584 && (cp_parser_non_integral_constant_expression
4585 (parser
, "an array reference")))
4586 postfix_expression
= error_mark_node
;
4588 return postfix_expression
;
4591 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4592 by cp_parser_builtin_offsetof. We're looking for
4594 postfix-expression . template [opt] id-expression
4595 postfix-expression . pseudo-destructor-name
4596 postfix-expression -> template [opt] id-expression
4597 postfix-expression -> pseudo-destructor-name
4599 FOR_OFFSETOF is set if we're being called in that context. That sorta
4600 limits what of the above we'll actually accept, but nevermind.
4601 TOKEN_TYPE is the "." or "->" token, which will already have been
4602 removed from the stream. */
4605 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4606 enum cpp_ttype token_type
,
4607 tree postfix_expression
,
4608 bool for_offsetof
, cp_id_kind
*idk
)
4612 bool pseudo_destructor_p
;
4613 tree scope
= NULL_TREE
;
4615 /* If this is a `->' operator, dereference the pointer. */
4616 if (token_type
== CPP_DEREF
)
4617 postfix_expression
= build_x_arrow (postfix_expression
);
4618 /* Check to see whether or not the expression is type-dependent. */
4619 dependent_p
= type_dependent_expression_p (postfix_expression
);
4620 /* The identifier following the `->' or `.' is not qualified. */
4621 parser
->scope
= NULL_TREE
;
4622 parser
->qualifying_scope
= NULL_TREE
;
4623 parser
->object_scope
= NULL_TREE
;
4624 *idk
= CP_ID_KIND_NONE
;
4625 /* Enter the scope corresponding to the type of the object
4626 given by the POSTFIX_EXPRESSION. */
4627 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4629 scope
= TREE_TYPE (postfix_expression
);
4630 /* According to the standard, no expression should ever have
4631 reference type. Unfortunately, we do not currently match
4632 the standard in this respect in that our internal representation
4633 of an expression may have reference type even when the standard
4634 says it does not. Therefore, we have to manually obtain the
4635 underlying type here. */
4636 scope
= non_reference (scope
);
4637 /* The type of the POSTFIX_EXPRESSION must be complete. */
4638 if (scope
== unknown_type_node
)
4640 error ("%qE does not have class type", postfix_expression
);
4644 scope
= complete_type_or_else (scope
, NULL_TREE
);
4645 /* Let the name lookup machinery know that we are processing a
4646 class member access expression. */
4647 parser
->context
->object_type
= scope
;
4648 /* If something went wrong, we want to be able to discern that case,
4649 as opposed to the case where there was no SCOPE due to the type
4650 of expression being dependent. */
4652 scope
= error_mark_node
;
4653 /* If the SCOPE was erroneous, make the various semantic analysis
4654 functions exit quickly -- and without issuing additional error
4656 if (scope
== error_mark_node
)
4657 postfix_expression
= error_mark_node
;
4660 /* Assume this expression is not a pseudo-destructor access. */
4661 pseudo_destructor_p
= false;
4663 /* If the SCOPE is a scalar type, then, if this is a valid program,
4664 we must be looking at a pseudo-destructor-name. */
4665 if (scope
&& SCALAR_TYPE_P (scope
))
4670 cp_parser_parse_tentatively (parser
);
4671 /* Parse the pseudo-destructor-name. */
4673 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4674 if (cp_parser_parse_definitely (parser
))
4676 pseudo_destructor_p
= true;
4678 = finish_pseudo_destructor_expr (postfix_expression
,
4679 s
, TREE_TYPE (type
));
4683 if (!pseudo_destructor_p
)
4685 /* If the SCOPE is not a scalar type, we are looking at an
4686 ordinary class member access expression, rather than a
4687 pseudo-destructor-name. */
4689 /* Parse the id-expression. */
4690 name
= (cp_parser_id_expression
4692 cp_parser_optional_template_keyword (parser
),
4693 /*check_dependency_p=*/true,
4695 /*declarator_p=*/false,
4696 /*optional_p=*/false));
4697 /* In general, build a SCOPE_REF if the member name is qualified.
4698 However, if the name was not dependent and has already been
4699 resolved; there is no need to build the SCOPE_REF. For example;
4701 struct X { void f(); };
4702 template <typename T> void f(T* t) { t->X::f(); }
4704 Even though "t" is dependent, "X::f" is not and has been resolved
4705 to a BASELINK; there is no need to include scope information. */
4707 /* But we do need to remember that there was an explicit scope for
4708 virtual function calls. */
4710 *idk
= CP_ID_KIND_QUALIFIED
;
4712 /* If the name is a template-id that names a type, we will get a
4713 TYPE_DECL here. That is invalid code. */
4714 if (TREE_CODE (name
) == TYPE_DECL
)
4716 error ("invalid use of %qD", name
);
4717 postfix_expression
= error_mark_node
;
4721 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4723 name
= build_qualified_name (/*type=*/NULL_TREE
,
4727 parser
->scope
= NULL_TREE
;
4728 parser
->qualifying_scope
= NULL_TREE
;
4729 parser
->object_scope
= NULL_TREE
;
4731 if (scope
&& name
&& BASELINK_P (name
))
4732 adjust_result_of_qualified_name_lookup
4733 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4735 = finish_class_member_access_expr (postfix_expression
, name
,
4740 /* We no longer need to look up names in the scope of the object on
4741 the left-hand side of the `.' or `->' operator. */
4742 parser
->context
->object_type
= NULL_TREE
;
4744 /* Outside of offsetof, these operators may not appear in
4745 constant-expressions. */
4747 && (cp_parser_non_integral_constant_expression
4748 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4749 postfix_expression
= error_mark_node
;
4751 return postfix_expression
;
4754 /* Parse a parenthesized expression-list.
4757 assignment-expression
4758 expression-list, assignment-expression
4763 identifier, expression-list
4765 CAST_P is true if this expression is the target of a cast.
4767 Returns a TREE_LIST. The TREE_VALUE of each node is a
4768 representation of an assignment-expression. Note that a TREE_LIST
4769 is returned even if there is only a single expression in the list.
4770 error_mark_node is returned if the ( and or ) are
4771 missing. NULL_TREE is returned on no expressions. The parentheses
4772 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4773 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4774 indicates whether or not all of the expressions in the list were
4778 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4779 bool is_attribute_list
,
4781 bool *non_constant_p
)
4783 tree expression_list
= NULL_TREE
;
4784 bool fold_expr_p
= is_attribute_list
;
4785 tree identifier
= NULL_TREE
;
4787 /* Assume all the expressions will be constant. */
4789 *non_constant_p
= false;
4791 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4792 return error_mark_node
;
4794 /* Consume expressions until there are no more. */
4795 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4800 /* At the beginning of attribute lists, check to see if the
4801 next token is an identifier. */
4802 if (is_attribute_list
4803 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4807 /* Consume the identifier. */
4808 token
= cp_lexer_consume_token (parser
->lexer
);
4809 /* Save the identifier. */
4810 identifier
= token
->u
.value
;
4814 /* Parse the next assignment-expression. */
4817 bool expr_non_constant_p
;
4818 expr
= (cp_parser_constant_expression
4819 (parser
, /*allow_non_constant_p=*/true,
4820 &expr_non_constant_p
));
4821 if (expr_non_constant_p
)
4822 *non_constant_p
= true;
4825 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4828 expr
= fold_non_dependent_expr (expr
);
4830 /* Add it to the list. We add error_mark_node
4831 expressions to the list, so that we can still tell if
4832 the correct form for a parenthesized expression-list
4833 is found. That gives better errors. */
4834 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4836 if (expr
== error_mark_node
)
4840 /* After the first item, attribute lists look the same as
4841 expression lists. */
4842 is_attribute_list
= false;
4845 /* If the next token isn't a `,', then we are done. */
4846 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4849 /* Otherwise, consume the `,' and keep going. */
4850 cp_lexer_consume_token (parser
->lexer
);
4853 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4858 /* We try and resync to an unnested comma, as that will give the
4859 user better diagnostics. */
4860 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4861 /*recovering=*/true,
4863 /*consume_paren=*/true);
4867 return error_mark_node
;
4870 /* We built up the list in reverse order so we must reverse it now. */
4871 expression_list
= nreverse (expression_list
);
4873 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4875 return expression_list
;
4878 /* Parse a pseudo-destructor-name.
4880 pseudo-destructor-name:
4881 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4882 :: [opt] nested-name-specifier template template-id :: ~ type-name
4883 :: [opt] nested-name-specifier [opt] ~ type-name
4885 If either of the first two productions is used, sets *SCOPE to the
4886 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4887 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4888 or ERROR_MARK_NODE if the parse fails. */
4891 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4895 bool nested_name_specifier_p
;
4897 /* Assume that things will not work out. */
4898 *type
= error_mark_node
;
4900 /* Look for the optional `::' operator. */
4901 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4902 /* Look for the optional nested-name-specifier. */
4903 nested_name_specifier_p
4904 = (cp_parser_nested_name_specifier_opt (parser
,
4905 /*typename_keyword_p=*/false,
4906 /*check_dependency_p=*/true,
4908 /*is_declaration=*/true)
4910 /* Now, if we saw a nested-name-specifier, we might be doing the
4911 second production. */
4912 if (nested_name_specifier_p
4913 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4915 /* Consume the `template' keyword. */
4916 cp_lexer_consume_token (parser
->lexer
);
4917 /* Parse the template-id. */
4918 cp_parser_template_id (parser
,
4919 /*template_keyword_p=*/true,
4920 /*check_dependency_p=*/false,
4921 /*is_declaration=*/true);
4922 /* Look for the `::' token. */
4923 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4925 /* If the next token is not a `~', then there might be some
4926 additional qualification. */
4927 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4929 /* Look for the type-name. */
4930 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4932 if (*scope
== error_mark_node
)
4935 /* If we don't have ::~, then something has gone wrong. Since
4936 the only caller of this function is looking for something
4937 after `.' or `->' after a scalar type, most likely the
4938 program is trying to get a member of a non-aggregate
4940 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4941 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4943 cp_parser_error (parser
, "request for member of non-aggregate type");
4947 /* Look for the `::' token. */
4948 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4953 /* Look for the `~'. */
4954 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4955 /* Look for the type-name again. We are not responsible for
4956 checking that it matches the first type-name. */
4957 *type
= cp_parser_type_name (parser
);
4960 /* Parse a unary-expression.
4966 unary-operator cast-expression
4967 sizeof unary-expression
4975 __extension__ cast-expression
4976 __alignof__ unary-expression
4977 __alignof__ ( type-id )
4978 __real__ cast-expression
4979 __imag__ cast-expression
4982 ADDRESS_P is true iff the unary-expression is appearing as the
4983 operand of the `&' operator. CAST_P is true if this expression is
4984 the target of a cast.
4986 Returns a representation of the expression. */
4989 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4992 enum tree_code unary_operator
;
4994 /* Peek at the next token. */
4995 token
= cp_lexer_peek_token (parser
->lexer
);
4996 /* Some keywords give away the kind of expression. */
4997 if (token
->type
== CPP_KEYWORD
)
4999 enum rid keyword
= token
->keyword
;
5009 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5010 /* Consume the token. */
5011 cp_lexer_consume_token (parser
->lexer
);
5012 /* Parse the operand. */
5013 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5015 if (TYPE_P (operand
))
5016 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5018 return cxx_sizeof_or_alignof_expr (operand
, op
);
5022 return cp_parser_new_expression (parser
);
5025 return cp_parser_delete_expression (parser
);
5029 /* The saved value of the PEDANTIC flag. */
5033 /* Save away the PEDANTIC flag. */
5034 cp_parser_extension_opt (parser
, &saved_pedantic
);
5035 /* Parse the cast-expression. */
5036 expr
= cp_parser_simple_cast_expression (parser
);
5037 /* Restore the PEDANTIC flag. */
5038 pedantic
= saved_pedantic
;
5048 /* Consume the `__real__' or `__imag__' token. */
5049 cp_lexer_consume_token (parser
->lexer
);
5050 /* Parse the cast-expression. */
5051 expression
= cp_parser_simple_cast_expression (parser
);
5052 /* Create the complete representation. */
5053 return build_x_unary_op ((keyword
== RID_REALPART
5054 ? REALPART_EXPR
: IMAGPART_EXPR
),
5064 /* Look for the `:: new' and `:: delete', which also signal the
5065 beginning of a new-expression, or delete-expression,
5066 respectively. If the next token is `::', then it might be one of
5068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5072 /* See if the token after the `::' is one of the keywords in
5073 which we're interested. */
5074 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5075 /* If it's `new', we have a new-expression. */
5076 if (keyword
== RID_NEW
)
5077 return cp_parser_new_expression (parser
);
5078 /* Similarly, for `delete'. */
5079 else if (keyword
== RID_DELETE
)
5080 return cp_parser_delete_expression (parser
);
5083 /* Look for a unary operator. */
5084 unary_operator
= cp_parser_unary_operator (token
);
5085 /* The `++' and `--' operators can be handled similarly, even though
5086 they are not technically unary-operators in the grammar. */
5087 if (unary_operator
== ERROR_MARK
)
5089 if (token
->type
== CPP_PLUS_PLUS
)
5090 unary_operator
= PREINCREMENT_EXPR
;
5091 else if (token
->type
== CPP_MINUS_MINUS
)
5092 unary_operator
= PREDECREMENT_EXPR
;
5093 /* Handle the GNU address-of-label extension. */
5094 else if (cp_parser_allow_gnu_extensions_p (parser
)
5095 && token
->type
== CPP_AND_AND
)
5099 /* Consume the '&&' token. */
5100 cp_lexer_consume_token (parser
->lexer
);
5101 /* Look for the identifier. */
5102 identifier
= cp_parser_identifier (parser
);
5103 /* Create an expression representing the address. */
5104 return finish_label_address_expr (identifier
);
5107 if (unary_operator
!= ERROR_MARK
)
5109 tree cast_expression
;
5110 tree expression
= error_mark_node
;
5111 const char *non_constant_p
= NULL
;
5113 /* Consume the operator token. */
5114 token
= cp_lexer_consume_token (parser
->lexer
);
5115 /* Parse the cast-expression. */
5117 = cp_parser_cast_expression (parser
,
5118 unary_operator
== ADDR_EXPR
,
5120 /* Now, build an appropriate representation. */
5121 switch (unary_operator
)
5124 non_constant_p
= "`*'";
5125 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5129 non_constant_p
= "`&'";
5132 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5135 case PREINCREMENT_EXPR
:
5136 case PREDECREMENT_EXPR
:
5137 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5140 case UNARY_PLUS_EXPR
:
5142 case TRUTH_NOT_EXPR
:
5143 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5151 && cp_parser_non_integral_constant_expression (parser
,
5153 expression
= error_mark_node
;
5158 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5161 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5162 unary-operator, the corresponding tree code is returned. */
5164 static enum tree_code
5165 cp_parser_unary_operator (cp_token
* token
)
5167 switch (token
->type
)
5170 return INDIRECT_REF
;
5176 return UNARY_PLUS_EXPR
;
5182 return TRUTH_NOT_EXPR
;
5185 return BIT_NOT_EXPR
;
5192 /* Parse a new-expression.
5195 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5196 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5198 Returns a representation of the expression. */
5201 cp_parser_new_expression (cp_parser
* parser
)
5203 bool global_scope_p
;
5209 /* Look for the optional `::' operator. */
5211 = (cp_parser_global_scope_opt (parser
,
5212 /*current_scope_valid_p=*/false)
5214 /* Look for the `new' operator. */
5215 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5216 /* There's no easy way to tell a new-placement from the
5217 `( type-id )' construct. */
5218 cp_parser_parse_tentatively (parser
);
5219 /* Look for a new-placement. */
5220 placement
= cp_parser_new_placement (parser
);
5221 /* If that didn't work out, there's no new-placement. */
5222 if (!cp_parser_parse_definitely (parser
))
5223 placement
= NULL_TREE
;
5225 /* If the next token is a `(', then we have a parenthesized
5227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5229 /* Consume the `('. */
5230 cp_lexer_consume_token (parser
->lexer
);
5231 /* Parse the type-id. */
5232 type
= cp_parser_type_id (parser
);
5233 /* Look for the closing `)'. */
5234 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5235 /* There should not be a direct-new-declarator in this production,
5236 but GCC used to allowed this, so we check and emit a sensible error
5237 message for this case. */
5238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5240 error ("array bound forbidden after parenthesized type-id");
5241 inform ("try removing the parentheses around the type-id");
5242 cp_parser_direct_new_declarator (parser
);
5246 /* Otherwise, there must be a new-type-id. */
5248 type
= cp_parser_new_type_id (parser
, &nelts
);
5250 /* If the next token is a `(', then we have a new-initializer. */
5251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5252 initializer
= cp_parser_new_initializer (parser
);
5254 initializer
= NULL_TREE
;
5256 /* A new-expression may not appear in an integral constant
5258 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5259 return error_mark_node
;
5261 /* Create a representation of the new-expression. */
5262 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5265 /* Parse a new-placement.
5270 Returns the same representation as for an expression-list. */
5273 cp_parser_new_placement (cp_parser
* parser
)
5275 tree expression_list
;
5277 /* Parse the expression-list. */
5278 expression_list
= (cp_parser_parenthesized_expression_list
5279 (parser
, false, /*cast_p=*/false,
5280 /*non_constant_p=*/NULL
));
5282 return expression_list
;
5285 /* Parse a new-type-id.
5288 type-specifier-seq new-declarator [opt]
5290 Returns the TYPE allocated. If the new-type-id indicates an array
5291 type, *NELTS is set to the number of elements in the last array
5292 bound; the TYPE will not include the last array bound. */
5295 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5297 cp_decl_specifier_seq type_specifier_seq
;
5298 cp_declarator
*new_declarator
;
5299 cp_declarator
*declarator
;
5300 cp_declarator
*outer_declarator
;
5301 const char *saved_message
;
5304 /* The type-specifier sequence must not contain type definitions.
5305 (It cannot contain declarations of new types either, but if they
5306 are not definitions we will catch that because they are not
5308 saved_message
= parser
->type_definition_forbidden_message
;
5309 parser
->type_definition_forbidden_message
5310 = "types may not be defined in a new-type-id";
5311 /* Parse the type-specifier-seq. */
5312 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5313 &type_specifier_seq
);
5314 /* Restore the old message. */
5315 parser
->type_definition_forbidden_message
= saved_message
;
5316 /* Parse the new-declarator. */
5317 new_declarator
= cp_parser_new_declarator_opt (parser
);
5319 /* Determine the number of elements in the last array dimension, if
5322 /* Skip down to the last array dimension. */
5323 declarator
= new_declarator
;
5324 outer_declarator
= NULL
;
5325 while (declarator
&& (declarator
->kind
== cdk_pointer
5326 || declarator
->kind
== cdk_ptrmem
))
5328 outer_declarator
= declarator
;
5329 declarator
= declarator
->declarator
;
5332 && declarator
->kind
== cdk_array
5333 && declarator
->declarator
5334 && declarator
->declarator
->kind
== cdk_array
)
5336 outer_declarator
= declarator
;
5337 declarator
= declarator
->declarator
;
5340 if (declarator
&& declarator
->kind
== cdk_array
)
5342 *nelts
= declarator
->u
.array
.bounds
;
5343 if (*nelts
== error_mark_node
)
5344 *nelts
= integer_one_node
;
5346 if (outer_declarator
)
5347 outer_declarator
->declarator
= declarator
->declarator
;
5349 new_declarator
= NULL
;
5352 type
= groktypename (&type_specifier_seq
, new_declarator
);
5353 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5355 *nelts
= array_type_nelts_top (type
);
5356 type
= TREE_TYPE (type
);
5361 /* Parse an (optional) new-declarator.
5364 ptr-operator new-declarator [opt]
5365 direct-new-declarator
5367 Returns the declarator. */
5369 static cp_declarator
*
5370 cp_parser_new_declarator_opt (cp_parser
* parser
)
5372 enum tree_code code
;
5374 cp_cv_quals cv_quals
;
5376 /* We don't know if there's a ptr-operator next, or not. */
5377 cp_parser_parse_tentatively (parser
);
5378 /* Look for a ptr-operator. */
5379 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5380 /* If that worked, look for more new-declarators. */
5381 if (cp_parser_parse_definitely (parser
))
5383 cp_declarator
*declarator
;
5385 /* Parse another optional declarator. */
5386 declarator
= cp_parser_new_declarator_opt (parser
);
5388 /* Create the representation of the declarator. */
5390 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5391 else if (code
== INDIRECT_REF
)
5392 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5394 declarator
= make_reference_declarator (cv_quals
, declarator
);
5399 /* If the next token is a `[', there is a direct-new-declarator. */
5400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5401 return cp_parser_direct_new_declarator (parser
);
5406 /* Parse a direct-new-declarator.
5408 direct-new-declarator:
5410 direct-new-declarator [constant-expression]
5414 static cp_declarator
*
5415 cp_parser_direct_new_declarator (cp_parser
* parser
)
5417 cp_declarator
*declarator
= NULL
;
5423 /* Look for the opening `['. */
5424 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5425 /* The first expression is not required to be constant. */
5428 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5429 /* The standard requires that the expression have integral
5430 type. DR 74 adds enumeration types. We believe that the
5431 real intent is that these expressions be handled like the
5432 expression in a `switch' condition, which also allows
5433 classes with a single conversion to integral or
5434 enumeration type. */
5435 if (!processing_template_decl
)
5438 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5443 error ("expression in new-declarator must have integral "
5444 "or enumeration type");
5445 expression
= error_mark_node
;
5449 /* But all the other expressions must be. */
5452 = cp_parser_constant_expression (parser
,
5453 /*allow_non_constant=*/false,
5455 /* Look for the closing `]'. */
5456 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5458 /* Add this bound to the declarator. */
5459 declarator
= make_array_declarator (declarator
, expression
);
5461 /* If the next token is not a `[', then there are no more
5463 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5470 /* Parse a new-initializer.
5473 ( expression-list [opt] )
5475 Returns a representation of the expression-list. If there is no
5476 expression-list, VOID_ZERO_NODE is returned. */
5479 cp_parser_new_initializer (cp_parser
* parser
)
5481 tree expression_list
;
5483 expression_list
= (cp_parser_parenthesized_expression_list
5484 (parser
, false, /*cast_p=*/false,
5485 /*non_constant_p=*/NULL
));
5486 if (!expression_list
)
5487 expression_list
= void_zero_node
;
5489 return expression_list
;
5492 /* Parse a delete-expression.
5495 :: [opt] delete cast-expression
5496 :: [opt] delete [ ] cast-expression
5498 Returns a representation of the expression. */
5501 cp_parser_delete_expression (cp_parser
* parser
)
5503 bool global_scope_p
;
5507 /* Look for the optional `::' operator. */
5509 = (cp_parser_global_scope_opt (parser
,
5510 /*current_scope_valid_p=*/false)
5512 /* Look for the `delete' keyword. */
5513 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5514 /* See if the array syntax is in use. */
5515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5517 /* Consume the `[' token. */
5518 cp_lexer_consume_token (parser
->lexer
);
5519 /* Look for the `]' token. */
5520 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5521 /* Remember that this is the `[]' construct. */
5527 /* Parse the cast-expression. */
5528 expression
= cp_parser_simple_cast_expression (parser
);
5530 /* A delete-expression may not appear in an integral constant
5532 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5533 return error_mark_node
;
5535 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5538 /* Parse a cast-expression.
5542 ( type-id ) cast-expression
5544 ADDRESS_P is true iff the unary-expression is appearing as the
5545 operand of the `&' operator. CAST_P is true if this expression is
5546 the target of a cast.
5548 Returns a representation of the expression. */
5551 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5553 /* If it's a `(', then we might be looking at a cast. */
5554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5556 tree type
= NULL_TREE
;
5557 tree expr
= NULL_TREE
;
5558 bool compound_literal_p
;
5559 const char *saved_message
;
5561 /* There's no way to know yet whether or not this is a cast.
5562 For example, `(int (3))' is a unary-expression, while `(int)
5563 3' is a cast. So, we resort to parsing tentatively. */
5564 cp_parser_parse_tentatively (parser
);
5565 /* Types may not be defined in a cast. */
5566 saved_message
= parser
->type_definition_forbidden_message
;
5567 parser
->type_definition_forbidden_message
5568 = "types may not be defined in casts";
5569 /* Consume the `('. */
5570 cp_lexer_consume_token (parser
->lexer
);
5571 /* A very tricky bit is that `(struct S) { 3 }' is a
5572 compound-literal (which we permit in C++ as an extension).
5573 But, that construct is not a cast-expression -- it is a
5574 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5575 is legal; if the compound-literal were a cast-expression,
5576 you'd need an extra set of parentheses.) But, if we parse
5577 the type-id, and it happens to be a class-specifier, then we
5578 will commit to the parse at that point, because we cannot
5579 undo the action that is done when creating a new class. So,
5580 then we cannot back up and do a postfix-expression.
5582 Therefore, we scan ahead to the closing `)', and check to see
5583 if the token after the `)' is a `{'. If so, we are not
5584 looking at a cast-expression.
5586 Save tokens so that we can put them back. */
5587 cp_lexer_save_tokens (parser
->lexer
);
5588 /* Skip tokens until the next token is a closing parenthesis.
5589 If we find the closing `)', and the next token is a `{', then
5590 we are looking at a compound-literal. */
5592 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5593 /*consume_paren=*/true)
5594 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5595 /* Roll back the tokens we skipped. */
5596 cp_lexer_rollback_tokens (parser
->lexer
);
5597 /* If we were looking at a compound-literal, simulate an error
5598 so that the call to cp_parser_parse_definitely below will
5600 if (compound_literal_p
)
5601 cp_parser_simulate_error (parser
);
5604 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5605 parser
->in_type_id_in_expr_p
= true;
5606 /* Look for the type-id. */
5607 type
= cp_parser_type_id (parser
);
5608 /* Look for the closing `)'. */
5609 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5610 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5613 /* Restore the saved message. */
5614 parser
->type_definition_forbidden_message
= saved_message
;
5616 /* If ok so far, parse the dependent expression. We cannot be
5617 sure it is a cast. Consider `(T ())'. It is a parenthesized
5618 ctor of T, but looks like a cast to function returning T
5619 without a dependent expression. */
5620 if (!cp_parser_error_occurred (parser
))
5621 expr
= cp_parser_cast_expression (parser
,
5622 /*address_p=*/false,
5625 if (cp_parser_parse_definitely (parser
))
5627 /* Warn about old-style casts, if so requested. */
5628 if (warn_old_style_cast
5629 && !in_system_header
5630 && !VOID_TYPE_P (type
)
5631 && current_lang_name
!= lang_name_c
)
5632 warning (OPT_Wold_style_cast
, "use of old-style cast");
5634 /* Only type conversions to integral or enumeration types
5635 can be used in constant-expressions. */
5636 if (!cast_valid_in_integral_constant_expression_p (type
)
5637 && (cp_parser_non_integral_constant_expression
5639 "a cast to a type other than an integral or "
5640 "enumeration type")))
5641 return error_mark_node
;
5643 /* Perform the cast. */
5644 expr
= build_c_cast (type
, expr
);
5649 /* If we get here, then it's not a cast, so it must be a
5650 unary-expression. */
5651 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5654 /* Parse a binary expression of the general form:
5658 pm-expression .* cast-expression
5659 pm-expression ->* cast-expression
5661 multiplicative-expression:
5663 multiplicative-expression * pm-expression
5664 multiplicative-expression / pm-expression
5665 multiplicative-expression % pm-expression
5667 additive-expression:
5668 multiplicative-expression
5669 additive-expression + multiplicative-expression
5670 additive-expression - multiplicative-expression
5674 shift-expression << additive-expression
5675 shift-expression >> additive-expression
5677 relational-expression:
5679 relational-expression < shift-expression
5680 relational-expression > shift-expression
5681 relational-expression <= shift-expression
5682 relational-expression >= shift-expression
5686 relational-expression:
5687 relational-expression <? shift-expression
5688 relational-expression >? shift-expression
5690 equality-expression:
5691 relational-expression
5692 equality-expression == relational-expression
5693 equality-expression != relational-expression
5697 and-expression & equality-expression
5699 exclusive-or-expression:
5701 exclusive-or-expression ^ and-expression
5703 inclusive-or-expression:
5704 exclusive-or-expression
5705 inclusive-or-expression | exclusive-or-expression
5707 logical-and-expression:
5708 inclusive-or-expression
5709 logical-and-expression && inclusive-or-expression
5711 logical-or-expression:
5712 logical-and-expression
5713 logical-or-expression || logical-and-expression
5715 All these are implemented with a single function like:
5718 simple-cast-expression
5719 binary-expression <token> binary-expression
5721 CAST_P is true if this expression is the target of a cast.
5723 The binops_by_token map is used to get the tree codes for each <token> type.
5724 binary-expressions are associated according to a precedence table. */
5726 #define TOKEN_PRECEDENCE(token) \
5727 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5728 ? PREC_NOT_OPERATOR \
5729 : binops_by_token[token->type].prec)
5732 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5734 cp_parser_expression_stack stack
;
5735 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5738 enum tree_code tree_type
, lhs_type
, rhs_type
;
5739 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5742 /* Parse the first expression. */
5743 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5744 lhs_type
= ERROR_MARK
;
5748 /* Get an operator token. */
5749 token
= cp_lexer_peek_token (parser
->lexer
);
5751 new_prec
= TOKEN_PRECEDENCE (token
);
5753 /* Popping an entry off the stack means we completed a subexpression:
5754 - either we found a token which is not an operator (`>' where it is not
5755 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5756 will happen repeatedly;
5757 - or, we found an operator which has lower priority. This is the case
5758 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5760 if (new_prec
<= prec
)
5769 tree_type
= binops_by_token
[token
->type
].tree_type
;
5771 /* We used the operator token. */
5772 cp_lexer_consume_token (parser
->lexer
);
5774 /* Extract another operand. It may be the RHS of this expression
5775 or the LHS of a new, higher priority expression. */
5776 rhs
= cp_parser_simple_cast_expression (parser
);
5777 rhs_type
= ERROR_MARK
;
5779 /* Get another operator token. Look up its precedence to avoid
5780 building a useless (immediately popped) stack entry for common
5781 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5782 token
= cp_lexer_peek_token (parser
->lexer
);
5783 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5784 if (lookahead_prec
> new_prec
)
5786 /* ... and prepare to parse the RHS of the new, higher priority
5787 expression. Since precedence levels on the stack are
5788 monotonically increasing, we do not have to care about
5791 sp
->tree_type
= tree_type
;
5793 sp
->lhs_type
= lhs_type
;
5796 lhs_type
= rhs_type
;
5798 new_prec
= lookahead_prec
;
5802 /* If the stack is not empty, we have parsed into LHS the right side
5803 (`4' in the example above) of an expression we had suspended.
5804 We can use the information on the stack to recover the LHS (`3')
5805 from the stack together with the tree code (`MULT_EXPR'), and
5806 the precedence of the higher level subexpression
5807 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5808 which will be used to actually build the additive expression. */
5811 tree_type
= sp
->tree_type
;
5813 rhs_type
= lhs_type
;
5815 lhs_type
= sp
->lhs_type
;
5818 overloaded_p
= false;
5819 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
5821 lhs_type
= tree_type
;
5823 /* If the binary operator required the use of an overloaded operator,
5824 then this expression cannot be an integral constant-expression.
5825 An overloaded operator can be used even if both operands are
5826 otherwise permissible in an integral constant-expression if at
5827 least one of the operands is of enumeration type. */
5830 && (cp_parser_non_integral_constant_expression
5831 (parser
, "calls to overloaded operators")))
5832 return error_mark_node
;
5839 /* Parse the `? expression : assignment-expression' part of a
5840 conditional-expression. The LOGICAL_OR_EXPR is the
5841 logical-or-expression that started the conditional-expression.
5842 Returns a representation of the entire conditional-expression.
5844 This routine is used by cp_parser_assignment_expression.
5846 ? expression : assignment-expression
5850 ? : assignment-expression */
5853 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5856 tree assignment_expr
;
5858 /* Consume the `?' token. */
5859 cp_lexer_consume_token (parser
->lexer
);
5860 if (cp_parser_allow_gnu_extensions_p (parser
)
5861 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5862 /* Implicit true clause. */
5865 /* Parse the expression. */
5866 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5868 /* The next token should be a `:'. */
5869 cp_parser_require (parser
, CPP_COLON
, "`:'");
5870 /* Parse the assignment-expression. */
5871 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5873 /* Build the conditional-expression. */
5874 return build_x_conditional_expr (logical_or_expr
,
5879 /* Parse an assignment-expression.
5881 assignment-expression:
5882 conditional-expression
5883 logical-or-expression assignment-operator assignment_expression
5886 CAST_P is true if this expression is the target of a cast.
5888 Returns a representation for the expression. */
5891 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5895 /* If the next token is the `throw' keyword, then we're looking at
5896 a throw-expression. */
5897 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5898 expr
= cp_parser_throw_expression (parser
);
5899 /* Otherwise, it must be that we are looking at a
5900 logical-or-expression. */
5903 /* Parse the binary expressions (logical-or-expression). */
5904 expr
= cp_parser_binary_expression (parser
, cast_p
);
5905 /* If the next token is a `?' then we're actually looking at a
5906 conditional-expression. */
5907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5908 return cp_parser_question_colon_clause (parser
, expr
);
5911 enum tree_code assignment_operator
;
5913 /* If it's an assignment-operator, we're using the second
5916 = cp_parser_assignment_operator_opt (parser
);
5917 if (assignment_operator
!= ERROR_MARK
)
5921 /* Parse the right-hand side of the assignment. */
5922 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5923 /* An assignment may not appear in a
5924 constant-expression. */
5925 if (cp_parser_non_integral_constant_expression (parser
,
5927 return error_mark_node
;
5928 /* Build the assignment expression. */
5929 expr
= build_x_modify_expr (expr
,
5930 assignment_operator
,
5939 /* Parse an (optional) assignment-operator.
5941 assignment-operator: one of
5942 = *= /= %= += -= >>= <<= &= ^= |=
5946 assignment-operator: one of
5949 If the next token is an assignment operator, the corresponding tree
5950 code is returned, and the token is consumed. For example, for
5951 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5952 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5953 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5954 operator, ERROR_MARK is returned. */
5956 static enum tree_code
5957 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5962 /* Peek at the next toen. */
5963 token
= cp_lexer_peek_token (parser
->lexer
);
5965 switch (token
->type
)
5976 op
= TRUNC_DIV_EXPR
;
5980 op
= TRUNC_MOD_EXPR
;
6012 /* Nothing else is an assignment operator. */
6016 /* If it was an assignment operator, consume it. */
6017 if (op
!= ERROR_MARK
)
6018 cp_lexer_consume_token (parser
->lexer
);
6023 /* Parse an expression.
6026 assignment-expression
6027 expression , assignment-expression
6029 CAST_P is true if this expression is the target of a cast.
6031 Returns a representation of the expression. */
6034 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
6036 tree expression
= NULL_TREE
;
6040 tree assignment_expression
;
6042 /* Parse the next assignment-expression. */
6043 assignment_expression
6044 = cp_parser_assignment_expression (parser
, cast_p
);
6045 /* If this is the first assignment-expression, we can just
6048 expression
= assignment_expression
;
6050 expression
= build_x_compound_expr (expression
,
6051 assignment_expression
);
6052 /* If the next token is not a comma, then we are done with the
6054 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6056 /* Consume the `,'. */
6057 cp_lexer_consume_token (parser
->lexer
);
6058 /* A comma operator cannot appear in a constant-expression. */
6059 if (cp_parser_non_integral_constant_expression (parser
,
6060 "a comma operator"))
6061 expression
= error_mark_node
;
6067 /* Parse a constant-expression.
6069 constant-expression:
6070 conditional-expression
6072 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6073 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6074 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6075 is false, NON_CONSTANT_P should be NULL. */
6078 cp_parser_constant_expression (cp_parser
* parser
,
6079 bool allow_non_constant_p
,
6080 bool *non_constant_p
)
6082 bool saved_integral_constant_expression_p
;
6083 bool saved_allow_non_integral_constant_expression_p
;
6084 bool saved_non_integral_constant_expression_p
;
6087 /* It might seem that we could simply parse the
6088 conditional-expression, and then check to see if it were
6089 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6090 one that the compiler can figure out is constant, possibly after
6091 doing some simplifications or optimizations. The standard has a
6092 precise definition of constant-expression, and we must honor
6093 that, even though it is somewhat more restrictive.
6099 is not a legal declaration, because `(2, 3)' is not a
6100 constant-expression. The `,' operator is forbidden in a
6101 constant-expression. However, GCC's constant-folding machinery
6102 will fold this operation to an INTEGER_CST for `3'. */
6104 /* Save the old settings. */
6105 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6106 saved_allow_non_integral_constant_expression_p
6107 = parser
->allow_non_integral_constant_expression_p
;
6108 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6109 /* We are now parsing a constant-expression. */
6110 parser
->integral_constant_expression_p
= true;
6111 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6112 parser
->non_integral_constant_expression_p
= false;
6113 /* Although the grammar says "conditional-expression", we parse an
6114 "assignment-expression", which also permits "throw-expression"
6115 and the use of assignment operators. In the case that
6116 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6117 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6118 actually essential that we look for an assignment-expression.
6119 For example, cp_parser_initializer_clauses uses this function to
6120 determine whether a particular assignment-expression is in fact
6122 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6123 /* Restore the old settings. */
6124 parser
->integral_constant_expression_p
6125 = saved_integral_constant_expression_p
;
6126 parser
->allow_non_integral_constant_expression_p
6127 = saved_allow_non_integral_constant_expression_p
;
6128 if (allow_non_constant_p
)
6129 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6130 else if (parser
->non_integral_constant_expression_p
)
6131 expression
= error_mark_node
;
6132 parser
->non_integral_constant_expression_p
6133 = saved_non_integral_constant_expression_p
;
6138 /* Parse __builtin_offsetof.
6140 offsetof-expression:
6141 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6143 offsetof-member-designator:
6145 | offsetof-member-designator "." id-expression
6146 | offsetof-member-designator "[" expression "]" */
6149 cp_parser_builtin_offsetof (cp_parser
*parser
)
6151 int save_ice_p
, save_non_ice_p
;
6155 /* We're about to accept non-integral-constant things, but will
6156 definitely yield an integral constant expression. Save and
6157 restore these values around our local parsing. */
6158 save_ice_p
= parser
->integral_constant_expression_p
;
6159 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6161 /* Consume the "__builtin_offsetof" token. */
6162 cp_lexer_consume_token (parser
->lexer
);
6163 /* Consume the opening `('. */
6164 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6165 /* Parse the type-id. */
6166 type
= cp_parser_type_id (parser
);
6167 /* Look for the `,'. */
6168 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6170 /* Build the (type *)null that begins the traditional offsetof macro. */
6171 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6173 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6174 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6178 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6179 switch (token
->type
)
6181 case CPP_OPEN_SQUARE
:
6182 /* offsetof-member-designator "[" expression "]" */
6183 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6187 /* offsetof-member-designator "." identifier */
6188 cp_lexer_consume_token (parser
->lexer
);
6189 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6193 case CPP_CLOSE_PAREN
:
6194 /* Consume the ")" token. */
6195 cp_lexer_consume_token (parser
->lexer
);
6199 /* Error. We know the following require will fail, but
6200 that gives the proper error message. */
6201 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6202 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6203 expr
= error_mark_node
;
6209 /* If we're processing a template, we can't finish the semantics yet.
6210 Otherwise we can fold the entire expression now. */
6211 if (processing_template_decl
)
6212 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6214 expr
= finish_offsetof (expr
);
6217 parser
->integral_constant_expression_p
= save_ice_p
;
6218 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6223 /* Statements [gram.stmt.stmt] */
6225 /* Parse a statement.
6229 expression-statement
6234 declaration-statement
6237 IN_COMPOUND is true when the statement is nested inside a
6238 cp_parser_compound_statement; this matters for certain pragmas.
6240 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6241 is a (possibly labeled) if statement which is not enclosed in braces
6242 and has an else clause. This is used to implement -Wparentheses. */
6245 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6246 bool in_compound
, bool *if_p
)
6250 location_t statement_location
;
6255 /* There is no statement yet. */
6256 statement
= NULL_TREE
;
6257 /* Peek at the next token. */
6258 token
= cp_lexer_peek_token (parser
->lexer
);
6259 /* Remember the location of the first token in the statement. */
6260 statement_location
= token
->location
;
6261 /* If this is a keyword, then that will often determine what kind of
6262 statement we have. */
6263 if (token
->type
== CPP_KEYWORD
)
6265 enum rid keyword
= token
->keyword
;
6271 /* Looks like a labeled-statement with a case label.
6272 Parse the label, and then use tail recursion to parse
6274 cp_parser_label_for_labeled_statement (parser
);
6279 statement
= cp_parser_selection_statement (parser
, if_p
);
6285 statement
= cp_parser_iteration_statement (parser
);
6292 statement
= cp_parser_jump_statement (parser
);
6295 /* Objective-C++ exception-handling constructs. */
6298 case RID_AT_FINALLY
:
6299 case RID_AT_SYNCHRONIZED
:
6301 statement
= cp_parser_objc_statement (parser
);
6305 statement
= cp_parser_try_block (parser
);
6309 /* It might be a keyword like `int' that can start a
6310 declaration-statement. */
6314 else if (token
->type
== CPP_NAME
)
6316 /* If the next token is a `:', then we are looking at a
6317 labeled-statement. */
6318 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6319 if (token
->type
== CPP_COLON
)
6321 /* Looks like a labeled-statement with an ordinary label.
6322 Parse the label, and then use tail recursion to parse
6324 cp_parser_label_for_labeled_statement (parser
);
6328 /* Anything that starts with a `{' must be a compound-statement. */
6329 else if (token
->type
== CPP_OPEN_BRACE
)
6330 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6331 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6332 a statement all its own. */
6333 else if (token
->type
== CPP_PRAGMA
)
6335 /* Only certain OpenMP pragmas are attached to statements, and thus
6336 are considered statements themselves. All others are not. In
6337 the context of a compound, accept the pragma as a "statement" and
6338 return so that we can check for a close brace. Otherwise we
6339 require a real statement and must go back and read one. */
6341 cp_parser_pragma (parser
, pragma_compound
);
6342 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6346 else if (token
->type
== CPP_EOF
)
6348 cp_parser_error (parser
, "expected statement");
6352 /* Everything else must be a declaration-statement or an
6353 expression-statement. Try for the declaration-statement
6354 first, unless we are looking at a `;', in which case we know that
6355 we have an expression-statement. */
6358 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6360 cp_parser_parse_tentatively (parser
);
6361 /* Try to parse the declaration-statement. */
6362 cp_parser_declaration_statement (parser
);
6363 /* If that worked, we're done. */
6364 if (cp_parser_parse_definitely (parser
))
6367 /* Look for an expression-statement instead. */
6368 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6371 /* Set the line number for the statement. */
6372 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6373 SET_EXPR_LOCATION (statement
, statement_location
);
6376 /* Parse the label for a labeled-statement, i.e.
6379 case constant-expression :
6383 case constant-expression ... constant-expression : statement
6385 When a label is parsed without errors, the label is added to the
6386 parse tree by the finish_* functions, so this function doesn't
6387 have to return the label. */
6390 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6394 /* The next token should be an identifier. */
6395 token
= cp_lexer_peek_token (parser
->lexer
);
6396 if (token
->type
!= CPP_NAME
6397 && token
->type
!= CPP_KEYWORD
)
6399 cp_parser_error (parser
, "expected labeled-statement");
6403 switch (token
->keyword
)
6410 /* Consume the `case' token. */
6411 cp_lexer_consume_token (parser
->lexer
);
6412 /* Parse the constant-expression. */
6413 expr
= cp_parser_constant_expression (parser
,
6414 /*allow_non_constant_p=*/false,
6417 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6418 if (ellipsis
->type
== CPP_ELLIPSIS
)
6420 /* Consume the `...' token. */
6421 cp_lexer_consume_token (parser
->lexer
);
6423 cp_parser_constant_expression (parser
,
6424 /*allow_non_constant_p=*/false,
6426 /* We don't need to emit warnings here, as the common code
6427 will do this for us. */
6430 expr_hi
= NULL_TREE
;
6432 if (parser
->in_switch_statement_p
)
6433 finish_case_label (expr
, expr_hi
);
6435 error ("case label %qE not within a switch statement", expr
);
6440 /* Consume the `default' token. */
6441 cp_lexer_consume_token (parser
->lexer
);
6443 if (parser
->in_switch_statement_p
)
6444 finish_case_label (NULL_TREE
, NULL_TREE
);
6446 error ("case label not within a switch statement");
6450 /* Anything else must be an ordinary label. */
6451 finish_label_stmt (cp_parser_identifier (parser
));
6455 /* Require the `:' token. */
6456 cp_parser_require (parser
, CPP_COLON
, "`:'");
6459 /* Parse an expression-statement.
6461 expression-statement:
6464 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6465 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6466 indicates whether this expression-statement is part of an
6467 expression statement. */
6470 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6472 tree statement
= NULL_TREE
;
6474 /* If the next token is a ';', then there is no expression
6476 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6477 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6479 /* Consume the final `;'. */
6480 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6482 if (in_statement_expr
6483 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6484 /* This is the final expression statement of a statement
6486 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6488 statement
= finish_expr_stmt (statement
);
6495 /* Parse a compound-statement.
6498 { statement-seq [opt] }
6500 Returns a tree representing the statement. */
6503 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6508 /* Consume the `{'. */
6509 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6510 return error_mark_node
;
6511 /* Begin the compound-statement. */
6512 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6513 /* Parse an (optional) statement-seq. */
6514 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6515 /* Finish the compound-statement. */
6516 finish_compound_stmt (compound_stmt
);
6517 /* Consume the `}'. */
6518 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6520 return compound_stmt
;
6523 /* Parse an (optional) statement-seq.
6527 statement-seq [opt] statement */
6530 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6532 /* Scan statements until there aren't any more. */
6535 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6537 /* If we're looking at a `}', then we've run out of statements. */
6538 if (token
->type
== CPP_CLOSE_BRACE
6539 || token
->type
== CPP_EOF
6540 || token
->type
== CPP_PRAGMA_EOL
)
6543 /* Parse the statement. */
6544 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
6548 /* Parse a selection-statement.
6550 selection-statement:
6551 if ( condition ) statement
6552 if ( condition ) statement else statement
6553 switch ( condition ) statement
6555 Returns the new IF_STMT or SWITCH_STMT.
6557 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6558 is a (possibly labeled) if statement which is not enclosed in
6559 braces and has an else clause. This is used to implement
6563 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
6571 /* Peek at the next token. */
6572 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6574 /* See what kind of keyword it is. */
6575 keyword
= token
->keyword
;
6584 /* Look for the `('. */
6585 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6587 cp_parser_skip_to_end_of_statement (parser
);
6588 return error_mark_node
;
6591 /* Begin the selection-statement. */
6592 if (keyword
== RID_IF
)
6593 statement
= begin_if_stmt ();
6595 statement
= begin_switch_stmt ();
6597 /* Parse the condition. */
6598 condition
= cp_parser_condition (parser
);
6599 /* Look for the `)'. */
6600 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6601 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6602 /*consume_paren=*/true);
6604 if (keyword
== RID_IF
)
6608 /* Add the condition. */
6609 finish_if_stmt_cond (condition
, statement
);
6611 /* Parse the then-clause. */
6612 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
6613 finish_then_clause (statement
);
6615 /* If the next token is `else', parse the else-clause. */
6616 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6619 /* Consume the `else' keyword. */
6620 cp_lexer_consume_token (parser
->lexer
);
6621 begin_else_clause (statement
);
6622 /* Parse the else-clause. */
6623 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6624 finish_else_clause (statement
);
6626 /* If we are currently parsing a then-clause, then
6627 IF_P will not be NULL. We set it to true to
6628 indicate that this if statement has an else clause.
6629 This may trigger the Wparentheses warning below
6630 when we get back up to the parent if statement. */
6636 /* This if statement does not have an else clause. If
6637 NESTED_IF is true, then the then-clause is an if
6638 statement which does have an else clause. We warn
6639 about the potential ambiguity. */
6641 warning (OPT_Wparentheses
,
6642 ("%Hsuggest explicit braces "
6643 "to avoid ambiguous %<else%>"),
6644 EXPR_LOCUS (statement
));
6647 /* Now we're all done with the if-statement. */
6648 finish_if_stmt (statement
);
6652 bool in_switch_statement_p
;
6653 unsigned char in_statement
;
6655 /* Add the condition. */
6656 finish_switch_cond (condition
, statement
);
6658 /* Parse the body of the switch-statement. */
6659 in_switch_statement_p
= parser
->in_switch_statement_p
;
6660 in_statement
= parser
->in_statement
;
6661 parser
->in_switch_statement_p
= true;
6662 parser
->in_statement
|= IN_SWITCH_STMT
;
6663 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6664 parser
->in_switch_statement_p
= in_switch_statement_p
;
6665 parser
->in_statement
= in_statement
;
6667 /* Now we're all done with the switch-statement. */
6668 finish_switch_stmt (statement
);
6676 cp_parser_error (parser
, "expected selection-statement");
6677 return error_mark_node
;
6681 /* Parse a condition.
6685 type-specifier-seq declarator = assignment-expression
6690 type-specifier-seq declarator asm-specification [opt]
6691 attributes [opt] = assignment-expression
6693 Returns the expression that should be tested. */
6696 cp_parser_condition (cp_parser
* parser
)
6698 cp_decl_specifier_seq type_specifiers
;
6699 const char *saved_message
;
6701 /* Try the declaration first. */
6702 cp_parser_parse_tentatively (parser
);
6703 /* New types are not allowed in the type-specifier-seq for a
6705 saved_message
= parser
->type_definition_forbidden_message
;
6706 parser
->type_definition_forbidden_message
6707 = "types may not be defined in conditions";
6708 /* Parse the type-specifier-seq. */
6709 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6711 /* Restore the saved message. */
6712 parser
->type_definition_forbidden_message
= saved_message
;
6713 /* If all is well, we might be looking at a declaration. */
6714 if (!cp_parser_error_occurred (parser
))
6717 tree asm_specification
;
6719 cp_declarator
*declarator
;
6720 tree initializer
= NULL_TREE
;
6722 /* Parse the declarator. */
6723 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6724 /*ctor_dtor_or_conv_p=*/NULL
,
6725 /*parenthesized_p=*/NULL
,
6726 /*member_p=*/false);
6727 /* Parse the attributes. */
6728 attributes
= cp_parser_attributes_opt (parser
);
6729 /* Parse the asm-specification. */
6730 asm_specification
= cp_parser_asm_specification_opt (parser
);
6731 /* If the next token is not an `=', then we might still be
6732 looking at an expression. For example:
6736 looks like a decl-specifier-seq and a declarator -- but then
6737 there is no `=', so this is an expression. */
6738 cp_parser_require (parser
, CPP_EQ
, "`='");
6739 /* If we did see an `=', then we are looking at a declaration
6741 if (cp_parser_parse_definitely (parser
))
6744 bool non_constant_p
;
6746 /* Create the declaration. */
6747 decl
= start_decl (declarator
, &type_specifiers
,
6748 /*initialized_p=*/true,
6749 attributes
, /*prefix_attributes=*/NULL_TREE
,
6751 /* Parse the assignment-expression. */
6753 = cp_parser_constant_expression (parser
,
6754 /*allow_non_constant_p=*/true,
6756 if (!non_constant_p
)
6757 initializer
= fold_non_dependent_expr (initializer
);
6759 /* Process the initializer. */
6760 cp_finish_decl (decl
,
6761 initializer
, !non_constant_p
,
6763 LOOKUP_ONLYCONVERTING
);
6766 pop_scope (pushed_scope
);
6768 return convert_from_reference (decl
);
6771 /* If we didn't even get past the declarator successfully, we are
6772 definitely not looking at a declaration. */
6774 cp_parser_abort_tentative_parse (parser
);
6776 /* Otherwise, we are looking at an expression. */
6777 return cp_parser_expression (parser
, /*cast_p=*/false);
6780 /* Parse an iteration-statement.
6782 iteration-statement:
6783 while ( condition ) statement
6784 do statement while ( expression ) ;
6785 for ( for-init-statement condition [opt] ; expression [opt] )
6788 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6791 cp_parser_iteration_statement (cp_parser
* parser
)
6796 unsigned char in_statement
;
6798 /* Peek at the next token. */
6799 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6801 return error_mark_node
;
6803 /* Remember whether or not we are already within an iteration
6805 in_statement
= parser
->in_statement
;
6807 /* See what kind of keyword it is. */
6808 keyword
= token
->keyword
;
6815 /* Begin the while-statement. */
6816 statement
= begin_while_stmt ();
6817 /* Look for the `('. */
6818 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6819 /* Parse the condition. */
6820 condition
= cp_parser_condition (parser
);
6821 finish_while_stmt_cond (condition
, statement
);
6822 /* Look for the `)'. */
6823 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6824 /* Parse the dependent statement. */
6825 parser
->in_statement
= IN_ITERATION_STMT
;
6826 cp_parser_already_scoped_statement (parser
);
6827 parser
->in_statement
= in_statement
;
6828 /* We're done with the while-statement. */
6829 finish_while_stmt (statement
);
6837 /* Begin the do-statement. */
6838 statement
= begin_do_stmt ();
6839 /* Parse the body of the do-statement. */
6840 parser
->in_statement
= IN_ITERATION_STMT
;
6841 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6842 parser
->in_statement
= in_statement
;
6843 finish_do_body (statement
);
6844 /* Look for the `while' keyword. */
6845 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6846 /* Look for the `('. */
6847 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6848 /* Parse the expression. */
6849 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6850 /* We're done with the do-statement. */
6851 finish_do_stmt (expression
, statement
);
6852 /* Look for the `)'. */
6853 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6854 /* Look for the `;'. */
6855 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6861 tree condition
= NULL_TREE
;
6862 tree expression
= NULL_TREE
;
6864 /* Begin the for-statement. */
6865 statement
= begin_for_stmt ();
6866 /* Look for the `('. */
6867 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6868 /* Parse the initialization. */
6869 cp_parser_for_init_statement (parser
);
6870 finish_for_init_stmt (statement
);
6872 /* If there's a condition, process it. */
6873 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6874 condition
= cp_parser_condition (parser
);
6875 finish_for_cond (condition
, statement
);
6876 /* Look for the `;'. */
6877 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6879 /* If there's an expression, process it. */
6880 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6881 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6882 finish_for_expr (expression
, statement
);
6883 /* Look for the `)'. */
6884 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6886 /* Parse the body of the for-statement. */
6887 parser
->in_statement
= IN_ITERATION_STMT
;
6888 cp_parser_already_scoped_statement (parser
);
6889 parser
->in_statement
= in_statement
;
6891 /* We're done with the for-statement. */
6892 finish_for_stmt (statement
);
6897 cp_parser_error (parser
, "expected iteration-statement");
6898 statement
= error_mark_node
;
6905 /* Parse a for-init-statement.
6908 expression-statement
6909 simple-declaration */
6912 cp_parser_for_init_statement (cp_parser
* parser
)
6914 /* If the next token is a `;', then we have an empty
6915 expression-statement. Grammatically, this is also a
6916 simple-declaration, but an invalid one, because it does not
6917 declare anything. Therefore, if we did not handle this case
6918 specially, we would issue an error message about an invalid
6920 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6922 /* We're going to speculatively look for a declaration, falling back
6923 to an expression, if necessary. */
6924 cp_parser_parse_tentatively (parser
);
6925 /* Parse the declaration. */
6926 cp_parser_simple_declaration (parser
,
6927 /*function_definition_allowed_p=*/false);
6928 /* If the tentative parse failed, then we shall need to look for an
6929 expression-statement. */
6930 if (cp_parser_parse_definitely (parser
))
6934 cp_parser_expression_statement (parser
, false);
6937 /* Parse a jump-statement.
6942 return expression [opt] ;
6950 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6953 cp_parser_jump_statement (cp_parser
* parser
)
6955 tree statement
= error_mark_node
;
6959 /* Peek at the next token. */
6960 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6962 return error_mark_node
;
6964 /* See what kind of keyword it is. */
6965 keyword
= token
->keyword
;
6969 switch (parser
->in_statement
)
6972 error ("break statement not within loop or switch");
6975 gcc_assert ((parser
->in_statement
& IN_SWITCH_STMT
)
6976 || parser
->in_statement
== IN_ITERATION_STMT
);
6977 statement
= finish_break_stmt ();
6980 error ("invalid exit from OpenMP structured block");
6983 error ("break statement used with OpenMP for loop");
6986 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6990 switch (parser
->in_statement
& ~IN_SWITCH_STMT
)
6993 error ("continue statement not within a loop");
6995 case IN_ITERATION_STMT
:
6997 statement
= finish_continue_stmt ();
7000 error ("invalid exit from OpenMP structured block");
7005 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7012 /* If the next token is a `;', then there is no
7014 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7015 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
7018 /* Build the return-statement. */
7019 statement
= finish_return_stmt (expr
);
7020 /* Look for the final `;'. */
7021 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7026 /* Create the goto-statement. */
7027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
7029 /* Issue a warning about this use of a GNU extension. */
7031 pedwarn ("ISO C++ forbids computed gotos");
7032 /* Consume the '*' token. */
7033 cp_lexer_consume_token (parser
->lexer
);
7034 /* Parse the dependent expression. */
7035 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
7038 finish_goto_stmt (cp_parser_identifier (parser
));
7039 /* Look for the final `;'. */
7040 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7044 cp_parser_error (parser
, "expected jump-statement");
7051 /* Parse a declaration-statement.
7053 declaration-statement:
7054 block-declaration */
7057 cp_parser_declaration_statement (cp_parser
* parser
)
7061 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7062 p
= obstack_alloc (&declarator_obstack
, 0);
7064 /* Parse the block-declaration. */
7065 cp_parser_block_declaration (parser
, /*statement_p=*/true);
7067 /* Free any declarators allocated. */
7068 obstack_free (&declarator_obstack
, p
);
7070 /* Finish off the statement. */
7074 /* Some dependent statements (like `if (cond) statement'), are
7075 implicitly in their own scope. In other words, if the statement is
7076 a single statement (as opposed to a compound-statement), it is
7077 none-the-less treated as if it were enclosed in braces. Any
7078 declarations appearing in the dependent statement are out of scope
7079 after control passes that point. This function parses a statement,
7080 but ensures that is in its own scope, even if it is not a
7083 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7084 is a (possibly labeled) if statement which is not enclosed in
7085 braces and has an else clause. This is used to implement
7088 Returns the new statement. */
7091 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
7098 /* Mark if () ; with a special NOP_EXPR. */
7099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7101 cp_lexer_consume_token (parser
->lexer
);
7102 statement
= add_stmt (build_empty_stmt ());
7104 /* if a compound is opened, we simply parse the statement directly. */
7105 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7106 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7107 /* If the token is not a `{', then we must take special action. */
7110 /* Create a compound-statement. */
7111 statement
= begin_compound_stmt (0);
7112 /* Parse the dependent-statement. */
7113 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
7114 /* Finish the dummy compound-statement. */
7115 finish_compound_stmt (statement
);
7118 /* Return the statement. */
7122 /* For some dependent statements (like `while (cond) statement'), we
7123 have already created a scope. Therefore, even if the dependent
7124 statement is a compound-statement, we do not want to create another
7128 cp_parser_already_scoped_statement (cp_parser
* parser
)
7130 /* If the token is a `{', then we must take special action. */
7131 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7132 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
7135 /* Avoid calling cp_parser_compound_statement, so that we
7136 don't create a new scope. Do everything else by hand. */
7137 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
7138 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7139 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7143 /* Declarations [gram.dcl.dcl] */
7145 /* Parse an optional declaration-sequence.
7149 declaration-seq declaration */
7152 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7158 token
= cp_lexer_peek_token (parser
->lexer
);
7160 if (token
->type
== CPP_CLOSE_BRACE
7161 || token
->type
== CPP_EOF
7162 || token
->type
== CPP_PRAGMA_EOL
)
7165 if (token
->type
== CPP_SEMICOLON
)
7167 /* A declaration consisting of a single semicolon is
7168 invalid. Allow it unless we're being pedantic. */
7169 cp_lexer_consume_token (parser
->lexer
);
7170 if (pedantic
&& !in_system_header
)
7171 pedwarn ("extra %<;%>");
7175 /* If we're entering or exiting a region that's implicitly
7176 extern "C", modify the lang context appropriately. */
7177 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7179 push_lang_context (lang_name_c
);
7180 parser
->implicit_extern_c
= true;
7182 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7184 pop_lang_context ();
7185 parser
->implicit_extern_c
= false;
7188 if (token
->type
== CPP_PRAGMA
)
7190 /* A top-level declaration can consist solely of a #pragma.
7191 A nested declaration cannot, so this is done here and not
7192 in cp_parser_declaration. (A #pragma at block scope is
7193 handled in cp_parser_statement.) */
7194 cp_parser_pragma (parser
, pragma_external
);
7198 /* Parse the declaration itself. */
7199 cp_parser_declaration (parser
);
7203 /* Parse a declaration.
7208 template-declaration
7209 explicit-instantiation
7210 explicit-specialization
7211 linkage-specification
7212 namespace-definition
7217 __extension__ declaration */
7220 cp_parser_declaration (cp_parser
* parser
)
7227 /* Check for the `__extension__' keyword. */
7228 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7230 /* Parse the qualified declaration. */
7231 cp_parser_declaration (parser
);
7232 /* Restore the PEDANTIC flag. */
7233 pedantic
= saved_pedantic
;
7238 /* Try to figure out what kind of declaration is present. */
7239 token1
= *cp_lexer_peek_token (parser
->lexer
);
7241 if (token1
.type
!= CPP_EOF
)
7242 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7245 token2
.type
= CPP_EOF
;
7246 token2
.keyword
= RID_MAX
;
7249 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7250 p
= obstack_alloc (&declarator_obstack
, 0);
7252 /* If the next token is `extern' and the following token is a string
7253 literal, then we have a linkage specification. */
7254 if (token1
.keyword
== RID_EXTERN
7255 && cp_parser_is_string_literal (&token2
))
7256 cp_parser_linkage_specification (parser
);
7257 /* If the next token is `template', then we have either a template
7258 declaration, an explicit instantiation, or an explicit
7260 else if (token1
.keyword
== RID_TEMPLATE
)
7262 /* `template <>' indicates a template specialization. */
7263 if (token2
.type
== CPP_LESS
7264 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7265 cp_parser_explicit_specialization (parser
);
7266 /* `template <' indicates a template declaration. */
7267 else if (token2
.type
== CPP_LESS
)
7268 cp_parser_template_declaration (parser
, /*member_p=*/false);
7269 /* Anything else must be an explicit instantiation. */
7271 cp_parser_explicit_instantiation (parser
);
7273 /* If the next token is `export', then we have a template
7275 else if (token1
.keyword
== RID_EXPORT
)
7276 cp_parser_template_declaration (parser
, /*member_p=*/false);
7277 /* If the next token is `extern', 'static' or 'inline' and the one
7278 after that is `template', we have a GNU extended explicit
7279 instantiation directive. */
7280 else if (cp_parser_allow_gnu_extensions_p (parser
)
7281 && (token1
.keyword
== RID_EXTERN
7282 || token1
.keyword
== RID_STATIC
7283 || token1
.keyword
== RID_INLINE
)
7284 && token2
.keyword
== RID_TEMPLATE
)
7285 cp_parser_explicit_instantiation (parser
);
7286 /* If the next token is `namespace', check for a named or unnamed
7287 namespace definition. */
7288 else if (token1
.keyword
== RID_NAMESPACE
7289 && (/* A named namespace definition. */
7290 (token2
.type
== CPP_NAME
7291 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7293 /* An unnamed namespace definition. */
7294 || token2
.type
== CPP_OPEN_BRACE
7295 || token2
.keyword
== RID_ATTRIBUTE
))
7296 cp_parser_namespace_definition (parser
);
7297 /* Objective-C++ declaration/definition. */
7298 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7299 cp_parser_objc_declaration (parser
);
7300 /* We must have either a block declaration or a function
7303 /* Try to parse a block-declaration, or a function-definition. */
7304 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7306 /* Free any declarators allocated. */
7307 obstack_free (&declarator_obstack
, p
);
7310 /* Parse a block-declaration.
7315 namespace-alias-definition
7322 __extension__ block-declaration
7328 static_assert-declaration
7330 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7331 part of a declaration-statement. */
7334 cp_parser_block_declaration (cp_parser
*parser
,
7340 /* Check for the `__extension__' keyword. */
7341 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7343 /* Parse the qualified declaration. */
7344 cp_parser_block_declaration (parser
, statement_p
);
7345 /* Restore the PEDANTIC flag. */
7346 pedantic
= saved_pedantic
;
7351 /* Peek at the next token to figure out which kind of declaration is
7353 token1
= cp_lexer_peek_token (parser
->lexer
);
7355 /* If the next keyword is `asm', we have an asm-definition. */
7356 if (token1
->keyword
== RID_ASM
)
7359 cp_parser_commit_to_tentative_parse (parser
);
7360 cp_parser_asm_definition (parser
);
7362 /* If the next keyword is `namespace', we have a
7363 namespace-alias-definition. */
7364 else if (token1
->keyword
== RID_NAMESPACE
)
7365 cp_parser_namespace_alias_definition (parser
);
7366 /* If the next keyword is `using', we have either a
7367 using-declaration or a using-directive. */
7368 else if (token1
->keyword
== RID_USING
)
7373 cp_parser_commit_to_tentative_parse (parser
);
7374 /* If the token after `using' is `namespace', then we have a
7376 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7377 if (token2
->keyword
== RID_NAMESPACE
)
7378 cp_parser_using_directive (parser
);
7379 /* Otherwise, it's a using-declaration. */
7381 cp_parser_using_declaration (parser
,
7382 /*access_declaration_p=*/false);
7384 /* If the next keyword is `__label__' we have a label declaration. */
7385 else if (token1
->keyword
== RID_LABEL
)
7388 cp_parser_commit_to_tentative_parse (parser
);
7389 cp_parser_label_declaration (parser
);
7391 /* If the next token is `static_assert' we have a static assertion. */
7392 else if (token1
->keyword
== RID_STATIC_ASSERT
)
7393 cp_parser_static_assert (parser
, /*member_p=*/false);
7394 /* Anything else must be a simple-declaration. */
7396 cp_parser_simple_declaration (parser
, !statement_p
);
7399 /* Parse a simple-declaration.
7402 decl-specifier-seq [opt] init-declarator-list [opt] ;
7404 init-declarator-list:
7406 init-declarator-list , init-declarator
7408 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7409 function-definition as a simple-declaration. */
7412 cp_parser_simple_declaration (cp_parser
* parser
,
7413 bool function_definition_allowed_p
)
7415 cp_decl_specifier_seq decl_specifiers
;
7416 int declares_class_or_enum
;
7417 bool saw_declarator
;
7419 /* Defer access checks until we know what is being declared; the
7420 checks for names appearing in the decl-specifier-seq should be
7421 done as if we were in the scope of the thing being declared. */
7422 push_deferring_access_checks (dk_deferred
);
7424 /* Parse the decl-specifier-seq. We have to keep track of whether
7425 or not the decl-specifier-seq declares a named class or
7426 enumeration type, since that is the only case in which the
7427 init-declarator-list is allowed to be empty.
7431 In a simple-declaration, the optional init-declarator-list can be
7432 omitted only when declaring a class or enumeration, that is when
7433 the decl-specifier-seq contains either a class-specifier, an
7434 elaborated-type-specifier, or an enum-specifier. */
7435 cp_parser_decl_specifier_seq (parser
,
7436 CP_PARSER_FLAGS_OPTIONAL
,
7438 &declares_class_or_enum
);
7439 /* We no longer need to defer access checks. */
7440 stop_deferring_access_checks ();
7442 /* In a block scope, a valid declaration must always have a
7443 decl-specifier-seq. By not trying to parse declarators, we can
7444 resolve the declaration/expression ambiguity more quickly. */
7445 if (!function_definition_allowed_p
7446 && !decl_specifiers
.any_specifiers_p
)
7448 cp_parser_error (parser
, "expected declaration");
7452 /* If the next two tokens are both identifiers, the code is
7453 erroneous. The usual cause of this situation is code like:
7457 where "T" should name a type -- but does not. */
7458 if (!decl_specifiers
.type
7459 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7461 /* If parsing tentatively, we should commit; we really are
7462 looking at a declaration. */
7463 cp_parser_commit_to_tentative_parse (parser
);
7468 /* If we have seen at least one decl-specifier, and the next token
7469 is not a parenthesis, then we must be looking at a declaration.
7470 (After "int (" we might be looking at a functional cast.) */
7471 if (decl_specifiers
.any_specifiers_p
7472 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7473 cp_parser_commit_to_tentative_parse (parser
);
7475 /* Keep going until we hit the `;' at the end of the simple
7477 saw_declarator
= false;
7478 while (cp_lexer_next_token_is_not (parser
->lexer
,
7482 bool function_definition_p
;
7487 /* If we are processing next declarator, coma is expected */
7488 token
= cp_lexer_peek_token (parser
->lexer
);
7489 gcc_assert (token
->type
== CPP_COMMA
);
7490 cp_lexer_consume_token (parser
->lexer
);
7493 saw_declarator
= true;
7495 /* Parse the init-declarator. */
7496 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7498 function_definition_allowed_p
,
7500 declares_class_or_enum
,
7501 &function_definition_p
);
7502 /* If an error occurred while parsing tentatively, exit quickly.
7503 (That usually happens when in the body of a function; each
7504 statement is treated as a declaration-statement until proven
7506 if (cp_parser_error_occurred (parser
))
7508 /* Handle function definitions specially. */
7509 if (function_definition_p
)
7511 /* If the next token is a `,', then we are probably
7512 processing something like:
7516 which is erroneous. */
7517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7518 error ("mixing declarations and function-definitions is forbidden");
7519 /* Otherwise, we're done with the list of declarators. */
7522 pop_deferring_access_checks ();
7526 /* The next token should be either a `,' or a `;'. */
7527 token
= cp_lexer_peek_token (parser
->lexer
);
7528 /* If it's a `,', there are more declarators to come. */
7529 if (token
->type
== CPP_COMMA
)
7530 /* will be consumed next time around */;
7531 /* If it's a `;', we are done. */
7532 else if (token
->type
== CPP_SEMICOLON
)
7534 /* Anything else is an error. */
7537 /* If we have already issued an error message we don't need
7538 to issue another one. */
7539 if (decl
!= error_mark_node
7540 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7541 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7542 /* Skip tokens until we reach the end of the statement. */
7543 cp_parser_skip_to_end_of_statement (parser
);
7544 /* If the next token is now a `;', consume it. */
7545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7546 cp_lexer_consume_token (parser
->lexer
);
7549 /* After the first time around, a function-definition is not
7550 allowed -- even if it was OK at first. For example:
7555 function_definition_allowed_p
= false;
7558 /* Issue an error message if no declarators are present, and the
7559 decl-specifier-seq does not itself declare a class or
7561 if (!saw_declarator
)
7563 if (cp_parser_declares_only_class_p (parser
))
7564 shadow_tag (&decl_specifiers
);
7565 /* Perform any deferred access checks. */
7566 perform_deferred_access_checks ();
7569 /* Consume the `;'. */
7570 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7573 pop_deferring_access_checks ();
7576 /* Parse a decl-specifier-seq.
7579 decl-specifier-seq [opt] decl-specifier
7582 storage-class-specifier
7593 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7595 The parser flags FLAGS is used to control type-specifier parsing.
7597 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7600 1: one of the decl-specifiers is an elaborated-type-specifier
7601 (i.e., a type declaration)
7602 2: one of the decl-specifiers is an enum-specifier or a
7603 class-specifier (i.e., a type definition)
7608 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7609 cp_parser_flags flags
,
7610 cp_decl_specifier_seq
*decl_specs
,
7611 int* declares_class_or_enum
)
7613 bool constructor_possible_p
= !parser
->in_declarator_p
;
7615 /* Clear DECL_SPECS. */
7616 clear_decl_specs (decl_specs
);
7618 /* Assume no class or enumeration type is declared. */
7619 *declares_class_or_enum
= 0;
7621 /* Keep reading specifiers until there are no more to read. */
7625 bool found_decl_spec
;
7628 /* Peek at the next token. */
7629 token
= cp_lexer_peek_token (parser
->lexer
);
7630 /* Handle attributes. */
7631 if (token
->keyword
== RID_ATTRIBUTE
)
7633 /* Parse the attributes. */
7634 decl_specs
->attributes
7635 = chainon (decl_specs
->attributes
,
7636 cp_parser_attributes_opt (parser
));
7639 /* Assume we will find a decl-specifier keyword. */
7640 found_decl_spec
= true;
7641 /* If the next token is an appropriate keyword, we can simply
7642 add it to the list. */
7643 switch (token
->keyword
)
7648 if (!at_class_scope_p ())
7650 error ("%<friend%> used outside of class");
7651 cp_lexer_purge_token (parser
->lexer
);
7655 ++decl_specs
->specs
[(int) ds_friend
];
7656 /* Consume the token. */
7657 cp_lexer_consume_token (parser
->lexer
);
7661 /* function-specifier:
7668 cp_parser_function_specifier_opt (parser
, decl_specs
);
7674 ++decl_specs
->specs
[(int) ds_typedef
];
7675 /* Consume the token. */
7676 cp_lexer_consume_token (parser
->lexer
);
7677 /* A constructor declarator cannot appear in a typedef. */
7678 constructor_possible_p
= false;
7679 /* The "typedef" keyword can only occur in a declaration; we
7680 may as well commit at this point. */
7681 cp_parser_commit_to_tentative_parse (parser
);
7683 if (decl_specs
->storage_class
!= sc_none
)
7684 decl_specs
->conflicting_specifiers_p
= true;
7687 /* storage-class-specifier:
7701 /* Consume the token. */
7702 cp_lexer_consume_token (parser
->lexer
);
7703 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7706 /* Consume the token. */
7707 cp_lexer_consume_token (parser
->lexer
);
7708 ++decl_specs
->specs
[(int) ds_thread
];
7712 /* We did not yet find a decl-specifier yet. */
7713 found_decl_spec
= false;
7717 /* Constructors are a special case. The `S' in `S()' is not a
7718 decl-specifier; it is the beginning of the declarator. */
7721 && constructor_possible_p
7722 && (cp_parser_constructor_declarator_p
7723 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7725 /* If we don't have a DECL_SPEC yet, then we must be looking at
7726 a type-specifier. */
7727 if (!found_decl_spec
&& !constructor_p
)
7729 int decl_spec_declares_class_or_enum
;
7730 bool is_cv_qualifier
;
7734 = cp_parser_type_specifier (parser
, flags
,
7736 /*is_declaration=*/true,
7737 &decl_spec_declares_class_or_enum
,
7740 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7742 /* If this type-specifier referenced a user-defined type
7743 (a typedef, class-name, etc.), then we can't allow any
7744 more such type-specifiers henceforth.
7748 The longest sequence of decl-specifiers that could
7749 possibly be a type name is taken as the
7750 decl-specifier-seq of a declaration. The sequence shall
7751 be self-consistent as described below.
7755 As a general rule, at most one type-specifier is allowed
7756 in the complete decl-specifier-seq of a declaration. The
7757 only exceptions are the following:
7759 -- const or volatile can be combined with any other
7762 -- signed or unsigned can be combined with char, long,
7770 void g (const int Pc);
7772 Here, Pc is *not* part of the decl-specifier seq; it's
7773 the declarator. Therefore, once we see a type-specifier
7774 (other than a cv-qualifier), we forbid any additional
7775 user-defined types. We *do* still allow things like `int
7776 int' to be considered a decl-specifier-seq, and issue the
7777 error message later. */
7778 if (type_spec
&& !is_cv_qualifier
)
7779 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7780 /* A constructor declarator cannot follow a type-specifier. */
7783 constructor_possible_p
= false;
7784 found_decl_spec
= true;
7788 /* If we still do not have a DECL_SPEC, then there are no more
7790 if (!found_decl_spec
)
7793 decl_specs
->any_specifiers_p
= true;
7794 /* After we see one decl-specifier, further decl-specifiers are
7796 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7799 cp_parser_check_decl_spec (decl_specs
);
7801 /* Don't allow a friend specifier with a class definition. */
7802 if (decl_specs
->specs
[(int) ds_friend
] != 0
7803 && (*declares_class_or_enum
& 2))
7804 error ("class definition may not be declared a friend");
7807 /* Parse an (optional) storage-class-specifier.
7809 storage-class-specifier:
7818 storage-class-specifier:
7821 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7824 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7826 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7834 /* Consume the token. */
7835 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
7842 /* Parse an (optional) function-specifier.
7849 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7850 Updates DECL_SPECS, if it is non-NULL. */
7853 cp_parser_function_specifier_opt (cp_parser
* parser
,
7854 cp_decl_specifier_seq
*decl_specs
)
7856 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7860 ++decl_specs
->specs
[(int) ds_inline
];
7864 /* 14.5.2.3 [temp.mem]
7866 A member function template shall not be virtual. */
7867 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7868 error ("templates may not be %<virtual%>");
7869 else if (decl_specs
)
7870 ++decl_specs
->specs
[(int) ds_virtual
];
7875 ++decl_specs
->specs
[(int) ds_explicit
];
7882 /* Consume the token. */
7883 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
7886 /* Parse a linkage-specification.
7888 linkage-specification:
7889 extern string-literal { declaration-seq [opt] }
7890 extern string-literal declaration */
7893 cp_parser_linkage_specification (cp_parser
* parser
)
7897 /* Look for the `extern' keyword. */
7898 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7900 /* Look for the string-literal. */
7901 linkage
= cp_parser_string_literal (parser
, false, false);
7903 /* Transform the literal into an identifier. If the literal is a
7904 wide-character string, or contains embedded NULs, then we can't
7905 handle it as the user wants. */
7906 if (strlen (TREE_STRING_POINTER (linkage
))
7907 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7909 cp_parser_error (parser
, "invalid linkage-specification");
7910 /* Assume C++ linkage. */
7911 linkage
= lang_name_cplusplus
;
7914 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7916 /* We're now using the new linkage. */
7917 push_lang_context (linkage
);
7919 /* If the next token is a `{', then we're using the first
7921 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7923 /* Consume the `{' token. */
7924 cp_lexer_consume_token (parser
->lexer
);
7925 /* Parse the declarations. */
7926 cp_parser_declaration_seq_opt (parser
);
7927 /* Look for the closing `}'. */
7928 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7930 /* Otherwise, there's just one declaration. */
7933 bool saved_in_unbraced_linkage_specification_p
;
7935 saved_in_unbraced_linkage_specification_p
7936 = parser
->in_unbraced_linkage_specification_p
;
7937 parser
->in_unbraced_linkage_specification_p
= true;
7938 cp_parser_declaration (parser
);
7939 parser
->in_unbraced_linkage_specification_p
7940 = saved_in_unbraced_linkage_specification_p
;
7943 /* We're done with the linkage-specification. */
7944 pop_lang_context ();
7947 /* Parse a static_assert-declaration.
7949 static_assert-declaration:
7950 static_assert ( constant-expression , string-literal ) ;
7952 If MEMBER_P, this static_assert is a class member. */
7955 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
7960 location_t saved_loc
;
7962 /* Peek at the `static_assert' token so we can keep track of exactly
7963 where the static assertion started. */
7964 token
= cp_lexer_peek_token (parser
->lexer
);
7965 saved_loc
= token
->location
;
7967 /* Look for the `static_assert' keyword. */
7968 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
7972 /* We know we are in a static assertion; commit to any tentative
7974 if (cp_parser_parsing_tentatively (parser
))
7975 cp_parser_commit_to_tentative_parse (parser
);
7977 /* Parse the `(' starting the static assertion condition. */
7978 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7980 /* Parse the constant-expression. */
7982 cp_parser_constant_expression (parser
,
7983 /*allow_non_constant_p=*/false,
7984 /*non_constant_p=*/NULL
);
7986 /* Parse the separating `,'. */
7987 cp_parser_require (parser
, CPP_COMMA
, "`,'");
7989 /* Parse the string-literal message. */
7990 message
= cp_parser_string_literal (parser
,
7991 /*translate=*/false,
7994 /* A `)' completes the static assertion. */
7995 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
7996 cp_parser_skip_to_closing_parenthesis (parser
,
7997 /*recovering=*/true,
7999 /*consume_paren=*/true);
8001 /* A semicolon terminates the declaration. */
8002 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8004 /* Complete the static assertion, which may mean either processing
8005 the static assert now or saving it for template instantiation. */
8006 finish_static_assert (condition
, message
, saved_loc
, member_p
);
8009 /* Special member functions [gram.special] */
8011 /* Parse a conversion-function-id.
8013 conversion-function-id:
8014 operator conversion-type-id
8016 Returns an IDENTIFIER_NODE representing the operator. */
8019 cp_parser_conversion_function_id (cp_parser
* parser
)
8023 tree saved_qualifying_scope
;
8024 tree saved_object_scope
;
8025 tree pushed_scope
= NULL_TREE
;
8027 /* Look for the `operator' token. */
8028 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8029 return error_mark_node
;
8030 /* When we parse the conversion-type-id, the current scope will be
8031 reset. However, we need that information in able to look up the
8032 conversion function later, so we save it here. */
8033 saved_scope
= parser
->scope
;
8034 saved_qualifying_scope
= parser
->qualifying_scope
;
8035 saved_object_scope
= parser
->object_scope
;
8036 /* We must enter the scope of the class so that the names of
8037 entities declared within the class are available in the
8038 conversion-type-id. For example, consider:
8045 S::operator I() { ... }
8047 In order to see that `I' is a type-name in the definition, we
8048 must be in the scope of `S'. */
8050 pushed_scope
= push_scope (saved_scope
);
8051 /* Parse the conversion-type-id. */
8052 type
= cp_parser_conversion_type_id (parser
);
8053 /* Leave the scope of the class, if any. */
8055 pop_scope (pushed_scope
);
8056 /* Restore the saved scope. */
8057 parser
->scope
= saved_scope
;
8058 parser
->qualifying_scope
= saved_qualifying_scope
;
8059 parser
->object_scope
= saved_object_scope
;
8060 /* If the TYPE is invalid, indicate failure. */
8061 if (type
== error_mark_node
)
8062 return error_mark_node
;
8063 return mangle_conv_op_name_for_type (type
);
8066 /* Parse a conversion-type-id:
8069 type-specifier-seq conversion-declarator [opt]
8071 Returns the TYPE specified. */
8074 cp_parser_conversion_type_id (cp_parser
* parser
)
8077 cp_decl_specifier_seq type_specifiers
;
8078 cp_declarator
*declarator
;
8079 tree type_specified
;
8081 /* Parse the attributes. */
8082 attributes
= cp_parser_attributes_opt (parser
);
8083 /* Parse the type-specifiers. */
8084 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
8086 /* If that didn't work, stop. */
8087 if (type_specifiers
.type
== error_mark_node
)
8088 return error_mark_node
;
8089 /* Parse the conversion-declarator. */
8090 declarator
= cp_parser_conversion_declarator_opt (parser
);
8092 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
8093 /*initialized=*/0, &attributes
);
8095 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
8096 return type_specified
;
8099 /* Parse an (optional) conversion-declarator.
8101 conversion-declarator:
8102 ptr-operator conversion-declarator [opt]
8106 static cp_declarator
*
8107 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
8109 enum tree_code code
;
8111 cp_cv_quals cv_quals
;
8113 /* We don't know if there's a ptr-operator next, or not. */
8114 cp_parser_parse_tentatively (parser
);
8115 /* Try the ptr-operator. */
8116 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
8117 /* If it worked, look for more conversion-declarators. */
8118 if (cp_parser_parse_definitely (parser
))
8120 cp_declarator
*declarator
;
8122 /* Parse another optional declarator. */
8123 declarator
= cp_parser_conversion_declarator_opt (parser
);
8125 /* Create the representation of the declarator. */
8127 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
8129 else if (code
== INDIRECT_REF
)
8130 declarator
= make_pointer_declarator (cv_quals
, declarator
);
8132 declarator
= make_reference_declarator (cv_quals
, declarator
);
8140 /* Parse an (optional) ctor-initializer.
8143 : mem-initializer-list
8145 Returns TRUE iff the ctor-initializer was actually present. */
8148 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
8150 /* If the next token is not a `:', then there is no
8151 ctor-initializer. */
8152 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8154 /* Do default initialization of any bases and members. */
8155 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8156 finish_mem_initializers (NULL_TREE
);
8161 /* Consume the `:' token. */
8162 cp_lexer_consume_token (parser
->lexer
);
8163 /* And the mem-initializer-list. */
8164 cp_parser_mem_initializer_list (parser
);
8169 /* Parse a mem-initializer-list.
8171 mem-initializer-list:
8173 mem-initializer , mem-initializer-list */
8176 cp_parser_mem_initializer_list (cp_parser
* parser
)
8178 tree mem_initializer_list
= NULL_TREE
;
8180 /* Let the semantic analysis code know that we are starting the
8181 mem-initializer-list. */
8182 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
8183 error ("only constructors take base initializers");
8185 /* Loop through the list. */
8188 tree mem_initializer
;
8190 /* Parse the mem-initializer. */
8191 mem_initializer
= cp_parser_mem_initializer (parser
);
8192 /* Add it to the list, unless it was erroneous. */
8193 if (mem_initializer
!= error_mark_node
)
8195 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
8196 mem_initializer_list
= mem_initializer
;
8198 /* If the next token is not a `,', we're done. */
8199 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8201 /* Consume the `,' token. */
8202 cp_lexer_consume_token (parser
->lexer
);
8205 /* Perform semantic analysis. */
8206 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8207 finish_mem_initializers (mem_initializer_list
);
8210 /* Parse a mem-initializer.
8213 mem-initializer-id ( expression-list [opt] )
8218 ( expression-list [opt] )
8220 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8221 class) or FIELD_DECL (for a non-static data member) to initialize;
8222 the TREE_VALUE is the expression-list. An empty initialization
8223 list is represented by void_list_node. */
8226 cp_parser_mem_initializer (cp_parser
* parser
)
8228 tree mem_initializer_id
;
8229 tree expression_list
;
8232 /* Find out what is being initialized. */
8233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8235 pedwarn ("anachronistic old-style base class initializer");
8236 mem_initializer_id
= NULL_TREE
;
8239 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8240 member
= expand_member_init (mem_initializer_id
);
8241 if (member
&& !DECL_P (member
))
8242 in_base_initializer
= 1;
8245 = cp_parser_parenthesized_expression_list (parser
, false,
8247 /*non_constant_p=*/NULL
);
8248 if (expression_list
== error_mark_node
)
8249 return error_mark_node
;
8250 if (!expression_list
)
8251 expression_list
= void_type_node
;
8253 in_base_initializer
= 0;
8255 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8258 /* Parse a mem-initializer-id.
8261 :: [opt] nested-name-specifier [opt] class-name
8264 Returns a TYPE indicating the class to be initializer for the first
8265 production. Returns an IDENTIFIER_NODE indicating the data member
8266 to be initialized for the second production. */
8269 cp_parser_mem_initializer_id (cp_parser
* parser
)
8271 bool global_scope_p
;
8272 bool nested_name_specifier_p
;
8273 bool template_p
= false;
8276 /* `typename' is not allowed in this context ([temp.res]). */
8277 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8279 error ("keyword %<typename%> not allowed in this context (a qualified "
8280 "member initializer is implicitly a type)");
8281 cp_lexer_consume_token (parser
->lexer
);
8283 /* Look for the optional `::' operator. */
8285 = (cp_parser_global_scope_opt (parser
,
8286 /*current_scope_valid_p=*/false)
8288 /* Look for the optional nested-name-specifier. The simplest way to
8293 The keyword `typename' is not permitted in a base-specifier or
8294 mem-initializer; in these contexts a qualified name that
8295 depends on a template-parameter is implicitly assumed to be a
8298 is to assume that we have seen the `typename' keyword at this
8300 nested_name_specifier_p
8301 = (cp_parser_nested_name_specifier_opt (parser
,
8302 /*typename_keyword_p=*/true,
8303 /*check_dependency_p=*/true,
8305 /*is_declaration=*/true)
8307 if (nested_name_specifier_p
)
8308 template_p
= cp_parser_optional_template_keyword (parser
);
8309 /* If there is a `::' operator or a nested-name-specifier, then we
8310 are definitely looking for a class-name. */
8311 if (global_scope_p
|| nested_name_specifier_p
)
8312 return cp_parser_class_name (parser
,
8313 /*typename_keyword_p=*/true,
8314 /*template_keyword_p=*/template_p
,
8316 /*check_dependency_p=*/true,
8317 /*class_head_p=*/false,
8318 /*is_declaration=*/true);
8319 /* Otherwise, we could also be looking for an ordinary identifier. */
8320 cp_parser_parse_tentatively (parser
);
8321 /* Try a class-name. */
8322 id
= cp_parser_class_name (parser
,
8323 /*typename_keyword_p=*/true,
8324 /*template_keyword_p=*/false,
8326 /*check_dependency_p=*/true,
8327 /*class_head_p=*/false,
8328 /*is_declaration=*/true);
8329 /* If we found one, we're done. */
8330 if (cp_parser_parse_definitely (parser
))
8332 /* Otherwise, look for an ordinary identifier. */
8333 return cp_parser_identifier (parser
);
8336 /* Overloading [gram.over] */
8338 /* Parse an operator-function-id.
8340 operator-function-id:
8343 Returns an IDENTIFIER_NODE for the operator which is a
8344 human-readable spelling of the identifier, e.g., `operator +'. */
8347 cp_parser_operator_function_id (cp_parser
* parser
)
8349 /* Look for the `operator' keyword. */
8350 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8351 return error_mark_node
;
8352 /* And then the name of the operator itself. */
8353 return cp_parser_operator (parser
);
8356 /* Parse an operator.
8359 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8360 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8361 || ++ -- , ->* -> () []
8368 Returns an IDENTIFIER_NODE for the operator which is a
8369 human-readable spelling of the identifier, e.g., `operator +'. */
8372 cp_parser_operator (cp_parser
* parser
)
8374 tree id
= NULL_TREE
;
8377 /* Peek at the next token. */
8378 token
= cp_lexer_peek_token (parser
->lexer
);
8379 /* Figure out which operator we have. */
8380 switch (token
->type
)
8386 /* The keyword should be either `new' or `delete'. */
8387 if (token
->keyword
== RID_NEW
)
8389 else if (token
->keyword
== RID_DELETE
)
8394 /* Consume the `new' or `delete' token. */
8395 cp_lexer_consume_token (parser
->lexer
);
8397 /* Peek at the next token. */
8398 token
= cp_lexer_peek_token (parser
->lexer
);
8399 /* If it's a `[' token then this is the array variant of the
8401 if (token
->type
== CPP_OPEN_SQUARE
)
8403 /* Consume the `[' token. */
8404 cp_lexer_consume_token (parser
->lexer
);
8405 /* Look for the `]' token. */
8406 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8407 id
= ansi_opname (op
== NEW_EXPR
8408 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8410 /* Otherwise, we have the non-array variant. */
8412 id
= ansi_opname (op
);
8418 id
= ansi_opname (PLUS_EXPR
);
8422 id
= ansi_opname (MINUS_EXPR
);
8426 id
= ansi_opname (MULT_EXPR
);
8430 id
= ansi_opname (TRUNC_DIV_EXPR
);
8434 id
= ansi_opname (TRUNC_MOD_EXPR
);
8438 id
= ansi_opname (BIT_XOR_EXPR
);
8442 id
= ansi_opname (BIT_AND_EXPR
);
8446 id
= ansi_opname (BIT_IOR_EXPR
);
8450 id
= ansi_opname (BIT_NOT_EXPR
);
8454 id
= ansi_opname (TRUTH_NOT_EXPR
);
8458 id
= ansi_assopname (NOP_EXPR
);
8462 id
= ansi_opname (LT_EXPR
);
8466 id
= ansi_opname (GT_EXPR
);
8470 id
= ansi_assopname (PLUS_EXPR
);
8474 id
= ansi_assopname (MINUS_EXPR
);
8478 id
= ansi_assopname (MULT_EXPR
);
8482 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8486 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8490 id
= ansi_assopname (BIT_XOR_EXPR
);
8494 id
= ansi_assopname (BIT_AND_EXPR
);
8498 id
= ansi_assopname (BIT_IOR_EXPR
);
8502 id
= ansi_opname (LSHIFT_EXPR
);
8506 id
= ansi_opname (RSHIFT_EXPR
);
8510 id
= ansi_assopname (LSHIFT_EXPR
);
8514 id
= ansi_assopname (RSHIFT_EXPR
);
8518 id
= ansi_opname (EQ_EXPR
);
8522 id
= ansi_opname (NE_EXPR
);
8526 id
= ansi_opname (LE_EXPR
);
8529 case CPP_GREATER_EQ
:
8530 id
= ansi_opname (GE_EXPR
);
8534 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8538 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8542 id
= ansi_opname (POSTINCREMENT_EXPR
);
8545 case CPP_MINUS_MINUS
:
8546 id
= ansi_opname (PREDECREMENT_EXPR
);
8550 id
= ansi_opname (COMPOUND_EXPR
);
8553 case CPP_DEREF_STAR
:
8554 id
= ansi_opname (MEMBER_REF
);
8558 id
= ansi_opname (COMPONENT_REF
);
8561 case CPP_OPEN_PAREN
:
8562 /* Consume the `('. */
8563 cp_lexer_consume_token (parser
->lexer
);
8564 /* Look for the matching `)'. */
8565 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8566 return ansi_opname (CALL_EXPR
);
8568 case CPP_OPEN_SQUARE
:
8569 /* Consume the `['. */
8570 cp_lexer_consume_token (parser
->lexer
);
8571 /* Look for the matching `]'. */
8572 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8573 return ansi_opname (ARRAY_REF
);
8576 /* Anything else is an error. */
8580 /* If we have selected an identifier, we need to consume the
8583 cp_lexer_consume_token (parser
->lexer
);
8584 /* Otherwise, no valid operator name was present. */
8587 cp_parser_error (parser
, "expected operator");
8588 id
= error_mark_node
;
8594 /* Parse a template-declaration.
8596 template-declaration:
8597 export [opt] template < template-parameter-list > declaration
8599 If MEMBER_P is TRUE, this template-declaration occurs within a
8602 The grammar rule given by the standard isn't correct. What
8605 template-declaration:
8606 export [opt] template-parameter-list-seq
8607 decl-specifier-seq [opt] init-declarator [opt] ;
8608 export [opt] template-parameter-list-seq
8611 template-parameter-list-seq:
8612 template-parameter-list-seq [opt]
8613 template < template-parameter-list > */
8616 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8618 /* Check for `export'. */
8619 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8621 /* Consume the `export' token. */
8622 cp_lexer_consume_token (parser
->lexer
);
8623 /* Warn that we do not support `export'. */
8624 warning (0, "keyword %<export%> not implemented, and will be ignored");
8627 cp_parser_template_declaration_after_export (parser
, member_p
);
8630 /* Parse a template-parameter-list.
8632 template-parameter-list:
8634 template-parameter-list , template-parameter
8636 Returns a TREE_LIST. Each node represents a template parameter.
8637 The nodes are connected via their TREE_CHAINs. */
8640 cp_parser_template_parameter_list (cp_parser
* parser
)
8642 tree parameter_list
= NULL_TREE
;
8644 begin_template_parm_list ();
8651 /* Parse the template-parameter. */
8652 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8653 /* Add it to the list. */
8654 if (parameter
!= error_mark_node
)
8655 parameter_list
= process_template_parm (parameter_list
,
8660 tree err_parm
= build_tree_list (parameter
, parameter
);
8661 TREE_VALUE (err_parm
) = error_mark_node
;
8662 parameter_list
= chainon (parameter_list
, err_parm
);
8665 /* Peek at the next token. */
8666 token
= cp_lexer_peek_token (parser
->lexer
);
8667 /* If it's not a `,', we're done. */
8668 if (token
->type
!= CPP_COMMA
)
8670 /* Otherwise, consume the `,' token. */
8671 cp_lexer_consume_token (parser
->lexer
);
8674 return end_template_parm_list (parameter_list
);
8677 /* Parse a template-parameter.
8681 parameter-declaration
8683 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8684 the parameter. The TREE_PURPOSE is the default value, if any.
8685 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8686 iff this parameter is a non-type parameter. */
8689 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8692 cp_parameter_declarator
*parameter_declarator
;
8695 /* Assume it is a type parameter or a template parameter. */
8696 *is_non_type
= false;
8697 /* Peek at the next token. */
8698 token
= cp_lexer_peek_token (parser
->lexer
);
8699 /* If it is `class' or `template', we have a type-parameter. */
8700 if (token
->keyword
== RID_TEMPLATE
)
8701 return cp_parser_type_parameter (parser
);
8702 /* If it is `class' or `typename' we do not know yet whether it is a
8703 type parameter or a non-type parameter. Consider:
8705 template <typename T, typename T::X X> ...
8709 template <class C, class D*> ...
8711 Here, the first parameter is a type parameter, and the second is
8712 a non-type parameter. We can tell by looking at the token after
8713 the identifier -- if it is a `,', `=', or `>' then we have a type
8715 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8717 /* Peek at the token after `class' or `typename'. */
8718 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8719 /* If it's an identifier, skip it. */
8720 if (token
->type
== CPP_NAME
)
8721 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8722 /* Now, see if the token looks like the end of a template
8724 if (token
->type
== CPP_COMMA
8725 || token
->type
== CPP_EQ
8726 || token
->type
== CPP_GREATER
)
8727 return cp_parser_type_parameter (parser
);
8730 /* Otherwise, it is a non-type parameter.
8734 When parsing a default template-argument for a non-type
8735 template-parameter, the first non-nested `>' is taken as the end
8736 of the template parameter-list rather than a greater-than
8738 *is_non_type
= true;
8739 parameter_declarator
8740 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8741 /*parenthesized_p=*/NULL
);
8742 parm
= grokdeclarator (parameter_declarator
->declarator
,
8743 ¶meter_declarator
->decl_specifiers
,
8744 PARM
, /*initialized=*/0,
8746 if (parm
== error_mark_node
)
8747 return error_mark_node
;
8748 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8751 /* Parse a type-parameter.
8754 class identifier [opt]
8755 class identifier [opt] = type-id
8756 typename identifier [opt]
8757 typename identifier [opt] = type-id
8758 template < template-parameter-list > class identifier [opt]
8759 template < template-parameter-list > class identifier [opt]
8762 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8763 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8764 the declaration of the parameter. */
8767 cp_parser_type_parameter (cp_parser
* parser
)
8772 /* Look for a keyword to tell us what kind of parameter this is. */
8773 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8774 "`class', `typename', or `template'");
8776 return error_mark_node
;
8778 switch (token
->keyword
)
8784 tree default_argument
;
8786 /* If the next token is an identifier, then it names the
8788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8789 identifier
= cp_parser_identifier (parser
);
8791 identifier
= NULL_TREE
;
8793 /* Create the parameter. */
8794 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8796 /* If the next token is an `=', we have a default argument. */
8797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8799 /* Consume the `=' token. */
8800 cp_lexer_consume_token (parser
->lexer
);
8801 /* Parse the default-argument. */
8802 push_deferring_access_checks (dk_no_deferred
);
8803 default_argument
= cp_parser_type_id (parser
);
8804 pop_deferring_access_checks ();
8807 default_argument
= NULL_TREE
;
8809 /* Create the combined representation of the parameter and the
8810 default argument. */
8811 parameter
= build_tree_list (default_argument
, parameter
);
8817 tree parameter_list
;
8819 tree default_argument
;
8821 /* Look for the `<'. */
8822 cp_parser_require (parser
, CPP_LESS
, "`<'");
8823 /* Parse the template-parameter-list. */
8824 parameter_list
= cp_parser_template_parameter_list (parser
);
8825 /* Look for the `>'. */
8826 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8827 /* Look for the `class' keyword. */
8828 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8829 /* If the next token is an `=', then there is a
8830 default-argument. If the next token is a `>', we are at
8831 the end of the parameter-list. If the next token is a `,',
8832 then we are at the end of this parameter. */
8833 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8834 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8835 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8837 identifier
= cp_parser_identifier (parser
);
8838 /* Treat invalid names as if the parameter were nameless. */
8839 if (identifier
== error_mark_node
)
8840 identifier
= NULL_TREE
;
8843 identifier
= NULL_TREE
;
8845 /* Create the template parameter. */
8846 parameter
= finish_template_template_parm (class_type_node
,
8849 /* If the next token is an `=', then there is a
8850 default-argument. */
8851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8855 /* Consume the `='. */
8856 cp_lexer_consume_token (parser
->lexer
);
8857 /* Parse the id-expression. */
8858 push_deferring_access_checks (dk_no_deferred
);
8860 = cp_parser_id_expression (parser
,
8861 /*template_keyword_p=*/false,
8862 /*check_dependency_p=*/true,
8863 /*template_p=*/&is_template
,
8864 /*declarator_p=*/false,
8865 /*optional_p=*/false);
8866 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8867 /* If the id-expression was a template-id that refers to
8868 a template-class, we already have the declaration here,
8869 so no further lookup is needed. */
8872 /* Look up the name. */
8874 = cp_parser_lookup_name (parser
, default_argument
,
8876 /*is_template=*/is_template
,
8877 /*is_namespace=*/false,
8878 /*check_dependency=*/true,
8879 /*ambiguous_decls=*/NULL
);
8880 /* See if the default argument is valid. */
8882 = check_template_template_default_arg (default_argument
);
8883 pop_deferring_access_checks ();
8886 default_argument
= NULL_TREE
;
8888 /* Create the combined representation of the parameter and the
8889 default argument. */
8890 parameter
= build_tree_list (default_argument
, parameter
);
8902 /* Parse a template-id.
8905 template-name < template-argument-list [opt] >
8907 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8908 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8909 returned. Otherwise, if the template-name names a function, or set
8910 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8911 names a class, returns a TYPE_DECL for the specialization.
8913 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8914 uninstantiated templates. */
8917 cp_parser_template_id (cp_parser
*parser
,
8918 bool template_keyword_p
,
8919 bool check_dependency_p
,
8920 bool is_declaration
)
8926 cp_token_position start_of_id
= 0;
8927 deferred_access_check
*chk
;
8928 VEC (deferred_access_check
,gc
) *access_check
;
8929 cp_token
*next_token
, *next_token_2
;
8932 /* If the next token corresponds to a template-id, there is no need
8934 next_token
= cp_lexer_peek_token (parser
->lexer
);
8935 if (next_token
->type
== CPP_TEMPLATE_ID
)
8937 struct tree_check
*check_value
;
8939 /* Get the stored value. */
8940 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
8941 /* Perform any access checks that were deferred. */
8942 access_check
= check_value
->checks
;
8946 VEC_iterate (deferred_access_check
, access_check
, i
, chk
) ;
8949 perform_or_defer_access_check (chk
->binfo
,
8954 /* Return the stored value. */
8955 return check_value
->value
;
8958 /* Avoid performing name lookup if there is no possibility of
8959 finding a template-id. */
8960 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8961 || (next_token
->type
== CPP_NAME
8962 && !cp_parser_nth_token_starts_template_argument_list_p
8965 cp_parser_error (parser
, "expected template-id");
8966 return error_mark_node
;
8969 /* Remember where the template-id starts. */
8970 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8971 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8973 push_deferring_access_checks (dk_deferred
);
8975 /* Parse the template-name. */
8976 is_identifier
= false;
8977 template = cp_parser_template_name (parser
, template_keyword_p
,
8981 if (template == error_mark_node
|| is_identifier
)
8983 pop_deferring_access_checks ();
8987 /* If we find the sequence `[:' after a template-name, it's probably
8988 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8989 parse correctly the argument list. */
8990 next_token
= cp_lexer_peek_token (parser
->lexer
);
8991 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8992 if (next_token
->type
== CPP_OPEN_SQUARE
8993 && next_token
->flags
& DIGRAPH
8994 && next_token_2
->type
== CPP_COLON
8995 && !(next_token_2
->flags
& PREV_WHITE
))
8997 cp_parser_parse_tentatively (parser
);
8998 /* Change `:' into `::'. */
8999 next_token_2
->type
= CPP_SCOPE
;
9000 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9002 cp_lexer_consume_token (parser
->lexer
);
9003 /* Parse the arguments. */
9004 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9005 if (!cp_parser_parse_definitely (parser
))
9007 /* If we couldn't parse an argument list, then we revert our changes
9008 and return simply an error. Maybe this is not a template-id
9010 next_token_2
->type
= CPP_COLON
;
9011 cp_parser_error (parser
, "expected %<<%>");
9012 pop_deferring_access_checks ();
9013 return error_mark_node
;
9015 /* Otherwise, emit an error about the invalid digraph, but continue
9016 parsing because we got our argument list. */
9017 pedwarn ("%<<::%> cannot begin a template-argument list");
9018 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9019 "between %<<%> and %<::%>");
9020 if (!flag_permissive
)
9025 inform ("(if you use -fpermissive G++ will accept your code)");
9032 /* Look for the `<' that starts the template-argument-list. */
9033 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
9035 pop_deferring_access_checks ();
9036 return error_mark_node
;
9038 /* Parse the arguments. */
9039 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9042 /* Build a representation of the specialization. */
9043 if (TREE_CODE (template) == IDENTIFIER_NODE
)
9044 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
9045 else if (DECL_CLASS_TEMPLATE_P (template)
9046 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9048 bool entering_scope
;
9049 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9050 template (rather than some instantiation thereof) only if
9051 is not nested within some other construct. For example, in
9052 "template <typename T> void f(T) { A<T>::", A<T> is just an
9053 instantiation of A. */
9054 entering_scope
= (template_parm_scope_p ()
9055 && cp_lexer_next_token_is (parser
->lexer
,
9058 = finish_template_type (template, arguments
, entering_scope
);
9062 /* If it's not a class-template or a template-template, it should be
9063 a function-template. */
9064 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9065 || TREE_CODE (template) == OVERLOAD
9066 || BASELINK_P (template)));
9068 template_id
= lookup_template_function (template, arguments
);
9071 /* If parsing tentatively, replace the sequence of tokens that makes
9072 up the template-id with a CPP_TEMPLATE_ID token. That way,
9073 should we re-parse the token stream, we will not have to repeat
9074 the effort required to do the parse, nor will we issue duplicate
9075 error messages about problems during instantiation of the
9079 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
9081 /* Reset the contents of the START_OF_ID token. */
9082 token
->type
= CPP_TEMPLATE_ID
;
9083 /* Retrieve any deferred checks. Do not pop this access checks yet
9084 so the memory will not be reclaimed during token replacing below. */
9085 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
9086 token
->u
.tree_check_value
->value
= template_id
;
9087 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
9088 token
->keyword
= RID_MAX
;
9090 /* Purge all subsequent tokens. */
9091 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
9093 /* ??? Can we actually assume that, if template_id ==
9094 error_mark_node, we will have issued a diagnostic to the
9095 user, as opposed to simply marking the tentative parse as
9097 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
9098 error ("parse error in template argument list");
9101 pop_deferring_access_checks ();
9105 /* Parse a template-name.
9110 The standard should actually say:
9114 operator-function-id
9116 A defect report has been filed about this issue.
9118 A conversion-function-id cannot be a template name because they cannot
9119 be part of a template-id. In fact, looking at this code:
9123 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9124 It is impossible to call a templated conversion-function-id with an
9125 explicit argument list, since the only allowed template parameter is
9126 the type to which it is converting.
9128 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9129 `template' keyword, in a construction like:
9133 In that case `f' is taken to be a template-name, even though there
9134 is no way of knowing for sure.
9136 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9137 name refers to a set of overloaded functions, at least one of which
9138 is a template, or an IDENTIFIER_NODE with the name of the template,
9139 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9140 names are looked up inside uninstantiated templates. */
9143 cp_parser_template_name (cp_parser
* parser
,
9144 bool template_keyword_p
,
9145 bool check_dependency_p
,
9146 bool is_declaration
,
9147 bool *is_identifier
)
9153 /* If the next token is `operator', then we have either an
9154 operator-function-id or a conversion-function-id. */
9155 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
9157 /* We don't know whether we're looking at an
9158 operator-function-id or a conversion-function-id. */
9159 cp_parser_parse_tentatively (parser
);
9160 /* Try an operator-function-id. */
9161 identifier
= cp_parser_operator_function_id (parser
);
9162 /* If that didn't work, try a conversion-function-id. */
9163 if (!cp_parser_parse_definitely (parser
))
9165 cp_parser_error (parser
, "expected template-name");
9166 return error_mark_node
;
9169 /* Look for the identifier. */
9171 identifier
= cp_parser_identifier (parser
);
9173 /* If we didn't find an identifier, we don't have a template-id. */
9174 if (identifier
== error_mark_node
)
9175 return error_mark_node
;
9177 /* If the name immediately followed the `template' keyword, then it
9178 is a template-name. However, if the next token is not `<', then
9179 we do not treat it as a template-name, since it is not being used
9180 as part of a template-id. This enables us to handle constructs
9183 template <typename T> struct S { S(); };
9184 template <typename T> S<T>::S();
9186 correctly. We would treat `S' as a template -- if it were `S<T>'
9187 -- but we do not if there is no `<'. */
9189 if (processing_template_decl
9190 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
9192 /* In a declaration, in a dependent context, we pretend that the
9193 "template" keyword was present in order to improve error
9194 recovery. For example, given:
9196 template <typename T> void f(T::X<int>);
9198 we want to treat "X<int>" as a template-id. */
9200 && !template_keyword_p
9201 && parser
->scope
&& TYPE_P (parser
->scope
)
9202 && check_dependency_p
9203 && dependent_type_p (parser
->scope
)
9204 /* Do not do this for dtors (or ctors), since they never
9205 need the template keyword before their name. */
9206 && !constructor_name_p (identifier
, parser
->scope
))
9208 cp_token_position start
= 0;
9210 /* Explain what went wrong. */
9211 error ("non-template %qD used as template", identifier
);
9212 inform ("use %<%T::template %D%> to indicate that it is a template",
9213 parser
->scope
, identifier
);
9214 /* If parsing tentatively, find the location of the "<" token. */
9215 if (cp_parser_simulate_error (parser
))
9216 start
= cp_lexer_token_position (parser
->lexer
, true);
9217 /* Parse the template arguments so that we can issue error
9218 messages about them. */
9219 cp_lexer_consume_token (parser
->lexer
);
9220 cp_parser_enclosed_template_argument_list (parser
);
9221 /* Skip tokens until we find a good place from which to
9222 continue parsing. */
9223 cp_parser_skip_to_closing_parenthesis (parser
,
9224 /*recovering=*/true,
9226 /*consume_paren=*/false);
9227 /* If parsing tentatively, permanently remove the
9228 template argument list. That will prevent duplicate
9229 error messages from being issued about the missing
9230 "template" keyword. */
9232 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
9234 *is_identifier
= true;
9238 /* If the "template" keyword is present, then there is generally
9239 no point in doing name-lookup, so we just return IDENTIFIER.
9240 But, if the qualifying scope is non-dependent then we can
9241 (and must) do name-lookup normally. */
9242 if (template_keyword_p
9244 || (TYPE_P (parser
->scope
)
9245 && dependent_type_p (parser
->scope
))))
9249 /* Look up the name. */
9250 decl
= cp_parser_lookup_name (parser
, identifier
,
9252 /*is_template=*/false,
9253 /*is_namespace=*/false,
9255 /*ambiguous_decls=*/NULL
);
9256 decl
= maybe_get_template_decl_from_type_decl (decl
);
9258 /* If DECL is a template, then the name was a template-name. */
9259 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9263 tree fn
= NULL_TREE
;
9265 /* The standard does not explicitly indicate whether a name that
9266 names a set of overloaded declarations, some of which are
9267 templates, is a template-name. However, such a name should
9268 be a template-name; otherwise, there is no way to form a
9269 template-id for the overloaded templates. */
9270 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9271 if (TREE_CODE (fns
) == OVERLOAD
)
9272 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9273 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9278 /* The name does not name a template. */
9279 cp_parser_error (parser
, "expected template-name");
9280 return error_mark_node
;
9284 /* If DECL is dependent, and refers to a function, then just return
9285 its name; we will look it up again during template instantiation. */
9286 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9288 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9289 if (TYPE_P (scope
) && dependent_type_p (scope
))
9296 /* Parse a template-argument-list.
9298 template-argument-list:
9300 template-argument-list , template-argument
9302 Returns a TREE_VEC containing the arguments. */
9305 cp_parser_template_argument_list (cp_parser
* parser
)
9307 tree fixed_args
[10];
9308 unsigned n_args
= 0;
9309 unsigned alloced
= 10;
9310 tree
*arg_ary
= fixed_args
;
9312 bool saved_in_template_argument_list_p
;
9314 bool saved_non_ice_p
;
9316 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9317 parser
->in_template_argument_list_p
= true;
9318 /* Even if the template-id appears in an integral
9319 constant-expression, the contents of the argument list do
9321 saved_ice_p
= parser
->integral_constant_expression_p
;
9322 parser
->integral_constant_expression_p
= false;
9323 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9324 parser
->non_integral_constant_expression_p
= false;
9325 /* Parse the arguments. */
9331 /* Consume the comma. */
9332 cp_lexer_consume_token (parser
->lexer
);
9334 /* Parse the template-argument. */
9335 argument
= cp_parser_template_argument (parser
);
9336 if (n_args
== alloced
)
9340 if (arg_ary
== fixed_args
)
9342 arg_ary
= XNEWVEC (tree
, alloced
);
9343 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9346 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9348 arg_ary
[n_args
++] = argument
;
9350 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9352 vec
= make_tree_vec (n_args
);
9355 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9357 if (arg_ary
!= fixed_args
)
9359 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9360 parser
->integral_constant_expression_p
= saved_ice_p
;
9361 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9365 /* Parse a template-argument.
9368 assignment-expression
9372 The representation is that of an assignment-expression, type-id, or
9373 id-expression -- except that the qualified id-expression is
9374 evaluated, so that the value returned is either a DECL or an
9377 Although the standard says "assignment-expression", it forbids
9378 throw-expressions or assignments in the template argument.
9379 Therefore, we use "conditional-expression" instead. */
9382 cp_parser_template_argument (cp_parser
* parser
)
9387 bool maybe_type_id
= false;
9391 /* There's really no way to know what we're looking at, so we just
9392 try each alternative in order.
9396 In a template-argument, an ambiguity between a type-id and an
9397 expression is resolved to a type-id, regardless of the form of
9398 the corresponding template-parameter.
9400 Therefore, we try a type-id first. */
9401 cp_parser_parse_tentatively (parser
);
9402 argument
= cp_parser_type_id (parser
);
9403 /* If there was no error parsing the type-id but the next token is a '>>',
9404 we probably found a typo for '> >'. But there are type-id which are
9405 also valid expressions. For instance:
9407 struct X { int operator >> (int); };
9408 template <int V> struct Foo {};
9411 Here 'X()' is a valid type-id of a function type, but the user just
9412 wanted to write the expression "X() >> 5". Thus, we remember that we
9413 found a valid type-id, but we still try to parse the argument as an
9414 expression to see what happens. */
9415 if (!cp_parser_error_occurred (parser
)
9416 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9418 maybe_type_id
= true;
9419 cp_parser_abort_tentative_parse (parser
);
9423 /* If the next token isn't a `,' or a `>', then this argument wasn't
9424 really finished. This means that the argument is not a valid
9426 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9427 cp_parser_error (parser
, "expected template-argument");
9428 /* If that worked, we're done. */
9429 if (cp_parser_parse_definitely (parser
))
9432 /* We're still not sure what the argument will be. */
9433 cp_parser_parse_tentatively (parser
);
9434 /* Try a template. */
9435 argument
= cp_parser_id_expression (parser
,
9436 /*template_keyword_p=*/false,
9437 /*check_dependency_p=*/true,
9439 /*declarator_p=*/false,
9440 /*optional_p=*/false);
9441 /* If the next token isn't a `,' or a `>', then this argument wasn't
9443 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9444 cp_parser_error (parser
, "expected template-argument");
9445 if (!cp_parser_error_occurred (parser
))
9447 /* Figure out what is being referred to. If the id-expression
9448 was for a class template specialization, then we will have a
9449 TYPE_DECL at this point. There is no need to do name lookup
9450 at this point in that case. */
9451 if (TREE_CODE (argument
) != TYPE_DECL
)
9452 argument
= cp_parser_lookup_name (parser
, argument
,
9454 /*is_template=*/template_p
,
9455 /*is_namespace=*/false,
9456 /*check_dependency=*/true,
9457 /*ambiguous_decls=*/NULL
);
9458 if (TREE_CODE (argument
) != TEMPLATE_DECL
9459 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9460 cp_parser_error (parser
, "expected template-name");
9462 if (cp_parser_parse_definitely (parser
))
9464 /* It must be a non-type argument. There permitted cases are given
9465 in [temp.arg.nontype]:
9467 -- an integral constant-expression of integral or enumeration
9470 -- the name of a non-type template-parameter; or
9472 -- the name of an object or function with external linkage...
9474 -- the address of an object or function with external linkage...
9476 -- a pointer to member... */
9477 /* Look for a non-type template parameter. */
9478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9480 cp_parser_parse_tentatively (parser
);
9481 argument
= cp_parser_primary_expression (parser
,
9484 /*template_arg_p=*/true,
9486 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9487 || !cp_parser_next_token_ends_template_argument_p (parser
))
9488 cp_parser_simulate_error (parser
);
9489 if (cp_parser_parse_definitely (parser
))
9493 /* If the next token is "&", the argument must be the address of an
9494 object or function with external linkage. */
9495 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9497 cp_lexer_consume_token (parser
->lexer
);
9498 /* See if we might have an id-expression. */
9499 token
= cp_lexer_peek_token (parser
->lexer
);
9500 if (token
->type
== CPP_NAME
9501 || token
->keyword
== RID_OPERATOR
9502 || token
->type
== CPP_SCOPE
9503 || token
->type
== CPP_TEMPLATE_ID
9504 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9506 cp_parser_parse_tentatively (parser
);
9507 argument
= cp_parser_primary_expression (parser
,
9510 /*template_arg_p=*/true,
9512 if (cp_parser_error_occurred (parser
)
9513 || !cp_parser_next_token_ends_template_argument_p (parser
))
9514 cp_parser_abort_tentative_parse (parser
);
9517 if (TREE_CODE (argument
) == INDIRECT_REF
)
9519 gcc_assert (REFERENCE_REF_P (argument
));
9520 argument
= TREE_OPERAND (argument
, 0);
9523 if (TREE_CODE (argument
) == VAR_DECL
)
9525 /* A variable without external linkage might still be a
9526 valid constant-expression, so no error is issued here
9527 if the external-linkage check fails. */
9528 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
9529 cp_parser_simulate_error (parser
);
9531 else if (is_overloaded_fn (argument
))
9532 /* All overloaded functions are allowed; if the external
9533 linkage test does not pass, an error will be issued
9537 && (TREE_CODE (argument
) == OFFSET_REF
9538 || TREE_CODE (argument
) == SCOPE_REF
))
9539 /* A pointer-to-member. */
9541 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9544 cp_parser_simulate_error (parser
);
9546 if (cp_parser_parse_definitely (parser
))
9549 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9554 /* If the argument started with "&", there are no other valid
9555 alternatives at this point. */
9558 cp_parser_error (parser
, "invalid non-type template argument");
9559 return error_mark_node
;
9562 /* If the argument wasn't successfully parsed as a type-id followed
9563 by '>>', the argument can only be a constant expression now.
9564 Otherwise, we try parsing the constant-expression tentatively,
9565 because the argument could really be a type-id. */
9567 cp_parser_parse_tentatively (parser
);
9568 argument
= cp_parser_constant_expression (parser
,
9569 /*allow_non_constant_p=*/false,
9570 /*non_constant_p=*/NULL
);
9571 argument
= fold_non_dependent_expr (argument
);
9574 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9575 cp_parser_error (parser
, "expected template-argument");
9576 if (cp_parser_parse_definitely (parser
))
9578 /* We did our best to parse the argument as a non type-id, but that
9579 was the only alternative that matched (albeit with a '>' after
9580 it). We can assume it's just a typo from the user, and a
9581 diagnostic will then be issued. */
9582 return cp_parser_type_id (parser
);
9585 /* Parse an explicit-instantiation.
9587 explicit-instantiation:
9588 template declaration
9590 Although the standard says `declaration', what it really means is:
9592 explicit-instantiation:
9593 template decl-specifier-seq [opt] declarator [opt] ;
9595 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9596 supposed to be allowed. A defect report has been filed about this
9601 explicit-instantiation:
9602 storage-class-specifier template
9603 decl-specifier-seq [opt] declarator [opt] ;
9604 function-specifier template
9605 decl-specifier-seq [opt] declarator [opt] ; */
9608 cp_parser_explicit_instantiation (cp_parser
* parser
)
9610 int declares_class_or_enum
;
9611 cp_decl_specifier_seq decl_specifiers
;
9612 tree extension_specifier
= NULL_TREE
;
9614 /* Look for an (optional) storage-class-specifier or
9615 function-specifier. */
9616 if (cp_parser_allow_gnu_extensions_p (parser
))
9619 = cp_parser_storage_class_specifier_opt (parser
);
9620 if (!extension_specifier
)
9622 = cp_parser_function_specifier_opt (parser
,
9623 /*decl_specs=*/NULL
);
9626 /* Look for the `template' keyword. */
9627 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9628 /* Let the front end know that we are processing an explicit
9630 begin_explicit_instantiation ();
9631 /* [temp.explicit] says that we are supposed to ignore access
9632 control while processing explicit instantiation directives. */
9633 push_deferring_access_checks (dk_no_check
);
9634 /* Parse a decl-specifier-seq. */
9635 cp_parser_decl_specifier_seq (parser
,
9636 CP_PARSER_FLAGS_OPTIONAL
,
9638 &declares_class_or_enum
);
9639 /* If there was exactly one decl-specifier, and it declared a class,
9640 and there's no declarator, then we have an explicit type
9642 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9646 type
= check_tag_decl (&decl_specifiers
);
9647 /* Turn access control back on for names used during
9648 template instantiation. */
9649 pop_deferring_access_checks ();
9651 do_type_instantiation (type
, extension_specifier
,
9652 /*complain=*/tf_error
);
9656 cp_declarator
*declarator
;
9659 /* Parse the declarator. */
9661 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9662 /*ctor_dtor_or_conv_p=*/NULL
,
9663 /*parenthesized_p=*/NULL
,
9664 /*member_p=*/false);
9665 if (declares_class_or_enum
& 2)
9666 cp_parser_check_for_definition_in_return_type (declarator
,
9667 decl_specifiers
.type
);
9668 if (declarator
!= cp_error_declarator
)
9670 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9671 NORMAL
, 0, &decl_specifiers
.attributes
);
9672 /* Turn access control back on for names used during
9673 template instantiation. */
9674 pop_deferring_access_checks ();
9675 /* Do the explicit instantiation. */
9676 do_decl_instantiation (decl
, extension_specifier
);
9680 pop_deferring_access_checks ();
9681 /* Skip the body of the explicit instantiation. */
9682 cp_parser_skip_to_end_of_statement (parser
);
9685 /* We're done with the instantiation. */
9686 end_explicit_instantiation ();
9688 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9691 /* Parse an explicit-specialization.
9693 explicit-specialization:
9694 template < > declaration
9696 Although the standard says `declaration', what it really means is:
9698 explicit-specialization:
9699 template <> decl-specifier [opt] init-declarator [opt] ;
9700 template <> function-definition
9701 template <> explicit-specialization
9702 template <> template-declaration */
9705 cp_parser_explicit_specialization (cp_parser
* parser
)
9708 /* Look for the `template' keyword. */
9709 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9710 /* Look for the `<'. */
9711 cp_parser_require (parser
, CPP_LESS
, "`<'");
9712 /* Look for the `>'. */
9713 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9714 /* We have processed another parameter list. */
9715 ++parser
->num_template_parameter_lists
;
9718 A template ... explicit specialization ... shall not have C
9720 if (current_lang_name
== lang_name_c
)
9722 error ("template specialization with C linkage");
9723 /* Give it C++ linkage to avoid confusing other parts of the
9725 push_lang_context (lang_name_cplusplus
);
9726 need_lang_pop
= true;
9729 need_lang_pop
= false;
9730 /* Let the front end know that we are beginning a specialization. */
9731 if (!begin_specialization ())
9733 end_specialization ();
9734 cp_parser_skip_to_end_of_block_or_statement (parser
);
9738 /* If the next keyword is `template', we need to figure out whether
9739 or not we're looking a template-declaration. */
9740 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9742 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9743 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9744 cp_parser_template_declaration_after_export (parser
,
9745 /*member_p=*/false);
9747 cp_parser_explicit_specialization (parser
);
9750 /* Parse the dependent declaration. */
9751 cp_parser_single_declaration (parser
,
9755 /* We're done with the specialization. */
9756 end_specialization ();
9757 /* For the erroneous case of a template with C linkage, we pushed an
9758 implicit C++ linkage scope; exit that scope now. */
9760 pop_lang_context ();
9761 /* We're done with this parameter list. */
9762 --parser
->num_template_parameter_lists
;
9765 /* Parse a type-specifier.
9768 simple-type-specifier
9771 elaborated-type-specifier
9779 Returns a representation of the type-specifier. For a
9780 class-specifier, enum-specifier, or elaborated-type-specifier, a
9781 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9783 The parser flags FLAGS is used to control type-specifier parsing.
9785 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9786 in a decl-specifier-seq.
9788 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9789 class-specifier, enum-specifier, or elaborated-type-specifier, then
9790 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9791 if a type is declared; 2 if it is defined. Otherwise, it is set to
9794 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9795 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9799 cp_parser_type_specifier (cp_parser
* parser
,
9800 cp_parser_flags flags
,
9801 cp_decl_specifier_seq
*decl_specs
,
9802 bool is_declaration
,
9803 int* declares_class_or_enum
,
9804 bool* is_cv_qualifier
)
9806 tree type_spec
= NULL_TREE
;
9809 cp_decl_spec ds
= ds_last
;
9811 /* Assume this type-specifier does not declare a new type. */
9812 if (declares_class_or_enum
)
9813 *declares_class_or_enum
= 0;
9814 /* And that it does not specify a cv-qualifier. */
9815 if (is_cv_qualifier
)
9816 *is_cv_qualifier
= false;
9817 /* Peek at the next token. */
9818 token
= cp_lexer_peek_token (parser
->lexer
);
9820 /* If we're looking at a keyword, we can use that to guide the
9821 production we choose. */
9822 keyword
= token
->keyword
;
9826 /* Look for the enum-specifier. */
9827 type_spec
= cp_parser_enum_specifier (parser
);
9828 /* If that worked, we're done. */
9831 if (declares_class_or_enum
)
9832 *declares_class_or_enum
= 2;
9834 cp_parser_set_decl_spec_type (decl_specs
,
9836 /*user_defined_p=*/true);
9840 goto elaborated_type_specifier
;
9842 /* Any of these indicate either a class-specifier, or an
9843 elaborated-type-specifier. */
9847 /* Parse tentatively so that we can back up if we don't find a
9849 cp_parser_parse_tentatively (parser
);
9850 /* Look for the class-specifier. */
9851 type_spec
= cp_parser_class_specifier (parser
);
9852 /* If that worked, we're done. */
9853 if (cp_parser_parse_definitely (parser
))
9855 if (declares_class_or_enum
)
9856 *declares_class_or_enum
= 2;
9858 cp_parser_set_decl_spec_type (decl_specs
,
9860 /*user_defined_p=*/true);
9865 elaborated_type_specifier
:
9866 /* We're declaring (not defining) a class or enum. */
9867 if (declares_class_or_enum
)
9868 *declares_class_or_enum
= 1;
9872 /* Look for an elaborated-type-specifier. */
9874 = (cp_parser_elaborated_type_specifier
9876 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9879 cp_parser_set_decl_spec_type (decl_specs
,
9881 /*user_defined_p=*/true);
9886 if (is_cv_qualifier
)
9887 *is_cv_qualifier
= true;
9892 if (is_cv_qualifier
)
9893 *is_cv_qualifier
= true;
9898 if (is_cv_qualifier
)
9899 *is_cv_qualifier
= true;
9903 /* The `__complex__' keyword is a GNU extension. */
9911 /* Handle simple keywords. */
9916 ++decl_specs
->specs
[(int)ds
];
9917 decl_specs
->any_specifiers_p
= true;
9919 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9922 /* If we do not already have a type-specifier, assume we are looking
9923 at a simple-type-specifier. */
9924 type_spec
= cp_parser_simple_type_specifier (parser
,
9928 /* If we didn't find a type-specifier, and a type-specifier was not
9929 optional in this context, issue an error message. */
9930 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9932 cp_parser_error (parser
, "expected type specifier");
9933 return error_mark_node
;
9939 /* Parse a simple-type-specifier.
9941 simple-type-specifier:
9942 :: [opt] nested-name-specifier [opt] type-name
9943 :: [opt] nested-name-specifier template template-id
9958 simple-type-specifier:
9959 __typeof__ unary-expression
9960 __typeof__ ( type-id )
9962 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9963 appropriately updated. */
9966 cp_parser_simple_type_specifier (cp_parser
* parser
,
9967 cp_decl_specifier_seq
*decl_specs
,
9968 cp_parser_flags flags
)
9970 tree type
= NULL_TREE
;
9973 /* Peek at the next token. */
9974 token
= cp_lexer_peek_token (parser
->lexer
);
9976 /* If we're looking at a keyword, things are easy. */
9977 switch (token
->keyword
)
9981 decl_specs
->explicit_char_p
= true;
9982 type
= char_type_node
;
9985 type
= wchar_type_node
;
9988 type
= boolean_type_node
;
9992 ++decl_specs
->specs
[(int) ds_short
];
9993 type
= short_integer_type_node
;
9997 decl_specs
->explicit_int_p
= true;
9998 type
= integer_type_node
;
10002 ++decl_specs
->specs
[(int) ds_long
];
10003 type
= long_integer_type_node
;
10007 ++decl_specs
->specs
[(int) ds_signed
];
10008 type
= integer_type_node
;
10012 ++decl_specs
->specs
[(int) ds_unsigned
];
10013 type
= unsigned_type_node
;
10016 type
= float_type_node
;
10019 type
= double_type_node
;
10022 type
= void_type_node
;
10026 /* Consume the `typeof' token. */
10027 cp_lexer_consume_token (parser
->lexer
);
10028 /* Parse the operand to `typeof'. */
10029 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
10030 /* If it is not already a TYPE, take its type. */
10031 if (!TYPE_P (type
))
10032 type
= finish_typeof (type
);
10035 cp_parser_set_decl_spec_type (decl_specs
, type
,
10036 /*user_defined_p=*/true);
10044 /* If the type-specifier was for a built-in type, we're done. */
10049 /* Record the type. */
10051 && (token
->keyword
!= RID_SIGNED
10052 && token
->keyword
!= RID_UNSIGNED
10053 && token
->keyword
!= RID_SHORT
10054 && token
->keyword
!= RID_LONG
))
10055 cp_parser_set_decl_spec_type (decl_specs
,
10057 /*user_defined=*/false);
10059 decl_specs
->any_specifiers_p
= true;
10061 /* Consume the token. */
10062 id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10064 /* There is no valid C++ program where a non-template type is
10065 followed by a "<". That usually indicates that the user thought
10066 that the type was a template. */
10067 cp_parser_check_for_invalid_template_id (parser
, type
);
10069 return TYPE_NAME (type
);
10072 /* The type-specifier must be a user-defined type. */
10073 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
10078 /* Don't gobble tokens or issue error messages if this is an
10079 optional type-specifier. */
10080 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
10081 cp_parser_parse_tentatively (parser
);
10083 /* Look for the optional `::' operator. */
10085 = (cp_parser_global_scope_opt (parser
,
10086 /*current_scope_valid_p=*/false)
10088 /* Look for the nested-name specifier. */
10090 = (cp_parser_nested_name_specifier_opt (parser
,
10091 /*typename_keyword_p=*/false,
10092 /*check_dependency_p=*/true,
10094 /*is_declaration=*/false)
10096 /* If we have seen a nested-name-specifier, and the next token
10097 is `template', then we are using the template-id production. */
10099 && cp_parser_optional_template_keyword (parser
))
10101 /* Look for the template-id. */
10102 type
= cp_parser_template_id (parser
,
10103 /*template_keyword_p=*/true,
10104 /*check_dependency_p=*/true,
10105 /*is_declaration=*/false);
10106 /* If the template-id did not name a type, we are out of
10108 if (TREE_CODE (type
) != TYPE_DECL
)
10110 cp_parser_error (parser
, "expected template-id for type");
10114 /* Otherwise, look for a type-name. */
10116 type
= cp_parser_type_name (parser
);
10117 /* Keep track of all name-lookups performed in class scopes. */
10121 && TREE_CODE (type
) == TYPE_DECL
10122 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
10123 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
10124 /* If it didn't work out, we don't have a TYPE. */
10125 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
10126 && !cp_parser_parse_definitely (parser
))
10128 if (type
&& decl_specs
)
10129 cp_parser_set_decl_spec_type (decl_specs
, type
,
10130 /*user_defined=*/true);
10133 /* If we didn't get a type-name, issue an error message. */
10134 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10136 cp_parser_error (parser
, "expected type-name");
10137 return error_mark_node
;
10140 /* There is no valid C++ program where a non-template type is
10141 followed by a "<". That usually indicates that the user thought
10142 that the type was a template. */
10143 if (type
&& type
!= error_mark_node
)
10145 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10146 If it is, then the '<'...'>' enclose protocol names rather than
10147 template arguments, and so everything is fine. */
10148 if (c_dialect_objc ()
10149 && (objc_is_id (type
) || objc_is_class_name (type
)))
10151 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10152 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
10154 /* Clobber the "unqualified" type previously entered into
10155 DECL_SPECS with the new, improved protocol-qualified version. */
10157 decl_specs
->type
= qual_type
;
10162 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
10168 /* Parse a type-name.
10181 Returns a TYPE_DECL for the type. */
10184 cp_parser_type_name (cp_parser
* parser
)
10189 /* We can't know yet whether it is a class-name or not. */
10190 cp_parser_parse_tentatively (parser
);
10191 /* Try a class-name. */
10192 type_decl
= cp_parser_class_name (parser
,
10193 /*typename_keyword_p=*/false,
10194 /*template_keyword_p=*/false,
10196 /*check_dependency_p=*/true,
10197 /*class_head_p=*/false,
10198 /*is_declaration=*/false);
10199 /* If it's not a class-name, keep looking. */
10200 if (!cp_parser_parse_definitely (parser
))
10202 /* It must be a typedef-name or an enum-name. */
10203 identifier
= cp_parser_identifier (parser
);
10204 if (identifier
== error_mark_node
)
10205 return error_mark_node
;
10207 /* Look up the type-name. */
10208 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10210 if (TREE_CODE (type_decl
) != TYPE_DECL
10211 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
10213 /* See if this is an Objective-C type. */
10214 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10215 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
10217 type_decl
= TYPE_NAME (type
);
10220 /* Issue an error if we did not find a type-name. */
10221 if (TREE_CODE (type_decl
) != TYPE_DECL
)
10223 if (!cp_parser_simulate_error (parser
))
10224 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
10226 type_decl
= error_mark_node
;
10228 /* Remember that the name was used in the definition of the
10229 current class so that we can check later to see if the
10230 meaning would have been different after the class was
10231 entirely defined. */
10232 else if (type_decl
!= error_mark_node
10234 maybe_note_name_used_in_class (identifier
, type_decl
);
10241 /* Parse an elaborated-type-specifier. Note that the grammar given
10242 here incorporates the resolution to DR68.
10244 elaborated-type-specifier:
10245 class-key :: [opt] nested-name-specifier [opt] identifier
10246 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10247 enum :: [opt] nested-name-specifier [opt] identifier
10248 typename :: [opt] nested-name-specifier identifier
10249 typename :: [opt] nested-name-specifier template [opt]
10254 elaborated-type-specifier:
10255 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10256 class-key attributes :: [opt] nested-name-specifier [opt]
10257 template [opt] template-id
10258 enum attributes :: [opt] nested-name-specifier [opt] identifier
10260 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10261 declared `friend'. If IS_DECLARATION is TRUE, then this
10262 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10263 something is being declared.
10265 Returns the TYPE specified. */
10268 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10270 bool is_declaration
)
10272 enum tag_types tag_type
;
10274 tree type
= NULL_TREE
;
10275 tree attributes
= NULL_TREE
;
10277 /* See if we're looking at the `enum' keyword. */
10278 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10280 /* Consume the `enum' token. */
10281 cp_lexer_consume_token (parser
->lexer
);
10282 /* Remember that it's an enumeration type. */
10283 tag_type
= enum_type
;
10284 /* Parse the attributes. */
10285 attributes
= cp_parser_attributes_opt (parser
);
10287 /* Or, it might be `typename'. */
10288 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10291 /* Consume the `typename' token. */
10292 cp_lexer_consume_token (parser
->lexer
);
10293 /* Remember that it's a `typename' type. */
10294 tag_type
= typename_type
;
10295 /* The `typename' keyword is only allowed in templates. */
10296 if (!processing_template_decl
)
10297 pedwarn ("using %<typename%> outside of template");
10299 /* Otherwise it must be a class-key. */
10302 tag_type
= cp_parser_class_key (parser
);
10303 if (tag_type
== none_type
)
10304 return error_mark_node
;
10305 /* Parse the attributes. */
10306 attributes
= cp_parser_attributes_opt (parser
);
10309 /* Look for the `::' operator. */
10310 cp_parser_global_scope_opt (parser
,
10311 /*current_scope_valid_p=*/false);
10312 /* Look for the nested-name-specifier. */
10313 if (tag_type
== typename_type
)
10315 if (!cp_parser_nested_name_specifier (parser
,
10316 /*typename_keyword_p=*/true,
10317 /*check_dependency_p=*/true,
10320 return error_mark_node
;
10323 /* Even though `typename' is not present, the proposed resolution
10324 to Core Issue 180 says that in `class A<T>::B', `B' should be
10325 considered a type-name, even if `A<T>' is dependent. */
10326 cp_parser_nested_name_specifier_opt (parser
,
10327 /*typename_keyword_p=*/true,
10328 /*check_dependency_p=*/true,
10331 /* For everything but enumeration types, consider a template-id.
10332 For an enumeration type, consider only a plain identifier. */
10333 if (tag_type
!= enum_type
)
10335 bool template_p
= false;
10338 /* Allow the `template' keyword. */
10339 template_p
= cp_parser_optional_template_keyword (parser
);
10340 /* If we didn't see `template', we don't know if there's a
10341 template-id or not. */
10343 cp_parser_parse_tentatively (parser
);
10344 /* Parse the template-id. */
10345 decl
= cp_parser_template_id (parser
, template_p
,
10346 /*check_dependency_p=*/true,
10348 /* If we didn't find a template-id, look for an ordinary
10350 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10352 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10353 in effect, then we must assume that, upon instantiation, the
10354 template will correspond to a class. */
10355 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10356 && tag_type
== typename_type
)
10357 type
= make_typename_type (parser
->scope
, decl
,
10359 /*complain=*/tf_error
);
10361 type
= TREE_TYPE (decl
);
10366 identifier
= cp_parser_identifier (parser
);
10368 if (identifier
== error_mark_node
)
10370 parser
->scope
= NULL_TREE
;
10371 return error_mark_node
;
10374 /* For a `typename', we needn't call xref_tag. */
10375 if (tag_type
== typename_type
10376 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10377 return cp_parser_make_typename_type (parser
, parser
->scope
,
10379 /* Look up a qualified name in the usual way. */
10384 decl
= cp_parser_lookup_name (parser
, identifier
,
10386 /*is_template=*/false,
10387 /*is_namespace=*/false,
10388 /*check_dependency=*/true,
10389 /*ambiguous_decls=*/NULL
);
10391 /* If we are parsing friend declaration, DECL may be a
10392 TEMPLATE_DECL tree node here. However, we need to check
10393 whether this TEMPLATE_DECL results in valid code. Consider
10394 the following example:
10397 template <class T> class C {};
10400 template <class T> friend class N::C; // #1, valid code
10402 template <class T> class Y {
10403 friend class N::C; // #2, invalid code
10406 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10407 name lookup of `N::C'. We see that friend declaration must
10408 be template for the code to be valid. Note that
10409 processing_template_decl does not work here since it is
10410 always 1 for the above two cases. */
10412 decl
= (cp_parser_maybe_treat_template_as_class
10413 (decl
, /*tag_name_p=*/is_friend
10414 && parser
->num_template_parameter_lists
));
10416 if (TREE_CODE (decl
) != TYPE_DECL
)
10418 cp_parser_diagnose_invalid_type_name (parser
,
10421 return error_mark_node
;
10424 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10426 bool allow_template
= (parser
->num_template_parameter_lists
10427 || DECL_SELF_REFERENCE_P (decl
));
10428 type
= check_elaborated_type_specifier (tag_type
, decl
,
10431 if (type
== error_mark_node
)
10432 return error_mark_node
;
10435 type
= TREE_TYPE (decl
);
10439 /* An elaborated-type-specifier sometimes introduces a new type and
10440 sometimes names an existing type. Normally, the rule is that it
10441 introduces a new type only if there is not an existing type of
10442 the same name already in scope. For example, given:
10445 void f() { struct S s; }
10447 the `struct S' in the body of `f' is the same `struct S' as in
10448 the global scope; the existing definition is used. However, if
10449 there were no global declaration, this would introduce a new
10450 local class named `S'.
10452 An exception to this rule applies to the following code:
10454 namespace N { struct S; }
10456 Here, the elaborated-type-specifier names a new type
10457 unconditionally; even if there is already an `S' in the
10458 containing scope this declaration names a new type.
10459 This exception only applies if the elaborated-type-specifier
10460 forms the complete declaration:
10464 A declaration consisting solely of `class-key identifier ;' is
10465 either a redeclaration of the name in the current scope or a
10466 forward declaration of the identifier as a class name. It
10467 introduces the name into the current scope.
10469 We are in this situation precisely when the next token is a `;'.
10471 An exception to the exception is that a `friend' declaration does
10472 *not* name a new type; i.e., given:
10474 struct S { friend struct T; };
10476 `T' is not a new type in the scope of `S'.
10478 Also, `new struct S' or `sizeof (struct S)' never results in the
10479 definition of a new type; a new type can only be declared in a
10480 declaration context. */
10486 /* Friends have special name lookup rules. */
10487 ts
= ts_within_enclosing_non_class
;
10488 else if (is_declaration
10489 && cp_lexer_next_token_is (parser
->lexer
,
10491 /* This is a `class-key identifier ;' */
10497 (parser
->num_template_parameter_lists
10498 && (cp_parser_next_token_starts_class_definition_p (parser
)
10499 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10500 /* An unqualified name was used to reference this type, so
10501 there were no qualifying templates. */
10502 if (!cp_parser_check_template_parameters (parser
,
10503 /*num_templates=*/0))
10504 return error_mark_node
;
10505 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10509 if (type
== error_mark_node
)
10510 return error_mark_node
;
10512 /* Allow attributes on forward declarations of classes. */
10515 if (TREE_CODE (type
) == TYPENAME_TYPE
)
10516 warning (OPT_Wattributes
,
10517 "attributes ignored on uninstantiated type");
10518 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10519 && ! processing_explicit_instantiation
)
10520 warning (OPT_Wattributes
,
10521 "attributes ignored on template instantiation");
10522 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10523 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10525 warning (OPT_Wattributes
,
10526 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10529 if (tag_type
!= enum_type
)
10530 cp_parser_check_class_key (tag_type
, type
);
10532 /* A "<" cannot follow an elaborated type specifier. If that
10533 happens, the user was probably trying to form a template-id. */
10534 cp_parser_check_for_invalid_template_id (parser
, type
);
10539 /* Parse an enum-specifier.
10542 enum identifier [opt] { enumerator-list [opt] }
10545 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10548 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10549 if the token stream isn't an enum-specifier after all. */
10552 cp_parser_enum_specifier (cp_parser
* parser
)
10558 /* Parse tentatively so that we can back up if we don't find a
10560 cp_parser_parse_tentatively (parser
);
10562 /* Caller guarantees that the current token is 'enum', an identifier
10563 possibly follows, and the token after that is an opening brace.
10564 If we don't have an identifier, fabricate an anonymous name for
10565 the enumeration being defined. */
10566 cp_lexer_consume_token (parser
->lexer
);
10568 attributes
= cp_parser_attributes_opt (parser
);
10570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10571 identifier
= cp_parser_identifier (parser
);
10573 identifier
= make_anon_name ();
10575 /* Look for the `{' but don't consume it yet. */
10576 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10577 cp_parser_simulate_error (parser
);
10579 if (!cp_parser_parse_definitely (parser
))
10582 /* Issue an error message if type-definitions are forbidden here. */
10583 if (!cp_parser_check_type_definition (parser
))
10584 type
= error_mark_node
;
10586 /* Create the new type. We do this before consuming the opening
10587 brace so the enum will be recorded as being on the line of its
10588 tag (or the 'enum' keyword, if there is no tag). */
10589 type
= start_enum (identifier
);
10591 /* Consume the opening brace. */
10592 cp_lexer_consume_token (parser
->lexer
);
10594 if (type
== error_mark_node
)
10596 cp_parser_skip_to_end_of_block_or_statement (parser
);
10597 return error_mark_node
;
10600 /* If the next token is not '}', then there are some enumerators. */
10601 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10602 cp_parser_enumerator_list (parser
, type
);
10604 /* Consume the final '}'. */
10605 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10607 /* Look for trailing attributes to apply to this enumeration, and
10608 apply them if appropriate. */
10609 if (cp_parser_allow_gnu_extensions_p (parser
))
10611 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10612 cplus_decl_attributes (&type
,
10614 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10617 /* Finish up the enumeration. */
10618 finish_enum (type
);
10623 /* Parse an enumerator-list. The enumerators all have the indicated
10627 enumerator-definition
10628 enumerator-list , enumerator-definition */
10631 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10635 /* Parse an enumerator-definition. */
10636 cp_parser_enumerator_definition (parser
, type
);
10638 /* If the next token is not a ',', we've reached the end of
10640 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10642 /* Otherwise, consume the `,' and keep going. */
10643 cp_lexer_consume_token (parser
->lexer
);
10644 /* If the next token is a `}', there is a trailing comma. */
10645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10647 if (pedantic
&& !in_system_header
)
10648 pedwarn ("comma at end of enumerator list");
10654 /* Parse an enumerator-definition. The enumerator has the indicated
10657 enumerator-definition:
10659 enumerator = constant-expression
10665 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10670 /* Look for the identifier. */
10671 identifier
= cp_parser_identifier (parser
);
10672 if (identifier
== error_mark_node
)
10675 /* If the next token is an '=', then there is an explicit value. */
10676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10678 /* Consume the `=' token. */
10679 cp_lexer_consume_token (parser
->lexer
);
10680 /* Parse the value. */
10681 value
= cp_parser_constant_expression (parser
,
10682 /*allow_non_constant_p=*/false,
10688 /* Create the enumerator. */
10689 build_enumerator (identifier
, value
, type
);
10692 /* Parse a namespace-name.
10695 original-namespace-name
10698 Returns the NAMESPACE_DECL for the namespace. */
10701 cp_parser_namespace_name (cp_parser
* parser
)
10704 tree namespace_decl
;
10706 /* Get the name of the namespace. */
10707 identifier
= cp_parser_identifier (parser
);
10708 if (identifier
== error_mark_node
)
10709 return error_mark_node
;
10711 /* Look up the identifier in the currently active scope. Look only
10712 for namespaces, due to:
10714 [basic.lookup.udir]
10716 When looking up a namespace-name in a using-directive or alias
10717 definition, only namespace names are considered.
10721 [basic.lookup.qual]
10723 During the lookup of a name preceding the :: scope resolution
10724 operator, object, function, and enumerator names are ignored.
10726 (Note that cp_parser_class_or_namespace_name only calls this
10727 function if the token after the name is the scope resolution
10729 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10731 /*is_template=*/false,
10732 /*is_namespace=*/true,
10733 /*check_dependency=*/true,
10734 /*ambiguous_decls=*/NULL
);
10735 /* If it's not a namespace, issue an error. */
10736 if (namespace_decl
== error_mark_node
10737 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10740 error ("%qD is not a namespace-name", identifier
);
10741 cp_parser_error (parser
, "expected namespace-name");
10742 namespace_decl
= error_mark_node
;
10745 return namespace_decl
;
10748 /* Parse a namespace-definition.
10750 namespace-definition:
10751 named-namespace-definition
10752 unnamed-namespace-definition
10754 named-namespace-definition:
10755 original-namespace-definition
10756 extension-namespace-definition
10758 original-namespace-definition:
10759 namespace identifier { namespace-body }
10761 extension-namespace-definition:
10762 namespace original-namespace-name { namespace-body }
10764 unnamed-namespace-definition:
10765 namespace { namespace-body } */
10768 cp_parser_namespace_definition (cp_parser
* parser
)
10770 tree identifier
, attribs
;
10772 /* Look for the `namespace' keyword. */
10773 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10775 /* Get the name of the namespace. We do not attempt to distinguish
10776 between an original-namespace-definition and an
10777 extension-namespace-definition at this point. The semantic
10778 analysis routines are responsible for that. */
10779 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10780 identifier
= cp_parser_identifier (parser
);
10782 identifier
= NULL_TREE
;
10784 /* Parse any specified attributes. */
10785 attribs
= cp_parser_attributes_opt (parser
);
10787 /* Look for the `{' to start the namespace. */
10788 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10789 /* Start the namespace. */
10790 push_namespace_with_attribs (identifier
, attribs
);
10791 /* Parse the body of the namespace. */
10792 cp_parser_namespace_body (parser
);
10793 /* Finish the namespace. */
10795 /* Look for the final `}'. */
10796 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10799 /* Parse a namespace-body.
10802 declaration-seq [opt] */
10805 cp_parser_namespace_body (cp_parser
* parser
)
10807 cp_parser_declaration_seq_opt (parser
);
10810 /* Parse a namespace-alias-definition.
10812 namespace-alias-definition:
10813 namespace identifier = qualified-namespace-specifier ; */
10816 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10819 tree namespace_specifier
;
10821 /* Look for the `namespace' keyword. */
10822 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10823 /* Look for the identifier. */
10824 identifier
= cp_parser_identifier (parser
);
10825 if (identifier
== error_mark_node
)
10827 /* Look for the `=' token. */
10828 cp_parser_require (parser
, CPP_EQ
, "`='");
10829 /* Look for the qualified-namespace-specifier. */
10830 namespace_specifier
10831 = cp_parser_qualified_namespace_specifier (parser
);
10832 /* Look for the `;' token. */
10833 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10835 /* Register the alias in the symbol table. */
10836 do_namespace_alias (identifier
, namespace_specifier
);
10839 /* Parse a qualified-namespace-specifier.
10841 qualified-namespace-specifier:
10842 :: [opt] nested-name-specifier [opt] namespace-name
10844 Returns a NAMESPACE_DECL corresponding to the specified
10848 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10850 /* Look for the optional `::'. */
10851 cp_parser_global_scope_opt (parser
,
10852 /*current_scope_valid_p=*/false);
10854 /* Look for the optional nested-name-specifier. */
10855 cp_parser_nested_name_specifier_opt (parser
,
10856 /*typename_keyword_p=*/false,
10857 /*check_dependency_p=*/true,
10859 /*is_declaration=*/true);
10861 return cp_parser_namespace_name (parser
);
10864 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10865 access declaration.
10868 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10869 using :: unqualified-id ;
10871 access-declaration:
10877 cp_parser_using_declaration (cp_parser
* parser
,
10878 bool access_declaration_p
)
10881 bool typename_p
= false;
10882 bool global_scope_p
;
10887 if (access_declaration_p
)
10888 cp_parser_parse_tentatively (parser
);
10891 /* Look for the `using' keyword. */
10892 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10894 /* Peek at the next token. */
10895 token
= cp_lexer_peek_token (parser
->lexer
);
10896 /* See if it's `typename'. */
10897 if (token
->keyword
== RID_TYPENAME
)
10899 /* Remember that we've seen it. */
10901 /* Consume the `typename' token. */
10902 cp_lexer_consume_token (parser
->lexer
);
10906 /* Look for the optional global scope qualification. */
10908 = (cp_parser_global_scope_opt (parser
,
10909 /*current_scope_valid_p=*/false)
10912 /* If we saw `typename', or didn't see `::', then there must be a
10913 nested-name-specifier present. */
10914 if (typename_p
|| !global_scope_p
)
10915 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10916 /*check_dependency_p=*/true,
10918 /*is_declaration=*/true);
10919 /* Otherwise, we could be in either of the two productions. In that
10920 case, treat the nested-name-specifier as optional. */
10922 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10923 /*typename_keyword_p=*/false,
10924 /*check_dependency_p=*/true,
10926 /*is_declaration=*/true);
10928 qscope
= global_namespace
;
10930 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
10931 /* Something has already gone wrong; there's no need to parse
10932 further. Since an error has occurred, the return value of
10933 cp_parser_parse_definitely will be false, as required. */
10934 return cp_parser_parse_definitely (parser
);
10936 /* Parse the unqualified-id. */
10937 identifier
= cp_parser_unqualified_id (parser
,
10938 /*template_keyword_p=*/false,
10939 /*check_dependency_p=*/true,
10940 /*declarator_p=*/true,
10941 /*optional_p=*/false);
10943 if (access_declaration_p
)
10945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10946 cp_parser_simulate_error (parser
);
10947 if (!cp_parser_parse_definitely (parser
))
10951 /* The function we call to handle a using-declaration is different
10952 depending on what scope we are in. */
10953 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10955 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10956 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10957 /* [namespace.udecl]
10959 A using declaration shall not name a template-id. */
10960 error ("a template-id may not appear in a using-declaration");
10963 if (at_class_scope_p ())
10965 /* Create the USING_DECL. */
10966 decl
= do_class_using_decl (parser
->scope
, identifier
);
10967 /* Add it to the list of members in this class. */
10968 finish_member_declaration (decl
);
10972 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10973 if (decl
== error_mark_node
)
10974 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10975 else if (!at_namespace_scope_p ())
10976 do_local_using_decl (decl
, qscope
, identifier
);
10978 do_toplevel_using_decl (decl
, qscope
, identifier
);
10982 /* Look for the final `;'. */
10983 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10988 /* Parse a using-directive.
10991 using namespace :: [opt] nested-name-specifier [opt]
10992 namespace-name ; */
10995 cp_parser_using_directive (cp_parser
* parser
)
10997 tree namespace_decl
;
11000 /* Look for the `using' keyword. */
11001 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11002 /* And the `namespace' keyword. */
11003 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11004 /* Look for the optional `::' operator. */
11005 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
11006 /* And the optional nested-name-specifier. */
11007 cp_parser_nested_name_specifier_opt (parser
,
11008 /*typename_keyword_p=*/false,
11009 /*check_dependency_p=*/true,
11011 /*is_declaration=*/true);
11012 /* Get the namespace being used. */
11013 namespace_decl
= cp_parser_namespace_name (parser
);
11014 /* And any specified attributes. */
11015 attribs
= cp_parser_attributes_opt (parser
);
11016 /* Update the symbol table. */
11017 parse_using_directive (namespace_decl
, attribs
);
11018 /* Look for the final `;'. */
11019 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11022 /* Parse an asm-definition.
11025 asm ( string-literal ) ;
11030 asm volatile [opt] ( string-literal ) ;
11031 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11032 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11033 : asm-operand-list [opt] ) ;
11034 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11035 : asm-operand-list [opt]
11036 : asm-operand-list [opt] ) ; */
11039 cp_parser_asm_definition (cp_parser
* parser
)
11042 tree outputs
= NULL_TREE
;
11043 tree inputs
= NULL_TREE
;
11044 tree clobbers
= NULL_TREE
;
11046 bool volatile_p
= false;
11047 bool extended_p
= false;
11049 /* Look for the `asm' keyword. */
11050 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
11051 /* See if the next token is `volatile'. */
11052 if (cp_parser_allow_gnu_extensions_p (parser
)
11053 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
11055 /* Remember that we saw the `volatile' keyword. */
11057 /* Consume the token. */
11058 cp_lexer_consume_token (parser
->lexer
);
11060 /* Look for the opening `('. */
11061 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
11063 /* Look for the string. */
11064 string
= cp_parser_string_literal (parser
, false, false);
11065 if (string
== error_mark_node
)
11067 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11068 /*consume_paren=*/true);
11072 /* If we're allowing GNU extensions, check for the extended assembly
11073 syntax. Unfortunately, the `:' tokens need not be separated by
11074 a space in C, and so, for compatibility, we tolerate that here
11075 too. Doing that means that we have to treat the `::' operator as
11077 if (cp_parser_allow_gnu_extensions_p (parser
)
11078 && parser
->in_function_body
11079 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
11080 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
11082 bool inputs_p
= false;
11083 bool clobbers_p
= false;
11085 /* The extended syntax was used. */
11088 /* Look for outputs. */
11089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11091 /* Consume the `:'. */
11092 cp_lexer_consume_token (parser
->lexer
);
11093 /* Parse the output-operands. */
11094 if (cp_lexer_next_token_is_not (parser
->lexer
,
11096 && cp_lexer_next_token_is_not (parser
->lexer
,
11098 && cp_lexer_next_token_is_not (parser
->lexer
,
11100 outputs
= cp_parser_asm_operand_list (parser
);
11102 /* If the next token is `::', there are no outputs, and the
11103 next token is the beginning of the inputs. */
11104 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11105 /* The inputs are coming next. */
11108 /* Look for inputs. */
11110 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11112 /* Consume the `:' or `::'. */
11113 cp_lexer_consume_token (parser
->lexer
);
11114 /* Parse the output-operands. */
11115 if (cp_lexer_next_token_is_not (parser
->lexer
,
11117 && cp_lexer_next_token_is_not (parser
->lexer
,
11119 inputs
= cp_parser_asm_operand_list (parser
);
11121 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11122 /* The clobbers are coming next. */
11125 /* Look for clobbers. */
11127 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11129 /* Consume the `:' or `::'. */
11130 cp_lexer_consume_token (parser
->lexer
);
11131 /* Parse the clobbers. */
11132 if (cp_lexer_next_token_is_not (parser
->lexer
,
11134 clobbers
= cp_parser_asm_clobber_list (parser
);
11137 /* Look for the closing `)'. */
11138 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11139 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11140 /*consume_paren=*/true);
11141 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11143 /* Create the ASM_EXPR. */
11144 if (parser
->in_function_body
)
11146 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
11148 /* If the extended syntax was not used, mark the ASM_EXPR. */
11151 tree temp
= asm_stmt
;
11152 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
11153 temp
= TREE_OPERAND (temp
, 0);
11155 ASM_INPUT_P (temp
) = 1;
11159 cgraph_add_asm_node (string
);
11162 /* Declarators [gram.dcl.decl] */
11164 /* Parse an init-declarator.
11167 declarator initializer [opt]
11172 declarator asm-specification [opt] attributes [opt] initializer [opt]
11174 function-definition:
11175 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11177 decl-specifier-seq [opt] declarator function-try-block
11181 function-definition:
11182 __extension__ function-definition
11184 The DECL_SPECIFIERS apply to this declarator. Returns a
11185 representation of the entity declared. If MEMBER_P is TRUE, then
11186 this declarator appears in a class scope. The new DECL created by
11187 this declarator is returned.
11189 The CHECKS are access checks that should be performed once we know
11190 what entity is being declared (and, therefore, what classes have
11193 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11194 for a function-definition here as well. If the declarator is a
11195 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11196 be TRUE upon return. By that point, the function-definition will
11197 have been completely parsed.
11199 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11203 cp_parser_init_declarator (cp_parser
* parser
,
11204 cp_decl_specifier_seq
*decl_specifiers
,
11205 VEC (deferred_access_check
,gc
)* checks
,
11206 bool function_definition_allowed_p
,
11208 int declares_class_or_enum
,
11209 bool* function_definition_p
)
11212 cp_declarator
*declarator
;
11213 tree prefix_attributes
;
11215 tree asm_specification
;
11217 tree decl
= NULL_TREE
;
11219 bool is_initialized
;
11220 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11221 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11223 enum cpp_ttype initialization_kind
;
11224 bool is_parenthesized_init
= false;
11225 bool is_non_constant_init
;
11226 int ctor_dtor_or_conv_p
;
11228 tree pushed_scope
= NULL
;
11230 /* Gather the attributes that were provided with the
11231 decl-specifiers. */
11232 prefix_attributes
= decl_specifiers
->attributes
;
11234 /* Assume that this is not the declarator for a function
11236 if (function_definition_p
)
11237 *function_definition_p
= false;
11239 /* Defer access checks while parsing the declarator; we cannot know
11240 what names are accessible until we know what is being
11242 resume_deferring_access_checks ();
11244 /* Parse the declarator. */
11246 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11247 &ctor_dtor_or_conv_p
,
11248 /*parenthesized_p=*/NULL
,
11249 /*member_p=*/false);
11250 /* Gather up the deferred checks. */
11251 stop_deferring_access_checks ();
11253 /* If the DECLARATOR was erroneous, there's no need to go
11255 if (declarator
== cp_error_declarator
)
11256 return error_mark_node
;
11258 /* Check that the number of template-parameter-lists is OK. */
11259 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11260 return error_mark_node
;
11262 if (declares_class_or_enum
& 2)
11263 cp_parser_check_for_definition_in_return_type (declarator
,
11264 decl_specifiers
->type
);
11266 /* Figure out what scope the entity declared by the DECLARATOR is
11267 located in. `grokdeclarator' sometimes changes the scope, so
11268 we compute it now. */
11269 scope
= get_scope_of_declarator (declarator
);
11271 /* If we're allowing GNU extensions, look for an asm-specification
11273 if (cp_parser_allow_gnu_extensions_p (parser
))
11275 /* Look for an asm-specification. */
11276 asm_specification
= cp_parser_asm_specification_opt (parser
);
11277 /* And attributes. */
11278 attributes
= cp_parser_attributes_opt (parser
);
11282 asm_specification
= NULL_TREE
;
11283 attributes
= NULL_TREE
;
11286 /* Peek at the next token. */
11287 token
= cp_lexer_peek_token (parser
->lexer
);
11288 /* Check to see if the token indicates the start of a
11289 function-definition. */
11290 if (cp_parser_token_starts_function_definition_p (token
))
11292 if (!function_definition_allowed_p
)
11294 /* If a function-definition should not appear here, issue an
11296 cp_parser_error (parser
,
11297 "a function-definition is not allowed here");
11298 return error_mark_node
;
11302 /* Neither attributes nor an asm-specification are allowed
11303 on a function-definition. */
11304 if (asm_specification
)
11305 error ("an asm-specification is not allowed on a function-definition");
11307 error ("attributes are not allowed on a function-definition");
11308 /* This is a function-definition. */
11309 *function_definition_p
= true;
11311 /* Parse the function definition. */
11313 decl
= cp_parser_save_member_function_body (parser
,
11316 prefix_attributes
);
11319 = (cp_parser_function_definition_from_specifiers_and_declarator
11320 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11328 Only in function declarations for constructors, destructors, and
11329 type conversions can the decl-specifier-seq be omitted.
11331 We explicitly postpone this check past the point where we handle
11332 function-definitions because we tolerate function-definitions
11333 that are missing their return types in some modes. */
11334 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11336 cp_parser_error (parser
,
11337 "expected constructor, destructor, or type conversion");
11338 return error_mark_node
;
11341 /* An `=' or an `(' indicates an initializer. */
11342 if (token
->type
== CPP_EQ
11343 || token
->type
== CPP_OPEN_PAREN
)
11345 is_initialized
= true;
11346 initialization_kind
= token
->type
;
11350 /* If the init-declarator isn't initialized and isn't followed by a
11351 `,' or `;', it's not a valid init-declarator. */
11352 if (token
->type
!= CPP_COMMA
11353 && token
->type
!= CPP_SEMICOLON
)
11355 cp_parser_error (parser
, "expected initializer");
11356 return error_mark_node
;
11358 is_initialized
= false;
11359 initialization_kind
= CPP_EOF
;
11362 /* Because start_decl has side-effects, we should only call it if we
11363 know we're going ahead. By this point, we know that we cannot
11364 possibly be looking at any other construct. */
11365 cp_parser_commit_to_tentative_parse (parser
);
11367 /* If the decl specifiers were bad, issue an error now that we're
11368 sure this was intended to be a declarator. Then continue
11369 declaring the variable(s), as int, to try to cut down on further
11371 if (decl_specifiers
->any_specifiers_p
11372 && decl_specifiers
->type
== error_mark_node
)
11374 cp_parser_error (parser
, "invalid type in declaration");
11375 decl_specifiers
->type
= integer_type_node
;
11378 /* Check to see whether or not this declaration is a friend. */
11379 friend_p
= cp_parser_friend_p (decl_specifiers
);
11381 /* Enter the newly declared entry in the symbol table. If we're
11382 processing a declaration in a class-specifier, we wait until
11383 after processing the initializer. */
11386 if (parser
->in_unbraced_linkage_specification_p
)
11387 decl_specifiers
->storage_class
= sc_extern
;
11388 decl
= start_decl (declarator
, decl_specifiers
,
11389 is_initialized
, attributes
, prefix_attributes
,
11393 /* Enter the SCOPE. That way unqualified names appearing in the
11394 initializer will be looked up in SCOPE. */
11395 pushed_scope
= push_scope (scope
);
11397 /* Perform deferred access control checks, now that we know in which
11398 SCOPE the declared entity resides. */
11399 if (!member_p
&& decl
)
11401 tree saved_current_function_decl
= NULL_TREE
;
11403 /* If the entity being declared is a function, pretend that we
11404 are in its scope. If it is a `friend', it may have access to
11405 things that would not otherwise be accessible. */
11406 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11408 saved_current_function_decl
= current_function_decl
;
11409 current_function_decl
= decl
;
11412 /* Perform access checks for template parameters. */
11413 cp_parser_perform_template_parameter_access_checks (checks
);
11415 /* Perform the access control checks for the declarator and the
11416 the decl-specifiers. */
11417 perform_deferred_access_checks ();
11419 /* Restore the saved value. */
11420 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11421 current_function_decl
= saved_current_function_decl
;
11424 /* Parse the initializer. */
11425 initializer
= NULL_TREE
;
11426 is_parenthesized_init
= false;
11427 is_non_constant_init
= true;
11428 if (is_initialized
)
11430 if (function_declarator_p (declarator
))
11432 if (initialization_kind
== CPP_EQ
)
11433 initializer
= cp_parser_pure_specifier (parser
);
11436 /* If the declaration was erroneous, we don't really
11437 know what the user intended, so just silently
11438 consume the initializer. */
11439 if (decl
!= error_mark_node
)
11440 error ("initializer provided for function");
11441 cp_parser_skip_to_closing_parenthesis (parser
,
11442 /*recovering=*/true,
11443 /*or_comma=*/false,
11444 /*consume_paren=*/true);
11448 initializer
= cp_parser_initializer (parser
,
11449 &is_parenthesized_init
,
11450 &is_non_constant_init
);
11453 /* The old parser allows attributes to appear after a parenthesized
11454 initializer. Mark Mitchell proposed removing this functionality
11455 on the GCC mailing lists on 2002-08-13. This parser accepts the
11456 attributes -- but ignores them. */
11457 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11458 if (cp_parser_attributes_opt (parser
))
11459 warning (OPT_Wattributes
,
11460 "attributes after parenthesized initializer ignored");
11462 /* For an in-class declaration, use `grokfield' to create the
11468 pop_scope (pushed_scope
);
11469 pushed_scope
= false;
11471 decl
= grokfield (declarator
, decl_specifiers
,
11472 initializer
, !is_non_constant_init
,
11473 /*asmspec=*/NULL_TREE
,
11474 prefix_attributes
);
11475 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11476 cp_parser_save_default_args (parser
, decl
);
11479 /* Finish processing the declaration. But, skip friend
11481 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11483 cp_finish_decl (decl
,
11484 initializer
, !is_non_constant_init
,
11486 /* If the initializer is in parentheses, then this is
11487 a direct-initialization, which means that an
11488 `explicit' constructor is OK. Otherwise, an
11489 `explicit' constructor cannot be used. */
11490 ((is_parenthesized_init
|| !is_initialized
)
11491 ? 0 : LOOKUP_ONLYCONVERTING
));
11493 if (!friend_p
&& pushed_scope
)
11494 pop_scope (pushed_scope
);
11499 /* Parse a declarator.
11503 ptr-operator declarator
11505 abstract-declarator:
11506 ptr-operator abstract-declarator [opt]
11507 direct-abstract-declarator
11512 attributes [opt] direct-declarator
11513 attributes [opt] ptr-operator declarator
11515 abstract-declarator:
11516 attributes [opt] ptr-operator abstract-declarator [opt]
11517 attributes [opt] direct-abstract-declarator
11519 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11520 detect constructor, destructor or conversion operators. It is set
11521 to -1 if the declarator is a name, and +1 if it is a
11522 function. Otherwise it is set to zero. Usually you just want to
11523 test for >0, but internally the negative value is used.
11525 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11526 a decl-specifier-seq unless it declares a constructor, destructor,
11527 or conversion. It might seem that we could check this condition in
11528 semantic analysis, rather than parsing, but that makes it difficult
11529 to handle something like `f()'. We want to notice that there are
11530 no decl-specifiers, and therefore realize that this is an
11531 expression, not a declaration.)
11533 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11534 the declarator is a direct-declarator of the form "(...)".
11536 MEMBER_P is true iff this declarator is a member-declarator. */
11538 static cp_declarator
*
11539 cp_parser_declarator (cp_parser
* parser
,
11540 cp_parser_declarator_kind dcl_kind
,
11541 int* ctor_dtor_or_conv_p
,
11542 bool* parenthesized_p
,
11546 cp_declarator
*declarator
;
11547 enum tree_code code
;
11548 cp_cv_quals cv_quals
;
11550 tree attributes
= NULL_TREE
;
11552 /* Assume this is not a constructor, destructor, or type-conversion
11554 if (ctor_dtor_or_conv_p
)
11555 *ctor_dtor_or_conv_p
= 0;
11557 if (cp_parser_allow_gnu_extensions_p (parser
))
11558 attributes
= cp_parser_attributes_opt (parser
);
11560 /* Peek at the next token. */
11561 token
= cp_lexer_peek_token (parser
->lexer
);
11563 /* Check for the ptr-operator production. */
11564 cp_parser_parse_tentatively (parser
);
11565 /* Parse the ptr-operator. */
11566 code
= cp_parser_ptr_operator (parser
,
11569 /* If that worked, then we have a ptr-operator. */
11570 if (cp_parser_parse_definitely (parser
))
11572 /* If a ptr-operator was found, then this declarator was not
11574 if (parenthesized_p
)
11575 *parenthesized_p
= true;
11576 /* The dependent declarator is optional if we are parsing an
11577 abstract-declarator. */
11578 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11579 cp_parser_parse_tentatively (parser
);
11581 /* Parse the dependent declarator. */
11582 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11583 /*ctor_dtor_or_conv_p=*/NULL
,
11584 /*parenthesized_p=*/NULL
,
11585 /*member_p=*/false);
11587 /* If we are parsing an abstract-declarator, we must handle the
11588 case where the dependent declarator is absent. */
11589 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11590 && !cp_parser_parse_definitely (parser
))
11593 /* Build the representation of the ptr-operator. */
11595 declarator
= make_ptrmem_declarator (cv_quals
,
11598 else if (code
== INDIRECT_REF
)
11599 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11601 declarator
= make_reference_declarator (cv_quals
, declarator
);
11603 /* Everything else is a direct-declarator. */
11606 if (parenthesized_p
)
11607 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11609 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11610 ctor_dtor_or_conv_p
,
11614 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11615 declarator
->attributes
= attributes
;
11620 /* Parse a direct-declarator or direct-abstract-declarator.
11624 direct-declarator ( parameter-declaration-clause )
11625 cv-qualifier-seq [opt]
11626 exception-specification [opt]
11627 direct-declarator [ constant-expression [opt] ]
11630 direct-abstract-declarator:
11631 direct-abstract-declarator [opt]
11632 ( parameter-declaration-clause )
11633 cv-qualifier-seq [opt]
11634 exception-specification [opt]
11635 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11636 ( abstract-declarator )
11638 Returns a representation of the declarator. DCL_KIND is
11639 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11640 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11641 we are parsing a direct-declarator. It is
11642 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11643 of ambiguity we prefer an abstract declarator, as per
11644 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11645 cp_parser_declarator. */
11647 static cp_declarator
*
11648 cp_parser_direct_declarator (cp_parser
* parser
,
11649 cp_parser_declarator_kind dcl_kind
,
11650 int* ctor_dtor_or_conv_p
,
11654 cp_declarator
*declarator
= NULL
;
11655 tree scope
= NULL_TREE
;
11656 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11657 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11659 tree pushed_scope
= NULL_TREE
;
11663 /* Peek at the next token. */
11664 token
= cp_lexer_peek_token (parser
->lexer
);
11665 if (token
->type
== CPP_OPEN_PAREN
)
11667 /* This is either a parameter-declaration-clause, or a
11668 parenthesized declarator. When we know we are parsing a
11669 named declarator, it must be a parenthesized declarator
11670 if FIRST is true. For instance, `(int)' is a
11671 parameter-declaration-clause, with an omitted
11672 direct-abstract-declarator. But `((*))', is a
11673 parenthesized abstract declarator. Finally, when T is a
11674 template parameter `(T)' is a
11675 parameter-declaration-clause, and not a parenthesized
11678 We first try and parse a parameter-declaration-clause,
11679 and then try a nested declarator (if FIRST is true).
11681 It is not an error for it not to be a
11682 parameter-declaration-clause, even when FIRST is
11688 The first is the declaration of a function while the
11689 second is a the definition of a variable, including its
11692 Having seen only the parenthesis, we cannot know which of
11693 these two alternatives should be selected. Even more
11694 complex are examples like:
11699 The former is a function-declaration; the latter is a
11700 variable initialization.
11702 Thus again, we try a parameter-declaration-clause, and if
11703 that fails, we back out and return. */
11705 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11707 cp_parameter_declarator
*params
;
11708 unsigned saved_num_template_parameter_lists
;
11710 /* In a member-declarator, the only valid interpretation
11711 of a parenthesis is the start of a
11712 parameter-declaration-clause. (It is invalid to
11713 initialize a static data member with a parenthesized
11714 initializer; only the "=" form of initialization is
11717 cp_parser_parse_tentatively (parser
);
11719 /* Consume the `('. */
11720 cp_lexer_consume_token (parser
->lexer
);
11723 /* If this is going to be an abstract declarator, we're
11724 in a declarator and we can't have default args. */
11725 parser
->default_arg_ok_p
= false;
11726 parser
->in_declarator_p
= true;
11729 /* Inside the function parameter list, surrounding
11730 template-parameter-lists do not apply. */
11731 saved_num_template_parameter_lists
11732 = parser
->num_template_parameter_lists
;
11733 parser
->num_template_parameter_lists
= 0;
11735 /* Parse the parameter-declaration-clause. */
11736 params
= cp_parser_parameter_declaration_clause (parser
);
11738 parser
->num_template_parameter_lists
11739 = saved_num_template_parameter_lists
;
11741 /* If all went well, parse the cv-qualifier-seq and the
11742 exception-specification. */
11743 if (member_p
|| cp_parser_parse_definitely (parser
))
11745 cp_cv_quals cv_quals
;
11746 tree exception_specification
;
11748 if (ctor_dtor_or_conv_p
)
11749 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11751 /* Consume the `)'. */
11752 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11754 /* Parse the cv-qualifier-seq. */
11755 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11756 /* And the exception-specification. */
11757 exception_specification
11758 = cp_parser_exception_specification_opt (parser
);
11760 /* Create the function-declarator. */
11761 declarator
= make_call_declarator (declarator
,
11764 exception_specification
);
11765 /* Any subsequent parameter lists are to do with
11766 return type, so are not those of the declared
11768 parser
->default_arg_ok_p
= false;
11770 /* Repeat the main loop. */
11775 /* If this is the first, we can try a parenthesized
11779 bool saved_in_type_id_in_expr_p
;
11781 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11782 parser
->in_declarator_p
= saved_in_declarator_p
;
11784 /* Consume the `('. */
11785 cp_lexer_consume_token (parser
->lexer
);
11786 /* Parse the nested declarator. */
11787 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11788 parser
->in_type_id_in_expr_p
= true;
11790 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11791 /*parenthesized_p=*/NULL
,
11793 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11795 /* Expect a `)'. */
11796 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11797 declarator
= cp_error_declarator
;
11798 if (declarator
== cp_error_declarator
)
11801 goto handle_declarator
;
11803 /* Otherwise, we must be done. */
11807 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11808 && token
->type
== CPP_OPEN_SQUARE
)
11810 /* Parse an array-declarator. */
11813 if (ctor_dtor_or_conv_p
)
11814 *ctor_dtor_or_conv_p
= 0;
11817 parser
->default_arg_ok_p
= false;
11818 parser
->in_declarator_p
= true;
11819 /* Consume the `['. */
11820 cp_lexer_consume_token (parser
->lexer
);
11821 /* Peek at the next token. */
11822 token
= cp_lexer_peek_token (parser
->lexer
);
11823 /* If the next token is `]', then there is no
11824 constant-expression. */
11825 if (token
->type
!= CPP_CLOSE_SQUARE
)
11827 bool non_constant_p
;
11830 = cp_parser_constant_expression (parser
,
11831 /*allow_non_constant=*/true,
11833 if (!non_constant_p
)
11834 bounds
= fold_non_dependent_expr (bounds
);
11835 /* Normally, the array bound must be an integral constant
11836 expression. However, as an extension, we allow VLAs
11837 in function scopes. */
11838 else if (!parser
->in_function_body
)
11840 error ("array bound is not an integer constant");
11841 bounds
= error_mark_node
;
11845 bounds
= NULL_TREE
;
11846 /* Look for the closing `]'. */
11847 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11849 declarator
= cp_error_declarator
;
11853 declarator
= make_array_declarator (declarator
, bounds
);
11855 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11857 tree qualifying_scope
;
11858 tree unqualified_name
;
11859 special_function_kind sfk
;
11862 /* Parse a declarator-id */
11863 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11865 cp_parser_parse_tentatively (parser
);
11867 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11868 qualifying_scope
= parser
->scope
;
11871 if (!cp_parser_parse_definitely (parser
))
11872 unqualified_name
= error_mark_node
;
11873 else if (unqualified_name
11874 && (qualifying_scope
11875 || (TREE_CODE (unqualified_name
)
11876 != IDENTIFIER_NODE
)))
11878 cp_parser_error (parser
, "expected unqualified-id");
11879 unqualified_name
= error_mark_node
;
11883 if (!unqualified_name
)
11885 if (unqualified_name
== error_mark_node
)
11887 declarator
= cp_error_declarator
;
11891 if (qualifying_scope
&& at_namespace_scope_p ()
11892 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11894 /* In the declaration of a member of a template class
11895 outside of the class itself, the SCOPE will sometimes
11896 be a TYPENAME_TYPE. For example, given:
11898 template <typename T>
11899 int S<T>::R::i = 3;
11901 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11902 this context, we must resolve S<T>::R to an ordinary
11903 type, rather than a typename type.
11905 The reason we normally avoid resolving TYPENAME_TYPEs
11906 is that a specialization of `S' might render
11907 `S<T>::R' not a type. However, if `S' is
11908 specialized, then this `i' will not be used, so there
11909 is no harm in resolving the types here. */
11912 /* Resolve the TYPENAME_TYPE. */
11913 type
= resolve_typename_type (qualifying_scope
,
11914 /*only_current_p=*/false);
11915 /* If that failed, the declarator is invalid. */
11916 if (type
== error_mark_node
)
11917 error ("%<%T::%D%> is not a type",
11918 TYPE_CONTEXT (qualifying_scope
),
11919 TYPE_IDENTIFIER (qualifying_scope
));
11920 qualifying_scope
= type
;
11924 if (unqualified_name
)
11928 if (qualifying_scope
11929 && CLASS_TYPE_P (qualifying_scope
))
11930 class_type
= qualifying_scope
;
11932 class_type
= current_class_type
;
11934 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11936 tree name_type
= TREE_TYPE (unqualified_name
);
11937 if (class_type
&& same_type_p (name_type
, class_type
))
11939 if (qualifying_scope
11940 && CLASSTYPE_USE_TEMPLATE (name_type
))
11942 error ("invalid use of constructor as a template");
11943 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11944 "name the constructor in a qualified name",
11946 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11947 class_type
, name_type
);
11948 declarator
= cp_error_declarator
;
11952 unqualified_name
= constructor_name (class_type
);
11956 /* We do not attempt to print the declarator
11957 here because we do not have enough
11958 information about its original syntactic
11960 cp_parser_error (parser
, "invalid declarator");
11961 declarator
= cp_error_declarator
;
11968 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11969 sfk
= sfk_destructor
;
11970 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11971 sfk
= sfk_conversion
;
11972 else if (/* There's no way to declare a constructor
11973 for an anonymous type, even if the type
11974 got a name for linkage purposes. */
11975 !TYPE_WAS_ANONYMOUS (class_type
)
11976 && constructor_name_p (unqualified_name
,
11979 unqualified_name
= constructor_name (class_type
);
11980 sfk
= sfk_constructor
;
11983 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11984 *ctor_dtor_or_conv_p
= -1;
11987 declarator
= make_id_declarator (qualifying_scope
,
11990 declarator
->id_loc
= token
->location
;
11992 handle_declarator
:;
11993 scope
= get_scope_of_declarator (declarator
);
11995 /* Any names that appear after the declarator-id for a
11996 member are looked up in the containing scope. */
11997 pushed_scope
= push_scope (scope
);
11998 parser
->in_declarator_p
= true;
11999 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
12000 || (declarator
&& declarator
->kind
== cdk_id
))
12001 /* Default args are only allowed on function
12003 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12005 parser
->default_arg_ok_p
= false;
12014 /* For an abstract declarator, we might wind up with nothing at this
12015 point. That's an error; the declarator is not optional. */
12017 cp_parser_error (parser
, "expected declarator");
12019 /* If we entered a scope, we must exit it now. */
12021 pop_scope (pushed_scope
);
12023 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12024 parser
->in_declarator_p
= saved_in_declarator_p
;
12029 /* Parse a ptr-operator.
12032 * cv-qualifier-seq [opt]
12034 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12039 & cv-qualifier-seq [opt]
12041 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12042 Returns ADDR_EXPR if a reference was used. In the case of a
12043 pointer-to-member, *TYPE is filled in with the TYPE containing the
12044 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12045 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12046 ERROR_MARK if an error occurred. */
12048 static enum tree_code
12049 cp_parser_ptr_operator (cp_parser
* parser
,
12051 cp_cv_quals
*cv_quals
)
12053 enum tree_code code
= ERROR_MARK
;
12056 /* Assume that it's not a pointer-to-member. */
12058 /* And that there are no cv-qualifiers. */
12059 *cv_quals
= TYPE_UNQUALIFIED
;
12061 /* Peek at the next token. */
12062 token
= cp_lexer_peek_token (parser
->lexer
);
12063 /* If it's a `*' or `&' we have a pointer or reference. */
12064 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
12066 /* Remember which ptr-operator we were processing. */
12067 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
12069 /* Consume the `*' or `&'. */
12070 cp_lexer_consume_token (parser
->lexer
);
12072 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12073 `&', if we are allowing GNU extensions. (The only qualifier
12074 that can legally appear after `&' is `restrict', but that is
12075 enforced during semantic analysis. */
12076 if (code
== INDIRECT_REF
12077 || cp_parser_allow_gnu_extensions_p (parser
))
12078 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12082 /* Try the pointer-to-member case. */
12083 cp_parser_parse_tentatively (parser
);
12084 /* Look for the optional `::' operator. */
12085 cp_parser_global_scope_opt (parser
,
12086 /*current_scope_valid_p=*/false);
12087 /* Look for the nested-name specifier. */
12088 cp_parser_nested_name_specifier (parser
,
12089 /*typename_keyword_p=*/false,
12090 /*check_dependency_p=*/true,
12092 /*is_declaration=*/false);
12093 /* If we found it, and the next token is a `*', then we are
12094 indeed looking at a pointer-to-member operator. */
12095 if (!cp_parser_error_occurred (parser
)
12096 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
12098 /* Indicate that the `*' operator was used. */
12099 code
= INDIRECT_REF
;
12101 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
12102 error ("%qD is a namespace", parser
->scope
);
12105 /* The type of which the member is a member is given by the
12107 *type
= parser
->scope
;
12108 /* The next name will not be qualified. */
12109 parser
->scope
= NULL_TREE
;
12110 parser
->qualifying_scope
= NULL_TREE
;
12111 parser
->object_scope
= NULL_TREE
;
12112 /* Look for the optional cv-qualifier-seq. */
12113 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12116 /* If that didn't work we don't have a ptr-operator. */
12117 if (!cp_parser_parse_definitely (parser
))
12118 cp_parser_error (parser
, "expected ptr-operator");
12124 /* Parse an (optional) cv-qualifier-seq.
12127 cv-qualifier cv-qualifier-seq [opt]
12138 Returns a bitmask representing the cv-qualifiers. */
12141 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
12143 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
12148 cp_cv_quals cv_qualifier
;
12150 /* Peek at the next token. */
12151 token
= cp_lexer_peek_token (parser
->lexer
);
12152 /* See if it's a cv-qualifier. */
12153 switch (token
->keyword
)
12156 cv_qualifier
= TYPE_QUAL_CONST
;
12160 cv_qualifier
= TYPE_QUAL_VOLATILE
;
12164 cv_qualifier
= TYPE_QUAL_RESTRICT
;
12168 cv_qualifier
= TYPE_UNQUALIFIED
;
12175 if (cv_quals
& cv_qualifier
)
12177 error ("duplicate cv-qualifier");
12178 cp_lexer_purge_token (parser
->lexer
);
12182 cp_lexer_consume_token (parser
->lexer
);
12183 cv_quals
|= cv_qualifier
;
12190 /* Parse a declarator-id.
12194 :: [opt] nested-name-specifier [opt] type-name
12196 In the `id-expression' case, the value returned is as for
12197 cp_parser_id_expression if the id-expression was an unqualified-id.
12198 If the id-expression was a qualified-id, then a SCOPE_REF is
12199 returned. The first operand is the scope (either a NAMESPACE_DECL
12200 or TREE_TYPE), but the second is still just a representation of an
12204 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
12207 /* The expression must be an id-expression. Assume that qualified
12208 names are the names of types so that:
12211 int S<T>::R::i = 3;
12213 will work; we must treat `S<T>::R' as the name of a type.
12214 Similarly, assume that qualified names are templates, where
12218 int S<T>::R<T>::i = 3;
12221 id
= cp_parser_id_expression (parser
,
12222 /*template_keyword_p=*/false,
12223 /*check_dependency_p=*/false,
12224 /*template_p=*/NULL
,
12225 /*declarator_p=*/true,
12227 if (id
&& BASELINK_P (id
))
12228 id
= BASELINK_FUNCTIONS (id
);
12232 /* Parse a type-id.
12235 type-specifier-seq abstract-declarator [opt]
12237 Returns the TYPE specified. */
12240 cp_parser_type_id (cp_parser
* parser
)
12242 cp_decl_specifier_seq type_specifier_seq
;
12243 cp_declarator
*abstract_declarator
;
12245 /* Parse the type-specifier-seq. */
12246 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
12247 &type_specifier_seq
);
12248 if (type_specifier_seq
.type
== error_mark_node
)
12249 return error_mark_node
;
12251 /* There might or might not be an abstract declarator. */
12252 cp_parser_parse_tentatively (parser
);
12253 /* Look for the declarator. */
12254 abstract_declarator
12255 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
12256 /*parenthesized_p=*/NULL
,
12257 /*member_p=*/false);
12258 /* Check to see if there really was a declarator. */
12259 if (!cp_parser_parse_definitely (parser
))
12260 abstract_declarator
= NULL
;
12262 return groktypename (&type_specifier_seq
, abstract_declarator
);
12265 /* Parse a type-specifier-seq.
12267 type-specifier-seq:
12268 type-specifier type-specifier-seq [opt]
12272 type-specifier-seq:
12273 attributes type-specifier-seq [opt]
12275 If IS_CONDITION is true, we are at the start of a "condition",
12276 e.g., we've just seen "if (".
12278 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12281 cp_parser_type_specifier_seq (cp_parser
* parser
,
12283 cp_decl_specifier_seq
*type_specifier_seq
)
12285 bool seen_type_specifier
= false;
12286 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12288 /* Clear the TYPE_SPECIFIER_SEQ. */
12289 clear_decl_specs (type_specifier_seq
);
12291 /* Parse the type-specifiers and attributes. */
12294 tree type_specifier
;
12295 bool is_cv_qualifier
;
12297 /* Check for attributes first. */
12298 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12300 type_specifier_seq
->attributes
=
12301 chainon (type_specifier_seq
->attributes
,
12302 cp_parser_attributes_opt (parser
));
12306 /* Look for the type-specifier. */
12307 type_specifier
= cp_parser_type_specifier (parser
,
12309 type_specifier_seq
,
12310 /*is_declaration=*/false,
12313 if (!type_specifier
)
12315 /* If the first type-specifier could not be found, this is not a
12316 type-specifier-seq at all. */
12317 if (!seen_type_specifier
)
12319 cp_parser_error (parser
, "expected type-specifier");
12320 type_specifier_seq
->type
= error_mark_node
;
12323 /* If subsequent type-specifiers could not be found, the
12324 type-specifier-seq is complete. */
12328 seen_type_specifier
= true;
12329 /* The standard says that a condition can be:
12331 type-specifier-seq declarator = assignment-expression
12338 we should treat the "S" as a declarator, not as a
12339 type-specifier. The standard doesn't say that explicitly for
12340 type-specifier-seq, but it does say that for
12341 decl-specifier-seq in an ordinary declaration. Perhaps it
12342 would be clearer just to allow a decl-specifier-seq here, and
12343 then add a semantic restriction that if any decl-specifiers
12344 that are not type-specifiers appear, the program is invalid. */
12345 if (is_condition
&& !is_cv_qualifier
)
12346 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12349 cp_parser_check_decl_spec (type_specifier_seq
);
12352 /* Parse a parameter-declaration-clause.
12354 parameter-declaration-clause:
12355 parameter-declaration-list [opt] ... [opt]
12356 parameter-declaration-list , ...
12358 Returns a representation for the parameter declarations. A return
12359 value of NULL indicates a parameter-declaration-clause consisting
12360 only of an ellipsis. */
12362 static cp_parameter_declarator
*
12363 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12365 cp_parameter_declarator
*parameters
;
12370 /* Peek at the next token. */
12371 token
= cp_lexer_peek_token (parser
->lexer
);
12372 /* Check for trivial parameter-declaration-clauses. */
12373 if (token
->type
== CPP_ELLIPSIS
)
12375 /* Consume the `...' token. */
12376 cp_lexer_consume_token (parser
->lexer
);
12379 else if (token
->type
== CPP_CLOSE_PAREN
)
12380 /* There are no parameters. */
12382 #ifndef NO_IMPLICIT_EXTERN_C
12383 if (in_system_header
&& current_class_type
== NULL
12384 && current_lang_name
== lang_name_c
)
12388 return no_parameters
;
12390 /* Check for `(void)', too, which is a special case. */
12391 else if (token
->keyword
== RID_VOID
12392 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12393 == CPP_CLOSE_PAREN
))
12395 /* Consume the `void' token. */
12396 cp_lexer_consume_token (parser
->lexer
);
12397 /* There are no parameters. */
12398 return no_parameters
;
12401 /* Parse the parameter-declaration-list. */
12402 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12403 /* If a parse error occurred while parsing the
12404 parameter-declaration-list, then the entire
12405 parameter-declaration-clause is erroneous. */
12409 /* Peek at the next token. */
12410 token
= cp_lexer_peek_token (parser
->lexer
);
12411 /* If it's a `,', the clause should terminate with an ellipsis. */
12412 if (token
->type
== CPP_COMMA
)
12414 /* Consume the `,'. */
12415 cp_lexer_consume_token (parser
->lexer
);
12416 /* Expect an ellipsis. */
12418 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12420 /* It might also be `...' if the optional trailing `,' was
12422 else if (token
->type
== CPP_ELLIPSIS
)
12424 /* Consume the `...' token. */
12425 cp_lexer_consume_token (parser
->lexer
);
12426 /* And remember that we saw it. */
12430 ellipsis_p
= false;
12432 /* Finish the parameter list. */
12433 if (parameters
&& ellipsis_p
)
12434 parameters
->ellipsis_p
= true;
12439 /* Parse a parameter-declaration-list.
12441 parameter-declaration-list:
12442 parameter-declaration
12443 parameter-declaration-list , parameter-declaration
12445 Returns a representation of the parameter-declaration-list, as for
12446 cp_parser_parameter_declaration_clause. However, the
12447 `void_list_node' is never appended to the list. Upon return,
12448 *IS_ERROR will be true iff an error occurred. */
12450 static cp_parameter_declarator
*
12451 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12453 cp_parameter_declarator
*parameters
= NULL
;
12454 cp_parameter_declarator
**tail
= ¶meters
;
12455 bool saved_in_unbraced_linkage_specification_p
;
12457 /* Assume all will go well. */
12459 /* The special considerations that apply to a function within an
12460 unbraced linkage specifications do not apply to the parameters
12461 to the function. */
12462 saved_in_unbraced_linkage_specification_p
12463 = parser
->in_unbraced_linkage_specification_p
;
12464 parser
->in_unbraced_linkage_specification_p
= false;
12466 /* Look for more parameters. */
12469 cp_parameter_declarator
*parameter
;
12470 bool parenthesized_p
;
12471 /* Parse the parameter. */
12473 = cp_parser_parameter_declaration (parser
,
12474 /*template_parm_p=*/false,
12477 /* If a parse error occurred parsing the parameter declaration,
12478 then the entire parameter-declaration-list is erroneous. */
12485 /* Add the new parameter to the list. */
12487 tail
= ¶meter
->next
;
12489 /* Peek at the next token. */
12490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12491 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12492 /* These are for Objective-C++ */
12493 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12494 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12495 /* The parameter-declaration-list is complete. */
12497 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12501 /* Peek at the next token. */
12502 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12503 /* If it's an ellipsis, then the list is complete. */
12504 if (token
->type
== CPP_ELLIPSIS
)
12506 /* Otherwise, there must be more parameters. Consume the
12508 cp_lexer_consume_token (parser
->lexer
);
12509 /* When parsing something like:
12511 int i(float f, double d)
12513 we can tell after seeing the declaration for "f" that we
12514 are not looking at an initialization of a variable "i",
12515 but rather at the declaration of a function "i".
12517 Due to the fact that the parsing of template arguments
12518 (as specified to a template-id) requires backtracking we
12519 cannot use this technique when inside a template argument
12521 if (!parser
->in_template_argument_list_p
12522 && !parser
->in_type_id_in_expr_p
12523 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12524 /* However, a parameter-declaration of the form
12525 "foat(f)" (which is a valid declaration of a
12526 parameter "f") can also be interpreted as an
12527 expression (the conversion of "f" to "float"). */
12528 && !parenthesized_p
)
12529 cp_parser_commit_to_tentative_parse (parser
);
12533 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12534 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12535 cp_parser_skip_to_closing_parenthesis (parser
,
12536 /*recovering=*/true,
12537 /*or_comma=*/false,
12538 /*consume_paren=*/false);
12543 parser
->in_unbraced_linkage_specification_p
12544 = saved_in_unbraced_linkage_specification_p
;
12549 /* Parse a parameter declaration.
12551 parameter-declaration:
12552 decl-specifier-seq declarator
12553 decl-specifier-seq declarator = assignment-expression
12554 decl-specifier-seq abstract-declarator [opt]
12555 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12557 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12558 declares a template parameter. (In that case, a non-nested `>'
12559 token encountered during the parsing of the assignment-expression
12560 is not interpreted as a greater-than operator.)
12562 Returns a representation of the parameter, or NULL if an error
12563 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12564 true iff the declarator is of the form "(p)". */
12566 static cp_parameter_declarator
*
12567 cp_parser_parameter_declaration (cp_parser
*parser
,
12568 bool template_parm_p
,
12569 bool *parenthesized_p
)
12571 int declares_class_or_enum
;
12572 bool greater_than_is_operator_p
;
12573 cp_decl_specifier_seq decl_specifiers
;
12574 cp_declarator
*declarator
;
12575 tree default_argument
;
12577 const char *saved_message
;
12579 /* In a template parameter, `>' is not an operator.
12583 When parsing a default template-argument for a non-type
12584 template-parameter, the first non-nested `>' is taken as the end
12585 of the template parameter-list rather than a greater-than
12587 greater_than_is_operator_p
= !template_parm_p
;
12589 /* Type definitions may not appear in parameter types. */
12590 saved_message
= parser
->type_definition_forbidden_message
;
12591 parser
->type_definition_forbidden_message
12592 = "types may not be defined in parameter types";
12594 /* Parse the declaration-specifiers. */
12595 cp_parser_decl_specifier_seq (parser
,
12596 CP_PARSER_FLAGS_NONE
,
12598 &declares_class_or_enum
);
12599 /* If an error occurred, there's no reason to attempt to parse the
12600 rest of the declaration. */
12601 if (cp_parser_error_occurred (parser
))
12603 parser
->type_definition_forbidden_message
= saved_message
;
12607 /* Peek at the next token. */
12608 token
= cp_lexer_peek_token (parser
->lexer
);
12609 /* If the next token is a `)', `,', `=', `>', or `...', then there
12610 is no declarator. */
12611 if (token
->type
== CPP_CLOSE_PAREN
12612 || token
->type
== CPP_COMMA
12613 || token
->type
== CPP_EQ
12614 || token
->type
== CPP_ELLIPSIS
12615 || token
->type
== CPP_GREATER
)
12618 if (parenthesized_p
)
12619 *parenthesized_p
= false;
12621 /* Otherwise, there should be a declarator. */
12624 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12625 parser
->default_arg_ok_p
= false;
12627 /* After seeing a decl-specifier-seq, if the next token is not a
12628 "(", there is no possibility that the code is a valid
12629 expression. Therefore, if parsing tentatively, we commit at
12631 if (!parser
->in_template_argument_list_p
12632 /* In an expression context, having seen:
12636 we cannot be sure whether we are looking at a
12637 function-type (taking a "char" as a parameter) or a cast
12638 of some object of type "char" to "int". */
12639 && !parser
->in_type_id_in_expr_p
12640 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12641 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12642 cp_parser_commit_to_tentative_parse (parser
);
12643 /* Parse the declarator. */
12644 declarator
= cp_parser_declarator (parser
,
12645 CP_PARSER_DECLARATOR_EITHER
,
12646 /*ctor_dtor_or_conv_p=*/NULL
,
12648 /*member_p=*/false);
12649 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12650 /* After the declarator, allow more attributes. */
12651 decl_specifiers
.attributes
12652 = chainon (decl_specifiers
.attributes
,
12653 cp_parser_attributes_opt (parser
));
12656 /* The restriction on defining new types applies only to the type
12657 of the parameter, not to the default argument. */
12658 parser
->type_definition_forbidden_message
= saved_message
;
12660 /* If the next token is `=', then process a default argument. */
12661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12663 bool saved_greater_than_is_operator_p
;
12664 /* Consume the `='. */
12665 cp_lexer_consume_token (parser
->lexer
);
12667 /* If we are defining a class, then the tokens that make up the
12668 default argument must be saved and processed later. */
12669 if (!template_parm_p
&& at_class_scope_p ()
12670 && TYPE_BEING_DEFINED (current_class_type
))
12672 unsigned depth
= 0;
12673 cp_token
*first_token
;
12676 /* Add tokens until we have processed the entire default
12677 argument. We add the range [first_token, token). */
12678 first_token
= cp_lexer_peek_token (parser
->lexer
);
12683 /* Peek at the next token. */
12684 token
= cp_lexer_peek_token (parser
->lexer
);
12685 /* What we do depends on what token we have. */
12686 switch (token
->type
)
12688 /* In valid code, a default argument must be
12689 immediately followed by a `,' `)', or `...'. */
12691 case CPP_CLOSE_PAREN
:
12693 /* If we run into a non-nested `;', `}', or `]',
12694 then the code is invalid -- but the default
12695 argument is certainly over. */
12696 case CPP_SEMICOLON
:
12697 case CPP_CLOSE_BRACE
:
12698 case CPP_CLOSE_SQUARE
:
12701 /* Update DEPTH, if necessary. */
12702 else if (token
->type
== CPP_CLOSE_PAREN
12703 || token
->type
== CPP_CLOSE_BRACE
12704 || token
->type
== CPP_CLOSE_SQUARE
)
12708 case CPP_OPEN_PAREN
:
12709 case CPP_OPEN_SQUARE
:
12710 case CPP_OPEN_BRACE
:
12715 /* If we see a non-nested `>', and `>' is not an
12716 operator, then it marks the end of the default
12718 if (!depth
&& !greater_than_is_operator_p
)
12722 /* If we run out of tokens, issue an error message. */
12724 case CPP_PRAGMA_EOL
:
12725 error ("file ends in default argument");
12731 /* In these cases, we should look for template-ids.
12732 For example, if the default argument is
12733 `X<int, double>()', we need to do name lookup to
12734 figure out whether or not `X' is a template; if
12735 so, the `,' does not end the default argument.
12737 That is not yet done. */
12744 /* If we've reached the end, stop. */
12748 /* Add the token to the token block. */
12749 token
= cp_lexer_consume_token (parser
->lexer
);
12752 /* Create a DEFAULT_ARG to represented the unparsed default
12754 default_argument
= make_node (DEFAULT_ARG
);
12755 DEFARG_TOKENS (default_argument
)
12756 = cp_token_cache_new (first_token
, token
);
12757 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12759 /* Outside of a class definition, we can just parse the
12760 assignment-expression. */
12763 bool saved_local_variables_forbidden_p
;
12765 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12767 saved_greater_than_is_operator_p
12768 = parser
->greater_than_is_operator_p
;
12769 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12770 /* Local variable names (and the `this' keyword) may not
12771 appear in a default argument. */
12772 saved_local_variables_forbidden_p
12773 = parser
->local_variables_forbidden_p
;
12774 parser
->local_variables_forbidden_p
= true;
12775 /* The default argument expression may cause implicitly
12776 defined member functions to be synthesized, which will
12777 result in garbage collection. We must treat this
12778 situation as if we were within the body of function so as
12779 to avoid collecting live data on the stack. */
12781 /* Parse the assignment-expression. */
12782 if (template_parm_p
)
12783 push_deferring_access_checks (dk_no_deferred
);
12785 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12786 if (template_parm_p
)
12787 pop_deferring_access_checks ();
12788 /* Restore saved state. */
12790 parser
->greater_than_is_operator_p
12791 = saved_greater_than_is_operator_p
;
12792 parser
->local_variables_forbidden_p
12793 = saved_local_variables_forbidden_p
;
12795 if (!parser
->default_arg_ok_p
)
12797 if (!flag_pedantic_errors
)
12798 warning (0, "deprecated use of default argument for parameter of non-function");
12801 error ("default arguments are only permitted for function parameters");
12802 default_argument
= NULL_TREE
;
12807 default_argument
= NULL_TREE
;
12809 return make_parameter_declarator (&decl_specifiers
,
12814 /* Parse a function-body.
12817 compound_statement */
12820 cp_parser_function_body (cp_parser
*parser
)
12822 cp_parser_compound_statement (parser
, NULL
, false);
12825 /* Parse a ctor-initializer-opt followed by a function-body. Return
12826 true if a ctor-initializer was present. */
12829 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12832 bool ctor_initializer_p
;
12834 /* Begin the function body. */
12835 body
= begin_function_body ();
12836 /* Parse the optional ctor-initializer. */
12837 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12838 /* Parse the function-body. */
12839 cp_parser_function_body (parser
);
12840 /* Finish the function body. */
12841 finish_function_body (body
);
12843 return ctor_initializer_p
;
12846 /* Parse an initializer.
12849 = initializer-clause
12850 ( expression-list )
12852 Returns an expression representing the initializer. If no
12853 initializer is present, NULL_TREE is returned.
12855 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12856 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12857 set to FALSE if there is no initializer present. If there is an
12858 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12859 is set to true; otherwise it is set to false. */
12862 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12863 bool* non_constant_p
)
12868 /* Peek at the next token. */
12869 token
= cp_lexer_peek_token (parser
->lexer
);
12871 /* Let our caller know whether or not this initializer was
12873 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12874 /* Assume that the initializer is constant. */
12875 *non_constant_p
= false;
12877 if (token
->type
== CPP_EQ
)
12879 /* Consume the `='. */
12880 cp_lexer_consume_token (parser
->lexer
);
12881 /* Parse the initializer-clause. */
12882 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12884 else if (token
->type
== CPP_OPEN_PAREN
)
12885 init
= cp_parser_parenthesized_expression_list (parser
, false,
12890 /* Anything else is an error. */
12891 cp_parser_error (parser
, "expected initializer");
12892 init
= error_mark_node
;
12898 /* Parse an initializer-clause.
12900 initializer-clause:
12901 assignment-expression
12902 { initializer-list , [opt] }
12905 Returns an expression representing the initializer.
12907 If the `assignment-expression' production is used the value
12908 returned is simply a representation for the expression.
12910 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12911 the elements of the initializer-list (or NULL, if the last
12912 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12913 NULL_TREE. There is no way to detect whether or not the optional
12914 trailing `,' was provided. NON_CONSTANT_P is as for
12915 cp_parser_initializer. */
12918 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12922 /* Assume the expression is constant. */
12923 *non_constant_p
= false;
12925 /* If it is not a `{', then we are looking at an
12926 assignment-expression. */
12927 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12930 = cp_parser_constant_expression (parser
,
12931 /*allow_non_constant_p=*/true,
12933 if (!*non_constant_p
)
12934 initializer
= fold_non_dependent_expr (initializer
);
12938 /* Consume the `{' token. */
12939 cp_lexer_consume_token (parser
->lexer
);
12940 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12941 initializer
= make_node (CONSTRUCTOR
);
12942 /* If it's not a `}', then there is a non-trivial initializer. */
12943 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12945 /* Parse the initializer list. */
12946 CONSTRUCTOR_ELTS (initializer
)
12947 = cp_parser_initializer_list (parser
, non_constant_p
);
12948 /* A trailing `,' token is allowed. */
12949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12950 cp_lexer_consume_token (parser
->lexer
);
12952 /* Now, there should be a trailing `}'. */
12953 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12956 return initializer
;
12959 /* Parse an initializer-list.
12963 initializer-list , initializer-clause
12968 identifier : initializer-clause
12969 initializer-list, identifier : initializer-clause
12971 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12972 for the initializer. If the INDEX of the elt is non-NULL, it is the
12973 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12974 as for cp_parser_initializer. */
12976 static VEC(constructor_elt
,gc
) *
12977 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12979 VEC(constructor_elt
,gc
) *v
= NULL
;
12981 /* Assume all of the expressions are constant. */
12982 *non_constant_p
= false;
12984 /* Parse the rest of the list. */
12990 bool clause_non_constant_p
;
12992 /* If the next token is an identifier and the following one is a
12993 colon, we are looking at the GNU designated-initializer
12995 if (cp_parser_allow_gnu_extensions_p (parser
)
12996 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12997 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12999 /* Warn the user that they are using an extension. */
13001 pedwarn ("ISO C++ does not allow designated initializers");
13002 /* Consume the identifier. */
13003 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13004 /* Consume the `:'. */
13005 cp_lexer_consume_token (parser
->lexer
);
13008 identifier
= NULL_TREE
;
13010 /* Parse the initializer. */
13011 initializer
= cp_parser_initializer_clause (parser
,
13012 &clause_non_constant_p
);
13013 /* If any clause is non-constant, so is the entire initializer. */
13014 if (clause_non_constant_p
)
13015 *non_constant_p
= true;
13017 /* Add it to the vector. */
13018 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
13020 /* If the next token is not a comma, we have reached the end of
13022 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13025 /* Peek at the next token. */
13026 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13027 /* If the next token is a `}', then we're still done. An
13028 initializer-clause can have a trailing `,' after the
13029 initializer-list and before the closing `}'. */
13030 if (token
->type
== CPP_CLOSE_BRACE
)
13033 /* Consume the `,' token. */
13034 cp_lexer_consume_token (parser
->lexer
);
13040 /* Classes [gram.class] */
13042 /* Parse a class-name.
13048 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13049 to indicate that names looked up in dependent types should be
13050 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13051 keyword has been used to indicate that the name that appears next
13052 is a template. TAG_TYPE indicates the explicit tag given before
13053 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13054 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13055 is the class being defined in a class-head.
13057 Returns the TYPE_DECL representing the class. */
13060 cp_parser_class_name (cp_parser
*parser
,
13061 bool typename_keyword_p
,
13062 bool template_keyword_p
,
13063 enum tag_types tag_type
,
13064 bool check_dependency_p
,
13066 bool is_declaration
)
13073 /* All class-names start with an identifier. */
13074 token
= cp_lexer_peek_token (parser
->lexer
);
13075 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
13077 cp_parser_error (parser
, "expected class-name");
13078 return error_mark_node
;
13081 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13082 to a template-id, so we save it here. */
13083 scope
= parser
->scope
;
13084 if (scope
== error_mark_node
)
13085 return error_mark_node
;
13087 /* Any name names a type if we're following the `typename' keyword
13088 in a qualified name where the enclosing scope is type-dependent. */
13089 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
13090 && dependent_type_p (scope
));
13091 /* Handle the common case (an identifier, but not a template-id)
13093 if (token
->type
== CPP_NAME
13094 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
13096 cp_token
*identifier_token
;
13100 /* Look for the identifier. */
13101 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
13102 ambiguous_p
= identifier_token
->ambiguous_p
;
13103 identifier
= cp_parser_identifier (parser
);
13104 /* If the next token isn't an identifier, we are certainly not
13105 looking at a class-name. */
13106 if (identifier
== error_mark_node
)
13107 decl
= error_mark_node
;
13108 /* If we know this is a type-name, there's no need to look it
13110 else if (typename_p
)
13114 tree ambiguous_decls
;
13115 /* If we already know that this lookup is ambiguous, then
13116 we've already issued an error message; there's no reason
13120 cp_parser_simulate_error (parser
);
13121 return error_mark_node
;
13123 /* If the next token is a `::', then the name must be a type
13126 [basic.lookup.qual]
13128 During the lookup for a name preceding the :: scope
13129 resolution operator, object, function, and enumerator
13130 names are ignored. */
13131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13132 tag_type
= typename_type
;
13133 /* Look up the name. */
13134 decl
= cp_parser_lookup_name (parser
, identifier
,
13136 /*is_template=*/false,
13137 /*is_namespace=*/false,
13138 check_dependency_p
,
13140 if (ambiguous_decls
)
13142 error ("reference to %qD is ambiguous", identifier
);
13143 print_candidates (ambiguous_decls
);
13144 if (cp_parser_parsing_tentatively (parser
))
13146 identifier_token
->ambiguous_p
= true;
13147 cp_parser_simulate_error (parser
);
13149 return error_mark_node
;
13155 /* Try a template-id. */
13156 decl
= cp_parser_template_id (parser
, template_keyword_p
,
13157 check_dependency_p
,
13159 if (decl
== error_mark_node
)
13160 return error_mark_node
;
13163 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
13165 /* If this is a typename, create a TYPENAME_TYPE. */
13166 if (typename_p
&& decl
!= error_mark_node
)
13168 decl
= make_typename_type (scope
, decl
, typename_type
,
13169 /*complain=*/tf_error
);
13170 if (decl
!= error_mark_node
)
13171 decl
= TYPE_NAME (decl
);
13174 /* Check to see that it is really the name of a class. */
13175 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
13176 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
13177 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13178 /* Situations like this:
13180 template <typename T> struct A {
13181 typename T::template X<int>::I i;
13184 are problematic. Is `T::template X<int>' a class-name? The
13185 standard does not seem to be definitive, but there is no other
13186 valid interpretation of the following `::'. Therefore, those
13187 names are considered class-names. */
13189 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
13190 if (decl
!= error_mark_node
)
13191 decl
= TYPE_NAME (decl
);
13193 else if (TREE_CODE (decl
) != TYPE_DECL
13194 || TREE_TYPE (decl
) == error_mark_node
13195 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
13196 decl
= error_mark_node
;
13198 if (decl
== error_mark_node
)
13199 cp_parser_error (parser
, "expected class-name");
13204 /* Parse a class-specifier.
13207 class-head { member-specification [opt] }
13209 Returns the TREE_TYPE representing the class. */
13212 cp_parser_class_specifier (cp_parser
* parser
)
13216 tree attributes
= NULL_TREE
;
13217 int has_trailing_semicolon
;
13218 bool nested_name_specifier_p
;
13219 unsigned saved_num_template_parameter_lists
;
13220 bool saved_in_function_body
;
13221 tree old_scope
= NULL_TREE
;
13222 tree scope
= NULL_TREE
;
13225 push_deferring_access_checks (dk_no_deferred
);
13227 /* Parse the class-head. */
13228 type
= cp_parser_class_head (parser
,
13229 &nested_name_specifier_p
,
13232 /* If the class-head was a semantic disaster, skip the entire body
13236 cp_parser_skip_to_end_of_block_or_statement (parser
);
13237 pop_deferring_access_checks ();
13238 return error_mark_node
;
13241 /* Look for the `{'. */
13242 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
13244 pop_deferring_access_checks ();
13245 return error_mark_node
;
13248 /* Process the base classes. If they're invalid, skip the
13249 entire class body. */
13250 if (!xref_basetypes (type
, bases
))
13252 cp_parser_skip_to_closing_brace (parser
);
13254 /* Consuming the closing brace yields better error messages
13256 cp_lexer_consume_token (parser
->lexer
);
13257 pop_deferring_access_checks ();
13258 return error_mark_node
;
13261 /* Issue an error message if type-definitions are forbidden here. */
13262 cp_parser_check_type_definition (parser
);
13263 /* Remember that we are defining one more class. */
13264 ++parser
->num_classes_being_defined
;
13265 /* Inside the class, surrounding template-parameter-lists do not
13267 saved_num_template_parameter_lists
13268 = parser
->num_template_parameter_lists
;
13269 parser
->num_template_parameter_lists
= 0;
13270 /* We are not in a function body. */
13271 saved_in_function_body
= parser
->in_function_body
;
13272 parser
->in_function_body
= false;
13274 /* Start the class. */
13275 if (nested_name_specifier_p
)
13277 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
13278 old_scope
= push_inner_scope (scope
);
13280 type
= begin_class_definition (type
, attributes
);
13282 if (type
== error_mark_node
)
13283 /* If the type is erroneous, skip the entire body of the class. */
13284 cp_parser_skip_to_closing_brace (parser
);
13286 /* Parse the member-specification. */
13287 cp_parser_member_specification_opt (parser
);
13289 /* Look for the trailing `}'. */
13290 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13291 /* We get better error messages by noticing a common problem: a
13292 missing trailing `;'. */
13293 token
= cp_lexer_peek_token (parser
->lexer
);
13294 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
13295 /* Look for trailing attributes to apply to this class. */
13296 if (cp_parser_allow_gnu_extensions_p (parser
))
13297 attributes
= cp_parser_attributes_opt (parser
);
13298 if (type
!= error_mark_node
)
13299 type
= finish_struct (type
, attributes
);
13300 if (nested_name_specifier_p
)
13301 pop_inner_scope (old_scope
, scope
);
13302 /* If this class is not itself within the scope of another class,
13303 then we need to parse the bodies of all of the queued function
13304 definitions. Note that the queued functions defined in a class
13305 are not always processed immediately following the
13306 class-specifier for that class. Consider:
13309 struct B { void f() { sizeof (A); } };
13312 If `f' were processed before the processing of `A' were
13313 completed, there would be no way to compute the size of `A'.
13314 Note that the nesting we are interested in here is lexical --
13315 not the semantic nesting given by TYPE_CONTEXT. In particular,
13318 struct A { struct B; };
13319 struct A::B { void f() { } };
13321 there is no need to delay the parsing of `A::B::f'. */
13322 if (--parser
->num_classes_being_defined
== 0)
13326 tree class_type
= NULL_TREE
;
13327 tree pushed_scope
= NULL_TREE
;
13329 /* In a first pass, parse default arguments to the functions.
13330 Then, in a second pass, parse the bodies of the functions.
13331 This two-phased approach handles cases like:
13339 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13340 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13341 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13342 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13343 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13345 fn
= TREE_VALUE (queue_entry
);
13346 /* If there are default arguments that have not yet been processed,
13347 take care of them now. */
13348 if (class_type
!= TREE_PURPOSE (queue_entry
))
13351 pop_scope (pushed_scope
);
13352 class_type
= TREE_PURPOSE (queue_entry
);
13353 pushed_scope
= push_scope (class_type
);
13355 /* Make sure that any template parameters are in scope. */
13356 maybe_begin_member_template_processing (fn
);
13357 /* Parse the default argument expressions. */
13358 cp_parser_late_parsing_default_args (parser
, fn
);
13359 /* Remove any template parameters from the symbol table. */
13360 maybe_end_member_template_processing ();
13363 pop_scope (pushed_scope
);
13364 /* Now parse the body of the functions. */
13365 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13366 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13367 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13368 TREE_VALUE (parser
->unparsed_functions_queues
)
13369 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13371 /* Figure out which function we need to process. */
13372 fn
= TREE_VALUE (queue_entry
);
13373 /* Parse the function. */
13374 cp_parser_late_parsing_for_member (parser
, fn
);
13378 /* Put back any saved access checks. */
13379 pop_deferring_access_checks ();
13381 /* Restore saved state. */
13382 parser
->in_function_body
= saved_in_function_body
;
13383 parser
->num_template_parameter_lists
13384 = saved_num_template_parameter_lists
;
13389 /* Parse a class-head.
13392 class-key identifier [opt] base-clause [opt]
13393 class-key nested-name-specifier identifier base-clause [opt]
13394 class-key nested-name-specifier [opt] template-id
13398 class-key attributes identifier [opt] base-clause [opt]
13399 class-key attributes nested-name-specifier identifier base-clause [opt]
13400 class-key attributes nested-name-specifier [opt] template-id
13403 Upon return BASES is initialized to the list of base classes (or
13404 NULL, if there are none) in the same form returned by
13405 cp_parser_base_clause.
13407 Returns the TYPE of the indicated class. Sets
13408 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13409 involving a nested-name-specifier was used, and FALSE otherwise.
13411 Returns error_mark_node if this is not a class-head.
13413 Returns NULL_TREE if the class-head is syntactically valid, but
13414 semantically invalid in a way that means we should skip the entire
13415 body of the class. */
13418 cp_parser_class_head (cp_parser
* parser
,
13419 bool* nested_name_specifier_p
,
13420 tree
*attributes_p
,
13423 tree nested_name_specifier
;
13424 enum tag_types class_key
;
13425 tree id
= NULL_TREE
;
13426 tree type
= NULL_TREE
;
13428 bool template_id_p
= false;
13429 bool qualified_p
= false;
13430 bool invalid_nested_name_p
= false;
13431 bool invalid_explicit_specialization_p
= false;
13432 tree pushed_scope
= NULL_TREE
;
13433 unsigned num_templates
;
13435 /* Assume no nested-name-specifier will be present. */
13436 *nested_name_specifier_p
= false;
13437 /* Assume no template parameter lists will be used in defining the
13441 *bases
= NULL_TREE
;
13443 /* Look for the class-key. */
13444 class_key
= cp_parser_class_key (parser
);
13445 if (class_key
== none_type
)
13446 return error_mark_node
;
13448 /* Parse the attributes. */
13449 attributes
= cp_parser_attributes_opt (parser
);
13451 /* If the next token is `::', that is invalid -- but sometimes
13452 people do try to write:
13456 Handle this gracefully by accepting the extra qualifier, and then
13457 issuing an error about it later if this really is a
13458 class-head. If it turns out just to be an elaborated type
13459 specifier, remain silent. */
13460 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13461 qualified_p
= true;
13463 push_deferring_access_checks (dk_no_check
);
13465 /* Determine the name of the class. Begin by looking for an
13466 optional nested-name-specifier. */
13467 nested_name_specifier
13468 = cp_parser_nested_name_specifier_opt (parser
,
13469 /*typename_keyword_p=*/false,
13470 /*check_dependency_p=*/false,
13472 /*is_declaration=*/false);
13473 /* If there was a nested-name-specifier, then there *must* be an
13475 if (nested_name_specifier
)
13477 /* Although the grammar says `identifier', it really means
13478 `class-name' or `template-name'. You are only allowed to
13479 define a class that has already been declared with this
13482 The proposed resolution for Core Issue 180 says that wherever
13483 you see `class T::X' you should treat `X' as a type-name.
13485 It is OK to define an inaccessible class; for example:
13487 class A { class B; };
13490 We do not know if we will see a class-name, or a
13491 template-name. We look for a class-name first, in case the
13492 class-name is a template-id; if we looked for the
13493 template-name first we would stop after the template-name. */
13494 cp_parser_parse_tentatively (parser
);
13495 type
= cp_parser_class_name (parser
,
13496 /*typename_keyword_p=*/false,
13497 /*template_keyword_p=*/false,
13499 /*check_dependency_p=*/false,
13500 /*class_head_p=*/true,
13501 /*is_declaration=*/false);
13502 /* If that didn't work, ignore the nested-name-specifier. */
13503 if (!cp_parser_parse_definitely (parser
))
13505 invalid_nested_name_p
= true;
13506 id
= cp_parser_identifier (parser
);
13507 if (id
== error_mark_node
)
13510 /* If we could not find a corresponding TYPE, treat this
13511 declaration like an unqualified declaration. */
13512 if (type
== error_mark_node
)
13513 nested_name_specifier
= NULL_TREE
;
13514 /* Otherwise, count the number of templates used in TYPE and its
13515 containing scopes. */
13520 for (scope
= TREE_TYPE (type
);
13521 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13522 scope
= (TYPE_P (scope
)
13523 ? TYPE_CONTEXT (scope
)
13524 : DECL_CONTEXT (scope
)))
13526 && CLASS_TYPE_P (scope
)
13527 && CLASSTYPE_TEMPLATE_INFO (scope
)
13528 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13529 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13533 /* Otherwise, the identifier is optional. */
13536 /* We don't know whether what comes next is a template-id,
13537 an identifier, or nothing at all. */
13538 cp_parser_parse_tentatively (parser
);
13539 /* Check for a template-id. */
13540 id
= cp_parser_template_id (parser
,
13541 /*template_keyword_p=*/false,
13542 /*check_dependency_p=*/true,
13543 /*is_declaration=*/true);
13544 /* If that didn't work, it could still be an identifier. */
13545 if (!cp_parser_parse_definitely (parser
))
13547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13548 id
= cp_parser_identifier (parser
);
13554 template_id_p
= true;
13559 pop_deferring_access_checks ();
13562 cp_parser_check_for_invalid_template_id (parser
, id
);
13564 /* If it's not a `:' or a `{' then we can't really be looking at a
13565 class-head, since a class-head only appears as part of a
13566 class-specifier. We have to detect this situation before calling
13567 xref_tag, since that has irreversible side-effects. */
13568 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13570 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13571 return error_mark_node
;
13574 /* At this point, we're going ahead with the class-specifier, even
13575 if some other problem occurs. */
13576 cp_parser_commit_to_tentative_parse (parser
);
13577 /* Issue the error about the overly-qualified name now. */
13579 cp_parser_error (parser
,
13580 "global qualification of class name is invalid");
13581 else if (invalid_nested_name_p
)
13582 cp_parser_error (parser
,
13583 "qualified name does not name a class");
13584 else if (nested_name_specifier
)
13588 /* Reject typedef-names in class heads. */
13589 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13591 error ("invalid class name in declaration of %qD", type
);
13596 /* Figure out in what scope the declaration is being placed. */
13597 scope
= current_scope ();
13598 /* If that scope does not contain the scope in which the
13599 class was originally declared, the program is invalid. */
13600 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13602 error ("declaration of %qD in %qD which does not enclose %qD",
13603 type
, scope
, nested_name_specifier
);
13609 A declarator-id shall not be qualified exception of the
13610 definition of a ... nested class outside of its class
13611 ... [or] a the definition or explicit instantiation of a
13612 class member of a namespace outside of its namespace. */
13613 if (scope
== nested_name_specifier
)
13615 pedwarn ("extra qualification ignored");
13616 nested_name_specifier
= NULL_TREE
;
13620 /* An explicit-specialization must be preceded by "template <>". If
13621 it is not, try to recover gracefully. */
13622 if (at_namespace_scope_p ()
13623 && parser
->num_template_parameter_lists
== 0
13626 error ("an explicit specialization must be preceded by %<template <>%>");
13627 invalid_explicit_specialization_p
= true;
13628 /* Take the same action that would have been taken by
13629 cp_parser_explicit_specialization. */
13630 ++parser
->num_template_parameter_lists
;
13631 begin_specialization ();
13633 /* There must be no "return" statements between this point and the
13634 end of this function; set "type "to the correct return value and
13635 use "goto done;" to return. */
13636 /* Make sure that the right number of template parameters were
13638 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13640 /* If something went wrong, there is no point in even trying to
13641 process the class-definition. */
13646 /* Look up the type. */
13649 type
= TREE_TYPE (id
);
13650 type
= maybe_process_partial_specialization (type
);
13651 if (nested_name_specifier
)
13652 pushed_scope
= push_scope (nested_name_specifier
);
13654 else if (nested_name_specifier
)
13660 template <typename T> struct S { struct T };
13661 template <typename T> struct S<T>::T { };
13663 we will get a TYPENAME_TYPE when processing the definition of
13664 `S::T'. We need to resolve it to the actual type before we
13665 try to define it. */
13666 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13668 class_type
= resolve_typename_type (TREE_TYPE (type
),
13669 /*only_current_p=*/false);
13670 if (class_type
!= error_mark_node
)
13671 type
= TYPE_NAME (class_type
);
13674 cp_parser_error (parser
, "could not resolve typename type");
13675 type
= error_mark_node
;
13679 maybe_process_partial_specialization (TREE_TYPE (type
));
13680 class_type
= current_class_type
;
13681 /* Enter the scope indicated by the nested-name-specifier. */
13682 pushed_scope
= push_scope (nested_name_specifier
);
13683 /* Get the canonical version of this type. */
13684 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13685 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13686 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13688 type
= push_template_decl (type
);
13689 if (type
== error_mark_node
)
13696 type
= TREE_TYPE (type
);
13697 *nested_name_specifier_p
= true;
13699 else /* The name is not a nested name. */
13701 /* If the class was unnamed, create a dummy name. */
13703 id
= make_anon_name ();
13704 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13705 parser
->num_template_parameter_lists
);
13708 /* Indicate whether this class was declared as a `class' or as a
13710 if (TREE_CODE (type
) == RECORD_TYPE
)
13711 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13712 cp_parser_check_class_key (class_key
, type
);
13714 /* If this type was already complete, and we see another definition,
13715 that's an error. */
13716 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13718 error ("redefinition of %q#T", type
);
13719 error ("previous definition of %q+#T", type
);
13723 else if (type
== error_mark_node
)
13726 /* We will have entered the scope containing the class; the names of
13727 base classes should be looked up in that context. For example:
13729 struct A { struct B {}; struct C; };
13730 struct A::C : B {};
13734 /* Get the list of base-classes, if there is one. */
13735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13736 *bases
= cp_parser_base_clause (parser
);
13739 /* Leave the scope given by the nested-name-specifier. We will
13740 enter the class scope itself while processing the members. */
13742 pop_scope (pushed_scope
);
13744 if (invalid_explicit_specialization_p
)
13746 end_specialization ();
13747 --parser
->num_template_parameter_lists
;
13749 *attributes_p
= attributes
;
13753 /* Parse a class-key.
13760 Returns the kind of class-key specified, or none_type to indicate
13763 static enum tag_types
13764 cp_parser_class_key (cp_parser
* parser
)
13767 enum tag_types tag_type
;
13769 /* Look for the class-key. */
13770 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13774 /* Check to see if the TOKEN is a class-key. */
13775 tag_type
= cp_parser_token_is_class_key (token
);
13777 cp_parser_error (parser
, "expected class-key");
13781 /* Parse an (optional) member-specification.
13783 member-specification:
13784 member-declaration member-specification [opt]
13785 access-specifier : member-specification [opt] */
13788 cp_parser_member_specification_opt (cp_parser
* parser
)
13795 /* Peek at the next token. */
13796 token
= cp_lexer_peek_token (parser
->lexer
);
13797 /* If it's a `}', or EOF then we've seen all the members. */
13798 if (token
->type
== CPP_CLOSE_BRACE
13799 || token
->type
== CPP_EOF
13800 || token
->type
== CPP_PRAGMA_EOL
)
13803 /* See if this token is a keyword. */
13804 keyword
= token
->keyword
;
13808 case RID_PROTECTED
:
13810 /* Consume the access-specifier. */
13811 cp_lexer_consume_token (parser
->lexer
);
13812 /* Remember which access-specifier is active. */
13813 current_access_specifier
= token
->u
.value
;
13814 /* Look for the `:'. */
13815 cp_parser_require (parser
, CPP_COLON
, "`:'");
13819 /* Accept #pragmas at class scope. */
13820 if (token
->type
== CPP_PRAGMA
)
13822 cp_parser_pragma (parser
, pragma_external
);
13826 /* Otherwise, the next construction must be a
13827 member-declaration. */
13828 cp_parser_member_declaration (parser
);
13833 /* Parse a member-declaration.
13835 member-declaration:
13836 decl-specifier-seq [opt] member-declarator-list [opt] ;
13837 function-definition ; [opt]
13838 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13840 template-declaration
13842 member-declarator-list:
13844 member-declarator-list , member-declarator
13847 declarator pure-specifier [opt]
13848 declarator constant-initializer [opt]
13849 identifier [opt] : constant-expression
13853 member-declaration:
13854 __extension__ member-declaration
13857 declarator attributes [opt] pure-specifier [opt]
13858 declarator attributes [opt] constant-initializer [opt]
13859 identifier [opt] attributes [opt] : constant-expression
13863 member-declaration:
13864 static_assert-declaration */
13867 cp_parser_member_declaration (cp_parser
* parser
)
13869 cp_decl_specifier_seq decl_specifiers
;
13870 tree prefix_attributes
;
13872 int declares_class_or_enum
;
13875 int saved_pedantic
;
13877 /* Check for the `__extension__' keyword. */
13878 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13881 cp_parser_member_declaration (parser
);
13882 /* Restore the old value of the PEDANTIC flag. */
13883 pedantic
= saved_pedantic
;
13888 /* Check for a template-declaration. */
13889 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13891 /* An explicit specialization here is an error condition, and we
13892 expect the specialization handler to detect and report this. */
13893 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13894 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13895 cp_parser_explicit_specialization (parser
);
13897 cp_parser_template_declaration (parser
, /*member_p=*/true);
13902 /* Check for a using-declaration. */
13903 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13905 /* Parse the using-declaration. */
13906 cp_parser_using_declaration (parser
,
13907 /*access_declaration_p=*/false);
13911 /* Check for @defs. */
13912 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13915 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13916 ivar
= ivar_chains
;
13920 ivar
= TREE_CHAIN (member
);
13921 TREE_CHAIN (member
) = NULL_TREE
;
13922 finish_member_declaration (member
);
13927 /* If the next token is `static_assert' we have a static assertion. */
13928 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
13930 cp_parser_static_assert (parser
, /*member_p=*/true);
13934 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
13937 /* Parse the decl-specifier-seq. */
13938 cp_parser_decl_specifier_seq (parser
,
13939 CP_PARSER_FLAGS_OPTIONAL
,
13941 &declares_class_or_enum
);
13942 prefix_attributes
= decl_specifiers
.attributes
;
13943 decl_specifiers
.attributes
= NULL_TREE
;
13944 /* Check for an invalid type-name. */
13945 if (!decl_specifiers
.type
13946 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13948 /* If there is no declarator, then the decl-specifier-seq should
13950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13952 /* If there was no decl-specifier-seq, and the next token is a
13953 `;', then we have something like:
13959 Each member-declaration shall declare at least one member
13960 name of the class. */
13961 if (!decl_specifiers
.any_specifiers_p
)
13963 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13964 if (pedantic
&& !token
->in_system_header
)
13965 pedwarn ("%Hextra %<;%>", &token
->location
);
13971 /* See if this declaration is a friend. */
13972 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13973 /* If there were decl-specifiers, check to see if there was
13974 a class-declaration. */
13975 type
= check_tag_decl (&decl_specifiers
);
13976 /* Nested classes have already been added to the class, but
13977 a `friend' needs to be explicitly registered. */
13980 /* If the `friend' keyword was present, the friend must
13981 be introduced with a class-key. */
13982 if (!declares_class_or_enum
)
13983 error ("a class-key must be used when declaring a friend");
13986 template <typename T> struct A {
13987 friend struct A<T>::B;
13990 A<T>::B will be represented by a TYPENAME_TYPE, and
13991 therefore not recognized by check_tag_decl. */
13993 && decl_specifiers
.type
13994 && TYPE_P (decl_specifiers
.type
))
13995 type
= decl_specifiers
.type
;
13996 if (!type
|| !TYPE_P (type
))
13997 error ("friend declaration does not name a class or "
14000 make_friend_class (current_class_type
, type
,
14001 /*complain=*/true);
14003 /* If there is no TYPE, an error message will already have
14005 else if (!type
|| type
== error_mark_node
)
14007 /* An anonymous aggregate has to be handled specially; such
14008 a declaration really declares a data member (with a
14009 particular type), as opposed to a nested class. */
14010 else if (ANON_AGGR_TYPE_P (type
))
14012 /* Remove constructors and such from TYPE, now that we
14013 know it is an anonymous aggregate. */
14014 fixup_anonymous_aggr (type
);
14015 /* And make the corresponding data member. */
14016 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
14017 /* Add it to the class. */
14018 finish_member_declaration (decl
);
14021 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
14026 /* See if these declarations will be friends. */
14027 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14029 /* Keep going until we hit the `;' at the end of the
14031 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14033 tree attributes
= NULL_TREE
;
14034 tree first_attribute
;
14036 /* Peek at the next token. */
14037 token
= cp_lexer_peek_token (parser
->lexer
);
14039 /* Check for a bitfield declaration. */
14040 if (token
->type
== CPP_COLON
14041 || (token
->type
== CPP_NAME
14042 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
14048 /* Get the name of the bitfield. Note that we cannot just
14049 check TOKEN here because it may have been invalidated by
14050 the call to cp_lexer_peek_nth_token above. */
14051 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
14052 identifier
= cp_parser_identifier (parser
);
14054 identifier
= NULL_TREE
;
14056 /* Consume the `:' token. */
14057 cp_lexer_consume_token (parser
->lexer
);
14058 /* Get the width of the bitfield. */
14060 = cp_parser_constant_expression (parser
,
14061 /*allow_non_constant=*/false,
14064 /* Look for attributes that apply to the bitfield. */
14065 attributes
= cp_parser_attributes_opt (parser
);
14066 /* Remember which attributes are prefix attributes and
14068 first_attribute
= attributes
;
14069 /* Combine the attributes. */
14070 attributes
= chainon (prefix_attributes
, attributes
);
14072 /* Create the bitfield declaration. */
14073 decl
= grokbitfield (identifier
14074 ? make_id_declarator (NULL_TREE
,
14080 /* Apply the attributes. */
14081 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
14085 cp_declarator
*declarator
;
14087 tree asm_specification
;
14088 int ctor_dtor_or_conv_p
;
14090 /* Parse the declarator. */
14092 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14093 &ctor_dtor_or_conv_p
,
14094 /*parenthesized_p=*/NULL
,
14095 /*member_p=*/true);
14097 /* If something went wrong parsing the declarator, make sure
14098 that we at least consume some tokens. */
14099 if (declarator
== cp_error_declarator
)
14101 /* Skip to the end of the statement. */
14102 cp_parser_skip_to_end_of_statement (parser
);
14103 /* If the next token is not a semicolon, that is
14104 probably because we just skipped over the body of
14105 a function. So, we consume a semicolon if
14106 present, but do not issue an error message if it
14108 if (cp_lexer_next_token_is (parser
->lexer
,
14110 cp_lexer_consume_token (parser
->lexer
);
14114 if (declares_class_or_enum
& 2)
14115 cp_parser_check_for_definition_in_return_type
14116 (declarator
, decl_specifiers
.type
);
14118 /* Look for an asm-specification. */
14119 asm_specification
= cp_parser_asm_specification_opt (parser
);
14120 /* Look for attributes that apply to the declaration. */
14121 attributes
= cp_parser_attributes_opt (parser
);
14122 /* Remember which attributes are prefix attributes and
14124 first_attribute
= attributes
;
14125 /* Combine the attributes. */
14126 attributes
= chainon (prefix_attributes
, attributes
);
14128 /* If it's an `=', then we have a constant-initializer or a
14129 pure-specifier. It is not correct to parse the
14130 initializer before registering the member declaration
14131 since the member declaration should be in scope while
14132 its initializer is processed. However, the rest of the
14133 front end does not yet provide an interface that allows
14134 us to handle this correctly. */
14135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14139 A pure-specifier shall be used only in the declaration of
14140 a virtual function.
14142 A member-declarator can contain a constant-initializer
14143 only if it declares a static member of integral or
14146 Therefore, if the DECLARATOR is for a function, we look
14147 for a pure-specifier; otherwise, we look for a
14148 constant-initializer. When we call `grokfield', it will
14149 perform more stringent semantics checks. */
14150 if (function_declarator_p (declarator
))
14151 initializer
= cp_parser_pure_specifier (parser
);
14153 /* Parse the initializer. */
14154 initializer
= cp_parser_constant_initializer (parser
);
14156 /* Otherwise, there is no initializer. */
14158 initializer
= NULL_TREE
;
14160 /* See if we are probably looking at a function
14161 definition. We are certainly not looking at a
14162 member-declarator. Calling `grokfield' has
14163 side-effects, so we must not do it unless we are sure
14164 that we are looking at a member-declarator. */
14165 if (cp_parser_token_starts_function_definition_p
14166 (cp_lexer_peek_token (parser
->lexer
)))
14168 /* The grammar does not allow a pure-specifier to be
14169 used when a member function is defined. (It is
14170 possible that this fact is an oversight in the
14171 standard, since a pure function may be defined
14172 outside of the class-specifier. */
14174 error ("pure-specifier on function-definition");
14175 decl
= cp_parser_save_member_function_body (parser
,
14179 /* If the member was not a friend, declare it here. */
14181 finish_member_declaration (decl
);
14182 /* Peek at the next token. */
14183 token
= cp_lexer_peek_token (parser
->lexer
);
14184 /* If the next token is a semicolon, consume it. */
14185 if (token
->type
== CPP_SEMICOLON
)
14186 cp_lexer_consume_token (parser
->lexer
);
14190 /* Create the declaration. */
14191 decl
= grokfield (declarator
, &decl_specifiers
,
14192 initializer
, /*init_const_expr_p=*/true,
14197 /* Reset PREFIX_ATTRIBUTES. */
14198 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
14199 attributes
= TREE_CHAIN (attributes
);
14201 TREE_CHAIN (attributes
) = NULL_TREE
;
14203 /* If there is any qualification still in effect, clear it
14204 now; we will be starting fresh with the next declarator. */
14205 parser
->scope
= NULL_TREE
;
14206 parser
->qualifying_scope
= NULL_TREE
;
14207 parser
->object_scope
= NULL_TREE
;
14208 /* If it's a `,', then there are more declarators. */
14209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14210 cp_lexer_consume_token (parser
->lexer
);
14211 /* If the next token isn't a `;', then we have a parse error. */
14212 else if (cp_lexer_next_token_is_not (parser
->lexer
,
14215 cp_parser_error (parser
, "expected %<;%>");
14216 /* Skip tokens until we find a `;'. */
14217 cp_parser_skip_to_end_of_statement (parser
);
14224 /* Add DECL to the list of members. */
14226 finish_member_declaration (decl
);
14228 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14229 cp_parser_save_default_args (parser
, decl
);
14234 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14237 /* Parse a pure-specifier.
14242 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14243 Otherwise, ERROR_MARK_NODE is returned. */
14246 cp_parser_pure_specifier (cp_parser
* parser
)
14250 /* Look for the `=' token. */
14251 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14252 return error_mark_node
;
14253 /* Look for the `0' token. */
14254 token
= cp_lexer_consume_token (parser
->lexer
);
14255 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14256 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
14258 cp_parser_error (parser
,
14259 "invalid pure specifier (only `= 0' is allowed)");
14260 cp_parser_skip_to_end_of_statement (parser
);
14261 return error_mark_node
;
14263 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14265 error ("templates may not be %<virtual%>");
14266 return error_mark_node
;
14269 return integer_zero_node
;
14272 /* Parse a constant-initializer.
14274 constant-initializer:
14275 = constant-expression
14277 Returns a representation of the constant-expression. */
14280 cp_parser_constant_initializer (cp_parser
* parser
)
14282 /* Look for the `=' token. */
14283 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14284 return error_mark_node
;
14286 /* It is invalid to write:
14288 struct S { static const int i = { 7 }; };
14291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14293 cp_parser_error (parser
,
14294 "a brace-enclosed initializer is not allowed here");
14295 /* Consume the opening brace. */
14296 cp_lexer_consume_token (parser
->lexer
);
14297 /* Skip the initializer. */
14298 cp_parser_skip_to_closing_brace (parser
);
14299 /* Look for the trailing `}'. */
14300 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14302 return error_mark_node
;
14305 return cp_parser_constant_expression (parser
,
14306 /*allow_non_constant=*/false,
14310 /* Derived classes [gram.class.derived] */
14312 /* Parse a base-clause.
14315 : base-specifier-list
14317 base-specifier-list:
14319 base-specifier-list , base-specifier
14321 Returns a TREE_LIST representing the base-classes, in the order in
14322 which they were declared. The representation of each node is as
14323 described by cp_parser_base_specifier.
14325 In the case that no bases are specified, this function will return
14326 NULL_TREE, not ERROR_MARK_NODE. */
14329 cp_parser_base_clause (cp_parser
* parser
)
14331 tree bases
= NULL_TREE
;
14333 /* Look for the `:' that begins the list. */
14334 cp_parser_require (parser
, CPP_COLON
, "`:'");
14336 /* Scan the base-specifier-list. */
14342 /* Look for the base-specifier. */
14343 base
= cp_parser_base_specifier (parser
);
14344 /* Add BASE to the front of the list. */
14345 if (base
!= error_mark_node
)
14347 TREE_CHAIN (base
) = bases
;
14350 /* Peek at the next token. */
14351 token
= cp_lexer_peek_token (parser
->lexer
);
14352 /* If it's not a comma, then the list is complete. */
14353 if (token
->type
!= CPP_COMMA
)
14355 /* Consume the `,'. */
14356 cp_lexer_consume_token (parser
->lexer
);
14359 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14360 base class had a qualified name. However, the next name that
14361 appears is certainly not qualified. */
14362 parser
->scope
= NULL_TREE
;
14363 parser
->qualifying_scope
= NULL_TREE
;
14364 parser
->object_scope
= NULL_TREE
;
14366 return nreverse (bases
);
14369 /* Parse a base-specifier.
14372 :: [opt] nested-name-specifier [opt] class-name
14373 virtual access-specifier [opt] :: [opt] nested-name-specifier
14375 access-specifier virtual [opt] :: [opt] nested-name-specifier
14378 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14379 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14380 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14381 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14384 cp_parser_base_specifier (cp_parser
* parser
)
14388 bool virtual_p
= false;
14389 bool duplicate_virtual_error_issued_p
= false;
14390 bool duplicate_access_error_issued_p
= false;
14391 bool class_scope_p
, template_p
;
14392 tree access
= access_default_node
;
14395 /* Process the optional `virtual' and `access-specifier'. */
14398 /* Peek at the next token. */
14399 token
= cp_lexer_peek_token (parser
->lexer
);
14400 /* Process `virtual'. */
14401 switch (token
->keyword
)
14404 /* If `virtual' appears more than once, issue an error. */
14405 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14407 cp_parser_error (parser
,
14408 "%<virtual%> specified more than once in base-specified");
14409 duplicate_virtual_error_issued_p
= true;
14414 /* Consume the `virtual' token. */
14415 cp_lexer_consume_token (parser
->lexer
);
14420 case RID_PROTECTED
:
14422 /* If more than one access specifier appears, issue an
14424 if (access
!= access_default_node
14425 && !duplicate_access_error_issued_p
)
14427 cp_parser_error (parser
,
14428 "more than one access specifier in base-specified");
14429 duplicate_access_error_issued_p
= true;
14432 access
= ridpointers
[(int) token
->keyword
];
14434 /* Consume the access-specifier. */
14435 cp_lexer_consume_token (parser
->lexer
);
14444 /* It is not uncommon to see programs mechanically, erroneously, use
14445 the 'typename' keyword to denote (dependent) qualified types
14446 as base classes. */
14447 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14449 if (!processing_template_decl
)
14450 error ("keyword %<typename%> not allowed outside of templates");
14452 error ("keyword %<typename%> not allowed in this context "
14453 "(the base class is implicitly a type)");
14454 cp_lexer_consume_token (parser
->lexer
);
14457 /* Look for the optional `::' operator. */
14458 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14459 /* Look for the nested-name-specifier. The simplest way to
14464 The keyword `typename' is not permitted in a base-specifier or
14465 mem-initializer; in these contexts a qualified name that
14466 depends on a template-parameter is implicitly assumed to be a
14469 is to pretend that we have seen the `typename' keyword at this
14471 cp_parser_nested_name_specifier_opt (parser
,
14472 /*typename_keyword_p=*/true,
14473 /*check_dependency_p=*/true,
14475 /*is_declaration=*/true);
14476 /* If the base class is given by a qualified name, assume that names
14477 we see are type names or templates, as appropriate. */
14478 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14479 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14481 /* Finally, look for the class-name. */
14482 type
= cp_parser_class_name (parser
,
14486 /*check_dependency_p=*/true,
14487 /*class_head_p=*/false,
14488 /*is_declaration=*/true);
14490 if (type
== error_mark_node
)
14491 return error_mark_node
;
14493 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14496 /* Exception handling [gram.exception] */
14498 /* Parse an (optional) exception-specification.
14500 exception-specification:
14501 throw ( type-id-list [opt] )
14503 Returns a TREE_LIST representing the exception-specification. The
14504 TREE_VALUE of each node is a type. */
14507 cp_parser_exception_specification_opt (cp_parser
* parser
)
14512 /* Peek at the next token. */
14513 token
= cp_lexer_peek_token (parser
->lexer
);
14514 /* If it's not `throw', then there's no exception-specification. */
14515 if (!cp_parser_is_keyword (token
, RID_THROW
))
14518 /* Consume the `throw'. */
14519 cp_lexer_consume_token (parser
->lexer
);
14521 /* Look for the `('. */
14522 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14524 /* Peek at the next token. */
14525 token
= cp_lexer_peek_token (parser
->lexer
);
14526 /* If it's not a `)', then there is a type-id-list. */
14527 if (token
->type
!= CPP_CLOSE_PAREN
)
14529 const char *saved_message
;
14531 /* Types may not be defined in an exception-specification. */
14532 saved_message
= parser
->type_definition_forbidden_message
;
14533 parser
->type_definition_forbidden_message
14534 = "types may not be defined in an exception-specification";
14535 /* Parse the type-id-list. */
14536 type_id_list
= cp_parser_type_id_list (parser
);
14537 /* Restore the saved message. */
14538 parser
->type_definition_forbidden_message
= saved_message
;
14541 type_id_list
= empty_except_spec
;
14543 /* Look for the `)'. */
14544 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14546 return type_id_list
;
14549 /* Parse an (optional) type-id-list.
14553 type-id-list , type-id
14555 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14556 in the order that the types were presented. */
14559 cp_parser_type_id_list (cp_parser
* parser
)
14561 tree types
= NULL_TREE
;
14568 /* Get the next type-id. */
14569 type
= cp_parser_type_id (parser
);
14570 /* Add it to the list. */
14571 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14572 /* Peek at the next token. */
14573 token
= cp_lexer_peek_token (parser
->lexer
);
14574 /* If it is not a `,', we are done. */
14575 if (token
->type
!= CPP_COMMA
)
14577 /* Consume the `,'. */
14578 cp_lexer_consume_token (parser
->lexer
);
14581 return nreverse (types
);
14584 /* Parse a try-block.
14587 try compound-statement handler-seq */
14590 cp_parser_try_block (cp_parser
* parser
)
14594 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14595 try_block
= begin_try_block ();
14596 cp_parser_compound_statement (parser
, NULL
, true);
14597 finish_try_block (try_block
);
14598 cp_parser_handler_seq (parser
);
14599 finish_handler_sequence (try_block
);
14604 /* Parse a function-try-block.
14606 function-try-block:
14607 try ctor-initializer [opt] function-body handler-seq */
14610 cp_parser_function_try_block (cp_parser
* parser
)
14612 tree compound_stmt
;
14614 bool ctor_initializer_p
;
14616 /* Look for the `try' keyword. */
14617 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14619 /* Let the rest of the front end know where we are. */
14620 try_block
= begin_function_try_block (&compound_stmt
);
14621 /* Parse the function-body. */
14623 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14624 /* We're done with the `try' part. */
14625 finish_function_try_block (try_block
);
14626 /* Parse the handlers. */
14627 cp_parser_handler_seq (parser
);
14628 /* We're done with the handlers. */
14629 finish_function_handler_sequence (try_block
, compound_stmt
);
14631 return ctor_initializer_p
;
14634 /* Parse a handler-seq.
14637 handler handler-seq [opt] */
14640 cp_parser_handler_seq (cp_parser
* parser
)
14646 /* Parse the handler. */
14647 cp_parser_handler (parser
);
14648 /* Peek at the next token. */
14649 token
= cp_lexer_peek_token (parser
->lexer
);
14650 /* If it's not `catch' then there are no more handlers. */
14651 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14656 /* Parse a handler.
14659 catch ( exception-declaration ) compound-statement */
14662 cp_parser_handler (cp_parser
* parser
)
14667 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14668 handler
= begin_handler ();
14669 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14670 declaration
= cp_parser_exception_declaration (parser
);
14671 finish_handler_parms (declaration
, handler
);
14672 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14673 cp_parser_compound_statement (parser
, NULL
, false);
14674 finish_handler (handler
);
14677 /* Parse an exception-declaration.
14679 exception-declaration:
14680 type-specifier-seq declarator
14681 type-specifier-seq abstract-declarator
14685 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14686 ellipsis variant is used. */
14689 cp_parser_exception_declaration (cp_parser
* parser
)
14691 cp_decl_specifier_seq type_specifiers
;
14692 cp_declarator
*declarator
;
14693 const char *saved_message
;
14695 /* If it's an ellipsis, it's easy to handle. */
14696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14698 /* Consume the `...' token. */
14699 cp_lexer_consume_token (parser
->lexer
);
14703 /* Types may not be defined in exception-declarations. */
14704 saved_message
= parser
->type_definition_forbidden_message
;
14705 parser
->type_definition_forbidden_message
14706 = "types may not be defined in exception-declarations";
14708 /* Parse the type-specifier-seq. */
14709 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14711 /* If it's a `)', then there is no declarator. */
14712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14715 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14716 /*ctor_dtor_or_conv_p=*/NULL
,
14717 /*parenthesized_p=*/NULL
,
14718 /*member_p=*/false);
14720 /* Restore the saved message. */
14721 parser
->type_definition_forbidden_message
= saved_message
;
14723 if (!type_specifiers
.any_specifiers_p
)
14724 return error_mark_node
;
14726 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14729 /* Parse a throw-expression.
14732 throw assignment-expression [opt]
14734 Returns a THROW_EXPR representing the throw-expression. */
14737 cp_parser_throw_expression (cp_parser
* parser
)
14742 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14743 token
= cp_lexer_peek_token (parser
->lexer
);
14744 /* Figure out whether or not there is an assignment-expression
14745 following the "throw" keyword. */
14746 if (token
->type
== CPP_COMMA
14747 || token
->type
== CPP_SEMICOLON
14748 || token
->type
== CPP_CLOSE_PAREN
14749 || token
->type
== CPP_CLOSE_SQUARE
14750 || token
->type
== CPP_CLOSE_BRACE
14751 || token
->type
== CPP_COLON
)
14752 expression
= NULL_TREE
;
14754 expression
= cp_parser_assignment_expression (parser
,
14757 return build_throw (expression
);
14760 /* GNU Extensions */
14762 /* Parse an (optional) asm-specification.
14765 asm ( string-literal )
14767 If the asm-specification is present, returns a STRING_CST
14768 corresponding to the string-literal. Otherwise, returns
14772 cp_parser_asm_specification_opt (cp_parser
* parser
)
14775 tree asm_specification
;
14777 /* Peek at the next token. */
14778 token
= cp_lexer_peek_token (parser
->lexer
);
14779 /* If the next token isn't the `asm' keyword, then there's no
14780 asm-specification. */
14781 if (!cp_parser_is_keyword (token
, RID_ASM
))
14784 /* Consume the `asm' token. */
14785 cp_lexer_consume_token (parser
->lexer
);
14786 /* Look for the `('. */
14787 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14789 /* Look for the string-literal. */
14790 asm_specification
= cp_parser_string_literal (parser
, false, false);
14792 /* Look for the `)'. */
14793 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14795 return asm_specification
;
14798 /* Parse an asm-operand-list.
14802 asm-operand-list , asm-operand
14805 string-literal ( expression )
14806 [ string-literal ] string-literal ( expression )
14808 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14809 each node is the expression. The TREE_PURPOSE is itself a
14810 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14811 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14812 is a STRING_CST for the string literal before the parenthesis. */
14815 cp_parser_asm_operand_list (cp_parser
* parser
)
14817 tree asm_operands
= NULL_TREE
;
14821 tree string_literal
;
14825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14827 /* Consume the `[' token. */
14828 cp_lexer_consume_token (parser
->lexer
);
14829 /* Read the operand name. */
14830 name
= cp_parser_identifier (parser
);
14831 if (name
!= error_mark_node
)
14832 name
= build_string (IDENTIFIER_LENGTH (name
),
14833 IDENTIFIER_POINTER (name
));
14834 /* Look for the closing `]'. */
14835 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14839 /* Look for the string-literal. */
14840 string_literal
= cp_parser_string_literal (parser
, false, false);
14842 /* Look for the `('. */
14843 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14844 /* Parse the expression. */
14845 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14846 /* Look for the `)'. */
14847 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14849 /* Add this operand to the list. */
14850 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14853 /* If the next token is not a `,', there are no more
14855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14857 /* Consume the `,'. */
14858 cp_lexer_consume_token (parser
->lexer
);
14861 return nreverse (asm_operands
);
14864 /* Parse an asm-clobber-list.
14868 asm-clobber-list , string-literal
14870 Returns a TREE_LIST, indicating the clobbers in the order that they
14871 appeared. The TREE_VALUE of each node is a STRING_CST. */
14874 cp_parser_asm_clobber_list (cp_parser
* parser
)
14876 tree clobbers
= NULL_TREE
;
14880 tree string_literal
;
14882 /* Look for the string literal. */
14883 string_literal
= cp_parser_string_literal (parser
, false, false);
14884 /* Add it to the list. */
14885 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14886 /* If the next token is not a `,', then the list is
14888 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14890 /* Consume the `,' token. */
14891 cp_lexer_consume_token (parser
->lexer
);
14897 /* Parse an (optional) series of attributes.
14900 attributes attribute
14903 __attribute__ (( attribute-list [opt] ))
14905 The return value is as for cp_parser_attribute_list. */
14908 cp_parser_attributes_opt (cp_parser
* parser
)
14910 tree attributes
= NULL_TREE
;
14915 tree attribute_list
;
14917 /* Peek at the next token. */
14918 token
= cp_lexer_peek_token (parser
->lexer
);
14919 /* If it's not `__attribute__', then we're done. */
14920 if (token
->keyword
!= RID_ATTRIBUTE
)
14923 /* Consume the `__attribute__' keyword. */
14924 cp_lexer_consume_token (parser
->lexer
);
14925 /* Look for the two `(' tokens. */
14926 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14927 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14929 /* Peek at the next token. */
14930 token
= cp_lexer_peek_token (parser
->lexer
);
14931 if (token
->type
!= CPP_CLOSE_PAREN
)
14932 /* Parse the attribute-list. */
14933 attribute_list
= cp_parser_attribute_list (parser
);
14935 /* If the next token is a `)', then there is no attribute
14937 attribute_list
= NULL
;
14939 /* Look for the two `)' tokens. */
14940 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14941 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14943 /* Add these new attributes to the list. */
14944 attributes
= chainon (attributes
, attribute_list
);
14950 /* Parse an attribute-list.
14954 attribute-list , attribute
14958 identifier ( identifier )
14959 identifier ( identifier , expression-list )
14960 identifier ( expression-list )
14962 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14963 to an attribute. The TREE_PURPOSE of each node is the identifier
14964 indicating which attribute is in use. The TREE_VALUE represents
14965 the arguments, if any. */
14968 cp_parser_attribute_list (cp_parser
* parser
)
14970 tree attribute_list
= NULL_TREE
;
14971 bool save_translate_strings_p
= parser
->translate_strings_p
;
14973 parser
->translate_strings_p
= false;
14980 /* Look for the identifier. We also allow keywords here; for
14981 example `__attribute__ ((const))' is legal. */
14982 token
= cp_lexer_peek_token (parser
->lexer
);
14983 if (token
->type
== CPP_NAME
14984 || token
->type
== CPP_KEYWORD
)
14986 tree arguments
= NULL_TREE
;
14988 /* Consume the token. */
14989 token
= cp_lexer_consume_token (parser
->lexer
);
14991 /* Save away the identifier that indicates which attribute
14993 identifier
= token
->u
.value
;
14994 attribute
= build_tree_list (identifier
, NULL_TREE
);
14996 /* Peek at the next token. */
14997 token
= cp_lexer_peek_token (parser
->lexer
);
14998 /* If it's an `(', then parse the attribute arguments. */
14999 if (token
->type
== CPP_OPEN_PAREN
)
15001 arguments
= cp_parser_parenthesized_expression_list
15002 (parser
, true, /*cast_p=*/false,
15003 /*non_constant_p=*/NULL
);
15004 /* Save the arguments away. */
15005 TREE_VALUE (attribute
) = arguments
;
15008 if (arguments
!= error_mark_node
)
15010 /* Add this attribute to the list. */
15011 TREE_CHAIN (attribute
) = attribute_list
;
15012 attribute_list
= attribute
;
15015 token
= cp_lexer_peek_token (parser
->lexer
);
15017 /* Now, look for more attributes. If the next token isn't a
15018 `,', we're done. */
15019 if (token
->type
!= CPP_COMMA
)
15022 /* Consume the comma and keep going. */
15023 cp_lexer_consume_token (parser
->lexer
);
15025 parser
->translate_strings_p
= save_translate_strings_p
;
15027 /* We built up the list in reverse order. */
15028 return nreverse (attribute_list
);
15031 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15032 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15033 current value of the PEDANTIC flag, regardless of whether or not
15034 the `__extension__' keyword is present. The caller is responsible
15035 for restoring the value of the PEDANTIC flag. */
15038 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
15040 /* Save the old value of the PEDANTIC flag. */
15041 *saved_pedantic
= pedantic
;
15043 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
15045 /* Consume the `__extension__' token. */
15046 cp_lexer_consume_token (parser
->lexer
);
15047 /* We're not being pedantic while the `__extension__' keyword is
15057 /* Parse a label declaration.
15060 __label__ label-declarator-seq ;
15062 label-declarator-seq:
15063 identifier , label-declarator-seq
15067 cp_parser_label_declaration (cp_parser
* parser
)
15069 /* Look for the `__label__' keyword. */
15070 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
15076 /* Look for an identifier. */
15077 identifier
= cp_parser_identifier (parser
);
15078 /* If we failed, stop. */
15079 if (identifier
== error_mark_node
)
15081 /* Declare it as a label. */
15082 finish_label_decl (identifier
);
15083 /* If the next token is a `;', stop. */
15084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15086 /* Look for the `,' separating the label declarations. */
15087 cp_parser_require (parser
, CPP_COMMA
, "`,'");
15090 /* Look for the final `;'. */
15091 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
15094 /* Support Functions */
15096 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15097 NAME should have one of the representations used for an
15098 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15099 is returned. If PARSER->SCOPE is a dependent type, then a
15100 SCOPE_REF is returned.
15102 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15103 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15104 was formed. Abstractly, such entities should not be passed to this
15105 function, because they do not need to be looked up, but it is
15106 simpler to check for this special case here, rather than at the
15109 In cases not explicitly covered above, this function returns a
15110 DECL, OVERLOAD, or baselink representing the result of the lookup.
15111 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15114 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15115 (e.g., "struct") that was used. In that case bindings that do not
15116 refer to types are ignored.
15118 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15121 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15124 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15127 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15128 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15129 NULL_TREE otherwise. */
15132 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
15133 enum tag_types tag_type
,
15136 bool check_dependency
,
15137 tree
*ambiguous_decls
)
15141 tree object_type
= parser
->context
->object_type
;
15143 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15144 flags
|= LOOKUP_COMPLAIN
;
15146 /* Assume that the lookup will be unambiguous. */
15147 if (ambiguous_decls
)
15148 *ambiguous_decls
= NULL_TREE
;
15150 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15151 no longer valid. Note that if we are parsing tentatively, and
15152 the parse fails, OBJECT_TYPE will be automatically restored. */
15153 parser
->context
->object_type
= NULL_TREE
;
15155 if (name
== error_mark_node
)
15156 return error_mark_node
;
15158 /* A template-id has already been resolved; there is no lookup to
15160 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15162 if (BASELINK_P (name
))
15164 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
15165 == TEMPLATE_ID_EXPR
);
15169 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15170 it should already have been checked to make sure that the name
15171 used matches the type being destroyed. */
15172 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15176 /* Figure out to which type this destructor applies. */
15178 type
= parser
->scope
;
15179 else if (object_type
)
15180 type
= object_type
;
15182 type
= current_class_type
;
15183 /* If that's not a class type, there is no destructor. */
15184 if (!type
|| !CLASS_TYPE_P (type
))
15185 return error_mark_node
;
15186 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
15187 lazily_declare_fn (sfk_destructor
, type
);
15188 if (!CLASSTYPE_DESTRUCTORS (type
))
15189 return error_mark_node
;
15190 /* If it was a class type, return the destructor. */
15191 return CLASSTYPE_DESTRUCTORS (type
);
15194 /* By this point, the NAME should be an ordinary identifier. If
15195 the id-expression was a qualified name, the qualifying scope is
15196 stored in PARSER->SCOPE at this point. */
15197 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
15199 /* Perform the lookup. */
15204 if (parser
->scope
== error_mark_node
)
15205 return error_mark_node
;
15207 /* If the SCOPE is dependent, the lookup must be deferred until
15208 the template is instantiated -- unless we are explicitly
15209 looking up names in uninstantiated templates. Even then, we
15210 cannot look up the name if the scope is not a class type; it
15211 might, for example, be a template type parameter. */
15212 dependent_p
= (TYPE_P (parser
->scope
)
15213 && !(parser
->in_declarator_p
15214 && currently_open_class (parser
->scope
))
15215 && dependent_type_p (parser
->scope
));
15216 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
15223 /* The resolution to Core Issue 180 says that `struct
15224 A::B' should be considered a type-name, even if `A'
15226 type
= make_typename_type (parser
->scope
, name
, tag_type
,
15227 /*complain=*/tf_error
);
15228 decl
= TYPE_NAME (type
);
15230 else if (is_template
15231 && (cp_parser_next_token_ends_template_argument_p (parser
)
15232 || cp_lexer_next_token_is (parser
->lexer
,
15234 decl
= make_unbound_class_template (parser
->scope
,
15236 /*complain=*/tf_error
);
15238 decl
= build_qualified_name (/*type=*/NULL_TREE
,
15239 parser
->scope
, name
,
15244 tree pushed_scope
= NULL_TREE
;
15246 /* If PARSER->SCOPE is a dependent type, then it must be a
15247 class type, and we must not be checking dependencies;
15248 otherwise, we would have processed this lookup above. So
15249 that PARSER->SCOPE is not considered a dependent base by
15250 lookup_member, we must enter the scope here. */
15252 pushed_scope
= push_scope (parser
->scope
);
15253 /* If the PARSER->SCOPE is a template specialization, it
15254 may be instantiated during name lookup. In that case,
15255 errors may be issued. Even if we rollback the current
15256 tentative parse, those errors are valid. */
15257 decl
= lookup_qualified_name (parser
->scope
, name
,
15258 tag_type
!= none_type
,
15259 /*complain=*/true);
15261 pop_scope (pushed_scope
);
15263 parser
->qualifying_scope
= parser
->scope
;
15264 parser
->object_scope
= NULL_TREE
;
15266 else if (object_type
)
15268 tree object_decl
= NULL_TREE
;
15269 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15270 OBJECT_TYPE is not a class. */
15271 if (CLASS_TYPE_P (object_type
))
15272 /* If the OBJECT_TYPE is a template specialization, it may
15273 be instantiated during name lookup. In that case, errors
15274 may be issued. Even if we rollback the current tentative
15275 parse, those errors are valid. */
15276 object_decl
= lookup_member (object_type
,
15279 tag_type
!= none_type
);
15280 /* Look it up in the enclosing context, too. */
15281 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15283 /*block_p=*/true, is_namespace
, flags
);
15284 parser
->object_scope
= object_type
;
15285 parser
->qualifying_scope
= NULL_TREE
;
15287 decl
= object_decl
;
15291 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15293 /*block_p=*/true, is_namespace
, flags
);
15294 parser
->qualifying_scope
= NULL_TREE
;
15295 parser
->object_scope
= NULL_TREE
;
15298 /* If the lookup failed, let our caller know. */
15299 if (!decl
|| decl
== error_mark_node
)
15300 return error_mark_node
;
15302 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15303 if (TREE_CODE (decl
) == TREE_LIST
)
15305 if (ambiguous_decls
)
15306 *ambiguous_decls
= decl
;
15307 /* The error message we have to print is too complicated for
15308 cp_parser_error, so we incorporate its actions directly. */
15309 if (!cp_parser_simulate_error (parser
))
15311 error ("reference to %qD is ambiguous", name
);
15312 print_candidates (decl
);
15314 return error_mark_node
;
15317 gcc_assert (DECL_P (decl
)
15318 || TREE_CODE (decl
) == OVERLOAD
15319 || TREE_CODE (decl
) == SCOPE_REF
15320 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
15321 || BASELINK_P (decl
));
15323 /* If we have resolved the name of a member declaration, check to
15324 see if the declaration is accessible. When the name resolves to
15325 set of overloaded functions, accessibility is checked when
15326 overload resolution is done.
15328 During an explicit instantiation, access is not checked at all,
15329 as per [temp.explicit]. */
15331 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15336 /* Like cp_parser_lookup_name, but for use in the typical case where
15337 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15338 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15341 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15343 return cp_parser_lookup_name (parser
, name
,
15345 /*is_template=*/false,
15346 /*is_namespace=*/false,
15347 /*check_dependency=*/true,
15348 /*ambiguous_decls=*/NULL
);
15351 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15352 the current context, return the TYPE_DECL. If TAG_NAME_P is
15353 true, the DECL indicates the class being defined in a class-head,
15354 or declared in an elaborated-type-specifier.
15356 Otherwise, return DECL. */
15359 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15361 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15362 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15365 template <typename T> struct B;
15368 template <typename T> struct A::B {};
15370 Similarly, in an elaborated-type-specifier:
15372 namespace N { struct X{}; }
15375 template <typename T> friend struct N::X;
15378 However, if the DECL refers to a class type, and we are in
15379 the scope of the class, then the name lookup automatically
15380 finds the TYPE_DECL created by build_self_reference rather
15381 than a TEMPLATE_DECL. For example, in:
15383 template <class T> struct S {
15387 there is no need to handle such case. */
15389 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15390 return DECL_TEMPLATE_RESULT (decl
);
15395 /* If too many, or too few, template-parameter lists apply to the
15396 declarator, issue an error message. Returns TRUE if all went well,
15397 and FALSE otherwise. */
15400 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15401 cp_declarator
*declarator
)
15403 unsigned num_templates
;
15405 /* We haven't seen any classes that involve template parameters yet. */
15408 switch (declarator
->kind
)
15411 if (declarator
->u
.id
.qualifying_scope
)
15416 scope
= declarator
->u
.id
.qualifying_scope
;
15417 member
= declarator
->u
.id
.unqualified_name
;
15419 while (scope
&& CLASS_TYPE_P (scope
))
15421 /* You're supposed to have one `template <...>'
15422 for every template class, but you don't need one
15423 for a full specialization. For example:
15425 template <class T> struct S{};
15426 template <> struct S<int> { void f(); };
15427 void S<int>::f () {}
15429 is correct; there shouldn't be a `template <>' for
15430 the definition of `S<int>::f'. */
15431 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
15432 /* If SCOPE does not have template information of any
15433 kind, then it is not a template, nor is it nested
15434 within a template. */
15436 if (explicit_class_specialization_p (scope
))
15438 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15441 scope
= TYPE_CONTEXT (scope
);
15444 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15445 == TEMPLATE_ID_EXPR
)
15446 /* If the DECLARATOR has the form `X<y>' then it uses one
15447 additional level of template parameters. */
15450 return cp_parser_check_template_parameters (parser
,
15456 case cdk_reference
:
15458 return (cp_parser_check_declarator_template_parameters
15459 (parser
, declarator
->declarator
));
15465 gcc_unreachable ();
15470 /* NUM_TEMPLATES were used in the current declaration. If that is
15471 invalid, return FALSE and issue an error messages. Otherwise,
15475 cp_parser_check_template_parameters (cp_parser
* parser
,
15476 unsigned num_templates
)
15478 /* If there are more template classes than parameter lists, we have
15481 template <class T> void S<T>::R<T>::f (); */
15482 if (parser
->num_template_parameter_lists
< num_templates
)
15484 error ("too few template-parameter-lists");
15487 /* If there are the same number of template classes and parameter
15488 lists, that's OK. */
15489 if (parser
->num_template_parameter_lists
== num_templates
)
15491 /* If there are more, but only one more, then we are referring to a
15492 member template. That's OK too. */
15493 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15495 /* Otherwise, there are too many template parameter lists. We have
15498 template <class T> template <class U> void S::f(); */
15499 error ("too many template-parameter-lists");
15503 /* Parse an optional `::' token indicating that the following name is
15504 from the global namespace. If so, PARSER->SCOPE is set to the
15505 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15506 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15507 Returns the new value of PARSER->SCOPE, if the `::' token is
15508 present, and NULL_TREE otherwise. */
15511 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15515 /* Peek at the next token. */
15516 token
= cp_lexer_peek_token (parser
->lexer
);
15517 /* If we're looking at a `::' token then we're starting from the
15518 global namespace, not our current location. */
15519 if (token
->type
== CPP_SCOPE
)
15521 /* Consume the `::' token. */
15522 cp_lexer_consume_token (parser
->lexer
);
15523 /* Set the SCOPE so that we know where to start the lookup. */
15524 parser
->scope
= global_namespace
;
15525 parser
->qualifying_scope
= global_namespace
;
15526 parser
->object_scope
= NULL_TREE
;
15528 return parser
->scope
;
15530 else if (!current_scope_valid_p
)
15532 parser
->scope
= NULL_TREE
;
15533 parser
->qualifying_scope
= NULL_TREE
;
15534 parser
->object_scope
= NULL_TREE
;
15540 /* Returns TRUE if the upcoming token sequence is the start of a
15541 constructor declarator. If FRIEND_P is true, the declarator is
15542 preceded by the `friend' specifier. */
15545 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15547 bool constructor_p
;
15548 tree type_decl
= NULL_TREE
;
15549 bool nested_name_p
;
15550 cp_token
*next_token
;
15552 /* The common case is that this is not a constructor declarator, so
15553 try to avoid doing lots of work if at all possible. It's not
15554 valid declare a constructor at function scope. */
15555 if (parser
->in_function_body
)
15557 /* And only certain tokens can begin a constructor declarator. */
15558 next_token
= cp_lexer_peek_token (parser
->lexer
);
15559 if (next_token
->type
!= CPP_NAME
15560 && next_token
->type
!= CPP_SCOPE
15561 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15562 && next_token
->type
!= CPP_TEMPLATE_ID
)
15565 /* Parse tentatively; we are going to roll back all of the tokens
15567 cp_parser_parse_tentatively (parser
);
15568 /* Assume that we are looking at a constructor declarator. */
15569 constructor_p
= true;
15571 /* Look for the optional `::' operator. */
15572 cp_parser_global_scope_opt (parser
,
15573 /*current_scope_valid_p=*/false);
15574 /* Look for the nested-name-specifier. */
15576 = (cp_parser_nested_name_specifier_opt (parser
,
15577 /*typename_keyword_p=*/false,
15578 /*check_dependency_p=*/false,
15580 /*is_declaration=*/false)
15582 /* Outside of a class-specifier, there must be a
15583 nested-name-specifier. */
15584 if (!nested_name_p
&&
15585 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15587 constructor_p
= false;
15588 /* If we still think that this might be a constructor-declarator,
15589 look for a class-name. */
15594 template <typename T> struct S { S(); };
15595 template <typename T> S<T>::S ();
15597 we must recognize that the nested `S' names a class.
15600 template <typename T> S<T>::S<T> ();
15602 we must recognize that the nested `S' names a template. */
15603 type_decl
= cp_parser_class_name (parser
,
15604 /*typename_keyword_p=*/false,
15605 /*template_keyword_p=*/false,
15607 /*check_dependency_p=*/false,
15608 /*class_head_p=*/false,
15609 /*is_declaration=*/false);
15610 /* If there was no class-name, then this is not a constructor. */
15611 constructor_p
= !cp_parser_error_occurred (parser
);
15614 /* If we're still considering a constructor, we have to see a `(',
15615 to begin the parameter-declaration-clause, followed by either a
15616 `)', an `...', or a decl-specifier. We need to check for a
15617 type-specifier to avoid being fooled into thinking that:
15621 is a constructor. (It is actually a function named `f' that
15622 takes one parameter (of type `int') and returns a value of type
15625 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15627 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15628 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15629 /* A parameter declaration begins with a decl-specifier,
15630 which is either the "attribute" keyword, a storage class
15631 specifier, or (usually) a type-specifier. */
15632 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
15635 tree pushed_scope
= NULL_TREE
;
15636 unsigned saved_num_template_parameter_lists
;
15638 /* Names appearing in the type-specifier should be looked up
15639 in the scope of the class. */
15640 if (current_class_type
)
15644 type
= TREE_TYPE (type_decl
);
15645 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15647 type
= resolve_typename_type (type
,
15648 /*only_current_p=*/false);
15649 if (type
== error_mark_node
)
15651 cp_parser_abort_tentative_parse (parser
);
15655 pushed_scope
= push_scope (type
);
15658 /* Inside the constructor parameter list, surrounding
15659 template-parameter-lists do not apply. */
15660 saved_num_template_parameter_lists
15661 = parser
->num_template_parameter_lists
;
15662 parser
->num_template_parameter_lists
= 0;
15664 /* Look for the type-specifier. */
15665 cp_parser_type_specifier (parser
,
15666 CP_PARSER_FLAGS_NONE
,
15667 /*decl_specs=*/NULL
,
15668 /*is_declarator=*/true,
15669 /*declares_class_or_enum=*/NULL
,
15670 /*is_cv_qualifier=*/NULL
);
15672 parser
->num_template_parameter_lists
15673 = saved_num_template_parameter_lists
;
15675 /* Leave the scope of the class. */
15677 pop_scope (pushed_scope
);
15679 constructor_p
= !cp_parser_error_occurred (parser
);
15683 constructor_p
= false;
15684 /* We did not really want to consume any tokens. */
15685 cp_parser_abort_tentative_parse (parser
);
15687 return constructor_p
;
15690 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15691 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15692 they must be performed once we are in the scope of the function.
15694 Returns the function defined. */
15697 cp_parser_function_definition_from_specifiers_and_declarator
15698 (cp_parser
* parser
,
15699 cp_decl_specifier_seq
*decl_specifiers
,
15701 const cp_declarator
*declarator
)
15706 /* Begin the function-definition. */
15707 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15709 /* The things we're about to see are not directly qualified by any
15710 template headers we've seen thus far. */
15711 reset_specialization ();
15713 /* If there were names looked up in the decl-specifier-seq that we
15714 did not check, check them now. We must wait until we are in the
15715 scope of the function to perform the checks, since the function
15716 might be a friend. */
15717 perform_deferred_access_checks ();
15721 /* Skip the entire function. */
15722 cp_parser_skip_to_end_of_block_or_statement (parser
);
15723 fn
= error_mark_node
;
15725 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
15727 /* Seen already, skip it. An error message has already been output. */
15728 cp_parser_skip_to_end_of_block_or_statement (parser
);
15729 fn
= current_function_decl
;
15730 current_function_decl
= NULL_TREE
;
15731 /* If this is a function from a class, pop the nested class. */
15732 if (current_class_name
)
15733 pop_nested_class ();
15736 fn
= cp_parser_function_definition_after_declarator (parser
,
15737 /*inline_p=*/false);
15742 /* Parse the part of a function-definition that follows the
15743 declarator. INLINE_P is TRUE iff this function is an inline
15744 function defined with a class-specifier.
15746 Returns the function defined. */
15749 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15753 bool ctor_initializer_p
= false;
15754 bool saved_in_unbraced_linkage_specification_p
;
15755 bool saved_in_function_body
;
15756 unsigned saved_num_template_parameter_lists
;
15758 saved_in_function_body
= parser
->in_function_body
;
15759 parser
->in_function_body
= true;
15760 /* If the next token is `return', then the code may be trying to
15761 make use of the "named return value" extension that G++ used to
15763 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15765 /* Consume the `return' keyword. */
15766 cp_lexer_consume_token (parser
->lexer
);
15767 /* Look for the identifier that indicates what value is to be
15769 cp_parser_identifier (parser
);
15770 /* Issue an error message. */
15771 error ("named return values are no longer supported");
15772 /* Skip tokens until we reach the start of the function body. */
15775 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15776 if (token
->type
== CPP_OPEN_BRACE
15777 || token
->type
== CPP_EOF
15778 || token
->type
== CPP_PRAGMA_EOL
)
15780 cp_lexer_consume_token (parser
->lexer
);
15783 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15784 anything declared inside `f'. */
15785 saved_in_unbraced_linkage_specification_p
15786 = parser
->in_unbraced_linkage_specification_p
;
15787 parser
->in_unbraced_linkage_specification_p
= false;
15788 /* Inside the function, surrounding template-parameter-lists do not
15790 saved_num_template_parameter_lists
15791 = parser
->num_template_parameter_lists
;
15792 parser
->num_template_parameter_lists
= 0;
15793 /* If the next token is `try', then we are looking at a
15794 function-try-block. */
15795 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15796 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15797 /* A function-try-block includes the function-body, so we only do
15798 this next part if we're not processing a function-try-block. */
15801 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15803 /* Finish the function. */
15804 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15805 (inline_p
? 2 : 0));
15806 /* Generate code for it, if necessary. */
15807 expand_or_defer_fn (fn
);
15808 /* Restore the saved values. */
15809 parser
->in_unbraced_linkage_specification_p
15810 = saved_in_unbraced_linkage_specification_p
;
15811 parser
->num_template_parameter_lists
15812 = saved_num_template_parameter_lists
;
15813 parser
->in_function_body
= saved_in_function_body
;
15818 /* Parse a template-declaration, assuming that the `export' (and
15819 `extern') keywords, if present, has already been scanned. MEMBER_P
15820 is as for cp_parser_template_declaration. */
15823 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15825 tree decl
= NULL_TREE
;
15826 VEC (deferred_access_check
,gc
) *checks
;
15827 tree parameter_list
;
15828 bool friend_p
= false;
15829 bool need_lang_pop
;
15831 /* Look for the `template' keyword. */
15832 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15836 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15838 if (at_class_scope_p () && current_function_decl
)
15840 /* 14.5.2.2 [temp.mem]
15842 A local class shall not have member templates. */
15843 error ("invalid declaration of member template in local class");
15844 cp_parser_skip_to_end_of_block_or_statement (parser
);
15849 A template ... shall not have C linkage. */
15850 if (current_lang_name
== lang_name_c
)
15852 error ("template with C linkage");
15853 /* Give it C++ linkage to avoid confusing other parts of the
15855 push_lang_context (lang_name_cplusplus
);
15856 need_lang_pop
= true;
15859 need_lang_pop
= false;
15861 /* We cannot perform access checks on the template parameter
15862 declarations until we know what is being declared, just as we
15863 cannot check the decl-specifier list. */
15864 push_deferring_access_checks (dk_deferred
);
15866 /* If the next token is `>', then we have an invalid
15867 specialization. Rather than complain about an invalid template
15868 parameter, issue an error message here. */
15869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15871 cp_parser_error (parser
, "invalid explicit specialization");
15872 begin_specialization ();
15873 parameter_list
= NULL_TREE
;
15876 /* Parse the template parameters. */
15877 parameter_list
= cp_parser_template_parameter_list (parser
);
15879 /* Get the deferred access checks from the parameter list. These
15880 will be checked once we know what is being declared, as for a
15881 member template the checks must be performed in the scope of the
15882 class containing the member. */
15883 checks
= get_deferred_access_checks ();
15885 /* Look for the `>'. */
15886 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15887 /* We just processed one more parameter list. */
15888 ++parser
->num_template_parameter_lists
;
15889 /* If the next token is `template', there are more template
15891 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15893 cp_parser_template_declaration_after_export (parser
, member_p
);
15896 /* There are no access checks when parsing a template, as we do not
15897 know if a specialization will be a friend. */
15898 push_deferring_access_checks (dk_no_check
);
15899 decl
= cp_parser_single_declaration (parser
,
15903 pop_deferring_access_checks ();
15905 /* If this is a member template declaration, let the front
15907 if (member_p
&& !friend_p
&& decl
)
15909 if (TREE_CODE (decl
) == TYPE_DECL
)
15910 cp_parser_check_access_in_redeclaration (decl
);
15912 decl
= finish_member_template_decl (decl
);
15914 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15915 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15916 /*complain=*/true);
15918 /* We are done with the current parameter list. */
15919 --parser
->num_template_parameter_lists
;
15921 pop_deferring_access_checks ();
15924 finish_template_decl (parameter_list
);
15926 /* Register member declarations. */
15927 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15928 finish_member_declaration (decl
);
15929 /* For the erroneous case of a template with C linkage, we pushed an
15930 implicit C++ linkage scope; exit that scope now. */
15932 pop_lang_context ();
15933 /* If DECL is a function template, we must return to parse it later.
15934 (Even though there is no definition, there might be default
15935 arguments that need handling.) */
15936 if (member_p
&& decl
15937 && (TREE_CODE (decl
) == FUNCTION_DECL
15938 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15939 TREE_VALUE (parser
->unparsed_functions_queues
)
15940 = tree_cons (NULL_TREE
, decl
,
15941 TREE_VALUE (parser
->unparsed_functions_queues
));
15944 /* Perform the deferred access checks from a template-parameter-list.
15945 CHECKS is a TREE_LIST of access checks, as returned by
15946 get_deferred_access_checks. */
15949 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
15951 ++processing_template_parmlist
;
15952 perform_access_checks (checks
);
15953 --processing_template_parmlist
;
15956 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15957 `function-definition' sequence. MEMBER_P is true, this declaration
15958 appears in a class scope.
15960 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15961 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15964 cp_parser_single_declaration (cp_parser
* parser
,
15965 VEC (deferred_access_check
,gc
)* checks
,
15969 int declares_class_or_enum
;
15970 tree decl
= NULL_TREE
;
15971 cp_decl_specifier_seq decl_specifiers
;
15972 bool function_definition_p
= false;
15974 /* This function is only used when processing a template
15976 gcc_assert (innermost_scope_kind () == sk_template_parms
15977 || innermost_scope_kind () == sk_template_spec
);
15979 /* Defer access checks until we know what is being declared. */
15980 push_deferring_access_checks (dk_deferred
);
15982 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15984 cp_parser_decl_specifier_seq (parser
,
15985 CP_PARSER_FLAGS_OPTIONAL
,
15987 &declares_class_or_enum
);
15989 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15991 /* There are no template typedefs. */
15992 if (decl_specifiers
.specs
[(int) ds_typedef
])
15994 error ("template declaration of %qs", "typedef");
15995 decl
= error_mark_node
;
15998 /* Gather up the access checks that occurred the
15999 decl-specifier-seq. */
16000 stop_deferring_access_checks ();
16002 /* Check for the declaration of a template class. */
16003 if (declares_class_or_enum
)
16005 if (cp_parser_declares_only_class_p (parser
))
16007 decl
= shadow_tag (&decl_specifiers
);
16012 friend template <typename T> struct A<T>::B;
16015 A<T>::B will be represented by a TYPENAME_TYPE, and
16016 therefore not recognized by shadow_tag. */
16017 if (friend_p
&& *friend_p
16019 && decl_specifiers
.type
16020 && TYPE_P (decl_specifiers
.type
))
16021 decl
= decl_specifiers
.type
;
16023 if (decl
&& decl
!= error_mark_node
)
16024 decl
= TYPE_NAME (decl
);
16026 decl
= error_mark_node
;
16028 /* Perform access checks for template parameters. */
16029 cp_parser_perform_template_parameter_access_checks (checks
);
16032 /* If it's not a template class, try for a template function. If
16033 the next token is a `;', then this declaration does not declare
16034 anything. But, if there were errors in the decl-specifiers, then
16035 the error might well have come from an attempted class-specifier.
16036 In that case, there's no need to warn about a missing declarator. */
16038 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
16039 || decl_specifiers
.type
!= error_mark_node
))
16040 decl
= cp_parser_init_declarator (parser
,
16043 /*function_definition_allowed_p=*/true,
16045 declares_class_or_enum
,
16046 &function_definition_p
);
16048 pop_deferring_access_checks ();
16050 /* Clear any current qualification; whatever comes next is the start
16051 of something new. */
16052 parser
->scope
= NULL_TREE
;
16053 parser
->qualifying_scope
= NULL_TREE
;
16054 parser
->object_scope
= NULL_TREE
;
16055 /* Look for a trailing `;' after the declaration. */
16056 if (!function_definition_p
16057 && (decl
== error_mark_node
16058 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
16059 cp_parser_skip_to_end_of_block_or_statement (parser
);
16064 /* Parse a cast-expression that is not the operand of a unary "&". */
16067 cp_parser_simple_cast_expression (cp_parser
*parser
)
16069 return cp_parser_cast_expression (parser
, /*address_p=*/false,
16073 /* Parse a functional cast to TYPE. Returns an expression
16074 representing the cast. */
16077 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
16079 tree expression_list
;
16083 = cp_parser_parenthesized_expression_list (parser
, false,
16085 /*non_constant_p=*/NULL
);
16087 cast
= build_functional_cast (type
, expression_list
);
16088 /* [expr.const]/1: In an integral constant expression "only type
16089 conversions to integral or enumeration type can be used". */
16090 if (TREE_CODE (type
) == TYPE_DECL
)
16091 type
= TREE_TYPE (type
);
16092 if (cast
!= error_mark_node
16093 && !cast_valid_in_integral_constant_expression_p (type
)
16094 && (cp_parser_non_integral_constant_expression
16095 (parser
, "a call to a constructor")))
16096 return error_mark_node
;
16100 /* Save the tokens that make up the body of a member function defined
16101 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16102 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16103 specifiers applied to the declaration. Returns the FUNCTION_DECL
16104 for the member function. */
16107 cp_parser_save_member_function_body (cp_parser
* parser
,
16108 cp_decl_specifier_seq
*decl_specifiers
,
16109 cp_declarator
*declarator
,
16116 /* Create the function-declaration. */
16117 fn
= start_method (decl_specifiers
, declarator
, attributes
);
16118 /* If something went badly wrong, bail out now. */
16119 if (fn
== error_mark_node
)
16121 /* If there's a function-body, skip it. */
16122 if (cp_parser_token_starts_function_definition_p
16123 (cp_lexer_peek_token (parser
->lexer
)))
16124 cp_parser_skip_to_end_of_block_or_statement (parser
);
16125 return error_mark_node
;
16128 /* Remember it, if there default args to post process. */
16129 cp_parser_save_default_args (parser
, fn
);
16131 /* Save away the tokens that make up the body of the
16133 first
= parser
->lexer
->next_token
;
16134 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16135 /* Handle function try blocks. */
16136 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
16137 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16138 last
= parser
->lexer
->next_token
;
16140 /* Save away the inline definition; we will process it when the
16141 class is complete. */
16142 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
16143 DECL_PENDING_INLINE_P (fn
) = 1;
16145 /* We need to know that this was defined in the class, so that
16146 friend templates are handled correctly. */
16147 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
16149 /* We're done with the inline definition. */
16150 finish_method (fn
);
16152 /* Add FN to the queue of functions to be parsed later. */
16153 TREE_VALUE (parser
->unparsed_functions_queues
)
16154 = tree_cons (NULL_TREE
, fn
,
16155 TREE_VALUE (parser
->unparsed_functions_queues
));
16160 /* Parse a template-argument-list, as well as the trailing ">" (but
16161 not the opening ">"). See cp_parser_template_argument_list for the
16165 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
16169 tree saved_qualifying_scope
;
16170 tree saved_object_scope
;
16171 bool saved_greater_than_is_operator_p
;
16172 bool saved_skip_evaluation
;
16176 When parsing a template-id, the first non-nested `>' is taken as
16177 the end of the template-argument-list rather than a greater-than
16179 saved_greater_than_is_operator_p
16180 = parser
->greater_than_is_operator_p
;
16181 parser
->greater_than_is_operator_p
= false;
16182 /* Parsing the argument list may modify SCOPE, so we save it
16184 saved_scope
= parser
->scope
;
16185 saved_qualifying_scope
= parser
->qualifying_scope
;
16186 saved_object_scope
= parser
->object_scope
;
16187 /* We need to evaluate the template arguments, even though this
16188 template-id may be nested within a "sizeof". */
16189 saved_skip_evaluation
= skip_evaluation
;
16190 skip_evaluation
= false;
16191 /* Parse the template-argument-list itself. */
16192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16193 arguments
= NULL_TREE
;
16195 arguments
= cp_parser_template_argument_list (parser
);
16196 /* Look for the `>' that ends the template-argument-list. If we find
16197 a '>>' instead, it's probably just a typo. */
16198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16200 if (!saved_greater_than_is_operator_p
)
16202 /* If we're in a nested template argument list, the '>>' has
16203 to be a typo for '> >'. We emit the error message, but we
16204 continue parsing and we push a '>' as next token, so that
16205 the argument list will be parsed correctly. Note that the
16206 global source location is still on the token before the
16207 '>>', so we need to say explicitly where we want it. */
16208 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16209 error ("%H%<>>%> should be %<> >%> "
16210 "within a nested template argument list",
16213 /* ??? Proper recovery should terminate two levels of
16214 template argument list here. */
16215 token
->type
= CPP_GREATER
;
16219 /* If this is not a nested template argument list, the '>>'
16220 is a typo for '>'. Emit an error message and continue.
16221 Same deal about the token location, but here we can get it
16222 right by consuming the '>>' before issuing the diagnostic. */
16223 cp_lexer_consume_token (parser
->lexer
);
16224 error ("spurious %<>>%>, use %<>%> to terminate "
16225 "a template argument list");
16229 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16230 /* The `>' token might be a greater-than operator again now. */
16231 parser
->greater_than_is_operator_p
16232 = saved_greater_than_is_operator_p
;
16233 /* Restore the SAVED_SCOPE. */
16234 parser
->scope
= saved_scope
;
16235 parser
->qualifying_scope
= saved_qualifying_scope
;
16236 parser
->object_scope
= saved_object_scope
;
16237 skip_evaluation
= saved_skip_evaluation
;
16242 /* MEMBER_FUNCTION is a member function, or a friend. If default
16243 arguments, or the body of the function have not yet been parsed,
16247 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
16249 /* If this member is a template, get the underlying
16251 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
16252 member_function
= DECL_TEMPLATE_RESULT (member_function
);
16254 /* There should not be any class definitions in progress at this
16255 point; the bodies of members are only parsed outside of all class
16257 gcc_assert (parser
->num_classes_being_defined
== 0);
16258 /* While we're parsing the member functions we might encounter more
16259 classes. We want to handle them right away, but we don't want
16260 them getting mixed up with functions that are currently in the
16262 parser
->unparsed_functions_queues
16263 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16265 /* Make sure that any template parameters are in scope. */
16266 maybe_begin_member_template_processing (member_function
);
16268 /* If the body of the function has not yet been parsed, parse it
16270 if (DECL_PENDING_INLINE_P (member_function
))
16272 tree function_scope
;
16273 cp_token_cache
*tokens
;
16275 /* The function is no longer pending; we are processing it. */
16276 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
16277 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
16278 DECL_PENDING_INLINE_P (member_function
) = 0;
16280 /* If this is a local class, enter the scope of the containing
16282 function_scope
= current_function_decl
;
16283 if (function_scope
)
16284 push_function_context_to (function_scope
);
16287 /* Push the body of the function onto the lexer stack. */
16288 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16290 /* Let the front end know that we going to be defining this
16292 start_preparsed_function (member_function
, NULL_TREE
,
16293 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
16295 /* Don't do access checking if it is a templated function. */
16296 if (processing_template_decl
)
16297 push_deferring_access_checks (dk_no_check
);
16299 /* Now, parse the body of the function. */
16300 cp_parser_function_definition_after_declarator (parser
,
16301 /*inline_p=*/true);
16303 if (processing_template_decl
)
16304 pop_deferring_access_checks ();
16306 /* Leave the scope of the containing function. */
16307 if (function_scope
)
16308 pop_function_context_from (function_scope
);
16309 cp_parser_pop_lexer (parser
);
16312 /* Remove any template parameters from the symbol table. */
16313 maybe_end_member_template_processing ();
16315 /* Restore the queue. */
16316 parser
->unparsed_functions_queues
16317 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16320 /* If DECL contains any default args, remember it on the unparsed
16321 functions queue. */
16324 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
16328 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
16330 probe
= TREE_CHAIN (probe
))
16331 if (TREE_PURPOSE (probe
))
16333 TREE_PURPOSE (parser
->unparsed_functions_queues
)
16334 = tree_cons (current_class_type
, decl
,
16335 TREE_PURPOSE (parser
->unparsed_functions_queues
));
16340 /* FN is a FUNCTION_DECL which may contains a parameter with an
16341 unparsed DEFAULT_ARG. Parse the default args now. This function
16342 assumes that the current scope is the scope in which the default
16343 argument should be processed. */
16346 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
16348 bool saved_local_variables_forbidden_p
;
16351 /* While we're parsing the default args, we might (due to the
16352 statement expression extension) encounter more classes. We want
16353 to handle them right away, but we don't want them getting mixed
16354 up with default args that are currently in the queue. */
16355 parser
->unparsed_functions_queues
16356 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16358 /* Local variable names (and the `this' keyword) may not appear
16359 in a default argument. */
16360 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16361 parser
->local_variables_forbidden_p
= true;
16363 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16365 parm
= TREE_CHAIN (parm
))
16367 cp_token_cache
*tokens
;
16368 tree default_arg
= TREE_PURPOSE (parm
);
16370 VEC(tree
,gc
) *insts
;
16377 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16378 /* This can happen for a friend declaration for a function
16379 already declared with default arguments. */
16382 /* Push the saved tokens for the default argument onto the parser's
16384 tokens
= DEFARG_TOKENS (default_arg
);
16385 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16387 /* Parse the assignment-expression. */
16388 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16390 if (!processing_template_decl
)
16391 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16393 TREE_PURPOSE (parm
) = parsed_arg
;
16395 /* Update any instantiations we've already created. */
16396 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16397 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16398 TREE_PURPOSE (copy
) = parsed_arg
;
16400 /* If the token stream has not been completely used up, then
16401 there was extra junk after the end of the default
16403 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16404 cp_parser_error (parser
, "expected %<,%>");
16406 /* Revert to the main lexer. */
16407 cp_parser_pop_lexer (parser
);
16410 /* Make sure no default arg is missing. */
16411 check_default_args (fn
);
16413 /* Restore the state of local_variables_forbidden_p. */
16414 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16416 /* Restore the queue. */
16417 parser
->unparsed_functions_queues
16418 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16421 /* Parse the operand of `sizeof' (or a similar operator). Returns
16422 either a TYPE or an expression, depending on the form of the
16423 input. The KEYWORD indicates which kind of expression we have
16427 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16429 static const char *format
;
16430 tree expr
= NULL_TREE
;
16431 const char *saved_message
;
16432 bool saved_integral_constant_expression_p
;
16433 bool saved_non_integral_constant_expression_p
;
16435 /* Initialize FORMAT the first time we get here. */
16437 format
= "types may not be defined in '%s' expressions";
16439 /* Types cannot be defined in a `sizeof' expression. Save away the
16441 saved_message
= parser
->type_definition_forbidden_message
;
16442 /* And create the new one. */
16443 parser
->type_definition_forbidden_message
16444 = XNEWVEC (const char, strlen (format
)
16445 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16447 sprintf ((char *) parser
->type_definition_forbidden_message
,
16448 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16450 /* The restrictions on constant-expressions do not apply inside
16451 sizeof expressions. */
16452 saved_integral_constant_expression_p
16453 = parser
->integral_constant_expression_p
;
16454 saved_non_integral_constant_expression_p
16455 = parser
->non_integral_constant_expression_p
;
16456 parser
->integral_constant_expression_p
= false;
16458 /* Do not actually evaluate the expression. */
16460 /* If it's a `(', then we might be looking at the type-id
16462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16465 bool saved_in_type_id_in_expr_p
;
16467 /* We can't be sure yet whether we're looking at a type-id or an
16469 cp_parser_parse_tentatively (parser
);
16470 /* Consume the `('. */
16471 cp_lexer_consume_token (parser
->lexer
);
16472 /* Parse the type-id. */
16473 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16474 parser
->in_type_id_in_expr_p
= true;
16475 type
= cp_parser_type_id (parser
);
16476 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16477 /* Now, look for the trailing `)'. */
16478 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16479 /* If all went well, then we're done. */
16480 if (cp_parser_parse_definitely (parser
))
16482 cp_decl_specifier_seq decl_specs
;
16484 /* Build a trivial decl-specifier-seq. */
16485 clear_decl_specs (&decl_specs
);
16486 decl_specs
.type
= type
;
16488 /* Call grokdeclarator to figure out what type this is. */
16489 expr
= grokdeclarator (NULL
,
16493 /*attrlist=*/NULL
);
16497 /* If the type-id production did not work out, then we must be
16498 looking at the unary-expression production. */
16500 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16502 /* Go back to evaluating expressions. */
16505 /* Free the message we created. */
16506 free ((char *) parser
->type_definition_forbidden_message
);
16507 /* And restore the old one. */
16508 parser
->type_definition_forbidden_message
= saved_message
;
16509 parser
->integral_constant_expression_p
16510 = saved_integral_constant_expression_p
;
16511 parser
->non_integral_constant_expression_p
16512 = saved_non_integral_constant_expression_p
;
16517 /* If the current declaration has no declarator, return true. */
16520 cp_parser_declares_only_class_p (cp_parser
*parser
)
16522 /* If the next token is a `;' or a `,' then there is no
16524 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16525 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16528 /* Update the DECL_SPECS to reflect the storage class indicated by
16532 cp_parser_set_storage_class (cp_parser
*parser
,
16533 cp_decl_specifier_seq
*decl_specs
,
16536 cp_storage_class storage_class
;
16538 if (parser
->in_unbraced_linkage_specification_p
)
16540 error ("invalid use of %qD in linkage specification",
16541 ridpointers
[keyword
]);
16544 else if (decl_specs
->storage_class
!= sc_none
)
16546 decl_specs
->conflicting_specifiers_p
= true;
16550 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16551 && decl_specs
->specs
[(int) ds_thread
])
16553 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16554 decl_specs
->specs
[(int) ds_thread
] = 0;
16560 storage_class
= sc_auto
;
16563 storage_class
= sc_register
;
16566 storage_class
= sc_static
;
16569 storage_class
= sc_extern
;
16572 storage_class
= sc_mutable
;
16575 gcc_unreachable ();
16577 decl_specs
->storage_class
= storage_class
;
16579 /* A storage class specifier cannot be applied alongside a typedef
16580 specifier. If there is a typedef specifier present then set
16581 conflicting_specifiers_p which will trigger an error later
16582 on in grokdeclarator. */
16583 if (decl_specs
->specs
[(int)ds_typedef
])
16584 decl_specs
->conflicting_specifiers_p
= true;
16587 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16588 is true, the type is a user-defined type; otherwise it is a
16589 built-in type specified by a keyword. */
16592 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16594 bool user_defined_p
)
16596 decl_specs
->any_specifiers_p
= true;
16598 /* If the user tries to redeclare bool or wchar_t (with, for
16599 example, in "typedef int wchar_t;") we remember that this is what
16600 happened. In system headers, we ignore these declarations so
16601 that G++ can work with system headers that are not C++-safe. */
16602 if (decl_specs
->specs
[(int) ds_typedef
]
16604 && (type_spec
== boolean_type_node
16605 || type_spec
== wchar_type_node
)
16606 && (decl_specs
->type
16607 || decl_specs
->specs
[(int) ds_long
]
16608 || decl_specs
->specs
[(int) ds_short
]
16609 || decl_specs
->specs
[(int) ds_unsigned
]
16610 || decl_specs
->specs
[(int) ds_signed
]))
16612 decl_specs
->redefined_builtin_type
= type_spec
;
16613 if (!decl_specs
->type
)
16615 decl_specs
->type
= type_spec
;
16616 decl_specs
->user_defined_type_p
= false;
16619 else if (decl_specs
->type
)
16620 decl_specs
->multiple_types_p
= true;
16623 decl_specs
->type
= type_spec
;
16624 decl_specs
->user_defined_type_p
= user_defined_p
;
16625 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16629 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16630 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16633 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16635 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16638 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16639 issue an error message indicating that TOKEN_DESC was expected.
16641 Returns the token consumed, if the token had the appropriate type.
16642 Otherwise, returns NULL. */
16645 cp_parser_require (cp_parser
* parser
,
16646 enum cpp_ttype type
,
16647 const char* token_desc
)
16649 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16650 return cp_lexer_consume_token (parser
->lexer
);
16653 /* Output the MESSAGE -- unless we're parsing tentatively. */
16654 if (!cp_parser_simulate_error (parser
))
16656 char *message
= concat ("expected ", token_desc
, NULL
);
16657 cp_parser_error (parser
, message
);
16664 /* An error message is produced if the next token is not '>'.
16665 All further tokens are skipped until the desired token is
16666 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16669 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
16671 /* Current level of '< ... >'. */
16672 unsigned level
= 0;
16673 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16674 unsigned nesting_depth
= 0;
16676 /* Are we ready, yet? If not, issue error message. */
16677 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
16680 /* Skip tokens until the desired token is found. */
16683 /* Peek at the next token. */
16684 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
16687 if (!nesting_depth
)
16692 if (!nesting_depth
&& level
-- == 0)
16694 /* We've reached the token we want, consume it and stop. */
16695 cp_lexer_consume_token (parser
->lexer
);
16700 case CPP_OPEN_PAREN
:
16701 case CPP_OPEN_SQUARE
:
16705 case CPP_CLOSE_PAREN
:
16706 case CPP_CLOSE_SQUARE
:
16707 if (nesting_depth
-- == 0)
16712 case CPP_PRAGMA_EOL
:
16713 case CPP_SEMICOLON
:
16714 case CPP_OPEN_BRACE
:
16715 case CPP_CLOSE_BRACE
:
16716 /* The '>' was probably forgotten, don't look further. */
16723 /* Consume this token. */
16724 cp_lexer_consume_token (parser
->lexer
);
16728 /* If the next token is the indicated keyword, consume it. Otherwise,
16729 issue an error message indicating that TOKEN_DESC was expected.
16731 Returns the token consumed, if the token had the appropriate type.
16732 Otherwise, returns NULL. */
16735 cp_parser_require_keyword (cp_parser
* parser
,
16737 const char* token_desc
)
16739 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16741 if (token
&& token
->keyword
!= keyword
)
16743 dyn_string_t error_msg
;
16745 /* Format the error message. */
16746 error_msg
= dyn_string_new (0);
16747 dyn_string_append_cstr (error_msg
, "expected ");
16748 dyn_string_append_cstr (error_msg
, token_desc
);
16749 cp_parser_error (parser
, error_msg
->s
);
16750 dyn_string_delete (error_msg
);
16757 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16758 function-definition. */
16761 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16763 return (/* An ordinary function-body begins with an `{'. */
16764 token
->type
== CPP_OPEN_BRACE
16765 /* A ctor-initializer begins with a `:'. */
16766 || token
->type
== CPP_COLON
16767 /* A function-try-block begins with `try'. */
16768 || token
->keyword
== RID_TRY
16769 /* The named return value extension begins with `return'. */
16770 || token
->keyword
== RID_RETURN
);
16773 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16777 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16781 token
= cp_lexer_peek_token (parser
->lexer
);
16782 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16785 /* Returns TRUE iff the next token is the "," or ">" ending a
16786 template-argument. */
16789 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16793 token
= cp_lexer_peek_token (parser
->lexer
);
16794 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16797 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16798 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16801 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16806 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16807 if (token
->type
== CPP_LESS
)
16809 /* Check for the sequence `<::' in the original code. It would be lexed as
16810 `[:', where `[' is a digraph, and there is no whitespace before
16812 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16815 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16816 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16822 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16823 or none_type otherwise. */
16825 static enum tag_types
16826 cp_parser_token_is_class_key (cp_token
* token
)
16828 switch (token
->keyword
)
16833 return record_type
;
16842 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16845 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16847 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16848 pedwarn ("%qs tag used in naming %q#T",
16849 class_key
== union_type
? "union"
16850 : class_key
== record_type
? "struct" : "class",
16854 /* Issue an error message if DECL is redeclared with different
16855 access than its original declaration [class.access.spec/3].
16856 This applies to nested classes and nested class templates.
16860 cp_parser_check_access_in_redeclaration (tree decl
)
16862 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16865 if ((TREE_PRIVATE (decl
)
16866 != (current_access_specifier
== access_private_node
))
16867 || (TREE_PROTECTED (decl
)
16868 != (current_access_specifier
== access_protected_node
)))
16869 error ("%qD redeclared with different access", decl
);
16872 /* Look for the `template' keyword, as a syntactic disambiguator.
16873 Return TRUE iff it is present, in which case it will be
16877 cp_parser_optional_template_keyword (cp_parser
*parser
)
16879 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16881 /* The `template' keyword can only be used within templates;
16882 outside templates the parser can always figure out what is a
16883 template and what is not. */
16884 if (!processing_template_decl
)
16886 error ("%<template%> (as a disambiguator) is only allowed "
16887 "within templates");
16888 /* If this part of the token stream is rescanned, the same
16889 error message would be generated. So, we purge the token
16890 from the stream. */
16891 cp_lexer_purge_token (parser
->lexer
);
16896 /* Consume the `template' keyword. */
16897 cp_lexer_consume_token (parser
->lexer
);
16905 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16906 set PARSER->SCOPE, and perform other related actions. */
16909 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16912 struct tree_check
*check_value
;
16913 deferred_access_check
*chk
;
16914 VEC (deferred_access_check
,gc
) *checks
;
16916 /* Get the stored value. */
16917 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
16918 /* Perform any access checks that were deferred. */
16919 checks
= check_value
->checks
;
16923 VEC_iterate (deferred_access_check
, checks
, i
, chk
) ;
16926 perform_or_defer_access_check (chk
->binfo
,
16931 /* Set the scope from the stored value. */
16932 parser
->scope
= check_value
->value
;
16933 parser
->qualifying_scope
= check_value
->qualifying_scope
;
16934 parser
->object_scope
= NULL_TREE
;
16937 /* Consume tokens up through a non-nested END token. */
16940 cp_parser_cache_group (cp_parser
*parser
,
16941 enum cpp_ttype end
,
16948 /* Abort a parenthesized expression if we encounter a brace. */
16949 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16950 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16952 /* If we've reached the end of the file, stop. */
16953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16954 || (end
!= CPP_PRAGMA_EOL
16955 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16957 /* Consume the next token. */
16958 token
= cp_lexer_consume_token (parser
->lexer
);
16959 /* See if it starts a new group. */
16960 if (token
->type
== CPP_OPEN_BRACE
)
16962 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16966 else if (token
->type
== CPP_OPEN_PAREN
)
16967 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16968 else if (token
->type
== CPP_PRAGMA
)
16969 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16970 else if (token
->type
== end
)
16975 /* Begin parsing tentatively. We always save tokens while parsing
16976 tentatively so that if the tentative parsing fails we can restore the
16980 cp_parser_parse_tentatively (cp_parser
* parser
)
16982 /* Enter a new parsing context. */
16983 parser
->context
= cp_parser_context_new (parser
->context
);
16984 /* Begin saving tokens. */
16985 cp_lexer_save_tokens (parser
->lexer
);
16986 /* In order to avoid repetitive access control error messages,
16987 access checks are queued up until we are no longer parsing
16989 push_deferring_access_checks (dk_deferred
);
16992 /* Commit to the currently active tentative parse. */
16995 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16997 cp_parser_context
*context
;
17000 /* Mark all of the levels as committed. */
17001 lexer
= parser
->lexer
;
17002 for (context
= parser
->context
; context
->next
; context
= context
->next
)
17004 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
17006 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
17007 while (!cp_lexer_saving_tokens (lexer
))
17008 lexer
= lexer
->next
;
17009 cp_lexer_commit_tokens (lexer
);
17013 /* Abort the currently active tentative parse. All consumed tokens
17014 will be rolled back, and no diagnostics will be issued. */
17017 cp_parser_abort_tentative_parse (cp_parser
* parser
)
17019 cp_parser_simulate_error (parser
);
17020 /* Now, pretend that we want to see if the construct was
17021 successfully parsed. */
17022 cp_parser_parse_definitely (parser
);
17025 /* Stop parsing tentatively. If a parse error has occurred, restore the
17026 token stream. Otherwise, commit to the tokens we have consumed.
17027 Returns true if no error occurred; false otherwise. */
17030 cp_parser_parse_definitely (cp_parser
* parser
)
17032 bool error_occurred
;
17033 cp_parser_context
*context
;
17035 /* Remember whether or not an error occurred, since we are about to
17036 destroy that information. */
17037 error_occurred
= cp_parser_error_occurred (parser
);
17038 /* Remove the topmost context from the stack. */
17039 context
= parser
->context
;
17040 parser
->context
= context
->next
;
17041 /* If no parse errors occurred, commit to the tentative parse. */
17042 if (!error_occurred
)
17044 /* Commit to the tokens read tentatively, unless that was
17046 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
17047 cp_lexer_commit_tokens (parser
->lexer
);
17049 pop_to_parent_deferring_access_checks ();
17051 /* Otherwise, if errors occurred, roll back our state so that things
17052 are just as they were before we began the tentative parse. */
17055 cp_lexer_rollback_tokens (parser
->lexer
);
17056 pop_deferring_access_checks ();
17058 /* Add the context to the front of the free list. */
17059 context
->next
= cp_parser_context_free_list
;
17060 cp_parser_context_free_list
= context
;
17062 return !error_occurred
;
17065 /* Returns true if we are parsing tentatively and are not committed to
17066 this tentative parse. */
17069 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
17071 return (cp_parser_parsing_tentatively (parser
)
17072 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
17075 /* Returns nonzero iff an error has occurred during the most recent
17076 tentative parse. */
17079 cp_parser_error_occurred (cp_parser
* parser
)
17081 return (cp_parser_parsing_tentatively (parser
)
17082 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
17085 /* Returns nonzero if GNU extensions are allowed. */
17088 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
17090 return parser
->allow_gnu_extensions_p
;
17093 /* Objective-C++ Productions */
17096 /* Parse an Objective-C expression, which feeds into a primary-expression
17100 objc-message-expression
17101 objc-string-literal
17102 objc-encode-expression
17103 objc-protocol-expression
17104 objc-selector-expression
17106 Returns a tree representation of the expression. */
17109 cp_parser_objc_expression (cp_parser
* parser
)
17111 /* Try to figure out what kind of declaration is present. */
17112 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17116 case CPP_OPEN_SQUARE
:
17117 return cp_parser_objc_message_expression (parser
);
17119 case CPP_OBJC_STRING
:
17120 kwd
= cp_lexer_consume_token (parser
->lexer
);
17121 return objc_build_string_object (kwd
->u
.value
);
17124 switch (kwd
->keyword
)
17126 case RID_AT_ENCODE
:
17127 return cp_parser_objc_encode_expression (parser
);
17129 case RID_AT_PROTOCOL
:
17130 return cp_parser_objc_protocol_expression (parser
);
17132 case RID_AT_SELECTOR
:
17133 return cp_parser_objc_selector_expression (parser
);
17139 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
17140 cp_parser_skip_to_end_of_block_or_statement (parser
);
17143 return error_mark_node
;
17146 /* Parse an Objective-C message expression.
17148 objc-message-expression:
17149 [ objc-message-receiver objc-message-args ]
17151 Returns a representation of an Objective-C message. */
17154 cp_parser_objc_message_expression (cp_parser
* parser
)
17156 tree receiver
, messageargs
;
17158 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
17159 receiver
= cp_parser_objc_message_receiver (parser
);
17160 messageargs
= cp_parser_objc_message_args (parser
);
17161 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
17163 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
17166 /* Parse an objc-message-receiver.
17168 objc-message-receiver:
17170 simple-type-specifier
17172 Returns a representation of the type or expression. */
17175 cp_parser_objc_message_receiver (cp_parser
* parser
)
17179 /* An Objective-C message receiver may be either (1) a type
17180 or (2) an expression. */
17181 cp_parser_parse_tentatively (parser
);
17182 rcv
= cp_parser_expression (parser
, false);
17184 if (cp_parser_parse_definitely (parser
))
17187 rcv
= cp_parser_simple_type_specifier (parser
,
17188 /*decl_specs=*/NULL
,
17189 CP_PARSER_FLAGS_NONE
);
17191 return objc_get_class_reference (rcv
);
17194 /* Parse the arguments and selectors comprising an Objective-C message.
17199 objc-selector-args , objc-comma-args
17201 objc-selector-args:
17202 objc-selector [opt] : assignment-expression
17203 objc-selector-args objc-selector [opt] : assignment-expression
17206 assignment-expression
17207 objc-comma-args , assignment-expression
17209 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17210 selector arguments and TREE_VALUE containing a list of comma
17214 cp_parser_objc_message_args (cp_parser
* parser
)
17216 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
17217 bool maybe_unary_selector_p
= true;
17218 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17220 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17222 tree selector
= NULL_TREE
, arg
;
17224 if (token
->type
!= CPP_COLON
)
17225 selector
= cp_parser_objc_selector (parser
);
17227 /* Detect if we have a unary selector. */
17228 if (maybe_unary_selector_p
17229 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17230 return build_tree_list (selector
, NULL_TREE
);
17232 maybe_unary_selector_p
= false;
17233 cp_parser_require (parser
, CPP_COLON
, "`:'");
17234 arg
= cp_parser_assignment_expression (parser
, false);
17237 = chainon (sel_args
,
17238 build_tree_list (selector
, arg
));
17240 token
= cp_lexer_peek_token (parser
->lexer
);
17243 /* Handle non-selector arguments, if any. */
17244 while (token
->type
== CPP_COMMA
)
17248 cp_lexer_consume_token (parser
->lexer
);
17249 arg
= cp_parser_assignment_expression (parser
, false);
17252 = chainon (addl_args
,
17253 build_tree_list (NULL_TREE
, arg
));
17255 token
= cp_lexer_peek_token (parser
->lexer
);
17258 return build_tree_list (sel_args
, addl_args
);
17261 /* Parse an Objective-C encode expression.
17263 objc-encode-expression:
17264 @encode objc-typename
17266 Returns an encoded representation of the type argument. */
17269 cp_parser_objc_encode_expression (cp_parser
* parser
)
17273 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
17274 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17275 type
= complete_type (cp_parser_type_id (parser
));
17276 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17280 error ("%<@encode%> must specify a type as an argument");
17281 return error_mark_node
;
17284 return objc_build_encode_expr (type
);
17287 /* Parse an Objective-C @defs expression. */
17290 cp_parser_objc_defs_expression (cp_parser
*parser
)
17294 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
17295 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17296 name
= cp_parser_identifier (parser
);
17297 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17299 return objc_get_class_ivars (name
);
17302 /* Parse an Objective-C protocol expression.
17304 objc-protocol-expression:
17305 @protocol ( identifier )
17307 Returns a representation of the protocol expression. */
17310 cp_parser_objc_protocol_expression (cp_parser
* parser
)
17314 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17315 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17316 proto
= cp_parser_identifier (parser
);
17317 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17319 return objc_build_protocol_expr (proto
);
17322 /* Parse an Objective-C selector expression.
17324 objc-selector-expression:
17325 @selector ( objc-method-signature )
17327 objc-method-signature:
17333 objc-selector-seq objc-selector :
17335 Returns a representation of the method selector. */
17338 cp_parser_objc_selector_expression (cp_parser
* parser
)
17340 tree sel_seq
= NULL_TREE
;
17341 bool maybe_unary_selector_p
= true;
17344 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
17345 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17346 token
= cp_lexer_peek_token (parser
->lexer
);
17348 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
17349 || token
->type
== CPP_SCOPE
)
17351 tree selector
= NULL_TREE
;
17353 if (token
->type
!= CPP_COLON
17354 || token
->type
== CPP_SCOPE
)
17355 selector
= cp_parser_objc_selector (parser
);
17357 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
17358 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
17360 /* Detect if we have a unary selector. */
17361 if (maybe_unary_selector_p
)
17363 sel_seq
= selector
;
17364 goto finish_selector
;
17368 cp_parser_error (parser
, "expected %<:%>");
17371 maybe_unary_selector_p
= false;
17372 token
= cp_lexer_consume_token (parser
->lexer
);
17374 if (token
->type
== CPP_SCOPE
)
17377 = chainon (sel_seq
,
17378 build_tree_list (selector
, NULL_TREE
));
17380 = chainon (sel_seq
,
17381 build_tree_list (NULL_TREE
, NULL_TREE
));
17385 = chainon (sel_seq
,
17386 build_tree_list (selector
, NULL_TREE
));
17388 token
= cp_lexer_peek_token (parser
->lexer
);
17392 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17394 return objc_build_selector_expr (sel_seq
);
17397 /* Parse a list of identifiers.
17399 objc-identifier-list:
17401 objc-identifier-list , identifier
17403 Returns a TREE_LIST of identifier nodes. */
17406 cp_parser_objc_identifier_list (cp_parser
* parser
)
17408 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17409 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17411 while (sep
->type
== CPP_COMMA
)
17413 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17414 list
= chainon (list
,
17415 build_tree_list (NULL_TREE
,
17416 cp_parser_identifier (parser
)));
17417 sep
= cp_lexer_peek_token (parser
->lexer
);
17423 /* Parse an Objective-C alias declaration.
17425 objc-alias-declaration:
17426 @compatibility_alias identifier identifier ;
17428 This function registers the alias mapping with the Objective-C front end.
17429 It returns nothing. */
17432 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17436 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17437 alias
= cp_parser_identifier (parser
);
17438 orig
= cp_parser_identifier (parser
);
17439 objc_declare_alias (alias
, orig
);
17440 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17443 /* Parse an Objective-C class forward-declaration.
17445 objc-class-declaration:
17446 @class objc-identifier-list ;
17448 The function registers the forward declarations with the Objective-C
17449 front end. It returns nothing. */
17452 cp_parser_objc_class_declaration (cp_parser
* parser
)
17454 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17455 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17456 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17459 /* Parse a list of Objective-C protocol references.
17461 objc-protocol-refs-opt:
17462 objc-protocol-refs [opt]
17464 objc-protocol-refs:
17465 < objc-identifier-list >
17467 Returns a TREE_LIST of identifiers, if any. */
17470 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17472 tree protorefs
= NULL_TREE
;
17474 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17476 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17477 protorefs
= cp_parser_objc_identifier_list (parser
);
17478 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17484 /* Parse a Objective-C visibility specification. */
17487 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17489 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17491 switch (vis
->keyword
)
17493 case RID_AT_PRIVATE
:
17494 objc_set_visibility (2);
17496 case RID_AT_PROTECTED
:
17497 objc_set_visibility (0);
17499 case RID_AT_PUBLIC
:
17500 objc_set_visibility (1);
17506 /* Eat '@private'/'@protected'/'@public'. */
17507 cp_lexer_consume_token (parser
->lexer
);
17510 /* Parse an Objective-C method type. */
17513 cp_parser_objc_method_type (cp_parser
* parser
)
17515 objc_set_method_type
17516 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17521 /* Parse an Objective-C protocol qualifier. */
17524 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17526 tree quals
= NULL_TREE
, node
;
17527 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17529 node
= token
->u
.value
;
17531 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17532 && (node
== ridpointers
[(int) RID_IN
]
17533 || node
== ridpointers
[(int) RID_OUT
]
17534 || node
== ridpointers
[(int) RID_INOUT
]
17535 || node
== ridpointers
[(int) RID_BYCOPY
]
17536 || node
== ridpointers
[(int) RID_BYREF
]
17537 || node
== ridpointers
[(int) RID_ONEWAY
]))
17539 quals
= tree_cons (NULL_TREE
, node
, quals
);
17540 cp_lexer_consume_token (parser
->lexer
);
17541 token
= cp_lexer_peek_token (parser
->lexer
);
17542 node
= token
->u
.value
;
17548 /* Parse an Objective-C typename. */
17551 cp_parser_objc_typename (cp_parser
* parser
)
17553 tree typename
= NULL_TREE
;
17555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17557 tree proto_quals
, cp_type
= NULL_TREE
;
17559 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17560 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17562 /* An ObjC type name may consist of just protocol qualifiers, in which
17563 case the type shall default to 'id'. */
17564 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17565 cp_type
= cp_parser_type_id (parser
);
17567 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17568 typename
= build_tree_list (proto_quals
, cp_type
);
17574 /* Check to see if TYPE refers to an Objective-C selector name. */
17577 cp_parser_objc_selector_p (enum cpp_ttype type
)
17579 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17580 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17581 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17582 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17583 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17586 /* Parse an Objective-C selector. */
17589 cp_parser_objc_selector (cp_parser
* parser
)
17591 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17593 if (!cp_parser_objc_selector_p (token
->type
))
17595 error ("invalid Objective-C++ selector name");
17596 return error_mark_node
;
17599 /* C++ operator names are allowed to appear in ObjC selectors. */
17600 switch (token
->type
)
17602 case CPP_AND_AND
: return get_identifier ("and");
17603 case CPP_AND_EQ
: return get_identifier ("and_eq");
17604 case CPP_AND
: return get_identifier ("bitand");
17605 case CPP_OR
: return get_identifier ("bitor");
17606 case CPP_COMPL
: return get_identifier ("compl");
17607 case CPP_NOT
: return get_identifier ("not");
17608 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17609 case CPP_OR_OR
: return get_identifier ("or");
17610 case CPP_OR_EQ
: return get_identifier ("or_eq");
17611 case CPP_XOR
: return get_identifier ("xor");
17612 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17613 default: return token
->u
.value
;
17617 /* Parse an Objective-C params list. */
17620 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17622 tree params
= NULL_TREE
;
17623 bool maybe_unary_selector_p
= true;
17624 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17626 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17628 tree selector
= NULL_TREE
, typename
, identifier
;
17630 if (token
->type
!= CPP_COLON
)
17631 selector
= cp_parser_objc_selector (parser
);
17633 /* Detect if we have a unary selector. */
17634 if (maybe_unary_selector_p
17635 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17638 maybe_unary_selector_p
= false;
17639 cp_parser_require (parser
, CPP_COLON
, "`:'");
17640 typename
= cp_parser_objc_typename (parser
);
17641 identifier
= cp_parser_identifier (parser
);
17645 objc_build_keyword_decl (selector
,
17649 token
= cp_lexer_peek_token (parser
->lexer
);
17655 /* Parse the non-keyword Objective-C params. */
17658 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17660 tree params
= make_node (TREE_LIST
);
17661 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17662 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17664 while (token
->type
== CPP_COMMA
)
17666 cp_parameter_declarator
*parmdecl
;
17669 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17670 token
= cp_lexer_peek_token (parser
->lexer
);
17672 if (token
->type
== CPP_ELLIPSIS
)
17674 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17679 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17680 parm
= grokdeclarator (parmdecl
->declarator
,
17681 &parmdecl
->decl_specifiers
,
17682 PARM
, /*initialized=*/0,
17683 /*attrlist=*/NULL
);
17685 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17686 token
= cp_lexer_peek_token (parser
->lexer
);
17692 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17695 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17699 /* If the next token is `extern' and the following token is a string
17700 literal, then we have a linkage specification. */
17701 if (token
->keyword
== RID_EXTERN
17702 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17703 cp_parser_linkage_specification (parser
);
17704 /* Handle #pragma, if any. */
17705 else if (token
->type
== CPP_PRAGMA
)
17706 cp_parser_pragma (parser
, pragma_external
);
17707 /* Allow stray semicolons. */
17708 else if (token
->type
== CPP_SEMICOLON
)
17709 cp_lexer_consume_token (parser
->lexer
);
17710 /* Finally, try to parse a block-declaration, or a function-definition. */
17712 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17715 /* Parse a method signature. */
17718 cp_parser_objc_method_signature (cp_parser
* parser
)
17720 tree rettype
, kwdparms
, optparms
;
17721 bool ellipsis
= false;
17723 cp_parser_objc_method_type (parser
);
17724 rettype
= cp_parser_objc_typename (parser
);
17725 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17726 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17728 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17731 /* Pars an Objective-C method prototype list. */
17734 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17738 while (token
->keyword
!= RID_AT_END
)
17740 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17742 objc_add_method_declaration
17743 (cp_parser_objc_method_signature (parser
));
17744 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17747 /* Allow for interspersed non-ObjC++ code. */
17748 cp_parser_objc_interstitial_code (parser
);
17750 token
= cp_lexer_peek_token (parser
->lexer
);
17753 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17754 objc_finish_interface ();
17757 /* Parse an Objective-C method definition list. */
17760 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17762 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17764 while (token
->keyword
!= RID_AT_END
)
17768 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17770 push_deferring_access_checks (dk_deferred
);
17771 objc_start_method_definition
17772 (cp_parser_objc_method_signature (parser
));
17774 /* For historical reasons, we accept an optional semicolon. */
17775 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17776 cp_lexer_consume_token (parser
->lexer
);
17778 perform_deferred_access_checks ();
17779 stop_deferring_access_checks ();
17780 meth
= cp_parser_function_definition_after_declarator (parser
,
17782 pop_deferring_access_checks ();
17783 objc_finish_method_definition (meth
);
17786 /* Allow for interspersed non-ObjC++ code. */
17787 cp_parser_objc_interstitial_code (parser
);
17789 token
= cp_lexer_peek_token (parser
->lexer
);
17792 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17793 objc_finish_implementation ();
17796 /* Parse Objective-C ivars. */
17799 cp_parser_objc_class_ivars (cp_parser
* parser
)
17801 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17803 if (token
->type
!= CPP_OPEN_BRACE
)
17804 return; /* No ivars specified. */
17806 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17807 token
= cp_lexer_peek_token (parser
->lexer
);
17809 while (token
->type
!= CPP_CLOSE_BRACE
)
17811 cp_decl_specifier_seq declspecs
;
17812 int decl_class_or_enum_p
;
17813 tree prefix_attributes
;
17815 cp_parser_objc_visibility_spec (parser
);
17817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17820 cp_parser_decl_specifier_seq (parser
,
17821 CP_PARSER_FLAGS_OPTIONAL
,
17823 &decl_class_or_enum_p
);
17824 prefix_attributes
= declspecs
.attributes
;
17825 declspecs
.attributes
= NULL_TREE
;
17827 /* Keep going until we hit the `;' at the end of the
17829 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17831 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17832 cp_declarator
*declarator
= NULL
;
17833 int ctor_dtor_or_conv_p
;
17835 /* Check for a (possibly unnamed) bitfield declaration. */
17836 token
= cp_lexer_peek_token (parser
->lexer
);
17837 if (token
->type
== CPP_COLON
)
17840 if (token
->type
== CPP_NAME
17841 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17844 /* Get the name of the bitfield. */
17845 declarator
= make_id_declarator (NULL_TREE
,
17846 cp_parser_identifier (parser
),
17850 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17851 /* Get the width of the bitfield. */
17853 = cp_parser_constant_expression (parser
,
17854 /*allow_non_constant=*/false,
17859 /* Parse the declarator. */
17861 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17862 &ctor_dtor_or_conv_p
,
17863 /*parenthesized_p=*/NULL
,
17864 /*member_p=*/false);
17867 /* Look for attributes that apply to the ivar. */
17868 attributes
= cp_parser_attributes_opt (parser
);
17869 /* Remember which attributes are prefix attributes and
17871 first_attribute
= attributes
;
17872 /* Combine the attributes. */
17873 attributes
= chainon (prefix_attributes
, attributes
);
17877 /* Create the bitfield declaration. */
17878 decl
= grokbitfield (declarator
, &declspecs
, width
);
17879 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17882 decl
= grokfield (declarator
, &declspecs
,
17883 NULL_TREE
, /*init_const_expr_p=*/false,
17884 NULL_TREE
, attributes
);
17886 /* Add the instance variable. */
17887 objc_add_instance_variable (decl
);
17889 /* Reset PREFIX_ATTRIBUTES. */
17890 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17891 attributes
= TREE_CHAIN (attributes
);
17893 TREE_CHAIN (attributes
) = NULL_TREE
;
17895 token
= cp_lexer_peek_token (parser
->lexer
);
17897 if (token
->type
== CPP_COMMA
)
17899 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17905 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17906 token
= cp_lexer_peek_token (parser
->lexer
);
17909 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17910 /* For historical reasons, we accept an optional semicolon. */
17911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17912 cp_lexer_consume_token (parser
->lexer
);
17915 /* Parse an Objective-C protocol declaration. */
17918 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17920 tree proto
, protorefs
;
17923 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17924 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17926 error ("identifier expected after %<@protocol%>");
17930 /* See if we have a forward declaration or a definition. */
17931 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17933 /* Try a forward declaration first. */
17934 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17936 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17938 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17941 /* Ok, we got a full-fledged definition (or at least should). */
17944 proto
= cp_parser_identifier (parser
);
17945 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17946 objc_start_protocol (proto
, protorefs
);
17947 cp_parser_objc_method_prototype_list (parser
);
17951 /* Parse an Objective-C superclass or category. */
17954 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17957 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17959 *super
= *categ
= NULL_TREE
;
17960 if (next
->type
== CPP_COLON
)
17962 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17963 *super
= cp_parser_identifier (parser
);
17965 else if (next
->type
== CPP_OPEN_PAREN
)
17967 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17968 *categ
= cp_parser_identifier (parser
);
17969 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17973 /* Parse an Objective-C class interface. */
17976 cp_parser_objc_class_interface (cp_parser
* parser
)
17978 tree name
, super
, categ
, protos
;
17980 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17981 name
= cp_parser_identifier (parser
);
17982 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17983 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17985 /* We have either a class or a category on our hands. */
17987 objc_start_category_interface (name
, categ
, protos
);
17990 objc_start_class_interface (name
, super
, protos
);
17991 /* Handle instance variable declarations, if any. */
17992 cp_parser_objc_class_ivars (parser
);
17993 objc_continue_interface ();
17996 cp_parser_objc_method_prototype_list (parser
);
17999 /* Parse an Objective-C class implementation. */
18002 cp_parser_objc_class_implementation (cp_parser
* parser
)
18004 tree name
, super
, categ
;
18006 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
18007 name
= cp_parser_identifier (parser
);
18008 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
18010 /* We have either a class or a category on our hands. */
18012 objc_start_category_implementation (name
, categ
);
18015 objc_start_class_implementation (name
, super
);
18016 /* Handle instance variable declarations, if any. */
18017 cp_parser_objc_class_ivars (parser
);
18018 objc_continue_implementation ();
18021 cp_parser_objc_method_definition_list (parser
);
18024 /* Consume the @end token and finish off the implementation. */
18027 cp_parser_objc_end_implementation (cp_parser
* parser
)
18029 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18030 objc_finish_implementation ();
18033 /* Parse an Objective-C declaration. */
18036 cp_parser_objc_declaration (cp_parser
* parser
)
18038 /* Try to figure out what kind of declaration is present. */
18039 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18041 switch (kwd
->keyword
)
18044 cp_parser_objc_alias_declaration (parser
);
18047 cp_parser_objc_class_declaration (parser
);
18049 case RID_AT_PROTOCOL
:
18050 cp_parser_objc_protocol_declaration (parser
);
18052 case RID_AT_INTERFACE
:
18053 cp_parser_objc_class_interface (parser
);
18055 case RID_AT_IMPLEMENTATION
:
18056 cp_parser_objc_class_implementation (parser
);
18059 cp_parser_objc_end_implementation (parser
);
18062 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18063 cp_parser_skip_to_end_of_block_or_statement (parser
);
18067 /* Parse an Objective-C try-catch-finally statement.
18069 objc-try-catch-finally-stmt:
18070 @try compound-statement objc-catch-clause-seq [opt]
18071 objc-finally-clause [opt]
18073 objc-catch-clause-seq:
18074 objc-catch-clause objc-catch-clause-seq [opt]
18077 @catch ( exception-declaration ) compound-statement
18079 objc-finally-clause
18080 @finally compound-statement
18082 Returns NULL_TREE. */
18085 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
18086 location_t location
;
18089 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
18090 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18091 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18092 node, lest it get absorbed into the surrounding block. */
18093 stmt
= push_stmt_list ();
18094 cp_parser_compound_statement (parser
, NULL
, false);
18095 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
18097 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
18099 cp_parameter_declarator
*parmdecl
;
18102 cp_lexer_consume_token (parser
->lexer
);
18103 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18104 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18105 parm
= grokdeclarator (parmdecl
->declarator
,
18106 &parmdecl
->decl_specifiers
,
18107 PARM
, /*initialized=*/0,
18108 /*attrlist=*/NULL
);
18109 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18110 objc_begin_catch_clause (parm
);
18111 cp_parser_compound_statement (parser
, NULL
, false);
18112 objc_finish_catch_clause ();
18115 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
18117 cp_lexer_consume_token (parser
->lexer
);
18118 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18119 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18120 node, lest it get absorbed into the surrounding block. */
18121 stmt
= push_stmt_list ();
18122 cp_parser_compound_statement (parser
, NULL
, false);
18123 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
18126 return objc_finish_try_stmt ();
18129 /* Parse an Objective-C synchronized statement.
18131 objc-synchronized-stmt:
18132 @synchronized ( expression ) compound-statement
18134 Returns NULL_TREE. */
18137 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
18138 location_t location
;
18141 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
18143 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18144 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18145 lock
= cp_parser_expression (parser
, false);
18146 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18148 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18149 node, lest it get absorbed into the surrounding block. */
18150 stmt
= push_stmt_list ();
18151 cp_parser_compound_statement (parser
, NULL
, false);
18153 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
18156 /* Parse an Objective-C throw statement.
18159 @throw assignment-expression [opt] ;
18161 Returns a constructed '@throw' statement. */
18164 cp_parser_objc_throw_statement (cp_parser
*parser
) {
18165 tree expr
= NULL_TREE
;
18167 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
18169 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18170 expr
= cp_parser_assignment_expression (parser
, false);
18172 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18174 return objc_build_throw_stmt (expr
);
18177 /* Parse an Objective-C statement. */
18180 cp_parser_objc_statement (cp_parser
* parser
) {
18181 /* Try to figure out what kind of declaration is present. */
18182 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18184 switch (kwd
->keyword
)
18187 return cp_parser_objc_try_catch_finally_statement (parser
);
18188 case RID_AT_SYNCHRONIZED
:
18189 return cp_parser_objc_synchronized_statement (parser
);
18191 return cp_parser_objc_throw_statement (parser
);
18193 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18194 cp_parser_skip_to_end_of_block_or_statement (parser
);
18197 return error_mark_node
;
18200 /* OpenMP 2.5 parsing routines. */
18202 /* Returns name of the next clause.
18203 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18204 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18205 returned and the token is consumed. */
18207 static pragma_omp_clause
18208 cp_parser_omp_clause_name (cp_parser
*parser
)
18210 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
18212 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
18213 result
= PRAGMA_OMP_CLAUSE_IF
;
18214 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
18215 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
18216 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
18217 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
18218 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18220 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18221 const char *p
= IDENTIFIER_POINTER (id
);
18226 if (!strcmp ("copyin", p
))
18227 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
18228 else if (!strcmp ("copyprivate", p
))
18229 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
18232 if (!strcmp ("firstprivate", p
))
18233 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
18236 if (!strcmp ("lastprivate", p
))
18237 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
18240 if (!strcmp ("nowait", p
))
18241 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
18242 else if (!strcmp ("num_threads", p
))
18243 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
18246 if (!strcmp ("ordered", p
))
18247 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
18250 if (!strcmp ("reduction", p
))
18251 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
18254 if (!strcmp ("schedule", p
))
18255 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
18256 else if (!strcmp ("shared", p
))
18257 result
= PRAGMA_OMP_CLAUSE_SHARED
;
18262 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
18263 cp_lexer_consume_token (parser
->lexer
);
18268 /* Validate that a clause of the given type does not already exist. */
18271 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
18275 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
18276 if (OMP_CLAUSE_CODE (c
) == code
)
18278 error ("too many %qs clauses", name
);
18286 variable-list , identifier
18288 In addition, we match a closing parenthesis. An opening parenthesis
18289 will have been consumed by the caller.
18291 If KIND is nonzero, create the appropriate node and install the decl
18292 in OMP_CLAUSE_DECL and add the node to the head of the list.
18294 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18295 return the list created. */
18298 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
18305 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
18306 /*check_dependency_p=*/true,
18307 /*template_p=*/NULL
,
18308 /*declarator_p=*/false,
18309 /*optional_p=*/false);
18310 if (name
== error_mark_node
)
18313 decl
= cp_parser_lookup_name_simple (parser
, name
);
18314 if (decl
== error_mark_node
)
18315 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
18316 else if (kind
!= 0)
18318 tree u
= build_omp_clause (kind
);
18319 OMP_CLAUSE_DECL (u
) = decl
;
18320 OMP_CLAUSE_CHAIN (u
) = list
;
18324 list
= tree_cons (decl
, NULL_TREE
, list
);
18327 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18329 cp_lexer_consume_token (parser
->lexer
);
18332 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18336 /* Try to resync to an unnested comma. Copied from
18337 cp_parser_parenthesized_expression_list. */
18339 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
18340 /*recovering=*/true,
18342 /*consume_paren=*/true);
18350 /* Similarly, but expect leading and trailing parenthesis. This is a very
18351 common case for omp clauses. */
18354 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18356 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18357 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18362 default ( shared | none ) */
18365 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18367 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18370 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18374 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18375 const char *p
= IDENTIFIER_POINTER (id
);
18380 if (strcmp ("none", p
) != 0)
18382 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18386 if (strcmp ("shared", p
) != 0)
18388 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18395 cp_lexer_consume_token (parser
->lexer
);
18400 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18403 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18404 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18405 /*or_comma=*/false,
18406 /*consume_paren=*/true);
18408 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18411 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18412 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18413 OMP_CLAUSE_CHAIN (c
) = list
;
18414 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18420 if ( expression ) */
18423 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18427 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18430 t
= cp_parser_condition (parser
);
18432 if (t
== error_mark_node
18433 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18434 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18435 /*or_comma=*/false,
18436 /*consume_paren=*/true);
18438 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18440 c
= build_omp_clause (OMP_CLAUSE_IF
);
18441 OMP_CLAUSE_IF_EXPR (c
) = t
;
18442 OMP_CLAUSE_CHAIN (c
) = list
;
18451 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18455 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18457 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18458 OMP_CLAUSE_CHAIN (c
) = list
;
18463 num_threads ( expression ) */
18466 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18470 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18473 t
= cp_parser_expression (parser
, false);
18475 if (t
== error_mark_node
18476 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18477 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18478 /*or_comma=*/false,
18479 /*consume_paren=*/true);
18481 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18483 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18484 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18485 OMP_CLAUSE_CHAIN (c
) = list
;
18494 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18498 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18500 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18501 OMP_CLAUSE_CHAIN (c
) = list
;
18506 reduction ( reduction-operator : variable-list )
18508 reduction-operator:
18509 One of: + * - & ^ | && || */
18512 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18514 enum tree_code code
;
18517 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18520 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18532 code
= BIT_AND_EXPR
;
18535 code
= BIT_XOR_EXPR
;
18538 code
= BIT_IOR_EXPR
;
18541 code
= TRUTH_ANDIF_EXPR
;
18544 code
= TRUTH_ORIF_EXPR
;
18547 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18549 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18550 /*or_comma=*/false,
18551 /*consume_paren=*/true);
18554 cp_lexer_consume_token (parser
->lexer
);
18556 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18559 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18560 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18561 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18567 schedule ( schedule-kind )
18568 schedule ( schedule-kind , expression )
18571 static | dynamic | guided | runtime */
18574 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18578 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18581 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18585 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18586 const char *p
= IDENTIFIER_POINTER (id
);
18591 if (strcmp ("dynamic", p
) != 0)
18593 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18597 if (strcmp ("guided", p
) != 0)
18599 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18603 if (strcmp ("runtime", p
) != 0)
18605 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18612 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18613 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18616 cp_lexer_consume_token (parser
->lexer
);
18618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18620 cp_lexer_consume_token (parser
->lexer
);
18622 t
= cp_parser_assignment_expression (parser
, false);
18624 if (t
== error_mark_node
)
18626 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18627 error ("schedule %<runtime%> does not take "
18628 "a %<chunk_size%> parameter");
18630 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18632 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18635 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18638 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18639 OMP_CLAUSE_CHAIN (c
) = list
;
18643 cp_parser_error (parser
, "invalid schedule kind");
18645 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18646 /*or_comma=*/false,
18647 /*consume_paren=*/true);
18651 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18652 is a bitmask in MASK. Return the list of clauses found; the result
18653 of clause default goes in *pdefault. */
18656 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18657 const char *where
, cp_token
*pragma_tok
)
18659 tree clauses
= NULL
;
18661 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18663 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18664 const char *c_name
;
18665 tree prev
= clauses
;
18669 case PRAGMA_OMP_CLAUSE_COPYIN
:
18670 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18673 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18674 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18676 c_name
= "copyprivate";
18678 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18679 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18680 c_name
= "default";
18682 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18683 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18685 c_name
= "firstprivate";
18687 case PRAGMA_OMP_CLAUSE_IF
:
18688 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18691 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18692 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18694 c_name
= "lastprivate";
18696 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18697 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18700 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18701 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18702 c_name
= "num_threads";
18704 case PRAGMA_OMP_CLAUSE_ORDERED
:
18705 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18706 c_name
= "ordered";
18708 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18709 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18711 c_name
= "private";
18713 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18714 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18715 c_name
= "reduction";
18717 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18718 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18719 c_name
= "schedule";
18721 case PRAGMA_OMP_CLAUSE_SHARED
:
18722 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18727 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18731 if (((mask
>> c_kind
) & 1) == 0)
18733 /* Remove the invalid clause(s) from the list to avoid
18734 confusing the rest of the compiler. */
18736 error ("%qs is not valid for %qs", c_name
, where
);
18740 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18741 return finish_omp_clauses (clauses
);
18748 In practice, we're also interested in adding the statement to an
18749 outer node. So it is convenient if we work around the fact that
18750 cp_parser_statement calls add_stmt. */
18753 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18755 unsigned save
= parser
->in_statement
;
18757 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18758 This preserves the "not within loop or switch" style error messages
18759 for nonsense cases like
18765 if (parser
->in_statement
)
18766 parser
->in_statement
= IN_OMP_BLOCK
;
18772 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18774 parser
->in_statement
= save
;
18778 cp_parser_omp_structured_block (cp_parser
*parser
)
18780 tree stmt
= begin_omp_structured_block ();
18781 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18783 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
18785 cp_parser_end_omp_structured_block (parser
, save
);
18786 return finish_omp_structured_block (stmt
);
18790 # pragma omp atomic new-line
18794 x binop= expr | x++ | ++x | x-- | --x
18796 +, *, -, /, &, ^, |, <<, >>
18798 where x is an lvalue expression with scalar type. */
18801 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18804 enum tree_code code
;
18806 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18808 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18810 switch (TREE_CODE (lhs
))
18815 case PREINCREMENT_EXPR
:
18816 case POSTINCREMENT_EXPR
:
18817 lhs
= TREE_OPERAND (lhs
, 0);
18819 rhs
= integer_one_node
;
18822 case PREDECREMENT_EXPR
:
18823 case POSTDECREMENT_EXPR
:
18824 lhs
= TREE_OPERAND (lhs
, 0);
18826 rhs
= integer_one_node
;
18830 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18836 code
= TRUNC_DIV_EXPR
;
18844 case CPP_LSHIFT_EQ
:
18845 code
= LSHIFT_EXPR
;
18847 case CPP_RSHIFT_EQ
:
18848 code
= RSHIFT_EXPR
;
18851 code
= BIT_AND_EXPR
;
18854 code
= BIT_IOR_EXPR
;
18857 code
= BIT_XOR_EXPR
;
18860 cp_parser_error (parser
,
18861 "invalid operator for %<#pragma omp atomic%>");
18864 cp_lexer_consume_token (parser
->lexer
);
18866 rhs
= cp_parser_expression (parser
, false);
18867 if (rhs
== error_mark_node
)
18871 finish_omp_atomic (code
, lhs
, rhs
);
18872 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18876 cp_parser_skip_to_end_of_block_or_statement (parser
);
18881 # pragma omp barrier new-line */
18884 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18886 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18887 finish_omp_barrier ();
18891 # pragma omp critical [(name)] new-line
18892 structured-block */
18895 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18897 tree stmt
, name
= NULL
;
18899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18901 cp_lexer_consume_token (parser
->lexer
);
18903 name
= cp_parser_identifier (parser
);
18905 if (name
== error_mark_node
18906 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18907 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18908 /*or_comma=*/false,
18909 /*consume_paren=*/true);
18910 if (name
== error_mark_node
)
18913 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18915 stmt
= cp_parser_omp_structured_block (parser
);
18916 return c_finish_omp_critical (stmt
, name
);
18920 # pragma omp flush flush-vars[opt] new-line
18923 ( variable-list ) */
18926 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18929 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18930 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18932 finish_omp_flush ();
18935 /* Parse the restricted form of the for statment allowed by OpenMP. */
18938 cp_parser_omp_for_loop (cp_parser
*parser
)
18940 tree init
, cond
, incr
, body
, decl
, pre_body
;
18943 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18945 cp_parser_error (parser
, "for statement expected");
18948 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18949 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18952 init
= decl
= NULL
;
18953 pre_body
= push_stmt_list ();
18954 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18956 cp_decl_specifier_seq type_specifiers
;
18958 /* First, try to parse as an initialized declaration. See
18959 cp_parser_condition, from whence the bulk of this is copied. */
18961 cp_parser_parse_tentatively (parser
);
18962 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18964 if (!cp_parser_error_occurred (parser
))
18966 tree asm_specification
, attributes
;
18967 cp_declarator
*declarator
;
18969 declarator
= cp_parser_declarator (parser
,
18970 CP_PARSER_DECLARATOR_NAMED
,
18971 /*ctor_dtor_or_conv_p=*/NULL
,
18972 /*parenthesized_p=*/NULL
,
18973 /*member_p=*/false);
18974 attributes
= cp_parser_attributes_opt (parser
);
18975 asm_specification
= cp_parser_asm_specification_opt (parser
);
18977 cp_parser_require (parser
, CPP_EQ
, "`='");
18978 if (cp_parser_parse_definitely (parser
))
18982 decl
= start_decl (declarator
, &type_specifiers
,
18983 /*initialized_p=*/false, attributes
,
18984 /*prefix_attributes=*/NULL_TREE
,
18987 init
= cp_parser_assignment_expression (parser
, false);
18989 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18990 asm_specification
, LOOKUP_ONLYCONVERTING
);
18993 pop_scope (pushed_scope
);
18997 cp_parser_abort_tentative_parse (parser
);
18999 /* If parsing as an initialized declaration failed, try again as
19000 a simple expression. */
19002 init
= cp_parser_expression (parser
, false);
19004 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
19005 pre_body
= pop_stmt_list (pre_body
);
19008 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19009 cond
= cp_parser_condition (parser
);
19010 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
19013 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
19014 incr
= cp_parser_expression (parser
, false);
19016 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19017 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19018 /*or_comma=*/false,
19019 /*consume_paren=*/true);
19021 /* Note that we saved the original contents of this flag when we entered
19022 the structured block, and so we don't need to re-save it here. */
19023 parser
->in_statement
= IN_OMP_FOR
;
19025 /* Note that the grammar doesn't call for a structured block here,
19026 though the loop as a whole is a structured block. */
19027 body
= push_stmt_list ();
19028 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19029 body
= pop_stmt_list (body
);
19031 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
19035 #pragma omp for for-clause[optseq] new-line
19038 #define OMP_FOR_CLAUSE_MASK \
19039 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19040 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19041 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19042 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19043 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19044 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19045 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19048 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
19050 tree clauses
, sb
, ret
;
19053 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
19054 "#pragma omp for", pragma_tok
);
19056 sb
= begin_omp_structured_block ();
19057 save
= cp_parser_begin_omp_structured_block (parser
);
19059 ret
= cp_parser_omp_for_loop (parser
);
19061 OMP_FOR_CLAUSES (ret
) = clauses
;
19063 cp_parser_end_omp_structured_block (parser
, save
);
19064 add_stmt (finish_omp_structured_block (sb
));
19070 # pragma omp master new-line
19071 structured-block */
19074 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
19076 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19077 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
19081 # pragma omp ordered new-line
19082 structured-block */
19085 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
19087 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19088 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
19094 { section-sequence }
19097 section-directive[opt] structured-block
19098 section-sequence section-directive structured-block */
19101 cp_parser_omp_sections_scope (cp_parser
*parser
)
19103 tree stmt
, substmt
;
19104 bool error_suppress
= false;
19107 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
19110 stmt
= push_stmt_list ();
19112 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
19116 substmt
= begin_omp_structured_block ();
19117 save
= cp_parser_begin_omp_structured_block (parser
);
19121 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19123 tok
= cp_lexer_peek_token (parser
->lexer
);
19124 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19126 if (tok
->type
== CPP_CLOSE_BRACE
)
19128 if (tok
->type
== CPP_EOF
)
19132 cp_parser_end_omp_structured_block (parser
, save
);
19133 substmt
= finish_omp_structured_block (substmt
);
19134 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19135 add_stmt (substmt
);
19140 tok
= cp_lexer_peek_token (parser
->lexer
);
19141 if (tok
->type
== CPP_CLOSE_BRACE
)
19143 if (tok
->type
== CPP_EOF
)
19146 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19148 cp_lexer_consume_token (parser
->lexer
);
19149 cp_parser_require_pragma_eol (parser
, tok
);
19150 error_suppress
= false;
19152 else if (!error_suppress
)
19154 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
19155 error_suppress
= true;
19158 substmt
= cp_parser_omp_structured_block (parser
);
19159 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19160 add_stmt (substmt
);
19162 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
19164 substmt
= pop_stmt_list (stmt
);
19166 stmt
= make_node (OMP_SECTIONS
);
19167 TREE_TYPE (stmt
) = void_type_node
;
19168 OMP_SECTIONS_BODY (stmt
) = substmt
;
19175 # pragma omp sections sections-clause[optseq] newline
19178 #define OMP_SECTIONS_CLAUSE_MASK \
19179 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19180 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19181 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19182 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19183 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19186 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
19190 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
19191 "#pragma omp sections", pragma_tok
);
19193 ret
= cp_parser_omp_sections_scope (parser
);
19195 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
19201 # pragma parallel parallel-clause new-line
19202 # pragma parallel for parallel-for-clause new-line
19203 # pragma parallel sections parallel-sections-clause new-line */
19205 #define OMP_PARALLEL_CLAUSE_MASK \
19206 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19207 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19208 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19209 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19210 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19211 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19212 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19213 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19216 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
19218 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
19219 const char *p_name
= "#pragma omp parallel";
19220 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
19221 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
19224 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19226 cp_lexer_consume_token (parser
->lexer
);
19227 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
19228 p_name
= "#pragma omp parallel for";
19229 mask
|= OMP_FOR_CLAUSE_MASK
;
19230 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19232 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19234 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19235 const char *p
= IDENTIFIER_POINTER (id
);
19236 if (strcmp (p
, "sections") == 0)
19238 cp_lexer_consume_token (parser
->lexer
);
19239 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
19240 p_name
= "#pragma omp parallel sections";
19241 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
19242 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19246 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
19247 block
= begin_omp_parallel ();
19248 save
= cp_parser_begin_omp_structured_block (parser
);
19252 case PRAGMA_OMP_PARALLEL
:
19253 cp_parser_already_scoped_statement (parser
);
19254 par_clause
= clauses
;
19257 case PRAGMA_OMP_PARALLEL_FOR
:
19258 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19259 stmt
= cp_parser_omp_for_loop (parser
);
19261 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
19264 case PRAGMA_OMP_PARALLEL_SECTIONS
:
19265 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19266 stmt
= cp_parser_omp_sections_scope (parser
);
19268 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
19272 gcc_unreachable ();
19275 cp_parser_end_omp_structured_block (parser
, save
);
19276 stmt
= finish_omp_parallel (par_clause
, block
);
19277 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
19278 OMP_PARALLEL_COMBINED (stmt
) = 1;
19283 # pragma omp single single-clause[optseq] new-line
19284 structured-block */
19286 #define OMP_SINGLE_CLAUSE_MASK \
19287 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19288 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19289 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19290 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19293 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
19295 tree stmt
= make_node (OMP_SINGLE
);
19296 TREE_TYPE (stmt
) = void_type_node
;
19298 OMP_SINGLE_CLAUSES (stmt
)
19299 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
19300 "#pragma omp single", pragma_tok
);
19301 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
19303 return add_stmt (stmt
);
19307 # pragma omp threadprivate (variable-list) */
19310 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
19314 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
19315 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19317 finish_omp_threadprivate (vars
);
19320 /* Main entry point to OpenMP statement pragmas. */
19323 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
19327 switch (pragma_tok
->pragma_kind
)
19329 case PRAGMA_OMP_ATOMIC
:
19330 cp_parser_omp_atomic (parser
, pragma_tok
);
19332 case PRAGMA_OMP_CRITICAL
:
19333 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
19335 case PRAGMA_OMP_FOR
:
19336 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
19338 case PRAGMA_OMP_MASTER
:
19339 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
19341 case PRAGMA_OMP_ORDERED
:
19342 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
19344 case PRAGMA_OMP_PARALLEL
:
19345 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19347 case PRAGMA_OMP_SECTIONS
:
19348 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19350 case PRAGMA_OMP_SINGLE
:
19351 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19354 gcc_unreachable ();
19358 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19363 static GTY (()) cp_parser
*the_parser
;
19366 /* Special handling for the first token or line in the file. The first
19367 thing in the file might be #pragma GCC pch_preprocess, which loads a
19368 PCH file, which is a GC collection point. So we need to handle this
19369 first pragma without benefit of an existing lexer structure.
19371 Always returns one token to the caller in *FIRST_TOKEN. This is
19372 either the true first token of the file, or the first token after
19373 the initial pragma. */
19376 cp_parser_initial_pragma (cp_token
*first_token
)
19380 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19381 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19384 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19385 if (first_token
->type
== CPP_STRING
)
19387 name
= first_token
->u
.value
;
19389 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19390 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19391 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19394 error ("expected string literal");
19396 /* Skip to the end of the pragma. */
19397 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19398 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19400 /* Now actually load the PCH file. */
19402 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19404 /* Read one more token to return to our caller. We have to do this
19405 after reading the PCH file in, since its pointers have to be
19407 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19410 /* Normal parsing of a pragma token. Here we can (and must) use the
19414 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19416 cp_token
*pragma_tok
;
19419 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19420 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19421 parser
->lexer
->in_pragma
= true;
19423 id
= pragma_tok
->pragma_kind
;
19426 case PRAGMA_GCC_PCH_PREPROCESS
:
19427 error ("%<#pragma GCC pch_preprocess%> must be first");
19430 case PRAGMA_OMP_BARRIER
:
19433 case pragma_compound
:
19434 cp_parser_omp_barrier (parser
, pragma_tok
);
19437 error ("%<#pragma omp barrier%> may only be "
19438 "used in compound statements");
19445 case PRAGMA_OMP_FLUSH
:
19448 case pragma_compound
:
19449 cp_parser_omp_flush (parser
, pragma_tok
);
19452 error ("%<#pragma omp flush%> may only be "
19453 "used in compound statements");
19460 case PRAGMA_OMP_THREADPRIVATE
:
19461 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19464 case PRAGMA_OMP_ATOMIC
:
19465 case PRAGMA_OMP_CRITICAL
:
19466 case PRAGMA_OMP_FOR
:
19467 case PRAGMA_OMP_MASTER
:
19468 case PRAGMA_OMP_ORDERED
:
19469 case PRAGMA_OMP_PARALLEL
:
19470 case PRAGMA_OMP_SECTIONS
:
19471 case PRAGMA_OMP_SINGLE
:
19472 if (context
== pragma_external
)
19474 cp_parser_omp_construct (parser
, pragma_tok
);
19477 case PRAGMA_OMP_SECTION
:
19478 error ("%<#pragma omp section%> may only be used in "
19479 "%<#pragma omp sections%> construct");
19483 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19484 c_invoke_pragma_handler (id
);
19488 cp_parser_error (parser
, "expected declaration specifiers");
19492 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19496 /* The interface the pragma parsers have to the lexer. */
19499 pragma_lex (tree
*value
)
19502 enum cpp_ttype ret
;
19504 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19507 *value
= tok
->u
.value
;
19509 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19511 else if (ret
== CPP_STRING
)
19512 *value
= cp_parser_string_literal (the_parser
, false, false);
19515 cp_lexer_consume_token (the_parser
->lexer
);
19516 if (ret
== CPP_KEYWORD
)
19524 /* External interface. */
19526 /* Parse one entire translation unit. */
19529 c_parse_file (void)
19531 bool error_occurred
;
19532 static bool already_called
= false;
19534 if (already_called
)
19536 sorry ("inter-module optimizations not implemented for C++");
19539 already_called
= true;
19541 the_parser
= cp_parser_new ();
19542 push_deferring_access_checks (flag_access_control
19543 ? dk_no_deferred
: dk_no_check
);
19544 error_occurred
= cp_parser_translation_unit (the_parser
);
19548 #include "gt-cp-parser.h"