2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 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 context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c
: 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p
: 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value
{
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check
* GTY((tag ("1"))) tree_check_value
;
84 /* Use for all other tokens. */
85 tree
GTY((tag ("0"))) value
;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u
;
87 /* The location at which this token was found. */
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token
*cp_token_position
;
93 DEF_VEC_P (cp_token_position
);
94 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
96 static cp_token eof_token
=
98 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, 0, { NULL
},
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
107 typedef struct cp_lexer
GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
112 /* If the lexer owns the buffer, this is the number of tokens in the
114 size_t buffer_length
;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position
GTY ((skip
)) last_token
;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position
GTY ((skip
)) next_token
;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
128 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer
*next
;
133 /* True if we should output debugging information. */
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
147 typedef struct cp_token_cache
GTY(())
149 /* The beginning of the token range. */
150 cp_token
* GTY((skip
)) first
;
152 /* Points immediately after the last token in the range. */
153 cp_token
* GTY ((skip
)) last
;
158 static cp_lexer
*cp_lexer_new_main
160 static cp_lexer
*cp_lexer_new_from_tokens
161 (cp_token_cache
*tokens
);
162 static void cp_lexer_destroy
164 static int cp_lexer_saving_tokens
166 static cp_token_position cp_lexer_token_position
168 static cp_token
*cp_lexer_token_at
169 (cp_lexer
*, cp_token_position
);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer
*, cp_token
*);
172 static inline cp_token
*cp_lexer_peek_token
174 static cp_token
*cp_lexer_peek_nth_token
175 (cp_lexer
*, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer
*, enum cpp_ttype
);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer
*, enum cpp_ttype
);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer
*, enum rid
);
182 static cp_token
*cp_lexer_consume_token
184 static void cp_lexer_purge_token
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer
*, cp_token_position
);
188 static void cp_lexer_save_tokens
190 static void cp_lexer_commit_tokens
192 static void cp_lexer_rollback_tokens
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token
*);
197 static inline bool cp_lexer_debugging_p
199 static void cp_lexer_start_debugging
200 (cp_lexer
*) ATTRIBUTE_UNUSED
;
201 static void cp_lexer_stop_debugging
202 (cp_lexer
*) ATTRIBUTE_UNUSED
;
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache
*cp_token_cache_new
214 (cp_token
*, cp_token
*);
216 static void cp_parser_initial_pragma
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream
;
252 #endif /* ENABLE_CHECKING */
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token
;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token
);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer
= GGC_CNEW (cp_lexer
);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer
->debugging_p
= false;
280 #endif /* ENABLE_CHECKING */
281 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
282 CP_SAVED_TOKEN_STACK
);
284 /* Create the buffer. */
285 alloc
= CP_LEXER_BUFFER_SIZE
;
286 buffer
= GGC_NEWVEC (cp_token
, alloc
);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos
->type
!= CPP_EOF
)
301 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
302 pos
= buffer
+ space
;
304 cp_lexer_get_preprocessor_token (lexer
, pos
);
306 lexer
->buffer
= buffer
;
307 lexer
->buffer_length
= alloc
- space
;
308 lexer
->last_token
= pos
;
309 lexer
->next_token
= lexer
->buffer_length
? buffer
: &eof_token
;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 cpp_get_options (parse_in
)->client_diagnostic
= true;
314 cpp_get_callbacks (parse_in
)->error
= cp_cpp_error
;
316 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
326 cp_token
*first
= cache
->first
;
327 cp_token
*last
= cache
->last
;
328 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
330 /* We do not own the buffer. */
331 lexer
->buffer
= NULL
;
332 lexer
->buffer_length
= 0;
333 lexer
->next_token
= first
== last
? &eof_token
: first
;
334 lexer
->last_token
= last
;
336 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
337 CP_SAVED_TOKEN_STACK
);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer
->debugging_p
= false;
344 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer
*lexer
)
354 ggc_free (lexer
->buffer
);
355 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer
*lexer
)
366 return lexer
->debugging_p
;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
374 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
376 return lexer
->next_token
- previous_p
;
379 static inline cp_token
*
380 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
390 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
401 static int is_extern_c
= 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
406 lexer
== NULL
? 0 : C_LEX_RAW_STRINGS
);
407 token
->keyword
= RID_MAX
;
408 token
->pragma_kind
= PRAGMA_NONE
;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c
+= pending_lang_change
;
414 pending_lang_change
= 0;
415 token
->implicit_extern_c
= is_extern_c
> 0;
417 /* Check to see if this token is a keyword. */
418 if (token
->type
== CPP_NAME
)
420 if (C_IS_RESERVED_WORD (token
->u
.value
))
422 /* Mark this token as a keyword. */
423 token
->type
= CPP_KEYWORD
;
424 /* Record which keyword. */
425 token
->keyword
= C_RID_CODE (token
->u
.value
);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token
->u
.value
= ridpointers
[token
->keyword
];
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
438 /* Warn about the C++0x keyword (but still treat it as
440 warning (OPT_Wc__0x_compat
,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token
->u
.value
));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
449 token
->ambiguous_p
= false;
450 token
->keyword
= RID_MAX
;
453 /* Handle Objective-C++ keywords. */
454 else if (token
->type
== CPP_AT_NAME
)
456 token
->type
= CPP_KEYWORD
;
457 switch (C_RID_CODE (token
->u
.value
))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
461 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
462 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
463 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
464 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
465 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
466 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
467 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
470 else if (token
->type
== CPP_PRAGMA
)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token
->pragma_kind
= TREE_INT_CST_LOW (token
->u
.value
);
474 token
->u
.value
= NULL_TREE
;
478 /* Update the globals input_location and the input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token
*token
)
482 if (token
->type
!= CPP_EOF
)
484 input_location
= token
->location
;
488 /* Return a pointer to the next token in the token stream, but do not
491 static inline cp_token
*
492 cp_lexer_peek_token (cp_lexer
*lexer
)
494 if (cp_lexer_debugging_p (lexer
))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
497 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
498 putc ('\n', cp_lexer_debug_stream
);
500 return lexer
->next_token
;
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
508 return cp_lexer_peek_token (lexer
)->type
== type
;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
516 return !cp_lexer_next_token_is (lexer
, type
);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
524 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
527 /* Return true if the next token is not the indicated KEYWORD. */
530 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
532 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
535 /* Return true if the next token is a keyword for a decl-specifier. */
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
542 token
= cp_lexer_peek_token (lexer
);
543 switch (token
->keyword
)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
548 /* Storage classes. */
554 /* Elaborated type specifiers. */
560 /* Simple type specifiers. */
574 /* GNU extensions. */
577 /* C++0x extensions. */
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
593 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
597 /* N is 1-based, not zero-based. */
600 if (cp_lexer_debugging_p (lexer
))
601 fprintf (cp_lexer_debug_stream
,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
605 token
= lexer
->next_token
;
606 gcc_assert (!n
|| token
!= &eof_token
);
610 if (token
== lexer
->last_token
)
616 if (token
->type
!= CPP_PURGED
)
620 if (cp_lexer_debugging_p (lexer
))
622 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
623 putc ('\n', cp_lexer_debug_stream
);
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
633 cp_lexer_consume_token (cp_lexer
* lexer
)
635 cp_token
*token
= lexer
->next_token
;
637 gcc_assert (token
!= &eof_token
);
638 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
643 if (lexer
->next_token
== lexer
->last_token
)
645 lexer
->next_token
= &eof_token
;
650 while (lexer
->next_token
->type
== CPP_PURGED
);
652 cp_lexer_set_source_position_from_token (token
);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer
))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
658 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
659 putc ('\n', cp_lexer_debug_stream
);
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
670 cp_lexer_purge_token (cp_lexer
*lexer
)
672 cp_token
*tok
= lexer
->next_token
;
674 gcc_assert (tok
!= &eof_token
);
675 tok
->type
= CPP_PURGED
;
676 tok
->location
= UNKNOWN_LOCATION
;
677 tok
->u
.value
= NULL_TREE
;
678 tok
->keyword
= RID_MAX
;
683 if (tok
== lexer
->last_token
)
689 while (tok
->type
== CPP_PURGED
);
690 lexer
->next_token
= tok
;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
698 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
700 cp_token
*peek
= lexer
->next_token
;
702 if (peek
== &eof_token
)
703 peek
= lexer
->last_token
;
705 gcc_assert (tok
< peek
);
707 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
709 tok
->type
= CPP_PURGED
;
710 tok
->location
= UNKNOWN_LOCATION
;
711 tok
->u
.value
= NULL_TREE
;
712 tok
->keyword
= RID_MAX
;
716 /* Begin saving tokens. All tokens consumed after this point will be
720 cp_lexer_save_tokens (cp_lexer
* lexer
)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer
))
724 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position
, heap
,
727 lexer
->saved_tokens
, lexer
->next_token
);
730 /* Commit to the portion of the token stream most recently saved. */
733 cp_lexer_commit_tokens (cp_lexer
* lexer
)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer
))
737 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
746 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer
))
750 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
752 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
760 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names
[] = {
765 /* cpplib-defined token types */
771 /* C++ parser token types - see "Manifest constants", above. */
774 "NESTED_NAME_SPECIFIER",
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
781 fputs (token_names
[token
->type
], stream
);
783 /* For some tokens, print the associated data. */
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
791 /* else fall through */
793 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
800 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer
* lexer
)
813 lexer
->debugging_p
= true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer
* lexer
)
821 lexer
->debugging_p
= false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache
*
829 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
831 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
832 cache
->first
= first
;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
845 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator
*make_call_declarator
856 (cp_declarator
*, tree
, cp_cv_quals
, tree
, tree
);
857 static cp_declarator
*make_array_declarator
858 (cp_declarator
*, tree
);
859 static cp_declarator
*make_pointer_declarator
860 (cp_cv_quals
, cp_declarator
*);
861 static cp_declarator
*make_reference_declarator
862 (cp_cv_quals
, cp_declarator
*, bool);
863 static cp_parameter_declarator
*make_parameter_declarator
864 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
865 static cp_declarator
*make_ptrmem_declarator
866 (cp_cv_quals
, tree
, cp_declarator
*);
868 /* An erroneous declarator. */
869 static cp_declarator
*cp_error_declarator
;
871 /* The obstack on which declarators and related data structures are
873 static struct obstack declarator_obstack
;
875 /* Alloc BYTES from the declarator memory pool. */
878 alloc_declarator (size_t bytes
)
880 return obstack_alloc (&declarator_obstack
, bytes
);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator
*
887 make_declarator (cp_declarator_kind kind
)
889 cp_declarator
*declarator
;
891 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
892 declarator
->kind
= kind
;
893 declarator
->attributes
= NULL_TREE
;
894 declarator
->declarator
= NULL
;
895 declarator
->parameter_pack_p
= false;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
906 static cp_declarator
*
907 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
908 special_function_kind sfk
)
910 cp_declarator
*declarator
;
912 /* It is valid to write:
914 class C { void f(); };
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
922 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
923 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
925 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
929 declarator
= make_declarator (cdk_id
);
930 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
931 declarator
->u
.id
.unqualified_name
= unqualified_name
;
932 declarator
->u
.id
.sfk
= sfk
;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
942 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
944 cp_declarator
*declarator
;
946 declarator
= make_declarator (cdk_pointer
);
947 declarator
->declarator
= target
;
948 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
949 declarator
->u
.pointer
.class_type
= NULL_TREE
;
952 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
953 target
->parameter_pack_p
= false;
956 declarator
->parameter_pack_p
= false;
961 /* Like make_pointer_declarator -- but for references. */
964 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
967 cp_declarator
*declarator
;
969 declarator
= make_declarator (cdk_reference
);
970 declarator
->declarator
= target
;
971 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
972 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
975 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
976 target
->parameter_pack_p
= false;
979 declarator
->parameter_pack_p
= false;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
989 cp_declarator
*pointee
)
991 cp_declarator
*declarator
;
993 declarator
= make_declarator (cdk_ptrmem
);
994 declarator
->declarator
= pointee
;
995 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
996 declarator
->u
.pointer
.class_type
= class_type
;
1000 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1001 pointee
->parameter_pack_p
= false;
1004 declarator
->parameter_pack_p
= false;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1015 make_call_declarator (cp_declarator
*target
,
1017 cp_cv_quals cv_qualifiers
,
1018 tree exception_specification
,
1019 tree late_return_type
)
1021 cp_declarator
*declarator
;
1023 declarator
= make_declarator (cdk_function
);
1024 declarator
->declarator
= target
;
1025 declarator
->u
.function
.parameters
= parms
;
1026 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1027 declarator
->u
.function
.exception_specification
= exception_specification
;
1028 declarator
->u
.function
.late_return_type
= late_return_type
;
1031 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1032 target
->parameter_pack_p
= false;
1035 declarator
->parameter_pack_p
= false;
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041 defined by ELEMENT. */
1044 make_array_declarator (cp_declarator
*element
, tree bounds
)
1046 cp_declarator
*declarator
;
1048 declarator
= make_declarator (cdk_array
);
1049 declarator
->declarator
= element
;
1050 declarator
->u
.array
.bounds
= bounds
;
1053 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1054 element
->parameter_pack_p
= false;
1057 declarator
->parameter_pack_p
= false;
1062 /* Determine whether the declarator we've seen so far can be a
1063 parameter pack, when followed by an ellipsis. */
1065 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1067 /* Search for a declarator name, or any other declarator that goes
1068 after the point where the ellipsis could appear in a parameter
1069 pack. If we find any of these, then this declarator can not be
1070 made into a parameter pack. */
1072 while (declarator
&& !found
)
1074 switch ((int)declarator
->kind
)
1085 declarator
= declarator
->declarator
;
1093 cp_parameter_declarator
*no_parameters
;
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096 DECLARATOR and DEFAULT_ARGUMENT. */
1098 cp_parameter_declarator
*
1099 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1100 cp_declarator
*declarator
,
1101 tree default_argument
)
1103 cp_parameter_declarator
*parameter
;
1105 parameter
= ((cp_parameter_declarator
*)
1106 alloc_declarator (sizeof (cp_parameter_declarator
)));
1107 parameter
->next
= NULL
;
1108 if (decl_specifiers
)
1109 parameter
->decl_specifiers
= *decl_specifiers
;
1111 clear_decl_specs (¶meter
->decl_specifiers
);
1112 parameter
->declarator
= declarator
;
1113 parameter
->default_argument
= default_argument
;
1114 parameter
->ellipsis_p
= false;
1119 /* Returns true iff DECLARATOR is a declaration for a function. */
1122 function_declarator_p (const cp_declarator
*declarator
)
1126 if (declarator
->kind
== cdk_function
1127 && declarator
->declarator
->kind
== cdk_id
)
1129 if (declarator
->kind
== cdk_id
1130 || declarator
->kind
== cdk_error
)
1132 declarator
= declarator
->declarator
;
1142 A cp_parser parses the token stream as specified by the C++
1143 grammar. Its job is purely parsing, not semantic analysis. For
1144 example, the parser breaks the token stream into declarators,
1145 expressions, statements, and other similar syntactic constructs.
1146 It does not check that the types of the expressions on either side
1147 of an assignment-statement are compatible, or that a function is
1148 not declared with a parameter of type `void'.
1150 The parser invokes routines elsewhere in the compiler to perform
1151 semantic analysis and to build up the abstract syntax tree for the
1154 The parser (and the template instantiation code, which is, in a
1155 way, a close relative of parsing) are the only parts of the
1156 compiler that should be calling push_scope and pop_scope, or
1157 related functions. The parser (and template instantiation code)
1158 keeps track of what scope is presently active; everything else
1159 should simply honor that. (The code that generates static
1160 initializers may also need to set the scope, in order to check
1161 access control correctly when emitting the initializers.)
1166 The parser is of the standard recursive-descent variety. Upcoming
1167 tokens in the token stream are examined in order to determine which
1168 production to use when parsing a non-terminal. Some C++ constructs
1169 require arbitrary look ahead to disambiguate. For example, it is
1170 impossible, in the general case, to tell whether a statement is an
1171 expression or declaration without scanning the entire statement.
1172 Therefore, the parser is capable of "parsing tentatively." When the
1173 parser is not sure what construct comes next, it enters this mode.
1174 Then, while we attempt to parse the construct, the parser queues up
1175 error messages, rather than issuing them immediately, and saves the
1176 tokens it consumes. If the construct is parsed successfully, the
1177 parser "commits", i.e., it issues any queued error messages and
1178 the tokens that were being preserved are permanently discarded.
1179 If, however, the construct is not parsed successfully, the parser
1180 rolls back its state completely so that it can resume parsing using
1181 a different alternative.
1186 The performance of the parser could probably be improved substantially.
1187 We could often eliminate the need to parse tentatively by looking ahead
1188 a little bit. In some places, this approach might not entirely eliminate
1189 the need to parse tentatively, but it might still speed up the average
1192 /* Flags that are passed to some parsing functions. These values can
1193 be bitwise-ored together. */
1195 typedef enum cp_parser_flags
1198 CP_PARSER_FLAGS_NONE
= 0x0,
1199 /* The construct is optional. If it is not present, then no error
1200 should be issued. */
1201 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1202 /* When parsing a type-specifier, do not allow user-defined types. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1206 /* The different kinds of declarators we want to parse. */
1208 typedef enum cp_parser_declarator_kind
1210 /* We want an abstract declarator. */
1211 CP_PARSER_DECLARATOR_ABSTRACT
,
1212 /* We want a named declarator. */
1213 CP_PARSER_DECLARATOR_NAMED
,
1214 /* We don't mind, but the name must be an unqualified-id. */
1215 CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind
;
1218 /* The precedence values used to parse binary expressions. The minimum value
1219 of PREC must be 1, because zero is reserved to quickly discriminate
1220 binary operators from other tokens. */
1225 PREC_LOGICAL_OR_EXPRESSION
,
1226 PREC_LOGICAL_AND_EXPRESSION
,
1227 PREC_INCLUSIVE_OR_EXPRESSION
,
1228 PREC_EXCLUSIVE_OR_EXPRESSION
,
1229 PREC_AND_EXPRESSION
,
1230 PREC_EQUALITY_EXPRESSION
,
1231 PREC_RELATIONAL_EXPRESSION
,
1232 PREC_SHIFT_EXPRESSION
,
1233 PREC_ADDITIVE_EXPRESSION
,
1234 PREC_MULTIPLICATIVE_EXPRESSION
,
1236 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240 precedence value. */
1242 typedef struct cp_parser_binary_operations_map_node
1244 /* The token type. */
1245 enum cpp_ttype token_type
;
1246 /* The corresponding tree code. */
1247 enum tree_code tree_type
;
1248 /* The precedence of this operator. */
1249 enum cp_parser_prec prec
;
1250 } cp_parser_binary_operations_map_node
;
1252 /* The status of a tentative parse. */
1254 typedef enum cp_parser_status_kind
1256 /* No errors have occurred. */
1257 CP_PARSER_STATUS_KIND_NO_ERROR
,
1258 /* An error has occurred. */
1259 CP_PARSER_STATUS_KIND_ERROR
,
1260 /* We are committed to this tentative parse, whether or not an error
1262 CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind
;
1265 typedef struct cp_parser_expression_stack_entry
1267 /* Left hand side of the binary operation we are currently
1270 /* Original tree code for left hand side, if it was a binary
1271 expression itself (used for -Wparentheses). */
1272 enum tree_code lhs_type
;
1273 /* Tree code for the binary operation we are parsing. */
1274 enum tree_code tree_type
;
1275 /* Precedence of the binary operation we are parsing. */
1277 } cp_parser_expression_stack_entry
;
1279 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1280 entries because precedence levels on the stack are monotonically
1282 typedef struct cp_parser_expression_stack_entry
1283 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1285 /* Context that is saved and restored when parsing tentatively. */
1286 typedef struct cp_parser_context
GTY (())
1288 /* If this is a tentative parsing context, the status of the
1290 enum cp_parser_status_kind status
;
1291 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1292 that are looked up in this context must be looked up both in the
1293 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294 the context of the containing expression. */
1297 /* The next parsing context in the stack. */
1298 struct cp_parser_context
*next
;
1299 } cp_parser_context
;
1303 /* Constructors and destructors. */
1305 static cp_parser_context
*cp_parser_context_new
1306 (cp_parser_context
*);
1308 /* Class variables. */
1310 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313 Transformed into an associative array (binops_by_token) by
1316 static const cp_parser_binary_operations_map_node binops
[] = {
1317 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1318 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1320 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1321 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1322 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1324 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1325 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1327 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1328 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1330 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1331 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1332 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1333 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1335 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1336 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1338 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1340 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1342 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1344 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1346 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1349 /* The same as binops, but initialized by cp_parser_new so that
1350 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1352 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1354 /* Constructors and destructors. */
1356 /* Construct a new context. The context below this one on the stack
1357 is given by NEXT. */
1359 static cp_parser_context
*
1360 cp_parser_context_new (cp_parser_context
* next
)
1362 cp_parser_context
*context
;
1364 /* Allocate the storage. */
1365 if (cp_parser_context_free_list
!= NULL
)
1367 /* Pull the first entry from the free list. */
1368 context
= cp_parser_context_free_list
;
1369 cp_parser_context_free_list
= context
->next
;
1370 memset (context
, 0, sizeof (*context
));
1373 context
= GGC_CNEW (cp_parser_context
);
1375 /* No errors have occurred yet in this context. */
1376 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1377 /* If this is not the bottommost context, copy information that we
1378 need from the previous context. */
1381 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382 expression, then we are parsing one in this context, too. */
1383 context
->object_type
= next
->object_type
;
1384 /* Thread the stack. */
1385 context
->next
= next
;
1391 /* The cp_parser structure represents the C++ parser. */
1393 typedef struct cp_parser
GTY(())
1395 /* The lexer from which we are obtaining tokens. */
1398 /* The scope in which names should be looked up. If NULL_TREE, then
1399 we look up names in the scope that is currently open in the
1400 source program. If non-NULL, this is either a TYPE or
1401 NAMESPACE_DECL for the scope in which we should look. It can
1402 also be ERROR_MARK, when we've parsed a bogus scope.
1404 This value is not cleared automatically after a name is looked
1405 up, so we must be careful to clear it before starting a new look
1406 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1407 will look up `Z' in the scope of `X', rather than the current
1408 scope.) Unfortunately, it is difficult to tell when name lookup
1409 is complete, because we sometimes peek at a token, look it up,
1410 and then decide not to consume it. */
1413 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414 last lookup took place. OBJECT_SCOPE is used if an expression
1415 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416 respectively. QUALIFYING_SCOPE is used for an expression of the
1417 form "X::Y"; it refers to X. */
1419 tree qualifying_scope
;
1421 /* A stack of parsing contexts. All but the bottom entry on the
1422 stack will be tentative contexts.
1424 We parse tentatively in order to determine which construct is in
1425 use in some situations. For example, in order to determine
1426 whether a statement is an expression-statement or a
1427 declaration-statement we parse it tentatively as a
1428 declaration-statement. If that fails, we then reparse the same
1429 token stream as an expression-statement. */
1430 cp_parser_context
*context
;
1432 /* True if we are parsing GNU C++. If this flag is not set, then
1433 GNU extensions are not recognized. */
1434 bool allow_gnu_extensions_p
;
1436 /* TRUE if the `>' token should be interpreted as the greater-than
1437 operator. FALSE if it is the end of a template-id or
1438 template-parameter-list. In C++0x mode, this flag also applies to
1439 `>>' tokens, which are viewed as two consecutive `>' tokens when
1440 this flag is FALSE. */
1441 bool greater_than_is_operator_p
;
1443 /* TRUE if default arguments are allowed within a parameter list
1444 that starts at this point. FALSE if only a gnu extension makes
1445 them permissible. */
1446 bool default_arg_ok_p
;
1448 /* TRUE if we are parsing an integral constant-expression. See
1449 [expr.const] for a precise definition. */
1450 bool integral_constant_expression_p
;
1452 /* TRUE if we are parsing an integral constant-expression -- but a
1453 non-constant expression should be permitted as well. This flag
1454 is used when parsing an array bound so that GNU variable-length
1455 arrays are tolerated. */
1456 bool allow_non_integral_constant_expression_p
;
1458 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459 been seen that makes the expression non-constant. */
1460 bool non_integral_constant_expression_p
;
1462 /* TRUE if local variable names and `this' are forbidden in the
1464 bool local_variables_forbidden_p
;
1466 /* TRUE if the declaration we are parsing is part of a
1467 linkage-specification of the form `extern string-literal
1469 bool in_unbraced_linkage_specification_p
;
1471 /* TRUE if we are presently parsing a declarator, after the
1472 direct-declarator. */
1473 bool in_declarator_p
;
1475 /* TRUE if we are presently parsing a template-argument-list. */
1476 bool in_template_argument_list_p
;
1478 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479 to IN_OMP_BLOCK if parsing OpenMP structured block and
1480 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1481 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482 iteration-statement, OpenMP block or loop within that switch. */
1483 #define IN_SWITCH_STMT 1
1484 #define IN_ITERATION_STMT 2
1485 #define IN_OMP_BLOCK 4
1486 #define IN_OMP_FOR 8
1487 #define IN_IF_STMT 16
1488 unsigned char in_statement
;
1490 /* TRUE if we are presently parsing the body of a switch statement.
1491 Note that this doesn't quite overlap with in_statement above.
1492 The difference relates to giving the right sets of error messages:
1493 "case not in switch" vs "break statement used with OpenMP...". */
1494 bool in_switch_statement_p
;
1496 /* TRUE if we are parsing a type-id in an expression context. In
1497 such a situation, both "type (expr)" and "type (type)" are valid
1499 bool in_type_id_in_expr_p
;
1501 /* TRUE if we are currently in a header file where declarations are
1502 implicitly extern "C". */
1503 bool implicit_extern_c
;
1505 /* TRUE if strings in expressions should be translated to the execution
1507 bool translate_strings_p
;
1509 /* TRUE if we are presently parsing the body of a function, but not
1511 bool in_function_body
;
1513 /* If non-NULL, then we are parsing a construct where new type
1514 definitions are not permitted. The string stored here will be
1515 issued as an error message if a type is defined. */
1516 const char *type_definition_forbidden_message
;
1518 /* A list of lists. The outer list is a stack, used for member
1519 functions of local classes. At each level there are two sub-list,
1520 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522 TREE_VALUE's. The functions are chained in reverse declaration
1525 The TREE_PURPOSE sublist contains those functions with default
1526 arguments that need post processing, and the TREE_VALUE sublist
1527 contains those functions with definitions that need post
1530 These lists can only be processed once the outermost class being
1531 defined is complete. */
1532 tree unparsed_functions_queues
;
1534 /* The number of classes whose definitions are currently in
1536 unsigned num_classes_being_defined
;
1538 /* The number of template parameter lists that apply directly to the
1539 current declaration. */
1540 unsigned num_template_parameter_lists
;
1545 /* Constructors and destructors. */
1547 static cp_parser
*cp_parser_new
1550 /* Routines to parse various constructs.
1552 Those that return `tree' will return the error_mark_node (rather
1553 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554 Sometimes, they will return an ordinary node if error-recovery was
1555 attempted, even though a parse error occurred. So, to check
1556 whether or not a parse error occurred, you should always use
1557 cp_parser_error_occurred. If the construct is optional (indicated
1558 either by an `_opt' in the name of the function that does the
1559 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560 the construct is not present. */
1562 /* Lexical conventions [gram.lex] */
1564 static tree cp_parser_identifier
1566 static tree cp_parser_string_literal
1567 (cp_parser
*, bool, bool);
1569 /* Basic concepts [gram.basic] */
1571 static bool cp_parser_translation_unit
1574 /* Expressions [gram.expr] */
1576 static tree cp_parser_primary_expression
1577 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1578 static tree cp_parser_id_expression
1579 (cp_parser
*, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581 (cp_parser
*, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583 (cp_parser
*, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585 (cp_parser
*, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587 (cp_parser
*, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1590 static tree cp_parser_postfix_open_square_expression
1591 (cp_parser
*, tree
, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1594 static tree cp_parser_parenthesized_expression_list
1595 (cp_parser
*, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597 (cp_parser
*, tree
*, tree
*);
1598 static tree cp_parser_unary_expression
1599 (cp_parser
*, bool, bool, cp_id_kind
*);
1600 static enum tree_code cp_parser_unary_operator
1602 static tree cp_parser_new_expression
1604 static tree cp_parser_new_placement
1606 static tree cp_parser_new_type_id
1607 (cp_parser
*, tree
*);
1608 static cp_declarator
*cp_parser_new_declarator_opt
1610 static cp_declarator
*cp_parser_direct_new_declarator
1612 static tree cp_parser_new_initializer
1614 static tree cp_parser_delete_expression
1616 static tree cp_parser_cast_expression
1617 (cp_parser
*, bool, bool, cp_id_kind
*);
1618 static tree cp_parser_binary_expression
1619 (cp_parser
*, bool, enum cp_parser_prec
, cp_id_kind
*);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser
*, tree
);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser
*, bool, cp_id_kind
*);
1624 static enum tree_code cp_parser_assignment_operator_opt
1626 static tree cp_parser_expression
1627 (cp_parser
*, bool, cp_id_kind
*);
1628 static tree cp_parser_constant_expression
1629 (cp_parser
*, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1633 /* Statements [gram.stmt.stmt] */
1635 static void cp_parser_statement
1636 (cp_parser
*, tree
, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1639 static tree cp_parser_expression_statement
1640 (cp_parser
*, tree
);
1641 static tree cp_parser_compound_statement
1642 (cp_parser
*, tree
, bool);
1643 static void cp_parser_statement_seq_opt
1644 (cp_parser
*, tree
);
1645 static tree cp_parser_selection_statement
1646 (cp_parser
*, bool *);
1647 static tree cp_parser_condition
1649 static tree cp_parser_iteration_statement
1651 static void cp_parser_for_init_statement
1653 static tree cp_parser_jump_statement
1655 static void cp_parser_declaration_statement
1658 static tree cp_parser_implicitly_scoped_statement
1659 (cp_parser
*, bool *);
1660 static void cp_parser_already_scoped_statement
1663 /* Declarations [gram.dcl.dcl] */
1665 static void cp_parser_declaration_seq_opt
1667 static void cp_parser_declaration
1669 static void cp_parser_block_declaration
1670 (cp_parser
*, bool);
1671 static void cp_parser_simple_declaration
1672 (cp_parser
*, bool);
1673 static void cp_parser_decl_specifier_seq
1674 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1677 static tree cp_parser_function_specifier_opt
1678 (cp_parser
*, cp_decl_specifier_seq
*);
1679 static tree cp_parser_type_specifier
1680 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1682 static tree cp_parser_simple_type_specifier
1683 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1684 static tree cp_parser_type_name
1686 static tree cp_parser_nonclass_name
1687 (cp_parser
* parser
);
1688 static tree cp_parser_elaborated_type_specifier
1689 (cp_parser
*, bool, bool);
1690 static tree cp_parser_enum_specifier
1692 static void cp_parser_enumerator_list
1693 (cp_parser
*, tree
);
1694 static void cp_parser_enumerator_definition
1695 (cp_parser
*, tree
);
1696 static tree cp_parser_namespace_name
1698 static void cp_parser_namespace_definition
1700 static void cp_parser_namespace_body
1702 static tree cp_parser_qualified_namespace_specifier
1704 static void cp_parser_namespace_alias_definition
1706 static bool cp_parser_using_declaration
1707 (cp_parser
*, bool);
1708 static void cp_parser_using_directive
1710 static void cp_parser_asm_definition
1712 static void cp_parser_linkage_specification
1714 static void cp_parser_static_assert
1715 (cp_parser
*, bool);
1716 static tree cp_parser_decltype
1719 /* Declarators [gram.dcl.decl] */
1721 static tree cp_parser_init_declarator
1722 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1723 static cp_declarator
*cp_parser_declarator
1724 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1725 static cp_declarator
*cp_parser_direct_declarator
1726 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728 (cp_parser
*, tree
*, cp_cv_quals
*);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1731 static tree cp_parser_late_return_type_opt
1733 static tree cp_parser_declarator_id
1734 (cp_parser
*, bool);
1735 static tree cp_parser_type_id
1737 static void cp_parser_type_specifier_seq
1738 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1739 static tree cp_parser_parameter_declaration_clause
1741 static tree cp_parser_parameter_declaration_list
1742 (cp_parser
*, bool *);
1743 static cp_parameter_declarator
*cp_parser_parameter_declaration
1744 (cp_parser
*, bool, bool *);
1745 static tree cp_parser_default_argument
1746 (cp_parser
*, bool);
1747 static void cp_parser_function_body
1749 static tree cp_parser_initializer
1750 (cp_parser
*, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752 (cp_parser
*, bool *);
1753 static tree cp_parser_braced_list
1754 (cp_parser
*, bool*);
1755 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1756 (cp_parser
*, bool *);
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1761 /* Classes [gram.class] */
1763 static tree cp_parser_class_name
1764 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1767 static tree cp_parser_class_head
1768 (cp_parser
*, bool *, tree
*, tree
*);
1769 static enum tag_types cp_parser_class_key
1771 static void cp_parser_member_specification_opt
1773 static void cp_parser_member_declaration
1775 static tree cp_parser_pure_specifier
1777 static tree cp_parser_constant_initializer
1780 /* Derived classes [gram.class.derived] */
1782 static tree cp_parser_base_clause
1784 static tree cp_parser_base_specifier
1787 /* Special member functions [gram.special] */
1789 static tree cp_parser_conversion_function_id
1791 static tree cp_parser_conversion_type_id
1793 static cp_declarator
*cp_parser_conversion_declarator_opt
1795 static bool cp_parser_ctor_initializer_opt
1797 static void cp_parser_mem_initializer_list
1799 static tree cp_parser_mem_initializer
1801 static tree cp_parser_mem_initializer_id
1804 /* Overloading [gram.over] */
1806 static tree cp_parser_operator_function_id
1808 static tree cp_parser_operator
1811 /* Templates [gram.temp] */
1813 static void cp_parser_template_declaration
1814 (cp_parser
*, bool);
1815 static tree cp_parser_template_parameter_list
1817 static tree cp_parser_template_parameter
1818 (cp_parser
*, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820 (cp_parser
*, bool *);
1821 static tree cp_parser_template_id
1822 (cp_parser
*, bool, bool, bool);
1823 static tree cp_parser_template_name
1824 (cp_parser
*, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1827 static tree cp_parser_template_argument
1829 static void cp_parser_explicit_instantiation
1831 static void cp_parser_explicit_specialization
1834 /* Exception handling [gram.exception] */
1836 static tree cp_parser_try_block
1838 static bool cp_parser_function_try_block
1840 static void cp_parser_handler_seq
1842 static void cp_parser_handler
1844 static tree cp_parser_exception_declaration
1846 static tree cp_parser_throw_expression
1848 static tree cp_parser_exception_specification_opt
1850 static tree cp_parser_type_id_list
1853 /* GNU Extensions */
1855 static tree cp_parser_asm_specification_opt
1857 static tree cp_parser_asm_operand_list
1859 static tree cp_parser_asm_clobber_list
1861 static tree cp_parser_attributes_opt
1863 static tree cp_parser_attribute_list
1865 static bool cp_parser_extension_opt
1866 (cp_parser
*, int *);
1867 static void cp_parser_label_declaration
1870 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1871 static bool cp_parser_pragma
1872 (cp_parser
*, enum pragma_context
);
1874 /* Objective-C++ Productions */
1876 static tree cp_parser_objc_message_receiver
1878 static tree cp_parser_objc_message_args
1880 static tree cp_parser_objc_message_expression
1882 static tree cp_parser_objc_encode_expression
1884 static tree cp_parser_objc_defs_expression
1886 static tree cp_parser_objc_protocol_expression
1888 static tree cp_parser_objc_selector_expression
1890 static tree cp_parser_objc_expression
1892 static bool cp_parser_objc_selector_p
1894 static tree cp_parser_objc_selector
1896 static tree cp_parser_objc_protocol_refs_opt
1898 static void cp_parser_objc_declaration
1900 static tree cp_parser_objc_statement
1903 /* Utility Routines */
1905 static tree cp_parser_lookup_name
1906 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
1907 static tree cp_parser_lookup_name_simple
1908 (cp_parser
*, tree
, location_t
);
1909 static tree cp_parser_maybe_treat_template_as_class
1911 static bool cp_parser_check_declarator_template_parameters
1912 (cp_parser
*, cp_declarator
*, location_t
);
1913 static bool cp_parser_check_template_parameters
1914 (cp_parser
*, unsigned, location_t
);
1915 static tree cp_parser_simple_cast_expression
1917 static tree cp_parser_global_scope_opt
1918 (cp_parser
*, bool);
1919 static bool cp_parser_constructor_declarator_p
1920 (cp_parser
*, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1923 static tree cp_parser_function_definition_after_declarator
1924 (cp_parser
*, bool);
1925 static void cp_parser_template_declaration_after_export
1926 (cp_parser
*, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928 (VEC (deferred_access_check
,gc
)*);
1929 static tree cp_parser_single_declaration
1930 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932 (cp_parser
*, tree
);
1933 static tree cp_parser_save_member_function_body
1934 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1935 static tree cp_parser_enclosed_template_argument_list
1937 static void cp_parser_save_default_args
1938 (cp_parser
*, tree
);
1939 static void cp_parser_late_parsing_for_member
1940 (cp_parser
*, tree
);
1941 static void cp_parser_late_parsing_default_args
1942 (cp_parser
*, tree
);
1943 static tree cp_parser_sizeof_operand
1944 (cp_parser
*, enum rid
);
1945 static tree cp_parser_trait_expr
1946 (cp_parser
*, enum rid
);
1947 static bool cp_parser_declares_only_class_p
1949 static void cp_parser_set_storage_class
1950 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, location_t
);
1951 static void cp_parser_set_decl_spec_type
1952 (cp_decl_specifier_seq
*, tree
, location_t
, bool);
1953 static bool cp_parser_friend_p
1954 (const cp_decl_specifier_seq
*);
1955 static cp_token
*cp_parser_require
1956 (cp_parser
*, enum cpp_ttype
, const char *);
1957 static cp_token
*cp_parser_require_keyword
1958 (cp_parser
*, enum rid
, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1961 static bool cp_parser_next_token_starts_class_definition_p
1963 static bool cp_parser_next_token_ends_template_argument_p
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966 (cp_parser
*, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1969 static void cp_parser_check_class_key
1970 (enum tag_types
, tree type
);
1971 static void cp_parser_check_access_in_redeclaration
1972 (tree type
, location_t location
);
1973 static bool cp_parser_optional_template_keyword
1975 static void cp_parser_pre_parsed_nested_name_specifier
1977 static bool cp_parser_cache_group
1978 (cp_parser
*, enum cpp_ttype
, unsigned);
1979 static void cp_parser_parse_tentatively
1981 static void cp_parser_commit_to_tentative_parse
1983 static void cp_parser_abort_tentative_parse
1985 static bool cp_parser_parse_definitely
1987 static inline bool cp_parser_parsing_tentatively
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1991 static void cp_parser_error
1992 (cp_parser
*, const char *);
1993 static void cp_parser_name_lookup_error
1994 (cp_parser
*, tree
, tree
, const char *, location_t
);
1995 static bool cp_parser_simulate_error
1997 static bool cp_parser_check_type_definition
1999 static void cp_parser_check_for_definition_in_return_type
2000 (cp_declarator
*, tree
, location_t type_location
);
2001 static void cp_parser_check_for_invalid_template_id
2002 (cp_parser
*, tree
, location_t location
);
2003 static bool cp_parser_non_integral_constant_expression
2004 (cp_parser
*, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006 (cp_parser
*, tree
, tree
, location_t
);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2009 static int cp_parser_skip_to_closing_parenthesis
2010 (cp_parser
*, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2015 static void cp_parser_skip_to_end_of_block_or_statement
2017 static bool cp_parser_skip_to_closing_brace
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2021 static void cp_parser_skip_to_pragma_eol
2022 (cp_parser
*, cp_token
*);
2023 static bool cp_parser_error_occurred
2025 static bool cp_parser_allow_gnu_extensions_p
2027 static bool cp_parser_is_string_literal
2029 static bool cp_parser_is_keyword
2030 (cp_token
*, enum rid
);
2031 static tree cp_parser_make_typename_type
2032 (cp_parser
*, tree
, tree
, location_t location
);
2033 static cp_declarator
* cp_parser_make_indirect_declarator
2034 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2036 /* Returns nonzero if we are parsing tentatively. */
2039 cp_parser_parsing_tentatively (cp_parser
* parser
)
2041 return parser
->context
->next
!= NULL
;
2044 /* Returns nonzero if TOKEN is a string literal. */
2047 cp_parser_is_string_literal (cp_token
* token
)
2049 return (token
->type
== CPP_STRING
||
2050 token
->type
== CPP_STRING16
||
2051 token
->type
== CPP_STRING32
||
2052 token
->type
== CPP_WSTRING
);
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2058 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2060 return token
->keyword
== keyword
;
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064 FILE:LINE: MESSAGE before TOKEN
2065 where TOKEN is the next token in the input stream. MESSAGE
2066 (specified by the caller) is usually of the form "expected
2070 cp_parser_error (cp_parser
* parser
, const char* message
)
2072 if (!cp_parser_simulate_error (parser
))
2074 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2075 /* This diagnostic makes more sense if it is tagged to the line
2076 of the token we just peeked at. */
2077 cp_lexer_set_source_position_from_token (token
);
2079 if (token
->type
== CPP_PRAGMA
)
2081 error ("%H%<#pragma%> is not allowed here", &token
->location
);
2082 cp_parser_skip_to_pragma_eol (parser
, token
);
2086 c_parse_error (message
,
2087 /* Because c_parser_error does not understand
2088 CPP_KEYWORD, keywords are treated like
2090 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2095 /* Issue an error about name-lookup failing. NAME is the
2096 IDENTIFIER_NODE DECL is the result of
2097 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2098 the thing that we hoped to find. */
2101 cp_parser_name_lookup_error (cp_parser
* parser
,
2104 const char* desired
,
2105 location_t location
)
2107 /* If name lookup completely failed, tell the user that NAME was not
2109 if (decl
== error_mark_node
)
2111 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2112 error ("%H%<%E::%E%> has not been declared",
2113 &location
, parser
->scope
, name
);
2114 else if (parser
->scope
== global_namespace
)
2115 error ("%H%<::%E%> has not been declared", &location
, name
);
2116 else if (parser
->object_scope
2117 && !CLASS_TYPE_P (parser
->object_scope
))
2118 error ("%Hrequest for member %qE in non-class type %qT",
2119 &location
, name
, parser
->object_scope
);
2120 else if (parser
->object_scope
)
2121 error ("%H%<%T::%E%> has not been declared",
2122 &location
, parser
->object_scope
, name
);
2124 error ("%H%qE has not been declared", &location
, name
);
2126 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2127 error ("%H%<%E::%E%> %s", &location
, parser
->scope
, name
, desired
);
2128 else if (parser
->scope
== global_namespace
)
2129 error ("%H%<::%E%> %s", &location
, name
, desired
);
2131 error ("%H%qE %s", &location
, name
, desired
);
2134 /* If we are parsing tentatively, remember that an error has occurred
2135 during this tentative parse. Returns true if the error was
2136 simulated; false if a message should be issued by the caller. */
2139 cp_parser_simulate_error (cp_parser
* parser
)
2141 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2143 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2149 /* Check for repeated decl-specifiers. */
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
,
2153 location_t location
)
2157 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2159 unsigned count
= decl_specs
->specs
[(int)ds
];
2162 /* The "long" specifier is a special case because of "long long". */
2166 error ("%H%<long long long%> is too long for GCC", &location
);
2167 else if (pedantic
&& !in_system_header
&& warn_long_long
2168 && cxx_dialect
== cxx98
)
2169 pedwarn (location
, OPT_Wlong_long
,
2170 "ISO C++ 1998 does not support %<long long%>");
2174 static const char *const decl_spec_names
[] = {
2190 error ("%Hduplicate %qs", &location
, decl_spec_names
[(int)ds
]);
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2200 cp_parser_check_type_definition (cp_parser
* parser
)
2202 /* If types are forbidden here, issue a message. */
2203 if (parser
->type_definition_forbidden_message
)
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser
->type_definition_forbidden_message
);
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2220 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2221 tree type
, location_t type_location
)
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2227 && (declarator
->kind
== cdk_pointer
2228 || declarator
->kind
== cdk_reference
2229 || declarator
->kind
== cdk_ptrmem
))
2230 declarator
= declarator
->declarator
;
2232 && declarator
->kind
== cdk_function
)
2234 error ("%Hnew types may not be defined in a return type", &type_location
);
2235 inform (type_location
,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2248 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2249 tree type
, location_t location
)
2251 cp_token_position start
= 0;
2253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2256 error ("%H%qT is not a template", &location
, type
);
2257 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2258 error ("%H%qE is not a template", &location
, type
);
2260 error ("%Hinvalid template-id", &location
);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2263 start
= cp_lexer_token_position (parser
->lexer
, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser
->lexer
);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser
);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2271 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2281 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2284 parser
->non_integral_constant_expression_p
= true;
2285 if (parser
->integral_constant_expression_p
)
2287 if (!parser
->allow_non_integral_constant_expression_p
)
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message
= concat (thing
,
2292 " cannot appear in a constant-expression",
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2309 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2310 tree scope
, tree id
,
2311 location_t location
)
2313 tree decl
, old_scope
;
2314 /* Try to lookup the identifier. */
2315 old_scope
= parser
->scope
;
2316 parser
->scope
= scope
;
2317 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2318 parser
->scope
= old_scope
;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2324 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2325 error ("%Hinvalid use of destructor %qD as a type", &location
, id
);
2326 else if (TREE_CODE (decl
) == TYPE_DECL
)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2330 else if (!parser
->scope
)
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location
, id
);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl
&& current_class_type
2342 && TYPE_BINFO (current_class_type
))
2346 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2350 tree base_type
= BINFO_TYPE (b
);
2351 if (CLASS_TYPE_P (base_type
)
2352 && dependent_type_p (base_type
))
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2359 for (field
= TYPE_FIELDS (base_type
);
2361 field
= TREE_CHAIN (field
))
2362 if (TREE_CODE (field
) == TYPE_DECL
2363 && DECL_NAME (field
) == id
)
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b
), id
);
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser
->scope
!= error_mark_node
)
2380 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location
, id
, parser
->scope
);
2383 else if (TYPE_P (parser
->scope
))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location
, id
, parser
->scope
);
2389 cp_parser_commit_to_tentative_parse (parser
);
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2406 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2408 cp_parser_parse_tentatively (parser
);
2409 id
= cp_parser_id_expression (parser
,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL
,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2419 || (parser
->scope
&& TYPE_P (parser
->scope
)
2420 && dependent_type_p (parser
->scope
))
2421 || TREE_CODE (id
) == TYPE_DECL
)
2423 cp_parser_abort_tentative_parse (parser
);
2426 if (!cp_parser_parse_definitely (parser
))
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2431 id
, token
->location
);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser
);
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2444 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2449 unsigned paren_depth
= 0;
2450 unsigned brace_depth
= 0;
2452 if (recovering
&& !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2458 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2460 switch (token
->type
)
2463 case CPP_PRAGMA_EOL
:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2468 /* This matches the processing in skip_to_end_of_statement. */
2473 case CPP_OPEN_BRACE
:
2476 case CPP_CLOSE_BRACE
:
2482 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2486 case CPP_OPEN_PAREN
:
2491 case CPP_CLOSE_PAREN
:
2492 if (!brace_depth
&& !paren_depth
--)
2495 cp_lexer_consume_token (parser
->lexer
);
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser
->lexer
);
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2514 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2516 unsigned nesting_depth
= 0;
2520 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2522 switch (token
->type
)
2525 case CPP_PRAGMA_EOL
:
2526 /* If we've run out of tokens, stop. */
2530 /* If the next token is a `;', we have reached the end of the
2536 case CPP_CLOSE_BRACE
:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth
== 0)
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2557 if (--nesting_depth
== 0)
2559 cp_lexer_consume_token (parser
->lexer
);
2563 case CPP_OPEN_BRACE
:
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser
->lexer
);
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>"))
2586 /* If there is additional (erroneous) input, skip to the end of
2588 cp_parser_skip_to_end_of_statement (parser
);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2591 cp_lexer_consume_token (parser
->lexer
);
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2601 int nesting_depth
= 0;
2603 while (nesting_depth
>= 0)
2605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2607 switch (token
->type
)
2610 case CPP_PRAGMA_EOL
:
2611 /* If we've run out of tokens, stop. */
2615 /* Stop if this is an unnested ';'. */
2620 case CPP_CLOSE_BRACE
:
2621 /* Stop if this is an unnested '}', or closes the outermost
2628 case CPP_OPEN_BRACE
:
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser
->lexer
);
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643 token, or there are no more tokens. Return true in the first case,
2647 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2649 unsigned nesting_depth
= 0;
2653 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2655 switch (token
->type
)
2658 case CPP_PRAGMA_EOL
:
2659 /* If we've run out of tokens, stop. */
2662 case CPP_CLOSE_BRACE
:
2663 /* If the next token is a non-nested `}', then we have reached
2664 the end of the current block. */
2665 if (nesting_depth
-- == 0)
2669 case CPP_OPEN_BRACE
:
2670 /* If it the next token is a `{', then we are entering a new
2671 block. Consume the entire block. */
2679 /* Consume the token. */
2680 cp_lexer_consume_token (parser
->lexer
);
2684 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2685 parameter is the PRAGMA token, allowing us to purge the entire pragma
2689 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2693 parser
->lexer
->in_pragma
= false;
2696 token
= cp_lexer_consume_token (parser
->lexer
);
2697 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2699 /* Ensure that the pragma is not parsed again. */
2700 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2703 /* Require pragma end of line, resyncing with it as necessary. The
2704 arguments are as for cp_parser_skip_to_pragma_eol. */
2707 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2709 parser
->lexer
->in_pragma
= false;
2710 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2711 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715 an unresolved identifier node, we can provide a superior diagnostic
2716 using cp_parser_diagnose_invalid_type_name. */
2719 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
2720 tree id
, location_t id_location
)
2723 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2725 result
= make_typename_type (scope
, id
, typename_type
,
2726 /*complain=*/tf_none
);
2727 if (result
== error_mark_node
)
2728 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
2731 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2734 /* This is a wrapper around the
2735 make_{pointer,ptrmem,reference}_declarator functions that decides
2736 which one to call based on the CODE and CLASS_TYPE arguments. The
2737 CODE argument should be one of the values returned by
2738 cp_parser_ptr_operator. */
2739 static cp_declarator
*
2740 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
2741 cp_cv_quals cv_qualifiers
,
2742 cp_declarator
*target
)
2744 if (code
== ERROR_MARK
)
2745 return cp_error_declarator
;
2747 if (code
== INDIRECT_REF
)
2748 if (class_type
== NULL_TREE
)
2749 return make_pointer_declarator (cv_qualifiers
, target
);
2751 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
2752 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
2753 return make_reference_declarator (cv_qualifiers
, target
, false);
2754 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
2755 return make_reference_declarator (cv_qualifiers
, target
, true);
2759 /* Create a new C++ parser. */
2762 cp_parser_new (void)
2768 /* cp_lexer_new_main is called before calling ggc_alloc because
2769 cp_lexer_new_main might load a PCH file. */
2770 lexer
= cp_lexer_new_main ();
2772 /* Initialize the binops_by_token so that we can get the tree
2773 directly from the token. */
2774 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2775 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2777 parser
= GGC_CNEW (cp_parser
);
2778 parser
->lexer
= lexer
;
2779 parser
->context
= cp_parser_context_new (NULL
);
2781 /* For now, we always accept GNU extensions. */
2782 parser
->allow_gnu_extensions_p
= 1;
2784 /* The `>' token is a greater-than operator, not the end of a
2786 parser
->greater_than_is_operator_p
= true;
2788 parser
->default_arg_ok_p
= true;
2790 /* We are not parsing a constant-expression. */
2791 parser
->integral_constant_expression_p
= false;
2792 parser
->allow_non_integral_constant_expression_p
= false;
2793 parser
->non_integral_constant_expression_p
= false;
2795 /* Local variable names are not forbidden. */
2796 parser
->local_variables_forbidden_p
= false;
2798 /* We are not processing an `extern "C"' declaration. */
2799 parser
->in_unbraced_linkage_specification_p
= false;
2801 /* We are not processing a declarator. */
2802 parser
->in_declarator_p
= false;
2804 /* We are not processing a template-argument-list. */
2805 parser
->in_template_argument_list_p
= false;
2807 /* We are not in an iteration statement. */
2808 parser
->in_statement
= 0;
2810 /* We are not in a switch statement. */
2811 parser
->in_switch_statement_p
= false;
2813 /* We are not parsing a type-id inside an expression. */
2814 parser
->in_type_id_in_expr_p
= false;
2816 /* Declarations aren't implicitly extern "C". */
2817 parser
->implicit_extern_c
= false;
2819 /* String literals should be translated to the execution character set. */
2820 parser
->translate_strings_p
= true;
2822 /* We are not parsing a function body. */
2823 parser
->in_function_body
= false;
2825 /* The unparsed function queue is empty. */
2826 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2828 /* There are no classes being defined. */
2829 parser
->num_classes_being_defined
= 0;
2831 /* No template parameters apply. */
2832 parser
->num_template_parameter_lists
= 0;
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838 and push it onto the parser's lexer stack. This is used for delayed
2839 parsing of in-class method bodies and default arguments, and should
2840 not be confused with tentative parsing. */
2842 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2844 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2845 lexer
->next
= parser
->lexer
;
2846 parser
->lexer
= lexer
;
2848 /* Move the current source position to that of the first token in the
2850 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2853 /* Pop the top lexer off the parser stack. This is never used for the
2854 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2856 cp_parser_pop_lexer (cp_parser
*parser
)
2858 cp_lexer
*lexer
= parser
->lexer
;
2859 parser
->lexer
= lexer
->next
;
2860 cp_lexer_destroy (lexer
);
2862 /* Put the current source position back where it was before this
2863 lexer was pushed. */
2864 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2867 /* Lexical conventions [gram.lex] */
2869 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2873 cp_parser_identifier (cp_parser
* parser
)
2877 /* Look for the identifier. */
2878 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2879 /* Return the value. */
2880 return token
? token
->u
.value
: error_mark_node
;
2883 /* Parse a sequence of adjacent string constants. Returns a
2884 TREE_STRING representing the combined, nul-terminated string
2885 constant. If TRANSLATE is true, translate the string to the
2886 execution character set. If WIDE_OK is true, a wide string is
2889 C++98 [lex.string] says that if a narrow string literal token is
2890 adjacent to a wide string literal token, the behavior is undefined.
2891 However, C99 6.4.5p4 says that this results in a wide string literal.
2892 We follow C99 here, for consistency with the C front end.
2894 This code is largely lifted from lex_string() in c-lex.c.
2896 FUTURE: ObjC++ will need to handle @-strings here. */
2898 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2902 struct obstack str_ob
;
2903 cpp_string str
, istr
, *strs
;
2905 enum cpp_ttype type
;
2907 tok
= cp_lexer_peek_token (parser
->lexer
);
2908 if (!cp_parser_is_string_literal (tok
))
2910 cp_parser_error (parser
, "expected string-literal");
2911 return error_mark_node
;
2916 /* Try to avoid the overhead of creating and destroying an obstack
2917 for the common case of just one string. */
2918 if (!cp_parser_is_string_literal
2919 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2921 cp_lexer_consume_token (parser
->lexer
);
2923 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2924 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2931 gcc_obstack_init (&str_ob
);
2936 cp_lexer_consume_token (parser
->lexer
);
2938 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2939 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2941 if (type
!= tok
->type
)
2943 if (type
== CPP_STRING
)
2945 else if (tok
->type
!= CPP_STRING
)
2946 error ("%Hunsupported non-standard concatenation "
2947 "of string literals", &tok
->location
);
2950 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2952 tok
= cp_lexer_peek_token (parser
->lexer
);
2954 while (cp_parser_is_string_literal (tok
));
2956 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2959 if (type
!= CPP_STRING
&& !wide_ok
)
2961 cp_parser_error (parser
, "a wide string is invalid in this context");
2965 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2966 (parse_in
, strs
, count
, &istr
, type
))
2968 value
= build_string (istr
.len
, (const char *)istr
.text
);
2969 free (CONST_CAST (unsigned char *, istr
.text
));
2975 TREE_TYPE (value
) = char_array_type_node
;
2978 TREE_TYPE (value
) = char16_array_type_node
;
2981 TREE_TYPE (value
) = char32_array_type_node
;
2984 TREE_TYPE (value
) = wchar_array_type_node
;
2988 value
= fix_string_type (value
);
2991 /* cpp_interpret_string has issued an error. */
2992 value
= error_mark_node
;
2995 obstack_free (&str_ob
, 0);
3001 /* Basic concepts [gram.basic] */
3003 /* Parse a translation-unit.
3006 declaration-seq [opt]
3008 Returns TRUE if all went well. */
3011 cp_parser_translation_unit (cp_parser
* parser
)
3013 /* The address of the first non-permanent object on the declarator
3015 static void *declarator_obstack_base
;
3019 /* Create the declarator obstack, if necessary. */
3020 if (!cp_error_declarator
)
3022 gcc_obstack_init (&declarator_obstack
);
3023 /* Create the error declarator. */
3024 cp_error_declarator
= make_declarator (cdk_error
);
3025 /* Create the empty parameter list. */
3026 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3027 /* Remember where the base of the declarator obstack lies. */
3028 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3031 cp_parser_declaration_seq_opt (parser
);
3033 /* If there are no tokens left then all went well. */
3034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3036 /* Get rid of the token array; we don't need it any more. */
3037 cp_lexer_destroy (parser
->lexer
);
3038 parser
->lexer
= NULL
;
3040 /* This file might have been a context that's implicitly extern
3041 "C". If so, pop the lang context. (Only relevant for PCH.) */
3042 if (parser
->implicit_extern_c
)
3044 pop_lang_context ();
3045 parser
->implicit_extern_c
= false;
3049 finish_translation_unit ();
3055 cp_parser_error (parser
, "expected declaration");
3059 /* Make sure the declarator obstack was fully cleaned up. */
3060 gcc_assert (obstack_next_free (&declarator_obstack
)
3061 == declarator_obstack_base
);
3063 /* All went well. */
3067 /* Expressions [gram.expr] */
3069 /* Parse a primary-expression.
3080 ( compound-statement )
3081 __builtin_va_arg ( assignment-expression , type-id )
3082 __builtin_offsetof ( type-id , offsetof-expression )
3085 __has_nothrow_assign ( type-id )
3086 __has_nothrow_constructor ( type-id )
3087 __has_nothrow_copy ( type-id )
3088 __has_trivial_assign ( type-id )
3089 __has_trivial_constructor ( type-id )
3090 __has_trivial_copy ( type-id )
3091 __has_trivial_destructor ( type-id )
3092 __has_virtual_destructor ( type-id )
3093 __is_abstract ( type-id )
3094 __is_base_of ( type-id , type-id )
3095 __is_class ( type-id )
3096 __is_convertible_to ( type-id , type-id )
3097 __is_empty ( type-id )
3098 __is_enum ( type-id )
3099 __is_pod ( type-id )
3100 __is_polymorphic ( type-id )
3101 __is_union ( type-id )
3103 Objective-C++ Extension:
3111 ADDRESS_P is true iff this expression was immediately preceded by
3112 "&" and therefore might denote a pointer-to-member. CAST_P is true
3113 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3114 true iff this expression is a template argument.
3116 Returns a representation of the expression. Upon return, *IDK
3117 indicates what kind of id-expression (if any) was present. */
3120 cp_parser_primary_expression (cp_parser
*parser
,
3123 bool template_arg_p
,
3126 cp_token
*token
= NULL
;
3128 /* Assume the primary expression is not an id-expression. */
3129 *idk
= CP_ID_KIND_NONE
;
3131 /* Peek at the next token. */
3132 token
= cp_lexer_peek_token (parser
->lexer
);
3133 switch (token
->type
)
3146 token
= cp_lexer_consume_token (parser
->lexer
);
3147 /* Floating-point literals are only allowed in an integral
3148 constant expression if they are cast to an integral or
3149 enumeration type. */
3150 if (TREE_CODE (token
->u
.value
) == REAL_CST
3151 && parser
->integral_constant_expression_p
3154 /* CAST_P will be set even in invalid code like "int(2.7 +
3155 ...)". Therefore, we have to check that the next token
3156 is sure to end the cast. */
3159 cp_token
*next_token
;
3161 next_token
= cp_lexer_peek_token (parser
->lexer
);
3162 if (/* The comma at the end of an
3163 enumerator-definition. */
3164 next_token
->type
!= CPP_COMMA
3165 /* The curly brace at the end of an enum-specifier. */
3166 && next_token
->type
!= CPP_CLOSE_BRACE
3167 /* The end of a statement. */
3168 && next_token
->type
!= CPP_SEMICOLON
3169 /* The end of the cast-expression. */
3170 && next_token
->type
!= CPP_CLOSE_PAREN
3171 /* The end of an array bound. */
3172 && next_token
->type
!= CPP_CLOSE_SQUARE
3173 /* The closing ">" in a template-argument-list. */
3174 && (next_token
->type
!= CPP_GREATER
3175 || parser
->greater_than_is_operator_p
)
3176 /* C++0x only: A ">>" treated like two ">" tokens,
3177 in a template-argument-list. */
3178 && (next_token
->type
!= CPP_RSHIFT
3179 || (cxx_dialect
== cxx98
)
3180 || parser
->greater_than_is_operator_p
))
3184 /* If we are within a cast, then the constraint that the
3185 cast is to an integral or enumeration type will be
3186 checked at that point. If we are not within a cast, then
3187 this code is invalid. */
3189 cp_parser_non_integral_constant_expression
3190 (parser
, "floating-point literal");
3192 return token
->u
.value
;
3198 /* ??? Should wide strings be allowed when parser->translate_strings_p
3199 is false (i.e. in attributes)? If not, we can kill the third
3200 argument to cp_parser_string_literal. */
3201 return cp_parser_string_literal (parser
,
3202 parser
->translate_strings_p
,
3205 case CPP_OPEN_PAREN
:
3208 bool saved_greater_than_is_operator_p
;
3210 /* Consume the `('. */
3211 cp_lexer_consume_token (parser
->lexer
);
3212 /* Within a parenthesized expression, a `>' token is always
3213 the greater-than operator. */
3214 saved_greater_than_is_operator_p
3215 = parser
->greater_than_is_operator_p
;
3216 parser
->greater_than_is_operator_p
= true;
3217 /* If we see `( { ' then we are looking at the beginning of
3218 a GNU statement-expression. */
3219 if (cp_parser_allow_gnu_extensions_p (parser
)
3220 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3222 /* Statement-expressions are not allowed by the standard. */
3223 pedwarn (token
->location
, OPT_pedantic
,
3224 "ISO C++ forbids braced-groups within expressions");
3226 /* And they're not allowed outside of a function-body; you
3227 cannot, for example, write:
3229 int i = ({ int j = 3; j + 1; });
3231 at class or namespace scope. */
3232 if (!parser
->in_function_body
3233 || parser
->in_template_argument_list_p
)
3235 error ("%Hstatement-expressions are not allowed outside "
3236 "functions nor in template-argument lists",
3238 cp_parser_skip_to_end_of_block_or_statement (parser
);
3239 expr
= error_mark_node
;
3243 /* Start the statement-expression. */
3244 expr
= begin_stmt_expr ();
3245 /* Parse the compound-statement. */
3246 cp_parser_compound_statement (parser
, expr
, false);
3248 expr
= finish_stmt_expr (expr
, false);
3253 /* Parse the parenthesized expression. */
3254 expr
= cp_parser_expression (parser
, cast_p
, idk
);
3255 /* Let the front end know that this expression was
3256 enclosed in parentheses. This matters in case, for
3257 example, the expression is of the form `A::B', since
3258 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3260 finish_parenthesized_expr (expr
);
3262 /* The `>' token might be the end of a template-id or
3263 template-parameter-list now. */
3264 parser
->greater_than_is_operator_p
3265 = saved_greater_than_is_operator_p
;
3266 /* Consume the `)'. */
3267 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
3268 cp_parser_skip_to_end_of_statement (parser
);
3274 switch (token
->keyword
)
3276 /* These two are the boolean literals. */
3278 cp_lexer_consume_token (parser
->lexer
);
3279 return boolean_true_node
;
3281 cp_lexer_consume_token (parser
->lexer
);
3282 return boolean_false_node
;
3284 /* The `__null' literal. */
3286 cp_lexer_consume_token (parser
->lexer
);
3289 /* Recognize the `this' keyword. */
3291 cp_lexer_consume_token (parser
->lexer
);
3292 if (parser
->local_variables_forbidden_p
)
3294 error ("%H%<this%> may not be used in this context",
3296 return error_mark_node
;
3298 /* Pointers cannot appear in constant-expressions. */
3299 if (cp_parser_non_integral_constant_expression (parser
, "%<this%>"))
3300 return error_mark_node
;
3301 return finish_this_expr ();
3303 /* The `operator' keyword can be the beginning of an
3308 case RID_FUNCTION_NAME
:
3309 case RID_PRETTY_FUNCTION_NAME
:
3310 case RID_C99_FUNCTION_NAME
:
3314 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3315 __func__ are the names of variables -- but they are
3316 treated specially. Therefore, they are handled here,
3317 rather than relying on the generic id-expression logic
3318 below. Grammatically, these names are id-expressions.
3320 Consume the token. */
3321 token
= cp_lexer_consume_token (parser
->lexer
);
3323 switch (token
->keyword
)
3325 case RID_FUNCTION_NAME
:
3326 name
= "%<__FUNCTION__%>";
3328 case RID_PRETTY_FUNCTION_NAME
:
3329 name
= "%<__PRETTY_FUNCTION__%>";
3331 case RID_C99_FUNCTION_NAME
:
3332 name
= "%<__func__%>";
3338 if (cp_parser_non_integral_constant_expression (parser
, name
))
3339 return error_mark_node
;
3341 /* Look up the name. */
3342 return finish_fname (token
->u
.value
);
3350 /* The `__builtin_va_arg' construct is used to handle
3351 `va_arg'. Consume the `__builtin_va_arg' token. */
3352 cp_lexer_consume_token (parser
->lexer
);
3353 /* Look for the opening `('. */
3354 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
3355 /* Now, parse the assignment-expression. */
3356 expression
= cp_parser_assignment_expression (parser
,
3357 /*cast_p=*/false, NULL
);
3358 /* Look for the `,'. */
3359 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
3360 /* Parse the type-id. */
3361 type
= cp_parser_type_id (parser
);
3362 /* Look for the closing `)'. */
3363 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
3364 /* Using `va_arg' in a constant-expression is not
3366 if (cp_parser_non_integral_constant_expression (parser
,
3368 return error_mark_node
;
3369 return build_x_va_arg (expression
, type
);
3373 return cp_parser_builtin_offsetof (parser
);
3375 case RID_HAS_NOTHROW_ASSIGN
:
3376 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3377 case RID_HAS_NOTHROW_COPY
:
3378 case RID_HAS_TRIVIAL_ASSIGN
:
3379 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3380 case RID_HAS_TRIVIAL_COPY
:
3381 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3382 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3383 case RID_IS_ABSTRACT
:
3384 case RID_IS_BASE_OF
:
3386 case RID_IS_CONVERTIBLE_TO
:
3390 case RID_IS_POLYMORPHIC
:
3392 return cp_parser_trait_expr (parser
, token
->keyword
);
3394 /* Objective-C++ expressions. */
3396 case RID_AT_PROTOCOL
:
3397 case RID_AT_SELECTOR
:
3398 return cp_parser_objc_expression (parser
);
3401 cp_parser_error (parser
, "expected primary-expression");
3402 return error_mark_node
;
3405 /* An id-expression can start with either an identifier, a
3406 `::' as the beginning of a qualified-id, or the "operator"
3410 case CPP_TEMPLATE_ID
:
3411 case CPP_NESTED_NAME_SPECIFIER
:
3415 const char *error_msg
;
3418 cp_token
*id_expr_token
;
3421 /* Parse the id-expression. */
3423 = cp_parser_id_expression (parser
,
3424 /*template_keyword_p=*/false,
3425 /*check_dependency_p=*/true,
3427 /*declarator_p=*/false,
3428 /*optional_p=*/false);
3429 if (id_expression
== error_mark_node
)
3430 return error_mark_node
;
3431 id_expr_token
= token
;
3432 token
= cp_lexer_peek_token (parser
->lexer
);
3433 done
= (token
->type
!= CPP_OPEN_SQUARE
3434 && token
->type
!= CPP_OPEN_PAREN
3435 && token
->type
!= CPP_DOT
3436 && token
->type
!= CPP_DEREF
3437 && token
->type
!= CPP_PLUS_PLUS
3438 && token
->type
!= CPP_MINUS_MINUS
);
3439 /* If we have a template-id, then no further lookup is
3440 required. If the template-id was for a template-class, we
3441 will sometimes have a TYPE_DECL at this point. */
3442 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3443 || TREE_CODE (id_expression
) == TYPE_DECL
)
3444 decl
= id_expression
;
3445 /* Look up the name. */
3448 tree ambiguous_decls
;
3450 decl
= cp_parser_lookup_name (parser
, id_expression
,
3453 /*is_namespace=*/false,
3454 /*check_dependency=*/true,
3456 id_expr_token
->location
);
3457 /* If the lookup was ambiguous, an error will already have
3459 if (ambiguous_decls
)
3460 return error_mark_node
;
3462 /* In Objective-C++, an instance variable (ivar) may be preferred
3463 to whatever cp_parser_lookup_name() found. */
3464 decl
= objc_lookup_ivar (decl
, id_expression
);
3466 /* If name lookup gives us a SCOPE_REF, then the
3467 qualifying scope was dependent. */
3468 if (TREE_CODE (decl
) == SCOPE_REF
)
3470 /* At this point, we do not know if DECL is a valid
3471 integral constant expression. We assume that it is
3472 in fact such an expression, so that code like:
3474 template <int N> struct A {
3478 is accepted. At template-instantiation time, we
3479 will check that B<N>::i is actually a constant. */
3482 /* Check to see if DECL is a local variable in a context
3483 where that is forbidden. */
3484 if (parser
->local_variables_forbidden_p
3485 && local_variable_p (decl
))
3487 /* It might be that we only found DECL because we are
3488 trying to be generous with pre-ISO scoping rules.
3489 For example, consider:
3493 for (int i = 0; i < 10; ++i) {}
3494 extern void f(int j = i);
3497 Here, name look up will originally find the out
3498 of scope `i'. We need to issue a warning message,
3499 but then use the global `i'. */
3500 decl
= check_for_out_of_scope_variable (decl
);
3501 if (local_variable_p (decl
))
3503 error ("%Hlocal variable %qD may not appear in this context",
3504 &id_expr_token
->location
, decl
);
3505 return error_mark_node
;
3510 decl
= (finish_id_expression
3511 (id_expression
, decl
, parser
->scope
,
3513 parser
->integral_constant_expression_p
,
3514 parser
->allow_non_integral_constant_expression_p
,
3515 &parser
->non_integral_constant_expression_p
,
3516 template_p
, done
, address_p
,
3519 id_expr_token
->location
));
3521 cp_parser_error (parser
, error_msg
);
3525 /* Anything else is an error. */
3527 /* ...unless we have an Objective-C++ message or string literal,
3529 if (c_dialect_objc ()
3530 && (token
->type
== CPP_OPEN_SQUARE
3531 || token
->type
== CPP_OBJC_STRING
))
3532 return cp_parser_objc_expression (parser
);
3534 cp_parser_error (parser
, "expected primary-expression");
3535 return error_mark_node
;
3539 /* Parse an id-expression.
3546 :: [opt] nested-name-specifier template [opt] unqualified-id
3548 :: operator-function-id
3551 Return a representation of the unqualified portion of the
3552 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3553 a `::' or nested-name-specifier.
3555 Often, if the id-expression was a qualified-id, the caller will
3556 want to make a SCOPE_REF to represent the qualified-id. This
3557 function does not do this in order to avoid wastefully creating
3558 SCOPE_REFs when they are not required.
3560 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3563 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3564 uninstantiated templates.
3566 If *TEMPLATE_P is non-NULL, it is set to true iff the
3567 `template' keyword is used to explicitly indicate that the entity
3568 named is a template.
3570 If DECLARATOR_P is true, the id-expression is appearing as part of
3571 a declarator, rather than as part of an expression. */
3574 cp_parser_id_expression (cp_parser
*parser
,
3575 bool template_keyword_p
,
3576 bool check_dependency_p
,
3581 bool global_scope_p
;
3582 bool nested_name_specifier_p
;
3584 /* Assume the `template' keyword was not used. */
3586 *template_p
= template_keyword_p
;
3588 /* Look for the optional `::' operator. */
3590 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3592 /* Look for the optional nested-name-specifier. */
3593 nested_name_specifier_p
3594 = (cp_parser_nested_name_specifier_opt (parser
,
3595 /*typename_keyword_p=*/false,
3600 /* If there is a nested-name-specifier, then we are looking at
3601 the first qualified-id production. */
3602 if (nested_name_specifier_p
)
3605 tree saved_object_scope
;
3606 tree saved_qualifying_scope
;
3607 tree unqualified_id
;
3610 /* See if the next token is the `template' keyword. */
3612 template_p
= &is_template
;
3613 *template_p
= cp_parser_optional_template_keyword (parser
);
3614 /* Name lookup we do during the processing of the
3615 unqualified-id might obliterate SCOPE. */
3616 saved_scope
= parser
->scope
;
3617 saved_object_scope
= parser
->object_scope
;
3618 saved_qualifying_scope
= parser
->qualifying_scope
;
3619 /* Process the final unqualified-id. */
3620 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3623 /*optional_p=*/false);
3624 /* Restore the SAVED_SCOPE for our caller. */
3625 parser
->scope
= saved_scope
;
3626 parser
->object_scope
= saved_object_scope
;
3627 parser
->qualifying_scope
= saved_qualifying_scope
;
3629 return unqualified_id
;
3631 /* Otherwise, if we are in global scope, then we are looking at one
3632 of the other qualified-id productions. */
3633 else if (global_scope_p
)
3638 /* Peek at the next token. */
3639 token
= cp_lexer_peek_token (parser
->lexer
);
3641 /* If it's an identifier, and the next token is not a "<", then
3642 we can avoid the template-id case. This is an optimization
3643 for this common case. */
3644 if (token
->type
== CPP_NAME
3645 && !cp_parser_nth_token_starts_template_argument_list_p
3647 return cp_parser_identifier (parser
);
3649 cp_parser_parse_tentatively (parser
);
3650 /* Try a template-id. */
3651 id
= cp_parser_template_id (parser
,
3652 /*template_keyword_p=*/false,
3653 /*check_dependency_p=*/true,
3655 /* If that worked, we're done. */
3656 if (cp_parser_parse_definitely (parser
))
3659 /* Peek at the next token. (Changes in the token buffer may
3660 have invalidated the pointer obtained above.) */
3661 token
= cp_lexer_peek_token (parser
->lexer
);
3663 switch (token
->type
)
3666 return cp_parser_identifier (parser
);
3669 if (token
->keyword
== RID_OPERATOR
)
3670 return cp_parser_operator_function_id (parser
);
3674 cp_parser_error (parser
, "expected id-expression");
3675 return error_mark_node
;
3679 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3680 /*check_dependency_p=*/true,
3685 /* Parse an unqualified-id.
3689 operator-function-id
3690 conversion-function-id
3694 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3695 keyword, in a construct like `A::template ...'.
3697 Returns a representation of unqualified-id. For the `identifier'
3698 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3699 production a BIT_NOT_EXPR is returned; the operand of the
3700 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3701 other productions, see the documentation accompanying the
3702 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3703 names are looked up in uninstantiated templates. If DECLARATOR_P
3704 is true, the unqualified-id is appearing as part of a declarator,
3705 rather than as part of an expression. */
3708 cp_parser_unqualified_id (cp_parser
* parser
,
3709 bool template_keyword_p
,
3710 bool check_dependency_p
,
3716 /* Peek at the next token. */
3717 token
= cp_lexer_peek_token (parser
->lexer
);
3719 switch (token
->type
)
3725 /* We don't know yet whether or not this will be a
3727 cp_parser_parse_tentatively (parser
);
3728 /* Try a template-id. */
3729 id
= cp_parser_template_id (parser
, template_keyword_p
,
3732 /* If it worked, we're done. */
3733 if (cp_parser_parse_definitely (parser
))
3735 /* Otherwise, it's an ordinary identifier. */
3736 return cp_parser_identifier (parser
);
3739 case CPP_TEMPLATE_ID
:
3740 return cp_parser_template_id (parser
, template_keyword_p
,
3747 tree qualifying_scope
;
3752 /* Consume the `~' token. */
3753 cp_lexer_consume_token (parser
->lexer
);
3754 /* Parse the class-name. The standard, as written, seems to
3757 template <typename T> struct S { ~S (); };
3758 template <typename T> S<T>::~S() {}
3760 is invalid, since `~' must be followed by a class-name, but
3761 `S<T>' is dependent, and so not known to be a class.
3762 That's not right; we need to look in uninstantiated
3763 templates. A further complication arises from:
3765 template <typename T> void f(T t) {
3769 Here, it is not possible to look up `T' in the scope of `T'
3770 itself. We must look in both the current scope, and the
3771 scope of the containing complete expression.
3773 Yet another issue is:
3782 The standard does not seem to say that the `S' in `~S'
3783 should refer to the type `S' and not the data member
3786 /* DR 244 says that we look up the name after the "~" in the
3787 same scope as we looked up the qualifying name. That idea
3788 isn't fully worked out; it's more complicated than that. */
3789 scope
= parser
->scope
;
3790 object_scope
= parser
->object_scope
;
3791 qualifying_scope
= parser
->qualifying_scope
;
3793 /* Check for invalid scopes. */
3794 if (scope
== error_mark_node
)
3796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3797 cp_lexer_consume_token (parser
->lexer
);
3798 return error_mark_node
;
3800 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3803 error ("%Hscope %qT before %<~%> is not a class-name",
3804 &token
->location
, scope
);
3805 cp_parser_simulate_error (parser
);
3806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3807 cp_lexer_consume_token (parser
->lexer
);
3808 return error_mark_node
;
3810 gcc_assert (!scope
|| TYPE_P (scope
));
3812 /* If the name is of the form "X::~X" it's OK. */
3813 token
= cp_lexer_peek_token (parser
->lexer
);
3815 && token
->type
== CPP_NAME
3816 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3818 && constructor_name_p (token
->u
.value
, scope
))
3820 cp_lexer_consume_token (parser
->lexer
);
3821 return build_nt (BIT_NOT_EXPR
, scope
);
3824 /* If there was an explicit qualification (S::~T), first look
3825 in the scope given by the qualification (i.e., S). */
3827 type_decl
= NULL_TREE
;
3830 cp_parser_parse_tentatively (parser
);
3831 type_decl
= cp_parser_class_name (parser
,
3832 /*typename_keyword_p=*/false,
3833 /*template_keyword_p=*/false,
3835 /*check_dependency=*/false,
3836 /*class_head_p=*/false,
3838 if (cp_parser_parse_definitely (parser
))
3841 /* In "N::S::~S", look in "N" as well. */
3842 if (!done
&& scope
&& qualifying_scope
)
3844 cp_parser_parse_tentatively (parser
);
3845 parser
->scope
= qualifying_scope
;
3846 parser
->object_scope
= NULL_TREE
;
3847 parser
->qualifying_scope
= NULL_TREE
;
3849 = cp_parser_class_name (parser
,
3850 /*typename_keyword_p=*/false,
3851 /*template_keyword_p=*/false,
3853 /*check_dependency=*/false,
3854 /*class_head_p=*/false,
3856 if (cp_parser_parse_definitely (parser
))
3859 /* In "p->S::~T", look in the scope given by "*p" as well. */
3860 else if (!done
&& object_scope
)
3862 cp_parser_parse_tentatively (parser
);
3863 parser
->scope
= object_scope
;
3864 parser
->object_scope
= NULL_TREE
;
3865 parser
->qualifying_scope
= NULL_TREE
;
3867 = cp_parser_class_name (parser
,
3868 /*typename_keyword_p=*/false,
3869 /*template_keyword_p=*/false,
3871 /*check_dependency=*/false,
3872 /*class_head_p=*/false,
3874 if (cp_parser_parse_definitely (parser
))
3877 /* Look in the surrounding context. */
3880 parser
->scope
= NULL_TREE
;
3881 parser
->object_scope
= NULL_TREE
;
3882 parser
->qualifying_scope
= NULL_TREE
;
3883 if (processing_template_decl
)
3884 cp_parser_parse_tentatively (parser
);
3886 = cp_parser_class_name (parser
,
3887 /*typename_keyword_p=*/false,
3888 /*template_keyword_p=*/false,
3890 /*check_dependency=*/false,
3891 /*class_head_p=*/false,
3893 if (processing_template_decl
3894 && ! cp_parser_parse_definitely (parser
))
3896 /* We couldn't find a type with this name, so just accept
3897 it and check for a match at instantiation time. */
3898 type_decl
= cp_parser_identifier (parser
);
3899 return build_nt (BIT_NOT_EXPR
, type_decl
);
3902 /* If an error occurred, assume that the name of the
3903 destructor is the same as the name of the qualifying
3904 class. That allows us to keep parsing after running
3905 into ill-formed destructor names. */
3906 if (type_decl
== error_mark_node
&& scope
)
3907 return build_nt (BIT_NOT_EXPR
, scope
);
3908 else if (type_decl
== error_mark_node
)
3909 return error_mark_node
;
3911 /* Check that destructor name and scope match. */
3912 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3914 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3915 error ("%Hdeclaration of %<~%T%> as member of %qT",
3916 &token
->location
, type_decl
, scope
);
3917 cp_parser_simulate_error (parser
);
3918 return error_mark_node
;
3923 A typedef-name that names a class shall not be used as the
3924 identifier in the declarator for a destructor declaration. */
3926 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3927 && !DECL_SELF_REFERENCE_P (type_decl
)
3928 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3929 error ("%Htypedef-name %qD used as destructor declarator",
3930 &token
->location
, type_decl
);
3932 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3936 if (token
->keyword
== RID_OPERATOR
)
3940 /* This could be a template-id, so we try that first. */
3941 cp_parser_parse_tentatively (parser
);
3942 /* Try a template-id. */
3943 id
= cp_parser_template_id (parser
, template_keyword_p
,
3944 /*check_dependency_p=*/true,
3946 /* If that worked, we're done. */
3947 if (cp_parser_parse_definitely (parser
))
3949 /* We still don't know whether we're looking at an
3950 operator-function-id or a conversion-function-id. */
3951 cp_parser_parse_tentatively (parser
);
3952 /* Try an operator-function-id. */
3953 id
= cp_parser_operator_function_id (parser
);
3954 /* If that didn't work, try a conversion-function-id. */
3955 if (!cp_parser_parse_definitely (parser
))
3956 id
= cp_parser_conversion_function_id (parser
);
3965 cp_parser_error (parser
, "expected unqualified-id");
3966 return error_mark_node
;
3970 /* Parse an (optional) nested-name-specifier.
3972 nested-name-specifier: [C++98]
3973 class-or-namespace-name :: nested-name-specifier [opt]
3974 class-or-namespace-name :: template nested-name-specifier [opt]
3976 nested-name-specifier: [C++0x]
3979 nested-name-specifier identifier ::
3980 nested-name-specifier template [opt] simple-template-id ::
3982 PARSER->SCOPE should be set appropriately before this function is
3983 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3984 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3987 Sets PARSER->SCOPE to the class (TYPE) or namespace
3988 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3989 it unchanged if there is no nested-name-specifier. Returns the new
3990 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3992 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3993 part of a declaration and/or decl-specifier. */
3996 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3997 bool typename_keyword_p
,
3998 bool check_dependency_p
,
4000 bool is_declaration
)
4002 bool success
= false;
4003 cp_token_position start
= 0;
4006 /* Remember where the nested-name-specifier starts. */
4007 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4009 start
= cp_lexer_token_position (parser
->lexer
, false);
4010 push_deferring_access_checks (dk_deferred
);
4017 tree saved_qualifying_scope
;
4018 bool template_keyword_p
;
4020 /* Spot cases that cannot be the beginning of a
4021 nested-name-specifier. */
4022 token
= cp_lexer_peek_token (parser
->lexer
);
4024 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4025 the already parsed nested-name-specifier. */
4026 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4028 /* Grab the nested-name-specifier and continue the loop. */
4029 cp_parser_pre_parsed_nested_name_specifier (parser
);
4030 /* If we originally encountered this nested-name-specifier
4031 with IS_DECLARATION set to false, we will not have
4032 resolved TYPENAME_TYPEs, so we must do so here. */
4034 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4036 new_scope
= resolve_typename_type (parser
->scope
,
4037 /*only_current_p=*/false);
4038 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4039 parser
->scope
= new_scope
;
4045 /* Spot cases that cannot be the beginning of a
4046 nested-name-specifier. On the second and subsequent times
4047 through the loop, we look for the `template' keyword. */
4048 if (success
&& token
->keyword
== RID_TEMPLATE
)
4050 /* A template-id can start a nested-name-specifier. */
4051 else if (token
->type
== CPP_TEMPLATE_ID
)
4055 /* If the next token is not an identifier, then it is
4056 definitely not a type-name or namespace-name. */
4057 if (token
->type
!= CPP_NAME
)
4059 /* If the following token is neither a `<' (to begin a
4060 template-id), nor a `::', then we are not looking at a
4061 nested-name-specifier. */
4062 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4063 if (token
->type
!= CPP_SCOPE
4064 && !cp_parser_nth_token_starts_template_argument_list_p
4069 /* The nested-name-specifier is optional, so we parse
4071 cp_parser_parse_tentatively (parser
);
4073 /* Look for the optional `template' keyword, if this isn't the
4074 first time through the loop. */
4076 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4078 template_keyword_p
= false;
4080 /* Save the old scope since the name lookup we are about to do
4081 might destroy it. */
4082 old_scope
= parser
->scope
;
4083 saved_qualifying_scope
= parser
->qualifying_scope
;
4084 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4085 look up names in "X<T>::I" in order to determine that "Y" is
4086 a template. So, if we have a typename at this point, we make
4087 an effort to look through it. */
4089 && !typename_keyword_p
4091 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4092 parser
->scope
= resolve_typename_type (parser
->scope
,
4093 /*only_current_p=*/false);
4094 /* Parse the qualifying entity. */
4096 = cp_parser_qualifying_entity (parser
,
4102 /* Look for the `::' token. */
4103 cp_parser_require (parser
, CPP_SCOPE
, "%<::%>");
4105 /* If we found what we wanted, we keep going; otherwise, we're
4107 if (!cp_parser_parse_definitely (parser
))
4109 bool error_p
= false;
4111 /* Restore the OLD_SCOPE since it was valid before the
4112 failed attempt at finding the last
4113 class-or-namespace-name. */
4114 parser
->scope
= old_scope
;
4115 parser
->qualifying_scope
= saved_qualifying_scope
;
4116 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4118 /* If the next token is an identifier, and the one after
4119 that is a `::', then any valid interpretation would have
4120 found a class-or-namespace-name. */
4121 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4122 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4124 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4127 token
= cp_lexer_consume_token (parser
->lexer
);
4130 if (!token
->ambiguous_p
)
4133 tree ambiguous_decls
;
4135 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4137 /*is_template=*/false,
4138 /*is_namespace=*/false,
4139 /*check_dependency=*/true,
4142 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4143 error ("%H%qD used without template parameters",
4144 &token
->location
, decl
);
4145 else if (ambiguous_decls
)
4147 error ("%Hreference to %qD is ambiguous",
4148 &token
->location
, token
->u
.value
);
4149 print_candidates (ambiguous_decls
);
4150 decl
= error_mark_node
;
4154 const char* msg
= "is not a class or namespace";
4155 if (cxx_dialect
!= cxx98
)
4156 msg
= "is not a class, namespace, or enumeration";
4157 cp_parser_name_lookup_error
4158 (parser
, token
->u
.value
, decl
, msg
,
4162 parser
->scope
= error_mark_node
;
4164 /* Treat this as a successful nested-name-specifier
4169 If the name found is not a class-name (clause
4170 _class_) or namespace-name (_namespace.def_), the
4171 program is ill-formed. */
4174 cp_lexer_consume_token (parser
->lexer
);
4178 /* We've found one valid nested-name-specifier. */
4180 /* Name lookup always gives us a DECL. */
4181 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4182 new_scope
= TREE_TYPE (new_scope
);
4183 /* Uses of "template" must be followed by actual templates. */
4184 if (template_keyword_p
4185 && !(CLASS_TYPE_P (new_scope
)
4186 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4187 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4188 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4189 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4190 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4191 == TEMPLATE_ID_EXPR
)))
4192 permerror (input_location
, TYPE_P (new_scope
)
4193 ? "%qT is not a template"
4194 : "%qD is not a template",
4196 /* If it is a class scope, try to complete it; we are about to
4197 be looking up names inside the class. */
4198 if (TYPE_P (new_scope
)
4199 /* Since checking types for dependency can be expensive,
4200 avoid doing it if the type is already complete. */
4201 && !COMPLETE_TYPE_P (new_scope
)
4202 /* Do not try to complete dependent types. */
4203 && !dependent_type_p (new_scope
))
4205 new_scope
= complete_type (new_scope
);
4206 /* If it is a typedef to current class, use the current
4207 class instead, as the typedef won't have any names inside
4209 if (!COMPLETE_TYPE_P (new_scope
)
4210 && currently_open_class (new_scope
))
4211 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4213 /* Make sure we look in the right scope the next time through
4215 parser
->scope
= new_scope
;
4218 /* If parsing tentatively, replace the sequence of tokens that makes
4219 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4220 token. That way, should we re-parse the token stream, we will
4221 not have to repeat the effort required to do the parse, nor will
4222 we issue duplicate error messages. */
4223 if (success
&& start
)
4227 token
= cp_lexer_token_at (parser
->lexer
, start
);
4228 /* Reset the contents of the START token. */
4229 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4230 /* Retrieve any deferred checks. Do not pop this access checks yet
4231 so the memory will not be reclaimed during token replacing below. */
4232 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
4233 token
->u
.tree_check_value
->value
= parser
->scope
;
4234 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4235 token
->u
.tree_check_value
->qualifying_scope
=
4236 parser
->qualifying_scope
;
4237 token
->keyword
= RID_MAX
;
4239 /* Purge all subsequent tokens. */
4240 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4244 pop_to_parent_deferring_access_checks ();
4246 return success
? parser
->scope
: NULL_TREE
;
4249 /* Parse a nested-name-specifier. See
4250 cp_parser_nested_name_specifier_opt for details. This function
4251 behaves identically, except that it will an issue an error if no
4252 nested-name-specifier is present. */
4255 cp_parser_nested_name_specifier (cp_parser
*parser
,
4256 bool typename_keyword_p
,
4257 bool check_dependency_p
,
4259 bool is_declaration
)
4263 /* Look for the nested-name-specifier. */
4264 scope
= cp_parser_nested_name_specifier_opt (parser
,
4269 /* If it was not present, issue an error message. */
4272 cp_parser_error (parser
, "expected nested-name-specifier");
4273 parser
->scope
= NULL_TREE
;
4279 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4280 this is either a class-name or a namespace-name (which corresponds
4281 to the class-or-namespace-name production in the grammar). For
4282 C++0x, it can also be a type-name that refers to an enumeration
4285 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4286 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4287 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4288 TYPE_P is TRUE iff the next name should be taken as a class-name,
4289 even the same name is declared to be another entity in the same
4292 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4293 specified by the class-or-namespace-name. If neither is found the
4294 ERROR_MARK_NODE is returned. */
4297 cp_parser_qualifying_entity (cp_parser
*parser
,
4298 bool typename_keyword_p
,
4299 bool template_keyword_p
,
4300 bool check_dependency_p
,
4302 bool is_declaration
)
4305 tree saved_qualifying_scope
;
4306 tree saved_object_scope
;
4309 bool successful_parse_p
;
4311 /* Before we try to parse the class-name, we must save away the
4312 current PARSER->SCOPE since cp_parser_class_name will destroy
4314 saved_scope
= parser
->scope
;
4315 saved_qualifying_scope
= parser
->qualifying_scope
;
4316 saved_object_scope
= parser
->object_scope
;
4317 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4318 there is no need to look for a namespace-name. */
4319 only_class_p
= template_keyword_p
4320 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
4322 cp_parser_parse_tentatively (parser
);
4323 scope
= cp_parser_class_name (parser
,
4326 type_p
? class_type
: none_type
,
4328 /*class_head_p=*/false,
4330 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
4331 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4333 && cxx_dialect
!= cxx98
4334 && !successful_parse_p
)
4336 /* Restore the saved scope. */
4337 parser
->scope
= saved_scope
;
4338 parser
->qualifying_scope
= saved_qualifying_scope
;
4339 parser
->object_scope
= saved_object_scope
;
4341 /* Parse tentatively. */
4342 cp_parser_parse_tentatively (parser
);
4344 /* Parse a typedef-name or enum-name. */
4345 scope
= cp_parser_nonclass_name (parser
);
4346 successful_parse_p
= cp_parser_parse_definitely (parser
);
4348 /* If that didn't work, try for a namespace-name. */
4349 if (!only_class_p
&& !successful_parse_p
)
4351 /* Restore the saved scope. */
4352 parser
->scope
= saved_scope
;
4353 parser
->qualifying_scope
= saved_qualifying_scope
;
4354 parser
->object_scope
= saved_object_scope
;
4355 /* If we are not looking at an identifier followed by the scope
4356 resolution operator, then this is not part of a
4357 nested-name-specifier. (Note that this function is only used
4358 to parse the components of a nested-name-specifier.) */
4359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4360 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4361 return error_mark_node
;
4362 scope
= cp_parser_namespace_name (parser
);
4368 /* Parse a postfix-expression.
4372 postfix-expression [ expression ]
4373 postfix-expression ( expression-list [opt] )
4374 simple-type-specifier ( expression-list [opt] )
4375 typename :: [opt] nested-name-specifier identifier
4376 ( expression-list [opt] )
4377 typename :: [opt] nested-name-specifier template [opt] template-id
4378 ( expression-list [opt] )
4379 postfix-expression . template [opt] id-expression
4380 postfix-expression -> template [opt] id-expression
4381 postfix-expression . pseudo-destructor-name
4382 postfix-expression -> pseudo-destructor-name
4383 postfix-expression ++
4384 postfix-expression --
4385 dynamic_cast < type-id > ( expression )
4386 static_cast < type-id > ( expression )
4387 reinterpret_cast < type-id > ( expression )
4388 const_cast < type-id > ( expression )
4389 typeid ( expression )
4395 ( type-id ) { initializer-list , [opt] }
4397 This extension is a GNU version of the C99 compound-literal
4398 construct. (The C99 grammar uses `type-name' instead of `type-id',
4399 but they are essentially the same concept.)
4401 If ADDRESS_P is true, the postfix expression is the operand of the
4402 `&' operator. CAST_P is true if this expression is the target of a
4405 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4406 class member access expressions [expr.ref].
4408 Returns a representation of the expression. */
4411 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4412 bool member_access_only_p
,
4413 cp_id_kind
* pidk_return
)
4417 cp_id_kind idk
= CP_ID_KIND_NONE
;
4418 tree postfix_expression
= NULL_TREE
;
4419 bool is_member_access
= false;
4421 /* Peek at the next token. */
4422 token
= cp_lexer_peek_token (parser
->lexer
);
4423 /* Some of the productions are determined by keywords. */
4424 keyword
= token
->keyword
;
4434 const char *saved_message
;
4436 /* All of these can be handled in the same way from the point
4437 of view of parsing. Begin by consuming the token
4438 identifying the cast. */
4439 cp_lexer_consume_token (parser
->lexer
);
4441 /* New types cannot be defined in the cast. */
4442 saved_message
= parser
->type_definition_forbidden_message
;
4443 parser
->type_definition_forbidden_message
4444 = "types may not be defined in casts";
4446 /* Look for the opening `<'. */
4447 cp_parser_require (parser
, CPP_LESS
, "%<<%>");
4448 /* Parse the type to which we are casting. */
4449 type
= cp_parser_type_id (parser
);
4450 /* Look for the closing `>'. */
4451 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
4452 /* Restore the old message. */
4453 parser
->type_definition_forbidden_message
= saved_message
;
4455 /* And the expression which is being cast. */
4456 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
4457 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
4458 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4460 /* Only type conversions to integral or enumeration types
4461 can be used in constant-expressions. */
4462 if (!cast_valid_in_integral_constant_expression_p (type
)
4463 && (cp_parser_non_integral_constant_expression
4465 "a cast to a type other than an integral or "
4466 "enumeration type")))
4467 return error_mark_node
;
4473 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
4477 = build_static_cast (type
, expression
, tf_warning_or_error
);
4481 = build_reinterpret_cast (type
, expression
,
4482 tf_warning_or_error
);
4486 = build_const_cast (type
, expression
, tf_warning_or_error
);
4497 const char *saved_message
;
4498 bool saved_in_type_id_in_expr_p
;
4500 /* Consume the `typeid' token. */
4501 cp_lexer_consume_token (parser
->lexer
);
4502 /* Look for the `(' token. */
4503 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
4504 /* Types cannot be defined in a `typeid' expression. */
4505 saved_message
= parser
->type_definition_forbidden_message
;
4506 parser
->type_definition_forbidden_message
4507 = "types may not be defined in a %<typeid%> expression";
4508 /* We can't be sure yet whether we're looking at a type-id or an
4510 cp_parser_parse_tentatively (parser
);
4511 /* Try a type-id first. */
4512 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4513 parser
->in_type_id_in_expr_p
= true;
4514 type
= cp_parser_type_id (parser
);
4515 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4516 /* Look for the `)' token. Otherwise, we can't be sure that
4517 we're not looking at an expression: consider `typeid (int
4518 (3))', for example. */
4519 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4520 /* If all went well, simply lookup the type-id. */
4521 if (cp_parser_parse_definitely (parser
))
4522 postfix_expression
= get_typeid (type
);
4523 /* Otherwise, fall back to the expression variant. */
4528 /* Look for an expression. */
4529 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
4530 /* Compute its typeid. */
4531 postfix_expression
= build_typeid (expression
);
4532 /* Look for the `)' token. */
4533 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4535 /* Restore the saved message. */
4536 parser
->type_definition_forbidden_message
= saved_message
;
4537 /* `typeid' may not appear in an integral constant expression. */
4538 if (cp_parser_non_integral_constant_expression(parser
,
4539 "%<typeid%> operator"))
4540 return error_mark_node
;
4547 /* The syntax permitted here is the same permitted for an
4548 elaborated-type-specifier. */
4549 type
= cp_parser_elaborated_type_specifier (parser
,
4550 /*is_friend=*/false,
4551 /*is_declaration=*/false);
4552 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4560 /* If the next thing is a simple-type-specifier, we may be
4561 looking at a functional cast. We could also be looking at
4562 an id-expression. So, we try the functional cast, and if
4563 that doesn't work we fall back to the primary-expression. */
4564 cp_parser_parse_tentatively (parser
);
4565 /* Look for the simple-type-specifier. */
4566 type
= cp_parser_simple_type_specifier (parser
,
4567 /*decl_specs=*/NULL
,
4568 CP_PARSER_FLAGS_NONE
);
4569 /* Parse the cast itself. */
4570 if (!cp_parser_error_occurred (parser
))
4572 = cp_parser_functional_cast (parser
, type
);
4573 /* If that worked, we're done. */
4574 if (cp_parser_parse_definitely (parser
))
4577 /* If the functional-cast didn't work out, try a
4578 compound-literal. */
4579 if (cp_parser_allow_gnu_extensions_p (parser
)
4580 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4582 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4583 bool saved_in_type_id_in_expr_p
;
4585 cp_parser_parse_tentatively (parser
);
4586 /* Consume the `('. */
4587 cp_lexer_consume_token (parser
->lexer
);
4588 /* Parse the type. */
4589 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4590 parser
->in_type_id_in_expr_p
= true;
4591 type
= cp_parser_type_id (parser
);
4592 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4593 /* Look for the `)'. */
4594 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4595 /* Look for the `{'. */
4596 cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>");
4597 /* If things aren't going well, there's no need to
4599 if (!cp_parser_error_occurred (parser
))
4601 bool non_constant_p
;
4602 /* Parse the initializer-list. */
4604 = cp_parser_initializer_list (parser
, &non_constant_p
);
4605 /* Allow a trailing `,'. */
4606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4607 cp_lexer_consume_token (parser
->lexer
);
4608 /* Look for the final `}'. */
4609 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
4611 /* If that worked, we're definitely looking at a
4612 compound-literal expression. */
4613 if (cp_parser_parse_definitely (parser
))
4615 /* Warn the user that a compound literal is not
4616 allowed in standard C++. */
4617 pedwarn (input_location
, OPT_pedantic
, "ISO C++ forbids compound-literals");
4618 /* For simplicity, we disallow compound literals in
4619 constant-expressions. We could
4620 allow compound literals of integer type, whose
4621 initializer was a constant, in constant
4622 expressions. Permitting that usage, as a further
4623 extension, would not change the meaning of any
4624 currently accepted programs. (Of course, as
4625 compound literals are not part of ISO C++, the
4626 standard has nothing to say.) */
4627 if (cp_parser_non_integral_constant_expression
4628 (parser
, "non-constant compound literals"))
4630 postfix_expression
= error_mark_node
;
4633 /* Form the representation of the compound-literal. */
4635 = (finish_compound_literal
4636 (type
, build_constructor (init_list_type_node
,
4637 initializer_list
)));
4642 /* It must be a primary-expression. */
4644 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4645 /*template_arg_p=*/false,
4651 /* Keep looping until the postfix-expression is complete. */
4654 if (idk
== CP_ID_KIND_UNQUALIFIED
4655 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4656 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4657 /* It is not a Koenig lookup function call. */
4659 = unqualified_name_lookup_error (postfix_expression
);
4661 /* Peek at the next token. */
4662 token
= cp_lexer_peek_token (parser
->lexer
);
4664 switch (token
->type
)
4666 case CPP_OPEN_SQUARE
:
4668 = cp_parser_postfix_open_square_expression (parser
,
4671 idk
= CP_ID_KIND_NONE
;
4672 is_member_access
= false;
4675 case CPP_OPEN_PAREN
:
4676 /* postfix-expression ( expression-list [opt] ) */
4679 bool is_builtin_constant_p
;
4680 bool saved_integral_constant_expression_p
= false;
4681 bool saved_non_integral_constant_expression_p
= false;
4684 is_member_access
= false;
4686 is_builtin_constant_p
4687 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4688 if (is_builtin_constant_p
)
4690 /* The whole point of __builtin_constant_p is to allow
4691 non-constant expressions to appear as arguments. */
4692 saved_integral_constant_expression_p
4693 = parser
->integral_constant_expression_p
;
4694 saved_non_integral_constant_expression_p
4695 = parser
->non_integral_constant_expression_p
;
4696 parser
->integral_constant_expression_p
= false;
4698 args
= (cp_parser_parenthesized_expression_list
4699 (parser
, /*is_attribute_list=*/false,
4700 /*cast_p=*/false, /*allow_expansion_p=*/true,
4701 /*non_constant_p=*/NULL
));
4702 if (is_builtin_constant_p
)
4704 parser
->integral_constant_expression_p
4705 = saved_integral_constant_expression_p
;
4706 parser
->non_integral_constant_expression_p
4707 = saved_non_integral_constant_expression_p
;
4710 if (args
== error_mark_node
)
4712 postfix_expression
= error_mark_node
;
4716 /* Function calls are not permitted in
4717 constant-expressions. */
4718 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4719 && cp_parser_non_integral_constant_expression (parser
,
4722 postfix_expression
= error_mark_node
;
4727 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4729 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4735 = perform_koenig_lookup (postfix_expression
, args
);
4739 = unqualified_fn_lookup_error (postfix_expression
);
4741 /* We do not perform argument-dependent lookup if
4742 normal lookup finds a non-function, in accordance
4743 with the expected resolution of DR 218. */
4744 else if (args
&& is_overloaded_fn (postfix_expression
))
4746 tree fn
= get_first_fn (postfix_expression
);
4748 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4749 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4751 /* Only do argument dependent lookup if regular
4752 lookup does not find a set of member functions.
4753 [basic.lookup.koenig]/2a */
4754 if (!DECL_FUNCTION_MEMBER_P (fn
))
4758 = perform_koenig_lookup (postfix_expression
, args
);
4763 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4765 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4766 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4768 if (processing_template_decl
4769 && (type_dependent_expression_p (instance
)
4770 || (!BASELINK_P (fn
)
4771 && TREE_CODE (fn
) != FIELD_DECL
)
4772 || type_dependent_expression_p (fn
)
4773 || any_type_dependent_arguments_p (args
)))
4776 = build_nt_call_list (postfix_expression
, args
);
4780 if (BASELINK_P (fn
))
4783 = (build_new_method_call
4784 (instance
, fn
, args
, NULL_TREE
,
4785 (idk
== CP_ID_KIND_QUALIFIED
4786 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4788 tf_warning_or_error
));
4792 = finish_call_expr (postfix_expression
, args
,
4793 /*disallow_virtual=*/false,
4795 tf_warning_or_error
);
4797 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4798 || TREE_CODE (postfix_expression
) == MEMBER_REF
4799 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4800 postfix_expression
= (build_offset_ref_call_from_tree
4801 (postfix_expression
, args
));
4802 else if (idk
== CP_ID_KIND_QUALIFIED
)
4803 /* A call to a static class member, or a namespace-scope
4806 = finish_call_expr (postfix_expression
, args
,
4807 /*disallow_virtual=*/true,
4809 tf_warning_or_error
);
4811 /* All other function calls. */
4813 = finish_call_expr (postfix_expression
, args
,
4814 /*disallow_virtual=*/false,
4816 tf_warning_or_error
);
4818 if (warn_disallowed_functions
)
4819 warn_if_disallowed_function_p (postfix_expression
);
4821 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4822 idk
= CP_ID_KIND_NONE
;
4828 /* postfix-expression . template [opt] id-expression
4829 postfix-expression . pseudo-destructor-name
4830 postfix-expression -> template [opt] id-expression
4831 postfix-expression -> pseudo-destructor-name */
4833 /* Consume the `.' or `->' operator. */
4834 cp_lexer_consume_token (parser
->lexer
);
4837 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4842 is_member_access
= true;
4846 /* postfix-expression ++ */
4847 /* Consume the `++' token. */
4848 cp_lexer_consume_token (parser
->lexer
);
4849 /* Generate a representation for the complete expression. */
4851 = finish_increment_expr (postfix_expression
,
4852 POSTINCREMENT_EXPR
);
4853 /* Increments may not appear in constant-expressions. */
4854 if (cp_parser_non_integral_constant_expression (parser
,
4856 postfix_expression
= error_mark_node
;
4857 idk
= CP_ID_KIND_NONE
;
4858 is_member_access
= false;
4861 case CPP_MINUS_MINUS
:
4862 /* postfix-expression -- */
4863 /* Consume the `--' token. */
4864 cp_lexer_consume_token (parser
->lexer
);
4865 /* Generate a representation for the complete expression. */
4867 = finish_increment_expr (postfix_expression
,
4868 POSTDECREMENT_EXPR
);
4869 /* Decrements may not appear in constant-expressions. */
4870 if (cp_parser_non_integral_constant_expression (parser
,
4872 postfix_expression
= error_mark_node
;
4873 idk
= CP_ID_KIND_NONE
;
4874 is_member_access
= false;
4878 if (pidk_return
!= NULL
)
4879 * pidk_return
= idk
;
4880 if (member_access_only_p
)
4881 return is_member_access
? postfix_expression
: error_mark_node
;
4883 return postfix_expression
;
4887 /* We should never get here. */
4889 return error_mark_node
;
4892 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4893 by cp_parser_builtin_offsetof. We're looking for
4895 postfix-expression [ expression ]
4897 FOR_OFFSETOF is set if we're being called in that context, which
4898 changes how we deal with integer constant expressions. */
4901 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4902 tree postfix_expression
,
4907 /* Consume the `[' token. */
4908 cp_lexer_consume_token (parser
->lexer
);
4910 /* Parse the index expression. */
4911 /* ??? For offsetof, there is a question of what to allow here. If
4912 offsetof is not being used in an integral constant expression context,
4913 then we *could* get the right answer by computing the value at runtime.
4914 If we are in an integral constant expression context, then we might
4915 could accept any constant expression; hard to say without analysis.
4916 Rather than open the barn door too wide right away, allow only integer
4917 constant expressions here. */
4919 index
= cp_parser_constant_expression (parser
, false, NULL
);
4921 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
4923 /* Look for the closing `]'. */
4924 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
4926 /* Build the ARRAY_REF. */
4927 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4929 /* When not doing offsetof, array references are not permitted in
4930 constant-expressions. */
4932 && (cp_parser_non_integral_constant_expression
4933 (parser
, "an array reference")))
4934 postfix_expression
= error_mark_node
;
4936 return postfix_expression
;
4939 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4940 by cp_parser_builtin_offsetof. We're looking for
4942 postfix-expression . template [opt] id-expression
4943 postfix-expression . pseudo-destructor-name
4944 postfix-expression -> template [opt] id-expression
4945 postfix-expression -> pseudo-destructor-name
4947 FOR_OFFSETOF is set if we're being called in that context. That sorta
4948 limits what of the above we'll actually accept, but nevermind.
4949 TOKEN_TYPE is the "." or "->" token, which will already have been
4950 removed from the stream. */
4953 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4954 enum cpp_ttype token_type
,
4955 tree postfix_expression
,
4956 bool for_offsetof
, cp_id_kind
*idk
,
4957 location_t location
)
4961 bool pseudo_destructor_p
;
4962 tree scope
= NULL_TREE
;
4964 /* If this is a `->' operator, dereference the pointer. */
4965 if (token_type
== CPP_DEREF
)
4966 postfix_expression
= build_x_arrow (postfix_expression
);
4967 /* Check to see whether or not the expression is type-dependent. */
4968 dependent_p
= type_dependent_expression_p (postfix_expression
);
4969 /* The identifier following the `->' or `.' is not qualified. */
4970 parser
->scope
= NULL_TREE
;
4971 parser
->qualifying_scope
= NULL_TREE
;
4972 parser
->object_scope
= NULL_TREE
;
4973 *idk
= CP_ID_KIND_NONE
;
4975 /* Enter the scope corresponding to the type of the object
4976 given by the POSTFIX_EXPRESSION. */
4977 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4979 scope
= TREE_TYPE (postfix_expression
);
4980 /* According to the standard, no expression should ever have
4981 reference type. Unfortunately, we do not currently match
4982 the standard in this respect in that our internal representation
4983 of an expression may have reference type even when the standard
4984 says it does not. Therefore, we have to manually obtain the
4985 underlying type here. */
4986 scope
= non_reference (scope
);
4987 /* The type of the POSTFIX_EXPRESSION must be complete. */
4988 if (scope
== unknown_type_node
)
4990 error ("%H%qE does not have class type", &location
, postfix_expression
);
4994 scope
= complete_type_or_else (scope
, NULL_TREE
);
4995 /* Let the name lookup machinery know that we are processing a
4996 class member access expression. */
4997 parser
->context
->object_type
= scope
;
4998 /* If something went wrong, we want to be able to discern that case,
4999 as opposed to the case where there was no SCOPE due to the type
5000 of expression being dependent. */
5002 scope
= error_mark_node
;
5003 /* If the SCOPE was erroneous, make the various semantic analysis
5004 functions exit quickly -- and without issuing additional error
5006 if (scope
== error_mark_node
)
5007 postfix_expression
= error_mark_node
;
5010 /* Assume this expression is not a pseudo-destructor access. */
5011 pseudo_destructor_p
= false;
5013 /* If the SCOPE is a scalar type, then, if this is a valid program,
5014 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5015 is type dependent, it can be pseudo-destructor-name or something else.
5016 Try to parse it as pseudo-destructor-name first. */
5017 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
5022 cp_parser_parse_tentatively (parser
);
5023 /* Parse the pseudo-destructor-name. */
5025 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
5027 && (cp_parser_error_occurred (parser
)
5028 || TREE_CODE (type
) != TYPE_DECL
5029 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
5030 cp_parser_abort_tentative_parse (parser
);
5031 else if (cp_parser_parse_definitely (parser
))
5033 pseudo_destructor_p
= true;
5035 = finish_pseudo_destructor_expr (postfix_expression
,
5036 s
, TREE_TYPE (type
));
5040 if (!pseudo_destructor_p
)
5042 /* If the SCOPE is not a scalar type, we are looking at an
5043 ordinary class member access expression, rather than a
5044 pseudo-destructor-name. */
5046 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5047 /* Parse the id-expression. */
5048 name
= (cp_parser_id_expression
5050 cp_parser_optional_template_keyword (parser
),
5051 /*check_dependency_p=*/true,
5053 /*declarator_p=*/false,
5054 /*optional_p=*/false));
5055 /* In general, build a SCOPE_REF if the member name is qualified.
5056 However, if the name was not dependent and has already been
5057 resolved; there is no need to build the SCOPE_REF. For example;
5059 struct X { void f(); };
5060 template <typename T> void f(T* t) { t->X::f(); }
5062 Even though "t" is dependent, "X::f" is not and has been resolved
5063 to a BASELINK; there is no need to include scope information. */
5065 /* But we do need to remember that there was an explicit scope for
5066 virtual function calls. */
5068 *idk
= CP_ID_KIND_QUALIFIED
;
5070 /* If the name is a template-id that names a type, we will get a
5071 TYPE_DECL here. That is invalid code. */
5072 if (TREE_CODE (name
) == TYPE_DECL
)
5074 error ("%Hinvalid use of %qD", &token
->location
, name
);
5075 postfix_expression
= error_mark_node
;
5079 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
5081 name
= build_qualified_name (/*type=*/NULL_TREE
,
5085 parser
->scope
= NULL_TREE
;
5086 parser
->qualifying_scope
= NULL_TREE
;
5087 parser
->object_scope
= NULL_TREE
;
5089 if (scope
&& name
&& BASELINK_P (name
))
5090 adjust_result_of_qualified_name_lookup
5091 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
5093 = finish_class_member_access_expr (postfix_expression
, name
,
5095 tf_warning_or_error
);
5099 /* We no longer need to look up names in the scope of the object on
5100 the left-hand side of the `.' or `->' operator. */
5101 parser
->context
->object_type
= NULL_TREE
;
5103 /* Outside of offsetof, these operators may not appear in
5104 constant-expressions. */
5106 && (cp_parser_non_integral_constant_expression
5107 (parser
, token_type
== CPP_DEREF
? "%<->%>" : "%<.%>")))
5108 postfix_expression
= error_mark_node
;
5110 return postfix_expression
;
5113 /* Parse a parenthesized expression-list.
5116 assignment-expression
5117 expression-list, assignment-expression
5122 identifier, expression-list
5124 CAST_P is true if this expression is the target of a cast.
5126 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5129 Returns a TREE_LIST. The TREE_VALUE of each node is a
5130 representation of an assignment-expression. Note that a TREE_LIST
5131 is returned even if there is only a single expression in the list.
5132 error_mark_node is returned if the ( and or ) are
5133 missing. NULL_TREE is returned on no expressions. The parentheses
5134 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5135 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5136 indicates whether or not all of the expressions in the list were
5140 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
5141 bool is_attribute_list
,
5143 bool allow_expansion_p
,
5144 bool *non_constant_p
)
5146 tree expression_list
= NULL_TREE
;
5147 bool fold_expr_p
= is_attribute_list
;
5148 tree identifier
= NULL_TREE
;
5149 bool saved_greater_than_is_operator_p
;
5151 /* Assume all the expressions will be constant. */
5153 *non_constant_p
= false;
5155 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
5156 return error_mark_node
;
5158 /* Within a parenthesized expression, a `>' token is always
5159 the greater-than operator. */
5160 saved_greater_than_is_operator_p
5161 = parser
->greater_than_is_operator_p
;
5162 parser
->greater_than_is_operator_p
= true;
5164 /* Consume expressions until there are no more. */
5165 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5170 /* At the beginning of attribute lists, check to see if the
5171 next token is an identifier. */
5172 if (is_attribute_list
5173 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5177 /* Consume the identifier. */
5178 token
= cp_lexer_consume_token (parser
->lexer
);
5179 /* Save the identifier. */
5180 identifier
= token
->u
.value
;
5184 bool expr_non_constant_p
;
5186 /* Parse the next assignment-expression. */
5187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5189 /* A braced-init-list. */
5190 maybe_warn_cpp0x ("extended initializer lists");
5191 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
5192 if (non_constant_p
&& expr_non_constant_p
)
5193 *non_constant_p
= true;
5195 else if (non_constant_p
)
5197 expr
= (cp_parser_constant_expression
5198 (parser
, /*allow_non_constant_p=*/true,
5199 &expr_non_constant_p
));
5200 if (expr_non_constant_p
)
5201 *non_constant_p
= true;
5204 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
5207 expr
= fold_non_dependent_expr (expr
);
5209 /* If we have an ellipsis, then this is an expression
5211 if (allow_expansion_p
5212 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5214 /* Consume the `...'. */
5215 cp_lexer_consume_token (parser
->lexer
);
5217 /* Build the argument pack. */
5218 expr
= make_pack_expansion (expr
);
5221 /* Add it to the list. We add error_mark_node
5222 expressions to the list, so that we can still tell if
5223 the correct form for a parenthesized expression-list
5224 is found. That gives better errors. */
5225 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
5227 if (expr
== error_mark_node
)
5231 /* After the first item, attribute lists look the same as
5232 expression lists. */
5233 is_attribute_list
= false;
5236 /* If the next token isn't a `,', then we are done. */
5237 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5240 /* Otherwise, consume the `,' and keep going. */
5241 cp_lexer_consume_token (parser
->lexer
);
5244 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
5249 /* We try and resync to an unnested comma, as that will give the
5250 user better diagnostics. */
5251 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5252 /*recovering=*/true,
5254 /*consume_paren=*/true);
5259 parser
->greater_than_is_operator_p
5260 = saved_greater_than_is_operator_p
;
5261 return error_mark_node
;
5265 parser
->greater_than_is_operator_p
5266 = saved_greater_than_is_operator_p
;
5268 /* We built up the list in reverse order so we must reverse it now. */
5269 expression_list
= nreverse (expression_list
);
5271 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
5273 return expression_list
;
5276 /* Parse a pseudo-destructor-name.
5278 pseudo-destructor-name:
5279 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5280 :: [opt] nested-name-specifier template template-id :: ~ type-name
5281 :: [opt] nested-name-specifier [opt] ~ type-name
5283 If either of the first two productions is used, sets *SCOPE to the
5284 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5285 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5286 or ERROR_MARK_NODE if the parse fails. */
5289 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5293 bool nested_name_specifier_p
;
5295 /* Assume that things will not work out. */
5296 *type
= error_mark_node
;
5298 /* Look for the optional `::' operator. */
5299 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5300 /* Look for the optional nested-name-specifier. */
5301 nested_name_specifier_p
5302 = (cp_parser_nested_name_specifier_opt (parser
,
5303 /*typename_keyword_p=*/false,
5304 /*check_dependency_p=*/true,
5306 /*is_declaration=*/false)
5308 /* Now, if we saw a nested-name-specifier, we might be doing the
5309 second production. */
5310 if (nested_name_specifier_p
5311 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5313 /* Consume the `template' keyword. */
5314 cp_lexer_consume_token (parser
->lexer
);
5315 /* Parse the template-id. */
5316 cp_parser_template_id (parser
,
5317 /*template_keyword_p=*/true,
5318 /*check_dependency_p=*/false,
5319 /*is_declaration=*/true);
5320 /* Look for the `::' token. */
5321 cp_parser_require (parser
, CPP_SCOPE
, "%<::%>");
5323 /* If the next token is not a `~', then there might be some
5324 additional qualification. */
5325 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5327 /* At this point, we're looking for "type-name :: ~". The type-name
5328 must not be a class-name, since this is a pseudo-destructor. So,
5329 it must be either an enum-name, or a typedef-name -- both of which
5330 are just identifiers. So, we peek ahead to check that the "::"
5331 and "~" tokens are present; if they are not, then we can avoid
5332 calling type_name. */
5333 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
5334 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
5335 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
5337 cp_parser_error (parser
, "non-scalar type");
5341 /* Look for the type-name. */
5342 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
5343 if (*scope
== error_mark_node
)
5346 /* Look for the `::' token. */
5347 cp_parser_require (parser
, CPP_SCOPE
, "%<::%>");
5352 /* Look for the `~'. */
5353 cp_parser_require (parser
, CPP_COMPL
, "%<~%>");
5354 /* Look for the type-name again. We are not responsible for
5355 checking that it matches the first type-name. */
5356 *type
= cp_parser_nonclass_name (parser
);
5359 /* Parse a unary-expression.
5365 unary-operator cast-expression
5366 sizeof unary-expression
5374 __extension__ cast-expression
5375 __alignof__ unary-expression
5376 __alignof__ ( type-id )
5377 __real__ cast-expression
5378 __imag__ cast-expression
5381 ADDRESS_P is true iff the unary-expression is appearing as the
5382 operand of the `&' operator. CAST_P is true if this expression is
5383 the target of a cast.
5385 Returns a representation of the expression. */
5388 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5392 enum tree_code unary_operator
;
5394 /* Peek at the next token. */
5395 token
= cp_lexer_peek_token (parser
->lexer
);
5396 /* Some keywords give away the kind of expression. */
5397 if (token
->type
== CPP_KEYWORD
)
5399 enum rid keyword
= token
->keyword
;
5409 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5410 /* Consume the token. */
5411 cp_lexer_consume_token (parser
->lexer
);
5412 /* Parse the operand. */
5413 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5415 if (TYPE_P (operand
))
5416 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5418 return cxx_sizeof_or_alignof_expr (operand
, op
, true);
5422 return cp_parser_new_expression (parser
);
5425 return cp_parser_delete_expression (parser
);
5429 /* The saved value of the PEDANTIC flag. */
5433 /* Save away the PEDANTIC flag. */
5434 cp_parser_extension_opt (parser
, &saved_pedantic
);
5435 /* Parse the cast-expression. */
5436 expr
= cp_parser_simple_cast_expression (parser
);
5437 /* Restore the PEDANTIC flag. */
5438 pedantic
= saved_pedantic
;
5448 /* Consume the `__real__' or `__imag__' token. */
5449 cp_lexer_consume_token (parser
->lexer
);
5450 /* Parse the cast-expression. */
5451 expression
= cp_parser_simple_cast_expression (parser
);
5452 /* Create the complete representation. */
5453 return build_x_unary_op ((keyword
== RID_REALPART
5454 ? REALPART_EXPR
: IMAGPART_EXPR
),
5456 tf_warning_or_error
);
5465 /* Look for the `:: new' and `:: delete', which also signal the
5466 beginning of a new-expression, or delete-expression,
5467 respectively. If the next token is `::', then it might be one of
5469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5473 /* See if the token after the `::' is one of the keywords in
5474 which we're interested. */
5475 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5476 /* If it's `new', we have a new-expression. */
5477 if (keyword
== RID_NEW
)
5478 return cp_parser_new_expression (parser
);
5479 /* Similarly, for `delete'. */
5480 else if (keyword
== RID_DELETE
)
5481 return cp_parser_delete_expression (parser
);
5484 /* Look for a unary operator. */
5485 unary_operator
= cp_parser_unary_operator (token
);
5486 /* The `++' and `--' operators can be handled similarly, even though
5487 they are not technically unary-operators in the grammar. */
5488 if (unary_operator
== ERROR_MARK
)
5490 if (token
->type
== CPP_PLUS_PLUS
)
5491 unary_operator
= PREINCREMENT_EXPR
;
5492 else if (token
->type
== CPP_MINUS_MINUS
)
5493 unary_operator
= PREDECREMENT_EXPR
;
5494 /* Handle the GNU address-of-label extension. */
5495 else if (cp_parser_allow_gnu_extensions_p (parser
)
5496 && token
->type
== CPP_AND_AND
)
5500 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5502 /* Consume the '&&' token. */
5503 cp_lexer_consume_token (parser
->lexer
);
5504 /* Look for the identifier. */
5505 identifier
= cp_parser_identifier (parser
);
5506 /* Create an expression representing the address. */
5507 expression
= finish_label_address_expr (identifier
, loc
);
5508 if (cp_parser_non_integral_constant_expression (parser
,
5509 "the address of a label"))
5510 expression
= error_mark_node
;
5514 if (unary_operator
!= ERROR_MARK
)
5516 tree cast_expression
;
5517 tree expression
= error_mark_node
;
5518 const char *non_constant_p
= NULL
;
5520 /* Consume the operator token. */
5521 token
= cp_lexer_consume_token (parser
->lexer
);
5522 /* Parse the cast-expression. */
5524 = cp_parser_cast_expression (parser
,
5525 unary_operator
== ADDR_EXPR
,
5526 /*cast_p=*/false, pidk
);
5527 /* Now, build an appropriate representation. */
5528 switch (unary_operator
)
5531 non_constant_p
= "%<*%>";
5532 expression
= build_x_indirect_ref (cast_expression
, "unary *",
5533 tf_warning_or_error
);
5537 non_constant_p
= "%<&%>";
5540 expression
= build_x_unary_op (unary_operator
, cast_expression
,
5541 tf_warning_or_error
);
5544 case PREINCREMENT_EXPR
:
5545 case PREDECREMENT_EXPR
:
5546 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5547 ? "%<++%>" : "%<--%>");
5549 case UNARY_PLUS_EXPR
:
5551 case TRUTH_NOT_EXPR
:
5552 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5560 && cp_parser_non_integral_constant_expression (parser
,
5562 expression
= error_mark_node
;
5567 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
5568 /*member_access_only_p=*/false,
5572 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5573 unary-operator, the corresponding tree code is returned. */
5575 static enum tree_code
5576 cp_parser_unary_operator (cp_token
* token
)
5578 switch (token
->type
)
5581 return INDIRECT_REF
;
5587 return UNARY_PLUS_EXPR
;
5593 return TRUTH_NOT_EXPR
;
5596 return BIT_NOT_EXPR
;
5603 /* Parse a new-expression.
5606 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5607 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5609 Returns a representation of the expression. */
5612 cp_parser_new_expression (cp_parser
* parser
)
5614 bool global_scope_p
;
5620 /* Look for the optional `::' operator. */
5622 = (cp_parser_global_scope_opt (parser
,
5623 /*current_scope_valid_p=*/false)
5625 /* Look for the `new' operator. */
5626 cp_parser_require_keyword (parser
, RID_NEW
, "%<new%>");
5627 /* There's no easy way to tell a new-placement from the
5628 `( type-id )' construct. */
5629 cp_parser_parse_tentatively (parser
);
5630 /* Look for a new-placement. */
5631 placement
= cp_parser_new_placement (parser
);
5632 /* If that didn't work out, there's no new-placement. */
5633 if (!cp_parser_parse_definitely (parser
))
5634 placement
= NULL_TREE
;
5636 /* If the next token is a `(', then we have a parenthesized
5638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5641 /* Consume the `('. */
5642 cp_lexer_consume_token (parser
->lexer
);
5643 /* Parse the type-id. */
5644 type
= cp_parser_type_id (parser
);
5645 /* Look for the closing `)'. */
5646 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
5647 token
= cp_lexer_peek_token (parser
->lexer
);
5648 /* There should not be a direct-new-declarator in this production,
5649 but GCC used to allowed this, so we check and emit a sensible error
5650 message for this case. */
5651 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5653 error ("%Harray bound forbidden after parenthesized type-id",
5655 inform (token
->location
,
5656 "try removing the parentheses around the type-id");
5657 cp_parser_direct_new_declarator (parser
);
5661 /* Otherwise, there must be a new-type-id. */
5663 type
= cp_parser_new_type_id (parser
, &nelts
);
5665 /* If the next token is a `(' or '{', then we have a new-initializer. */
5666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
5667 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5668 initializer
= cp_parser_new_initializer (parser
);
5670 initializer
= NULL_TREE
;
5672 /* A new-expression may not appear in an integral constant
5674 if (cp_parser_non_integral_constant_expression (parser
, "%<new%>"))
5675 return error_mark_node
;
5677 /* Create a representation of the new-expression. */
5678 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
,
5679 tf_warning_or_error
);
5682 /* Parse a new-placement.
5687 Returns the same representation as for an expression-list. */
5690 cp_parser_new_placement (cp_parser
* parser
)
5692 tree expression_list
;
5694 /* Parse the expression-list. */
5695 expression_list
= (cp_parser_parenthesized_expression_list
5696 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5697 /*non_constant_p=*/NULL
));
5699 return expression_list
;
5702 /* Parse a new-type-id.
5705 type-specifier-seq new-declarator [opt]
5707 Returns the TYPE allocated. If the new-type-id indicates an array
5708 type, *NELTS is set to the number of elements in the last array
5709 bound; the TYPE will not include the last array bound. */
5712 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5714 cp_decl_specifier_seq type_specifier_seq
;
5715 cp_declarator
*new_declarator
;
5716 cp_declarator
*declarator
;
5717 cp_declarator
*outer_declarator
;
5718 const char *saved_message
;
5721 /* The type-specifier sequence must not contain type definitions.
5722 (It cannot contain declarations of new types either, but if they
5723 are not definitions we will catch that because they are not
5725 saved_message
= parser
->type_definition_forbidden_message
;
5726 parser
->type_definition_forbidden_message
5727 = "types may not be defined in a new-type-id";
5728 /* Parse the type-specifier-seq. */
5729 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5730 &type_specifier_seq
);
5731 /* Restore the old message. */
5732 parser
->type_definition_forbidden_message
= saved_message
;
5733 /* Parse the new-declarator. */
5734 new_declarator
= cp_parser_new_declarator_opt (parser
);
5736 /* Determine the number of elements in the last array dimension, if
5739 /* Skip down to the last array dimension. */
5740 declarator
= new_declarator
;
5741 outer_declarator
= NULL
;
5742 while (declarator
&& (declarator
->kind
== cdk_pointer
5743 || declarator
->kind
== cdk_ptrmem
))
5745 outer_declarator
= declarator
;
5746 declarator
= declarator
->declarator
;
5749 && declarator
->kind
== cdk_array
5750 && declarator
->declarator
5751 && declarator
->declarator
->kind
== cdk_array
)
5753 outer_declarator
= declarator
;
5754 declarator
= declarator
->declarator
;
5757 if (declarator
&& declarator
->kind
== cdk_array
)
5759 *nelts
= declarator
->u
.array
.bounds
;
5760 if (*nelts
== error_mark_node
)
5761 *nelts
= integer_one_node
;
5763 if (outer_declarator
)
5764 outer_declarator
->declarator
= declarator
->declarator
;
5766 new_declarator
= NULL
;
5769 type
= groktypename (&type_specifier_seq
, new_declarator
);
5773 /* Parse an (optional) new-declarator.
5776 ptr-operator new-declarator [opt]
5777 direct-new-declarator
5779 Returns the declarator. */
5781 static cp_declarator
*
5782 cp_parser_new_declarator_opt (cp_parser
* parser
)
5784 enum tree_code code
;
5786 cp_cv_quals cv_quals
;
5788 /* We don't know if there's a ptr-operator next, or not. */
5789 cp_parser_parse_tentatively (parser
);
5790 /* Look for a ptr-operator. */
5791 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5792 /* If that worked, look for more new-declarators. */
5793 if (cp_parser_parse_definitely (parser
))
5795 cp_declarator
*declarator
;
5797 /* Parse another optional declarator. */
5798 declarator
= cp_parser_new_declarator_opt (parser
);
5800 return cp_parser_make_indirect_declarator
5801 (code
, type
, cv_quals
, declarator
);
5804 /* If the next token is a `[', there is a direct-new-declarator. */
5805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5806 return cp_parser_direct_new_declarator (parser
);
5811 /* Parse a direct-new-declarator.
5813 direct-new-declarator:
5815 direct-new-declarator [constant-expression]
5819 static cp_declarator
*
5820 cp_parser_direct_new_declarator (cp_parser
* parser
)
5822 cp_declarator
*declarator
= NULL
;
5828 /* Look for the opening `['. */
5829 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "%<[%>");
5830 /* The first expression is not required to be constant. */
5833 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5834 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5835 /* The standard requires that the expression have integral
5836 type. DR 74 adds enumeration types. We believe that the
5837 real intent is that these expressions be handled like the
5838 expression in a `switch' condition, which also allows
5839 classes with a single conversion to integral or
5840 enumeration type. */
5841 if (!processing_template_decl
)
5844 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5849 error ("%Hexpression in new-declarator must have integral "
5850 "or enumeration type", &token
->location
);
5851 expression
= error_mark_node
;
5855 /* But all the other expressions must be. */
5858 = cp_parser_constant_expression (parser
,
5859 /*allow_non_constant=*/false,
5861 /* Look for the closing `]'. */
5862 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
5864 /* Add this bound to the declarator. */
5865 declarator
= make_array_declarator (declarator
, expression
);
5867 /* If the next token is not a `[', then there are no more
5869 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5876 /* Parse a new-initializer.
5879 ( expression-list [opt] )
5882 Returns a representation of the expression-list. If there is no
5883 expression-list, VOID_ZERO_NODE is returned. */
5886 cp_parser_new_initializer (cp_parser
* parser
)
5888 tree expression_list
;
5890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5892 bool expr_non_constant_p
;
5893 maybe_warn_cpp0x ("extended initializer lists");
5894 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
5895 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
5896 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
5899 expression_list
= (cp_parser_parenthesized_expression_list
5900 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5901 /*non_constant_p=*/NULL
));
5902 if (!expression_list
)
5903 expression_list
= void_zero_node
;
5905 return expression_list
;
5908 /* Parse a delete-expression.
5911 :: [opt] delete cast-expression
5912 :: [opt] delete [ ] cast-expression
5914 Returns a representation of the expression. */
5917 cp_parser_delete_expression (cp_parser
* parser
)
5919 bool global_scope_p
;
5923 /* Look for the optional `::' operator. */
5925 = (cp_parser_global_scope_opt (parser
,
5926 /*current_scope_valid_p=*/false)
5928 /* Look for the `delete' keyword. */
5929 cp_parser_require_keyword (parser
, RID_DELETE
, "%<delete%>");
5930 /* See if the array syntax is in use. */
5931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5933 /* Consume the `[' token. */
5934 cp_lexer_consume_token (parser
->lexer
);
5935 /* Look for the `]' token. */
5936 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
5937 /* Remember that this is the `[]' construct. */
5943 /* Parse the cast-expression. */
5944 expression
= cp_parser_simple_cast_expression (parser
);
5946 /* A delete-expression may not appear in an integral constant
5948 if (cp_parser_non_integral_constant_expression (parser
, "%<delete%>"))
5949 return error_mark_node
;
5951 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5954 /* Returns true if TOKEN may start a cast-expression and false
5958 cp_parser_token_starts_cast_expression (cp_token
*token
)
5960 switch (token
->type
)
5966 case CPP_CLOSE_SQUARE
:
5967 case CPP_CLOSE_PAREN
:
5968 case CPP_CLOSE_BRACE
:
5972 case CPP_DEREF_STAR
:
5980 case CPP_GREATER_EQ
:
6000 /* '[' may start a primary-expression in obj-c++. */
6001 case CPP_OPEN_SQUARE
:
6002 return c_dialect_objc ();
6009 /* Parse a cast-expression.
6013 ( type-id ) cast-expression
6015 ADDRESS_P is true iff the unary-expression is appearing as the
6016 operand of the `&' operator. CAST_P is true if this expression is
6017 the target of a cast.
6019 Returns a representation of the expression. */
6022 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6025 /* If it's a `(', then we might be looking at a cast. */
6026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6028 tree type
= NULL_TREE
;
6029 tree expr
= NULL_TREE
;
6030 bool compound_literal_p
;
6031 const char *saved_message
;
6033 /* There's no way to know yet whether or not this is a cast.
6034 For example, `(int (3))' is a unary-expression, while `(int)
6035 3' is a cast. So, we resort to parsing tentatively. */
6036 cp_parser_parse_tentatively (parser
);
6037 /* Types may not be defined in a cast. */
6038 saved_message
= parser
->type_definition_forbidden_message
;
6039 parser
->type_definition_forbidden_message
6040 = "types may not be defined in casts";
6041 /* Consume the `('. */
6042 cp_lexer_consume_token (parser
->lexer
);
6043 /* A very tricky bit is that `(struct S) { 3 }' is a
6044 compound-literal (which we permit in C++ as an extension).
6045 But, that construct is not a cast-expression -- it is a
6046 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6047 is legal; if the compound-literal were a cast-expression,
6048 you'd need an extra set of parentheses.) But, if we parse
6049 the type-id, and it happens to be a class-specifier, then we
6050 will commit to the parse at that point, because we cannot
6051 undo the action that is done when creating a new class. So,
6052 then we cannot back up and do a postfix-expression.
6054 Therefore, we scan ahead to the closing `)', and check to see
6055 if the token after the `)' is a `{'. If so, we are not
6056 looking at a cast-expression.
6058 Save tokens so that we can put them back. */
6059 cp_lexer_save_tokens (parser
->lexer
);
6060 /* Skip tokens until the next token is a closing parenthesis.
6061 If we find the closing `)', and the next token is a `{', then
6062 we are looking at a compound-literal. */
6064 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6065 /*consume_paren=*/true)
6066 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6067 /* Roll back the tokens we skipped. */
6068 cp_lexer_rollback_tokens (parser
->lexer
);
6069 /* If we were looking at a compound-literal, simulate an error
6070 so that the call to cp_parser_parse_definitely below will
6072 if (compound_literal_p
)
6073 cp_parser_simulate_error (parser
);
6076 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6077 parser
->in_type_id_in_expr_p
= true;
6078 /* Look for the type-id. */
6079 type
= cp_parser_type_id (parser
);
6080 /* Look for the closing `)'. */
6081 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
6082 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6085 /* Restore the saved message. */
6086 parser
->type_definition_forbidden_message
= saved_message
;
6088 /* At this point this can only be either a cast or a
6089 parenthesized ctor such as `(T ())' that looks like a cast to
6090 function returning T. */
6091 if (!cp_parser_error_occurred (parser
)
6092 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6095 cp_parser_parse_definitely (parser
);
6096 expr
= cp_parser_cast_expression (parser
,
6097 /*address_p=*/false,
6098 /*cast_p=*/true, pidk
);
6100 /* Warn about old-style casts, if so requested. */
6101 if (warn_old_style_cast
6102 && !in_system_header
6103 && !VOID_TYPE_P (type
)
6104 && current_lang_name
!= lang_name_c
)
6105 warning (OPT_Wold_style_cast
, "use of old-style cast");
6107 /* Only type conversions to integral or enumeration types
6108 can be used in constant-expressions. */
6109 if (!cast_valid_in_integral_constant_expression_p (type
)
6110 && (cp_parser_non_integral_constant_expression
6112 "a cast to a type other than an integral or "
6113 "enumeration type")))
6114 return error_mark_node
;
6116 /* Perform the cast. */
6117 expr
= build_c_cast (type
, expr
);
6121 cp_parser_abort_tentative_parse (parser
);
6124 /* If we get here, then it's not a cast, so it must be a
6125 unary-expression. */
6126 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
6129 /* Parse a binary expression of the general form:
6133 pm-expression .* cast-expression
6134 pm-expression ->* cast-expression
6136 multiplicative-expression:
6138 multiplicative-expression * pm-expression
6139 multiplicative-expression / pm-expression
6140 multiplicative-expression % pm-expression
6142 additive-expression:
6143 multiplicative-expression
6144 additive-expression + multiplicative-expression
6145 additive-expression - multiplicative-expression
6149 shift-expression << additive-expression
6150 shift-expression >> additive-expression
6152 relational-expression:
6154 relational-expression < shift-expression
6155 relational-expression > shift-expression
6156 relational-expression <= shift-expression
6157 relational-expression >= shift-expression
6161 relational-expression:
6162 relational-expression <? shift-expression
6163 relational-expression >? shift-expression
6165 equality-expression:
6166 relational-expression
6167 equality-expression == relational-expression
6168 equality-expression != relational-expression
6172 and-expression & equality-expression
6174 exclusive-or-expression:
6176 exclusive-or-expression ^ and-expression
6178 inclusive-or-expression:
6179 exclusive-or-expression
6180 inclusive-or-expression | exclusive-or-expression
6182 logical-and-expression:
6183 inclusive-or-expression
6184 logical-and-expression && inclusive-or-expression
6186 logical-or-expression:
6187 logical-and-expression
6188 logical-or-expression || logical-and-expression
6190 All these are implemented with a single function like:
6193 simple-cast-expression
6194 binary-expression <token> binary-expression
6196 CAST_P is true if this expression is the target of a cast.
6198 The binops_by_token map is used to get the tree codes for each <token> type.
6199 binary-expressions are associated according to a precedence table. */
6201 #define TOKEN_PRECEDENCE(token) \
6202 (((token->type == CPP_GREATER \
6203 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6204 && !parser->greater_than_is_operator_p) \
6205 ? PREC_NOT_OPERATOR \
6206 : binops_by_token[token->type].prec)
6209 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
6210 enum cp_parser_prec prec
,
6213 cp_parser_expression_stack stack
;
6214 cp_parser_expression_stack_entry
*sp
= &stack
[0];
6217 enum tree_code tree_type
, lhs_type
, rhs_type
;
6218 enum cp_parser_prec new_prec
, lookahead_prec
;
6221 /* Parse the first expression. */
6222 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
, pidk
);
6223 lhs_type
= ERROR_MARK
;
6227 /* Get an operator token. */
6228 token
= cp_lexer_peek_token (parser
->lexer
);
6230 if (warn_cxx0x_compat
6231 && token
->type
== CPP_RSHIFT
6232 && !parser
->greater_than_is_operator_p
)
6234 warning (OPT_Wc__0x_compat
,
6235 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6237 warning (OPT_Wc__0x_compat
,
6238 "suggest parentheses around %<>>%> expression");
6241 new_prec
= TOKEN_PRECEDENCE (token
);
6243 /* Popping an entry off the stack means we completed a subexpression:
6244 - either we found a token which is not an operator (`>' where it is not
6245 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6246 will happen repeatedly;
6247 - or, we found an operator which has lower priority. This is the case
6248 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6250 if (new_prec
<= prec
)
6259 tree_type
= binops_by_token
[token
->type
].tree_type
;
6261 /* We used the operator token. */
6262 cp_lexer_consume_token (parser
->lexer
);
6264 /* Extract another operand. It may be the RHS of this expression
6265 or the LHS of a new, higher priority expression. */
6266 rhs
= cp_parser_simple_cast_expression (parser
);
6267 rhs_type
= ERROR_MARK
;
6269 /* Get another operator token. Look up its precedence to avoid
6270 building a useless (immediately popped) stack entry for common
6271 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6272 token
= cp_lexer_peek_token (parser
->lexer
);
6273 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6274 if (lookahead_prec
> new_prec
)
6276 /* ... and prepare to parse the RHS of the new, higher priority
6277 expression. Since precedence levels on the stack are
6278 monotonically increasing, we do not have to care about
6281 sp
->tree_type
= tree_type
;
6283 sp
->lhs_type
= lhs_type
;
6286 lhs_type
= rhs_type
;
6288 new_prec
= lookahead_prec
;
6292 /* If the stack is not empty, we have parsed into LHS the right side
6293 (`4' in the example above) of an expression we had suspended.
6294 We can use the information on the stack to recover the LHS (`3')
6295 from the stack together with the tree code (`MULT_EXPR'), and
6296 the precedence of the higher level subexpression
6297 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6298 which will be used to actually build the additive expression. */
6301 tree_type
= sp
->tree_type
;
6303 rhs_type
= lhs_type
;
6305 lhs_type
= sp
->lhs_type
;
6308 overloaded_p
= false;
6309 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6310 ERROR_MARK for everything that is not a binary expression.
6311 This makes warn_about_parentheses miss some warnings that
6312 involve unary operators. For unary expressions we should
6313 pass the correct tree_code unless the unary expression was
6314 surrounded by parentheses.
6316 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6317 &overloaded_p
, tf_warning_or_error
);
6318 lhs_type
= tree_type
;
6320 /* If the binary operator required the use of an overloaded operator,
6321 then this expression cannot be an integral constant-expression.
6322 An overloaded operator can be used even if both operands are
6323 otherwise permissible in an integral constant-expression if at
6324 least one of the operands is of enumeration type. */
6327 && (cp_parser_non_integral_constant_expression
6328 (parser
, "calls to overloaded operators")))
6329 return error_mark_node
;
6336 /* Parse the `? expression : assignment-expression' part of a
6337 conditional-expression. The LOGICAL_OR_EXPR is the
6338 logical-or-expression that started the conditional-expression.
6339 Returns a representation of the entire conditional-expression.
6341 This routine is used by cp_parser_assignment_expression.
6343 ? expression : assignment-expression
6347 ? : assignment-expression */
6350 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6353 tree assignment_expr
;
6355 /* Consume the `?' token. */
6356 cp_lexer_consume_token (parser
->lexer
);
6357 if (cp_parser_allow_gnu_extensions_p (parser
)
6358 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
6359 /* Implicit true clause. */
6362 /* Parse the expression. */
6363 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6365 /* The next token should be a `:'. */
6366 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
6367 /* Parse the assignment-expression. */
6368 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6370 /* Build the conditional-expression. */
6371 return build_x_conditional_expr (logical_or_expr
,
6374 tf_warning_or_error
);
6377 /* Parse an assignment-expression.
6379 assignment-expression:
6380 conditional-expression
6381 logical-or-expression assignment-operator assignment_expression
6384 CAST_P is true if this expression is the target of a cast.
6386 Returns a representation for the expression. */
6389 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
6394 /* If the next token is the `throw' keyword, then we're looking at
6395 a throw-expression. */
6396 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6397 expr
= cp_parser_throw_expression (parser
);
6398 /* Otherwise, it must be that we are looking at a
6399 logical-or-expression. */
6402 /* Parse the binary expressions (logical-or-expression). */
6403 expr
= cp_parser_binary_expression (parser
, cast_p
, PREC_NOT_OPERATOR
, pidk
);
6404 /* If the next token is a `?' then we're actually looking at a
6405 conditional-expression. */
6406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6407 return cp_parser_question_colon_clause (parser
, expr
);
6410 enum tree_code assignment_operator
;
6412 /* If it's an assignment-operator, we're using the second
6415 = cp_parser_assignment_operator_opt (parser
);
6416 if (assignment_operator
!= ERROR_MARK
)
6418 bool non_constant_p
;
6420 /* Parse the right-hand side of the assignment. */
6421 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
6423 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
6424 maybe_warn_cpp0x ("extended initializer lists");
6426 /* An assignment may not appear in a
6427 constant-expression. */
6428 if (cp_parser_non_integral_constant_expression (parser
,
6430 return error_mark_node
;
6431 /* Build the assignment expression. */
6432 expr
= build_x_modify_expr (expr
,
6433 assignment_operator
,
6435 tf_warning_or_error
);
6443 /* Parse an (optional) assignment-operator.
6445 assignment-operator: one of
6446 = *= /= %= += -= >>= <<= &= ^= |=
6450 assignment-operator: one of
6453 If the next token is an assignment operator, the corresponding tree
6454 code is returned, and the token is consumed. For example, for
6455 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6456 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6457 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6458 operator, ERROR_MARK is returned. */
6460 static enum tree_code
6461 cp_parser_assignment_operator_opt (cp_parser
* parser
)
6466 /* Peek at the next token. */
6467 token
= cp_lexer_peek_token (parser
->lexer
);
6469 switch (token
->type
)
6480 op
= TRUNC_DIV_EXPR
;
6484 op
= TRUNC_MOD_EXPR
;
6516 /* Nothing else is an assignment operator. */
6520 /* If it was an assignment operator, consume it. */
6521 if (op
!= ERROR_MARK
)
6522 cp_lexer_consume_token (parser
->lexer
);
6527 /* Parse an expression.
6530 assignment-expression
6531 expression , assignment-expression
6533 CAST_P is true if this expression is the target of a cast.
6535 Returns a representation of the expression. */
6538 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
6540 tree expression
= NULL_TREE
;
6544 tree assignment_expression
;
6546 /* Parse the next assignment-expression. */
6547 assignment_expression
6548 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
6549 /* If this is the first assignment-expression, we can just
6552 expression
= assignment_expression
;
6554 expression
= build_x_compound_expr (expression
,
6555 assignment_expression
,
6556 tf_warning_or_error
);
6557 /* If the next token is not a comma, then we are done with the
6559 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6561 /* Consume the `,'. */
6562 cp_lexer_consume_token (parser
->lexer
);
6563 /* A comma operator cannot appear in a constant-expression. */
6564 if (cp_parser_non_integral_constant_expression (parser
,
6565 "a comma operator"))
6566 expression
= error_mark_node
;
6572 /* Parse a constant-expression.
6574 constant-expression:
6575 conditional-expression
6577 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6578 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6579 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6580 is false, NON_CONSTANT_P should be NULL. */
6583 cp_parser_constant_expression (cp_parser
* parser
,
6584 bool allow_non_constant_p
,
6585 bool *non_constant_p
)
6587 bool saved_integral_constant_expression_p
;
6588 bool saved_allow_non_integral_constant_expression_p
;
6589 bool saved_non_integral_constant_expression_p
;
6592 /* It might seem that we could simply parse the
6593 conditional-expression, and then check to see if it were
6594 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6595 one that the compiler can figure out is constant, possibly after
6596 doing some simplifications or optimizations. The standard has a
6597 precise definition of constant-expression, and we must honor
6598 that, even though it is somewhat more restrictive.
6604 is not a legal declaration, because `(2, 3)' is not a
6605 constant-expression. The `,' operator is forbidden in a
6606 constant-expression. However, GCC's constant-folding machinery
6607 will fold this operation to an INTEGER_CST for `3'. */
6609 /* Save the old settings. */
6610 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6611 saved_allow_non_integral_constant_expression_p
6612 = parser
->allow_non_integral_constant_expression_p
;
6613 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6614 /* We are now parsing a constant-expression. */
6615 parser
->integral_constant_expression_p
= true;
6616 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6617 parser
->non_integral_constant_expression_p
= false;
6618 /* Although the grammar says "conditional-expression", we parse an
6619 "assignment-expression", which also permits "throw-expression"
6620 and the use of assignment operators. In the case that
6621 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6622 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6623 actually essential that we look for an assignment-expression.
6624 For example, cp_parser_initializer_clauses uses this function to
6625 determine whether a particular assignment-expression is in fact
6627 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6628 /* Restore the old settings. */
6629 parser
->integral_constant_expression_p
6630 = saved_integral_constant_expression_p
;
6631 parser
->allow_non_integral_constant_expression_p
6632 = saved_allow_non_integral_constant_expression_p
;
6633 if (allow_non_constant_p
)
6634 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6635 else if (parser
->non_integral_constant_expression_p
)
6636 expression
= error_mark_node
;
6637 parser
->non_integral_constant_expression_p
6638 = saved_non_integral_constant_expression_p
;
6643 /* Parse __builtin_offsetof.
6645 offsetof-expression:
6646 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6648 offsetof-member-designator:
6650 | offsetof-member-designator "." id-expression
6651 | offsetof-member-designator "[" expression "]"
6652 | offsetof-member-designator "->" id-expression */
6655 cp_parser_builtin_offsetof (cp_parser
*parser
)
6657 int save_ice_p
, save_non_ice_p
;
6662 /* We're about to accept non-integral-constant things, but will
6663 definitely yield an integral constant expression. Save and
6664 restore these values around our local parsing. */
6665 save_ice_p
= parser
->integral_constant_expression_p
;
6666 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6668 /* Consume the "__builtin_offsetof" token. */
6669 cp_lexer_consume_token (parser
->lexer
);
6670 /* Consume the opening `('. */
6671 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
6672 /* Parse the type-id. */
6673 type
= cp_parser_type_id (parser
);
6674 /* Look for the `,'. */
6675 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
6676 token
= cp_lexer_peek_token (parser
->lexer
);
6678 /* Build the (type *)null that begins the traditional offsetof macro. */
6679 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
6680 tf_warning_or_error
);
6682 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6683 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6684 true, &dummy
, token
->location
);
6687 token
= cp_lexer_peek_token (parser
->lexer
);
6688 switch (token
->type
)
6690 case CPP_OPEN_SQUARE
:
6691 /* offsetof-member-designator "[" expression "]" */
6692 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6696 /* offsetof-member-designator "->" identifier */
6697 expr
= grok_array_decl (expr
, integer_zero_node
);
6701 /* offsetof-member-designator "." identifier */
6702 cp_lexer_consume_token (parser
->lexer
);
6703 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
6708 case CPP_CLOSE_PAREN
:
6709 /* Consume the ")" token. */
6710 cp_lexer_consume_token (parser
->lexer
);
6714 /* Error. We know the following require will fail, but
6715 that gives the proper error message. */
6716 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
6717 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6718 expr
= error_mark_node
;
6724 /* If we're processing a template, we can't finish the semantics yet.
6725 Otherwise we can fold the entire expression now. */
6726 if (processing_template_decl
)
6727 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6729 expr
= finish_offsetof (expr
);
6732 parser
->integral_constant_expression_p
= save_ice_p
;
6733 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6738 /* Parse a trait expression. */
6741 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
6744 tree type1
, type2
= NULL_TREE
;
6745 bool binary
= false;
6746 cp_decl_specifier_seq decl_specs
;
6750 case RID_HAS_NOTHROW_ASSIGN
:
6751 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
6753 case RID_HAS_NOTHROW_CONSTRUCTOR
:
6754 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
6756 case RID_HAS_NOTHROW_COPY
:
6757 kind
= CPTK_HAS_NOTHROW_COPY
;
6759 case RID_HAS_TRIVIAL_ASSIGN
:
6760 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
6762 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
6763 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
6765 case RID_HAS_TRIVIAL_COPY
:
6766 kind
= CPTK_HAS_TRIVIAL_COPY
;
6768 case RID_HAS_TRIVIAL_DESTRUCTOR
:
6769 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
6771 case RID_HAS_VIRTUAL_DESTRUCTOR
:
6772 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
6774 case RID_IS_ABSTRACT
:
6775 kind
= CPTK_IS_ABSTRACT
;
6777 case RID_IS_BASE_OF
:
6778 kind
= CPTK_IS_BASE_OF
;
6782 kind
= CPTK_IS_CLASS
;
6784 case RID_IS_CONVERTIBLE_TO
:
6785 kind
= CPTK_IS_CONVERTIBLE_TO
;
6789 kind
= CPTK_IS_EMPTY
;
6792 kind
= CPTK_IS_ENUM
;
6797 case RID_IS_POLYMORPHIC
:
6798 kind
= CPTK_IS_POLYMORPHIC
;
6801 kind
= CPTK_IS_UNION
;
6807 /* Consume the token. */
6808 cp_lexer_consume_token (parser
->lexer
);
6810 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
6812 type1
= cp_parser_type_id (parser
);
6814 if (type1
== error_mark_node
)
6815 return error_mark_node
;
6817 /* Build a trivial decl-specifier-seq. */
6818 clear_decl_specs (&decl_specs
);
6819 decl_specs
.type
= type1
;
6821 /* Call grokdeclarator to figure out what type this is. */
6822 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
6823 /*initialized=*/0, /*attrlist=*/NULL
);
6827 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
6829 type2
= cp_parser_type_id (parser
);
6831 if (type2
== error_mark_node
)
6832 return error_mark_node
;
6834 /* Build a trivial decl-specifier-seq. */
6835 clear_decl_specs (&decl_specs
);
6836 decl_specs
.type
= type2
;
6838 /* Call grokdeclarator to figure out what type this is. */
6839 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
6840 /*initialized=*/0, /*attrlist=*/NULL
);
6843 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
6845 /* Complete the trait expression, which may mean either processing
6846 the trait expr now or saving it for template instantiation. */
6847 return finish_trait_expr (kind
, type1
, type2
);
6850 /* Statements [gram.stmt.stmt] */
6852 /* Parse a statement.
6856 expression-statement
6861 declaration-statement
6864 IN_COMPOUND is true when the statement is nested inside a
6865 cp_parser_compound_statement; this matters for certain pragmas.
6867 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6868 is a (possibly labeled) if statement which is not enclosed in braces
6869 and has an else clause. This is used to implement -Wparentheses. */
6872 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6873 bool in_compound
, bool *if_p
)
6877 location_t statement_location
;
6882 /* There is no statement yet. */
6883 statement
= NULL_TREE
;
6884 /* Peek at the next token. */
6885 token
= cp_lexer_peek_token (parser
->lexer
);
6886 /* Remember the location of the first token in the statement. */
6887 statement_location
= token
->location
;
6888 /* If this is a keyword, then that will often determine what kind of
6889 statement we have. */
6890 if (token
->type
== CPP_KEYWORD
)
6892 enum rid keyword
= token
->keyword
;
6898 /* Looks like a labeled-statement with a case label.
6899 Parse the label, and then use tail recursion to parse
6901 cp_parser_label_for_labeled_statement (parser
);
6906 statement
= cp_parser_selection_statement (parser
, if_p
);
6912 statement
= cp_parser_iteration_statement (parser
);
6919 statement
= cp_parser_jump_statement (parser
);
6922 /* Objective-C++ exception-handling constructs. */
6925 case RID_AT_FINALLY
:
6926 case RID_AT_SYNCHRONIZED
:
6928 statement
= cp_parser_objc_statement (parser
);
6932 statement
= cp_parser_try_block (parser
);
6936 /* This must be a namespace alias definition. */
6937 cp_parser_declaration_statement (parser
);
6941 /* It might be a keyword like `int' that can start a
6942 declaration-statement. */
6946 else if (token
->type
== CPP_NAME
)
6948 /* If the next token is a `:', then we are looking at a
6949 labeled-statement. */
6950 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6951 if (token
->type
== CPP_COLON
)
6953 /* Looks like a labeled-statement with an ordinary label.
6954 Parse the label, and then use tail recursion to parse
6956 cp_parser_label_for_labeled_statement (parser
);
6960 /* Anything that starts with a `{' must be a compound-statement. */
6961 else if (token
->type
== CPP_OPEN_BRACE
)
6962 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6963 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6964 a statement all its own. */
6965 else if (token
->type
== CPP_PRAGMA
)
6967 /* Only certain OpenMP pragmas are attached to statements, and thus
6968 are considered statements themselves. All others are not. In
6969 the context of a compound, accept the pragma as a "statement" and
6970 return so that we can check for a close brace. Otherwise we
6971 require a real statement and must go back and read one. */
6973 cp_parser_pragma (parser
, pragma_compound
);
6974 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6978 else if (token
->type
== CPP_EOF
)
6980 cp_parser_error (parser
, "expected statement");
6984 /* Everything else must be a declaration-statement or an
6985 expression-statement. Try for the declaration-statement
6986 first, unless we are looking at a `;', in which case we know that
6987 we have an expression-statement. */
6990 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6992 cp_parser_parse_tentatively (parser
);
6993 /* Try to parse the declaration-statement. */
6994 cp_parser_declaration_statement (parser
);
6995 /* If that worked, we're done. */
6996 if (cp_parser_parse_definitely (parser
))
6999 /* Look for an expression-statement instead. */
7000 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
7003 /* Set the line number for the statement. */
7004 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
7005 SET_EXPR_LOCATION (statement
, statement_location
);
7008 /* Parse the label for a labeled-statement, i.e.
7011 case constant-expression :
7015 case constant-expression ... constant-expression : statement
7017 When a label is parsed without errors, the label is added to the
7018 parse tree by the finish_* functions, so this function doesn't
7019 have to return the label. */
7022 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
7026 /* The next token should be an identifier. */
7027 token
= cp_lexer_peek_token (parser
->lexer
);
7028 if (token
->type
!= CPP_NAME
7029 && token
->type
!= CPP_KEYWORD
)
7031 cp_parser_error (parser
, "expected labeled-statement");
7035 switch (token
->keyword
)
7042 /* Consume the `case' token. */
7043 cp_lexer_consume_token (parser
->lexer
);
7044 /* Parse the constant-expression. */
7045 expr
= cp_parser_constant_expression (parser
,
7046 /*allow_non_constant_p=*/false,
7049 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
7050 if (ellipsis
->type
== CPP_ELLIPSIS
)
7052 /* Consume the `...' token. */
7053 cp_lexer_consume_token (parser
->lexer
);
7055 cp_parser_constant_expression (parser
,
7056 /*allow_non_constant_p=*/false,
7058 /* We don't need to emit warnings here, as the common code
7059 will do this for us. */
7062 expr_hi
= NULL_TREE
;
7064 if (parser
->in_switch_statement_p
)
7065 finish_case_label (expr
, expr_hi
);
7067 error ("%Hcase label %qE not within a switch statement",
7068 &token
->location
, expr
);
7073 /* Consume the `default' token. */
7074 cp_lexer_consume_token (parser
->lexer
);
7076 if (parser
->in_switch_statement_p
)
7077 finish_case_label (NULL_TREE
, NULL_TREE
);
7079 error ("%Hcase label not within a switch statement", &token
->location
);
7083 /* Anything else must be an ordinary label. */
7084 finish_label_stmt (cp_parser_identifier (parser
));
7088 /* Require the `:' token. */
7089 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
7092 /* Parse an expression-statement.
7094 expression-statement:
7097 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7098 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7099 indicates whether this expression-statement is part of an
7100 expression statement. */
7103 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
7105 tree statement
= NULL_TREE
;
7107 /* If the next token is a ';', then there is no expression
7109 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7110 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7112 /* Consume the final `;'. */
7113 cp_parser_consume_semicolon_at_end_of_statement (parser
);
7115 if (in_statement_expr
7116 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
7117 /* This is the final expression statement of a statement
7119 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
7121 statement
= finish_expr_stmt (statement
);
7128 /* Parse a compound-statement.
7131 { statement-seq [opt] }
7136 { label-declaration-seq [opt] statement-seq [opt] }
7138 label-declaration-seq:
7140 label-declaration-seq label-declaration
7142 Returns a tree representing the statement. */
7145 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
7150 /* Consume the `{'. */
7151 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>"))
7152 return error_mark_node
;
7153 /* Begin the compound-statement. */
7154 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
7155 /* If the next keyword is `__label__' we have a label declaration. */
7156 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
7157 cp_parser_label_declaration (parser
);
7158 /* Parse an (optional) statement-seq. */
7159 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
7160 /* Finish the compound-statement. */
7161 finish_compound_stmt (compound_stmt
);
7162 /* Consume the `}'. */
7163 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
7165 return compound_stmt
;
7168 /* Parse an (optional) statement-seq.
7172 statement-seq [opt] statement */
7175 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
7177 /* Scan statements until there aren't any more. */
7180 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7182 /* If we're looking at a `}', then we've run out of statements. */
7183 if (token
->type
== CPP_CLOSE_BRACE
7184 || token
->type
== CPP_EOF
7185 || token
->type
== CPP_PRAGMA_EOL
)
7188 /* If we are in a compound statement and find 'else' then
7189 something went wrong. */
7190 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
7192 if (parser
->in_statement
& IN_IF_STMT
)
7196 token
= cp_lexer_consume_token (parser
->lexer
);
7197 error ("%H%<else%> without a previous %<if%>", &token
->location
);
7201 /* Parse the statement. */
7202 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
7206 /* Parse a selection-statement.
7208 selection-statement:
7209 if ( condition ) statement
7210 if ( condition ) statement else statement
7211 switch ( condition ) statement
7213 Returns the new IF_STMT or SWITCH_STMT.
7215 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7216 is a (possibly labeled) if statement which is not enclosed in
7217 braces and has an else clause. This is used to implement
7221 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
7229 /* Peek at the next token. */
7230 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
7232 /* See what kind of keyword it is. */
7233 keyword
= token
->keyword
;
7242 /* Look for the `('. */
7243 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
7245 cp_parser_skip_to_end_of_statement (parser
);
7246 return error_mark_node
;
7249 /* Begin the selection-statement. */
7250 if (keyword
== RID_IF
)
7251 statement
= begin_if_stmt ();
7253 statement
= begin_switch_stmt ();
7255 /* Parse the condition. */
7256 condition
= cp_parser_condition (parser
);
7257 /* Look for the `)'. */
7258 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
7259 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
7260 /*consume_paren=*/true);
7262 if (keyword
== RID_IF
)
7265 unsigned char in_statement
;
7267 /* Add the condition. */
7268 finish_if_stmt_cond (condition
, statement
);
7270 /* Parse the then-clause. */
7271 in_statement
= parser
->in_statement
;
7272 parser
->in_statement
|= IN_IF_STMT
;
7273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7275 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7276 add_stmt (build_empty_stmt ());
7277 cp_lexer_consume_token (parser
->lexer
);
7278 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
7279 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
7280 "empty body in an %<if%> statement");
7284 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
7285 parser
->in_statement
= in_statement
;
7287 finish_then_clause (statement
);
7289 /* If the next token is `else', parse the else-clause. */
7290 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
7293 /* Consume the `else' keyword. */
7294 cp_lexer_consume_token (parser
->lexer
);
7295 begin_else_clause (statement
);
7296 /* Parse the else-clause. */
7297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7299 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
7300 OPT_Wempty_body
, "suggest braces around "
7301 "empty body in an %<else%> statement");
7302 add_stmt (build_empty_stmt ());
7303 cp_lexer_consume_token (parser
->lexer
);
7306 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7308 finish_else_clause (statement
);
7310 /* If we are currently parsing a then-clause, then
7311 IF_P will not be NULL. We set it to true to
7312 indicate that this if statement has an else clause.
7313 This may trigger the Wparentheses warning below
7314 when we get back up to the parent if statement. */
7320 /* This if statement does not have an else clause. If
7321 NESTED_IF is true, then the then-clause is an if
7322 statement which does have an else clause. We warn
7323 about the potential ambiguity. */
7325 warning (OPT_Wparentheses
,
7326 ("%Hsuggest explicit braces "
7327 "to avoid ambiguous %<else%>"),
7328 EXPR_LOCUS (statement
));
7331 /* Now we're all done with the if-statement. */
7332 finish_if_stmt (statement
);
7336 bool in_switch_statement_p
;
7337 unsigned char in_statement
;
7339 /* Add the condition. */
7340 finish_switch_cond (condition
, statement
);
7342 /* Parse the body of the switch-statement. */
7343 in_switch_statement_p
= parser
->in_switch_statement_p
;
7344 in_statement
= parser
->in_statement
;
7345 parser
->in_switch_statement_p
= true;
7346 parser
->in_statement
|= IN_SWITCH_STMT
;
7347 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7348 parser
->in_switch_statement_p
= in_switch_statement_p
;
7349 parser
->in_statement
= in_statement
;
7351 /* Now we're all done with the switch-statement. */
7352 finish_switch_stmt (statement
);
7360 cp_parser_error (parser
, "expected selection-statement");
7361 return error_mark_node
;
7365 /* Parse a condition.
7369 type-specifier-seq declarator = initializer-clause
7370 type-specifier-seq declarator braced-init-list
7375 type-specifier-seq declarator asm-specification [opt]
7376 attributes [opt] = assignment-expression
7378 Returns the expression that should be tested. */
7381 cp_parser_condition (cp_parser
* parser
)
7383 cp_decl_specifier_seq type_specifiers
;
7384 const char *saved_message
;
7386 /* Try the declaration first. */
7387 cp_parser_parse_tentatively (parser
);
7388 /* New types are not allowed in the type-specifier-seq for a
7390 saved_message
= parser
->type_definition_forbidden_message
;
7391 parser
->type_definition_forbidden_message
7392 = "types may not be defined in conditions";
7393 /* Parse the type-specifier-seq. */
7394 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
7396 /* Restore the saved message. */
7397 parser
->type_definition_forbidden_message
= saved_message
;
7398 /* If all is well, we might be looking at a declaration. */
7399 if (!cp_parser_error_occurred (parser
))
7402 tree asm_specification
;
7404 cp_declarator
*declarator
;
7405 tree initializer
= NULL_TREE
;
7407 /* Parse the declarator. */
7408 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
7409 /*ctor_dtor_or_conv_p=*/NULL
,
7410 /*parenthesized_p=*/NULL
,
7411 /*member_p=*/false);
7412 /* Parse the attributes. */
7413 attributes
= cp_parser_attributes_opt (parser
);
7414 /* Parse the asm-specification. */
7415 asm_specification
= cp_parser_asm_specification_opt (parser
);
7416 /* If the next token is not an `=' or '{', then we might still be
7417 looking at an expression. For example:
7421 looks like a decl-specifier-seq and a declarator -- but then
7422 there is no `=', so this is an expression. */
7423 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7424 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7425 cp_parser_simulate_error (parser
);
7427 /* If we did see an `=' or '{', then we are looking at a declaration
7429 if (cp_parser_parse_definitely (parser
))
7432 bool non_constant_p
;
7433 bool flags
= LOOKUP_ONLYCONVERTING
;
7435 /* Create the declaration. */
7436 decl
= start_decl (declarator
, &type_specifiers
,
7437 /*initialized_p=*/true,
7438 attributes
, /*prefix_attributes=*/NULL_TREE
,
7441 /* Parse the initializer. */
7442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7444 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
7445 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
7450 /* Consume the `='. */
7451 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
7452 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
7454 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
7455 maybe_warn_cpp0x ("extended initializer lists");
7457 if (!non_constant_p
)
7458 initializer
= fold_non_dependent_expr (initializer
);
7460 /* Process the initializer. */
7461 cp_finish_decl (decl
,
7462 initializer
, !non_constant_p
,
7467 pop_scope (pushed_scope
);
7469 return convert_from_reference (decl
);
7472 /* If we didn't even get past the declarator successfully, we are
7473 definitely not looking at a declaration. */
7475 cp_parser_abort_tentative_parse (parser
);
7477 /* Otherwise, we are looking at an expression. */
7478 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7481 /* Parse an iteration-statement.
7483 iteration-statement:
7484 while ( condition ) statement
7485 do statement while ( expression ) ;
7486 for ( for-init-statement condition [opt] ; expression [opt] )
7489 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7492 cp_parser_iteration_statement (cp_parser
* parser
)
7497 unsigned char in_statement
;
7499 /* Peek at the next token. */
7500 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
7502 return error_mark_node
;
7504 /* Remember whether or not we are already within an iteration
7506 in_statement
= parser
->in_statement
;
7508 /* See what kind of keyword it is. */
7509 keyword
= token
->keyword
;
7516 /* Begin the while-statement. */
7517 statement
= begin_while_stmt ();
7518 /* Look for the `('. */
7519 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
7520 /* Parse the condition. */
7521 condition
= cp_parser_condition (parser
);
7522 finish_while_stmt_cond (condition
, statement
);
7523 /* Look for the `)'. */
7524 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
7525 /* Parse the dependent statement. */
7526 parser
->in_statement
= IN_ITERATION_STMT
;
7527 cp_parser_already_scoped_statement (parser
);
7528 parser
->in_statement
= in_statement
;
7529 /* We're done with the while-statement. */
7530 finish_while_stmt (statement
);
7538 /* Begin the do-statement. */
7539 statement
= begin_do_stmt ();
7540 /* Parse the body of the do-statement. */
7541 parser
->in_statement
= IN_ITERATION_STMT
;
7542 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7543 parser
->in_statement
= in_statement
;
7544 finish_do_body (statement
);
7545 /* Look for the `while' keyword. */
7546 cp_parser_require_keyword (parser
, RID_WHILE
, "%<while%>");
7547 /* Look for the `('. */
7548 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
7549 /* Parse the expression. */
7550 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7551 /* We're done with the do-statement. */
7552 finish_do_stmt (expression
, statement
);
7553 /* Look for the `)'. */
7554 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
7555 /* Look for the `;'. */
7556 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7562 tree condition
= NULL_TREE
;
7563 tree expression
= NULL_TREE
;
7565 /* Begin the for-statement. */
7566 statement
= begin_for_stmt ();
7567 /* Look for the `('. */
7568 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
7569 /* Parse the initialization. */
7570 cp_parser_for_init_statement (parser
);
7571 finish_for_init_stmt (statement
);
7573 /* If there's a condition, process it. */
7574 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7575 condition
= cp_parser_condition (parser
);
7576 finish_for_cond (condition
, statement
);
7577 /* Look for the `;'. */
7578 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7580 /* If there's an expression, process it. */
7581 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7582 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7583 finish_for_expr (expression
, statement
);
7584 /* Look for the `)'. */
7585 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
7587 /* Parse the body of the for-statement. */
7588 parser
->in_statement
= IN_ITERATION_STMT
;
7589 cp_parser_already_scoped_statement (parser
);
7590 parser
->in_statement
= in_statement
;
7592 /* We're done with the for-statement. */
7593 finish_for_stmt (statement
);
7598 cp_parser_error (parser
, "expected iteration-statement");
7599 statement
= error_mark_node
;
7606 /* Parse a for-init-statement.
7609 expression-statement
7610 simple-declaration */
7613 cp_parser_for_init_statement (cp_parser
* parser
)
7615 /* If the next token is a `;', then we have an empty
7616 expression-statement. Grammatically, this is also a
7617 simple-declaration, but an invalid one, because it does not
7618 declare anything. Therefore, if we did not handle this case
7619 specially, we would issue an error message about an invalid
7621 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7623 /* We're going to speculatively look for a declaration, falling back
7624 to an expression, if necessary. */
7625 cp_parser_parse_tentatively (parser
);
7626 /* Parse the declaration. */
7627 cp_parser_simple_declaration (parser
,
7628 /*function_definition_allowed_p=*/false);
7629 /* If the tentative parse failed, then we shall need to look for an
7630 expression-statement. */
7631 if (cp_parser_parse_definitely (parser
))
7635 cp_parser_expression_statement (parser
, false);
7638 /* Parse a jump-statement.
7643 return expression [opt] ;
7644 return braced-init-list ;
7652 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7655 cp_parser_jump_statement (cp_parser
* parser
)
7657 tree statement
= error_mark_node
;
7660 unsigned char in_statement
;
7662 /* Peek at the next token. */
7663 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
7665 return error_mark_node
;
7667 /* See what kind of keyword it is. */
7668 keyword
= token
->keyword
;
7672 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
7673 switch (in_statement
)
7676 error ("%Hbreak statement not within loop or switch", &token
->location
);
7679 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
7680 || in_statement
== IN_ITERATION_STMT
);
7681 statement
= finish_break_stmt ();
7684 error ("%Hinvalid exit from OpenMP structured block", &token
->location
);
7687 error ("%Hbreak statement used with OpenMP for loop", &token
->location
);
7690 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7694 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
7697 error ("%Hcontinue statement not within a loop", &token
->location
);
7699 case IN_ITERATION_STMT
:
7701 statement
= finish_continue_stmt ();
7704 error ("%Hinvalid exit from OpenMP structured block", &token
->location
);
7709 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7715 bool expr_non_constant_p
;
7717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7719 maybe_warn_cpp0x ("extended initializer lists");
7720 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7722 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7723 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7725 /* If the next token is a `;', then there is no
7728 /* Build the return-statement. */
7729 statement
= finish_return_stmt (expr
);
7730 /* Look for the final `;'. */
7731 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7736 /* Create the goto-statement. */
7737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
7739 /* Issue a warning about this use of a GNU extension. */
7740 pedwarn (token
->location
, OPT_pedantic
, "ISO C++ forbids computed gotos");
7741 /* Consume the '*' token. */
7742 cp_lexer_consume_token (parser
->lexer
);
7743 /* Parse the dependent expression. */
7744 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
7747 finish_goto_stmt (cp_parser_identifier (parser
));
7748 /* Look for the final `;'. */
7749 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7753 cp_parser_error (parser
, "expected jump-statement");
7760 /* Parse a declaration-statement.
7762 declaration-statement:
7763 block-declaration */
7766 cp_parser_declaration_statement (cp_parser
* parser
)
7770 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7771 p
= obstack_alloc (&declarator_obstack
, 0);
7773 /* Parse the block-declaration. */
7774 cp_parser_block_declaration (parser
, /*statement_p=*/true);
7776 /* Free any declarators allocated. */
7777 obstack_free (&declarator_obstack
, p
);
7779 /* Finish off the statement. */
7783 /* Some dependent statements (like `if (cond) statement'), are
7784 implicitly in their own scope. In other words, if the statement is
7785 a single statement (as opposed to a compound-statement), it is
7786 none-the-less treated as if it were enclosed in braces. Any
7787 declarations appearing in the dependent statement are out of scope
7788 after control passes that point. This function parses a statement,
7789 but ensures that is in its own scope, even if it is not a
7792 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7793 is a (possibly labeled) if statement which is not enclosed in
7794 braces and has an else clause. This is used to implement
7797 Returns the new statement. */
7800 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
7807 /* Mark if () ; with a special NOP_EXPR. */
7808 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7810 cp_lexer_consume_token (parser
->lexer
);
7811 statement
= add_stmt (build_empty_stmt ());
7813 /* if a compound is opened, we simply parse the statement directly. */
7814 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7815 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7816 /* If the token is not a `{', then we must take special action. */
7819 /* Create a compound-statement. */
7820 statement
= begin_compound_stmt (0);
7821 /* Parse the dependent-statement. */
7822 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
7823 /* Finish the dummy compound-statement. */
7824 finish_compound_stmt (statement
);
7827 /* Return the statement. */
7831 /* For some dependent statements (like `while (cond) statement'), we
7832 have already created a scope. Therefore, even if the dependent
7833 statement is a compound-statement, we do not want to create another
7837 cp_parser_already_scoped_statement (cp_parser
* parser
)
7839 /* If the token is a `{', then we must take special action. */
7840 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7841 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
7844 /* Avoid calling cp_parser_compound_statement, so that we
7845 don't create a new scope. Do everything else by hand. */
7846 cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>");
7847 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7848 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
7852 /* Declarations [gram.dcl.dcl] */
7854 /* Parse an optional declaration-sequence.
7858 declaration-seq declaration */
7861 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7867 token
= cp_lexer_peek_token (parser
->lexer
);
7869 if (token
->type
== CPP_CLOSE_BRACE
7870 || token
->type
== CPP_EOF
7871 || token
->type
== CPP_PRAGMA_EOL
)
7874 if (token
->type
== CPP_SEMICOLON
)
7876 /* A declaration consisting of a single semicolon is
7877 invalid. Allow it unless we're being pedantic. */
7878 cp_lexer_consume_token (parser
->lexer
);
7879 if (!in_system_header
)
7880 pedwarn (input_location
, OPT_pedantic
, "extra %<;%>");
7884 /* If we're entering or exiting a region that's implicitly
7885 extern "C", modify the lang context appropriately. */
7886 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7888 push_lang_context (lang_name_c
);
7889 parser
->implicit_extern_c
= true;
7891 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7893 pop_lang_context ();
7894 parser
->implicit_extern_c
= false;
7897 if (token
->type
== CPP_PRAGMA
)
7899 /* A top-level declaration can consist solely of a #pragma.
7900 A nested declaration cannot, so this is done here and not
7901 in cp_parser_declaration. (A #pragma at block scope is
7902 handled in cp_parser_statement.) */
7903 cp_parser_pragma (parser
, pragma_external
);
7907 /* Parse the declaration itself. */
7908 cp_parser_declaration (parser
);
7912 /* Parse a declaration.
7917 template-declaration
7918 explicit-instantiation
7919 explicit-specialization
7920 linkage-specification
7921 namespace-definition
7926 __extension__ declaration */
7929 cp_parser_declaration (cp_parser
* parser
)
7936 /* Check for the `__extension__' keyword. */
7937 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7939 /* Parse the qualified declaration. */
7940 cp_parser_declaration (parser
);
7941 /* Restore the PEDANTIC flag. */
7942 pedantic
= saved_pedantic
;
7947 /* Try to figure out what kind of declaration is present. */
7948 token1
= *cp_lexer_peek_token (parser
->lexer
);
7950 if (token1
.type
!= CPP_EOF
)
7951 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7954 token2
.type
= CPP_EOF
;
7955 token2
.keyword
= RID_MAX
;
7958 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7959 p
= obstack_alloc (&declarator_obstack
, 0);
7961 /* If the next token is `extern' and the following token is a string
7962 literal, then we have a linkage specification. */
7963 if (token1
.keyword
== RID_EXTERN
7964 && cp_parser_is_string_literal (&token2
))
7965 cp_parser_linkage_specification (parser
);
7966 /* If the next token is `template', then we have either a template
7967 declaration, an explicit instantiation, or an explicit
7969 else if (token1
.keyword
== RID_TEMPLATE
)
7971 /* `template <>' indicates a template specialization. */
7972 if (token2
.type
== CPP_LESS
7973 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7974 cp_parser_explicit_specialization (parser
);
7975 /* `template <' indicates a template declaration. */
7976 else if (token2
.type
== CPP_LESS
)
7977 cp_parser_template_declaration (parser
, /*member_p=*/false);
7978 /* Anything else must be an explicit instantiation. */
7980 cp_parser_explicit_instantiation (parser
);
7982 /* If the next token is `export', then we have a template
7984 else if (token1
.keyword
== RID_EXPORT
)
7985 cp_parser_template_declaration (parser
, /*member_p=*/false);
7986 /* If the next token is `extern', 'static' or 'inline' and the one
7987 after that is `template', we have a GNU extended explicit
7988 instantiation directive. */
7989 else if (cp_parser_allow_gnu_extensions_p (parser
)
7990 && (token1
.keyword
== RID_EXTERN
7991 || token1
.keyword
== RID_STATIC
7992 || token1
.keyword
== RID_INLINE
)
7993 && token2
.keyword
== RID_TEMPLATE
)
7994 cp_parser_explicit_instantiation (parser
);
7995 /* If the next token is `namespace', check for a named or unnamed
7996 namespace definition. */
7997 else if (token1
.keyword
== RID_NAMESPACE
7998 && (/* A named namespace definition. */
7999 (token2
.type
== CPP_NAME
8000 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
8002 /* An unnamed namespace definition. */
8003 || token2
.type
== CPP_OPEN_BRACE
8004 || token2
.keyword
== RID_ATTRIBUTE
))
8005 cp_parser_namespace_definition (parser
);
8006 /* An inline (associated) namespace definition. */
8007 else if (token1
.keyword
== RID_INLINE
8008 && token2
.keyword
== RID_NAMESPACE
)
8009 cp_parser_namespace_definition (parser
);
8010 /* Objective-C++ declaration/definition. */
8011 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
8012 cp_parser_objc_declaration (parser
);
8013 /* We must have either a block declaration or a function
8016 /* Try to parse a block-declaration, or a function-definition. */
8017 cp_parser_block_declaration (parser
, /*statement_p=*/false);
8019 /* Free any declarators allocated. */
8020 obstack_free (&declarator_obstack
, p
);
8023 /* Parse a block-declaration.
8028 namespace-alias-definition
8035 __extension__ block-declaration
8040 static_assert-declaration
8042 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8043 part of a declaration-statement. */
8046 cp_parser_block_declaration (cp_parser
*parser
,
8052 /* Check for the `__extension__' keyword. */
8053 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
8055 /* Parse the qualified declaration. */
8056 cp_parser_block_declaration (parser
, statement_p
);
8057 /* Restore the PEDANTIC flag. */
8058 pedantic
= saved_pedantic
;
8063 /* Peek at the next token to figure out which kind of declaration is
8065 token1
= cp_lexer_peek_token (parser
->lexer
);
8067 /* If the next keyword is `asm', we have an asm-definition. */
8068 if (token1
->keyword
== RID_ASM
)
8071 cp_parser_commit_to_tentative_parse (parser
);
8072 cp_parser_asm_definition (parser
);
8074 /* If the next keyword is `namespace', we have a
8075 namespace-alias-definition. */
8076 else if (token1
->keyword
== RID_NAMESPACE
)
8077 cp_parser_namespace_alias_definition (parser
);
8078 /* If the next keyword is `using', we have either a
8079 using-declaration or a using-directive. */
8080 else if (token1
->keyword
== RID_USING
)
8085 cp_parser_commit_to_tentative_parse (parser
);
8086 /* If the token after `using' is `namespace', then we have a
8088 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8089 if (token2
->keyword
== RID_NAMESPACE
)
8090 cp_parser_using_directive (parser
);
8091 /* Otherwise, it's a using-declaration. */
8093 cp_parser_using_declaration (parser
,
8094 /*access_declaration_p=*/false);
8096 /* If the next keyword is `__label__' we have a misplaced label
8098 else if (token1
->keyword
== RID_LABEL
)
8100 cp_lexer_consume_token (parser
->lexer
);
8101 error ("%H%<__label__%> not at the beginning of a block", &token1
->location
);
8102 cp_parser_skip_to_end_of_statement (parser
);
8103 /* If the next token is now a `;', consume it. */
8104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8105 cp_lexer_consume_token (parser
->lexer
);
8107 /* If the next token is `static_assert' we have a static assertion. */
8108 else if (token1
->keyword
== RID_STATIC_ASSERT
)
8109 cp_parser_static_assert (parser
, /*member_p=*/false);
8110 /* Anything else must be a simple-declaration. */
8112 cp_parser_simple_declaration (parser
, !statement_p
);
8115 /* Parse a simple-declaration.
8118 decl-specifier-seq [opt] init-declarator-list [opt] ;
8120 init-declarator-list:
8122 init-declarator-list , init-declarator
8124 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8125 function-definition as a simple-declaration. */
8128 cp_parser_simple_declaration (cp_parser
* parser
,
8129 bool function_definition_allowed_p
)
8131 cp_decl_specifier_seq decl_specifiers
;
8132 int declares_class_or_enum
;
8133 bool saw_declarator
;
8135 /* Defer access checks until we know what is being declared; the
8136 checks for names appearing in the decl-specifier-seq should be
8137 done as if we were in the scope of the thing being declared. */
8138 push_deferring_access_checks (dk_deferred
);
8140 /* Parse the decl-specifier-seq. We have to keep track of whether
8141 or not the decl-specifier-seq declares a named class or
8142 enumeration type, since that is the only case in which the
8143 init-declarator-list is allowed to be empty.
8147 In a simple-declaration, the optional init-declarator-list can be
8148 omitted only when declaring a class or enumeration, that is when
8149 the decl-specifier-seq contains either a class-specifier, an
8150 elaborated-type-specifier, or an enum-specifier. */
8151 cp_parser_decl_specifier_seq (parser
,
8152 CP_PARSER_FLAGS_OPTIONAL
,
8154 &declares_class_or_enum
);
8155 /* We no longer need to defer access checks. */
8156 stop_deferring_access_checks ();
8158 /* In a block scope, a valid declaration must always have a
8159 decl-specifier-seq. By not trying to parse declarators, we can
8160 resolve the declaration/expression ambiguity more quickly. */
8161 if (!function_definition_allowed_p
8162 && !decl_specifiers
.any_specifiers_p
)
8164 cp_parser_error (parser
, "expected declaration");
8168 /* If the next two tokens are both identifiers, the code is
8169 erroneous. The usual cause of this situation is code like:
8173 where "T" should name a type -- but does not. */
8174 if (!decl_specifiers
.type
8175 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
8177 /* If parsing tentatively, we should commit; we really are
8178 looking at a declaration. */
8179 cp_parser_commit_to_tentative_parse (parser
);
8184 /* If we have seen at least one decl-specifier, and the next token
8185 is not a parenthesis, then we must be looking at a declaration.
8186 (After "int (" we might be looking at a functional cast.) */
8187 if (decl_specifiers
.any_specifiers_p
8188 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
8189 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
8190 && !cp_parser_error_occurred (parser
))
8191 cp_parser_commit_to_tentative_parse (parser
);
8193 /* Keep going until we hit the `;' at the end of the simple
8195 saw_declarator
= false;
8196 while (cp_lexer_next_token_is_not (parser
->lexer
,
8200 bool function_definition_p
;
8205 /* If we are processing next declarator, coma is expected */
8206 token
= cp_lexer_peek_token (parser
->lexer
);
8207 gcc_assert (token
->type
== CPP_COMMA
);
8208 cp_lexer_consume_token (parser
->lexer
);
8211 saw_declarator
= true;
8213 /* Parse the init-declarator. */
8214 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
8216 function_definition_allowed_p
,
8218 declares_class_or_enum
,
8219 &function_definition_p
);
8220 /* If an error occurred while parsing tentatively, exit quickly.
8221 (That usually happens when in the body of a function; each
8222 statement is treated as a declaration-statement until proven
8224 if (cp_parser_error_occurred (parser
))
8226 /* Handle function definitions specially. */
8227 if (function_definition_p
)
8229 /* If the next token is a `,', then we are probably
8230 processing something like:
8234 which is erroneous. */
8235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8237 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8238 error ("%Hmixing declarations and function-definitions is forbidden",
8241 /* Otherwise, we're done with the list of declarators. */
8244 pop_deferring_access_checks ();
8248 /* The next token should be either a `,' or a `;'. */
8249 token
= cp_lexer_peek_token (parser
->lexer
);
8250 /* If it's a `,', there are more declarators to come. */
8251 if (token
->type
== CPP_COMMA
)
8252 /* will be consumed next time around */;
8253 /* If it's a `;', we are done. */
8254 else if (token
->type
== CPP_SEMICOLON
)
8256 /* Anything else is an error. */
8259 /* If we have already issued an error message we don't need
8260 to issue another one. */
8261 if (decl
!= error_mark_node
8262 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
8263 cp_parser_error (parser
, "expected %<,%> or %<;%>");
8264 /* Skip tokens until we reach the end of the statement. */
8265 cp_parser_skip_to_end_of_statement (parser
);
8266 /* If the next token is now a `;', consume it. */
8267 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8268 cp_lexer_consume_token (parser
->lexer
);
8271 /* After the first time around, a function-definition is not
8272 allowed -- even if it was OK at first. For example:
8277 function_definition_allowed_p
= false;
8280 /* Issue an error message if no declarators are present, and the
8281 decl-specifier-seq does not itself declare a class or
8283 if (!saw_declarator
)
8285 if (cp_parser_declares_only_class_p (parser
))
8286 shadow_tag (&decl_specifiers
);
8287 /* Perform any deferred access checks. */
8288 perform_deferred_access_checks ();
8291 /* Consume the `;'. */
8292 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8295 pop_deferring_access_checks ();
8298 /* Parse a decl-specifier-seq.
8301 decl-specifier-seq [opt] decl-specifier
8304 storage-class-specifier
8315 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8317 The parser flags FLAGS is used to control type-specifier parsing.
8319 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8322 1: one of the decl-specifiers is an elaborated-type-specifier
8323 (i.e., a type declaration)
8324 2: one of the decl-specifiers is an enum-specifier or a
8325 class-specifier (i.e., a type definition)
8330 cp_parser_decl_specifier_seq (cp_parser
* parser
,
8331 cp_parser_flags flags
,
8332 cp_decl_specifier_seq
*decl_specs
,
8333 int* declares_class_or_enum
)
8335 bool constructor_possible_p
= !parser
->in_declarator_p
;
8336 cp_token
*start_token
= NULL
;
8338 /* Clear DECL_SPECS. */
8339 clear_decl_specs (decl_specs
);
8341 /* Assume no class or enumeration type is declared. */
8342 *declares_class_or_enum
= 0;
8344 /* Keep reading specifiers until there are no more to read. */
8348 bool found_decl_spec
;
8351 /* Peek at the next token. */
8352 token
= cp_lexer_peek_token (parser
->lexer
);
8354 /* Save the first token of the decl spec list for error
8357 start_token
= token
;
8358 /* Handle attributes. */
8359 if (token
->keyword
== RID_ATTRIBUTE
)
8361 /* Parse the attributes. */
8362 decl_specs
->attributes
8363 = chainon (decl_specs
->attributes
,
8364 cp_parser_attributes_opt (parser
));
8367 /* Assume we will find a decl-specifier keyword. */
8368 found_decl_spec
= true;
8369 /* If the next token is an appropriate keyword, we can simply
8370 add it to the list. */
8371 switch (token
->keyword
)
8376 if (!at_class_scope_p ())
8378 error ("%H%<friend%> used outside of class", &token
->location
);
8379 cp_lexer_purge_token (parser
->lexer
);
8383 ++decl_specs
->specs
[(int) ds_friend
];
8384 /* Consume the token. */
8385 cp_lexer_consume_token (parser
->lexer
);
8389 /* function-specifier:
8396 cp_parser_function_specifier_opt (parser
, decl_specs
);
8402 ++decl_specs
->specs
[(int) ds_typedef
];
8403 /* Consume the token. */
8404 cp_lexer_consume_token (parser
->lexer
);
8405 /* A constructor declarator cannot appear in a typedef. */
8406 constructor_possible_p
= false;
8407 /* The "typedef" keyword can only occur in a declaration; we
8408 may as well commit at this point. */
8409 cp_parser_commit_to_tentative_parse (parser
);
8411 if (decl_specs
->storage_class
!= sc_none
)
8412 decl_specs
->conflicting_specifiers_p
= true;
8415 /* storage-class-specifier:
8425 if (cxx_dialect
== cxx98
)
8427 /* Consume the token. */
8428 cp_lexer_consume_token (parser
->lexer
);
8430 /* Complain about `auto' as a storage specifier, if
8431 we're complaining about C++0x compatibility. */
8434 "%H%<auto%> will change meaning in C++0x; please remove it",
8437 /* Set the storage class anyway. */
8438 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
8442 /* C++0x auto type-specifier. */
8443 found_decl_spec
= false;
8450 /* Consume the token. */
8451 cp_lexer_consume_token (parser
->lexer
);
8452 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
8456 /* Consume the token. */
8457 cp_lexer_consume_token (parser
->lexer
);
8458 ++decl_specs
->specs
[(int) ds_thread
];
8462 /* We did not yet find a decl-specifier yet. */
8463 found_decl_spec
= false;
8467 /* Constructors are a special case. The `S' in `S()' is not a
8468 decl-specifier; it is the beginning of the declarator. */
8471 && constructor_possible_p
8472 && (cp_parser_constructor_declarator_p
8473 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
8475 /* If we don't have a DECL_SPEC yet, then we must be looking at
8476 a type-specifier. */
8477 if (!found_decl_spec
&& !constructor_p
)
8479 int decl_spec_declares_class_or_enum
;
8480 bool is_cv_qualifier
;
8484 = cp_parser_type_specifier (parser
, flags
,
8486 /*is_declaration=*/true,
8487 &decl_spec_declares_class_or_enum
,
8489 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
8491 /* If this type-specifier referenced a user-defined type
8492 (a typedef, class-name, etc.), then we can't allow any
8493 more such type-specifiers henceforth.
8497 The longest sequence of decl-specifiers that could
8498 possibly be a type name is taken as the
8499 decl-specifier-seq of a declaration. The sequence shall
8500 be self-consistent as described below.
8504 As a general rule, at most one type-specifier is allowed
8505 in the complete decl-specifier-seq of a declaration. The
8506 only exceptions are the following:
8508 -- const or volatile can be combined with any other
8511 -- signed or unsigned can be combined with char, long,
8519 void g (const int Pc);
8521 Here, Pc is *not* part of the decl-specifier seq; it's
8522 the declarator. Therefore, once we see a type-specifier
8523 (other than a cv-qualifier), we forbid any additional
8524 user-defined types. We *do* still allow things like `int
8525 int' to be considered a decl-specifier-seq, and issue the
8526 error message later. */
8527 if (type_spec
&& !is_cv_qualifier
)
8528 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
8529 /* A constructor declarator cannot follow a type-specifier. */
8532 constructor_possible_p
= false;
8533 found_decl_spec
= true;
8537 /* If we still do not have a DECL_SPEC, then there are no more
8539 if (!found_decl_spec
)
8542 decl_specs
->any_specifiers_p
= true;
8543 /* After we see one decl-specifier, further decl-specifiers are
8545 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
8548 cp_parser_check_decl_spec (decl_specs
, start_token
->location
);
8550 /* Don't allow a friend specifier with a class definition. */
8551 if (decl_specs
->specs
[(int) ds_friend
] != 0
8552 && (*declares_class_or_enum
& 2))
8553 error ("%Hclass definition may not be declared a friend",
8554 &start_token
->location
);
8557 /* Parse an (optional) storage-class-specifier.
8559 storage-class-specifier:
8568 storage-class-specifier:
8571 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8574 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
8576 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
8579 if (cxx_dialect
!= cxx98
)
8581 /* Fall through for C++98. */
8588 /* Consume the token. */
8589 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
8596 /* Parse an (optional) function-specifier.
8603 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8604 Updates DECL_SPECS, if it is non-NULL. */
8607 cp_parser_function_specifier_opt (cp_parser
* parser
,
8608 cp_decl_specifier_seq
*decl_specs
)
8610 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8611 switch (token
->keyword
)
8615 ++decl_specs
->specs
[(int) ds_inline
];
8619 /* 14.5.2.3 [temp.mem]
8621 A member function template shall not be virtual. */
8622 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8623 error ("%Htemplates may not be %<virtual%>", &token
->location
);
8624 else if (decl_specs
)
8625 ++decl_specs
->specs
[(int) ds_virtual
];
8630 ++decl_specs
->specs
[(int) ds_explicit
];
8637 /* Consume the token. */
8638 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
8641 /* Parse a linkage-specification.
8643 linkage-specification:
8644 extern string-literal { declaration-seq [opt] }
8645 extern string-literal declaration */
8648 cp_parser_linkage_specification (cp_parser
* parser
)
8652 /* Look for the `extern' keyword. */
8653 cp_parser_require_keyword (parser
, RID_EXTERN
, "%<extern%>");
8655 /* Look for the string-literal. */
8656 linkage
= cp_parser_string_literal (parser
, false, false);
8658 /* Transform the literal into an identifier. If the literal is a
8659 wide-character string, or contains embedded NULs, then we can't
8660 handle it as the user wants. */
8661 if (strlen (TREE_STRING_POINTER (linkage
))
8662 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
8664 cp_parser_error (parser
, "invalid linkage-specification");
8665 /* Assume C++ linkage. */
8666 linkage
= lang_name_cplusplus
;
8669 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
8671 /* We're now using the new linkage. */
8672 push_lang_context (linkage
);
8674 /* If the next token is a `{', then we're using the first
8676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8678 /* Consume the `{' token. */
8679 cp_lexer_consume_token (parser
->lexer
);
8680 /* Parse the declarations. */
8681 cp_parser_declaration_seq_opt (parser
);
8682 /* Look for the closing `}'. */
8683 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
8685 /* Otherwise, there's just one declaration. */
8688 bool saved_in_unbraced_linkage_specification_p
;
8690 saved_in_unbraced_linkage_specification_p
8691 = parser
->in_unbraced_linkage_specification_p
;
8692 parser
->in_unbraced_linkage_specification_p
= true;
8693 cp_parser_declaration (parser
);
8694 parser
->in_unbraced_linkage_specification_p
8695 = saved_in_unbraced_linkage_specification_p
;
8698 /* We're done with the linkage-specification. */
8699 pop_lang_context ();
8702 /* Parse a static_assert-declaration.
8704 static_assert-declaration:
8705 static_assert ( constant-expression , string-literal ) ;
8707 If MEMBER_P, this static_assert is a class member. */
8710 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
8715 location_t saved_loc
;
8717 /* Peek at the `static_assert' token so we can keep track of exactly
8718 where the static assertion started. */
8719 token
= cp_lexer_peek_token (parser
->lexer
);
8720 saved_loc
= token
->location
;
8722 /* Look for the `static_assert' keyword. */
8723 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
8724 "%<static_assert%>"))
8727 /* We know we are in a static assertion; commit to any tentative
8729 if (cp_parser_parsing_tentatively (parser
))
8730 cp_parser_commit_to_tentative_parse (parser
);
8732 /* Parse the `(' starting the static assertion condition. */
8733 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
8735 /* Parse the constant-expression. */
8737 cp_parser_constant_expression (parser
,
8738 /*allow_non_constant_p=*/false,
8739 /*non_constant_p=*/NULL
);
8741 /* Parse the separating `,'. */
8742 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
8744 /* Parse the string-literal message. */
8745 message
= cp_parser_string_literal (parser
,
8746 /*translate=*/false,
8749 /* A `)' completes the static assertion. */
8750 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
8751 cp_parser_skip_to_closing_parenthesis (parser
,
8752 /*recovering=*/true,
8754 /*consume_paren=*/true);
8756 /* A semicolon terminates the declaration. */
8757 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8759 /* Complete the static assertion, which may mean either processing
8760 the static assert now or saving it for template instantiation. */
8761 finish_static_assert (condition
, message
, saved_loc
, member_p
);
8764 /* Parse a `decltype' type. Returns the type.
8766 simple-type-specifier:
8767 decltype ( expression ) */
8770 cp_parser_decltype (cp_parser
*parser
)
8773 bool id_expression_or_member_access_p
= false;
8774 const char *saved_message
;
8775 bool saved_integral_constant_expression_p
;
8776 bool saved_non_integral_constant_expression_p
;
8777 cp_token
*id_expr_start_token
;
8779 /* Look for the `decltype' token. */
8780 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, "%<decltype%>"))
8781 return error_mark_node
;
8783 /* Types cannot be defined in a `decltype' expression. Save away the
8785 saved_message
= parser
->type_definition_forbidden_message
;
8787 /* And create the new one. */
8788 parser
->type_definition_forbidden_message
8789 = "types may not be defined in %<decltype%> expressions";
8791 /* The restrictions on constant-expressions do not apply inside
8792 decltype expressions. */
8793 saved_integral_constant_expression_p
8794 = parser
->integral_constant_expression_p
;
8795 saved_non_integral_constant_expression_p
8796 = parser
->non_integral_constant_expression_p
;
8797 parser
->integral_constant_expression_p
= false;
8799 /* Do not actually evaluate the expression. */
8802 /* Parse the opening `('. */
8803 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
8804 return error_mark_node
;
8806 /* First, try parsing an id-expression. */
8807 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
8808 cp_parser_parse_tentatively (parser
);
8809 expr
= cp_parser_id_expression (parser
,
8810 /*template_keyword_p=*/false,
8811 /*check_dependency_p=*/true,
8812 /*template_p=*/NULL
,
8813 /*declarator_p=*/false,
8814 /*optional_p=*/false);
8816 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
8818 bool non_integral_constant_expression_p
= false;
8819 tree id_expression
= expr
;
8821 const char *error_msg
;
8823 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
8824 /* Lookup the name we got back from the id-expression. */
8825 expr
= cp_parser_lookup_name (parser
, expr
,
8827 /*is_template=*/false,
8828 /*is_namespace=*/false,
8829 /*check_dependency=*/true,
8830 /*ambiguous_decls=*/NULL
,
8831 id_expr_start_token
->location
);
8834 && expr
!= error_mark_node
8835 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
8836 && TREE_CODE (expr
) != TYPE_DECL
8837 && (TREE_CODE (expr
) != BIT_NOT_EXPR
8838 || !TYPE_P (TREE_OPERAND (expr
, 0)))
8839 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8841 /* Complete lookup of the id-expression. */
8842 expr
= (finish_id_expression
8843 (id_expression
, expr
, parser
->scope
, &idk
,
8844 /*integral_constant_expression_p=*/false,
8845 /*allow_non_integral_constant_expression_p=*/true,
8846 &non_integral_constant_expression_p
,
8847 /*template_p=*/false,
8849 /*address_p=*/false,
8850 /*template_arg_p=*/false,
8852 id_expr_start_token
->location
));
8854 if (expr
== error_mark_node
)
8855 /* We found an id-expression, but it was something that we
8856 should not have found. This is an error, not something
8857 we can recover from, so note that we found an
8858 id-expression and we'll recover as gracefully as
8860 id_expression_or_member_access_p
= true;
8864 && expr
!= error_mark_node
8865 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8866 /* We have an id-expression. */
8867 id_expression_or_member_access_p
= true;
8870 if (!id_expression_or_member_access_p
)
8872 /* Abort the id-expression parse. */
8873 cp_parser_abort_tentative_parse (parser
);
8875 /* Parsing tentatively, again. */
8876 cp_parser_parse_tentatively (parser
);
8878 /* Parse a class member access. */
8879 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
8881 /*member_access_only_p=*/true, NULL
);
8884 && expr
!= error_mark_node
8885 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8886 /* We have an id-expression. */
8887 id_expression_or_member_access_p
= true;
8890 if (id_expression_or_member_access_p
)
8891 /* We have parsed the complete id-expression or member access. */
8892 cp_parser_parse_definitely (parser
);
8895 /* Abort our attempt to parse an id-expression or member access
8897 cp_parser_abort_tentative_parse (parser
);
8899 /* Parse a full expression. */
8900 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8903 /* Go back to evaluating expressions. */
8906 /* Restore the old message and the integral constant expression
8908 parser
->type_definition_forbidden_message
= saved_message
;
8909 parser
->integral_constant_expression_p
8910 = saved_integral_constant_expression_p
;
8911 parser
->non_integral_constant_expression_p
8912 = saved_non_integral_constant_expression_p
;
8914 if (expr
== error_mark_node
)
8916 /* Skip everything up to the closing `)'. */
8917 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8918 /*consume_paren=*/true);
8919 return error_mark_node
;
8922 /* Parse to the closing `)'. */
8923 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
8925 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8926 /*consume_paren=*/true);
8927 return error_mark_node
;
8930 return finish_decltype_type (expr
, id_expression_or_member_access_p
);
8933 /* Special member functions [gram.special] */
8935 /* Parse a conversion-function-id.
8937 conversion-function-id:
8938 operator conversion-type-id
8940 Returns an IDENTIFIER_NODE representing the operator. */
8943 cp_parser_conversion_function_id (cp_parser
* parser
)
8947 tree saved_qualifying_scope
;
8948 tree saved_object_scope
;
8949 tree pushed_scope
= NULL_TREE
;
8951 /* Look for the `operator' token. */
8952 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "%<operator%>"))
8953 return error_mark_node
;
8954 /* When we parse the conversion-type-id, the current scope will be
8955 reset. However, we need that information in able to look up the
8956 conversion function later, so we save it here. */
8957 saved_scope
= parser
->scope
;
8958 saved_qualifying_scope
= parser
->qualifying_scope
;
8959 saved_object_scope
= parser
->object_scope
;
8960 /* We must enter the scope of the class so that the names of
8961 entities declared within the class are available in the
8962 conversion-type-id. For example, consider:
8969 S::operator I() { ... }
8971 In order to see that `I' is a type-name in the definition, we
8972 must be in the scope of `S'. */
8974 pushed_scope
= push_scope (saved_scope
);
8975 /* Parse the conversion-type-id. */
8976 type
= cp_parser_conversion_type_id (parser
);
8977 /* Leave the scope of the class, if any. */
8979 pop_scope (pushed_scope
);
8980 /* Restore the saved scope. */
8981 parser
->scope
= saved_scope
;
8982 parser
->qualifying_scope
= saved_qualifying_scope
;
8983 parser
->object_scope
= saved_object_scope
;
8984 /* If the TYPE is invalid, indicate failure. */
8985 if (type
== error_mark_node
)
8986 return error_mark_node
;
8987 return mangle_conv_op_name_for_type (type
);
8990 /* Parse a conversion-type-id:
8993 type-specifier-seq conversion-declarator [opt]
8995 Returns the TYPE specified. */
8998 cp_parser_conversion_type_id (cp_parser
* parser
)
9001 cp_decl_specifier_seq type_specifiers
;
9002 cp_declarator
*declarator
;
9003 tree type_specified
;
9005 /* Parse the attributes. */
9006 attributes
= cp_parser_attributes_opt (parser
);
9007 /* Parse the type-specifiers. */
9008 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
9010 /* If that didn't work, stop. */
9011 if (type_specifiers
.type
== error_mark_node
)
9012 return error_mark_node
;
9013 /* Parse the conversion-declarator. */
9014 declarator
= cp_parser_conversion_declarator_opt (parser
);
9016 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
9017 /*initialized=*/0, &attributes
);
9019 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
9021 /* Don't give this error when parsing tentatively. This happens to
9022 work because we always parse this definitively once. */
9023 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
9024 && type_uses_auto (type_specified
))
9026 error ("invalid use of %<auto%> in conversion operator");
9027 return error_mark_node
;
9030 return type_specified
;
9033 /* Parse an (optional) conversion-declarator.
9035 conversion-declarator:
9036 ptr-operator conversion-declarator [opt]
9040 static cp_declarator
*
9041 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
9043 enum tree_code code
;
9045 cp_cv_quals cv_quals
;
9047 /* We don't know if there's a ptr-operator next, or not. */
9048 cp_parser_parse_tentatively (parser
);
9049 /* Try the ptr-operator. */
9050 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
9051 /* If it worked, look for more conversion-declarators. */
9052 if (cp_parser_parse_definitely (parser
))
9054 cp_declarator
*declarator
;
9056 /* Parse another optional declarator. */
9057 declarator
= cp_parser_conversion_declarator_opt (parser
);
9059 return cp_parser_make_indirect_declarator
9060 (code
, class_type
, cv_quals
, declarator
);
9066 /* Parse an (optional) ctor-initializer.
9069 : mem-initializer-list
9071 Returns TRUE iff the ctor-initializer was actually present. */
9074 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
9076 /* If the next token is not a `:', then there is no
9077 ctor-initializer. */
9078 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
9080 /* Do default initialization of any bases and members. */
9081 if (DECL_CONSTRUCTOR_P (current_function_decl
))
9082 finish_mem_initializers (NULL_TREE
);
9087 /* Consume the `:' token. */
9088 cp_lexer_consume_token (parser
->lexer
);
9089 /* And the mem-initializer-list. */
9090 cp_parser_mem_initializer_list (parser
);
9095 /* Parse a mem-initializer-list.
9097 mem-initializer-list:
9098 mem-initializer ... [opt]
9099 mem-initializer ... [opt] , mem-initializer-list */
9102 cp_parser_mem_initializer_list (cp_parser
* parser
)
9104 tree mem_initializer_list
= NULL_TREE
;
9105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9107 /* Let the semantic analysis code know that we are starting the
9108 mem-initializer-list. */
9109 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
9110 error ("%Honly constructors take base initializers",
9113 /* Loop through the list. */
9116 tree mem_initializer
;
9118 token
= cp_lexer_peek_token (parser
->lexer
);
9119 /* Parse the mem-initializer. */
9120 mem_initializer
= cp_parser_mem_initializer (parser
);
9121 /* If the next token is a `...', we're expanding member initializers. */
9122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9124 /* Consume the `...'. */
9125 cp_lexer_consume_token (parser
->lexer
);
9127 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9128 can be expanded but members cannot. */
9129 if (mem_initializer
!= error_mark_node
9130 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
9132 error ("%Hcannot expand initializer for member %<%D%>",
9133 &token
->location
, TREE_PURPOSE (mem_initializer
));
9134 mem_initializer
= error_mark_node
;
9137 /* Construct the pack expansion type. */
9138 if (mem_initializer
!= error_mark_node
)
9139 mem_initializer
= make_pack_expansion (mem_initializer
);
9141 /* Add it to the list, unless it was erroneous. */
9142 if (mem_initializer
!= error_mark_node
)
9144 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
9145 mem_initializer_list
= mem_initializer
;
9147 /* If the next token is not a `,', we're done. */
9148 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9150 /* Consume the `,' token. */
9151 cp_lexer_consume_token (parser
->lexer
);
9154 /* Perform semantic analysis. */
9155 if (DECL_CONSTRUCTOR_P (current_function_decl
))
9156 finish_mem_initializers (mem_initializer_list
);
9159 /* Parse a mem-initializer.
9162 mem-initializer-id ( expression-list [opt] )
9163 mem-initializer-id braced-init-list
9168 ( expression-list [opt] )
9170 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9171 class) or FIELD_DECL (for a non-static data member) to initialize;
9172 the TREE_VALUE is the expression-list. An empty initialization
9173 list is represented by void_list_node. */
9176 cp_parser_mem_initializer (cp_parser
* parser
)
9178 tree mem_initializer_id
;
9179 tree expression_list
;
9181 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9183 /* Find out what is being initialized. */
9184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9186 permerror (token
->location
,
9187 "anachronistic old-style base class initializer");
9188 mem_initializer_id
= NULL_TREE
;
9191 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
9192 member
= expand_member_init (mem_initializer_id
);
9193 if (member
&& !DECL_P (member
))
9194 in_base_initializer
= 1;
9196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9198 bool expr_non_constant_p
;
9199 maybe_warn_cpp0x ("extended initializer lists");
9200 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9201 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
9202 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
9206 = cp_parser_parenthesized_expression_list (parser
, false,
9208 /*allow_expansion_p=*/true,
9209 /*non_constant_p=*/NULL
);
9210 if (expression_list
== error_mark_node
)
9211 return error_mark_node
;
9212 if (!expression_list
)
9213 expression_list
= void_type_node
;
9215 in_base_initializer
= 0;
9217 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
9220 /* Parse a mem-initializer-id.
9223 :: [opt] nested-name-specifier [opt] class-name
9226 Returns a TYPE indicating the class to be initializer for the first
9227 production. Returns an IDENTIFIER_NODE indicating the data member
9228 to be initialized for the second production. */
9231 cp_parser_mem_initializer_id (cp_parser
* parser
)
9233 bool global_scope_p
;
9234 bool nested_name_specifier_p
;
9235 bool template_p
= false;
9238 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9240 /* `typename' is not allowed in this context ([temp.res]). */
9241 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
9243 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9244 "member initializer is implicitly a type)",
9246 cp_lexer_consume_token (parser
->lexer
);
9248 /* Look for the optional `::' operator. */
9250 = (cp_parser_global_scope_opt (parser
,
9251 /*current_scope_valid_p=*/false)
9253 /* Look for the optional nested-name-specifier. The simplest way to
9258 The keyword `typename' is not permitted in a base-specifier or
9259 mem-initializer; in these contexts a qualified name that
9260 depends on a template-parameter is implicitly assumed to be a
9263 is to assume that we have seen the `typename' keyword at this
9265 nested_name_specifier_p
9266 = (cp_parser_nested_name_specifier_opt (parser
,
9267 /*typename_keyword_p=*/true,
9268 /*check_dependency_p=*/true,
9270 /*is_declaration=*/true)
9272 if (nested_name_specifier_p
)
9273 template_p
= cp_parser_optional_template_keyword (parser
);
9274 /* If there is a `::' operator or a nested-name-specifier, then we
9275 are definitely looking for a class-name. */
9276 if (global_scope_p
|| nested_name_specifier_p
)
9277 return cp_parser_class_name (parser
,
9278 /*typename_keyword_p=*/true,
9279 /*template_keyword_p=*/template_p
,
9281 /*check_dependency_p=*/true,
9282 /*class_head_p=*/false,
9283 /*is_declaration=*/true);
9284 /* Otherwise, we could also be looking for an ordinary identifier. */
9285 cp_parser_parse_tentatively (parser
);
9286 /* Try a class-name. */
9287 id
= cp_parser_class_name (parser
,
9288 /*typename_keyword_p=*/true,
9289 /*template_keyword_p=*/false,
9291 /*check_dependency_p=*/true,
9292 /*class_head_p=*/false,
9293 /*is_declaration=*/true);
9294 /* If we found one, we're done. */
9295 if (cp_parser_parse_definitely (parser
))
9297 /* Otherwise, look for an ordinary identifier. */
9298 return cp_parser_identifier (parser
);
9301 /* Overloading [gram.over] */
9303 /* Parse an operator-function-id.
9305 operator-function-id:
9308 Returns an IDENTIFIER_NODE for the operator which is a
9309 human-readable spelling of the identifier, e.g., `operator +'. */
9312 cp_parser_operator_function_id (cp_parser
* parser
)
9314 /* Look for the `operator' keyword. */
9315 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "%<operator%>"))
9316 return error_mark_node
;
9317 /* And then the name of the operator itself. */
9318 return cp_parser_operator (parser
);
9321 /* Parse an operator.
9324 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9325 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9326 || ++ -- , ->* -> () []
9333 Returns an IDENTIFIER_NODE for the operator which is a
9334 human-readable spelling of the identifier, e.g., `operator +'. */
9337 cp_parser_operator (cp_parser
* parser
)
9339 tree id
= NULL_TREE
;
9342 /* Peek at the next token. */
9343 token
= cp_lexer_peek_token (parser
->lexer
);
9344 /* Figure out which operator we have. */
9345 switch (token
->type
)
9351 /* The keyword should be either `new' or `delete'. */
9352 if (token
->keyword
== RID_NEW
)
9354 else if (token
->keyword
== RID_DELETE
)
9359 /* Consume the `new' or `delete' token. */
9360 cp_lexer_consume_token (parser
->lexer
);
9362 /* Peek at the next token. */
9363 token
= cp_lexer_peek_token (parser
->lexer
);
9364 /* If it's a `[' token then this is the array variant of the
9366 if (token
->type
== CPP_OPEN_SQUARE
)
9368 /* Consume the `[' token. */
9369 cp_lexer_consume_token (parser
->lexer
);
9370 /* Look for the `]' token. */
9371 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
9372 id
= ansi_opname (op
== NEW_EXPR
9373 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
9375 /* Otherwise, we have the non-array variant. */
9377 id
= ansi_opname (op
);
9383 id
= ansi_opname (PLUS_EXPR
);
9387 id
= ansi_opname (MINUS_EXPR
);
9391 id
= ansi_opname (MULT_EXPR
);
9395 id
= ansi_opname (TRUNC_DIV_EXPR
);
9399 id
= ansi_opname (TRUNC_MOD_EXPR
);
9403 id
= ansi_opname (BIT_XOR_EXPR
);
9407 id
= ansi_opname (BIT_AND_EXPR
);
9411 id
= ansi_opname (BIT_IOR_EXPR
);
9415 id
= ansi_opname (BIT_NOT_EXPR
);
9419 id
= ansi_opname (TRUTH_NOT_EXPR
);
9423 id
= ansi_assopname (NOP_EXPR
);
9427 id
= ansi_opname (LT_EXPR
);
9431 id
= ansi_opname (GT_EXPR
);
9435 id
= ansi_assopname (PLUS_EXPR
);
9439 id
= ansi_assopname (MINUS_EXPR
);
9443 id
= ansi_assopname (MULT_EXPR
);
9447 id
= ansi_assopname (TRUNC_DIV_EXPR
);
9451 id
= ansi_assopname (TRUNC_MOD_EXPR
);
9455 id
= ansi_assopname (BIT_XOR_EXPR
);
9459 id
= ansi_assopname (BIT_AND_EXPR
);
9463 id
= ansi_assopname (BIT_IOR_EXPR
);
9467 id
= ansi_opname (LSHIFT_EXPR
);
9471 id
= ansi_opname (RSHIFT_EXPR
);
9475 id
= ansi_assopname (LSHIFT_EXPR
);
9479 id
= ansi_assopname (RSHIFT_EXPR
);
9483 id
= ansi_opname (EQ_EXPR
);
9487 id
= ansi_opname (NE_EXPR
);
9491 id
= ansi_opname (LE_EXPR
);
9494 case CPP_GREATER_EQ
:
9495 id
= ansi_opname (GE_EXPR
);
9499 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
9503 id
= ansi_opname (TRUTH_ORIF_EXPR
);
9507 id
= ansi_opname (POSTINCREMENT_EXPR
);
9510 case CPP_MINUS_MINUS
:
9511 id
= ansi_opname (PREDECREMENT_EXPR
);
9515 id
= ansi_opname (COMPOUND_EXPR
);
9518 case CPP_DEREF_STAR
:
9519 id
= ansi_opname (MEMBER_REF
);
9523 id
= ansi_opname (COMPONENT_REF
);
9526 case CPP_OPEN_PAREN
:
9527 /* Consume the `('. */
9528 cp_lexer_consume_token (parser
->lexer
);
9529 /* Look for the matching `)'. */
9530 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
9531 return ansi_opname (CALL_EXPR
);
9533 case CPP_OPEN_SQUARE
:
9534 /* Consume the `['. */
9535 cp_lexer_consume_token (parser
->lexer
);
9536 /* Look for the matching `]'. */
9537 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
9538 return ansi_opname (ARRAY_REF
);
9541 /* Anything else is an error. */
9545 /* If we have selected an identifier, we need to consume the
9548 cp_lexer_consume_token (parser
->lexer
);
9549 /* Otherwise, no valid operator name was present. */
9552 cp_parser_error (parser
, "expected operator");
9553 id
= error_mark_node
;
9559 /* Parse a template-declaration.
9561 template-declaration:
9562 export [opt] template < template-parameter-list > declaration
9564 If MEMBER_P is TRUE, this template-declaration occurs within a
9567 The grammar rule given by the standard isn't correct. What
9570 template-declaration:
9571 export [opt] template-parameter-list-seq
9572 decl-specifier-seq [opt] init-declarator [opt] ;
9573 export [opt] template-parameter-list-seq
9576 template-parameter-list-seq:
9577 template-parameter-list-seq [opt]
9578 template < template-parameter-list > */
9581 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
9583 /* Check for `export'. */
9584 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
9586 /* Consume the `export' token. */
9587 cp_lexer_consume_token (parser
->lexer
);
9588 /* Warn that we do not support `export'. */
9589 warning (0, "keyword %<export%> not implemented, and will be ignored");
9592 cp_parser_template_declaration_after_export (parser
, member_p
);
9595 /* Parse a template-parameter-list.
9597 template-parameter-list:
9599 template-parameter-list , template-parameter
9601 Returns a TREE_LIST. Each node represents a template parameter.
9602 The nodes are connected via their TREE_CHAINs. */
9605 cp_parser_template_parameter_list (cp_parser
* parser
)
9607 tree parameter_list
= NULL_TREE
;
9609 begin_template_parm_list ();
9614 bool is_parameter_pack
;
9616 /* Parse the template-parameter. */
9617 parameter
= cp_parser_template_parameter (parser
,
9619 &is_parameter_pack
);
9620 /* Add it to the list. */
9621 if (parameter
!= error_mark_node
)
9622 parameter_list
= process_template_parm (parameter_list
,
9628 tree err_parm
= build_tree_list (parameter
, parameter
);
9629 TREE_VALUE (err_parm
) = error_mark_node
;
9630 parameter_list
= chainon (parameter_list
, err_parm
);
9633 /* If the next token is not a `,', we're done. */
9634 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9636 /* Otherwise, consume the `,' token. */
9637 cp_lexer_consume_token (parser
->lexer
);
9640 return end_template_parm_list (parameter_list
);
9643 /* Parse a template-parameter.
9647 parameter-declaration
9649 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9650 the parameter. The TREE_PURPOSE is the default value, if any.
9651 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9652 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9653 set to true iff this parameter is a parameter pack. */
9656 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
9657 bool *is_parameter_pack
)
9660 cp_parameter_declarator
*parameter_declarator
;
9661 cp_declarator
*id_declarator
;
9664 /* Assume it is a type parameter or a template parameter. */
9665 *is_non_type
= false;
9666 /* Assume it not a parameter pack. */
9667 *is_parameter_pack
= false;
9668 /* Peek at the next token. */
9669 token
= cp_lexer_peek_token (parser
->lexer
);
9670 /* If it is `class' or `template', we have a type-parameter. */
9671 if (token
->keyword
== RID_TEMPLATE
)
9672 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9673 /* If it is `class' or `typename' we do not know yet whether it is a
9674 type parameter or a non-type parameter. Consider:
9676 template <typename T, typename T::X X> ...
9680 template <class C, class D*> ...
9682 Here, the first parameter is a type parameter, and the second is
9683 a non-type parameter. We can tell by looking at the token after
9684 the identifier -- if it is a `,', `=', or `>' then we have a type
9686 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
9688 /* Peek at the token after `class' or `typename'. */
9689 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9690 /* If it's an ellipsis, we have a template type parameter
9692 if (token
->type
== CPP_ELLIPSIS
)
9693 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9694 /* If it's an identifier, skip it. */
9695 if (token
->type
== CPP_NAME
)
9696 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
9697 /* Now, see if the token looks like the end of a template
9699 if (token
->type
== CPP_COMMA
9700 || token
->type
== CPP_EQ
9701 || token
->type
== CPP_GREATER
)
9702 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9705 /* Otherwise, it is a non-type parameter.
9709 When parsing a default template-argument for a non-type
9710 template-parameter, the first non-nested `>' is taken as the end
9711 of the template parameter-list rather than a greater-than
9713 *is_non_type
= true;
9714 parameter_declarator
9715 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
9716 /*parenthesized_p=*/NULL
);
9718 /* If the parameter declaration is marked as a parameter pack, set
9719 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9720 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9722 if (parameter_declarator
9723 && parameter_declarator
->declarator
9724 && parameter_declarator
->declarator
->parameter_pack_p
)
9726 *is_parameter_pack
= true;
9727 parameter_declarator
->declarator
->parameter_pack_p
= false;
9730 /* If the next token is an ellipsis, and we don't already have it
9731 marked as a parameter pack, then we have a parameter pack (that
9732 has no declarator). */
9733 if (!*is_parameter_pack
9734 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
9735 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
9737 /* Consume the `...'. */
9738 cp_lexer_consume_token (parser
->lexer
);
9739 maybe_warn_variadic_templates ();
9741 *is_parameter_pack
= true;
9743 /* We might end up with a pack expansion as the type of the non-type
9744 template parameter, in which case this is a non-type template
9746 else if (parameter_declarator
9747 && parameter_declarator
->decl_specifiers
.type
9748 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
9750 *is_parameter_pack
= true;
9751 parameter_declarator
->decl_specifiers
.type
=
9752 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
9755 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9757 /* Parameter packs cannot have default arguments. However, a
9758 user may try to do so, so we'll parse them and give an
9759 appropriate diagnostic here. */
9761 /* Consume the `='. */
9762 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
9763 cp_lexer_consume_token (parser
->lexer
);
9765 /* Find the name of the parameter pack. */
9766 id_declarator
= parameter_declarator
->declarator
;
9767 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
9768 id_declarator
= id_declarator
->declarator
;
9770 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
9771 error ("%Htemplate parameter pack %qD cannot have a default argument",
9772 &start_token
->location
, id_declarator
->u
.id
.unqualified_name
);
9774 error ("%Htemplate parameter pack cannot have a default argument",
9775 &start_token
->location
);
9777 /* Parse the default argument, but throw away the result. */
9778 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
9781 parm
= grokdeclarator (parameter_declarator
->declarator
,
9782 ¶meter_declarator
->decl_specifiers
,
9783 PARM
, /*initialized=*/0,
9785 if (parm
== error_mark_node
)
9786 return error_mark_node
;
9788 return build_tree_list (parameter_declarator
->default_argument
, parm
);
9791 /* Parse a type-parameter.
9794 class identifier [opt]
9795 class identifier [opt] = type-id
9796 typename identifier [opt]
9797 typename identifier [opt] = type-id
9798 template < template-parameter-list > class identifier [opt]
9799 template < template-parameter-list > class identifier [opt]
9802 GNU Extension (variadic templates):
9805 class ... identifier [opt]
9806 typename ... identifier [opt]
9808 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9809 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9810 the declaration of the parameter.
9812 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9815 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
9820 /* Look for a keyword to tell us what kind of parameter this is. */
9821 token
= cp_parser_require (parser
, CPP_KEYWORD
,
9822 "%<class%>, %<typename%>, or %<template%>");
9824 return error_mark_node
;
9826 switch (token
->keyword
)
9832 tree default_argument
;
9834 /* If the next token is an ellipsis, we have a template
9836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9838 /* Consume the `...' token. */
9839 cp_lexer_consume_token (parser
->lexer
);
9840 maybe_warn_variadic_templates ();
9842 *is_parameter_pack
= true;
9845 /* If the next token is an identifier, then it names the
9847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9848 identifier
= cp_parser_identifier (parser
);
9850 identifier
= NULL_TREE
;
9852 /* Create the parameter. */
9853 parameter
= finish_template_type_parm (class_type_node
, identifier
);
9855 /* If the next token is an `=', we have a default argument. */
9856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9858 /* Consume the `=' token. */
9859 cp_lexer_consume_token (parser
->lexer
);
9860 /* Parse the default-argument. */
9861 push_deferring_access_checks (dk_no_deferred
);
9862 default_argument
= cp_parser_type_id (parser
);
9864 /* Template parameter packs cannot have default
9866 if (*is_parameter_pack
)
9869 error ("%Htemplate parameter pack %qD cannot have a "
9870 "default argument", &token
->location
, identifier
);
9872 error ("%Htemplate parameter packs cannot have "
9873 "default arguments", &token
->location
);
9874 default_argument
= NULL_TREE
;
9876 pop_deferring_access_checks ();
9879 default_argument
= NULL_TREE
;
9881 /* Create the combined representation of the parameter and the
9882 default argument. */
9883 parameter
= build_tree_list (default_argument
, parameter
);
9889 tree parameter_list
;
9891 tree default_argument
;
9893 /* Look for the `<'. */
9894 cp_parser_require (parser
, CPP_LESS
, "%<<%>");
9895 /* Parse the template-parameter-list. */
9896 parameter_list
= cp_parser_template_parameter_list (parser
);
9897 /* Look for the `>'. */
9898 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
9899 /* Look for the `class' keyword. */
9900 cp_parser_require_keyword (parser
, RID_CLASS
, "%<class%>");
9901 /* If the next token is an ellipsis, we have a template
9903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9905 /* Consume the `...' token. */
9906 cp_lexer_consume_token (parser
->lexer
);
9907 maybe_warn_variadic_templates ();
9909 *is_parameter_pack
= true;
9911 /* If the next token is an `=', then there is a
9912 default-argument. If the next token is a `>', we are at
9913 the end of the parameter-list. If the next token is a `,',
9914 then we are at the end of this parameter. */
9915 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9916 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
9917 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9919 identifier
= cp_parser_identifier (parser
);
9920 /* Treat invalid names as if the parameter were nameless. */
9921 if (identifier
== error_mark_node
)
9922 identifier
= NULL_TREE
;
9925 identifier
= NULL_TREE
;
9927 /* Create the template parameter. */
9928 parameter
= finish_template_template_parm (class_type_node
,
9931 /* If the next token is an `=', then there is a
9932 default-argument. */
9933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9937 /* Consume the `='. */
9938 cp_lexer_consume_token (parser
->lexer
);
9939 /* Parse the id-expression. */
9940 push_deferring_access_checks (dk_no_deferred
);
9941 /* save token before parsing the id-expression, for error
9943 token
= cp_lexer_peek_token (parser
->lexer
);
9945 = cp_parser_id_expression (parser
,
9946 /*template_keyword_p=*/false,
9947 /*check_dependency_p=*/true,
9948 /*template_p=*/&is_template
,
9949 /*declarator_p=*/false,
9950 /*optional_p=*/false);
9951 if (TREE_CODE (default_argument
) == TYPE_DECL
)
9952 /* If the id-expression was a template-id that refers to
9953 a template-class, we already have the declaration here,
9954 so no further lookup is needed. */
9957 /* Look up the name. */
9959 = cp_parser_lookup_name (parser
, default_argument
,
9961 /*is_template=*/is_template
,
9962 /*is_namespace=*/false,
9963 /*check_dependency=*/true,
9964 /*ambiguous_decls=*/NULL
,
9966 /* See if the default argument is valid. */
9968 = check_template_template_default_arg (default_argument
);
9970 /* Template parameter packs cannot have default
9972 if (*is_parameter_pack
)
9975 error ("%Htemplate parameter pack %qD cannot "
9976 "have a default argument",
9977 &token
->location
, identifier
);
9979 error ("%Htemplate parameter packs cannot "
9980 "have default arguments",
9982 default_argument
= NULL_TREE
;
9984 pop_deferring_access_checks ();
9987 default_argument
= NULL_TREE
;
9989 /* Create the combined representation of the parameter and the
9990 default argument. */
9991 parameter
= build_tree_list (default_argument
, parameter
);
10003 /* Parse a template-id.
10006 template-name < template-argument-list [opt] >
10008 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10009 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10010 returned. Otherwise, if the template-name names a function, or set
10011 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10012 names a class, returns a TYPE_DECL for the specialization.
10014 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10015 uninstantiated templates. */
10018 cp_parser_template_id (cp_parser
*parser
,
10019 bool template_keyword_p
,
10020 bool check_dependency_p
,
10021 bool is_declaration
)
10027 cp_token_position start_of_id
= 0;
10028 deferred_access_check
*chk
;
10029 VEC (deferred_access_check
,gc
) *access_check
;
10030 cp_token
*next_token
= NULL
, *next_token_2
= NULL
, *token
= NULL
;
10031 bool is_identifier
;
10033 /* If the next token corresponds to a template-id, there is no need
10035 next_token
= cp_lexer_peek_token (parser
->lexer
);
10036 if (next_token
->type
== CPP_TEMPLATE_ID
)
10038 struct tree_check
*check_value
;
10040 /* Get the stored value. */
10041 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
10042 /* Perform any access checks that were deferred. */
10043 access_check
= check_value
->checks
;
10047 VEC_iterate (deferred_access_check
, access_check
, i
, chk
) ;
10050 perform_or_defer_access_check (chk
->binfo
,
10055 /* Return the stored value. */
10056 return check_value
->value
;
10059 /* Avoid performing name lookup if there is no possibility of
10060 finding a template-id. */
10061 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
10062 || (next_token
->type
== CPP_NAME
10063 && !cp_parser_nth_token_starts_template_argument_list_p
10066 cp_parser_error (parser
, "expected template-id");
10067 return error_mark_node
;
10070 /* Remember where the template-id starts. */
10071 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
10072 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
10074 push_deferring_access_checks (dk_deferred
);
10076 /* Parse the template-name. */
10077 is_identifier
= false;
10078 token
= cp_lexer_peek_token (parser
->lexer
);
10079 templ
= cp_parser_template_name (parser
, template_keyword_p
,
10080 check_dependency_p
,
10083 if (templ
== error_mark_node
|| is_identifier
)
10085 pop_deferring_access_checks ();
10089 /* If we find the sequence `[:' after a template-name, it's probably
10090 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10091 parse correctly the argument list. */
10092 next_token
= cp_lexer_peek_token (parser
->lexer
);
10093 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10094 if (next_token
->type
== CPP_OPEN_SQUARE
10095 && next_token
->flags
& DIGRAPH
10096 && next_token_2
->type
== CPP_COLON
10097 && !(next_token_2
->flags
& PREV_WHITE
))
10099 cp_parser_parse_tentatively (parser
);
10100 /* Change `:' into `::'. */
10101 next_token_2
->type
= CPP_SCOPE
;
10102 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10104 cp_lexer_consume_token (parser
->lexer
);
10106 /* Parse the arguments. */
10107 arguments
= cp_parser_enclosed_template_argument_list (parser
);
10108 if (!cp_parser_parse_definitely (parser
))
10110 /* If we couldn't parse an argument list, then we revert our changes
10111 and return simply an error. Maybe this is not a template-id
10113 next_token_2
->type
= CPP_COLON
;
10114 cp_parser_error (parser
, "expected %<<%>");
10115 pop_deferring_access_checks ();
10116 return error_mark_node
;
10118 /* Otherwise, emit an error about the invalid digraph, but continue
10119 parsing because we got our argument list. */
10120 if (permerror (next_token
->location
,
10121 "%<<::%> cannot begin a template-argument list"))
10123 static bool hint
= false;
10124 inform (next_token
->location
,
10125 "%<<:%> is an alternate spelling for %<[%>."
10126 " Insert whitespace between %<<%> and %<::%>");
10127 if (!hint
&& !flag_permissive
)
10129 inform (next_token
->location
, "(if you use %<-fpermissive%>"
10130 " G++ will accept your code)");
10137 /* Look for the `<' that starts the template-argument-list. */
10138 if (!cp_parser_require (parser
, CPP_LESS
, "%<<%>"))
10140 pop_deferring_access_checks ();
10141 return error_mark_node
;
10143 /* Parse the arguments. */
10144 arguments
= cp_parser_enclosed_template_argument_list (parser
);
10147 /* Build a representation of the specialization. */
10148 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
10149 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, templ
, arguments
);
10150 else if (DECL_CLASS_TEMPLATE_P (templ
)
10151 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
10153 bool entering_scope
;
10154 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10155 template (rather than some instantiation thereof) only if
10156 is not nested within some other construct. For example, in
10157 "template <typename T> void f(T) { A<T>::", A<T> is just an
10158 instantiation of A. */
10159 entering_scope
= (template_parm_scope_p ()
10160 && cp_lexer_next_token_is (parser
->lexer
,
10163 = finish_template_type (templ
, arguments
, entering_scope
);
10167 /* If it's not a class-template or a template-template, it should be
10168 a function-template. */
10169 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
10170 || TREE_CODE (templ
) == OVERLOAD
10171 || BASELINK_P (templ
)));
10173 template_id
= lookup_template_function (templ
, arguments
);
10176 /* If parsing tentatively, replace the sequence of tokens that makes
10177 up the template-id with a CPP_TEMPLATE_ID token. That way,
10178 should we re-parse the token stream, we will not have to repeat
10179 the effort required to do the parse, nor will we issue duplicate
10180 error messages about problems during instantiation of the
10184 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
10186 /* Reset the contents of the START_OF_ID token. */
10187 token
->type
= CPP_TEMPLATE_ID
;
10188 /* Retrieve any deferred checks. Do not pop this access checks yet
10189 so the memory will not be reclaimed during token replacing below. */
10190 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
10191 token
->u
.tree_check_value
->value
= template_id
;
10192 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
10193 token
->keyword
= RID_MAX
;
10195 /* Purge all subsequent tokens. */
10196 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
10198 /* ??? Can we actually assume that, if template_id ==
10199 error_mark_node, we will have issued a diagnostic to the
10200 user, as opposed to simply marking the tentative parse as
10202 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
10203 error ("%Hparse error in template argument list",
10207 pop_deferring_access_checks ();
10208 return template_id
;
10211 /* Parse a template-name.
10216 The standard should actually say:
10220 operator-function-id
10222 A defect report has been filed about this issue.
10224 A conversion-function-id cannot be a template name because they cannot
10225 be part of a template-id. In fact, looking at this code:
10227 a.operator K<int>()
10229 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10230 It is impossible to call a templated conversion-function-id with an
10231 explicit argument list, since the only allowed template parameter is
10232 the type to which it is converting.
10234 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10235 `template' keyword, in a construction like:
10239 In that case `f' is taken to be a template-name, even though there
10240 is no way of knowing for sure.
10242 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10243 name refers to a set of overloaded functions, at least one of which
10244 is a template, or an IDENTIFIER_NODE with the name of the template,
10245 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10246 names are looked up inside uninstantiated templates. */
10249 cp_parser_template_name (cp_parser
* parser
,
10250 bool template_keyword_p
,
10251 bool check_dependency_p
,
10252 bool is_declaration
,
10253 bool *is_identifier
)
10258 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10260 /* If the next token is `operator', then we have either an
10261 operator-function-id or a conversion-function-id. */
10262 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
10264 /* We don't know whether we're looking at an
10265 operator-function-id or a conversion-function-id. */
10266 cp_parser_parse_tentatively (parser
);
10267 /* Try an operator-function-id. */
10268 identifier
= cp_parser_operator_function_id (parser
);
10269 /* If that didn't work, try a conversion-function-id. */
10270 if (!cp_parser_parse_definitely (parser
))
10272 cp_parser_error (parser
, "expected template-name");
10273 return error_mark_node
;
10276 /* Look for the identifier. */
10278 identifier
= cp_parser_identifier (parser
);
10280 /* If we didn't find an identifier, we don't have a template-id. */
10281 if (identifier
== error_mark_node
)
10282 return error_mark_node
;
10284 /* If the name immediately followed the `template' keyword, then it
10285 is a template-name. However, if the next token is not `<', then
10286 we do not treat it as a template-name, since it is not being used
10287 as part of a template-id. This enables us to handle constructs
10290 template <typename T> struct S { S(); };
10291 template <typename T> S<T>::S();
10293 correctly. We would treat `S' as a template -- if it were `S<T>'
10294 -- but we do not if there is no `<'. */
10296 if (processing_template_decl
10297 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
10299 /* In a declaration, in a dependent context, we pretend that the
10300 "template" keyword was present in order to improve error
10301 recovery. For example, given:
10303 template <typename T> void f(T::X<int>);
10305 we want to treat "X<int>" as a template-id. */
10307 && !template_keyword_p
10308 && parser
->scope
&& TYPE_P (parser
->scope
)
10309 && check_dependency_p
10310 && dependent_type_p (parser
->scope
)
10311 /* Do not do this for dtors (or ctors), since they never
10312 need the template keyword before their name. */
10313 && !constructor_name_p (identifier
, parser
->scope
))
10315 cp_token_position start
= 0;
10317 /* Explain what went wrong. */
10318 error ("%Hnon-template %qD used as template",
10319 &token
->location
, identifier
);
10320 inform (input_location
, "use %<%T::template %D%> to indicate that it is a template",
10321 parser
->scope
, identifier
);
10322 /* If parsing tentatively, find the location of the "<" token. */
10323 if (cp_parser_simulate_error (parser
))
10324 start
= cp_lexer_token_position (parser
->lexer
, true);
10325 /* Parse the template arguments so that we can issue error
10326 messages about them. */
10327 cp_lexer_consume_token (parser
->lexer
);
10328 cp_parser_enclosed_template_argument_list (parser
);
10329 /* Skip tokens until we find a good place from which to
10330 continue parsing. */
10331 cp_parser_skip_to_closing_parenthesis (parser
,
10332 /*recovering=*/true,
10334 /*consume_paren=*/false);
10335 /* If parsing tentatively, permanently remove the
10336 template argument list. That will prevent duplicate
10337 error messages from being issued about the missing
10338 "template" keyword. */
10340 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
10342 *is_identifier
= true;
10346 /* If the "template" keyword is present, then there is generally
10347 no point in doing name-lookup, so we just return IDENTIFIER.
10348 But, if the qualifying scope is non-dependent then we can
10349 (and must) do name-lookup normally. */
10350 if (template_keyword_p
10352 || (TYPE_P (parser
->scope
)
10353 && dependent_type_p (parser
->scope
))))
10357 /* Look up the name. */
10358 decl
= cp_parser_lookup_name (parser
, identifier
,
10360 /*is_template=*/false,
10361 /*is_namespace=*/false,
10362 check_dependency_p
,
10363 /*ambiguous_decls=*/NULL
,
10365 decl
= maybe_get_template_decl_from_type_decl (decl
);
10367 /* If DECL is a template, then the name was a template-name. */
10368 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10372 tree fn
= NULL_TREE
;
10374 /* The standard does not explicitly indicate whether a name that
10375 names a set of overloaded declarations, some of which are
10376 templates, is a template-name. However, such a name should
10377 be a template-name; otherwise, there is no way to form a
10378 template-id for the overloaded templates. */
10379 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
10380 if (TREE_CODE (fns
) == OVERLOAD
)
10381 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
10382 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
10387 /* The name does not name a template. */
10388 cp_parser_error (parser
, "expected template-name");
10389 return error_mark_node
;
10393 /* If DECL is dependent, and refers to a function, then just return
10394 its name; we will look it up again during template instantiation. */
10395 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
10397 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
10398 if (TYPE_P (scope
) && dependent_type_p (scope
))
10405 /* Parse a template-argument-list.
10407 template-argument-list:
10408 template-argument ... [opt]
10409 template-argument-list , template-argument ... [opt]
10411 Returns a TREE_VEC containing the arguments. */
10414 cp_parser_template_argument_list (cp_parser
* parser
)
10416 tree fixed_args
[10];
10417 unsigned n_args
= 0;
10418 unsigned alloced
= 10;
10419 tree
*arg_ary
= fixed_args
;
10421 bool saved_in_template_argument_list_p
;
10423 bool saved_non_ice_p
;
10425 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
10426 parser
->in_template_argument_list_p
= true;
10427 /* Even if the template-id appears in an integral
10428 constant-expression, the contents of the argument list do
10430 saved_ice_p
= parser
->integral_constant_expression_p
;
10431 parser
->integral_constant_expression_p
= false;
10432 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
10433 parser
->non_integral_constant_expression_p
= false;
10434 /* Parse the arguments. */
10440 /* Consume the comma. */
10441 cp_lexer_consume_token (parser
->lexer
);
10443 /* Parse the template-argument. */
10444 argument
= cp_parser_template_argument (parser
);
10446 /* If the next token is an ellipsis, we're expanding a template
10448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10450 /* Consume the `...' token. */
10451 cp_lexer_consume_token (parser
->lexer
);
10453 /* Make the argument into a TYPE_PACK_EXPANSION or
10454 EXPR_PACK_EXPANSION. */
10455 argument
= make_pack_expansion (argument
);
10458 if (n_args
== alloced
)
10462 if (arg_ary
== fixed_args
)
10464 arg_ary
= XNEWVEC (tree
, alloced
);
10465 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
10468 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
10470 arg_ary
[n_args
++] = argument
;
10472 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
10474 vec
= make_tree_vec (n_args
);
10477 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
10479 if (arg_ary
!= fixed_args
)
10481 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
10482 parser
->integral_constant_expression_p
= saved_ice_p
;
10483 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
10487 /* Parse a template-argument.
10490 assignment-expression
10494 The representation is that of an assignment-expression, type-id, or
10495 id-expression -- except that the qualified id-expression is
10496 evaluated, so that the value returned is either a DECL or an
10499 Although the standard says "assignment-expression", it forbids
10500 throw-expressions or assignments in the template argument.
10501 Therefore, we use "conditional-expression" instead. */
10504 cp_parser_template_argument (cp_parser
* parser
)
10509 bool maybe_type_id
= false;
10510 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
10513 /* There's really no way to know what we're looking at, so we just
10514 try each alternative in order.
10518 In a template-argument, an ambiguity between a type-id and an
10519 expression is resolved to a type-id, regardless of the form of
10520 the corresponding template-parameter.
10522 Therefore, we try a type-id first. */
10523 cp_parser_parse_tentatively (parser
);
10524 argument
= cp_parser_type_id (parser
);
10525 /* If there was no error parsing the type-id but the next token is a
10526 '>>', our behavior depends on which dialect of C++ we're
10527 parsing. In C++98, we probably found a typo for '> >'. But there
10528 are type-id which are also valid expressions. For instance:
10530 struct X { int operator >> (int); };
10531 template <int V> struct Foo {};
10534 Here 'X()' is a valid type-id of a function type, but the user just
10535 wanted to write the expression "X() >> 5". Thus, we remember that we
10536 found a valid type-id, but we still try to parse the argument as an
10537 expression to see what happens.
10539 In C++0x, the '>>' will be considered two separate '>'
10541 if (!cp_parser_error_occurred (parser
)
10542 && cxx_dialect
== cxx98
10543 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
10545 maybe_type_id
= true;
10546 cp_parser_abort_tentative_parse (parser
);
10550 /* If the next token isn't a `,' or a `>', then this argument wasn't
10551 really finished. This means that the argument is not a valid
10553 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10554 cp_parser_error (parser
, "expected template-argument");
10555 /* If that worked, we're done. */
10556 if (cp_parser_parse_definitely (parser
))
10559 /* We're still not sure what the argument will be. */
10560 cp_parser_parse_tentatively (parser
);
10561 /* Try a template. */
10562 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
10563 argument
= cp_parser_id_expression (parser
,
10564 /*template_keyword_p=*/false,
10565 /*check_dependency_p=*/true,
10567 /*declarator_p=*/false,
10568 /*optional_p=*/false);
10569 /* If the next token isn't a `,' or a `>', then this argument wasn't
10570 really finished. */
10571 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10572 cp_parser_error (parser
, "expected template-argument");
10573 if (!cp_parser_error_occurred (parser
))
10575 /* Figure out what is being referred to. If the id-expression
10576 was for a class template specialization, then we will have a
10577 TYPE_DECL at this point. There is no need to do name lookup
10578 at this point in that case. */
10579 if (TREE_CODE (argument
) != TYPE_DECL
)
10580 argument
= cp_parser_lookup_name (parser
, argument
,
10582 /*is_template=*/template_p
,
10583 /*is_namespace=*/false,
10584 /*check_dependency=*/true,
10585 /*ambiguous_decls=*/NULL
,
10586 argument_start_token
->location
);
10587 if (TREE_CODE (argument
) != TEMPLATE_DECL
10588 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
10589 cp_parser_error (parser
, "expected template-name");
10591 if (cp_parser_parse_definitely (parser
))
10593 /* It must be a non-type argument. There permitted cases are given
10594 in [temp.arg.nontype]:
10596 -- an integral constant-expression of integral or enumeration
10599 -- the name of a non-type template-parameter; or
10601 -- the name of an object or function with external linkage...
10603 -- the address of an object or function with external linkage...
10605 -- a pointer to member... */
10606 /* Look for a non-type template parameter. */
10607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10609 cp_parser_parse_tentatively (parser
);
10610 argument
= cp_parser_primary_expression (parser
,
10611 /*address_p=*/false,
10613 /*template_arg_p=*/true,
10615 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
10616 || !cp_parser_next_token_ends_template_argument_p (parser
))
10617 cp_parser_simulate_error (parser
);
10618 if (cp_parser_parse_definitely (parser
))
10622 /* If the next token is "&", the argument must be the address of an
10623 object or function with external linkage. */
10624 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
10626 cp_lexer_consume_token (parser
->lexer
);
10627 /* See if we might have an id-expression. */
10628 token
= cp_lexer_peek_token (parser
->lexer
);
10629 if (token
->type
== CPP_NAME
10630 || token
->keyword
== RID_OPERATOR
10631 || token
->type
== CPP_SCOPE
10632 || token
->type
== CPP_TEMPLATE_ID
10633 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
10635 cp_parser_parse_tentatively (parser
);
10636 argument
= cp_parser_primary_expression (parser
,
10639 /*template_arg_p=*/true,
10641 if (cp_parser_error_occurred (parser
)
10642 || !cp_parser_next_token_ends_template_argument_p (parser
))
10643 cp_parser_abort_tentative_parse (parser
);
10646 if (TREE_CODE (argument
) == INDIRECT_REF
)
10648 gcc_assert (REFERENCE_REF_P (argument
));
10649 argument
= TREE_OPERAND (argument
, 0);
10652 if (TREE_CODE (argument
) == VAR_DECL
)
10654 /* A variable without external linkage might still be a
10655 valid constant-expression, so no error is issued here
10656 if the external-linkage check fails. */
10657 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
10658 cp_parser_simulate_error (parser
);
10660 else if (is_overloaded_fn (argument
))
10661 /* All overloaded functions are allowed; if the external
10662 linkage test does not pass, an error will be issued
10666 && (TREE_CODE (argument
) == OFFSET_REF
10667 || TREE_CODE (argument
) == SCOPE_REF
))
10668 /* A pointer-to-member. */
10670 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
10673 cp_parser_simulate_error (parser
);
10675 if (cp_parser_parse_definitely (parser
))
10678 argument
= build_x_unary_op (ADDR_EXPR
, argument
,
10679 tf_warning_or_error
);
10684 /* If the argument started with "&", there are no other valid
10685 alternatives at this point. */
10688 cp_parser_error (parser
, "invalid non-type template argument");
10689 return error_mark_node
;
10692 /* If the argument wasn't successfully parsed as a type-id followed
10693 by '>>', the argument can only be a constant expression now.
10694 Otherwise, we try parsing the constant-expression tentatively,
10695 because the argument could really be a type-id. */
10697 cp_parser_parse_tentatively (parser
);
10698 argument
= cp_parser_constant_expression (parser
,
10699 /*allow_non_constant_p=*/false,
10700 /*non_constant_p=*/NULL
);
10701 argument
= fold_non_dependent_expr (argument
);
10702 if (!maybe_type_id
)
10704 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10705 cp_parser_error (parser
, "expected template-argument");
10706 if (cp_parser_parse_definitely (parser
))
10708 /* We did our best to parse the argument as a non type-id, but that
10709 was the only alternative that matched (albeit with a '>' after
10710 it). We can assume it's just a typo from the user, and a
10711 diagnostic will then be issued. */
10712 return cp_parser_type_id (parser
);
10715 /* Parse an explicit-instantiation.
10717 explicit-instantiation:
10718 template declaration
10720 Although the standard says `declaration', what it really means is:
10722 explicit-instantiation:
10723 template decl-specifier-seq [opt] declarator [opt] ;
10725 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10726 supposed to be allowed. A defect report has been filed about this
10731 explicit-instantiation:
10732 storage-class-specifier template
10733 decl-specifier-seq [opt] declarator [opt] ;
10734 function-specifier template
10735 decl-specifier-seq [opt] declarator [opt] ; */
10738 cp_parser_explicit_instantiation (cp_parser
* parser
)
10740 int declares_class_or_enum
;
10741 cp_decl_specifier_seq decl_specifiers
;
10742 tree extension_specifier
= NULL_TREE
;
10745 /* Look for an (optional) storage-class-specifier or
10746 function-specifier. */
10747 if (cp_parser_allow_gnu_extensions_p (parser
))
10749 extension_specifier
10750 = cp_parser_storage_class_specifier_opt (parser
);
10751 if (!extension_specifier
)
10752 extension_specifier
10753 = cp_parser_function_specifier_opt (parser
,
10754 /*decl_specs=*/NULL
);
10757 /* Look for the `template' keyword. */
10758 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "%<template%>");
10759 /* Let the front end know that we are processing an explicit
10761 begin_explicit_instantiation ();
10762 /* [temp.explicit] says that we are supposed to ignore access
10763 control while processing explicit instantiation directives. */
10764 push_deferring_access_checks (dk_no_check
);
10765 /* Parse a decl-specifier-seq. */
10766 token
= cp_lexer_peek_token (parser
->lexer
);
10767 cp_parser_decl_specifier_seq (parser
,
10768 CP_PARSER_FLAGS_OPTIONAL
,
10770 &declares_class_or_enum
);
10771 /* If there was exactly one decl-specifier, and it declared a class,
10772 and there's no declarator, then we have an explicit type
10774 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
10778 type
= check_tag_decl (&decl_specifiers
);
10779 /* Turn access control back on for names used during
10780 template instantiation. */
10781 pop_deferring_access_checks ();
10783 do_type_instantiation (type
, extension_specifier
,
10784 /*complain=*/tf_error
);
10788 cp_declarator
*declarator
;
10791 /* Parse the declarator. */
10793 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10794 /*ctor_dtor_or_conv_p=*/NULL
,
10795 /*parenthesized_p=*/NULL
,
10796 /*member_p=*/false);
10797 if (declares_class_or_enum
& 2)
10798 cp_parser_check_for_definition_in_return_type (declarator
,
10799 decl_specifiers
.type
,
10800 decl_specifiers
.type_location
);
10801 if (declarator
!= cp_error_declarator
)
10803 decl
= grokdeclarator (declarator
, &decl_specifiers
,
10804 NORMAL
, 0, &decl_specifiers
.attributes
);
10805 /* Turn access control back on for names used during
10806 template instantiation. */
10807 pop_deferring_access_checks ();
10808 /* Do the explicit instantiation. */
10809 do_decl_instantiation (decl
, extension_specifier
);
10813 pop_deferring_access_checks ();
10814 /* Skip the body of the explicit instantiation. */
10815 cp_parser_skip_to_end_of_statement (parser
);
10818 /* We're done with the instantiation. */
10819 end_explicit_instantiation ();
10821 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10824 /* Parse an explicit-specialization.
10826 explicit-specialization:
10827 template < > declaration
10829 Although the standard says `declaration', what it really means is:
10831 explicit-specialization:
10832 template <> decl-specifier [opt] init-declarator [opt] ;
10833 template <> function-definition
10834 template <> explicit-specialization
10835 template <> template-declaration */
10838 cp_parser_explicit_specialization (cp_parser
* parser
)
10840 bool need_lang_pop
;
10841 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10843 /* Look for the `template' keyword. */
10844 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "%<template%>");
10845 /* Look for the `<'. */
10846 cp_parser_require (parser
, CPP_LESS
, "%<<%>");
10847 /* Look for the `>'. */
10848 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
10849 /* We have processed another parameter list. */
10850 ++parser
->num_template_parameter_lists
;
10853 A template ... explicit specialization ... shall not have C
10855 if (current_lang_name
== lang_name_c
)
10857 error ("%Htemplate specialization with C linkage", &token
->location
);
10858 /* Give it C++ linkage to avoid confusing other parts of the
10860 push_lang_context (lang_name_cplusplus
);
10861 need_lang_pop
= true;
10864 need_lang_pop
= false;
10865 /* Let the front end know that we are beginning a specialization. */
10866 if (!begin_specialization ())
10868 end_specialization ();
10869 cp_parser_skip_to_end_of_block_or_statement (parser
);
10873 /* If the next keyword is `template', we need to figure out whether
10874 or not we're looking a template-declaration. */
10875 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
10877 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
10878 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
10879 cp_parser_template_declaration_after_export (parser
,
10880 /*member_p=*/false);
10882 cp_parser_explicit_specialization (parser
);
10885 /* Parse the dependent declaration. */
10886 cp_parser_single_declaration (parser
,
10888 /*member_p=*/false,
10889 /*explicit_specialization_p=*/true,
10890 /*friend_p=*/NULL
);
10891 /* We're done with the specialization. */
10892 end_specialization ();
10893 /* For the erroneous case of a template with C linkage, we pushed an
10894 implicit C++ linkage scope; exit that scope now. */
10896 pop_lang_context ();
10897 /* We're done with this parameter list. */
10898 --parser
->num_template_parameter_lists
;
10901 /* Parse a type-specifier.
10904 simple-type-specifier
10907 elaborated-type-specifier
10915 Returns a representation of the type-specifier. For a
10916 class-specifier, enum-specifier, or elaborated-type-specifier, a
10917 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10919 The parser flags FLAGS is used to control type-specifier parsing.
10921 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10922 in a decl-specifier-seq.
10924 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10925 class-specifier, enum-specifier, or elaborated-type-specifier, then
10926 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10927 if a type is declared; 2 if it is defined. Otherwise, it is set to
10930 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10931 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10932 is set to FALSE. */
10935 cp_parser_type_specifier (cp_parser
* parser
,
10936 cp_parser_flags flags
,
10937 cp_decl_specifier_seq
*decl_specs
,
10938 bool is_declaration
,
10939 int* declares_class_or_enum
,
10940 bool* is_cv_qualifier
)
10942 tree type_spec
= NULL_TREE
;
10945 cp_decl_spec ds
= ds_last
;
10947 /* Assume this type-specifier does not declare a new type. */
10948 if (declares_class_or_enum
)
10949 *declares_class_or_enum
= 0;
10950 /* And that it does not specify a cv-qualifier. */
10951 if (is_cv_qualifier
)
10952 *is_cv_qualifier
= false;
10953 /* Peek at the next token. */
10954 token
= cp_lexer_peek_token (parser
->lexer
);
10956 /* If we're looking at a keyword, we can use that to guide the
10957 production we choose. */
10958 keyword
= token
->keyword
;
10962 /* Look for the enum-specifier. */
10963 type_spec
= cp_parser_enum_specifier (parser
);
10964 /* If that worked, we're done. */
10967 if (declares_class_or_enum
)
10968 *declares_class_or_enum
= 2;
10970 cp_parser_set_decl_spec_type (decl_specs
,
10973 /*user_defined_p=*/true);
10977 goto elaborated_type_specifier
;
10979 /* Any of these indicate either a class-specifier, or an
10980 elaborated-type-specifier. */
10984 /* Parse tentatively so that we can back up if we don't find a
10985 class-specifier. */
10986 cp_parser_parse_tentatively (parser
);
10987 /* Look for the class-specifier. */
10988 type_spec
= cp_parser_class_specifier (parser
);
10989 /* If that worked, we're done. */
10990 if (cp_parser_parse_definitely (parser
))
10992 if (declares_class_or_enum
)
10993 *declares_class_or_enum
= 2;
10995 cp_parser_set_decl_spec_type (decl_specs
,
10998 /*user_defined_p=*/true);
11002 /* Fall through. */
11003 elaborated_type_specifier
:
11004 /* We're declaring (not defining) a class or enum. */
11005 if (declares_class_or_enum
)
11006 *declares_class_or_enum
= 1;
11008 /* Fall through. */
11010 /* Look for an elaborated-type-specifier. */
11012 = (cp_parser_elaborated_type_specifier
11014 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
11017 cp_parser_set_decl_spec_type (decl_specs
,
11020 /*user_defined_p=*/true);
11025 if (is_cv_qualifier
)
11026 *is_cv_qualifier
= true;
11031 if (is_cv_qualifier
)
11032 *is_cv_qualifier
= true;
11037 if (is_cv_qualifier
)
11038 *is_cv_qualifier
= true;
11042 /* The `__complex__' keyword is a GNU extension. */
11050 /* Handle simple keywords. */
11055 ++decl_specs
->specs
[(int)ds
];
11056 decl_specs
->any_specifiers_p
= true;
11058 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11061 /* If we do not already have a type-specifier, assume we are looking
11062 at a simple-type-specifier. */
11063 type_spec
= cp_parser_simple_type_specifier (parser
,
11067 /* If we didn't find a type-specifier, and a type-specifier was not
11068 optional in this context, issue an error message. */
11069 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
11071 cp_parser_error (parser
, "expected type specifier");
11072 return error_mark_node
;
11078 /* Parse a simple-type-specifier.
11080 simple-type-specifier:
11081 :: [opt] nested-name-specifier [opt] type-name
11082 :: [opt] nested-name-specifier template template-id
11097 simple-type-specifier:
11099 decltype ( expression )
11105 simple-type-specifier:
11106 __typeof__ unary-expression
11107 __typeof__ ( type-id )
11109 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11110 appropriately updated. */
11113 cp_parser_simple_type_specifier (cp_parser
* parser
,
11114 cp_decl_specifier_seq
*decl_specs
,
11115 cp_parser_flags flags
)
11117 tree type
= NULL_TREE
;
11120 /* Peek at the next token. */
11121 token
= cp_lexer_peek_token (parser
->lexer
);
11123 /* If we're looking at a keyword, things are easy. */
11124 switch (token
->keyword
)
11128 decl_specs
->explicit_char_p
= true;
11129 type
= char_type_node
;
11132 type
= char16_type_node
;
11135 type
= char32_type_node
;
11138 type
= wchar_type_node
;
11141 type
= boolean_type_node
;
11145 ++decl_specs
->specs
[(int) ds_short
];
11146 type
= short_integer_type_node
;
11150 decl_specs
->explicit_int_p
= true;
11151 type
= integer_type_node
;
11155 ++decl_specs
->specs
[(int) ds_long
];
11156 type
= long_integer_type_node
;
11160 ++decl_specs
->specs
[(int) ds_signed
];
11161 type
= integer_type_node
;
11165 ++decl_specs
->specs
[(int) ds_unsigned
];
11166 type
= unsigned_type_node
;
11169 type
= float_type_node
;
11172 type
= double_type_node
;
11175 type
= void_type_node
;
11179 maybe_warn_cpp0x ("C++0x auto");
11180 type
= make_auto ();
11184 /* Parse the `decltype' type. */
11185 type
= cp_parser_decltype (parser
);
11188 cp_parser_set_decl_spec_type (decl_specs
, type
,
11190 /*user_defined_p=*/true);
11195 /* Consume the `typeof' token. */
11196 cp_lexer_consume_token (parser
->lexer
);
11197 /* Parse the operand to `typeof'. */
11198 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
11199 /* If it is not already a TYPE, take its type. */
11200 if (!TYPE_P (type
))
11201 type
= finish_typeof (type
);
11204 cp_parser_set_decl_spec_type (decl_specs
, type
,
11206 /*user_defined_p=*/true);
11214 /* If the type-specifier was for a built-in type, we're done. */
11219 /* Record the type. */
11221 && (token
->keyword
!= RID_SIGNED
11222 && token
->keyword
!= RID_UNSIGNED
11223 && token
->keyword
!= RID_SHORT
11224 && token
->keyword
!= RID_LONG
))
11225 cp_parser_set_decl_spec_type (decl_specs
,
11228 /*user_defined=*/false);
11230 decl_specs
->any_specifiers_p
= true;
11232 /* Consume the token. */
11233 id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11235 /* There is no valid C++ program where a non-template type is
11236 followed by a "<". That usually indicates that the user thought
11237 that the type was a template. */
11238 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
11240 return TYPE_NAME (type
);
11243 /* The type-specifier must be a user-defined type. */
11244 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
11249 /* Don't gobble tokens or issue error messages if this is an
11250 optional type-specifier. */
11251 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
11252 cp_parser_parse_tentatively (parser
);
11254 /* Look for the optional `::' operator. */
11256 = (cp_parser_global_scope_opt (parser
,
11257 /*current_scope_valid_p=*/false)
11259 /* Look for the nested-name specifier. */
11261 = (cp_parser_nested_name_specifier_opt (parser
,
11262 /*typename_keyword_p=*/false,
11263 /*check_dependency_p=*/true,
11265 /*is_declaration=*/false)
11267 token
= cp_lexer_peek_token (parser
->lexer
);
11268 /* If we have seen a nested-name-specifier, and the next token
11269 is `template', then we are using the template-id production. */
11271 && cp_parser_optional_template_keyword (parser
))
11273 /* Look for the template-id. */
11274 type
= cp_parser_template_id (parser
,
11275 /*template_keyword_p=*/true,
11276 /*check_dependency_p=*/true,
11277 /*is_declaration=*/false);
11278 /* If the template-id did not name a type, we are out of
11280 if (TREE_CODE (type
) != TYPE_DECL
)
11282 cp_parser_error (parser
, "expected template-id for type");
11286 /* Otherwise, look for a type-name. */
11288 type
= cp_parser_type_name (parser
);
11289 /* Keep track of all name-lookups performed in class scopes. */
11293 && TREE_CODE (type
) == TYPE_DECL
11294 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
11295 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
11296 /* If it didn't work out, we don't have a TYPE. */
11297 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
11298 && !cp_parser_parse_definitely (parser
))
11300 if (type
&& decl_specs
)
11301 cp_parser_set_decl_spec_type (decl_specs
, type
,
11303 /*user_defined=*/true);
11306 /* If we didn't get a type-name, issue an error message. */
11307 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
11309 cp_parser_error (parser
, "expected type-name");
11310 return error_mark_node
;
11313 /* There is no valid C++ program where a non-template type is
11314 followed by a "<". That usually indicates that the user thought
11315 that the type was a template. */
11316 if (type
&& type
!= error_mark_node
)
11318 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11319 If it is, then the '<'...'>' enclose protocol names rather than
11320 template arguments, and so everything is fine. */
11321 if (c_dialect_objc ()
11322 && (objc_is_id (type
) || objc_is_class_name (type
)))
11324 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
11325 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
11327 /* Clobber the "unqualified" type previously entered into
11328 DECL_SPECS with the new, improved protocol-qualified version. */
11330 decl_specs
->type
= qual_type
;
11335 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
11342 /* Parse a type-name.
11355 Returns a TYPE_DECL for the type. */
11358 cp_parser_type_name (cp_parser
* parser
)
11362 /* We can't know yet whether it is a class-name or not. */
11363 cp_parser_parse_tentatively (parser
);
11364 /* Try a class-name. */
11365 type_decl
= cp_parser_class_name (parser
,
11366 /*typename_keyword_p=*/false,
11367 /*template_keyword_p=*/false,
11369 /*check_dependency_p=*/true,
11370 /*class_head_p=*/false,
11371 /*is_declaration=*/false);
11372 /* If it's not a class-name, keep looking. */
11373 if (!cp_parser_parse_definitely (parser
))
11375 /* It must be a typedef-name or an enum-name. */
11376 return cp_parser_nonclass_name (parser
);
11382 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11390 Returns a TYPE_DECL for the type. */
11393 cp_parser_nonclass_name (cp_parser
* parser
)
11398 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11399 identifier
= cp_parser_identifier (parser
);
11400 if (identifier
== error_mark_node
)
11401 return error_mark_node
;
11403 /* Look up the type-name. */
11404 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
11406 if (TREE_CODE (type_decl
) != TYPE_DECL
11407 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
11409 /* See if this is an Objective-C type. */
11410 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
11411 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
11413 type_decl
= TYPE_NAME (type
);
11416 /* Issue an error if we did not find a type-name. */
11417 if (TREE_CODE (type_decl
) != TYPE_DECL
)
11419 if (!cp_parser_simulate_error (parser
))
11420 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
11421 "is not a type", token
->location
);
11422 return error_mark_node
;
11424 /* Remember that the name was used in the definition of the
11425 current class so that we can check later to see if the
11426 meaning would have been different after the class was
11427 entirely defined. */
11428 else if (type_decl
!= error_mark_node
11430 maybe_note_name_used_in_class (identifier
, type_decl
);
11435 /* Parse an elaborated-type-specifier. Note that the grammar given
11436 here incorporates the resolution to DR68.
11438 elaborated-type-specifier:
11439 class-key :: [opt] nested-name-specifier [opt] identifier
11440 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11441 enum-key :: [opt] nested-name-specifier [opt] identifier
11442 typename :: [opt] nested-name-specifier identifier
11443 typename :: [opt] nested-name-specifier template [opt]
11448 elaborated-type-specifier:
11449 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11450 class-key attributes :: [opt] nested-name-specifier [opt]
11451 template [opt] template-id
11452 enum attributes :: [opt] nested-name-specifier [opt] identifier
11454 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11455 declared `friend'. If IS_DECLARATION is TRUE, then this
11456 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11457 something is being declared.
11459 Returns the TYPE specified. */
11462 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
11464 bool is_declaration
)
11466 enum tag_types tag_type
;
11468 tree type
= NULL_TREE
;
11469 tree attributes
= NULL_TREE
;
11470 cp_token
*token
= NULL
;
11472 /* See if we're looking at the `enum' keyword. */
11473 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
11475 /* Consume the `enum' token. */
11476 cp_lexer_consume_token (parser
->lexer
);
11477 /* Remember that it's an enumeration type. */
11478 tag_type
= enum_type
;
11479 /* Parse the optional `struct' or `class' key (for C++0x scoped
11481 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
11482 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
11484 if (cxx_dialect
== cxx98
)
11485 maybe_warn_cpp0x ("scoped enums");
11487 /* Consume the `struct' or `class'. */
11488 cp_lexer_consume_token (parser
->lexer
);
11490 /* Parse the attributes. */
11491 attributes
= cp_parser_attributes_opt (parser
);
11493 /* Or, it might be `typename'. */
11494 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11497 /* Consume the `typename' token. */
11498 cp_lexer_consume_token (parser
->lexer
);
11499 /* Remember that it's a `typename' type. */
11500 tag_type
= typename_type
;
11501 /* The `typename' keyword is only allowed in templates. */
11502 if (!processing_template_decl
)
11503 permerror (input_location
, "using %<typename%> outside of template");
11505 /* Otherwise it must be a class-key. */
11508 tag_type
= cp_parser_class_key (parser
);
11509 if (tag_type
== none_type
)
11510 return error_mark_node
;
11511 /* Parse the attributes. */
11512 attributes
= cp_parser_attributes_opt (parser
);
11515 /* Look for the `::' operator. */
11516 cp_parser_global_scope_opt (parser
,
11517 /*current_scope_valid_p=*/false);
11518 /* Look for the nested-name-specifier. */
11519 if (tag_type
== typename_type
)
11521 if (!cp_parser_nested_name_specifier (parser
,
11522 /*typename_keyword_p=*/true,
11523 /*check_dependency_p=*/true,
11526 return error_mark_node
;
11529 /* Even though `typename' is not present, the proposed resolution
11530 to Core Issue 180 says that in `class A<T>::B', `B' should be
11531 considered a type-name, even if `A<T>' is dependent. */
11532 cp_parser_nested_name_specifier_opt (parser
,
11533 /*typename_keyword_p=*/true,
11534 /*check_dependency_p=*/true,
11537 /* For everything but enumeration types, consider a template-id.
11538 For an enumeration type, consider only a plain identifier. */
11539 if (tag_type
!= enum_type
)
11541 bool template_p
= false;
11544 /* Allow the `template' keyword. */
11545 template_p
= cp_parser_optional_template_keyword (parser
);
11546 /* If we didn't see `template', we don't know if there's a
11547 template-id or not. */
11549 cp_parser_parse_tentatively (parser
);
11550 /* Parse the template-id. */
11551 token
= cp_lexer_peek_token (parser
->lexer
);
11552 decl
= cp_parser_template_id (parser
, template_p
,
11553 /*check_dependency_p=*/true,
11555 /* If we didn't find a template-id, look for an ordinary
11557 if (!template_p
&& !cp_parser_parse_definitely (parser
))
11559 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11560 in effect, then we must assume that, upon instantiation, the
11561 template will correspond to a class. */
11562 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11563 && tag_type
== typename_type
)
11564 type
= make_typename_type (parser
->scope
, decl
,
11566 /*complain=*/tf_error
);
11568 type
= TREE_TYPE (decl
);
11573 token
= cp_lexer_peek_token (parser
->lexer
);
11574 identifier
= cp_parser_identifier (parser
);
11576 if (identifier
== error_mark_node
)
11578 parser
->scope
= NULL_TREE
;
11579 return error_mark_node
;
11582 /* For a `typename', we needn't call xref_tag. */
11583 if (tag_type
== typename_type
11584 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
11585 return cp_parser_make_typename_type (parser
, parser
->scope
,
11588 /* Look up a qualified name in the usual way. */
11592 tree ambiguous_decls
;
11594 decl
= cp_parser_lookup_name (parser
, identifier
,
11596 /*is_template=*/false,
11597 /*is_namespace=*/false,
11598 /*check_dependency=*/true,
11602 /* If the lookup was ambiguous, an error will already have been
11604 if (ambiguous_decls
)
11605 return error_mark_node
;
11607 /* If we are parsing friend declaration, DECL may be a
11608 TEMPLATE_DECL tree node here. However, we need to check
11609 whether this TEMPLATE_DECL results in valid code. Consider
11610 the following example:
11613 template <class T> class C {};
11616 template <class T> friend class N::C; // #1, valid code
11618 template <class T> class Y {
11619 friend class N::C; // #2, invalid code
11622 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11623 name lookup of `N::C'. We see that friend declaration must
11624 be template for the code to be valid. Note that
11625 processing_template_decl does not work here since it is
11626 always 1 for the above two cases. */
11628 decl
= (cp_parser_maybe_treat_template_as_class
11629 (decl
, /*tag_name_p=*/is_friend
11630 && parser
->num_template_parameter_lists
));
11632 if (TREE_CODE (decl
) != TYPE_DECL
)
11634 cp_parser_diagnose_invalid_type_name (parser
,
11638 return error_mark_node
;
11641 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
11643 bool allow_template
= (parser
->num_template_parameter_lists
11644 || DECL_SELF_REFERENCE_P (decl
));
11645 type
= check_elaborated_type_specifier (tag_type
, decl
,
11648 if (type
== error_mark_node
)
11649 return error_mark_node
;
11652 /* Forward declarations of nested types, such as
11657 are invalid unless all components preceding the final '::'
11658 are complete. If all enclosing types are complete, these
11659 declarations become merely pointless.
11661 Invalid forward declarations of nested types are errors
11662 caught elsewhere in parsing. Those that are pointless arrive
11665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
11666 && !is_friend
&& !processing_explicit_instantiation
)
11667 warning (0, "declaration %qD does not declare anything", decl
);
11669 type
= TREE_TYPE (decl
);
11673 /* An elaborated-type-specifier sometimes introduces a new type and
11674 sometimes names an existing type. Normally, the rule is that it
11675 introduces a new type only if there is not an existing type of
11676 the same name already in scope. For example, given:
11679 void f() { struct S s; }
11681 the `struct S' in the body of `f' is the same `struct S' as in
11682 the global scope; the existing definition is used. However, if
11683 there were no global declaration, this would introduce a new
11684 local class named `S'.
11686 An exception to this rule applies to the following code:
11688 namespace N { struct S; }
11690 Here, the elaborated-type-specifier names a new type
11691 unconditionally; even if there is already an `S' in the
11692 containing scope this declaration names a new type.
11693 This exception only applies if the elaborated-type-specifier
11694 forms the complete declaration:
11698 A declaration consisting solely of `class-key identifier ;' is
11699 either a redeclaration of the name in the current scope or a
11700 forward declaration of the identifier as a class name. It
11701 introduces the name into the current scope.
11703 We are in this situation precisely when the next token is a `;'.
11705 An exception to the exception is that a `friend' declaration does
11706 *not* name a new type; i.e., given:
11708 struct S { friend struct T; };
11710 `T' is not a new type in the scope of `S'.
11712 Also, `new struct S' or `sizeof (struct S)' never results in the
11713 definition of a new type; a new type can only be declared in a
11714 declaration context. */
11720 /* Friends have special name lookup rules. */
11721 ts
= ts_within_enclosing_non_class
;
11722 else if (is_declaration
11723 && cp_lexer_next_token_is (parser
->lexer
,
11725 /* This is a `class-key identifier ;' */
11731 (parser
->num_template_parameter_lists
11732 && (cp_parser_next_token_starts_class_definition_p (parser
)
11733 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
11734 /* An unqualified name was used to reference this type, so
11735 there were no qualifying templates. */
11736 if (!cp_parser_check_template_parameters (parser
,
11737 /*num_templates=*/0,
11739 return error_mark_node
;
11740 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
11744 if (type
== error_mark_node
)
11745 return error_mark_node
;
11747 /* Allow attributes on forward declarations of classes. */
11750 if (TREE_CODE (type
) == TYPENAME_TYPE
)
11751 warning (OPT_Wattributes
,
11752 "attributes ignored on uninstantiated type");
11753 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
11754 && ! processing_explicit_instantiation
)
11755 warning (OPT_Wattributes
,
11756 "attributes ignored on template instantiation");
11757 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
11758 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
11760 warning (OPT_Wattributes
,
11761 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11764 if (tag_type
!= enum_type
)
11765 cp_parser_check_class_key (tag_type
, type
);
11767 /* A "<" cannot follow an elaborated type specifier. If that
11768 happens, the user was probably trying to form a template-id. */
11769 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
11774 /* Parse an enum-specifier.
11777 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11782 enum struct [C++0x]
11785 : type-specifier-seq
11788 enum-key attributes[opt] identifier [opt] enum-base [opt]
11789 { enumerator-list [opt] }attributes[opt]
11791 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11792 if the token stream isn't an enum-specifier after all. */
11795 cp_parser_enum_specifier (cp_parser
* parser
)
11800 bool scoped_enum_p
= false;
11801 bool has_underlying_type
= false;
11802 tree underlying_type
= NULL_TREE
;
11804 /* Parse tentatively so that we can back up if we don't find a
11806 cp_parser_parse_tentatively (parser
);
11808 /* Caller guarantees that the current token is 'enum', an identifier
11809 possibly follows, and the token after that is an opening brace.
11810 If we don't have an identifier, fabricate an anonymous name for
11811 the enumeration being defined. */
11812 cp_lexer_consume_token (parser
->lexer
);
11814 /* Parse the "class" or "struct", which indicates a scoped
11815 enumeration type in C++0x. */
11816 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
11817 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
11819 if (cxx_dialect
== cxx98
)
11820 maybe_warn_cpp0x ("scoped enums");
11822 /* Consume the `struct' or `class' token. */
11823 cp_lexer_consume_token (parser
->lexer
);
11825 scoped_enum_p
= true;
11828 attributes
= cp_parser_attributes_opt (parser
);
11830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11831 identifier
= cp_parser_identifier (parser
);
11833 identifier
= make_anon_name ();
11835 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11838 cp_decl_specifier_seq type_specifiers
;
11840 /* At this point this is surely not elaborated type specifier. */
11841 if (!cp_parser_parse_definitely (parser
))
11844 if (cxx_dialect
== cxx98
)
11845 maybe_warn_cpp0x ("scoped enums");
11847 /* Consume the `:'. */
11848 cp_lexer_consume_token (parser
->lexer
);
11850 has_underlying_type
= true;
11852 /* Parse the type-specifier-seq. */
11853 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11856 /* If that didn't work, stop. */
11857 if (type_specifiers
.type
!= error_mark_node
)
11859 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
11860 /*initialized=*/0, NULL
);
11861 if (underlying_type
== error_mark_node
)
11862 underlying_type
= NULL_TREE
;
11866 /* Look for the `{' but don't consume it yet. */
11867 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11869 cp_parser_error (parser
, "expected %<{%>");
11870 if (has_underlying_type
)
11874 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
11877 /* Issue an error message if type-definitions are forbidden here. */
11878 if (!cp_parser_check_type_definition (parser
))
11879 type
= error_mark_node
;
11881 /* Create the new type. We do this before consuming the opening
11882 brace so the enum will be recorded as being on the line of its
11883 tag (or the 'enum' keyword, if there is no tag). */
11884 type
= start_enum (identifier
, underlying_type
, scoped_enum_p
);
11886 /* Consume the opening brace. */
11887 cp_lexer_consume_token (parser
->lexer
);
11889 if (type
== error_mark_node
)
11891 cp_parser_skip_to_end_of_block_or_statement (parser
);
11892 return error_mark_node
;
11895 /* If the next token is not '}', then there are some enumerators. */
11896 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11897 cp_parser_enumerator_list (parser
, type
);
11899 /* Consume the final '}'. */
11900 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
11902 /* Look for trailing attributes to apply to this enumeration, and
11903 apply them if appropriate. */
11904 if (cp_parser_allow_gnu_extensions_p (parser
))
11906 tree trailing_attr
= cp_parser_attributes_opt (parser
);
11907 cplus_decl_attributes (&type
,
11909 (int) ATTR_FLAG_TYPE_IN_PLACE
);
11912 /* Finish up the enumeration. */
11913 finish_enum (type
);
11918 /* Parse an enumerator-list. The enumerators all have the indicated
11922 enumerator-definition
11923 enumerator-list , enumerator-definition */
11926 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
11930 /* Parse an enumerator-definition. */
11931 cp_parser_enumerator_definition (parser
, type
);
11933 /* If the next token is not a ',', we've reached the end of
11935 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11937 /* Otherwise, consume the `,' and keep going. */
11938 cp_lexer_consume_token (parser
->lexer
);
11939 /* If the next token is a `}', there is a trailing comma. */
11940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11942 if (!in_system_header
)
11943 pedwarn (input_location
, OPT_pedantic
, "comma at end of enumerator list");
11949 /* Parse an enumerator-definition. The enumerator has the indicated
11952 enumerator-definition:
11954 enumerator = constant-expression
11960 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
11965 /* Look for the identifier. */
11966 identifier
= cp_parser_identifier (parser
);
11967 if (identifier
== error_mark_node
)
11970 /* If the next token is an '=', then there is an explicit value. */
11971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11973 /* Consume the `=' token. */
11974 cp_lexer_consume_token (parser
->lexer
);
11975 /* Parse the value. */
11976 value
= cp_parser_constant_expression (parser
,
11977 /*allow_non_constant_p=*/false,
11983 /* Create the enumerator. */
11984 build_enumerator (identifier
, value
, type
);
11987 /* Parse a namespace-name.
11990 original-namespace-name
11993 Returns the NAMESPACE_DECL for the namespace. */
11996 cp_parser_namespace_name (cp_parser
* parser
)
11999 tree namespace_decl
;
12001 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12003 /* Get the name of the namespace. */
12004 identifier
= cp_parser_identifier (parser
);
12005 if (identifier
== error_mark_node
)
12006 return error_mark_node
;
12008 /* Look up the identifier in the currently active scope. Look only
12009 for namespaces, due to:
12011 [basic.lookup.udir]
12013 When looking up a namespace-name in a using-directive or alias
12014 definition, only namespace names are considered.
12018 [basic.lookup.qual]
12020 During the lookup of a name preceding the :: scope resolution
12021 operator, object, function, and enumerator names are ignored.
12023 (Note that cp_parser_qualifying_entity only calls this
12024 function if the token after the name is the scope resolution
12026 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
12028 /*is_template=*/false,
12029 /*is_namespace=*/true,
12030 /*check_dependency=*/true,
12031 /*ambiguous_decls=*/NULL
,
12033 /* If it's not a namespace, issue an error. */
12034 if (namespace_decl
== error_mark_node
12035 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
12037 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12038 error ("%H%qD is not a namespace-name", &token
->location
, identifier
);
12039 cp_parser_error (parser
, "expected namespace-name");
12040 namespace_decl
= error_mark_node
;
12043 return namespace_decl
;
12046 /* Parse a namespace-definition.
12048 namespace-definition:
12049 named-namespace-definition
12050 unnamed-namespace-definition
12052 named-namespace-definition:
12053 original-namespace-definition
12054 extension-namespace-definition
12056 original-namespace-definition:
12057 namespace identifier { namespace-body }
12059 extension-namespace-definition:
12060 namespace original-namespace-name { namespace-body }
12062 unnamed-namespace-definition:
12063 namespace { namespace-body } */
12066 cp_parser_namespace_definition (cp_parser
* parser
)
12068 tree identifier
, attribs
;
12069 bool has_visibility
;
12072 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
12075 cp_lexer_consume_token (parser
->lexer
);
12080 /* Look for the `namespace' keyword. */
12081 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "%<namespace%>");
12083 /* Get the name of the namespace. We do not attempt to distinguish
12084 between an original-namespace-definition and an
12085 extension-namespace-definition at this point. The semantic
12086 analysis routines are responsible for that. */
12087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12088 identifier
= cp_parser_identifier (parser
);
12090 identifier
= NULL_TREE
;
12092 /* Parse any specified attributes. */
12093 attribs
= cp_parser_attributes_opt (parser
);
12095 /* Look for the `{' to start the namespace. */
12096 cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>");
12097 /* Start the namespace. */
12098 push_namespace (identifier
);
12100 /* "inline namespace" is equivalent to a stub namespace definition
12101 followed by a strong using directive. */
12104 tree name_space
= current_namespace
;
12105 /* Set up namespace association. */
12106 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
12107 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
12108 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
12109 /* Import the contents of the inline namespace. */
12111 do_using_directive (name_space
);
12112 push_namespace (identifier
);
12115 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
12117 /* Parse the body of the namespace. */
12118 cp_parser_namespace_body (parser
);
12120 #ifdef HANDLE_PRAGMA_VISIBILITY
12121 if (has_visibility
)
12125 /* Finish the namespace. */
12127 /* Look for the final `}'. */
12128 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
12131 /* Parse a namespace-body.
12134 declaration-seq [opt] */
12137 cp_parser_namespace_body (cp_parser
* parser
)
12139 cp_parser_declaration_seq_opt (parser
);
12142 /* Parse a namespace-alias-definition.
12144 namespace-alias-definition:
12145 namespace identifier = qualified-namespace-specifier ; */
12148 cp_parser_namespace_alias_definition (cp_parser
* parser
)
12151 tree namespace_specifier
;
12153 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12155 /* Look for the `namespace' keyword. */
12156 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "%<namespace%>");
12157 /* Look for the identifier. */
12158 identifier
= cp_parser_identifier (parser
);
12159 if (identifier
== error_mark_node
)
12161 /* Look for the `=' token. */
12162 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
12163 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12165 error ("%H%<namespace%> definition is not allowed here", &token
->location
);
12166 /* Skip the definition. */
12167 cp_lexer_consume_token (parser
->lexer
);
12168 if (cp_parser_skip_to_closing_brace (parser
))
12169 cp_lexer_consume_token (parser
->lexer
);
12172 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
12173 /* Look for the qualified-namespace-specifier. */
12174 namespace_specifier
12175 = cp_parser_qualified_namespace_specifier (parser
);
12176 /* Look for the `;' token. */
12177 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
12179 /* Register the alias in the symbol table. */
12180 do_namespace_alias (identifier
, namespace_specifier
);
12183 /* Parse a qualified-namespace-specifier.
12185 qualified-namespace-specifier:
12186 :: [opt] nested-name-specifier [opt] namespace-name
12188 Returns a NAMESPACE_DECL corresponding to the specified
12192 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
12194 /* Look for the optional `::'. */
12195 cp_parser_global_scope_opt (parser
,
12196 /*current_scope_valid_p=*/false);
12198 /* Look for the optional nested-name-specifier. */
12199 cp_parser_nested_name_specifier_opt (parser
,
12200 /*typename_keyword_p=*/false,
12201 /*check_dependency_p=*/true,
12203 /*is_declaration=*/true);
12205 return cp_parser_namespace_name (parser
);
12208 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12209 access declaration.
12212 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12213 using :: unqualified-id ;
12215 access-declaration:
12221 cp_parser_using_declaration (cp_parser
* parser
,
12222 bool access_declaration_p
)
12225 bool typename_p
= false;
12226 bool global_scope_p
;
12231 if (access_declaration_p
)
12232 cp_parser_parse_tentatively (parser
);
12235 /* Look for the `using' keyword. */
12236 cp_parser_require_keyword (parser
, RID_USING
, "%<using%>");
12238 /* Peek at the next token. */
12239 token
= cp_lexer_peek_token (parser
->lexer
);
12240 /* See if it's `typename'. */
12241 if (token
->keyword
== RID_TYPENAME
)
12243 /* Remember that we've seen it. */
12245 /* Consume the `typename' token. */
12246 cp_lexer_consume_token (parser
->lexer
);
12250 /* Look for the optional global scope qualification. */
12252 = (cp_parser_global_scope_opt (parser
,
12253 /*current_scope_valid_p=*/false)
12256 /* If we saw `typename', or didn't see `::', then there must be a
12257 nested-name-specifier present. */
12258 if (typename_p
|| !global_scope_p
)
12259 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
12260 /*check_dependency_p=*/true,
12262 /*is_declaration=*/true);
12263 /* Otherwise, we could be in either of the two productions. In that
12264 case, treat the nested-name-specifier as optional. */
12266 qscope
= cp_parser_nested_name_specifier_opt (parser
,
12267 /*typename_keyword_p=*/false,
12268 /*check_dependency_p=*/true,
12270 /*is_declaration=*/true);
12272 qscope
= global_namespace
;
12274 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
12275 /* Something has already gone wrong; there's no need to parse
12276 further. Since an error has occurred, the return value of
12277 cp_parser_parse_definitely will be false, as required. */
12278 return cp_parser_parse_definitely (parser
);
12280 token
= cp_lexer_peek_token (parser
->lexer
);
12281 /* Parse the unqualified-id. */
12282 identifier
= cp_parser_unqualified_id (parser
,
12283 /*template_keyword_p=*/false,
12284 /*check_dependency_p=*/true,
12285 /*declarator_p=*/true,
12286 /*optional_p=*/false);
12288 if (access_declaration_p
)
12290 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12291 cp_parser_simulate_error (parser
);
12292 if (!cp_parser_parse_definitely (parser
))
12296 /* The function we call to handle a using-declaration is different
12297 depending on what scope we are in. */
12298 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
12300 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
12301 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
12302 /* [namespace.udecl]
12304 A using declaration shall not name a template-id. */
12305 error ("%Ha template-id may not appear in a using-declaration",
12309 if (at_class_scope_p ())
12311 /* Create the USING_DECL. */
12312 decl
= do_class_using_decl (parser
->scope
, identifier
);
12314 if (check_for_bare_parameter_packs (decl
))
12317 /* Add it to the list of members in this class. */
12318 finish_member_declaration (decl
);
12322 decl
= cp_parser_lookup_name_simple (parser
,
12325 if (decl
== error_mark_node
)
12326 cp_parser_name_lookup_error (parser
, identifier
,
12329 else if (check_for_bare_parameter_packs (decl
))
12331 else if (!at_namespace_scope_p ())
12332 do_local_using_decl (decl
, qscope
, identifier
);
12334 do_toplevel_using_decl (decl
, qscope
, identifier
);
12338 /* Look for the final `;'. */
12339 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
12344 /* Parse a using-directive.
12347 using namespace :: [opt] nested-name-specifier [opt]
12348 namespace-name ; */
12351 cp_parser_using_directive (cp_parser
* parser
)
12353 tree namespace_decl
;
12356 /* Look for the `using' keyword. */
12357 cp_parser_require_keyword (parser
, RID_USING
, "%<using%>");
12358 /* And the `namespace' keyword. */
12359 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "%<namespace%>");
12360 /* Look for the optional `::' operator. */
12361 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
12362 /* And the optional nested-name-specifier. */
12363 cp_parser_nested_name_specifier_opt (parser
,
12364 /*typename_keyword_p=*/false,
12365 /*check_dependency_p=*/true,
12367 /*is_declaration=*/true);
12368 /* Get the namespace being used. */
12369 namespace_decl
= cp_parser_namespace_name (parser
);
12370 /* And any specified attributes. */
12371 attribs
= cp_parser_attributes_opt (parser
);
12372 /* Update the symbol table. */
12373 parse_using_directive (namespace_decl
, attribs
);
12374 /* Look for the final `;'. */
12375 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
12378 /* Parse an asm-definition.
12381 asm ( string-literal ) ;
12386 asm volatile [opt] ( string-literal ) ;
12387 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12388 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12389 : asm-operand-list [opt] ) ;
12390 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12391 : asm-operand-list [opt]
12392 : asm-operand-list [opt] ) ; */
12395 cp_parser_asm_definition (cp_parser
* parser
)
12398 tree outputs
= NULL_TREE
;
12399 tree inputs
= NULL_TREE
;
12400 tree clobbers
= NULL_TREE
;
12402 bool volatile_p
= false;
12403 bool extended_p
= false;
12404 bool invalid_inputs_p
= false;
12405 bool invalid_outputs_p
= false;
12407 /* Look for the `asm' keyword. */
12408 cp_parser_require_keyword (parser
, RID_ASM
, "%<asm%>");
12409 /* See if the next token is `volatile'. */
12410 if (cp_parser_allow_gnu_extensions_p (parser
)
12411 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
12413 /* Remember that we saw the `volatile' keyword. */
12415 /* Consume the token. */
12416 cp_lexer_consume_token (parser
->lexer
);
12418 /* Look for the opening `('. */
12419 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
12421 /* Look for the string. */
12422 string
= cp_parser_string_literal (parser
, false, false);
12423 if (string
== error_mark_node
)
12425 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12426 /*consume_paren=*/true);
12430 /* If we're allowing GNU extensions, check for the extended assembly
12431 syntax. Unfortunately, the `:' tokens need not be separated by
12432 a space in C, and so, for compatibility, we tolerate that here
12433 too. Doing that means that we have to treat the `::' operator as
12435 if (cp_parser_allow_gnu_extensions_p (parser
)
12436 && parser
->in_function_body
12437 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
12438 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
12440 bool inputs_p
= false;
12441 bool clobbers_p
= false;
12443 /* The extended syntax was used. */
12446 /* Look for outputs. */
12447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12449 /* Consume the `:'. */
12450 cp_lexer_consume_token (parser
->lexer
);
12451 /* Parse the output-operands. */
12452 if (cp_lexer_next_token_is_not (parser
->lexer
,
12454 && cp_lexer_next_token_is_not (parser
->lexer
,
12456 && cp_lexer_next_token_is_not (parser
->lexer
,
12458 outputs
= cp_parser_asm_operand_list (parser
);
12460 if (outputs
== error_mark_node
)
12461 invalid_outputs_p
= true;
12463 /* If the next token is `::', there are no outputs, and the
12464 next token is the beginning of the inputs. */
12465 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12466 /* The inputs are coming next. */
12469 /* Look for inputs. */
12471 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12473 /* Consume the `:' or `::'. */
12474 cp_lexer_consume_token (parser
->lexer
);
12475 /* Parse the output-operands. */
12476 if (cp_lexer_next_token_is_not (parser
->lexer
,
12478 && cp_lexer_next_token_is_not (parser
->lexer
,
12480 inputs
= cp_parser_asm_operand_list (parser
);
12482 if (inputs
== error_mark_node
)
12483 invalid_inputs_p
= true;
12485 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12486 /* The clobbers are coming next. */
12489 /* Look for clobbers. */
12491 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12493 /* Consume the `:' or `::'. */
12494 cp_lexer_consume_token (parser
->lexer
);
12495 /* Parse the clobbers. */
12496 if (cp_lexer_next_token_is_not (parser
->lexer
,
12498 clobbers
= cp_parser_asm_clobber_list (parser
);
12501 /* Look for the closing `)'. */
12502 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
12503 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12504 /*consume_paren=*/true);
12505 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
12507 if (!invalid_inputs_p
&& !invalid_outputs_p
)
12509 /* Create the ASM_EXPR. */
12510 if (parser
->in_function_body
)
12512 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
12514 /* If the extended syntax was not used, mark the ASM_EXPR. */
12517 tree temp
= asm_stmt
;
12518 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
12519 temp
= TREE_OPERAND (temp
, 0);
12521 ASM_INPUT_P (temp
) = 1;
12525 cgraph_add_asm_node (string
);
12529 /* Declarators [gram.dcl.decl] */
12531 /* Parse an init-declarator.
12534 declarator initializer [opt]
12539 declarator asm-specification [opt] attributes [opt] initializer [opt]
12541 function-definition:
12542 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12544 decl-specifier-seq [opt] declarator function-try-block
12548 function-definition:
12549 __extension__ function-definition
12551 The DECL_SPECIFIERS apply to this declarator. Returns a
12552 representation of the entity declared. If MEMBER_P is TRUE, then
12553 this declarator appears in a class scope. The new DECL created by
12554 this declarator is returned.
12556 The CHECKS are access checks that should be performed once we know
12557 what entity is being declared (and, therefore, what classes have
12560 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12561 for a function-definition here as well. If the declarator is a
12562 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12563 be TRUE upon return. By that point, the function-definition will
12564 have been completely parsed.
12566 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12570 cp_parser_init_declarator (cp_parser
* parser
,
12571 cp_decl_specifier_seq
*decl_specifiers
,
12572 VEC (deferred_access_check
,gc
)* checks
,
12573 bool function_definition_allowed_p
,
12575 int declares_class_or_enum
,
12576 bool* function_definition_p
)
12578 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
12579 *attributes_start_token
= NULL
;
12580 cp_declarator
*declarator
;
12581 tree prefix_attributes
;
12583 tree asm_specification
;
12585 tree decl
= NULL_TREE
;
12587 int is_initialized
;
12588 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12589 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12591 enum cpp_ttype initialization_kind
;
12592 bool is_direct_init
= false;
12593 bool is_non_constant_init
;
12594 int ctor_dtor_or_conv_p
;
12596 tree pushed_scope
= NULL
;
12598 /* Gather the attributes that were provided with the
12599 decl-specifiers. */
12600 prefix_attributes
= decl_specifiers
->attributes
;
12602 /* Assume that this is not the declarator for a function
12604 if (function_definition_p
)
12605 *function_definition_p
= false;
12607 /* Defer access checks while parsing the declarator; we cannot know
12608 what names are accessible until we know what is being
12610 resume_deferring_access_checks ();
12612 /* Parse the declarator. */
12613 token
= cp_lexer_peek_token (parser
->lexer
);
12615 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12616 &ctor_dtor_or_conv_p
,
12617 /*parenthesized_p=*/NULL
,
12618 /*member_p=*/false);
12619 /* Gather up the deferred checks. */
12620 stop_deferring_access_checks ();
12622 /* If the DECLARATOR was erroneous, there's no need to go
12624 if (declarator
== cp_error_declarator
)
12625 return error_mark_node
;
12627 /* Check that the number of template-parameter-lists is OK. */
12628 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
12630 return error_mark_node
;
12632 if (declares_class_or_enum
& 2)
12633 cp_parser_check_for_definition_in_return_type (declarator
,
12634 decl_specifiers
->type
,
12635 decl_specifiers
->type_location
);
12637 /* Figure out what scope the entity declared by the DECLARATOR is
12638 located in. `grokdeclarator' sometimes changes the scope, so
12639 we compute it now. */
12640 scope
= get_scope_of_declarator (declarator
);
12642 /* If we're allowing GNU extensions, look for an asm-specification
12644 if (cp_parser_allow_gnu_extensions_p (parser
))
12646 /* Look for an asm-specification. */
12647 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
12648 asm_specification
= cp_parser_asm_specification_opt (parser
);
12649 /* And attributes. */
12650 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
12651 attributes
= cp_parser_attributes_opt (parser
);
12655 asm_specification
= NULL_TREE
;
12656 attributes
= NULL_TREE
;
12659 /* Peek at the next token. */
12660 token
= cp_lexer_peek_token (parser
->lexer
);
12661 /* Check to see if the token indicates the start of a
12662 function-definition. */
12663 if (function_declarator_p (declarator
)
12664 && cp_parser_token_starts_function_definition_p (token
))
12666 if (!function_definition_allowed_p
)
12668 /* If a function-definition should not appear here, issue an
12670 cp_parser_error (parser
,
12671 "a function-definition is not allowed here");
12672 return error_mark_node
;
12676 location_t func_brace_location
12677 = cp_lexer_peek_token (parser
->lexer
)->location
;
12679 /* Neither attributes nor an asm-specification are allowed
12680 on a function-definition. */
12681 if (asm_specification
)
12682 error ("%Han asm-specification is not allowed "
12683 "on a function-definition",
12684 &asm_spec_start_token
->location
);
12686 error ("%Hattributes are not allowed on a function-definition",
12687 &attributes_start_token
->location
);
12688 /* This is a function-definition. */
12689 *function_definition_p
= true;
12691 /* Parse the function definition. */
12693 decl
= cp_parser_save_member_function_body (parser
,
12696 prefix_attributes
);
12699 = (cp_parser_function_definition_from_specifiers_and_declarator
12700 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
12702 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
12704 /* This is where the prologue starts... */
12705 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
12706 = func_brace_location
;
12715 Only in function declarations for constructors, destructors, and
12716 type conversions can the decl-specifier-seq be omitted.
12718 We explicitly postpone this check past the point where we handle
12719 function-definitions because we tolerate function-definitions
12720 that are missing their return types in some modes. */
12721 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
12723 cp_parser_error (parser
,
12724 "expected constructor, destructor, or type conversion");
12725 return error_mark_node
;
12728 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12729 if (token
->type
== CPP_EQ
12730 || token
->type
== CPP_OPEN_PAREN
12731 || token
->type
== CPP_OPEN_BRACE
)
12733 is_initialized
= SD_INITIALIZED
;
12734 initialization_kind
= token
->type
;
12736 if (token
->type
== CPP_EQ
12737 && function_declarator_p (declarator
))
12739 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12740 if (t2
->keyword
== RID_DEFAULT
)
12741 is_initialized
= SD_DEFAULTED
;
12742 else if (t2
->keyword
== RID_DELETE
)
12743 is_initialized
= SD_DELETED
;
12748 /* If the init-declarator isn't initialized and isn't followed by a
12749 `,' or `;', it's not a valid init-declarator. */
12750 if (token
->type
!= CPP_COMMA
12751 && token
->type
!= CPP_SEMICOLON
)
12753 cp_parser_error (parser
, "expected initializer");
12754 return error_mark_node
;
12756 is_initialized
= SD_UNINITIALIZED
;
12757 initialization_kind
= CPP_EOF
;
12760 /* Because start_decl has side-effects, we should only call it if we
12761 know we're going ahead. By this point, we know that we cannot
12762 possibly be looking at any other construct. */
12763 cp_parser_commit_to_tentative_parse (parser
);
12765 /* If the decl specifiers were bad, issue an error now that we're
12766 sure this was intended to be a declarator. Then continue
12767 declaring the variable(s), as int, to try to cut down on further
12769 if (decl_specifiers
->any_specifiers_p
12770 && decl_specifiers
->type
== error_mark_node
)
12772 cp_parser_error (parser
, "invalid type in declaration");
12773 decl_specifiers
->type
= integer_type_node
;
12776 /* Check to see whether or not this declaration is a friend. */
12777 friend_p
= cp_parser_friend_p (decl_specifiers
);
12779 /* Enter the newly declared entry in the symbol table. If we're
12780 processing a declaration in a class-specifier, we wait until
12781 after processing the initializer. */
12784 if (parser
->in_unbraced_linkage_specification_p
)
12785 decl_specifiers
->storage_class
= sc_extern
;
12786 decl
= start_decl (declarator
, decl_specifiers
,
12787 is_initialized
, attributes
, prefix_attributes
,
12791 /* Enter the SCOPE. That way unqualified names appearing in the
12792 initializer will be looked up in SCOPE. */
12793 pushed_scope
= push_scope (scope
);
12795 /* Perform deferred access control checks, now that we know in which
12796 SCOPE the declared entity resides. */
12797 if (!member_p
&& decl
)
12799 tree saved_current_function_decl
= NULL_TREE
;
12801 /* If the entity being declared is a function, pretend that we
12802 are in its scope. If it is a `friend', it may have access to
12803 things that would not otherwise be accessible. */
12804 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12806 saved_current_function_decl
= current_function_decl
;
12807 current_function_decl
= decl
;
12810 /* Perform access checks for template parameters. */
12811 cp_parser_perform_template_parameter_access_checks (checks
);
12813 /* Perform the access control checks for the declarator and the
12814 decl-specifiers. */
12815 perform_deferred_access_checks ();
12817 /* Restore the saved value. */
12818 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12819 current_function_decl
= saved_current_function_decl
;
12822 /* Parse the initializer. */
12823 initializer
= NULL_TREE
;
12824 is_direct_init
= false;
12825 is_non_constant_init
= true;
12826 if (is_initialized
)
12828 if (function_declarator_p (declarator
))
12830 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
12831 if (initialization_kind
== CPP_EQ
)
12832 initializer
= cp_parser_pure_specifier (parser
);
12835 /* If the declaration was erroneous, we don't really
12836 know what the user intended, so just silently
12837 consume the initializer. */
12838 if (decl
!= error_mark_node
)
12839 error ("%Hinitializer provided for function",
12840 &initializer_start_token
->location
);
12841 cp_parser_skip_to_closing_parenthesis (parser
,
12842 /*recovering=*/true,
12843 /*or_comma=*/false,
12844 /*consume_paren=*/true);
12848 initializer
= cp_parser_initializer (parser
,
12850 &is_non_constant_init
);
12853 /* The old parser allows attributes to appear after a parenthesized
12854 initializer. Mark Mitchell proposed removing this functionality
12855 on the GCC mailing lists on 2002-08-13. This parser accepts the
12856 attributes -- but ignores them. */
12857 if (cp_parser_allow_gnu_extensions_p (parser
)
12858 && initialization_kind
== CPP_OPEN_PAREN
)
12859 if (cp_parser_attributes_opt (parser
))
12860 warning (OPT_Wattributes
,
12861 "attributes after parenthesized initializer ignored");
12863 /* For an in-class declaration, use `grokfield' to create the
12869 pop_scope (pushed_scope
);
12870 pushed_scope
= false;
12872 decl
= grokfield (declarator
, decl_specifiers
,
12873 initializer
, !is_non_constant_init
,
12874 /*asmspec=*/NULL_TREE
,
12875 prefix_attributes
);
12876 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
12877 cp_parser_save_default_args (parser
, decl
);
12880 /* Finish processing the declaration. But, skip friend
12882 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
12884 cp_finish_decl (decl
,
12885 initializer
, !is_non_constant_init
,
12887 /* If the initializer is in parentheses, then this is
12888 a direct-initialization, which means that an
12889 `explicit' constructor is OK. Otherwise, an
12890 `explicit' constructor cannot be used. */
12891 ((is_direct_init
|| !is_initialized
)
12892 ? 0 : LOOKUP_ONLYCONVERTING
));
12894 else if ((cxx_dialect
!= cxx98
) && friend_p
12895 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
12896 /* Core issue #226 (C++0x only): A default template-argument
12897 shall not be specified in a friend class template
12899 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
12900 /*is_partial=*/0, /*is_friend_decl=*/1);
12902 if (!friend_p
&& pushed_scope
)
12903 pop_scope (pushed_scope
);
12908 /* Parse a declarator.
12912 ptr-operator declarator
12914 abstract-declarator:
12915 ptr-operator abstract-declarator [opt]
12916 direct-abstract-declarator
12921 attributes [opt] direct-declarator
12922 attributes [opt] ptr-operator declarator
12924 abstract-declarator:
12925 attributes [opt] ptr-operator abstract-declarator [opt]
12926 attributes [opt] direct-abstract-declarator
12928 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12929 detect constructor, destructor or conversion operators. It is set
12930 to -1 if the declarator is a name, and +1 if it is a
12931 function. Otherwise it is set to zero. Usually you just want to
12932 test for >0, but internally the negative value is used.
12934 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12935 a decl-specifier-seq unless it declares a constructor, destructor,
12936 or conversion. It might seem that we could check this condition in
12937 semantic analysis, rather than parsing, but that makes it difficult
12938 to handle something like `f()'. We want to notice that there are
12939 no decl-specifiers, and therefore realize that this is an
12940 expression, not a declaration.)
12942 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12943 the declarator is a direct-declarator of the form "(...)".
12945 MEMBER_P is true iff this declarator is a member-declarator. */
12947 static cp_declarator
*
12948 cp_parser_declarator (cp_parser
* parser
,
12949 cp_parser_declarator_kind dcl_kind
,
12950 int* ctor_dtor_or_conv_p
,
12951 bool* parenthesized_p
,
12955 cp_declarator
*declarator
;
12956 enum tree_code code
;
12957 cp_cv_quals cv_quals
;
12959 tree attributes
= NULL_TREE
;
12961 /* Assume this is not a constructor, destructor, or type-conversion
12963 if (ctor_dtor_or_conv_p
)
12964 *ctor_dtor_or_conv_p
= 0;
12966 if (cp_parser_allow_gnu_extensions_p (parser
))
12967 attributes
= cp_parser_attributes_opt (parser
);
12969 /* Peek at the next token. */
12970 token
= cp_lexer_peek_token (parser
->lexer
);
12972 /* Check for the ptr-operator production. */
12973 cp_parser_parse_tentatively (parser
);
12974 /* Parse the ptr-operator. */
12975 code
= cp_parser_ptr_operator (parser
,
12978 /* If that worked, then we have a ptr-operator. */
12979 if (cp_parser_parse_definitely (parser
))
12981 /* If a ptr-operator was found, then this declarator was not
12983 if (parenthesized_p
)
12984 *parenthesized_p
= true;
12985 /* The dependent declarator is optional if we are parsing an
12986 abstract-declarator. */
12987 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12988 cp_parser_parse_tentatively (parser
);
12990 /* Parse the dependent declarator. */
12991 declarator
= cp_parser_declarator (parser
, dcl_kind
,
12992 /*ctor_dtor_or_conv_p=*/NULL
,
12993 /*parenthesized_p=*/NULL
,
12994 /*member_p=*/false);
12996 /* If we are parsing an abstract-declarator, we must handle the
12997 case where the dependent declarator is absent. */
12998 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
12999 && !cp_parser_parse_definitely (parser
))
13002 declarator
= cp_parser_make_indirect_declarator
13003 (code
, class_type
, cv_quals
, declarator
);
13005 /* Everything else is a direct-declarator. */
13008 if (parenthesized_p
)
13009 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
13011 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
13012 ctor_dtor_or_conv_p
,
13016 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
13017 declarator
->attributes
= attributes
;
13022 /* Parse a direct-declarator or direct-abstract-declarator.
13026 direct-declarator ( parameter-declaration-clause )
13027 cv-qualifier-seq [opt]
13028 exception-specification [opt]
13029 direct-declarator [ constant-expression [opt] ]
13032 direct-abstract-declarator:
13033 direct-abstract-declarator [opt]
13034 ( parameter-declaration-clause )
13035 cv-qualifier-seq [opt]
13036 exception-specification [opt]
13037 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13038 ( abstract-declarator )
13040 Returns a representation of the declarator. DCL_KIND is
13041 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13042 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13043 we are parsing a direct-declarator. It is
13044 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13045 of ambiguity we prefer an abstract declarator, as per
13046 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13047 cp_parser_declarator. */
13049 static cp_declarator
*
13050 cp_parser_direct_declarator (cp_parser
* parser
,
13051 cp_parser_declarator_kind dcl_kind
,
13052 int* ctor_dtor_or_conv_p
,
13056 cp_declarator
*declarator
= NULL
;
13057 tree scope
= NULL_TREE
;
13058 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
13059 bool saved_in_declarator_p
= parser
->in_declarator_p
;
13061 tree pushed_scope
= NULL_TREE
;
13065 /* Peek at the next token. */
13066 token
= cp_lexer_peek_token (parser
->lexer
);
13067 if (token
->type
== CPP_OPEN_PAREN
)
13069 /* This is either a parameter-declaration-clause, or a
13070 parenthesized declarator. When we know we are parsing a
13071 named declarator, it must be a parenthesized declarator
13072 if FIRST is true. For instance, `(int)' is a
13073 parameter-declaration-clause, with an omitted
13074 direct-abstract-declarator. But `((*))', is a
13075 parenthesized abstract declarator. Finally, when T is a
13076 template parameter `(T)' is a
13077 parameter-declaration-clause, and not a parenthesized
13080 We first try and parse a parameter-declaration-clause,
13081 and then try a nested declarator (if FIRST is true).
13083 It is not an error for it not to be a
13084 parameter-declaration-clause, even when FIRST is
13090 The first is the declaration of a function while the
13091 second is the definition of a variable, including its
13094 Having seen only the parenthesis, we cannot know which of
13095 these two alternatives should be selected. Even more
13096 complex are examples like:
13101 The former is a function-declaration; the latter is a
13102 variable initialization.
13104 Thus again, we try a parameter-declaration-clause, and if
13105 that fails, we back out and return. */
13107 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
13110 unsigned saved_num_template_parameter_lists
;
13111 bool is_declarator
= false;
13114 /* In a member-declarator, the only valid interpretation
13115 of a parenthesis is the start of a
13116 parameter-declaration-clause. (It is invalid to
13117 initialize a static data member with a parenthesized
13118 initializer; only the "=" form of initialization is
13121 cp_parser_parse_tentatively (parser
);
13123 /* Consume the `('. */
13124 cp_lexer_consume_token (parser
->lexer
);
13127 /* If this is going to be an abstract declarator, we're
13128 in a declarator and we can't have default args. */
13129 parser
->default_arg_ok_p
= false;
13130 parser
->in_declarator_p
= true;
13133 /* Inside the function parameter list, surrounding
13134 template-parameter-lists do not apply. */
13135 saved_num_template_parameter_lists
13136 = parser
->num_template_parameter_lists
;
13137 parser
->num_template_parameter_lists
= 0;
13139 begin_scope (sk_function_parms
, NULL_TREE
);
13141 /* Parse the parameter-declaration-clause. */
13142 params
= cp_parser_parameter_declaration_clause (parser
);
13144 parser
->num_template_parameter_lists
13145 = saved_num_template_parameter_lists
;
13147 /* If all went well, parse the cv-qualifier-seq and the
13148 exception-specification. */
13149 if (member_p
|| cp_parser_parse_definitely (parser
))
13151 cp_cv_quals cv_quals
;
13152 tree exception_specification
;
13155 is_declarator
= true;
13157 if (ctor_dtor_or_conv_p
)
13158 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
13160 /* Consume the `)'. */
13161 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
13163 /* Parse the cv-qualifier-seq. */
13164 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
13165 /* And the exception-specification. */
13166 exception_specification
13167 = cp_parser_exception_specification_opt (parser
);
13170 = cp_parser_late_return_type_opt (parser
);
13172 /* Create the function-declarator. */
13173 declarator
= make_call_declarator (declarator
,
13176 exception_specification
,
13178 /* Any subsequent parameter lists are to do with
13179 return type, so are not those of the declared
13181 parser
->default_arg_ok_p
= false;
13184 /* Remove the function parms from scope. */
13185 for (t
= current_binding_level
->names
; t
; t
= TREE_CHAIN (t
))
13186 pop_binding (DECL_NAME (t
), t
);
13190 /* Repeat the main loop. */
13194 /* If this is the first, we can try a parenthesized
13198 bool saved_in_type_id_in_expr_p
;
13200 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
13201 parser
->in_declarator_p
= saved_in_declarator_p
;
13203 /* Consume the `('. */
13204 cp_lexer_consume_token (parser
->lexer
);
13205 /* Parse the nested declarator. */
13206 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
13207 parser
->in_type_id_in_expr_p
= true;
13209 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
13210 /*parenthesized_p=*/NULL
,
13212 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
13214 /* Expect a `)'. */
13215 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
13216 declarator
= cp_error_declarator
;
13217 if (declarator
== cp_error_declarator
)
13220 goto handle_declarator
;
13222 /* Otherwise, we must be done. */
13226 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
13227 && token
->type
== CPP_OPEN_SQUARE
)
13229 /* Parse an array-declarator. */
13232 if (ctor_dtor_or_conv_p
)
13233 *ctor_dtor_or_conv_p
= 0;
13236 parser
->default_arg_ok_p
= false;
13237 parser
->in_declarator_p
= true;
13238 /* Consume the `['. */
13239 cp_lexer_consume_token (parser
->lexer
);
13240 /* Peek at the next token. */
13241 token
= cp_lexer_peek_token (parser
->lexer
);
13242 /* If the next token is `]', then there is no
13243 constant-expression. */
13244 if (token
->type
!= CPP_CLOSE_SQUARE
)
13246 bool non_constant_p
;
13249 = cp_parser_constant_expression (parser
,
13250 /*allow_non_constant=*/true,
13252 if (!non_constant_p
)
13253 bounds
= fold_non_dependent_expr (bounds
);
13254 /* Normally, the array bound must be an integral constant
13255 expression. However, as an extension, we allow VLAs
13256 in function scopes. */
13257 else if (!parser
->in_function_body
)
13259 error ("%Harray bound is not an integer constant",
13261 bounds
= error_mark_node
;
13265 bounds
= NULL_TREE
;
13266 /* Look for the closing `]'. */
13267 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>"))
13269 declarator
= cp_error_declarator
;
13273 declarator
= make_array_declarator (declarator
, bounds
);
13275 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
13277 tree qualifying_scope
;
13278 tree unqualified_name
;
13279 special_function_kind sfk
;
13281 bool pack_expansion_p
= false;
13282 cp_token
*declarator_id_start_token
;
13284 /* Parse a declarator-id */
13285 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
13288 cp_parser_parse_tentatively (parser
);
13290 /* If we see an ellipsis, we should be looking at a
13292 if (token
->type
== CPP_ELLIPSIS
)
13294 /* Consume the `...' */
13295 cp_lexer_consume_token (parser
->lexer
);
13297 pack_expansion_p
= true;
13301 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
13303 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
13304 qualifying_scope
= parser
->scope
;
13309 if (!unqualified_name
&& pack_expansion_p
)
13311 /* Check whether an error occurred. */
13312 okay
= !cp_parser_error_occurred (parser
);
13314 /* We already consumed the ellipsis to mark a
13315 parameter pack, but we have no way to report it,
13316 so abort the tentative parse. We will be exiting
13317 immediately anyway. */
13318 cp_parser_abort_tentative_parse (parser
);
13321 okay
= cp_parser_parse_definitely (parser
);
13324 unqualified_name
= error_mark_node
;
13325 else if (unqualified_name
13326 && (qualifying_scope
13327 || (TREE_CODE (unqualified_name
)
13328 != IDENTIFIER_NODE
)))
13330 cp_parser_error (parser
, "expected unqualified-id");
13331 unqualified_name
= error_mark_node
;
13335 if (!unqualified_name
)
13337 if (unqualified_name
== error_mark_node
)
13339 declarator
= cp_error_declarator
;
13340 pack_expansion_p
= false;
13341 declarator
->parameter_pack_p
= false;
13345 if (qualifying_scope
&& at_namespace_scope_p ()
13346 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
13348 /* In the declaration of a member of a template class
13349 outside of the class itself, the SCOPE will sometimes
13350 be a TYPENAME_TYPE. For example, given:
13352 template <typename T>
13353 int S<T>::R::i = 3;
13355 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13356 this context, we must resolve S<T>::R to an ordinary
13357 type, rather than a typename type.
13359 The reason we normally avoid resolving TYPENAME_TYPEs
13360 is that a specialization of `S' might render
13361 `S<T>::R' not a type. However, if `S' is
13362 specialized, then this `i' will not be used, so there
13363 is no harm in resolving the types here. */
13366 /* Resolve the TYPENAME_TYPE. */
13367 type
= resolve_typename_type (qualifying_scope
,
13368 /*only_current_p=*/false);
13369 /* If that failed, the declarator is invalid. */
13370 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13371 error ("%H%<%T::%E%> is not a type",
13372 &declarator_id_start_token
->location
,
13373 TYPE_CONTEXT (qualifying_scope
),
13374 TYPE_IDENTIFIER (qualifying_scope
));
13375 qualifying_scope
= type
;
13380 if (unqualified_name
)
13384 if (qualifying_scope
13385 && CLASS_TYPE_P (qualifying_scope
))
13386 class_type
= qualifying_scope
;
13388 class_type
= current_class_type
;
13390 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
13392 tree name_type
= TREE_TYPE (unqualified_name
);
13393 if (class_type
&& same_type_p (name_type
, class_type
))
13395 if (qualifying_scope
13396 && CLASSTYPE_USE_TEMPLATE (name_type
))
13398 error ("%Hinvalid use of constructor as a template",
13399 &declarator_id_start_token
->location
);
13400 inform (input_location
, "use %<%T::%D%> instead of %<%T::%D%> to "
13401 "name the constructor in a qualified name",
13403 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
13404 class_type
, name_type
);
13405 declarator
= cp_error_declarator
;
13409 unqualified_name
= constructor_name (class_type
);
13413 /* We do not attempt to print the declarator
13414 here because we do not have enough
13415 information about its original syntactic
13417 cp_parser_error (parser
, "invalid declarator");
13418 declarator
= cp_error_declarator
;
13425 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
13426 sfk
= sfk_destructor
;
13427 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
13428 sfk
= sfk_conversion
;
13429 else if (/* There's no way to declare a constructor
13430 for an anonymous type, even if the type
13431 got a name for linkage purposes. */
13432 !TYPE_WAS_ANONYMOUS (class_type
)
13433 && constructor_name_p (unqualified_name
,
13436 unqualified_name
= constructor_name (class_type
);
13437 sfk
= sfk_constructor
;
13440 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
13441 *ctor_dtor_or_conv_p
= -1;
13444 declarator
= make_id_declarator (qualifying_scope
,
13447 declarator
->id_loc
= token
->location
;
13448 declarator
->parameter_pack_p
= pack_expansion_p
;
13450 if (pack_expansion_p
)
13451 maybe_warn_variadic_templates ();
13453 handle_declarator
:;
13454 scope
= get_scope_of_declarator (declarator
);
13456 /* Any names that appear after the declarator-id for a
13457 member are looked up in the containing scope. */
13458 pushed_scope
= push_scope (scope
);
13459 parser
->in_declarator_p
= true;
13460 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
13461 || (declarator
&& declarator
->kind
== cdk_id
))
13462 /* Default args are only allowed on function
13464 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
13466 parser
->default_arg_ok_p
= false;
13475 /* For an abstract declarator, we might wind up with nothing at this
13476 point. That's an error; the declarator is not optional. */
13478 cp_parser_error (parser
, "expected declarator");
13480 /* If we entered a scope, we must exit it now. */
13482 pop_scope (pushed_scope
);
13484 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
13485 parser
->in_declarator_p
= saved_in_declarator_p
;
13490 /* Parse a ptr-operator.
13493 * cv-qualifier-seq [opt]
13495 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13500 & cv-qualifier-seq [opt]
13502 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13503 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13504 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13505 filled in with the TYPE containing the member. *CV_QUALS is
13506 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13507 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13508 Note that the tree codes returned by this function have nothing
13509 to do with the types of trees that will be eventually be created
13510 to represent the pointer or reference type being parsed. They are
13511 just constants with suggestive names. */
13512 static enum tree_code
13513 cp_parser_ptr_operator (cp_parser
* parser
,
13515 cp_cv_quals
*cv_quals
)
13517 enum tree_code code
= ERROR_MARK
;
13520 /* Assume that it's not a pointer-to-member. */
13522 /* And that there are no cv-qualifiers. */
13523 *cv_quals
= TYPE_UNQUALIFIED
;
13525 /* Peek at the next token. */
13526 token
= cp_lexer_peek_token (parser
->lexer
);
13528 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13529 if (token
->type
== CPP_MULT
)
13530 code
= INDIRECT_REF
;
13531 else if (token
->type
== CPP_AND
)
13533 else if ((cxx_dialect
!= cxx98
) &&
13534 token
->type
== CPP_AND_AND
) /* C++0x only */
13535 code
= NON_LVALUE_EXPR
;
13537 if (code
!= ERROR_MARK
)
13539 /* Consume the `*', `&' or `&&'. */
13540 cp_lexer_consume_token (parser
->lexer
);
13542 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13543 `&', if we are allowing GNU extensions. (The only qualifier
13544 that can legally appear after `&' is `restrict', but that is
13545 enforced during semantic analysis. */
13546 if (code
== INDIRECT_REF
13547 || cp_parser_allow_gnu_extensions_p (parser
))
13548 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
13552 /* Try the pointer-to-member case. */
13553 cp_parser_parse_tentatively (parser
);
13554 /* Look for the optional `::' operator. */
13555 cp_parser_global_scope_opt (parser
,
13556 /*current_scope_valid_p=*/false);
13557 /* Look for the nested-name specifier. */
13558 token
= cp_lexer_peek_token (parser
->lexer
);
13559 cp_parser_nested_name_specifier (parser
,
13560 /*typename_keyword_p=*/false,
13561 /*check_dependency_p=*/true,
13563 /*is_declaration=*/false);
13564 /* If we found it, and the next token is a `*', then we are
13565 indeed looking at a pointer-to-member operator. */
13566 if (!cp_parser_error_occurred (parser
)
13567 && cp_parser_require (parser
, CPP_MULT
, "%<*%>"))
13569 /* Indicate that the `*' operator was used. */
13570 code
= INDIRECT_REF
;
13572 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
13573 error ("%H%qD is a namespace", &token
->location
, parser
->scope
);
13576 /* The type of which the member is a member is given by the
13578 *type
= parser
->scope
;
13579 /* The next name will not be qualified. */
13580 parser
->scope
= NULL_TREE
;
13581 parser
->qualifying_scope
= NULL_TREE
;
13582 parser
->object_scope
= NULL_TREE
;
13583 /* Look for the optional cv-qualifier-seq. */
13584 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
13587 /* If that didn't work we don't have a ptr-operator. */
13588 if (!cp_parser_parse_definitely (parser
))
13589 cp_parser_error (parser
, "expected ptr-operator");
13595 /* Parse an (optional) cv-qualifier-seq.
13598 cv-qualifier cv-qualifier-seq [opt]
13609 Returns a bitmask representing the cv-qualifiers. */
13612 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
13614 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
13619 cp_cv_quals cv_qualifier
;
13621 /* Peek at the next token. */
13622 token
= cp_lexer_peek_token (parser
->lexer
);
13623 /* See if it's a cv-qualifier. */
13624 switch (token
->keyword
)
13627 cv_qualifier
= TYPE_QUAL_CONST
;
13631 cv_qualifier
= TYPE_QUAL_VOLATILE
;
13635 cv_qualifier
= TYPE_QUAL_RESTRICT
;
13639 cv_qualifier
= TYPE_UNQUALIFIED
;
13646 if (cv_quals
& cv_qualifier
)
13648 error ("%Hduplicate cv-qualifier", &token
->location
);
13649 cp_lexer_purge_token (parser
->lexer
);
13653 cp_lexer_consume_token (parser
->lexer
);
13654 cv_quals
|= cv_qualifier
;
13661 /* Parse a late-specified return type, if any. This is not a separate
13662 non-terminal, but part of a function declarator, which looks like
13666 Returns the type indicated by the type-id. */
13669 cp_parser_late_return_type_opt (cp_parser
* parser
)
13673 /* Peek at the next token. */
13674 token
= cp_lexer_peek_token (parser
->lexer
);
13675 /* A late-specified return type is indicated by an initial '->'. */
13676 if (token
->type
!= CPP_DEREF
)
13679 /* Consume the ->. */
13680 cp_lexer_consume_token (parser
->lexer
);
13682 return cp_parser_type_id (parser
);
13685 /* Parse a declarator-id.
13689 :: [opt] nested-name-specifier [opt] type-name
13691 In the `id-expression' case, the value returned is as for
13692 cp_parser_id_expression if the id-expression was an unqualified-id.
13693 If the id-expression was a qualified-id, then a SCOPE_REF is
13694 returned. The first operand is the scope (either a NAMESPACE_DECL
13695 or TREE_TYPE), but the second is still just a representation of an
13699 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
13702 /* The expression must be an id-expression. Assume that qualified
13703 names are the names of types so that:
13706 int S<T>::R::i = 3;
13708 will work; we must treat `S<T>::R' as the name of a type.
13709 Similarly, assume that qualified names are templates, where
13713 int S<T>::R<T>::i = 3;
13716 id
= cp_parser_id_expression (parser
,
13717 /*template_keyword_p=*/false,
13718 /*check_dependency_p=*/false,
13719 /*template_p=*/NULL
,
13720 /*declarator_p=*/true,
13722 if (id
&& BASELINK_P (id
))
13723 id
= BASELINK_FUNCTIONS (id
);
13727 /* Parse a type-id.
13730 type-specifier-seq abstract-declarator [opt]
13732 Returns the TYPE specified. */
13735 cp_parser_type_id (cp_parser
* parser
)
13737 cp_decl_specifier_seq type_specifier_seq
;
13738 cp_declarator
*abstract_declarator
;
13740 /* Parse the type-specifier-seq. */
13741 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
13742 &type_specifier_seq
);
13743 if (type_specifier_seq
.type
== error_mark_node
)
13744 return error_mark_node
;
13746 /* There might or might not be an abstract declarator. */
13747 cp_parser_parse_tentatively (parser
);
13748 /* Look for the declarator. */
13749 abstract_declarator
13750 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
13751 /*parenthesized_p=*/NULL
,
13752 /*member_p=*/false);
13753 /* Check to see if there really was a declarator. */
13754 if (!cp_parser_parse_definitely (parser
))
13755 abstract_declarator
= NULL
;
13757 if (type_specifier_seq
.type
13758 && type_uses_auto (type_specifier_seq
.type
))
13760 error ("invalid use of %<auto%>");
13761 return error_mark_node
;
13764 return groktypename (&type_specifier_seq
, abstract_declarator
);
13767 /* Parse a type-specifier-seq.
13769 type-specifier-seq:
13770 type-specifier type-specifier-seq [opt]
13774 type-specifier-seq:
13775 attributes type-specifier-seq [opt]
13777 If IS_CONDITION is true, we are at the start of a "condition",
13778 e.g., we've just seen "if (".
13780 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13783 cp_parser_type_specifier_seq (cp_parser
* parser
,
13785 cp_decl_specifier_seq
*type_specifier_seq
)
13787 bool seen_type_specifier
= false;
13788 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
13789 cp_token
*start_token
= NULL
;
13791 /* Clear the TYPE_SPECIFIER_SEQ. */
13792 clear_decl_specs (type_specifier_seq
);
13794 /* Parse the type-specifiers and attributes. */
13797 tree type_specifier
;
13798 bool is_cv_qualifier
;
13800 /* Check for attributes first. */
13801 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
13803 type_specifier_seq
->attributes
=
13804 chainon (type_specifier_seq
->attributes
,
13805 cp_parser_attributes_opt (parser
));
13809 /* record the token of the beginning of the type specifier seq,
13810 for error reporting purposes*/
13812 start_token
= cp_lexer_peek_token (parser
->lexer
);
13814 /* Look for the type-specifier. */
13815 type_specifier
= cp_parser_type_specifier (parser
,
13817 type_specifier_seq
,
13818 /*is_declaration=*/false,
13821 if (!type_specifier
)
13823 /* If the first type-specifier could not be found, this is not a
13824 type-specifier-seq at all. */
13825 if (!seen_type_specifier
)
13827 cp_parser_error (parser
, "expected type-specifier");
13828 type_specifier_seq
->type
= error_mark_node
;
13831 /* If subsequent type-specifiers could not be found, the
13832 type-specifier-seq is complete. */
13836 seen_type_specifier
= true;
13837 /* The standard says that a condition can be:
13839 type-specifier-seq declarator = assignment-expression
13846 we should treat the "S" as a declarator, not as a
13847 type-specifier. The standard doesn't say that explicitly for
13848 type-specifier-seq, but it does say that for
13849 decl-specifier-seq in an ordinary declaration. Perhaps it
13850 would be clearer just to allow a decl-specifier-seq here, and
13851 then add a semantic restriction that if any decl-specifiers
13852 that are not type-specifiers appear, the program is invalid. */
13853 if (is_condition
&& !is_cv_qualifier
)
13854 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13857 cp_parser_check_decl_spec (type_specifier_seq
, start_token
->location
);
13860 /* Parse a parameter-declaration-clause.
13862 parameter-declaration-clause:
13863 parameter-declaration-list [opt] ... [opt]
13864 parameter-declaration-list , ...
13866 Returns a representation for the parameter declarations. A return
13867 value of NULL indicates a parameter-declaration-clause consisting
13868 only of an ellipsis. */
13871 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
13878 /* Peek at the next token. */
13879 token
= cp_lexer_peek_token (parser
->lexer
);
13880 /* Check for trivial parameter-declaration-clauses. */
13881 if (token
->type
== CPP_ELLIPSIS
)
13883 /* Consume the `...' token. */
13884 cp_lexer_consume_token (parser
->lexer
);
13887 else if (token
->type
== CPP_CLOSE_PAREN
)
13888 /* There are no parameters. */
13890 #ifndef NO_IMPLICIT_EXTERN_C
13891 if (in_system_header
&& current_class_type
== NULL
13892 && current_lang_name
== lang_name_c
)
13896 return void_list_node
;
13898 /* Check for `(void)', too, which is a special case. */
13899 else if (token
->keyword
== RID_VOID
13900 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13901 == CPP_CLOSE_PAREN
))
13903 /* Consume the `void' token. */
13904 cp_lexer_consume_token (parser
->lexer
);
13905 /* There are no parameters. */
13906 return void_list_node
;
13909 /* Parse the parameter-declaration-list. */
13910 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
13911 /* If a parse error occurred while parsing the
13912 parameter-declaration-list, then the entire
13913 parameter-declaration-clause is erroneous. */
13917 /* Peek at the next token. */
13918 token
= cp_lexer_peek_token (parser
->lexer
);
13919 /* If it's a `,', the clause should terminate with an ellipsis. */
13920 if (token
->type
== CPP_COMMA
)
13922 /* Consume the `,'. */
13923 cp_lexer_consume_token (parser
->lexer
);
13924 /* Expect an ellipsis. */
13926 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "%<...%>") != NULL
);
13928 /* It might also be `...' if the optional trailing `,' was
13930 else if (token
->type
== CPP_ELLIPSIS
)
13932 /* Consume the `...' token. */
13933 cp_lexer_consume_token (parser
->lexer
);
13934 /* And remember that we saw it. */
13938 ellipsis_p
= false;
13940 /* Finish the parameter list. */
13942 parameters
= chainon (parameters
, void_list_node
);
13947 /* Parse a parameter-declaration-list.
13949 parameter-declaration-list:
13950 parameter-declaration
13951 parameter-declaration-list , parameter-declaration
13953 Returns a representation of the parameter-declaration-list, as for
13954 cp_parser_parameter_declaration_clause. However, the
13955 `void_list_node' is never appended to the list. Upon return,
13956 *IS_ERROR will be true iff an error occurred. */
13959 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
13961 tree parameters
= NULL_TREE
;
13962 tree
*tail
= ¶meters
;
13963 bool saved_in_unbraced_linkage_specification_p
;
13965 /* Assume all will go well. */
13967 /* The special considerations that apply to a function within an
13968 unbraced linkage specifications do not apply to the parameters
13969 to the function. */
13970 saved_in_unbraced_linkage_specification_p
13971 = parser
->in_unbraced_linkage_specification_p
;
13972 parser
->in_unbraced_linkage_specification_p
= false;
13974 /* Look for more parameters. */
13977 cp_parameter_declarator
*parameter
;
13978 tree decl
= error_mark_node
;
13979 bool parenthesized_p
;
13980 /* Parse the parameter. */
13982 = cp_parser_parameter_declaration (parser
,
13983 /*template_parm_p=*/false,
13986 /* We don't know yet if the enclosing context is deprecated, so wait
13987 and warn in grokparms if appropriate. */
13988 deprecated_state
= DEPRECATED_SUPPRESS
;
13991 decl
= grokdeclarator (parameter
->declarator
,
13992 ¶meter
->decl_specifiers
,
13994 parameter
->default_argument
!= NULL_TREE
,
13995 ¶meter
->decl_specifiers
.attributes
);
13997 deprecated_state
= DEPRECATED_NORMAL
;
13999 /* If a parse error occurred parsing the parameter declaration,
14000 then the entire parameter-declaration-list is erroneous. */
14001 if (decl
== error_mark_node
)
14004 parameters
= error_mark_node
;
14008 if (parameter
->decl_specifiers
.attributes
)
14009 cplus_decl_attributes (&decl
,
14010 parameter
->decl_specifiers
.attributes
,
14012 if (DECL_NAME (decl
))
14013 decl
= pushdecl (decl
);
14015 /* Add the new parameter to the list. */
14016 *tail
= build_tree_list (parameter
->default_argument
, decl
);
14017 tail
= &TREE_CHAIN (*tail
);
14019 /* Peek at the next token. */
14020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
14021 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
14022 /* These are for Objective-C++ */
14023 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14024 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14025 /* The parameter-declaration-list is complete. */
14027 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14031 /* Peek at the next token. */
14032 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14033 /* If it's an ellipsis, then the list is complete. */
14034 if (token
->type
== CPP_ELLIPSIS
)
14036 /* Otherwise, there must be more parameters. Consume the
14038 cp_lexer_consume_token (parser
->lexer
);
14039 /* When parsing something like:
14041 int i(float f, double d)
14043 we can tell after seeing the declaration for "f" that we
14044 are not looking at an initialization of a variable "i",
14045 but rather at the declaration of a function "i".
14047 Due to the fact that the parsing of template arguments
14048 (as specified to a template-id) requires backtracking we
14049 cannot use this technique when inside a template argument
14051 if (!parser
->in_template_argument_list_p
14052 && !parser
->in_type_id_in_expr_p
14053 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
14054 /* However, a parameter-declaration of the form
14055 "foat(f)" (which is a valid declaration of a
14056 parameter "f") can also be interpreted as an
14057 expression (the conversion of "f" to "float"). */
14058 && !parenthesized_p
)
14059 cp_parser_commit_to_tentative_parse (parser
);
14063 cp_parser_error (parser
, "expected %<,%> or %<...%>");
14064 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14065 cp_parser_skip_to_closing_parenthesis (parser
,
14066 /*recovering=*/true,
14067 /*or_comma=*/false,
14068 /*consume_paren=*/false);
14073 parser
->in_unbraced_linkage_specification_p
14074 = saved_in_unbraced_linkage_specification_p
;
14079 /* Parse a parameter declaration.
14081 parameter-declaration:
14082 decl-specifier-seq ... [opt] declarator
14083 decl-specifier-seq declarator = assignment-expression
14084 decl-specifier-seq ... [opt] abstract-declarator [opt]
14085 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14087 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14088 declares a template parameter. (In that case, a non-nested `>'
14089 token encountered during the parsing of the assignment-expression
14090 is not interpreted as a greater-than operator.)
14092 Returns a representation of the parameter, or NULL if an error
14093 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14094 true iff the declarator is of the form "(p)". */
14096 static cp_parameter_declarator
*
14097 cp_parser_parameter_declaration (cp_parser
*parser
,
14098 bool template_parm_p
,
14099 bool *parenthesized_p
)
14101 int declares_class_or_enum
;
14102 bool greater_than_is_operator_p
;
14103 cp_decl_specifier_seq decl_specifiers
;
14104 cp_declarator
*declarator
;
14105 tree default_argument
;
14106 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
14107 const char *saved_message
;
14109 /* In a template parameter, `>' is not an operator.
14113 When parsing a default template-argument for a non-type
14114 template-parameter, the first non-nested `>' is taken as the end
14115 of the template parameter-list rather than a greater-than
14117 greater_than_is_operator_p
= !template_parm_p
;
14119 /* Type definitions may not appear in parameter types. */
14120 saved_message
= parser
->type_definition_forbidden_message
;
14121 parser
->type_definition_forbidden_message
14122 = "types may not be defined in parameter types";
14124 /* Parse the declaration-specifiers. */
14125 cp_parser_decl_specifier_seq (parser
,
14126 CP_PARSER_FLAGS_NONE
,
14128 &declares_class_or_enum
);
14129 /* If an error occurred, there's no reason to attempt to parse the
14130 rest of the declaration. */
14131 if (cp_parser_error_occurred (parser
))
14133 parser
->type_definition_forbidden_message
= saved_message
;
14137 /* Peek at the next token. */
14138 token
= cp_lexer_peek_token (parser
->lexer
);
14140 /* If the next token is a `)', `,', `=', `>', or `...', then there
14141 is no declarator. However, when variadic templates are enabled,
14142 there may be a declarator following `...'. */
14143 if (token
->type
== CPP_CLOSE_PAREN
14144 || token
->type
== CPP_COMMA
14145 || token
->type
== CPP_EQ
14146 || token
->type
== CPP_GREATER
)
14149 if (parenthesized_p
)
14150 *parenthesized_p
= false;
14152 /* Otherwise, there should be a declarator. */
14155 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
14156 parser
->default_arg_ok_p
= false;
14158 /* After seeing a decl-specifier-seq, if the next token is not a
14159 "(", there is no possibility that the code is a valid
14160 expression. Therefore, if parsing tentatively, we commit at
14162 if (!parser
->in_template_argument_list_p
14163 /* In an expression context, having seen:
14167 we cannot be sure whether we are looking at a
14168 function-type (taking a "char" as a parameter) or a cast
14169 of some object of type "char" to "int". */
14170 && !parser
->in_type_id_in_expr_p
14171 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
14172 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
14173 cp_parser_commit_to_tentative_parse (parser
);
14174 /* Parse the declarator. */
14175 declarator_token_start
= token
;
14176 declarator
= cp_parser_declarator (parser
,
14177 CP_PARSER_DECLARATOR_EITHER
,
14178 /*ctor_dtor_or_conv_p=*/NULL
,
14180 /*member_p=*/false);
14181 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14182 /* After the declarator, allow more attributes. */
14183 decl_specifiers
.attributes
14184 = chainon (decl_specifiers
.attributes
,
14185 cp_parser_attributes_opt (parser
));
14188 /* If the next token is an ellipsis, and we have not seen a
14189 declarator name, and the type of the declarator contains parameter
14190 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14191 a parameter pack expansion expression. Otherwise, leave the
14192 ellipsis for a C-style variadic function. */
14193 token
= cp_lexer_peek_token (parser
->lexer
);
14194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14196 tree type
= decl_specifiers
.type
;
14198 if (type
&& DECL_P (type
))
14199 type
= TREE_TYPE (type
);
14202 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
14203 && declarator_can_be_parameter_pack (declarator
)
14204 && (!declarator
|| !declarator
->parameter_pack_p
)
14205 && uses_parameter_packs (type
))
14207 /* Consume the `...'. */
14208 cp_lexer_consume_token (parser
->lexer
);
14209 maybe_warn_variadic_templates ();
14211 /* Build a pack expansion type */
14213 declarator
->parameter_pack_p
= true;
14215 decl_specifiers
.type
= make_pack_expansion (type
);
14219 /* The restriction on defining new types applies only to the type
14220 of the parameter, not to the default argument. */
14221 parser
->type_definition_forbidden_message
= saved_message
;
14223 /* If the next token is `=', then process a default argument. */
14224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14226 /* Consume the `='. */
14227 cp_lexer_consume_token (parser
->lexer
);
14229 /* If we are defining a class, then the tokens that make up the
14230 default argument must be saved and processed later. */
14231 if (!template_parm_p
&& at_class_scope_p ()
14232 && TYPE_BEING_DEFINED (current_class_type
))
14234 unsigned depth
= 0;
14235 int maybe_template_id
= 0;
14236 cp_token
*first_token
;
14239 /* Add tokens until we have processed the entire default
14240 argument. We add the range [first_token, token). */
14241 first_token
= cp_lexer_peek_token (parser
->lexer
);
14246 /* Peek at the next token. */
14247 token
= cp_lexer_peek_token (parser
->lexer
);
14248 /* What we do depends on what token we have. */
14249 switch (token
->type
)
14251 /* In valid code, a default argument must be
14252 immediately followed by a `,' `)', or `...'. */
14254 if (depth
== 0 && maybe_template_id
)
14256 /* If we've seen a '<', we might be in a
14257 template-argument-list. Until Core issue 325 is
14258 resolved, we don't know how this situation ought
14259 to be handled, so try to DTRT. We check whether
14260 what comes after the comma is a valid parameter
14261 declaration list. If it is, then the comma ends
14262 the default argument; otherwise the default
14263 argument continues. */
14264 bool error
= false;
14266 /* Set ITALP so cp_parser_parameter_declaration_list
14267 doesn't decide to commit to this parse. */
14268 bool saved_italp
= parser
->in_template_argument_list_p
;
14269 parser
->in_template_argument_list_p
= true;
14271 cp_parser_parse_tentatively (parser
);
14272 cp_lexer_consume_token (parser
->lexer
);
14273 cp_parser_parameter_declaration_list (parser
, &error
);
14274 if (!cp_parser_error_occurred (parser
) && !error
)
14276 cp_parser_abort_tentative_parse (parser
);
14278 parser
->in_template_argument_list_p
= saved_italp
;
14281 case CPP_CLOSE_PAREN
:
14283 /* If we run into a non-nested `;', `}', or `]',
14284 then the code is invalid -- but the default
14285 argument is certainly over. */
14286 case CPP_SEMICOLON
:
14287 case CPP_CLOSE_BRACE
:
14288 case CPP_CLOSE_SQUARE
:
14291 /* Update DEPTH, if necessary. */
14292 else if (token
->type
== CPP_CLOSE_PAREN
14293 || token
->type
== CPP_CLOSE_BRACE
14294 || token
->type
== CPP_CLOSE_SQUARE
)
14298 case CPP_OPEN_PAREN
:
14299 case CPP_OPEN_SQUARE
:
14300 case CPP_OPEN_BRACE
:
14306 /* This might be the comparison operator, or it might
14307 start a template argument list. */
14308 ++maybe_template_id
;
14312 if (cxx_dialect
== cxx98
)
14314 /* Fall through for C++0x, which treats the `>>'
14315 operator like two `>' tokens in certain
14321 /* This might be an operator, or it might close a
14322 template argument list. But if a previous '<'
14323 started a template argument list, this will have
14324 closed it, so we can't be in one anymore. */
14325 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
14326 if (maybe_template_id
< 0)
14327 maybe_template_id
= 0;
14331 /* If we run out of tokens, issue an error message. */
14333 case CPP_PRAGMA_EOL
:
14334 error ("%Hfile ends in default argument", &token
->location
);
14340 /* In these cases, we should look for template-ids.
14341 For example, if the default argument is
14342 `X<int, double>()', we need to do name lookup to
14343 figure out whether or not `X' is a template; if
14344 so, the `,' does not end the default argument.
14346 That is not yet done. */
14353 /* If we've reached the end, stop. */
14357 /* Add the token to the token block. */
14358 token
= cp_lexer_consume_token (parser
->lexer
);
14361 /* Create a DEFAULT_ARG to represent the unparsed default
14363 default_argument
= make_node (DEFAULT_ARG
);
14364 DEFARG_TOKENS (default_argument
)
14365 = cp_token_cache_new (first_token
, token
);
14366 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
14368 /* Outside of a class definition, we can just parse the
14369 assignment-expression. */
14372 token
= cp_lexer_peek_token (parser
->lexer
);
14374 = cp_parser_default_argument (parser
, template_parm_p
);
14377 if (!parser
->default_arg_ok_p
)
14379 if (flag_permissive
)
14380 warning (0, "deprecated use of default argument for parameter of non-function");
14383 error ("%Hdefault arguments are only "
14384 "permitted for function parameters",
14386 default_argument
= NULL_TREE
;
14389 else if ((declarator
&& declarator
->parameter_pack_p
)
14390 || (decl_specifiers
.type
14391 && PACK_EXPANSION_P (decl_specifiers
.type
)))
14393 const char* kind
= template_parm_p
? "template " : "";
14395 /* Find the name of the parameter pack. */
14396 cp_declarator
*id_declarator
= declarator
;
14397 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
14398 id_declarator
= id_declarator
->declarator
;
14400 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
14401 error ("%H%sparameter pack %qD cannot have a default argument",
14402 &declarator_token_start
->location
,
14403 kind
, id_declarator
->u
.id
.unqualified_name
);
14405 error ("%H%sparameter pack cannot have a default argument",
14406 &declarator_token_start
->location
, kind
);
14408 default_argument
= NULL_TREE
;
14412 default_argument
= NULL_TREE
;
14414 return make_parameter_declarator (&decl_specifiers
,
14419 /* Parse a default argument and return it.
14421 TEMPLATE_PARM_P is true if this is a default argument for a
14422 non-type template parameter. */
14424 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
14426 tree default_argument
= NULL_TREE
;
14427 bool saved_greater_than_is_operator_p
;
14428 bool saved_local_variables_forbidden_p
;
14430 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14432 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
14433 parser
->greater_than_is_operator_p
= !template_parm_p
;
14434 /* Local variable names (and the `this' keyword) may not
14435 appear in a default argument. */
14436 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14437 parser
->local_variables_forbidden_p
= true;
14438 /* The default argument expression may cause implicitly
14439 defined member functions to be synthesized, which will
14440 result in garbage collection. We must treat this
14441 situation as if we were within the body of function so as
14442 to avoid collecting live data on the stack. */
14444 /* Parse the assignment-expression. */
14445 if (template_parm_p
)
14446 push_deferring_access_checks (dk_no_deferred
);
14448 = cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
14449 if (template_parm_p
)
14450 pop_deferring_access_checks ();
14451 /* Restore saved state. */
14453 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
14454 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14456 return default_argument
;
14459 /* Parse a function-body.
14462 compound_statement */
14465 cp_parser_function_body (cp_parser
*parser
)
14467 cp_parser_compound_statement (parser
, NULL
, false);
14470 /* Parse a ctor-initializer-opt followed by a function-body. Return
14471 true if a ctor-initializer was present. */
14474 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
14477 bool ctor_initializer_p
;
14479 /* Begin the function body. */
14480 body
= begin_function_body ();
14481 /* Parse the optional ctor-initializer. */
14482 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
14483 /* Parse the function-body. */
14484 cp_parser_function_body (parser
);
14485 /* Finish the function body. */
14486 finish_function_body (body
);
14488 return ctor_initializer_p
;
14491 /* Parse an initializer.
14494 = initializer-clause
14495 ( expression-list )
14497 Returns an expression representing the initializer. If no
14498 initializer is present, NULL_TREE is returned.
14500 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14501 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14502 set to TRUE if there is no initializer present. If there is an
14503 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14504 is set to true; otherwise it is set to false. */
14507 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
14508 bool* non_constant_p
)
14513 /* Peek at the next token. */
14514 token
= cp_lexer_peek_token (parser
->lexer
);
14516 /* Let our caller know whether or not this initializer was
14518 *is_direct_init
= (token
->type
!= CPP_EQ
);
14519 /* Assume that the initializer is constant. */
14520 *non_constant_p
= false;
14522 if (token
->type
== CPP_EQ
)
14524 /* Consume the `='. */
14525 cp_lexer_consume_token (parser
->lexer
);
14526 /* Parse the initializer-clause. */
14527 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
14529 else if (token
->type
== CPP_OPEN_PAREN
)
14530 init
= cp_parser_parenthesized_expression_list (parser
, false,
14532 /*allow_expansion_p=*/true,
14534 else if (token
->type
== CPP_OPEN_BRACE
)
14536 maybe_warn_cpp0x ("extended initializer lists");
14537 init
= cp_parser_braced_list (parser
, non_constant_p
);
14538 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
14542 /* Anything else is an error. */
14543 cp_parser_error (parser
, "expected initializer");
14544 init
= error_mark_node
;
14550 /* Parse an initializer-clause.
14552 initializer-clause:
14553 assignment-expression
14556 Returns an expression representing the initializer.
14558 If the `assignment-expression' production is used the value
14559 returned is simply a representation for the expression.
14561 Otherwise, calls cp_parser_braced_list. */
14564 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
14568 /* Assume the expression is constant. */
14569 *non_constant_p
= false;
14571 /* If it is not a `{', then we are looking at an
14572 assignment-expression. */
14573 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
14576 = cp_parser_constant_expression (parser
,
14577 /*allow_non_constant_p=*/true,
14579 if (!*non_constant_p
)
14580 initializer
= fold_non_dependent_expr (initializer
);
14583 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
14585 return initializer
;
14588 /* Parse a brace-enclosed initializer list.
14591 { initializer-list , [opt] }
14594 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14595 the elements of the initializer-list (or NULL, if the last
14596 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14597 NULL_TREE. There is no way to detect whether or not the optional
14598 trailing `,' was provided. NON_CONSTANT_P is as for
14599 cp_parser_initializer. */
14602 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
14606 /* Consume the `{' token. */
14607 cp_lexer_consume_token (parser
->lexer
);
14608 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14609 initializer
= make_node (CONSTRUCTOR
);
14610 /* If it's not a `}', then there is a non-trivial initializer. */
14611 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
14613 /* Parse the initializer list. */
14614 CONSTRUCTOR_ELTS (initializer
)
14615 = cp_parser_initializer_list (parser
, non_constant_p
);
14616 /* A trailing `,' token is allowed. */
14617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14618 cp_lexer_consume_token (parser
->lexer
);
14620 /* Now, there should be a trailing `}'. */
14621 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
14622 TREE_TYPE (initializer
) = init_list_type_node
;
14623 return initializer
;
14626 /* Parse an initializer-list.
14629 initializer-clause ... [opt]
14630 initializer-list , initializer-clause ... [opt]
14635 identifier : initializer-clause
14636 initializer-list, identifier : initializer-clause
14638 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14639 for the initializer. If the INDEX of the elt is non-NULL, it is the
14640 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14641 as for cp_parser_initializer. */
14643 static VEC(constructor_elt
,gc
) *
14644 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
14646 VEC(constructor_elt
,gc
) *v
= NULL
;
14648 /* Assume all of the expressions are constant. */
14649 *non_constant_p
= false;
14651 /* Parse the rest of the list. */
14657 bool clause_non_constant_p
;
14659 /* If the next token is an identifier and the following one is a
14660 colon, we are looking at the GNU designated-initializer
14662 if (cp_parser_allow_gnu_extensions_p (parser
)
14663 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
14664 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
14666 /* Warn the user that they are using an extension. */
14667 pedwarn (input_location
, OPT_pedantic
,
14668 "ISO C++ does not allow designated initializers");
14669 /* Consume the identifier. */
14670 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14671 /* Consume the `:'. */
14672 cp_lexer_consume_token (parser
->lexer
);
14675 identifier
= NULL_TREE
;
14677 /* Parse the initializer. */
14678 initializer
= cp_parser_initializer_clause (parser
,
14679 &clause_non_constant_p
);
14680 /* If any clause is non-constant, so is the entire initializer. */
14681 if (clause_non_constant_p
)
14682 *non_constant_p
= true;
14684 /* If we have an ellipsis, this is an initializer pack
14686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14688 /* Consume the `...'. */
14689 cp_lexer_consume_token (parser
->lexer
);
14691 /* Turn the initializer into an initializer expansion. */
14692 initializer
= make_pack_expansion (initializer
);
14695 /* Add it to the vector. */
14696 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
14698 /* If the next token is not a comma, we have reached the end of
14700 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14703 /* Peek at the next token. */
14704 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14705 /* If the next token is a `}', then we're still done. An
14706 initializer-clause can have a trailing `,' after the
14707 initializer-list and before the closing `}'. */
14708 if (token
->type
== CPP_CLOSE_BRACE
)
14711 /* Consume the `,' token. */
14712 cp_lexer_consume_token (parser
->lexer
);
14718 /* Classes [gram.class] */
14720 /* Parse a class-name.
14726 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14727 to indicate that names looked up in dependent types should be
14728 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14729 keyword has been used to indicate that the name that appears next
14730 is a template. TAG_TYPE indicates the explicit tag given before
14731 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14732 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14733 is the class being defined in a class-head.
14735 Returns the TYPE_DECL representing the class. */
14738 cp_parser_class_name (cp_parser
*parser
,
14739 bool typename_keyword_p
,
14740 bool template_keyword_p
,
14741 enum tag_types tag_type
,
14742 bool check_dependency_p
,
14744 bool is_declaration
)
14750 tree identifier
= NULL_TREE
;
14752 /* All class-names start with an identifier. */
14753 token
= cp_lexer_peek_token (parser
->lexer
);
14754 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
14756 cp_parser_error (parser
, "expected class-name");
14757 return error_mark_node
;
14760 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14761 to a template-id, so we save it here. */
14762 scope
= parser
->scope
;
14763 if (scope
== error_mark_node
)
14764 return error_mark_node
;
14766 /* Any name names a type if we're following the `typename' keyword
14767 in a qualified name where the enclosing scope is type-dependent. */
14768 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
14769 && dependent_type_p (scope
));
14770 /* Handle the common case (an identifier, but not a template-id)
14772 if (token
->type
== CPP_NAME
14773 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
14775 cp_token
*identifier_token
;
14778 /* Look for the identifier. */
14779 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
14780 ambiguous_p
= identifier_token
->ambiguous_p
;
14781 identifier
= cp_parser_identifier (parser
);
14782 /* If the next token isn't an identifier, we are certainly not
14783 looking at a class-name. */
14784 if (identifier
== error_mark_node
)
14785 decl
= error_mark_node
;
14786 /* If we know this is a type-name, there's no need to look it
14788 else if (typename_p
)
14792 tree ambiguous_decls
;
14793 /* If we already know that this lookup is ambiguous, then
14794 we've already issued an error message; there's no reason
14798 cp_parser_simulate_error (parser
);
14799 return error_mark_node
;
14801 /* If the next token is a `::', then the name must be a type
14804 [basic.lookup.qual]
14806 During the lookup for a name preceding the :: scope
14807 resolution operator, object, function, and enumerator
14808 names are ignored. */
14809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14810 tag_type
= typename_type
;
14811 /* Look up the name. */
14812 decl
= cp_parser_lookup_name (parser
, identifier
,
14814 /*is_template=*/false,
14815 /*is_namespace=*/false,
14816 check_dependency_p
,
14818 identifier_token
->location
);
14819 if (ambiguous_decls
)
14821 error ("%Hreference to %qD is ambiguous",
14822 &identifier_token
->location
, identifier
);
14823 print_candidates (ambiguous_decls
);
14824 if (cp_parser_parsing_tentatively (parser
))
14826 identifier_token
->ambiguous_p
= true;
14827 cp_parser_simulate_error (parser
);
14829 return error_mark_node
;
14835 /* Try a template-id. */
14836 decl
= cp_parser_template_id (parser
, template_keyword_p
,
14837 check_dependency_p
,
14839 if (decl
== error_mark_node
)
14840 return error_mark_node
;
14843 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
14845 /* If this is a typename, create a TYPENAME_TYPE. */
14846 if (typename_p
&& decl
!= error_mark_node
)
14848 decl
= make_typename_type (scope
, decl
, typename_type
,
14849 /*complain=*/tf_error
);
14850 if (decl
!= error_mark_node
)
14851 decl
= TYPE_NAME (decl
);
14854 /* Check to see that it is really the name of a class. */
14855 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14856 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
14857 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14858 /* Situations like this:
14860 template <typename T> struct A {
14861 typename T::template X<int>::I i;
14864 are problematic. Is `T::template X<int>' a class-name? The
14865 standard does not seem to be definitive, but there is no other
14866 valid interpretation of the following `::'. Therefore, those
14867 names are considered class-names. */
14869 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
14870 if (decl
!= error_mark_node
)
14871 decl
= TYPE_NAME (decl
);
14873 else if (TREE_CODE (decl
) != TYPE_DECL
14874 || TREE_TYPE (decl
) == error_mark_node
14875 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
)))
14876 decl
= error_mark_node
;
14878 if (decl
== error_mark_node
)
14879 cp_parser_error (parser
, "expected class-name");
14880 else if (identifier
&& !parser
->scope
)
14881 maybe_note_name_used_in_class (identifier
, decl
);
14886 /* Parse a class-specifier.
14889 class-head { member-specification [opt] }
14891 Returns the TREE_TYPE representing the class. */
14894 cp_parser_class_specifier (cp_parser
* parser
)
14898 tree attributes
= NULL_TREE
;
14899 int has_trailing_semicolon
;
14900 bool nested_name_specifier_p
;
14901 unsigned saved_num_template_parameter_lists
;
14902 bool saved_in_function_body
;
14903 bool saved_in_unbraced_linkage_specification_p
;
14904 tree old_scope
= NULL_TREE
;
14905 tree scope
= NULL_TREE
;
14908 push_deferring_access_checks (dk_no_deferred
);
14910 /* Parse the class-head. */
14911 type
= cp_parser_class_head (parser
,
14912 &nested_name_specifier_p
,
14915 /* If the class-head was a semantic disaster, skip the entire body
14919 cp_parser_skip_to_end_of_block_or_statement (parser
);
14920 pop_deferring_access_checks ();
14921 return error_mark_node
;
14924 /* Look for the `{'. */
14925 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>"))
14927 pop_deferring_access_checks ();
14928 return error_mark_node
;
14931 /* Process the base classes. If they're invalid, skip the
14932 entire class body. */
14933 if (!xref_basetypes (type
, bases
))
14935 /* Consuming the closing brace yields better error messages
14937 if (cp_parser_skip_to_closing_brace (parser
))
14938 cp_lexer_consume_token (parser
->lexer
);
14939 pop_deferring_access_checks ();
14940 return error_mark_node
;
14943 /* Issue an error message if type-definitions are forbidden here. */
14944 cp_parser_check_type_definition (parser
);
14945 /* Remember that we are defining one more class. */
14946 ++parser
->num_classes_being_defined
;
14947 /* Inside the class, surrounding template-parameter-lists do not
14949 saved_num_template_parameter_lists
14950 = parser
->num_template_parameter_lists
;
14951 parser
->num_template_parameter_lists
= 0;
14952 /* We are not in a function body. */
14953 saved_in_function_body
= parser
->in_function_body
;
14954 parser
->in_function_body
= false;
14955 /* We are not immediately inside an extern "lang" block. */
14956 saved_in_unbraced_linkage_specification_p
14957 = parser
->in_unbraced_linkage_specification_p
;
14958 parser
->in_unbraced_linkage_specification_p
= false;
14960 /* Start the class. */
14961 if (nested_name_specifier_p
)
14963 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
14964 old_scope
= push_inner_scope (scope
);
14966 type
= begin_class_definition (type
, attributes
);
14968 if (type
== error_mark_node
)
14969 /* If the type is erroneous, skip the entire body of the class. */
14970 cp_parser_skip_to_closing_brace (parser
);
14972 /* Parse the member-specification. */
14973 cp_parser_member_specification_opt (parser
);
14975 /* Look for the trailing `}'. */
14976 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
14977 /* We get better error messages by noticing a common problem: a
14978 missing trailing `;'. */
14979 token
= cp_lexer_peek_token (parser
->lexer
);
14980 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
14981 /* Look for trailing attributes to apply to this class. */
14982 if (cp_parser_allow_gnu_extensions_p (parser
))
14983 attributes
= cp_parser_attributes_opt (parser
);
14984 if (type
!= error_mark_node
)
14985 type
= finish_struct (type
, attributes
);
14986 if (nested_name_specifier_p
)
14987 pop_inner_scope (old_scope
, scope
);
14988 /* If this class is not itself within the scope of another class,
14989 then we need to parse the bodies of all of the queued function
14990 definitions. Note that the queued functions defined in a class
14991 are not always processed immediately following the
14992 class-specifier for that class. Consider:
14995 struct B { void f() { sizeof (A); } };
14998 If `f' were processed before the processing of `A' were
14999 completed, there would be no way to compute the size of `A'.
15000 Note that the nesting we are interested in here is lexical --
15001 not the semantic nesting given by TYPE_CONTEXT. In particular,
15004 struct A { struct B; };
15005 struct A::B { void f() { } };
15007 there is no need to delay the parsing of `A::B::f'. */
15008 if (--parser
->num_classes_being_defined
== 0)
15012 tree class_type
= NULL_TREE
;
15013 tree pushed_scope
= NULL_TREE
;
15015 /* In a first pass, parse default arguments to the functions.
15016 Then, in a second pass, parse the bodies of the functions.
15017 This two-phased approach handles cases like:
15025 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
15026 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
15027 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
15028 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15029 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
15031 fn
= TREE_VALUE (queue_entry
);
15032 /* If there are default arguments that have not yet been processed,
15033 take care of them now. */
15034 if (class_type
!= TREE_PURPOSE (queue_entry
))
15037 pop_scope (pushed_scope
);
15038 class_type
= TREE_PURPOSE (queue_entry
);
15039 pushed_scope
= push_scope (class_type
);
15041 /* Make sure that any template parameters are in scope. */
15042 maybe_begin_member_template_processing (fn
);
15043 /* Parse the default argument expressions. */
15044 cp_parser_late_parsing_default_args (parser
, fn
);
15045 /* Remove any template parameters from the symbol table. */
15046 maybe_end_member_template_processing ();
15049 pop_scope (pushed_scope
);
15050 /* Now parse the body of the functions. */
15051 for (TREE_VALUE (parser
->unparsed_functions_queues
)
15052 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
15053 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
15054 TREE_VALUE (parser
->unparsed_functions_queues
)
15055 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
15057 /* Figure out which function we need to process. */
15058 fn
= TREE_VALUE (queue_entry
);
15059 /* Parse the function. */
15060 cp_parser_late_parsing_for_member (parser
, fn
);
15064 /* Put back any saved access checks. */
15065 pop_deferring_access_checks ();
15067 /* Restore saved state. */
15068 parser
->in_function_body
= saved_in_function_body
;
15069 parser
->num_template_parameter_lists
15070 = saved_num_template_parameter_lists
;
15071 parser
->in_unbraced_linkage_specification_p
15072 = saved_in_unbraced_linkage_specification_p
;
15077 /* Parse a class-head.
15080 class-key identifier [opt] base-clause [opt]
15081 class-key nested-name-specifier identifier base-clause [opt]
15082 class-key nested-name-specifier [opt] template-id
15086 class-key attributes identifier [opt] base-clause [opt]
15087 class-key attributes nested-name-specifier identifier base-clause [opt]
15088 class-key attributes nested-name-specifier [opt] template-id
15091 Upon return BASES is initialized to the list of base classes (or
15092 NULL, if there are none) in the same form returned by
15093 cp_parser_base_clause.
15095 Returns the TYPE of the indicated class. Sets
15096 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15097 involving a nested-name-specifier was used, and FALSE otherwise.
15099 Returns error_mark_node if this is not a class-head.
15101 Returns NULL_TREE if the class-head is syntactically valid, but
15102 semantically invalid in a way that means we should skip the entire
15103 body of the class. */
15106 cp_parser_class_head (cp_parser
* parser
,
15107 bool* nested_name_specifier_p
,
15108 tree
*attributes_p
,
15111 tree nested_name_specifier
;
15112 enum tag_types class_key
;
15113 tree id
= NULL_TREE
;
15114 tree type
= NULL_TREE
;
15116 bool template_id_p
= false;
15117 bool qualified_p
= false;
15118 bool invalid_nested_name_p
= false;
15119 bool invalid_explicit_specialization_p
= false;
15120 tree pushed_scope
= NULL_TREE
;
15121 unsigned num_templates
;
15122 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
15123 /* Assume no nested-name-specifier will be present. */
15124 *nested_name_specifier_p
= false;
15125 /* Assume no template parameter lists will be used in defining the
15129 *bases
= NULL_TREE
;
15131 /* Look for the class-key. */
15132 class_key
= cp_parser_class_key (parser
);
15133 if (class_key
== none_type
)
15134 return error_mark_node
;
15136 /* Parse the attributes. */
15137 attributes
= cp_parser_attributes_opt (parser
);
15139 /* If the next token is `::', that is invalid -- but sometimes
15140 people do try to write:
15144 Handle this gracefully by accepting the extra qualifier, and then
15145 issuing an error about it later if this really is a
15146 class-head. If it turns out just to be an elaborated type
15147 specifier, remain silent. */
15148 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
15149 qualified_p
= true;
15151 push_deferring_access_checks (dk_no_check
);
15153 /* Determine the name of the class. Begin by looking for an
15154 optional nested-name-specifier. */
15155 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
15156 nested_name_specifier
15157 = cp_parser_nested_name_specifier_opt (parser
,
15158 /*typename_keyword_p=*/false,
15159 /*check_dependency_p=*/false,
15161 /*is_declaration=*/false);
15162 /* If there was a nested-name-specifier, then there *must* be an
15164 if (nested_name_specifier
)
15166 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15167 /* Although the grammar says `identifier', it really means
15168 `class-name' or `template-name'. You are only allowed to
15169 define a class that has already been declared with this
15172 The proposed resolution for Core Issue 180 says that wherever
15173 you see `class T::X' you should treat `X' as a type-name.
15175 It is OK to define an inaccessible class; for example:
15177 class A { class B; };
15180 We do not know if we will see a class-name, or a
15181 template-name. We look for a class-name first, in case the
15182 class-name is a template-id; if we looked for the
15183 template-name first we would stop after the template-name. */
15184 cp_parser_parse_tentatively (parser
);
15185 type
= cp_parser_class_name (parser
,
15186 /*typename_keyword_p=*/false,
15187 /*template_keyword_p=*/false,
15189 /*check_dependency_p=*/false,
15190 /*class_head_p=*/true,
15191 /*is_declaration=*/false);
15192 /* If that didn't work, ignore the nested-name-specifier. */
15193 if (!cp_parser_parse_definitely (parser
))
15195 invalid_nested_name_p
= true;
15196 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15197 id
= cp_parser_identifier (parser
);
15198 if (id
== error_mark_node
)
15201 /* If we could not find a corresponding TYPE, treat this
15202 declaration like an unqualified declaration. */
15203 if (type
== error_mark_node
)
15204 nested_name_specifier
= NULL_TREE
;
15205 /* Otherwise, count the number of templates used in TYPE and its
15206 containing scopes. */
15211 for (scope
= TREE_TYPE (type
);
15212 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
15213 scope
= (TYPE_P (scope
)
15214 ? TYPE_CONTEXT (scope
)
15215 : DECL_CONTEXT (scope
)))
15217 && CLASS_TYPE_P (scope
)
15218 && CLASSTYPE_TEMPLATE_INFO (scope
)
15219 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
15220 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
15224 /* Otherwise, the identifier is optional. */
15227 /* We don't know whether what comes next is a template-id,
15228 an identifier, or nothing at all. */
15229 cp_parser_parse_tentatively (parser
);
15230 /* Check for a template-id. */
15231 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15232 id
= cp_parser_template_id (parser
,
15233 /*template_keyword_p=*/false,
15234 /*check_dependency_p=*/true,
15235 /*is_declaration=*/true);
15236 /* If that didn't work, it could still be an identifier. */
15237 if (!cp_parser_parse_definitely (parser
))
15239 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15241 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15242 id
= cp_parser_identifier (parser
);
15249 template_id_p
= true;
15254 pop_deferring_access_checks ();
15257 cp_parser_check_for_invalid_template_id (parser
, id
,
15258 type_start_token
->location
);
15260 /* If it's not a `:' or a `{' then we can't really be looking at a
15261 class-head, since a class-head only appears as part of a
15262 class-specifier. We have to detect this situation before calling
15263 xref_tag, since that has irreversible side-effects. */
15264 if (!cp_parser_next_token_starts_class_definition_p (parser
))
15266 cp_parser_error (parser
, "expected %<{%> or %<:%>");
15267 return error_mark_node
;
15270 /* At this point, we're going ahead with the class-specifier, even
15271 if some other problem occurs. */
15272 cp_parser_commit_to_tentative_parse (parser
);
15273 /* Issue the error about the overly-qualified name now. */
15276 cp_parser_error (parser
,
15277 "global qualification of class name is invalid");
15278 return error_mark_node
;
15280 else if (invalid_nested_name_p
)
15282 cp_parser_error (parser
,
15283 "qualified name does not name a class");
15284 return error_mark_node
;
15286 else if (nested_name_specifier
)
15290 /* Reject typedef-names in class heads. */
15291 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
15293 error ("%Hinvalid class name in declaration of %qD",
15294 &type_start_token
->location
, type
);
15299 /* Figure out in what scope the declaration is being placed. */
15300 scope
= current_scope ();
15301 /* If that scope does not contain the scope in which the
15302 class was originally declared, the program is invalid. */
15303 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
15305 if (at_namespace_scope_p ())
15306 error ("%Hdeclaration of %qD in namespace %qD which does not "
15308 &type_start_token
->location
,
15309 type
, scope
, nested_name_specifier
);
15311 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15312 &type_start_token
->location
,
15313 type
, scope
, nested_name_specifier
);
15319 A declarator-id shall not be qualified except for the
15320 definition of a ... nested class outside of its class
15321 ... [or] the definition or explicit instantiation of a
15322 class member of a namespace outside of its namespace. */
15323 if (scope
== nested_name_specifier
)
15325 permerror (input_location
, "%Hextra qualification not allowed",
15326 &nested_name_specifier_token_start
->location
);
15327 nested_name_specifier
= NULL_TREE
;
15331 /* An explicit-specialization must be preceded by "template <>". If
15332 it is not, try to recover gracefully. */
15333 if (at_namespace_scope_p ()
15334 && parser
->num_template_parameter_lists
== 0
15337 error ("%Han explicit specialization must be preceded by %<template <>%>",
15338 &type_start_token
->location
);
15339 invalid_explicit_specialization_p
= true;
15340 /* Take the same action that would have been taken by
15341 cp_parser_explicit_specialization. */
15342 ++parser
->num_template_parameter_lists
;
15343 begin_specialization ();
15345 /* There must be no "return" statements between this point and the
15346 end of this function; set "type "to the correct return value and
15347 use "goto done;" to return. */
15348 /* Make sure that the right number of template parameters were
15350 if (!cp_parser_check_template_parameters (parser
, num_templates
,
15351 type_start_token
->location
))
15353 /* If something went wrong, there is no point in even trying to
15354 process the class-definition. */
15359 /* Look up the type. */
15362 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
15363 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
15364 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
15366 error ("%Hfunction template %qD redeclared as a class template",
15367 &type_start_token
->location
, id
);
15368 type
= error_mark_node
;
15372 type
= TREE_TYPE (id
);
15373 type
= maybe_process_partial_specialization (type
);
15375 if (nested_name_specifier
)
15376 pushed_scope
= push_scope (nested_name_specifier
);
15378 else if (nested_name_specifier
)
15384 template <typename T> struct S { struct T };
15385 template <typename T> struct S<T>::T { };
15387 we will get a TYPENAME_TYPE when processing the definition of
15388 `S::T'. We need to resolve it to the actual type before we
15389 try to define it. */
15390 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
15392 class_type
= resolve_typename_type (TREE_TYPE (type
),
15393 /*only_current_p=*/false);
15394 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
15395 type
= TYPE_NAME (class_type
);
15398 cp_parser_error (parser
, "could not resolve typename type");
15399 type
= error_mark_node
;
15403 if (maybe_process_partial_specialization (TREE_TYPE (type
))
15404 == error_mark_node
)
15410 class_type
= current_class_type
;
15411 /* Enter the scope indicated by the nested-name-specifier. */
15412 pushed_scope
= push_scope (nested_name_specifier
);
15413 /* Get the canonical version of this type. */
15414 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
15415 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15416 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
15418 type
= push_template_decl (type
);
15419 if (type
== error_mark_node
)
15426 type
= TREE_TYPE (type
);
15427 *nested_name_specifier_p
= true;
15429 else /* The name is not a nested name. */
15431 /* If the class was unnamed, create a dummy name. */
15433 id
= make_anon_name ();
15434 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
15435 parser
->num_template_parameter_lists
);
15438 /* Indicate whether this class was declared as a `class' or as a
15440 if (TREE_CODE (type
) == RECORD_TYPE
)
15441 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
15442 cp_parser_check_class_key (class_key
, type
);
15444 /* If this type was already complete, and we see another definition,
15445 that's an error. */
15446 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
15448 error ("%Hredefinition of %q#T",
15449 &type_start_token
->location
, type
);
15450 error ("%Hprevious definition of %q+#T",
15451 &type_start_token
->location
, type
);
15455 else if (type
== error_mark_node
)
15458 /* We will have entered the scope containing the class; the names of
15459 base classes should be looked up in that context. For example:
15461 struct A { struct B {}; struct C; };
15462 struct A::C : B {};
15466 /* Get the list of base-classes, if there is one. */
15467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15468 *bases
= cp_parser_base_clause (parser
);
15471 /* Leave the scope given by the nested-name-specifier. We will
15472 enter the class scope itself while processing the members. */
15474 pop_scope (pushed_scope
);
15476 if (invalid_explicit_specialization_p
)
15478 end_specialization ();
15479 --parser
->num_template_parameter_lists
;
15481 *attributes_p
= attributes
;
15485 /* Parse a class-key.
15492 Returns the kind of class-key specified, or none_type to indicate
15495 static enum tag_types
15496 cp_parser_class_key (cp_parser
* parser
)
15499 enum tag_types tag_type
;
15501 /* Look for the class-key. */
15502 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
15506 /* Check to see if the TOKEN is a class-key. */
15507 tag_type
= cp_parser_token_is_class_key (token
);
15509 cp_parser_error (parser
, "expected class-key");
15513 /* Parse an (optional) member-specification.
15515 member-specification:
15516 member-declaration member-specification [opt]
15517 access-specifier : member-specification [opt] */
15520 cp_parser_member_specification_opt (cp_parser
* parser
)
15527 /* Peek at the next token. */
15528 token
= cp_lexer_peek_token (parser
->lexer
);
15529 /* If it's a `}', or EOF then we've seen all the members. */
15530 if (token
->type
== CPP_CLOSE_BRACE
15531 || token
->type
== CPP_EOF
15532 || token
->type
== CPP_PRAGMA_EOL
)
15535 /* See if this token is a keyword. */
15536 keyword
= token
->keyword
;
15540 case RID_PROTECTED
:
15542 /* Consume the access-specifier. */
15543 cp_lexer_consume_token (parser
->lexer
);
15544 /* Remember which access-specifier is active. */
15545 current_access_specifier
= token
->u
.value
;
15546 /* Look for the `:'. */
15547 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
15551 /* Accept #pragmas at class scope. */
15552 if (token
->type
== CPP_PRAGMA
)
15554 cp_parser_pragma (parser
, pragma_external
);
15558 /* Otherwise, the next construction must be a
15559 member-declaration. */
15560 cp_parser_member_declaration (parser
);
15565 /* Parse a member-declaration.
15567 member-declaration:
15568 decl-specifier-seq [opt] member-declarator-list [opt] ;
15569 function-definition ; [opt]
15570 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15572 template-declaration
15574 member-declarator-list:
15576 member-declarator-list , member-declarator
15579 declarator pure-specifier [opt]
15580 declarator constant-initializer [opt]
15581 identifier [opt] : constant-expression
15585 member-declaration:
15586 __extension__ member-declaration
15589 declarator attributes [opt] pure-specifier [opt]
15590 declarator attributes [opt] constant-initializer [opt]
15591 identifier [opt] attributes [opt] : constant-expression
15595 member-declaration:
15596 static_assert-declaration */
15599 cp_parser_member_declaration (cp_parser
* parser
)
15601 cp_decl_specifier_seq decl_specifiers
;
15602 tree prefix_attributes
;
15604 int declares_class_or_enum
;
15606 cp_token
*token
= NULL
;
15607 cp_token
*decl_spec_token_start
= NULL
;
15608 cp_token
*initializer_token_start
= NULL
;
15609 int saved_pedantic
;
15611 /* Check for the `__extension__' keyword. */
15612 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
15615 cp_parser_member_declaration (parser
);
15616 /* Restore the old value of the PEDANTIC flag. */
15617 pedantic
= saved_pedantic
;
15622 /* Check for a template-declaration. */
15623 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15625 /* An explicit specialization here is an error condition, and we
15626 expect the specialization handler to detect and report this. */
15627 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
15628 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
15629 cp_parser_explicit_specialization (parser
);
15631 cp_parser_template_declaration (parser
, /*member_p=*/true);
15636 /* Check for a using-declaration. */
15637 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
15639 /* Parse the using-declaration. */
15640 cp_parser_using_declaration (parser
,
15641 /*access_declaration_p=*/false);
15645 /* Check for @defs. */
15646 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
15649 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
15650 ivar
= ivar_chains
;
15654 ivar
= TREE_CHAIN (member
);
15655 TREE_CHAIN (member
) = NULL_TREE
;
15656 finish_member_declaration (member
);
15661 /* If the next token is `static_assert' we have a static assertion. */
15662 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
15664 cp_parser_static_assert (parser
, /*member_p=*/true);
15668 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
15671 /* Parse the decl-specifier-seq. */
15672 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
15673 cp_parser_decl_specifier_seq (parser
,
15674 CP_PARSER_FLAGS_OPTIONAL
,
15676 &declares_class_or_enum
);
15677 prefix_attributes
= decl_specifiers
.attributes
;
15678 decl_specifiers
.attributes
= NULL_TREE
;
15679 /* Check for an invalid type-name. */
15680 if (!decl_specifiers
.type
15681 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
15683 /* If there is no declarator, then the decl-specifier-seq should
15685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15687 /* If there was no decl-specifier-seq, and the next token is a
15688 `;', then we have something like:
15694 Each member-declaration shall declare at least one member
15695 name of the class. */
15696 if (!decl_specifiers
.any_specifiers_p
)
15698 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15699 if (!in_system_header_at (token
->location
))
15700 pedwarn (token
->location
, OPT_pedantic
, "extra %<;%>");
15706 /* See if this declaration is a friend. */
15707 friend_p
= cp_parser_friend_p (&decl_specifiers
);
15708 /* If there were decl-specifiers, check to see if there was
15709 a class-declaration. */
15710 type
= check_tag_decl (&decl_specifiers
);
15711 /* Nested classes have already been added to the class, but
15712 a `friend' needs to be explicitly registered. */
15715 /* If the `friend' keyword was present, the friend must
15716 be introduced with a class-key. */
15717 if (!declares_class_or_enum
)
15718 error ("%Ha class-key must be used when declaring a friend",
15719 &decl_spec_token_start
->location
);
15722 template <typename T> struct A {
15723 friend struct A<T>::B;
15726 A<T>::B will be represented by a TYPENAME_TYPE, and
15727 therefore not recognized by check_tag_decl. */
15729 && decl_specifiers
.type
15730 && TYPE_P (decl_specifiers
.type
))
15731 type
= decl_specifiers
.type
;
15732 if (!type
|| !TYPE_P (type
))
15733 error ("%Hfriend declaration does not name a class or "
15734 "function", &decl_spec_token_start
->location
);
15736 make_friend_class (current_class_type
, type
,
15737 /*complain=*/true);
15739 /* If there is no TYPE, an error message will already have
15741 else if (!type
|| type
== error_mark_node
)
15743 /* An anonymous aggregate has to be handled specially; such
15744 a declaration really declares a data member (with a
15745 particular type), as opposed to a nested class. */
15746 else if (ANON_AGGR_TYPE_P (type
))
15748 /* Remove constructors and such from TYPE, now that we
15749 know it is an anonymous aggregate. */
15750 fixup_anonymous_aggr (type
);
15751 /* And make the corresponding data member. */
15752 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
15753 /* Add it to the class. */
15754 finish_member_declaration (decl
);
15757 cp_parser_check_access_in_redeclaration
15759 decl_spec_token_start
->location
);
15764 /* See if these declarations will be friends. */
15765 friend_p
= cp_parser_friend_p (&decl_specifiers
);
15767 /* Keep going until we hit the `;' at the end of the
15769 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15771 tree attributes
= NULL_TREE
;
15772 tree first_attribute
;
15774 /* Peek at the next token. */
15775 token
= cp_lexer_peek_token (parser
->lexer
);
15777 /* Check for a bitfield declaration. */
15778 if (token
->type
== CPP_COLON
15779 || (token
->type
== CPP_NAME
15780 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15786 /* Get the name of the bitfield. Note that we cannot just
15787 check TOKEN here because it may have been invalidated by
15788 the call to cp_lexer_peek_nth_token above. */
15789 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
15790 identifier
= cp_parser_identifier (parser
);
15792 identifier
= NULL_TREE
;
15794 /* Consume the `:' token. */
15795 cp_lexer_consume_token (parser
->lexer
);
15796 /* Get the width of the bitfield. */
15798 = cp_parser_constant_expression (parser
,
15799 /*allow_non_constant=*/false,
15802 /* Look for attributes that apply to the bitfield. */
15803 attributes
= cp_parser_attributes_opt (parser
);
15804 /* Remember which attributes are prefix attributes and
15806 first_attribute
= attributes
;
15807 /* Combine the attributes. */
15808 attributes
= chainon (prefix_attributes
, attributes
);
15810 /* Create the bitfield declaration. */
15811 decl
= grokbitfield (identifier
15812 ? make_id_declarator (NULL_TREE
,
15822 cp_declarator
*declarator
;
15824 tree asm_specification
;
15825 int ctor_dtor_or_conv_p
;
15827 /* Parse the declarator. */
15829 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
15830 &ctor_dtor_or_conv_p
,
15831 /*parenthesized_p=*/NULL
,
15832 /*member_p=*/true);
15834 /* If something went wrong parsing the declarator, make sure
15835 that we at least consume some tokens. */
15836 if (declarator
== cp_error_declarator
)
15838 /* Skip to the end of the statement. */
15839 cp_parser_skip_to_end_of_statement (parser
);
15840 /* If the next token is not a semicolon, that is
15841 probably because we just skipped over the body of
15842 a function. So, we consume a semicolon if
15843 present, but do not issue an error message if it
15845 if (cp_lexer_next_token_is (parser
->lexer
,
15847 cp_lexer_consume_token (parser
->lexer
);
15851 if (declares_class_or_enum
& 2)
15852 cp_parser_check_for_definition_in_return_type
15853 (declarator
, decl_specifiers
.type
,
15854 decl_specifiers
.type_location
);
15856 /* Look for an asm-specification. */
15857 asm_specification
= cp_parser_asm_specification_opt (parser
);
15858 /* Look for attributes that apply to the declaration. */
15859 attributes
= cp_parser_attributes_opt (parser
);
15860 /* Remember which attributes are prefix attributes and
15862 first_attribute
= attributes
;
15863 /* Combine the attributes. */
15864 attributes
= chainon (prefix_attributes
, attributes
);
15866 /* If it's an `=', then we have a constant-initializer or a
15867 pure-specifier. It is not correct to parse the
15868 initializer before registering the member declaration
15869 since the member declaration should be in scope while
15870 its initializer is processed. However, the rest of the
15871 front end does not yet provide an interface that allows
15872 us to handle this correctly. */
15873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15877 A pure-specifier shall be used only in the declaration of
15878 a virtual function.
15880 A member-declarator can contain a constant-initializer
15881 only if it declares a static member of integral or
15884 Therefore, if the DECLARATOR is for a function, we look
15885 for a pure-specifier; otherwise, we look for a
15886 constant-initializer. When we call `grokfield', it will
15887 perform more stringent semantics checks. */
15888 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
15889 if (function_declarator_p (declarator
))
15890 initializer
= cp_parser_pure_specifier (parser
);
15892 /* Parse the initializer. */
15893 initializer
= cp_parser_constant_initializer (parser
);
15895 /* Otherwise, there is no initializer. */
15897 initializer
= NULL_TREE
;
15899 /* See if we are probably looking at a function
15900 definition. We are certainly not looking at a
15901 member-declarator. Calling `grokfield' has
15902 side-effects, so we must not do it unless we are sure
15903 that we are looking at a member-declarator. */
15904 if (cp_parser_token_starts_function_definition_p
15905 (cp_lexer_peek_token (parser
->lexer
)))
15907 /* The grammar does not allow a pure-specifier to be
15908 used when a member function is defined. (It is
15909 possible that this fact is an oversight in the
15910 standard, since a pure function may be defined
15911 outside of the class-specifier. */
15913 error ("%Hpure-specifier on function-definition",
15914 &initializer_token_start
->location
);
15915 decl
= cp_parser_save_member_function_body (parser
,
15919 /* If the member was not a friend, declare it here. */
15921 finish_member_declaration (decl
);
15922 /* Peek at the next token. */
15923 token
= cp_lexer_peek_token (parser
->lexer
);
15924 /* If the next token is a semicolon, consume it. */
15925 if (token
->type
== CPP_SEMICOLON
)
15926 cp_lexer_consume_token (parser
->lexer
);
15930 if (declarator
->kind
== cdk_function
)
15931 declarator
->id_loc
= token
->location
;
15932 /* Create the declaration. */
15933 decl
= grokfield (declarator
, &decl_specifiers
,
15934 initializer
, /*init_const_expr_p=*/true,
15939 /* Reset PREFIX_ATTRIBUTES. */
15940 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
15941 attributes
= TREE_CHAIN (attributes
);
15943 TREE_CHAIN (attributes
) = NULL_TREE
;
15945 /* If there is any qualification still in effect, clear it
15946 now; we will be starting fresh with the next declarator. */
15947 parser
->scope
= NULL_TREE
;
15948 parser
->qualifying_scope
= NULL_TREE
;
15949 parser
->object_scope
= NULL_TREE
;
15950 /* If it's a `,', then there are more declarators. */
15951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15952 cp_lexer_consume_token (parser
->lexer
);
15953 /* If the next token isn't a `;', then we have a parse error. */
15954 else if (cp_lexer_next_token_is_not (parser
->lexer
,
15957 cp_parser_error (parser
, "expected %<;%>");
15958 /* Skip tokens until we find a `;'. */
15959 cp_parser_skip_to_end_of_statement (parser
);
15966 /* Add DECL to the list of members. */
15968 finish_member_declaration (decl
);
15970 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15971 cp_parser_save_default_args (parser
, decl
);
15976 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
15979 /* Parse a pure-specifier.
15984 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15985 Otherwise, ERROR_MARK_NODE is returned. */
15988 cp_parser_pure_specifier (cp_parser
* parser
)
15992 /* Look for the `=' token. */
15993 if (!cp_parser_require (parser
, CPP_EQ
, "%<=%>"))
15994 return error_mark_node
;
15995 /* Look for the `0' token. */
15996 token
= cp_lexer_consume_token (parser
->lexer
);
15998 /* Accept = default or = delete in c++0x mode. */
15999 if (token
->keyword
== RID_DEFAULT
16000 || token
->keyword
== RID_DELETE
)
16002 maybe_warn_cpp0x ("defaulted and deleted functions");
16003 return token
->u
.value
;
16006 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16007 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
16009 cp_parser_error (parser
,
16010 "invalid pure specifier (only %<= 0%> is allowed)");
16011 cp_parser_skip_to_end_of_statement (parser
);
16012 return error_mark_node
;
16014 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16016 error ("%Htemplates may not be %<virtual%>", &token
->location
);
16017 return error_mark_node
;
16020 return integer_zero_node
;
16023 /* Parse a constant-initializer.
16025 constant-initializer:
16026 = constant-expression
16028 Returns a representation of the constant-expression. */
16031 cp_parser_constant_initializer (cp_parser
* parser
)
16033 /* Look for the `=' token. */
16034 if (!cp_parser_require (parser
, CPP_EQ
, "%<=%>"))
16035 return error_mark_node
;
16037 /* It is invalid to write:
16039 struct S { static const int i = { 7 }; };
16042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16044 cp_parser_error (parser
,
16045 "a brace-enclosed initializer is not allowed here");
16046 /* Consume the opening brace. */
16047 cp_lexer_consume_token (parser
->lexer
);
16048 /* Skip the initializer. */
16049 cp_parser_skip_to_closing_brace (parser
);
16050 /* Look for the trailing `}'. */
16051 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
16053 return error_mark_node
;
16056 return cp_parser_constant_expression (parser
,
16057 /*allow_non_constant=*/false,
16061 /* Derived classes [gram.class.derived] */
16063 /* Parse a base-clause.
16066 : base-specifier-list
16068 base-specifier-list:
16069 base-specifier ... [opt]
16070 base-specifier-list , base-specifier ... [opt]
16072 Returns a TREE_LIST representing the base-classes, in the order in
16073 which they were declared. The representation of each node is as
16074 described by cp_parser_base_specifier.
16076 In the case that no bases are specified, this function will return
16077 NULL_TREE, not ERROR_MARK_NODE. */
16080 cp_parser_base_clause (cp_parser
* parser
)
16082 tree bases
= NULL_TREE
;
16084 /* Look for the `:' that begins the list. */
16085 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
16087 /* Scan the base-specifier-list. */
16092 bool pack_expansion_p
= false;
16094 /* Look for the base-specifier. */
16095 base
= cp_parser_base_specifier (parser
);
16096 /* Look for the (optional) ellipsis. */
16097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16099 /* Consume the `...'. */
16100 cp_lexer_consume_token (parser
->lexer
);
16102 pack_expansion_p
= true;
16105 /* Add BASE to the front of the list. */
16106 if (base
!= error_mark_node
)
16108 if (pack_expansion_p
)
16109 /* Make this a pack expansion type. */
16110 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
16113 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
16115 TREE_CHAIN (base
) = bases
;
16119 /* Peek at the next token. */
16120 token
= cp_lexer_peek_token (parser
->lexer
);
16121 /* If it's not a comma, then the list is complete. */
16122 if (token
->type
!= CPP_COMMA
)
16124 /* Consume the `,'. */
16125 cp_lexer_consume_token (parser
->lexer
);
16128 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16129 base class had a qualified name. However, the next name that
16130 appears is certainly not qualified. */
16131 parser
->scope
= NULL_TREE
;
16132 parser
->qualifying_scope
= NULL_TREE
;
16133 parser
->object_scope
= NULL_TREE
;
16135 return nreverse (bases
);
16138 /* Parse a base-specifier.
16141 :: [opt] nested-name-specifier [opt] class-name
16142 virtual access-specifier [opt] :: [opt] nested-name-specifier
16144 access-specifier virtual [opt] :: [opt] nested-name-specifier
16147 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16148 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16149 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16150 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16153 cp_parser_base_specifier (cp_parser
* parser
)
16157 bool virtual_p
= false;
16158 bool duplicate_virtual_error_issued_p
= false;
16159 bool duplicate_access_error_issued_p
= false;
16160 bool class_scope_p
, template_p
;
16161 tree access
= access_default_node
;
16164 /* Process the optional `virtual' and `access-specifier'. */
16167 /* Peek at the next token. */
16168 token
= cp_lexer_peek_token (parser
->lexer
);
16169 /* Process `virtual'. */
16170 switch (token
->keyword
)
16173 /* If `virtual' appears more than once, issue an error. */
16174 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
16176 cp_parser_error (parser
,
16177 "%<virtual%> specified more than once in base-specified");
16178 duplicate_virtual_error_issued_p
= true;
16183 /* Consume the `virtual' token. */
16184 cp_lexer_consume_token (parser
->lexer
);
16189 case RID_PROTECTED
:
16191 /* If more than one access specifier appears, issue an
16193 if (access
!= access_default_node
16194 && !duplicate_access_error_issued_p
)
16196 cp_parser_error (parser
,
16197 "more than one access specifier in base-specified");
16198 duplicate_access_error_issued_p
= true;
16201 access
= ridpointers
[(int) token
->keyword
];
16203 /* Consume the access-specifier. */
16204 cp_lexer_consume_token (parser
->lexer
);
16213 /* It is not uncommon to see programs mechanically, erroneously, use
16214 the 'typename' keyword to denote (dependent) qualified types
16215 as base classes. */
16216 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
16218 token
= cp_lexer_peek_token (parser
->lexer
);
16219 if (!processing_template_decl
)
16220 error ("%Hkeyword %<typename%> not allowed outside of templates",
16223 error ("%Hkeyword %<typename%> not allowed in this context "
16224 "(the base class is implicitly a type)",
16226 cp_lexer_consume_token (parser
->lexer
);
16229 /* Look for the optional `::' operator. */
16230 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16231 /* Look for the nested-name-specifier. The simplest way to
16236 The keyword `typename' is not permitted in a base-specifier or
16237 mem-initializer; in these contexts a qualified name that
16238 depends on a template-parameter is implicitly assumed to be a
16241 is to pretend that we have seen the `typename' keyword at this
16243 cp_parser_nested_name_specifier_opt (parser
,
16244 /*typename_keyword_p=*/true,
16245 /*check_dependency_p=*/true,
16247 /*is_declaration=*/true);
16248 /* If the base class is given by a qualified name, assume that names
16249 we see are type names or templates, as appropriate. */
16250 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
16251 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
16253 /* Finally, look for the class-name. */
16254 type
= cp_parser_class_name (parser
,
16258 /*check_dependency_p=*/true,
16259 /*class_head_p=*/false,
16260 /*is_declaration=*/true);
16262 if (type
== error_mark_node
)
16263 return error_mark_node
;
16265 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
16268 /* Exception handling [gram.exception] */
16270 /* Parse an (optional) exception-specification.
16272 exception-specification:
16273 throw ( type-id-list [opt] )
16275 Returns a TREE_LIST representing the exception-specification. The
16276 TREE_VALUE of each node is a type. */
16279 cp_parser_exception_specification_opt (cp_parser
* parser
)
16284 /* Peek at the next token. */
16285 token
= cp_lexer_peek_token (parser
->lexer
);
16286 /* If it's not `throw', then there's no exception-specification. */
16287 if (!cp_parser_is_keyword (token
, RID_THROW
))
16290 /* Consume the `throw'. */
16291 cp_lexer_consume_token (parser
->lexer
);
16293 /* Look for the `('. */
16294 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
16296 /* Peek at the next token. */
16297 token
= cp_lexer_peek_token (parser
->lexer
);
16298 /* If it's not a `)', then there is a type-id-list. */
16299 if (token
->type
!= CPP_CLOSE_PAREN
)
16301 const char *saved_message
;
16303 /* Types may not be defined in an exception-specification. */
16304 saved_message
= parser
->type_definition_forbidden_message
;
16305 parser
->type_definition_forbidden_message
16306 = "types may not be defined in an exception-specification";
16307 /* Parse the type-id-list. */
16308 type_id_list
= cp_parser_type_id_list (parser
);
16309 /* Restore the saved message. */
16310 parser
->type_definition_forbidden_message
= saved_message
;
16313 type_id_list
= empty_except_spec
;
16315 /* Look for the `)'. */
16316 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16318 return type_id_list
;
16321 /* Parse an (optional) type-id-list.
16325 type-id-list , type-id ... [opt]
16327 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16328 in the order that the types were presented. */
16331 cp_parser_type_id_list (cp_parser
* parser
)
16333 tree types
= NULL_TREE
;
16340 /* Get the next type-id. */
16341 type
= cp_parser_type_id (parser
);
16342 /* Parse the optional ellipsis. */
16343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16345 /* Consume the `...'. */
16346 cp_lexer_consume_token (parser
->lexer
);
16348 /* Turn the type into a pack expansion expression. */
16349 type
= make_pack_expansion (type
);
16351 /* Add it to the list. */
16352 types
= add_exception_specifier (types
, type
, /*complain=*/1);
16353 /* Peek at the next token. */
16354 token
= cp_lexer_peek_token (parser
->lexer
);
16355 /* If it is not a `,', we are done. */
16356 if (token
->type
!= CPP_COMMA
)
16358 /* Consume the `,'. */
16359 cp_lexer_consume_token (parser
->lexer
);
16362 return nreverse (types
);
16365 /* Parse a try-block.
16368 try compound-statement handler-seq */
16371 cp_parser_try_block (cp_parser
* parser
)
16375 cp_parser_require_keyword (parser
, RID_TRY
, "%<try%>");
16376 try_block
= begin_try_block ();
16377 cp_parser_compound_statement (parser
, NULL
, true);
16378 finish_try_block (try_block
);
16379 cp_parser_handler_seq (parser
);
16380 finish_handler_sequence (try_block
);
16385 /* Parse a function-try-block.
16387 function-try-block:
16388 try ctor-initializer [opt] function-body handler-seq */
16391 cp_parser_function_try_block (cp_parser
* parser
)
16393 tree compound_stmt
;
16395 bool ctor_initializer_p
;
16397 /* Look for the `try' keyword. */
16398 if (!cp_parser_require_keyword (parser
, RID_TRY
, "%<try%>"))
16400 /* Let the rest of the front end know where we are. */
16401 try_block
= begin_function_try_block (&compound_stmt
);
16402 /* Parse the function-body. */
16404 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
16405 /* We're done with the `try' part. */
16406 finish_function_try_block (try_block
);
16407 /* Parse the handlers. */
16408 cp_parser_handler_seq (parser
);
16409 /* We're done with the handlers. */
16410 finish_function_handler_sequence (try_block
, compound_stmt
);
16412 return ctor_initializer_p
;
16415 /* Parse a handler-seq.
16418 handler handler-seq [opt] */
16421 cp_parser_handler_seq (cp_parser
* parser
)
16427 /* Parse the handler. */
16428 cp_parser_handler (parser
);
16429 /* Peek at the next token. */
16430 token
= cp_lexer_peek_token (parser
->lexer
);
16431 /* If it's not `catch' then there are no more handlers. */
16432 if (!cp_parser_is_keyword (token
, RID_CATCH
))
16437 /* Parse a handler.
16440 catch ( exception-declaration ) compound-statement */
16443 cp_parser_handler (cp_parser
* parser
)
16448 cp_parser_require_keyword (parser
, RID_CATCH
, "%<catch%>");
16449 handler
= begin_handler ();
16450 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
16451 declaration
= cp_parser_exception_declaration (parser
);
16452 finish_handler_parms (declaration
, handler
);
16453 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16454 cp_parser_compound_statement (parser
, NULL
, false);
16455 finish_handler (handler
);
16458 /* Parse an exception-declaration.
16460 exception-declaration:
16461 type-specifier-seq declarator
16462 type-specifier-seq abstract-declarator
16466 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16467 ellipsis variant is used. */
16470 cp_parser_exception_declaration (cp_parser
* parser
)
16472 cp_decl_specifier_seq type_specifiers
;
16473 cp_declarator
*declarator
;
16474 const char *saved_message
;
16476 /* If it's an ellipsis, it's easy to handle. */
16477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16479 /* Consume the `...' token. */
16480 cp_lexer_consume_token (parser
->lexer
);
16484 /* Types may not be defined in exception-declarations. */
16485 saved_message
= parser
->type_definition_forbidden_message
;
16486 parser
->type_definition_forbidden_message
16487 = "types may not be defined in exception-declarations";
16489 /* Parse the type-specifier-seq. */
16490 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
16492 /* If it's a `)', then there is no declarator. */
16493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
16496 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
16497 /*ctor_dtor_or_conv_p=*/NULL
,
16498 /*parenthesized_p=*/NULL
,
16499 /*member_p=*/false);
16501 /* Restore the saved message. */
16502 parser
->type_definition_forbidden_message
= saved_message
;
16504 if (!type_specifiers
.any_specifiers_p
)
16505 return error_mark_node
;
16507 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
16510 /* Parse a throw-expression.
16513 throw assignment-expression [opt]
16515 Returns a THROW_EXPR representing the throw-expression. */
16518 cp_parser_throw_expression (cp_parser
* parser
)
16523 cp_parser_require_keyword (parser
, RID_THROW
, "%<throw%>");
16524 token
= cp_lexer_peek_token (parser
->lexer
);
16525 /* Figure out whether or not there is an assignment-expression
16526 following the "throw" keyword. */
16527 if (token
->type
== CPP_COMMA
16528 || token
->type
== CPP_SEMICOLON
16529 || token
->type
== CPP_CLOSE_PAREN
16530 || token
->type
== CPP_CLOSE_SQUARE
16531 || token
->type
== CPP_CLOSE_BRACE
16532 || token
->type
== CPP_COLON
)
16533 expression
= NULL_TREE
;
16535 expression
= cp_parser_assignment_expression (parser
,
16536 /*cast_p=*/false, NULL
);
16538 return build_throw (expression
);
16541 /* GNU Extensions */
16543 /* Parse an (optional) asm-specification.
16546 asm ( string-literal )
16548 If the asm-specification is present, returns a STRING_CST
16549 corresponding to the string-literal. Otherwise, returns
16553 cp_parser_asm_specification_opt (cp_parser
* parser
)
16556 tree asm_specification
;
16558 /* Peek at the next token. */
16559 token
= cp_lexer_peek_token (parser
->lexer
);
16560 /* If the next token isn't the `asm' keyword, then there's no
16561 asm-specification. */
16562 if (!cp_parser_is_keyword (token
, RID_ASM
))
16565 /* Consume the `asm' token. */
16566 cp_lexer_consume_token (parser
->lexer
);
16567 /* Look for the `('. */
16568 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
16570 /* Look for the string-literal. */
16571 asm_specification
= cp_parser_string_literal (parser
, false, false);
16573 /* Look for the `)'. */
16574 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16576 return asm_specification
;
16579 /* Parse an asm-operand-list.
16583 asm-operand-list , asm-operand
16586 string-literal ( expression )
16587 [ string-literal ] string-literal ( expression )
16589 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16590 each node is the expression. The TREE_PURPOSE is itself a
16591 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16592 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16593 is a STRING_CST for the string literal before the parenthesis. Returns
16594 ERROR_MARK_NODE if any of the operands are invalid. */
16597 cp_parser_asm_operand_list (cp_parser
* parser
)
16599 tree asm_operands
= NULL_TREE
;
16600 bool invalid_operands
= false;
16604 tree string_literal
;
16608 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
16610 /* Consume the `[' token. */
16611 cp_lexer_consume_token (parser
->lexer
);
16612 /* Read the operand name. */
16613 name
= cp_parser_identifier (parser
);
16614 if (name
!= error_mark_node
)
16615 name
= build_string (IDENTIFIER_LENGTH (name
),
16616 IDENTIFIER_POINTER (name
));
16617 /* Look for the closing `]'. */
16618 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
16622 /* Look for the string-literal. */
16623 string_literal
= cp_parser_string_literal (parser
, false, false);
16625 /* Look for the `('. */
16626 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
16627 /* Parse the expression. */
16628 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
16629 /* Look for the `)'. */
16630 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16632 if (name
== error_mark_node
16633 || string_literal
== error_mark_node
16634 || expression
== error_mark_node
)
16635 invalid_operands
= true;
16637 /* Add this operand to the list. */
16638 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
16641 /* If the next token is not a `,', there are no more
16643 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16645 /* Consume the `,'. */
16646 cp_lexer_consume_token (parser
->lexer
);
16649 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
16652 /* Parse an asm-clobber-list.
16656 asm-clobber-list , string-literal
16658 Returns a TREE_LIST, indicating the clobbers in the order that they
16659 appeared. The TREE_VALUE of each node is a STRING_CST. */
16662 cp_parser_asm_clobber_list (cp_parser
* parser
)
16664 tree clobbers
= NULL_TREE
;
16668 tree string_literal
;
16670 /* Look for the string literal. */
16671 string_literal
= cp_parser_string_literal (parser
, false, false);
16672 /* Add it to the list. */
16673 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
16674 /* If the next token is not a `,', then the list is
16676 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16678 /* Consume the `,' token. */
16679 cp_lexer_consume_token (parser
->lexer
);
16685 /* Parse an (optional) series of attributes.
16688 attributes attribute
16691 __attribute__ (( attribute-list [opt] ))
16693 The return value is as for cp_parser_attribute_list. */
16696 cp_parser_attributes_opt (cp_parser
* parser
)
16698 tree attributes
= NULL_TREE
;
16703 tree attribute_list
;
16705 /* Peek at the next token. */
16706 token
= cp_lexer_peek_token (parser
->lexer
);
16707 /* If it's not `__attribute__', then we're done. */
16708 if (token
->keyword
!= RID_ATTRIBUTE
)
16711 /* Consume the `__attribute__' keyword. */
16712 cp_lexer_consume_token (parser
->lexer
);
16713 /* Look for the two `(' tokens. */
16714 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
16715 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
16717 /* Peek at the next token. */
16718 token
= cp_lexer_peek_token (parser
->lexer
);
16719 if (token
->type
!= CPP_CLOSE_PAREN
)
16720 /* Parse the attribute-list. */
16721 attribute_list
= cp_parser_attribute_list (parser
);
16723 /* If the next token is a `)', then there is no attribute
16725 attribute_list
= NULL
;
16727 /* Look for the two `)' tokens. */
16728 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16729 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16731 /* Add these new attributes to the list. */
16732 attributes
= chainon (attributes
, attribute_list
);
16738 /* Parse an attribute-list.
16742 attribute-list , attribute
16746 identifier ( identifier )
16747 identifier ( identifier , expression-list )
16748 identifier ( expression-list )
16750 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16751 to an attribute. The TREE_PURPOSE of each node is the identifier
16752 indicating which attribute is in use. The TREE_VALUE represents
16753 the arguments, if any. */
16756 cp_parser_attribute_list (cp_parser
* parser
)
16758 tree attribute_list
= NULL_TREE
;
16759 bool save_translate_strings_p
= parser
->translate_strings_p
;
16761 parser
->translate_strings_p
= false;
16768 /* Look for the identifier. We also allow keywords here; for
16769 example `__attribute__ ((const))' is legal. */
16770 token
= cp_lexer_peek_token (parser
->lexer
);
16771 if (token
->type
== CPP_NAME
16772 || token
->type
== CPP_KEYWORD
)
16774 tree arguments
= NULL_TREE
;
16776 /* Consume the token. */
16777 token
= cp_lexer_consume_token (parser
->lexer
);
16779 /* Save away the identifier that indicates which attribute
16781 identifier
= token
->u
.value
;
16782 attribute
= build_tree_list (identifier
, NULL_TREE
);
16784 /* Peek at the next token. */
16785 token
= cp_lexer_peek_token (parser
->lexer
);
16786 /* If it's an `(', then parse the attribute arguments. */
16787 if (token
->type
== CPP_OPEN_PAREN
)
16789 arguments
= cp_parser_parenthesized_expression_list
16790 (parser
, true, /*cast_p=*/false,
16791 /*allow_expansion_p=*/false,
16792 /*non_constant_p=*/NULL
);
16793 /* Save the arguments away. */
16794 TREE_VALUE (attribute
) = arguments
;
16797 if (arguments
!= error_mark_node
)
16799 /* Add this attribute to the list. */
16800 TREE_CHAIN (attribute
) = attribute_list
;
16801 attribute_list
= attribute
;
16804 token
= cp_lexer_peek_token (parser
->lexer
);
16806 /* Now, look for more attributes. If the next token isn't a
16807 `,', we're done. */
16808 if (token
->type
!= CPP_COMMA
)
16811 /* Consume the comma and keep going. */
16812 cp_lexer_consume_token (parser
->lexer
);
16814 parser
->translate_strings_p
= save_translate_strings_p
;
16816 /* We built up the list in reverse order. */
16817 return nreverse (attribute_list
);
16820 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16821 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16822 current value of the PEDANTIC flag, regardless of whether or not
16823 the `__extension__' keyword is present. The caller is responsible
16824 for restoring the value of the PEDANTIC flag. */
16827 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
16829 /* Save the old value of the PEDANTIC flag. */
16830 *saved_pedantic
= pedantic
;
16832 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
16834 /* Consume the `__extension__' token. */
16835 cp_lexer_consume_token (parser
->lexer
);
16836 /* We're not being pedantic while the `__extension__' keyword is
16846 /* Parse a label declaration.
16849 __label__ label-declarator-seq ;
16851 label-declarator-seq:
16852 identifier , label-declarator-seq
16856 cp_parser_label_declaration (cp_parser
* parser
)
16858 /* Look for the `__label__' keyword. */
16859 cp_parser_require_keyword (parser
, RID_LABEL
, "%<__label__%>");
16865 /* Look for an identifier. */
16866 identifier
= cp_parser_identifier (parser
);
16867 /* If we failed, stop. */
16868 if (identifier
== error_mark_node
)
16870 /* Declare it as a label. */
16871 finish_label_decl (identifier
);
16872 /* If the next token is a `;', stop. */
16873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16875 /* Look for the `,' separating the label declarations. */
16876 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
16879 /* Look for the final `;'. */
16880 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
16883 /* Support Functions */
16885 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16886 NAME should have one of the representations used for an
16887 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16888 is returned. If PARSER->SCOPE is a dependent type, then a
16889 SCOPE_REF is returned.
16891 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16892 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16893 was formed. Abstractly, such entities should not be passed to this
16894 function, because they do not need to be looked up, but it is
16895 simpler to check for this special case here, rather than at the
16898 In cases not explicitly covered above, this function returns a
16899 DECL, OVERLOAD, or baselink representing the result of the lookup.
16900 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16903 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16904 (e.g., "struct") that was used. In that case bindings that do not
16905 refer to types are ignored.
16907 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16910 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16913 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16916 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16917 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16918 NULL_TREE otherwise. */
16921 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
16922 enum tag_types tag_type
,
16925 bool check_dependency
,
16926 tree
*ambiguous_decls
,
16927 location_t name_location
)
16931 tree object_type
= parser
->context
->object_type
;
16933 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16934 flags
|= LOOKUP_COMPLAIN
;
16936 /* Assume that the lookup will be unambiguous. */
16937 if (ambiguous_decls
)
16938 *ambiguous_decls
= NULL_TREE
;
16940 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16941 no longer valid. Note that if we are parsing tentatively, and
16942 the parse fails, OBJECT_TYPE will be automatically restored. */
16943 parser
->context
->object_type
= NULL_TREE
;
16945 if (name
== error_mark_node
)
16946 return error_mark_node
;
16948 /* A template-id has already been resolved; there is no lookup to
16950 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16952 if (BASELINK_P (name
))
16954 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
16955 == TEMPLATE_ID_EXPR
);
16959 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16960 it should already have been checked to make sure that the name
16961 used matches the type being destroyed. */
16962 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16966 /* Figure out to which type this destructor applies. */
16968 type
= parser
->scope
;
16969 else if (object_type
)
16970 type
= object_type
;
16972 type
= current_class_type
;
16973 /* If that's not a class type, there is no destructor. */
16974 if (!type
|| !CLASS_TYPE_P (type
))
16975 return error_mark_node
;
16976 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
16977 lazily_declare_fn (sfk_destructor
, type
);
16978 if (!CLASSTYPE_DESTRUCTORS (type
))
16979 return error_mark_node
;
16980 /* If it was a class type, return the destructor. */
16981 return CLASSTYPE_DESTRUCTORS (type
);
16984 /* By this point, the NAME should be an ordinary identifier. If
16985 the id-expression was a qualified name, the qualifying scope is
16986 stored in PARSER->SCOPE at this point. */
16987 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
16989 /* Perform the lookup. */
16994 if (parser
->scope
== error_mark_node
)
16995 return error_mark_node
;
16997 /* If the SCOPE is dependent, the lookup must be deferred until
16998 the template is instantiated -- unless we are explicitly
16999 looking up names in uninstantiated templates. Even then, we
17000 cannot look up the name if the scope is not a class type; it
17001 might, for example, be a template type parameter. */
17002 dependent_p
= (TYPE_P (parser
->scope
)
17003 && !(parser
->in_declarator_p
17004 && currently_open_class (parser
->scope
))
17005 && dependent_type_p (parser
->scope
));
17006 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
17013 /* The resolution to Core Issue 180 says that `struct
17014 A::B' should be considered a type-name, even if `A'
17016 type
= make_typename_type (parser
->scope
, name
, tag_type
,
17017 /*complain=*/tf_error
);
17018 decl
= TYPE_NAME (type
);
17020 else if (is_template
17021 && (cp_parser_next_token_ends_template_argument_p (parser
)
17022 || cp_lexer_next_token_is (parser
->lexer
,
17024 decl
= make_unbound_class_template (parser
->scope
,
17026 /*complain=*/tf_error
);
17028 decl
= build_qualified_name (/*type=*/NULL_TREE
,
17029 parser
->scope
, name
,
17034 tree pushed_scope
= NULL_TREE
;
17036 /* If PARSER->SCOPE is a dependent type, then it must be a
17037 class type, and we must not be checking dependencies;
17038 otherwise, we would have processed this lookup above. So
17039 that PARSER->SCOPE is not considered a dependent base by
17040 lookup_member, we must enter the scope here. */
17042 pushed_scope
= push_scope (parser
->scope
);
17043 /* If the PARSER->SCOPE is a template specialization, it
17044 may be instantiated during name lookup. In that case,
17045 errors may be issued. Even if we rollback the current
17046 tentative parse, those errors are valid. */
17047 decl
= lookup_qualified_name (parser
->scope
, name
,
17048 tag_type
!= none_type
,
17049 /*complain=*/true);
17051 /* If we have a single function from a using decl, pull it out. */
17053 && TREE_CODE (decl
) == OVERLOAD
17054 && !really_overloaded_fn (decl
))
17055 decl
= OVL_FUNCTION (decl
);
17058 pop_scope (pushed_scope
);
17060 parser
->qualifying_scope
= parser
->scope
;
17061 parser
->object_scope
= NULL_TREE
;
17063 else if (object_type
)
17065 tree object_decl
= NULL_TREE
;
17066 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17067 OBJECT_TYPE is not a class. */
17068 if (CLASS_TYPE_P (object_type
))
17069 /* If the OBJECT_TYPE is a template specialization, it may
17070 be instantiated during name lookup. In that case, errors
17071 may be issued. Even if we rollback the current tentative
17072 parse, those errors are valid. */
17073 object_decl
= lookup_member (object_type
,
17076 tag_type
!= none_type
);
17077 /* Look it up in the enclosing context, too. */
17078 decl
= lookup_name_real (name
, tag_type
!= none_type
,
17080 /*block_p=*/true, is_namespace
, flags
);
17081 parser
->object_scope
= object_type
;
17082 parser
->qualifying_scope
= NULL_TREE
;
17084 decl
= object_decl
;
17088 decl
= lookup_name_real (name
, tag_type
!= none_type
,
17090 /*block_p=*/true, is_namespace
, flags
);
17091 parser
->qualifying_scope
= NULL_TREE
;
17092 parser
->object_scope
= NULL_TREE
;
17095 /* If the lookup failed, let our caller know. */
17096 if (!decl
|| decl
== error_mark_node
)
17097 return error_mark_node
;
17099 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17100 if (TREE_CODE (decl
) == TREE_LIST
)
17102 if (ambiguous_decls
)
17103 *ambiguous_decls
= decl
;
17104 /* The error message we have to print is too complicated for
17105 cp_parser_error, so we incorporate its actions directly. */
17106 if (!cp_parser_simulate_error (parser
))
17108 error ("%Hreference to %qD is ambiguous",
17109 &name_location
, name
);
17110 print_candidates (decl
);
17112 return error_mark_node
;
17115 gcc_assert (DECL_P (decl
)
17116 || TREE_CODE (decl
) == OVERLOAD
17117 || TREE_CODE (decl
) == SCOPE_REF
17118 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
17119 || BASELINK_P (decl
));
17121 /* If we have resolved the name of a member declaration, check to
17122 see if the declaration is accessible. When the name resolves to
17123 set of overloaded functions, accessibility is checked when
17124 overload resolution is done.
17126 During an explicit instantiation, access is not checked at all,
17127 as per [temp.explicit]. */
17129 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
17134 /* Like cp_parser_lookup_name, but for use in the typical case where
17135 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17136 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17139 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
17141 return cp_parser_lookup_name (parser
, name
,
17143 /*is_template=*/false,
17144 /*is_namespace=*/false,
17145 /*check_dependency=*/true,
17146 /*ambiguous_decls=*/NULL
,
17150 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17151 the current context, return the TYPE_DECL. If TAG_NAME_P is
17152 true, the DECL indicates the class being defined in a class-head,
17153 or declared in an elaborated-type-specifier.
17155 Otherwise, return DECL. */
17158 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
17160 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17161 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17164 template <typename T> struct B;
17167 template <typename T> struct A::B {};
17169 Similarly, in an elaborated-type-specifier:
17171 namespace N { struct X{}; }
17174 template <typename T> friend struct N::X;
17177 However, if the DECL refers to a class type, and we are in
17178 the scope of the class, then the name lookup automatically
17179 finds the TYPE_DECL created by build_self_reference rather
17180 than a TEMPLATE_DECL. For example, in:
17182 template <class T> struct S {
17186 there is no need to handle such case. */
17188 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
17189 return DECL_TEMPLATE_RESULT (decl
);
17194 /* If too many, or too few, template-parameter lists apply to the
17195 declarator, issue an error message. Returns TRUE if all went well,
17196 and FALSE otherwise. */
17199 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
17200 cp_declarator
*declarator
,
17201 location_t declarator_location
)
17203 unsigned num_templates
;
17205 /* We haven't seen any classes that involve template parameters yet. */
17208 switch (declarator
->kind
)
17211 if (declarator
->u
.id
.qualifying_scope
)
17216 scope
= declarator
->u
.id
.qualifying_scope
;
17217 member
= declarator
->u
.id
.unqualified_name
;
17219 while (scope
&& CLASS_TYPE_P (scope
))
17221 /* You're supposed to have one `template <...>'
17222 for every template class, but you don't need one
17223 for a full specialization. For example:
17225 template <class T> struct S{};
17226 template <> struct S<int> { void f(); };
17227 void S<int>::f () {}
17229 is correct; there shouldn't be a `template <>' for
17230 the definition of `S<int>::f'. */
17231 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
17232 /* If SCOPE does not have template information of any
17233 kind, then it is not a template, nor is it nested
17234 within a template. */
17236 if (explicit_class_specialization_p (scope
))
17238 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
17241 scope
= TYPE_CONTEXT (scope
);
17244 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
17245 == TEMPLATE_ID_EXPR
)
17246 /* If the DECLARATOR has the form `X<y>' then it uses one
17247 additional level of template parameters. */
17250 return cp_parser_check_template_parameters (parser
,
17252 declarator_location
);
17257 case cdk_reference
:
17259 return (cp_parser_check_declarator_template_parameters
17260 (parser
, declarator
->declarator
, declarator_location
));
17266 gcc_unreachable ();
17271 /* NUM_TEMPLATES were used in the current declaration. If that is
17272 invalid, return FALSE and issue an error messages. Otherwise,
17276 cp_parser_check_template_parameters (cp_parser
* parser
,
17277 unsigned num_templates
,
17278 location_t location
)
17280 /* If there are more template classes than parameter lists, we have
17283 template <class T> void S<T>::R<T>::f (); */
17284 if (parser
->num_template_parameter_lists
< num_templates
)
17286 error ("%Htoo few template-parameter-lists", &location
);
17289 /* If there are the same number of template classes and parameter
17290 lists, that's OK. */
17291 if (parser
->num_template_parameter_lists
== num_templates
)
17293 /* If there are more, but only one more, then we are referring to a
17294 member template. That's OK too. */
17295 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
17297 /* Otherwise, there are too many template parameter lists. We have
17300 template <class T> template <class U> void S::f(); */
17301 error ("%Htoo many template-parameter-lists", &location
);
17305 /* Parse an optional `::' token indicating that the following name is
17306 from the global namespace. If so, PARSER->SCOPE is set to the
17307 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17308 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17309 Returns the new value of PARSER->SCOPE, if the `::' token is
17310 present, and NULL_TREE otherwise. */
17313 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
17317 /* Peek at the next token. */
17318 token
= cp_lexer_peek_token (parser
->lexer
);
17319 /* If we're looking at a `::' token then we're starting from the
17320 global namespace, not our current location. */
17321 if (token
->type
== CPP_SCOPE
)
17323 /* Consume the `::' token. */
17324 cp_lexer_consume_token (parser
->lexer
);
17325 /* Set the SCOPE so that we know where to start the lookup. */
17326 parser
->scope
= global_namespace
;
17327 parser
->qualifying_scope
= global_namespace
;
17328 parser
->object_scope
= NULL_TREE
;
17330 return parser
->scope
;
17332 else if (!current_scope_valid_p
)
17334 parser
->scope
= NULL_TREE
;
17335 parser
->qualifying_scope
= NULL_TREE
;
17336 parser
->object_scope
= NULL_TREE
;
17342 /* Returns TRUE if the upcoming token sequence is the start of a
17343 constructor declarator. If FRIEND_P is true, the declarator is
17344 preceded by the `friend' specifier. */
17347 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
17349 bool constructor_p
;
17350 tree type_decl
= NULL_TREE
;
17351 bool nested_name_p
;
17352 cp_token
*next_token
;
17354 /* The common case is that this is not a constructor declarator, so
17355 try to avoid doing lots of work if at all possible. It's not
17356 valid declare a constructor at function scope. */
17357 if (parser
->in_function_body
)
17359 /* And only certain tokens can begin a constructor declarator. */
17360 next_token
= cp_lexer_peek_token (parser
->lexer
);
17361 if (next_token
->type
!= CPP_NAME
17362 && next_token
->type
!= CPP_SCOPE
17363 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
17364 && next_token
->type
!= CPP_TEMPLATE_ID
)
17367 /* Parse tentatively; we are going to roll back all of the tokens
17369 cp_parser_parse_tentatively (parser
);
17370 /* Assume that we are looking at a constructor declarator. */
17371 constructor_p
= true;
17373 /* Look for the optional `::' operator. */
17374 cp_parser_global_scope_opt (parser
,
17375 /*current_scope_valid_p=*/false);
17376 /* Look for the nested-name-specifier. */
17378 = (cp_parser_nested_name_specifier_opt (parser
,
17379 /*typename_keyword_p=*/false,
17380 /*check_dependency_p=*/false,
17382 /*is_declaration=*/false)
17384 /* Outside of a class-specifier, there must be a
17385 nested-name-specifier. */
17386 if (!nested_name_p
&&
17387 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
17389 constructor_p
= false;
17390 /* If we still think that this might be a constructor-declarator,
17391 look for a class-name. */
17396 template <typename T> struct S { S(); };
17397 template <typename T> S<T>::S ();
17399 we must recognize that the nested `S' names a class.
17402 template <typename T> S<T>::S<T> ();
17404 we must recognize that the nested `S' names a template. */
17405 type_decl
= cp_parser_class_name (parser
,
17406 /*typename_keyword_p=*/false,
17407 /*template_keyword_p=*/false,
17409 /*check_dependency_p=*/false,
17410 /*class_head_p=*/false,
17411 /*is_declaration=*/false);
17412 /* If there was no class-name, then this is not a constructor. */
17413 constructor_p
= !cp_parser_error_occurred (parser
);
17416 /* If we're still considering a constructor, we have to see a `(',
17417 to begin the parameter-declaration-clause, followed by either a
17418 `)', an `...', or a decl-specifier. We need to check for a
17419 type-specifier to avoid being fooled into thinking that:
17423 is a constructor. (It is actually a function named `f' that
17424 takes one parameter (of type `int') and returns a value of type
17427 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
17429 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17430 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
17431 /* A parameter declaration begins with a decl-specifier,
17432 which is either the "attribute" keyword, a storage class
17433 specifier, or (usually) a type-specifier. */
17434 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
17437 tree pushed_scope
= NULL_TREE
;
17438 unsigned saved_num_template_parameter_lists
;
17440 /* Names appearing in the type-specifier should be looked up
17441 in the scope of the class. */
17442 if (current_class_type
)
17446 type
= TREE_TYPE (type_decl
);
17447 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17449 type
= resolve_typename_type (type
,
17450 /*only_current_p=*/false);
17451 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17453 cp_parser_abort_tentative_parse (parser
);
17457 pushed_scope
= push_scope (type
);
17460 /* Inside the constructor parameter list, surrounding
17461 template-parameter-lists do not apply. */
17462 saved_num_template_parameter_lists
17463 = parser
->num_template_parameter_lists
;
17464 parser
->num_template_parameter_lists
= 0;
17466 /* Look for the type-specifier. */
17467 cp_parser_type_specifier (parser
,
17468 CP_PARSER_FLAGS_NONE
,
17469 /*decl_specs=*/NULL
,
17470 /*is_declarator=*/true,
17471 /*declares_class_or_enum=*/NULL
,
17472 /*is_cv_qualifier=*/NULL
);
17474 parser
->num_template_parameter_lists
17475 = saved_num_template_parameter_lists
;
17477 /* Leave the scope of the class. */
17479 pop_scope (pushed_scope
);
17481 constructor_p
= !cp_parser_error_occurred (parser
);
17485 constructor_p
= false;
17486 /* We did not really want to consume any tokens. */
17487 cp_parser_abort_tentative_parse (parser
);
17489 return constructor_p
;
17492 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17493 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17494 they must be performed once we are in the scope of the function.
17496 Returns the function defined. */
17499 cp_parser_function_definition_from_specifiers_and_declarator
17500 (cp_parser
* parser
,
17501 cp_decl_specifier_seq
*decl_specifiers
,
17503 const cp_declarator
*declarator
)
17508 /* Begin the function-definition. */
17509 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
17511 /* The things we're about to see are not directly qualified by any
17512 template headers we've seen thus far. */
17513 reset_specialization ();
17515 /* If there were names looked up in the decl-specifier-seq that we
17516 did not check, check them now. We must wait until we are in the
17517 scope of the function to perform the checks, since the function
17518 might be a friend. */
17519 perform_deferred_access_checks ();
17523 /* Skip the entire function. */
17524 cp_parser_skip_to_end_of_block_or_statement (parser
);
17525 fn
= error_mark_node
;
17527 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
17529 /* Seen already, skip it. An error message has already been output. */
17530 cp_parser_skip_to_end_of_block_or_statement (parser
);
17531 fn
= current_function_decl
;
17532 current_function_decl
= NULL_TREE
;
17533 /* If this is a function from a class, pop the nested class. */
17534 if (current_class_name
)
17535 pop_nested_class ();
17538 fn
= cp_parser_function_definition_after_declarator (parser
,
17539 /*inline_p=*/false);
17544 /* Parse the part of a function-definition that follows the
17545 declarator. INLINE_P is TRUE iff this function is an inline
17546 function defined with a class-specifier.
17548 Returns the function defined. */
17551 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
17555 bool ctor_initializer_p
= false;
17556 bool saved_in_unbraced_linkage_specification_p
;
17557 bool saved_in_function_body
;
17558 unsigned saved_num_template_parameter_lists
;
17561 saved_in_function_body
= parser
->in_function_body
;
17562 parser
->in_function_body
= true;
17563 /* If the next token is `return', then the code may be trying to
17564 make use of the "named return value" extension that G++ used to
17566 token
= cp_lexer_peek_token (parser
->lexer
);
17567 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
17569 /* Consume the `return' keyword. */
17570 cp_lexer_consume_token (parser
->lexer
);
17571 /* Look for the identifier that indicates what value is to be
17573 cp_parser_identifier (parser
);
17574 /* Issue an error message. */
17575 error ("%Hnamed return values are no longer supported",
17577 /* Skip tokens until we reach the start of the function body. */
17580 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17581 if (token
->type
== CPP_OPEN_BRACE
17582 || token
->type
== CPP_EOF
17583 || token
->type
== CPP_PRAGMA_EOL
)
17585 cp_lexer_consume_token (parser
->lexer
);
17588 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17589 anything declared inside `f'. */
17590 saved_in_unbraced_linkage_specification_p
17591 = parser
->in_unbraced_linkage_specification_p
;
17592 parser
->in_unbraced_linkage_specification_p
= false;
17593 /* Inside the function, surrounding template-parameter-lists do not
17595 saved_num_template_parameter_lists
17596 = parser
->num_template_parameter_lists
;
17597 parser
->num_template_parameter_lists
= 0;
17598 /* If the next token is `try', then we are looking at a
17599 function-try-block. */
17600 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
17601 ctor_initializer_p
= cp_parser_function_try_block (parser
);
17602 /* A function-try-block includes the function-body, so we only do
17603 this next part if we're not processing a function-try-block. */
17606 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
17608 /* Finish the function. */
17609 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
17610 (inline_p
? 2 : 0));
17611 /* Generate code for it, if necessary. */
17612 expand_or_defer_fn (fn
);
17613 /* Restore the saved values. */
17614 parser
->in_unbraced_linkage_specification_p
17615 = saved_in_unbraced_linkage_specification_p
;
17616 parser
->num_template_parameter_lists
17617 = saved_num_template_parameter_lists
;
17618 parser
->in_function_body
= saved_in_function_body
;
17623 /* Parse a template-declaration, assuming that the `export' (and
17624 `extern') keywords, if present, has already been scanned. MEMBER_P
17625 is as for cp_parser_template_declaration. */
17628 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
17630 tree decl
= NULL_TREE
;
17631 VEC (deferred_access_check
,gc
) *checks
;
17632 tree parameter_list
;
17633 bool friend_p
= false;
17634 bool need_lang_pop
;
17637 /* Look for the `template' keyword. */
17638 token
= cp_lexer_peek_token (parser
->lexer
);
17639 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "%<template%>"))
17643 if (!cp_parser_require (parser
, CPP_LESS
, "%<<%>"))
17645 if (at_class_scope_p () && current_function_decl
)
17647 /* 14.5.2.2 [temp.mem]
17649 A local class shall not have member templates. */
17650 error ("%Hinvalid declaration of member template in local class",
17652 cp_parser_skip_to_end_of_block_or_statement (parser
);
17657 A template ... shall not have C linkage. */
17658 if (current_lang_name
== lang_name_c
)
17660 error ("%Htemplate with C linkage", &token
->location
);
17661 /* Give it C++ linkage to avoid confusing other parts of the
17663 push_lang_context (lang_name_cplusplus
);
17664 need_lang_pop
= true;
17667 need_lang_pop
= false;
17669 /* We cannot perform access checks on the template parameter
17670 declarations until we know what is being declared, just as we
17671 cannot check the decl-specifier list. */
17672 push_deferring_access_checks (dk_deferred
);
17674 /* If the next token is `>', then we have an invalid
17675 specialization. Rather than complain about an invalid template
17676 parameter, issue an error message here. */
17677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
17679 cp_parser_error (parser
, "invalid explicit specialization");
17680 begin_specialization ();
17681 parameter_list
= NULL_TREE
;
17684 /* Parse the template parameters. */
17685 parameter_list
= cp_parser_template_parameter_list (parser
);
17687 /* Get the deferred access checks from the parameter list. These
17688 will be checked once we know what is being declared, as for a
17689 member template the checks must be performed in the scope of the
17690 class containing the member. */
17691 checks
= get_deferred_access_checks ();
17693 /* Look for the `>'. */
17694 cp_parser_skip_to_end_of_template_parameter_list (parser
);
17695 /* We just processed one more parameter list. */
17696 ++parser
->num_template_parameter_lists
;
17697 /* If the next token is `template', there are more template
17699 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17701 cp_parser_template_declaration_after_export (parser
, member_p
);
17704 /* There are no access checks when parsing a template, as we do not
17705 know if a specialization will be a friend. */
17706 push_deferring_access_checks (dk_no_check
);
17707 token
= cp_lexer_peek_token (parser
->lexer
);
17708 decl
= cp_parser_single_declaration (parser
,
17711 /*explicit_specialization_p=*/false,
17713 pop_deferring_access_checks ();
17715 /* If this is a member template declaration, let the front
17717 if (member_p
&& !friend_p
&& decl
)
17719 if (TREE_CODE (decl
) == TYPE_DECL
)
17720 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
17722 decl
= finish_member_template_decl (decl
);
17724 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
17725 make_friend_class (current_class_type
, TREE_TYPE (decl
),
17726 /*complain=*/true);
17728 /* We are done with the current parameter list. */
17729 --parser
->num_template_parameter_lists
;
17731 pop_deferring_access_checks ();
17734 finish_template_decl (parameter_list
);
17736 /* Register member declarations. */
17737 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
17738 finish_member_declaration (decl
);
17739 /* For the erroneous case of a template with C linkage, we pushed an
17740 implicit C++ linkage scope; exit that scope now. */
17742 pop_lang_context ();
17743 /* If DECL is a function template, we must return to parse it later.
17744 (Even though there is no definition, there might be default
17745 arguments that need handling.) */
17746 if (member_p
&& decl
17747 && (TREE_CODE (decl
) == FUNCTION_DECL
17748 || DECL_FUNCTION_TEMPLATE_P (decl
)))
17749 TREE_VALUE (parser
->unparsed_functions_queues
)
17750 = tree_cons (NULL_TREE
, decl
,
17751 TREE_VALUE (parser
->unparsed_functions_queues
));
17754 /* Perform the deferred access checks from a template-parameter-list.
17755 CHECKS is a TREE_LIST of access checks, as returned by
17756 get_deferred_access_checks. */
17759 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
17761 ++processing_template_parmlist
;
17762 perform_access_checks (checks
);
17763 --processing_template_parmlist
;
17766 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17767 `function-definition' sequence. MEMBER_P is true, this declaration
17768 appears in a class scope.
17770 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17771 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17774 cp_parser_single_declaration (cp_parser
* parser
,
17775 VEC (deferred_access_check
,gc
)* checks
,
17777 bool explicit_specialization_p
,
17780 int declares_class_or_enum
;
17781 tree decl
= NULL_TREE
;
17782 cp_decl_specifier_seq decl_specifiers
;
17783 bool function_definition_p
= false;
17784 cp_token
*decl_spec_token_start
;
17786 /* This function is only used when processing a template
17788 gcc_assert (innermost_scope_kind () == sk_template_parms
17789 || innermost_scope_kind () == sk_template_spec
);
17791 /* Defer access checks until we know what is being declared. */
17792 push_deferring_access_checks (dk_deferred
);
17794 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17796 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
17797 cp_parser_decl_specifier_seq (parser
,
17798 CP_PARSER_FLAGS_OPTIONAL
,
17800 &declares_class_or_enum
);
17802 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
17804 /* There are no template typedefs. */
17805 if (decl_specifiers
.specs
[(int) ds_typedef
])
17807 error ("%Htemplate declaration of %qs",
17808 &decl_spec_token_start
->location
, "typedef");
17809 decl
= error_mark_node
;
17812 /* Gather up the access checks that occurred the
17813 decl-specifier-seq. */
17814 stop_deferring_access_checks ();
17816 /* Check for the declaration of a template class. */
17817 if (declares_class_or_enum
)
17819 if (cp_parser_declares_only_class_p (parser
))
17821 decl
= shadow_tag (&decl_specifiers
);
17826 friend template <typename T> struct A<T>::B;
17829 A<T>::B will be represented by a TYPENAME_TYPE, and
17830 therefore not recognized by shadow_tag. */
17831 if (friend_p
&& *friend_p
17833 && decl_specifiers
.type
17834 && TYPE_P (decl_specifiers
.type
))
17835 decl
= decl_specifiers
.type
;
17837 if (decl
&& decl
!= error_mark_node
)
17838 decl
= TYPE_NAME (decl
);
17840 decl
= error_mark_node
;
17842 /* Perform access checks for template parameters. */
17843 cp_parser_perform_template_parameter_access_checks (checks
);
17846 /* If it's not a template class, try for a template function. If
17847 the next token is a `;', then this declaration does not declare
17848 anything. But, if there were errors in the decl-specifiers, then
17849 the error might well have come from an attempted class-specifier.
17850 In that case, there's no need to warn about a missing declarator. */
17852 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
17853 || decl_specifiers
.type
!= error_mark_node
))
17855 decl
= cp_parser_init_declarator (parser
,
17858 /*function_definition_allowed_p=*/true,
17860 declares_class_or_enum
,
17861 &function_definition_p
);
17863 /* 7.1.1-1 [dcl.stc]
17865 A storage-class-specifier shall not be specified in an explicit
17866 specialization... */
17868 && explicit_specialization_p
17869 && decl_specifiers
.storage_class
!= sc_none
)
17871 error ("%Hexplicit template specialization cannot have a storage class",
17872 &decl_spec_token_start
->location
);
17873 decl
= error_mark_node
;
17877 pop_deferring_access_checks ();
17879 /* Clear any current qualification; whatever comes next is the start
17880 of something new. */
17881 parser
->scope
= NULL_TREE
;
17882 parser
->qualifying_scope
= NULL_TREE
;
17883 parser
->object_scope
= NULL_TREE
;
17884 /* Look for a trailing `;' after the declaration. */
17885 if (!function_definition_p
17886 && (decl
== error_mark_node
17887 || !cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>")))
17888 cp_parser_skip_to_end_of_block_or_statement (parser
);
17893 /* Parse a cast-expression that is not the operand of a unary "&". */
17896 cp_parser_simple_cast_expression (cp_parser
*parser
)
17898 return cp_parser_cast_expression (parser
, /*address_p=*/false,
17899 /*cast_p=*/false, NULL
);
17902 /* Parse a functional cast to TYPE. Returns an expression
17903 representing the cast. */
17906 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
17908 tree expression_list
;
17912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17914 maybe_warn_cpp0x ("extended initializer lists");
17915 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
17916 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
17917 if (TREE_CODE (type
) == TYPE_DECL
)
17918 type
= TREE_TYPE (type
);
17919 return finish_compound_literal (type
, expression_list
);
17923 = cp_parser_parenthesized_expression_list (parser
, false,
17925 /*allow_expansion_p=*/true,
17926 /*non_constant_p=*/NULL
);
17928 cast
= build_functional_cast (type
, expression_list
,
17929 tf_warning_or_error
);
17930 /* [expr.const]/1: In an integral constant expression "only type
17931 conversions to integral or enumeration type can be used". */
17932 if (TREE_CODE (type
) == TYPE_DECL
)
17933 type
= TREE_TYPE (type
);
17934 if (cast
!= error_mark_node
17935 && !cast_valid_in_integral_constant_expression_p (type
)
17936 && (cp_parser_non_integral_constant_expression
17937 (parser
, "a call to a constructor")))
17938 return error_mark_node
;
17942 /* Save the tokens that make up the body of a member function defined
17943 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17944 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17945 specifiers applied to the declaration. Returns the FUNCTION_DECL
17946 for the member function. */
17949 cp_parser_save_member_function_body (cp_parser
* parser
,
17950 cp_decl_specifier_seq
*decl_specifiers
,
17951 cp_declarator
*declarator
,
17958 /* Create the function-declaration. */
17959 fn
= start_method (decl_specifiers
, declarator
, attributes
);
17960 /* If something went badly wrong, bail out now. */
17961 if (fn
== error_mark_node
)
17963 /* If there's a function-body, skip it. */
17964 if (cp_parser_token_starts_function_definition_p
17965 (cp_lexer_peek_token (parser
->lexer
)))
17966 cp_parser_skip_to_end_of_block_or_statement (parser
);
17967 return error_mark_node
;
17970 /* Remember it, if there default args to post process. */
17971 cp_parser_save_default_args (parser
, fn
);
17973 /* Save away the tokens that make up the body of the
17975 first
= parser
->lexer
->next_token
;
17976 /* We can have braced-init-list mem-initializers before the fn body. */
17977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17979 cp_lexer_consume_token (parser
->lexer
);
17980 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
17981 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
17983 /* cache_group will stop after an un-nested { } pair, too. */
17984 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
17987 /* variadic mem-inits have ... after the ')'. */
17988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17989 cp_lexer_consume_token (parser
->lexer
);
17992 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
17993 /* Handle function try blocks. */
17994 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
17995 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
17996 last
= parser
->lexer
->next_token
;
17998 /* Save away the inline definition; we will process it when the
17999 class is complete. */
18000 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
18001 DECL_PENDING_INLINE_P (fn
) = 1;
18003 /* We need to know that this was defined in the class, so that
18004 friend templates are handled correctly. */
18005 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
18007 /* We're done with the inline definition. */
18008 finish_method (fn
);
18010 /* Add FN to the queue of functions to be parsed later. */
18011 TREE_VALUE (parser
->unparsed_functions_queues
)
18012 = tree_cons (NULL_TREE
, fn
,
18013 TREE_VALUE (parser
->unparsed_functions_queues
));
18018 /* Parse a template-argument-list, as well as the trailing ">" (but
18019 not the opening ">"). See cp_parser_template_argument_list for the
18023 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
18027 tree saved_qualifying_scope
;
18028 tree saved_object_scope
;
18029 bool saved_greater_than_is_operator_p
;
18030 bool saved_skip_evaluation
;
18034 When parsing a template-id, the first non-nested `>' is taken as
18035 the end of the template-argument-list rather than a greater-than
18037 saved_greater_than_is_operator_p
18038 = parser
->greater_than_is_operator_p
;
18039 parser
->greater_than_is_operator_p
= false;
18040 /* Parsing the argument list may modify SCOPE, so we save it
18042 saved_scope
= parser
->scope
;
18043 saved_qualifying_scope
= parser
->qualifying_scope
;
18044 saved_object_scope
= parser
->object_scope
;
18045 /* We need to evaluate the template arguments, even though this
18046 template-id may be nested within a "sizeof". */
18047 saved_skip_evaluation
= skip_evaluation
;
18048 skip_evaluation
= false;
18049 /* Parse the template-argument-list itself. */
18050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
18051 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
18052 arguments
= NULL_TREE
;
18054 arguments
= cp_parser_template_argument_list (parser
);
18055 /* Look for the `>' that ends the template-argument-list. If we find
18056 a '>>' instead, it's probably just a typo. */
18057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
18059 if (cxx_dialect
!= cxx98
)
18061 /* In C++0x, a `>>' in a template argument list or cast
18062 expression is considered to be two separate `>'
18063 tokens. So, change the current token to a `>', but don't
18064 consume it: it will be consumed later when the outer
18065 template argument list (or cast expression) is parsed.
18066 Note that this replacement of `>' for `>>' is necessary
18067 even if we are parsing tentatively: in the tentative
18068 case, after calling
18069 cp_parser_enclosed_template_argument_list we will always
18070 throw away all of the template arguments and the first
18071 closing `>', either because the template argument list
18072 was erroneous or because we are replacing those tokens
18073 with a CPP_TEMPLATE_ID token. The second `>' (which will
18074 not have been thrown away) is needed either to close an
18075 outer template argument list or to complete a new-style
18077 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18078 token
->type
= CPP_GREATER
;
18080 else if (!saved_greater_than_is_operator_p
)
18082 /* If we're in a nested template argument list, the '>>' has
18083 to be a typo for '> >'. We emit the error message, but we
18084 continue parsing and we push a '>' as next token, so that
18085 the argument list will be parsed correctly. Note that the
18086 global source location is still on the token before the
18087 '>>', so we need to say explicitly where we want it. */
18088 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18089 error ("%H%<>>%> should be %<> >%> "
18090 "within a nested template argument list",
18093 token
->type
= CPP_GREATER
;
18097 /* If this is not a nested template argument list, the '>>'
18098 is a typo for '>'. Emit an error message and continue.
18099 Same deal about the token location, but here we can get it
18100 right by consuming the '>>' before issuing the diagnostic. */
18101 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
18102 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18103 "a template argument list", &token
->location
);
18107 cp_parser_skip_to_end_of_template_parameter_list (parser
);
18108 /* The `>' token might be a greater-than operator again now. */
18109 parser
->greater_than_is_operator_p
18110 = saved_greater_than_is_operator_p
;
18111 /* Restore the SAVED_SCOPE. */
18112 parser
->scope
= saved_scope
;
18113 parser
->qualifying_scope
= saved_qualifying_scope
;
18114 parser
->object_scope
= saved_object_scope
;
18115 skip_evaluation
= saved_skip_evaluation
;
18120 /* MEMBER_FUNCTION is a member function, or a friend. If default
18121 arguments, or the body of the function have not yet been parsed,
18125 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
18127 /* If this member is a template, get the underlying
18129 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
18130 member_function
= DECL_TEMPLATE_RESULT (member_function
);
18132 /* There should not be any class definitions in progress at this
18133 point; the bodies of members are only parsed outside of all class
18135 gcc_assert (parser
->num_classes_being_defined
== 0);
18136 /* While we're parsing the member functions we might encounter more
18137 classes. We want to handle them right away, but we don't want
18138 them getting mixed up with functions that are currently in the
18140 parser
->unparsed_functions_queues
18141 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
18143 /* Make sure that any template parameters are in scope. */
18144 maybe_begin_member_template_processing (member_function
);
18146 /* If the body of the function has not yet been parsed, parse it
18148 if (DECL_PENDING_INLINE_P (member_function
))
18150 tree function_scope
;
18151 cp_token_cache
*tokens
;
18153 /* The function is no longer pending; we are processing it. */
18154 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
18155 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
18156 DECL_PENDING_INLINE_P (member_function
) = 0;
18158 /* If this is a local class, enter the scope of the containing
18160 function_scope
= current_function_decl
;
18161 if (function_scope
)
18162 push_function_context ();
18164 /* Push the body of the function onto the lexer stack. */
18165 cp_parser_push_lexer_for_tokens (parser
, tokens
);
18167 /* Let the front end know that we going to be defining this
18169 start_preparsed_function (member_function
, NULL_TREE
,
18170 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
18172 /* Don't do access checking if it is a templated function. */
18173 if (processing_template_decl
)
18174 push_deferring_access_checks (dk_no_check
);
18176 /* Now, parse the body of the function. */
18177 cp_parser_function_definition_after_declarator (parser
,
18178 /*inline_p=*/true);
18180 if (processing_template_decl
)
18181 pop_deferring_access_checks ();
18183 /* Leave the scope of the containing function. */
18184 if (function_scope
)
18185 pop_function_context ();
18186 cp_parser_pop_lexer (parser
);
18189 /* Remove any template parameters from the symbol table. */
18190 maybe_end_member_template_processing ();
18192 /* Restore the queue. */
18193 parser
->unparsed_functions_queues
18194 = TREE_CHAIN (parser
->unparsed_functions_queues
);
18197 /* If DECL contains any default args, remember it on the unparsed
18198 functions queue. */
18201 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
18205 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
18207 probe
= TREE_CHAIN (probe
))
18208 if (TREE_PURPOSE (probe
))
18210 TREE_PURPOSE (parser
->unparsed_functions_queues
)
18211 = tree_cons (current_class_type
, decl
,
18212 TREE_PURPOSE (parser
->unparsed_functions_queues
));
18217 /* FN is a FUNCTION_DECL which may contains a parameter with an
18218 unparsed DEFAULT_ARG. Parse the default args now. This function
18219 assumes that the current scope is the scope in which the default
18220 argument should be processed. */
18223 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
18225 bool saved_local_variables_forbidden_p
;
18228 /* While we're parsing the default args, we might (due to the
18229 statement expression extension) encounter more classes. We want
18230 to handle them right away, but we don't want them getting mixed
18231 up with default args that are currently in the queue. */
18232 parser
->unparsed_functions_queues
18233 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
18235 /* Local variable names (and the `this' keyword) may not appear
18236 in a default argument. */
18237 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
18238 parser
->local_variables_forbidden_p
= true;
18240 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
18242 parm
= TREE_CHAIN (parm
))
18244 cp_token_cache
*tokens
;
18245 tree default_arg
= TREE_PURPOSE (parm
);
18247 VEC(tree
,gc
) *insts
;
18254 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
18255 /* This can happen for a friend declaration for a function
18256 already declared with default arguments. */
18259 /* Push the saved tokens for the default argument onto the parser's
18261 tokens
= DEFARG_TOKENS (default_arg
);
18262 cp_parser_push_lexer_for_tokens (parser
, tokens
);
18264 /* Parse the assignment-expression. */
18265 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
18267 if (!processing_template_decl
)
18268 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
18270 TREE_PURPOSE (parm
) = parsed_arg
;
18272 /* Update any instantiations we've already created. */
18273 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
18274 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
18275 TREE_PURPOSE (copy
) = parsed_arg
;
18277 /* If the token stream has not been completely used up, then
18278 there was extra junk after the end of the default
18280 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
18281 cp_parser_error (parser
, "expected %<,%>");
18283 /* Revert to the main lexer. */
18284 cp_parser_pop_lexer (parser
);
18287 /* Make sure no default arg is missing. */
18288 check_default_args (fn
);
18290 /* Restore the state of local_variables_forbidden_p. */
18291 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
18293 /* Restore the queue. */
18294 parser
->unparsed_functions_queues
18295 = TREE_CHAIN (parser
->unparsed_functions_queues
);
18298 /* Parse the operand of `sizeof' (or a similar operator). Returns
18299 either a TYPE or an expression, depending on the form of the
18300 input. The KEYWORD indicates which kind of expression we have
18304 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
18306 tree expr
= NULL_TREE
;
18307 const char *saved_message
;
18309 bool saved_integral_constant_expression_p
;
18310 bool saved_non_integral_constant_expression_p
;
18311 bool pack_expansion_p
= false;
18313 /* Types cannot be defined in a `sizeof' expression. Save away the
18315 saved_message
= parser
->type_definition_forbidden_message
;
18316 /* And create the new one. */
18317 tmp
= concat ("types may not be defined in %<",
18318 IDENTIFIER_POINTER (ridpointers
[keyword
]),
18319 "%> expressions", NULL
);
18320 parser
->type_definition_forbidden_message
= tmp
;
18322 /* The restrictions on constant-expressions do not apply inside
18323 sizeof expressions. */
18324 saved_integral_constant_expression_p
18325 = parser
->integral_constant_expression_p
;
18326 saved_non_integral_constant_expression_p
18327 = parser
->non_integral_constant_expression_p
;
18328 parser
->integral_constant_expression_p
= false;
18330 /* If it's a `...', then we are computing the length of a parameter
18332 if (keyword
== RID_SIZEOF
18333 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18335 /* Consume the `...'. */
18336 cp_lexer_consume_token (parser
->lexer
);
18337 maybe_warn_variadic_templates ();
18339 /* Note that this is an expansion. */
18340 pack_expansion_p
= true;
18343 /* Do not actually evaluate the expression. */
18345 /* If it's a `(', then we might be looking at the type-id
18347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18350 bool saved_in_type_id_in_expr_p
;
18352 /* We can't be sure yet whether we're looking at a type-id or an
18354 cp_parser_parse_tentatively (parser
);
18355 /* Consume the `('. */
18356 cp_lexer_consume_token (parser
->lexer
);
18357 /* Parse the type-id. */
18358 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
18359 parser
->in_type_id_in_expr_p
= true;
18360 type
= cp_parser_type_id (parser
);
18361 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
18362 /* Now, look for the trailing `)'. */
18363 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
18364 /* If all went well, then we're done. */
18365 if (cp_parser_parse_definitely (parser
))
18367 cp_decl_specifier_seq decl_specs
;
18369 /* Build a trivial decl-specifier-seq. */
18370 clear_decl_specs (&decl_specs
);
18371 decl_specs
.type
= type
;
18373 /* Call grokdeclarator to figure out what type this is. */
18374 expr
= grokdeclarator (NULL
,
18378 /*attrlist=*/NULL
);
18382 /* If the type-id production did not work out, then we must be
18383 looking at the unary-expression production. */
18385 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18386 /*cast_p=*/false, NULL
);
18388 if (pack_expansion_p
)
18389 /* Build a pack expansion. */
18390 expr
= make_pack_expansion (expr
);
18392 /* Go back to evaluating expressions. */
18395 /* Free the message we created. */
18397 /* And restore the old one. */
18398 parser
->type_definition_forbidden_message
= saved_message
;
18399 parser
->integral_constant_expression_p
18400 = saved_integral_constant_expression_p
;
18401 parser
->non_integral_constant_expression_p
18402 = saved_non_integral_constant_expression_p
;
18407 /* If the current declaration has no declarator, return true. */
18410 cp_parser_declares_only_class_p (cp_parser
*parser
)
18412 /* If the next token is a `;' or a `,' then there is no
18414 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18415 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
18418 /* Update the DECL_SPECS to reflect the storage class indicated by
18422 cp_parser_set_storage_class (cp_parser
*parser
,
18423 cp_decl_specifier_seq
*decl_specs
,
18425 location_t location
)
18427 cp_storage_class storage_class
;
18429 if (parser
->in_unbraced_linkage_specification_p
)
18431 error ("%Hinvalid use of %qD in linkage specification",
18432 &location
, ridpointers
[keyword
]);
18435 else if (decl_specs
->storage_class
!= sc_none
)
18437 decl_specs
->conflicting_specifiers_p
= true;
18441 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
18442 && decl_specs
->specs
[(int) ds_thread
])
18444 error ("%H%<__thread%> before %qD", &location
, ridpointers
[keyword
]);
18445 decl_specs
->specs
[(int) ds_thread
] = 0;
18451 storage_class
= sc_auto
;
18454 storage_class
= sc_register
;
18457 storage_class
= sc_static
;
18460 storage_class
= sc_extern
;
18463 storage_class
= sc_mutable
;
18466 gcc_unreachable ();
18468 decl_specs
->storage_class
= storage_class
;
18470 /* A storage class specifier cannot be applied alongside a typedef
18471 specifier. If there is a typedef specifier present then set
18472 conflicting_specifiers_p which will trigger an error later
18473 on in grokdeclarator. */
18474 if (decl_specs
->specs
[(int)ds_typedef
])
18475 decl_specs
->conflicting_specifiers_p
= true;
18478 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18479 is true, the type is a user-defined type; otherwise it is a
18480 built-in type specified by a keyword. */
18483 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
18485 location_t location
,
18486 bool user_defined_p
)
18488 decl_specs
->any_specifiers_p
= true;
18490 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18491 (with, for example, in "typedef int wchar_t;") we remember that
18492 this is what happened. In system headers, we ignore these
18493 declarations so that G++ can work with system headers that are not
18495 if (decl_specs
->specs
[(int) ds_typedef
]
18497 && (type_spec
== boolean_type_node
18498 || type_spec
== char16_type_node
18499 || type_spec
== char32_type_node
18500 || type_spec
== wchar_type_node
)
18501 && (decl_specs
->type
18502 || decl_specs
->specs
[(int) ds_long
]
18503 || decl_specs
->specs
[(int) ds_short
]
18504 || decl_specs
->specs
[(int) ds_unsigned
]
18505 || decl_specs
->specs
[(int) ds_signed
]))
18507 decl_specs
->redefined_builtin_type
= type_spec
;
18508 if (!decl_specs
->type
)
18510 decl_specs
->type
= type_spec
;
18511 decl_specs
->user_defined_type_p
= false;
18512 decl_specs
->type_location
= location
;
18515 else if (decl_specs
->type
)
18516 decl_specs
->multiple_types_p
= true;
18519 decl_specs
->type
= type_spec
;
18520 decl_specs
->user_defined_type_p
= user_defined_p
;
18521 decl_specs
->redefined_builtin_type
= NULL_TREE
;
18522 decl_specs
->type_location
= location
;
18526 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18527 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18530 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
18532 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
18535 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18536 issue an error message indicating that TOKEN_DESC was expected.
18538 Returns the token consumed, if the token had the appropriate type.
18539 Otherwise, returns NULL. */
18542 cp_parser_require (cp_parser
* parser
,
18543 enum cpp_ttype type
,
18544 const char* token_desc
)
18546 if (cp_lexer_next_token_is (parser
->lexer
, type
))
18547 return cp_lexer_consume_token (parser
->lexer
);
18550 /* Output the MESSAGE -- unless we're parsing tentatively. */
18551 if (!cp_parser_simulate_error (parser
))
18553 char *message
= concat ("expected ", token_desc
, NULL
);
18554 cp_parser_error (parser
, message
);
18561 /* An error message is produced if the next token is not '>'.
18562 All further tokens are skipped until the desired token is
18563 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18566 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
18568 /* Current level of '< ... >'. */
18569 unsigned level
= 0;
18570 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18571 unsigned nesting_depth
= 0;
18573 /* Are we ready, yet? If not, issue error message. */
18574 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
18577 /* Skip tokens until the desired token is found. */
18580 /* Peek at the next token. */
18581 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18584 if (!nesting_depth
)
18589 if (cxx_dialect
== cxx98
)
18590 /* C++0x views the `>>' operator as two `>' tokens, but
18593 else if (!nesting_depth
&& level
-- == 0)
18595 /* We've hit a `>>' where the first `>' closes the
18596 template argument list, and the second `>' is
18597 spurious. Just consume the `>>' and stop; we've
18598 already produced at least one error. */
18599 cp_lexer_consume_token (parser
->lexer
);
18602 /* Fall through for C++0x, so we handle the second `>' in
18606 if (!nesting_depth
&& level
-- == 0)
18608 /* We've reached the token we want, consume it and stop. */
18609 cp_lexer_consume_token (parser
->lexer
);
18614 case CPP_OPEN_PAREN
:
18615 case CPP_OPEN_SQUARE
:
18619 case CPP_CLOSE_PAREN
:
18620 case CPP_CLOSE_SQUARE
:
18621 if (nesting_depth
-- == 0)
18626 case CPP_PRAGMA_EOL
:
18627 case CPP_SEMICOLON
:
18628 case CPP_OPEN_BRACE
:
18629 case CPP_CLOSE_BRACE
:
18630 /* The '>' was probably forgotten, don't look further. */
18637 /* Consume this token. */
18638 cp_lexer_consume_token (parser
->lexer
);
18642 /* If the next token is the indicated keyword, consume it. Otherwise,
18643 issue an error message indicating that TOKEN_DESC was expected.
18645 Returns the token consumed, if the token had the appropriate type.
18646 Otherwise, returns NULL. */
18649 cp_parser_require_keyword (cp_parser
* parser
,
18651 const char* token_desc
)
18653 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
18655 if (token
&& token
->keyword
!= keyword
)
18657 dyn_string_t error_msg
;
18659 /* Format the error message. */
18660 error_msg
= dyn_string_new (0);
18661 dyn_string_append_cstr (error_msg
, "expected ");
18662 dyn_string_append_cstr (error_msg
, token_desc
);
18663 cp_parser_error (parser
, error_msg
->s
);
18664 dyn_string_delete (error_msg
);
18671 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18672 function-definition. */
18675 cp_parser_token_starts_function_definition_p (cp_token
* token
)
18677 return (/* An ordinary function-body begins with an `{'. */
18678 token
->type
== CPP_OPEN_BRACE
18679 /* A ctor-initializer begins with a `:'. */
18680 || token
->type
== CPP_COLON
18681 /* A function-try-block begins with `try'. */
18682 || token
->keyword
== RID_TRY
18683 /* The named return value extension begins with `return'. */
18684 || token
->keyword
== RID_RETURN
);
18687 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18691 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
18695 token
= cp_lexer_peek_token (parser
->lexer
);
18696 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
18699 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18700 C++0x) ending a template-argument. */
18703 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
18707 token
= cp_lexer_peek_token (parser
->lexer
);
18708 return (token
->type
== CPP_COMMA
18709 || token
->type
== CPP_GREATER
18710 || token
->type
== CPP_ELLIPSIS
18711 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
18714 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18715 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18718 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
18723 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
18724 if (token
->type
== CPP_LESS
)
18726 /* Check for the sequence `<::' in the original code. It would be lexed as
18727 `[:', where `[' is a digraph, and there is no whitespace before
18729 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
18732 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
18733 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
18739 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18740 or none_type otherwise. */
18742 static enum tag_types
18743 cp_parser_token_is_class_key (cp_token
* token
)
18745 switch (token
->keyword
)
18750 return record_type
;
18759 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18762 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
18764 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
18765 permerror (input_location
, "%qs tag used in naming %q#T",
18766 class_key
== union_type
? "union"
18767 : class_key
== record_type
? "struct" : "class",
18771 /* Issue an error message if DECL is redeclared with different
18772 access than its original declaration [class.access.spec/3].
18773 This applies to nested classes and nested class templates.
18777 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
18779 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
18782 if ((TREE_PRIVATE (decl
)
18783 != (current_access_specifier
== access_private_node
))
18784 || (TREE_PROTECTED (decl
)
18785 != (current_access_specifier
== access_protected_node
)))
18786 error ("%H%qD redeclared with different access", &location
, decl
);
18789 /* Look for the `template' keyword, as a syntactic disambiguator.
18790 Return TRUE iff it is present, in which case it will be
18794 cp_parser_optional_template_keyword (cp_parser
*parser
)
18796 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
18798 /* The `template' keyword can only be used within templates;
18799 outside templates the parser can always figure out what is a
18800 template and what is not. */
18801 if (!processing_template_decl
)
18803 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18804 error ("%H%<template%> (as a disambiguator) is only allowed "
18805 "within templates", &token
->location
);
18806 /* If this part of the token stream is rescanned, the same
18807 error message would be generated. So, we purge the token
18808 from the stream. */
18809 cp_lexer_purge_token (parser
->lexer
);
18814 /* Consume the `template' keyword. */
18815 cp_lexer_consume_token (parser
->lexer
);
18823 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18824 set PARSER->SCOPE, and perform other related actions. */
18827 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
18830 struct tree_check
*check_value
;
18831 deferred_access_check
*chk
;
18832 VEC (deferred_access_check
,gc
) *checks
;
18834 /* Get the stored value. */
18835 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
18836 /* Perform any access checks that were deferred. */
18837 checks
= check_value
->checks
;
18841 VEC_iterate (deferred_access_check
, checks
, i
, chk
) ;
18844 perform_or_defer_access_check (chk
->binfo
,
18849 /* Set the scope from the stored value. */
18850 parser
->scope
= check_value
->value
;
18851 parser
->qualifying_scope
= check_value
->qualifying_scope
;
18852 parser
->object_scope
= NULL_TREE
;
18855 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18856 encounter the end of a block before what we were looking for. */
18859 cp_parser_cache_group (cp_parser
*parser
,
18860 enum cpp_ttype end
,
18865 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18867 /* Abort a parenthesized expression if we encounter a semicolon. */
18868 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
18869 && token
->type
== CPP_SEMICOLON
)
18871 /* If we've reached the end of the file, stop. */
18872 if (token
->type
== CPP_EOF
18873 || (end
!= CPP_PRAGMA_EOL
18874 && token
->type
== CPP_PRAGMA_EOL
))
18876 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
18877 /* We've hit the end of an enclosing block, so there's been some
18878 kind of syntax error. */
18881 /* Consume the token. */
18882 cp_lexer_consume_token (parser
->lexer
);
18883 /* See if it starts a new group. */
18884 if (token
->type
== CPP_OPEN_BRACE
)
18886 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
18887 /* In theory this should probably check end == '}', but
18888 cp_parser_save_member_function_body needs it to exit
18889 after either '}' or ')' when called with ')'. */
18893 else if (token
->type
== CPP_OPEN_PAREN
)
18895 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
18896 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
18899 else if (token
->type
== CPP_PRAGMA
)
18900 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
18901 else if (token
->type
== end
)
18906 /* Begin parsing tentatively. We always save tokens while parsing
18907 tentatively so that if the tentative parsing fails we can restore the
18911 cp_parser_parse_tentatively (cp_parser
* parser
)
18913 /* Enter a new parsing context. */
18914 parser
->context
= cp_parser_context_new (parser
->context
);
18915 /* Begin saving tokens. */
18916 cp_lexer_save_tokens (parser
->lexer
);
18917 /* In order to avoid repetitive access control error messages,
18918 access checks are queued up until we are no longer parsing
18920 push_deferring_access_checks (dk_deferred
);
18923 /* Commit to the currently active tentative parse. */
18926 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
18928 cp_parser_context
*context
;
18931 /* Mark all of the levels as committed. */
18932 lexer
= parser
->lexer
;
18933 for (context
= parser
->context
; context
->next
; context
= context
->next
)
18935 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
18937 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
18938 while (!cp_lexer_saving_tokens (lexer
))
18939 lexer
= lexer
->next
;
18940 cp_lexer_commit_tokens (lexer
);
18944 /* Abort the currently active tentative parse. All consumed tokens
18945 will be rolled back, and no diagnostics will be issued. */
18948 cp_parser_abort_tentative_parse (cp_parser
* parser
)
18950 cp_parser_simulate_error (parser
);
18951 /* Now, pretend that we want to see if the construct was
18952 successfully parsed. */
18953 cp_parser_parse_definitely (parser
);
18956 /* Stop parsing tentatively. If a parse error has occurred, restore the
18957 token stream. Otherwise, commit to the tokens we have consumed.
18958 Returns true if no error occurred; false otherwise. */
18961 cp_parser_parse_definitely (cp_parser
* parser
)
18963 bool error_occurred
;
18964 cp_parser_context
*context
;
18966 /* Remember whether or not an error occurred, since we are about to
18967 destroy that information. */
18968 error_occurred
= cp_parser_error_occurred (parser
);
18969 /* Remove the topmost context from the stack. */
18970 context
= parser
->context
;
18971 parser
->context
= context
->next
;
18972 /* If no parse errors occurred, commit to the tentative parse. */
18973 if (!error_occurred
)
18975 /* Commit to the tokens read tentatively, unless that was
18977 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
18978 cp_lexer_commit_tokens (parser
->lexer
);
18980 pop_to_parent_deferring_access_checks ();
18982 /* Otherwise, if errors occurred, roll back our state so that things
18983 are just as they were before we began the tentative parse. */
18986 cp_lexer_rollback_tokens (parser
->lexer
);
18987 pop_deferring_access_checks ();
18989 /* Add the context to the front of the free list. */
18990 context
->next
= cp_parser_context_free_list
;
18991 cp_parser_context_free_list
= context
;
18993 return !error_occurred
;
18996 /* Returns true if we are parsing tentatively and are not committed to
18997 this tentative parse. */
19000 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
19002 return (cp_parser_parsing_tentatively (parser
)
19003 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
19006 /* Returns nonzero iff an error has occurred during the most recent
19007 tentative parse. */
19010 cp_parser_error_occurred (cp_parser
* parser
)
19012 return (cp_parser_parsing_tentatively (parser
)
19013 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
19016 /* Returns nonzero if GNU extensions are allowed. */
19019 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
19021 return parser
->allow_gnu_extensions_p
;
19024 /* Objective-C++ Productions */
19027 /* Parse an Objective-C expression, which feeds into a primary-expression
19031 objc-message-expression
19032 objc-string-literal
19033 objc-encode-expression
19034 objc-protocol-expression
19035 objc-selector-expression
19037 Returns a tree representation of the expression. */
19040 cp_parser_objc_expression (cp_parser
* parser
)
19042 /* Try to figure out what kind of declaration is present. */
19043 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
19047 case CPP_OPEN_SQUARE
:
19048 return cp_parser_objc_message_expression (parser
);
19050 case CPP_OBJC_STRING
:
19051 kwd
= cp_lexer_consume_token (parser
->lexer
);
19052 return objc_build_string_object (kwd
->u
.value
);
19055 switch (kwd
->keyword
)
19057 case RID_AT_ENCODE
:
19058 return cp_parser_objc_encode_expression (parser
);
19060 case RID_AT_PROTOCOL
:
19061 return cp_parser_objc_protocol_expression (parser
);
19063 case RID_AT_SELECTOR
:
19064 return cp_parser_objc_selector_expression (parser
);
19070 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19071 &kwd
->location
, kwd
->u
.value
);
19072 cp_parser_skip_to_end_of_block_or_statement (parser
);
19075 return error_mark_node
;
19078 /* Parse an Objective-C message expression.
19080 objc-message-expression:
19081 [ objc-message-receiver objc-message-args ]
19083 Returns a representation of an Objective-C message. */
19086 cp_parser_objc_message_expression (cp_parser
* parser
)
19088 tree receiver
, messageargs
;
19090 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
19091 receiver
= cp_parser_objc_message_receiver (parser
);
19092 messageargs
= cp_parser_objc_message_args (parser
);
19093 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
19095 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
19098 /* Parse an objc-message-receiver.
19100 objc-message-receiver:
19102 simple-type-specifier
19104 Returns a representation of the type or expression. */
19107 cp_parser_objc_message_receiver (cp_parser
* parser
)
19111 /* An Objective-C message receiver may be either (1) a type
19112 or (2) an expression. */
19113 cp_parser_parse_tentatively (parser
);
19114 rcv
= cp_parser_expression (parser
, false, NULL
);
19116 if (cp_parser_parse_definitely (parser
))
19119 rcv
= cp_parser_simple_type_specifier (parser
,
19120 /*decl_specs=*/NULL
,
19121 CP_PARSER_FLAGS_NONE
);
19123 return objc_get_class_reference (rcv
);
19126 /* Parse the arguments and selectors comprising an Objective-C message.
19131 objc-selector-args , objc-comma-args
19133 objc-selector-args:
19134 objc-selector [opt] : assignment-expression
19135 objc-selector-args objc-selector [opt] : assignment-expression
19138 assignment-expression
19139 objc-comma-args , assignment-expression
19141 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19142 selector arguments and TREE_VALUE containing a list of comma
19146 cp_parser_objc_message_args (cp_parser
* parser
)
19148 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
19149 bool maybe_unary_selector_p
= true;
19150 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19152 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
19154 tree selector
= NULL_TREE
, arg
;
19156 if (token
->type
!= CPP_COLON
)
19157 selector
= cp_parser_objc_selector (parser
);
19159 /* Detect if we have a unary selector. */
19160 if (maybe_unary_selector_p
19161 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
19162 return build_tree_list (selector
, NULL_TREE
);
19164 maybe_unary_selector_p
= false;
19165 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
19166 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
19169 = chainon (sel_args
,
19170 build_tree_list (selector
, arg
));
19172 token
= cp_lexer_peek_token (parser
->lexer
);
19175 /* Handle non-selector arguments, if any. */
19176 while (token
->type
== CPP_COMMA
)
19180 cp_lexer_consume_token (parser
->lexer
);
19181 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
19184 = chainon (addl_args
,
19185 build_tree_list (NULL_TREE
, arg
));
19187 token
= cp_lexer_peek_token (parser
->lexer
);
19190 return build_tree_list (sel_args
, addl_args
);
19193 /* Parse an Objective-C encode expression.
19195 objc-encode-expression:
19196 @encode objc-typename
19198 Returns an encoded representation of the type argument. */
19201 cp_parser_objc_encode_expression (cp_parser
* parser
)
19206 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
19207 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
19208 token
= cp_lexer_peek_token (parser
->lexer
);
19209 type
= complete_type (cp_parser_type_id (parser
));
19210 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
19214 error ("%H%<@encode%> must specify a type as an argument",
19216 return error_mark_node
;
19219 return objc_build_encode_expr (type
);
19222 /* Parse an Objective-C @defs expression. */
19225 cp_parser_objc_defs_expression (cp_parser
*parser
)
19229 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
19230 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
19231 name
= cp_parser_identifier (parser
);
19232 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
19234 return objc_get_class_ivars (name
);
19237 /* Parse an Objective-C protocol expression.
19239 objc-protocol-expression:
19240 @protocol ( identifier )
19242 Returns a representation of the protocol expression. */
19245 cp_parser_objc_protocol_expression (cp_parser
* parser
)
19249 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
19250 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
19251 proto
= cp_parser_identifier (parser
);
19252 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
19254 return objc_build_protocol_expr (proto
);
19257 /* Parse an Objective-C selector expression.
19259 objc-selector-expression:
19260 @selector ( objc-method-signature )
19262 objc-method-signature:
19268 objc-selector-seq objc-selector :
19270 Returns a representation of the method selector. */
19273 cp_parser_objc_selector_expression (cp_parser
* parser
)
19275 tree sel_seq
= NULL_TREE
;
19276 bool maybe_unary_selector_p
= true;
19279 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
19280 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
19281 token
= cp_lexer_peek_token (parser
->lexer
);
19283 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
19284 || token
->type
== CPP_SCOPE
)
19286 tree selector
= NULL_TREE
;
19288 if (token
->type
!= CPP_COLON
19289 || token
->type
== CPP_SCOPE
)
19290 selector
= cp_parser_objc_selector (parser
);
19292 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
19293 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19295 /* Detect if we have a unary selector. */
19296 if (maybe_unary_selector_p
)
19298 sel_seq
= selector
;
19299 goto finish_selector
;
19303 cp_parser_error (parser
, "expected %<:%>");
19306 maybe_unary_selector_p
= false;
19307 token
= cp_lexer_consume_token (parser
->lexer
);
19309 if (token
->type
== CPP_SCOPE
)
19312 = chainon (sel_seq
,
19313 build_tree_list (selector
, NULL_TREE
));
19315 = chainon (sel_seq
,
19316 build_tree_list (NULL_TREE
, NULL_TREE
));
19320 = chainon (sel_seq
,
19321 build_tree_list (selector
, NULL_TREE
));
19323 token
= cp_lexer_peek_token (parser
->lexer
);
19327 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
19329 return objc_build_selector_expr (sel_seq
);
19332 /* Parse a list of identifiers.
19334 objc-identifier-list:
19336 objc-identifier-list , identifier
19338 Returns a TREE_LIST of identifier nodes. */
19341 cp_parser_objc_identifier_list (cp_parser
* parser
)
19343 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
19344 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
19346 while (sep
->type
== CPP_COMMA
)
19348 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
19349 list
= chainon (list
,
19350 build_tree_list (NULL_TREE
,
19351 cp_parser_identifier (parser
)));
19352 sep
= cp_lexer_peek_token (parser
->lexer
);
19358 /* Parse an Objective-C alias declaration.
19360 objc-alias-declaration:
19361 @compatibility_alias identifier identifier ;
19363 This function registers the alias mapping with the Objective-C front end.
19364 It returns nothing. */
19367 cp_parser_objc_alias_declaration (cp_parser
* parser
)
19371 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
19372 alias
= cp_parser_identifier (parser
);
19373 orig
= cp_parser_identifier (parser
);
19374 objc_declare_alias (alias
, orig
);
19375 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19378 /* Parse an Objective-C class forward-declaration.
19380 objc-class-declaration:
19381 @class objc-identifier-list ;
19383 The function registers the forward declarations with the Objective-C
19384 front end. It returns nothing. */
19387 cp_parser_objc_class_declaration (cp_parser
* parser
)
19389 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
19390 objc_declare_class (cp_parser_objc_identifier_list (parser
));
19391 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19394 /* Parse a list of Objective-C protocol references.
19396 objc-protocol-refs-opt:
19397 objc-protocol-refs [opt]
19399 objc-protocol-refs:
19400 < objc-identifier-list >
19402 Returns a TREE_LIST of identifiers, if any. */
19405 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
19407 tree protorefs
= NULL_TREE
;
19409 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
19411 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
19412 protorefs
= cp_parser_objc_identifier_list (parser
);
19413 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
19419 /* Parse a Objective-C visibility specification. */
19422 cp_parser_objc_visibility_spec (cp_parser
* parser
)
19424 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
19426 switch (vis
->keyword
)
19428 case RID_AT_PRIVATE
:
19429 objc_set_visibility (2);
19431 case RID_AT_PROTECTED
:
19432 objc_set_visibility (0);
19434 case RID_AT_PUBLIC
:
19435 objc_set_visibility (1);
19441 /* Eat '@private'/'@protected'/'@public'. */
19442 cp_lexer_consume_token (parser
->lexer
);
19445 /* Parse an Objective-C method type. */
19448 cp_parser_objc_method_type (cp_parser
* parser
)
19450 objc_set_method_type
19451 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
19456 /* Parse an Objective-C protocol qualifier. */
19459 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
19461 tree quals
= NULL_TREE
, node
;
19462 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19464 node
= token
->u
.value
;
19466 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
19467 && (node
== ridpointers
[(int) RID_IN
]
19468 || node
== ridpointers
[(int) RID_OUT
]
19469 || node
== ridpointers
[(int) RID_INOUT
]
19470 || node
== ridpointers
[(int) RID_BYCOPY
]
19471 || node
== ridpointers
[(int) RID_BYREF
]
19472 || node
== ridpointers
[(int) RID_ONEWAY
]))
19474 quals
= tree_cons (NULL_TREE
, node
, quals
);
19475 cp_lexer_consume_token (parser
->lexer
);
19476 token
= cp_lexer_peek_token (parser
->lexer
);
19477 node
= token
->u
.value
;
19483 /* Parse an Objective-C typename. */
19486 cp_parser_objc_typename (cp_parser
* parser
)
19488 tree type_name
= NULL_TREE
;
19490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19492 tree proto_quals
, cp_type
= NULL_TREE
;
19494 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
19495 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
19497 /* An ObjC type name may consist of just protocol qualifiers, in which
19498 case the type shall default to 'id'. */
19499 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
19500 cp_type
= cp_parser_type_id (parser
);
19502 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
19503 type_name
= build_tree_list (proto_quals
, cp_type
);
19509 /* Check to see if TYPE refers to an Objective-C selector name. */
19512 cp_parser_objc_selector_p (enum cpp_ttype type
)
19514 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
19515 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
19516 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
19517 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
19518 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
19521 /* Parse an Objective-C selector. */
19524 cp_parser_objc_selector (cp_parser
* parser
)
19526 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
19528 if (!cp_parser_objc_selector_p (token
->type
))
19530 error ("%Hinvalid Objective-C++ selector name", &token
->location
);
19531 return error_mark_node
;
19534 /* C++ operator names are allowed to appear in ObjC selectors. */
19535 switch (token
->type
)
19537 case CPP_AND_AND
: return get_identifier ("and");
19538 case CPP_AND_EQ
: return get_identifier ("and_eq");
19539 case CPP_AND
: return get_identifier ("bitand");
19540 case CPP_OR
: return get_identifier ("bitor");
19541 case CPP_COMPL
: return get_identifier ("compl");
19542 case CPP_NOT
: return get_identifier ("not");
19543 case CPP_NOT_EQ
: return get_identifier ("not_eq");
19544 case CPP_OR_OR
: return get_identifier ("or");
19545 case CPP_OR_EQ
: return get_identifier ("or_eq");
19546 case CPP_XOR
: return get_identifier ("xor");
19547 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
19548 default: return token
->u
.value
;
19552 /* Parse an Objective-C params list. */
19555 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
19557 tree params
= NULL_TREE
;
19558 bool maybe_unary_selector_p
= true;
19559 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19561 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
19563 tree selector
= NULL_TREE
, type_name
, identifier
;
19565 if (token
->type
!= CPP_COLON
)
19566 selector
= cp_parser_objc_selector (parser
);
19568 /* Detect if we have a unary selector. */
19569 if (maybe_unary_selector_p
19570 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
19573 maybe_unary_selector_p
= false;
19574 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
19575 type_name
= cp_parser_objc_typename (parser
);
19576 identifier
= cp_parser_identifier (parser
);
19580 objc_build_keyword_decl (selector
,
19584 token
= cp_lexer_peek_token (parser
->lexer
);
19590 /* Parse the non-keyword Objective-C params. */
19593 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
19595 tree params
= make_node (TREE_LIST
);
19596 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19597 *ellipsisp
= false; /* Initially, assume no ellipsis. */
19599 while (token
->type
== CPP_COMMA
)
19601 cp_parameter_declarator
*parmdecl
;
19604 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
19605 token
= cp_lexer_peek_token (parser
->lexer
);
19607 if (token
->type
== CPP_ELLIPSIS
)
19609 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
19614 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
19615 parm
= grokdeclarator (parmdecl
->declarator
,
19616 &parmdecl
->decl_specifiers
,
19617 PARM
, /*initialized=*/0,
19618 /*attrlist=*/NULL
);
19620 chainon (params
, build_tree_list (NULL_TREE
, parm
));
19621 token
= cp_lexer_peek_token (parser
->lexer
);
19627 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19630 cp_parser_objc_interstitial_code (cp_parser
* parser
)
19632 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19634 /* If the next token is `extern' and the following token is a string
19635 literal, then we have a linkage specification. */
19636 if (token
->keyword
== RID_EXTERN
19637 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
19638 cp_parser_linkage_specification (parser
);
19639 /* Handle #pragma, if any. */
19640 else if (token
->type
== CPP_PRAGMA
)
19641 cp_parser_pragma (parser
, pragma_external
);
19642 /* Allow stray semicolons. */
19643 else if (token
->type
== CPP_SEMICOLON
)
19644 cp_lexer_consume_token (parser
->lexer
);
19645 /* Finally, try to parse a block-declaration, or a function-definition. */
19647 cp_parser_block_declaration (parser
, /*statement_p=*/false);
19650 /* Parse a method signature. */
19653 cp_parser_objc_method_signature (cp_parser
* parser
)
19655 tree rettype
, kwdparms
, optparms
;
19656 bool ellipsis
= false;
19658 cp_parser_objc_method_type (parser
);
19659 rettype
= cp_parser_objc_typename (parser
);
19660 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
19661 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
19663 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
19666 /* Pars an Objective-C method prototype list. */
19669 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
19671 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19673 while (token
->keyword
!= RID_AT_END
)
19675 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
19677 objc_add_method_declaration
19678 (cp_parser_objc_method_signature (parser
));
19679 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19682 /* Allow for interspersed non-ObjC++ code. */
19683 cp_parser_objc_interstitial_code (parser
);
19685 token
= cp_lexer_peek_token (parser
->lexer
);
19688 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
19689 objc_finish_interface ();
19692 /* Parse an Objective-C method definition list. */
19695 cp_parser_objc_method_definition_list (cp_parser
* parser
)
19697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19699 while (token
->keyword
!= RID_AT_END
)
19703 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
19705 push_deferring_access_checks (dk_deferred
);
19706 objc_start_method_definition
19707 (cp_parser_objc_method_signature (parser
));
19709 /* For historical reasons, we accept an optional semicolon. */
19710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19711 cp_lexer_consume_token (parser
->lexer
);
19713 perform_deferred_access_checks ();
19714 stop_deferring_access_checks ();
19715 meth
= cp_parser_function_definition_after_declarator (parser
,
19717 pop_deferring_access_checks ();
19718 objc_finish_method_definition (meth
);
19721 /* Allow for interspersed non-ObjC++ code. */
19722 cp_parser_objc_interstitial_code (parser
);
19724 token
= cp_lexer_peek_token (parser
->lexer
);
19727 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
19728 objc_finish_implementation ();
19731 /* Parse Objective-C ivars. */
19734 cp_parser_objc_class_ivars (cp_parser
* parser
)
19736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19738 if (token
->type
!= CPP_OPEN_BRACE
)
19739 return; /* No ivars specified. */
19741 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
19742 token
= cp_lexer_peek_token (parser
->lexer
);
19744 while (token
->type
!= CPP_CLOSE_BRACE
)
19746 cp_decl_specifier_seq declspecs
;
19747 int decl_class_or_enum_p
;
19748 tree prefix_attributes
;
19750 cp_parser_objc_visibility_spec (parser
);
19752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19755 cp_parser_decl_specifier_seq (parser
,
19756 CP_PARSER_FLAGS_OPTIONAL
,
19758 &decl_class_or_enum_p
);
19759 prefix_attributes
= declspecs
.attributes
;
19760 declspecs
.attributes
= NULL_TREE
;
19762 /* Keep going until we hit the `;' at the end of the
19764 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19766 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
19767 cp_declarator
*declarator
= NULL
;
19768 int ctor_dtor_or_conv_p
;
19770 /* Check for a (possibly unnamed) bitfield declaration. */
19771 token
= cp_lexer_peek_token (parser
->lexer
);
19772 if (token
->type
== CPP_COLON
)
19775 if (token
->type
== CPP_NAME
19776 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
19779 /* Get the name of the bitfield. */
19780 declarator
= make_id_declarator (NULL_TREE
,
19781 cp_parser_identifier (parser
),
19785 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
19786 /* Get the width of the bitfield. */
19788 = cp_parser_constant_expression (parser
,
19789 /*allow_non_constant=*/false,
19794 /* Parse the declarator. */
19796 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19797 &ctor_dtor_or_conv_p
,
19798 /*parenthesized_p=*/NULL
,
19799 /*member_p=*/false);
19802 /* Look for attributes that apply to the ivar. */
19803 attributes
= cp_parser_attributes_opt (parser
);
19804 /* Remember which attributes are prefix attributes and
19806 first_attribute
= attributes
;
19807 /* Combine the attributes. */
19808 attributes
= chainon (prefix_attributes
, attributes
);
19811 /* Create the bitfield declaration. */
19812 decl
= grokbitfield (declarator
, &declspecs
,
19816 decl
= grokfield (declarator
, &declspecs
,
19817 NULL_TREE
, /*init_const_expr_p=*/false,
19818 NULL_TREE
, attributes
);
19820 /* Add the instance variable. */
19821 objc_add_instance_variable (decl
);
19823 /* Reset PREFIX_ATTRIBUTES. */
19824 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
19825 attributes
= TREE_CHAIN (attributes
);
19827 TREE_CHAIN (attributes
) = NULL_TREE
;
19829 token
= cp_lexer_peek_token (parser
->lexer
);
19831 if (token
->type
== CPP_COMMA
)
19833 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
19839 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19840 token
= cp_lexer_peek_token (parser
->lexer
);
19843 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
19844 /* For historical reasons, we accept an optional semicolon. */
19845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19846 cp_lexer_consume_token (parser
->lexer
);
19849 /* Parse an Objective-C protocol declaration. */
19852 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
19854 tree proto
, protorefs
;
19857 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
19858 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
19860 tok
= cp_lexer_peek_token (parser
->lexer
);
19861 error ("%Hidentifier expected after %<@protocol%>", &tok
->location
);
19865 /* See if we have a forward declaration or a definition. */
19866 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19868 /* Try a forward declaration first. */
19869 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
19871 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
19873 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19876 /* Ok, we got a full-fledged definition (or at least should). */
19879 proto
= cp_parser_identifier (parser
);
19880 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
19881 objc_start_protocol (proto
, protorefs
);
19882 cp_parser_objc_method_prototype_list (parser
);
19886 /* Parse an Objective-C superclass or category. */
19889 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
19892 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
19894 *super
= *categ
= NULL_TREE
;
19895 if (next
->type
== CPP_COLON
)
19897 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
19898 *super
= cp_parser_identifier (parser
);
19900 else if (next
->type
== CPP_OPEN_PAREN
)
19902 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
19903 *categ
= cp_parser_identifier (parser
);
19904 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
19908 /* Parse an Objective-C class interface. */
19911 cp_parser_objc_class_interface (cp_parser
* parser
)
19913 tree name
, super
, categ
, protos
;
19915 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
19916 name
= cp_parser_identifier (parser
);
19917 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
19918 protos
= cp_parser_objc_protocol_refs_opt (parser
);
19920 /* We have either a class or a category on our hands. */
19922 objc_start_category_interface (name
, categ
, protos
);
19925 objc_start_class_interface (name
, super
, protos
);
19926 /* Handle instance variable declarations, if any. */
19927 cp_parser_objc_class_ivars (parser
);
19928 objc_continue_interface ();
19931 cp_parser_objc_method_prototype_list (parser
);
19934 /* Parse an Objective-C class implementation. */
19937 cp_parser_objc_class_implementation (cp_parser
* parser
)
19939 tree name
, super
, categ
;
19941 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
19942 name
= cp_parser_identifier (parser
);
19943 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
19945 /* We have either a class or a category on our hands. */
19947 objc_start_category_implementation (name
, categ
);
19950 objc_start_class_implementation (name
, super
);
19951 /* Handle instance variable declarations, if any. */
19952 cp_parser_objc_class_ivars (parser
);
19953 objc_continue_implementation ();
19956 cp_parser_objc_method_definition_list (parser
);
19959 /* Consume the @end token and finish off the implementation. */
19962 cp_parser_objc_end_implementation (cp_parser
* parser
)
19964 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
19965 objc_finish_implementation ();
19968 /* Parse an Objective-C declaration. */
19971 cp_parser_objc_declaration (cp_parser
* parser
)
19973 /* Try to figure out what kind of declaration is present. */
19974 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
19976 switch (kwd
->keyword
)
19979 cp_parser_objc_alias_declaration (parser
);
19982 cp_parser_objc_class_declaration (parser
);
19984 case RID_AT_PROTOCOL
:
19985 cp_parser_objc_protocol_declaration (parser
);
19987 case RID_AT_INTERFACE
:
19988 cp_parser_objc_class_interface (parser
);
19990 case RID_AT_IMPLEMENTATION
:
19991 cp_parser_objc_class_implementation (parser
);
19994 cp_parser_objc_end_implementation (parser
);
19997 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19998 &kwd
->location
, kwd
->u
.value
);
19999 cp_parser_skip_to_end_of_block_or_statement (parser
);
20003 /* Parse an Objective-C try-catch-finally statement.
20005 objc-try-catch-finally-stmt:
20006 @try compound-statement objc-catch-clause-seq [opt]
20007 objc-finally-clause [opt]
20009 objc-catch-clause-seq:
20010 objc-catch-clause objc-catch-clause-seq [opt]
20013 @catch ( exception-declaration ) compound-statement
20015 objc-finally-clause
20016 @finally compound-statement
20018 Returns NULL_TREE. */
20021 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
20022 location_t location
;
20025 cp_parser_require_keyword (parser
, RID_AT_TRY
, "%<@try%>");
20026 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
20027 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20028 node, lest it get absorbed into the surrounding block. */
20029 stmt
= push_stmt_list ();
20030 cp_parser_compound_statement (parser
, NULL
, false);
20031 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
20033 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
20035 cp_parameter_declarator
*parmdecl
;
20038 cp_lexer_consume_token (parser
->lexer
);
20039 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
20040 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
20041 parm
= grokdeclarator (parmdecl
->declarator
,
20042 &parmdecl
->decl_specifiers
,
20043 PARM
, /*initialized=*/0,
20044 /*attrlist=*/NULL
);
20045 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20046 objc_begin_catch_clause (parm
);
20047 cp_parser_compound_statement (parser
, NULL
, false);
20048 objc_finish_catch_clause ();
20051 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
20053 cp_lexer_consume_token (parser
->lexer
);
20054 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
20055 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20056 node, lest it get absorbed into the surrounding block. */
20057 stmt
= push_stmt_list ();
20058 cp_parser_compound_statement (parser
, NULL
, false);
20059 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
20062 return objc_finish_try_stmt ();
20065 /* Parse an Objective-C synchronized statement.
20067 objc-synchronized-stmt:
20068 @synchronized ( expression ) compound-statement
20070 Returns NULL_TREE. */
20073 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
20074 location_t location
;
20077 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "%<@synchronized%>");
20079 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
20080 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
20081 lock
= cp_parser_expression (parser
, false, NULL
);
20082 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20084 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20085 node, lest it get absorbed into the surrounding block. */
20086 stmt
= push_stmt_list ();
20087 cp_parser_compound_statement (parser
, NULL
, false);
20089 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
20092 /* Parse an Objective-C throw statement.
20095 @throw assignment-expression [opt] ;
20097 Returns a constructed '@throw' statement. */
20100 cp_parser_objc_throw_statement (cp_parser
*parser
) {
20101 tree expr
= NULL_TREE
;
20103 cp_parser_require_keyword (parser
, RID_AT_THROW
, "%<@throw%>");
20105 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20106 expr
= cp_parser_assignment_expression (parser
, false, NULL
);
20108 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20110 return objc_build_throw_stmt (expr
);
20113 /* Parse an Objective-C statement. */
20116 cp_parser_objc_statement (cp_parser
* parser
) {
20117 /* Try to figure out what kind of declaration is present. */
20118 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
20120 switch (kwd
->keyword
)
20123 return cp_parser_objc_try_catch_finally_statement (parser
);
20124 case RID_AT_SYNCHRONIZED
:
20125 return cp_parser_objc_synchronized_statement (parser
);
20127 return cp_parser_objc_throw_statement (parser
);
20129 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20130 &kwd
->location
, kwd
->u
.value
);
20131 cp_parser_skip_to_end_of_block_or_statement (parser
);
20134 return error_mark_node
;
20137 /* OpenMP 2.5 parsing routines. */
20139 /* Returns name of the next clause.
20140 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20141 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20142 returned and the token is consumed. */
20144 static pragma_omp_clause
20145 cp_parser_omp_clause_name (cp_parser
*parser
)
20147 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
20149 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
20150 result
= PRAGMA_OMP_CLAUSE_IF
;
20151 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
20152 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
20153 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
20154 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
20155 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20157 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
20158 const char *p
= IDENTIFIER_POINTER (id
);
20163 if (!strcmp ("collapse", p
))
20164 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
20165 else if (!strcmp ("copyin", p
))
20166 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
20167 else if (!strcmp ("copyprivate", p
))
20168 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
20171 if (!strcmp ("firstprivate", p
))
20172 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
20175 if (!strcmp ("lastprivate", p
))
20176 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
20179 if (!strcmp ("nowait", p
))
20180 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
20181 else if (!strcmp ("num_threads", p
))
20182 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
20185 if (!strcmp ("ordered", p
))
20186 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
20189 if (!strcmp ("reduction", p
))
20190 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
20193 if (!strcmp ("schedule", p
))
20194 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
20195 else if (!strcmp ("shared", p
))
20196 result
= PRAGMA_OMP_CLAUSE_SHARED
;
20199 if (!strcmp ("untied", p
))
20200 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
20205 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
20206 cp_lexer_consume_token (parser
->lexer
);
20211 /* Validate that a clause of the given type does not already exist. */
20214 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
20215 const char *name
, location_t location
)
20219 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
20220 if (OMP_CLAUSE_CODE (c
) == code
)
20222 error ("%Htoo many %qs clauses", &location
, name
);
20230 variable-list , identifier
20232 In addition, we match a closing parenthesis. An opening parenthesis
20233 will have been consumed by the caller.
20235 If KIND is nonzero, create the appropriate node and install the decl
20236 in OMP_CLAUSE_DECL and add the node to the head of the list.
20238 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20239 return the list created. */
20242 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
20250 token
= cp_lexer_peek_token (parser
->lexer
);
20251 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
20252 /*check_dependency_p=*/true,
20253 /*template_p=*/NULL
,
20254 /*declarator_p=*/false,
20255 /*optional_p=*/false);
20256 if (name
== error_mark_node
)
20259 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
20260 if (decl
== error_mark_node
)
20261 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
, token
->location
);
20262 else if (kind
!= 0)
20264 tree u
= build_omp_clause (kind
);
20265 OMP_CLAUSE_DECL (u
) = decl
;
20266 OMP_CLAUSE_CHAIN (u
) = list
;
20270 list
= tree_cons (decl
, NULL_TREE
, list
);
20273 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20275 cp_lexer_consume_token (parser
->lexer
);
20278 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
20282 /* Try to resync to an unnested comma. Copied from
20283 cp_parser_parenthesized_expression_list. */
20285 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
20286 /*recovering=*/true,
20288 /*consume_paren=*/true);
20296 /* Similarly, but expect leading and trailing parenthesis. This is a very
20297 common case for omp clauses. */
20300 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
20302 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
20303 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
20308 collapse ( constant-expression ) */
20311 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
20317 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20318 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
20321 num
= cp_parser_constant_expression (parser
, false, NULL
);
20323 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
20324 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20325 /*or_comma=*/false,
20326 /*consume_paren=*/true);
20328 if (num
== error_mark_node
)
20330 num
= fold_non_dependent_expr (num
);
20331 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
20332 || !host_integerp (num
, 0)
20333 || (n
= tree_low_cst (num
, 0)) <= 0
20336 error ("%Hcollapse argument needs positive constant integer expression",
20341 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
20342 c
= build_omp_clause (OMP_CLAUSE_COLLAPSE
);
20343 OMP_CLAUSE_CHAIN (c
) = list
;
20344 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
20350 default ( shared | none ) */
20353 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
20355 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
20358 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
20360 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
);
20368 if (strcmp ("none", p
) != 0)
20370 kind
= OMP_CLAUSE_DEFAULT_NONE
;
20374 if (strcmp ("shared", p
) != 0)
20376 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
20383 cp_lexer_consume_token (parser
->lexer
);
20388 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
20391 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
20392 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20393 /*or_comma=*/false,
20394 /*consume_paren=*/true);
20396 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
20399 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
20400 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
20401 OMP_CLAUSE_CHAIN (c
) = list
;
20402 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
20408 if ( expression ) */
20411 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
20415 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
20418 t
= cp_parser_condition (parser
);
20420 if (t
== error_mark_node
20421 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
20422 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20423 /*or_comma=*/false,
20424 /*consume_paren=*/true);
20426 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
20428 c
= build_omp_clause (OMP_CLAUSE_IF
);
20429 OMP_CLAUSE_IF_EXPR (c
) = t
;
20430 OMP_CLAUSE_CHAIN (c
) = list
;
20439 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
,
20440 tree list
, location_t location
)
20444 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
20446 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
20447 OMP_CLAUSE_CHAIN (c
) = list
;
20452 num_threads ( expression ) */
20455 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
20456 location_t location
)
20460 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
20463 t
= cp_parser_expression (parser
, false, NULL
);
20465 if (t
== error_mark_node
20466 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
20467 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20468 /*or_comma=*/false,
20469 /*consume_paren=*/true);
20471 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
20472 "num_threads", location
);
20474 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
20475 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
20476 OMP_CLAUSE_CHAIN (c
) = list
;
20485 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
,
20486 tree list
, location_t location
)
20490 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
20491 "ordered", location
);
20493 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
20494 OMP_CLAUSE_CHAIN (c
) = list
;
20499 reduction ( reduction-operator : variable-list )
20501 reduction-operator:
20502 One of: + * - & ^ | && || */
20505 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
20507 enum tree_code code
;
20510 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
20513 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
20525 code
= BIT_AND_EXPR
;
20528 code
= BIT_XOR_EXPR
;
20531 code
= BIT_IOR_EXPR
;
20534 code
= TRUTH_ANDIF_EXPR
;
20537 code
= TRUTH_ORIF_EXPR
;
20540 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20541 "%<|%>, %<&&%>, or %<||%>");
20543 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20544 /*or_comma=*/false,
20545 /*consume_paren=*/true);
20548 cp_lexer_consume_token (parser
->lexer
);
20550 if (!cp_parser_require (parser
, CPP_COLON
, "%<:%>"))
20553 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
20554 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
20555 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
20561 schedule ( schedule-kind )
20562 schedule ( schedule-kind , expression )
20565 static | dynamic | guided | runtime | auto */
20568 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
20572 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
20575 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
20577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20579 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
20580 const char *p
= IDENTIFIER_POINTER (id
);
20585 if (strcmp ("dynamic", p
) != 0)
20587 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
20591 if (strcmp ("guided", p
) != 0)
20593 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
20597 if (strcmp ("runtime", p
) != 0)
20599 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
20606 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
20607 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
20608 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
20609 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
20612 cp_lexer_consume_token (parser
->lexer
);
20614 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20617 cp_lexer_consume_token (parser
->lexer
);
20619 token
= cp_lexer_peek_token (parser
->lexer
);
20620 t
= cp_parser_assignment_expression (parser
, false, NULL
);
20622 if (t
== error_mark_node
)
20624 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
20625 error ("%Hschedule %<runtime%> does not take "
20626 "a %<chunk_size%> parameter", &token
->location
);
20627 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
20628 error ("%Hschedule %<auto%> does not take "
20629 "a %<chunk_size%> parameter", &token
->location
);
20631 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
20633 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
20636 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<,%> or %<)%>"))
20639 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
20640 OMP_CLAUSE_CHAIN (c
) = list
;
20644 cp_parser_error (parser
, "invalid schedule kind");
20646 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20647 /*or_comma=*/false,
20648 /*consume_paren=*/true);
20656 cp_parser_omp_clause_untied (cp_parser
*parser ATTRIBUTE_UNUSED
,
20657 tree list
, location_t location
)
20661 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
20663 c
= build_omp_clause (OMP_CLAUSE_UNTIED
);
20664 OMP_CLAUSE_CHAIN (c
) = list
;
20668 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20669 is a bitmask in MASK. Return the list of clauses found; the result
20670 of clause default goes in *pdefault. */
20673 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
20674 const char *where
, cp_token
*pragma_tok
)
20676 tree clauses
= NULL
;
20678 cp_token
*token
= NULL
;
20680 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
20682 pragma_omp_clause c_kind
;
20683 const char *c_name
;
20684 tree prev
= clauses
;
20686 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20687 cp_lexer_consume_token (parser
->lexer
);
20689 token
= cp_lexer_peek_token (parser
->lexer
);
20690 c_kind
= cp_parser_omp_clause_name (parser
);
20695 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
20696 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
20698 c_name
= "collapse";
20700 case PRAGMA_OMP_CLAUSE_COPYIN
:
20701 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
20704 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
20705 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
20707 c_name
= "copyprivate";
20709 case PRAGMA_OMP_CLAUSE_DEFAULT
:
20710 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
20712 c_name
= "default";
20714 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
20715 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
20717 c_name
= "firstprivate";
20719 case PRAGMA_OMP_CLAUSE_IF
:
20720 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
20723 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
20724 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
20726 c_name
= "lastprivate";
20728 case PRAGMA_OMP_CLAUSE_NOWAIT
:
20729 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
20732 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
20733 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
20735 c_name
= "num_threads";
20737 case PRAGMA_OMP_CLAUSE_ORDERED
:
20738 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
20740 c_name
= "ordered";
20742 case PRAGMA_OMP_CLAUSE_PRIVATE
:
20743 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
20745 c_name
= "private";
20747 case PRAGMA_OMP_CLAUSE_REDUCTION
:
20748 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
20749 c_name
= "reduction";
20751 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
20752 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
20754 c_name
= "schedule";
20756 case PRAGMA_OMP_CLAUSE_SHARED
:
20757 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
20761 case PRAGMA_OMP_CLAUSE_UNTIED
:
20762 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
20767 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
20771 if (((mask
>> c_kind
) & 1) == 0)
20773 /* Remove the invalid clause(s) from the list to avoid
20774 confusing the rest of the compiler. */
20776 error ("%H%qs is not valid for %qs", &token
->location
, c_name
, where
);
20780 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
20781 return finish_omp_clauses (clauses
);
20788 In practice, we're also interested in adding the statement to an
20789 outer node. So it is convenient if we work around the fact that
20790 cp_parser_statement calls add_stmt. */
20793 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
20795 unsigned save
= parser
->in_statement
;
20797 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20798 This preserves the "not within loop or switch" style error messages
20799 for nonsense cases like
20805 if (parser
->in_statement
)
20806 parser
->in_statement
= IN_OMP_BLOCK
;
20812 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
20814 parser
->in_statement
= save
;
20818 cp_parser_omp_structured_block (cp_parser
*parser
)
20820 tree stmt
= begin_omp_structured_block ();
20821 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
20823 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
20825 cp_parser_end_omp_structured_block (parser
, save
);
20826 return finish_omp_structured_block (stmt
);
20830 # pragma omp atomic new-line
20834 x binop= expr | x++ | ++x | x-- | --x
20836 +, *, -, /, &, ^, |, <<, >>
20838 where x is an lvalue expression with scalar type. */
20841 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
20844 enum tree_code code
;
20846 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20848 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
20849 /*cast_p=*/false, NULL
);
20850 switch (TREE_CODE (lhs
))
20855 case PREINCREMENT_EXPR
:
20856 case POSTINCREMENT_EXPR
:
20857 lhs
= TREE_OPERAND (lhs
, 0);
20859 rhs
= integer_one_node
;
20862 case PREDECREMENT_EXPR
:
20863 case POSTDECREMENT_EXPR
:
20864 lhs
= TREE_OPERAND (lhs
, 0);
20866 rhs
= integer_one_node
;
20870 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
20876 code
= TRUNC_DIV_EXPR
;
20884 case CPP_LSHIFT_EQ
:
20885 code
= LSHIFT_EXPR
;
20887 case CPP_RSHIFT_EQ
:
20888 code
= RSHIFT_EXPR
;
20891 code
= BIT_AND_EXPR
;
20894 code
= BIT_IOR_EXPR
;
20897 code
= BIT_XOR_EXPR
;
20900 cp_parser_error (parser
,
20901 "invalid operator for %<#pragma omp atomic%>");
20904 cp_lexer_consume_token (parser
->lexer
);
20906 rhs
= cp_parser_expression (parser
, false, NULL
);
20907 if (rhs
== error_mark_node
)
20911 finish_omp_atomic (code
, lhs
, rhs
);
20912 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20916 cp_parser_skip_to_end_of_block_or_statement (parser
);
20921 # pragma omp barrier new-line */
20924 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
20926 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20927 finish_omp_barrier ();
20931 # pragma omp critical [(name)] new-line
20932 structured-block */
20935 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
20937 tree stmt
, name
= NULL
;
20939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20941 cp_lexer_consume_token (parser
->lexer
);
20943 name
= cp_parser_identifier (parser
);
20945 if (name
== error_mark_node
20946 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
20947 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20948 /*or_comma=*/false,
20949 /*consume_paren=*/true);
20950 if (name
== error_mark_node
)
20953 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20955 stmt
= cp_parser_omp_structured_block (parser
);
20956 return c_finish_omp_critical (stmt
, name
);
20960 # pragma omp flush flush-vars[opt] new-line
20963 ( variable-list ) */
20966 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
20968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20969 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
20970 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20972 finish_omp_flush ();
20975 /* Helper function, to parse omp for increment expression. */
20978 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
20980 tree lhs
= cp_parser_cast_expression (parser
, false, false, NULL
), rhs
;
20986 cp_parser_skip_to_end_of_statement (parser
);
20987 return error_mark_node
;
20990 token
= cp_lexer_peek_token (parser
->lexer
);
20991 op
= binops_by_token
[token
->type
].tree_type
;
21000 cp_parser_skip_to_end_of_statement (parser
);
21001 return error_mark_node
;
21004 cp_lexer_consume_token (parser
->lexer
);
21005 rhs
= cp_parser_binary_expression (parser
, false,
21006 PREC_RELATIONAL_EXPRESSION
, NULL
);
21007 if (rhs
== error_mark_node
21008 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21010 cp_parser_skip_to_end_of_statement (parser
);
21011 return error_mark_node
;
21014 return build2 (op
, boolean_type_node
, lhs
, rhs
);
21017 /* Helper function, to parse omp for increment expression. */
21020 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
21022 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21028 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
21030 op
= (token
->type
== CPP_PLUS_PLUS
21031 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
21032 cp_lexer_consume_token (parser
->lexer
);
21033 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
21035 return error_mark_node
;
21036 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
21039 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
21041 return error_mark_node
;
21043 token
= cp_lexer_peek_token (parser
->lexer
);
21044 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
21046 op
= (token
->type
== CPP_PLUS_PLUS
21047 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
21048 cp_lexer_consume_token (parser
->lexer
);
21049 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
21052 op
= cp_parser_assignment_operator_opt (parser
);
21053 if (op
== ERROR_MARK
)
21054 return error_mark_node
;
21056 if (op
!= NOP_EXPR
)
21058 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
21059 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
21060 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
21063 lhs
= cp_parser_binary_expression (parser
, false,
21064 PREC_ADDITIVE_EXPRESSION
, NULL
);
21065 token
= cp_lexer_peek_token (parser
->lexer
);
21066 decl_first
= lhs
== decl
;
21069 if (token
->type
!= CPP_PLUS
21070 && token
->type
!= CPP_MINUS
)
21071 return error_mark_node
;
21075 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
21076 cp_lexer_consume_token (parser
->lexer
);
21077 rhs
= cp_parser_binary_expression (parser
, false,
21078 PREC_ADDITIVE_EXPRESSION
, NULL
);
21079 token
= cp_lexer_peek_token (parser
->lexer
);
21080 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
21082 if (lhs
== NULL_TREE
)
21084 if (op
== PLUS_EXPR
)
21087 lhs
= build_x_unary_op (NEGATE_EXPR
, rhs
, tf_warning_or_error
);
21090 lhs
= build_x_binary_op (op
, lhs
, ERROR_MARK
, rhs
, ERROR_MARK
,
21091 NULL
, tf_warning_or_error
);
21094 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
21098 if (rhs
!= decl
|| op
== MINUS_EXPR
)
21099 return error_mark_node
;
21100 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
21103 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
21105 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
21108 /* Parse the restricted form of the for statement allowed by OpenMP. */
21111 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
21113 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
21114 tree for_block
= NULL_TREE
, real_decl
, initv
, condv
, incrv
, declv
;
21115 tree this_pre_body
, cl
;
21116 location_t loc_first
;
21117 bool collapse_err
= false;
21118 int i
, collapse
= 1, nbraces
= 0;
21120 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
21121 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
21122 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
21124 gcc_assert (collapse
>= 1);
21126 declv
= make_tree_vec (collapse
);
21127 initv
= make_tree_vec (collapse
);
21128 condv
= make_tree_vec (collapse
);
21129 incrv
= make_tree_vec (collapse
);
21131 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
21133 for (i
= 0; i
< collapse
; i
++)
21135 int bracecount
= 0;
21136 bool add_private_clause
= false;
21139 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
21141 cp_parser_error (parser
, "for statement expected");
21144 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
21146 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21149 init
= decl
= real_decl
= NULL
;
21150 this_pre_body
= push_stmt_list ();
21151 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21153 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21157 integer-type var = lb
21158 random-access-iterator-type var = lb
21159 pointer-type var = lb
21161 cp_decl_specifier_seq type_specifiers
;
21163 /* First, try to parse as an initialized declaration. See
21164 cp_parser_condition, from whence the bulk of this is copied. */
21166 cp_parser_parse_tentatively (parser
);
21167 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
21169 if (cp_parser_parse_definitely (parser
))
21171 /* If parsing a type specifier seq succeeded, then this
21172 MUST be a initialized declaration. */
21173 tree asm_specification
, attributes
;
21174 cp_declarator
*declarator
;
21176 declarator
= cp_parser_declarator (parser
,
21177 CP_PARSER_DECLARATOR_NAMED
,
21178 /*ctor_dtor_or_conv_p=*/NULL
,
21179 /*parenthesized_p=*/NULL
,
21180 /*member_p=*/false);
21181 attributes
= cp_parser_attributes_opt (parser
);
21182 asm_specification
= cp_parser_asm_specification_opt (parser
);
21184 if (declarator
== cp_error_declarator
)
21185 cp_parser_skip_to_end_of_statement (parser
);
21189 tree pushed_scope
, auto_node
;
21191 decl
= start_decl (declarator
, &type_specifiers
,
21192 SD_INITIALIZED
, attributes
,
21193 /*prefix_attributes=*/NULL_TREE
,
21196 auto_node
= type_uses_auto (TREE_TYPE (decl
));
21197 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
21199 if (cp_lexer_next_token_is (parser
->lexer
,
21201 error ("parenthesized initialization is not allowed in "
21202 "OpenMP %<for%> loop");
21204 /* Trigger an error. */
21205 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
21207 init
= error_mark_node
;
21208 cp_parser_skip_to_end_of_statement (parser
);
21210 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
21211 || type_dependent_expression_p (decl
)
21214 bool is_direct_init
, is_non_constant_init
;
21216 init
= cp_parser_initializer (parser
,
21218 &is_non_constant_init
);
21220 if (auto_node
&& describable_type (init
))
21223 = do_auto_deduction (TREE_TYPE (decl
), init
,
21226 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
21227 && !type_dependent_expression_p (decl
))
21231 cp_finish_decl (decl
, init
, !is_non_constant_init
,
21233 LOOKUP_ONLYCONVERTING
);
21234 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
21237 = tree_cons (NULL
, this_pre_body
, for_block
);
21241 init
= pop_stmt_list (this_pre_body
);
21242 this_pre_body
= NULL_TREE
;
21247 cp_lexer_consume_token (parser
->lexer
);
21248 init
= cp_parser_assignment_expression (parser
, false, NULL
);
21251 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
21252 init
= error_mark_node
;
21254 cp_finish_decl (decl
, NULL_TREE
,
21255 /*init_const_expr_p=*/false,
21257 LOOKUP_ONLYCONVERTING
);
21261 pop_scope (pushed_scope
);
21267 /* If parsing a type specifier sequence failed, then
21268 this MUST be a simple expression. */
21269 cp_parser_parse_tentatively (parser
);
21270 decl
= cp_parser_primary_expression (parser
, false, false,
21272 if (!cp_parser_error_occurred (parser
)
21275 && CLASS_TYPE_P (TREE_TYPE (decl
)))
21279 cp_parser_parse_definitely (parser
);
21280 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
21281 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
21282 finish_expr_stmt (build_x_modify_expr (decl
, NOP_EXPR
,
21284 tf_warning_or_error
));
21285 add_private_clause
= true;
21290 cp_parser_abort_tentative_parse (parser
);
21291 init
= cp_parser_expression (parser
, false, NULL
);
21294 if (TREE_CODE (init
) == MODIFY_EXPR
21295 || TREE_CODE (init
) == MODOP_EXPR
)
21296 real_decl
= TREE_OPERAND (init
, 0);
21301 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
21304 this_pre_body
= pop_stmt_list (this_pre_body
);
21308 pre_body
= push_stmt_list ();
21310 add_stmt (this_pre_body
);
21311 pre_body
= pop_stmt_list (pre_body
);
21314 pre_body
= this_pre_body
;
21319 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
21322 for (c
= par_clauses
; *c
; )
21323 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
21324 && OMP_CLAUSE_DECL (*c
) == real_decl
)
21326 error ("%Hiteration variable %qD should not be firstprivate",
21328 *c
= OMP_CLAUSE_CHAIN (*c
);
21330 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
21331 && OMP_CLAUSE_DECL (*c
) == real_decl
)
21333 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21334 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21335 tree l
= build_omp_clause (OMP_CLAUSE_LASTPRIVATE
);
21336 OMP_CLAUSE_DECL (l
) = real_decl
;
21337 OMP_CLAUSE_CHAIN (l
) = clauses
;
21338 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
21340 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
21341 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
21342 add_private_clause
= false;
21346 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
21347 && OMP_CLAUSE_DECL (*c
) == real_decl
)
21348 add_private_clause
= false;
21349 c
= &OMP_CLAUSE_CHAIN (*c
);
21353 if (add_private_clause
)
21356 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
21358 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
21359 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
21360 && OMP_CLAUSE_DECL (c
) == decl
)
21362 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
21363 && OMP_CLAUSE_DECL (c
) == decl
)
21364 error ("%Hiteration variable %qD should not be firstprivate",
21366 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
21367 && OMP_CLAUSE_DECL (c
) == decl
)
21368 error ("%Hiteration variable %qD should not be reduction",
21373 c
= build_omp_clause (OMP_CLAUSE_PRIVATE
);
21374 OMP_CLAUSE_DECL (c
) = decl
;
21375 c
= finish_omp_clauses (c
);
21378 OMP_CLAUSE_CHAIN (c
) = clauses
;
21385 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21387 /* If decl is an iterator, preserve LHS and RHS of the relational
21388 expr until finish_omp_for. */
21390 && (type_dependent_expression_p (decl
)
21391 || CLASS_TYPE_P (TREE_TYPE (decl
))))
21392 cond
= cp_parser_omp_for_cond (parser
, decl
);
21394 cond
= cp_parser_condition (parser
);
21396 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
21399 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
21401 /* If decl is an iterator, preserve the operator on decl
21402 until finish_omp_for. */
21404 && (type_dependent_expression_p (decl
)
21405 || CLASS_TYPE_P (TREE_TYPE (decl
))))
21406 incr
= cp_parser_omp_for_incr (parser
, decl
);
21408 incr
= cp_parser_expression (parser
, false, NULL
);
21411 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
21412 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
21413 /*or_comma=*/false,
21414 /*consume_paren=*/true);
21416 TREE_VEC_ELT (declv
, i
) = decl
;
21417 TREE_VEC_ELT (initv
, i
) = init
;
21418 TREE_VEC_ELT (condv
, i
) = cond
;
21419 TREE_VEC_ELT (incrv
, i
) = incr
;
21421 if (i
== collapse
- 1)
21424 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21425 in between the collapsed for loops to be still considered perfectly
21426 nested. Hopefully the final version clarifies this.
21427 For now handle (multiple) {'s and empty statements. */
21428 cp_parser_parse_tentatively (parser
);
21431 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
21433 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21435 cp_lexer_consume_token (parser
->lexer
);
21438 else if (bracecount
21439 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21440 cp_lexer_consume_token (parser
->lexer
);
21443 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21444 error ("%Hnot enough collapsed for loops", &loc
);
21445 collapse_err
= true;
21446 cp_parser_abort_tentative_parse (parser
);
21455 cp_parser_parse_definitely (parser
);
21456 nbraces
+= bracecount
;
21460 /* Note that we saved the original contents of this flag when we entered
21461 the structured block, and so we don't need to re-save it here. */
21462 parser
->in_statement
= IN_OMP_FOR
;
21464 /* Note that the grammar doesn't call for a structured block here,
21465 though the loop as a whole is a structured block. */
21466 body
= push_stmt_list ();
21467 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
21468 body
= pop_stmt_list (body
);
21470 if (declv
== NULL_TREE
)
21473 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
21474 pre_body
, clauses
);
21478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
21480 cp_lexer_consume_token (parser
->lexer
);
21483 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21484 cp_lexer_consume_token (parser
->lexer
);
21489 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21490 error ("%Hcollapsed loops not perfectly nested", &loc
);
21492 collapse_err
= true;
21493 cp_parser_statement_seq_opt (parser
, NULL
);
21494 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
21500 add_stmt (pop_stmt_list (TREE_VALUE (for_block
)));
21501 for_block
= TREE_CHAIN (for_block
);
21508 #pragma omp for for-clause[optseq] new-line
21511 #define OMP_FOR_CLAUSE_MASK \
21512 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21513 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21514 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21515 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21516 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21517 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21518 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21519 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21522 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
21524 tree clauses
, sb
, ret
;
21527 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
21528 "#pragma omp for", pragma_tok
);
21530 sb
= begin_omp_structured_block ();
21531 save
= cp_parser_begin_omp_structured_block (parser
);
21533 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
21535 cp_parser_end_omp_structured_block (parser
, save
);
21536 add_stmt (finish_omp_structured_block (sb
));
21542 # pragma omp master new-line
21543 structured-block */
21546 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
21548 cp_parser_require_pragma_eol (parser
, pragma_tok
);
21549 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
21553 # pragma omp ordered new-line
21554 structured-block */
21557 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
21559 cp_parser_require_pragma_eol (parser
, pragma_tok
);
21560 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
21566 { section-sequence }
21569 section-directive[opt] structured-block
21570 section-sequence section-directive structured-block */
21573 cp_parser_omp_sections_scope (cp_parser
*parser
)
21575 tree stmt
, substmt
;
21576 bool error_suppress
= false;
21579 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>"))
21582 stmt
= push_stmt_list ();
21584 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
21588 substmt
= begin_omp_structured_block ();
21589 save
= cp_parser_begin_omp_structured_block (parser
);
21593 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
21595 tok
= cp_lexer_peek_token (parser
->lexer
);
21596 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
21598 if (tok
->type
== CPP_CLOSE_BRACE
)
21600 if (tok
->type
== CPP_EOF
)
21604 cp_parser_end_omp_structured_block (parser
, save
);
21605 substmt
= finish_omp_structured_block (substmt
);
21606 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
21607 add_stmt (substmt
);
21612 tok
= cp_lexer_peek_token (parser
->lexer
);
21613 if (tok
->type
== CPP_CLOSE_BRACE
)
21615 if (tok
->type
== CPP_EOF
)
21618 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
21620 cp_lexer_consume_token (parser
->lexer
);
21621 cp_parser_require_pragma_eol (parser
, tok
);
21622 error_suppress
= false;
21624 else if (!error_suppress
)
21626 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
21627 error_suppress
= true;
21630 substmt
= cp_parser_omp_structured_block (parser
);
21631 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
21632 add_stmt (substmt
);
21634 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
21636 substmt
= pop_stmt_list (stmt
);
21638 stmt
= make_node (OMP_SECTIONS
);
21639 TREE_TYPE (stmt
) = void_type_node
;
21640 OMP_SECTIONS_BODY (stmt
) = substmt
;
21647 # pragma omp sections sections-clause[optseq] newline
21650 #define OMP_SECTIONS_CLAUSE_MASK \
21651 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21652 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21653 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21654 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21655 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21658 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
21662 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
21663 "#pragma omp sections", pragma_tok
);
21665 ret
= cp_parser_omp_sections_scope (parser
);
21667 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
21673 # pragma parallel parallel-clause new-line
21674 # pragma parallel for parallel-for-clause new-line
21675 # pragma parallel sections parallel-sections-clause new-line */
21677 #define OMP_PARALLEL_CLAUSE_MASK \
21678 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21679 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21680 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21681 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21682 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21683 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21684 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21685 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21688 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
21690 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
21691 const char *p_name
= "#pragma omp parallel";
21692 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
21693 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
21696 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
21698 cp_lexer_consume_token (parser
->lexer
);
21699 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
21700 p_name
= "#pragma omp parallel for";
21701 mask
|= OMP_FOR_CLAUSE_MASK
;
21702 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
21704 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21706 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
21707 const char *p
= IDENTIFIER_POINTER (id
);
21708 if (strcmp (p
, "sections") == 0)
21710 cp_lexer_consume_token (parser
->lexer
);
21711 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
21712 p_name
= "#pragma omp parallel sections";
21713 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
21714 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
21718 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
21719 block
= begin_omp_parallel ();
21720 save
= cp_parser_begin_omp_structured_block (parser
);
21724 case PRAGMA_OMP_PARALLEL
:
21725 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
21726 par_clause
= clauses
;
21729 case PRAGMA_OMP_PARALLEL_FOR
:
21730 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
21731 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
21734 case PRAGMA_OMP_PARALLEL_SECTIONS
:
21735 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
21736 stmt
= cp_parser_omp_sections_scope (parser
);
21738 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
21742 gcc_unreachable ();
21745 cp_parser_end_omp_structured_block (parser
, save
);
21746 stmt
= finish_omp_parallel (par_clause
, block
);
21747 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
21748 OMP_PARALLEL_COMBINED (stmt
) = 1;
21753 # pragma omp single single-clause[optseq] new-line
21754 structured-block */
21756 #define OMP_SINGLE_CLAUSE_MASK \
21757 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21758 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21759 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21760 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21763 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
21765 tree stmt
= make_node (OMP_SINGLE
);
21766 TREE_TYPE (stmt
) = void_type_node
;
21768 OMP_SINGLE_CLAUSES (stmt
)
21769 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
21770 "#pragma omp single", pragma_tok
);
21771 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
21773 return add_stmt (stmt
);
21777 # pragma omp task task-clause[optseq] new-line
21778 structured-block */
21780 #define OMP_TASK_CLAUSE_MASK \
21781 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21782 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21783 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21784 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21785 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21786 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21789 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
21791 tree clauses
, block
;
21794 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
21795 "#pragma omp task", pragma_tok
);
21796 block
= begin_omp_task ();
21797 save
= cp_parser_begin_omp_structured_block (parser
);
21798 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
21799 cp_parser_end_omp_structured_block (parser
, save
);
21800 return finish_omp_task (clauses
, block
);
21804 # pragma omp taskwait new-line */
21807 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
21809 cp_parser_require_pragma_eol (parser
, pragma_tok
);
21810 finish_omp_taskwait ();
21814 # pragma omp threadprivate (variable-list) */
21817 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
21821 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
21822 cp_parser_require_pragma_eol (parser
, pragma_tok
);
21824 finish_omp_threadprivate (vars
);
21827 /* Main entry point to OpenMP statement pragmas. */
21830 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
21834 switch (pragma_tok
->pragma_kind
)
21836 case PRAGMA_OMP_ATOMIC
:
21837 cp_parser_omp_atomic (parser
, pragma_tok
);
21839 case PRAGMA_OMP_CRITICAL
:
21840 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
21842 case PRAGMA_OMP_FOR
:
21843 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
21845 case PRAGMA_OMP_MASTER
:
21846 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
21848 case PRAGMA_OMP_ORDERED
:
21849 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
21851 case PRAGMA_OMP_PARALLEL
:
21852 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
21854 case PRAGMA_OMP_SECTIONS
:
21855 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
21857 case PRAGMA_OMP_SINGLE
:
21858 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
21860 case PRAGMA_OMP_TASK
:
21861 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
21864 gcc_unreachable ();
21868 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
21873 static GTY (()) cp_parser
*the_parser
;
21876 /* Special handling for the first token or line in the file. The first
21877 thing in the file might be #pragma GCC pch_preprocess, which loads a
21878 PCH file, which is a GC collection point. So we need to handle this
21879 first pragma without benefit of an existing lexer structure.
21881 Always returns one token to the caller in *FIRST_TOKEN. This is
21882 either the true first token of the file, or the first token after
21883 the initial pragma. */
21886 cp_parser_initial_pragma (cp_token
*first_token
)
21890 cp_lexer_get_preprocessor_token (NULL
, first_token
);
21891 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
21894 cp_lexer_get_preprocessor_token (NULL
, first_token
);
21895 if (first_token
->type
== CPP_STRING
)
21897 name
= first_token
->u
.value
;
21899 cp_lexer_get_preprocessor_token (NULL
, first_token
);
21900 if (first_token
->type
!= CPP_PRAGMA_EOL
)
21901 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21902 &first_token
->location
);
21905 error ("%Hexpected string literal", &first_token
->location
);
21907 /* Skip to the end of the pragma. */
21908 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
21909 cp_lexer_get_preprocessor_token (NULL
, first_token
);
21911 /* Now actually load the PCH file. */
21913 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
21915 /* Read one more token to return to our caller. We have to do this
21916 after reading the PCH file in, since its pointers have to be
21918 cp_lexer_get_preprocessor_token (NULL
, first_token
);
21921 /* Normal parsing of a pragma token. Here we can (and must) use the
21925 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
21927 cp_token
*pragma_tok
;
21930 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
21931 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
21932 parser
->lexer
->in_pragma
= true;
21934 id
= pragma_tok
->pragma_kind
;
21937 case PRAGMA_GCC_PCH_PREPROCESS
:
21938 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21939 &pragma_tok
->location
);
21942 case PRAGMA_OMP_BARRIER
:
21945 case pragma_compound
:
21946 cp_parser_omp_barrier (parser
, pragma_tok
);
21949 error ("%H%<#pragma omp barrier%> may only be "
21950 "used in compound statements", &pragma_tok
->location
);
21957 case PRAGMA_OMP_FLUSH
:
21960 case pragma_compound
:
21961 cp_parser_omp_flush (parser
, pragma_tok
);
21964 error ("%H%<#pragma omp flush%> may only be "
21965 "used in compound statements", &pragma_tok
->location
);
21972 case PRAGMA_OMP_TASKWAIT
:
21975 case pragma_compound
:
21976 cp_parser_omp_taskwait (parser
, pragma_tok
);
21979 error ("%H%<#pragma omp taskwait%> may only be "
21980 "used in compound statements",
21981 &pragma_tok
->location
);
21988 case PRAGMA_OMP_THREADPRIVATE
:
21989 cp_parser_omp_threadprivate (parser
, pragma_tok
);
21992 case PRAGMA_OMP_ATOMIC
:
21993 case PRAGMA_OMP_CRITICAL
:
21994 case PRAGMA_OMP_FOR
:
21995 case PRAGMA_OMP_MASTER
:
21996 case PRAGMA_OMP_ORDERED
:
21997 case PRAGMA_OMP_PARALLEL
:
21998 case PRAGMA_OMP_SECTIONS
:
21999 case PRAGMA_OMP_SINGLE
:
22000 case PRAGMA_OMP_TASK
:
22001 if (context
== pragma_external
)
22003 cp_parser_omp_construct (parser
, pragma_tok
);
22006 case PRAGMA_OMP_SECTION
:
22007 error ("%H%<#pragma omp section%> may only be used in "
22008 "%<#pragma omp sections%> construct", &pragma_tok
->location
);
22012 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
22013 c_invoke_pragma_handler (id
);
22017 cp_parser_error (parser
, "expected declaration specifiers");
22021 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
22025 /* The interface the pragma parsers have to the lexer. */
22028 pragma_lex (tree
*value
)
22031 enum cpp_ttype ret
;
22033 tok
= cp_lexer_peek_token (the_parser
->lexer
);
22036 *value
= tok
->u
.value
;
22038 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
22040 else if (ret
== CPP_STRING
)
22041 *value
= cp_parser_string_literal (the_parser
, false, false);
22044 cp_lexer_consume_token (the_parser
->lexer
);
22045 if (ret
== CPP_KEYWORD
)
22053 /* External interface. */
22055 /* Parse one entire translation unit. */
22058 c_parse_file (void)
22060 bool error_occurred
;
22061 static bool already_called
= false;
22063 if (already_called
)
22065 sorry ("inter-module optimizations not implemented for C++");
22068 already_called
= true;
22070 the_parser
= cp_parser_new ();
22071 push_deferring_access_checks (flag_access_control
22072 ? dk_no_deferred
: dk_no_check
);
22073 error_occurred
= cp_parser_translation_unit (the_parser
);
22077 #include "gt-cp-parser.h"