2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check
GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check
, gc
)* checks
;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope
;
63 typedef struct cp_token
GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype
) type
: 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid
) keyword
: 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header
: 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c
: 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p
: 1;
82 /* The value associated with this token, if any. */
83 union cp_token_value
{
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check
* GTY((tag ("1"))) tree_check_value
;
86 /* Use for all other tokens. */
87 tree
GTY((tag ("0"))) value
;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u
;
89 /* The location at which this token was found. */
93 /* We use a stack of token pointer for saving token sets. */
94 typedef struct cp_token
*cp_token_position
;
95 DEF_VEC_P (cp_token_position
);
96 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
98 static cp_token eof_token
=
100 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, 0, false, 0, { NULL
},
104 /* The cp_lexer structure represents the C++ lexer. It is responsible
105 for managing the token stream from the preprocessor and supplying
106 it to the parser. Tokens are never added to the cp_lexer after
109 typedef struct cp_lexer
GTY (())
111 /* The memory allocated for the buffer. NULL if this lexer does not
112 own the token buffer. */
113 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
114 /* If the lexer owns the buffer, this is the number of tokens in the
116 size_t buffer_length
;
118 /* A pointer just past the last available token. The tokens
119 in this lexer are [buffer, last_token). */
120 cp_token_position
GTY ((skip
)) last_token
;
122 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
123 no more available tokens. */
124 cp_token_position
GTY ((skip
)) next_token
;
126 /* A stack indicating positions at which cp_lexer_save_tokens was
127 called. The top entry is the most recent position at which we
128 began saving tokens. If the stack is non-empty, we are saving
130 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
132 /* The next lexer in a linked list of lexers. */
133 struct cp_lexer
*next
;
135 /* True if we should output debugging information. */
138 /* True if we're in the context of parsing a pragma, and should not
139 increment past the end-of-line marker. */
143 /* cp_token_cache is a range of tokens. There is no need to represent
144 allocate heap memory for it, since tokens are never removed from the
145 lexer's array. There is also no need for the GC to walk through
146 a cp_token_cache, since everything in here is referenced through
149 typedef struct cp_token_cache
GTY(())
151 /* The beginning of the token range. */
152 cp_token
* GTY((skip
)) first
;
154 /* Points immediately after the last token in the range. */
155 cp_token
* GTY ((skip
)) last
;
160 static cp_lexer
*cp_lexer_new_main
162 static cp_lexer
*cp_lexer_new_from_tokens
163 (cp_token_cache
*tokens
);
164 static void cp_lexer_destroy
166 static int cp_lexer_saving_tokens
168 static cp_token_position cp_lexer_token_position
170 static cp_token
*cp_lexer_token_at
171 (cp_lexer
*, cp_token_position
);
172 static void cp_lexer_get_preprocessor_token
173 (cp_lexer
*, cp_token
*);
174 static inline cp_token
*cp_lexer_peek_token
176 static cp_token
*cp_lexer_peek_nth_token
177 (cp_lexer
*, size_t);
178 static inline bool cp_lexer_next_token_is
179 (cp_lexer
*, enum cpp_ttype
);
180 static bool cp_lexer_next_token_is_not
181 (cp_lexer
*, enum cpp_ttype
);
182 static bool cp_lexer_next_token_is_keyword
183 (cp_lexer
*, enum rid
);
184 static cp_token
*cp_lexer_consume_token
186 static void cp_lexer_purge_token
188 static void cp_lexer_purge_tokens_after
189 (cp_lexer
*, cp_token_position
);
190 static void cp_lexer_save_tokens
192 static void cp_lexer_commit_tokens
194 static void cp_lexer_rollback_tokens
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198 (FILE *, cp_token
*);
199 static inline bool cp_lexer_debugging_p
201 static void cp_lexer_start_debugging
202 (cp_lexer
*) ATTRIBUTE_UNUSED
;
203 static void cp_lexer_stop_debugging
204 (cp_lexer
*) ATTRIBUTE_UNUSED
;
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207 about passing NULL to functions that require non-NULL arguments
208 (fputs, fprintf). It will never be used, so all we need is a value
209 of the right type that's guaranteed not to be NULL. */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
215 static cp_token_cache
*cp_token_cache_new
216 (cp_token
*, cp_token
*);
218 static void cp_parser_initial_pragma
221 /* Manifest constants. */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
225 /* A token type for keywords, as opposed to ordinary identifiers. */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
228 /* A token type for template-ids. If a template-id is processed while
229 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230 the value of the CPP_TEMPLATE_ID is whatever was returned by
231 cp_parser_template_id. */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
234 /* A token type for nested-name-specifiers. If a
235 nested-name-specifier is processed while parsing tentatively, it is
236 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238 cp_parser_nested_name_specifier_opt. */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
241 /* A token type for tokens that are not tokens at all; these are used
242 to represent slots in the array where there used to be a token
243 that has now been deleted. */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
246 /* The number of token types, including C++-specific ones. */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream
;
254 #endif /* ENABLE_CHECKING */
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
260 cp_lexer_new_main (void)
262 cp_token first_token
;
269 /* It's possible that parsing the first pragma will load a PCH file,
270 which is a GC collection point. So we have to do that before
271 allocating any memory. */
272 cp_parser_initial_pragma (&first_token
);
274 c_common_no_more_pch ();
276 /* Allocate the memory. */
277 lexer
= GGC_CNEW (cp_lexer
);
279 #ifdef ENABLE_CHECKING
280 /* Initially we are not debugging. */
281 lexer
->debugging_p
= false;
282 #endif /* ENABLE_CHECKING */
283 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
284 CP_SAVED_TOKEN_STACK
);
286 /* Create the buffer. */
287 alloc
= CP_LEXER_BUFFER_SIZE
;
288 buffer
= GGC_NEWVEC (cp_token
, alloc
);
290 /* Put the first token in the buffer. */
295 /* Get the remaining tokens from the preprocessor. */
296 while (pos
->type
!= CPP_EOF
)
303 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
304 pos
= buffer
+ space
;
306 cp_lexer_get_preprocessor_token (lexer
, pos
);
308 lexer
->buffer
= buffer
;
309 lexer
->buffer_length
= alloc
- space
;
310 lexer
->last_token
= pos
;
311 lexer
->next_token
= lexer
->buffer_length
? buffer
: &eof_token
;
313 /* Subsequent preprocessor diagnostics should use compiler
314 diagnostic functions to get the compiler source location. */
315 cpp_get_options (parse_in
)->client_diagnostic
= true;
316 cpp_get_callbacks (parse_in
)->error
= cp_cpp_error
;
318 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
322 /* Create a new lexer whose token stream is primed with the tokens in
323 CACHE. When these tokens are exhausted, no new tokens will be read. */
326 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
328 cp_token
*first
= cache
->first
;
329 cp_token
*last
= cache
->last
;
330 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
332 /* We do not own the buffer. */
333 lexer
->buffer
= NULL
;
334 lexer
->buffer_length
= 0;
335 lexer
->next_token
= first
== last
? &eof_token
: first
;
336 lexer
->last_token
= last
;
338 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
339 CP_SAVED_TOKEN_STACK
);
341 #ifdef ENABLE_CHECKING
342 /* Initially we are not debugging. */
343 lexer
->debugging_p
= false;
346 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
350 /* Frees all resources associated with LEXER. */
353 cp_lexer_destroy (cp_lexer
*lexer
)
356 ggc_free (lexer
->buffer
);
357 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
361 /* Returns nonzero if debugging information should be output. */
363 #ifdef ENABLE_CHECKING
366 cp_lexer_debugging_p (cp_lexer
*lexer
)
368 return lexer
->debugging_p
;
371 #endif /* ENABLE_CHECKING */
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
376 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
378 return lexer
->next_token
- previous_p
;
381 static inline cp_token
*
382 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
387 /* nonzero if we are presently saving tokens. */
390 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
392 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
395 /* Store the next token from the preprocessor in *TOKEN. Return true
396 if we reach EOF. If LEXER is NULL, assume we are handling an
397 initial #pragma pch_preprocess, and thus want the lexer to return
398 processed strings. */
401 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
403 static int is_extern_c
= 0;
405 /* Get a new token from the preprocessor. */
407 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
408 lexer
== NULL
? 0 : C_LEX_RAW_STRINGS
);
409 token
->keyword
= RID_MAX
;
410 token
->pragma_kind
= PRAGMA_NONE
;
411 token
->in_system_header
= in_system_header
;
413 /* On some systems, some header files are surrounded by an
414 implicit extern "C" block. Set a flag in the token if it
415 comes from such a header. */
416 is_extern_c
+= pending_lang_change
;
417 pending_lang_change
= 0;
418 token
->implicit_extern_c
= is_extern_c
> 0;
420 /* Check to see if this token is a keyword. */
421 if (token
->type
== CPP_NAME
)
423 if (C_IS_RESERVED_WORD (token
->u
.value
))
425 /* Mark this token as a keyword. */
426 token
->type
= CPP_KEYWORD
;
427 /* Record which keyword. */
428 token
->keyword
= C_RID_CODE (token
->u
.value
);
429 /* Update the value. Some keywords are mapped to particular
430 entities, rather than simply having the value of the
431 corresponding IDENTIFIER_NODE. For example, `__const' is
432 mapped to `const'. */
433 token
->u
.value
= ridpointers
[token
->keyword
];
437 if (warn_cxx0x_compat
438 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
439 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
441 /* Warn about the C++0x keyword (but still treat it as
443 warning (OPT_Wc__0x_compat
,
444 "identifier %<%s%> will become a keyword in C++0x",
445 IDENTIFIER_POINTER (token
->u
.value
));
447 /* Clear out the C_RID_CODE so we don't warn about this
448 particular identifier-turned-keyword again. */
449 C_RID_CODE (token
->u
.value
) = RID_MAX
;
452 token
->ambiguous_p
= false;
453 token
->keyword
= RID_MAX
;
456 /* Handle Objective-C++ keywords. */
457 else if (token
->type
== CPP_AT_NAME
)
459 token
->type
= CPP_KEYWORD
;
460 switch (C_RID_CODE (token
->u
.value
))
462 /* Map 'class' to '@class', 'private' to '@private', etc. */
463 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
464 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
465 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
466 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
467 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
468 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
469 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
470 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
473 else if (token
->type
== CPP_PRAGMA
)
475 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
476 token
->pragma_kind
= TREE_INT_CST_LOW (token
->u
.value
);
477 token
->u
.value
= NULL_TREE
;
481 /* Update the globals input_location and in_system_header and the
482 input file stack from TOKEN. */
484 cp_lexer_set_source_position_from_token (cp_token
*token
)
486 if (token
->type
!= CPP_EOF
)
488 input_location
= token
->location
;
489 in_system_header
= token
->in_system_header
;
493 /* Return a pointer to the next token in the token stream, but do not
496 static inline cp_token
*
497 cp_lexer_peek_token (cp_lexer
*lexer
)
499 if (cp_lexer_debugging_p (lexer
))
501 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
502 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
503 putc ('\n', cp_lexer_debug_stream
);
505 return lexer
->next_token
;
508 /* Return true if the next token has the indicated TYPE. */
511 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
513 return cp_lexer_peek_token (lexer
)->type
== type
;
516 /* Return true if the next token does not have the indicated TYPE. */
519 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
521 return !cp_lexer_next_token_is (lexer
, type
);
524 /* Return true if the next token is the indicated KEYWORD. */
527 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
529 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
532 /* Return true if the next token is a keyword for a decl-specifier. */
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
539 token
= cp_lexer_peek_token (lexer
);
540 switch (token
->keyword
)
542 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
567 /* GNU extensions. */
570 /* C++0x extensions. */
579 /* Return a pointer to the Nth token in the token stream. If N is 1,
580 then this is precisely equivalent to cp_lexer_peek_token (except
581 that it is not inline). One would like to disallow that case, but
582 there is one case (cp_parser_nth_token_starts_template_id) where
583 the caller passes a variable for N and it might be 1. */
586 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
590 /* N is 1-based, not zero-based. */
593 if (cp_lexer_debugging_p (lexer
))
594 fprintf (cp_lexer_debug_stream
,
595 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
598 token
= lexer
->next_token
;
599 gcc_assert (!n
|| token
!= &eof_token
);
603 if (token
== lexer
->last_token
)
609 if (token
->type
!= CPP_PURGED
)
613 if (cp_lexer_debugging_p (lexer
))
615 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
616 putc ('\n', cp_lexer_debug_stream
);
622 /* Return the next token, and advance the lexer's next_token pointer
623 to point to the next non-purged token. */
626 cp_lexer_consume_token (cp_lexer
* lexer
)
628 cp_token
*token
= lexer
->next_token
;
630 gcc_assert (token
!= &eof_token
);
631 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
636 if (lexer
->next_token
== lexer
->last_token
)
638 lexer
->next_token
= &eof_token
;
643 while (lexer
->next_token
->type
== CPP_PURGED
);
645 cp_lexer_set_source_position_from_token (token
);
647 /* Provide debugging output. */
648 if (cp_lexer_debugging_p (lexer
))
650 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
651 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
652 putc ('\n', cp_lexer_debug_stream
);
658 /* Permanently remove the next token from the token stream, and
659 advance the next_token pointer to refer to the next non-purged
663 cp_lexer_purge_token (cp_lexer
*lexer
)
665 cp_token
*tok
= lexer
->next_token
;
667 gcc_assert (tok
!= &eof_token
);
668 tok
->type
= CPP_PURGED
;
669 tok
->location
= UNKNOWN_LOCATION
;
670 tok
->u
.value
= NULL_TREE
;
671 tok
->keyword
= RID_MAX
;
676 if (tok
== lexer
->last_token
)
682 while (tok
->type
== CPP_PURGED
);
683 lexer
->next_token
= tok
;
686 /* Permanently remove all tokens after TOK, up to, but not
687 including, the token that will be returned next by
688 cp_lexer_peek_token. */
691 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
693 cp_token
*peek
= lexer
->next_token
;
695 if (peek
== &eof_token
)
696 peek
= lexer
->last_token
;
698 gcc_assert (tok
< peek
);
700 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
702 tok
->type
= CPP_PURGED
;
703 tok
->location
= UNKNOWN_LOCATION
;
704 tok
->u
.value
= NULL_TREE
;
705 tok
->keyword
= RID_MAX
;
709 /* Begin saving tokens. All tokens consumed after this point will be
713 cp_lexer_save_tokens (cp_lexer
* lexer
)
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer
))
717 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
719 VEC_safe_push (cp_token_position
, heap
,
720 lexer
->saved_tokens
, lexer
->next_token
);
723 /* Commit to the portion of the token stream most recently saved. */
726 cp_lexer_commit_tokens (cp_lexer
* lexer
)
728 /* Provide debugging output. */
729 if (cp_lexer_debugging_p (lexer
))
730 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
732 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
735 /* Return all tokens saved since the last call to cp_lexer_save_tokens
736 to the token stream. Stop saving tokens. */
739 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
741 /* Provide debugging output. */
742 if (cp_lexer_debugging_p (lexer
))
743 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
745 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
748 /* Print a representation of the TOKEN on the STREAM. */
750 #ifdef ENABLE_CHECKING
753 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
755 /* We don't use cpp_type2name here because the parser defines
756 a few tokens of its own. */
757 static const char *const token_names
[] = {
758 /* cpplib-defined token types */
764 /* C++ parser token types - see "Manifest constants", above. */
767 "NESTED_NAME_SPECIFIER",
771 /* If we have a name for the token, print it out. Otherwise, we
772 simply give the numeric code. */
773 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
774 fputs (token_names
[token
->type
], stream
);
776 /* For some tokens, print the associated data. */
780 /* Some keywords have a value that is not an IDENTIFIER_NODE.
781 For example, `struct' is mapped to an INTEGER_CST. */
782 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
784 /* else fall through */
786 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
791 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
799 /* Start emitting debugging information. */
802 cp_lexer_start_debugging (cp_lexer
* lexer
)
804 lexer
->debugging_p
= true;
807 /* Stop emitting debugging information. */
810 cp_lexer_stop_debugging (cp_lexer
* lexer
)
812 lexer
->debugging_p
= false;
815 #endif /* ENABLE_CHECKING */
817 /* Create a new cp_token_cache, representing a range of tokens. */
819 static cp_token_cache
*
820 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
822 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
823 cache
->first
= first
;
829 /* Decl-specifiers. */
831 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
834 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
836 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
841 /* Nothing other than the parser should be creating declarators;
842 declarators are a semi-syntactic representation of C++ entities.
843 Other parts of the front end that need to create entities (like
844 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
846 static cp_declarator
*make_call_declarator
847 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
848 static cp_declarator
*make_array_declarator
849 (cp_declarator
*, tree
);
850 static cp_declarator
*make_pointer_declarator
851 (cp_cv_quals
, cp_declarator
*);
852 static cp_declarator
*make_reference_declarator
853 (cp_cv_quals
, cp_declarator
*, bool);
854 static cp_parameter_declarator
*make_parameter_declarator
855 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
856 static cp_declarator
*make_ptrmem_declarator
857 (cp_cv_quals
, tree
, cp_declarator
*);
859 /* An erroneous declarator. */
860 static cp_declarator
*cp_error_declarator
;
862 /* The obstack on which declarators and related data structures are
864 static struct obstack declarator_obstack
;
866 /* Alloc BYTES from the declarator memory pool. */
869 alloc_declarator (size_t bytes
)
871 return obstack_alloc (&declarator_obstack
, bytes
);
874 /* Allocate a declarator of the indicated KIND. Clear fields that are
875 common to all declarators. */
877 static cp_declarator
*
878 make_declarator (cp_declarator_kind kind
)
880 cp_declarator
*declarator
;
882 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
883 declarator
->kind
= kind
;
884 declarator
->attributes
= NULL_TREE
;
885 declarator
->declarator
= NULL
;
886 declarator
->parameter_pack_p
= false;
891 /* Make a declarator for a generalized identifier. If
892 QUALIFYING_SCOPE is non-NULL, the identifier is
893 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894 UNQUALIFIED_NAME. SFK indicates the kind of special function this
897 static cp_declarator
*
898 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
899 special_function_kind sfk
)
901 cp_declarator
*declarator
;
903 /* It is valid to write:
905 class C { void f(); };
909 The standard is not clear about whether `typedef const C D' is
910 legal; as of 2002-09-15 the committee is considering that
911 question. EDG 3.0 allows that syntax. Therefore, we do as
913 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
914 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
916 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
917 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
918 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
920 declarator
= make_declarator (cdk_id
);
921 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
922 declarator
->u
.id
.unqualified_name
= unqualified_name
;
923 declarator
->u
.id
.sfk
= sfk
;
928 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
929 of modifiers such as const or volatile to apply to the pointer
930 type, represented as identifiers. */
933 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
935 cp_declarator
*declarator
;
937 declarator
= make_declarator (cdk_pointer
);
938 declarator
->declarator
= target
;
939 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
940 declarator
->u
.pointer
.class_type
= NULL_TREE
;
943 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
944 target
->parameter_pack_p
= false;
947 declarator
->parameter_pack_p
= false;
952 /* Like make_pointer_declarator -- but for references. */
955 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
958 cp_declarator
*declarator
;
960 declarator
= make_declarator (cdk_reference
);
961 declarator
->declarator
= target
;
962 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
963 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
966 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
967 target
->parameter_pack_p
= false;
970 declarator
->parameter_pack_p
= false;
975 /* Like make_pointer_declarator -- but for a pointer to a non-static
976 member of CLASS_TYPE. */
979 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
980 cp_declarator
*pointee
)
982 cp_declarator
*declarator
;
984 declarator
= make_declarator (cdk_ptrmem
);
985 declarator
->declarator
= pointee
;
986 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
987 declarator
->u
.pointer
.class_type
= class_type
;
991 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
992 pointee
->parameter_pack_p
= false;
995 declarator
->parameter_pack_p
= false;
1000 /* Make a declarator for the function given by TARGET, with the
1001 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1002 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1003 indicates what exceptions can be thrown. */
1006 make_call_declarator (cp_declarator
*target
,
1007 cp_parameter_declarator
*parms
,
1008 cp_cv_quals cv_qualifiers
,
1009 tree exception_specification
)
1011 cp_declarator
*declarator
;
1013 declarator
= make_declarator (cdk_function
);
1014 declarator
->declarator
= target
;
1015 declarator
->u
.function
.parameters
= parms
;
1016 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1017 declarator
->u
.function
.exception_specification
= exception_specification
;
1020 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1021 target
->parameter_pack_p
= false;
1024 declarator
->parameter_pack_p
= false;
1029 /* Make a declarator for an array of BOUNDS elements, each of which is
1030 defined by ELEMENT. */
1033 make_array_declarator (cp_declarator
*element
, tree bounds
)
1035 cp_declarator
*declarator
;
1037 declarator
= make_declarator (cdk_array
);
1038 declarator
->declarator
= element
;
1039 declarator
->u
.array
.bounds
= bounds
;
1042 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1043 element
->parameter_pack_p
= false;
1046 declarator
->parameter_pack_p
= false;
1051 /* Determine whether the declarator we've seen so far can be a
1052 parameter pack, when followed by an ellipsis. */
1054 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1056 /* Search for a declarator name, or any other declarator that goes
1057 after the point where the ellipsis could appear in a parameter
1058 pack. If we find any of these, then this declarator can not be
1059 made into a parameter pack. */
1061 while (declarator
&& !found
)
1063 switch ((int)declarator
->kind
)
1074 declarator
= declarator
->declarator
;
1082 cp_parameter_declarator
*no_parameters
;
1084 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1085 DECLARATOR and DEFAULT_ARGUMENT. */
1087 cp_parameter_declarator
*
1088 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1089 cp_declarator
*declarator
,
1090 tree default_argument
)
1092 cp_parameter_declarator
*parameter
;
1094 parameter
= ((cp_parameter_declarator
*)
1095 alloc_declarator (sizeof (cp_parameter_declarator
)));
1096 parameter
->next
= NULL
;
1097 if (decl_specifiers
)
1098 parameter
->decl_specifiers
= *decl_specifiers
;
1100 clear_decl_specs (¶meter
->decl_specifiers
);
1101 parameter
->declarator
= declarator
;
1102 parameter
->default_argument
= default_argument
;
1103 parameter
->ellipsis_p
= false;
1108 /* Returns true iff DECLARATOR is a declaration for a function. */
1111 function_declarator_p (const cp_declarator
*declarator
)
1115 if (declarator
->kind
== cdk_function
1116 && declarator
->declarator
->kind
== cdk_id
)
1118 if (declarator
->kind
== cdk_id
1119 || declarator
->kind
== cdk_error
)
1121 declarator
= declarator
->declarator
;
1131 A cp_parser parses the token stream as specified by the C++
1132 grammar. Its job is purely parsing, not semantic analysis. For
1133 example, the parser breaks the token stream into declarators,
1134 expressions, statements, and other similar syntactic constructs.
1135 It does not check that the types of the expressions on either side
1136 of an assignment-statement are compatible, or that a function is
1137 not declared with a parameter of type `void'.
1139 The parser invokes routines elsewhere in the compiler to perform
1140 semantic analysis and to build up the abstract syntax tree for the
1143 The parser (and the template instantiation code, which is, in a
1144 way, a close relative of parsing) are the only parts of the
1145 compiler that should be calling push_scope and pop_scope, or
1146 related functions. The parser (and template instantiation code)
1147 keeps track of what scope is presently active; everything else
1148 should simply honor that. (The code that generates static
1149 initializers may also need to set the scope, in order to check
1150 access control correctly when emitting the initializers.)
1155 The parser is of the standard recursive-descent variety. Upcoming
1156 tokens in the token stream are examined in order to determine which
1157 production to use when parsing a non-terminal. Some C++ constructs
1158 require arbitrary look ahead to disambiguate. For example, it is
1159 impossible, in the general case, to tell whether a statement is an
1160 expression or declaration without scanning the entire statement.
1161 Therefore, the parser is capable of "parsing tentatively." When the
1162 parser is not sure what construct comes next, it enters this mode.
1163 Then, while we attempt to parse the construct, the parser queues up
1164 error messages, rather than issuing them immediately, and saves the
1165 tokens it consumes. If the construct is parsed successfully, the
1166 parser "commits", i.e., it issues any queued error messages and
1167 the tokens that were being preserved are permanently discarded.
1168 If, however, the construct is not parsed successfully, the parser
1169 rolls back its state completely so that it can resume parsing using
1170 a different alternative.
1175 The performance of the parser could probably be improved substantially.
1176 We could often eliminate the need to parse tentatively by looking ahead
1177 a little bit. In some places, this approach might not entirely eliminate
1178 the need to parse tentatively, but it might still speed up the average
1181 /* Flags that are passed to some parsing functions. These values can
1182 be bitwise-ored together. */
1184 typedef enum cp_parser_flags
1187 CP_PARSER_FLAGS_NONE
= 0x0,
1188 /* The construct is optional. If it is not present, then no error
1189 should be issued. */
1190 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1191 /* When parsing a type-specifier, do not allow user-defined types. */
1192 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1195 /* The different kinds of declarators we want to parse. */
1197 typedef enum cp_parser_declarator_kind
1199 /* We want an abstract declarator. */
1200 CP_PARSER_DECLARATOR_ABSTRACT
,
1201 /* We want a named declarator. */
1202 CP_PARSER_DECLARATOR_NAMED
,
1203 /* We don't mind, but the name must be an unqualified-id. */
1204 CP_PARSER_DECLARATOR_EITHER
1205 } cp_parser_declarator_kind
;
1207 /* The precedence values used to parse binary expressions. The minimum value
1208 of PREC must be 1, because zero is reserved to quickly discriminate
1209 binary operators from other tokens. */
1214 PREC_LOGICAL_OR_EXPRESSION
,
1215 PREC_LOGICAL_AND_EXPRESSION
,
1216 PREC_INCLUSIVE_OR_EXPRESSION
,
1217 PREC_EXCLUSIVE_OR_EXPRESSION
,
1218 PREC_AND_EXPRESSION
,
1219 PREC_EQUALITY_EXPRESSION
,
1220 PREC_RELATIONAL_EXPRESSION
,
1221 PREC_SHIFT_EXPRESSION
,
1222 PREC_ADDITIVE_EXPRESSION
,
1223 PREC_MULTIPLICATIVE_EXPRESSION
,
1225 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1228 /* A mapping from a token type to a corresponding tree node type, with a
1229 precedence value. */
1231 typedef struct cp_parser_binary_operations_map_node
1233 /* The token type. */
1234 enum cpp_ttype token_type
;
1235 /* The corresponding tree code. */
1236 enum tree_code tree_type
;
1237 /* The precedence of this operator. */
1238 enum cp_parser_prec prec
;
1239 } cp_parser_binary_operations_map_node
;
1241 /* The status of a tentative parse. */
1243 typedef enum cp_parser_status_kind
1245 /* No errors have occurred. */
1246 CP_PARSER_STATUS_KIND_NO_ERROR
,
1247 /* An error has occurred. */
1248 CP_PARSER_STATUS_KIND_ERROR
,
1249 /* We are committed to this tentative parse, whether or not an error
1251 CP_PARSER_STATUS_KIND_COMMITTED
1252 } cp_parser_status_kind
;
1254 typedef struct cp_parser_expression_stack_entry
1256 /* Left hand side of the binary operation we are currently
1259 /* Original tree code for left hand side, if it was a binary
1260 expression itself (used for -Wparentheses). */
1261 enum tree_code lhs_type
;
1262 /* Tree code for the binary operation we are parsing. */
1263 enum tree_code tree_type
;
1264 /* Precedence of the binary operation we are parsing. */
1266 } cp_parser_expression_stack_entry
;
1268 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1269 entries because precedence levels on the stack are monotonically
1271 typedef struct cp_parser_expression_stack_entry
1272 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1274 /* Context that is saved and restored when parsing tentatively. */
1275 typedef struct cp_parser_context
GTY (())
1277 /* If this is a tentative parsing context, the status of the
1279 enum cp_parser_status_kind status
;
1280 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1281 that are looked up in this context must be looked up both in the
1282 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1283 the context of the containing expression. */
1286 /* The next parsing context in the stack. */
1287 struct cp_parser_context
*next
;
1288 } cp_parser_context
;
1292 /* Constructors and destructors. */
1294 static cp_parser_context
*cp_parser_context_new
1295 (cp_parser_context
*);
1297 /* Class variables. */
1299 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1301 /* The operator-precedence table used by cp_parser_binary_expression.
1302 Transformed into an associative array (binops_by_token) by
1305 static const cp_parser_binary_operations_map_node binops
[] = {
1306 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1307 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1309 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1310 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1311 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1313 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1314 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1316 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1317 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1319 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1320 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1321 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1322 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1324 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1325 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1327 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1329 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1331 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1333 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1335 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1338 /* The same as binops, but initialized by cp_parser_new so that
1339 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1341 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1343 /* Constructors and destructors. */
1345 /* Construct a new context. The context below this one on the stack
1346 is given by NEXT. */
1348 static cp_parser_context
*
1349 cp_parser_context_new (cp_parser_context
* next
)
1351 cp_parser_context
*context
;
1353 /* Allocate the storage. */
1354 if (cp_parser_context_free_list
!= NULL
)
1356 /* Pull the first entry from the free list. */
1357 context
= cp_parser_context_free_list
;
1358 cp_parser_context_free_list
= context
->next
;
1359 memset (context
, 0, sizeof (*context
));
1362 context
= GGC_CNEW (cp_parser_context
);
1364 /* No errors have occurred yet in this context. */
1365 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1366 /* If this is not the bottomost context, copy information that we
1367 need from the previous context. */
1370 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1371 expression, then we are parsing one in this context, too. */
1372 context
->object_type
= next
->object_type
;
1373 /* Thread the stack. */
1374 context
->next
= next
;
1380 /* The cp_parser structure represents the C++ parser. */
1382 typedef struct cp_parser
GTY(())
1384 /* The lexer from which we are obtaining tokens. */
1387 /* The scope in which names should be looked up. If NULL_TREE, then
1388 we look up names in the scope that is currently open in the
1389 source program. If non-NULL, this is either a TYPE or
1390 NAMESPACE_DECL for the scope in which we should look. It can
1391 also be ERROR_MARK, when we've parsed a bogus scope.
1393 This value is not cleared automatically after a name is looked
1394 up, so we must be careful to clear it before starting a new look
1395 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1396 will look up `Z' in the scope of `X', rather than the current
1397 scope.) Unfortunately, it is difficult to tell when name lookup
1398 is complete, because we sometimes peek at a token, look it up,
1399 and then decide not to consume it. */
1402 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1403 last lookup took place. OBJECT_SCOPE is used if an expression
1404 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1405 respectively. QUALIFYING_SCOPE is used for an expression of the
1406 form "X::Y"; it refers to X. */
1408 tree qualifying_scope
;
1410 /* A stack of parsing contexts. All but the bottom entry on the
1411 stack will be tentative contexts.
1413 We parse tentatively in order to determine which construct is in
1414 use in some situations. For example, in order to determine
1415 whether a statement is an expression-statement or a
1416 declaration-statement we parse it tentatively as a
1417 declaration-statement. If that fails, we then reparse the same
1418 token stream as an expression-statement. */
1419 cp_parser_context
*context
;
1421 /* True if we are parsing GNU C++. If this flag is not set, then
1422 GNU extensions are not recognized. */
1423 bool allow_gnu_extensions_p
;
1425 /* TRUE if the `>' token should be interpreted as the greater-than
1426 operator. FALSE if it is the end of a template-id or
1427 template-parameter-list. In C++0x mode, this flag also applies to
1428 `>>' tokens, which are viewed as two consecutive `>' tokens when
1429 this flag is FALSE. */
1430 bool greater_than_is_operator_p
;
1432 /* TRUE if default arguments are allowed within a parameter list
1433 that starts at this point. FALSE if only a gnu extension makes
1434 them permissible. */
1435 bool default_arg_ok_p
;
1437 /* TRUE if we are parsing an integral constant-expression. See
1438 [expr.const] for a precise definition. */
1439 bool integral_constant_expression_p
;
1441 /* TRUE if we are parsing an integral constant-expression -- but a
1442 non-constant expression should be permitted as well. This flag
1443 is used when parsing an array bound so that GNU variable-length
1444 arrays are tolerated. */
1445 bool allow_non_integral_constant_expression_p
;
1447 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1448 been seen that makes the expression non-constant. */
1449 bool non_integral_constant_expression_p
;
1451 /* TRUE if local variable names and `this' are forbidden in the
1453 bool local_variables_forbidden_p
;
1455 /* TRUE if the declaration we are parsing is part of a
1456 linkage-specification of the form `extern string-literal
1458 bool in_unbraced_linkage_specification_p
;
1460 /* TRUE if we are presently parsing a declarator, after the
1461 direct-declarator. */
1462 bool in_declarator_p
;
1464 /* TRUE if we are presently parsing a template-argument-list. */
1465 bool in_template_argument_list_p
;
1467 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1468 to IN_OMP_BLOCK if parsing OpenMP structured block and
1469 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1470 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1471 iteration-statement, OpenMP block or loop within that switch. */
1472 #define IN_SWITCH_STMT 1
1473 #define IN_ITERATION_STMT 2
1474 #define IN_OMP_BLOCK 4
1475 #define IN_OMP_FOR 8
1476 #define IN_IF_STMT 16
1477 unsigned char in_statement
;
1479 /* TRUE if we are presently parsing the body of a switch statement.
1480 Note that this doesn't quite overlap with in_statement above.
1481 The difference relates to giving the right sets of error messages:
1482 "case not in switch" vs "break statement used with OpenMP...". */
1483 bool in_switch_statement_p
;
1485 /* TRUE if we are parsing a type-id in an expression context. In
1486 such a situation, both "type (expr)" and "type (type)" are valid
1488 bool in_type_id_in_expr_p
;
1490 /* TRUE if we are currently in a header file where declarations are
1491 implicitly extern "C". */
1492 bool implicit_extern_c
;
1494 /* TRUE if strings in expressions should be translated to the execution
1496 bool translate_strings_p
;
1498 /* TRUE if we are presently parsing the body of a function, but not
1500 bool in_function_body
;
1502 /* If non-NULL, then we are parsing a construct where new type
1503 definitions are not permitted. The string stored here will be
1504 issued as an error message if a type is defined. */
1505 const char *type_definition_forbidden_message
;
1507 /* A list of lists. The outer list is a stack, used for member
1508 functions of local classes. At each level there are two sub-list,
1509 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1510 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1511 TREE_VALUE's. The functions are chained in reverse declaration
1514 The TREE_PURPOSE sublist contains those functions with default
1515 arguments that need post processing, and the TREE_VALUE sublist
1516 contains those functions with definitions that need post
1519 These lists can only be processed once the outermost class being
1520 defined is complete. */
1521 tree unparsed_functions_queues
;
1523 /* The number of classes whose definitions are currently in
1525 unsigned num_classes_being_defined
;
1527 /* The number of template parameter lists that apply directly to the
1528 current declaration. */
1529 unsigned num_template_parameter_lists
;
1534 /* Constructors and destructors. */
1536 static cp_parser
*cp_parser_new
1539 /* Routines to parse various constructs.
1541 Those that return `tree' will return the error_mark_node (rather
1542 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1543 Sometimes, they will return an ordinary node if error-recovery was
1544 attempted, even though a parse error occurred. So, to check
1545 whether or not a parse error occurred, you should always use
1546 cp_parser_error_occurred. If the construct is optional (indicated
1547 either by an `_opt' in the name of the function that does the
1548 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1549 the construct is not present. */
1551 /* Lexical conventions [gram.lex] */
1553 static tree cp_parser_identifier
1555 static tree cp_parser_string_literal
1556 (cp_parser
*, bool, bool);
1558 /* Basic concepts [gram.basic] */
1560 static bool cp_parser_translation_unit
1563 /* Expressions [gram.expr] */
1565 static tree cp_parser_primary_expression
1566 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1567 static tree cp_parser_id_expression
1568 (cp_parser
*, bool, bool, bool *, bool, bool);
1569 static tree cp_parser_unqualified_id
1570 (cp_parser
*, bool, bool, bool, bool);
1571 static tree cp_parser_nested_name_specifier_opt
1572 (cp_parser
*, bool, bool, bool, bool);
1573 static tree cp_parser_nested_name_specifier
1574 (cp_parser
*, bool, bool, bool, bool);
1575 static tree cp_parser_class_or_namespace_name
1576 (cp_parser
*, bool, bool, bool, bool, bool);
1577 static tree cp_parser_postfix_expression
1578 (cp_parser
*, bool, bool, bool);
1579 static tree cp_parser_postfix_open_square_expression
1580 (cp_parser
*, tree
, bool);
1581 static tree cp_parser_postfix_dot_deref_expression
1582 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1583 static tree cp_parser_parenthesized_expression_list
1584 (cp_parser
*, bool, bool, bool, bool *);
1585 static void cp_parser_pseudo_destructor_name
1586 (cp_parser
*, tree
*, tree
*);
1587 static tree cp_parser_unary_expression
1588 (cp_parser
*, bool, bool);
1589 static enum tree_code cp_parser_unary_operator
1591 static tree cp_parser_new_expression
1593 static tree cp_parser_new_placement
1595 static tree cp_parser_new_type_id
1596 (cp_parser
*, tree
*);
1597 static cp_declarator
*cp_parser_new_declarator_opt
1599 static cp_declarator
*cp_parser_direct_new_declarator
1601 static tree cp_parser_new_initializer
1603 static tree cp_parser_delete_expression
1605 static tree cp_parser_cast_expression
1606 (cp_parser
*, bool, bool);
1607 static tree cp_parser_binary_expression
1608 (cp_parser
*, bool);
1609 static tree cp_parser_question_colon_clause
1610 (cp_parser
*, tree
);
1611 static tree cp_parser_assignment_expression
1612 (cp_parser
*, bool);
1613 static enum tree_code cp_parser_assignment_operator_opt
1615 static tree cp_parser_expression
1616 (cp_parser
*, bool);
1617 static tree cp_parser_constant_expression
1618 (cp_parser
*, bool, bool *);
1619 static tree cp_parser_builtin_offsetof
1622 /* Statements [gram.stmt.stmt] */
1624 static void cp_parser_statement
1625 (cp_parser
*, tree
, bool, bool *);
1626 static void cp_parser_label_for_labeled_statement
1628 static tree cp_parser_expression_statement
1629 (cp_parser
*, tree
);
1630 static tree cp_parser_compound_statement
1631 (cp_parser
*, tree
, bool);
1632 static void cp_parser_statement_seq_opt
1633 (cp_parser
*, tree
);
1634 static tree cp_parser_selection_statement
1635 (cp_parser
*, bool *);
1636 static tree cp_parser_condition
1638 static tree cp_parser_iteration_statement
1640 static void cp_parser_for_init_statement
1642 static tree cp_parser_jump_statement
1644 static void cp_parser_declaration_statement
1647 static tree cp_parser_implicitly_scoped_statement
1648 (cp_parser
*, bool *);
1649 static void cp_parser_already_scoped_statement
1652 /* Declarations [gram.dcl.dcl] */
1654 static void cp_parser_declaration_seq_opt
1656 static void cp_parser_declaration
1658 static void cp_parser_block_declaration
1659 (cp_parser
*, bool);
1660 static void cp_parser_simple_declaration
1661 (cp_parser
*, bool);
1662 static void cp_parser_decl_specifier_seq
1663 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1664 static tree cp_parser_storage_class_specifier_opt
1666 static tree cp_parser_function_specifier_opt
1667 (cp_parser
*, cp_decl_specifier_seq
*);
1668 static tree cp_parser_type_specifier
1669 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1671 static tree cp_parser_simple_type_specifier
1672 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1673 static tree cp_parser_type_name
1675 static tree cp_parser_nonclass_name
1676 (cp_parser
* parser
);
1677 static tree cp_parser_elaborated_type_specifier
1678 (cp_parser
*, bool, bool);
1679 static tree cp_parser_enum_specifier
1681 static void cp_parser_enumerator_list
1682 (cp_parser
*, tree
);
1683 static void cp_parser_enumerator_definition
1684 (cp_parser
*, tree
);
1685 static tree cp_parser_namespace_name
1687 static void cp_parser_namespace_definition
1689 static void cp_parser_namespace_body
1691 static tree cp_parser_qualified_namespace_specifier
1693 static void cp_parser_namespace_alias_definition
1695 static bool cp_parser_using_declaration
1696 (cp_parser
*, bool);
1697 static void cp_parser_using_directive
1699 static void cp_parser_asm_definition
1701 static void cp_parser_linkage_specification
1703 static void cp_parser_static_assert
1704 (cp_parser
*, bool);
1705 static tree cp_parser_decltype
1708 /* Declarators [gram.dcl.decl] */
1710 static tree cp_parser_init_declarator
1711 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1712 static cp_declarator
*cp_parser_declarator
1713 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1714 static cp_declarator
*cp_parser_direct_declarator
1715 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1716 static enum tree_code cp_parser_ptr_operator
1717 (cp_parser
*, tree
*, cp_cv_quals
*);
1718 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1720 static tree cp_parser_declarator_id
1721 (cp_parser
*, bool);
1722 static tree cp_parser_type_id
1724 static void cp_parser_type_specifier_seq
1725 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1726 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1728 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1729 (cp_parser
*, bool *);
1730 static cp_parameter_declarator
*cp_parser_parameter_declaration
1731 (cp_parser
*, bool, bool *);
1732 static tree cp_parser_default_argument
1733 (cp_parser
*, bool);
1734 static void cp_parser_function_body
1736 static tree cp_parser_initializer
1737 (cp_parser
*, bool *, bool *);
1738 static tree cp_parser_initializer_clause
1739 (cp_parser
*, bool *);
1740 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1741 (cp_parser
*, bool *);
1743 static bool cp_parser_ctor_initializer_opt_and_function_body
1746 /* Classes [gram.class] */
1748 static tree cp_parser_class_name
1749 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1750 static tree cp_parser_class_specifier
1752 static tree cp_parser_class_head
1753 (cp_parser
*, bool *, tree
*, tree
*);
1754 static enum tag_types cp_parser_class_key
1756 static void cp_parser_member_specification_opt
1758 static void cp_parser_member_declaration
1760 static tree cp_parser_pure_specifier
1762 static tree cp_parser_constant_initializer
1765 /* Derived classes [gram.class.derived] */
1767 static tree cp_parser_base_clause
1769 static tree cp_parser_base_specifier
1772 /* Special member functions [gram.special] */
1774 static tree cp_parser_conversion_function_id
1776 static tree cp_parser_conversion_type_id
1778 static cp_declarator
*cp_parser_conversion_declarator_opt
1780 static bool cp_parser_ctor_initializer_opt
1782 static void cp_parser_mem_initializer_list
1784 static tree cp_parser_mem_initializer
1786 static tree cp_parser_mem_initializer_id
1789 /* Overloading [gram.over] */
1791 static tree cp_parser_operator_function_id
1793 static tree cp_parser_operator
1796 /* Templates [gram.temp] */
1798 static void cp_parser_template_declaration
1799 (cp_parser
*, bool);
1800 static tree cp_parser_template_parameter_list
1802 static tree cp_parser_template_parameter
1803 (cp_parser
*, bool *, bool *);
1804 static tree cp_parser_type_parameter
1805 (cp_parser
*, bool *);
1806 static tree cp_parser_template_id
1807 (cp_parser
*, bool, bool, bool);
1808 static tree cp_parser_template_name
1809 (cp_parser
*, bool, bool, bool, bool *);
1810 static tree cp_parser_template_argument_list
1812 static tree cp_parser_template_argument
1814 static void cp_parser_explicit_instantiation
1816 static void cp_parser_explicit_specialization
1819 /* Exception handling [gram.exception] */
1821 static tree cp_parser_try_block
1823 static bool cp_parser_function_try_block
1825 static void cp_parser_handler_seq
1827 static void cp_parser_handler
1829 static tree cp_parser_exception_declaration
1831 static tree cp_parser_throw_expression
1833 static tree cp_parser_exception_specification_opt
1835 static tree cp_parser_type_id_list
1838 /* GNU Extensions */
1840 static tree cp_parser_asm_specification_opt
1842 static tree cp_parser_asm_operand_list
1844 static tree cp_parser_asm_clobber_list
1846 static tree cp_parser_attributes_opt
1848 static tree cp_parser_attribute_list
1850 static bool cp_parser_extension_opt
1851 (cp_parser
*, int *);
1852 static void cp_parser_label_declaration
1855 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1856 static bool cp_parser_pragma
1857 (cp_parser
*, enum pragma_context
);
1859 /* Objective-C++ Productions */
1861 static tree cp_parser_objc_message_receiver
1863 static tree cp_parser_objc_message_args
1865 static tree cp_parser_objc_message_expression
1867 static tree cp_parser_objc_encode_expression
1869 static tree cp_parser_objc_defs_expression
1871 static tree cp_parser_objc_protocol_expression
1873 static tree cp_parser_objc_selector_expression
1875 static tree cp_parser_objc_expression
1877 static bool cp_parser_objc_selector_p
1879 static tree cp_parser_objc_selector
1881 static tree cp_parser_objc_protocol_refs_opt
1883 static void cp_parser_objc_declaration
1885 static tree cp_parser_objc_statement
1888 /* Utility Routines */
1890 static tree cp_parser_lookup_name
1891 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1892 static tree cp_parser_lookup_name_simple
1893 (cp_parser
*, tree
);
1894 static tree cp_parser_maybe_treat_template_as_class
1896 static bool cp_parser_check_declarator_template_parameters
1897 (cp_parser
*, cp_declarator
*);
1898 static bool cp_parser_check_template_parameters
1899 (cp_parser
*, unsigned);
1900 static tree cp_parser_simple_cast_expression
1902 static tree cp_parser_global_scope_opt
1903 (cp_parser
*, bool);
1904 static bool cp_parser_constructor_declarator_p
1905 (cp_parser
*, bool);
1906 static tree cp_parser_function_definition_from_specifiers_and_declarator
1907 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1908 static tree cp_parser_function_definition_after_declarator
1909 (cp_parser
*, bool);
1910 static void cp_parser_template_declaration_after_export
1911 (cp_parser
*, bool);
1912 static void cp_parser_perform_template_parameter_access_checks
1913 (VEC (deferred_access_check
,gc
)*);
1914 static tree cp_parser_single_declaration
1915 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
1916 static tree cp_parser_functional_cast
1917 (cp_parser
*, tree
);
1918 static tree cp_parser_save_member_function_body
1919 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1920 static tree cp_parser_enclosed_template_argument_list
1922 static void cp_parser_save_default_args
1923 (cp_parser
*, tree
);
1924 static void cp_parser_late_parsing_for_member
1925 (cp_parser
*, tree
);
1926 static void cp_parser_late_parsing_default_args
1927 (cp_parser
*, tree
);
1928 static tree cp_parser_sizeof_operand
1929 (cp_parser
*, enum rid
);
1930 static tree cp_parser_trait_expr
1931 (cp_parser
*, enum rid
);
1932 static bool cp_parser_declares_only_class_p
1934 static void cp_parser_set_storage_class
1935 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1936 static void cp_parser_set_decl_spec_type
1937 (cp_decl_specifier_seq
*, tree
, bool);
1938 static bool cp_parser_friend_p
1939 (const cp_decl_specifier_seq
*);
1940 static cp_token
*cp_parser_require
1941 (cp_parser
*, enum cpp_ttype
, const char *);
1942 static cp_token
*cp_parser_require_keyword
1943 (cp_parser
*, enum rid
, const char *);
1944 static bool cp_parser_token_starts_function_definition_p
1946 static bool cp_parser_next_token_starts_class_definition_p
1948 static bool cp_parser_next_token_ends_template_argument_p
1950 static bool cp_parser_nth_token_starts_template_argument_list_p
1951 (cp_parser
*, size_t);
1952 static enum tag_types cp_parser_token_is_class_key
1954 static void cp_parser_check_class_key
1955 (enum tag_types
, tree type
);
1956 static void cp_parser_check_access_in_redeclaration
1958 static bool cp_parser_optional_template_keyword
1960 static void cp_parser_pre_parsed_nested_name_specifier
1962 static void cp_parser_cache_group
1963 (cp_parser
*, enum cpp_ttype
, unsigned);
1964 static void cp_parser_parse_tentatively
1966 static void cp_parser_commit_to_tentative_parse
1968 static void cp_parser_abort_tentative_parse
1970 static bool cp_parser_parse_definitely
1972 static inline bool cp_parser_parsing_tentatively
1974 static bool cp_parser_uncommitted_to_tentative_parse_p
1976 static void cp_parser_error
1977 (cp_parser
*, const char *);
1978 static void cp_parser_name_lookup_error
1979 (cp_parser
*, tree
, tree
, const char *);
1980 static bool cp_parser_simulate_error
1982 static bool cp_parser_check_type_definition
1984 static void cp_parser_check_for_definition_in_return_type
1985 (cp_declarator
*, tree
);
1986 static void cp_parser_check_for_invalid_template_id
1987 (cp_parser
*, tree
);
1988 static bool cp_parser_non_integral_constant_expression
1989 (cp_parser
*, const char *);
1990 static void cp_parser_diagnose_invalid_type_name
1991 (cp_parser
*, tree
, tree
);
1992 static bool cp_parser_parse_and_diagnose_invalid_type_name
1994 static int cp_parser_skip_to_closing_parenthesis
1995 (cp_parser
*, bool, bool, bool);
1996 static void cp_parser_skip_to_end_of_statement
1998 static void cp_parser_consume_semicolon_at_end_of_statement
2000 static void cp_parser_skip_to_end_of_block_or_statement
2002 static bool cp_parser_skip_to_closing_brace
2004 static void cp_parser_skip_to_end_of_template_parameter_list
2006 static void cp_parser_skip_to_pragma_eol
2007 (cp_parser
*, cp_token
*);
2008 static bool cp_parser_error_occurred
2010 static bool cp_parser_allow_gnu_extensions_p
2012 static bool cp_parser_is_string_literal
2014 static bool cp_parser_is_keyword
2015 (cp_token
*, enum rid
);
2016 static tree cp_parser_make_typename_type
2017 (cp_parser
*, tree
, tree
);
2018 static cp_declarator
* cp_parser_make_indirect_declarator
2019 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2021 /* Returns nonzero if we are parsing tentatively. */
2024 cp_parser_parsing_tentatively (cp_parser
* parser
)
2026 return parser
->context
->next
!= NULL
;
2029 /* Returns nonzero if TOKEN is a string literal. */
2032 cp_parser_is_string_literal (cp_token
* token
)
2034 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
2037 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2040 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2042 return token
->keyword
== keyword
;
2045 /* If not parsing tentatively, issue a diagnostic of the form
2046 FILE:LINE: MESSAGE before TOKEN
2047 where TOKEN is the next token in the input stream. MESSAGE
2048 (specified by the caller) is usually of the form "expected
2052 cp_parser_error (cp_parser
* parser
, const char* message
)
2054 if (!cp_parser_simulate_error (parser
))
2056 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2057 /* This diagnostic makes more sense if it is tagged to the line
2058 of the token we just peeked at. */
2059 cp_lexer_set_source_position_from_token (token
);
2061 if (token
->type
== CPP_PRAGMA
)
2063 error ("%<#pragma%> is not allowed here");
2064 cp_parser_skip_to_pragma_eol (parser
, token
);
2068 c_parse_error (message
,
2069 /* Because c_parser_error does not understand
2070 CPP_KEYWORD, keywords are treated like
2072 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2077 /* Issue an error about name-lookup failing. NAME is the
2078 IDENTIFIER_NODE DECL is the result of
2079 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2080 the thing that we hoped to find. */
2083 cp_parser_name_lookup_error (cp_parser
* parser
,
2086 const char* desired
)
2088 /* If name lookup completely failed, tell the user that NAME was not
2090 if (decl
== error_mark_node
)
2092 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2093 error ("%<%E::%E%> has not been declared",
2094 parser
->scope
, name
);
2095 else if (parser
->scope
== global_namespace
)
2096 error ("%<::%E%> has not been declared", name
);
2097 else if (parser
->object_scope
2098 && !CLASS_TYPE_P (parser
->object_scope
))
2099 error ("request for member %qE in non-class type %qT",
2100 name
, parser
->object_scope
);
2101 else if (parser
->object_scope
)
2102 error ("%<%T::%E%> has not been declared",
2103 parser
->object_scope
, name
);
2105 error ("%qE has not been declared", name
);
2107 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2108 error ("%<%E::%E%> %s", parser
->scope
, name
, desired
);
2109 else if (parser
->scope
== global_namespace
)
2110 error ("%<::%E%> %s", name
, desired
);
2112 error ("%qE %s", name
, desired
);
2115 /* If we are parsing tentatively, remember that an error has occurred
2116 during this tentative parse. Returns true if the error was
2117 simulated; false if a message should be issued by the caller. */
2120 cp_parser_simulate_error (cp_parser
* parser
)
2122 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2124 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2130 /* Check for repeated decl-specifiers. */
2133 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
2137 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2139 unsigned count
= decl_specs
->specs
[(int)ds
];
2142 /* The "long" specifier is a special case because of "long long". */
2146 error ("%<long long long%> is too long for GCC");
2147 else if (pedantic
&& !in_system_header
&& warn_long_long
2148 && cxx_dialect
== cxx98
)
2149 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2153 static const char *const decl_spec_names
[] = {
2169 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2174 /* This function is called when a type is defined. If type
2175 definitions are forbidden at this point, an error message is
2179 cp_parser_check_type_definition (cp_parser
* parser
)
2181 /* If types are forbidden here, issue a message. */
2182 if (parser
->type_definition_forbidden_message
)
2184 /* Use `%s' to print the string in case there are any escape
2185 characters in the message. */
2186 error ("%s", parser
->type_definition_forbidden_message
);
2192 /* This function is called when the DECLARATOR is processed. The TYPE
2193 was a type defined in the decl-specifiers. If it is invalid to
2194 define a type in the decl-specifiers for DECLARATOR, an error is
2198 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2201 /* [dcl.fct] forbids type definitions in return types.
2202 Unfortunately, it's not easy to know whether or not we are
2203 processing a return type until after the fact. */
2205 && (declarator
->kind
== cdk_pointer
2206 || declarator
->kind
== cdk_reference
2207 || declarator
->kind
== cdk_ptrmem
))
2208 declarator
= declarator
->declarator
;
2210 && declarator
->kind
== cdk_function
)
2212 error ("new types may not be defined in a return type");
2213 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2218 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2219 "<" in any valid C++ program. If the next token is indeed "<",
2220 issue a message warning the user about what appears to be an
2221 invalid attempt to form a template-id. */
2224 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2227 cp_token_position start
= 0;
2229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2232 error ("%qT is not a template", type
);
2233 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2234 error ("%qE is not a template", type
);
2236 error ("invalid template-id");
2237 /* Remember the location of the invalid "<". */
2238 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2239 start
= cp_lexer_token_position (parser
->lexer
, true);
2240 /* Consume the "<". */
2241 cp_lexer_consume_token (parser
->lexer
);
2242 /* Parse the template arguments. */
2243 cp_parser_enclosed_template_argument_list (parser
);
2244 /* Permanently remove the invalid template arguments so that
2245 this error message is not issued again. */
2247 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2251 /* If parsing an integral constant-expression, issue an error message
2252 about the fact that THING appeared and return true. Otherwise,
2253 return false. In either case, set
2254 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2257 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2260 parser
->non_integral_constant_expression_p
= true;
2261 if (parser
->integral_constant_expression_p
)
2263 if (!parser
->allow_non_integral_constant_expression_p
)
2265 error ("%s cannot appear in a constant-expression", thing
);
2272 /* Emit a diagnostic for an invalid type name. SCOPE is the
2273 qualifying scope (or NULL, if none) for ID. This function commits
2274 to the current active tentative parse, if any. (Otherwise, the
2275 problematic construct might be encountered again later, resulting
2276 in duplicate error messages.) */
2279 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2281 tree decl
, old_scope
;
2282 /* Try to lookup the identifier. */
2283 old_scope
= parser
->scope
;
2284 parser
->scope
= scope
;
2285 decl
= cp_parser_lookup_name_simple (parser
, id
);
2286 parser
->scope
= old_scope
;
2287 /* If the lookup found a template-name, it means that the user forgot
2288 to specify an argument list. Emit a useful error message. */
2289 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2290 error ("invalid use of template-name %qE without an argument list", decl
);
2291 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2292 error ("invalid use of destructor %qD as a type", id
);
2293 else if (TREE_CODE (decl
) == TYPE_DECL
)
2294 /* Something like 'unsigned A a;' */
2295 error ("invalid combination of multiple type-specifiers");
2296 else if (!parser
->scope
)
2298 /* Issue an error message. */
2299 error ("%qE does not name a type", id
);
2300 /* If we're in a template class, it's possible that the user was
2301 referring to a type from a base class. For example:
2303 template <typename T> struct A { typedef T X; };
2304 template <typename T> struct B : public A<T> { X x; };
2306 The user should have said "typename A<T>::X". */
2307 if (processing_template_decl
&& current_class_type
2308 && TYPE_BINFO (current_class_type
))
2312 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2316 tree base_type
= BINFO_TYPE (b
);
2317 if (CLASS_TYPE_P (base_type
)
2318 && dependent_type_p (base_type
))
2321 /* Go from a particular instantiation of the
2322 template (which will have an empty TYPE_FIELDs),
2323 to the main version. */
2324 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2325 for (field
= TYPE_FIELDS (base_type
);
2327 field
= TREE_CHAIN (field
))
2328 if (TREE_CODE (field
) == TYPE_DECL
2329 && DECL_NAME (field
) == id
)
2331 inform ("(perhaps %<typename %T::%E%> was intended)",
2332 BINFO_TYPE (b
), id
);
2341 /* Here we diagnose qualified-ids where the scope is actually correct,
2342 but the identifier does not resolve to a valid type name. */
2343 else if (parser
->scope
!= error_mark_node
)
2345 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2346 error ("%qE in namespace %qE does not name a type",
2348 else if (TYPE_P (parser
->scope
))
2349 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2353 cp_parser_commit_to_tentative_parse (parser
);
2356 /* Check for a common situation where a type-name should be present,
2357 but is not, and issue a sensible error message. Returns true if an
2358 invalid type-name was detected.
2360 The situation handled by this function are variable declarations of the
2361 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2362 Usually, `ID' should name a type, but if we got here it means that it
2363 does not. We try to emit the best possible error message depending on
2364 how exactly the id-expression looks like. */
2367 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2371 cp_parser_parse_tentatively (parser
);
2372 id
= cp_parser_id_expression (parser
,
2373 /*template_keyword_p=*/false,
2374 /*check_dependency_p=*/true,
2375 /*template_p=*/NULL
,
2376 /*declarator_p=*/true,
2377 /*optional_p=*/false);
2378 /* After the id-expression, there should be a plain identifier,
2379 otherwise this is not a simple variable declaration. Also, if
2380 the scope is dependent, we cannot do much. */
2381 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2382 || (parser
->scope
&& TYPE_P (parser
->scope
)
2383 && dependent_type_p (parser
->scope
))
2384 || TREE_CODE (id
) == TYPE_DECL
)
2386 cp_parser_abort_tentative_parse (parser
);
2389 if (!cp_parser_parse_definitely (parser
))
2392 /* Emit a diagnostic for the invalid type. */
2393 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2394 /* Skip to the end of the declaration; there's no point in
2395 trying to process it. */
2396 cp_parser_skip_to_end_of_block_or_statement (parser
);
2400 /* Consume tokens up to, and including, the next non-nested closing `)'.
2401 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2402 are doing error recovery. Returns -1 if OR_COMMA is true and we
2403 found an unnested comma. */
2406 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2411 unsigned paren_depth
= 0;
2412 unsigned brace_depth
= 0;
2414 if (recovering
&& !or_comma
2415 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2420 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2422 switch (token
->type
)
2425 case CPP_PRAGMA_EOL
:
2426 /* If we've run out of tokens, then there is no closing `)'. */
2430 /* This matches the processing in skip_to_end_of_statement. */
2435 case CPP_OPEN_BRACE
:
2438 case CPP_CLOSE_BRACE
:
2444 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2448 case CPP_OPEN_PAREN
:
2453 case CPP_CLOSE_PAREN
:
2454 if (!brace_depth
&& !paren_depth
--)
2457 cp_lexer_consume_token (parser
->lexer
);
2466 /* Consume the token. */
2467 cp_lexer_consume_token (parser
->lexer
);
2471 /* Consume tokens until we reach the end of the current statement.
2472 Normally, that will be just before consuming a `;'. However, if a
2473 non-nested `}' comes first, then we stop before consuming that. */
2476 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2478 unsigned nesting_depth
= 0;
2482 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2484 switch (token
->type
)
2487 case CPP_PRAGMA_EOL
:
2488 /* If we've run out of tokens, stop. */
2492 /* If the next token is a `;', we have reached the end of the
2498 case CPP_CLOSE_BRACE
:
2499 /* If this is a non-nested '}', stop before consuming it.
2500 That way, when confronted with something like:
2504 we stop before consuming the closing '}', even though we
2505 have not yet reached a `;'. */
2506 if (nesting_depth
== 0)
2509 /* If it is the closing '}' for a block that we have
2510 scanned, stop -- but only after consuming the token.
2516 we will stop after the body of the erroneously declared
2517 function, but before consuming the following `typedef'
2519 if (--nesting_depth
== 0)
2521 cp_lexer_consume_token (parser
->lexer
);
2525 case CPP_OPEN_BRACE
:
2533 /* Consume the token. */
2534 cp_lexer_consume_token (parser
->lexer
);
2538 /* This function is called at the end of a statement or declaration.
2539 If the next token is a semicolon, it is consumed; otherwise, error
2540 recovery is attempted. */
2543 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2545 /* Look for the trailing `;'. */
2546 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2548 /* If there is additional (erroneous) input, skip to the end of
2550 cp_parser_skip_to_end_of_statement (parser
);
2551 /* If the next token is now a `;', consume it. */
2552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2553 cp_lexer_consume_token (parser
->lexer
);
2557 /* Skip tokens until we have consumed an entire block, or until we
2558 have consumed a non-nested `;'. */
2561 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2563 int nesting_depth
= 0;
2565 while (nesting_depth
>= 0)
2567 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2569 switch (token
->type
)
2572 case CPP_PRAGMA_EOL
:
2573 /* If we've run out of tokens, stop. */
2577 /* Stop if this is an unnested ';'. */
2582 case CPP_CLOSE_BRACE
:
2583 /* Stop if this is an unnested '}', or closes the outermost
2590 case CPP_OPEN_BRACE
:
2599 /* Consume the token. */
2600 cp_lexer_consume_token (parser
->lexer
);
2604 /* Skip tokens until a non-nested closing curly brace is the next
2605 token, or there are no more tokens. Return true in the first case,
2609 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2611 unsigned nesting_depth
= 0;
2615 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2617 switch (token
->type
)
2620 case CPP_PRAGMA_EOL
:
2621 /* If we've run out of tokens, stop. */
2624 case CPP_CLOSE_BRACE
:
2625 /* If the next token is a non-nested `}', then we have reached
2626 the end of the current block. */
2627 if (nesting_depth
-- == 0)
2631 case CPP_OPEN_BRACE
:
2632 /* If it the next token is a `{', then we are entering a new
2633 block. Consume the entire block. */
2641 /* Consume the token. */
2642 cp_lexer_consume_token (parser
->lexer
);
2646 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2647 parameter is the PRAGMA token, allowing us to purge the entire pragma
2651 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2655 parser
->lexer
->in_pragma
= false;
2658 token
= cp_lexer_consume_token (parser
->lexer
);
2659 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2661 /* Ensure that the pragma is not parsed again. */
2662 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2665 /* Require pragma end of line, resyncing with it as necessary. The
2666 arguments are as for cp_parser_skip_to_pragma_eol. */
2669 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2671 parser
->lexer
->in_pragma
= false;
2672 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2673 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2676 /* This is a simple wrapper around make_typename_type. When the id is
2677 an unresolved identifier node, we can provide a superior diagnostic
2678 using cp_parser_diagnose_invalid_type_name. */
2681 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2684 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2686 result
= make_typename_type (scope
, id
, typename_type
,
2687 /*complain=*/tf_none
);
2688 if (result
== error_mark_node
)
2689 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2692 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2695 /* This is a wrapper around the
2696 make_{pointer,ptrmem,reference}_declarator functions that decides
2697 which one to call based on the CODE and CLASS_TYPE arguments. The
2698 CODE argument should be one of the values returned by
2699 cp_parser_ptr_operator. */
2700 static cp_declarator
*
2701 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
2702 cp_cv_quals cv_qualifiers
,
2703 cp_declarator
*target
)
2705 if (code
== ERROR_MARK
)
2706 return cp_error_declarator
;
2708 if (code
== INDIRECT_REF
)
2709 if (class_type
== NULL_TREE
)
2710 return make_pointer_declarator (cv_qualifiers
, target
);
2712 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
2713 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
2714 return make_reference_declarator (cv_qualifiers
, target
, false);
2715 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
2716 return make_reference_declarator (cv_qualifiers
, target
, true);
2720 /* Create a new C++ parser. */
2723 cp_parser_new (void)
2729 /* cp_lexer_new_main is called before calling ggc_alloc because
2730 cp_lexer_new_main might load a PCH file. */
2731 lexer
= cp_lexer_new_main ();
2733 /* Initialize the binops_by_token so that we can get the tree
2734 directly from the token. */
2735 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2736 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2738 parser
= GGC_CNEW (cp_parser
);
2739 parser
->lexer
= lexer
;
2740 parser
->context
= cp_parser_context_new (NULL
);
2742 /* For now, we always accept GNU extensions. */
2743 parser
->allow_gnu_extensions_p
= 1;
2745 /* The `>' token is a greater-than operator, not the end of a
2747 parser
->greater_than_is_operator_p
= true;
2749 parser
->default_arg_ok_p
= true;
2751 /* We are not parsing a constant-expression. */
2752 parser
->integral_constant_expression_p
= false;
2753 parser
->allow_non_integral_constant_expression_p
= false;
2754 parser
->non_integral_constant_expression_p
= false;
2756 /* Local variable names are not forbidden. */
2757 parser
->local_variables_forbidden_p
= false;
2759 /* We are not processing an `extern "C"' declaration. */
2760 parser
->in_unbraced_linkage_specification_p
= false;
2762 /* We are not processing a declarator. */
2763 parser
->in_declarator_p
= false;
2765 /* We are not processing a template-argument-list. */
2766 parser
->in_template_argument_list_p
= false;
2768 /* We are not in an iteration statement. */
2769 parser
->in_statement
= 0;
2771 /* We are not in a switch statement. */
2772 parser
->in_switch_statement_p
= false;
2774 /* We are not parsing a type-id inside an expression. */
2775 parser
->in_type_id_in_expr_p
= false;
2777 /* Declarations aren't implicitly extern "C". */
2778 parser
->implicit_extern_c
= false;
2780 /* String literals should be translated to the execution character set. */
2781 parser
->translate_strings_p
= true;
2783 /* We are not parsing a function body. */
2784 parser
->in_function_body
= false;
2786 /* The unparsed function queue is empty. */
2787 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2789 /* There are no classes being defined. */
2790 parser
->num_classes_being_defined
= 0;
2792 /* No template parameters apply. */
2793 parser
->num_template_parameter_lists
= 0;
2798 /* Create a cp_lexer structure which will emit the tokens in CACHE
2799 and push it onto the parser's lexer stack. This is used for delayed
2800 parsing of in-class method bodies and default arguments, and should
2801 not be confused with tentative parsing. */
2803 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2805 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2806 lexer
->next
= parser
->lexer
;
2807 parser
->lexer
= lexer
;
2809 /* Move the current source position to that of the first token in the
2811 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2814 /* Pop the top lexer off the parser stack. This is never used for the
2815 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2817 cp_parser_pop_lexer (cp_parser
*parser
)
2819 cp_lexer
*lexer
= parser
->lexer
;
2820 parser
->lexer
= lexer
->next
;
2821 cp_lexer_destroy (lexer
);
2823 /* Put the current source position back where it was before this
2824 lexer was pushed. */
2825 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2828 /* Lexical conventions [gram.lex] */
2830 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2834 cp_parser_identifier (cp_parser
* parser
)
2838 /* Look for the identifier. */
2839 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2840 /* Return the value. */
2841 return token
? token
->u
.value
: error_mark_node
;
2844 /* Parse a sequence of adjacent string constants. Returns a
2845 TREE_STRING representing the combined, nul-terminated string
2846 constant. If TRANSLATE is true, translate the string to the
2847 execution character set. If WIDE_OK is true, a wide string is
2850 C++98 [lex.string] says that if a narrow string literal token is
2851 adjacent to a wide string literal token, the behavior is undefined.
2852 However, C99 6.4.5p4 says that this results in a wide string literal.
2853 We follow C99 here, for consistency with the C front end.
2855 This code is largely lifted from lex_string() in c-lex.c.
2857 FUTURE: ObjC++ will need to handle @-strings here. */
2859 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2864 struct obstack str_ob
;
2865 cpp_string str
, istr
, *strs
;
2868 tok
= cp_lexer_peek_token (parser
->lexer
);
2869 if (!cp_parser_is_string_literal (tok
))
2871 cp_parser_error (parser
, "expected string-literal");
2872 return error_mark_node
;
2875 /* Try to avoid the overhead of creating and destroying an obstack
2876 for the common case of just one string. */
2877 if (!cp_parser_is_string_literal
2878 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2880 cp_lexer_consume_token (parser
->lexer
);
2882 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2883 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2885 if (tok
->type
== CPP_WSTRING
)
2892 gcc_obstack_init (&str_ob
);
2897 cp_lexer_consume_token (parser
->lexer
);
2899 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2900 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2901 if (tok
->type
== CPP_WSTRING
)
2904 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2906 tok
= cp_lexer_peek_token (parser
->lexer
);
2908 while (cp_parser_is_string_literal (tok
));
2910 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2913 if (wide
&& !wide_ok
)
2915 cp_parser_error (parser
, "a wide string is invalid in this context");
2919 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2920 (parse_in
, strs
, count
, &istr
, wide
))
2922 value
= build_string (istr
.len
, (const char *)istr
.text
);
2923 free (CONST_CAST (unsigned char *, istr
.text
));
2925 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2926 value
= fix_string_type (value
);
2929 /* cpp_interpret_string has issued an error. */
2930 value
= error_mark_node
;
2933 obstack_free (&str_ob
, 0);
2939 /* Basic concepts [gram.basic] */
2941 /* Parse a translation-unit.
2944 declaration-seq [opt]
2946 Returns TRUE if all went well. */
2949 cp_parser_translation_unit (cp_parser
* parser
)
2951 /* The address of the first non-permanent object on the declarator
2953 static void *declarator_obstack_base
;
2957 /* Create the declarator obstack, if necessary. */
2958 if (!cp_error_declarator
)
2960 gcc_obstack_init (&declarator_obstack
);
2961 /* Create the error declarator. */
2962 cp_error_declarator
= make_declarator (cdk_error
);
2963 /* Create the empty parameter list. */
2964 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2965 /* Remember where the base of the declarator obstack lies. */
2966 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2969 cp_parser_declaration_seq_opt (parser
);
2971 /* If there are no tokens left then all went well. */
2972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2974 /* Get rid of the token array; we don't need it any more. */
2975 cp_lexer_destroy (parser
->lexer
);
2976 parser
->lexer
= NULL
;
2978 /* This file might have been a context that's implicitly extern
2979 "C". If so, pop the lang context. (Only relevant for PCH.) */
2980 if (parser
->implicit_extern_c
)
2982 pop_lang_context ();
2983 parser
->implicit_extern_c
= false;
2987 finish_translation_unit ();
2993 cp_parser_error (parser
, "expected declaration");
2997 /* Make sure the declarator obstack was fully cleaned up. */
2998 gcc_assert (obstack_next_free (&declarator_obstack
)
2999 == declarator_obstack_base
);
3001 /* All went well. */
3005 /* Expressions [gram.expr] */
3007 /* Parse a primary-expression.
3018 ( compound-statement )
3019 __builtin_va_arg ( assignment-expression , type-id )
3020 __builtin_offsetof ( type-id , offsetof-expression )
3023 __has_nothrow_assign ( type-id )
3024 __has_nothrow_constructor ( type-id )
3025 __has_nothrow_copy ( type-id )
3026 __has_trivial_assign ( type-id )
3027 __has_trivial_constructor ( type-id )
3028 __has_trivial_copy ( type-id )
3029 __has_trivial_destructor ( type-id )
3030 __has_virtual_destructor ( type-id )
3031 __is_abstract ( type-id )
3032 __is_base_of ( type-id , type-id )
3033 __is_class ( type-id )
3034 __is_convertible_to ( type-id , type-id )
3035 __is_empty ( type-id )
3036 __is_enum ( type-id )
3037 __is_pod ( type-id )
3038 __is_polymorphic ( type-id )
3039 __is_union ( type-id )
3041 Objective-C++ Extension:
3049 ADDRESS_P is true iff this expression was immediately preceded by
3050 "&" and therefore might denote a pointer-to-member. CAST_P is true
3051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3052 true iff this expression is a template argument.
3054 Returns a representation of the expression. Upon return, *IDK
3055 indicates what kind of id-expression (if any) was present. */
3058 cp_parser_primary_expression (cp_parser
*parser
,
3061 bool template_arg_p
,
3066 /* Assume the primary expression is not an id-expression. */
3067 *idk
= CP_ID_KIND_NONE
;
3069 /* Peek at the next token. */
3070 token
= cp_lexer_peek_token (parser
->lexer
);
3071 switch (token
->type
)
3082 token
= cp_lexer_consume_token (parser
->lexer
);
3083 /* Floating-point literals are only allowed in an integral
3084 constant expression if they are cast to an integral or
3085 enumeration type. */
3086 if (TREE_CODE (token
->u
.value
) == REAL_CST
3087 && parser
->integral_constant_expression_p
3090 /* CAST_P will be set even in invalid code like "int(2.7 +
3091 ...)". Therefore, we have to check that the next token
3092 is sure to end the cast. */
3095 cp_token
*next_token
;
3097 next_token
= cp_lexer_peek_token (parser
->lexer
);
3098 if (/* The comma at the end of an
3099 enumerator-definition. */
3100 next_token
->type
!= CPP_COMMA
3101 /* The curly brace at the end of an enum-specifier. */
3102 && next_token
->type
!= CPP_CLOSE_BRACE
3103 /* The end of a statement. */
3104 && next_token
->type
!= CPP_SEMICOLON
3105 /* The end of the cast-expression. */
3106 && next_token
->type
!= CPP_CLOSE_PAREN
3107 /* The end of an array bound. */
3108 && next_token
->type
!= CPP_CLOSE_SQUARE
3109 /* The closing ">" in a template-argument-list. */
3110 && (next_token
->type
!= CPP_GREATER
3111 || parser
->greater_than_is_operator_p
)
3112 /* C++0x only: A ">>" treated like two ">" tokens,
3113 in a template-argument-list. */
3114 && (next_token
->type
!= CPP_RSHIFT
3115 || (cxx_dialect
== cxx98
)
3116 || parser
->greater_than_is_operator_p
))
3120 /* If we are within a cast, then the constraint that the
3121 cast is to an integral or enumeration type will be
3122 checked at that point. If we are not within a cast, then
3123 this code is invalid. */
3125 cp_parser_non_integral_constant_expression
3126 (parser
, "floating-point literal");
3128 return token
->u
.value
;
3132 /* ??? Should wide strings be allowed when parser->translate_strings_p
3133 is false (i.e. in attributes)? If not, we can kill the third
3134 argument to cp_parser_string_literal. */
3135 return cp_parser_string_literal (parser
,
3136 parser
->translate_strings_p
,
3139 case CPP_OPEN_PAREN
:
3142 bool saved_greater_than_is_operator_p
;
3144 /* Consume the `('. */
3145 cp_lexer_consume_token (parser
->lexer
);
3146 /* Within a parenthesized expression, a `>' token is always
3147 the greater-than operator. */
3148 saved_greater_than_is_operator_p
3149 = parser
->greater_than_is_operator_p
;
3150 parser
->greater_than_is_operator_p
= true;
3151 /* If we see `( { ' then we are looking at the beginning of
3152 a GNU statement-expression. */
3153 if (cp_parser_allow_gnu_extensions_p (parser
)
3154 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3156 /* Statement-expressions are not allowed by the standard. */
3158 pedwarn ("ISO C++ forbids braced-groups within expressions");
3160 /* And they're not allowed outside of a function-body; you
3161 cannot, for example, write:
3163 int i = ({ int j = 3; j + 1; });
3165 at class or namespace scope. */
3166 if (!parser
->in_function_body
3167 || parser
->in_template_argument_list_p
)
3169 error ("statement-expressions are not allowed outside "
3170 "functions nor in template-argument lists");
3171 cp_parser_skip_to_end_of_block_or_statement (parser
);
3172 expr
= error_mark_node
;
3176 /* Start the statement-expression. */
3177 expr
= begin_stmt_expr ();
3178 /* Parse the compound-statement. */
3179 cp_parser_compound_statement (parser
, expr
, false);
3181 expr
= finish_stmt_expr (expr
, false);
3186 /* Parse the parenthesized expression. */
3187 expr
= cp_parser_expression (parser
, cast_p
);
3188 /* Let the front end know that this expression was
3189 enclosed in parentheses. This matters in case, for
3190 example, the expression is of the form `A::B', since
3191 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3193 finish_parenthesized_expr (expr
);
3195 /* The `>' token might be the end of a template-id or
3196 template-parameter-list now. */
3197 parser
->greater_than_is_operator_p
3198 = saved_greater_than_is_operator_p
;
3199 /* Consume the `)'. */
3200 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3201 cp_parser_skip_to_end_of_statement (parser
);
3207 switch (token
->keyword
)
3209 /* These two are the boolean literals. */
3211 cp_lexer_consume_token (parser
->lexer
);
3212 return boolean_true_node
;
3214 cp_lexer_consume_token (parser
->lexer
);
3215 return boolean_false_node
;
3217 /* The `__null' literal. */
3219 cp_lexer_consume_token (parser
->lexer
);
3222 /* Recognize the `this' keyword. */
3224 cp_lexer_consume_token (parser
->lexer
);
3225 if (parser
->local_variables_forbidden_p
)
3227 error ("%<this%> may not be used in this context");
3228 return error_mark_node
;
3230 /* Pointers cannot appear in constant-expressions. */
3231 if (cp_parser_non_integral_constant_expression (parser
,
3233 return error_mark_node
;
3234 return finish_this_expr ();
3236 /* The `operator' keyword can be the beginning of an
3241 case RID_FUNCTION_NAME
:
3242 case RID_PRETTY_FUNCTION_NAME
:
3243 case RID_C99_FUNCTION_NAME
:
3244 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3245 __func__ are the names of variables -- but they are
3246 treated specially. Therefore, they are handled here,
3247 rather than relying on the generic id-expression logic
3248 below. Grammatically, these names are id-expressions.
3250 Consume the token. */
3251 token
= cp_lexer_consume_token (parser
->lexer
);
3252 /* Look up the name. */
3253 return finish_fname (token
->u
.value
);
3260 /* The `__builtin_va_arg' construct is used to handle
3261 `va_arg'. Consume the `__builtin_va_arg' token. */
3262 cp_lexer_consume_token (parser
->lexer
);
3263 /* Look for the opening `('. */
3264 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3265 /* Now, parse the assignment-expression. */
3266 expression
= cp_parser_assignment_expression (parser
,
3268 /* Look for the `,'. */
3269 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3270 /* Parse the type-id. */
3271 type
= cp_parser_type_id (parser
);
3272 /* Look for the closing `)'. */
3273 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3274 /* Using `va_arg' in a constant-expression is not
3276 if (cp_parser_non_integral_constant_expression (parser
,
3278 return error_mark_node
;
3279 return build_x_va_arg (expression
, type
);
3283 return cp_parser_builtin_offsetof (parser
);
3285 case RID_HAS_NOTHROW_ASSIGN
:
3286 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3287 case RID_HAS_NOTHROW_COPY
:
3288 case RID_HAS_TRIVIAL_ASSIGN
:
3289 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3290 case RID_HAS_TRIVIAL_COPY
:
3291 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3292 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3293 case RID_IS_ABSTRACT
:
3294 case RID_IS_BASE_OF
:
3296 case RID_IS_CONVERTIBLE_TO
:
3300 case RID_IS_POLYMORPHIC
:
3302 return cp_parser_trait_expr (parser
, token
->keyword
);
3304 /* Objective-C++ expressions. */
3306 case RID_AT_PROTOCOL
:
3307 case RID_AT_SELECTOR
:
3308 return cp_parser_objc_expression (parser
);
3311 cp_parser_error (parser
, "expected primary-expression");
3312 return error_mark_node
;
3315 /* An id-expression can start with either an identifier, a
3316 `::' as the beginning of a qualified-id, or the "operator"
3320 case CPP_TEMPLATE_ID
:
3321 case CPP_NESTED_NAME_SPECIFIER
:
3325 const char *error_msg
;
3330 /* Parse the id-expression. */
3332 = cp_parser_id_expression (parser
,
3333 /*template_keyword_p=*/false,
3334 /*check_dependency_p=*/true,
3336 /*declarator_p=*/false,
3337 /*optional_p=*/false);
3338 if (id_expression
== error_mark_node
)
3339 return error_mark_node
;
3340 token
= cp_lexer_peek_token (parser
->lexer
);
3341 done
= (token
->type
!= CPP_OPEN_SQUARE
3342 && token
->type
!= CPP_OPEN_PAREN
3343 && token
->type
!= CPP_DOT
3344 && token
->type
!= CPP_DEREF
3345 && token
->type
!= CPP_PLUS_PLUS
3346 && token
->type
!= CPP_MINUS_MINUS
);
3347 /* If we have a template-id, then no further lookup is
3348 required. If the template-id was for a template-class, we
3349 will sometimes have a TYPE_DECL at this point. */
3350 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3351 || TREE_CODE (id_expression
) == TYPE_DECL
)
3352 decl
= id_expression
;
3353 /* Look up the name. */
3356 tree ambiguous_decls
;
3358 decl
= cp_parser_lookup_name (parser
, id_expression
,
3361 /*is_namespace=*/false,
3362 /*check_dependency=*/true,
3364 /* If the lookup was ambiguous, an error will already have
3366 if (ambiguous_decls
)
3367 return error_mark_node
;
3369 /* In Objective-C++, an instance variable (ivar) may be preferred
3370 to whatever cp_parser_lookup_name() found. */
3371 decl
= objc_lookup_ivar (decl
, id_expression
);
3373 /* If name lookup gives us a SCOPE_REF, then the
3374 qualifying scope was dependent. */
3375 if (TREE_CODE (decl
) == SCOPE_REF
)
3377 /* At this point, we do not know if DECL is a valid
3378 integral constant expression. We assume that it is
3379 in fact such an expression, so that code like:
3381 template <int N> struct A {
3385 is accepted. At template-instantiation time, we
3386 will check that B<N>::i is actually a constant. */
3389 /* Check to see if DECL is a local variable in a context
3390 where that is forbidden. */
3391 if (parser
->local_variables_forbidden_p
3392 && local_variable_p (decl
))
3394 /* It might be that we only found DECL because we are
3395 trying to be generous with pre-ISO scoping rules.
3396 For example, consider:
3400 for (int i = 0; i < 10; ++i) {}
3401 extern void f(int j = i);
3404 Here, name look up will originally find the out
3405 of scope `i'. We need to issue a warning message,
3406 but then use the global `i'. */
3407 decl
= check_for_out_of_scope_variable (decl
);
3408 if (local_variable_p (decl
))
3410 error ("local variable %qD may not appear in this context",
3412 return error_mark_node
;
3417 decl
= (finish_id_expression
3418 (id_expression
, decl
, parser
->scope
,
3420 parser
->integral_constant_expression_p
,
3421 parser
->allow_non_integral_constant_expression_p
,
3422 &parser
->non_integral_constant_expression_p
,
3423 template_p
, done
, address_p
,
3427 cp_parser_error (parser
, error_msg
);
3431 /* Anything else is an error. */
3433 /* ...unless we have an Objective-C++ message or string literal,
3435 if (c_dialect_objc ()
3436 && (token
->type
== CPP_OPEN_SQUARE
3437 || token
->type
== CPP_OBJC_STRING
))
3438 return cp_parser_objc_expression (parser
);
3440 cp_parser_error (parser
, "expected primary-expression");
3441 return error_mark_node
;
3445 /* Parse an id-expression.
3452 :: [opt] nested-name-specifier template [opt] unqualified-id
3454 :: operator-function-id
3457 Return a representation of the unqualified portion of the
3458 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3459 a `::' or nested-name-specifier.
3461 Often, if the id-expression was a qualified-id, the caller will
3462 want to make a SCOPE_REF to represent the qualified-id. This
3463 function does not do this in order to avoid wastefully creating
3464 SCOPE_REFs when they are not required.
3466 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3469 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3470 uninstantiated templates.
3472 If *TEMPLATE_P is non-NULL, it is set to true iff the
3473 `template' keyword is used to explicitly indicate that the entity
3474 named is a template.
3476 If DECLARATOR_P is true, the id-expression is appearing as part of
3477 a declarator, rather than as part of an expression. */
3480 cp_parser_id_expression (cp_parser
*parser
,
3481 bool template_keyword_p
,
3482 bool check_dependency_p
,
3487 bool global_scope_p
;
3488 bool nested_name_specifier_p
;
3490 /* Assume the `template' keyword was not used. */
3492 *template_p
= template_keyword_p
;
3494 /* Look for the optional `::' operator. */
3496 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3498 /* Look for the optional nested-name-specifier. */
3499 nested_name_specifier_p
3500 = (cp_parser_nested_name_specifier_opt (parser
,
3501 /*typename_keyword_p=*/false,
3506 /* If there is a nested-name-specifier, then we are looking at
3507 the first qualified-id production. */
3508 if (nested_name_specifier_p
)
3511 tree saved_object_scope
;
3512 tree saved_qualifying_scope
;
3513 tree unqualified_id
;
3516 /* See if the next token is the `template' keyword. */
3518 template_p
= &is_template
;
3519 *template_p
= cp_parser_optional_template_keyword (parser
);
3520 /* Name lookup we do during the processing of the
3521 unqualified-id might obliterate SCOPE. */
3522 saved_scope
= parser
->scope
;
3523 saved_object_scope
= parser
->object_scope
;
3524 saved_qualifying_scope
= parser
->qualifying_scope
;
3525 /* Process the final unqualified-id. */
3526 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3529 /*optional_p=*/false);
3530 /* Restore the SAVED_SCOPE for our caller. */
3531 parser
->scope
= saved_scope
;
3532 parser
->object_scope
= saved_object_scope
;
3533 parser
->qualifying_scope
= saved_qualifying_scope
;
3535 return unqualified_id
;
3537 /* Otherwise, if we are in global scope, then we are looking at one
3538 of the other qualified-id productions. */
3539 else if (global_scope_p
)
3544 /* Peek at the next token. */
3545 token
= cp_lexer_peek_token (parser
->lexer
);
3547 /* If it's an identifier, and the next token is not a "<", then
3548 we can avoid the template-id case. This is an optimization
3549 for this common case. */
3550 if (token
->type
== CPP_NAME
3551 && !cp_parser_nth_token_starts_template_argument_list_p
3553 return cp_parser_identifier (parser
);
3555 cp_parser_parse_tentatively (parser
);
3556 /* Try a template-id. */
3557 id
= cp_parser_template_id (parser
,
3558 /*template_keyword_p=*/false,
3559 /*check_dependency_p=*/true,
3561 /* If that worked, we're done. */
3562 if (cp_parser_parse_definitely (parser
))
3565 /* Peek at the next token. (Changes in the token buffer may
3566 have invalidated the pointer obtained above.) */
3567 token
= cp_lexer_peek_token (parser
->lexer
);
3569 switch (token
->type
)
3572 return cp_parser_identifier (parser
);
3575 if (token
->keyword
== RID_OPERATOR
)
3576 return cp_parser_operator_function_id (parser
);
3580 cp_parser_error (parser
, "expected id-expression");
3581 return error_mark_node
;
3585 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3586 /*check_dependency_p=*/true,
3591 /* Parse an unqualified-id.
3595 operator-function-id
3596 conversion-function-id
3600 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3601 keyword, in a construct like `A::template ...'.
3603 Returns a representation of unqualified-id. For the `identifier'
3604 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3605 production a BIT_NOT_EXPR is returned; the operand of the
3606 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3607 other productions, see the documentation accompanying the
3608 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3609 names are looked up in uninstantiated templates. If DECLARATOR_P
3610 is true, the unqualified-id is appearing as part of a declarator,
3611 rather than as part of an expression. */
3614 cp_parser_unqualified_id (cp_parser
* parser
,
3615 bool template_keyword_p
,
3616 bool check_dependency_p
,
3622 /* Peek at the next token. */
3623 token
= cp_lexer_peek_token (parser
->lexer
);
3625 switch (token
->type
)
3631 /* We don't know yet whether or not this will be a
3633 cp_parser_parse_tentatively (parser
);
3634 /* Try a template-id. */
3635 id
= cp_parser_template_id (parser
, template_keyword_p
,
3638 /* If it worked, we're done. */
3639 if (cp_parser_parse_definitely (parser
))
3641 /* Otherwise, it's an ordinary identifier. */
3642 return cp_parser_identifier (parser
);
3645 case CPP_TEMPLATE_ID
:
3646 return cp_parser_template_id (parser
, template_keyword_p
,
3653 tree qualifying_scope
;
3658 /* Consume the `~' token. */
3659 cp_lexer_consume_token (parser
->lexer
);
3660 /* Parse the class-name. The standard, as written, seems to
3663 template <typename T> struct S { ~S (); };
3664 template <typename T> S<T>::~S() {}
3666 is invalid, since `~' must be followed by a class-name, but
3667 `S<T>' is dependent, and so not known to be a class.
3668 That's not right; we need to look in uninstantiated
3669 templates. A further complication arises from:
3671 template <typename T> void f(T t) {
3675 Here, it is not possible to look up `T' in the scope of `T'
3676 itself. We must look in both the current scope, and the
3677 scope of the containing complete expression.
3679 Yet another issue is:
3688 The standard does not seem to say that the `S' in `~S'
3689 should refer to the type `S' and not the data member
3692 /* DR 244 says that we look up the name after the "~" in the
3693 same scope as we looked up the qualifying name. That idea
3694 isn't fully worked out; it's more complicated than that. */
3695 scope
= parser
->scope
;
3696 object_scope
= parser
->object_scope
;
3697 qualifying_scope
= parser
->qualifying_scope
;
3699 /* Check for invalid scopes. */
3700 if (scope
== error_mark_node
)
3702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3703 cp_lexer_consume_token (parser
->lexer
);
3704 return error_mark_node
;
3706 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3709 error ("scope %qT before %<~%> is not a class-name", scope
);
3710 cp_parser_simulate_error (parser
);
3711 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3712 cp_lexer_consume_token (parser
->lexer
);
3713 return error_mark_node
;
3715 gcc_assert (!scope
|| TYPE_P (scope
));
3717 /* If the name is of the form "X::~X" it's OK. */
3718 token
= cp_lexer_peek_token (parser
->lexer
);
3720 && token
->type
== CPP_NAME
3721 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3723 && constructor_name_p (token
->u
.value
, scope
))
3725 cp_lexer_consume_token (parser
->lexer
);
3726 return build_nt (BIT_NOT_EXPR
, scope
);
3729 /* If there was an explicit qualification (S::~T), first look
3730 in the scope given by the qualification (i.e., S). */
3732 type_decl
= NULL_TREE
;
3735 cp_parser_parse_tentatively (parser
);
3736 type_decl
= cp_parser_class_name (parser
,
3737 /*typename_keyword_p=*/false,
3738 /*template_keyword_p=*/false,
3740 /*check_dependency=*/false,
3741 /*class_head_p=*/false,
3743 if (cp_parser_parse_definitely (parser
))
3746 /* In "N::S::~S", look in "N" as well. */
3747 if (!done
&& scope
&& qualifying_scope
)
3749 cp_parser_parse_tentatively (parser
);
3750 parser
->scope
= qualifying_scope
;
3751 parser
->object_scope
= NULL_TREE
;
3752 parser
->qualifying_scope
= NULL_TREE
;
3754 = cp_parser_class_name (parser
,
3755 /*typename_keyword_p=*/false,
3756 /*template_keyword_p=*/false,
3758 /*check_dependency=*/false,
3759 /*class_head_p=*/false,
3761 if (cp_parser_parse_definitely (parser
))
3764 /* In "p->S::~T", look in the scope given by "*p" as well. */
3765 else if (!done
&& object_scope
)
3767 cp_parser_parse_tentatively (parser
);
3768 parser
->scope
= object_scope
;
3769 parser
->object_scope
= NULL_TREE
;
3770 parser
->qualifying_scope
= NULL_TREE
;
3772 = cp_parser_class_name (parser
,
3773 /*typename_keyword_p=*/false,
3774 /*template_keyword_p=*/false,
3776 /*check_dependency=*/false,
3777 /*class_head_p=*/false,
3779 if (cp_parser_parse_definitely (parser
))
3782 /* Look in the surrounding context. */
3785 parser
->scope
= NULL_TREE
;
3786 parser
->object_scope
= NULL_TREE
;
3787 parser
->qualifying_scope
= NULL_TREE
;
3789 = cp_parser_class_name (parser
,
3790 /*typename_keyword_p=*/false,
3791 /*template_keyword_p=*/false,
3793 /*check_dependency=*/false,
3794 /*class_head_p=*/false,
3797 /* If an error occurred, assume that the name of the
3798 destructor is the same as the name of the qualifying
3799 class. That allows us to keep parsing after running
3800 into ill-formed destructor names. */
3801 if (type_decl
== error_mark_node
&& scope
)
3802 return build_nt (BIT_NOT_EXPR
, scope
);
3803 else if (type_decl
== error_mark_node
)
3804 return error_mark_node
;
3806 /* Check that destructor name and scope match. */
3807 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3809 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3810 error ("declaration of %<~%T%> as member of %qT",
3812 cp_parser_simulate_error (parser
);
3813 return error_mark_node
;
3818 A typedef-name that names a class shall not be used as the
3819 identifier in the declarator for a destructor declaration. */
3821 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3822 && !DECL_SELF_REFERENCE_P (type_decl
)
3823 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3824 error ("typedef-name %qD used as destructor declarator",
3827 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3831 if (token
->keyword
== RID_OPERATOR
)
3835 /* This could be a template-id, so we try that first. */
3836 cp_parser_parse_tentatively (parser
);
3837 /* Try a template-id. */
3838 id
= cp_parser_template_id (parser
, template_keyword_p
,
3839 /*check_dependency_p=*/true,
3841 /* If that worked, we're done. */
3842 if (cp_parser_parse_definitely (parser
))
3844 /* We still don't know whether we're looking at an
3845 operator-function-id or a conversion-function-id. */
3846 cp_parser_parse_tentatively (parser
);
3847 /* Try an operator-function-id. */
3848 id
= cp_parser_operator_function_id (parser
);
3849 /* If that didn't work, try a conversion-function-id. */
3850 if (!cp_parser_parse_definitely (parser
))
3851 id
= cp_parser_conversion_function_id (parser
);
3860 cp_parser_error (parser
, "expected unqualified-id");
3861 return error_mark_node
;
3865 /* Parse an (optional) nested-name-specifier.
3867 nested-name-specifier:
3868 class-or-namespace-name :: nested-name-specifier [opt]
3869 class-or-namespace-name :: template nested-name-specifier [opt]
3871 PARSER->SCOPE should be set appropriately before this function is
3872 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3873 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3876 Sets PARSER->SCOPE to the class (TYPE) or namespace
3877 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3878 it unchanged if there is no nested-name-specifier. Returns the new
3879 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3881 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3882 part of a declaration and/or decl-specifier. */
3885 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3886 bool typename_keyword_p
,
3887 bool check_dependency_p
,
3889 bool is_declaration
)
3891 bool success
= false;
3892 cp_token_position start
= 0;
3895 /* Remember where the nested-name-specifier starts. */
3896 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3898 start
= cp_lexer_token_position (parser
->lexer
, false);
3899 push_deferring_access_checks (dk_deferred
);
3906 tree saved_qualifying_scope
;
3907 bool template_keyword_p
;
3909 /* Spot cases that cannot be the beginning of a
3910 nested-name-specifier. */
3911 token
= cp_lexer_peek_token (parser
->lexer
);
3913 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3914 the already parsed nested-name-specifier. */
3915 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3917 /* Grab the nested-name-specifier and continue the loop. */
3918 cp_parser_pre_parsed_nested_name_specifier (parser
);
3919 /* If we originally encountered this nested-name-specifier
3920 with IS_DECLARATION set to false, we will not have
3921 resolved TYPENAME_TYPEs, so we must do so here. */
3923 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3925 new_scope
= resolve_typename_type (parser
->scope
,
3926 /*only_current_p=*/false);
3927 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
3928 parser
->scope
= new_scope
;
3934 /* Spot cases that cannot be the beginning of a
3935 nested-name-specifier. On the second and subsequent times
3936 through the loop, we look for the `template' keyword. */
3937 if (success
&& token
->keyword
== RID_TEMPLATE
)
3939 /* A template-id can start a nested-name-specifier. */
3940 else if (token
->type
== CPP_TEMPLATE_ID
)
3944 /* If the next token is not an identifier, then it is
3945 definitely not a class-or-namespace-name. */
3946 if (token
->type
!= CPP_NAME
)
3948 /* If the following token is neither a `<' (to begin a
3949 template-id), nor a `::', then we are not looking at a
3950 nested-name-specifier. */
3951 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3952 if (token
->type
!= CPP_SCOPE
3953 && !cp_parser_nth_token_starts_template_argument_list_p
3958 /* The nested-name-specifier is optional, so we parse
3960 cp_parser_parse_tentatively (parser
);
3962 /* Look for the optional `template' keyword, if this isn't the
3963 first time through the loop. */
3965 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3967 template_keyword_p
= false;
3969 /* Save the old scope since the name lookup we are about to do
3970 might destroy it. */
3971 old_scope
= parser
->scope
;
3972 saved_qualifying_scope
= parser
->qualifying_scope
;
3973 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3974 look up names in "X<T>::I" in order to determine that "Y" is
3975 a template. So, if we have a typename at this point, we make
3976 an effort to look through it. */
3978 && !typename_keyword_p
3980 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3981 parser
->scope
= resolve_typename_type (parser
->scope
,
3982 /*only_current_p=*/false);
3983 /* Parse the qualifying entity. */
3985 = cp_parser_class_or_namespace_name (parser
,
3991 /* Look for the `::' token. */
3992 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3994 /* If we found what we wanted, we keep going; otherwise, we're
3996 if (!cp_parser_parse_definitely (parser
))
3998 bool error_p
= false;
4000 /* Restore the OLD_SCOPE since it was valid before the
4001 failed attempt at finding the last
4002 class-or-namespace-name. */
4003 parser
->scope
= old_scope
;
4004 parser
->qualifying_scope
= saved_qualifying_scope
;
4005 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4007 /* If the next token is an identifier, and the one after
4008 that is a `::', then any valid interpretation would have
4009 found a class-or-namespace-name. */
4010 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4011 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4013 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4016 token
= cp_lexer_consume_token (parser
->lexer
);
4019 if (!token
->ambiguous_p
)
4022 tree ambiguous_decls
;
4024 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4026 /*is_template=*/false,
4027 /*is_namespace=*/false,
4028 /*check_dependency=*/true,
4030 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4031 error ("%qD used without template parameters", decl
);
4032 else if (ambiguous_decls
)
4034 error ("reference to %qD is ambiguous",
4036 print_candidates (ambiguous_decls
);
4037 decl
= error_mark_node
;
4040 cp_parser_name_lookup_error
4041 (parser
, token
->u
.value
, decl
,
4042 "is not a class or namespace");
4044 parser
->scope
= error_mark_node
;
4046 /* Treat this as a successful nested-name-specifier
4051 If the name found is not a class-name (clause
4052 _class_) or namespace-name (_namespace.def_), the
4053 program is ill-formed. */
4056 cp_lexer_consume_token (parser
->lexer
);
4060 /* We've found one valid nested-name-specifier. */
4062 /* Name lookup always gives us a DECL. */
4063 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4064 new_scope
= TREE_TYPE (new_scope
);
4065 /* Uses of "template" must be followed by actual templates. */
4066 if (template_keyword_p
4067 && !(CLASS_TYPE_P (new_scope
)
4068 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4069 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4070 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4071 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4072 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4073 == TEMPLATE_ID_EXPR
)))
4074 pedwarn (TYPE_P (new_scope
)
4075 ? "%qT is not a template"
4076 : "%qD is not a template",
4078 /* If it is a class scope, try to complete it; we are about to
4079 be looking up names inside the class. */
4080 if (TYPE_P (new_scope
)
4081 /* Since checking types for dependency can be expensive,
4082 avoid doing it if the type is already complete. */
4083 && !COMPLETE_TYPE_P (new_scope
)
4084 /* Do not try to complete dependent types. */
4085 && !dependent_type_p (new_scope
))
4087 new_scope
= complete_type (new_scope
);
4088 /* If it is a typedef to current class, use the current
4089 class instead, as the typedef won't have any names inside
4091 if (!COMPLETE_TYPE_P (new_scope
)
4092 && currently_open_class (new_scope
))
4093 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4095 /* Make sure we look in the right scope the next time through
4097 parser
->scope
= new_scope
;
4100 /* If parsing tentatively, replace the sequence of tokens that makes
4101 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4102 token. That way, should we re-parse the token stream, we will
4103 not have to repeat the effort required to do the parse, nor will
4104 we issue duplicate error messages. */
4105 if (success
&& start
)
4109 token
= cp_lexer_token_at (parser
->lexer
, start
);
4110 /* Reset the contents of the START token. */
4111 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4112 /* Retrieve any deferred checks. Do not pop this access checks yet
4113 so the memory will not be reclaimed during token replacing below. */
4114 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
4115 token
->u
.tree_check_value
->value
= parser
->scope
;
4116 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4117 token
->u
.tree_check_value
->qualifying_scope
=
4118 parser
->qualifying_scope
;
4119 token
->keyword
= RID_MAX
;
4121 /* Purge all subsequent tokens. */
4122 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4126 pop_to_parent_deferring_access_checks ();
4128 return success
? parser
->scope
: NULL_TREE
;
4131 /* Parse a nested-name-specifier. See
4132 cp_parser_nested_name_specifier_opt for details. This function
4133 behaves identically, except that it will an issue an error if no
4134 nested-name-specifier is present. */
4137 cp_parser_nested_name_specifier (cp_parser
*parser
,
4138 bool typename_keyword_p
,
4139 bool check_dependency_p
,
4141 bool is_declaration
)
4145 /* Look for the nested-name-specifier. */
4146 scope
= cp_parser_nested_name_specifier_opt (parser
,
4151 /* If it was not present, issue an error message. */
4154 cp_parser_error (parser
, "expected nested-name-specifier");
4155 parser
->scope
= NULL_TREE
;
4161 /* Parse a class-or-namespace-name.
4163 class-or-namespace-name:
4167 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4168 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4169 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4170 TYPE_P is TRUE iff the next name should be taken as a class-name,
4171 even the same name is declared to be another entity in the same
4174 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4175 specified by the class-or-namespace-name. If neither is found the
4176 ERROR_MARK_NODE is returned. */
4179 cp_parser_class_or_namespace_name (cp_parser
*parser
,
4180 bool typename_keyword_p
,
4181 bool template_keyword_p
,
4182 bool check_dependency_p
,
4184 bool is_declaration
)
4187 tree saved_qualifying_scope
;
4188 tree saved_object_scope
;
4192 /* Before we try to parse the class-name, we must save away the
4193 current PARSER->SCOPE since cp_parser_class_name will destroy
4195 saved_scope
= parser
->scope
;
4196 saved_qualifying_scope
= parser
->qualifying_scope
;
4197 saved_object_scope
= parser
->object_scope
;
4198 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4199 there is no need to look for a namespace-name. */
4200 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
4202 cp_parser_parse_tentatively (parser
);
4203 scope
= cp_parser_class_name (parser
,
4206 type_p
? class_type
: none_type
,
4208 /*class_head_p=*/false,
4210 /* If that didn't work, try for a namespace-name. */
4211 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
4213 /* Restore the saved scope. */
4214 parser
->scope
= saved_scope
;
4215 parser
->qualifying_scope
= saved_qualifying_scope
;
4216 parser
->object_scope
= saved_object_scope
;
4217 /* If we are not looking at an identifier followed by the scope
4218 resolution operator, then this is not part of a
4219 nested-name-specifier. (Note that this function is only used
4220 to parse the components of a nested-name-specifier.) */
4221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4222 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4223 return error_mark_node
;
4224 scope
= cp_parser_namespace_name (parser
);
4230 /* Parse a postfix-expression.
4234 postfix-expression [ expression ]
4235 postfix-expression ( expression-list [opt] )
4236 simple-type-specifier ( expression-list [opt] )
4237 typename :: [opt] nested-name-specifier identifier
4238 ( expression-list [opt] )
4239 typename :: [opt] nested-name-specifier template [opt] template-id
4240 ( expression-list [opt] )
4241 postfix-expression . template [opt] id-expression
4242 postfix-expression -> template [opt] id-expression
4243 postfix-expression . pseudo-destructor-name
4244 postfix-expression -> pseudo-destructor-name
4245 postfix-expression ++
4246 postfix-expression --
4247 dynamic_cast < type-id > ( expression )
4248 static_cast < type-id > ( expression )
4249 reinterpret_cast < type-id > ( expression )
4250 const_cast < type-id > ( expression )
4251 typeid ( expression )
4257 ( type-id ) { initializer-list , [opt] }
4259 This extension is a GNU version of the C99 compound-literal
4260 construct. (The C99 grammar uses `type-name' instead of `type-id',
4261 but they are essentially the same concept.)
4263 If ADDRESS_P is true, the postfix expression is the operand of the
4264 `&' operator. CAST_P is true if this expression is the target of a
4267 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4268 class member access expressions [expr.ref].
4270 Returns a representation of the expression. */
4273 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4274 bool member_access_only_p
)
4278 cp_id_kind idk
= CP_ID_KIND_NONE
;
4279 tree postfix_expression
= NULL_TREE
;
4280 bool is_member_access
= false;
4282 /* Peek at the next token. */
4283 token
= cp_lexer_peek_token (parser
->lexer
);
4284 /* Some of the productions are determined by keywords. */
4285 keyword
= token
->keyword
;
4295 const char *saved_message
;
4297 /* All of these can be handled in the same way from the point
4298 of view of parsing. Begin by consuming the token
4299 identifying the cast. */
4300 cp_lexer_consume_token (parser
->lexer
);
4302 /* New types cannot be defined in the cast. */
4303 saved_message
= parser
->type_definition_forbidden_message
;
4304 parser
->type_definition_forbidden_message
4305 = "types may not be defined in casts";
4307 /* Look for the opening `<'. */
4308 cp_parser_require (parser
, CPP_LESS
, "`<'");
4309 /* Parse the type to which we are casting. */
4310 type
= cp_parser_type_id (parser
);
4311 /* Look for the closing `>'. */
4312 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4313 /* Restore the old message. */
4314 parser
->type_definition_forbidden_message
= saved_message
;
4316 /* And the expression which is being cast. */
4317 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4318 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4319 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4321 /* Only type conversions to integral or enumeration types
4322 can be used in constant-expressions. */
4323 if (!cast_valid_in_integral_constant_expression_p (type
)
4324 && (cp_parser_non_integral_constant_expression
4326 "a cast to a type other than an integral or "
4327 "enumeration type")))
4328 return error_mark_node
;
4334 = build_dynamic_cast (type
, expression
);
4338 = build_static_cast (type
, expression
);
4342 = build_reinterpret_cast (type
, expression
);
4346 = build_const_cast (type
, expression
);
4357 const char *saved_message
;
4358 bool saved_in_type_id_in_expr_p
;
4360 /* Consume the `typeid' token. */
4361 cp_lexer_consume_token (parser
->lexer
);
4362 /* Look for the `(' token. */
4363 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4364 /* Types cannot be defined in a `typeid' expression. */
4365 saved_message
= parser
->type_definition_forbidden_message
;
4366 parser
->type_definition_forbidden_message
4367 = "types may not be defined in a `typeid\' expression";
4368 /* We can't be sure yet whether we're looking at a type-id or an
4370 cp_parser_parse_tentatively (parser
);
4371 /* Try a type-id first. */
4372 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4373 parser
->in_type_id_in_expr_p
= true;
4374 type
= cp_parser_type_id (parser
);
4375 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4376 /* Look for the `)' token. Otherwise, we can't be sure that
4377 we're not looking at an expression: consider `typeid (int
4378 (3))', for example. */
4379 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4380 /* If all went well, simply lookup the type-id. */
4381 if (cp_parser_parse_definitely (parser
))
4382 postfix_expression
= get_typeid (type
);
4383 /* Otherwise, fall back to the expression variant. */
4388 /* Look for an expression. */
4389 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4390 /* Compute its typeid. */
4391 postfix_expression
= build_typeid (expression
);
4392 /* Look for the `)' token. */
4393 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4395 /* Restore the saved message. */
4396 parser
->type_definition_forbidden_message
= saved_message
;
4397 /* `typeid' may not appear in an integral constant expression. */
4398 if (cp_parser_non_integral_constant_expression(parser
,
4399 "`typeid' operator"))
4400 return error_mark_node
;
4407 /* The syntax permitted here is the same permitted for an
4408 elaborated-type-specifier. */
4409 type
= cp_parser_elaborated_type_specifier (parser
,
4410 /*is_friend=*/false,
4411 /*is_declaration=*/false);
4412 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4420 /* If the next thing is a simple-type-specifier, we may be
4421 looking at a functional cast. We could also be looking at
4422 an id-expression. So, we try the functional cast, and if
4423 that doesn't work we fall back to the primary-expression. */
4424 cp_parser_parse_tentatively (parser
);
4425 /* Look for the simple-type-specifier. */
4426 type
= cp_parser_simple_type_specifier (parser
,
4427 /*decl_specs=*/NULL
,
4428 CP_PARSER_FLAGS_NONE
);
4429 /* Parse the cast itself. */
4430 if (!cp_parser_error_occurred (parser
))
4432 = cp_parser_functional_cast (parser
, type
);
4433 /* If that worked, we're done. */
4434 if (cp_parser_parse_definitely (parser
))
4437 /* If the functional-cast didn't work out, try a
4438 compound-literal. */
4439 if (cp_parser_allow_gnu_extensions_p (parser
)
4440 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4442 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4443 bool saved_in_type_id_in_expr_p
;
4445 cp_parser_parse_tentatively (parser
);
4446 /* Consume the `('. */
4447 cp_lexer_consume_token (parser
->lexer
);
4448 /* Parse the type. */
4449 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4450 parser
->in_type_id_in_expr_p
= true;
4451 type
= cp_parser_type_id (parser
);
4452 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4453 /* Look for the `)'. */
4454 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4455 /* Look for the `{'. */
4456 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4457 /* If things aren't going well, there's no need to
4459 if (!cp_parser_error_occurred (parser
))
4461 bool non_constant_p
;
4462 /* Parse the initializer-list. */
4464 = cp_parser_initializer_list (parser
, &non_constant_p
);
4465 /* Allow a trailing `,'. */
4466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4467 cp_lexer_consume_token (parser
->lexer
);
4468 /* Look for the final `}'. */
4469 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4471 /* If that worked, we're definitely looking at a
4472 compound-literal expression. */
4473 if (cp_parser_parse_definitely (parser
))
4475 /* Warn the user that a compound literal is not
4476 allowed in standard C++. */
4478 pedwarn ("ISO C++ forbids compound-literals");
4479 /* For simplicity, we disallow compound literals in
4480 constant-expressions. We could
4481 allow compound literals of integer type, whose
4482 initializer was a constant, in constant
4483 expressions. Permitting that usage, as a further
4484 extension, would not change the meaning of any
4485 currently accepted programs. (Of course, as
4486 compound literals are not part of ISO C++, the
4487 standard has nothing to say.) */
4488 if (cp_parser_non_integral_constant_expression
4489 (parser
, "non-constant compound literals"))
4491 postfix_expression
= error_mark_node
;
4494 /* Form the representation of the compound-literal. */
4496 = finish_compound_literal (type
, initializer_list
);
4501 /* It must be a primary-expression. */
4503 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4504 /*template_arg_p=*/false,
4510 /* Keep looping until the postfix-expression is complete. */
4513 if (idk
== CP_ID_KIND_UNQUALIFIED
4514 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4515 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4516 /* It is not a Koenig lookup function call. */
4518 = unqualified_name_lookup_error (postfix_expression
);
4520 /* Peek at the next token. */
4521 token
= cp_lexer_peek_token (parser
->lexer
);
4523 switch (token
->type
)
4525 case CPP_OPEN_SQUARE
:
4527 = cp_parser_postfix_open_square_expression (parser
,
4530 idk
= CP_ID_KIND_NONE
;
4531 is_member_access
= false;
4534 case CPP_OPEN_PAREN
:
4535 /* postfix-expression ( expression-list [opt] ) */
4538 bool is_builtin_constant_p
;
4539 bool saved_integral_constant_expression_p
= false;
4540 bool saved_non_integral_constant_expression_p
= false;
4543 is_member_access
= false;
4545 is_builtin_constant_p
4546 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4547 if (is_builtin_constant_p
)
4549 /* The whole point of __builtin_constant_p is to allow
4550 non-constant expressions to appear as arguments. */
4551 saved_integral_constant_expression_p
4552 = parser
->integral_constant_expression_p
;
4553 saved_non_integral_constant_expression_p
4554 = parser
->non_integral_constant_expression_p
;
4555 parser
->integral_constant_expression_p
= false;
4557 args
= (cp_parser_parenthesized_expression_list
4558 (parser
, /*is_attribute_list=*/false,
4559 /*cast_p=*/false, /*allow_expansion_p=*/true,
4560 /*non_constant_p=*/NULL
));
4561 if (is_builtin_constant_p
)
4563 parser
->integral_constant_expression_p
4564 = saved_integral_constant_expression_p
;
4565 parser
->non_integral_constant_expression_p
4566 = saved_non_integral_constant_expression_p
;
4569 if (args
== error_mark_node
)
4571 postfix_expression
= error_mark_node
;
4575 /* Function calls are not permitted in
4576 constant-expressions. */
4577 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4578 && cp_parser_non_integral_constant_expression (parser
,
4581 postfix_expression
= error_mark_node
;
4586 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4588 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4594 = perform_koenig_lookup (postfix_expression
, args
);
4598 = unqualified_fn_lookup_error (postfix_expression
);
4600 /* We do not perform argument-dependent lookup if
4601 normal lookup finds a non-function, in accordance
4602 with the expected resolution of DR 218. */
4603 else if (args
&& is_overloaded_fn (postfix_expression
))
4605 tree fn
= get_first_fn (postfix_expression
);
4607 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4608 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4610 /* Only do argument dependent lookup if regular
4611 lookup does not find a set of member functions.
4612 [basic.lookup.koenig]/2a */
4613 if (!DECL_FUNCTION_MEMBER_P (fn
))
4617 = perform_koenig_lookup (postfix_expression
, args
);
4622 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4624 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4625 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4627 if (processing_template_decl
4628 && (type_dependent_expression_p (instance
)
4629 || (!BASELINK_P (fn
)
4630 && TREE_CODE (fn
) != FIELD_DECL
)
4631 || type_dependent_expression_p (fn
)
4632 || any_type_dependent_arguments_p (args
)))
4635 = build_nt_call_list (postfix_expression
, args
);
4639 if (BASELINK_P (fn
))
4641 = (build_new_method_call
4642 (instance
, fn
, args
, NULL_TREE
,
4643 (idk
== CP_ID_KIND_QUALIFIED
4644 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4648 = finish_call_expr (postfix_expression
, args
,
4649 /*disallow_virtual=*/false,
4650 /*koenig_p=*/false);
4652 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4653 || TREE_CODE (postfix_expression
) == MEMBER_REF
4654 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4655 postfix_expression
= (build_offset_ref_call_from_tree
4656 (postfix_expression
, args
));
4657 else if (idk
== CP_ID_KIND_QUALIFIED
)
4658 /* A call to a static class member, or a namespace-scope
4661 = finish_call_expr (postfix_expression
, args
,
4662 /*disallow_virtual=*/true,
4665 /* All other function calls. */
4667 = finish_call_expr (postfix_expression
, args
,
4668 /*disallow_virtual=*/false,
4671 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4672 idk
= CP_ID_KIND_NONE
;
4678 /* postfix-expression . template [opt] id-expression
4679 postfix-expression . pseudo-destructor-name
4680 postfix-expression -> template [opt] id-expression
4681 postfix-expression -> pseudo-destructor-name */
4683 /* Consume the `.' or `->' operator. */
4684 cp_lexer_consume_token (parser
->lexer
);
4687 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4691 is_member_access
= true;
4695 /* postfix-expression ++ */
4696 /* Consume the `++' token. */
4697 cp_lexer_consume_token (parser
->lexer
);
4698 /* Generate a representation for the complete expression. */
4700 = finish_increment_expr (postfix_expression
,
4701 POSTINCREMENT_EXPR
);
4702 /* Increments may not appear in constant-expressions. */
4703 if (cp_parser_non_integral_constant_expression (parser
,
4705 postfix_expression
= error_mark_node
;
4706 idk
= CP_ID_KIND_NONE
;
4707 is_member_access
= false;
4710 case CPP_MINUS_MINUS
:
4711 /* postfix-expression -- */
4712 /* Consume the `--' token. */
4713 cp_lexer_consume_token (parser
->lexer
);
4714 /* Generate a representation for the complete expression. */
4716 = finish_increment_expr (postfix_expression
,
4717 POSTDECREMENT_EXPR
);
4718 /* Decrements may not appear in constant-expressions. */
4719 if (cp_parser_non_integral_constant_expression (parser
,
4721 postfix_expression
= error_mark_node
;
4722 idk
= CP_ID_KIND_NONE
;
4723 is_member_access
= false;
4727 if (member_access_only_p
)
4728 return is_member_access
? postfix_expression
: error_mark_node
;
4730 return postfix_expression
;
4734 /* We should never get here. */
4736 return error_mark_node
;
4739 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4740 by cp_parser_builtin_offsetof. We're looking for
4742 postfix-expression [ expression ]
4744 FOR_OFFSETOF is set if we're being called in that context, which
4745 changes how we deal with integer constant expressions. */
4748 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4749 tree postfix_expression
,
4754 /* Consume the `[' token. */
4755 cp_lexer_consume_token (parser
->lexer
);
4757 /* Parse the index expression. */
4758 /* ??? For offsetof, there is a question of what to allow here. If
4759 offsetof is not being used in an integral constant expression context,
4760 then we *could* get the right answer by computing the value at runtime.
4761 If we are in an integral constant expression context, then we might
4762 could accept any constant expression; hard to say without analysis.
4763 Rather than open the barn door too wide right away, allow only integer
4764 constant expressions here. */
4766 index
= cp_parser_constant_expression (parser
, false, NULL
);
4768 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4770 /* Look for the closing `]'. */
4771 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4773 /* Build the ARRAY_REF. */
4774 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4776 /* When not doing offsetof, array references are not permitted in
4777 constant-expressions. */
4779 && (cp_parser_non_integral_constant_expression
4780 (parser
, "an array reference")))
4781 postfix_expression
= error_mark_node
;
4783 return postfix_expression
;
4786 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4787 by cp_parser_builtin_offsetof. We're looking for
4789 postfix-expression . template [opt] id-expression
4790 postfix-expression . pseudo-destructor-name
4791 postfix-expression -> template [opt] id-expression
4792 postfix-expression -> pseudo-destructor-name
4794 FOR_OFFSETOF is set if we're being called in that context. That sorta
4795 limits what of the above we'll actually accept, but nevermind.
4796 TOKEN_TYPE is the "." or "->" token, which will already have been
4797 removed from the stream. */
4800 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4801 enum cpp_ttype token_type
,
4802 tree postfix_expression
,
4803 bool for_offsetof
, cp_id_kind
*idk
)
4807 bool pseudo_destructor_p
;
4808 tree scope
= NULL_TREE
;
4810 /* If this is a `->' operator, dereference the pointer. */
4811 if (token_type
== CPP_DEREF
)
4812 postfix_expression
= build_x_arrow (postfix_expression
);
4813 /* Check to see whether or not the expression is type-dependent. */
4814 dependent_p
= type_dependent_expression_p (postfix_expression
);
4815 /* The identifier following the `->' or `.' is not qualified. */
4816 parser
->scope
= NULL_TREE
;
4817 parser
->qualifying_scope
= NULL_TREE
;
4818 parser
->object_scope
= NULL_TREE
;
4819 *idk
= CP_ID_KIND_NONE
;
4820 /* Enter the scope corresponding to the type of the object
4821 given by the POSTFIX_EXPRESSION. */
4822 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4824 scope
= TREE_TYPE (postfix_expression
);
4825 /* According to the standard, no expression should ever have
4826 reference type. Unfortunately, we do not currently match
4827 the standard in this respect in that our internal representation
4828 of an expression may have reference type even when the standard
4829 says it does not. Therefore, we have to manually obtain the
4830 underlying type here. */
4831 scope
= non_reference (scope
);
4832 /* The type of the POSTFIX_EXPRESSION must be complete. */
4833 if (scope
== unknown_type_node
)
4835 error ("%qE does not have class type", postfix_expression
);
4839 scope
= complete_type_or_else (scope
, NULL_TREE
);
4840 /* Let the name lookup machinery know that we are processing a
4841 class member access expression. */
4842 parser
->context
->object_type
= scope
;
4843 /* If something went wrong, we want to be able to discern that case,
4844 as opposed to the case where there was no SCOPE due to the type
4845 of expression being dependent. */
4847 scope
= error_mark_node
;
4848 /* If the SCOPE was erroneous, make the various semantic analysis
4849 functions exit quickly -- and without issuing additional error
4851 if (scope
== error_mark_node
)
4852 postfix_expression
= error_mark_node
;
4855 /* Assume this expression is not a pseudo-destructor access. */
4856 pseudo_destructor_p
= false;
4858 /* If the SCOPE is a scalar type, then, if this is a valid program,
4859 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4860 is type dependent, it can be pseudo-destructor-name or something else.
4861 Try to parse it as pseudo-destructor-name first. */
4862 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
4867 cp_parser_parse_tentatively (parser
);
4868 /* Parse the pseudo-destructor-name. */
4870 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4872 && (cp_parser_error_occurred (parser
)
4873 || TREE_CODE (type
) != TYPE_DECL
4874 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
4875 cp_parser_abort_tentative_parse (parser
);
4876 else if (cp_parser_parse_definitely (parser
))
4878 pseudo_destructor_p
= true;
4880 = finish_pseudo_destructor_expr (postfix_expression
,
4881 s
, TREE_TYPE (type
));
4885 if (!pseudo_destructor_p
)
4887 /* If the SCOPE is not a scalar type, we are looking at an
4888 ordinary class member access expression, rather than a
4889 pseudo-destructor-name. */
4891 /* Parse the id-expression. */
4892 name
= (cp_parser_id_expression
4894 cp_parser_optional_template_keyword (parser
),
4895 /*check_dependency_p=*/true,
4897 /*declarator_p=*/false,
4898 /*optional_p=*/false));
4899 /* In general, build a SCOPE_REF if the member name is qualified.
4900 However, if the name was not dependent and has already been
4901 resolved; there is no need to build the SCOPE_REF. For example;
4903 struct X { void f(); };
4904 template <typename T> void f(T* t) { t->X::f(); }
4906 Even though "t" is dependent, "X::f" is not and has been resolved
4907 to a BASELINK; there is no need to include scope information. */
4909 /* But we do need to remember that there was an explicit scope for
4910 virtual function calls. */
4912 *idk
= CP_ID_KIND_QUALIFIED
;
4914 /* If the name is a template-id that names a type, we will get a
4915 TYPE_DECL here. That is invalid code. */
4916 if (TREE_CODE (name
) == TYPE_DECL
)
4918 error ("invalid use of %qD", name
);
4919 postfix_expression
= error_mark_node
;
4923 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4925 name
= build_qualified_name (/*type=*/NULL_TREE
,
4929 parser
->scope
= NULL_TREE
;
4930 parser
->qualifying_scope
= NULL_TREE
;
4931 parser
->object_scope
= NULL_TREE
;
4933 if (scope
&& name
&& BASELINK_P (name
))
4934 adjust_result_of_qualified_name_lookup
4935 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4937 = finish_class_member_access_expr (postfix_expression
, name
,
4942 /* We no longer need to look up names in the scope of the object on
4943 the left-hand side of the `.' or `->' operator. */
4944 parser
->context
->object_type
= NULL_TREE
;
4946 /* Outside of offsetof, these operators may not appear in
4947 constant-expressions. */
4949 && (cp_parser_non_integral_constant_expression
4950 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4951 postfix_expression
= error_mark_node
;
4953 return postfix_expression
;
4956 /* Parse a parenthesized expression-list.
4959 assignment-expression
4960 expression-list, assignment-expression
4965 identifier, expression-list
4967 CAST_P is true if this expression is the target of a cast.
4969 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4972 Returns a TREE_LIST. The TREE_VALUE of each node is a
4973 representation of an assignment-expression. Note that a TREE_LIST
4974 is returned even if there is only a single expression in the list.
4975 error_mark_node is returned if the ( and or ) are
4976 missing. NULL_TREE is returned on no expressions. The parentheses
4977 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4978 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4979 indicates whether or not all of the expressions in the list were
4983 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4984 bool is_attribute_list
,
4986 bool allow_expansion_p
,
4987 bool *non_constant_p
)
4989 tree expression_list
= NULL_TREE
;
4990 bool fold_expr_p
= is_attribute_list
;
4991 tree identifier
= NULL_TREE
;
4992 bool saved_greater_than_is_operator_p
;
4994 /* Assume all the expressions will be constant. */
4996 *non_constant_p
= false;
4998 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4999 return error_mark_node
;
5001 /* Within a parenthesized expression, a `>' token is always
5002 the greater-than operator. */
5003 saved_greater_than_is_operator_p
5004 = parser
->greater_than_is_operator_p
;
5005 parser
->greater_than_is_operator_p
= true;
5007 /* Consume expressions until there are no more. */
5008 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5013 /* At the beginning of attribute lists, check to see if the
5014 next token is an identifier. */
5015 if (is_attribute_list
5016 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5020 /* Consume the identifier. */
5021 token
= cp_lexer_consume_token (parser
->lexer
);
5022 /* Save the identifier. */
5023 identifier
= token
->u
.value
;
5027 /* Parse the next assignment-expression. */
5030 bool expr_non_constant_p
;
5031 expr
= (cp_parser_constant_expression
5032 (parser
, /*allow_non_constant_p=*/true,
5033 &expr_non_constant_p
));
5034 if (expr_non_constant_p
)
5035 *non_constant_p
= true;
5038 expr
= cp_parser_assignment_expression (parser
, cast_p
);
5041 expr
= fold_non_dependent_expr (expr
);
5043 /* If we have an ellipsis, then this is an expression
5045 if (allow_expansion_p
5046 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5048 /* Consume the `...'. */
5049 cp_lexer_consume_token (parser
->lexer
);
5051 /* Build the argument pack. */
5052 expr
= make_pack_expansion (expr
);
5055 /* Add it to the list. We add error_mark_node
5056 expressions to the list, so that we can still tell if
5057 the correct form for a parenthesized expression-list
5058 is found. That gives better errors. */
5059 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
5061 if (expr
== error_mark_node
)
5065 /* After the first item, attribute lists look the same as
5066 expression lists. */
5067 is_attribute_list
= false;
5070 /* If the next token isn't a `,', then we are done. */
5071 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5074 /* Otherwise, consume the `,' and keep going. */
5075 cp_lexer_consume_token (parser
->lexer
);
5078 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5083 /* We try and resync to an unnested comma, as that will give the
5084 user better diagnostics. */
5085 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5086 /*recovering=*/true,
5088 /*consume_paren=*/true);
5093 parser
->greater_than_is_operator_p
5094 = saved_greater_than_is_operator_p
;
5095 return error_mark_node
;
5099 parser
->greater_than_is_operator_p
5100 = saved_greater_than_is_operator_p
;
5102 /* We built up the list in reverse order so we must reverse it now. */
5103 expression_list
= nreverse (expression_list
);
5105 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
5107 return expression_list
;
5110 /* Parse a pseudo-destructor-name.
5112 pseudo-destructor-name:
5113 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5114 :: [opt] nested-name-specifier template template-id :: ~ type-name
5115 :: [opt] nested-name-specifier [opt] ~ type-name
5117 If either of the first two productions is used, sets *SCOPE to the
5118 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5119 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5120 or ERROR_MARK_NODE if the parse fails. */
5123 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5127 bool nested_name_specifier_p
;
5129 /* Assume that things will not work out. */
5130 *type
= error_mark_node
;
5132 /* Look for the optional `::' operator. */
5133 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5134 /* Look for the optional nested-name-specifier. */
5135 nested_name_specifier_p
5136 = (cp_parser_nested_name_specifier_opt (parser
,
5137 /*typename_keyword_p=*/false,
5138 /*check_dependency_p=*/true,
5140 /*is_declaration=*/true)
5142 /* Now, if we saw a nested-name-specifier, we might be doing the
5143 second production. */
5144 if (nested_name_specifier_p
5145 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5147 /* Consume the `template' keyword. */
5148 cp_lexer_consume_token (parser
->lexer
);
5149 /* Parse the template-id. */
5150 cp_parser_template_id (parser
,
5151 /*template_keyword_p=*/true,
5152 /*check_dependency_p=*/false,
5153 /*is_declaration=*/true);
5154 /* Look for the `::' token. */
5155 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
5157 /* If the next token is not a `~', then there might be some
5158 additional qualification. */
5159 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5161 /* At this point, we're looking for "type-name :: ~". The type-name
5162 must not be a class-name, since this is a pseudo-destructor. So,
5163 it must be either an enum-name, or a typedef-name -- both of which
5164 are just identifiers. So, we peek ahead to check that the "::"
5165 and "~" tokens are present; if they are not, then we can avoid
5166 calling type_name. */
5167 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
5168 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
5169 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
5171 cp_parser_error (parser
, "non-scalar type");
5175 /* Look for the type-name. */
5176 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
5177 if (*scope
== error_mark_node
)
5180 /* Look for the `::' token. */
5181 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
5186 /* Look for the `~'. */
5187 cp_parser_require (parser
, CPP_COMPL
, "`~'");
5188 /* Look for the type-name again. We are not responsible for
5189 checking that it matches the first type-name. */
5190 *type
= cp_parser_nonclass_name (parser
);
5193 /* Parse a unary-expression.
5199 unary-operator cast-expression
5200 sizeof unary-expression
5208 __extension__ cast-expression
5209 __alignof__ unary-expression
5210 __alignof__ ( type-id )
5211 __real__ cast-expression
5212 __imag__ cast-expression
5215 ADDRESS_P is true iff the unary-expression is appearing as the
5216 operand of the `&' operator. CAST_P is true if this expression is
5217 the target of a cast.
5219 Returns a representation of the expression. */
5222 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5225 enum tree_code unary_operator
;
5227 /* Peek at the next token. */
5228 token
= cp_lexer_peek_token (parser
->lexer
);
5229 /* Some keywords give away the kind of expression. */
5230 if (token
->type
== CPP_KEYWORD
)
5232 enum rid keyword
= token
->keyword
;
5242 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5243 /* Consume the token. */
5244 cp_lexer_consume_token (parser
->lexer
);
5245 /* Parse the operand. */
5246 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5248 if (TYPE_P (operand
))
5249 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5251 return cxx_sizeof_or_alignof_expr (operand
, op
);
5255 return cp_parser_new_expression (parser
);
5258 return cp_parser_delete_expression (parser
);
5262 /* The saved value of the PEDANTIC flag. */
5266 /* Save away the PEDANTIC flag. */
5267 cp_parser_extension_opt (parser
, &saved_pedantic
);
5268 /* Parse the cast-expression. */
5269 expr
= cp_parser_simple_cast_expression (parser
);
5270 /* Restore the PEDANTIC flag. */
5271 pedantic
= saved_pedantic
;
5281 /* Consume the `__real__' or `__imag__' token. */
5282 cp_lexer_consume_token (parser
->lexer
);
5283 /* Parse the cast-expression. */
5284 expression
= cp_parser_simple_cast_expression (parser
);
5285 /* Create the complete representation. */
5286 return build_x_unary_op ((keyword
== RID_REALPART
5287 ? REALPART_EXPR
: IMAGPART_EXPR
),
5297 /* Look for the `:: new' and `:: delete', which also signal the
5298 beginning of a new-expression, or delete-expression,
5299 respectively. If the next token is `::', then it might be one of
5301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5305 /* See if the token after the `::' is one of the keywords in
5306 which we're interested. */
5307 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5308 /* If it's `new', we have a new-expression. */
5309 if (keyword
== RID_NEW
)
5310 return cp_parser_new_expression (parser
);
5311 /* Similarly, for `delete'. */
5312 else if (keyword
== RID_DELETE
)
5313 return cp_parser_delete_expression (parser
);
5316 /* Look for a unary operator. */
5317 unary_operator
= cp_parser_unary_operator (token
);
5318 /* The `++' and `--' operators can be handled similarly, even though
5319 they are not technically unary-operators in the grammar. */
5320 if (unary_operator
== ERROR_MARK
)
5322 if (token
->type
== CPP_PLUS_PLUS
)
5323 unary_operator
= PREINCREMENT_EXPR
;
5324 else if (token
->type
== CPP_MINUS_MINUS
)
5325 unary_operator
= PREDECREMENT_EXPR
;
5326 /* Handle the GNU address-of-label extension. */
5327 else if (cp_parser_allow_gnu_extensions_p (parser
)
5328 && token
->type
== CPP_AND_AND
)
5333 /* Consume the '&&' token. */
5334 cp_lexer_consume_token (parser
->lexer
);
5335 /* Look for the identifier. */
5336 identifier
= cp_parser_identifier (parser
);
5337 /* Create an expression representing the address. */
5338 expression
= finish_label_address_expr (identifier
);
5339 if (cp_parser_non_integral_constant_expression (parser
,
5340 "the address of a label"))
5341 expression
= error_mark_node
;
5345 if (unary_operator
!= ERROR_MARK
)
5347 tree cast_expression
;
5348 tree expression
= error_mark_node
;
5349 const char *non_constant_p
= NULL
;
5351 /* Consume the operator token. */
5352 token
= cp_lexer_consume_token (parser
->lexer
);
5353 /* Parse the cast-expression. */
5355 = cp_parser_cast_expression (parser
,
5356 unary_operator
== ADDR_EXPR
,
5358 /* Now, build an appropriate representation. */
5359 switch (unary_operator
)
5362 non_constant_p
= "`*'";
5363 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5367 non_constant_p
= "`&'";
5370 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5373 case PREINCREMENT_EXPR
:
5374 case PREDECREMENT_EXPR
:
5375 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5378 case UNARY_PLUS_EXPR
:
5380 case TRUTH_NOT_EXPR
:
5381 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5389 && cp_parser_non_integral_constant_expression (parser
,
5391 expression
= error_mark_node
;
5396 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
5397 /*member_access_only_p=*/false);
5400 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5401 unary-operator, the corresponding tree code is returned. */
5403 static enum tree_code
5404 cp_parser_unary_operator (cp_token
* token
)
5406 switch (token
->type
)
5409 return INDIRECT_REF
;
5415 return UNARY_PLUS_EXPR
;
5421 return TRUTH_NOT_EXPR
;
5424 return BIT_NOT_EXPR
;
5431 /* Parse a new-expression.
5434 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5435 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5437 Returns a representation of the expression. */
5440 cp_parser_new_expression (cp_parser
* parser
)
5442 bool global_scope_p
;
5448 /* Look for the optional `::' operator. */
5450 = (cp_parser_global_scope_opt (parser
,
5451 /*current_scope_valid_p=*/false)
5453 /* Look for the `new' operator. */
5454 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5455 /* There's no easy way to tell a new-placement from the
5456 `( type-id )' construct. */
5457 cp_parser_parse_tentatively (parser
);
5458 /* Look for a new-placement. */
5459 placement
= cp_parser_new_placement (parser
);
5460 /* If that didn't work out, there's no new-placement. */
5461 if (!cp_parser_parse_definitely (parser
))
5462 placement
= NULL_TREE
;
5464 /* If the next token is a `(', then we have a parenthesized
5466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5468 /* Consume the `('. */
5469 cp_lexer_consume_token (parser
->lexer
);
5470 /* Parse the type-id. */
5471 type
= cp_parser_type_id (parser
);
5472 /* Look for the closing `)'. */
5473 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5474 /* There should not be a direct-new-declarator in this production,
5475 but GCC used to allowed this, so we check and emit a sensible error
5476 message for this case. */
5477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5479 error ("array bound forbidden after parenthesized type-id");
5480 inform ("try removing the parentheses around the type-id");
5481 cp_parser_direct_new_declarator (parser
);
5485 /* Otherwise, there must be a new-type-id. */
5487 type
= cp_parser_new_type_id (parser
, &nelts
);
5489 /* If the next token is a `(', then we have a new-initializer. */
5490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5491 initializer
= cp_parser_new_initializer (parser
);
5493 initializer
= NULL_TREE
;
5495 /* A new-expression may not appear in an integral constant
5497 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5498 return error_mark_node
;
5500 /* Create a representation of the new-expression. */
5501 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5504 /* Parse a new-placement.
5509 Returns the same representation as for an expression-list. */
5512 cp_parser_new_placement (cp_parser
* parser
)
5514 tree expression_list
;
5516 /* Parse the expression-list. */
5517 expression_list
= (cp_parser_parenthesized_expression_list
5518 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5519 /*non_constant_p=*/NULL
));
5521 return expression_list
;
5524 /* Parse a new-type-id.
5527 type-specifier-seq new-declarator [opt]
5529 Returns the TYPE allocated. If the new-type-id indicates an array
5530 type, *NELTS is set to the number of elements in the last array
5531 bound; the TYPE will not include the last array bound. */
5534 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5536 cp_decl_specifier_seq type_specifier_seq
;
5537 cp_declarator
*new_declarator
;
5538 cp_declarator
*declarator
;
5539 cp_declarator
*outer_declarator
;
5540 const char *saved_message
;
5543 /* The type-specifier sequence must not contain type definitions.
5544 (It cannot contain declarations of new types either, but if they
5545 are not definitions we will catch that because they are not
5547 saved_message
= parser
->type_definition_forbidden_message
;
5548 parser
->type_definition_forbidden_message
5549 = "types may not be defined in a new-type-id";
5550 /* Parse the type-specifier-seq. */
5551 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5552 &type_specifier_seq
);
5553 /* Restore the old message. */
5554 parser
->type_definition_forbidden_message
= saved_message
;
5555 /* Parse the new-declarator. */
5556 new_declarator
= cp_parser_new_declarator_opt (parser
);
5558 /* Determine the number of elements in the last array dimension, if
5561 /* Skip down to the last array dimension. */
5562 declarator
= new_declarator
;
5563 outer_declarator
= NULL
;
5564 while (declarator
&& (declarator
->kind
== cdk_pointer
5565 || declarator
->kind
== cdk_ptrmem
))
5567 outer_declarator
= declarator
;
5568 declarator
= declarator
->declarator
;
5571 && declarator
->kind
== cdk_array
5572 && declarator
->declarator
5573 && declarator
->declarator
->kind
== cdk_array
)
5575 outer_declarator
= declarator
;
5576 declarator
= declarator
->declarator
;
5579 if (declarator
&& declarator
->kind
== cdk_array
)
5581 *nelts
= declarator
->u
.array
.bounds
;
5582 if (*nelts
== error_mark_node
)
5583 *nelts
= integer_one_node
;
5585 if (outer_declarator
)
5586 outer_declarator
->declarator
= declarator
->declarator
;
5588 new_declarator
= NULL
;
5591 type
= groktypename (&type_specifier_seq
, new_declarator
);
5595 /* Parse an (optional) new-declarator.
5598 ptr-operator new-declarator [opt]
5599 direct-new-declarator
5601 Returns the declarator. */
5603 static cp_declarator
*
5604 cp_parser_new_declarator_opt (cp_parser
* parser
)
5606 enum tree_code code
;
5608 cp_cv_quals cv_quals
;
5610 /* We don't know if there's a ptr-operator next, or not. */
5611 cp_parser_parse_tentatively (parser
);
5612 /* Look for a ptr-operator. */
5613 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5614 /* If that worked, look for more new-declarators. */
5615 if (cp_parser_parse_definitely (parser
))
5617 cp_declarator
*declarator
;
5619 /* Parse another optional declarator. */
5620 declarator
= cp_parser_new_declarator_opt (parser
);
5622 return cp_parser_make_indirect_declarator
5623 (code
, type
, cv_quals
, declarator
);
5626 /* If the next token is a `[', there is a direct-new-declarator. */
5627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5628 return cp_parser_direct_new_declarator (parser
);
5633 /* Parse a direct-new-declarator.
5635 direct-new-declarator:
5637 direct-new-declarator [constant-expression]
5641 static cp_declarator
*
5642 cp_parser_direct_new_declarator (cp_parser
* parser
)
5644 cp_declarator
*declarator
= NULL
;
5650 /* Look for the opening `['. */
5651 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5652 /* The first expression is not required to be constant. */
5655 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5656 /* The standard requires that the expression have integral
5657 type. DR 74 adds enumeration types. We believe that the
5658 real intent is that these expressions be handled like the
5659 expression in a `switch' condition, which also allows
5660 classes with a single conversion to integral or
5661 enumeration type. */
5662 if (!processing_template_decl
)
5665 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5670 error ("expression in new-declarator must have integral "
5671 "or enumeration type");
5672 expression
= error_mark_node
;
5676 /* But all the other expressions must be. */
5679 = cp_parser_constant_expression (parser
,
5680 /*allow_non_constant=*/false,
5682 /* Look for the closing `]'. */
5683 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5685 /* Add this bound to the declarator. */
5686 declarator
= make_array_declarator (declarator
, expression
);
5688 /* If the next token is not a `[', then there are no more
5690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5697 /* Parse a new-initializer.
5700 ( expression-list [opt] )
5702 Returns a representation of the expression-list. If there is no
5703 expression-list, VOID_ZERO_NODE is returned. */
5706 cp_parser_new_initializer (cp_parser
* parser
)
5708 tree expression_list
;
5710 expression_list
= (cp_parser_parenthesized_expression_list
5711 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5712 /*non_constant_p=*/NULL
));
5713 if (!expression_list
)
5714 expression_list
= void_zero_node
;
5716 return expression_list
;
5719 /* Parse a delete-expression.
5722 :: [opt] delete cast-expression
5723 :: [opt] delete [ ] cast-expression
5725 Returns a representation of the expression. */
5728 cp_parser_delete_expression (cp_parser
* parser
)
5730 bool global_scope_p
;
5734 /* Look for the optional `::' operator. */
5736 = (cp_parser_global_scope_opt (parser
,
5737 /*current_scope_valid_p=*/false)
5739 /* Look for the `delete' keyword. */
5740 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5741 /* See if the array syntax is in use. */
5742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5744 /* Consume the `[' token. */
5745 cp_lexer_consume_token (parser
->lexer
);
5746 /* Look for the `]' token. */
5747 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5748 /* Remember that this is the `[]' construct. */
5754 /* Parse the cast-expression. */
5755 expression
= cp_parser_simple_cast_expression (parser
);
5757 /* A delete-expression may not appear in an integral constant
5759 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5760 return error_mark_node
;
5762 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5765 /* Parse a cast-expression.
5769 ( type-id ) cast-expression
5771 ADDRESS_P is true iff the unary-expression is appearing as the
5772 operand of the `&' operator. CAST_P is true if this expression is
5773 the target of a cast.
5775 Returns a representation of the expression. */
5778 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5780 /* If it's a `(', then we might be looking at a cast. */
5781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5783 tree type
= NULL_TREE
;
5784 tree expr
= NULL_TREE
;
5785 bool compound_literal_p
;
5786 const char *saved_message
;
5788 /* There's no way to know yet whether or not this is a cast.
5789 For example, `(int (3))' is a unary-expression, while `(int)
5790 3' is a cast. So, we resort to parsing tentatively. */
5791 cp_parser_parse_tentatively (parser
);
5792 /* Types may not be defined in a cast. */
5793 saved_message
= parser
->type_definition_forbidden_message
;
5794 parser
->type_definition_forbidden_message
5795 = "types may not be defined in casts";
5796 /* Consume the `('. */
5797 cp_lexer_consume_token (parser
->lexer
);
5798 /* A very tricky bit is that `(struct S) { 3 }' is a
5799 compound-literal (which we permit in C++ as an extension).
5800 But, that construct is not a cast-expression -- it is a
5801 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5802 is legal; if the compound-literal were a cast-expression,
5803 you'd need an extra set of parentheses.) But, if we parse
5804 the type-id, and it happens to be a class-specifier, then we
5805 will commit to the parse at that point, because we cannot
5806 undo the action that is done when creating a new class. So,
5807 then we cannot back up and do a postfix-expression.
5809 Therefore, we scan ahead to the closing `)', and check to see
5810 if the token after the `)' is a `{'. If so, we are not
5811 looking at a cast-expression.
5813 Save tokens so that we can put them back. */
5814 cp_lexer_save_tokens (parser
->lexer
);
5815 /* Skip tokens until the next token is a closing parenthesis.
5816 If we find the closing `)', and the next token is a `{', then
5817 we are looking at a compound-literal. */
5819 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5820 /*consume_paren=*/true)
5821 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5822 /* Roll back the tokens we skipped. */
5823 cp_lexer_rollback_tokens (parser
->lexer
);
5824 /* If we were looking at a compound-literal, simulate an error
5825 so that the call to cp_parser_parse_definitely below will
5827 if (compound_literal_p
)
5828 cp_parser_simulate_error (parser
);
5831 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5832 parser
->in_type_id_in_expr_p
= true;
5833 /* Look for the type-id. */
5834 type
= cp_parser_type_id (parser
);
5835 /* Look for the closing `)'. */
5836 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5837 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5840 /* Restore the saved message. */
5841 parser
->type_definition_forbidden_message
= saved_message
;
5843 /* If ok so far, parse the dependent expression. We cannot be
5844 sure it is a cast. Consider `(T ())'. It is a parenthesized
5845 ctor of T, but looks like a cast to function returning T
5846 without a dependent expression. */
5847 if (!cp_parser_error_occurred (parser
))
5848 expr
= cp_parser_cast_expression (parser
,
5849 /*address_p=*/false,
5852 if (cp_parser_parse_definitely (parser
))
5854 /* Warn about old-style casts, if so requested. */
5855 if (warn_old_style_cast
5856 && !in_system_header
5857 && !VOID_TYPE_P (type
)
5858 && current_lang_name
!= lang_name_c
)
5859 warning (OPT_Wold_style_cast
, "use of old-style cast");
5861 /* Only type conversions to integral or enumeration types
5862 can be used in constant-expressions. */
5863 if (!cast_valid_in_integral_constant_expression_p (type
)
5864 && (cp_parser_non_integral_constant_expression
5866 "a cast to a type other than an integral or "
5867 "enumeration type")))
5868 return error_mark_node
;
5870 /* Perform the cast. */
5871 expr
= build_c_cast (type
, expr
);
5876 /* If we get here, then it's not a cast, so it must be a
5877 unary-expression. */
5878 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5881 /* Parse a binary expression of the general form:
5885 pm-expression .* cast-expression
5886 pm-expression ->* cast-expression
5888 multiplicative-expression:
5890 multiplicative-expression * pm-expression
5891 multiplicative-expression / pm-expression
5892 multiplicative-expression % pm-expression
5894 additive-expression:
5895 multiplicative-expression
5896 additive-expression + multiplicative-expression
5897 additive-expression - multiplicative-expression
5901 shift-expression << additive-expression
5902 shift-expression >> additive-expression
5904 relational-expression:
5906 relational-expression < shift-expression
5907 relational-expression > shift-expression
5908 relational-expression <= shift-expression
5909 relational-expression >= shift-expression
5913 relational-expression:
5914 relational-expression <? shift-expression
5915 relational-expression >? shift-expression
5917 equality-expression:
5918 relational-expression
5919 equality-expression == relational-expression
5920 equality-expression != relational-expression
5924 and-expression & equality-expression
5926 exclusive-or-expression:
5928 exclusive-or-expression ^ and-expression
5930 inclusive-or-expression:
5931 exclusive-or-expression
5932 inclusive-or-expression | exclusive-or-expression
5934 logical-and-expression:
5935 inclusive-or-expression
5936 logical-and-expression && inclusive-or-expression
5938 logical-or-expression:
5939 logical-and-expression
5940 logical-or-expression || logical-and-expression
5942 All these are implemented with a single function like:
5945 simple-cast-expression
5946 binary-expression <token> binary-expression
5948 CAST_P is true if this expression is the target of a cast.
5950 The binops_by_token map is used to get the tree codes for each <token> type.
5951 binary-expressions are associated according to a precedence table. */
5953 #define TOKEN_PRECEDENCE(token) \
5954 (((token->type == CPP_GREATER \
5955 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5956 && !parser->greater_than_is_operator_p) \
5957 ? PREC_NOT_OPERATOR \
5958 : binops_by_token[token->type].prec)
5961 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5963 cp_parser_expression_stack stack
;
5964 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5967 enum tree_code tree_type
, lhs_type
, rhs_type
;
5968 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5971 /* Parse the first expression. */
5972 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5973 lhs_type
= ERROR_MARK
;
5977 /* Get an operator token. */
5978 token
= cp_lexer_peek_token (parser
->lexer
);
5980 if (warn_cxx0x_compat
5981 && token
->type
== CPP_RSHIFT
5982 && !parser
->greater_than_is_operator_p
)
5984 warning (OPT_Wc__0x_compat
,
5985 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5987 warning (OPT_Wc__0x_compat
,
5988 "suggest parentheses around %<>>%> expression");
5991 new_prec
= TOKEN_PRECEDENCE (token
);
5993 /* Popping an entry off the stack means we completed a subexpression:
5994 - either we found a token which is not an operator (`>' where it is not
5995 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5996 will happen repeatedly;
5997 - or, we found an operator which has lower priority. This is the case
5998 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6000 if (new_prec
<= prec
)
6009 tree_type
= binops_by_token
[token
->type
].tree_type
;
6011 /* We used the operator token. */
6012 cp_lexer_consume_token (parser
->lexer
);
6014 /* Extract another operand. It may be the RHS of this expression
6015 or the LHS of a new, higher priority expression. */
6016 rhs
= cp_parser_simple_cast_expression (parser
);
6017 rhs_type
= ERROR_MARK
;
6019 /* Get another operator token. Look up its precedence to avoid
6020 building a useless (immediately popped) stack entry for common
6021 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6022 token
= cp_lexer_peek_token (parser
->lexer
);
6023 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6024 if (lookahead_prec
> new_prec
)
6026 /* ... and prepare to parse the RHS of the new, higher priority
6027 expression. Since precedence levels on the stack are
6028 monotonically increasing, we do not have to care about
6031 sp
->tree_type
= tree_type
;
6033 sp
->lhs_type
= lhs_type
;
6036 lhs_type
= rhs_type
;
6038 new_prec
= lookahead_prec
;
6042 /* If the stack is not empty, we have parsed into LHS the right side
6043 (`4' in the example above) of an expression we had suspended.
6044 We can use the information on the stack to recover the LHS (`3')
6045 from the stack together with the tree code (`MULT_EXPR'), and
6046 the precedence of the higher level subexpression
6047 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6048 which will be used to actually build the additive expression. */
6051 tree_type
= sp
->tree_type
;
6053 rhs_type
= lhs_type
;
6055 lhs_type
= sp
->lhs_type
;
6058 overloaded_p
= false;
6059 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6061 lhs_type
= tree_type
;
6063 /* If the binary operator required the use of an overloaded operator,
6064 then this expression cannot be an integral constant-expression.
6065 An overloaded operator can be used even if both operands are
6066 otherwise permissible in an integral constant-expression if at
6067 least one of the operands is of enumeration type. */
6070 && (cp_parser_non_integral_constant_expression
6071 (parser
, "calls to overloaded operators")))
6072 return error_mark_node
;
6079 /* Parse the `? expression : assignment-expression' part of a
6080 conditional-expression. The LOGICAL_OR_EXPR is the
6081 logical-or-expression that started the conditional-expression.
6082 Returns a representation of the entire conditional-expression.
6084 This routine is used by cp_parser_assignment_expression.
6086 ? expression : assignment-expression
6090 ? : assignment-expression */
6093 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6096 tree assignment_expr
;
6098 /* Consume the `?' token. */
6099 cp_lexer_consume_token (parser
->lexer
);
6100 if (cp_parser_allow_gnu_extensions_p (parser
)
6101 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
6102 /* Implicit true clause. */
6105 /* Parse the expression. */
6106 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6108 /* The next token should be a `:'. */
6109 cp_parser_require (parser
, CPP_COLON
, "`:'");
6110 /* Parse the assignment-expression. */
6111 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6113 /* Build the conditional-expression. */
6114 return build_x_conditional_expr (logical_or_expr
,
6119 /* Parse an assignment-expression.
6121 assignment-expression:
6122 conditional-expression
6123 logical-or-expression assignment-operator assignment_expression
6126 CAST_P is true if this expression is the target of a cast.
6128 Returns a representation for the expression. */
6131 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
6135 /* If the next token is the `throw' keyword, then we're looking at
6136 a throw-expression. */
6137 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6138 expr
= cp_parser_throw_expression (parser
);
6139 /* Otherwise, it must be that we are looking at a
6140 logical-or-expression. */
6143 /* Parse the binary expressions (logical-or-expression). */
6144 expr
= cp_parser_binary_expression (parser
, cast_p
);
6145 /* If the next token is a `?' then we're actually looking at a
6146 conditional-expression. */
6147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6148 return cp_parser_question_colon_clause (parser
, expr
);
6151 enum tree_code assignment_operator
;
6153 /* If it's an assignment-operator, we're using the second
6156 = cp_parser_assignment_operator_opt (parser
);
6157 if (assignment_operator
!= ERROR_MARK
)
6161 /* Parse the right-hand side of the assignment. */
6162 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
6163 /* An assignment may not appear in a
6164 constant-expression. */
6165 if (cp_parser_non_integral_constant_expression (parser
,
6167 return error_mark_node
;
6168 /* Build the assignment expression. */
6169 expr
= build_x_modify_expr (expr
,
6170 assignment_operator
,
6179 /* Parse an (optional) assignment-operator.
6181 assignment-operator: one of
6182 = *= /= %= += -= >>= <<= &= ^= |=
6186 assignment-operator: one of
6189 If the next token is an assignment operator, the corresponding tree
6190 code is returned, and the token is consumed. For example, for
6191 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6192 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6193 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6194 operator, ERROR_MARK is returned. */
6196 static enum tree_code
6197 cp_parser_assignment_operator_opt (cp_parser
* parser
)
6202 /* Peek at the next toen. */
6203 token
= cp_lexer_peek_token (parser
->lexer
);
6205 switch (token
->type
)
6216 op
= TRUNC_DIV_EXPR
;
6220 op
= TRUNC_MOD_EXPR
;
6252 /* Nothing else is an assignment operator. */
6256 /* If it was an assignment operator, consume it. */
6257 if (op
!= ERROR_MARK
)
6258 cp_lexer_consume_token (parser
->lexer
);
6263 /* Parse an expression.
6266 assignment-expression
6267 expression , assignment-expression
6269 CAST_P is true if this expression is the target of a cast.
6271 Returns a representation of the expression. */
6274 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
6276 tree expression
= NULL_TREE
;
6280 tree assignment_expression
;
6282 /* Parse the next assignment-expression. */
6283 assignment_expression
6284 = cp_parser_assignment_expression (parser
, cast_p
);
6285 /* If this is the first assignment-expression, we can just
6288 expression
= assignment_expression
;
6290 expression
= build_x_compound_expr (expression
,
6291 assignment_expression
);
6292 /* If the next token is not a comma, then we are done with the
6294 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6296 /* Consume the `,'. */
6297 cp_lexer_consume_token (parser
->lexer
);
6298 /* A comma operator cannot appear in a constant-expression. */
6299 if (cp_parser_non_integral_constant_expression (parser
,
6300 "a comma operator"))
6301 expression
= error_mark_node
;
6307 /* Parse a constant-expression.
6309 constant-expression:
6310 conditional-expression
6312 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6313 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6314 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6315 is false, NON_CONSTANT_P should be NULL. */
6318 cp_parser_constant_expression (cp_parser
* parser
,
6319 bool allow_non_constant_p
,
6320 bool *non_constant_p
)
6322 bool saved_integral_constant_expression_p
;
6323 bool saved_allow_non_integral_constant_expression_p
;
6324 bool saved_non_integral_constant_expression_p
;
6327 /* It might seem that we could simply parse the
6328 conditional-expression, and then check to see if it were
6329 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6330 one that the compiler can figure out is constant, possibly after
6331 doing some simplifications or optimizations. The standard has a
6332 precise definition of constant-expression, and we must honor
6333 that, even though it is somewhat more restrictive.
6339 is not a legal declaration, because `(2, 3)' is not a
6340 constant-expression. The `,' operator is forbidden in a
6341 constant-expression. However, GCC's constant-folding machinery
6342 will fold this operation to an INTEGER_CST for `3'. */
6344 /* Save the old settings. */
6345 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6346 saved_allow_non_integral_constant_expression_p
6347 = parser
->allow_non_integral_constant_expression_p
;
6348 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6349 /* We are now parsing a constant-expression. */
6350 parser
->integral_constant_expression_p
= true;
6351 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6352 parser
->non_integral_constant_expression_p
= false;
6353 /* Although the grammar says "conditional-expression", we parse an
6354 "assignment-expression", which also permits "throw-expression"
6355 and the use of assignment operators. In the case that
6356 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6357 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6358 actually essential that we look for an assignment-expression.
6359 For example, cp_parser_initializer_clauses uses this function to
6360 determine whether a particular assignment-expression is in fact
6362 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6363 /* Restore the old settings. */
6364 parser
->integral_constant_expression_p
6365 = saved_integral_constant_expression_p
;
6366 parser
->allow_non_integral_constant_expression_p
6367 = saved_allow_non_integral_constant_expression_p
;
6368 if (allow_non_constant_p
)
6369 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6370 else if (parser
->non_integral_constant_expression_p
)
6371 expression
= error_mark_node
;
6372 parser
->non_integral_constant_expression_p
6373 = saved_non_integral_constant_expression_p
;
6378 /* Parse __builtin_offsetof.
6380 offsetof-expression:
6381 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6383 offsetof-member-designator:
6385 | offsetof-member-designator "." id-expression
6386 | offsetof-member-designator "[" expression "]" */
6389 cp_parser_builtin_offsetof (cp_parser
*parser
)
6391 int save_ice_p
, save_non_ice_p
;
6395 /* We're about to accept non-integral-constant things, but will
6396 definitely yield an integral constant expression. Save and
6397 restore these values around our local parsing. */
6398 save_ice_p
= parser
->integral_constant_expression_p
;
6399 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6401 /* Consume the "__builtin_offsetof" token. */
6402 cp_lexer_consume_token (parser
->lexer
);
6403 /* Consume the opening `('. */
6404 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6405 /* Parse the type-id. */
6406 type
= cp_parser_type_id (parser
);
6407 /* Look for the `,'. */
6408 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6410 /* Build the (type *)null that begins the traditional offsetof macro. */
6411 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6413 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6414 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6418 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6419 switch (token
->type
)
6421 case CPP_OPEN_SQUARE
:
6422 /* offsetof-member-designator "[" expression "]" */
6423 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6427 /* offsetof-member-designator "." identifier */
6428 cp_lexer_consume_token (parser
->lexer
);
6429 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6433 case CPP_CLOSE_PAREN
:
6434 /* Consume the ")" token. */
6435 cp_lexer_consume_token (parser
->lexer
);
6439 /* Error. We know the following require will fail, but
6440 that gives the proper error message. */
6441 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6442 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6443 expr
= error_mark_node
;
6449 /* If we're processing a template, we can't finish the semantics yet.
6450 Otherwise we can fold the entire expression now. */
6451 if (processing_template_decl
)
6452 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6454 expr
= finish_offsetof (expr
);
6457 parser
->integral_constant_expression_p
= save_ice_p
;
6458 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6463 /* Parse a trait expression. */
6466 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
6469 tree type1
, type2
= NULL_TREE
;
6470 bool binary
= false;
6471 cp_decl_specifier_seq decl_specs
;
6475 case RID_HAS_NOTHROW_ASSIGN
:
6476 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
6478 case RID_HAS_NOTHROW_CONSTRUCTOR
:
6479 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
6481 case RID_HAS_NOTHROW_COPY
:
6482 kind
= CPTK_HAS_NOTHROW_COPY
;
6484 case RID_HAS_TRIVIAL_ASSIGN
:
6485 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
6487 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
6488 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
6490 case RID_HAS_TRIVIAL_COPY
:
6491 kind
= CPTK_HAS_TRIVIAL_COPY
;
6493 case RID_HAS_TRIVIAL_DESTRUCTOR
:
6494 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
6496 case RID_HAS_VIRTUAL_DESTRUCTOR
:
6497 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
6499 case RID_IS_ABSTRACT
:
6500 kind
= CPTK_IS_ABSTRACT
;
6502 case RID_IS_BASE_OF
:
6503 kind
= CPTK_IS_BASE_OF
;
6507 kind
= CPTK_IS_CLASS
;
6509 case RID_IS_CONVERTIBLE_TO
:
6510 kind
= CPTK_IS_CONVERTIBLE_TO
;
6514 kind
= CPTK_IS_EMPTY
;
6517 kind
= CPTK_IS_ENUM
;
6522 case RID_IS_POLYMORPHIC
:
6523 kind
= CPTK_IS_POLYMORPHIC
;
6526 kind
= CPTK_IS_UNION
;
6532 /* Consume the token. */
6533 cp_lexer_consume_token (parser
->lexer
);
6535 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6537 type1
= cp_parser_type_id (parser
);
6539 if (type1
== error_mark_node
)
6540 return error_mark_node
;
6542 /* Build a trivial decl-specifier-seq. */
6543 clear_decl_specs (&decl_specs
);
6544 decl_specs
.type
= type1
;
6546 /* Call grokdeclarator to figure out what type this is. */
6547 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
6548 /*initialized=*/0, /*attrlist=*/NULL
);
6552 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6554 type2
= cp_parser_type_id (parser
);
6556 if (type2
== error_mark_node
)
6557 return error_mark_node
;
6559 /* Build a trivial decl-specifier-seq. */
6560 clear_decl_specs (&decl_specs
);
6561 decl_specs
.type
= type2
;
6563 /* Call grokdeclarator to figure out what type this is. */
6564 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
6565 /*initialized=*/0, /*attrlist=*/NULL
);
6568 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6570 /* Complete the trait expression, which may mean either processing
6571 the trait expr now or saving it for template instantiation. */
6572 return finish_trait_expr (kind
, type1
, type2
);
6575 /* Statements [gram.stmt.stmt] */
6577 /* Parse a statement.
6581 expression-statement
6586 declaration-statement
6589 IN_COMPOUND is true when the statement is nested inside a
6590 cp_parser_compound_statement; this matters for certain pragmas.
6592 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6593 is a (possibly labeled) if statement which is not enclosed in braces
6594 and has an else clause. This is used to implement -Wparentheses. */
6597 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6598 bool in_compound
, bool *if_p
)
6602 location_t statement_location
;
6607 /* There is no statement yet. */
6608 statement
= NULL_TREE
;
6609 /* Peek at the next token. */
6610 token
= cp_lexer_peek_token (parser
->lexer
);
6611 /* Remember the location of the first token in the statement. */
6612 statement_location
= token
->location
;
6613 /* If this is a keyword, then that will often determine what kind of
6614 statement we have. */
6615 if (token
->type
== CPP_KEYWORD
)
6617 enum rid keyword
= token
->keyword
;
6623 /* Looks like a labeled-statement with a case label.
6624 Parse the label, and then use tail recursion to parse
6626 cp_parser_label_for_labeled_statement (parser
);
6631 statement
= cp_parser_selection_statement (parser
, if_p
);
6637 statement
= cp_parser_iteration_statement (parser
);
6644 statement
= cp_parser_jump_statement (parser
);
6647 /* Objective-C++ exception-handling constructs. */
6650 case RID_AT_FINALLY
:
6651 case RID_AT_SYNCHRONIZED
:
6653 statement
= cp_parser_objc_statement (parser
);
6657 statement
= cp_parser_try_block (parser
);
6661 /* This must be a namespace alias definition. */
6662 cp_parser_declaration_statement (parser
);
6666 /* It might be a keyword like `int' that can start a
6667 declaration-statement. */
6671 else if (token
->type
== CPP_NAME
)
6673 /* If the next token is a `:', then we are looking at a
6674 labeled-statement. */
6675 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6676 if (token
->type
== CPP_COLON
)
6678 /* Looks like a labeled-statement with an ordinary label.
6679 Parse the label, and then use tail recursion to parse
6681 cp_parser_label_for_labeled_statement (parser
);
6685 /* Anything that starts with a `{' must be a compound-statement. */
6686 else if (token
->type
== CPP_OPEN_BRACE
)
6687 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6688 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6689 a statement all its own. */
6690 else if (token
->type
== CPP_PRAGMA
)
6692 /* Only certain OpenMP pragmas are attached to statements, and thus
6693 are considered statements themselves. All others are not. In
6694 the context of a compound, accept the pragma as a "statement" and
6695 return so that we can check for a close brace. Otherwise we
6696 require a real statement and must go back and read one. */
6698 cp_parser_pragma (parser
, pragma_compound
);
6699 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6703 else if (token
->type
== CPP_EOF
)
6705 cp_parser_error (parser
, "expected statement");
6709 /* Everything else must be a declaration-statement or an
6710 expression-statement. Try for the declaration-statement
6711 first, unless we are looking at a `;', in which case we know that
6712 we have an expression-statement. */
6715 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6717 cp_parser_parse_tentatively (parser
);
6718 /* Try to parse the declaration-statement. */
6719 cp_parser_declaration_statement (parser
);
6720 /* If that worked, we're done. */
6721 if (cp_parser_parse_definitely (parser
))
6724 /* Look for an expression-statement instead. */
6725 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6728 /* Set the line number for the statement. */
6729 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6730 SET_EXPR_LOCATION (statement
, statement_location
);
6733 /* Parse the label for a labeled-statement, i.e.
6736 case constant-expression :
6740 case constant-expression ... constant-expression : statement
6742 When a label is parsed without errors, the label is added to the
6743 parse tree by the finish_* functions, so this function doesn't
6744 have to return the label. */
6747 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6751 /* The next token should be an identifier. */
6752 token
= cp_lexer_peek_token (parser
->lexer
);
6753 if (token
->type
!= CPP_NAME
6754 && token
->type
!= CPP_KEYWORD
)
6756 cp_parser_error (parser
, "expected labeled-statement");
6760 switch (token
->keyword
)
6767 /* Consume the `case' token. */
6768 cp_lexer_consume_token (parser
->lexer
);
6769 /* Parse the constant-expression. */
6770 expr
= cp_parser_constant_expression (parser
,
6771 /*allow_non_constant_p=*/false,
6774 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6775 if (ellipsis
->type
== CPP_ELLIPSIS
)
6777 /* Consume the `...' token. */
6778 cp_lexer_consume_token (parser
->lexer
);
6780 cp_parser_constant_expression (parser
,
6781 /*allow_non_constant_p=*/false,
6783 /* We don't need to emit warnings here, as the common code
6784 will do this for us. */
6787 expr_hi
= NULL_TREE
;
6789 if (parser
->in_switch_statement_p
)
6790 finish_case_label (expr
, expr_hi
);
6792 error ("case label %qE not within a switch statement", expr
);
6797 /* Consume the `default' token. */
6798 cp_lexer_consume_token (parser
->lexer
);
6800 if (parser
->in_switch_statement_p
)
6801 finish_case_label (NULL_TREE
, NULL_TREE
);
6803 error ("case label not within a switch statement");
6807 /* Anything else must be an ordinary label. */
6808 finish_label_stmt (cp_parser_identifier (parser
));
6812 /* Require the `:' token. */
6813 cp_parser_require (parser
, CPP_COLON
, "`:'");
6816 /* Parse an expression-statement.
6818 expression-statement:
6821 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6822 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6823 indicates whether this expression-statement is part of an
6824 expression statement. */
6827 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6829 tree statement
= NULL_TREE
;
6831 /* If the next token is a ';', then there is no expression
6833 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6834 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6836 /* Consume the final `;'. */
6837 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6839 if (in_statement_expr
6840 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6841 /* This is the final expression statement of a statement
6843 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6845 statement
= finish_expr_stmt (statement
);
6852 /* Parse a compound-statement.
6855 { statement-seq [opt] }
6860 { label-declaration-seq [opt] statement-seq [opt] }
6862 label-declaration-seq:
6864 label-declaration-seq label-declaration
6866 Returns a tree representing the statement. */
6869 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6874 /* Consume the `{'. */
6875 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6876 return error_mark_node
;
6877 /* Begin the compound-statement. */
6878 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6879 /* If the next keyword is `__label__' we have a label declaration. */
6880 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
6881 cp_parser_label_declaration (parser
);
6882 /* Parse an (optional) statement-seq. */
6883 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6884 /* Finish the compound-statement. */
6885 finish_compound_stmt (compound_stmt
);
6886 /* Consume the `}'. */
6887 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6889 return compound_stmt
;
6892 /* Parse an (optional) statement-seq.
6896 statement-seq [opt] statement */
6899 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6901 /* Scan statements until there aren't any more. */
6904 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6906 /* If we're looking at a `}', then we've run out of statements. */
6907 if (token
->type
== CPP_CLOSE_BRACE
6908 || token
->type
== CPP_EOF
6909 || token
->type
== CPP_PRAGMA_EOL
)
6912 /* If we are in a compound statement and find 'else' then
6913 something went wrong. */
6914 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
6916 if (parser
->in_statement
& IN_IF_STMT
)
6920 token
= cp_lexer_consume_token (parser
->lexer
);
6921 error ("%<else%> without a previous %<if%>");
6925 /* Parse the statement. */
6926 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
6930 /* Parse a selection-statement.
6932 selection-statement:
6933 if ( condition ) statement
6934 if ( condition ) statement else statement
6935 switch ( condition ) statement
6937 Returns the new IF_STMT or SWITCH_STMT.
6939 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6940 is a (possibly labeled) if statement which is not enclosed in
6941 braces and has an else clause. This is used to implement
6945 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
6953 /* Peek at the next token. */
6954 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6956 /* See what kind of keyword it is. */
6957 keyword
= token
->keyword
;
6966 /* Look for the `('. */
6967 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6969 cp_parser_skip_to_end_of_statement (parser
);
6970 return error_mark_node
;
6973 /* Begin the selection-statement. */
6974 if (keyword
== RID_IF
)
6975 statement
= begin_if_stmt ();
6977 statement
= begin_switch_stmt ();
6979 /* Parse the condition. */
6980 condition
= cp_parser_condition (parser
);
6981 /* Look for the `)'. */
6982 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6983 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6984 /*consume_paren=*/true);
6986 if (keyword
== RID_IF
)
6989 unsigned char in_statement
;
6991 /* Add the condition. */
6992 finish_if_stmt_cond (condition
, statement
);
6994 /* Parse the then-clause. */
6995 in_statement
= parser
->in_statement
;
6996 parser
->in_statement
|= IN_IF_STMT
;
6997 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
6998 parser
->in_statement
= in_statement
;
7000 finish_then_clause (statement
);
7002 /* If the next token is `else', parse the else-clause. */
7003 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
7006 /* Consume the `else' keyword. */
7007 cp_lexer_consume_token (parser
->lexer
);
7008 begin_else_clause (statement
);
7009 /* Parse the else-clause. */
7010 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7011 finish_else_clause (statement
);
7013 /* If we are currently parsing a then-clause, then
7014 IF_P will not be NULL. We set it to true to
7015 indicate that this if statement has an else clause.
7016 This may trigger the Wparentheses warning below
7017 when we get back up to the parent if statement. */
7023 /* This if statement does not have an else clause. If
7024 NESTED_IF is true, then the then-clause is an if
7025 statement which does have an else clause. We warn
7026 about the potential ambiguity. */
7028 warning (OPT_Wparentheses
,
7029 ("%Hsuggest explicit braces "
7030 "to avoid ambiguous %<else%>"),
7031 EXPR_LOCUS (statement
));
7034 /* Now we're all done with the if-statement. */
7035 finish_if_stmt (statement
);
7039 bool in_switch_statement_p
;
7040 unsigned char in_statement
;
7042 /* Add the condition. */
7043 finish_switch_cond (condition
, statement
);
7045 /* Parse the body of the switch-statement. */
7046 in_switch_statement_p
= parser
->in_switch_statement_p
;
7047 in_statement
= parser
->in_statement
;
7048 parser
->in_switch_statement_p
= true;
7049 parser
->in_statement
|= IN_SWITCH_STMT
;
7050 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7051 parser
->in_switch_statement_p
= in_switch_statement_p
;
7052 parser
->in_statement
= in_statement
;
7054 /* Now we're all done with the switch-statement. */
7055 finish_switch_stmt (statement
);
7063 cp_parser_error (parser
, "expected selection-statement");
7064 return error_mark_node
;
7068 /* Parse a condition.
7072 type-specifier-seq declarator = assignment-expression
7077 type-specifier-seq declarator asm-specification [opt]
7078 attributes [opt] = assignment-expression
7080 Returns the expression that should be tested. */
7083 cp_parser_condition (cp_parser
* parser
)
7085 cp_decl_specifier_seq type_specifiers
;
7086 const char *saved_message
;
7088 /* Try the declaration first. */
7089 cp_parser_parse_tentatively (parser
);
7090 /* New types are not allowed in the type-specifier-seq for a
7092 saved_message
= parser
->type_definition_forbidden_message
;
7093 parser
->type_definition_forbidden_message
7094 = "types may not be defined in conditions";
7095 /* Parse the type-specifier-seq. */
7096 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
7098 /* Restore the saved message. */
7099 parser
->type_definition_forbidden_message
= saved_message
;
7100 /* If all is well, we might be looking at a declaration. */
7101 if (!cp_parser_error_occurred (parser
))
7104 tree asm_specification
;
7106 cp_declarator
*declarator
;
7107 tree initializer
= NULL_TREE
;
7109 /* Parse the declarator. */
7110 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
7111 /*ctor_dtor_or_conv_p=*/NULL
,
7112 /*parenthesized_p=*/NULL
,
7113 /*member_p=*/false);
7114 /* Parse the attributes. */
7115 attributes
= cp_parser_attributes_opt (parser
);
7116 /* Parse the asm-specification. */
7117 asm_specification
= cp_parser_asm_specification_opt (parser
);
7118 /* If the next token is not an `=', then we might still be
7119 looking at an expression. For example:
7123 looks like a decl-specifier-seq and a declarator -- but then
7124 there is no `=', so this is an expression. */
7125 cp_parser_require (parser
, CPP_EQ
, "`='");
7126 /* If we did see an `=', then we are looking at a declaration
7128 if (cp_parser_parse_definitely (parser
))
7131 bool non_constant_p
;
7133 /* Create the declaration. */
7134 decl
= start_decl (declarator
, &type_specifiers
,
7135 /*initialized_p=*/true,
7136 attributes
, /*prefix_attributes=*/NULL_TREE
,
7138 /* Parse the assignment-expression. */
7140 = cp_parser_constant_expression (parser
,
7141 /*allow_non_constant_p=*/true,
7143 if (!non_constant_p
)
7144 initializer
= fold_non_dependent_expr (initializer
);
7146 /* Process the initializer. */
7147 cp_finish_decl (decl
,
7148 initializer
, !non_constant_p
,
7150 LOOKUP_ONLYCONVERTING
);
7153 pop_scope (pushed_scope
);
7155 return convert_from_reference (decl
);
7158 /* If we didn't even get past the declarator successfully, we are
7159 definitely not looking at a declaration. */
7161 cp_parser_abort_tentative_parse (parser
);
7163 /* Otherwise, we are looking at an expression. */
7164 return cp_parser_expression (parser
, /*cast_p=*/false);
7167 /* We check for a ) immediately followed by ; with no whitespacing
7168 between. This is used to issue a warning for:
7176 as the semicolon is probably extraneous.
7178 On parse errors, the next token might not be a ), so do nothing in
7182 check_empty_body (cp_parser
* parser
, const char* type
)
7185 cp_token
*close_paren
;
7186 expanded_location close_loc
;
7187 expanded_location semi_loc
;
7189 close_paren
= cp_lexer_peek_token (parser
->lexer
);
7190 if (close_paren
->type
!= CPP_CLOSE_PAREN
)
7193 close_loc
= expand_location (close_paren
->location
);
7194 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7196 if (token
->type
!= CPP_SEMICOLON
7197 || (token
->flags
& PREV_WHITE
))
7200 semi_loc
= expand_location (token
->location
);
7201 if (close_loc
.line
== semi_loc
.line
7202 && close_loc
.column
+1 == semi_loc
.column
)
7203 warning (OPT_Wempty_body
,
7204 "suggest a space before %<;%> or explicit braces around empty "
7205 "body in %<%s%> statement",
7209 /* Parse an iteration-statement.
7211 iteration-statement:
7212 while ( condition ) statement
7213 do statement while ( expression ) ;
7214 for ( for-init-statement condition [opt] ; expression [opt] )
7217 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7220 cp_parser_iteration_statement (cp_parser
* parser
)
7225 unsigned char in_statement
;
7227 /* Peek at the next token. */
7228 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
7230 return error_mark_node
;
7232 /* Remember whether or not we are already within an iteration
7234 in_statement
= parser
->in_statement
;
7236 /* See what kind of keyword it is. */
7237 keyword
= token
->keyword
;
7244 /* Begin the while-statement. */
7245 statement
= begin_while_stmt ();
7246 /* Look for the `('. */
7247 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7248 /* Parse the condition. */
7249 condition
= cp_parser_condition (parser
);
7250 finish_while_stmt_cond (condition
, statement
);
7251 check_empty_body (parser
, "while");
7252 /* Look for the `)'. */
7253 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7254 /* Parse the dependent statement. */
7255 parser
->in_statement
= IN_ITERATION_STMT
;
7256 cp_parser_already_scoped_statement (parser
);
7257 parser
->in_statement
= in_statement
;
7258 /* We're done with the while-statement. */
7259 finish_while_stmt (statement
);
7267 /* Begin the do-statement. */
7268 statement
= begin_do_stmt ();
7269 /* Parse the body of the do-statement. */
7270 parser
->in_statement
= IN_ITERATION_STMT
;
7271 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7272 parser
->in_statement
= in_statement
;
7273 finish_do_body (statement
);
7274 /* Look for the `while' keyword. */
7275 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
7276 /* Look for the `('. */
7277 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7278 /* Parse the expression. */
7279 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
7280 /* We're done with the do-statement. */
7281 finish_do_stmt (expression
, statement
);
7282 /* Look for the `)'. */
7283 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7284 /* Look for the `;'. */
7285 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7291 tree condition
= NULL_TREE
;
7292 tree expression
= NULL_TREE
;
7294 /* Begin the for-statement. */
7295 statement
= begin_for_stmt ();
7296 /* Look for the `('. */
7297 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7298 /* Parse the initialization. */
7299 cp_parser_for_init_statement (parser
);
7300 finish_for_init_stmt (statement
);
7302 /* If there's a condition, process it. */
7303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7304 condition
= cp_parser_condition (parser
);
7305 finish_for_cond (condition
, statement
);
7306 /* Look for the `;'. */
7307 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7309 /* If there's an expression, process it. */
7310 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7311 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
7312 finish_for_expr (expression
, statement
);
7313 check_empty_body (parser
, "for");
7314 /* Look for the `)'. */
7315 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7317 /* Parse the body of the for-statement. */
7318 parser
->in_statement
= IN_ITERATION_STMT
;
7319 cp_parser_already_scoped_statement (parser
);
7320 parser
->in_statement
= in_statement
;
7322 /* We're done with the for-statement. */
7323 finish_for_stmt (statement
);
7328 cp_parser_error (parser
, "expected iteration-statement");
7329 statement
= error_mark_node
;
7336 /* Parse a for-init-statement.
7339 expression-statement
7340 simple-declaration */
7343 cp_parser_for_init_statement (cp_parser
* parser
)
7345 /* If the next token is a `;', then we have an empty
7346 expression-statement. Grammatically, this is also a
7347 simple-declaration, but an invalid one, because it does not
7348 declare anything. Therefore, if we did not handle this case
7349 specially, we would issue an error message about an invalid
7351 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7353 /* We're going to speculatively look for a declaration, falling back
7354 to an expression, if necessary. */
7355 cp_parser_parse_tentatively (parser
);
7356 /* Parse the declaration. */
7357 cp_parser_simple_declaration (parser
,
7358 /*function_definition_allowed_p=*/false);
7359 /* If the tentative parse failed, then we shall need to look for an
7360 expression-statement. */
7361 if (cp_parser_parse_definitely (parser
))
7365 cp_parser_expression_statement (parser
, false);
7368 /* Parse a jump-statement.
7373 return expression [opt] ;
7381 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7384 cp_parser_jump_statement (cp_parser
* parser
)
7386 tree statement
= error_mark_node
;
7389 unsigned char in_statement
;
7391 /* Peek at the next token. */
7392 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
7394 return error_mark_node
;
7396 /* See what kind of keyword it is. */
7397 keyword
= token
->keyword
;
7401 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
7402 switch (in_statement
)
7405 error ("break statement not within loop or switch");
7408 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
7409 || in_statement
== IN_ITERATION_STMT
);
7410 statement
= finish_break_stmt ();
7413 error ("invalid exit from OpenMP structured block");
7416 error ("break statement used with OpenMP for loop");
7419 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7423 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
7426 error ("continue statement not within a loop");
7428 case IN_ITERATION_STMT
:
7430 statement
= finish_continue_stmt ();
7433 error ("invalid exit from OpenMP structured block");
7438 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7445 /* If the next token is a `;', then there is no
7447 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7448 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
7451 /* Build the return-statement. */
7452 statement
= finish_return_stmt (expr
);
7453 /* Look for the final `;'. */
7454 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7459 /* Create the goto-statement. */
7460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
7462 /* Issue a warning about this use of a GNU extension. */
7464 pedwarn ("ISO C++ forbids computed gotos");
7465 /* Consume the '*' token. */
7466 cp_lexer_consume_token (parser
->lexer
);
7467 /* Parse the dependent expression. */
7468 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
7471 finish_goto_stmt (cp_parser_identifier (parser
));
7472 /* Look for the final `;'. */
7473 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7477 cp_parser_error (parser
, "expected jump-statement");
7484 /* Parse a declaration-statement.
7486 declaration-statement:
7487 block-declaration */
7490 cp_parser_declaration_statement (cp_parser
* parser
)
7494 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7495 p
= obstack_alloc (&declarator_obstack
, 0);
7497 /* Parse the block-declaration. */
7498 cp_parser_block_declaration (parser
, /*statement_p=*/true);
7500 /* Free any declarators allocated. */
7501 obstack_free (&declarator_obstack
, p
);
7503 /* Finish off the statement. */
7507 /* Some dependent statements (like `if (cond) statement'), are
7508 implicitly in their own scope. In other words, if the statement is
7509 a single statement (as opposed to a compound-statement), it is
7510 none-the-less treated as if it were enclosed in braces. Any
7511 declarations appearing in the dependent statement are out of scope
7512 after control passes that point. This function parses a statement,
7513 but ensures that is in its own scope, even if it is not a
7516 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7517 is a (possibly labeled) if statement which is not enclosed in
7518 braces and has an else clause. This is used to implement
7521 Returns the new statement. */
7524 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
7531 /* Mark if () ; with a special NOP_EXPR. */
7532 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7534 cp_lexer_consume_token (parser
->lexer
);
7535 statement
= add_stmt (build_empty_stmt ());
7537 /* if a compound is opened, we simply parse the statement directly. */
7538 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7539 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7540 /* If the token is not a `{', then we must take special action. */
7543 /* Create a compound-statement. */
7544 statement
= begin_compound_stmt (0);
7545 /* Parse the dependent-statement. */
7546 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
7547 /* Finish the dummy compound-statement. */
7548 finish_compound_stmt (statement
);
7551 /* Return the statement. */
7555 /* For some dependent statements (like `while (cond) statement'), we
7556 have already created a scope. Therefore, even if the dependent
7557 statement is a compound-statement, we do not want to create another
7561 cp_parser_already_scoped_statement (cp_parser
* parser
)
7563 /* If the token is a `{', then we must take special action. */
7564 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7565 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
7568 /* Avoid calling cp_parser_compound_statement, so that we
7569 don't create a new scope. Do everything else by hand. */
7570 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
7571 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7572 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7576 /* Declarations [gram.dcl.dcl] */
7578 /* Parse an optional declaration-sequence.
7582 declaration-seq declaration */
7585 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7591 token
= cp_lexer_peek_token (parser
->lexer
);
7593 if (token
->type
== CPP_CLOSE_BRACE
7594 || token
->type
== CPP_EOF
7595 || token
->type
== CPP_PRAGMA_EOL
)
7598 if (token
->type
== CPP_SEMICOLON
)
7600 /* A declaration consisting of a single semicolon is
7601 invalid. Allow it unless we're being pedantic. */
7602 cp_lexer_consume_token (parser
->lexer
);
7603 if (pedantic
&& !in_system_header
)
7604 pedwarn ("extra %<;%>");
7608 /* If we're entering or exiting a region that's implicitly
7609 extern "C", modify the lang context appropriately. */
7610 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7612 push_lang_context (lang_name_c
);
7613 parser
->implicit_extern_c
= true;
7615 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7617 pop_lang_context ();
7618 parser
->implicit_extern_c
= false;
7621 if (token
->type
== CPP_PRAGMA
)
7623 /* A top-level declaration can consist solely of a #pragma.
7624 A nested declaration cannot, so this is done here and not
7625 in cp_parser_declaration. (A #pragma at block scope is
7626 handled in cp_parser_statement.) */
7627 cp_parser_pragma (parser
, pragma_external
);
7631 /* Parse the declaration itself. */
7632 cp_parser_declaration (parser
);
7636 /* Parse a declaration.
7641 template-declaration
7642 explicit-instantiation
7643 explicit-specialization
7644 linkage-specification
7645 namespace-definition
7650 __extension__ declaration */
7653 cp_parser_declaration (cp_parser
* parser
)
7660 /* Check for the `__extension__' keyword. */
7661 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7663 /* Parse the qualified declaration. */
7664 cp_parser_declaration (parser
);
7665 /* Restore the PEDANTIC flag. */
7666 pedantic
= saved_pedantic
;
7671 /* Try to figure out what kind of declaration is present. */
7672 token1
= *cp_lexer_peek_token (parser
->lexer
);
7674 if (token1
.type
!= CPP_EOF
)
7675 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7678 token2
.type
= CPP_EOF
;
7679 token2
.keyword
= RID_MAX
;
7682 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7683 p
= obstack_alloc (&declarator_obstack
, 0);
7685 /* If the next token is `extern' and the following token is a string
7686 literal, then we have a linkage specification. */
7687 if (token1
.keyword
== RID_EXTERN
7688 && cp_parser_is_string_literal (&token2
))
7689 cp_parser_linkage_specification (parser
);
7690 /* If the next token is `template', then we have either a template
7691 declaration, an explicit instantiation, or an explicit
7693 else if (token1
.keyword
== RID_TEMPLATE
)
7695 /* `template <>' indicates a template specialization. */
7696 if (token2
.type
== CPP_LESS
7697 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7698 cp_parser_explicit_specialization (parser
);
7699 /* `template <' indicates a template declaration. */
7700 else if (token2
.type
== CPP_LESS
)
7701 cp_parser_template_declaration (parser
, /*member_p=*/false);
7702 /* Anything else must be an explicit instantiation. */
7704 cp_parser_explicit_instantiation (parser
);
7706 /* If the next token is `export', then we have a template
7708 else if (token1
.keyword
== RID_EXPORT
)
7709 cp_parser_template_declaration (parser
, /*member_p=*/false);
7710 /* If the next token is `extern', 'static' or 'inline' and the one
7711 after that is `template', we have a GNU extended explicit
7712 instantiation directive. */
7713 else if (cp_parser_allow_gnu_extensions_p (parser
)
7714 && (token1
.keyword
== RID_EXTERN
7715 || token1
.keyword
== RID_STATIC
7716 || token1
.keyword
== RID_INLINE
)
7717 && token2
.keyword
== RID_TEMPLATE
)
7718 cp_parser_explicit_instantiation (parser
);
7719 /* If the next token is `namespace', check for a named or unnamed
7720 namespace definition. */
7721 else if (token1
.keyword
== RID_NAMESPACE
7722 && (/* A named namespace definition. */
7723 (token2
.type
== CPP_NAME
7724 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7726 /* An unnamed namespace definition. */
7727 || token2
.type
== CPP_OPEN_BRACE
7728 || token2
.keyword
== RID_ATTRIBUTE
))
7729 cp_parser_namespace_definition (parser
);
7730 /* An inline (associated) namespace definition. */
7731 else if (token1
.keyword
== RID_INLINE
7732 && token2
.keyword
== RID_NAMESPACE
)
7733 cp_parser_namespace_definition (parser
);
7734 /* Objective-C++ declaration/definition. */
7735 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7736 cp_parser_objc_declaration (parser
);
7737 /* We must have either a block declaration or a function
7740 /* Try to parse a block-declaration, or a function-definition. */
7741 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7743 /* Free any declarators allocated. */
7744 obstack_free (&declarator_obstack
, p
);
7747 /* Parse a block-declaration.
7752 namespace-alias-definition
7759 __extension__ block-declaration
7764 static_assert-declaration
7766 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7767 part of a declaration-statement. */
7770 cp_parser_block_declaration (cp_parser
*parser
,
7776 /* Check for the `__extension__' keyword. */
7777 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7779 /* Parse the qualified declaration. */
7780 cp_parser_block_declaration (parser
, statement_p
);
7781 /* Restore the PEDANTIC flag. */
7782 pedantic
= saved_pedantic
;
7787 /* Peek at the next token to figure out which kind of declaration is
7789 token1
= cp_lexer_peek_token (parser
->lexer
);
7791 /* If the next keyword is `asm', we have an asm-definition. */
7792 if (token1
->keyword
== RID_ASM
)
7795 cp_parser_commit_to_tentative_parse (parser
);
7796 cp_parser_asm_definition (parser
);
7798 /* If the next keyword is `namespace', we have a
7799 namespace-alias-definition. */
7800 else if (token1
->keyword
== RID_NAMESPACE
)
7801 cp_parser_namespace_alias_definition (parser
);
7802 /* If the next keyword is `using', we have either a
7803 using-declaration or a using-directive. */
7804 else if (token1
->keyword
== RID_USING
)
7809 cp_parser_commit_to_tentative_parse (parser
);
7810 /* If the token after `using' is `namespace', then we have a
7812 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7813 if (token2
->keyword
== RID_NAMESPACE
)
7814 cp_parser_using_directive (parser
);
7815 /* Otherwise, it's a using-declaration. */
7817 cp_parser_using_declaration (parser
,
7818 /*access_declaration_p=*/false);
7820 /* If the next keyword is `__label__' we have a misplaced label
7822 else if (token1
->keyword
== RID_LABEL
)
7824 cp_lexer_consume_token (parser
->lexer
);
7825 error ("%<__label__%> not at the beginning of a block");
7826 cp_parser_skip_to_end_of_statement (parser
);
7827 /* If the next token is now a `;', consume it. */
7828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7829 cp_lexer_consume_token (parser
->lexer
);
7831 /* If the next token is `static_assert' we have a static assertion. */
7832 else if (token1
->keyword
== RID_STATIC_ASSERT
)
7833 cp_parser_static_assert (parser
, /*member_p=*/false);
7834 /* Anything else must be a simple-declaration. */
7836 cp_parser_simple_declaration (parser
, !statement_p
);
7839 /* Parse a simple-declaration.
7842 decl-specifier-seq [opt] init-declarator-list [opt] ;
7844 init-declarator-list:
7846 init-declarator-list , init-declarator
7848 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7849 function-definition as a simple-declaration. */
7852 cp_parser_simple_declaration (cp_parser
* parser
,
7853 bool function_definition_allowed_p
)
7855 cp_decl_specifier_seq decl_specifiers
;
7856 int declares_class_or_enum
;
7857 bool saw_declarator
;
7859 /* Defer access checks until we know what is being declared; the
7860 checks for names appearing in the decl-specifier-seq should be
7861 done as if we were in the scope of the thing being declared. */
7862 push_deferring_access_checks (dk_deferred
);
7864 /* Parse the decl-specifier-seq. We have to keep track of whether
7865 or not the decl-specifier-seq declares a named class or
7866 enumeration type, since that is the only case in which the
7867 init-declarator-list is allowed to be empty.
7871 In a simple-declaration, the optional init-declarator-list can be
7872 omitted only when declaring a class or enumeration, that is when
7873 the decl-specifier-seq contains either a class-specifier, an
7874 elaborated-type-specifier, or an enum-specifier. */
7875 cp_parser_decl_specifier_seq (parser
,
7876 CP_PARSER_FLAGS_OPTIONAL
,
7878 &declares_class_or_enum
);
7879 /* We no longer need to defer access checks. */
7880 stop_deferring_access_checks ();
7882 /* In a block scope, a valid declaration must always have a
7883 decl-specifier-seq. By not trying to parse declarators, we can
7884 resolve the declaration/expression ambiguity more quickly. */
7885 if (!function_definition_allowed_p
7886 && !decl_specifiers
.any_specifiers_p
)
7888 cp_parser_error (parser
, "expected declaration");
7892 /* If the next two tokens are both identifiers, the code is
7893 erroneous. The usual cause of this situation is code like:
7897 where "T" should name a type -- but does not. */
7898 if (!decl_specifiers
.type
7899 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7901 /* If parsing tentatively, we should commit; we really are
7902 looking at a declaration. */
7903 cp_parser_commit_to_tentative_parse (parser
);
7908 /* If we have seen at least one decl-specifier, and the next token
7909 is not a parenthesis, then we must be looking at a declaration.
7910 (After "int (" we might be looking at a functional cast.) */
7911 if (decl_specifiers
.any_specifiers_p
7912 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7913 cp_parser_commit_to_tentative_parse (parser
);
7915 /* Keep going until we hit the `;' at the end of the simple
7917 saw_declarator
= false;
7918 while (cp_lexer_next_token_is_not (parser
->lexer
,
7922 bool function_definition_p
;
7927 /* If we are processing next declarator, coma is expected */
7928 token
= cp_lexer_peek_token (parser
->lexer
);
7929 gcc_assert (token
->type
== CPP_COMMA
);
7930 cp_lexer_consume_token (parser
->lexer
);
7933 saw_declarator
= true;
7935 /* Parse the init-declarator. */
7936 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7938 function_definition_allowed_p
,
7940 declares_class_or_enum
,
7941 &function_definition_p
);
7942 /* If an error occurred while parsing tentatively, exit quickly.
7943 (That usually happens when in the body of a function; each
7944 statement is treated as a declaration-statement until proven
7946 if (cp_parser_error_occurred (parser
))
7948 /* Handle function definitions specially. */
7949 if (function_definition_p
)
7951 /* If the next token is a `,', then we are probably
7952 processing something like:
7956 which is erroneous. */
7957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7958 error ("mixing declarations and function-definitions is forbidden");
7959 /* Otherwise, we're done with the list of declarators. */
7962 pop_deferring_access_checks ();
7966 /* The next token should be either a `,' or a `;'. */
7967 token
= cp_lexer_peek_token (parser
->lexer
);
7968 /* If it's a `,', there are more declarators to come. */
7969 if (token
->type
== CPP_COMMA
)
7970 /* will be consumed next time around */;
7971 /* If it's a `;', we are done. */
7972 else if (token
->type
== CPP_SEMICOLON
)
7974 /* Anything else is an error. */
7977 /* If we have already issued an error message we don't need
7978 to issue another one. */
7979 if (decl
!= error_mark_node
7980 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7981 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7982 /* Skip tokens until we reach the end of the statement. */
7983 cp_parser_skip_to_end_of_statement (parser
);
7984 /* If the next token is now a `;', consume it. */
7985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7986 cp_lexer_consume_token (parser
->lexer
);
7989 /* After the first time around, a function-definition is not
7990 allowed -- even if it was OK at first. For example:
7995 function_definition_allowed_p
= false;
7998 /* Issue an error message if no declarators are present, and the
7999 decl-specifier-seq does not itself declare a class or
8001 if (!saw_declarator
)
8003 if (cp_parser_declares_only_class_p (parser
))
8004 shadow_tag (&decl_specifiers
);
8005 /* Perform any deferred access checks. */
8006 perform_deferred_access_checks ();
8009 /* Consume the `;'. */
8010 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8013 pop_deferring_access_checks ();
8016 /* Parse a decl-specifier-seq.
8019 decl-specifier-seq [opt] decl-specifier
8022 storage-class-specifier
8033 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8035 The parser flags FLAGS is used to control type-specifier parsing.
8037 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8040 1: one of the decl-specifiers is an elaborated-type-specifier
8041 (i.e., a type declaration)
8042 2: one of the decl-specifiers is an enum-specifier or a
8043 class-specifier (i.e., a type definition)
8048 cp_parser_decl_specifier_seq (cp_parser
* parser
,
8049 cp_parser_flags flags
,
8050 cp_decl_specifier_seq
*decl_specs
,
8051 int* declares_class_or_enum
)
8053 bool constructor_possible_p
= !parser
->in_declarator_p
;
8055 /* Clear DECL_SPECS. */
8056 clear_decl_specs (decl_specs
);
8058 /* Assume no class or enumeration type is declared. */
8059 *declares_class_or_enum
= 0;
8061 /* Keep reading specifiers until there are no more to read. */
8065 bool found_decl_spec
;
8068 /* Peek at the next token. */
8069 token
= cp_lexer_peek_token (parser
->lexer
);
8070 /* Handle attributes. */
8071 if (token
->keyword
== RID_ATTRIBUTE
)
8073 /* Parse the attributes. */
8074 decl_specs
->attributes
8075 = chainon (decl_specs
->attributes
,
8076 cp_parser_attributes_opt (parser
));
8079 /* Assume we will find a decl-specifier keyword. */
8080 found_decl_spec
= true;
8081 /* If the next token is an appropriate keyword, we can simply
8082 add it to the list. */
8083 switch (token
->keyword
)
8088 if (!at_class_scope_p ())
8090 error ("%<friend%> used outside of class");
8091 cp_lexer_purge_token (parser
->lexer
);
8095 ++decl_specs
->specs
[(int) ds_friend
];
8096 /* Consume the token. */
8097 cp_lexer_consume_token (parser
->lexer
);
8101 /* function-specifier:
8108 cp_parser_function_specifier_opt (parser
, decl_specs
);
8114 ++decl_specs
->specs
[(int) ds_typedef
];
8115 /* Consume the token. */
8116 cp_lexer_consume_token (parser
->lexer
);
8117 /* A constructor declarator cannot appear in a typedef. */
8118 constructor_possible_p
= false;
8119 /* The "typedef" keyword can only occur in a declaration; we
8120 may as well commit at this point. */
8121 cp_parser_commit_to_tentative_parse (parser
);
8123 if (decl_specs
->storage_class
!= sc_none
)
8124 decl_specs
->conflicting_specifiers_p
= true;
8127 /* storage-class-specifier:
8141 /* Consume the token. */
8142 cp_lexer_consume_token (parser
->lexer
);
8143 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
8146 /* Consume the token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 ++decl_specs
->specs
[(int) ds_thread
];
8152 /* We did not yet find a decl-specifier yet. */
8153 found_decl_spec
= false;
8157 /* Constructors are a special case. The `S' in `S()' is not a
8158 decl-specifier; it is the beginning of the declarator. */
8161 && constructor_possible_p
8162 && (cp_parser_constructor_declarator_p
8163 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
8165 /* If we don't have a DECL_SPEC yet, then we must be looking at
8166 a type-specifier. */
8167 if (!found_decl_spec
&& !constructor_p
)
8169 int decl_spec_declares_class_or_enum
;
8170 bool is_cv_qualifier
;
8174 = cp_parser_type_specifier (parser
, flags
,
8176 /*is_declaration=*/true,
8177 &decl_spec_declares_class_or_enum
,
8180 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
8182 /* If this type-specifier referenced a user-defined type
8183 (a typedef, class-name, etc.), then we can't allow any
8184 more such type-specifiers henceforth.
8188 The longest sequence of decl-specifiers that could
8189 possibly be a type name is taken as the
8190 decl-specifier-seq of a declaration. The sequence shall
8191 be self-consistent as described below.
8195 As a general rule, at most one type-specifier is allowed
8196 in the complete decl-specifier-seq of a declaration. The
8197 only exceptions are the following:
8199 -- const or volatile can be combined with any other
8202 -- signed or unsigned can be combined with char, long,
8210 void g (const int Pc);
8212 Here, Pc is *not* part of the decl-specifier seq; it's
8213 the declarator. Therefore, once we see a type-specifier
8214 (other than a cv-qualifier), we forbid any additional
8215 user-defined types. We *do* still allow things like `int
8216 int' to be considered a decl-specifier-seq, and issue the
8217 error message later. */
8218 if (type_spec
&& !is_cv_qualifier
)
8219 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
8220 /* A constructor declarator cannot follow a type-specifier. */
8223 constructor_possible_p
= false;
8224 found_decl_spec
= true;
8228 /* If we still do not have a DECL_SPEC, then there are no more
8230 if (!found_decl_spec
)
8233 decl_specs
->any_specifiers_p
= true;
8234 /* After we see one decl-specifier, further decl-specifiers are
8236 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
8239 cp_parser_check_decl_spec (decl_specs
);
8241 /* Don't allow a friend specifier with a class definition. */
8242 if (decl_specs
->specs
[(int) ds_friend
] != 0
8243 && (*declares_class_or_enum
& 2))
8244 error ("class definition may not be declared a friend");
8247 /* Parse an (optional) storage-class-specifier.
8249 storage-class-specifier:
8258 storage-class-specifier:
8261 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8264 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
8266 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
8274 /* Consume the token. */
8275 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
8282 /* Parse an (optional) function-specifier.
8289 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8290 Updates DECL_SPECS, if it is non-NULL. */
8293 cp_parser_function_specifier_opt (cp_parser
* parser
,
8294 cp_decl_specifier_seq
*decl_specs
)
8296 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
8300 ++decl_specs
->specs
[(int) ds_inline
];
8304 /* 14.5.2.3 [temp.mem]
8306 A member function template shall not be virtual. */
8307 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8308 error ("templates may not be %<virtual%>");
8309 else if (decl_specs
)
8310 ++decl_specs
->specs
[(int) ds_virtual
];
8315 ++decl_specs
->specs
[(int) ds_explicit
];
8322 /* Consume the token. */
8323 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
8326 /* Parse a linkage-specification.
8328 linkage-specification:
8329 extern string-literal { declaration-seq [opt] }
8330 extern string-literal declaration */
8333 cp_parser_linkage_specification (cp_parser
* parser
)
8337 /* Look for the `extern' keyword. */
8338 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
8340 /* Look for the string-literal. */
8341 linkage
= cp_parser_string_literal (parser
, false, false);
8343 /* Transform the literal into an identifier. If the literal is a
8344 wide-character string, or contains embedded NULs, then we can't
8345 handle it as the user wants. */
8346 if (strlen (TREE_STRING_POINTER (linkage
))
8347 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
8349 cp_parser_error (parser
, "invalid linkage-specification");
8350 /* Assume C++ linkage. */
8351 linkage
= lang_name_cplusplus
;
8354 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
8356 /* We're now using the new linkage. */
8357 push_lang_context (linkage
);
8359 /* If the next token is a `{', then we're using the first
8361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8363 /* Consume the `{' token. */
8364 cp_lexer_consume_token (parser
->lexer
);
8365 /* Parse the declarations. */
8366 cp_parser_declaration_seq_opt (parser
);
8367 /* Look for the closing `}'. */
8368 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8370 /* Otherwise, there's just one declaration. */
8373 bool saved_in_unbraced_linkage_specification_p
;
8375 saved_in_unbraced_linkage_specification_p
8376 = parser
->in_unbraced_linkage_specification_p
;
8377 parser
->in_unbraced_linkage_specification_p
= true;
8378 cp_parser_declaration (parser
);
8379 parser
->in_unbraced_linkage_specification_p
8380 = saved_in_unbraced_linkage_specification_p
;
8383 /* We're done with the linkage-specification. */
8384 pop_lang_context ();
8387 /* Parse a static_assert-declaration.
8389 static_assert-declaration:
8390 static_assert ( constant-expression , string-literal ) ;
8392 If MEMBER_P, this static_assert is a class member. */
8395 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
8400 location_t saved_loc
;
8402 /* Peek at the `static_assert' token so we can keep track of exactly
8403 where the static assertion started. */
8404 token
= cp_lexer_peek_token (parser
->lexer
);
8405 saved_loc
= token
->location
;
8407 /* Look for the `static_assert' keyword. */
8408 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
8412 /* We know we are in a static assertion; commit to any tentative
8414 if (cp_parser_parsing_tentatively (parser
))
8415 cp_parser_commit_to_tentative_parse (parser
);
8417 /* Parse the `(' starting the static assertion condition. */
8418 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
8420 /* Parse the constant-expression. */
8422 cp_parser_constant_expression (parser
,
8423 /*allow_non_constant_p=*/false,
8424 /*non_constant_p=*/NULL
);
8426 /* Parse the separating `,'. */
8427 cp_parser_require (parser
, CPP_COMMA
, "`,'");
8429 /* Parse the string-literal message. */
8430 message
= cp_parser_string_literal (parser
,
8431 /*translate=*/false,
8434 /* A `)' completes the static assertion. */
8435 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
8436 cp_parser_skip_to_closing_parenthesis (parser
,
8437 /*recovering=*/true,
8439 /*consume_paren=*/true);
8441 /* A semicolon terminates the declaration. */
8442 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8444 /* Complete the static assertion, which may mean either processing
8445 the static assert now or saving it for template instantiation. */
8446 finish_static_assert (condition
, message
, saved_loc
, member_p
);
8449 /* Parse a `decltype' type. Returns the type.
8451 simple-type-specifier:
8452 decltype ( expression ) */
8455 cp_parser_decltype (cp_parser
*parser
)
8458 bool id_expression_or_member_access_p
= false;
8459 const char *saved_message
;
8460 bool saved_integral_constant_expression_p
;
8461 bool saved_non_integral_constant_expression_p
;
8463 /* Look for the `decltype' token. */
8464 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, "`decltype'"))
8465 return error_mark_node
;
8467 /* Types cannot be defined in a `decltype' expression. Save away the
8469 saved_message
= parser
->type_definition_forbidden_message
;
8471 /* And create the new one. */
8472 parser
->type_definition_forbidden_message
8473 = "types may not be defined in `decltype' expressions";
8475 /* The restrictions on constant-expressions do not apply inside
8476 decltype expressions. */
8477 saved_integral_constant_expression_p
8478 = parser
->integral_constant_expression_p
;
8479 saved_non_integral_constant_expression_p
8480 = parser
->non_integral_constant_expression_p
;
8481 parser
->integral_constant_expression_p
= false;
8483 /* Do not actually evaluate the expression. */
8486 /* Parse the opening `('. */
8487 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
8488 return error_mark_node
;
8490 /* First, try parsing an id-expression. */
8491 cp_parser_parse_tentatively (parser
);
8492 expr
= cp_parser_id_expression (parser
,
8493 /*template_keyword_p=*/false,
8494 /*check_dependency_p=*/true,
8495 /*template_p=*/NULL
,
8496 /*declarator_p=*/false,
8497 /*optional_p=*/false);
8499 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
8501 bool non_integral_constant_expression_p
= false;
8502 tree id_expression
= expr
;
8504 const char *error_msg
;
8506 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
8507 /* Lookup the name we got back from the id-expression. */
8508 expr
= cp_parser_lookup_name (parser
, expr
,
8510 /*is_template=*/false,
8511 /*is_namespace=*/false,
8512 /*check_dependency=*/true,
8513 /*ambiguous_decls=*/NULL
);
8516 && expr
!= error_mark_node
8517 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
8518 && TREE_CODE (expr
) != TYPE_DECL
8519 && (TREE_CODE (expr
) != BIT_NOT_EXPR
8520 || !TYPE_P (TREE_OPERAND (expr
, 0)))
8521 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8523 /* Complete lookup of the id-expression. */
8524 expr
= (finish_id_expression
8525 (id_expression
, expr
, parser
->scope
, &idk
,
8526 /*integral_constant_expression_p=*/false,
8527 /*allow_non_integral_constant_expression_p=*/true,
8528 &non_integral_constant_expression_p
,
8529 /*template_p=*/false,
8531 /*address_p=*/false,
8532 /*template_arg_p=*/false,
8535 if (expr
== error_mark_node
)
8536 /* We found an id-expression, but it was something that we
8537 should not have found. This is an error, not something
8538 we can recover from, so note that we found an
8539 id-expression and we'll recover as gracefully as
8541 id_expression_or_member_access_p
= true;
8545 && expr
!= error_mark_node
8546 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8547 /* We have an id-expression. */
8548 id_expression_or_member_access_p
= true;
8551 if (!id_expression_or_member_access_p
)
8553 /* Abort the id-expression parse. */
8554 cp_parser_abort_tentative_parse (parser
);
8556 /* Parsing tentatively, again. */
8557 cp_parser_parse_tentatively (parser
);
8559 /* Parse a class member access. */
8560 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
8562 /*member_access_only_p=*/true);
8565 && expr
!= error_mark_node
8566 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8567 /* We have an id-expression. */
8568 id_expression_or_member_access_p
= true;
8571 if (id_expression_or_member_access_p
)
8572 /* We have parsed the complete id-expression or member access. */
8573 cp_parser_parse_definitely (parser
);
8576 /* Abort our attempt to parse an id-expression or member access
8578 cp_parser_abort_tentative_parse (parser
);
8580 /* Parse a full expression. */
8581 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
8584 /* Go back to evaluating expressions. */
8587 /* Restore the old message and the integral constant expression
8589 parser
->type_definition_forbidden_message
= saved_message
;
8590 parser
->integral_constant_expression_p
8591 = saved_integral_constant_expression_p
;
8592 parser
->non_integral_constant_expression_p
8593 = saved_non_integral_constant_expression_p
;
8595 if (expr
== error_mark_node
)
8597 /* Skip everything up to the closing `)'. */
8598 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8599 /*consume_paren=*/true);
8600 return error_mark_node
;
8603 /* Parse to the closing `)'. */
8604 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
8606 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8607 /*consume_paren=*/true);
8608 return error_mark_node
;
8611 return finish_decltype_type (expr
, id_expression_or_member_access_p
);
8614 /* Special member functions [gram.special] */
8616 /* Parse a conversion-function-id.
8618 conversion-function-id:
8619 operator conversion-type-id
8621 Returns an IDENTIFIER_NODE representing the operator. */
8624 cp_parser_conversion_function_id (cp_parser
* parser
)
8628 tree saved_qualifying_scope
;
8629 tree saved_object_scope
;
8630 tree pushed_scope
= NULL_TREE
;
8632 /* Look for the `operator' token. */
8633 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8634 return error_mark_node
;
8635 /* When we parse the conversion-type-id, the current scope will be
8636 reset. However, we need that information in able to look up the
8637 conversion function later, so we save it here. */
8638 saved_scope
= parser
->scope
;
8639 saved_qualifying_scope
= parser
->qualifying_scope
;
8640 saved_object_scope
= parser
->object_scope
;
8641 /* We must enter the scope of the class so that the names of
8642 entities declared within the class are available in the
8643 conversion-type-id. For example, consider:
8650 S::operator I() { ... }
8652 In order to see that `I' is a type-name in the definition, we
8653 must be in the scope of `S'. */
8655 pushed_scope
= push_scope (saved_scope
);
8656 /* Parse the conversion-type-id. */
8657 type
= cp_parser_conversion_type_id (parser
);
8658 /* Leave the scope of the class, if any. */
8660 pop_scope (pushed_scope
);
8661 /* Restore the saved scope. */
8662 parser
->scope
= saved_scope
;
8663 parser
->qualifying_scope
= saved_qualifying_scope
;
8664 parser
->object_scope
= saved_object_scope
;
8665 /* If the TYPE is invalid, indicate failure. */
8666 if (type
== error_mark_node
)
8667 return error_mark_node
;
8668 return mangle_conv_op_name_for_type (type
);
8671 /* Parse a conversion-type-id:
8674 type-specifier-seq conversion-declarator [opt]
8676 Returns the TYPE specified. */
8679 cp_parser_conversion_type_id (cp_parser
* parser
)
8682 cp_decl_specifier_seq type_specifiers
;
8683 cp_declarator
*declarator
;
8684 tree type_specified
;
8686 /* Parse the attributes. */
8687 attributes
= cp_parser_attributes_opt (parser
);
8688 /* Parse the type-specifiers. */
8689 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
8691 /* If that didn't work, stop. */
8692 if (type_specifiers
.type
== error_mark_node
)
8693 return error_mark_node
;
8694 /* Parse the conversion-declarator. */
8695 declarator
= cp_parser_conversion_declarator_opt (parser
);
8697 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
8698 /*initialized=*/0, &attributes
);
8700 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
8701 return type_specified
;
8704 /* Parse an (optional) conversion-declarator.
8706 conversion-declarator:
8707 ptr-operator conversion-declarator [opt]
8711 static cp_declarator
*
8712 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
8714 enum tree_code code
;
8716 cp_cv_quals cv_quals
;
8718 /* We don't know if there's a ptr-operator next, or not. */
8719 cp_parser_parse_tentatively (parser
);
8720 /* Try the ptr-operator. */
8721 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
8722 /* If it worked, look for more conversion-declarators. */
8723 if (cp_parser_parse_definitely (parser
))
8725 cp_declarator
*declarator
;
8727 /* Parse another optional declarator. */
8728 declarator
= cp_parser_conversion_declarator_opt (parser
);
8730 return cp_parser_make_indirect_declarator
8731 (code
, class_type
, cv_quals
, declarator
);
8737 /* Parse an (optional) ctor-initializer.
8740 : mem-initializer-list
8742 Returns TRUE iff the ctor-initializer was actually present. */
8745 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
8747 /* If the next token is not a `:', then there is no
8748 ctor-initializer. */
8749 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8751 /* Do default initialization of any bases and members. */
8752 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8753 finish_mem_initializers (NULL_TREE
);
8758 /* Consume the `:' token. */
8759 cp_lexer_consume_token (parser
->lexer
);
8760 /* And the mem-initializer-list. */
8761 cp_parser_mem_initializer_list (parser
);
8766 /* Parse a mem-initializer-list.
8768 mem-initializer-list:
8769 mem-initializer ... [opt]
8770 mem-initializer ... [opt] , mem-initializer-list */
8773 cp_parser_mem_initializer_list (cp_parser
* parser
)
8775 tree mem_initializer_list
= NULL_TREE
;
8777 /* Let the semantic analysis code know that we are starting the
8778 mem-initializer-list. */
8779 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
8780 error ("only constructors take base initializers");
8782 /* Loop through the list. */
8785 tree mem_initializer
;
8787 /* Parse the mem-initializer. */
8788 mem_initializer
= cp_parser_mem_initializer (parser
);
8789 /* If the next token is a `...', we're expanding member initializers. */
8790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8792 /* Consume the `...'. */
8793 cp_lexer_consume_token (parser
->lexer
);
8795 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8796 can be expanded but members cannot. */
8797 if (mem_initializer
!= error_mark_node
8798 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
8800 error ("cannot expand initializer for member %<%D%>",
8801 TREE_PURPOSE (mem_initializer
));
8802 mem_initializer
= error_mark_node
;
8805 /* Construct the pack expansion type. */
8806 if (mem_initializer
!= error_mark_node
)
8807 mem_initializer
= make_pack_expansion (mem_initializer
);
8809 /* Add it to the list, unless it was erroneous. */
8810 if (mem_initializer
!= error_mark_node
)
8812 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
8813 mem_initializer_list
= mem_initializer
;
8815 /* If the next token is not a `,', we're done. */
8816 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8818 /* Consume the `,' token. */
8819 cp_lexer_consume_token (parser
->lexer
);
8822 /* Perform semantic analysis. */
8823 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8824 finish_mem_initializers (mem_initializer_list
);
8827 /* Parse a mem-initializer.
8830 mem-initializer-id ( expression-list [opt] )
8835 ( expression-list [opt] )
8837 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8838 class) or FIELD_DECL (for a non-static data member) to initialize;
8839 the TREE_VALUE is the expression-list. An empty initialization
8840 list is represented by void_list_node. */
8843 cp_parser_mem_initializer (cp_parser
* parser
)
8845 tree mem_initializer_id
;
8846 tree expression_list
;
8849 /* Find out what is being initialized. */
8850 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8852 pedwarn ("anachronistic old-style base class initializer");
8853 mem_initializer_id
= NULL_TREE
;
8856 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8857 member
= expand_member_init (mem_initializer_id
);
8858 if (member
&& !DECL_P (member
))
8859 in_base_initializer
= 1;
8862 = cp_parser_parenthesized_expression_list (parser
, false,
8864 /*allow_expansion_p=*/true,
8865 /*non_constant_p=*/NULL
);
8866 if (expression_list
== error_mark_node
)
8867 return error_mark_node
;
8868 if (!expression_list
)
8869 expression_list
= void_type_node
;
8871 in_base_initializer
= 0;
8873 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8876 /* Parse a mem-initializer-id.
8879 :: [opt] nested-name-specifier [opt] class-name
8882 Returns a TYPE indicating the class to be initializer for the first
8883 production. Returns an IDENTIFIER_NODE indicating the data member
8884 to be initialized for the second production. */
8887 cp_parser_mem_initializer_id (cp_parser
* parser
)
8889 bool global_scope_p
;
8890 bool nested_name_specifier_p
;
8891 bool template_p
= false;
8894 /* `typename' is not allowed in this context ([temp.res]). */
8895 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8897 error ("keyword %<typename%> not allowed in this context (a qualified "
8898 "member initializer is implicitly a type)");
8899 cp_lexer_consume_token (parser
->lexer
);
8901 /* Look for the optional `::' operator. */
8903 = (cp_parser_global_scope_opt (parser
,
8904 /*current_scope_valid_p=*/false)
8906 /* Look for the optional nested-name-specifier. The simplest way to
8911 The keyword `typename' is not permitted in a base-specifier or
8912 mem-initializer; in these contexts a qualified name that
8913 depends on a template-parameter is implicitly assumed to be a
8916 is to assume that we have seen the `typename' keyword at this
8918 nested_name_specifier_p
8919 = (cp_parser_nested_name_specifier_opt (parser
,
8920 /*typename_keyword_p=*/true,
8921 /*check_dependency_p=*/true,
8923 /*is_declaration=*/true)
8925 if (nested_name_specifier_p
)
8926 template_p
= cp_parser_optional_template_keyword (parser
);
8927 /* If there is a `::' operator or a nested-name-specifier, then we
8928 are definitely looking for a class-name. */
8929 if (global_scope_p
|| nested_name_specifier_p
)
8930 return cp_parser_class_name (parser
,
8931 /*typename_keyword_p=*/true,
8932 /*template_keyword_p=*/template_p
,
8934 /*check_dependency_p=*/true,
8935 /*class_head_p=*/false,
8936 /*is_declaration=*/true);
8937 /* Otherwise, we could also be looking for an ordinary identifier. */
8938 cp_parser_parse_tentatively (parser
);
8939 /* Try a class-name. */
8940 id
= cp_parser_class_name (parser
,
8941 /*typename_keyword_p=*/true,
8942 /*template_keyword_p=*/false,
8944 /*check_dependency_p=*/true,
8945 /*class_head_p=*/false,
8946 /*is_declaration=*/true);
8947 /* If we found one, we're done. */
8948 if (cp_parser_parse_definitely (parser
))
8950 /* Otherwise, look for an ordinary identifier. */
8951 return cp_parser_identifier (parser
);
8954 /* Overloading [gram.over] */
8956 /* Parse an operator-function-id.
8958 operator-function-id:
8961 Returns an IDENTIFIER_NODE for the operator which is a
8962 human-readable spelling of the identifier, e.g., `operator +'. */
8965 cp_parser_operator_function_id (cp_parser
* parser
)
8967 /* Look for the `operator' keyword. */
8968 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8969 return error_mark_node
;
8970 /* And then the name of the operator itself. */
8971 return cp_parser_operator (parser
);
8974 /* Parse an operator.
8977 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8978 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8979 || ++ -- , ->* -> () []
8986 Returns an IDENTIFIER_NODE for the operator which is a
8987 human-readable spelling of the identifier, e.g., `operator +'. */
8990 cp_parser_operator (cp_parser
* parser
)
8992 tree id
= NULL_TREE
;
8995 /* Peek at the next token. */
8996 token
= cp_lexer_peek_token (parser
->lexer
);
8997 /* Figure out which operator we have. */
8998 switch (token
->type
)
9004 /* The keyword should be either `new' or `delete'. */
9005 if (token
->keyword
== RID_NEW
)
9007 else if (token
->keyword
== RID_DELETE
)
9012 /* Consume the `new' or `delete' token. */
9013 cp_lexer_consume_token (parser
->lexer
);
9015 /* Peek at the next token. */
9016 token
= cp_lexer_peek_token (parser
->lexer
);
9017 /* If it's a `[' token then this is the array variant of the
9019 if (token
->type
== CPP_OPEN_SQUARE
)
9021 /* Consume the `[' token. */
9022 cp_lexer_consume_token (parser
->lexer
);
9023 /* Look for the `]' token. */
9024 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
9025 id
= ansi_opname (op
== NEW_EXPR
9026 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
9028 /* Otherwise, we have the non-array variant. */
9030 id
= ansi_opname (op
);
9036 id
= ansi_opname (PLUS_EXPR
);
9040 id
= ansi_opname (MINUS_EXPR
);
9044 id
= ansi_opname (MULT_EXPR
);
9048 id
= ansi_opname (TRUNC_DIV_EXPR
);
9052 id
= ansi_opname (TRUNC_MOD_EXPR
);
9056 id
= ansi_opname (BIT_XOR_EXPR
);
9060 id
= ansi_opname (BIT_AND_EXPR
);
9064 id
= ansi_opname (BIT_IOR_EXPR
);
9068 id
= ansi_opname (BIT_NOT_EXPR
);
9072 id
= ansi_opname (TRUTH_NOT_EXPR
);
9076 id
= ansi_assopname (NOP_EXPR
);
9080 id
= ansi_opname (LT_EXPR
);
9084 id
= ansi_opname (GT_EXPR
);
9088 id
= ansi_assopname (PLUS_EXPR
);
9092 id
= ansi_assopname (MINUS_EXPR
);
9096 id
= ansi_assopname (MULT_EXPR
);
9100 id
= ansi_assopname (TRUNC_DIV_EXPR
);
9104 id
= ansi_assopname (TRUNC_MOD_EXPR
);
9108 id
= ansi_assopname (BIT_XOR_EXPR
);
9112 id
= ansi_assopname (BIT_AND_EXPR
);
9116 id
= ansi_assopname (BIT_IOR_EXPR
);
9120 id
= ansi_opname (LSHIFT_EXPR
);
9124 id
= ansi_opname (RSHIFT_EXPR
);
9128 id
= ansi_assopname (LSHIFT_EXPR
);
9132 id
= ansi_assopname (RSHIFT_EXPR
);
9136 id
= ansi_opname (EQ_EXPR
);
9140 id
= ansi_opname (NE_EXPR
);
9144 id
= ansi_opname (LE_EXPR
);
9147 case CPP_GREATER_EQ
:
9148 id
= ansi_opname (GE_EXPR
);
9152 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
9156 id
= ansi_opname (TRUTH_ORIF_EXPR
);
9160 id
= ansi_opname (POSTINCREMENT_EXPR
);
9163 case CPP_MINUS_MINUS
:
9164 id
= ansi_opname (PREDECREMENT_EXPR
);
9168 id
= ansi_opname (COMPOUND_EXPR
);
9171 case CPP_DEREF_STAR
:
9172 id
= ansi_opname (MEMBER_REF
);
9176 id
= ansi_opname (COMPONENT_REF
);
9179 case CPP_OPEN_PAREN
:
9180 /* Consume the `('. */
9181 cp_lexer_consume_token (parser
->lexer
);
9182 /* Look for the matching `)'. */
9183 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
9184 return ansi_opname (CALL_EXPR
);
9186 case CPP_OPEN_SQUARE
:
9187 /* Consume the `['. */
9188 cp_lexer_consume_token (parser
->lexer
);
9189 /* Look for the matching `]'. */
9190 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
9191 return ansi_opname (ARRAY_REF
);
9194 /* Anything else is an error. */
9198 /* If we have selected an identifier, we need to consume the
9201 cp_lexer_consume_token (parser
->lexer
);
9202 /* Otherwise, no valid operator name was present. */
9205 cp_parser_error (parser
, "expected operator");
9206 id
= error_mark_node
;
9212 /* Parse a template-declaration.
9214 template-declaration:
9215 export [opt] template < template-parameter-list > declaration
9217 If MEMBER_P is TRUE, this template-declaration occurs within a
9220 The grammar rule given by the standard isn't correct. What
9223 template-declaration:
9224 export [opt] template-parameter-list-seq
9225 decl-specifier-seq [opt] init-declarator [opt] ;
9226 export [opt] template-parameter-list-seq
9229 template-parameter-list-seq:
9230 template-parameter-list-seq [opt]
9231 template < template-parameter-list > */
9234 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
9236 /* Check for `export'. */
9237 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
9239 /* Consume the `export' token. */
9240 cp_lexer_consume_token (parser
->lexer
);
9241 /* Warn that we do not support `export'. */
9242 warning (0, "keyword %<export%> not implemented, and will be ignored");
9245 cp_parser_template_declaration_after_export (parser
, member_p
);
9248 /* Parse a template-parameter-list.
9250 template-parameter-list:
9252 template-parameter-list , template-parameter
9254 Returns a TREE_LIST. Each node represents a template parameter.
9255 The nodes are connected via their TREE_CHAINs. */
9258 cp_parser_template_parameter_list (cp_parser
* parser
)
9260 tree parameter_list
= NULL_TREE
;
9262 begin_template_parm_list ();
9268 bool is_parameter_pack
;
9270 /* Parse the template-parameter. */
9271 parameter
= cp_parser_template_parameter (parser
,
9273 &is_parameter_pack
);
9274 /* Add it to the list. */
9275 if (parameter
!= error_mark_node
)
9276 parameter_list
= process_template_parm (parameter_list
,
9282 tree err_parm
= build_tree_list (parameter
, parameter
);
9283 TREE_VALUE (err_parm
) = error_mark_node
;
9284 parameter_list
= chainon (parameter_list
, err_parm
);
9287 /* Peek at the next token. */
9288 token
= cp_lexer_peek_token (parser
->lexer
);
9289 /* If it's not a `,', we're done. */
9290 if (token
->type
!= CPP_COMMA
)
9292 /* Otherwise, consume the `,' token. */
9293 cp_lexer_consume_token (parser
->lexer
);
9296 return end_template_parm_list (parameter_list
);
9299 /* Parse a template-parameter.
9303 parameter-declaration
9305 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9306 the parameter. The TREE_PURPOSE is the default value, if any.
9307 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9308 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9309 set to true iff this parameter is a parameter pack. */
9312 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
9313 bool *is_parameter_pack
)
9316 cp_parameter_declarator
*parameter_declarator
;
9317 cp_declarator
*id_declarator
;
9320 /* Assume it is a type parameter or a template parameter. */
9321 *is_non_type
= false;
9322 /* Assume it not a parameter pack. */
9323 *is_parameter_pack
= false;
9324 /* Peek at the next token. */
9325 token
= cp_lexer_peek_token (parser
->lexer
);
9326 /* If it is `class' or `template', we have a type-parameter. */
9327 if (token
->keyword
== RID_TEMPLATE
)
9328 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9329 /* If it is `class' or `typename' we do not know yet whether it is a
9330 type parameter or a non-type parameter. Consider:
9332 template <typename T, typename T::X X> ...
9336 template <class C, class D*> ...
9338 Here, the first parameter is a type parameter, and the second is
9339 a non-type parameter. We can tell by looking at the token after
9340 the identifier -- if it is a `,', `=', or `>' then we have a type
9342 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
9344 /* Peek at the token after `class' or `typename'. */
9345 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9346 /* If it's an ellipsis, we have a template type parameter
9348 if (token
->type
== CPP_ELLIPSIS
)
9349 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9350 /* If it's an identifier, skip it. */
9351 if (token
->type
== CPP_NAME
)
9352 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
9353 /* Now, see if the token looks like the end of a template
9355 if (token
->type
== CPP_COMMA
9356 || token
->type
== CPP_EQ
9357 || token
->type
== CPP_GREATER
)
9358 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9361 /* Otherwise, it is a non-type parameter.
9365 When parsing a default template-argument for a non-type
9366 template-parameter, the first non-nested `>' is taken as the end
9367 of the template parameter-list rather than a greater-than
9369 *is_non_type
= true;
9370 parameter_declarator
9371 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
9372 /*parenthesized_p=*/NULL
);
9374 /* If the parameter declaration is marked as a parameter pack, set
9375 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9376 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9378 if (parameter_declarator
9379 && parameter_declarator
->declarator
9380 && parameter_declarator
->declarator
->parameter_pack_p
)
9382 *is_parameter_pack
= true;
9383 parameter_declarator
->declarator
->parameter_pack_p
= false;
9386 /* If the next token is an ellipsis, and we don't already have it
9387 marked as a parameter pack, then we have a parameter pack (that
9388 has no declarator). */
9389 if (!*is_parameter_pack
9390 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
9391 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
9393 /* Consume the `...'. */
9394 cp_lexer_consume_token (parser
->lexer
);
9395 maybe_warn_variadic_templates ();
9397 *is_parameter_pack
= true;
9399 /* Parameter packs cannot have default arguments. However, a
9400 user may try to do so, so we'll parse them and give an
9401 appropriate diagnostic here. */
9402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9404 /* Consume the `='. */
9405 cp_lexer_consume_token (parser
->lexer
);
9407 /* Find the name of the parameter pack. */
9408 id_declarator
= parameter_declarator
->declarator
;
9409 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
9410 id_declarator
= id_declarator
->declarator
;
9412 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
9413 error ("template parameter pack %qD cannot have a default argument",
9414 id_declarator
->u
.id
.unqualified_name
);
9416 error ("template parameter pack cannot have a default argument");
9418 /* Parse the default argument, but throw away the result. */
9419 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
9423 parm
= grokdeclarator (parameter_declarator
->declarator
,
9424 ¶meter_declarator
->decl_specifiers
,
9425 PARM
, /*initialized=*/0,
9427 if (parm
== error_mark_node
)
9428 return error_mark_node
;
9430 return build_tree_list (parameter_declarator
->default_argument
, parm
);
9433 /* Parse a type-parameter.
9436 class identifier [opt]
9437 class identifier [opt] = type-id
9438 typename identifier [opt]
9439 typename identifier [opt] = type-id
9440 template < template-parameter-list > class identifier [opt]
9441 template < template-parameter-list > class identifier [opt]
9444 GNU Extension (variadic templates):
9447 class ... identifier [opt]
9448 typename ... identifier [opt]
9450 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9451 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9452 the declaration of the parameter.
9454 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9457 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
9462 /* Look for a keyword to tell us what kind of parameter this is. */
9463 token
= cp_parser_require (parser
, CPP_KEYWORD
,
9464 "`class', `typename', or `template'");
9466 return error_mark_node
;
9468 switch (token
->keyword
)
9474 tree default_argument
;
9476 /* If the next token is an ellipsis, we have a template
9478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9480 /* Consume the `...' token. */
9481 cp_lexer_consume_token (parser
->lexer
);
9482 maybe_warn_variadic_templates ();
9484 *is_parameter_pack
= true;
9487 /* If the next token is an identifier, then it names the
9489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9490 identifier
= cp_parser_identifier (parser
);
9492 identifier
= NULL_TREE
;
9494 /* Create the parameter. */
9495 parameter
= finish_template_type_parm (class_type_node
, identifier
);
9497 /* If the next token is an `=', we have a default argument. */
9498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9500 /* Consume the `=' token. */
9501 cp_lexer_consume_token (parser
->lexer
);
9502 /* Parse the default-argument. */
9503 push_deferring_access_checks (dk_no_deferred
);
9504 default_argument
= cp_parser_type_id (parser
);
9506 /* Template parameter packs cannot have default
9508 if (*is_parameter_pack
)
9511 error ("template parameter pack %qD cannot have a default argument",
9514 error ("template parameter packs cannot have default arguments");
9515 default_argument
= NULL_TREE
;
9517 pop_deferring_access_checks ();
9520 default_argument
= NULL_TREE
;
9522 /* Create the combined representation of the parameter and the
9523 default argument. */
9524 parameter
= build_tree_list (default_argument
, parameter
);
9530 tree parameter_list
;
9532 tree default_argument
;
9534 /* Look for the `<'. */
9535 cp_parser_require (parser
, CPP_LESS
, "`<'");
9536 /* Parse the template-parameter-list. */
9537 parameter_list
= cp_parser_template_parameter_list (parser
);
9538 /* Look for the `>'. */
9539 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9540 /* Look for the `class' keyword. */
9541 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
9542 /* If the next token is an ellipsis, we have a template
9544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9546 /* Consume the `...' token. */
9547 cp_lexer_consume_token (parser
->lexer
);
9548 maybe_warn_variadic_templates ();
9550 *is_parameter_pack
= true;
9552 /* If the next token is an `=', then there is a
9553 default-argument. If the next token is a `>', we are at
9554 the end of the parameter-list. If the next token is a `,',
9555 then we are at the end of this parameter. */
9556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9557 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
9558 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9560 identifier
= cp_parser_identifier (parser
);
9561 /* Treat invalid names as if the parameter were nameless. */
9562 if (identifier
== error_mark_node
)
9563 identifier
= NULL_TREE
;
9566 identifier
= NULL_TREE
;
9568 /* Create the template parameter. */
9569 parameter
= finish_template_template_parm (class_type_node
,
9572 /* If the next token is an `=', then there is a
9573 default-argument. */
9574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9578 /* Consume the `='. */
9579 cp_lexer_consume_token (parser
->lexer
);
9580 /* Parse the id-expression. */
9581 push_deferring_access_checks (dk_no_deferred
);
9583 = cp_parser_id_expression (parser
,
9584 /*template_keyword_p=*/false,
9585 /*check_dependency_p=*/true,
9586 /*template_p=*/&is_template
,
9587 /*declarator_p=*/false,
9588 /*optional_p=*/false);
9589 if (TREE_CODE (default_argument
) == TYPE_DECL
)
9590 /* If the id-expression was a template-id that refers to
9591 a template-class, we already have the declaration here,
9592 so no further lookup is needed. */
9595 /* Look up the name. */
9597 = cp_parser_lookup_name (parser
, default_argument
,
9599 /*is_template=*/is_template
,
9600 /*is_namespace=*/false,
9601 /*check_dependency=*/true,
9602 /*ambiguous_decls=*/NULL
);
9603 /* See if the default argument is valid. */
9605 = check_template_template_default_arg (default_argument
);
9607 /* Template parameter packs cannot have default
9609 if (*is_parameter_pack
)
9612 error ("template parameter pack %qD cannot have a default argument",
9615 error ("template parameter packs cannot have default arguments");
9616 default_argument
= NULL_TREE
;
9618 pop_deferring_access_checks ();
9621 default_argument
= NULL_TREE
;
9623 /* Create the combined representation of the parameter and the
9624 default argument. */
9625 parameter
= build_tree_list (default_argument
, parameter
);
9637 /* Parse a template-id.
9640 template-name < template-argument-list [opt] >
9642 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9643 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9644 returned. Otherwise, if the template-name names a function, or set
9645 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9646 names a class, returns a TYPE_DECL for the specialization.
9648 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9649 uninstantiated templates. */
9652 cp_parser_template_id (cp_parser
*parser
,
9653 bool template_keyword_p
,
9654 bool check_dependency_p
,
9655 bool is_declaration
)
9661 cp_token_position start_of_id
= 0;
9662 deferred_access_check
*chk
;
9663 VEC (deferred_access_check
,gc
) *access_check
;
9664 cp_token
*next_token
, *next_token_2
;
9667 /* If the next token corresponds to a template-id, there is no need
9669 next_token
= cp_lexer_peek_token (parser
->lexer
);
9670 if (next_token
->type
== CPP_TEMPLATE_ID
)
9672 struct tree_check
*check_value
;
9674 /* Get the stored value. */
9675 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
9676 /* Perform any access checks that were deferred. */
9677 access_check
= check_value
->checks
;
9681 VEC_iterate (deferred_access_check
, access_check
, i
, chk
) ;
9684 perform_or_defer_access_check (chk
->binfo
,
9689 /* Return the stored value. */
9690 return check_value
->value
;
9693 /* Avoid performing name lookup if there is no possibility of
9694 finding a template-id. */
9695 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
9696 || (next_token
->type
== CPP_NAME
9697 && !cp_parser_nth_token_starts_template_argument_list_p
9700 cp_parser_error (parser
, "expected template-id");
9701 return error_mark_node
;
9704 /* Remember where the template-id starts. */
9705 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
9706 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
9708 push_deferring_access_checks (dk_deferred
);
9710 /* Parse the template-name. */
9711 is_identifier
= false;
9712 template = cp_parser_template_name (parser
, template_keyword_p
,
9716 if (template == error_mark_node
|| is_identifier
)
9718 pop_deferring_access_checks ();
9722 /* If we find the sequence `[:' after a template-name, it's probably
9723 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9724 parse correctly the argument list. */
9725 next_token
= cp_lexer_peek_token (parser
->lexer
);
9726 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9727 if (next_token
->type
== CPP_OPEN_SQUARE
9728 && next_token
->flags
& DIGRAPH
9729 && next_token_2
->type
== CPP_COLON
9730 && !(next_token_2
->flags
& PREV_WHITE
))
9732 cp_parser_parse_tentatively (parser
);
9733 /* Change `:' into `::'. */
9734 next_token_2
->type
= CPP_SCOPE
;
9735 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9737 cp_lexer_consume_token (parser
->lexer
);
9738 /* Parse the arguments. */
9739 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9740 if (!cp_parser_parse_definitely (parser
))
9742 /* If we couldn't parse an argument list, then we revert our changes
9743 and return simply an error. Maybe this is not a template-id
9745 next_token_2
->type
= CPP_COLON
;
9746 cp_parser_error (parser
, "expected %<<%>");
9747 pop_deferring_access_checks ();
9748 return error_mark_node
;
9750 /* Otherwise, emit an error about the invalid digraph, but continue
9751 parsing because we got our argument list. */
9752 pedwarn ("%<<::%> cannot begin a template-argument list");
9753 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9754 "between %<<%> and %<::%>");
9755 if (!flag_permissive
)
9760 inform ("(if you use -fpermissive G++ will accept your code)");
9767 /* Look for the `<' that starts the template-argument-list. */
9768 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
9770 pop_deferring_access_checks ();
9771 return error_mark_node
;
9773 /* Parse the arguments. */
9774 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9777 /* Build a representation of the specialization. */
9778 if (TREE_CODE (template) == IDENTIFIER_NODE
)
9779 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
9780 else if (DECL_CLASS_TEMPLATE_P (template)
9781 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9783 bool entering_scope
;
9784 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9785 template (rather than some instantiation thereof) only if
9786 is not nested within some other construct. For example, in
9787 "template <typename T> void f(T) { A<T>::", A<T> is just an
9788 instantiation of A. */
9789 entering_scope
= (template_parm_scope_p ()
9790 && cp_lexer_next_token_is (parser
->lexer
,
9793 = finish_template_type (template, arguments
, entering_scope
);
9797 /* If it's not a class-template or a template-template, it should be
9798 a function-template. */
9799 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9800 || TREE_CODE (template) == OVERLOAD
9801 || BASELINK_P (template)));
9803 template_id
= lookup_template_function (template, arguments
);
9806 /* If parsing tentatively, replace the sequence of tokens that makes
9807 up the template-id with a CPP_TEMPLATE_ID token. That way,
9808 should we re-parse the token stream, we will not have to repeat
9809 the effort required to do the parse, nor will we issue duplicate
9810 error messages about problems during instantiation of the
9814 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
9816 /* Reset the contents of the START_OF_ID token. */
9817 token
->type
= CPP_TEMPLATE_ID
;
9818 /* Retrieve any deferred checks. Do not pop this access checks yet
9819 so the memory will not be reclaimed during token replacing below. */
9820 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
9821 token
->u
.tree_check_value
->value
= template_id
;
9822 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
9823 token
->keyword
= RID_MAX
;
9825 /* Purge all subsequent tokens. */
9826 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
9828 /* ??? Can we actually assume that, if template_id ==
9829 error_mark_node, we will have issued a diagnostic to the
9830 user, as opposed to simply marking the tentative parse as
9832 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
9833 error ("parse error in template argument list");
9836 pop_deferring_access_checks ();
9840 /* Parse a template-name.
9845 The standard should actually say:
9849 operator-function-id
9851 A defect report has been filed about this issue.
9853 A conversion-function-id cannot be a template name because they cannot
9854 be part of a template-id. In fact, looking at this code:
9858 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9859 It is impossible to call a templated conversion-function-id with an
9860 explicit argument list, since the only allowed template parameter is
9861 the type to which it is converting.
9863 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9864 `template' keyword, in a construction like:
9868 In that case `f' is taken to be a template-name, even though there
9869 is no way of knowing for sure.
9871 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9872 name refers to a set of overloaded functions, at least one of which
9873 is a template, or an IDENTIFIER_NODE with the name of the template,
9874 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9875 names are looked up inside uninstantiated templates. */
9878 cp_parser_template_name (cp_parser
* parser
,
9879 bool template_keyword_p
,
9880 bool check_dependency_p
,
9881 bool is_declaration
,
9882 bool *is_identifier
)
9888 /* If the next token is `operator', then we have either an
9889 operator-function-id or a conversion-function-id. */
9890 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
9892 /* We don't know whether we're looking at an
9893 operator-function-id or a conversion-function-id. */
9894 cp_parser_parse_tentatively (parser
);
9895 /* Try an operator-function-id. */
9896 identifier
= cp_parser_operator_function_id (parser
);
9897 /* If that didn't work, try a conversion-function-id. */
9898 if (!cp_parser_parse_definitely (parser
))
9900 cp_parser_error (parser
, "expected template-name");
9901 return error_mark_node
;
9904 /* Look for the identifier. */
9906 identifier
= cp_parser_identifier (parser
);
9908 /* If we didn't find an identifier, we don't have a template-id. */
9909 if (identifier
== error_mark_node
)
9910 return error_mark_node
;
9912 /* If the name immediately followed the `template' keyword, then it
9913 is a template-name. However, if the next token is not `<', then
9914 we do not treat it as a template-name, since it is not being used
9915 as part of a template-id. This enables us to handle constructs
9918 template <typename T> struct S { S(); };
9919 template <typename T> S<T>::S();
9921 correctly. We would treat `S' as a template -- if it were `S<T>'
9922 -- but we do not if there is no `<'. */
9924 if (processing_template_decl
9925 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
9927 /* In a declaration, in a dependent context, we pretend that the
9928 "template" keyword was present in order to improve error
9929 recovery. For example, given:
9931 template <typename T> void f(T::X<int>);
9933 we want to treat "X<int>" as a template-id. */
9935 && !template_keyword_p
9936 && parser
->scope
&& TYPE_P (parser
->scope
)
9937 && check_dependency_p
9938 && dependent_type_p (parser
->scope
)
9939 /* Do not do this for dtors (or ctors), since they never
9940 need the template keyword before their name. */
9941 && !constructor_name_p (identifier
, parser
->scope
))
9943 cp_token_position start
= 0;
9945 /* Explain what went wrong. */
9946 error ("non-template %qD used as template", identifier
);
9947 inform ("use %<%T::template %D%> to indicate that it is a template",
9948 parser
->scope
, identifier
);
9949 /* If parsing tentatively, find the location of the "<" token. */
9950 if (cp_parser_simulate_error (parser
))
9951 start
= cp_lexer_token_position (parser
->lexer
, true);
9952 /* Parse the template arguments so that we can issue error
9953 messages about them. */
9954 cp_lexer_consume_token (parser
->lexer
);
9955 cp_parser_enclosed_template_argument_list (parser
);
9956 /* Skip tokens until we find a good place from which to
9957 continue parsing. */
9958 cp_parser_skip_to_closing_parenthesis (parser
,
9959 /*recovering=*/true,
9961 /*consume_paren=*/false);
9962 /* If parsing tentatively, permanently remove the
9963 template argument list. That will prevent duplicate
9964 error messages from being issued about the missing
9965 "template" keyword. */
9967 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
9969 *is_identifier
= true;
9973 /* If the "template" keyword is present, then there is generally
9974 no point in doing name-lookup, so we just return IDENTIFIER.
9975 But, if the qualifying scope is non-dependent then we can
9976 (and must) do name-lookup normally. */
9977 if (template_keyword_p
9979 || (TYPE_P (parser
->scope
)
9980 && dependent_type_p (parser
->scope
))))
9984 /* Look up the name. */
9985 decl
= cp_parser_lookup_name (parser
, identifier
,
9987 /*is_template=*/false,
9988 /*is_namespace=*/false,
9990 /*ambiguous_decls=*/NULL
);
9991 decl
= maybe_get_template_decl_from_type_decl (decl
);
9993 /* If DECL is a template, then the name was a template-name. */
9994 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9998 tree fn
= NULL_TREE
;
10000 /* The standard does not explicitly indicate whether a name that
10001 names a set of overloaded declarations, some of which are
10002 templates, is a template-name. However, such a name should
10003 be a template-name; otherwise, there is no way to form a
10004 template-id for the overloaded templates. */
10005 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
10006 if (TREE_CODE (fns
) == OVERLOAD
)
10007 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
10008 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
10013 /* The name does not name a template. */
10014 cp_parser_error (parser
, "expected template-name");
10015 return error_mark_node
;
10019 /* If DECL is dependent, and refers to a function, then just return
10020 its name; we will look it up again during template instantiation. */
10021 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
10023 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
10024 if (TYPE_P (scope
) && dependent_type_p (scope
))
10031 /* Parse a template-argument-list.
10033 template-argument-list:
10034 template-argument ... [opt]
10035 template-argument-list , template-argument ... [opt]
10037 Returns a TREE_VEC containing the arguments. */
10040 cp_parser_template_argument_list (cp_parser
* parser
)
10042 tree fixed_args
[10];
10043 unsigned n_args
= 0;
10044 unsigned alloced
= 10;
10045 tree
*arg_ary
= fixed_args
;
10047 bool saved_in_template_argument_list_p
;
10049 bool saved_non_ice_p
;
10051 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
10052 parser
->in_template_argument_list_p
= true;
10053 /* Even if the template-id appears in an integral
10054 constant-expression, the contents of the argument list do
10056 saved_ice_p
= parser
->integral_constant_expression_p
;
10057 parser
->integral_constant_expression_p
= false;
10058 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
10059 parser
->non_integral_constant_expression_p
= false;
10060 /* Parse the arguments. */
10066 /* Consume the comma. */
10067 cp_lexer_consume_token (parser
->lexer
);
10069 /* Parse the template-argument. */
10070 argument
= cp_parser_template_argument (parser
);
10072 /* If the next token is an ellipsis, we're expanding a template
10074 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10076 /* Consume the `...' token. */
10077 cp_lexer_consume_token (parser
->lexer
);
10079 /* Make the argument into a TYPE_PACK_EXPANSION or
10080 EXPR_PACK_EXPANSION. */
10081 argument
= make_pack_expansion (argument
);
10084 if (n_args
== alloced
)
10088 if (arg_ary
== fixed_args
)
10090 arg_ary
= XNEWVEC (tree
, alloced
);
10091 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
10094 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
10096 arg_ary
[n_args
++] = argument
;
10098 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
10100 vec
= make_tree_vec (n_args
);
10103 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
10105 if (arg_ary
!= fixed_args
)
10107 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
10108 parser
->integral_constant_expression_p
= saved_ice_p
;
10109 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
10113 /* Parse a template-argument.
10116 assignment-expression
10120 The representation is that of an assignment-expression, type-id, or
10121 id-expression -- except that the qualified id-expression is
10122 evaluated, so that the value returned is either a DECL or an
10125 Although the standard says "assignment-expression", it forbids
10126 throw-expressions or assignments in the template argument.
10127 Therefore, we use "conditional-expression" instead. */
10130 cp_parser_template_argument (cp_parser
* parser
)
10135 bool maybe_type_id
= false;
10139 /* There's really no way to know what we're looking at, so we just
10140 try each alternative in order.
10144 In a template-argument, an ambiguity between a type-id and an
10145 expression is resolved to a type-id, regardless of the form of
10146 the corresponding template-parameter.
10148 Therefore, we try a type-id first. */
10149 cp_parser_parse_tentatively (parser
);
10150 argument
= cp_parser_type_id (parser
);
10151 /* If there was no error parsing the type-id but the next token is a '>>',
10152 we probably found a typo for '> >'. But there are type-id which are
10153 also valid expressions. For instance:
10155 struct X { int operator >> (int); };
10156 template <int V> struct Foo {};
10159 Here 'X()' is a valid type-id of a function type, but the user just
10160 wanted to write the expression "X() >> 5". Thus, we remember that we
10161 found a valid type-id, but we still try to parse the argument as an
10162 expression to see what happens. */
10163 if (!cp_parser_error_occurred (parser
)
10164 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
10166 maybe_type_id
= true;
10167 cp_parser_abort_tentative_parse (parser
);
10171 /* If the next token isn't a `,' or a `>', then this argument wasn't
10172 really finished. This means that the argument is not a valid
10174 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10175 cp_parser_error (parser
, "expected template-argument");
10176 /* If that worked, we're done. */
10177 if (cp_parser_parse_definitely (parser
))
10180 /* We're still not sure what the argument will be. */
10181 cp_parser_parse_tentatively (parser
);
10182 /* Try a template. */
10183 argument
= cp_parser_id_expression (parser
,
10184 /*template_keyword_p=*/false,
10185 /*check_dependency_p=*/true,
10187 /*declarator_p=*/false,
10188 /*optional_p=*/false);
10189 /* If the next token isn't a `,' or a `>', then this argument wasn't
10190 really finished. */
10191 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10192 cp_parser_error (parser
, "expected template-argument");
10193 if (!cp_parser_error_occurred (parser
))
10195 /* Figure out what is being referred to. If the id-expression
10196 was for a class template specialization, then we will have a
10197 TYPE_DECL at this point. There is no need to do name lookup
10198 at this point in that case. */
10199 if (TREE_CODE (argument
) != TYPE_DECL
)
10200 argument
= cp_parser_lookup_name (parser
, argument
,
10202 /*is_template=*/template_p
,
10203 /*is_namespace=*/false,
10204 /*check_dependency=*/true,
10205 /*ambiguous_decls=*/NULL
);
10206 if (TREE_CODE (argument
) != TEMPLATE_DECL
10207 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
10208 cp_parser_error (parser
, "expected template-name");
10210 if (cp_parser_parse_definitely (parser
))
10212 /* It must be a non-type argument. There permitted cases are given
10213 in [temp.arg.nontype]:
10215 -- an integral constant-expression of integral or enumeration
10218 -- the name of a non-type template-parameter; or
10220 -- the name of an object or function with external linkage...
10222 -- the address of an object or function with external linkage...
10224 -- a pointer to member... */
10225 /* Look for a non-type template parameter. */
10226 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10228 cp_parser_parse_tentatively (parser
);
10229 argument
= cp_parser_primary_expression (parser
,
10230 /*adress_p=*/false,
10232 /*template_arg_p=*/true,
10234 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
10235 || !cp_parser_next_token_ends_template_argument_p (parser
))
10236 cp_parser_simulate_error (parser
);
10237 if (cp_parser_parse_definitely (parser
))
10241 /* If the next token is "&", the argument must be the address of an
10242 object or function with external linkage. */
10243 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
10245 cp_lexer_consume_token (parser
->lexer
);
10246 /* See if we might have an id-expression. */
10247 token
= cp_lexer_peek_token (parser
->lexer
);
10248 if (token
->type
== CPP_NAME
10249 || token
->keyword
== RID_OPERATOR
10250 || token
->type
== CPP_SCOPE
10251 || token
->type
== CPP_TEMPLATE_ID
10252 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
10254 cp_parser_parse_tentatively (parser
);
10255 argument
= cp_parser_primary_expression (parser
,
10258 /*template_arg_p=*/true,
10260 if (cp_parser_error_occurred (parser
)
10261 || !cp_parser_next_token_ends_template_argument_p (parser
))
10262 cp_parser_abort_tentative_parse (parser
);
10265 if (TREE_CODE (argument
) == INDIRECT_REF
)
10267 gcc_assert (REFERENCE_REF_P (argument
));
10268 argument
= TREE_OPERAND (argument
, 0);
10271 if (TREE_CODE (argument
) == VAR_DECL
)
10273 /* A variable without external linkage might still be a
10274 valid constant-expression, so no error is issued here
10275 if the external-linkage check fails. */
10276 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
10277 cp_parser_simulate_error (parser
);
10279 else if (is_overloaded_fn (argument
))
10280 /* All overloaded functions are allowed; if the external
10281 linkage test does not pass, an error will be issued
10285 && (TREE_CODE (argument
) == OFFSET_REF
10286 || TREE_CODE (argument
) == SCOPE_REF
))
10287 /* A pointer-to-member. */
10289 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
10292 cp_parser_simulate_error (parser
);
10294 if (cp_parser_parse_definitely (parser
))
10297 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
10302 /* If the argument started with "&", there are no other valid
10303 alternatives at this point. */
10306 cp_parser_error (parser
, "invalid non-type template argument");
10307 return error_mark_node
;
10310 /* If the argument wasn't successfully parsed as a type-id followed
10311 by '>>', the argument can only be a constant expression now.
10312 Otherwise, we try parsing the constant-expression tentatively,
10313 because the argument could really be a type-id. */
10315 cp_parser_parse_tentatively (parser
);
10316 argument
= cp_parser_constant_expression (parser
,
10317 /*allow_non_constant_p=*/false,
10318 /*non_constant_p=*/NULL
);
10319 argument
= fold_non_dependent_expr (argument
);
10320 if (!maybe_type_id
)
10322 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10323 cp_parser_error (parser
, "expected template-argument");
10324 if (cp_parser_parse_definitely (parser
))
10326 /* We did our best to parse the argument as a non type-id, but that
10327 was the only alternative that matched (albeit with a '>' after
10328 it). We can assume it's just a typo from the user, and a
10329 diagnostic will then be issued. */
10330 return cp_parser_type_id (parser
);
10333 /* Parse an explicit-instantiation.
10335 explicit-instantiation:
10336 template declaration
10338 Although the standard says `declaration', what it really means is:
10340 explicit-instantiation:
10341 template decl-specifier-seq [opt] declarator [opt] ;
10343 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10344 supposed to be allowed. A defect report has been filed about this
10349 explicit-instantiation:
10350 storage-class-specifier template
10351 decl-specifier-seq [opt] declarator [opt] ;
10352 function-specifier template
10353 decl-specifier-seq [opt] declarator [opt] ; */
10356 cp_parser_explicit_instantiation (cp_parser
* parser
)
10358 int declares_class_or_enum
;
10359 cp_decl_specifier_seq decl_specifiers
;
10360 tree extension_specifier
= NULL_TREE
;
10362 /* Look for an (optional) storage-class-specifier or
10363 function-specifier. */
10364 if (cp_parser_allow_gnu_extensions_p (parser
))
10366 extension_specifier
10367 = cp_parser_storage_class_specifier_opt (parser
);
10368 if (!extension_specifier
)
10369 extension_specifier
10370 = cp_parser_function_specifier_opt (parser
,
10371 /*decl_specs=*/NULL
);
10374 /* Look for the `template' keyword. */
10375 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
10376 /* Let the front end know that we are processing an explicit
10378 begin_explicit_instantiation ();
10379 /* [temp.explicit] says that we are supposed to ignore access
10380 control while processing explicit instantiation directives. */
10381 push_deferring_access_checks (dk_no_check
);
10382 /* Parse a decl-specifier-seq. */
10383 cp_parser_decl_specifier_seq (parser
,
10384 CP_PARSER_FLAGS_OPTIONAL
,
10386 &declares_class_or_enum
);
10387 /* If there was exactly one decl-specifier, and it declared a class,
10388 and there's no declarator, then we have an explicit type
10390 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
10394 type
= check_tag_decl (&decl_specifiers
);
10395 /* Turn access control back on for names used during
10396 template instantiation. */
10397 pop_deferring_access_checks ();
10399 do_type_instantiation (type
, extension_specifier
,
10400 /*complain=*/tf_error
);
10404 cp_declarator
*declarator
;
10407 /* Parse the declarator. */
10409 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10410 /*ctor_dtor_or_conv_p=*/NULL
,
10411 /*parenthesized_p=*/NULL
,
10412 /*member_p=*/false);
10413 if (declares_class_or_enum
& 2)
10414 cp_parser_check_for_definition_in_return_type (declarator
,
10415 decl_specifiers
.type
);
10416 if (declarator
!= cp_error_declarator
)
10418 decl
= grokdeclarator (declarator
, &decl_specifiers
,
10419 NORMAL
, 0, &decl_specifiers
.attributes
);
10420 /* Turn access control back on for names used during
10421 template instantiation. */
10422 pop_deferring_access_checks ();
10423 /* Do the explicit instantiation. */
10424 do_decl_instantiation (decl
, extension_specifier
);
10428 pop_deferring_access_checks ();
10429 /* Skip the body of the explicit instantiation. */
10430 cp_parser_skip_to_end_of_statement (parser
);
10433 /* We're done with the instantiation. */
10434 end_explicit_instantiation ();
10436 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10439 /* Parse an explicit-specialization.
10441 explicit-specialization:
10442 template < > declaration
10444 Although the standard says `declaration', what it really means is:
10446 explicit-specialization:
10447 template <> decl-specifier [opt] init-declarator [opt] ;
10448 template <> function-definition
10449 template <> explicit-specialization
10450 template <> template-declaration */
10453 cp_parser_explicit_specialization (cp_parser
* parser
)
10455 bool need_lang_pop
;
10456 /* Look for the `template' keyword. */
10457 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
10458 /* Look for the `<'. */
10459 cp_parser_require (parser
, CPP_LESS
, "`<'");
10460 /* Look for the `>'. */
10461 cp_parser_require (parser
, CPP_GREATER
, "`>'");
10462 /* We have processed another parameter list. */
10463 ++parser
->num_template_parameter_lists
;
10466 A template ... explicit specialization ... shall not have C
10468 if (current_lang_name
== lang_name_c
)
10470 error ("template specialization with C linkage");
10471 /* Give it C++ linkage to avoid confusing other parts of the
10473 push_lang_context (lang_name_cplusplus
);
10474 need_lang_pop
= true;
10477 need_lang_pop
= false;
10478 /* Let the front end know that we are beginning a specialization. */
10479 if (!begin_specialization ())
10481 end_specialization ();
10482 cp_parser_skip_to_end_of_block_or_statement (parser
);
10486 /* If the next keyword is `template', we need to figure out whether
10487 or not we're looking a template-declaration. */
10488 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
10490 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
10491 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
10492 cp_parser_template_declaration_after_export (parser
,
10493 /*member_p=*/false);
10495 cp_parser_explicit_specialization (parser
);
10498 /* Parse the dependent declaration. */
10499 cp_parser_single_declaration (parser
,
10501 /*member_p=*/false,
10502 /*explicit_specialization_p=*/true,
10503 /*friend_p=*/NULL
);
10504 /* We're done with the specialization. */
10505 end_specialization ();
10506 /* For the erroneous case of a template with C linkage, we pushed an
10507 implicit C++ linkage scope; exit that scope now. */
10509 pop_lang_context ();
10510 /* We're done with this parameter list. */
10511 --parser
->num_template_parameter_lists
;
10514 /* Parse a type-specifier.
10517 simple-type-specifier
10520 elaborated-type-specifier
10528 Returns a representation of the type-specifier. For a
10529 class-specifier, enum-specifier, or elaborated-type-specifier, a
10530 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10532 The parser flags FLAGS is used to control type-specifier parsing.
10534 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10535 in a decl-specifier-seq.
10537 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10538 class-specifier, enum-specifier, or elaborated-type-specifier, then
10539 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10540 if a type is declared; 2 if it is defined. Otherwise, it is set to
10543 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10544 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10545 is set to FALSE. */
10548 cp_parser_type_specifier (cp_parser
* parser
,
10549 cp_parser_flags flags
,
10550 cp_decl_specifier_seq
*decl_specs
,
10551 bool is_declaration
,
10552 int* declares_class_or_enum
,
10553 bool* is_cv_qualifier
)
10555 tree type_spec
= NULL_TREE
;
10558 cp_decl_spec ds
= ds_last
;
10560 /* Assume this type-specifier does not declare a new type. */
10561 if (declares_class_or_enum
)
10562 *declares_class_or_enum
= 0;
10563 /* And that it does not specify a cv-qualifier. */
10564 if (is_cv_qualifier
)
10565 *is_cv_qualifier
= false;
10566 /* Peek at the next token. */
10567 token
= cp_lexer_peek_token (parser
->lexer
);
10569 /* If we're looking at a keyword, we can use that to guide the
10570 production we choose. */
10571 keyword
= token
->keyword
;
10575 /* Look for the enum-specifier. */
10576 type_spec
= cp_parser_enum_specifier (parser
);
10577 /* If that worked, we're done. */
10580 if (declares_class_or_enum
)
10581 *declares_class_or_enum
= 2;
10583 cp_parser_set_decl_spec_type (decl_specs
,
10585 /*user_defined_p=*/true);
10589 goto elaborated_type_specifier
;
10591 /* Any of these indicate either a class-specifier, or an
10592 elaborated-type-specifier. */
10596 /* Parse tentatively so that we can back up if we don't find a
10597 class-specifier. */
10598 cp_parser_parse_tentatively (parser
);
10599 /* Look for the class-specifier. */
10600 type_spec
= cp_parser_class_specifier (parser
);
10601 /* If that worked, we're done. */
10602 if (cp_parser_parse_definitely (parser
))
10604 if (declares_class_or_enum
)
10605 *declares_class_or_enum
= 2;
10607 cp_parser_set_decl_spec_type (decl_specs
,
10609 /*user_defined_p=*/true);
10613 /* Fall through. */
10614 elaborated_type_specifier
:
10615 /* We're declaring (not defining) a class or enum. */
10616 if (declares_class_or_enum
)
10617 *declares_class_or_enum
= 1;
10619 /* Fall through. */
10621 /* Look for an elaborated-type-specifier. */
10623 = (cp_parser_elaborated_type_specifier
10625 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
10628 cp_parser_set_decl_spec_type (decl_specs
,
10630 /*user_defined_p=*/true);
10635 if (is_cv_qualifier
)
10636 *is_cv_qualifier
= true;
10641 if (is_cv_qualifier
)
10642 *is_cv_qualifier
= true;
10647 if (is_cv_qualifier
)
10648 *is_cv_qualifier
= true;
10652 /* The `__complex__' keyword is a GNU extension. */
10660 /* Handle simple keywords. */
10665 ++decl_specs
->specs
[(int)ds
];
10666 decl_specs
->any_specifiers_p
= true;
10668 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10671 /* If we do not already have a type-specifier, assume we are looking
10672 at a simple-type-specifier. */
10673 type_spec
= cp_parser_simple_type_specifier (parser
,
10677 /* If we didn't find a type-specifier, and a type-specifier was not
10678 optional in this context, issue an error message. */
10679 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10681 cp_parser_error (parser
, "expected type specifier");
10682 return error_mark_node
;
10688 /* Parse a simple-type-specifier.
10690 simple-type-specifier:
10691 :: [opt] nested-name-specifier [opt] type-name
10692 :: [opt] nested-name-specifier template template-id
10707 simple-type-specifier:
10708 decltype ( expression )
10712 simple-type-specifier:
10713 __typeof__ unary-expression
10714 __typeof__ ( type-id )
10716 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10717 appropriately updated. */
10720 cp_parser_simple_type_specifier (cp_parser
* parser
,
10721 cp_decl_specifier_seq
*decl_specs
,
10722 cp_parser_flags flags
)
10724 tree type
= NULL_TREE
;
10727 /* Peek at the next token. */
10728 token
= cp_lexer_peek_token (parser
->lexer
);
10730 /* If we're looking at a keyword, things are easy. */
10731 switch (token
->keyword
)
10735 decl_specs
->explicit_char_p
= true;
10736 type
= char_type_node
;
10739 type
= wchar_type_node
;
10742 type
= boolean_type_node
;
10746 ++decl_specs
->specs
[(int) ds_short
];
10747 type
= short_integer_type_node
;
10751 decl_specs
->explicit_int_p
= true;
10752 type
= integer_type_node
;
10756 ++decl_specs
->specs
[(int) ds_long
];
10757 type
= long_integer_type_node
;
10761 ++decl_specs
->specs
[(int) ds_signed
];
10762 type
= integer_type_node
;
10766 ++decl_specs
->specs
[(int) ds_unsigned
];
10767 type
= unsigned_type_node
;
10770 type
= float_type_node
;
10773 type
= double_type_node
;
10776 type
= void_type_node
;
10780 /* Parse the `decltype' type. */
10781 type
= cp_parser_decltype (parser
);
10784 cp_parser_set_decl_spec_type (decl_specs
, type
,
10785 /*user_defined_p=*/true);
10790 /* Consume the `typeof' token. */
10791 cp_lexer_consume_token (parser
->lexer
);
10792 /* Parse the operand to `typeof'. */
10793 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
10794 /* If it is not already a TYPE, take its type. */
10795 if (!TYPE_P (type
))
10796 type
= finish_typeof (type
);
10799 cp_parser_set_decl_spec_type (decl_specs
, type
,
10800 /*user_defined_p=*/true);
10808 /* If the type-specifier was for a built-in type, we're done. */
10813 /* Record the type. */
10815 && (token
->keyword
!= RID_SIGNED
10816 && token
->keyword
!= RID_UNSIGNED
10817 && token
->keyword
!= RID_SHORT
10818 && token
->keyword
!= RID_LONG
))
10819 cp_parser_set_decl_spec_type (decl_specs
,
10821 /*user_defined=*/false);
10823 decl_specs
->any_specifiers_p
= true;
10825 /* Consume the token. */
10826 id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10828 /* There is no valid C++ program where a non-template type is
10829 followed by a "<". That usually indicates that the user thought
10830 that the type was a template. */
10831 cp_parser_check_for_invalid_template_id (parser
, type
);
10833 return TYPE_NAME (type
);
10836 /* The type-specifier must be a user-defined type. */
10837 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
10842 /* Don't gobble tokens or issue error messages if this is an
10843 optional type-specifier. */
10844 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
10845 cp_parser_parse_tentatively (parser
);
10847 /* Look for the optional `::' operator. */
10849 = (cp_parser_global_scope_opt (parser
,
10850 /*current_scope_valid_p=*/false)
10852 /* Look for the nested-name specifier. */
10854 = (cp_parser_nested_name_specifier_opt (parser
,
10855 /*typename_keyword_p=*/false,
10856 /*check_dependency_p=*/true,
10858 /*is_declaration=*/false)
10860 /* If we have seen a nested-name-specifier, and the next token
10861 is `template', then we are using the template-id production. */
10863 && cp_parser_optional_template_keyword (parser
))
10865 /* Look for the template-id. */
10866 type
= cp_parser_template_id (parser
,
10867 /*template_keyword_p=*/true,
10868 /*check_dependency_p=*/true,
10869 /*is_declaration=*/false);
10870 /* If the template-id did not name a type, we are out of
10872 if (TREE_CODE (type
) != TYPE_DECL
)
10874 cp_parser_error (parser
, "expected template-id for type");
10878 /* Otherwise, look for a type-name. */
10880 type
= cp_parser_type_name (parser
);
10881 /* Keep track of all name-lookups performed in class scopes. */
10885 && TREE_CODE (type
) == TYPE_DECL
10886 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
10887 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
10888 /* If it didn't work out, we don't have a TYPE. */
10889 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
10890 && !cp_parser_parse_definitely (parser
))
10892 if (type
&& decl_specs
)
10893 cp_parser_set_decl_spec_type (decl_specs
, type
,
10894 /*user_defined=*/true);
10897 /* If we didn't get a type-name, issue an error message. */
10898 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10900 cp_parser_error (parser
, "expected type-name");
10901 return error_mark_node
;
10904 /* There is no valid C++ program where a non-template type is
10905 followed by a "<". That usually indicates that the user thought
10906 that the type was a template. */
10907 if (type
&& type
!= error_mark_node
)
10909 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10910 If it is, then the '<'...'>' enclose protocol names rather than
10911 template arguments, and so everything is fine. */
10912 if (c_dialect_objc ()
10913 && (objc_is_id (type
) || objc_is_class_name (type
)))
10915 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10916 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
10918 /* Clobber the "unqualified" type previously entered into
10919 DECL_SPECS with the new, improved protocol-qualified version. */
10921 decl_specs
->type
= qual_type
;
10926 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
10932 /* Parse a type-name.
10945 Returns a TYPE_DECL for the type. */
10948 cp_parser_type_name (cp_parser
* parser
)
10952 /* We can't know yet whether it is a class-name or not. */
10953 cp_parser_parse_tentatively (parser
);
10954 /* Try a class-name. */
10955 type_decl
= cp_parser_class_name (parser
,
10956 /*typename_keyword_p=*/false,
10957 /*template_keyword_p=*/false,
10959 /*check_dependency_p=*/true,
10960 /*class_head_p=*/false,
10961 /*is_declaration=*/false);
10962 /* If it's not a class-name, keep looking. */
10963 if (!cp_parser_parse_definitely (parser
))
10965 /* It must be a typedef-name or an enum-name. */
10966 return cp_parser_nonclass_name (parser
);
10972 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
10980 Returns a TYPE_DECL for the type. */
10983 cp_parser_nonclass_name (cp_parser
* parser
)
10988 identifier
= cp_parser_identifier (parser
);
10989 if (identifier
== error_mark_node
)
10990 return error_mark_node
;
10992 /* Look up the type-name. */
10993 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10995 if (TREE_CODE (type_decl
) != TYPE_DECL
10996 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
10998 /* See if this is an Objective-C type. */
10999 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
11000 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
11002 type_decl
= TYPE_NAME (type
);
11005 /* Issue an error if we did not find a type-name. */
11006 if (TREE_CODE (type_decl
) != TYPE_DECL
)
11008 if (!cp_parser_simulate_error (parser
))
11009 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
11011 return error_mark_node
;
11013 /* Remember that the name was used in the definition of the
11014 current class so that we can check later to see if the
11015 meaning would have been different after the class was
11016 entirely defined. */
11017 else if (type_decl
!= error_mark_node
11019 maybe_note_name_used_in_class (identifier
, type_decl
);
11024 /* Parse an elaborated-type-specifier. Note that the grammar given
11025 here incorporates the resolution to DR68.
11027 elaborated-type-specifier:
11028 class-key :: [opt] nested-name-specifier [opt] identifier
11029 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11030 enum :: [opt] nested-name-specifier [opt] identifier
11031 typename :: [opt] nested-name-specifier identifier
11032 typename :: [opt] nested-name-specifier template [opt]
11037 elaborated-type-specifier:
11038 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11039 class-key attributes :: [opt] nested-name-specifier [opt]
11040 template [opt] template-id
11041 enum attributes :: [opt] nested-name-specifier [opt] identifier
11043 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11044 declared `friend'. If IS_DECLARATION is TRUE, then this
11045 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11046 something is being declared.
11048 Returns the TYPE specified. */
11051 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
11053 bool is_declaration
)
11055 enum tag_types tag_type
;
11057 tree type
= NULL_TREE
;
11058 tree attributes
= NULL_TREE
;
11060 /* See if we're looking at the `enum' keyword. */
11061 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
11063 /* Consume the `enum' token. */
11064 cp_lexer_consume_token (parser
->lexer
);
11065 /* Remember that it's an enumeration type. */
11066 tag_type
= enum_type
;
11067 /* Parse the attributes. */
11068 attributes
= cp_parser_attributes_opt (parser
);
11070 /* Or, it might be `typename'. */
11071 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11074 /* Consume the `typename' token. */
11075 cp_lexer_consume_token (parser
->lexer
);
11076 /* Remember that it's a `typename' type. */
11077 tag_type
= typename_type
;
11078 /* The `typename' keyword is only allowed in templates. */
11079 if (!processing_template_decl
)
11080 pedwarn ("using %<typename%> outside of template");
11082 /* Otherwise it must be a class-key. */
11085 tag_type
= cp_parser_class_key (parser
);
11086 if (tag_type
== none_type
)
11087 return error_mark_node
;
11088 /* Parse the attributes. */
11089 attributes
= cp_parser_attributes_opt (parser
);
11092 /* Look for the `::' operator. */
11093 cp_parser_global_scope_opt (parser
,
11094 /*current_scope_valid_p=*/false);
11095 /* Look for the nested-name-specifier. */
11096 if (tag_type
== typename_type
)
11098 if (!cp_parser_nested_name_specifier (parser
,
11099 /*typename_keyword_p=*/true,
11100 /*check_dependency_p=*/true,
11103 return error_mark_node
;
11106 /* Even though `typename' is not present, the proposed resolution
11107 to Core Issue 180 says that in `class A<T>::B', `B' should be
11108 considered a type-name, even if `A<T>' is dependent. */
11109 cp_parser_nested_name_specifier_opt (parser
,
11110 /*typename_keyword_p=*/true,
11111 /*check_dependency_p=*/true,
11114 /* For everything but enumeration types, consider a template-id.
11115 For an enumeration type, consider only a plain identifier. */
11116 if (tag_type
!= enum_type
)
11118 bool template_p
= false;
11121 /* Allow the `template' keyword. */
11122 template_p
= cp_parser_optional_template_keyword (parser
);
11123 /* If we didn't see `template', we don't know if there's a
11124 template-id or not. */
11126 cp_parser_parse_tentatively (parser
);
11127 /* Parse the template-id. */
11128 decl
= cp_parser_template_id (parser
, template_p
,
11129 /*check_dependency_p=*/true,
11131 /* If we didn't find a template-id, look for an ordinary
11133 if (!template_p
&& !cp_parser_parse_definitely (parser
))
11135 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11136 in effect, then we must assume that, upon instantiation, the
11137 template will correspond to a class. */
11138 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11139 && tag_type
== typename_type
)
11140 type
= make_typename_type (parser
->scope
, decl
,
11142 /*complain=*/tf_error
);
11144 type
= TREE_TYPE (decl
);
11149 identifier
= cp_parser_identifier (parser
);
11151 if (identifier
== error_mark_node
)
11153 parser
->scope
= NULL_TREE
;
11154 return error_mark_node
;
11157 /* For a `typename', we needn't call xref_tag. */
11158 if (tag_type
== typename_type
11159 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
11160 return cp_parser_make_typename_type (parser
, parser
->scope
,
11162 /* Look up a qualified name in the usual way. */
11166 tree ambiguous_decls
;
11168 decl
= cp_parser_lookup_name (parser
, identifier
,
11170 /*is_template=*/false,
11171 /*is_namespace=*/false,
11172 /*check_dependency=*/true,
11175 /* If the lookup was ambiguous, an error will already have been
11177 if (ambiguous_decls
)
11178 return error_mark_node
;
11180 /* If we are parsing friend declaration, DECL may be a
11181 TEMPLATE_DECL tree node here. However, we need to check
11182 whether this TEMPLATE_DECL results in valid code. Consider
11183 the following example:
11186 template <class T> class C {};
11189 template <class T> friend class N::C; // #1, valid code
11191 template <class T> class Y {
11192 friend class N::C; // #2, invalid code
11195 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11196 name lookup of `N::C'. We see that friend declaration must
11197 be template for the code to be valid. Note that
11198 processing_template_decl does not work here since it is
11199 always 1 for the above two cases. */
11201 decl
= (cp_parser_maybe_treat_template_as_class
11202 (decl
, /*tag_name_p=*/is_friend
11203 && parser
->num_template_parameter_lists
));
11205 if (TREE_CODE (decl
) != TYPE_DECL
)
11207 cp_parser_diagnose_invalid_type_name (parser
,
11210 return error_mark_node
;
11213 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
11215 bool allow_template
= (parser
->num_template_parameter_lists
11216 || DECL_SELF_REFERENCE_P (decl
));
11217 type
= check_elaborated_type_specifier (tag_type
, decl
,
11220 if (type
== error_mark_node
)
11221 return error_mark_node
;
11224 /* Forward declarations of nested types, such as
11229 are invalid unless all components preceding the final '::'
11230 are complete. If all enclosing types are complete, these
11231 declarations become merely pointless.
11233 Invalid forward declarations of nested types are errors
11234 caught elsewhere in parsing. Those that are pointless arrive
11237 if (cp_parser_declares_only_class_p (parser
)
11238 && !is_friend
&& !processing_explicit_instantiation
)
11239 warning (0, "declaration %qD does not declare anything", decl
);
11241 type
= TREE_TYPE (decl
);
11245 /* An elaborated-type-specifier sometimes introduces a new type and
11246 sometimes names an existing type. Normally, the rule is that it
11247 introduces a new type only if there is not an existing type of
11248 the same name already in scope. For example, given:
11251 void f() { struct S s; }
11253 the `struct S' in the body of `f' is the same `struct S' as in
11254 the global scope; the existing definition is used. However, if
11255 there were no global declaration, this would introduce a new
11256 local class named `S'.
11258 An exception to this rule applies to the following code:
11260 namespace N { struct S; }
11262 Here, the elaborated-type-specifier names a new type
11263 unconditionally; even if there is already an `S' in the
11264 containing scope this declaration names a new type.
11265 This exception only applies if the elaborated-type-specifier
11266 forms the complete declaration:
11270 A declaration consisting solely of `class-key identifier ;' is
11271 either a redeclaration of the name in the current scope or a
11272 forward declaration of the identifier as a class name. It
11273 introduces the name into the current scope.
11275 We are in this situation precisely when the next token is a `;'.
11277 An exception to the exception is that a `friend' declaration does
11278 *not* name a new type; i.e., given:
11280 struct S { friend struct T; };
11282 `T' is not a new type in the scope of `S'.
11284 Also, `new struct S' or `sizeof (struct S)' never results in the
11285 definition of a new type; a new type can only be declared in a
11286 declaration context. */
11292 /* Friends have special name lookup rules. */
11293 ts
= ts_within_enclosing_non_class
;
11294 else if (is_declaration
11295 && cp_lexer_next_token_is (parser
->lexer
,
11297 /* This is a `class-key identifier ;' */
11303 (parser
->num_template_parameter_lists
11304 && (cp_parser_next_token_starts_class_definition_p (parser
)
11305 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
11306 /* An unqualified name was used to reference this type, so
11307 there were no qualifying templates. */
11308 if (!cp_parser_check_template_parameters (parser
,
11309 /*num_templates=*/0))
11310 return error_mark_node
;
11311 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
11315 if (type
== error_mark_node
)
11316 return error_mark_node
;
11318 /* Allow attributes on forward declarations of classes. */
11321 if (TREE_CODE (type
) == TYPENAME_TYPE
)
11322 warning (OPT_Wattributes
,
11323 "attributes ignored on uninstantiated type");
11324 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
11325 && ! processing_explicit_instantiation
)
11326 warning (OPT_Wattributes
,
11327 "attributes ignored on template instantiation");
11328 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
11329 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
11331 warning (OPT_Wattributes
,
11332 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11335 if (tag_type
!= enum_type
)
11336 cp_parser_check_class_key (tag_type
, type
);
11338 /* A "<" cannot follow an elaborated type specifier. If that
11339 happens, the user was probably trying to form a template-id. */
11340 cp_parser_check_for_invalid_template_id (parser
, type
);
11345 /* Parse an enum-specifier.
11348 enum identifier [opt] { enumerator-list [opt] }
11351 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11354 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11355 if the token stream isn't an enum-specifier after all. */
11358 cp_parser_enum_specifier (cp_parser
* parser
)
11364 /* Parse tentatively so that we can back up if we don't find a
11366 cp_parser_parse_tentatively (parser
);
11368 /* Caller guarantees that the current token is 'enum', an identifier
11369 possibly follows, and the token after that is an opening brace.
11370 If we don't have an identifier, fabricate an anonymous name for
11371 the enumeration being defined. */
11372 cp_lexer_consume_token (parser
->lexer
);
11374 attributes
= cp_parser_attributes_opt (parser
);
11376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11377 identifier
= cp_parser_identifier (parser
);
11379 identifier
= make_anon_name ();
11381 /* Look for the `{' but don't consume it yet. */
11382 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11383 cp_parser_simulate_error (parser
);
11385 if (!cp_parser_parse_definitely (parser
))
11388 /* Issue an error message if type-definitions are forbidden here. */
11389 if (!cp_parser_check_type_definition (parser
))
11390 type
= error_mark_node
;
11392 /* Create the new type. We do this before consuming the opening
11393 brace so the enum will be recorded as being on the line of its
11394 tag (or the 'enum' keyword, if there is no tag). */
11395 type
= start_enum (identifier
);
11397 /* Consume the opening brace. */
11398 cp_lexer_consume_token (parser
->lexer
);
11400 if (type
== error_mark_node
)
11402 cp_parser_skip_to_end_of_block_or_statement (parser
);
11403 return error_mark_node
;
11406 /* If the next token is not '}', then there are some enumerators. */
11407 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11408 cp_parser_enumerator_list (parser
, type
);
11410 /* Consume the final '}'. */
11411 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11413 /* Look for trailing attributes to apply to this enumeration, and
11414 apply them if appropriate. */
11415 if (cp_parser_allow_gnu_extensions_p (parser
))
11417 tree trailing_attr
= cp_parser_attributes_opt (parser
);
11418 cplus_decl_attributes (&type
,
11420 (int) ATTR_FLAG_TYPE_IN_PLACE
);
11423 /* Finish up the enumeration. */
11424 finish_enum (type
);
11429 /* Parse an enumerator-list. The enumerators all have the indicated
11433 enumerator-definition
11434 enumerator-list , enumerator-definition */
11437 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
11441 /* Parse an enumerator-definition. */
11442 cp_parser_enumerator_definition (parser
, type
);
11444 /* If the next token is not a ',', we've reached the end of
11446 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11448 /* Otherwise, consume the `,' and keep going. */
11449 cp_lexer_consume_token (parser
->lexer
);
11450 /* If the next token is a `}', there is a trailing comma. */
11451 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11453 if (pedantic
&& !in_system_header
)
11454 pedwarn ("comma at end of enumerator list");
11460 /* Parse an enumerator-definition. The enumerator has the indicated
11463 enumerator-definition:
11465 enumerator = constant-expression
11471 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
11476 /* Look for the identifier. */
11477 identifier
= cp_parser_identifier (parser
);
11478 if (identifier
== error_mark_node
)
11481 /* If the next token is an '=', then there is an explicit value. */
11482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11484 /* Consume the `=' token. */
11485 cp_lexer_consume_token (parser
->lexer
);
11486 /* Parse the value. */
11487 value
= cp_parser_constant_expression (parser
,
11488 /*allow_non_constant_p=*/false,
11494 /* Create the enumerator. */
11495 build_enumerator (identifier
, value
, type
);
11498 /* Parse a namespace-name.
11501 original-namespace-name
11504 Returns the NAMESPACE_DECL for the namespace. */
11507 cp_parser_namespace_name (cp_parser
* parser
)
11510 tree namespace_decl
;
11512 /* Get the name of the namespace. */
11513 identifier
= cp_parser_identifier (parser
);
11514 if (identifier
== error_mark_node
)
11515 return error_mark_node
;
11517 /* Look up the identifier in the currently active scope. Look only
11518 for namespaces, due to:
11520 [basic.lookup.udir]
11522 When looking up a namespace-name in a using-directive or alias
11523 definition, only namespace names are considered.
11527 [basic.lookup.qual]
11529 During the lookup of a name preceding the :: scope resolution
11530 operator, object, function, and enumerator names are ignored.
11532 (Note that cp_parser_class_or_namespace_name only calls this
11533 function if the token after the name is the scope resolution
11535 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
11537 /*is_template=*/false,
11538 /*is_namespace=*/true,
11539 /*check_dependency=*/true,
11540 /*ambiguous_decls=*/NULL
);
11541 /* If it's not a namespace, issue an error. */
11542 if (namespace_decl
== error_mark_node
11543 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
11545 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
11546 error ("%qD is not a namespace-name", identifier
);
11547 cp_parser_error (parser
, "expected namespace-name");
11548 namespace_decl
= error_mark_node
;
11551 return namespace_decl
;
11554 /* Parse a namespace-definition.
11556 namespace-definition:
11557 named-namespace-definition
11558 unnamed-namespace-definition
11560 named-namespace-definition:
11561 original-namespace-definition
11562 extension-namespace-definition
11564 original-namespace-definition:
11565 namespace identifier { namespace-body }
11567 extension-namespace-definition:
11568 namespace original-namespace-name { namespace-body }
11570 unnamed-namespace-definition:
11571 namespace { namespace-body } */
11574 cp_parser_namespace_definition (cp_parser
* parser
)
11576 tree identifier
, attribs
;
11577 bool has_visibility
;
11580 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
11583 cp_lexer_consume_token (parser
->lexer
);
11588 /* Look for the `namespace' keyword. */
11589 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11591 /* Get the name of the namespace. We do not attempt to distinguish
11592 between an original-namespace-definition and an
11593 extension-namespace-definition at this point. The semantic
11594 analysis routines are responsible for that. */
11595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11596 identifier
= cp_parser_identifier (parser
);
11598 identifier
= NULL_TREE
;
11600 /* Parse any specified attributes. */
11601 attribs
= cp_parser_attributes_opt (parser
);
11603 /* Look for the `{' to start the namespace. */
11604 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
11605 /* Start the namespace. */
11606 push_namespace (identifier
);
11608 /* "inline namespace" is equivalent to a stub namespace definition
11609 followed by a strong using directive. */
11612 tree
namespace = current_namespace
;
11613 /* Set up namespace association. */
11614 DECL_NAMESPACE_ASSOCIATIONS (namespace)
11615 = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE
,
11616 DECL_NAMESPACE_ASSOCIATIONS (namespace));
11617 /* Import the contents of the inline namespace. */
11619 do_using_directive (namespace);
11620 push_namespace (identifier
);
11623 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
11625 /* Parse the body of the namespace. */
11626 cp_parser_namespace_body (parser
);
11628 #ifdef HANDLE_PRAGMA_VISIBILITY
11629 if (has_visibility
)
11633 /* Finish the namespace. */
11635 /* Look for the final `}'. */
11636 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11639 /* Parse a namespace-body.
11642 declaration-seq [opt] */
11645 cp_parser_namespace_body (cp_parser
* parser
)
11647 cp_parser_declaration_seq_opt (parser
);
11650 /* Parse a namespace-alias-definition.
11652 namespace-alias-definition:
11653 namespace identifier = qualified-namespace-specifier ; */
11656 cp_parser_namespace_alias_definition (cp_parser
* parser
)
11659 tree namespace_specifier
;
11661 /* Look for the `namespace' keyword. */
11662 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11663 /* Look for the identifier. */
11664 identifier
= cp_parser_identifier (parser
);
11665 if (identifier
== error_mark_node
)
11667 /* Look for the `=' token. */
11668 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
11669 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11671 error ("%<namespace%> definition is not allowed here");
11672 /* Skip the definition. */
11673 cp_lexer_consume_token (parser
->lexer
);
11674 if (cp_parser_skip_to_closing_brace (parser
))
11675 cp_lexer_consume_token (parser
->lexer
);
11678 cp_parser_require (parser
, CPP_EQ
, "`='");
11679 /* Look for the qualified-namespace-specifier. */
11680 namespace_specifier
11681 = cp_parser_qualified_namespace_specifier (parser
);
11682 /* Look for the `;' token. */
11683 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11685 /* Register the alias in the symbol table. */
11686 do_namespace_alias (identifier
, namespace_specifier
);
11689 /* Parse a qualified-namespace-specifier.
11691 qualified-namespace-specifier:
11692 :: [opt] nested-name-specifier [opt] namespace-name
11694 Returns a NAMESPACE_DECL corresponding to the specified
11698 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
11700 /* Look for the optional `::'. */
11701 cp_parser_global_scope_opt (parser
,
11702 /*current_scope_valid_p=*/false);
11704 /* Look for the optional nested-name-specifier. */
11705 cp_parser_nested_name_specifier_opt (parser
,
11706 /*typename_keyword_p=*/false,
11707 /*check_dependency_p=*/true,
11709 /*is_declaration=*/true);
11711 return cp_parser_namespace_name (parser
);
11714 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11715 access declaration.
11718 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11719 using :: unqualified-id ;
11721 access-declaration:
11727 cp_parser_using_declaration (cp_parser
* parser
,
11728 bool access_declaration_p
)
11731 bool typename_p
= false;
11732 bool global_scope_p
;
11737 if (access_declaration_p
)
11738 cp_parser_parse_tentatively (parser
);
11741 /* Look for the `using' keyword. */
11742 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11744 /* Peek at the next token. */
11745 token
= cp_lexer_peek_token (parser
->lexer
);
11746 /* See if it's `typename'. */
11747 if (token
->keyword
== RID_TYPENAME
)
11749 /* Remember that we've seen it. */
11751 /* Consume the `typename' token. */
11752 cp_lexer_consume_token (parser
->lexer
);
11756 /* Look for the optional global scope qualification. */
11758 = (cp_parser_global_scope_opt (parser
,
11759 /*current_scope_valid_p=*/false)
11762 /* If we saw `typename', or didn't see `::', then there must be a
11763 nested-name-specifier present. */
11764 if (typename_p
|| !global_scope_p
)
11765 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
11766 /*check_dependency_p=*/true,
11768 /*is_declaration=*/true);
11769 /* Otherwise, we could be in either of the two productions. In that
11770 case, treat the nested-name-specifier as optional. */
11772 qscope
= cp_parser_nested_name_specifier_opt (parser
,
11773 /*typename_keyword_p=*/false,
11774 /*check_dependency_p=*/true,
11776 /*is_declaration=*/true);
11778 qscope
= global_namespace
;
11780 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
11781 /* Something has already gone wrong; there's no need to parse
11782 further. Since an error has occurred, the return value of
11783 cp_parser_parse_definitely will be false, as required. */
11784 return cp_parser_parse_definitely (parser
);
11786 /* Parse the unqualified-id. */
11787 identifier
= cp_parser_unqualified_id (parser
,
11788 /*template_keyword_p=*/false,
11789 /*check_dependency_p=*/true,
11790 /*declarator_p=*/true,
11791 /*optional_p=*/false);
11793 if (access_declaration_p
)
11795 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11796 cp_parser_simulate_error (parser
);
11797 if (!cp_parser_parse_definitely (parser
))
11801 /* The function we call to handle a using-declaration is different
11802 depending on what scope we are in. */
11803 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
11805 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
11806 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
11807 /* [namespace.udecl]
11809 A using declaration shall not name a template-id. */
11810 error ("a template-id may not appear in a using-declaration");
11813 if (at_class_scope_p ())
11815 /* Create the USING_DECL. */
11816 decl
= do_class_using_decl (parser
->scope
, identifier
);
11818 if (check_for_bare_parameter_packs (decl
))
11821 /* Add it to the list of members in this class. */
11822 finish_member_declaration (decl
);
11826 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
11827 if (decl
== error_mark_node
)
11828 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
11829 else if (check_for_bare_parameter_packs (decl
))
11831 else if (!at_namespace_scope_p ())
11832 do_local_using_decl (decl
, qscope
, identifier
);
11834 do_toplevel_using_decl (decl
, qscope
, identifier
);
11838 /* Look for the final `;'. */
11839 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11844 /* Parse a using-directive.
11847 using namespace :: [opt] nested-name-specifier [opt]
11848 namespace-name ; */
11851 cp_parser_using_directive (cp_parser
* parser
)
11853 tree namespace_decl
;
11856 /* Look for the `using' keyword. */
11857 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11858 /* And the `namespace' keyword. */
11859 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11860 /* Look for the optional `::' operator. */
11861 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
11862 /* And the optional nested-name-specifier. */
11863 cp_parser_nested_name_specifier_opt (parser
,
11864 /*typename_keyword_p=*/false,
11865 /*check_dependency_p=*/true,
11867 /*is_declaration=*/true);
11868 /* Get the namespace being used. */
11869 namespace_decl
= cp_parser_namespace_name (parser
);
11870 /* And any specified attributes. */
11871 attribs
= cp_parser_attributes_opt (parser
);
11872 /* Update the symbol table. */
11873 parse_using_directive (namespace_decl
, attribs
);
11874 /* Look for the final `;'. */
11875 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11878 /* Parse an asm-definition.
11881 asm ( string-literal ) ;
11886 asm volatile [opt] ( string-literal ) ;
11887 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11888 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11889 : asm-operand-list [opt] ) ;
11890 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11891 : asm-operand-list [opt]
11892 : asm-operand-list [opt] ) ; */
11895 cp_parser_asm_definition (cp_parser
* parser
)
11898 tree outputs
= NULL_TREE
;
11899 tree inputs
= NULL_TREE
;
11900 tree clobbers
= NULL_TREE
;
11902 bool volatile_p
= false;
11903 bool extended_p
= false;
11904 bool invalid_inputs_p
= false;
11905 bool invalid_outputs_p
= false;
11907 /* Look for the `asm' keyword. */
11908 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
11909 /* See if the next token is `volatile'. */
11910 if (cp_parser_allow_gnu_extensions_p (parser
)
11911 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
11913 /* Remember that we saw the `volatile' keyword. */
11915 /* Consume the token. */
11916 cp_lexer_consume_token (parser
->lexer
);
11918 /* Look for the opening `('. */
11919 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
11921 /* Look for the string. */
11922 string
= cp_parser_string_literal (parser
, false, false);
11923 if (string
== error_mark_node
)
11925 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11926 /*consume_paren=*/true);
11930 /* If we're allowing GNU extensions, check for the extended assembly
11931 syntax. Unfortunately, the `:' tokens need not be separated by
11932 a space in C, and so, for compatibility, we tolerate that here
11933 too. Doing that means that we have to treat the `::' operator as
11935 if (cp_parser_allow_gnu_extensions_p (parser
)
11936 && parser
->in_function_body
11937 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
11938 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
11940 bool inputs_p
= false;
11941 bool clobbers_p
= false;
11943 /* The extended syntax was used. */
11946 /* Look for outputs. */
11947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11949 /* Consume the `:'. */
11950 cp_lexer_consume_token (parser
->lexer
);
11951 /* Parse the output-operands. */
11952 if (cp_lexer_next_token_is_not (parser
->lexer
,
11954 && cp_lexer_next_token_is_not (parser
->lexer
,
11956 && cp_lexer_next_token_is_not (parser
->lexer
,
11958 outputs
= cp_parser_asm_operand_list (parser
);
11960 if (outputs
== error_mark_node
)
11961 invalid_outputs_p
= true;
11963 /* If the next token is `::', there are no outputs, and the
11964 next token is the beginning of the inputs. */
11965 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11966 /* The inputs are coming next. */
11969 /* Look for inputs. */
11971 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11973 /* Consume the `:' or `::'. */
11974 cp_lexer_consume_token (parser
->lexer
);
11975 /* Parse the output-operands. */
11976 if (cp_lexer_next_token_is_not (parser
->lexer
,
11978 && cp_lexer_next_token_is_not (parser
->lexer
,
11980 inputs
= cp_parser_asm_operand_list (parser
);
11982 if (inputs
== error_mark_node
)
11983 invalid_inputs_p
= true;
11985 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11986 /* The clobbers are coming next. */
11989 /* Look for clobbers. */
11991 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11993 /* Consume the `:' or `::'. */
11994 cp_lexer_consume_token (parser
->lexer
);
11995 /* Parse the clobbers. */
11996 if (cp_lexer_next_token_is_not (parser
->lexer
,
11998 clobbers
= cp_parser_asm_clobber_list (parser
);
12001 /* Look for the closing `)'. */
12002 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
12003 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12004 /*consume_paren=*/true);
12005 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12007 if (!invalid_inputs_p
&& !invalid_outputs_p
)
12009 /* Create the ASM_EXPR. */
12010 if (parser
->in_function_body
)
12012 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
12014 /* If the extended syntax was not used, mark the ASM_EXPR. */
12017 tree temp
= asm_stmt
;
12018 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
12019 temp
= TREE_OPERAND (temp
, 0);
12021 ASM_INPUT_P (temp
) = 1;
12025 cgraph_add_asm_node (string
);
12029 /* Declarators [gram.dcl.decl] */
12031 /* Parse an init-declarator.
12034 declarator initializer [opt]
12039 declarator asm-specification [opt] attributes [opt] initializer [opt]
12041 function-definition:
12042 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12044 decl-specifier-seq [opt] declarator function-try-block
12048 function-definition:
12049 __extension__ function-definition
12051 The DECL_SPECIFIERS apply to this declarator. Returns a
12052 representation of the entity declared. If MEMBER_P is TRUE, then
12053 this declarator appears in a class scope. The new DECL created by
12054 this declarator is returned.
12056 The CHECKS are access checks that should be performed once we know
12057 what entity is being declared (and, therefore, what classes have
12060 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12061 for a function-definition here as well. If the declarator is a
12062 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12063 be TRUE upon return. By that point, the function-definition will
12064 have been completely parsed.
12066 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12070 cp_parser_init_declarator (cp_parser
* parser
,
12071 cp_decl_specifier_seq
*decl_specifiers
,
12072 VEC (deferred_access_check
,gc
)* checks
,
12073 bool function_definition_allowed_p
,
12075 int declares_class_or_enum
,
12076 bool* function_definition_p
)
12079 cp_declarator
*declarator
;
12080 tree prefix_attributes
;
12082 tree asm_specification
;
12084 tree decl
= NULL_TREE
;
12086 bool is_initialized
;
12087 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12088 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12090 enum cpp_ttype initialization_kind
;
12091 bool is_parenthesized_init
= false;
12092 bool is_non_constant_init
;
12093 int ctor_dtor_or_conv_p
;
12095 tree pushed_scope
= NULL
;
12097 /* Gather the attributes that were provided with the
12098 decl-specifiers. */
12099 prefix_attributes
= decl_specifiers
->attributes
;
12101 /* Assume that this is not the declarator for a function
12103 if (function_definition_p
)
12104 *function_definition_p
= false;
12106 /* Defer access checks while parsing the declarator; we cannot know
12107 what names are accessible until we know what is being
12109 resume_deferring_access_checks ();
12111 /* Parse the declarator. */
12113 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12114 &ctor_dtor_or_conv_p
,
12115 /*parenthesized_p=*/NULL
,
12116 /*member_p=*/false);
12117 /* Gather up the deferred checks. */
12118 stop_deferring_access_checks ();
12120 /* If the DECLARATOR was erroneous, there's no need to go
12122 if (declarator
== cp_error_declarator
)
12123 return error_mark_node
;
12125 /* Check that the number of template-parameter-lists is OK. */
12126 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
12127 return error_mark_node
;
12129 if (declares_class_or_enum
& 2)
12130 cp_parser_check_for_definition_in_return_type (declarator
,
12131 decl_specifiers
->type
);
12133 /* Figure out what scope the entity declared by the DECLARATOR is
12134 located in. `grokdeclarator' sometimes changes the scope, so
12135 we compute it now. */
12136 scope
= get_scope_of_declarator (declarator
);
12138 /* If we're allowing GNU extensions, look for an asm-specification
12140 if (cp_parser_allow_gnu_extensions_p (parser
))
12142 /* Look for an asm-specification. */
12143 asm_specification
= cp_parser_asm_specification_opt (parser
);
12144 /* And attributes. */
12145 attributes
= cp_parser_attributes_opt (parser
);
12149 asm_specification
= NULL_TREE
;
12150 attributes
= NULL_TREE
;
12153 /* Peek at the next token. */
12154 token
= cp_lexer_peek_token (parser
->lexer
);
12155 /* Check to see if the token indicates the start of a
12156 function-definition. */
12157 if (cp_parser_token_starts_function_definition_p (token
))
12159 if (!function_definition_allowed_p
)
12161 /* If a function-definition should not appear here, issue an
12163 cp_parser_error (parser
,
12164 "a function-definition is not allowed here");
12165 return error_mark_node
;
12169 /* Neither attributes nor an asm-specification are allowed
12170 on a function-definition. */
12171 if (asm_specification
)
12172 error ("an asm-specification is not allowed on a function-definition");
12174 error ("attributes are not allowed on a function-definition");
12175 /* This is a function-definition. */
12176 *function_definition_p
= true;
12178 /* Parse the function definition. */
12180 decl
= cp_parser_save_member_function_body (parser
,
12183 prefix_attributes
);
12186 = (cp_parser_function_definition_from_specifiers_and_declarator
12187 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
12195 Only in function declarations for constructors, destructors, and
12196 type conversions can the decl-specifier-seq be omitted.
12198 We explicitly postpone this check past the point where we handle
12199 function-definitions because we tolerate function-definitions
12200 that are missing their return types in some modes. */
12201 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
12203 cp_parser_error (parser
,
12204 "expected constructor, destructor, or type conversion");
12205 return error_mark_node
;
12208 /* An `=' or an `(' indicates an initializer. */
12209 if (token
->type
== CPP_EQ
12210 || token
->type
== CPP_OPEN_PAREN
)
12212 is_initialized
= true;
12213 initialization_kind
= token
->type
;
12217 /* If the init-declarator isn't initialized and isn't followed by a
12218 `,' or `;', it's not a valid init-declarator. */
12219 if (token
->type
!= CPP_COMMA
12220 && token
->type
!= CPP_SEMICOLON
)
12222 cp_parser_error (parser
, "expected initializer");
12223 return error_mark_node
;
12225 is_initialized
= false;
12226 initialization_kind
= CPP_EOF
;
12229 /* Because start_decl has side-effects, we should only call it if we
12230 know we're going ahead. By this point, we know that we cannot
12231 possibly be looking at any other construct. */
12232 cp_parser_commit_to_tentative_parse (parser
);
12234 /* If the decl specifiers were bad, issue an error now that we're
12235 sure this was intended to be a declarator. Then continue
12236 declaring the variable(s), as int, to try to cut down on further
12238 if (decl_specifiers
->any_specifiers_p
12239 && decl_specifiers
->type
== error_mark_node
)
12241 cp_parser_error (parser
, "invalid type in declaration");
12242 decl_specifiers
->type
= integer_type_node
;
12245 /* Check to see whether or not this declaration is a friend. */
12246 friend_p
= cp_parser_friend_p (decl_specifiers
);
12248 /* Enter the newly declared entry in the symbol table. If we're
12249 processing a declaration in a class-specifier, we wait until
12250 after processing the initializer. */
12253 if (parser
->in_unbraced_linkage_specification_p
)
12254 decl_specifiers
->storage_class
= sc_extern
;
12255 decl
= start_decl (declarator
, decl_specifiers
,
12256 is_initialized
, attributes
, prefix_attributes
,
12260 /* Enter the SCOPE. That way unqualified names appearing in the
12261 initializer will be looked up in SCOPE. */
12262 pushed_scope
= push_scope (scope
);
12264 /* Perform deferred access control checks, now that we know in which
12265 SCOPE the declared entity resides. */
12266 if (!member_p
&& decl
)
12268 tree saved_current_function_decl
= NULL_TREE
;
12270 /* If the entity being declared is a function, pretend that we
12271 are in its scope. If it is a `friend', it may have access to
12272 things that would not otherwise be accessible. */
12273 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12275 saved_current_function_decl
= current_function_decl
;
12276 current_function_decl
= decl
;
12279 /* Perform access checks for template parameters. */
12280 cp_parser_perform_template_parameter_access_checks (checks
);
12282 /* Perform the access control checks for the declarator and the
12283 the decl-specifiers. */
12284 perform_deferred_access_checks ();
12286 /* Restore the saved value. */
12287 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12288 current_function_decl
= saved_current_function_decl
;
12291 /* Parse the initializer. */
12292 initializer
= NULL_TREE
;
12293 is_parenthesized_init
= false;
12294 is_non_constant_init
= true;
12295 if (is_initialized
)
12297 if (function_declarator_p (declarator
))
12299 if (initialization_kind
== CPP_EQ
)
12300 initializer
= cp_parser_pure_specifier (parser
);
12303 /* If the declaration was erroneous, we don't really
12304 know what the user intended, so just silently
12305 consume the initializer. */
12306 if (decl
!= error_mark_node
)
12307 error ("initializer provided for function");
12308 cp_parser_skip_to_closing_parenthesis (parser
,
12309 /*recovering=*/true,
12310 /*or_comma=*/false,
12311 /*consume_paren=*/true);
12315 initializer
= cp_parser_initializer (parser
,
12316 &is_parenthesized_init
,
12317 &is_non_constant_init
);
12320 /* The old parser allows attributes to appear after a parenthesized
12321 initializer. Mark Mitchell proposed removing this functionality
12322 on the GCC mailing lists on 2002-08-13. This parser accepts the
12323 attributes -- but ignores them. */
12324 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
12325 if (cp_parser_attributes_opt (parser
))
12326 warning (OPT_Wattributes
,
12327 "attributes after parenthesized initializer ignored");
12329 /* For an in-class declaration, use `grokfield' to create the
12335 pop_scope (pushed_scope
);
12336 pushed_scope
= false;
12338 decl
= grokfield (declarator
, decl_specifiers
,
12339 initializer
, !is_non_constant_init
,
12340 /*asmspec=*/NULL_TREE
,
12341 prefix_attributes
);
12342 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
12343 cp_parser_save_default_args (parser
, decl
);
12346 /* Finish processing the declaration. But, skip friend
12348 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
12350 cp_finish_decl (decl
,
12351 initializer
, !is_non_constant_init
,
12353 /* If the initializer is in parentheses, then this is
12354 a direct-initialization, which means that an
12355 `explicit' constructor is OK. Otherwise, an
12356 `explicit' constructor cannot be used. */
12357 ((is_parenthesized_init
|| !is_initialized
)
12358 ? 0 : LOOKUP_ONLYCONVERTING
));
12360 else if ((cxx_dialect
!= cxx98
) && friend_p
12361 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
12362 /* Core issue #226 (C++0x only): A default template-argument
12363 shall not be specified in a friend class template
12365 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
12366 /*is_partial=*/0, /*is_friend_decl=*/1);
12368 if (!friend_p
&& pushed_scope
)
12369 pop_scope (pushed_scope
);
12374 /* Parse a declarator.
12378 ptr-operator declarator
12380 abstract-declarator:
12381 ptr-operator abstract-declarator [opt]
12382 direct-abstract-declarator
12387 attributes [opt] direct-declarator
12388 attributes [opt] ptr-operator declarator
12390 abstract-declarator:
12391 attributes [opt] ptr-operator abstract-declarator [opt]
12392 attributes [opt] direct-abstract-declarator
12394 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12395 detect constructor, destructor or conversion operators. It is set
12396 to -1 if the declarator is a name, and +1 if it is a
12397 function. Otherwise it is set to zero. Usually you just want to
12398 test for >0, but internally the negative value is used.
12400 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12401 a decl-specifier-seq unless it declares a constructor, destructor,
12402 or conversion. It might seem that we could check this condition in
12403 semantic analysis, rather than parsing, but that makes it difficult
12404 to handle something like `f()'. We want to notice that there are
12405 no decl-specifiers, and therefore realize that this is an
12406 expression, not a declaration.)
12408 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12409 the declarator is a direct-declarator of the form "(...)".
12411 MEMBER_P is true iff this declarator is a member-declarator. */
12413 static cp_declarator
*
12414 cp_parser_declarator (cp_parser
* parser
,
12415 cp_parser_declarator_kind dcl_kind
,
12416 int* ctor_dtor_or_conv_p
,
12417 bool* parenthesized_p
,
12421 cp_declarator
*declarator
;
12422 enum tree_code code
;
12423 cp_cv_quals cv_quals
;
12425 tree attributes
= NULL_TREE
;
12427 /* Assume this is not a constructor, destructor, or type-conversion
12429 if (ctor_dtor_or_conv_p
)
12430 *ctor_dtor_or_conv_p
= 0;
12432 if (cp_parser_allow_gnu_extensions_p (parser
))
12433 attributes
= cp_parser_attributes_opt (parser
);
12435 /* Peek at the next token. */
12436 token
= cp_lexer_peek_token (parser
->lexer
);
12438 /* Check for the ptr-operator production. */
12439 cp_parser_parse_tentatively (parser
);
12440 /* Parse the ptr-operator. */
12441 code
= cp_parser_ptr_operator (parser
,
12444 /* If that worked, then we have a ptr-operator. */
12445 if (cp_parser_parse_definitely (parser
))
12447 /* If a ptr-operator was found, then this declarator was not
12449 if (parenthesized_p
)
12450 *parenthesized_p
= true;
12451 /* The dependent declarator is optional if we are parsing an
12452 abstract-declarator. */
12453 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12454 cp_parser_parse_tentatively (parser
);
12456 /* Parse the dependent declarator. */
12457 declarator
= cp_parser_declarator (parser
, dcl_kind
,
12458 /*ctor_dtor_or_conv_p=*/NULL
,
12459 /*parenthesized_p=*/NULL
,
12460 /*member_p=*/false);
12462 /* If we are parsing an abstract-declarator, we must handle the
12463 case where the dependent declarator is absent. */
12464 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
12465 && !cp_parser_parse_definitely (parser
))
12468 declarator
= cp_parser_make_indirect_declarator
12469 (code
, class_type
, cv_quals
, declarator
);
12471 /* Everything else is a direct-declarator. */
12474 if (parenthesized_p
)
12475 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
12477 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
12478 ctor_dtor_or_conv_p
,
12482 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
12483 declarator
->attributes
= attributes
;
12488 /* Parse a direct-declarator or direct-abstract-declarator.
12492 direct-declarator ( parameter-declaration-clause )
12493 cv-qualifier-seq [opt]
12494 exception-specification [opt]
12495 direct-declarator [ constant-expression [opt] ]
12498 direct-abstract-declarator:
12499 direct-abstract-declarator [opt]
12500 ( parameter-declaration-clause )
12501 cv-qualifier-seq [opt]
12502 exception-specification [opt]
12503 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12504 ( abstract-declarator )
12506 Returns a representation of the declarator. DCL_KIND is
12507 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12508 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12509 we are parsing a direct-declarator. It is
12510 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12511 of ambiguity we prefer an abstract declarator, as per
12512 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12513 cp_parser_declarator. */
12515 static cp_declarator
*
12516 cp_parser_direct_declarator (cp_parser
* parser
,
12517 cp_parser_declarator_kind dcl_kind
,
12518 int* ctor_dtor_or_conv_p
,
12522 cp_declarator
*declarator
= NULL
;
12523 tree scope
= NULL_TREE
;
12524 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12525 bool saved_in_declarator_p
= parser
->in_declarator_p
;
12527 tree pushed_scope
= NULL_TREE
;
12531 /* Peek at the next token. */
12532 token
= cp_lexer_peek_token (parser
->lexer
);
12533 if (token
->type
== CPP_OPEN_PAREN
)
12535 /* This is either a parameter-declaration-clause, or a
12536 parenthesized declarator. When we know we are parsing a
12537 named declarator, it must be a parenthesized declarator
12538 if FIRST is true. For instance, `(int)' is a
12539 parameter-declaration-clause, with an omitted
12540 direct-abstract-declarator. But `((*))', is a
12541 parenthesized abstract declarator. Finally, when T is a
12542 template parameter `(T)' is a
12543 parameter-declaration-clause, and not a parenthesized
12546 We first try and parse a parameter-declaration-clause,
12547 and then try a nested declarator (if FIRST is true).
12549 It is not an error for it not to be a
12550 parameter-declaration-clause, even when FIRST is
12556 The first is the declaration of a function while the
12557 second is a the definition of a variable, including its
12560 Having seen only the parenthesis, we cannot know which of
12561 these two alternatives should be selected. Even more
12562 complex are examples like:
12567 The former is a function-declaration; the latter is a
12568 variable initialization.
12570 Thus again, we try a parameter-declaration-clause, and if
12571 that fails, we back out and return. */
12573 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12575 cp_parameter_declarator
*params
;
12576 unsigned saved_num_template_parameter_lists
;
12578 /* In a member-declarator, the only valid interpretation
12579 of a parenthesis is the start of a
12580 parameter-declaration-clause. (It is invalid to
12581 initialize a static data member with a parenthesized
12582 initializer; only the "=" form of initialization is
12585 cp_parser_parse_tentatively (parser
);
12587 /* Consume the `('. */
12588 cp_lexer_consume_token (parser
->lexer
);
12591 /* If this is going to be an abstract declarator, we're
12592 in a declarator and we can't have default args. */
12593 parser
->default_arg_ok_p
= false;
12594 parser
->in_declarator_p
= true;
12597 /* Inside the function parameter list, surrounding
12598 template-parameter-lists do not apply. */
12599 saved_num_template_parameter_lists
12600 = parser
->num_template_parameter_lists
;
12601 parser
->num_template_parameter_lists
= 0;
12603 /* Parse the parameter-declaration-clause. */
12604 params
= cp_parser_parameter_declaration_clause (parser
);
12606 parser
->num_template_parameter_lists
12607 = saved_num_template_parameter_lists
;
12609 /* If all went well, parse the cv-qualifier-seq and the
12610 exception-specification. */
12611 if (member_p
|| cp_parser_parse_definitely (parser
))
12613 cp_cv_quals cv_quals
;
12614 tree exception_specification
;
12616 if (ctor_dtor_or_conv_p
)
12617 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
12619 /* Consume the `)'. */
12620 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12622 /* Parse the cv-qualifier-seq. */
12623 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12624 /* And the exception-specification. */
12625 exception_specification
12626 = cp_parser_exception_specification_opt (parser
);
12628 /* Create the function-declarator. */
12629 declarator
= make_call_declarator (declarator
,
12632 exception_specification
);
12633 /* Any subsequent parameter lists are to do with
12634 return type, so are not those of the declared
12636 parser
->default_arg_ok_p
= false;
12638 /* Repeat the main loop. */
12643 /* If this is the first, we can try a parenthesized
12647 bool saved_in_type_id_in_expr_p
;
12649 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12650 parser
->in_declarator_p
= saved_in_declarator_p
;
12652 /* Consume the `('. */
12653 cp_lexer_consume_token (parser
->lexer
);
12654 /* Parse the nested declarator. */
12655 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
12656 parser
->in_type_id_in_expr_p
= true;
12658 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
12659 /*parenthesized_p=*/NULL
,
12661 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
12663 /* Expect a `)'. */
12664 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
12665 declarator
= cp_error_declarator
;
12666 if (declarator
== cp_error_declarator
)
12669 goto handle_declarator
;
12671 /* Otherwise, we must be done. */
12675 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12676 && token
->type
== CPP_OPEN_SQUARE
)
12678 /* Parse an array-declarator. */
12681 if (ctor_dtor_or_conv_p
)
12682 *ctor_dtor_or_conv_p
= 0;
12685 parser
->default_arg_ok_p
= false;
12686 parser
->in_declarator_p
= true;
12687 /* Consume the `['. */
12688 cp_lexer_consume_token (parser
->lexer
);
12689 /* Peek at the next token. */
12690 token
= cp_lexer_peek_token (parser
->lexer
);
12691 /* If the next token is `]', then there is no
12692 constant-expression. */
12693 if (token
->type
!= CPP_CLOSE_SQUARE
)
12695 bool non_constant_p
;
12698 = cp_parser_constant_expression (parser
,
12699 /*allow_non_constant=*/true,
12701 if (!non_constant_p
)
12702 bounds
= fold_non_dependent_expr (bounds
);
12703 /* Normally, the array bound must be an integral constant
12704 expression. However, as an extension, we allow VLAs
12705 in function scopes. */
12706 else if (!parser
->in_function_body
)
12708 error ("array bound is not an integer constant");
12709 bounds
= error_mark_node
;
12713 bounds
= NULL_TREE
;
12714 /* Look for the closing `]'. */
12715 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
12717 declarator
= cp_error_declarator
;
12721 declarator
= make_array_declarator (declarator
, bounds
);
12723 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
12725 tree qualifying_scope
;
12726 tree unqualified_name
;
12727 special_function_kind sfk
;
12729 bool pack_expansion_p
= false;
12731 /* Parse a declarator-id */
12732 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
12735 cp_parser_parse_tentatively (parser
);
12737 /* If we see an ellipsis, we should be looking at a
12739 if (token
->type
== CPP_ELLIPSIS
)
12741 /* Consume the `...' */
12742 cp_lexer_consume_token (parser
->lexer
);
12744 pack_expansion_p
= true;
12749 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
12750 qualifying_scope
= parser
->scope
;
12755 if (!unqualified_name
&& pack_expansion_p
)
12757 /* Check whether an error occurred. */
12758 okay
= !cp_parser_error_occurred (parser
);
12760 /* We already consumed the ellipsis to mark a
12761 parameter pack, but we have no way to report it,
12762 so abort the tentative parse. We will be exiting
12763 immediately anyway. */
12764 cp_parser_abort_tentative_parse (parser
);
12767 okay
= cp_parser_parse_definitely (parser
);
12770 unqualified_name
= error_mark_node
;
12771 else if (unqualified_name
12772 && (qualifying_scope
12773 || (TREE_CODE (unqualified_name
)
12774 != IDENTIFIER_NODE
)))
12776 cp_parser_error (parser
, "expected unqualified-id");
12777 unqualified_name
= error_mark_node
;
12781 if (!unqualified_name
)
12783 if (unqualified_name
== error_mark_node
)
12785 declarator
= cp_error_declarator
;
12786 pack_expansion_p
= false;
12787 declarator
->parameter_pack_p
= false;
12791 if (qualifying_scope
&& at_namespace_scope_p ()
12792 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
12794 /* In the declaration of a member of a template class
12795 outside of the class itself, the SCOPE will sometimes
12796 be a TYPENAME_TYPE. For example, given:
12798 template <typename T>
12799 int S<T>::R::i = 3;
12801 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12802 this context, we must resolve S<T>::R to an ordinary
12803 type, rather than a typename type.
12805 The reason we normally avoid resolving TYPENAME_TYPEs
12806 is that a specialization of `S' might render
12807 `S<T>::R' not a type. However, if `S' is
12808 specialized, then this `i' will not be used, so there
12809 is no harm in resolving the types here. */
12812 /* Resolve the TYPENAME_TYPE. */
12813 type
= resolve_typename_type (qualifying_scope
,
12814 /*only_current_p=*/false);
12815 /* If that failed, the declarator is invalid. */
12816 if (TREE_CODE (type
) == TYPENAME_TYPE
)
12817 error ("%<%T::%E%> is not a type",
12818 TYPE_CONTEXT (qualifying_scope
),
12819 TYPE_IDENTIFIER (qualifying_scope
));
12820 qualifying_scope
= type
;
12825 if (unqualified_name
)
12829 if (qualifying_scope
12830 && CLASS_TYPE_P (qualifying_scope
))
12831 class_type
= qualifying_scope
;
12833 class_type
= current_class_type
;
12835 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
12837 tree name_type
= TREE_TYPE (unqualified_name
);
12838 if (class_type
&& same_type_p (name_type
, class_type
))
12840 if (qualifying_scope
12841 && CLASSTYPE_USE_TEMPLATE (name_type
))
12843 error ("invalid use of constructor as a template");
12844 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12845 "name the constructor in a qualified name",
12847 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
12848 class_type
, name_type
);
12849 declarator
= cp_error_declarator
;
12853 unqualified_name
= constructor_name (class_type
);
12857 /* We do not attempt to print the declarator
12858 here because we do not have enough
12859 information about its original syntactic
12861 cp_parser_error (parser
, "invalid declarator");
12862 declarator
= cp_error_declarator
;
12869 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
12870 sfk
= sfk_destructor
;
12871 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
12872 sfk
= sfk_conversion
;
12873 else if (/* There's no way to declare a constructor
12874 for an anonymous type, even if the type
12875 got a name for linkage purposes. */
12876 !TYPE_WAS_ANONYMOUS (class_type
)
12877 && constructor_name_p (unqualified_name
,
12880 unqualified_name
= constructor_name (class_type
);
12881 sfk
= sfk_constructor
;
12884 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
12885 *ctor_dtor_or_conv_p
= -1;
12888 declarator
= make_id_declarator (qualifying_scope
,
12891 declarator
->id_loc
= token
->location
;
12892 declarator
->parameter_pack_p
= pack_expansion_p
;
12894 if (pack_expansion_p
)
12895 maybe_warn_variadic_templates ();
12897 handle_declarator
:;
12898 scope
= get_scope_of_declarator (declarator
);
12900 /* Any names that appear after the declarator-id for a
12901 member are looked up in the containing scope. */
12902 pushed_scope
= push_scope (scope
);
12903 parser
->in_declarator_p
= true;
12904 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
12905 || (declarator
&& declarator
->kind
== cdk_id
))
12906 /* Default args are only allowed on function
12908 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12910 parser
->default_arg_ok_p
= false;
12919 /* For an abstract declarator, we might wind up with nothing at this
12920 point. That's an error; the declarator is not optional. */
12922 cp_parser_error (parser
, "expected declarator");
12924 /* If we entered a scope, we must exit it now. */
12926 pop_scope (pushed_scope
);
12928 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12929 parser
->in_declarator_p
= saved_in_declarator_p
;
12934 /* Parse a ptr-operator.
12937 * cv-qualifier-seq [opt]
12939 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12944 & cv-qualifier-seq [opt]
12946 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12947 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12948 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12949 filled in with the TYPE containing the member. *CV_QUALS is
12950 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12951 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12952 Note that the tree codes returned by this function have nothing
12953 to do with the types of trees that will be eventually be created
12954 to represent the pointer or reference type being parsed. They are
12955 just constants with suggestive names. */
12956 static enum tree_code
12957 cp_parser_ptr_operator (cp_parser
* parser
,
12959 cp_cv_quals
*cv_quals
)
12961 enum tree_code code
= ERROR_MARK
;
12964 /* Assume that it's not a pointer-to-member. */
12966 /* And that there are no cv-qualifiers. */
12967 *cv_quals
= TYPE_UNQUALIFIED
;
12969 /* Peek at the next token. */
12970 token
= cp_lexer_peek_token (parser
->lexer
);
12972 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12973 if (token
->type
== CPP_MULT
)
12974 code
= INDIRECT_REF
;
12975 else if (token
->type
== CPP_AND
)
12977 else if ((cxx_dialect
!= cxx98
) &&
12978 token
->type
== CPP_AND_AND
) /* C++0x only */
12979 code
= NON_LVALUE_EXPR
;
12981 if (code
!= ERROR_MARK
)
12983 /* Consume the `*', `&' or `&&'. */
12984 cp_lexer_consume_token (parser
->lexer
);
12986 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12987 `&', if we are allowing GNU extensions. (The only qualifier
12988 that can legally appear after `&' is `restrict', but that is
12989 enforced during semantic analysis. */
12990 if (code
== INDIRECT_REF
12991 || cp_parser_allow_gnu_extensions_p (parser
))
12992 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12996 /* Try the pointer-to-member case. */
12997 cp_parser_parse_tentatively (parser
);
12998 /* Look for the optional `::' operator. */
12999 cp_parser_global_scope_opt (parser
,
13000 /*current_scope_valid_p=*/false);
13001 /* Look for the nested-name specifier. */
13002 cp_parser_nested_name_specifier (parser
,
13003 /*typename_keyword_p=*/false,
13004 /*check_dependency_p=*/true,
13006 /*is_declaration=*/false);
13007 /* If we found it, and the next token is a `*', then we are
13008 indeed looking at a pointer-to-member operator. */
13009 if (!cp_parser_error_occurred (parser
)
13010 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
13012 /* Indicate that the `*' operator was used. */
13013 code
= INDIRECT_REF
;
13015 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
13016 error ("%qD is a namespace", parser
->scope
);
13019 /* The type of which the member is a member is given by the
13021 *type
= parser
->scope
;
13022 /* The next name will not be qualified. */
13023 parser
->scope
= NULL_TREE
;
13024 parser
->qualifying_scope
= NULL_TREE
;
13025 parser
->object_scope
= NULL_TREE
;
13026 /* Look for the optional cv-qualifier-seq. */
13027 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
13030 /* If that didn't work we don't have a ptr-operator. */
13031 if (!cp_parser_parse_definitely (parser
))
13032 cp_parser_error (parser
, "expected ptr-operator");
13038 /* Parse an (optional) cv-qualifier-seq.
13041 cv-qualifier cv-qualifier-seq [opt]
13052 Returns a bitmask representing the cv-qualifiers. */
13055 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
13057 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
13062 cp_cv_quals cv_qualifier
;
13064 /* Peek at the next token. */
13065 token
= cp_lexer_peek_token (parser
->lexer
);
13066 /* See if it's a cv-qualifier. */
13067 switch (token
->keyword
)
13070 cv_qualifier
= TYPE_QUAL_CONST
;
13074 cv_qualifier
= TYPE_QUAL_VOLATILE
;
13078 cv_qualifier
= TYPE_QUAL_RESTRICT
;
13082 cv_qualifier
= TYPE_UNQUALIFIED
;
13089 if (cv_quals
& cv_qualifier
)
13091 error ("duplicate cv-qualifier");
13092 cp_lexer_purge_token (parser
->lexer
);
13096 cp_lexer_consume_token (parser
->lexer
);
13097 cv_quals
|= cv_qualifier
;
13104 /* Parse a declarator-id.
13108 :: [opt] nested-name-specifier [opt] type-name
13110 In the `id-expression' case, the value returned is as for
13111 cp_parser_id_expression if the id-expression was an unqualified-id.
13112 If the id-expression was a qualified-id, then a SCOPE_REF is
13113 returned. The first operand is the scope (either a NAMESPACE_DECL
13114 or TREE_TYPE), but the second is still just a representation of an
13118 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
13121 /* The expression must be an id-expression. Assume that qualified
13122 names are the names of types so that:
13125 int S<T>::R::i = 3;
13127 will work; we must treat `S<T>::R' as the name of a type.
13128 Similarly, assume that qualified names are templates, where
13132 int S<T>::R<T>::i = 3;
13135 id
= cp_parser_id_expression (parser
,
13136 /*template_keyword_p=*/false,
13137 /*check_dependency_p=*/false,
13138 /*template_p=*/NULL
,
13139 /*declarator_p=*/true,
13141 if (id
&& BASELINK_P (id
))
13142 id
= BASELINK_FUNCTIONS (id
);
13146 /* Parse a type-id.
13149 type-specifier-seq abstract-declarator [opt]
13151 Returns the TYPE specified. */
13154 cp_parser_type_id (cp_parser
* parser
)
13156 cp_decl_specifier_seq type_specifier_seq
;
13157 cp_declarator
*abstract_declarator
;
13159 /* Parse the type-specifier-seq. */
13160 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
13161 &type_specifier_seq
);
13162 if (type_specifier_seq
.type
== error_mark_node
)
13163 return error_mark_node
;
13165 /* There might or might not be an abstract declarator. */
13166 cp_parser_parse_tentatively (parser
);
13167 /* Look for the declarator. */
13168 abstract_declarator
13169 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
13170 /*parenthesized_p=*/NULL
,
13171 /*member_p=*/false);
13172 /* Check to see if there really was a declarator. */
13173 if (!cp_parser_parse_definitely (parser
))
13174 abstract_declarator
= NULL
;
13176 return groktypename (&type_specifier_seq
, abstract_declarator
);
13179 /* Parse a type-specifier-seq.
13181 type-specifier-seq:
13182 type-specifier type-specifier-seq [opt]
13186 type-specifier-seq:
13187 attributes type-specifier-seq [opt]
13189 If IS_CONDITION is true, we are at the start of a "condition",
13190 e.g., we've just seen "if (".
13192 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13195 cp_parser_type_specifier_seq (cp_parser
* parser
,
13197 cp_decl_specifier_seq
*type_specifier_seq
)
13199 bool seen_type_specifier
= false;
13200 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
13202 /* Clear the TYPE_SPECIFIER_SEQ. */
13203 clear_decl_specs (type_specifier_seq
);
13205 /* Parse the type-specifiers and attributes. */
13208 tree type_specifier
;
13209 bool is_cv_qualifier
;
13211 /* Check for attributes first. */
13212 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
13214 type_specifier_seq
->attributes
=
13215 chainon (type_specifier_seq
->attributes
,
13216 cp_parser_attributes_opt (parser
));
13220 /* Look for the type-specifier. */
13221 type_specifier
= cp_parser_type_specifier (parser
,
13223 type_specifier_seq
,
13224 /*is_declaration=*/false,
13227 if (!type_specifier
)
13229 /* If the first type-specifier could not be found, this is not a
13230 type-specifier-seq at all. */
13231 if (!seen_type_specifier
)
13233 cp_parser_error (parser
, "expected type-specifier");
13234 type_specifier_seq
->type
= error_mark_node
;
13237 /* If subsequent type-specifiers could not be found, the
13238 type-specifier-seq is complete. */
13242 seen_type_specifier
= true;
13243 /* The standard says that a condition can be:
13245 type-specifier-seq declarator = assignment-expression
13252 we should treat the "S" as a declarator, not as a
13253 type-specifier. The standard doesn't say that explicitly for
13254 type-specifier-seq, but it does say that for
13255 decl-specifier-seq in an ordinary declaration. Perhaps it
13256 would be clearer just to allow a decl-specifier-seq here, and
13257 then add a semantic restriction that if any decl-specifiers
13258 that are not type-specifiers appear, the program is invalid. */
13259 if (is_condition
&& !is_cv_qualifier
)
13260 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13263 cp_parser_check_decl_spec (type_specifier_seq
);
13266 /* Parse a parameter-declaration-clause.
13268 parameter-declaration-clause:
13269 parameter-declaration-list [opt] ... [opt]
13270 parameter-declaration-list , ...
13272 Returns a representation for the parameter declarations. A return
13273 value of NULL indicates a parameter-declaration-clause consisting
13274 only of an ellipsis. */
13276 static cp_parameter_declarator
*
13277 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
13279 cp_parameter_declarator
*parameters
;
13284 /* Peek at the next token. */
13285 token
= cp_lexer_peek_token (parser
->lexer
);
13286 /* Check for trivial parameter-declaration-clauses. */
13287 if (token
->type
== CPP_ELLIPSIS
)
13289 /* Consume the `...' token. */
13290 cp_lexer_consume_token (parser
->lexer
);
13293 else if (token
->type
== CPP_CLOSE_PAREN
)
13294 /* There are no parameters. */
13296 #ifndef NO_IMPLICIT_EXTERN_C
13297 if (in_system_header
&& current_class_type
== NULL
13298 && current_lang_name
== lang_name_c
)
13302 return no_parameters
;
13304 /* Check for `(void)', too, which is a special case. */
13305 else if (token
->keyword
== RID_VOID
13306 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13307 == CPP_CLOSE_PAREN
))
13309 /* Consume the `void' token. */
13310 cp_lexer_consume_token (parser
->lexer
);
13311 /* There are no parameters. */
13312 return no_parameters
;
13315 /* Parse the parameter-declaration-list. */
13316 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
13317 /* If a parse error occurred while parsing the
13318 parameter-declaration-list, then the entire
13319 parameter-declaration-clause is erroneous. */
13323 /* Peek at the next token. */
13324 token
= cp_lexer_peek_token (parser
->lexer
);
13325 /* If it's a `,', the clause should terminate with an ellipsis. */
13326 if (token
->type
== CPP_COMMA
)
13328 /* Consume the `,'. */
13329 cp_lexer_consume_token (parser
->lexer
);
13330 /* Expect an ellipsis. */
13332 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
13334 /* It might also be `...' if the optional trailing `,' was
13336 else if (token
->type
== CPP_ELLIPSIS
)
13338 /* Consume the `...' token. */
13339 cp_lexer_consume_token (parser
->lexer
);
13340 /* And remember that we saw it. */
13344 ellipsis_p
= false;
13346 /* Finish the parameter list. */
13347 if (parameters
&& ellipsis_p
)
13348 parameters
->ellipsis_p
= true;
13353 /* Parse a parameter-declaration-list.
13355 parameter-declaration-list:
13356 parameter-declaration
13357 parameter-declaration-list , parameter-declaration
13359 Returns a representation of the parameter-declaration-list, as for
13360 cp_parser_parameter_declaration_clause. However, the
13361 `void_list_node' is never appended to the list. Upon return,
13362 *IS_ERROR will be true iff an error occurred. */
13364 static cp_parameter_declarator
*
13365 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
13367 cp_parameter_declarator
*parameters
= NULL
;
13368 cp_parameter_declarator
**tail
= ¶meters
;
13369 bool saved_in_unbraced_linkage_specification_p
;
13371 /* Assume all will go well. */
13373 /* The special considerations that apply to a function within an
13374 unbraced linkage specifications do not apply to the parameters
13375 to the function. */
13376 saved_in_unbraced_linkage_specification_p
13377 = parser
->in_unbraced_linkage_specification_p
;
13378 parser
->in_unbraced_linkage_specification_p
= false;
13380 /* Look for more parameters. */
13383 cp_parameter_declarator
*parameter
;
13384 bool parenthesized_p
;
13385 /* Parse the parameter. */
13387 = cp_parser_parameter_declaration (parser
,
13388 /*template_parm_p=*/false,
13391 /* If a parse error occurred parsing the parameter declaration,
13392 then the entire parameter-declaration-list is erroneous. */
13399 /* Add the new parameter to the list. */
13401 tail
= ¶meter
->next
;
13403 /* Peek at the next token. */
13404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
13405 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13406 /* These are for Objective-C++ */
13407 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
13408 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13409 /* The parameter-declaration-list is complete. */
13411 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13415 /* Peek at the next token. */
13416 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13417 /* If it's an ellipsis, then the list is complete. */
13418 if (token
->type
== CPP_ELLIPSIS
)
13420 /* Otherwise, there must be more parameters. Consume the
13422 cp_lexer_consume_token (parser
->lexer
);
13423 /* When parsing something like:
13425 int i(float f, double d)
13427 we can tell after seeing the declaration for "f" that we
13428 are not looking at an initialization of a variable "i",
13429 but rather at the declaration of a function "i".
13431 Due to the fact that the parsing of template arguments
13432 (as specified to a template-id) requires backtracking we
13433 cannot use this technique when inside a template argument
13435 if (!parser
->in_template_argument_list_p
13436 && !parser
->in_type_id_in_expr_p
13437 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
13438 /* However, a parameter-declaration of the form
13439 "foat(f)" (which is a valid declaration of a
13440 parameter "f") can also be interpreted as an
13441 expression (the conversion of "f" to "float"). */
13442 && !parenthesized_p
)
13443 cp_parser_commit_to_tentative_parse (parser
);
13447 cp_parser_error (parser
, "expected %<,%> or %<...%>");
13448 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
13449 cp_parser_skip_to_closing_parenthesis (parser
,
13450 /*recovering=*/true,
13451 /*or_comma=*/false,
13452 /*consume_paren=*/false);
13457 parser
->in_unbraced_linkage_specification_p
13458 = saved_in_unbraced_linkage_specification_p
;
13463 /* Parse a parameter declaration.
13465 parameter-declaration:
13466 decl-specifier-seq ... [opt] declarator
13467 decl-specifier-seq declarator = assignment-expression
13468 decl-specifier-seq ... [opt] abstract-declarator [opt]
13469 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13471 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13472 declares a template parameter. (In that case, a non-nested `>'
13473 token encountered during the parsing of the assignment-expression
13474 is not interpreted as a greater-than operator.)
13476 Returns a representation of the parameter, or NULL if an error
13477 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13478 true iff the declarator is of the form "(p)". */
13480 static cp_parameter_declarator
*
13481 cp_parser_parameter_declaration (cp_parser
*parser
,
13482 bool template_parm_p
,
13483 bool *parenthesized_p
)
13485 int declares_class_or_enum
;
13486 bool greater_than_is_operator_p
;
13487 cp_decl_specifier_seq decl_specifiers
;
13488 cp_declarator
*declarator
;
13489 tree default_argument
;
13491 const char *saved_message
;
13493 /* In a template parameter, `>' is not an operator.
13497 When parsing a default template-argument for a non-type
13498 template-parameter, the first non-nested `>' is taken as the end
13499 of the template parameter-list rather than a greater-than
13501 greater_than_is_operator_p
= !template_parm_p
;
13503 /* Type definitions may not appear in parameter types. */
13504 saved_message
= parser
->type_definition_forbidden_message
;
13505 parser
->type_definition_forbidden_message
13506 = "types may not be defined in parameter types";
13508 /* Parse the declaration-specifiers. */
13509 cp_parser_decl_specifier_seq (parser
,
13510 CP_PARSER_FLAGS_NONE
,
13512 &declares_class_or_enum
);
13513 /* If an error occurred, there's no reason to attempt to parse the
13514 rest of the declaration. */
13515 if (cp_parser_error_occurred (parser
))
13517 parser
->type_definition_forbidden_message
= saved_message
;
13521 /* Peek at the next token. */
13522 token
= cp_lexer_peek_token (parser
->lexer
);
13524 /* If the next token is a `)', `,', `=', `>', or `...', then there
13525 is no declarator. However, when variadic templates are enabled,
13526 there may be a declarator following `...'. */
13527 if (token
->type
== CPP_CLOSE_PAREN
13528 || token
->type
== CPP_COMMA
13529 || token
->type
== CPP_EQ
13530 || token
->type
== CPP_GREATER
)
13533 if (parenthesized_p
)
13534 *parenthesized_p
= false;
13536 /* Otherwise, there should be a declarator. */
13539 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
13540 parser
->default_arg_ok_p
= false;
13542 /* After seeing a decl-specifier-seq, if the next token is not a
13543 "(", there is no possibility that the code is a valid
13544 expression. Therefore, if parsing tentatively, we commit at
13546 if (!parser
->in_template_argument_list_p
13547 /* In an expression context, having seen:
13551 we cannot be sure whether we are looking at a
13552 function-type (taking a "char" as a parameter) or a cast
13553 of some object of type "char" to "int". */
13554 && !parser
->in_type_id_in_expr_p
13555 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
13556 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
13557 cp_parser_commit_to_tentative_parse (parser
);
13558 /* Parse the declarator. */
13559 declarator
= cp_parser_declarator (parser
,
13560 CP_PARSER_DECLARATOR_EITHER
,
13561 /*ctor_dtor_or_conv_p=*/NULL
,
13563 /*member_p=*/false);
13564 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
13565 /* After the declarator, allow more attributes. */
13566 decl_specifiers
.attributes
13567 = chainon (decl_specifiers
.attributes
,
13568 cp_parser_attributes_opt (parser
));
13571 /* If the next token is an ellipsis, and we have not seen a
13572 declarator name, and the type of the declarator contains parameter
13573 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13574 a parameter pack expansion expression. Otherwise, leave the
13575 ellipsis for a C-style variadic function. */
13576 token
= cp_lexer_peek_token (parser
->lexer
);
13577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13579 tree type
= decl_specifiers
.type
;
13581 if (type
&& DECL_P (type
))
13582 type
= TREE_TYPE (type
);
13585 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
13586 && declarator_can_be_parameter_pack (declarator
)
13587 && (!declarator
|| !declarator
->parameter_pack_p
)
13588 && uses_parameter_packs (type
))
13590 /* Consume the `...'. */
13591 cp_lexer_consume_token (parser
->lexer
);
13592 maybe_warn_variadic_templates ();
13594 /* Build a pack expansion type */
13596 declarator
->parameter_pack_p
= true;
13598 decl_specifiers
.type
= make_pack_expansion (type
);
13602 /* The restriction on defining new types applies only to the type
13603 of the parameter, not to the default argument. */
13604 parser
->type_definition_forbidden_message
= saved_message
;
13606 /* If the next token is `=', then process a default argument. */
13607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13609 /* Consume the `='. */
13610 cp_lexer_consume_token (parser
->lexer
);
13612 /* If we are defining a class, then the tokens that make up the
13613 default argument must be saved and processed later. */
13614 if (!template_parm_p
&& at_class_scope_p ()
13615 && TYPE_BEING_DEFINED (current_class_type
))
13617 unsigned depth
= 0;
13618 cp_token
*first_token
;
13621 /* Add tokens until we have processed the entire default
13622 argument. We add the range [first_token, token). */
13623 first_token
= cp_lexer_peek_token (parser
->lexer
);
13628 /* Peek at the next token. */
13629 token
= cp_lexer_peek_token (parser
->lexer
);
13630 /* What we do depends on what token we have. */
13631 switch (token
->type
)
13633 /* In valid code, a default argument must be
13634 immediately followed by a `,' `)', or `...'. */
13636 case CPP_CLOSE_PAREN
:
13638 /* If we run into a non-nested `;', `}', or `]',
13639 then the code is invalid -- but the default
13640 argument is certainly over. */
13641 case CPP_SEMICOLON
:
13642 case CPP_CLOSE_BRACE
:
13643 case CPP_CLOSE_SQUARE
:
13646 /* Update DEPTH, if necessary. */
13647 else if (token
->type
== CPP_CLOSE_PAREN
13648 || token
->type
== CPP_CLOSE_BRACE
13649 || token
->type
== CPP_CLOSE_SQUARE
)
13653 case CPP_OPEN_PAREN
:
13654 case CPP_OPEN_SQUARE
:
13655 case CPP_OPEN_BRACE
:
13660 if (cxx_dialect
== cxx98
)
13662 /* Fall through for C++0x, which treats the `>>'
13663 operator like two `>' tokens in certain
13667 /* If we see a non-nested `>', and `>' is not an
13668 operator, then it marks the end of the default
13670 if (!depth
&& !greater_than_is_operator_p
)
13674 /* If we run out of tokens, issue an error message. */
13676 case CPP_PRAGMA_EOL
:
13677 error ("file ends in default argument");
13683 /* In these cases, we should look for template-ids.
13684 For example, if the default argument is
13685 `X<int, double>()', we need to do name lookup to
13686 figure out whether or not `X' is a template; if
13687 so, the `,' does not end the default argument.
13689 That is not yet done. */
13696 /* If we've reached the end, stop. */
13700 /* Add the token to the token block. */
13701 token
= cp_lexer_consume_token (parser
->lexer
);
13704 /* Create a DEFAULT_ARG to represent the unparsed default
13706 default_argument
= make_node (DEFAULT_ARG
);
13707 DEFARG_TOKENS (default_argument
)
13708 = cp_token_cache_new (first_token
, token
);
13709 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
13711 /* Outside of a class definition, we can just parse the
13712 assignment-expression. */
13715 = cp_parser_default_argument (parser
, template_parm_p
);
13717 if (!parser
->default_arg_ok_p
)
13719 if (!flag_pedantic_errors
)
13720 warning (0, "deprecated use of default argument for parameter of non-function");
13723 error ("default arguments are only permitted for function parameters");
13724 default_argument
= NULL_TREE
;
13727 else if ((declarator
&& declarator
->parameter_pack_p
)
13728 || (decl_specifiers
.type
13729 && PACK_EXPANSION_P (decl_specifiers
.type
)))
13731 const char* kind
= template_parm_p
? "template " : "";
13733 /* Find the name of the parameter pack. */
13734 cp_declarator
*id_declarator
= declarator
;
13735 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13736 id_declarator
= id_declarator
->declarator
;
13738 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13739 error ("%sparameter pack %qD cannot have a default argument",
13740 kind
, id_declarator
->u
.id
.unqualified_name
);
13742 error ("%sparameter pack cannot have a default argument",
13745 default_argument
= NULL_TREE
;
13749 default_argument
= NULL_TREE
;
13751 return make_parameter_declarator (&decl_specifiers
,
13756 /* Parse a default argument and return it.
13758 TEMPLATE_PARM_P is true if this is a default argument for a
13759 non-type template parameter. */
13761 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
13763 tree default_argument
= NULL_TREE
;
13764 bool saved_greater_than_is_operator_p
;
13765 bool saved_local_variables_forbidden_p
;
13767 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13769 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
13770 parser
->greater_than_is_operator_p
= !template_parm_p
;
13771 /* Local variable names (and the `this' keyword) may not
13772 appear in a default argument. */
13773 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
13774 parser
->local_variables_forbidden_p
= true;
13775 /* The default argument expression may cause implicitly
13776 defined member functions to be synthesized, which will
13777 result in garbage collection. We must treat this
13778 situation as if we were within the body of function so as
13779 to avoid collecting live data on the stack. */
13781 /* Parse the assignment-expression. */
13782 if (template_parm_p
)
13783 push_deferring_access_checks (dk_no_deferred
);
13785 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
13786 if (template_parm_p
)
13787 pop_deferring_access_checks ();
13788 /* Restore saved state. */
13790 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
13791 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
13793 return default_argument
;
13796 /* Parse a function-body.
13799 compound_statement */
13802 cp_parser_function_body (cp_parser
*parser
)
13804 cp_parser_compound_statement (parser
, NULL
, false);
13807 /* Parse a ctor-initializer-opt followed by a function-body. Return
13808 true if a ctor-initializer was present. */
13811 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
13814 bool ctor_initializer_p
;
13816 /* Begin the function body. */
13817 body
= begin_function_body ();
13818 /* Parse the optional ctor-initializer. */
13819 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
13820 /* Parse the function-body. */
13821 cp_parser_function_body (parser
);
13822 /* Finish the function body. */
13823 finish_function_body (body
);
13825 return ctor_initializer_p
;
13828 /* Parse an initializer.
13831 = initializer-clause
13832 ( expression-list )
13834 Returns an expression representing the initializer. If no
13835 initializer is present, NULL_TREE is returned.
13837 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13838 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13839 set to FALSE if there is no initializer present. If there is an
13840 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13841 is set to true; otherwise it is set to false. */
13844 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
13845 bool* non_constant_p
)
13850 /* Peek at the next token. */
13851 token
= cp_lexer_peek_token (parser
->lexer
);
13853 /* Let our caller know whether or not this initializer was
13855 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
13856 /* Assume that the initializer is constant. */
13857 *non_constant_p
= false;
13859 if (token
->type
== CPP_EQ
)
13861 /* Consume the `='. */
13862 cp_lexer_consume_token (parser
->lexer
);
13863 /* Parse the initializer-clause. */
13864 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
13866 else if (token
->type
== CPP_OPEN_PAREN
)
13867 init
= cp_parser_parenthesized_expression_list (parser
, false,
13869 /*allow_expansion_p=*/true,
13873 /* Anything else is an error. */
13874 cp_parser_error (parser
, "expected initializer");
13875 init
= error_mark_node
;
13881 /* Parse an initializer-clause.
13883 initializer-clause:
13884 assignment-expression
13885 { initializer-list , [opt] }
13888 Returns an expression representing the initializer.
13890 If the `assignment-expression' production is used the value
13891 returned is simply a representation for the expression.
13893 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13894 the elements of the initializer-list (or NULL, if the last
13895 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13896 NULL_TREE. There is no way to detect whether or not the optional
13897 trailing `,' was provided. NON_CONSTANT_P is as for
13898 cp_parser_initializer. */
13901 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
13905 /* Assume the expression is constant. */
13906 *non_constant_p
= false;
13908 /* If it is not a `{', then we are looking at an
13909 assignment-expression. */
13910 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13913 = cp_parser_constant_expression (parser
,
13914 /*allow_non_constant_p=*/true,
13916 if (!*non_constant_p
)
13917 initializer
= fold_non_dependent_expr (initializer
);
13921 /* Consume the `{' token. */
13922 cp_lexer_consume_token (parser
->lexer
);
13923 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13924 initializer
= make_node (CONSTRUCTOR
);
13925 /* If it's not a `}', then there is a non-trivial initializer. */
13926 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13928 /* Parse the initializer list. */
13929 CONSTRUCTOR_ELTS (initializer
)
13930 = cp_parser_initializer_list (parser
, non_constant_p
);
13931 /* A trailing `,' token is allowed. */
13932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13933 cp_lexer_consume_token (parser
->lexer
);
13935 /* Now, there should be a trailing `}'. */
13936 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13939 return initializer
;
13942 /* Parse an initializer-list.
13945 initializer-clause ... [opt]
13946 initializer-list , initializer-clause ... [opt]
13951 identifier : initializer-clause
13952 initializer-list, identifier : initializer-clause
13954 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13955 for the initializer. If the INDEX of the elt is non-NULL, it is the
13956 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13957 as for cp_parser_initializer. */
13959 static VEC(constructor_elt
,gc
) *
13960 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
13962 VEC(constructor_elt
,gc
) *v
= NULL
;
13964 /* Assume all of the expressions are constant. */
13965 *non_constant_p
= false;
13967 /* Parse the rest of the list. */
13973 bool clause_non_constant_p
;
13975 /* If the next token is an identifier and the following one is a
13976 colon, we are looking at the GNU designated-initializer
13978 if (cp_parser_allow_gnu_extensions_p (parser
)
13979 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
13980 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
13982 /* Warn the user that they are using an extension. */
13984 pedwarn ("ISO C++ does not allow designated initializers");
13985 /* Consume the identifier. */
13986 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13987 /* Consume the `:'. */
13988 cp_lexer_consume_token (parser
->lexer
);
13991 identifier
= NULL_TREE
;
13993 /* Parse the initializer. */
13994 initializer
= cp_parser_initializer_clause (parser
,
13995 &clause_non_constant_p
);
13996 /* If any clause is non-constant, so is the entire initializer. */
13997 if (clause_non_constant_p
)
13998 *non_constant_p
= true;
14000 /* If we have an ellipsis, this is an initializer pack
14002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14004 /* Consume the `...'. */
14005 cp_lexer_consume_token (parser
->lexer
);
14007 /* Turn the initializer into an initializer expansion. */
14008 initializer
= make_pack_expansion (initializer
);
14011 /* Add it to the vector. */
14012 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
14014 /* If the next token is not a comma, we have reached the end of
14016 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14019 /* Peek at the next token. */
14020 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14021 /* If the next token is a `}', then we're still done. An
14022 initializer-clause can have a trailing `,' after the
14023 initializer-list and before the closing `}'. */
14024 if (token
->type
== CPP_CLOSE_BRACE
)
14027 /* Consume the `,' token. */
14028 cp_lexer_consume_token (parser
->lexer
);
14034 /* Classes [gram.class] */
14036 /* Parse a class-name.
14042 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14043 to indicate that names looked up in dependent types should be
14044 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14045 keyword has been used to indicate that the name that appears next
14046 is a template. TAG_TYPE indicates the explicit tag given before
14047 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14048 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14049 is the class being defined in a class-head.
14051 Returns the TYPE_DECL representing the class. */
14054 cp_parser_class_name (cp_parser
*parser
,
14055 bool typename_keyword_p
,
14056 bool template_keyword_p
,
14057 enum tag_types tag_type
,
14058 bool check_dependency_p
,
14060 bool is_declaration
)
14067 /* All class-names start with an identifier. */
14068 token
= cp_lexer_peek_token (parser
->lexer
);
14069 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
14071 cp_parser_error (parser
, "expected class-name");
14072 return error_mark_node
;
14075 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14076 to a template-id, so we save it here. */
14077 scope
= parser
->scope
;
14078 if (scope
== error_mark_node
)
14079 return error_mark_node
;
14081 /* Any name names a type if we're following the `typename' keyword
14082 in a qualified name where the enclosing scope is type-dependent. */
14083 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
14084 && dependent_type_p (scope
));
14085 /* Handle the common case (an identifier, but not a template-id)
14087 if (token
->type
== CPP_NAME
14088 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
14090 cp_token
*identifier_token
;
14094 /* Look for the identifier. */
14095 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
14096 ambiguous_p
= identifier_token
->ambiguous_p
;
14097 identifier
= cp_parser_identifier (parser
);
14098 /* If the next token isn't an identifier, we are certainly not
14099 looking at a class-name. */
14100 if (identifier
== error_mark_node
)
14101 decl
= error_mark_node
;
14102 /* If we know this is a type-name, there's no need to look it
14104 else if (typename_p
)
14108 tree ambiguous_decls
;
14109 /* If we already know that this lookup is ambiguous, then
14110 we've already issued an error message; there's no reason
14114 cp_parser_simulate_error (parser
);
14115 return error_mark_node
;
14117 /* If the next token is a `::', then the name must be a type
14120 [basic.lookup.qual]
14122 During the lookup for a name preceding the :: scope
14123 resolution operator, object, function, and enumerator
14124 names are ignored. */
14125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14126 tag_type
= typename_type
;
14127 /* Look up the name. */
14128 decl
= cp_parser_lookup_name (parser
, identifier
,
14130 /*is_template=*/false,
14131 /*is_namespace=*/false,
14132 check_dependency_p
,
14134 if (ambiguous_decls
)
14136 error ("reference to %qD is ambiguous", identifier
);
14137 print_candidates (ambiguous_decls
);
14138 if (cp_parser_parsing_tentatively (parser
))
14140 identifier_token
->ambiguous_p
= true;
14141 cp_parser_simulate_error (parser
);
14143 return error_mark_node
;
14149 /* Try a template-id. */
14150 decl
= cp_parser_template_id (parser
, template_keyword_p
,
14151 check_dependency_p
,
14153 if (decl
== error_mark_node
)
14154 return error_mark_node
;
14157 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
14159 /* If this is a typename, create a TYPENAME_TYPE. */
14160 if (typename_p
&& decl
!= error_mark_node
)
14162 decl
= make_typename_type (scope
, decl
, typename_type
,
14163 /*complain=*/tf_error
);
14164 if (decl
!= error_mark_node
)
14165 decl
= TYPE_NAME (decl
);
14168 /* Check to see that it is really the name of a class. */
14169 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14170 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
14171 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14172 /* Situations like this:
14174 template <typename T> struct A {
14175 typename T::template X<int>::I i;
14178 are problematic. Is `T::template X<int>' a class-name? The
14179 standard does not seem to be definitive, but there is no other
14180 valid interpretation of the following `::'. Therefore, those
14181 names are considered class-names. */
14183 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
14184 if (decl
!= error_mark_node
)
14185 decl
= TYPE_NAME (decl
);
14187 else if (TREE_CODE (decl
) != TYPE_DECL
14188 || TREE_TYPE (decl
) == error_mark_node
14189 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
14190 decl
= error_mark_node
;
14192 if (decl
== error_mark_node
)
14193 cp_parser_error (parser
, "expected class-name");
14198 /* Parse a class-specifier.
14201 class-head { member-specification [opt] }
14203 Returns the TREE_TYPE representing the class. */
14206 cp_parser_class_specifier (cp_parser
* parser
)
14210 tree attributes
= NULL_TREE
;
14211 int has_trailing_semicolon
;
14212 bool nested_name_specifier_p
;
14213 unsigned saved_num_template_parameter_lists
;
14214 bool saved_in_function_body
;
14215 tree old_scope
= NULL_TREE
;
14216 tree scope
= NULL_TREE
;
14219 push_deferring_access_checks (dk_no_deferred
);
14221 /* Parse the class-head. */
14222 type
= cp_parser_class_head (parser
,
14223 &nested_name_specifier_p
,
14226 /* If the class-head was a semantic disaster, skip the entire body
14230 cp_parser_skip_to_end_of_block_or_statement (parser
);
14231 pop_deferring_access_checks ();
14232 return error_mark_node
;
14235 /* Look for the `{'. */
14236 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
14238 pop_deferring_access_checks ();
14239 return error_mark_node
;
14242 /* Process the base classes. If they're invalid, skip the
14243 entire class body. */
14244 if (!xref_basetypes (type
, bases
))
14246 /* Consuming the closing brace yields better error messages
14248 if (cp_parser_skip_to_closing_brace (parser
))
14249 cp_lexer_consume_token (parser
->lexer
);
14250 pop_deferring_access_checks ();
14251 return error_mark_node
;
14254 /* Issue an error message if type-definitions are forbidden here. */
14255 cp_parser_check_type_definition (parser
);
14256 /* Remember that we are defining one more class. */
14257 ++parser
->num_classes_being_defined
;
14258 /* Inside the class, surrounding template-parameter-lists do not
14260 saved_num_template_parameter_lists
14261 = parser
->num_template_parameter_lists
;
14262 parser
->num_template_parameter_lists
= 0;
14263 /* We are not in a function body. */
14264 saved_in_function_body
= parser
->in_function_body
;
14265 parser
->in_function_body
= false;
14267 /* Start the class. */
14268 if (nested_name_specifier_p
)
14270 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
14271 old_scope
= push_inner_scope (scope
);
14273 type
= begin_class_definition (type
, attributes
);
14275 if (type
== error_mark_node
)
14276 /* If the type is erroneous, skip the entire body of the class. */
14277 cp_parser_skip_to_closing_brace (parser
);
14279 /* Parse the member-specification. */
14280 cp_parser_member_specification_opt (parser
);
14282 /* Look for the trailing `}'. */
14283 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14284 /* We get better error messages by noticing a common problem: a
14285 missing trailing `;'. */
14286 token
= cp_lexer_peek_token (parser
->lexer
);
14287 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
14288 /* Look for trailing attributes to apply to this class. */
14289 if (cp_parser_allow_gnu_extensions_p (parser
))
14290 attributes
= cp_parser_attributes_opt (parser
);
14291 if (type
!= error_mark_node
)
14292 type
= finish_struct (type
, attributes
);
14293 if (nested_name_specifier_p
)
14294 pop_inner_scope (old_scope
, scope
);
14295 /* If this class is not itself within the scope of another class,
14296 then we need to parse the bodies of all of the queued function
14297 definitions. Note that the queued functions defined in a class
14298 are not always processed immediately following the
14299 class-specifier for that class. Consider:
14302 struct B { void f() { sizeof (A); } };
14305 If `f' were processed before the processing of `A' were
14306 completed, there would be no way to compute the size of `A'.
14307 Note that the nesting we are interested in here is lexical --
14308 not the semantic nesting given by TYPE_CONTEXT. In particular,
14311 struct A { struct B; };
14312 struct A::B { void f() { } };
14314 there is no need to delay the parsing of `A::B::f'. */
14315 if (--parser
->num_classes_being_defined
== 0)
14319 tree class_type
= NULL_TREE
;
14320 tree pushed_scope
= NULL_TREE
;
14322 /* In a first pass, parse default arguments to the functions.
14323 Then, in a second pass, parse the bodies of the functions.
14324 This two-phased approach handles cases like:
14332 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
14333 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
14334 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
14335 TREE_PURPOSE (parser
->unparsed_functions_queues
)
14336 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
14338 fn
= TREE_VALUE (queue_entry
);
14339 /* If there are default arguments that have not yet been processed,
14340 take care of them now. */
14341 if (class_type
!= TREE_PURPOSE (queue_entry
))
14344 pop_scope (pushed_scope
);
14345 class_type
= TREE_PURPOSE (queue_entry
);
14346 pushed_scope
= push_scope (class_type
);
14348 /* Make sure that any template parameters are in scope. */
14349 maybe_begin_member_template_processing (fn
);
14350 /* Parse the default argument expressions. */
14351 cp_parser_late_parsing_default_args (parser
, fn
);
14352 /* Remove any template parameters from the symbol table. */
14353 maybe_end_member_template_processing ();
14356 pop_scope (pushed_scope
);
14357 /* Now parse the body of the functions. */
14358 for (TREE_VALUE (parser
->unparsed_functions_queues
)
14359 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
14360 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
14361 TREE_VALUE (parser
->unparsed_functions_queues
)
14362 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
14364 /* Figure out which function we need to process. */
14365 fn
= TREE_VALUE (queue_entry
);
14366 /* Parse the function. */
14367 cp_parser_late_parsing_for_member (parser
, fn
);
14371 /* Put back any saved access checks. */
14372 pop_deferring_access_checks ();
14374 /* Restore saved state. */
14375 parser
->in_function_body
= saved_in_function_body
;
14376 parser
->num_template_parameter_lists
14377 = saved_num_template_parameter_lists
;
14382 /* Parse a class-head.
14385 class-key identifier [opt] base-clause [opt]
14386 class-key nested-name-specifier identifier base-clause [opt]
14387 class-key nested-name-specifier [opt] template-id
14391 class-key attributes identifier [opt] base-clause [opt]
14392 class-key attributes nested-name-specifier identifier base-clause [opt]
14393 class-key attributes nested-name-specifier [opt] template-id
14396 Upon return BASES is initialized to the list of base classes (or
14397 NULL, if there are none) in the same form returned by
14398 cp_parser_base_clause.
14400 Returns the TYPE of the indicated class. Sets
14401 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14402 involving a nested-name-specifier was used, and FALSE otherwise.
14404 Returns error_mark_node if this is not a class-head.
14406 Returns NULL_TREE if the class-head is syntactically valid, but
14407 semantically invalid in a way that means we should skip the entire
14408 body of the class. */
14411 cp_parser_class_head (cp_parser
* parser
,
14412 bool* nested_name_specifier_p
,
14413 tree
*attributes_p
,
14416 tree nested_name_specifier
;
14417 enum tag_types class_key
;
14418 tree id
= NULL_TREE
;
14419 tree type
= NULL_TREE
;
14421 bool template_id_p
= false;
14422 bool qualified_p
= false;
14423 bool invalid_nested_name_p
= false;
14424 bool invalid_explicit_specialization_p
= false;
14425 tree pushed_scope
= NULL_TREE
;
14426 unsigned num_templates
;
14428 /* Assume no nested-name-specifier will be present. */
14429 *nested_name_specifier_p
= false;
14430 /* Assume no template parameter lists will be used in defining the
14434 *bases
= NULL_TREE
;
14436 /* Look for the class-key. */
14437 class_key
= cp_parser_class_key (parser
);
14438 if (class_key
== none_type
)
14439 return error_mark_node
;
14441 /* Parse the attributes. */
14442 attributes
= cp_parser_attributes_opt (parser
);
14444 /* If the next token is `::', that is invalid -- but sometimes
14445 people do try to write:
14449 Handle this gracefully by accepting the extra qualifier, and then
14450 issuing an error about it later if this really is a
14451 class-head. If it turns out just to be an elaborated type
14452 specifier, remain silent. */
14453 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
14454 qualified_p
= true;
14456 push_deferring_access_checks (dk_no_check
);
14458 /* Determine the name of the class. Begin by looking for an
14459 optional nested-name-specifier. */
14460 nested_name_specifier
14461 = cp_parser_nested_name_specifier_opt (parser
,
14462 /*typename_keyword_p=*/false,
14463 /*check_dependency_p=*/false,
14465 /*is_declaration=*/false);
14466 /* If there was a nested-name-specifier, then there *must* be an
14468 if (nested_name_specifier
)
14470 /* Although the grammar says `identifier', it really means
14471 `class-name' or `template-name'. You are only allowed to
14472 define a class that has already been declared with this
14475 The proposed resolution for Core Issue 180 says that wherever
14476 you see `class T::X' you should treat `X' as a type-name.
14478 It is OK to define an inaccessible class; for example:
14480 class A { class B; };
14483 We do not know if we will see a class-name, or a
14484 template-name. We look for a class-name first, in case the
14485 class-name is a template-id; if we looked for the
14486 template-name first we would stop after the template-name. */
14487 cp_parser_parse_tentatively (parser
);
14488 type
= cp_parser_class_name (parser
,
14489 /*typename_keyword_p=*/false,
14490 /*template_keyword_p=*/false,
14492 /*check_dependency_p=*/false,
14493 /*class_head_p=*/true,
14494 /*is_declaration=*/false);
14495 /* If that didn't work, ignore the nested-name-specifier. */
14496 if (!cp_parser_parse_definitely (parser
))
14498 invalid_nested_name_p
= true;
14499 id
= cp_parser_identifier (parser
);
14500 if (id
== error_mark_node
)
14503 /* If we could not find a corresponding TYPE, treat this
14504 declaration like an unqualified declaration. */
14505 if (type
== error_mark_node
)
14506 nested_name_specifier
= NULL_TREE
;
14507 /* Otherwise, count the number of templates used in TYPE and its
14508 containing scopes. */
14513 for (scope
= TREE_TYPE (type
);
14514 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
14515 scope
= (TYPE_P (scope
)
14516 ? TYPE_CONTEXT (scope
)
14517 : DECL_CONTEXT (scope
)))
14519 && CLASS_TYPE_P (scope
)
14520 && CLASSTYPE_TEMPLATE_INFO (scope
)
14521 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
14522 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
14526 /* Otherwise, the identifier is optional. */
14529 /* We don't know whether what comes next is a template-id,
14530 an identifier, or nothing at all. */
14531 cp_parser_parse_tentatively (parser
);
14532 /* Check for a template-id. */
14533 id
= cp_parser_template_id (parser
,
14534 /*template_keyword_p=*/false,
14535 /*check_dependency_p=*/true,
14536 /*is_declaration=*/true);
14537 /* If that didn't work, it could still be an identifier. */
14538 if (!cp_parser_parse_definitely (parser
))
14540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14541 id
= cp_parser_identifier (parser
);
14547 template_id_p
= true;
14552 pop_deferring_access_checks ();
14555 cp_parser_check_for_invalid_template_id (parser
, id
);
14557 /* If it's not a `:' or a `{' then we can't really be looking at a
14558 class-head, since a class-head only appears as part of a
14559 class-specifier. We have to detect this situation before calling
14560 xref_tag, since that has irreversible side-effects. */
14561 if (!cp_parser_next_token_starts_class_definition_p (parser
))
14563 cp_parser_error (parser
, "expected %<{%> or %<:%>");
14564 return error_mark_node
;
14567 /* At this point, we're going ahead with the class-specifier, even
14568 if some other problem occurs. */
14569 cp_parser_commit_to_tentative_parse (parser
);
14570 /* Issue the error about the overly-qualified name now. */
14572 cp_parser_error (parser
,
14573 "global qualification of class name is invalid");
14574 else if (invalid_nested_name_p
)
14575 cp_parser_error (parser
,
14576 "qualified name does not name a class");
14577 else if (nested_name_specifier
)
14581 /* Reject typedef-names in class heads. */
14582 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
14584 error ("invalid class name in declaration of %qD", type
);
14589 /* Figure out in what scope the declaration is being placed. */
14590 scope
= current_scope ();
14591 /* If that scope does not contain the scope in which the
14592 class was originally declared, the program is invalid. */
14593 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
14595 if (at_namespace_scope_p ())
14596 error ("declaration of %qD in namespace %qD which does not "
14597 "enclose %qD", type
, scope
, nested_name_specifier
);
14599 error ("declaration of %qD in %qD which does not enclose %qD",
14600 type
, scope
, nested_name_specifier
);
14606 A declarator-id shall not be qualified exception of the
14607 definition of a ... nested class outside of its class
14608 ... [or] a the definition or explicit instantiation of a
14609 class member of a namespace outside of its namespace. */
14610 if (scope
== nested_name_specifier
)
14612 pedwarn ("extra qualification ignored");
14613 nested_name_specifier
= NULL_TREE
;
14617 /* An explicit-specialization must be preceded by "template <>". If
14618 it is not, try to recover gracefully. */
14619 if (at_namespace_scope_p ()
14620 && parser
->num_template_parameter_lists
== 0
14623 error ("an explicit specialization must be preceded by %<template <>%>");
14624 invalid_explicit_specialization_p
= true;
14625 /* Take the same action that would have been taken by
14626 cp_parser_explicit_specialization. */
14627 ++parser
->num_template_parameter_lists
;
14628 begin_specialization ();
14630 /* There must be no "return" statements between this point and the
14631 end of this function; set "type "to the correct return value and
14632 use "goto done;" to return. */
14633 /* Make sure that the right number of template parameters were
14635 if (!cp_parser_check_template_parameters (parser
, num_templates
))
14637 /* If something went wrong, there is no point in even trying to
14638 process the class-definition. */
14643 /* Look up the type. */
14646 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
14647 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
14648 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
14650 error ("function template %qD redeclared as a class template", id
);
14651 type
= error_mark_node
;
14655 type
= TREE_TYPE (id
);
14656 type
= maybe_process_partial_specialization (type
);
14658 if (nested_name_specifier
)
14659 pushed_scope
= push_scope (nested_name_specifier
);
14661 else if (nested_name_specifier
)
14667 template <typename T> struct S { struct T };
14668 template <typename T> struct S<T>::T { };
14670 we will get a TYPENAME_TYPE when processing the definition of
14671 `S::T'. We need to resolve it to the actual type before we
14672 try to define it. */
14673 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
14675 class_type
= resolve_typename_type (TREE_TYPE (type
),
14676 /*only_current_p=*/false);
14677 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
14678 type
= TYPE_NAME (class_type
);
14681 cp_parser_error (parser
, "could not resolve typename type");
14682 type
= error_mark_node
;
14686 maybe_process_partial_specialization (TREE_TYPE (type
));
14687 class_type
= current_class_type
;
14688 /* Enter the scope indicated by the nested-name-specifier. */
14689 pushed_scope
= push_scope (nested_name_specifier
);
14690 /* Get the canonical version of this type. */
14691 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
14692 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14693 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
14695 type
= push_template_decl (type
);
14696 if (type
== error_mark_node
)
14703 type
= TREE_TYPE (type
);
14704 *nested_name_specifier_p
= true;
14706 else /* The name is not a nested name. */
14708 /* If the class was unnamed, create a dummy name. */
14710 id
= make_anon_name ();
14711 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
14712 parser
->num_template_parameter_lists
);
14715 /* Indicate whether this class was declared as a `class' or as a
14717 if (TREE_CODE (type
) == RECORD_TYPE
)
14718 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
14719 cp_parser_check_class_key (class_key
, type
);
14721 /* If this type was already complete, and we see another definition,
14722 that's an error. */
14723 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
14725 error ("redefinition of %q#T", type
);
14726 error ("previous definition of %q+#T", type
);
14730 else if (type
== error_mark_node
)
14733 /* We will have entered the scope containing the class; the names of
14734 base classes should be looked up in that context. For example:
14736 struct A { struct B {}; struct C; };
14737 struct A::C : B {};
14741 /* Get the list of base-classes, if there is one. */
14742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14743 *bases
= cp_parser_base_clause (parser
);
14746 /* Leave the scope given by the nested-name-specifier. We will
14747 enter the class scope itself while processing the members. */
14749 pop_scope (pushed_scope
);
14751 if (invalid_explicit_specialization_p
)
14753 end_specialization ();
14754 --parser
->num_template_parameter_lists
;
14756 *attributes_p
= attributes
;
14760 /* Parse a class-key.
14767 Returns the kind of class-key specified, or none_type to indicate
14770 static enum tag_types
14771 cp_parser_class_key (cp_parser
* parser
)
14774 enum tag_types tag_type
;
14776 /* Look for the class-key. */
14777 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
14781 /* Check to see if the TOKEN is a class-key. */
14782 tag_type
= cp_parser_token_is_class_key (token
);
14784 cp_parser_error (parser
, "expected class-key");
14788 /* Parse an (optional) member-specification.
14790 member-specification:
14791 member-declaration member-specification [opt]
14792 access-specifier : member-specification [opt] */
14795 cp_parser_member_specification_opt (cp_parser
* parser
)
14802 /* Peek at the next token. */
14803 token
= cp_lexer_peek_token (parser
->lexer
);
14804 /* If it's a `}', or EOF then we've seen all the members. */
14805 if (token
->type
== CPP_CLOSE_BRACE
14806 || token
->type
== CPP_EOF
14807 || token
->type
== CPP_PRAGMA_EOL
)
14810 /* See if this token is a keyword. */
14811 keyword
= token
->keyword
;
14815 case RID_PROTECTED
:
14817 /* Consume the access-specifier. */
14818 cp_lexer_consume_token (parser
->lexer
);
14819 /* Remember which access-specifier is active. */
14820 current_access_specifier
= token
->u
.value
;
14821 /* Look for the `:'. */
14822 cp_parser_require (parser
, CPP_COLON
, "`:'");
14826 /* Accept #pragmas at class scope. */
14827 if (token
->type
== CPP_PRAGMA
)
14829 cp_parser_pragma (parser
, pragma_external
);
14833 /* Otherwise, the next construction must be a
14834 member-declaration. */
14835 cp_parser_member_declaration (parser
);
14840 /* Parse a member-declaration.
14842 member-declaration:
14843 decl-specifier-seq [opt] member-declarator-list [opt] ;
14844 function-definition ; [opt]
14845 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14847 template-declaration
14849 member-declarator-list:
14851 member-declarator-list , member-declarator
14854 declarator pure-specifier [opt]
14855 declarator constant-initializer [opt]
14856 identifier [opt] : constant-expression
14860 member-declaration:
14861 __extension__ member-declaration
14864 declarator attributes [opt] pure-specifier [opt]
14865 declarator attributes [opt] constant-initializer [opt]
14866 identifier [opt] attributes [opt] : constant-expression
14870 member-declaration:
14871 static_assert-declaration */
14874 cp_parser_member_declaration (cp_parser
* parser
)
14876 cp_decl_specifier_seq decl_specifiers
;
14877 tree prefix_attributes
;
14879 int declares_class_or_enum
;
14882 int saved_pedantic
;
14884 /* Check for the `__extension__' keyword. */
14885 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14888 cp_parser_member_declaration (parser
);
14889 /* Restore the old value of the PEDANTIC flag. */
14890 pedantic
= saved_pedantic
;
14895 /* Check for a template-declaration. */
14896 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14898 /* An explicit specialization here is an error condition, and we
14899 expect the specialization handler to detect and report this. */
14900 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14901 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
14902 cp_parser_explicit_specialization (parser
);
14904 cp_parser_template_declaration (parser
, /*member_p=*/true);
14909 /* Check for a using-declaration. */
14910 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
14912 /* Parse the using-declaration. */
14913 cp_parser_using_declaration (parser
,
14914 /*access_declaration_p=*/false);
14918 /* Check for @defs. */
14919 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
14922 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
14923 ivar
= ivar_chains
;
14927 ivar
= TREE_CHAIN (member
);
14928 TREE_CHAIN (member
) = NULL_TREE
;
14929 finish_member_declaration (member
);
14934 /* If the next token is `static_assert' we have a static assertion. */
14935 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
14937 cp_parser_static_assert (parser
, /*member_p=*/true);
14941 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
14944 /* Parse the decl-specifier-seq. */
14945 cp_parser_decl_specifier_seq (parser
,
14946 CP_PARSER_FLAGS_OPTIONAL
,
14948 &declares_class_or_enum
);
14949 prefix_attributes
= decl_specifiers
.attributes
;
14950 decl_specifiers
.attributes
= NULL_TREE
;
14951 /* Check for an invalid type-name. */
14952 if (!decl_specifiers
.type
14953 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
14955 /* If there is no declarator, then the decl-specifier-seq should
14957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14959 /* If there was no decl-specifier-seq, and the next token is a
14960 `;', then we have something like:
14966 Each member-declaration shall declare at least one member
14967 name of the class. */
14968 if (!decl_specifiers
.any_specifiers_p
)
14970 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14971 if (pedantic
&& !token
->in_system_header
)
14972 pedwarn ("%Hextra %<;%>", &token
->location
);
14978 /* See if this declaration is a friend. */
14979 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14980 /* If there were decl-specifiers, check to see if there was
14981 a class-declaration. */
14982 type
= check_tag_decl (&decl_specifiers
);
14983 /* Nested classes have already been added to the class, but
14984 a `friend' needs to be explicitly registered. */
14987 /* If the `friend' keyword was present, the friend must
14988 be introduced with a class-key. */
14989 if (!declares_class_or_enum
)
14990 error ("a class-key must be used when declaring a friend");
14993 template <typename T> struct A {
14994 friend struct A<T>::B;
14997 A<T>::B will be represented by a TYPENAME_TYPE, and
14998 therefore not recognized by check_tag_decl. */
15000 && decl_specifiers
.type
15001 && TYPE_P (decl_specifiers
.type
))
15002 type
= decl_specifiers
.type
;
15003 if (!type
|| !TYPE_P (type
))
15004 error ("friend declaration does not name a class or "
15007 make_friend_class (current_class_type
, type
,
15008 /*complain=*/true);
15010 /* If there is no TYPE, an error message will already have
15012 else if (!type
|| type
== error_mark_node
)
15014 /* An anonymous aggregate has to be handled specially; such
15015 a declaration really declares a data member (with a
15016 particular type), as opposed to a nested class. */
15017 else if (ANON_AGGR_TYPE_P (type
))
15019 /* Remove constructors and such from TYPE, now that we
15020 know it is an anonymous aggregate. */
15021 fixup_anonymous_aggr (type
);
15022 /* And make the corresponding data member. */
15023 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
15024 /* Add it to the class. */
15025 finish_member_declaration (decl
);
15028 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
15033 /* See if these declarations will be friends. */
15034 friend_p
= cp_parser_friend_p (&decl_specifiers
);
15036 /* Keep going until we hit the `;' at the end of the
15038 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15040 tree attributes
= NULL_TREE
;
15041 tree first_attribute
;
15043 /* Peek at the next token. */
15044 token
= cp_lexer_peek_token (parser
->lexer
);
15046 /* Check for a bitfield declaration. */
15047 if (token
->type
== CPP_COLON
15048 || (token
->type
== CPP_NAME
15049 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15055 /* Get the name of the bitfield. Note that we cannot just
15056 check TOKEN here because it may have been invalidated by
15057 the call to cp_lexer_peek_nth_token above. */
15058 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
15059 identifier
= cp_parser_identifier (parser
);
15061 identifier
= NULL_TREE
;
15063 /* Consume the `:' token. */
15064 cp_lexer_consume_token (parser
->lexer
);
15065 /* Get the width of the bitfield. */
15067 = cp_parser_constant_expression (parser
,
15068 /*allow_non_constant=*/false,
15071 /* Look for attributes that apply to the bitfield. */
15072 attributes
= cp_parser_attributes_opt (parser
);
15073 /* Remember which attributes are prefix attributes and
15075 first_attribute
= attributes
;
15076 /* Combine the attributes. */
15077 attributes
= chainon (prefix_attributes
, attributes
);
15079 /* Create the bitfield declaration. */
15080 decl
= grokbitfield (identifier
15081 ? make_id_declarator (NULL_TREE
,
15087 /* Apply the attributes. */
15088 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
15092 cp_declarator
*declarator
;
15094 tree asm_specification
;
15095 int ctor_dtor_or_conv_p
;
15097 /* Parse the declarator. */
15099 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
15100 &ctor_dtor_or_conv_p
,
15101 /*parenthesized_p=*/NULL
,
15102 /*member_p=*/true);
15104 /* If something went wrong parsing the declarator, make sure
15105 that we at least consume some tokens. */
15106 if (declarator
== cp_error_declarator
)
15108 /* Skip to the end of the statement. */
15109 cp_parser_skip_to_end_of_statement (parser
);
15110 /* If the next token is not a semicolon, that is
15111 probably because we just skipped over the body of
15112 a function. So, we consume a semicolon if
15113 present, but do not issue an error message if it
15115 if (cp_lexer_next_token_is (parser
->lexer
,
15117 cp_lexer_consume_token (parser
->lexer
);
15121 if (declares_class_or_enum
& 2)
15122 cp_parser_check_for_definition_in_return_type
15123 (declarator
, decl_specifiers
.type
);
15125 /* Look for an asm-specification. */
15126 asm_specification
= cp_parser_asm_specification_opt (parser
);
15127 /* Look for attributes that apply to the declaration. */
15128 attributes
= cp_parser_attributes_opt (parser
);
15129 /* Remember which attributes are prefix attributes and
15131 first_attribute
= attributes
;
15132 /* Combine the attributes. */
15133 attributes
= chainon (prefix_attributes
, attributes
);
15135 /* If it's an `=', then we have a constant-initializer or a
15136 pure-specifier. It is not correct to parse the
15137 initializer before registering the member declaration
15138 since the member declaration should be in scope while
15139 its initializer is processed. However, the rest of the
15140 front end does not yet provide an interface that allows
15141 us to handle this correctly. */
15142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15146 A pure-specifier shall be used only in the declaration of
15147 a virtual function.
15149 A member-declarator can contain a constant-initializer
15150 only if it declares a static member of integral or
15153 Therefore, if the DECLARATOR is for a function, we look
15154 for a pure-specifier; otherwise, we look for a
15155 constant-initializer. When we call `grokfield', it will
15156 perform more stringent semantics checks. */
15157 if (function_declarator_p (declarator
))
15158 initializer
= cp_parser_pure_specifier (parser
);
15160 /* Parse the initializer. */
15161 initializer
= cp_parser_constant_initializer (parser
);
15163 /* Otherwise, there is no initializer. */
15165 initializer
= NULL_TREE
;
15167 /* See if we are probably looking at a function
15168 definition. We are certainly not looking at a
15169 member-declarator. Calling `grokfield' has
15170 side-effects, so we must not do it unless we are sure
15171 that we are looking at a member-declarator. */
15172 if (cp_parser_token_starts_function_definition_p
15173 (cp_lexer_peek_token (parser
->lexer
)))
15175 /* The grammar does not allow a pure-specifier to be
15176 used when a member function is defined. (It is
15177 possible that this fact is an oversight in the
15178 standard, since a pure function may be defined
15179 outside of the class-specifier. */
15181 error ("pure-specifier on function-definition");
15182 decl
= cp_parser_save_member_function_body (parser
,
15186 /* If the member was not a friend, declare it here. */
15188 finish_member_declaration (decl
);
15189 /* Peek at the next token. */
15190 token
= cp_lexer_peek_token (parser
->lexer
);
15191 /* If the next token is a semicolon, consume it. */
15192 if (token
->type
== CPP_SEMICOLON
)
15193 cp_lexer_consume_token (parser
->lexer
);
15197 /* Create the declaration. */
15198 decl
= grokfield (declarator
, &decl_specifiers
,
15199 initializer
, /*init_const_expr_p=*/true,
15204 /* Reset PREFIX_ATTRIBUTES. */
15205 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
15206 attributes
= TREE_CHAIN (attributes
);
15208 TREE_CHAIN (attributes
) = NULL_TREE
;
15210 /* If there is any qualification still in effect, clear it
15211 now; we will be starting fresh with the next declarator. */
15212 parser
->scope
= NULL_TREE
;
15213 parser
->qualifying_scope
= NULL_TREE
;
15214 parser
->object_scope
= NULL_TREE
;
15215 /* If it's a `,', then there are more declarators. */
15216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15217 cp_lexer_consume_token (parser
->lexer
);
15218 /* If the next token isn't a `;', then we have a parse error. */
15219 else if (cp_lexer_next_token_is_not (parser
->lexer
,
15222 cp_parser_error (parser
, "expected %<;%>");
15223 /* Skip tokens until we find a `;'. */
15224 cp_parser_skip_to_end_of_statement (parser
);
15231 /* Add DECL to the list of members. */
15233 finish_member_declaration (decl
);
15235 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15236 cp_parser_save_default_args (parser
, decl
);
15241 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
15244 /* Parse a pure-specifier.
15249 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15250 Otherwise, ERROR_MARK_NODE is returned. */
15253 cp_parser_pure_specifier (cp_parser
* parser
)
15257 /* Look for the `=' token. */
15258 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
15259 return error_mark_node
;
15260 /* Look for the `0' token. */
15261 token
= cp_lexer_consume_token (parser
->lexer
);
15262 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15263 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
15265 cp_parser_error (parser
,
15266 "invalid pure specifier (only `= 0' is allowed)");
15267 cp_parser_skip_to_end_of_statement (parser
);
15268 return error_mark_node
;
15270 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15272 error ("templates may not be %<virtual%>");
15273 return error_mark_node
;
15276 return integer_zero_node
;
15279 /* Parse a constant-initializer.
15281 constant-initializer:
15282 = constant-expression
15284 Returns a representation of the constant-expression. */
15287 cp_parser_constant_initializer (cp_parser
* parser
)
15289 /* Look for the `=' token. */
15290 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
15291 return error_mark_node
;
15293 /* It is invalid to write:
15295 struct S { static const int i = { 7 }; };
15298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15300 cp_parser_error (parser
,
15301 "a brace-enclosed initializer is not allowed here");
15302 /* Consume the opening brace. */
15303 cp_lexer_consume_token (parser
->lexer
);
15304 /* Skip the initializer. */
15305 cp_parser_skip_to_closing_brace (parser
);
15306 /* Look for the trailing `}'. */
15307 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
15309 return error_mark_node
;
15312 return cp_parser_constant_expression (parser
,
15313 /*allow_non_constant=*/false,
15317 /* Derived classes [gram.class.derived] */
15319 /* Parse a base-clause.
15322 : base-specifier-list
15324 base-specifier-list:
15325 base-specifier ... [opt]
15326 base-specifier-list , base-specifier ... [opt]
15328 Returns a TREE_LIST representing the base-classes, in the order in
15329 which they were declared. The representation of each node is as
15330 described by cp_parser_base_specifier.
15332 In the case that no bases are specified, this function will return
15333 NULL_TREE, not ERROR_MARK_NODE. */
15336 cp_parser_base_clause (cp_parser
* parser
)
15338 tree bases
= NULL_TREE
;
15340 /* Look for the `:' that begins the list. */
15341 cp_parser_require (parser
, CPP_COLON
, "`:'");
15343 /* Scan the base-specifier-list. */
15348 bool pack_expansion_p
= false;
15350 /* Look for the base-specifier. */
15351 base
= cp_parser_base_specifier (parser
);
15352 /* Look for the (optional) ellipsis. */
15353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15355 /* Consume the `...'. */
15356 cp_lexer_consume_token (parser
->lexer
);
15358 pack_expansion_p
= true;
15361 /* Add BASE to the front of the list. */
15362 if (base
!= error_mark_node
)
15364 if (pack_expansion_p
)
15365 /* Make this a pack expansion type. */
15366 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
15369 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
15371 TREE_CHAIN (base
) = bases
;
15375 /* Peek at the next token. */
15376 token
= cp_lexer_peek_token (parser
->lexer
);
15377 /* If it's not a comma, then the list is complete. */
15378 if (token
->type
!= CPP_COMMA
)
15380 /* Consume the `,'. */
15381 cp_lexer_consume_token (parser
->lexer
);
15384 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15385 base class had a qualified name. However, the next name that
15386 appears is certainly not qualified. */
15387 parser
->scope
= NULL_TREE
;
15388 parser
->qualifying_scope
= NULL_TREE
;
15389 parser
->object_scope
= NULL_TREE
;
15391 return nreverse (bases
);
15394 /* Parse a base-specifier.
15397 :: [opt] nested-name-specifier [opt] class-name
15398 virtual access-specifier [opt] :: [opt] nested-name-specifier
15400 access-specifier virtual [opt] :: [opt] nested-name-specifier
15403 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15404 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15405 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15406 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15409 cp_parser_base_specifier (cp_parser
* parser
)
15413 bool virtual_p
= false;
15414 bool duplicate_virtual_error_issued_p
= false;
15415 bool duplicate_access_error_issued_p
= false;
15416 bool class_scope_p
, template_p
;
15417 tree access
= access_default_node
;
15420 /* Process the optional `virtual' and `access-specifier'. */
15423 /* Peek at the next token. */
15424 token
= cp_lexer_peek_token (parser
->lexer
);
15425 /* Process `virtual'. */
15426 switch (token
->keyword
)
15429 /* If `virtual' appears more than once, issue an error. */
15430 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
15432 cp_parser_error (parser
,
15433 "%<virtual%> specified more than once in base-specified");
15434 duplicate_virtual_error_issued_p
= true;
15439 /* Consume the `virtual' token. */
15440 cp_lexer_consume_token (parser
->lexer
);
15445 case RID_PROTECTED
:
15447 /* If more than one access specifier appears, issue an
15449 if (access
!= access_default_node
15450 && !duplicate_access_error_issued_p
)
15452 cp_parser_error (parser
,
15453 "more than one access specifier in base-specified");
15454 duplicate_access_error_issued_p
= true;
15457 access
= ridpointers
[(int) token
->keyword
];
15459 /* Consume the access-specifier. */
15460 cp_lexer_consume_token (parser
->lexer
);
15469 /* It is not uncommon to see programs mechanically, erroneously, use
15470 the 'typename' keyword to denote (dependent) qualified types
15471 as base classes. */
15472 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15474 if (!processing_template_decl
)
15475 error ("keyword %<typename%> not allowed outside of templates");
15477 error ("keyword %<typename%> not allowed in this context "
15478 "(the base class is implicitly a type)");
15479 cp_lexer_consume_token (parser
->lexer
);
15482 /* Look for the optional `::' operator. */
15483 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
15484 /* Look for the nested-name-specifier. The simplest way to
15489 The keyword `typename' is not permitted in a base-specifier or
15490 mem-initializer; in these contexts a qualified name that
15491 depends on a template-parameter is implicitly assumed to be a
15494 is to pretend that we have seen the `typename' keyword at this
15496 cp_parser_nested_name_specifier_opt (parser
,
15497 /*typename_keyword_p=*/true,
15498 /*check_dependency_p=*/true,
15500 /*is_declaration=*/true);
15501 /* If the base class is given by a qualified name, assume that names
15502 we see are type names or templates, as appropriate. */
15503 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
15504 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
15506 /* Finally, look for the class-name. */
15507 type
= cp_parser_class_name (parser
,
15511 /*check_dependency_p=*/true,
15512 /*class_head_p=*/false,
15513 /*is_declaration=*/true);
15515 if (type
== error_mark_node
)
15516 return error_mark_node
;
15518 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
15521 /* Exception handling [gram.exception] */
15523 /* Parse an (optional) exception-specification.
15525 exception-specification:
15526 throw ( type-id-list [opt] )
15528 Returns a TREE_LIST representing the exception-specification. The
15529 TREE_VALUE of each node is a type. */
15532 cp_parser_exception_specification_opt (cp_parser
* parser
)
15537 /* Peek at the next token. */
15538 token
= cp_lexer_peek_token (parser
->lexer
);
15539 /* If it's not `throw', then there's no exception-specification. */
15540 if (!cp_parser_is_keyword (token
, RID_THROW
))
15543 /* Consume the `throw'. */
15544 cp_lexer_consume_token (parser
->lexer
);
15546 /* Look for the `('. */
15547 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15549 /* Peek at the next token. */
15550 token
= cp_lexer_peek_token (parser
->lexer
);
15551 /* If it's not a `)', then there is a type-id-list. */
15552 if (token
->type
!= CPP_CLOSE_PAREN
)
15554 const char *saved_message
;
15556 /* Types may not be defined in an exception-specification. */
15557 saved_message
= parser
->type_definition_forbidden_message
;
15558 parser
->type_definition_forbidden_message
15559 = "types may not be defined in an exception-specification";
15560 /* Parse the type-id-list. */
15561 type_id_list
= cp_parser_type_id_list (parser
);
15562 /* Restore the saved message. */
15563 parser
->type_definition_forbidden_message
= saved_message
;
15566 type_id_list
= empty_except_spec
;
15568 /* Look for the `)'. */
15569 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15571 return type_id_list
;
15574 /* Parse an (optional) type-id-list.
15578 type-id-list , type-id ... [opt]
15580 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15581 in the order that the types were presented. */
15584 cp_parser_type_id_list (cp_parser
* parser
)
15586 tree types
= NULL_TREE
;
15593 /* Get the next type-id. */
15594 type
= cp_parser_type_id (parser
);
15595 /* Parse the optional ellipsis. */
15596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15598 /* Consume the `...'. */
15599 cp_lexer_consume_token (parser
->lexer
);
15601 /* Turn the type into a pack expansion expression. */
15602 type
= make_pack_expansion (type
);
15604 /* Add it to the list. */
15605 types
= add_exception_specifier (types
, type
, /*complain=*/1);
15606 /* Peek at the next token. */
15607 token
= cp_lexer_peek_token (parser
->lexer
);
15608 /* If it is not a `,', we are done. */
15609 if (token
->type
!= CPP_COMMA
)
15611 /* Consume the `,'. */
15612 cp_lexer_consume_token (parser
->lexer
);
15615 return nreverse (types
);
15618 /* Parse a try-block.
15621 try compound-statement handler-seq */
15624 cp_parser_try_block (cp_parser
* parser
)
15628 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
15629 try_block
= begin_try_block ();
15630 cp_parser_compound_statement (parser
, NULL
, true);
15631 finish_try_block (try_block
);
15632 cp_parser_handler_seq (parser
);
15633 finish_handler_sequence (try_block
);
15638 /* Parse a function-try-block.
15640 function-try-block:
15641 try ctor-initializer [opt] function-body handler-seq */
15644 cp_parser_function_try_block (cp_parser
* parser
)
15646 tree compound_stmt
;
15648 bool ctor_initializer_p
;
15650 /* Look for the `try' keyword. */
15651 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
15653 /* Let the rest of the front end know where we are. */
15654 try_block
= begin_function_try_block (&compound_stmt
);
15655 /* Parse the function-body. */
15657 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15658 /* We're done with the `try' part. */
15659 finish_function_try_block (try_block
);
15660 /* Parse the handlers. */
15661 cp_parser_handler_seq (parser
);
15662 /* We're done with the handlers. */
15663 finish_function_handler_sequence (try_block
, compound_stmt
);
15665 return ctor_initializer_p
;
15668 /* Parse a handler-seq.
15671 handler handler-seq [opt] */
15674 cp_parser_handler_seq (cp_parser
* parser
)
15680 /* Parse the handler. */
15681 cp_parser_handler (parser
);
15682 /* Peek at the next token. */
15683 token
= cp_lexer_peek_token (parser
->lexer
);
15684 /* If it's not `catch' then there are no more handlers. */
15685 if (!cp_parser_is_keyword (token
, RID_CATCH
))
15690 /* Parse a handler.
15693 catch ( exception-declaration ) compound-statement */
15696 cp_parser_handler (cp_parser
* parser
)
15701 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
15702 handler
= begin_handler ();
15703 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15704 declaration
= cp_parser_exception_declaration (parser
);
15705 finish_handler_parms (declaration
, handler
);
15706 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15707 cp_parser_compound_statement (parser
, NULL
, false);
15708 finish_handler (handler
);
15711 /* Parse an exception-declaration.
15713 exception-declaration:
15714 type-specifier-seq declarator
15715 type-specifier-seq abstract-declarator
15719 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15720 ellipsis variant is used. */
15723 cp_parser_exception_declaration (cp_parser
* parser
)
15725 cp_decl_specifier_seq type_specifiers
;
15726 cp_declarator
*declarator
;
15727 const char *saved_message
;
15729 /* If it's an ellipsis, it's easy to handle. */
15730 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15732 /* Consume the `...' token. */
15733 cp_lexer_consume_token (parser
->lexer
);
15737 /* Types may not be defined in exception-declarations. */
15738 saved_message
= parser
->type_definition_forbidden_message
;
15739 parser
->type_definition_forbidden_message
15740 = "types may not be defined in exception-declarations";
15742 /* Parse the type-specifier-seq. */
15743 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
15745 /* If it's a `)', then there is no declarator. */
15746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
15749 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
15750 /*ctor_dtor_or_conv_p=*/NULL
,
15751 /*parenthesized_p=*/NULL
,
15752 /*member_p=*/false);
15754 /* Restore the saved message. */
15755 parser
->type_definition_forbidden_message
= saved_message
;
15757 if (!type_specifiers
.any_specifiers_p
)
15758 return error_mark_node
;
15760 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
15763 /* Parse a throw-expression.
15766 throw assignment-expression [opt]
15768 Returns a THROW_EXPR representing the throw-expression. */
15771 cp_parser_throw_expression (cp_parser
* parser
)
15776 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
15777 token
= cp_lexer_peek_token (parser
->lexer
);
15778 /* Figure out whether or not there is an assignment-expression
15779 following the "throw" keyword. */
15780 if (token
->type
== CPP_COMMA
15781 || token
->type
== CPP_SEMICOLON
15782 || token
->type
== CPP_CLOSE_PAREN
15783 || token
->type
== CPP_CLOSE_SQUARE
15784 || token
->type
== CPP_CLOSE_BRACE
15785 || token
->type
== CPP_COLON
)
15786 expression
= NULL_TREE
;
15788 expression
= cp_parser_assignment_expression (parser
,
15791 return build_throw (expression
);
15794 /* GNU Extensions */
15796 /* Parse an (optional) asm-specification.
15799 asm ( string-literal )
15801 If the asm-specification is present, returns a STRING_CST
15802 corresponding to the string-literal. Otherwise, returns
15806 cp_parser_asm_specification_opt (cp_parser
* parser
)
15809 tree asm_specification
;
15811 /* Peek at the next token. */
15812 token
= cp_lexer_peek_token (parser
->lexer
);
15813 /* If the next token isn't the `asm' keyword, then there's no
15814 asm-specification. */
15815 if (!cp_parser_is_keyword (token
, RID_ASM
))
15818 /* Consume the `asm' token. */
15819 cp_lexer_consume_token (parser
->lexer
);
15820 /* Look for the `('. */
15821 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15823 /* Look for the string-literal. */
15824 asm_specification
= cp_parser_string_literal (parser
, false, false);
15826 /* Look for the `)'. */
15827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
15829 return asm_specification
;
15832 /* Parse an asm-operand-list.
15836 asm-operand-list , asm-operand
15839 string-literal ( expression )
15840 [ string-literal ] string-literal ( expression )
15842 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15843 each node is the expression. The TREE_PURPOSE is itself a
15844 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15845 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15846 is a STRING_CST for the string literal before the parenthesis. Returns
15847 ERROR_MARK_NODE if any of the operands are invalid. */
15850 cp_parser_asm_operand_list (cp_parser
* parser
)
15852 tree asm_operands
= NULL_TREE
;
15853 bool invalid_operands
= false;
15857 tree string_literal
;
15861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
15863 /* Consume the `[' token. */
15864 cp_lexer_consume_token (parser
->lexer
);
15865 /* Read the operand name. */
15866 name
= cp_parser_identifier (parser
);
15867 if (name
!= error_mark_node
)
15868 name
= build_string (IDENTIFIER_LENGTH (name
),
15869 IDENTIFIER_POINTER (name
));
15870 /* Look for the closing `]'. */
15871 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
15875 /* Look for the string-literal. */
15876 string_literal
= cp_parser_string_literal (parser
, false, false);
15878 /* Look for the `('. */
15879 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15880 /* Parse the expression. */
15881 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
15882 /* Look for the `)'. */
15883 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15885 if (name
== error_mark_node
15886 || string_literal
== error_mark_node
15887 || expression
== error_mark_node
)
15888 invalid_operands
= true;
15890 /* Add this operand to the list. */
15891 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
15894 /* If the next token is not a `,', there are no more
15896 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15898 /* Consume the `,'. */
15899 cp_lexer_consume_token (parser
->lexer
);
15902 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
15905 /* Parse an asm-clobber-list.
15909 asm-clobber-list , string-literal
15911 Returns a TREE_LIST, indicating the clobbers in the order that they
15912 appeared. The TREE_VALUE of each node is a STRING_CST. */
15915 cp_parser_asm_clobber_list (cp_parser
* parser
)
15917 tree clobbers
= NULL_TREE
;
15921 tree string_literal
;
15923 /* Look for the string literal. */
15924 string_literal
= cp_parser_string_literal (parser
, false, false);
15925 /* Add it to the list. */
15926 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
15927 /* If the next token is not a `,', then the list is
15929 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15931 /* Consume the `,' token. */
15932 cp_lexer_consume_token (parser
->lexer
);
15938 /* Parse an (optional) series of attributes.
15941 attributes attribute
15944 __attribute__ (( attribute-list [opt] ))
15946 The return value is as for cp_parser_attribute_list. */
15949 cp_parser_attributes_opt (cp_parser
* parser
)
15951 tree attributes
= NULL_TREE
;
15956 tree attribute_list
;
15958 /* Peek at the next token. */
15959 token
= cp_lexer_peek_token (parser
->lexer
);
15960 /* If it's not `__attribute__', then we're done. */
15961 if (token
->keyword
!= RID_ATTRIBUTE
)
15964 /* Consume the `__attribute__' keyword. */
15965 cp_lexer_consume_token (parser
->lexer
);
15966 /* Look for the two `(' tokens. */
15967 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15968 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15970 /* Peek at the next token. */
15971 token
= cp_lexer_peek_token (parser
->lexer
);
15972 if (token
->type
!= CPP_CLOSE_PAREN
)
15973 /* Parse the attribute-list. */
15974 attribute_list
= cp_parser_attribute_list (parser
);
15976 /* If the next token is a `)', then there is no attribute
15978 attribute_list
= NULL
;
15980 /* Look for the two `)' tokens. */
15981 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15982 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15984 /* Add these new attributes to the list. */
15985 attributes
= chainon (attributes
, attribute_list
);
15991 /* Parse an attribute-list.
15995 attribute-list , attribute
15999 identifier ( identifier )
16000 identifier ( identifier , expression-list )
16001 identifier ( expression-list )
16003 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16004 to an attribute. The TREE_PURPOSE of each node is the identifier
16005 indicating which attribute is in use. The TREE_VALUE represents
16006 the arguments, if any. */
16009 cp_parser_attribute_list (cp_parser
* parser
)
16011 tree attribute_list
= NULL_TREE
;
16012 bool save_translate_strings_p
= parser
->translate_strings_p
;
16014 parser
->translate_strings_p
= false;
16021 /* Look for the identifier. We also allow keywords here; for
16022 example `__attribute__ ((const))' is legal. */
16023 token
= cp_lexer_peek_token (parser
->lexer
);
16024 if (token
->type
== CPP_NAME
16025 || token
->type
== CPP_KEYWORD
)
16027 tree arguments
= NULL_TREE
;
16029 /* Consume the token. */
16030 token
= cp_lexer_consume_token (parser
->lexer
);
16032 /* Save away the identifier that indicates which attribute
16034 identifier
= token
->u
.value
;
16035 attribute
= build_tree_list (identifier
, NULL_TREE
);
16037 /* Peek at the next token. */
16038 token
= cp_lexer_peek_token (parser
->lexer
);
16039 /* If it's an `(', then parse the attribute arguments. */
16040 if (token
->type
== CPP_OPEN_PAREN
)
16042 arguments
= cp_parser_parenthesized_expression_list
16043 (parser
, true, /*cast_p=*/false,
16044 /*allow_expansion_p=*/false,
16045 /*non_constant_p=*/NULL
);
16046 /* Save the arguments away. */
16047 TREE_VALUE (attribute
) = arguments
;
16050 if (arguments
!= error_mark_node
)
16052 /* Add this attribute to the list. */
16053 TREE_CHAIN (attribute
) = attribute_list
;
16054 attribute_list
= attribute
;
16057 token
= cp_lexer_peek_token (parser
->lexer
);
16059 /* Now, look for more attributes. If the next token isn't a
16060 `,', we're done. */
16061 if (token
->type
!= CPP_COMMA
)
16064 /* Consume the comma and keep going. */
16065 cp_lexer_consume_token (parser
->lexer
);
16067 parser
->translate_strings_p
= save_translate_strings_p
;
16069 /* We built up the list in reverse order. */
16070 return nreverse (attribute_list
);
16073 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16074 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16075 current value of the PEDANTIC flag, regardless of whether or not
16076 the `__extension__' keyword is present. The caller is responsible
16077 for restoring the value of the PEDANTIC flag. */
16080 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
16082 /* Save the old value of the PEDANTIC flag. */
16083 *saved_pedantic
= pedantic
;
16085 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
16087 /* Consume the `__extension__' token. */
16088 cp_lexer_consume_token (parser
->lexer
);
16089 /* We're not being pedantic while the `__extension__' keyword is
16099 /* Parse a label declaration.
16102 __label__ label-declarator-seq ;
16104 label-declarator-seq:
16105 identifier , label-declarator-seq
16109 cp_parser_label_declaration (cp_parser
* parser
)
16111 /* Look for the `__label__' keyword. */
16112 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
16118 /* Look for an identifier. */
16119 identifier
= cp_parser_identifier (parser
);
16120 /* If we failed, stop. */
16121 if (identifier
== error_mark_node
)
16123 /* Declare it as a label. */
16124 finish_label_decl (identifier
);
16125 /* If the next token is a `;', stop. */
16126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16128 /* Look for the `,' separating the label declarations. */
16129 cp_parser_require (parser
, CPP_COMMA
, "`,'");
16132 /* Look for the final `;'. */
16133 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
16136 /* Support Functions */
16138 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16139 NAME should have one of the representations used for an
16140 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16141 is returned. If PARSER->SCOPE is a dependent type, then a
16142 SCOPE_REF is returned.
16144 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16145 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16146 was formed. Abstractly, such entities should not be passed to this
16147 function, because they do not need to be looked up, but it is
16148 simpler to check for this special case here, rather than at the
16151 In cases not explicitly covered above, this function returns a
16152 DECL, OVERLOAD, or baselink representing the result of the lookup.
16153 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16156 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16157 (e.g., "struct") that was used. In that case bindings that do not
16158 refer to types are ignored.
16160 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16163 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16166 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16169 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16170 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16171 NULL_TREE otherwise. */
16174 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
16175 enum tag_types tag_type
,
16178 bool check_dependency
,
16179 tree
*ambiguous_decls
)
16183 tree object_type
= parser
->context
->object_type
;
16185 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16186 flags
|= LOOKUP_COMPLAIN
;
16188 /* Assume that the lookup will be unambiguous. */
16189 if (ambiguous_decls
)
16190 *ambiguous_decls
= NULL_TREE
;
16192 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16193 no longer valid. Note that if we are parsing tentatively, and
16194 the parse fails, OBJECT_TYPE will be automatically restored. */
16195 parser
->context
->object_type
= NULL_TREE
;
16197 if (name
== error_mark_node
)
16198 return error_mark_node
;
16200 /* A template-id has already been resolved; there is no lookup to
16202 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16204 if (BASELINK_P (name
))
16206 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
16207 == TEMPLATE_ID_EXPR
);
16211 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16212 it should already have been checked to make sure that the name
16213 used matches the type being destroyed. */
16214 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16218 /* Figure out to which type this destructor applies. */
16220 type
= parser
->scope
;
16221 else if (object_type
)
16222 type
= object_type
;
16224 type
= current_class_type
;
16225 /* If that's not a class type, there is no destructor. */
16226 if (!type
|| !CLASS_TYPE_P (type
))
16227 return error_mark_node
;
16228 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
16229 lazily_declare_fn (sfk_destructor
, type
);
16230 if (!CLASSTYPE_DESTRUCTORS (type
))
16231 return error_mark_node
;
16232 /* If it was a class type, return the destructor. */
16233 return CLASSTYPE_DESTRUCTORS (type
);
16236 /* By this point, the NAME should be an ordinary identifier. If
16237 the id-expression was a qualified name, the qualifying scope is
16238 stored in PARSER->SCOPE at this point. */
16239 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
16241 /* Perform the lookup. */
16246 if (parser
->scope
== error_mark_node
)
16247 return error_mark_node
;
16249 /* If the SCOPE is dependent, the lookup must be deferred until
16250 the template is instantiated -- unless we are explicitly
16251 looking up names in uninstantiated templates. Even then, we
16252 cannot look up the name if the scope is not a class type; it
16253 might, for example, be a template type parameter. */
16254 dependent_p
= (TYPE_P (parser
->scope
)
16255 && !(parser
->in_declarator_p
16256 && currently_open_class (parser
->scope
))
16257 && dependent_type_p (parser
->scope
));
16258 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
16265 /* The resolution to Core Issue 180 says that `struct
16266 A::B' should be considered a type-name, even if `A'
16268 type
= make_typename_type (parser
->scope
, name
, tag_type
,
16269 /*complain=*/tf_error
);
16270 decl
= TYPE_NAME (type
);
16272 else if (is_template
16273 && (cp_parser_next_token_ends_template_argument_p (parser
)
16274 || cp_lexer_next_token_is (parser
->lexer
,
16276 decl
= make_unbound_class_template (parser
->scope
,
16278 /*complain=*/tf_error
);
16280 decl
= build_qualified_name (/*type=*/NULL_TREE
,
16281 parser
->scope
, name
,
16286 tree pushed_scope
= NULL_TREE
;
16288 /* If PARSER->SCOPE is a dependent type, then it must be a
16289 class type, and we must not be checking dependencies;
16290 otherwise, we would have processed this lookup above. So
16291 that PARSER->SCOPE is not considered a dependent base by
16292 lookup_member, we must enter the scope here. */
16294 pushed_scope
= push_scope (parser
->scope
);
16295 /* If the PARSER->SCOPE is a template specialization, it
16296 may be instantiated during name lookup. In that case,
16297 errors may be issued. Even if we rollback the current
16298 tentative parse, those errors are valid. */
16299 decl
= lookup_qualified_name (parser
->scope
, name
,
16300 tag_type
!= none_type
,
16301 /*complain=*/true);
16303 pop_scope (pushed_scope
);
16305 parser
->qualifying_scope
= parser
->scope
;
16306 parser
->object_scope
= NULL_TREE
;
16308 else if (object_type
)
16310 tree object_decl
= NULL_TREE
;
16311 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16312 OBJECT_TYPE is not a class. */
16313 if (CLASS_TYPE_P (object_type
))
16314 /* If the OBJECT_TYPE is a template specialization, it may
16315 be instantiated during name lookup. In that case, errors
16316 may be issued. Even if we rollback the current tentative
16317 parse, those errors are valid. */
16318 object_decl
= lookup_member (object_type
,
16321 tag_type
!= none_type
);
16322 /* Look it up in the enclosing context, too. */
16323 decl
= lookup_name_real (name
, tag_type
!= none_type
,
16325 /*block_p=*/true, is_namespace
, flags
);
16326 parser
->object_scope
= object_type
;
16327 parser
->qualifying_scope
= NULL_TREE
;
16329 decl
= object_decl
;
16333 decl
= lookup_name_real (name
, tag_type
!= none_type
,
16335 /*block_p=*/true, is_namespace
, flags
);
16336 parser
->qualifying_scope
= NULL_TREE
;
16337 parser
->object_scope
= NULL_TREE
;
16340 /* If the lookup failed, let our caller know. */
16341 if (!decl
|| decl
== error_mark_node
)
16342 return error_mark_node
;
16344 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16345 if (TREE_CODE (decl
) == TREE_LIST
)
16347 if (ambiguous_decls
)
16348 *ambiguous_decls
= decl
;
16349 /* The error message we have to print is too complicated for
16350 cp_parser_error, so we incorporate its actions directly. */
16351 if (!cp_parser_simulate_error (parser
))
16353 error ("reference to %qD is ambiguous", name
);
16354 print_candidates (decl
);
16356 return error_mark_node
;
16359 gcc_assert (DECL_P (decl
)
16360 || TREE_CODE (decl
) == OVERLOAD
16361 || TREE_CODE (decl
) == SCOPE_REF
16362 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
16363 || BASELINK_P (decl
));
16365 /* If we have resolved the name of a member declaration, check to
16366 see if the declaration is accessible. When the name resolves to
16367 set of overloaded functions, accessibility is checked when
16368 overload resolution is done.
16370 During an explicit instantiation, access is not checked at all,
16371 as per [temp.explicit]. */
16373 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
16378 /* Like cp_parser_lookup_name, but for use in the typical case where
16379 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16380 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16383 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
16385 return cp_parser_lookup_name (parser
, name
,
16387 /*is_template=*/false,
16388 /*is_namespace=*/false,
16389 /*check_dependency=*/true,
16390 /*ambiguous_decls=*/NULL
);
16393 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16394 the current context, return the TYPE_DECL. If TAG_NAME_P is
16395 true, the DECL indicates the class being defined in a class-head,
16396 or declared in an elaborated-type-specifier.
16398 Otherwise, return DECL. */
16401 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
16403 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16404 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16407 template <typename T> struct B;
16410 template <typename T> struct A::B {};
16412 Similarly, in an elaborated-type-specifier:
16414 namespace N { struct X{}; }
16417 template <typename T> friend struct N::X;
16420 However, if the DECL refers to a class type, and we are in
16421 the scope of the class, then the name lookup automatically
16422 finds the TYPE_DECL created by build_self_reference rather
16423 than a TEMPLATE_DECL. For example, in:
16425 template <class T> struct S {
16429 there is no need to handle such case. */
16431 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
16432 return DECL_TEMPLATE_RESULT (decl
);
16437 /* If too many, or too few, template-parameter lists apply to the
16438 declarator, issue an error message. Returns TRUE if all went well,
16439 and FALSE otherwise. */
16442 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
16443 cp_declarator
*declarator
)
16445 unsigned num_templates
;
16447 /* We haven't seen any classes that involve template parameters yet. */
16450 switch (declarator
->kind
)
16453 if (declarator
->u
.id
.qualifying_scope
)
16458 scope
= declarator
->u
.id
.qualifying_scope
;
16459 member
= declarator
->u
.id
.unqualified_name
;
16461 while (scope
&& CLASS_TYPE_P (scope
))
16463 /* You're supposed to have one `template <...>'
16464 for every template class, but you don't need one
16465 for a full specialization. For example:
16467 template <class T> struct S{};
16468 template <> struct S<int> { void f(); };
16469 void S<int>::f () {}
16471 is correct; there shouldn't be a `template <>' for
16472 the definition of `S<int>::f'. */
16473 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
16474 /* If SCOPE does not have template information of any
16475 kind, then it is not a template, nor is it nested
16476 within a template. */
16478 if (explicit_class_specialization_p (scope
))
16480 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
16483 scope
= TYPE_CONTEXT (scope
);
16486 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
16487 == TEMPLATE_ID_EXPR
)
16488 /* If the DECLARATOR has the form `X<y>' then it uses one
16489 additional level of template parameters. */
16492 return cp_parser_check_template_parameters (parser
,
16498 case cdk_reference
:
16500 return (cp_parser_check_declarator_template_parameters
16501 (parser
, declarator
->declarator
));
16507 gcc_unreachable ();
16512 /* NUM_TEMPLATES were used in the current declaration. If that is
16513 invalid, return FALSE and issue an error messages. Otherwise,
16517 cp_parser_check_template_parameters (cp_parser
* parser
,
16518 unsigned num_templates
)
16520 /* If there are more template classes than parameter lists, we have
16523 template <class T> void S<T>::R<T>::f (); */
16524 if (parser
->num_template_parameter_lists
< num_templates
)
16526 error ("too few template-parameter-lists");
16529 /* If there are the same number of template classes and parameter
16530 lists, that's OK. */
16531 if (parser
->num_template_parameter_lists
== num_templates
)
16533 /* If there are more, but only one more, then we are referring to a
16534 member template. That's OK too. */
16535 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
16537 /* Otherwise, there are too many template parameter lists. We have
16540 template <class T> template <class U> void S::f(); */
16541 error ("too many template-parameter-lists");
16545 /* Parse an optional `::' token indicating that the following name is
16546 from the global namespace. If so, PARSER->SCOPE is set to the
16547 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16548 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16549 Returns the new value of PARSER->SCOPE, if the `::' token is
16550 present, and NULL_TREE otherwise. */
16553 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
16557 /* Peek at the next token. */
16558 token
= cp_lexer_peek_token (parser
->lexer
);
16559 /* If we're looking at a `::' token then we're starting from the
16560 global namespace, not our current location. */
16561 if (token
->type
== CPP_SCOPE
)
16563 /* Consume the `::' token. */
16564 cp_lexer_consume_token (parser
->lexer
);
16565 /* Set the SCOPE so that we know where to start the lookup. */
16566 parser
->scope
= global_namespace
;
16567 parser
->qualifying_scope
= global_namespace
;
16568 parser
->object_scope
= NULL_TREE
;
16570 return parser
->scope
;
16572 else if (!current_scope_valid_p
)
16574 parser
->scope
= NULL_TREE
;
16575 parser
->qualifying_scope
= NULL_TREE
;
16576 parser
->object_scope
= NULL_TREE
;
16582 /* Returns TRUE if the upcoming token sequence is the start of a
16583 constructor declarator. If FRIEND_P is true, the declarator is
16584 preceded by the `friend' specifier. */
16587 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
16589 bool constructor_p
;
16590 tree type_decl
= NULL_TREE
;
16591 bool nested_name_p
;
16592 cp_token
*next_token
;
16594 /* The common case is that this is not a constructor declarator, so
16595 try to avoid doing lots of work if at all possible. It's not
16596 valid declare a constructor at function scope. */
16597 if (parser
->in_function_body
)
16599 /* And only certain tokens can begin a constructor declarator. */
16600 next_token
= cp_lexer_peek_token (parser
->lexer
);
16601 if (next_token
->type
!= CPP_NAME
16602 && next_token
->type
!= CPP_SCOPE
16603 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
16604 && next_token
->type
!= CPP_TEMPLATE_ID
)
16607 /* Parse tentatively; we are going to roll back all of the tokens
16609 cp_parser_parse_tentatively (parser
);
16610 /* Assume that we are looking at a constructor declarator. */
16611 constructor_p
= true;
16613 /* Look for the optional `::' operator. */
16614 cp_parser_global_scope_opt (parser
,
16615 /*current_scope_valid_p=*/false);
16616 /* Look for the nested-name-specifier. */
16618 = (cp_parser_nested_name_specifier_opt (parser
,
16619 /*typename_keyword_p=*/false,
16620 /*check_dependency_p=*/false,
16622 /*is_declaration=*/false)
16624 /* Outside of a class-specifier, there must be a
16625 nested-name-specifier. */
16626 if (!nested_name_p
&&
16627 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
16629 constructor_p
= false;
16630 /* If we still think that this might be a constructor-declarator,
16631 look for a class-name. */
16636 template <typename T> struct S { S(); };
16637 template <typename T> S<T>::S ();
16639 we must recognize that the nested `S' names a class.
16642 template <typename T> S<T>::S<T> ();
16644 we must recognize that the nested `S' names a template. */
16645 type_decl
= cp_parser_class_name (parser
,
16646 /*typename_keyword_p=*/false,
16647 /*template_keyword_p=*/false,
16649 /*check_dependency_p=*/false,
16650 /*class_head_p=*/false,
16651 /*is_declaration=*/false);
16652 /* If there was no class-name, then this is not a constructor. */
16653 constructor_p
= !cp_parser_error_occurred (parser
);
16656 /* If we're still considering a constructor, we have to see a `(',
16657 to begin the parameter-declaration-clause, followed by either a
16658 `)', an `...', or a decl-specifier. We need to check for a
16659 type-specifier to avoid being fooled into thinking that:
16663 is a constructor. (It is actually a function named `f' that
16664 takes one parameter (of type `int') and returns a value of type
16667 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
16669 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16670 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
16671 /* A parameter declaration begins with a decl-specifier,
16672 which is either the "attribute" keyword, a storage class
16673 specifier, or (usually) a type-specifier. */
16674 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
16677 tree pushed_scope
= NULL_TREE
;
16678 unsigned saved_num_template_parameter_lists
;
16680 /* Names appearing in the type-specifier should be looked up
16681 in the scope of the class. */
16682 if (current_class_type
)
16686 type
= TREE_TYPE (type_decl
);
16687 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16689 type
= resolve_typename_type (type
,
16690 /*only_current_p=*/false);
16691 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16693 cp_parser_abort_tentative_parse (parser
);
16697 pushed_scope
= push_scope (type
);
16700 /* Inside the constructor parameter list, surrounding
16701 template-parameter-lists do not apply. */
16702 saved_num_template_parameter_lists
16703 = parser
->num_template_parameter_lists
;
16704 parser
->num_template_parameter_lists
= 0;
16706 /* Look for the type-specifier. */
16707 cp_parser_type_specifier (parser
,
16708 CP_PARSER_FLAGS_NONE
,
16709 /*decl_specs=*/NULL
,
16710 /*is_declarator=*/true,
16711 /*declares_class_or_enum=*/NULL
,
16712 /*is_cv_qualifier=*/NULL
);
16714 parser
->num_template_parameter_lists
16715 = saved_num_template_parameter_lists
;
16717 /* Leave the scope of the class. */
16719 pop_scope (pushed_scope
);
16721 constructor_p
= !cp_parser_error_occurred (parser
);
16725 constructor_p
= false;
16726 /* We did not really want to consume any tokens. */
16727 cp_parser_abort_tentative_parse (parser
);
16729 return constructor_p
;
16732 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16733 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16734 they must be performed once we are in the scope of the function.
16736 Returns the function defined. */
16739 cp_parser_function_definition_from_specifiers_and_declarator
16740 (cp_parser
* parser
,
16741 cp_decl_specifier_seq
*decl_specifiers
,
16743 const cp_declarator
*declarator
)
16748 /* Begin the function-definition. */
16749 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
16751 /* The things we're about to see are not directly qualified by any
16752 template headers we've seen thus far. */
16753 reset_specialization ();
16755 /* If there were names looked up in the decl-specifier-seq that we
16756 did not check, check them now. We must wait until we are in the
16757 scope of the function to perform the checks, since the function
16758 might be a friend. */
16759 perform_deferred_access_checks ();
16763 /* Skip the entire function. */
16764 cp_parser_skip_to_end_of_block_or_statement (parser
);
16765 fn
= error_mark_node
;
16767 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
16769 /* Seen already, skip it. An error message has already been output. */
16770 cp_parser_skip_to_end_of_block_or_statement (parser
);
16771 fn
= current_function_decl
;
16772 current_function_decl
= NULL_TREE
;
16773 /* If this is a function from a class, pop the nested class. */
16774 if (current_class_name
)
16775 pop_nested_class ();
16778 fn
= cp_parser_function_definition_after_declarator (parser
,
16779 /*inline_p=*/false);
16784 /* Parse the part of a function-definition that follows the
16785 declarator. INLINE_P is TRUE iff this function is an inline
16786 function defined with a class-specifier.
16788 Returns the function defined. */
16791 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
16795 bool ctor_initializer_p
= false;
16796 bool saved_in_unbraced_linkage_specification_p
;
16797 bool saved_in_function_body
;
16798 unsigned saved_num_template_parameter_lists
;
16800 saved_in_function_body
= parser
->in_function_body
;
16801 parser
->in_function_body
= true;
16802 /* If the next token is `return', then the code may be trying to
16803 make use of the "named return value" extension that G++ used to
16805 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
16807 /* Consume the `return' keyword. */
16808 cp_lexer_consume_token (parser
->lexer
);
16809 /* Look for the identifier that indicates what value is to be
16811 cp_parser_identifier (parser
);
16812 /* Issue an error message. */
16813 error ("named return values are no longer supported");
16814 /* Skip tokens until we reach the start of the function body. */
16817 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16818 if (token
->type
== CPP_OPEN_BRACE
16819 || token
->type
== CPP_EOF
16820 || token
->type
== CPP_PRAGMA_EOL
)
16822 cp_lexer_consume_token (parser
->lexer
);
16825 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16826 anything declared inside `f'. */
16827 saved_in_unbraced_linkage_specification_p
16828 = parser
->in_unbraced_linkage_specification_p
;
16829 parser
->in_unbraced_linkage_specification_p
= false;
16830 /* Inside the function, surrounding template-parameter-lists do not
16832 saved_num_template_parameter_lists
16833 = parser
->num_template_parameter_lists
;
16834 parser
->num_template_parameter_lists
= 0;
16835 /* If the next token is `try', then we are looking at a
16836 function-try-block. */
16837 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
16838 ctor_initializer_p
= cp_parser_function_try_block (parser
);
16839 /* A function-try-block includes the function-body, so we only do
16840 this next part if we're not processing a function-try-block. */
16843 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
16845 /* Finish the function. */
16846 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
16847 (inline_p
? 2 : 0));
16848 /* Generate code for it, if necessary. */
16849 expand_or_defer_fn (fn
);
16850 /* Restore the saved values. */
16851 parser
->in_unbraced_linkage_specification_p
16852 = saved_in_unbraced_linkage_specification_p
;
16853 parser
->num_template_parameter_lists
16854 = saved_num_template_parameter_lists
;
16855 parser
->in_function_body
= saved_in_function_body
;
16860 /* Parse a template-declaration, assuming that the `export' (and
16861 `extern') keywords, if present, has already been scanned. MEMBER_P
16862 is as for cp_parser_template_declaration. */
16865 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
16867 tree decl
= NULL_TREE
;
16868 VEC (deferred_access_check
,gc
) *checks
;
16869 tree parameter_list
;
16870 bool friend_p
= false;
16871 bool need_lang_pop
;
16873 /* Look for the `template' keyword. */
16874 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
16878 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
16880 if (at_class_scope_p () && current_function_decl
)
16882 /* 14.5.2.2 [temp.mem]
16884 A local class shall not have member templates. */
16885 error ("invalid declaration of member template in local class");
16886 cp_parser_skip_to_end_of_block_or_statement (parser
);
16891 A template ... shall not have C linkage. */
16892 if (current_lang_name
== lang_name_c
)
16894 error ("template with C linkage");
16895 /* Give it C++ linkage to avoid confusing other parts of the
16897 push_lang_context (lang_name_cplusplus
);
16898 need_lang_pop
= true;
16901 need_lang_pop
= false;
16903 /* We cannot perform access checks on the template parameter
16904 declarations until we know what is being declared, just as we
16905 cannot check the decl-specifier list. */
16906 push_deferring_access_checks (dk_deferred
);
16908 /* If the next token is `>', then we have an invalid
16909 specialization. Rather than complain about an invalid template
16910 parameter, issue an error message here. */
16911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16913 cp_parser_error (parser
, "invalid explicit specialization");
16914 begin_specialization ();
16915 parameter_list
= NULL_TREE
;
16918 /* Parse the template parameters. */
16919 parameter_list
= cp_parser_template_parameter_list (parser
);
16921 /* Get the deferred access checks from the parameter list. These
16922 will be checked once we know what is being declared, as for a
16923 member template the checks must be performed in the scope of the
16924 class containing the member. */
16925 checks
= get_deferred_access_checks ();
16927 /* Look for the `>'. */
16928 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16929 /* We just processed one more parameter list. */
16930 ++parser
->num_template_parameter_lists
;
16931 /* If the next token is `template', there are more template
16933 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
16935 cp_parser_template_declaration_after_export (parser
, member_p
);
16938 /* There are no access checks when parsing a template, as we do not
16939 know if a specialization will be a friend. */
16940 push_deferring_access_checks (dk_no_check
);
16941 decl
= cp_parser_single_declaration (parser
,
16944 /*explicit_specialization_p=*/false,
16946 pop_deferring_access_checks ();
16948 /* If this is a member template declaration, let the front
16950 if (member_p
&& !friend_p
&& decl
)
16952 if (TREE_CODE (decl
) == TYPE_DECL
)
16953 cp_parser_check_access_in_redeclaration (decl
);
16955 decl
= finish_member_template_decl (decl
);
16957 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
16958 make_friend_class (current_class_type
, TREE_TYPE (decl
),
16959 /*complain=*/true);
16961 /* We are done with the current parameter list. */
16962 --parser
->num_template_parameter_lists
;
16964 pop_deferring_access_checks ();
16967 finish_template_decl (parameter_list
);
16969 /* Register member declarations. */
16970 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
16971 finish_member_declaration (decl
);
16972 /* For the erroneous case of a template with C linkage, we pushed an
16973 implicit C++ linkage scope; exit that scope now. */
16975 pop_lang_context ();
16976 /* If DECL is a function template, we must return to parse it later.
16977 (Even though there is no definition, there might be default
16978 arguments that need handling.) */
16979 if (member_p
&& decl
16980 && (TREE_CODE (decl
) == FUNCTION_DECL
16981 || DECL_FUNCTION_TEMPLATE_P (decl
)))
16982 TREE_VALUE (parser
->unparsed_functions_queues
)
16983 = tree_cons (NULL_TREE
, decl
,
16984 TREE_VALUE (parser
->unparsed_functions_queues
));
16987 /* Perform the deferred access checks from a template-parameter-list.
16988 CHECKS is a TREE_LIST of access checks, as returned by
16989 get_deferred_access_checks. */
16992 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
16994 ++processing_template_parmlist
;
16995 perform_access_checks (checks
);
16996 --processing_template_parmlist
;
16999 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17000 `function-definition' sequence. MEMBER_P is true, this declaration
17001 appears in a class scope.
17003 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17004 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17007 cp_parser_single_declaration (cp_parser
* parser
,
17008 VEC (deferred_access_check
,gc
)* checks
,
17010 bool explicit_specialization_p
,
17013 int declares_class_or_enum
;
17014 tree decl
= NULL_TREE
;
17015 cp_decl_specifier_seq decl_specifiers
;
17016 bool function_definition_p
= false;
17018 /* This function is only used when processing a template
17020 gcc_assert (innermost_scope_kind () == sk_template_parms
17021 || innermost_scope_kind () == sk_template_spec
);
17023 /* Defer access checks until we know what is being declared. */
17024 push_deferring_access_checks (dk_deferred
);
17026 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17028 cp_parser_decl_specifier_seq (parser
,
17029 CP_PARSER_FLAGS_OPTIONAL
,
17031 &declares_class_or_enum
);
17033 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
17035 /* There are no template typedefs. */
17036 if (decl_specifiers
.specs
[(int) ds_typedef
])
17038 error ("template declaration of %qs", "typedef");
17039 decl
= error_mark_node
;
17042 /* Gather up the access checks that occurred the
17043 decl-specifier-seq. */
17044 stop_deferring_access_checks ();
17046 /* Check for the declaration of a template class. */
17047 if (declares_class_or_enum
)
17049 if (cp_parser_declares_only_class_p (parser
))
17051 decl
= shadow_tag (&decl_specifiers
);
17056 friend template <typename T> struct A<T>::B;
17059 A<T>::B will be represented by a TYPENAME_TYPE, and
17060 therefore not recognized by shadow_tag. */
17061 if (friend_p
&& *friend_p
17063 && decl_specifiers
.type
17064 && TYPE_P (decl_specifiers
.type
))
17065 decl
= decl_specifiers
.type
;
17067 if (decl
&& decl
!= error_mark_node
)
17068 decl
= TYPE_NAME (decl
);
17070 decl
= error_mark_node
;
17072 /* Perform access checks for template parameters. */
17073 cp_parser_perform_template_parameter_access_checks (checks
);
17076 /* If it's not a template class, try for a template function. If
17077 the next token is a `;', then this declaration does not declare
17078 anything. But, if there were errors in the decl-specifiers, then
17079 the error might well have come from an attempted class-specifier.
17080 In that case, there's no need to warn about a missing declarator. */
17082 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
17083 || decl_specifiers
.type
!= error_mark_node
))
17085 decl
= cp_parser_init_declarator (parser
,
17088 /*function_definition_allowed_p=*/true,
17090 declares_class_or_enum
,
17091 &function_definition_p
);
17093 /* 7.1.1-1 [dcl.stc]
17095 A storage-class-specifier shall not be specified in an explicit
17096 specialization... */
17098 && explicit_specialization_p
17099 && decl_specifiers
.storage_class
!= sc_none
)
17101 error ("explicit template specialization cannot have a storage class");
17102 decl
= error_mark_node
;
17106 pop_deferring_access_checks ();
17108 /* Clear any current qualification; whatever comes next is the start
17109 of something new. */
17110 parser
->scope
= NULL_TREE
;
17111 parser
->qualifying_scope
= NULL_TREE
;
17112 parser
->object_scope
= NULL_TREE
;
17113 /* Look for a trailing `;' after the declaration. */
17114 if (!function_definition_p
17115 && (decl
== error_mark_node
17116 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
17117 cp_parser_skip_to_end_of_block_or_statement (parser
);
17122 /* Parse a cast-expression that is not the operand of a unary "&". */
17125 cp_parser_simple_cast_expression (cp_parser
*parser
)
17127 return cp_parser_cast_expression (parser
, /*address_p=*/false,
17131 /* Parse a functional cast to TYPE. Returns an expression
17132 representing the cast. */
17135 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
17137 tree expression_list
;
17141 = cp_parser_parenthesized_expression_list (parser
, false,
17143 /*allow_expansion_p=*/true,
17144 /*non_constant_p=*/NULL
);
17146 cast
= build_functional_cast (type
, expression_list
);
17147 /* [expr.const]/1: In an integral constant expression "only type
17148 conversions to integral or enumeration type can be used". */
17149 if (TREE_CODE (type
) == TYPE_DECL
)
17150 type
= TREE_TYPE (type
);
17151 if (cast
!= error_mark_node
17152 && !cast_valid_in_integral_constant_expression_p (type
)
17153 && (cp_parser_non_integral_constant_expression
17154 (parser
, "a call to a constructor")))
17155 return error_mark_node
;
17159 /* Save the tokens that make up the body of a member function defined
17160 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17161 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17162 specifiers applied to the declaration. Returns the FUNCTION_DECL
17163 for the member function. */
17166 cp_parser_save_member_function_body (cp_parser
* parser
,
17167 cp_decl_specifier_seq
*decl_specifiers
,
17168 cp_declarator
*declarator
,
17175 /* Create the function-declaration. */
17176 fn
= start_method (decl_specifiers
, declarator
, attributes
);
17177 /* If something went badly wrong, bail out now. */
17178 if (fn
== error_mark_node
)
17180 /* If there's a function-body, skip it. */
17181 if (cp_parser_token_starts_function_definition_p
17182 (cp_lexer_peek_token (parser
->lexer
)))
17183 cp_parser_skip_to_end_of_block_or_statement (parser
);
17184 return error_mark_node
;
17187 /* Remember it, if there default args to post process. */
17188 cp_parser_save_default_args (parser
, fn
);
17190 /* Save away the tokens that make up the body of the
17192 first
= parser
->lexer
->next_token
;
17193 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
17194 /* Handle function try blocks. */
17195 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
17196 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
17197 last
= parser
->lexer
->next_token
;
17199 /* Save away the inline definition; we will process it when the
17200 class is complete. */
17201 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
17202 DECL_PENDING_INLINE_P (fn
) = 1;
17204 /* We need to know that this was defined in the class, so that
17205 friend templates are handled correctly. */
17206 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
17208 /* We're done with the inline definition. */
17209 finish_method (fn
);
17211 /* Add FN to the queue of functions to be parsed later. */
17212 TREE_VALUE (parser
->unparsed_functions_queues
)
17213 = tree_cons (NULL_TREE
, fn
,
17214 TREE_VALUE (parser
->unparsed_functions_queues
));
17219 /* Parse a template-argument-list, as well as the trailing ">" (but
17220 not the opening ">"). See cp_parser_template_argument_list for the
17224 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
17228 tree saved_qualifying_scope
;
17229 tree saved_object_scope
;
17230 bool saved_greater_than_is_operator_p
;
17231 bool saved_skip_evaluation
;
17235 When parsing a template-id, the first non-nested `>' is taken as
17236 the end of the template-argument-list rather than a greater-than
17238 saved_greater_than_is_operator_p
17239 = parser
->greater_than_is_operator_p
;
17240 parser
->greater_than_is_operator_p
= false;
17241 /* Parsing the argument list may modify SCOPE, so we save it
17243 saved_scope
= parser
->scope
;
17244 saved_qualifying_scope
= parser
->qualifying_scope
;
17245 saved_object_scope
= parser
->object_scope
;
17246 /* We need to evaluate the template arguments, even though this
17247 template-id may be nested within a "sizeof". */
17248 saved_skip_evaluation
= skip_evaluation
;
17249 skip_evaluation
= false;
17250 /* Parse the template-argument-list itself. */
17251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
17252 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17253 arguments
= NULL_TREE
;
17255 arguments
= cp_parser_template_argument_list (parser
);
17256 /* Look for the `>' that ends the template-argument-list. If we find
17257 a '>>' instead, it's probably just a typo. */
17258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17260 if (cxx_dialect
!= cxx98
)
17262 /* In C++0x, a `>>' in a template argument list or cast
17263 expression is considered to be two separate `>'
17264 tokens. So, change the current token to a `>', but don't
17265 consume it: it will be consumed later when the outer
17266 template argument list (or cast expression) is parsed.
17267 Note that this replacement of `>' for `>>' is necessary
17268 even if we are parsing tentatively: in the tentative
17269 case, after calling
17270 cp_parser_enclosed_template_argument_list we will always
17271 throw away all of the template arguments and the first
17272 closing `>', either because the template argument list
17273 was erroneous or because we are replacing those tokens
17274 with a CPP_TEMPLATE_ID token. The second `>' (which will
17275 not have been thrown away) is needed either to close an
17276 outer template argument list or to complete a new-style
17278 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17279 token
->type
= CPP_GREATER
;
17281 else if (!saved_greater_than_is_operator_p
)
17283 /* If we're in a nested template argument list, the '>>' has
17284 to be a typo for '> >'. We emit the error message, but we
17285 continue parsing and we push a '>' as next token, so that
17286 the argument list will be parsed correctly. Note that the
17287 global source location is still on the token before the
17288 '>>', so we need to say explicitly where we want it. */
17289 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17290 error ("%H%<>>%> should be %<> >%> "
17291 "within a nested template argument list",
17294 token
->type
= CPP_GREATER
;
17298 /* If this is not a nested template argument list, the '>>'
17299 is a typo for '>'. Emit an error message and continue.
17300 Same deal about the token location, but here we can get it
17301 right by consuming the '>>' before issuing the diagnostic. */
17302 cp_lexer_consume_token (parser
->lexer
);
17303 error ("spurious %<>>%>, use %<>%> to terminate "
17304 "a template argument list");
17308 cp_parser_skip_to_end_of_template_parameter_list (parser
);
17309 /* The `>' token might be a greater-than operator again now. */
17310 parser
->greater_than_is_operator_p
17311 = saved_greater_than_is_operator_p
;
17312 /* Restore the SAVED_SCOPE. */
17313 parser
->scope
= saved_scope
;
17314 parser
->qualifying_scope
= saved_qualifying_scope
;
17315 parser
->object_scope
= saved_object_scope
;
17316 skip_evaluation
= saved_skip_evaluation
;
17321 /* MEMBER_FUNCTION is a member function, or a friend. If default
17322 arguments, or the body of the function have not yet been parsed,
17326 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
17328 /* If this member is a template, get the underlying
17330 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
17331 member_function
= DECL_TEMPLATE_RESULT (member_function
);
17333 /* There should not be any class definitions in progress at this
17334 point; the bodies of members are only parsed outside of all class
17336 gcc_assert (parser
->num_classes_being_defined
== 0);
17337 /* While we're parsing the member functions we might encounter more
17338 classes. We want to handle them right away, but we don't want
17339 them getting mixed up with functions that are currently in the
17341 parser
->unparsed_functions_queues
17342 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
17344 /* Make sure that any template parameters are in scope. */
17345 maybe_begin_member_template_processing (member_function
);
17347 /* If the body of the function has not yet been parsed, parse it
17349 if (DECL_PENDING_INLINE_P (member_function
))
17351 tree function_scope
;
17352 cp_token_cache
*tokens
;
17354 /* The function is no longer pending; we are processing it. */
17355 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
17356 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
17357 DECL_PENDING_INLINE_P (member_function
) = 0;
17359 /* If this is a local class, enter the scope of the containing
17361 function_scope
= current_function_decl
;
17362 if (function_scope
)
17363 push_function_context_to (function_scope
);
17366 /* Push the body of the function onto the lexer stack. */
17367 cp_parser_push_lexer_for_tokens (parser
, tokens
);
17369 /* Let the front end know that we going to be defining this
17371 start_preparsed_function (member_function
, NULL_TREE
,
17372 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
17374 /* Don't do access checking if it is a templated function. */
17375 if (processing_template_decl
)
17376 push_deferring_access_checks (dk_no_check
);
17378 /* Now, parse the body of the function. */
17379 cp_parser_function_definition_after_declarator (parser
,
17380 /*inline_p=*/true);
17382 if (processing_template_decl
)
17383 pop_deferring_access_checks ();
17385 /* Leave the scope of the containing function. */
17386 if (function_scope
)
17387 pop_function_context_from (function_scope
);
17388 cp_parser_pop_lexer (parser
);
17391 /* Remove any template parameters from the symbol table. */
17392 maybe_end_member_template_processing ();
17394 /* Restore the queue. */
17395 parser
->unparsed_functions_queues
17396 = TREE_CHAIN (parser
->unparsed_functions_queues
);
17399 /* If DECL contains any default args, remember it on the unparsed
17400 functions queue. */
17403 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
17407 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
17409 probe
= TREE_CHAIN (probe
))
17410 if (TREE_PURPOSE (probe
))
17412 TREE_PURPOSE (parser
->unparsed_functions_queues
)
17413 = tree_cons (current_class_type
, decl
,
17414 TREE_PURPOSE (parser
->unparsed_functions_queues
));
17419 /* FN is a FUNCTION_DECL which may contains a parameter with an
17420 unparsed DEFAULT_ARG. Parse the default args now. This function
17421 assumes that the current scope is the scope in which the default
17422 argument should be processed. */
17425 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
17427 bool saved_local_variables_forbidden_p
;
17430 /* While we're parsing the default args, we might (due to the
17431 statement expression extension) encounter more classes. We want
17432 to handle them right away, but we don't want them getting mixed
17433 up with default args that are currently in the queue. */
17434 parser
->unparsed_functions_queues
17435 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
17437 /* Local variable names (and the `this' keyword) may not appear
17438 in a default argument. */
17439 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
17440 parser
->local_variables_forbidden_p
= true;
17442 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
17444 parm
= TREE_CHAIN (parm
))
17446 cp_token_cache
*tokens
;
17447 tree default_arg
= TREE_PURPOSE (parm
);
17449 VEC(tree
,gc
) *insts
;
17456 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
17457 /* This can happen for a friend declaration for a function
17458 already declared with default arguments. */
17461 /* Push the saved tokens for the default argument onto the parser's
17463 tokens
= DEFARG_TOKENS (default_arg
);
17464 cp_parser_push_lexer_for_tokens (parser
, tokens
);
17466 /* Parse the assignment-expression. */
17467 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
17469 if (!processing_template_decl
)
17470 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
17472 TREE_PURPOSE (parm
) = parsed_arg
;
17474 /* Update any instantiations we've already created. */
17475 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
17476 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
17477 TREE_PURPOSE (copy
) = parsed_arg
;
17479 /* If the token stream has not been completely used up, then
17480 there was extra junk after the end of the default
17482 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
17483 cp_parser_error (parser
, "expected %<,%>");
17485 /* Revert to the main lexer. */
17486 cp_parser_pop_lexer (parser
);
17489 /* Make sure no default arg is missing. */
17490 check_default_args (fn
);
17492 /* Restore the state of local_variables_forbidden_p. */
17493 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
17495 /* Restore the queue. */
17496 parser
->unparsed_functions_queues
17497 = TREE_CHAIN (parser
->unparsed_functions_queues
);
17500 /* Parse the operand of `sizeof' (or a similar operator). Returns
17501 either a TYPE or an expression, depending on the form of the
17502 input. The KEYWORD indicates which kind of expression we have
17506 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
17508 static const char *format
;
17509 tree expr
= NULL_TREE
;
17510 const char *saved_message
;
17512 bool saved_integral_constant_expression_p
;
17513 bool saved_non_integral_constant_expression_p
;
17514 bool pack_expansion_p
= false;
17516 /* Initialize FORMAT the first time we get here. */
17518 format
= "types may not be defined in '%s' expressions";
17520 /* Types cannot be defined in a `sizeof' expression. Save away the
17522 saved_message
= parser
->type_definition_forbidden_message
;
17523 /* And create the new one. */
17524 parser
->type_definition_forbidden_message
= tmp
17525 = XNEWVEC (char, strlen (format
)
17526 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
17528 sprintf (tmp
, format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
17530 /* The restrictions on constant-expressions do not apply inside
17531 sizeof expressions. */
17532 saved_integral_constant_expression_p
17533 = parser
->integral_constant_expression_p
;
17534 saved_non_integral_constant_expression_p
17535 = parser
->non_integral_constant_expression_p
;
17536 parser
->integral_constant_expression_p
= false;
17538 /* If it's a `...', then we are computing the length of a parameter
17540 if (keyword
== RID_SIZEOF
17541 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17543 /* Consume the `...'. */
17544 cp_lexer_consume_token (parser
->lexer
);
17545 maybe_warn_variadic_templates ();
17547 /* Note that this is an expansion. */
17548 pack_expansion_p
= true;
17551 /* Do not actually evaluate the expression. */
17553 /* If it's a `(', then we might be looking at the type-id
17555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17558 bool saved_in_type_id_in_expr_p
;
17560 /* We can't be sure yet whether we're looking at a type-id or an
17562 cp_parser_parse_tentatively (parser
);
17563 /* Consume the `('. */
17564 cp_lexer_consume_token (parser
->lexer
);
17565 /* Parse the type-id. */
17566 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17567 parser
->in_type_id_in_expr_p
= true;
17568 type
= cp_parser_type_id (parser
);
17569 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17570 /* Now, look for the trailing `)'. */
17571 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17572 /* If all went well, then we're done. */
17573 if (cp_parser_parse_definitely (parser
))
17575 cp_decl_specifier_seq decl_specs
;
17577 /* Build a trivial decl-specifier-seq. */
17578 clear_decl_specs (&decl_specs
);
17579 decl_specs
.type
= type
;
17581 /* Call grokdeclarator to figure out what type this is. */
17582 expr
= grokdeclarator (NULL
,
17586 /*attrlist=*/NULL
);
17590 /* If the type-id production did not work out, then we must be
17591 looking at the unary-expression production. */
17593 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
17596 if (pack_expansion_p
)
17597 /* Build a pack expansion. */
17598 expr
= make_pack_expansion (expr
);
17600 /* Go back to evaluating expressions. */
17603 /* Free the message we created. */
17605 /* And restore the old one. */
17606 parser
->type_definition_forbidden_message
= saved_message
;
17607 parser
->integral_constant_expression_p
17608 = saved_integral_constant_expression_p
;
17609 parser
->non_integral_constant_expression_p
17610 = saved_non_integral_constant_expression_p
;
17615 /* If the current declaration has no declarator, return true. */
17618 cp_parser_declares_only_class_p (cp_parser
*parser
)
17620 /* If the next token is a `;' or a `,' then there is no
17622 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17623 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
17626 /* Update the DECL_SPECS to reflect the storage class indicated by
17630 cp_parser_set_storage_class (cp_parser
*parser
,
17631 cp_decl_specifier_seq
*decl_specs
,
17634 cp_storage_class storage_class
;
17636 if (parser
->in_unbraced_linkage_specification_p
)
17638 error ("invalid use of %qD in linkage specification",
17639 ridpointers
[keyword
]);
17642 else if (decl_specs
->storage_class
!= sc_none
)
17644 decl_specs
->conflicting_specifiers_p
= true;
17648 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
17649 && decl_specs
->specs
[(int) ds_thread
])
17651 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
17652 decl_specs
->specs
[(int) ds_thread
] = 0;
17658 storage_class
= sc_auto
;
17661 storage_class
= sc_register
;
17664 storage_class
= sc_static
;
17667 storage_class
= sc_extern
;
17670 storage_class
= sc_mutable
;
17673 gcc_unreachable ();
17675 decl_specs
->storage_class
= storage_class
;
17677 /* A storage class specifier cannot be applied alongside a typedef
17678 specifier. If there is a typedef specifier present then set
17679 conflicting_specifiers_p which will trigger an error later
17680 on in grokdeclarator. */
17681 if (decl_specs
->specs
[(int)ds_typedef
])
17682 decl_specs
->conflicting_specifiers_p
= true;
17685 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17686 is true, the type is a user-defined type; otherwise it is a
17687 built-in type specified by a keyword. */
17690 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
17692 bool user_defined_p
)
17694 decl_specs
->any_specifiers_p
= true;
17696 /* If the user tries to redeclare bool or wchar_t (with, for
17697 example, in "typedef int wchar_t;") we remember that this is what
17698 happened. In system headers, we ignore these declarations so
17699 that G++ can work with system headers that are not C++-safe. */
17700 if (decl_specs
->specs
[(int) ds_typedef
]
17702 && (type_spec
== boolean_type_node
17703 || type_spec
== wchar_type_node
)
17704 && (decl_specs
->type
17705 || decl_specs
->specs
[(int) ds_long
]
17706 || decl_specs
->specs
[(int) ds_short
]
17707 || decl_specs
->specs
[(int) ds_unsigned
]
17708 || decl_specs
->specs
[(int) ds_signed
]))
17710 decl_specs
->redefined_builtin_type
= type_spec
;
17711 if (!decl_specs
->type
)
17713 decl_specs
->type
= type_spec
;
17714 decl_specs
->user_defined_type_p
= false;
17717 else if (decl_specs
->type
)
17718 decl_specs
->multiple_types_p
= true;
17721 decl_specs
->type
= type_spec
;
17722 decl_specs
->user_defined_type_p
= user_defined_p
;
17723 decl_specs
->redefined_builtin_type
= NULL_TREE
;
17727 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17728 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17731 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
17733 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
17736 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17737 issue an error message indicating that TOKEN_DESC was expected.
17739 Returns the token consumed, if the token had the appropriate type.
17740 Otherwise, returns NULL. */
17743 cp_parser_require (cp_parser
* parser
,
17744 enum cpp_ttype type
,
17745 const char* token_desc
)
17747 if (cp_lexer_next_token_is (parser
->lexer
, type
))
17748 return cp_lexer_consume_token (parser
->lexer
);
17751 /* Output the MESSAGE -- unless we're parsing tentatively. */
17752 if (!cp_parser_simulate_error (parser
))
17754 char *message
= concat ("expected ", token_desc
, NULL
);
17755 cp_parser_error (parser
, message
);
17762 /* An error message is produced if the next token is not '>'.
17763 All further tokens are skipped until the desired token is
17764 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17767 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
17769 /* Current level of '< ... >'. */
17770 unsigned level
= 0;
17771 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17772 unsigned nesting_depth
= 0;
17774 /* Are we ready, yet? If not, issue error message. */
17775 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
17778 /* Skip tokens until the desired token is found. */
17781 /* Peek at the next token. */
17782 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
17785 if (!nesting_depth
)
17790 if (cxx_dialect
== cxx98
)
17791 /* C++0x views the `>>' operator as two `>' tokens, but
17794 else if (!nesting_depth
&& level
-- == 0)
17796 /* We've hit a `>>' where the first `>' closes the
17797 template argument list, and the second `>' is
17798 spurious. Just consume the `>>' and stop; we've
17799 already produced at least one error. */
17800 cp_lexer_consume_token (parser
->lexer
);
17803 /* Fall through for C++0x, so we handle the second `>' in
17807 if (!nesting_depth
&& level
-- == 0)
17809 /* We've reached the token we want, consume it and stop. */
17810 cp_lexer_consume_token (parser
->lexer
);
17815 case CPP_OPEN_PAREN
:
17816 case CPP_OPEN_SQUARE
:
17820 case CPP_CLOSE_PAREN
:
17821 case CPP_CLOSE_SQUARE
:
17822 if (nesting_depth
-- == 0)
17827 case CPP_PRAGMA_EOL
:
17828 case CPP_SEMICOLON
:
17829 case CPP_OPEN_BRACE
:
17830 case CPP_CLOSE_BRACE
:
17831 /* The '>' was probably forgotten, don't look further. */
17838 /* Consume this token. */
17839 cp_lexer_consume_token (parser
->lexer
);
17843 /* If the next token is the indicated keyword, consume it. Otherwise,
17844 issue an error message indicating that TOKEN_DESC was expected.
17846 Returns the token consumed, if the token had the appropriate type.
17847 Otherwise, returns NULL. */
17850 cp_parser_require_keyword (cp_parser
* parser
,
17852 const char* token_desc
)
17854 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
17856 if (token
&& token
->keyword
!= keyword
)
17858 dyn_string_t error_msg
;
17860 /* Format the error message. */
17861 error_msg
= dyn_string_new (0);
17862 dyn_string_append_cstr (error_msg
, "expected ");
17863 dyn_string_append_cstr (error_msg
, token_desc
);
17864 cp_parser_error (parser
, error_msg
->s
);
17865 dyn_string_delete (error_msg
);
17872 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17873 function-definition. */
17876 cp_parser_token_starts_function_definition_p (cp_token
* token
)
17878 return (/* An ordinary function-body begins with an `{'. */
17879 token
->type
== CPP_OPEN_BRACE
17880 /* A ctor-initializer begins with a `:'. */
17881 || token
->type
== CPP_COLON
17882 /* A function-try-block begins with `try'. */
17883 || token
->keyword
== RID_TRY
17884 /* The named return value extension begins with `return'. */
17885 || token
->keyword
== RID_RETURN
);
17888 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17892 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
17896 token
= cp_lexer_peek_token (parser
->lexer
);
17897 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
17900 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17901 C++0x) ending a template-argument. */
17904 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
17908 token
= cp_lexer_peek_token (parser
->lexer
);
17909 return (token
->type
== CPP_COMMA
17910 || token
->type
== CPP_GREATER
17911 || token
->type
== CPP_ELLIPSIS
17912 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
17915 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17916 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17919 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
17924 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
17925 if (token
->type
== CPP_LESS
)
17927 /* Check for the sequence `<::' in the original code. It would be lexed as
17928 `[:', where `[' is a digraph, and there is no whitespace before
17930 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
17933 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
17934 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
17940 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17941 or none_type otherwise. */
17943 static enum tag_types
17944 cp_parser_token_is_class_key (cp_token
* token
)
17946 switch (token
->keyword
)
17951 return record_type
;
17960 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17963 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
17965 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
17966 pedwarn ("%qs tag used in naming %q#T",
17967 class_key
== union_type
? "union"
17968 : class_key
== record_type
? "struct" : "class",
17972 /* Issue an error message if DECL is redeclared with different
17973 access than its original declaration [class.access.spec/3].
17974 This applies to nested classes and nested class templates.
17978 cp_parser_check_access_in_redeclaration (tree decl
)
17980 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
17983 if ((TREE_PRIVATE (decl
)
17984 != (current_access_specifier
== access_private_node
))
17985 || (TREE_PROTECTED (decl
)
17986 != (current_access_specifier
== access_protected_node
)))
17987 error ("%qD redeclared with different access", decl
);
17990 /* Look for the `template' keyword, as a syntactic disambiguator.
17991 Return TRUE iff it is present, in which case it will be
17995 cp_parser_optional_template_keyword (cp_parser
*parser
)
17997 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17999 /* The `template' keyword can only be used within templates;
18000 outside templates the parser can always figure out what is a
18001 template and what is not. */
18002 if (!processing_template_decl
)
18004 error ("%<template%> (as a disambiguator) is only allowed "
18005 "within templates");
18006 /* If this part of the token stream is rescanned, the same
18007 error message would be generated. So, we purge the token
18008 from the stream. */
18009 cp_lexer_purge_token (parser
->lexer
);
18014 /* Consume the `template' keyword. */
18015 cp_lexer_consume_token (parser
->lexer
);
18023 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18024 set PARSER->SCOPE, and perform other related actions. */
18027 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
18030 struct tree_check
*check_value
;
18031 deferred_access_check
*chk
;
18032 VEC (deferred_access_check
,gc
) *checks
;
18034 /* Get the stored value. */
18035 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
18036 /* Perform any access checks that were deferred. */
18037 checks
= check_value
->checks
;
18041 VEC_iterate (deferred_access_check
, checks
, i
, chk
) ;
18044 perform_or_defer_access_check (chk
->binfo
,
18049 /* Set the scope from the stored value. */
18050 parser
->scope
= check_value
->value
;
18051 parser
->qualifying_scope
= check_value
->qualifying_scope
;
18052 parser
->object_scope
= NULL_TREE
;
18055 /* Consume tokens up through a non-nested END token. */
18058 cp_parser_cache_group (cp_parser
*parser
,
18059 enum cpp_ttype end
,
18066 /* Abort a parenthesized expression if we encounter a brace. */
18067 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
18068 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18070 /* If we've reached the end of the file, stop. */
18071 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
18072 || (end
!= CPP_PRAGMA_EOL
18073 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
18075 /* Consume the next token. */
18076 token
= cp_lexer_consume_token (parser
->lexer
);
18077 /* See if it starts a new group. */
18078 if (token
->type
== CPP_OPEN_BRACE
)
18080 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
18084 else if (token
->type
== CPP_OPEN_PAREN
)
18085 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
18086 else if (token
->type
== CPP_PRAGMA
)
18087 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
18088 else if (token
->type
== end
)
18093 /* Begin parsing tentatively. We always save tokens while parsing
18094 tentatively so that if the tentative parsing fails we can restore the
18098 cp_parser_parse_tentatively (cp_parser
* parser
)
18100 /* Enter a new parsing context. */
18101 parser
->context
= cp_parser_context_new (parser
->context
);
18102 /* Begin saving tokens. */
18103 cp_lexer_save_tokens (parser
->lexer
);
18104 /* In order to avoid repetitive access control error messages,
18105 access checks are queued up until we are no longer parsing
18107 push_deferring_access_checks (dk_deferred
);
18110 /* Commit to the currently active tentative parse. */
18113 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
18115 cp_parser_context
*context
;
18118 /* Mark all of the levels as committed. */
18119 lexer
= parser
->lexer
;
18120 for (context
= parser
->context
; context
->next
; context
= context
->next
)
18122 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
18124 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
18125 while (!cp_lexer_saving_tokens (lexer
))
18126 lexer
= lexer
->next
;
18127 cp_lexer_commit_tokens (lexer
);
18131 /* Abort the currently active tentative parse. All consumed tokens
18132 will be rolled back, and no diagnostics will be issued. */
18135 cp_parser_abort_tentative_parse (cp_parser
* parser
)
18137 cp_parser_simulate_error (parser
);
18138 /* Now, pretend that we want to see if the construct was
18139 successfully parsed. */
18140 cp_parser_parse_definitely (parser
);
18143 /* Stop parsing tentatively. If a parse error has occurred, restore the
18144 token stream. Otherwise, commit to the tokens we have consumed.
18145 Returns true if no error occurred; false otherwise. */
18148 cp_parser_parse_definitely (cp_parser
* parser
)
18150 bool error_occurred
;
18151 cp_parser_context
*context
;
18153 /* Remember whether or not an error occurred, since we are about to
18154 destroy that information. */
18155 error_occurred
= cp_parser_error_occurred (parser
);
18156 /* Remove the topmost context from the stack. */
18157 context
= parser
->context
;
18158 parser
->context
= context
->next
;
18159 /* If no parse errors occurred, commit to the tentative parse. */
18160 if (!error_occurred
)
18162 /* Commit to the tokens read tentatively, unless that was
18164 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
18165 cp_lexer_commit_tokens (parser
->lexer
);
18167 pop_to_parent_deferring_access_checks ();
18169 /* Otherwise, if errors occurred, roll back our state so that things
18170 are just as they were before we began the tentative parse. */
18173 cp_lexer_rollback_tokens (parser
->lexer
);
18174 pop_deferring_access_checks ();
18176 /* Add the context to the front of the free list. */
18177 context
->next
= cp_parser_context_free_list
;
18178 cp_parser_context_free_list
= context
;
18180 return !error_occurred
;
18183 /* Returns true if we are parsing tentatively and are not committed to
18184 this tentative parse. */
18187 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
18189 return (cp_parser_parsing_tentatively (parser
)
18190 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
18193 /* Returns nonzero iff an error has occurred during the most recent
18194 tentative parse. */
18197 cp_parser_error_occurred (cp_parser
* parser
)
18199 return (cp_parser_parsing_tentatively (parser
)
18200 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
18203 /* Returns nonzero if GNU extensions are allowed. */
18206 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
18208 return parser
->allow_gnu_extensions_p
;
18211 /* Objective-C++ Productions */
18214 /* Parse an Objective-C expression, which feeds into a primary-expression
18218 objc-message-expression
18219 objc-string-literal
18220 objc-encode-expression
18221 objc-protocol-expression
18222 objc-selector-expression
18224 Returns a tree representation of the expression. */
18227 cp_parser_objc_expression (cp_parser
* parser
)
18229 /* Try to figure out what kind of declaration is present. */
18230 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18234 case CPP_OPEN_SQUARE
:
18235 return cp_parser_objc_message_expression (parser
);
18237 case CPP_OBJC_STRING
:
18238 kwd
= cp_lexer_consume_token (parser
->lexer
);
18239 return objc_build_string_object (kwd
->u
.value
);
18242 switch (kwd
->keyword
)
18244 case RID_AT_ENCODE
:
18245 return cp_parser_objc_encode_expression (parser
);
18247 case RID_AT_PROTOCOL
:
18248 return cp_parser_objc_protocol_expression (parser
);
18250 case RID_AT_SELECTOR
:
18251 return cp_parser_objc_selector_expression (parser
);
18257 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18258 cp_parser_skip_to_end_of_block_or_statement (parser
);
18261 return error_mark_node
;
18264 /* Parse an Objective-C message expression.
18266 objc-message-expression:
18267 [ objc-message-receiver objc-message-args ]
18269 Returns a representation of an Objective-C message. */
18272 cp_parser_objc_message_expression (cp_parser
* parser
)
18274 tree receiver
, messageargs
;
18276 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
18277 receiver
= cp_parser_objc_message_receiver (parser
);
18278 messageargs
= cp_parser_objc_message_args (parser
);
18279 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
18281 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
18284 /* Parse an objc-message-receiver.
18286 objc-message-receiver:
18288 simple-type-specifier
18290 Returns a representation of the type or expression. */
18293 cp_parser_objc_message_receiver (cp_parser
* parser
)
18297 /* An Objective-C message receiver may be either (1) a type
18298 or (2) an expression. */
18299 cp_parser_parse_tentatively (parser
);
18300 rcv
= cp_parser_expression (parser
, false);
18302 if (cp_parser_parse_definitely (parser
))
18305 rcv
= cp_parser_simple_type_specifier (parser
,
18306 /*decl_specs=*/NULL
,
18307 CP_PARSER_FLAGS_NONE
);
18309 return objc_get_class_reference (rcv
);
18312 /* Parse the arguments and selectors comprising an Objective-C message.
18317 objc-selector-args , objc-comma-args
18319 objc-selector-args:
18320 objc-selector [opt] : assignment-expression
18321 objc-selector-args objc-selector [opt] : assignment-expression
18324 assignment-expression
18325 objc-comma-args , assignment-expression
18327 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18328 selector arguments and TREE_VALUE containing a list of comma
18332 cp_parser_objc_message_args (cp_parser
* parser
)
18334 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
18335 bool maybe_unary_selector_p
= true;
18336 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18338 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
18340 tree selector
= NULL_TREE
, arg
;
18342 if (token
->type
!= CPP_COLON
)
18343 selector
= cp_parser_objc_selector (parser
);
18345 /* Detect if we have a unary selector. */
18346 if (maybe_unary_selector_p
18347 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
18348 return build_tree_list (selector
, NULL_TREE
);
18350 maybe_unary_selector_p
= false;
18351 cp_parser_require (parser
, CPP_COLON
, "`:'");
18352 arg
= cp_parser_assignment_expression (parser
, false);
18355 = chainon (sel_args
,
18356 build_tree_list (selector
, arg
));
18358 token
= cp_lexer_peek_token (parser
->lexer
);
18361 /* Handle non-selector arguments, if any. */
18362 while (token
->type
== CPP_COMMA
)
18366 cp_lexer_consume_token (parser
->lexer
);
18367 arg
= cp_parser_assignment_expression (parser
, false);
18370 = chainon (addl_args
,
18371 build_tree_list (NULL_TREE
, arg
));
18373 token
= cp_lexer_peek_token (parser
->lexer
);
18376 return build_tree_list (sel_args
, addl_args
);
18379 /* Parse an Objective-C encode expression.
18381 objc-encode-expression:
18382 @encode objc-typename
18384 Returns an encoded representation of the type argument. */
18387 cp_parser_objc_encode_expression (cp_parser
* parser
)
18391 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
18392 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18393 type
= complete_type (cp_parser_type_id (parser
));
18394 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18398 error ("%<@encode%> must specify a type as an argument");
18399 return error_mark_node
;
18402 return objc_build_encode_expr (type
);
18405 /* Parse an Objective-C @defs expression. */
18408 cp_parser_objc_defs_expression (cp_parser
*parser
)
18412 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
18413 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18414 name
= cp_parser_identifier (parser
);
18415 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18417 return objc_get_class_ivars (name
);
18420 /* Parse an Objective-C protocol expression.
18422 objc-protocol-expression:
18423 @protocol ( identifier )
18425 Returns a representation of the protocol expression. */
18428 cp_parser_objc_protocol_expression (cp_parser
* parser
)
18432 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
18433 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18434 proto
= cp_parser_identifier (parser
);
18435 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18437 return objc_build_protocol_expr (proto
);
18440 /* Parse an Objective-C selector expression.
18442 objc-selector-expression:
18443 @selector ( objc-method-signature )
18445 objc-method-signature:
18451 objc-selector-seq objc-selector :
18453 Returns a representation of the method selector. */
18456 cp_parser_objc_selector_expression (cp_parser
* parser
)
18458 tree sel_seq
= NULL_TREE
;
18459 bool maybe_unary_selector_p
= true;
18462 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
18463 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18464 token
= cp_lexer_peek_token (parser
->lexer
);
18466 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
18467 || token
->type
== CPP_SCOPE
)
18469 tree selector
= NULL_TREE
;
18471 if (token
->type
!= CPP_COLON
18472 || token
->type
== CPP_SCOPE
)
18473 selector
= cp_parser_objc_selector (parser
);
18475 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
18476 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18478 /* Detect if we have a unary selector. */
18479 if (maybe_unary_selector_p
)
18481 sel_seq
= selector
;
18482 goto finish_selector
;
18486 cp_parser_error (parser
, "expected %<:%>");
18489 maybe_unary_selector_p
= false;
18490 token
= cp_lexer_consume_token (parser
->lexer
);
18492 if (token
->type
== CPP_SCOPE
)
18495 = chainon (sel_seq
,
18496 build_tree_list (selector
, NULL_TREE
));
18498 = chainon (sel_seq
,
18499 build_tree_list (NULL_TREE
, NULL_TREE
));
18503 = chainon (sel_seq
,
18504 build_tree_list (selector
, NULL_TREE
));
18506 token
= cp_lexer_peek_token (parser
->lexer
);
18510 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18512 return objc_build_selector_expr (sel_seq
);
18515 /* Parse a list of identifiers.
18517 objc-identifier-list:
18519 objc-identifier-list , identifier
18521 Returns a TREE_LIST of identifier nodes. */
18524 cp_parser_objc_identifier_list (cp_parser
* parser
)
18526 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
18527 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
18529 while (sep
->type
== CPP_COMMA
)
18531 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18532 list
= chainon (list
,
18533 build_tree_list (NULL_TREE
,
18534 cp_parser_identifier (parser
)));
18535 sep
= cp_lexer_peek_token (parser
->lexer
);
18541 /* Parse an Objective-C alias declaration.
18543 objc-alias-declaration:
18544 @compatibility_alias identifier identifier ;
18546 This function registers the alias mapping with the Objective-C front end.
18547 It returns nothing. */
18550 cp_parser_objc_alias_declaration (cp_parser
* parser
)
18554 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
18555 alias
= cp_parser_identifier (parser
);
18556 orig
= cp_parser_identifier (parser
);
18557 objc_declare_alias (alias
, orig
);
18558 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18561 /* Parse an Objective-C class forward-declaration.
18563 objc-class-declaration:
18564 @class objc-identifier-list ;
18566 The function registers the forward declarations with the Objective-C
18567 front end. It returns nothing. */
18570 cp_parser_objc_class_declaration (cp_parser
* parser
)
18572 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
18573 objc_declare_class (cp_parser_objc_identifier_list (parser
));
18574 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18577 /* Parse a list of Objective-C protocol references.
18579 objc-protocol-refs-opt:
18580 objc-protocol-refs [opt]
18582 objc-protocol-refs:
18583 < objc-identifier-list >
18585 Returns a TREE_LIST of identifiers, if any. */
18588 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
18590 tree protorefs
= NULL_TREE
;
18592 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
18594 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
18595 protorefs
= cp_parser_objc_identifier_list (parser
);
18596 cp_parser_require (parser
, CPP_GREATER
, "`>'");
18602 /* Parse a Objective-C visibility specification. */
18605 cp_parser_objc_visibility_spec (cp_parser
* parser
)
18607 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
18609 switch (vis
->keyword
)
18611 case RID_AT_PRIVATE
:
18612 objc_set_visibility (2);
18614 case RID_AT_PROTECTED
:
18615 objc_set_visibility (0);
18617 case RID_AT_PUBLIC
:
18618 objc_set_visibility (1);
18624 /* Eat '@private'/'@protected'/'@public'. */
18625 cp_lexer_consume_token (parser
->lexer
);
18628 /* Parse an Objective-C method type. */
18631 cp_parser_objc_method_type (cp_parser
* parser
)
18633 objc_set_method_type
18634 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
18639 /* Parse an Objective-C protocol qualifier. */
18642 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
18644 tree quals
= NULL_TREE
, node
;
18645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18647 node
= token
->u
.value
;
18649 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
18650 && (node
== ridpointers
[(int) RID_IN
]
18651 || node
== ridpointers
[(int) RID_OUT
]
18652 || node
== ridpointers
[(int) RID_INOUT
]
18653 || node
== ridpointers
[(int) RID_BYCOPY
]
18654 || node
== ridpointers
[(int) RID_BYREF
]
18655 || node
== ridpointers
[(int) RID_ONEWAY
]))
18657 quals
= tree_cons (NULL_TREE
, node
, quals
);
18658 cp_lexer_consume_token (parser
->lexer
);
18659 token
= cp_lexer_peek_token (parser
->lexer
);
18660 node
= token
->u
.value
;
18666 /* Parse an Objective-C typename. */
18669 cp_parser_objc_typename (cp_parser
* parser
)
18671 tree typename
= NULL_TREE
;
18673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18675 tree proto_quals
, cp_type
= NULL_TREE
;
18677 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
18678 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
18680 /* An ObjC type name may consist of just protocol qualifiers, in which
18681 case the type shall default to 'id'. */
18682 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18683 cp_type
= cp_parser_type_id (parser
);
18685 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18686 typename
= build_tree_list (proto_quals
, cp_type
);
18692 /* Check to see if TYPE refers to an Objective-C selector name. */
18695 cp_parser_objc_selector_p (enum cpp_ttype type
)
18697 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
18698 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
18699 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
18700 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
18701 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
18704 /* Parse an Objective-C selector. */
18707 cp_parser_objc_selector (cp_parser
* parser
)
18709 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
18711 if (!cp_parser_objc_selector_p (token
->type
))
18713 error ("invalid Objective-C++ selector name");
18714 return error_mark_node
;
18717 /* C++ operator names are allowed to appear in ObjC selectors. */
18718 switch (token
->type
)
18720 case CPP_AND_AND
: return get_identifier ("and");
18721 case CPP_AND_EQ
: return get_identifier ("and_eq");
18722 case CPP_AND
: return get_identifier ("bitand");
18723 case CPP_OR
: return get_identifier ("bitor");
18724 case CPP_COMPL
: return get_identifier ("compl");
18725 case CPP_NOT
: return get_identifier ("not");
18726 case CPP_NOT_EQ
: return get_identifier ("not_eq");
18727 case CPP_OR_OR
: return get_identifier ("or");
18728 case CPP_OR_EQ
: return get_identifier ("or_eq");
18729 case CPP_XOR
: return get_identifier ("xor");
18730 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
18731 default: return token
->u
.value
;
18735 /* Parse an Objective-C params list. */
18738 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
18740 tree params
= NULL_TREE
;
18741 bool maybe_unary_selector_p
= true;
18742 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18744 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
18746 tree selector
= NULL_TREE
, typename
, identifier
;
18748 if (token
->type
!= CPP_COLON
)
18749 selector
= cp_parser_objc_selector (parser
);
18751 /* Detect if we have a unary selector. */
18752 if (maybe_unary_selector_p
18753 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
18756 maybe_unary_selector_p
= false;
18757 cp_parser_require (parser
, CPP_COLON
, "`:'");
18758 typename
= cp_parser_objc_typename (parser
);
18759 identifier
= cp_parser_identifier (parser
);
18763 objc_build_keyword_decl (selector
,
18767 token
= cp_lexer_peek_token (parser
->lexer
);
18773 /* Parse the non-keyword Objective-C params. */
18776 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
18778 tree params
= make_node (TREE_LIST
);
18779 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18780 *ellipsisp
= false; /* Initially, assume no ellipsis. */
18782 while (token
->type
== CPP_COMMA
)
18784 cp_parameter_declarator
*parmdecl
;
18787 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18788 token
= cp_lexer_peek_token (parser
->lexer
);
18790 if (token
->type
== CPP_ELLIPSIS
)
18792 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
18797 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18798 parm
= grokdeclarator (parmdecl
->declarator
,
18799 &parmdecl
->decl_specifiers
,
18800 PARM
, /*initialized=*/0,
18801 /*attrlist=*/NULL
);
18803 chainon (params
, build_tree_list (NULL_TREE
, parm
));
18804 token
= cp_lexer_peek_token (parser
->lexer
);
18810 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18813 cp_parser_objc_interstitial_code (cp_parser
* parser
)
18815 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18817 /* If the next token is `extern' and the following token is a string
18818 literal, then we have a linkage specification. */
18819 if (token
->keyword
== RID_EXTERN
18820 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
18821 cp_parser_linkage_specification (parser
);
18822 /* Handle #pragma, if any. */
18823 else if (token
->type
== CPP_PRAGMA
)
18824 cp_parser_pragma (parser
, pragma_external
);
18825 /* Allow stray semicolons. */
18826 else if (token
->type
== CPP_SEMICOLON
)
18827 cp_lexer_consume_token (parser
->lexer
);
18828 /* Finally, try to parse a block-declaration, or a function-definition. */
18830 cp_parser_block_declaration (parser
, /*statement_p=*/false);
18833 /* Parse a method signature. */
18836 cp_parser_objc_method_signature (cp_parser
* parser
)
18838 tree rettype
, kwdparms
, optparms
;
18839 bool ellipsis
= false;
18841 cp_parser_objc_method_type (parser
);
18842 rettype
= cp_parser_objc_typename (parser
);
18843 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
18844 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
18846 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
18849 /* Pars an Objective-C method prototype list. */
18852 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
18854 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18856 while (token
->keyword
!= RID_AT_END
)
18858 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18860 objc_add_method_declaration
18861 (cp_parser_objc_method_signature (parser
));
18862 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18865 /* Allow for interspersed non-ObjC++ code. */
18866 cp_parser_objc_interstitial_code (parser
);
18868 token
= cp_lexer_peek_token (parser
->lexer
);
18871 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18872 objc_finish_interface ();
18875 /* Parse an Objective-C method definition list. */
18878 cp_parser_objc_method_definition_list (cp_parser
* parser
)
18880 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18882 while (token
->keyword
!= RID_AT_END
)
18886 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18888 push_deferring_access_checks (dk_deferred
);
18889 objc_start_method_definition
18890 (cp_parser_objc_method_signature (parser
));
18892 /* For historical reasons, we accept an optional semicolon. */
18893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18894 cp_lexer_consume_token (parser
->lexer
);
18896 perform_deferred_access_checks ();
18897 stop_deferring_access_checks ();
18898 meth
= cp_parser_function_definition_after_declarator (parser
,
18900 pop_deferring_access_checks ();
18901 objc_finish_method_definition (meth
);
18904 /* Allow for interspersed non-ObjC++ code. */
18905 cp_parser_objc_interstitial_code (parser
);
18907 token
= cp_lexer_peek_token (parser
->lexer
);
18910 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18911 objc_finish_implementation ();
18914 /* Parse Objective-C ivars. */
18917 cp_parser_objc_class_ivars (cp_parser
* parser
)
18919 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18921 if (token
->type
!= CPP_OPEN_BRACE
)
18922 return; /* No ivars specified. */
18924 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
18925 token
= cp_lexer_peek_token (parser
->lexer
);
18927 while (token
->type
!= CPP_CLOSE_BRACE
)
18929 cp_decl_specifier_seq declspecs
;
18930 int decl_class_or_enum_p
;
18931 tree prefix_attributes
;
18933 cp_parser_objc_visibility_spec (parser
);
18935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18938 cp_parser_decl_specifier_seq (parser
,
18939 CP_PARSER_FLAGS_OPTIONAL
,
18941 &decl_class_or_enum_p
);
18942 prefix_attributes
= declspecs
.attributes
;
18943 declspecs
.attributes
= NULL_TREE
;
18945 /* Keep going until we hit the `;' at the end of the
18947 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18949 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
18950 cp_declarator
*declarator
= NULL
;
18951 int ctor_dtor_or_conv_p
;
18953 /* Check for a (possibly unnamed) bitfield declaration. */
18954 token
= cp_lexer_peek_token (parser
->lexer
);
18955 if (token
->type
== CPP_COLON
)
18958 if (token
->type
== CPP_NAME
18959 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18962 /* Get the name of the bitfield. */
18963 declarator
= make_id_declarator (NULL_TREE
,
18964 cp_parser_identifier (parser
),
18968 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
18969 /* Get the width of the bitfield. */
18971 = cp_parser_constant_expression (parser
,
18972 /*allow_non_constant=*/false,
18977 /* Parse the declarator. */
18979 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
18980 &ctor_dtor_or_conv_p
,
18981 /*parenthesized_p=*/NULL
,
18982 /*member_p=*/false);
18985 /* Look for attributes that apply to the ivar. */
18986 attributes
= cp_parser_attributes_opt (parser
);
18987 /* Remember which attributes are prefix attributes and
18989 first_attribute
= attributes
;
18990 /* Combine the attributes. */
18991 attributes
= chainon (prefix_attributes
, attributes
);
18995 /* Create the bitfield declaration. */
18996 decl
= grokbitfield (declarator
, &declspecs
, width
);
18997 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
19000 decl
= grokfield (declarator
, &declspecs
,
19001 NULL_TREE
, /*init_const_expr_p=*/false,
19002 NULL_TREE
, attributes
);
19004 /* Add the instance variable. */
19005 objc_add_instance_variable (decl
);
19007 /* Reset PREFIX_ATTRIBUTES. */
19008 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
19009 attributes
= TREE_CHAIN (attributes
);
19011 TREE_CHAIN (attributes
) = NULL_TREE
;
19013 token
= cp_lexer_peek_token (parser
->lexer
);
19015 if (token
->type
== CPP_COMMA
)
19017 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
19023 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19024 token
= cp_lexer_peek_token (parser
->lexer
);
19027 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
19028 /* For historical reasons, we accept an optional semicolon. */
19029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19030 cp_lexer_consume_token (parser
->lexer
);
19033 /* Parse an Objective-C protocol declaration. */
19036 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
19038 tree proto
, protorefs
;
19041 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
19042 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
19044 error ("identifier expected after %<@protocol%>");
19048 /* See if we have a forward declaration or a definition. */
19049 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19051 /* Try a forward declaration first. */
19052 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
19054 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
19056 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19059 /* Ok, we got a full-fledged definition (or at least should). */
19062 proto
= cp_parser_identifier (parser
);
19063 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
19064 objc_start_protocol (proto
, protorefs
);
19065 cp_parser_objc_method_prototype_list (parser
);
19069 /* Parse an Objective-C superclass or category. */
19072 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
19075 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
19077 *super
= *categ
= NULL_TREE
;
19078 if (next
->type
== CPP_COLON
)
19080 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
19081 *super
= cp_parser_identifier (parser
);
19083 else if (next
->type
== CPP_OPEN_PAREN
)
19085 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
19086 *categ
= cp_parser_identifier (parser
);
19087 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
19091 /* Parse an Objective-C class interface. */
19094 cp_parser_objc_class_interface (cp_parser
* parser
)
19096 tree name
, super
, categ
, protos
;
19098 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
19099 name
= cp_parser_identifier (parser
);
19100 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
19101 protos
= cp_parser_objc_protocol_refs_opt (parser
);
19103 /* We have either a class or a category on our hands. */
19105 objc_start_category_interface (name
, categ
, protos
);
19108 objc_start_class_interface (name
, super
, protos
);
19109 /* Handle instance variable declarations, if any. */
19110 cp_parser_objc_class_ivars (parser
);
19111 objc_continue_interface ();
19114 cp_parser_objc_method_prototype_list (parser
);
19117 /* Parse an Objective-C class implementation. */
19120 cp_parser_objc_class_implementation (cp_parser
* parser
)
19122 tree name
, super
, categ
;
19124 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
19125 name
= cp_parser_identifier (parser
);
19126 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
19128 /* We have either a class or a category on our hands. */
19130 objc_start_category_implementation (name
, categ
);
19133 objc_start_class_implementation (name
, super
);
19134 /* Handle instance variable declarations, if any. */
19135 cp_parser_objc_class_ivars (parser
);
19136 objc_continue_implementation ();
19139 cp_parser_objc_method_definition_list (parser
);
19142 /* Consume the @end token and finish off the implementation. */
19145 cp_parser_objc_end_implementation (cp_parser
* parser
)
19147 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
19148 objc_finish_implementation ();
19151 /* Parse an Objective-C declaration. */
19154 cp_parser_objc_declaration (cp_parser
* parser
)
19156 /* Try to figure out what kind of declaration is present. */
19157 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
19159 switch (kwd
->keyword
)
19162 cp_parser_objc_alias_declaration (parser
);
19165 cp_parser_objc_class_declaration (parser
);
19167 case RID_AT_PROTOCOL
:
19168 cp_parser_objc_protocol_declaration (parser
);
19170 case RID_AT_INTERFACE
:
19171 cp_parser_objc_class_interface (parser
);
19173 case RID_AT_IMPLEMENTATION
:
19174 cp_parser_objc_class_implementation (parser
);
19177 cp_parser_objc_end_implementation (parser
);
19180 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
19181 cp_parser_skip_to_end_of_block_or_statement (parser
);
19185 /* Parse an Objective-C try-catch-finally statement.
19187 objc-try-catch-finally-stmt:
19188 @try compound-statement objc-catch-clause-seq [opt]
19189 objc-finally-clause [opt]
19191 objc-catch-clause-seq:
19192 objc-catch-clause objc-catch-clause-seq [opt]
19195 @catch ( exception-declaration ) compound-statement
19197 objc-finally-clause
19198 @finally compound-statement
19200 Returns NULL_TREE. */
19203 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
19204 location_t location
;
19207 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
19208 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19209 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19210 node, lest it get absorbed into the surrounding block. */
19211 stmt
= push_stmt_list ();
19212 cp_parser_compound_statement (parser
, NULL
, false);
19213 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
19215 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
19217 cp_parameter_declarator
*parmdecl
;
19220 cp_lexer_consume_token (parser
->lexer
);
19221 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
19222 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
19223 parm
= grokdeclarator (parmdecl
->declarator
,
19224 &parmdecl
->decl_specifiers
,
19225 PARM
, /*initialized=*/0,
19226 /*attrlist=*/NULL
);
19227 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
19228 objc_begin_catch_clause (parm
);
19229 cp_parser_compound_statement (parser
, NULL
, false);
19230 objc_finish_catch_clause ();
19233 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
19235 cp_lexer_consume_token (parser
->lexer
);
19236 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19237 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19238 node, lest it get absorbed into the surrounding block. */
19239 stmt
= push_stmt_list ();
19240 cp_parser_compound_statement (parser
, NULL
, false);
19241 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
19244 return objc_finish_try_stmt ();
19247 /* Parse an Objective-C synchronized statement.
19249 objc-synchronized-stmt:
19250 @synchronized ( expression ) compound-statement
19252 Returns NULL_TREE. */
19255 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
19256 location_t location
;
19259 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
19261 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19262 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
19263 lock
= cp_parser_expression (parser
, false);
19264 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
19266 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19267 node, lest it get absorbed into the surrounding block. */
19268 stmt
= push_stmt_list ();
19269 cp_parser_compound_statement (parser
, NULL
, false);
19271 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
19274 /* Parse an Objective-C throw statement.
19277 @throw assignment-expression [opt] ;
19279 Returns a constructed '@throw' statement. */
19282 cp_parser_objc_throw_statement (cp_parser
*parser
) {
19283 tree expr
= NULL_TREE
;
19285 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
19287 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19288 expr
= cp_parser_assignment_expression (parser
, false);
19290 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19292 return objc_build_throw_stmt (expr
);
19295 /* Parse an Objective-C statement. */
19298 cp_parser_objc_statement (cp_parser
* parser
) {
19299 /* Try to figure out what kind of declaration is present. */
19300 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
19302 switch (kwd
->keyword
)
19305 return cp_parser_objc_try_catch_finally_statement (parser
);
19306 case RID_AT_SYNCHRONIZED
:
19307 return cp_parser_objc_synchronized_statement (parser
);
19309 return cp_parser_objc_throw_statement (parser
);
19311 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
19312 cp_parser_skip_to_end_of_block_or_statement (parser
);
19315 return error_mark_node
;
19318 /* OpenMP 2.5 parsing routines. */
19320 /* Returns name of the next clause.
19321 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19322 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19323 returned and the token is consumed. */
19325 static pragma_omp_clause
19326 cp_parser_omp_clause_name (cp_parser
*parser
)
19328 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
19330 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
19331 result
= PRAGMA_OMP_CLAUSE_IF
;
19332 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
19333 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
19334 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
19335 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
19336 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19338 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19339 const char *p
= IDENTIFIER_POINTER (id
);
19344 if (!strcmp ("copyin", p
))
19345 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
19346 else if (!strcmp ("copyprivate", p
))
19347 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
19350 if (!strcmp ("firstprivate", p
))
19351 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
19354 if (!strcmp ("lastprivate", p
))
19355 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
19358 if (!strcmp ("nowait", p
))
19359 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
19360 else if (!strcmp ("num_threads", p
))
19361 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
19364 if (!strcmp ("ordered", p
))
19365 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
19368 if (!strcmp ("reduction", p
))
19369 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
19372 if (!strcmp ("schedule", p
))
19373 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
19374 else if (!strcmp ("shared", p
))
19375 result
= PRAGMA_OMP_CLAUSE_SHARED
;
19380 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
19381 cp_lexer_consume_token (parser
->lexer
);
19386 /* Validate that a clause of the given type does not already exist. */
19389 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
19393 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
19394 if (OMP_CLAUSE_CODE (c
) == code
)
19396 error ("too many %qs clauses", name
);
19404 variable-list , identifier
19406 In addition, we match a closing parenthesis. An opening parenthesis
19407 will have been consumed by the caller.
19409 If KIND is nonzero, create the appropriate node and install the decl
19410 in OMP_CLAUSE_DECL and add the node to the head of the list.
19412 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19413 return the list created. */
19416 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
19423 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
19424 /*check_dependency_p=*/true,
19425 /*template_p=*/NULL
,
19426 /*declarator_p=*/false,
19427 /*optional_p=*/false);
19428 if (name
== error_mark_node
)
19431 decl
= cp_parser_lookup_name_simple (parser
, name
);
19432 if (decl
== error_mark_node
)
19433 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
19434 else if (kind
!= 0)
19436 tree u
= build_omp_clause (kind
);
19437 OMP_CLAUSE_DECL (u
) = decl
;
19438 OMP_CLAUSE_CHAIN (u
) = list
;
19442 list
= tree_cons (decl
, NULL_TREE
, list
);
19445 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19447 cp_lexer_consume_token (parser
->lexer
);
19450 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19454 /* Try to resync to an unnested comma. Copied from
19455 cp_parser_parenthesized_expression_list. */
19457 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
19458 /*recovering=*/true,
19460 /*consume_paren=*/true);
19468 /* Similarly, but expect leading and trailing parenthesis. This is a very
19469 common case for omp clauses. */
19472 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
19474 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19475 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
19480 default ( shared | none ) */
19483 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
19485 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
19488 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19492 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19493 const char *p
= IDENTIFIER_POINTER (id
);
19498 if (strcmp ("none", p
) != 0)
19500 kind
= OMP_CLAUSE_DEFAULT_NONE
;
19504 if (strcmp ("shared", p
) != 0)
19506 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
19513 cp_lexer_consume_token (parser
->lexer
);
19518 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
19521 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19522 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19523 /*or_comma=*/false,
19524 /*consume_paren=*/true);
19526 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
19529 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
19530 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
19531 OMP_CLAUSE_CHAIN (c
) = list
;
19532 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
19538 if ( expression ) */
19541 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
19545 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19548 t
= cp_parser_condition (parser
);
19550 if (t
== error_mark_node
19551 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19552 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19553 /*or_comma=*/false,
19554 /*consume_paren=*/true);
19556 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
19558 c
= build_omp_clause (OMP_CLAUSE_IF
);
19559 OMP_CLAUSE_IF_EXPR (c
) = t
;
19560 OMP_CLAUSE_CHAIN (c
) = list
;
19569 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
19573 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
19575 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
19576 OMP_CLAUSE_CHAIN (c
) = list
;
19581 num_threads ( expression ) */
19584 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
19588 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19591 t
= cp_parser_expression (parser
, false);
19593 if (t
== error_mark_node
19594 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19595 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19596 /*or_comma=*/false,
19597 /*consume_paren=*/true);
19599 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
19601 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
19602 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
19603 OMP_CLAUSE_CHAIN (c
) = list
;
19612 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
19616 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
19618 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
19619 OMP_CLAUSE_CHAIN (c
) = list
;
19624 reduction ( reduction-operator : variable-list )
19626 reduction-operator:
19627 One of: + * - & ^ | && || */
19630 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
19632 enum tree_code code
;
19635 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19638 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
19650 code
= BIT_AND_EXPR
;
19653 code
= BIT_XOR_EXPR
;
19656 code
= BIT_IOR_EXPR
;
19659 code
= TRUTH_ANDIF_EXPR
;
19662 code
= TRUTH_ORIF_EXPR
;
19665 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19667 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19668 /*or_comma=*/false,
19669 /*consume_paren=*/true);
19672 cp_lexer_consume_token (parser
->lexer
);
19674 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
19677 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
19678 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
19679 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
19685 schedule ( schedule-kind )
19686 schedule ( schedule-kind , expression )
19689 static | dynamic | guided | runtime */
19692 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
19696 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
19699 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
19701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19703 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19704 const char *p
= IDENTIFIER_POINTER (id
);
19709 if (strcmp ("dynamic", p
) != 0)
19711 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
19715 if (strcmp ("guided", p
) != 0)
19717 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
19721 if (strcmp ("runtime", p
) != 0)
19723 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
19730 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
19731 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
19734 cp_lexer_consume_token (parser
->lexer
);
19736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19738 cp_lexer_consume_token (parser
->lexer
);
19740 t
= cp_parser_assignment_expression (parser
, false);
19742 if (t
== error_mark_node
)
19744 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
19745 error ("schedule %<runtime%> does not take "
19746 "a %<chunk_size%> parameter");
19748 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
19750 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19753 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
19756 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
19757 OMP_CLAUSE_CHAIN (c
) = list
;
19761 cp_parser_error (parser
, "invalid schedule kind");
19763 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19764 /*or_comma=*/false,
19765 /*consume_paren=*/true);
19769 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19770 is a bitmask in MASK. Return the list of clauses found; the result
19771 of clause default goes in *pdefault. */
19774 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
19775 const char *where
, cp_token
*pragma_tok
)
19777 tree clauses
= NULL
;
19780 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
19782 pragma_omp_clause c_kind
;
19783 const char *c_name
;
19784 tree prev
= clauses
;
19786 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19787 cp_lexer_consume_token (parser
->lexer
);
19789 c_kind
= cp_parser_omp_clause_name (parser
);
19794 case PRAGMA_OMP_CLAUSE_COPYIN
:
19795 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
19798 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
19799 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
19801 c_name
= "copyprivate";
19803 case PRAGMA_OMP_CLAUSE_DEFAULT
:
19804 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
19805 c_name
= "default";
19807 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
19808 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
19810 c_name
= "firstprivate";
19812 case PRAGMA_OMP_CLAUSE_IF
:
19813 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
19816 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
19817 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
19819 c_name
= "lastprivate";
19821 case PRAGMA_OMP_CLAUSE_NOWAIT
:
19822 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
19825 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
19826 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
19827 c_name
= "num_threads";
19829 case PRAGMA_OMP_CLAUSE_ORDERED
:
19830 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
19831 c_name
= "ordered";
19833 case PRAGMA_OMP_CLAUSE_PRIVATE
:
19834 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
19836 c_name
= "private";
19838 case PRAGMA_OMP_CLAUSE_REDUCTION
:
19839 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
19840 c_name
= "reduction";
19842 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
19843 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
19844 c_name
= "schedule";
19846 case PRAGMA_OMP_CLAUSE_SHARED
:
19847 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
19852 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
19856 if (((mask
>> c_kind
) & 1) == 0)
19858 /* Remove the invalid clause(s) from the list to avoid
19859 confusing the rest of the compiler. */
19861 error ("%qs is not valid for %qs", c_name
, where
);
19865 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19866 return finish_omp_clauses (clauses
);
19873 In practice, we're also interested in adding the statement to an
19874 outer node. So it is convenient if we work around the fact that
19875 cp_parser_statement calls add_stmt. */
19878 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
19880 unsigned save
= parser
->in_statement
;
19882 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19883 This preserves the "not within loop or switch" style error messages
19884 for nonsense cases like
19890 if (parser
->in_statement
)
19891 parser
->in_statement
= IN_OMP_BLOCK
;
19897 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
19899 parser
->in_statement
= save
;
19903 cp_parser_omp_structured_block (cp_parser
*parser
)
19905 tree stmt
= begin_omp_structured_block ();
19906 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
19908 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19910 cp_parser_end_omp_structured_block (parser
, save
);
19911 return finish_omp_structured_block (stmt
);
19915 # pragma omp atomic new-line
19919 x binop= expr | x++ | ++x | x-- | --x
19921 +, *, -, /, &, ^, |, <<, >>
19923 where x is an lvalue expression with scalar type. */
19926 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
19929 enum tree_code code
;
19931 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19933 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
19935 switch (TREE_CODE (lhs
))
19940 case PREINCREMENT_EXPR
:
19941 case POSTINCREMENT_EXPR
:
19942 lhs
= TREE_OPERAND (lhs
, 0);
19944 rhs
= integer_one_node
;
19947 case PREDECREMENT_EXPR
:
19948 case POSTDECREMENT_EXPR
:
19949 lhs
= TREE_OPERAND (lhs
, 0);
19951 rhs
= integer_one_node
;
19955 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
19961 code
= TRUNC_DIV_EXPR
;
19969 case CPP_LSHIFT_EQ
:
19970 code
= LSHIFT_EXPR
;
19972 case CPP_RSHIFT_EQ
:
19973 code
= RSHIFT_EXPR
;
19976 code
= BIT_AND_EXPR
;
19979 code
= BIT_IOR_EXPR
;
19982 code
= BIT_XOR_EXPR
;
19985 cp_parser_error (parser
,
19986 "invalid operator for %<#pragma omp atomic%>");
19989 cp_lexer_consume_token (parser
->lexer
);
19991 rhs
= cp_parser_expression (parser
, false);
19992 if (rhs
== error_mark_node
)
19996 finish_omp_atomic (code
, lhs
, rhs
);
19997 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20001 cp_parser_skip_to_end_of_block_or_statement (parser
);
20006 # pragma omp barrier new-line */
20009 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
20011 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20012 finish_omp_barrier ();
20016 # pragma omp critical [(name)] new-line
20017 structured-block */
20020 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
20022 tree stmt
, name
= NULL
;
20024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20026 cp_lexer_consume_token (parser
->lexer
);
20028 name
= cp_parser_identifier (parser
);
20030 if (name
== error_mark_node
20031 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
20032 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20033 /*or_comma=*/false,
20034 /*consume_paren=*/true);
20035 if (name
== error_mark_node
)
20038 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20040 stmt
= cp_parser_omp_structured_block (parser
);
20041 return c_finish_omp_critical (stmt
, name
);
20045 # pragma omp flush flush-vars[opt] new-line
20048 ( variable-list ) */
20051 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
20053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20054 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
20055 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20057 finish_omp_flush ();
20060 /* Parse the restricted form of the for statment allowed by OpenMP. */
20063 cp_parser_omp_for_loop (cp_parser
*parser
)
20065 tree init
, cond
, incr
, body
, decl
, pre_body
;
20068 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
20070 cp_parser_error (parser
, "for statement expected");
20073 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
20074 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
20077 init
= decl
= NULL
;
20078 pre_body
= push_stmt_list ();
20079 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20081 cp_decl_specifier_seq type_specifiers
;
20083 /* First, try to parse as an initialized declaration. See
20084 cp_parser_condition, from whence the bulk of this is copied. */
20086 cp_parser_parse_tentatively (parser
);
20087 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
20089 if (!cp_parser_error_occurred (parser
))
20091 tree asm_specification
, attributes
;
20092 cp_declarator
*declarator
;
20094 declarator
= cp_parser_declarator (parser
,
20095 CP_PARSER_DECLARATOR_NAMED
,
20096 /*ctor_dtor_or_conv_p=*/NULL
,
20097 /*parenthesized_p=*/NULL
,
20098 /*member_p=*/false);
20099 attributes
= cp_parser_attributes_opt (parser
);
20100 asm_specification
= cp_parser_asm_specification_opt (parser
);
20102 cp_parser_require (parser
, CPP_EQ
, "`='");
20103 if (cp_parser_parse_definitely (parser
))
20107 decl
= start_decl (declarator
, &type_specifiers
,
20108 /*initialized_p=*/false, attributes
,
20109 /*prefix_attributes=*/NULL_TREE
,
20112 init
= cp_parser_assignment_expression (parser
, false);
20114 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
20115 init
= error_mark_node
;
20117 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
20118 asm_specification
, LOOKUP_ONLYCONVERTING
);
20121 pop_scope (pushed_scope
);
20125 cp_parser_abort_tentative_parse (parser
);
20127 /* If parsing as an initialized declaration failed, try again as
20128 a simple expression. */
20130 init
= cp_parser_expression (parser
, false);
20132 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
20133 pre_body
= pop_stmt_list (pre_body
);
20136 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20137 cond
= cp_parser_condition (parser
);
20138 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
20141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
20142 incr
= cp_parser_expression (parser
, false);
20144 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
20145 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20146 /*or_comma=*/false,
20147 /*consume_paren=*/true);
20149 /* Note that we saved the original contents of this flag when we entered
20150 the structured block, and so we don't need to re-save it here. */
20151 parser
->in_statement
= IN_OMP_FOR
;
20153 /* Note that the grammar doesn't call for a structured block here,
20154 though the loop as a whole is a structured block. */
20155 body
= push_stmt_list ();
20156 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
20157 body
= pop_stmt_list (body
);
20159 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
20163 #pragma omp for for-clause[optseq] new-line
20166 #define OMP_FOR_CLAUSE_MASK \
20167 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20168 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20169 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20170 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20171 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20172 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20173 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20176 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
20178 tree clauses
, sb
, ret
;
20181 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
20182 "#pragma omp for", pragma_tok
);
20184 sb
= begin_omp_structured_block ();
20185 save
= cp_parser_begin_omp_structured_block (parser
);
20187 ret
= cp_parser_omp_for_loop (parser
);
20189 OMP_FOR_CLAUSES (ret
) = clauses
;
20191 cp_parser_end_omp_structured_block (parser
, save
);
20192 add_stmt (finish_omp_structured_block (sb
));
20198 # pragma omp master new-line
20199 structured-block */
20202 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
20204 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20205 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
20209 # pragma omp ordered new-line
20210 structured-block */
20213 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
20215 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20216 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
20222 { section-sequence }
20225 section-directive[opt] structured-block
20226 section-sequence section-directive structured-block */
20229 cp_parser_omp_sections_scope (cp_parser
*parser
)
20231 tree stmt
, substmt
;
20232 bool error_suppress
= false;
20235 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
20238 stmt
= push_stmt_list ();
20240 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
20244 substmt
= begin_omp_structured_block ();
20245 save
= cp_parser_begin_omp_structured_block (parser
);
20249 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
20251 tok
= cp_lexer_peek_token (parser
->lexer
);
20252 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
20254 if (tok
->type
== CPP_CLOSE_BRACE
)
20256 if (tok
->type
== CPP_EOF
)
20260 cp_parser_end_omp_structured_block (parser
, save
);
20261 substmt
= finish_omp_structured_block (substmt
);
20262 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
20263 add_stmt (substmt
);
20268 tok
= cp_lexer_peek_token (parser
->lexer
);
20269 if (tok
->type
== CPP_CLOSE_BRACE
)
20271 if (tok
->type
== CPP_EOF
)
20274 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
20276 cp_lexer_consume_token (parser
->lexer
);
20277 cp_parser_require_pragma_eol (parser
, tok
);
20278 error_suppress
= false;
20280 else if (!error_suppress
)
20282 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
20283 error_suppress
= true;
20286 substmt
= cp_parser_omp_structured_block (parser
);
20287 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
20288 add_stmt (substmt
);
20290 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
20292 substmt
= pop_stmt_list (stmt
);
20294 stmt
= make_node (OMP_SECTIONS
);
20295 TREE_TYPE (stmt
) = void_type_node
;
20296 OMP_SECTIONS_BODY (stmt
) = substmt
;
20303 # pragma omp sections sections-clause[optseq] newline
20306 #define OMP_SECTIONS_CLAUSE_MASK \
20307 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20308 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20309 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20310 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20311 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20314 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
20318 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
20319 "#pragma omp sections", pragma_tok
);
20321 ret
= cp_parser_omp_sections_scope (parser
);
20323 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
20329 # pragma parallel parallel-clause new-line
20330 # pragma parallel for parallel-for-clause new-line
20331 # pragma parallel sections parallel-sections-clause new-line */
20333 #define OMP_PARALLEL_CLAUSE_MASK \
20334 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20335 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20336 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20337 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20338 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20339 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20340 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20341 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20344 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
20346 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
20347 const char *p_name
= "#pragma omp parallel";
20348 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
20349 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
20352 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
20354 cp_lexer_consume_token (parser
->lexer
);
20355 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
20356 p_name
= "#pragma omp parallel for";
20357 mask
|= OMP_FOR_CLAUSE_MASK
;
20358 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
20360 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20362 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
20363 const char *p
= IDENTIFIER_POINTER (id
);
20364 if (strcmp (p
, "sections") == 0)
20366 cp_lexer_consume_token (parser
->lexer
);
20367 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
20368 p_name
= "#pragma omp parallel sections";
20369 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
20370 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
20374 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
20375 block
= begin_omp_parallel ();
20376 save
= cp_parser_begin_omp_structured_block (parser
);
20380 case PRAGMA_OMP_PARALLEL
:
20381 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
20382 par_clause
= clauses
;
20385 case PRAGMA_OMP_PARALLEL_FOR
:
20386 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
20387 stmt
= cp_parser_omp_for_loop (parser
);
20389 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
20392 case PRAGMA_OMP_PARALLEL_SECTIONS
:
20393 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
20394 stmt
= cp_parser_omp_sections_scope (parser
);
20396 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
20400 gcc_unreachable ();
20403 cp_parser_end_omp_structured_block (parser
, save
);
20404 stmt
= finish_omp_parallel (par_clause
, block
);
20405 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
20406 OMP_PARALLEL_COMBINED (stmt
) = 1;
20411 # pragma omp single single-clause[optseq] new-line
20412 structured-block */
20414 #define OMP_SINGLE_CLAUSE_MASK \
20415 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20416 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20417 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20418 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20421 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
20423 tree stmt
= make_node (OMP_SINGLE
);
20424 TREE_TYPE (stmt
) = void_type_node
;
20426 OMP_SINGLE_CLAUSES (stmt
)
20427 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
20428 "#pragma omp single", pragma_tok
);
20429 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
20431 return add_stmt (stmt
);
20435 # pragma omp threadprivate (variable-list) */
20438 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
20442 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
20443 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20445 finish_omp_threadprivate (vars
);
20448 /* Main entry point to OpenMP statement pragmas. */
20451 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
20455 switch (pragma_tok
->pragma_kind
)
20457 case PRAGMA_OMP_ATOMIC
:
20458 cp_parser_omp_atomic (parser
, pragma_tok
);
20460 case PRAGMA_OMP_CRITICAL
:
20461 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
20463 case PRAGMA_OMP_FOR
:
20464 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
20466 case PRAGMA_OMP_MASTER
:
20467 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
20469 case PRAGMA_OMP_ORDERED
:
20470 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
20472 case PRAGMA_OMP_PARALLEL
:
20473 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
20475 case PRAGMA_OMP_SECTIONS
:
20476 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
20478 case PRAGMA_OMP_SINGLE
:
20479 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
20482 gcc_unreachable ();
20486 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
20491 static GTY (()) cp_parser
*the_parser
;
20494 /* Special handling for the first token or line in the file. The first
20495 thing in the file might be #pragma GCC pch_preprocess, which loads a
20496 PCH file, which is a GC collection point. So we need to handle this
20497 first pragma without benefit of an existing lexer structure.
20499 Always returns one token to the caller in *FIRST_TOKEN. This is
20500 either the true first token of the file, or the first token after
20501 the initial pragma. */
20504 cp_parser_initial_pragma (cp_token
*first_token
)
20508 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20509 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
20512 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20513 if (first_token
->type
== CPP_STRING
)
20515 name
= first_token
->u
.value
;
20517 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20518 if (first_token
->type
!= CPP_PRAGMA_EOL
)
20519 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20522 error ("expected string literal");
20524 /* Skip to the end of the pragma. */
20525 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
20526 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20528 /* Now actually load the PCH file. */
20530 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
20532 /* Read one more token to return to our caller. We have to do this
20533 after reading the PCH file in, since its pointers have to be
20535 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20538 /* Normal parsing of a pragma token. Here we can (and must) use the
20542 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
20544 cp_token
*pragma_tok
;
20547 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
20548 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
20549 parser
->lexer
->in_pragma
= true;
20551 id
= pragma_tok
->pragma_kind
;
20554 case PRAGMA_GCC_PCH_PREPROCESS
:
20555 error ("%<#pragma GCC pch_preprocess%> must be first");
20558 case PRAGMA_OMP_BARRIER
:
20561 case pragma_compound
:
20562 cp_parser_omp_barrier (parser
, pragma_tok
);
20565 error ("%<#pragma omp barrier%> may only be "
20566 "used in compound statements");
20573 case PRAGMA_OMP_FLUSH
:
20576 case pragma_compound
:
20577 cp_parser_omp_flush (parser
, pragma_tok
);
20580 error ("%<#pragma omp flush%> may only be "
20581 "used in compound statements");
20588 case PRAGMA_OMP_THREADPRIVATE
:
20589 cp_parser_omp_threadprivate (parser
, pragma_tok
);
20592 case PRAGMA_OMP_ATOMIC
:
20593 case PRAGMA_OMP_CRITICAL
:
20594 case PRAGMA_OMP_FOR
:
20595 case PRAGMA_OMP_MASTER
:
20596 case PRAGMA_OMP_ORDERED
:
20597 case PRAGMA_OMP_PARALLEL
:
20598 case PRAGMA_OMP_SECTIONS
:
20599 case PRAGMA_OMP_SINGLE
:
20600 if (context
== pragma_external
)
20602 cp_parser_omp_construct (parser
, pragma_tok
);
20605 case PRAGMA_OMP_SECTION
:
20606 error ("%<#pragma omp section%> may only be used in "
20607 "%<#pragma omp sections%> construct");
20611 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
20612 c_invoke_pragma_handler (id
);
20616 cp_parser_error (parser
, "expected declaration specifiers");
20620 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
20624 /* The interface the pragma parsers have to the lexer. */
20627 pragma_lex (tree
*value
)
20630 enum cpp_ttype ret
;
20632 tok
= cp_lexer_peek_token (the_parser
->lexer
);
20635 *value
= tok
->u
.value
;
20637 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
20639 else if (ret
== CPP_STRING
)
20640 *value
= cp_parser_string_literal (the_parser
, false, false);
20643 cp_lexer_consume_token (the_parser
->lexer
);
20644 if (ret
== CPP_KEYWORD
)
20652 /* External interface. */
20654 /* Parse one entire translation unit. */
20657 c_parse_file (void)
20659 bool error_occurred
;
20660 static bool already_called
= false;
20662 if (already_called
)
20664 sorry ("inter-module optimizations not implemented for C++");
20667 already_called
= true;
20669 the_parser
= cp_parser_new ();
20670 push_deferring_access_checks (flag_access_control
20671 ? dk_no_deferred
: dk_no_check
);
20672 error_occurred
= cp_parser_translation_unit (the_parser
);
20676 #include "gt-cp-parser.h"