2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
47 typedef struct cp_token
GTY (())
49 /* The kind of token. */
50 ENUM_BITFIELD (cpp_ttype
) type
: 8;
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
53 ENUM_BITFIELD (rid
) keyword
: 8;
56 /* True if this token is from a system header. */
57 BOOL_BITFIELD in_system_header
: 1;
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c
: 1;
60 /* The value associated with this token, if any. */
62 /* The location at which this token was found. */
66 /* The cp_lexer structure represents the C++ lexer. It is responsible
67 for managing the token stream from the preprocessor and supplying
68 it to the parser. Tokens are never added to the cp_lexer after
71 typedef struct cp_lexer
GTY (())
73 /* The memory allocated for the buffer. Never NULL. */
74 cp_token
* GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer
;
75 /* A pointer just past the end of the memory allocated for the buffer. */
76 cp_token
* GTY ((skip
)) buffer_end
;
77 /* A pointer just past the last available token. The tokens
78 in this lexer are [buffer, last_token). */
79 cp_token
* GTY ((skip
)) last_token
;
81 /* The next available token. If NEXT_TOKEN is NULL, then there are
82 no more available tokens. */
83 cp_token
* GTY ((skip
)) next_token
;
85 /* A stack indicating positions at which cp_lexer_save_tokens was
86 called. The top entry is the most recent position at which we
87 began saving tokens. The entries are differences in token
88 position between BUFFER and the first saved token.
89 If the stack is non-empty, we are saving tokens. */
90 varray_type saved_tokens
;
92 /* True if we should output debugging information. */
95 /* The next lexer in a linked list of lexers. */
96 struct cp_lexer
*next
;
99 /* cp_token_cache is a range of tokens. There is no need to represent
100 allocate heap memory for it, since tokens are never removed from the
101 lexer's array. There is also no need for the GC to walk through
102 a cp_token_cache, since everything in here is referenced through
105 typedef struct cp_token_cache
GTY(())
107 /* The beginning of the token range. */
108 cp_token
* GTY((skip
)) first
;
110 /* Points immediately after the last token in the range. */
111 cp_token
* GTY ((skip
)) last
;
116 static cp_lexer
*cp_lexer_new_main
118 static cp_lexer
*cp_lexer_new_from_tokens
119 (cp_token_cache
*tokens
);
120 static void cp_lexer_destroy
122 static int cp_lexer_saving_tokens
124 static cp_token
*cp_lexer_next_token
125 (cp_lexer
*, cp_token
*);
126 static cp_token
*cp_lexer_prev_token
127 (cp_lexer
*, cp_token
*);
128 static ptrdiff_t cp_lexer_token_difference
129 (cp_lexer
*, cp_token
*, cp_token
*);
130 static void cp_lexer_grow_buffer
132 static void cp_lexer_get_preprocessor_token
133 (cp_lexer
*, cp_token
*);
134 static inline cp_token
*cp_lexer_peek_token
136 static cp_token
*cp_lexer_peek_nth_token
137 (cp_lexer
*, size_t);
138 static inline bool cp_lexer_next_token_is
139 (cp_lexer
*, enum cpp_ttype
);
140 static bool cp_lexer_next_token_is_not
141 (cp_lexer
*, enum cpp_ttype
);
142 static bool cp_lexer_next_token_is_keyword
143 (cp_lexer
*, enum rid
);
144 static cp_token
*cp_lexer_consume_token
146 static void cp_lexer_purge_token
148 static void cp_lexer_purge_tokens_after
149 (cp_lexer
*, cp_token
*);
150 static void cp_lexer_handle_pragma
152 static void cp_lexer_save_tokens
154 static void cp_lexer_commit_tokens
156 static void cp_lexer_rollback_tokens
158 #ifdef ENABLE_CHECKING
159 static void cp_lexer_print_token
160 (FILE *, cp_token
*);
161 static inline bool cp_lexer_debugging_p
163 static void cp_lexer_start_debugging
164 (cp_lexer
*) ATTRIBUTE_UNUSED
;
165 static void cp_lexer_stop_debugging
166 (cp_lexer
*) ATTRIBUTE_UNUSED
;
167 static void cp_lexer_peek_token_emit_debug_info
168 (cp_lexer
*, cp_token
*);
170 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
171 about passing NULL to functions that require non-NULL arguments
172 (fputs, fprintf). It will never be used, so all we need is a value
173 of the right type that's guaranteed not to be NULL. */
174 #define cp_lexer_debug_stream stdout
175 #define cp_lexer_print_token(str, tok) (void) 0
176 #define cp_lexer_debugging_p(lexer) 0
177 #define cp_lexer_peek_token_emit_debug_info(lexer, tok) (void) 0
178 #endif /* ENABLE_CHECKING */
180 static cp_token_cache
*cp_token_cache_new
181 (cp_token
*, cp_token
*);
183 /* Manifest constants. */
185 #define CP_LEXER_BUFFER_SIZE 10000
186 #define CP_SAVED_TOKENS_SIZE 5
188 /* A token type for keywords, as opposed to ordinary identifiers. */
189 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
191 /* A token type for template-ids. If a template-id is processed while
192 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
193 the value of the CPP_TEMPLATE_ID is whatever was returned by
194 cp_parser_template_id. */
195 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
197 /* A token type for nested-name-specifiers. If a
198 nested-name-specifier is processed while parsing tentatively, it is
199 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
200 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
201 cp_parser_nested_name_specifier_opt. */
202 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
204 /* A token type for tokens that are not tokens at all; these are used
205 to represent slots in the array where there used to be a token
206 that has now been deleted. */
207 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
209 /* The number of token types, including C++-specific ones. */
210 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
214 #ifdef ENABLE_CHECKING
215 /* The stream to which debugging output should be written. */
216 static FILE *cp_lexer_debug_stream
;
217 #endif /* ENABLE_CHECKING */
219 /* Create a new main C++ lexer, the lexer that gets tokens from the
223 cp_lexer_new_main (void)
226 cp_token first_token
;
228 /* Tell cpplib we want CPP_PRAGMA tokens. */
229 cpp_get_options (parse_in
)->defer_pragmas
= true;
231 /* Tell c_lex not to merge string constants. */
232 c_lex_return_raw_strings
= true;
234 /* It's possible that lexing the first token will load a PCH file,
235 which is a GC collection point. So we have to grab the first
236 token before allocating any memory. */
237 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
238 c_common_no_more_pch ();
240 /* Allocate the memory. */
241 lexer
= GGC_CNEW (cp_lexer
);
243 /* Create the buffer. */
244 lexer
->buffer
= ggc_calloc (CP_LEXER_BUFFER_SIZE
, sizeof (cp_token
));
245 lexer
->buffer_end
= lexer
->buffer
+ CP_LEXER_BUFFER_SIZE
;
247 /* There is one token in the buffer. */
248 lexer
->last_token
= lexer
->buffer
+ 1;
249 lexer
->next_token
= lexer
->buffer
;
250 *lexer
->next_token
= first_token
;
252 /* Create the SAVED_TOKENS stack. */
253 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
255 #ifdef ENABLE_CHECKING
256 /* Initially we are not debugging. */
257 lexer
->debugging_p
= false;
258 #endif /* ENABLE_CHECKING */
260 /* Get the rest of the tokens from the preprocessor. */
261 while (lexer
->last_token
[-1].type
!= CPP_EOF
)
263 if (lexer
->last_token
== lexer
->buffer_end
)
264 cp_lexer_grow_buffer (lexer
);
265 cp_lexer_get_preprocessor_token (lexer
, lexer
->last_token
++);
268 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
269 direct calls to c_lex. Those callers all expect c_lex to do
270 string constant concatenation. */
271 c_lex_return_raw_strings
= false;
273 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
277 /* Create a new lexer whose token stream is primed with the tokens in
278 CACHE. When these tokens are exhausted, no new tokens will be read. */
281 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
283 cp_token
*first
= cache
->first
;
284 cp_token
*last
= cache
->last
;
285 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
288 /* Allocate a new buffer. The reason we do this is to make sure
289 there's a CPP_EOF token at the end. An alternative would be to
290 modify cp_lexer_peek_token so that it checks for end-of-buffer
291 and returns a CPP_EOF when appropriate. */
293 lexer
->buffer
= GGC_NEWVEC (cp_token
, (last
- first
) + 1);
294 memcpy (lexer
->buffer
, first
, sizeof (cp_token
) * (last
- first
));
295 lexer
->next_token
= lexer
->buffer
;
296 lexer
->buffer_end
= lexer
->last_token
= lexer
->buffer
+ (last
- first
);
298 eof
= lexer
->buffer
+ (last
- first
);
300 eof
->location
= UNKNOWN_LOCATION
;
301 eof
->value
= NULL_TREE
;
302 eof
->keyword
= RID_MAX
;
304 /* Create the SAVED_TOKENS stack. */
305 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
307 #ifdef ENABLE_CHECKING
308 /* Initially we are not debugging. */
309 lexer
->debugging_p
= false;
312 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
316 /* Frees all resources associated with LEXER. */
319 cp_lexer_destroy (cp_lexer
*lexer
)
321 ggc_free (lexer
->buffer
);
325 /* Returns nonzero if debugging information should be output. */
327 #ifdef ENABLE_CHECKING
330 cp_lexer_debugging_p (cp_lexer
*lexer
)
332 return lexer
->debugging_p
;
335 #endif /* ENABLE_CHECKING */
337 /* TOKEN points into the circular token buffer. Return a pointer to
338 the next token in the buffer. */
340 static inline cp_token
*
341 cp_lexer_next_token (cp_lexer
* lexer ATTRIBUTE_UNUSED
, cp_token
* token
)
347 /* TOKEN points into the circular token buffer. Return a pointer to
348 the previous token in the buffer. */
350 static inline cp_token
*
351 cp_lexer_prev_token (cp_lexer
* lexer ATTRIBUTE_UNUSED
, cp_token
* token
)
356 /* nonzero if we are presently saving tokens. */
359 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
361 return VARRAY_ACTIVE_SIZE (lexer
->saved_tokens
) != 0;
364 /* Return a pointer to the token that is N tokens beyond TOKEN in the
367 static inline cp_token
*
368 cp_lexer_advance_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
369 cp_token
*token
, ptrdiff_t n
)
374 /* Returns the number of times that START would have to be incremented
375 to reach FINISH. If START and FINISH are the same, returns zero. */
377 static inline ptrdiff_t
378 cp_lexer_token_difference (cp_lexer
* lexer ATTRIBUTE_UNUSED
,
379 cp_token
* start
, cp_token
* finish
)
381 return finish
- start
;
384 /* If the buffer is full, make it bigger. */
386 cp_lexer_grow_buffer (cp_lexer
* lexer
)
388 cp_token
*old_buffer
;
389 cp_token
*new_buffer
;
390 ptrdiff_t buffer_length
;
392 /* This function should only be called when buffer is full. */
393 gcc_assert (lexer
->last_token
== lexer
->buffer_end
);
395 /* Remember the current buffer pointer. It will become invalid,
396 but we will need to do pointer arithmetic involving this
398 old_buffer
= lexer
->buffer
;
399 /* Compute the current buffer size. */
400 buffer_length
= lexer
->buffer_end
- lexer
->buffer
;
401 /* Allocate a buffer twice as big. */
402 new_buffer
= ggc_realloc (lexer
->buffer
,
403 2 * buffer_length
* sizeof (cp_token
));
405 /* Recompute buffer positions. */
406 lexer
->buffer
= new_buffer
;
407 lexer
->buffer_end
= new_buffer
+ 2 * buffer_length
;
408 lexer
->last_token
= new_buffer
+ (lexer
->last_token
- old_buffer
);
409 lexer
->next_token
= new_buffer
+ (lexer
->next_token
- old_buffer
);
411 /* Clear the rest of the buffer. We never look at this storage,
412 but the garbage collector may. */
413 memset (lexer
->last_token
, 0,
414 (lexer
->buffer_end
- lexer
->last_token
) * sizeof(cp_token
));
417 /* Store the next token from the preprocessor in *TOKEN. */
420 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
423 static int is_extern_c
= 0;
427 /* Keep going until we get a token we like. */
430 /* Get a new token from the preprocessor. */
431 token
->type
= c_lex_with_flags (&token
->value
, &token
->flags
);
432 /* Issue messages about tokens we cannot process. */
438 error ("invalid token");
442 /* This is a good token, so we exit the loop. */
447 /* Now we've got our token. */
448 token
->location
= input_location
;
449 token
->in_system_header
= in_system_header
;
451 /* On some systems, some header files are surrounded by an
452 implicit extern "C" block. Set a flag in the token if it
453 comes from such a header. */
454 is_extern_c
+= pending_lang_change
;
455 pending_lang_change
= 0;
456 token
->implicit_extern_c
= is_extern_c
> 0;
458 /* Check to see if this token is a keyword. */
459 if (token
->type
== CPP_NAME
460 && C_IS_RESERVED_WORD (token
->value
))
462 /* Mark this token as a keyword. */
463 token
->type
= CPP_KEYWORD
;
464 /* Record which keyword. */
465 token
->keyword
= C_RID_CODE (token
->value
);
466 /* Update the value. Some keywords are mapped to particular
467 entities, rather than simply having the value of the
468 corresponding IDENTIFIER_NODE. For example, `__const' is
469 mapped to `const'. */
470 token
->value
= ridpointers
[token
->keyword
];
473 token
->keyword
= RID_MAX
;
476 /* Update the globals input_location and in_system_header from TOKEN. */
478 cp_lexer_set_source_position_from_token (cp_token
*token
)
480 if (token
->type
!= CPP_EOF
)
482 input_location
= token
->location
;
483 in_system_header
= token
->in_system_header
;
487 /* Return a pointer to the next token in the token stream, but do not
490 static inline cp_token
*
491 cp_lexer_peek_token (cp_lexer
*lexer
)
493 if (cp_lexer_debugging_p (lexer
))
494 cp_lexer_peek_token_emit_debug_info (lexer
, lexer
->next_token
);
495 return lexer
->next_token
;
498 #ifdef ENABLE_CHECKING
499 /* Emit debug output for cp_lexer_peek_token. Split out into a
500 separate function so that cp_lexer_peek_token can be small and
504 cp_lexer_peek_token_emit_debug_info (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
505 cp_token
*token ATTRIBUTE_UNUSED
)
507 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
508 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
509 putc ('\n', cp_lexer_debug_stream
);
513 /* Return true if the next token has the indicated TYPE. */
516 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
518 return cp_lexer_peek_token (lexer
)->type
== type
;
521 /* Return true if the next token does not have the indicated TYPE. */
524 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
526 return !cp_lexer_next_token_is (lexer
, type
);
529 /* Return true if the next token is the indicated KEYWORD. */
532 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
536 /* Peek at the next token. */
537 token
= cp_lexer_peek_token (lexer
);
538 /* Check to see if it is the indicated keyword. */
539 return token
->keyword
== keyword
;
542 /* Return a pointer to the Nth token in the token stream. If N is 1,
543 then this is precisely equivalent to cp_lexer_peek_token (except
544 that it is not inline). One would like to disallow that case, but
545 there is one case (cp_parser_nth_token_starts_template_id) where
546 the caller passes a variable for N and it might be 1. */
549 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
553 /* N is 1-based, not zero-based. */
556 if (cp_lexer_debugging_p (lexer
))
557 fprintf (cp_lexer_debug_stream
,
558 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
561 token
= lexer
->next_token
;
565 if (token
->type
!= CPP_PURGED
)
569 if (cp_lexer_debugging_p (lexer
))
571 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
572 putc ('\n', cp_lexer_debug_stream
);
578 /* Return the next token, and advance the lexer's next_token pointer
579 to point to the next non-purged token. */
582 cp_lexer_consume_token (cp_lexer
* lexer
)
584 cp_token
*token
= lexer
->next_token
;
588 while (lexer
->next_token
->type
== CPP_PURGED
);
590 cp_lexer_set_source_position_from_token (token
);
592 /* Provide debugging output. */
593 if (cp_lexer_debugging_p (lexer
))
595 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
596 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
597 putc ('\n', cp_lexer_debug_stream
);
603 /* Permanently remove the next token from the token stream, and
604 advance the next_token pointer to refer to the next non-purged
608 cp_lexer_purge_token (cp_lexer
*lexer
)
610 cp_token
*tok
= lexer
->next_token
;
611 tok
->type
= CPP_PURGED
;
612 tok
->location
= UNKNOWN_LOCATION
;
613 tok
->value
= NULL_TREE
;
614 tok
->keyword
= RID_MAX
;
618 while (lexer
->next_token
->type
== CPP_PURGED
);
621 /* Permanently remove all tokens after TOK, up to, but not
622 including, the token that will be returned next by
623 cp_lexer_peek_token. */
626 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
630 peek
= cp_lexer_peek_token (lexer
);
631 gcc_assert (tok
< peek
);
633 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
635 tok
->type
= CPP_PURGED
;
636 tok
->location
= UNKNOWN_LOCATION
;
637 tok
->value
= NULL_TREE
;
638 tok
->keyword
= RID_MAX
;
642 /* Consume and handle a pragma token. */
644 cp_lexer_handle_pragma (cp_lexer
*lexer
)
647 cp_token
*token
= cp_lexer_consume_token (lexer
);
648 gcc_assert (token
->type
== CPP_PRAGMA
);
649 gcc_assert (token
->value
);
651 s
.len
= TREE_STRING_LENGTH (token
->value
);
652 s
.text
= (const unsigned char *) TREE_STRING_POINTER (token
->value
);
654 cpp_handle_deferred_pragma (parse_in
, &s
);
656 /* Clearing token->value here means that we will get an ICE if we
657 try to process this #pragma again (which should be impossible). */
661 /* Begin saving tokens. All tokens consumed after this point will be
665 cp_lexer_save_tokens (cp_lexer
* lexer
)
667 /* Provide debugging output. */
668 if (cp_lexer_debugging_p (lexer
))
669 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
671 VARRAY_PUSH_INT (lexer
->saved_tokens
,
672 cp_lexer_token_difference (lexer
,
677 /* Commit to the portion of the token stream most recently saved. */
680 cp_lexer_commit_tokens (cp_lexer
* lexer
)
682 /* Provide debugging output. */
683 if (cp_lexer_debugging_p (lexer
))
684 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
686 VARRAY_POP (lexer
->saved_tokens
);
689 /* Return all tokens saved since the last call to cp_lexer_save_tokens
690 to the token stream. Stop saving tokens. */
693 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
697 /* Provide debugging output. */
698 if (cp_lexer_debugging_p (lexer
))
699 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
701 /* Find the token that was the NEXT_TOKEN when we started saving
703 delta
= VARRAY_TOP_INT(lexer
->saved_tokens
);
704 /* Make it the next token again now. */
705 lexer
->next_token
= cp_lexer_advance_token (lexer
, lexer
->buffer
, delta
);
707 /* Stop saving tokens. */
708 VARRAY_POP (lexer
->saved_tokens
);
711 /* Print a representation of the TOKEN on the STREAM. */
713 #ifdef ENABLE_CHECKING
716 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
718 /* We don't use cpp_type2name here because the parser defines
719 a few tokens of its own. */
720 static const char *const token_names
[] = {
721 /* cpplib-defined token types */
727 /* C++ parser token types - see "Manifest constants", above. */
730 "NESTED_NAME_SPECIFIER",
734 /* If we have a name for the token, print it out. Otherwise, we
735 simply give the numeric code. */
736 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
737 fputs (token_names
[token
->type
], stream
);
739 /* For some tokens, print the associated data. */
743 /* Some keywords have a value that is not an IDENTIFIER_NODE.
744 For example, `struct' is mapped to an INTEGER_CST. */
745 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
747 /* else fall through */
749 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
755 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
763 /* Start emitting debugging information. */
766 cp_lexer_start_debugging (cp_lexer
* lexer
)
768 ++lexer
->debugging_p
;
771 /* Stop emitting debugging information. */
774 cp_lexer_stop_debugging (cp_lexer
* lexer
)
776 --lexer
->debugging_p
;
779 #endif /* ENABLE_CHECKING */
781 /* Create a new cp_token_cache, representing a range of tokens. */
783 static cp_token_cache
*
784 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
786 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
787 cache
->first
= first
;
793 /* Decl-specifiers. */
795 static void clear_decl_specs
796 (cp_decl_specifier_seq
*);
798 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
801 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
803 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
808 /* Nothing other than the parser should be creating declarators;
809 declarators are a semi-syntactic representation of C++ entities.
810 Other parts of the front end that need to create entities (like
811 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
813 static cp_declarator
*make_id_declarator
815 static cp_declarator
*make_call_declarator
816 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
817 static cp_declarator
*make_array_declarator
818 (cp_declarator
*, tree
);
819 static cp_declarator
*make_pointer_declarator
820 (cp_cv_quals
, cp_declarator
*);
821 static cp_declarator
*make_reference_declarator
822 (cp_cv_quals
, cp_declarator
*);
823 static cp_parameter_declarator
*make_parameter_declarator
824 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
825 static cp_declarator
*make_ptrmem_declarator
826 (cp_cv_quals
, tree
, cp_declarator
*);
828 cp_declarator
*cp_error_declarator
;
830 /* The obstack on which declarators and related data structures are
832 static struct obstack declarator_obstack
;
834 /* Alloc BYTES from the declarator memory pool. */
837 alloc_declarator (size_t bytes
)
839 return obstack_alloc (&declarator_obstack
, bytes
);
842 /* Allocate a declarator of the indicated KIND. Clear fields that are
843 common to all declarators. */
845 static cp_declarator
*
846 make_declarator (cp_declarator_kind kind
)
848 cp_declarator
*declarator
;
850 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
851 declarator
->kind
= kind
;
852 declarator
->attributes
= NULL_TREE
;
853 declarator
->declarator
= NULL
;
858 /* Make a declarator for a generalized identifier. */
861 make_id_declarator (tree id
)
863 cp_declarator
*declarator
;
865 declarator
= make_declarator (cdk_id
);
866 declarator
->u
.id
.name
= id
;
867 declarator
->u
.id
.sfk
= sfk_none
;
872 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
873 of modifiers such as const or volatile to apply to the pointer
874 type, represented as identifiers. */
877 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
879 cp_declarator
*declarator
;
881 declarator
= make_declarator (cdk_pointer
);
882 declarator
->declarator
= target
;
883 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
884 declarator
->u
.pointer
.class_type
= NULL_TREE
;
889 /* Like make_pointer_declarator -- but for references. */
892 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
894 cp_declarator
*declarator
;
896 declarator
= make_declarator (cdk_reference
);
897 declarator
->declarator
= target
;
898 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
899 declarator
->u
.pointer
.class_type
= NULL_TREE
;
904 /* Like make_pointer_declarator -- but for a pointer to a non-static
905 member of CLASS_TYPE. */
908 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
909 cp_declarator
*pointee
)
911 cp_declarator
*declarator
;
913 declarator
= make_declarator (cdk_ptrmem
);
914 declarator
->declarator
= pointee
;
915 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
916 declarator
->u
.pointer
.class_type
= class_type
;
921 /* Make a declarator for the function given by TARGET, with the
922 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
923 "const"-qualified member function. The EXCEPTION_SPECIFICATION
924 indicates what exceptions can be thrown. */
927 make_call_declarator (cp_declarator
*target
,
928 cp_parameter_declarator
*parms
,
929 cp_cv_quals cv_qualifiers
,
930 tree exception_specification
)
932 cp_declarator
*declarator
;
934 declarator
= make_declarator (cdk_function
);
935 declarator
->declarator
= target
;
936 declarator
->u
.function
.parameters
= parms
;
937 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
938 declarator
->u
.function
.exception_specification
= exception_specification
;
943 /* Make a declarator for an array of BOUNDS elements, each of which is
944 defined by ELEMENT. */
947 make_array_declarator (cp_declarator
*element
, tree bounds
)
949 cp_declarator
*declarator
;
951 declarator
= make_declarator (cdk_array
);
952 declarator
->declarator
= element
;
953 declarator
->u
.array
.bounds
= bounds
;
958 cp_parameter_declarator
*no_parameters
;
960 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
961 DECLARATOR and DEFAULT_ARGUMENT. */
963 cp_parameter_declarator
*
964 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
965 cp_declarator
*declarator
,
966 tree default_argument
)
968 cp_parameter_declarator
*parameter
;
970 parameter
= ((cp_parameter_declarator
*)
971 alloc_declarator (sizeof (cp_parameter_declarator
)));
972 parameter
->next
= NULL
;
974 parameter
->decl_specifiers
= *decl_specifiers
;
976 clear_decl_specs (¶meter
->decl_specifiers
);
977 parameter
->declarator
= declarator
;
978 parameter
->default_argument
= default_argument
;
979 parameter
->ellipsis_p
= false;
989 A cp_parser parses the token stream as specified by the C++
990 grammar. Its job is purely parsing, not semantic analysis. For
991 example, the parser breaks the token stream into declarators,
992 expressions, statements, and other similar syntactic constructs.
993 It does not check that the types of the expressions on either side
994 of an assignment-statement are compatible, or that a function is
995 not declared with a parameter of type `void'.
997 The parser invokes routines elsewhere in the compiler to perform
998 semantic analysis and to build up the abstract syntax tree for the
1001 The parser (and the template instantiation code, which is, in a
1002 way, a close relative of parsing) are the only parts of the
1003 compiler that should be calling push_scope and pop_scope, or
1004 related functions. The parser (and template instantiation code)
1005 keeps track of what scope is presently active; everything else
1006 should simply honor that. (The code that generates static
1007 initializers may also need to set the scope, in order to check
1008 access control correctly when emitting the initializers.)
1013 The parser is of the standard recursive-descent variety. Upcoming
1014 tokens in the token stream are examined in order to determine which
1015 production to use when parsing a non-terminal. Some C++ constructs
1016 require arbitrary look ahead to disambiguate. For example, it is
1017 impossible, in the general case, to tell whether a statement is an
1018 expression or declaration without scanning the entire statement.
1019 Therefore, the parser is capable of "parsing tentatively." When the
1020 parser is not sure what construct comes next, it enters this mode.
1021 Then, while we attempt to parse the construct, the parser queues up
1022 error messages, rather than issuing them immediately, and saves the
1023 tokens it consumes. If the construct is parsed successfully, the
1024 parser "commits", i.e., it issues any queued error messages and
1025 the tokens that were being preserved are permanently discarded.
1026 If, however, the construct is not parsed successfully, the parser
1027 rolls back its state completely so that it can resume parsing using
1028 a different alternative.
1033 The performance of the parser could probably be improved substantially.
1034 We could often eliminate the need to parse tentatively by looking ahead
1035 a little bit. In some places, this approach might not entirely eliminate
1036 the need to parse tentatively, but it might still speed up the average
1039 /* Flags that are passed to some parsing functions. These values can
1040 be bitwise-ored together. */
1042 typedef enum cp_parser_flags
1045 CP_PARSER_FLAGS_NONE
= 0x0,
1046 /* The construct is optional. If it is not present, then no error
1047 should be issued. */
1048 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1049 /* When parsing a type-specifier, do not allow user-defined types. */
1050 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1053 /* The different kinds of declarators we want to parse. */
1055 typedef enum cp_parser_declarator_kind
1057 /* We want an abstract declarator. */
1058 CP_PARSER_DECLARATOR_ABSTRACT
,
1059 /* We want a named declarator. */
1060 CP_PARSER_DECLARATOR_NAMED
,
1061 /* We don't mind, but the name must be an unqualified-id. */
1062 CP_PARSER_DECLARATOR_EITHER
1063 } cp_parser_declarator_kind
;
1065 /* The precedence values used to parse binary expressions. The minimum value
1066 of PREC must be 1, because zero is reserved to quickly discriminate
1067 binary operators from other tokens. */
1072 PREC_LOGICAL_OR_EXPRESSION
,
1073 PREC_LOGICAL_AND_EXPRESSION
,
1074 PREC_INCLUSIVE_OR_EXPRESSION
,
1075 PREC_EXCLUSIVE_OR_EXPRESSION
,
1076 PREC_AND_EXPRESSION
,
1077 PREC_RELATIONAL_EXPRESSION
,
1078 PREC_EQUALITY_EXPRESSION
,
1079 PREC_SHIFT_EXPRESSION
,
1080 PREC_ADDITIVE_EXPRESSION
,
1081 PREC_MULTIPLICATIVE_EXPRESSION
,
1083 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1086 /* A mapping from a token type to a corresponding tree node type, with a
1087 precedence value. */
1089 typedef struct cp_parser_binary_operations_map_node
1091 /* The token type. */
1092 enum cpp_ttype token_type
;
1093 /* The corresponding tree code. */
1094 enum tree_code tree_type
;
1095 /* The precedence of this operator. */
1096 enum cp_parser_prec prec
;
1097 } cp_parser_binary_operations_map_node
;
1099 /* The status of a tentative parse. */
1101 typedef enum cp_parser_status_kind
1103 /* No errors have occurred. */
1104 CP_PARSER_STATUS_KIND_NO_ERROR
,
1105 /* An error has occurred. */
1106 CP_PARSER_STATUS_KIND_ERROR
,
1107 /* We are committed to this tentative parse, whether or not an error
1109 CP_PARSER_STATUS_KIND_COMMITTED
1110 } cp_parser_status_kind
;
1112 typedef struct cp_parser_expression_stack_entry
1115 enum tree_code tree_type
;
1117 } cp_parser_expression_stack_entry
;
1119 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1120 entries because precedence levels on the stack are monotonically
1122 typedef struct cp_parser_expression_stack_entry
1123 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1125 /* Context that is saved and restored when parsing tentatively. */
1126 typedef struct cp_parser_context
GTY (())
1128 /* If this is a tentative parsing context, the status of the
1130 enum cp_parser_status_kind status
;
1131 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1132 that are looked up in this context must be looked up both in the
1133 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1134 the context of the containing expression. */
1137 /* The next parsing context in the stack. */
1138 struct cp_parser_context
*next
;
1139 } cp_parser_context
;
1143 /* Constructors and destructors. */
1145 static cp_parser_context
*cp_parser_context_new
1146 (cp_parser_context
*);
1148 /* Class variables. */
1150 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1152 /* The operator-precedence table used by cp_parser_binary_expression.
1153 Transformed into an associative array (binops_by_token) by
1156 static const cp_parser_binary_operations_map_node binops
[] = {
1157 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1158 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1160 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1161 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1162 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1164 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1165 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1167 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1168 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1170 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1171 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1172 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1173 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1174 { CPP_MIN
, MIN_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1175 { CPP_MAX
, MAX_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1177 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1178 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1180 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1182 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1184 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1186 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1188 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1191 /* The same as binops, but initialized by cp_parser_new so that
1192 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1194 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1196 /* Constructors and destructors. */
1198 /* Construct a new context. The context below this one on the stack
1199 is given by NEXT. */
1201 static cp_parser_context
*
1202 cp_parser_context_new (cp_parser_context
* next
)
1204 cp_parser_context
*context
;
1206 /* Allocate the storage. */
1207 if (cp_parser_context_free_list
!= NULL
)
1209 /* Pull the first entry from the free list. */
1210 context
= cp_parser_context_free_list
;
1211 cp_parser_context_free_list
= context
->next
;
1212 memset (context
, 0, sizeof (*context
));
1215 context
= GGC_CNEW (cp_parser_context
);
1217 /* No errors have occurred yet in this context. */
1218 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1219 /* If this is not the bottomost context, copy information that we
1220 need from the previous context. */
1223 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1224 expression, then we are parsing one in this context, too. */
1225 context
->object_type
= next
->object_type
;
1226 /* Thread the stack. */
1227 context
->next
= next
;
1233 /* The cp_parser structure represents the C++ parser. */
1235 typedef struct cp_parser
GTY(())
1237 /* The lexer from which we are obtaining tokens. */
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look.
1245 This value is not cleared automatically after a name is looked
1246 up, so we must be careful to clear it before starting a new look
1247 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1248 will look up `Z' in the scope of `X', rather than the current
1249 scope.) Unfortunately, it is difficult to tell when name lookup
1250 is complete, because we sometimes peek at a token, look it up,
1251 and then decide not to consume it. */
1254 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1255 last lookup took place. OBJECT_SCOPE is used if an expression
1256 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1257 respectively. QUALIFYING_SCOPE is used for an expression of the
1258 form "X::Y"; it refers to X. */
1260 tree qualifying_scope
;
1262 /* A stack of parsing contexts. All but the bottom entry on the
1263 stack will be tentative contexts.
1265 We parse tentatively in order to determine which construct is in
1266 use in some situations. For example, in order to determine
1267 whether a statement is an expression-statement or a
1268 declaration-statement we parse it tentatively as a
1269 declaration-statement. If that fails, we then reparse the same
1270 token stream as an expression-statement. */
1271 cp_parser_context
*context
;
1273 /* True if we are parsing GNU C++. If this flag is not set, then
1274 GNU extensions are not recognized. */
1275 bool allow_gnu_extensions_p
;
1277 /* TRUE if the `>' token should be interpreted as the greater-than
1278 operator. FALSE if it is the end of a template-id or
1279 template-parameter-list. */
1280 bool greater_than_is_operator_p
;
1282 /* TRUE if default arguments are allowed within a parameter list
1283 that starts at this point. FALSE if only a gnu extension makes
1284 them permissible. */
1285 bool default_arg_ok_p
;
1287 /* TRUE if we are parsing an integral constant-expression. See
1288 [expr.const] for a precise definition. */
1289 bool integral_constant_expression_p
;
1291 /* TRUE if we are parsing an integral constant-expression -- but a
1292 non-constant expression should be permitted as well. This flag
1293 is used when parsing an array bound so that GNU variable-length
1294 arrays are tolerated. */
1295 bool allow_non_integral_constant_expression_p
;
1297 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1298 been seen that makes the expression non-constant. */
1299 bool non_integral_constant_expression_p
;
1301 /* TRUE if local variable names and `this' are forbidden in the
1303 bool local_variables_forbidden_p
;
1305 /* TRUE if the declaration we are parsing is part of a
1306 linkage-specification of the form `extern string-literal
1308 bool in_unbraced_linkage_specification_p
;
1310 /* TRUE if we are presently parsing a declarator, after the
1311 direct-declarator. */
1312 bool in_declarator_p
;
1314 /* TRUE if we are presently parsing a template-argument-list. */
1315 bool in_template_argument_list_p
;
1317 /* TRUE if we are presently parsing the body of an
1318 iteration-statement. */
1319 bool in_iteration_statement_p
;
1321 /* TRUE if we are presently parsing the body of a switch
1323 bool in_switch_statement_p
;
1325 /* TRUE if we are parsing a type-id in an expression context. In
1326 such a situation, both "type (expr)" and "type (type)" are valid
1328 bool in_type_id_in_expr_p
;
1330 /* TRUE if we are currently in a header file where declarations are
1331 implicitly extern "C". */
1332 bool implicit_extern_c
;
1334 /* TRUE if strings in expressions should be translated to the execution
1336 bool translate_strings_p
;
1338 /* If non-NULL, then we are parsing a construct where new type
1339 definitions are not permitted. The string stored here will be
1340 issued as an error message if a type is defined. */
1341 const char *type_definition_forbidden_message
;
1343 /* A list of lists. The outer list is a stack, used for member
1344 functions of local classes. At each level there are two sub-list,
1345 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1346 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1347 TREE_VALUE's. The functions are chained in reverse declaration
1350 The TREE_PURPOSE sublist contains those functions with default
1351 arguments that need post processing, and the TREE_VALUE sublist
1352 contains those functions with definitions that need post
1355 These lists can only be processed once the outermost class being
1356 defined is complete. */
1357 tree unparsed_functions_queues
;
1359 /* The number of classes whose definitions are currently in
1361 unsigned num_classes_being_defined
;
1363 /* The number of template parameter lists that apply directly to the
1364 current declaration. */
1365 unsigned num_template_parameter_lists
;
1368 /* The type of a function that parses some kind of expression. */
1369 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1373 /* Constructors and destructors. */
1375 static cp_parser
*cp_parser_new
1378 /* Routines to parse various constructs.
1380 Those that return `tree' will return the error_mark_node (rather
1381 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1382 Sometimes, they will return an ordinary node if error-recovery was
1383 attempted, even though a parse error occurred. So, to check
1384 whether or not a parse error occurred, you should always use
1385 cp_parser_error_occurred. If the construct is optional (indicated
1386 either by an `_opt' in the name of the function that does the
1387 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1388 the construct is not present. */
1390 /* Lexical conventions [gram.lex] */
1392 static tree cp_parser_identifier
1394 static tree cp_parser_string_literal
1395 (cp_parser
*, bool, bool);
1397 /* Basic concepts [gram.basic] */
1399 static bool cp_parser_translation_unit
1402 /* Expressions [gram.expr] */
1404 static tree cp_parser_primary_expression
1405 (cp_parser
*, cp_id_kind
*, tree
*);
1406 static tree cp_parser_id_expression
1407 (cp_parser
*, bool, bool, bool *, bool);
1408 static tree cp_parser_unqualified_id
1409 (cp_parser
*, bool, bool, bool);
1410 static tree cp_parser_nested_name_specifier_opt
1411 (cp_parser
*, bool, bool, bool, bool);
1412 static tree cp_parser_nested_name_specifier
1413 (cp_parser
*, bool, bool, bool, bool);
1414 static tree cp_parser_class_or_namespace_name
1415 (cp_parser
*, bool, bool, bool, bool, bool);
1416 static tree cp_parser_postfix_expression
1417 (cp_parser
*, bool);
1418 static tree cp_parser_postfix_open_square_expression
1419 (cp_parser
*, tree
, bool);
1420 static tree cp_parser_postfix_dot_deref_expression
1421 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1422 static tree cp_parser_parenthesized_expression_list
1423 (cp_parser
*, bool, bool *);
1424 static void cp_parser_pseudo_destructor_name
1425 (cp_parser
*, tree
*, tree
*);
1426 static tree cp_parser_unary_expression
1427 (cp_parser
*, bool);
1428 static enum tree_code cp_parser_unary_operator
1430 static tree cp_parser_new_expression
1432 static tree cp_parser_new_placement
1434 static tree cp_parser_new_type_id
1435 (cp_parser
*, tree
*);
1436 static cp_declarator
*cp_parser_new_declarator_opt
1438 static cp_declarator
*cp_parser_direct_new_declarator
1440 static tree cp_parser_new_initializer
1442 static tree cp_parser_delete_expression
1444 static tree cp_parser_cast_expression
1445 (cp_parser
*, bool);
1446 static tree cp_parser_binary_expression
1448 static tree cp_parser_question_colon_clause
1449 (cp_parser
*, tree
);
1450 static tree cp_parser_assignment_expression
1452 static enum tree_code cp_parser_assignment_operator_opt
1454 static tree cp_parser_expression
1456 static tree cp_parser_constant_expression
1457 (cp_parser
*, bool, bool *);
1458 static tree cp_parser_builtin_offsetof
1461 /* Statements [gram.stmt.stmt] */
1463 static void cp_parser_statement
1464 (cp_parser
*, tree
);
1465 static tree cp_parser_labeled_statement
1466 (cp_parser
*, tree
);
1467 static tree cp_parser_expression_statement
1468 (cp_parser
*, tree
);
1469 static tree cp_parser_compound_statement
1470 (cp_parser
*, tree
, bool);
1471 static void cp_parser_statement_seq_opt
1472 (cp_parser
*, tree
);
1473 static tree cp_parser_selection_statement
1475 static tree cp_parser_condition
1477 static tree cp_parser_iteration_statement
1479 static void cp_parser_for_init_statement
1481 static tree cp_parser_jump_statement
1483 static void cp_parser_declaration_statement
1486 static tree cp_parser_implicitly_scoped_statement
1488 static void cp_parser_already_scoped_statement
1491 /* Declarations [gram.dcl.dcl] */
1493 static void cp_parser_declaration_seq_opt
1495 static void cp_parser_declaration
1497 static void cp_parser_block_declaration
1498 (cp_parser
*, bool);
1499 static void cp_parser_simple_declaration
1500 (cp_parser
*, bool);
1501 static void cp_parser_decl_specifier_seq
1502 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1503 static tree cp_parser_storage_class_specifier_opt
1505 static tree cp_parser_function_specifier_opt
1506 (cp_parser
*, cp_decl_specifier_seq
*);
1507 static tree cp_parser_type_specifier
1508 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1510 static tree cp_parser_simple_type_specifier
1511 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1512 static tree cp_parser_type_name
1514 static tree cp_parser_elaborated_type_specifier
1515 (cp_parser
*, bool, bool);
1516 static tree cp_parser_enum_specifier
1518 static void cp_parser_enumerator_list
1519 (cp_parser
*, tree
);
1520 static void cp_parser_enumerator_definition
1521 (cp_parser
*, tree
);
1522 static tree cp_parser_namespace_name
1524 static void cp_parser_namespace_definition
1526 static void cp_parser_namespace_body
1528 static tree cp_parser_qualified_namespace_specifier
1530 static void cp_parser_namespace_alias_definition
1532 static void cp_parser_using_declaration
1534 static void cp_parser_using_directive
1536 static void cp_parser_asm_definition
1538 static void cp_parser_linkage_specification
1541 /* Declarators [gram.dcl.decl] */
1543 static tree cp_parser_init_declarator
1544 (cp_parser
*, cp_decl_specifier_seq
*, bool, bool, int, bool *);
1545 static cp_declarator
*cp_parser_declarator
1546 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1547 static cp_declarator
*cp_parser_direct_declarator
1548 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1549 static enum tree_code cp_parser_ptr_operator
1550 (cp_parser
*, tree
*, cp_cv_quals
*);
1551 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1553 static tree cp_parser_declarator_id
1555 static tree cp_parser_type_id
1557 static void cp_parser_type_specifier_seq
1558 (cp_parser
*, cp_decl_specifier_seq
*);
1559 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1561 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1562 (cp_parser
*, bool *);
1563 static cp_parameter_declarator
*cp_parser_parameter_declaration
1564 (cp_parser
*, bool, bool *);
1565 static void cp_parser_function_body
1567 static tree cp_parser_initializer
1568 (cp_parser
*, bool *, bool *);
1569 static tree cp_parser_initializer_clause
1570 (cp_parser
*, bool *);
1571 static tree cp_parser_initializer_list
1572 (cp_parser
*, bool *);
1574 static bool cp_parser_ctor_initializer_opt_and_function_body
1577 /* Classes [gram.class] */
1579 static tree cp_parser_class_name
1580 (cp_parser
*, bool, bool, bool, bool, bool, bool);
1581 static tree cp_parser_class_specifier
1583 static tree cp_parser_class_head
1584 (cp_parser
*, bool *, tree
*);
1585 static enum tag_types cp_parser_class_key
1587 static void cp_parser_member_specification_opt
1589 static void cp_parser_member_declaration
1591 static tree cp_parser_pure_specifier
1593 static tree cp_parser_constant_initializer
1596 /* Derived classes [gram.class.derived] */
1598 static tree cp_parser_base_clause
1600 static tree cp_parser_base_specifier
1603 /* Special member functions [gram.special] */
1605 static tree cp_parser_conversion_function_id
1607 static tree cp_parser_conversion_type_id
1609 static cp_declarator
*cp_parser_conversion_declarator_opt
1611 static bool cp_parser_ctor_initializer_opt
1613 static void cp_parser_mem_initializer_list
1615 static tree cp_parser_mem_initializer
1617 static tree cp_parser_mem_initializer_id
1620 /* Overloading [gram.over] */
1622 static tree cp_parser_operator_function_id
1624 static tree cp_parser_operator
1627 /* Templates [gram.temp] */
1629 static void cp_parser_template_declaration
1630 (cp_parser
*, bool);
1631 static tree cp_parser_template_parameter_list
1633 static tree cp_parser_template_parameter
1634 (cp_parser
*, bool *);
1635 static tree cp_parser_type_parameter
1637 static tree cp_parser_template_id
1638 (cp_parser
*, bool, bool, bool);
1639 static tree cp_parser_template_name
1640 (cp_parser
*, bool, bool, bool, bool *);
1641 static tree cp_parser_template_argument_list
1643 static tree cp_parser_template_argument
1645 static void cp_parser_explicit_instantiation
1647 static void cp_parser_explicit_specialization
1650 /* Exception handling [gram.exception] */
1652 static tree cp_parser_try_block
1654 static bool cp_parser_function_try_block
1656 static void cp_parser_handler_seq
1658 static void cp_parser_handler
1660 static tree cp_parser_exception_declaration
1662 static tree cp_parser_throw_expression
1664 static tree cp_parser_exception_specification_opt
1666 static tree cp_parser_type_id_list
1669 /* GNU Extensions */
1671 static tree cp_parser_asm_specification_opt
1673 static tree cp_parser_asm_operand_list
1675 static tree cp_parser_asm_clobber_list
1677 static tree cp_parser_attributes_opt
1679 static tree cp_parser_attribute_list
1681 static bool cp_parser_extension_opt
1682 (cp_parser
*, int *);
1683 static void cp_parser_label_declaration
1686 /* Utility Routines */
1688 static tree cp_parser_lookup_name
1689 (cp_parser
*, tree
, bool, bool, bool, bool, bool *);
1690 static tree cp_parser_lookup_name_simple
1691 (cp_parser
*, tree
);
1692 static tree cp_parser_maybe_treat_template_as_class
1694 static bool cp_parser_check_declarator_template_parameters
1695 (cp_parser
*, cp_declarator
*);
1696 static bool cp_parser_check_template_parameters
1697 (cp_parser
*, unsigned);
1698 static tree cp_parser_simple_cast_expression
1700 static tree cp_parser_global_scope_opt
1701 (cp_parser
*, bool);
1702 static bool cp_parser_constructor_declarator_p
1703 (cp_parser
*, bool);
1704 static tree cp_parser_function_definition_from_specifiers_and_declarator
1705 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1706 static tree cp_parser_function_definition_after_declarator
1707 (cp_parser
*, bool);
1708 static void cp_parser_template_declaration_after_export
1709 (cp_parser
*, bool);
1710 static tree cp_parser_single_declaration
1711 (cp_parser
*, bool, bool *);
1712 static tree cp_parser_functional_cast
1713 (cp_parser
*, tree
);
1714 static tree cp_parser_save_member_function_body
1715 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1716 static tree cp_parser_enclosed_template_argument_list
1718 static void cp_parser_save_default_args
1719 (cp_parser
*, tree
);
1720 static void cp_parser_late_parsing_for_member
1721 (cp_parser
*, tree
);
1722 static void cp_parser_late_parsing_default_args
1723 (cp_parser
*, tree
);
1724 static tree cp_parser_sizeof_operand
1725 (cp_parser
*, enum rid
);
1726 static bool cp_parser_declares_only_class_p
1728 static void cp_parser_set_storage_class
1729 (cp_decl_specifier_seq
*, cp_storage_class
);
1730 static void cp_parser_set_decl_spec_type
1731 (cp_decl_specifier_seq
*, tree
, bool);
1732 static bool cp_parser_friend_p
1733 (const cp_decl_specifier_seq
*);
1734 static cp_token
*cp_parser_require
1735 (cp_parser
*, enum cpp_ttype
, const char *);
1736 static cp_token
*cp_parser_require_keyword
1737 (cp_parser
*, enum rid
, const char *);
1738 static bool cp_parser_token_starts_function_definition_p
1740 static bool cp_parser_next_token_starts_class_definition_p
1742 static bool cp_parser_next_token_ends_template_argument_p
1744 static bool cp_parser_nth_token_starts_template_argument_list_p
1745 (cp_parser
*, size_t);
1746 static enum tag_types cp_parser_token_is_class_key
1748 static void cp_parser_check_class_key
1749 (enum tag_types
, tree type
);
1750 static void cp_parser_check_access_in_redeclaration
1752 static bool cp_parser_optional_template_keyword
1754 static void cp_parser_pre_parsed_nested_name_specifier
1756 static void cp_parser_cache_group
1757 (cp_parser
*, enum cpp_ttype
, unsigned);
1758 static void cp_parser_parse_tentatively
1760 static void cp_parser_commit_to_tentative_parse
1762 static void cp_parser_abort_tentative_parse
1764 static bool cp_parser_parse_definitely
1766 static inline bool cp_parser_parsing_tentatively
1768 static bool cp_parser_committed_to_tentative_parse
1770 static void cp_parser_error
1771 (cp_parser
*, const char *);
1772 static void cp_parser_name_lookup_error
1773 (cp_parser
*, tree
, tree
, const char *);
1774 static bool cp_parser_simulate_error
1776 static void cp_parser_check_type_definition
1778 static void cp_parser_check_for_definition_in_return_type
1779 (cp_declarator
*, int);
1780 static void cp_parser_check_for_invalid_template_id
1781 (cp_parser
*, tree
);
1782 static bool cp_parser_non_integral_constant_expression
1783 (cp_parser
*, const char *);
1784 static void cp_parser_diagnose_invalid_type_name
1785 (cp_parser
*, tree
, tree
);
1786 static bool cp_parser_parse_and_diagnose_invalid_type_name
1788 static int cp_parser_skip_to_closing_parenthesis
1789 (cp_parser
*, bool, bool, bool);
1790 static void cp_parser_skip_to_end_of_statement
1792 static void cp_parser_consume_semicolon_at_end_of_statement
1794 static void cp_parser_skip_to_end_of_block_or_statement
1796 static void cp_parser_skip_to_closing_brace
1798 static void cp_parser_skip_until_found
1799 (cp_parser
*, enum cpp_ttype
, const char *);
1800 static bool cp_parser_error_occurred
1802 static bool cp_parser_allow_gnu_extensions_p
1804 static bool cp_parser_is_string_literal
1806 static bool cp_parser_is_keyword
1807 (cp_token
*, enum rid
);
1808 static tree cp_parser_make_typename_type
1809 (cp_parser
*, tree
, tree
);
1811 /* Returns nonzero if we are parsing tentatively. */
1814 cp_parser_parsing_tentatively (cp_parser
* parser
)
1816 return parser
->context
->next
!= NULL
;
1819 /* Returns nonzero if TOKEN is a string literal. */
1822 cp_parser_is_string_literal (cp_token
* token
)
1824 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1827 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1830 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1832 return token
->keyword
== keyword
;
1835 /* If not parsing tentatively, issue a diagnostic of the form
1836 FILE:LINE: MESSAGE before TOKEN
1837 where TOKEN is the next token in the input stream. MESSAGE
1838 (specified by the caller) is usually of the form "expected
1842 cp_parser_error (cp_parser
* parser
, const char* message
)
1844 if (!cp_parser_simulate_error (parser
))
1846 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1847 /* This diagnostic makes more sense if it is tagged to the line
1848 of the token we just peeked at. */
1849 cp_lexer_set_source_position_from_token (token
);
1850 c_parse_error (message
,
1851 /* Because c_parser_error does not understand
1852 CPP_KEYWORD, keywords are treated like
1854 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1859 /* Issue an error about name-lookup failing. NAME is the
1860 IDENTIFIER_NODE DECL is the result of
1861 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1862 the thing that we hoped to find. */
1865 cp_parser_name_lookup_error (cp_parser
* parser
,
1868 const char* desired
)
1870 /* If name lookup completely failed, tell the user that NAME was not
1872 if (decl
== error_mark_node
)
1874 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1875 error ("%<%D::%D%> has not been declared",
1876 parser
->scope
, name
);
1877 else if (parser
->scope
== global_namespace
)
1878 error ("%<::%D%> has not been declared", name
);
1879 else if (parser
->object_scope
1880 && !CLASS_TYPE_P (parser
->object_scope
))
1881 error ("request for member %qD in non-class type %qT",
1882 name
, parser
->object_scope
);
1883 else if (parser
->object_scope
)
1884 error ("%<%T::%D%> has not been declared",
1885 parser
->object_scope
, name
);
1887 error ("`%D' has not been declared", name
);
1889 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1890 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1891 else if (parser
->scope
== global_namespace
)
1892 error ("%<::%D%> %s", name
, desired
);
1894 error ("%qD %s", name
, desired
);
1897 /* If we are parsing tentatively, remember that an error has occurred
1898 during this tentative parse. Returns true if the error was
1899 simulated; false if a message should be issued by the caller. */
1902 cp_parser_simulate_error (cp_parser
* parser
)
1904 if (cp_parser_parsing_tentatively (parser
)
1905 && !cp_parser_committed_to_tentative_parse (parser
))
1907 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1913 /* This function is called when a type is defined. If type
1914 definitions are forbidden at this point, an error message is
1918 cp_parser_check_type_definition (cp_parser
* parser
)
1920 /* If types are forbidden here, issue a message. */
1921 if (parser
->type_definition_forbidden_message
)
1922 /* Use `%s' to print the string in case there are any escape
1923 characters in the message. */
1924 error ("%s", parser
->type_definition_forbidden_message
);
1927 /* This function is called when a declaration is parsed. If
1928 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1929 indicates that a type was defined in the decl-specifiers for DECL,
1930 then an error is issued. */
1933 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
1934 int declares_class_or_enum
)
1936 /* [dcl.fct] forbids type definitions in return types.
1937 Unfortunately, it's not easy to know whether or not we are
1938 processing a return type until after the fact. */
1940 && (declarator
->kind
== cdk_pointer
1941 || declarator
->kind
== cdk_reference
1942 || declarator
->kind
== cdk_ptrmem
))
1943 declarator
= declarator
->declarator
;
1945 && declarator
->kind
== cdk_function
1946 && declares_class_or_enum
& 2)
1947 error ("new types may not be defined in a return type");
1950 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1951 "<" in any valid C++ program. If the next token is indeed "<",
1952 issue a message warning the user about what appears to be an
1953 invalid attempt to form a template-id. */
1956 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1965 error ("%qT is not a template", type
);
1966 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1967 error ("%qE is not a template", type
);
1969 error ("invalid template-id");
1970 /* Remember the location of the invalid "<". */
1971 if (cp_parser_parsing_tentatively (parser
)
1972 && !cp_parser_committed_to_tentative_parse (parser
))
1974 token
= cp_lexer_peek_token (parser
->lexer
);
1975 token
= cp_lexer_prev_token (parser
->lexer
, token
);
1976 start
= cp_lexer_token_difference (parser
->lexer
,
1977 parser
->lexer
->buffer
,
1982 /* Consume the "<". */
1983 cp_lexer_consume_token (parser
->lexer
);
1984 /* Parse the template arguments. */
1985 cp_parser_enclosed_template_argument_list (parser
);
1986 /* Permanently remove the invalid template arguments so that
1987 this error message is not issued again. */
1990 token
= cp_lexer_advance_token (parser
->lexer
,
1991 parser
->lexer
->buffer
,
1993 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
1998 /* If parsing an integral constant-expression, issue an error message
1999 about the fact that THING appeared and return true. Otherwise,
2000 return false, marking the current expression as non-constant. */
2003 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2006 if (parser
->integral_constant_expression_p
)
2008 if (!parser
->allow_non_integral_constant_expression_p
)
2010 error ("%s cannot appear in a constant-expression", thing
);
2013 parser
->non_integral_constant_expression_p
= true;
2018 /* Emit a diagnostic for an invalid type name. Consider also if it is
2019 qualified or not and the result of a lookup, to provide a better
2023 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2025 tree decl
, old_scope
;
2026 /* Try to lookup the identifier. */
2027 old_scope
= parser
->scope
;
2028 parser
->scope
= scope
;
2029 decl
= cp_parser_lookup_name_simple (parser
, id
);
2030 parser
->scope
= old_scope
;
2031 /* If the lookup found a template-name, it means that the user forgot
2032 to specify an argument list. Emit an useful error message. */
2033 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2034 error ("invalid use of template-name %qE without an argument list",
2036 else if (!parser
->scope
)
2038 /* Issue an error message. */
2039 error ("%qE does not name a type", id
);
2040 /* If we're in a template class, it's possible that the user was
2041 referring to a type from a base class. For example:
2043 template <typename T> struct A { typedef T X; };
2044 template <typename T> struct B : public A<T> { X x; };
2046 The user should have said "typename A<T>::X". */
2047 if (processing_template_decl
&& current_class_type
)
2051 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2055 tree base_type
= BINFO_TYPE (b
);
2056 if (CLASS_TYPE_P (base_type
)
2057 && dependent_type_p (base_type
))
2060 /* Go from a particular instantiation of the
2061 template (which will have an empty TYPE_FIELDs),
2062 to the main version. */
2063 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2064 for (field
= TYPE_FIELDS (base_type
);
2066 field
= TREE_CHAIN (field
))
2067 if (TREE_CODE (field
) == TYPE_DECL
2068 && DECL_NAME (field
) == id
)
2070 inform ("(perhaps `typename %T::%E' was intended)",
2071 BINFO_TYPE (b
), id
);
2080 /* Here we diagnose qualified-ids where the scope is actually correct,
2081 but the identifier does not resolve to a valid type name. */
2084 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2085 error ("%qE in namespace %qE does not name a type",
2087 else if (TYPE_P (parser
->scope
))
2088 error ("q%E in class %qT does not name a type", id
, parser
->scope
);
2094 /* Check for a common situation where a type-name should be present,
2095 but is not, and issue a sensible error message. Returns true if an
2096 invalid type-name was detected.
2098 The situation handled by this function are variable declarations of the
2099 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2100 Usually, `ID' should name a type, but if we got here it means that it
2101 does not. We try to emit the best possible error message depending on
2102 how exactly the id-expression looks like.
2106 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2110 cp_parser_parse_tentatively (parser
);
2111 id
= cp_parser_id_expression (parser
,
2112 /*template_keyword_p=*/false,
2113 /*check_dependency_p=*/true,
2114 /*template_p=*/NULL
,
2115 /*declarator_p=*/true);
2116 /* After the id-expression, there should be a plain identifier,
2117 otherwise this is not a simple variable declaration. Also, if
2118 the scope is dependent, we cannot do much. */
2119 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2120 || (parser
->scope
&& TYPE_P (parser
->scope
)
2121 && dependent_type_p (parser
->scope
)))
2123 cp_parser_abort_tentative_parse (parser
);
2126 if (!cp_parser_parse_definitely (parser
)
2127 || TREE_CODE (id
) != IDENTIFIER_NODE
)
2130 /* Emit a diagnostic for the invalid type. */
2131 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2132 /* Skip to the end of the declaration; there's no point in
2133 trying to process it. */
2134 cp_parser_skip_to_end_of_block_or_statement (parser
);
2138 /* Consume tokens up to, and including, the next non-nested closing `)'.
2139 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2140 are doing error recovery. Returns -1 if OR_COMMA is true and we
2141 found an unnested comma. */
2144 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2149 unsigned paren_depth
= 0;
2150 unsigned brace_depth
= 0;
2153 if (recovering
&& !or_comma
&& cp_parser_parsing_tentatively (parser
)
2154 && !cp_parser_committed_to_tentative_parse (parser
))
2161 /* If we've run out of tokens, then there is no closing `)'. */
2162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2168 token
= cp_lexer_peek_token (parser
->lexer
);
2170 /* This matches the processing in skip_to_end_of_statement. */
2171 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2176 if (token
->type
== CPP_OPEN_BRACE
)
2178 if (token
->type
== CPP_CLOSE_BRACE
)
2186 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2187 && !brace_depth
&& !paren_depth
)
2195 /* If it is an `(', we have entered another level of nesting. */
2196 if (token
->type
== CPP_OPEN_PAREN
)
2198 /* If it is a `)', then we might be done. */
2199 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2202 cp_lexer_consume_token (parser
->lexer
);
2210 /* Consume the token. */
2211 cp_lexer_consume_token (parser
->lexer
);
2217 /* Consume tokens until we reach the end of the current statement.
2218 Normally, that will be just before consuming a `;'. However, if a
2219 non-nested `}' comes first, then we stop before consuming that. */
2222 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2224 unsigned nesting_depth
= 0;
2230 /* Peek at the next token. */
2231 token
= cp_lexer_peek_token (parser
->lexer
);
2232 /* If we've run out of tokens, stop. */
2233 if (token
->type
== CPP_EOF
)
2235 /* If the next token is a `;', we have reached the end of the
2237 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2239 /* If the next token is a non-nested `}', then we have reached
2240 the end of the current block. */
2241 if (token
->type
== CPP_CLOSE_BRACE
)
2243 /* If this is a non-nested `}', stop before consuming it.
2244 That way, when confronted with something like:
2248 we stop before consuming the closing `}', even though we
2249 have not yet reached a `;'. */
2250 if (nesting_depth
== 0)
2252 /* If it is the closing `}' for a block that we have
2253 scanned, stop -- but only after consuming the token.
2259 we will stop after the body of the erroneously declared
2260 function, but before consuming the following `typedef'
2262 if (--nesting_depth
== 0)
2264 cp_lexer_consume_token (parser
->lexer
);
2268 /* If it the next token is a `{', then we are entering a new
2269 block. Consume the entire block. */
2270 else if (token
->type
== CPP_OPEN_BRACE
)
2272 /* Consume the token. */
2273 cp_lexer_consume_token (parser
->lexer
);
2277 /* This function is called at the end of a statement or declaration.
2278 If the next token is a semicolon, it is consumed; otherwise, error
2279 recovery is attempted. */
2282 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2284 /* Look for the trailing `;'. */
2285 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2287 /* If there is additional (erroneous) input, skip to the end of
2289 cp_parser_skip_to_end_of_statement (parser
);
2290 /* If the next token is now a `;', consume it. */
2291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2292 cp_lexer_consume_token (parser
->lexer
);
2296 /* Skip tokens until we have consumed an entire block, or until we
2297 have consumed a non-nested `;'. */
2300 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2302 unsigned nesting_depth
= 0;
2308 /* Peek at the next token. */
2309 token
= cp_lexer_peek_token (parser
->lexer
);
2310 /* If we've run out of tokens, stop. */
2311 if (token
->type
== CPP_EOF
)
2313 /* If the next token is a `;', we have reached the end of the
2315 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2317 /* Consume the `;'. */
2318 cp_lexer_consume_token (parser
->lexer
);
2321 /* Consume the token. */
2322 token
= cp_lexer_consume_token (parser
->lexer
);
2323 /* If the next token is a non-nested `}', then we have reached
2324 the end of the current block. */
2325 if (token
->type
== CPP_CLOSE_BRACE
2326 && (nesting_depth
== 0 || --nesting_depth
== 0))
2328 /* If it the next token is a `{', then we are entering a new
2329 block. Consume the entire block. */
2330 if (token
->type
== CPP_OPEN_BRACE
)
2335 /* Skip tokens until a non-nested closing curly brace is the next
2339 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2341 unsigned nesting_depth
= 0;
2347 /* Peek at the next token. */
2348 token
= cp_lexer_peek_token (parser
->lexer
);
2349 /* If we've run out of tokens, stop. */
2350 if (token
->type
== CPP_EOF
)
2352 /* If the next token is a non-nested `}', then we have reached
2353 the end of the current block. */
2354 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2356 /* If it the next token is a `{', then we are entering a new
2357 block. Consume the entire block. */
2358 else if (token
->type
== CPP_OPEN_BRACE
)
2360 /* Consume the token. */
2361 cp_lexer_consume_token (parser
->lexer
);
2365 /* This is a simple wrapper around make_typename_type. When the id is
2366 an unresolved identifier node, we can provide a superior diagnostic
2367 using cp_parser_diagnose_invalid_type_name. */
2370 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2373 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2375 result
= make_typename_type (scope
, id
, /*complain=*/0);
2376 if (result
== error_mark_node
)
2377 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2380 return make_typename_type (scope
, id
, tf_error
);
2384 /* Create a new C++ parser. */
2387 cp_parser_new (void)
2393 /* cp_lexer_new_main is called before calling ggc_alloc because
2394 cp_lexer_new_main might load a PCH file. */
2395 lexer
= cp_lexer_new_main ();
2397 /* Initialize the binops_by_token so that we can get the tree
2398 directly from the token. */
2399 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2400 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2402 parser
= GGC_CNEW (cp_parser
);
2403 parser
->lexer
= lexer
;
2404 parser
->context
= cp_parser_context_new (NULL
);
2406 /* For now, we always accept GNU extensions. */
2407 parser
->allow_gnu_extensions_p
= 1;
2409 /* The `>' token is a greater-than operator, not the end of a
2411 parser
->greater_than_is_operator_p
= true;
2413 parser
->default_arg_ok_p
= true;
2415 /* We are not parsing a constant-expression. */
2416 parser
->integral_constant_expression_p
= false;
2417 parser
->allow_non_integral_constant_expression_p
= false;
2418 parser
->non_integral_constant_expression_p
= false;
2420 /* Local variable names are not forbidden. */
2421 parser
->local_variables_forbidden_p
= false;
2423 /* We are not processing an `extern "C"' declaration. */
2424 parser
->in_unbraced_linkage_specification_p
= false;
2426 /* We are not processing a declarator. */
2427 parser
->in_declarator_p
= false;
2429 /* We are not processing a template-argument-list. */
2430 parser
->in_template_argument_list_p
= false;
2432 /* We are not in an iteration statement. */
2433 parser
->in_iteration_statement_p
= false;
2435 /* We are not in a switch statement. */
2436 parser
->in_switch_statement_p
= false;
2438 /* We are not parsing a type-id inside an expression. */
2439 parser
->in_type_id_in_expr_p
= false;
2441 /* Declarations aren't implicitly extern "C". */
2442 parser
->implicit_extern_c
= false;
2444 /* String literals should be translated to the execution character set. */
2445 parser
->translate_strings_p
= true;
2447 /* The unparsed function queue is empty. */
2448 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2450 /* There are no classes being defined. */
2451 parser
->num_classes_being_defined
= 0;
2453 /* No template parameters apply. */
2454 parser
->num_template_parameter_lists
= 0;
2459 /* Create a cp_lexer structure which will emit the tokens in CACHE
2460 and push it onto the parser's lexer stack. This is used for delayed
2461 parsing of in-class method bodies and default arguments, and should
2462 not be confused with tentative parsing. */
2464 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2466 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2467 lexer
->next
= parser
->lexer
;
2468 parser
->lexer
= lexer
;
2470 /* Move the current source position to that of the first token in the
2472 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2475 /* Pop the top lexer off the parser stack. This is never used for the
2476 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2478 cp_parser_pop_lexer (cp_parser
*parser
)
2480 cp_lexer
*lexer
= parser
->lexer
;
2481 parser
->lexer
= lexer
->next
;
2482 cp_lexer_destroy (lexer
);
2484 /* Put the current source position back where it was before this
2485 lexer was pushed. */
2486 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2489 /* Lexical conventions [gram.lex] */
2491 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2495 cp_parser_identifier (cp_parser
* parser
)
2499 /* Look for the identifier. */
2500 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2501 /* Return the value. */
2502 return token
? token
->value
: error_mark_node
;
2505 /* Parse a sequence of adjacent string constants. Returns a
2506 TREE_STRING representing the combined, nul-terminated string
2507 constant. If TRANSLATE is true, translate the string to the
2508 execution character set. If WIDE_OK is true, a wide string is
2511 C++98 [lex.string] says that if a narrow string literal token is
2512 adjacent to a wide string literal token, the behavior is undefined.
2513 However, C99 6.4.5p4 says that this results in a wide string literal.
2514 We follow C99 here, for consistency with the C front end.
2516 This code is largely lifted from lex_string() in c-lex.c.
2518 FUTURE: ObjC++ will need to handle @-strings here. */
2520 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2525 struct obstack str_ob
;
2526 cpp_string str
, istr
, *strs
;
2529 tok
= cp_lexer_peek_token (parser
->lexer
);
2530 if (!cp_parser_is_string_literal (tok
))
2532 cp_parser_error (parser
, "expected string-literal");
2533 return error_mark_node
;
2536 /* Try to avoid the overhead of creating and destroying an obstack
2537 for the common case of just one string. */
2538 if (!cp_parser_is_string_literal
2539 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2541 cp_lexer_consume_token (parser
->lexer
);
2543 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2544 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2546 if (tok
->type
== CPP_WSTRING
)
2553 gcc_obstack_init (&str_ob
);
2558 cp_lexer_consume_token (parser
->lexer
);
2560 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2561 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2562 if (tok
->type
== CPP_WSTRING
)
2565 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2567 tok
= cp_lexer_peek_token (parser
->lexer
);
2569 while (cp_parser_is_string_literal (tok
));
2571 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2574 if (wide
&& !wide_ok
)
2576 cp_parser_error (parser
, "a wide string is invalid in this context");
2580 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2581 (parse_in
, strs
, count
, &istr
, wide
))
2583 value
= build_string (istr
.len
, (char *)istr
.text
);
2584 free ((void *)istr
.text
);
2586 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2587 value
= fix_string_type (value
);
2590 /* cpp_interpret_string has issued an error. */
2591 value
= error_mark_node
;
2594 obstack_free (&str_ob
, 0);
2600 /* Basic concepts [gram.basic] */
2602 /* Parse a translation-unit.
2605 declaration-seq [opt]
2607 Returns TRUE if all went well. */
2610 cp_parser_translation_unit (cp_parser
* parser
)
2612 /* The address of the first non-permanent object on the declarator
2614 static void *declarator_obstack_base
;
2618 /* Create the declarator obstack, if necessary. */
2619 if (!cp_error_declarator
)
2621 gcc_obstack_init (&declarator_obstack
);
2622 /* Create the error declarator. */
2623 cp_error_declarator
= make_declarator (cdk_error
);
2624 /* Create the empty parameter list. */
2625 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2626 /* Remember where the base of the declarator obstack lies. */
2627 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2632 cp_parser_declaration_seq_opt (parser
);
2634 /* If there are no tokens left then all went well. */
2635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2637 /* Consume the EOF token. */
2638 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2640 /* Get rid of the token array; we don't need it any more. */
2641 cp_lexer_destroy (parser
->lexer
);
2642 parser
->lexer
= NULL
;
2644 /* This file might have been a context that's implicitly extern
2645 "C". If so, pop the lang context. (Only relevant for PCH.) */
2646 if (parser
->implicit_extern_c
)
2648 pop_lang_context ();
2649 parser
->implicit_extern_c
= false;
2653 finish_translation_unit ();
2660 cp_parser_error (parser
, "expected declaration");
2666 /* Make sure the declarator obstack was fully cleaned up. */
2667 gcc_assert (obstack_next_free (&declarator_obstack
)
2668 == declarator_obstack_base
);
2670 /* All went well. */
2674 /* Expressions [gram.expr] */
2676 /* Parse a primary-expression.
2687 ( compound-statement )
2688 __builtin_va_arg ( assignment-expression , type-id )
2693 Returns a representation of the expression.
2695 *IDK indicates what kind of id-expression (if any) was present.
2697 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2698 used as the operand of a pointer-to-member. In that case,
2699 *QUALIFYING_CLASS gives the class that is used as the qualifying
2700 class in the pointer-to-member. */
2703 cp_parser_primary_expression (cp_parser
*parser
,
2705 tree
*qualifying_class
)
2709 /* Assume the primary expression is not an id-expression. */
2710 *idk
= CP_ID_KIND_NONE
;
2711 /* And that it cannot be used as pointer-to-member. */
2712 *qualifying_class
= NULL_TREE
;
2714 /* Peek at the next token. */
2715 token
= cp_lexer_peek_token (parser
->lexer
);
2716 switch (token
->type
)
2727 token
= cp_lexer_consume_token (parser
->lexer
);
2728 return token
->value
;
2732 /* ??? Should wide strings be allowed when parser->translate_strings_p
2733 is false (i.e. in attributes)? If not, we can kill the third
2734 argument to cp_parser_string_literal. */
2735 return cp_parser_string_literal (parser
,
2736 parser
->translate_strings_p
,
2739 case CPP_OPEN_PAREN
:
2742 bool saved_greater_than_is_operator_p
;
2744 /* Consume the `('. */
2745 cp_lexer_consume_token (parser
->lexer
);
2746 /* Within a parenthesized expression, a `>' token is always
2747 the greater-than operator. */
2748 saved_greater_than_is_operator_p
2749 = parser
->greater_than_is_operator_p
;
2750 parser
->greater_than_is_operator_p
= true;
2751 /* If we see `( { ' then we are looking at the beginning of
2752 a GNU statement-expression. */
2753 if (cp_parser_allow_gnu_extensions_p (parser
)
2754 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2756 /* Statement-expressions are not allowed by the standard. */
2758 pedwarn ("ISO C++ forbids braced-groups within expressions");
2760 /* And they're not allowed outside of a function-body; you
2761 cannot, for example, write:
2763 int i = ({ int j = 3; j + 1; });
2765 at class or namespace scope. */
2766 if (!at_function_scope_p ())
2767 error ("statement-expressions are allowed only inside functions");
2768 /* Start the statement-expression. */
2769 expr
= begin_stmt_expr ();
2770 /* Parse the compound-statement. */
2771 cp_parser_compound_statement (parser
, expr
, false);
2773 expr
= finish_stmt_expr (expr
, false);
2777 /* Parse the parenthesized expression. */
2778 expr
= cp_parser_expression (parser
);
2779 /* Let the front end know that this expression was
2780 enclosed in parentheses. This matters in case, for
2781 example, the expression is of the form `A::B', since
2782 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2784 finish_parenthesized_expr (expr
);
2786 /* The `>' token might be the end of a template-id or
2787 template-parameter-list now. */
2788 parser
->greater_than_is_operator_p
2789 = saved_greater_than_is_operator_p
;
2790 /* Consume the `)'. */
2791 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2792 cp_parser_skip_to_end_of_statement (parser
);
2798 switch (token
->keyword
)
2800 /* These two are the boolean literals. */
2802 cp_lexer_consume_token (parser
->lexer
);
2803 return boolean_true_node
;
2805 cp_lexer_consume_token (parser
->lexer
);
2806 return boolean_false_node
;
2808 /* The `__null' literal. */
2810 cp_lexer_consume_token (parser
->lexer
);
2813 /* Recognize the `this' keyword. */
2815 cp_lexer_consume_token (parser
->lexer
);
2816 if (parser
->local_variables_forbidden_p
)
2818 error ("%<this%> may not be used in this context");
2819 return error_mark_node
;
2821 /* Pointers cannot appear in constant-expressions. */
2822 if (cp_parser_non_integral_constant_expression (parser
,
2824 return error_mark_node
;
2825 return finish_this_expr ();
2827 /* The `operator' keyword can be the beginning of an
2832 case RID_FUNCTION_NAME
:
2833 case RID_PRETTY_FUNCTION_NAME
:
2834 case RID_C99_FUNCTION_NAME
:
2835 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2836 __func__ are the names of variables -- but they are
2837 treated specially. Therefore, they are handled here,
2838 rather than relying on the generic id-expression logic
2839 below. Grammatically, these names are id-expressions.
2841 Consume the token. */
2842 token
= cp_lexer_consume_token (parser
->lexer
);
2843 /* Look up the name. */
2844 return finish_fname (token
->value
);
2851 /* The `__builtin_va_arg' construct is used to handle
2852 `va_arg'. Consume the `__builtin_va_arg' token. */
2853 cp_lexer_consume_token (parser
->lexer
);
2854 /* Look for the opening `('. */
2855 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2856 /* Now, parse the assignment-expression. */
2857 expression
= cp_parser_assignment_expression (parser
);
2858 /* Look for the `,'. */
2859 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2860 /* Parse the type-id. */
2861 type
= cp_parser_type_id (parser
);
2862 /* Look for the closing `)'. */
2863 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2864 /* Using `va_arg' in a constant-expression is not
2866 if (cp_parser_non_integral_constant_expression (parser
,
2868 return error_mark_node
;
2869 return build_x_va_arg (expression
, type
);
2873 return cp_parser_builtin_offsetof (parser
);
2876 cp_parser_error (parser
, "expected primary-expression");
2877 return error_mark_node
;
2880 /* An id-expression can start with either an identifier, a
2881 `::' as the beginning of a qualified-id, or the "operator"
2885 case CPP_TEMPLATE_ID
:
2886 case CPP_NESTED_NAME_SPECIFIER
:
2890 const char *error_msg
;
2893 /* Parse the id-expression. */
2895 = cp_parser_id_expression (parser
,
2896 /*template_keyword_p=*/false,
2897 /*check_dependency_p=*/true,
2898 /*template_p=*/NULL
,
2899 /*declarator_p=*/false);
2900 if (id_expression
== error_mark_node
)
2901 return error_mark_node
;
2902 /* If we have a template-id, then no further lookup is
2903 required. If the template-id was for a template-class, we
2904 will sometimes have a TYPE_DECL at this point. */
2905 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2906 || TREE_CODE (id_expression
) == TYPE_DECL
)
2907 decl
= id_expression
;
2908 /* Look up the name. */
2913 decl
= cp_parser_lookup_name (parser
, id_expression
,
2915 /*is_template=*/false,
2916 /*is_namespace=*/false,
2917 /*check_dependency=*/true,
2919 /* If the lookup was ambiguous, an error will already have
2922 return error_mark_node
;
2923 /* If name lookup gives us a SCOPE_REF, then the
2924 qualifying scope was dependent. Just propagate the
2926 if (TREE_CODE (decl
) == SCOPE_REF
)
2928 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2929 *qualifying_class
= TREE_OPERAND (decl
, 0);
2932 /* Check to see if DECL is a local variable in a context
2933 where that is forbidden. */
2934 if (parser
->local_variables_forbidden_p
2935 && local_variable_p (decl
))
2937 /* It might be that we only found DECL because we are
2938 trying to be generous with pre-ISO scoping rules.
2939 For example, consider:
2943 for (int i = 0; i < 10; ++i) {}
2944 extern void f(int j = i);
2947 Here, name look up will originally find the out
2948 of scope `i'. We need to issue a warning message,
2949 but then use the global `i'. */
2950 decl
= check_for_out_of_scope_variable (decl
);
2951 if (local_variable_p (decl
))
2953 error ("local variable %qD may not appear in this context",
2955 return error_mark_node
;
2960 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2961 idk
, qualifying_class
,
2962 parser
->integral_constant_expression_p
,
2963 parser
->allow_non_integral_constant_expression_p
,
2964 &parser
->non_integral_constant_expression_p
,
2967 cp_parser_error (parser
, error_msg
);
2971 /* Anything else is an error. */
2973 cp_parser_error (parser
, "expected primary-expression");
2974 return error_mark_node
;
2978 /* Parse an id-expression.
2985 :: [opt] nested-name-specifier template [opt] unqualified-id
2987 :: operator-function-id
2990 Return a representation of the unqualified portion of the
2991 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2992 a `::' or nested-name-specifier.
2994 Often, if the id-expression was a qualified-id, the caller will
2995 want to make a SCOPE_REF to represent the qualified-id. This
2996 function does not do this in order to avoid wastefully creating
2997 SCOPE_REFs when they are not required.
2999 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3002 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3003 uninstantiated templates.
3005 If *TEMPLATE_P is non-NULL, it is set to true iff the
3006 `template' keyword is used to explicitly indicate that the entity
3007 named is a template.
3009 If DECLARATOR_P is true, the id-expression is appearing as part of
3010 a declarator, rather than as part of an expression. */
3013 cp_parser_id_expression (cp_parser
*parser
,
3014 bool template_keyword_p
,
3015 bool check_dependency_p
,
3019 bool global_scope_p
;
3020 bool nested_name_specifier_p
;
3022 /* Assume the `template' keyword was not used. */
3024 *template_p
= false;
3026 /* Look for the optional `::' operator. */
3028 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3030 /* Look for the optional nested-name-specifier. */
3031 nested_name_specifier_p
3032 = (cp_parser_nested_name_specifier_opt (parser
,
3033 /*typename_keyword_p=*/false,
3038 /* If there is a nested-name-specifier, then we are looking at
3039 the first qualified-id production. */
3040 if (nested_name_specifier_p
)
3043 tree saved_object_scope
;
3044 tree saved_qualifying_scope
;
3045 tree unqualified_id
;
3048 /* See if the next token is the `template' keyword. */
3050 template_p
= &is_template
;
3051 *template_p
= cp_parser_optional_template_keyword (parser
);
3052 /* Name lookup we do during the processing of the
3053 unqualified-id might obliterate SCOPE. */
3054 saved_scope
= parser
->scope
;
3055 saved_object_scope
= parser
->object_scope
;
3056 saved_qualifying_scope
= parser
->qualifying_scope
;
3057 /* Process the final unqualified-id. */
3058 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3061 /* Restore the SAVED_SCOPE for our caller. */
3062 parser
->scope
= saved_scope
;
3063 parser
->object_scope
= saved_object_scope
;
3064 parser
->qualifying_scope
= saved_qualifying_scope
;
3066 return unqualified_id
;
3068 /* Otherwise, if we are in global scope, then we are looking at one
3069 of the other qualified-id productions. */
3070 else if (global_scope_p
)
3075 /* Peek at the next token. */
3076 token
= cp_lexer_peek_token (parser
->lexer
);
3078 /* If it's an identifier, and the next token is not a "<", then
3079 we can avoid the template-id case. This is an optimization
3080 for this common case. */
3081 if (token
->type
== CPP_NAME
3082 && !cp_parser_nth_token_starts_template_argument_list_p
3084 return cp_parser_identifier (parser
);
3086 cp_parser_parse_tentatively (parser
);
3087 /* Try a template-id. */
3088 id
= cp_parser_template_id (parser
,
3089 /*template_keyword_p=*/false,
3090 /*check_dependency_p=*/true,
3092 /* If that worked, we're done. */
3093 if (cp_parser_parse_definitely (parser
))
3096 /* Peek at the next token. (Changes in the token buffer may
3097 have invalidated the pointer obtained above.) */
3098 token
= cp_lexer_peek_token (parser
->lexer
);
3100 switch (token
->type
)
3103 return cp_parser_identifier (parser
);
3106 if (token
->keyword
== RID_OPERATOR
)
3107 return cp_parser_operator_function_id (parser
);
3111 cp_parser_error (parser
, "expected id-expression");
3112 return error_mark_node
;
3116 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3117 /*check_dependency_p=*/true,
3121 /* Parse an unqualified-id.
3125 operator-function-id
3126 conversion-function-id
3130 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3131 keyword, in a construct like `A::template ...'.
3133 Returns a representation of unqualified-id. For the `identifier'
3134 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3135 production a BIT_NOT_EXPR is returned; the operand of the
3136 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3137 other productions, see the documentation accompanying the
3138 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3139 names are looked up in uninstantiated templates. If DECLARATOR_P
3140 is true, the unqualified-id is appearing as part of a declarator,
3141 rather than as part of an expression. */
3144 cp_parser_unqualified_id (cp_parser
* parser
,
3145 bool template_keyword_p
,
3146 bool check_dependency_p
,
3151 /* Peek at the next token. */
3152 token
= cp_lexer_peek_token (parser
->lexer
);
3154 switch (token
->type
)
3160 /* We don't know yet whether or not this will be a
3162 cp_parser_parse_tentatively (parser
);
3163 /* Try a template-id. */
3164 id
= cp_parser_template_id (parser
, template_keyword_p
,
3167 /* If it worked, we're done. */
3168 if (cp_parser_parse_definitely (parser
))
3170 /* Otherwise, it's an ordinary identifier. */
3171 return cp_parser_identifier (parser
);
3174 case CPP_TEMPLATE_ID
:
3175 return cp_parser_template_id (parser
, template_keyword_p
,
3182 tree qualifying_scope
;
3186 /* Consume the `~' token. */
3187 cp_lexer_consume_token (parser
->lexer
);
3188 /* Parse the class-name. The standard, as written, seems to
3191 template <typename T> struct S { ~S (); };
3192 template <typename T> S<T>::~S() {}
3194 is invalid, since `~' must be followed by a class-name, but
3195 `S<T>' is dependent, and so not known to be a class.
3196 That's not right; we need to look in uninstantiated
3197 templates. A further complication arises from:
3199 template <typename T> void f(T t) {
3203 Here, it is not possible to look up `T' in the scope of `T'
3204 itself. We must look in both the current scope, and the
3205 scope of the containing complete expression.
3207 Yet another issue is:
3216 The standard does not seem to say that the `S' in `~S'
3217 should refer to the type `S' and not the data member
3220 /* DR 244 says that we look up the name after the "~" in the
3221 same scope as we looked up the qualifying name. That idea
3222 isn't fully worked out; it's more complicated than that. */
3223 scope
= parser
->scope
;
3224 object_scope
= parser
->object_scope
;
3225 qualifying_scope
= parser
->qualifying_scope
;
3227 /* If the name is of the form "X::~X" it's OK. */
3228 if (scope
&& TYPE_P (scope
)
3229 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3230 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3232 && (cp_lexer_peek_token (parser
->lexer
)->value
3233 == TYPE_IDENTIFIER (scope
)))
3235 cp_lexer_consume_token (parser
->lexer
);
3236 return build_nt (BIT_NOT_EXPR
, scope
);
3239 /* If there was an explicit qualification (S::~T), first look
3240 in the scope given by the qualification (i.e., S). */
3243 cp_parser_parse_tentatively (parser
);
3244 type_decl
= cp_parser_class_name (parser
,
3245 /*typename_keyword_p=*/false,
3246 /*template_keyword_p=*/false,
3248 /*check_dependency=*/false,
3249 /*class_head_p=*/false,
3251 if (cp_parser_parse_definitely (parser
))
3252 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3254 /* In "N::S::~S", look in "N" as well. */
3255 if (scope
&& qualifying_scope
)
3257 cp_parser_parse_tentatively (parser
);
3258 parser
->scope
= qualifying_scope
;
3259 parser
->object_scope
= NULL_TREE
;
3260 parser
->qualifying_scope
= NULL_TREE
;
3262 = cp_parser_class_name (parser
,
3263 /*typename_keyword_p=*/false,
3264 /*template_keyword_p=*/false,
3266 /*check_dependency=*/false,
3267 /*class_head_p=*/false,
3269 if (cp_parser_parse_definitely (parser
))
3270 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3272 /* In "p->S::~T", look in the scope given by "*p" as well. */
3273 else if (object_scope
)
3275 cp_parser_parse_tentatively (parser
);
3276 parser
->scope
= object_scope
;
3277 parser
->object_scope
= NULL_TREE
;
3278 parser
->qualifying_scope
= NULL_TREE
;
3280 = cp_parser_class_name (parser
,
3281 /*typename_keyword_p=*/false,
3282 /*template_keyword_p=*/false,
3284 /*check_dependency=*/false,
3285 /*class_head_p=*/false,
3287 if (cp_parser_parse_definitely (parser
))
3288 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3290 /* Look in the surrounding context. */
3291 parser
->scope
= NULL_TREE
;
3292 parser
->object_scope
= NULL_TREE
;
3293 parser
->qualifying_scope
= NULL_TREE
;
3295 = cp_parser_class_name (parser
,
3296 /*typename_keyword_p=*/false,
3297 /*template_keyword_p=*/false,
3299 /*check_dependency=*/false,
3300 /*class_head_p=*/false,
3302 /* If an error occurred, assume that the name of the
3303 destructor is the same as the name of the qualifying
3304 class. That allows us to keep parsing after running
3305 into ill-formed destructor names. */
3306 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3307 return build_nt (BIT_NOT_EXPR
, scope
);
3308 else if (type_decl
== error_mark_node
)
3309 return error_mark_node
;
3313 A typedef-name that names a class shall not be used as the
3314 identifier in the declarator for a destructor declaration. */
3316 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3317 && !DECL_SELF_REFERENCE_P (type_decl
))
3318 error ("typedef-name %qD used as destructor declarator",
3321 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3325 if (token
->keyword
== RID_OPERATOR
)
3329 /* This could be a template-id, so we try that first. */
3330 cp_parser_parse_tentatively (parser
);
3331 /* Try a template-id. */
3332 id
= cp_parser_template_id (parser
, template_keyword_p
,
3333 /*check_dependency_p=*/true,
3335 /* If that worked, we're done. */
3336 if (cp_parser_parse_definitely (parser
))
3338 /* We still don't know whether we're looking at an
3339 operator-function-id or a conversion-function-id. */
3340 cp_parser_parse_tentatively (parser
);
3341 /* Try an operator-function-id. */
3342 id
= cp_parser_operator_function_id (parser
);
3343 /* If that didn't work, try a conversion-function-id. */
3344 if (!cp_parser_parse_definitely (parser
))
3345 id
= cp_parser_conversion_function_id (parser
);
3352 cp_parser_error (parser
, "expected unqualified-id");
3353 return error_mark_node
;
3357 /* Parse an (optional) nested-name-specifier.
3359 nested-name-specifier:
3360 class-or-namespace-name :: nested-name-specifier [opt]
3361 class-or-namespace-name :: template nested-name-specifier [opt]
3363 PARSER->SCOPE should be set appropriately before this function is
3364 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3365 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3368 Sets PARSER->SCOPE to the class (TYPE) or namespace
3369 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3370 it unchanged if there is no nested-name-specifier. Returns the new
3371 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3373 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3374 part of a declaration and/or decl-specifier. */
3377 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3378 bool typename_keyword_p
,
3379 bool check_dependency_p
,
3381 bool is_declaration
)
3383 bool success
= false;
3384 tree access_check
= NULL_TREE
;
3388 /* If the next token corresponds to a nested name specifier, there
3389 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3390 false, it may have been true before, in which case something
3391 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3392 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3393 CHECK_DEPENDENCY_P is false, we have to fall through into the
3395 if (check_dependency_p
3396 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3398 cp_parser_pre_parsed_nested_name_specifier (parser
);
3399 return parser
->scope
;
3402 /* Remember where the nested-name-specifier starts. */
3403 if (cp_parser_parsing_tentatively (parser
)
3404 && !cp_parser_committed_to_tentative_parse (parser
))
3406 token
= cp_lexer_peek_token (parser
->lexer
);
3407 start
= cp_lexer_token_difference (parser
->lexer
,
3408 parser
->lexer
->buffer
,
3414 push_deferring_access_checks (dk_deferred
);
3420 tree saved_qualifying_scope
;
3421 bool template_keyword_p
;
3423 /* Spot cases that cannot be the beginning of a
3424 nested-name-specifier. */
3425 token
= cp_lexer_peek_token (parser
->lexer
);
3427 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3428 the already parsed nested-name-specifier. */
3429 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3431 /* Grab the nested-name-specifier and continue the loop. */
3432 cp_parser_pre_parsed_nested_name_specifier (parser
);
3437 /* Spot cases that cannot be the beginning of a
3438 nested-name-specifier. On the second and subsequent times
3439 through the loop, we look for the `template' keyword. */
3440 if (success
&& token
->keyword
== RID_TEMPLATE
)
3442 /* A template-id can start a nested-name-specifier. */
3443 else if (token
->type
== CPP_TEMPLATE_ID
)
3447 /* If the next token is not an identifier, then it is
3448 definitely not a class-or-namespace-name. */
3449 if (token
->type
!= CPP_NAME
)
3451 /* If the following token is neither a `<' (to begin a
3452 template-id), nor a `::', then we are not looking at a
3453 nested-name-specifier. */
3454 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3455 if (token
->type
!= CPP_SCOPE
3456 && !cp_parser_nth_token_starts_template_argument_list_p
3461 /* The nested-name-specifier is optional, so we parse
3463 cp_parser_parse_tentatively (parser
);
3465 /* Look for the optional `template' keyword, if this isn't the
3466 first time through the loop. */
3468 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3470 template_keyword_p
= false;
3472 /* Save the old scope since the name lookup we are about to do
3473 might destroy it. */
3474 old_scope
= parser
->scope
;
3475 saved_qualifying_scope
= parser
->qualifying_scope
;
3476 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3477 look up names in "X<T>::I" in order to determine that "Y" is
3478 a template. So, if we have a typename at this point, we make
3479 an effort to look through it. */
3481 && !typename_keyword_p
3483 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3484 parser
->scope
= resolve_typename_type (parser
->scope
,
3485 /*only_current_p=*/false);
3486 /* Parse the qualifying entity. */
3488 = cp_parser_class_or_namespace_name (parser
,
3494 /* Look for the `::' token. */
3495 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3497 /* If we found what we wanted, we keep going; otherwise, we're
3499 if (!cp_parser_parse_definitely (parser
))
3501 bool error_p
= false;
3503 /* Restore the OLD_SCOPE since it was valid before the
3504 failed attempt at finding the last
3505 class-or-namespace-name. */
3506 parser
->scope
= old_scope
;
3507 parser
->qualifying_scope
= saved_qualifying_scope
;
3508 /* If the next token is an identifier, and the one after
3509 that is a `::', then any valid interpretation would have
3510 found a class-or-namespace-name. */
3511 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3512 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3514 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3517 token
= cp_lexer_consume_token (parser
->lexer
);
3522 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3523 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3524 error ("%qD used without template parameters", decl
);
3526 cp_parser_name_lookup_error
3527 (parser
, token
->value
, decl
,
3528 "is not a class or namespace");
3529 parser
->scope
= NULL_TREE
;
3531 /* Treat this as a successful nested-name-specifier
3536 If the name found is not a class-name (clause
3537 _class_) or namespace-name (_namespace.def_), the
3538 program is ill-formed. */
3541 cp_lexer_consume_token (parser
->lexer
);
3546 /* We've found one valid nested-name-specifier. */
3548 /* Make sure we look in the right scope the next time through
3550 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3551 ? TREE_TYPE (new_scope
)
3553 /* If it is a class scope, try to complete it; we are about to
3554 be looking up names inside the class. */
3555 if (TYPE_P (parser
->scope
)
3556 /* Since checking types for dependency can be expensive,
3557 avoid doing it if the type is already complete. */
3558 && !COMPLETE_TYPE_P (parser
->scope
)
3559 /* Do not try to complete dependent types. */
3560 && !dependent_type_p (parser
->scope
))
3561 complete_type (parser
->scope
);
3564 /* Retrieve any deferred checks. Do not pop this access checks yet
3565 so the memory will not be reclaimed during token replacing below. */
3566 access_check
= get_deferred_access_checks ();
3568 /* If parsing tentatively, replace the sequence of tokens that makes
3569 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3570 token. That way, should we re-parse the token stream, we will
3571 not have to repeat the effort required to do the parse, nor will
3572 we issue duplicate error messages. */
3573 if (success
&& start
>= 0)
3575 /* Find the token that corresponds to the start of the
3577 token
= cp_lexer_advance_token (parser
->lexer
,
3578 parser
->lexer
->buffer
,
3581 /* Reset the contents of the START token. */
3582 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3583 token
->value
= build_tree_list (access_check
, parser
->scope
);
3584 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3585 token
->keyword
= RID_MAX
;
3586 /* Purge all subsequent tokens. */
3587 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3590 pop_deferring_access_checks ();
3591 return success
? parser
->scope
: NULL_TREE
;
3594 /* Parse a nested-name-specifier. See
3595 cp_parser_nested_name_specifier_opt for details. This function
3596 behaves identically, except that it will an issue an error if no
3597 nested-name-specifier is present, and it will return
3598 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3602 cp_parser_nested_name_specifier (cp_parser
*parser
,
3603 bool typename_keyword_p
,
3604 bool check_dependency_p
,
3606 bool is_declaration
)
3610 /* Look for the nested-name-specifier. */
3611 scope
= cp_parser_nested_name_specifier_opt (parser
,
3616 /* If it was not present, issue an error message. */
3619 cp_parser_error (parser
, "expected nested-name-specifier");
3620 parser
->scope
= NULL_TREE
;
3621 return error_mark_node
;
3627 /* Parse a class-or-namespace-name.
3629 class-or-namespace-name:
3633 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3634 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3635 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3636 TYPE_P is TRUE iff the next name should be taken as a class-name,
3637 even the same name is declared to be another entity in the same
3640 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3641 specified by the class-or-namespace-name. If neither is found the
3642 ERROR_MARK_NODE is returned. */
3645 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3646 bool typename_keyword_p
,
3647 bool template_keyword_p
,
3648 bool check_dependency_p
,
3650 bool is_declaration
)
3653 tree saved_qualifying_scope
;
3654 tree saved_object_scope
;
3658 /* Before we try to parse the class-name, we must save away the
3659 current PARSER->SCOPE since cp_parser_class_name will destroy
3661 saved_scope
= parser
->scope
;
3662 saved_qualifying_scope
= parser
->qualifying_scope
;
3663 saved_object_scope
= parser
->object_scope
;
3664 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3665 there is no need to look for a namespace-name. */
3666 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3668 cp_parser_parse_tentatively (parser
);
3669 scope
= cp_parser_class_name (parser
,
3674 /*class_head_p=*/false,
3676 /* If that didn't work, try for a namespace-name. */
3677 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3679 /* Restore the saved scope. */
3680 parser
->scope
= saved_scope
;
3681 parser
->qualifying_scope
= saved_qualifying_scope
;
3682 parser
->object_scope
= saved_object_scope
;
3683 /* If we are not looking at an identifier followed by the scope
3684 resolution operator, then this is not part of a
3685 nested-name-specifier. (Note that this function is only used
3686 to parse the components of a nested-name-specifier.) */
3687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3688 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3689 return error_mark_node
;
3690 scope
= cp_parser_namespace_name (parser
);
3696 /* Parse a postfix-expression.
3700 postfix-expression [ expression ]
3701 postfix-expression ( expression-list [opt] )
3702 simple-type-specifier ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier identifier
3704 ( expression-list [opt] )
3705 typename :: [opt] nested-name-specifier template [opt] template-id
3706 ( expression-list [opt] )
3707 postfix-expression . template [opt] id-expression
3708 postfix-expression -> template [opt] id-expression
3709 postfix-expression . pseudo-destructor-name
3710 postfix-expression -> pseudo-destructor-name
3711 postfix-expression ++
3712 postfix-expression --
3713 dynamic_cast < type-id > ( expression )
3714 static_cast < type-id > ( expression )
3715 reinterpret_cast < type-id > ( expression )
3716 const_cast < type-id > ( expression )
3717 typeid ( expression )
3723 ( type-id ) { initializer-list , [opt] }
3725 This extension is a GNU version of the C99 compound-literal
3726 construct. (The C99 grammar uses `type-name' instead of `type-id',
3727 but they are essentially the same concept.)
3729 If ADDRESS_P is true, the postfix expression is the operand of the
3732 Returns a representation of the expression. */
3735 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3739 cp_id_kind idk
= CP_ID_KIND_NONE
;
3740 tree postfix_expression
= NULL_TREE
;
3741 /* Non-NULL only if the current postfix-expression can be used to
3742 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3743 class used to qualify the member. */
3744 tree qualifying_class
= NULL_TREE
;
3746 /* Peek at the next token. */
3747 token
= cp_lexer_peek_token (parser
->lexer
);
3748 /* Some of the productions are determined by keywords. */
3749 keyword
= token
->keyword
;
3759 const char *saved_message
;
3761 /* All of these can be handled in the same way from the point
3762 of view of parsing. Begin by consuming the token
3763 identifying the cast. */
3764 cp_lexer_consume_token (parser
->lexer
);
3766 /* New types cannot be defined in the cast. */
3767 saved_message
= parser
->type_definition_forbidden_message
;
3768 parser
->type_definition_forbidden_message
3769 = "types may not be defined in casts";
3771 /* Look for the opening `<'. */
3772 cp_parser_require (parser
, CPP_LESS
, "`<'");
3773 /* Parse the type to which we are casting. */
3774 type
= cp_parser_type_id (parser
);
3775 /* Look for the closing `>'. */
3776 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3777 /* Restore the old message. */
3778 parser
->type_definition_forbidden_message
= saved_message
;
3780 /* And the expression which is being cast. */
3781 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3782 expression
= cp_parser_expression (parser
);
3783 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3785 /* Only type conversions to integral or enumeration types
3786 can be used in constant-expressions. */
3787 if (parser
->integral_constant_expression_p
3788 && !dependent_type_p (type
)
3789 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3790 && (cp_parser_non_integral_constant_expression
3792 "a cast to a type other than an integral or "
3793 "enumeration type")))
3794 return error_mark_node
;
3800 = build_dynamic_cast (type
, expression
);
3804 = build_static_cast (type
, expression
);
3808 = build_reinterpret_cast (type
, expression
);
3812 = build_const_cast (type
, expression
);
3823 const char *saved_message
;
3824 bool saved_in_type_id_in_expr_p
;
3826 /* Consume the `typeid' token. */
3827 cp_lexer_consume_token (parser
->lexer
);
3828 /* Look for the `(' token. */
3829 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3830 /* Types cannot be defined in a `typeid' expression. */
3831 saved_message
= parser
->type_definition_forbidden_message
;
3832 parser
->type_definition_forbidden_message
3833 = "types may not be defined in a `typeid\' expression";
3834 /* We can't be sure yet whether we're looking at a type-id or an
3836 cp_parser_parse_tentatively (parser
);
3837 /* Try a type-id first. */
3838 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3839 parser
->in_type_id_in_expr_p
= true;
3840 type
= cp_parser_type_id (parser
);
3841 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3842 /* Look for the `)' token. Otherwise, we can't be sure that
3843 we're not looking at an expression: consider `typeid (int
3844 (3))', for example. */
3845 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3846 /* If all went well, simply lookup the type-id. */
3847 if (cp_parser_parse_definitely (parser
))
3848 postfix_expression
= get_typeid (type
);
3849 /* Otherwise, fall back to the expression variant. */
3854 /* Look for an expression. */
3855 expression
= cp_parser_expression (parser
);
3856 /* Compute its typeid. */
3857 postfix_expression
= build_typeid (expression
);
3858 /* Look for the `)' token. */
3859 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3861 /* `typeid' may not appear in an integral constant expression. */
3862 if (cp_parser_non_integral_constant_expression(parser
,
3863 "`typeid' operator"))
3864 return error_mark_node
;
3865 /* Restore the saved message. */
3866 parser
->type_definition_forbidden_message
= saved_message
;
3872 bool template_p
= false;
3876 /* Consume the `typename' token. */
3877 cp_lexer_consume_token (parser
->lexer
);
3878 /* Look for the optional `::' operator. */
3879 cp_parser_global_scope_opt (parser
,
3880 /*current_scope_valid_p=*/false);
3881 /* Look for the nested-name-specifier. */
3882 cp_parser_nested_name_specifier (parser
,
3883 /*typename_keyword_p=*/true,
3884 /*check_dependency_p=*/true,
3886 /*is_declaration=*/true);
3887 /* Look for the optional `template' keyword. */
3888 template_p
= cp_parser_optional_template_keyword (parser
);
3889 /* We don't know whether we're looking at a template-id or an
3891 cp_parser_parse_tentatively (parser
);
3892 /* Try a template-id. */
3893 id
= cp_parser_template_id (parser
, template_p
,
3894 /*check_dependency_p=*/true,
3895 /*is_declaration=*/true);
3896 /* If that didn't work, try an identifier. */
3897 if (!cp_parser_parse_definitely (parser
))
3898 id
= cp_parser_identifier (parser
);
3899 /* If we look up a template-id in a non-dependent qualifying
3900 scope, there's no need to create a dependent type. */
3901 if (TREE_CODE (id
) == TYPE_DECL
3902 && !dependent_type_p (parser
->scope
))
3903 type
= TREE_TYPE (id
);
3904 /* Create a TYPENAME_TYPE to represent the type to which the
3905 functional cast is being performed. */
3907 type
= make_typename_type (parser
->scope
, id
,
3910 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3918 /* If the next thing is a simple-type-specifier, we may be
3919 looking at a functional cast. We could also be looking at
3920 an id-expression. So, we try the functional cast, and if
3921 that doesn't work we fall back to the primary-expression. */
3922 cp_parser_parse_tentatively (parser
);
3923 /* Look for the simple-type-specifier. */
3924 type
= cp_parser_simple_type_specifier (parser
,
3925 /*decl_specs=*/NULL
,
3926 CP_PARSER_FLAGS_NONE
);
3927 /* Parse the cast itself. */
3928 if (!cp_parser_error_occurred (parser
))
3930 = cp_parser_functional_cast (parser
, type
);
3931 /* If that worked, we're done. */
3932 if (cp_parser_parse_definitely (parser
))
3935 /* If the functional-cast didn't work out, try a
3936 compound-literal. */
3937 if (cp_parser_allow_gnu_extensions_p (parser
)
3938 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3940 tree initializer_list
= NULL_TREE
;
3941 bool saved_in_type_id_in_expr_p
;
3943 cp_parser_parse_tentatively (parser
);
3944 /* Consume the `('. */
3945 cp_lexer_consume_token (parser
->lexer
);
3946 /* Parse the type. */
3947 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3948 parser
->in_type_id_in_expr_p
= true;
3949 type
= cp_parser_type_id (parser
);
3950 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3951 /* Look for the `)'. */
3952 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3953 /* Look for the `{'. */
3954 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3955 /* If things aren't going well, there's no need to
3957 if (!cp_parser_error_occurred (parser
))
3959 bool non_constant_p
;
3960 /* Parse the initializer-list. */
3962 = cp_parser_initializer_list (parser
, &non_constant_p
);
3963 /* Allow a trailing `,'. */
3964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3965 cp_lexer_consume_token (parser
->lexer
);
3966 /* Look for the final `}'. */
3967 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3969 /* If that worked, we're definitely looking at a
3970 compound-literal expression. */
3971 if (cp_parser_parse_definitely (parser
))
3973 /* Warn the user that a compound literal is not
3974 allowed in standard C++. */
3976 pedwarn ("ISO C++ forbids compound-literals");
3977 /* Form the representation of the compound-literal. */
3979 = finish_compound_literal (type
, initializer_list
);
3984 /* It must be a primary-expression. */
3985 postfix_expression
= cp_parser_primary_expression (parser
,
3992 /* If we were avoiding committing to the processing of a
3993 qualified-id until we knew whether or not we had a
3994 pointer-to-member, we now know. */
3995 if (qualifying_class
)
3999 /* Peek at the next token. */
4000 token
= cp_lexer_peek_token (parser
->lexer
);
4001 done
= (token
->type
!= CPP_OPEN_SQUARE
4002 && token
->type
!= CPP_OPEN_PAREN
4003 && token
->type
!= CPP_DOT
4004 && token
->type
!= CPP_DEREF
4005 && token
->type
!= CPP_PLUS_PLUS
4006 && token
->type
!= CPP_MINUS_MINUS
);
4008 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
4013 return postfix_expression
;
4016 /* Keep looping until the postfix-expression is complete. */
4019 if (idk
== CP_ID_KIND_UNQUALIFIED
4020 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4021 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4022 /* It is not a Koenig lookup function call. */
4024 = unqualified_name_lookup_error (postfix_expression
);
4026 /* Peek at the next token. */
4027 token
= cp_lexer_peek_token (parser
->lexer
);
4029 switch (token
->type
)
4031 case CPP_OPEN_SQUARE
:
4033 = cp_parser_postfix_open_square_expression (parser
,
4036 idk
= CP_ID_KIND_NONE
;
4039 case CPP_OPEN_PAREN
:
4040 /* postfix-expression ( expression-list [opt] ) */
4043 tree args
= (cp_parser_parenthesized_expression_list
4044 (parser
, false, /*non_constant_p=*/NULL
));
4046 if (args
== error_mark_node
)
4048 postfix_expression
= error_mark_node
;
4052 /* Function calls are not permitted in
4053 constant-expressions. */
4054 if (cp_parser_non_integral_constant_expression (parser
,
4057 postfix_expression
= error_mark_node
;
4062 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4064 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4070 = perform_koenig_lookup (postfix_expression
, args
);
4074 = unqualified_fn_lookup_error (postfix_expression
);
4076 /* We do not perform argument-dependent lookup if
4077 normal lookup finds a non-function, in accordance
4078 with the expected resolution of DR 218. */
4079 else if (args
&& is_overloaded_fn (postfix_expression
))
4081 tree fn
= get_first_fn (postfix_expression
);
4083 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4084 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4086 /* Only do argument dependent lookup if regular
4087 lookup does not find a set of member functions.
4088 [basic.lookup.koenig]/2a */
4089 if (!DECL_FUNCTION_MEMBER_P (fn
))
4093 = perform_koenig_lookup (postfix_expression
, args
);
4098 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4100 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4101 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4103 if (processing_template_decl
4104 && (type_dependent_expression_p (instance
)
4105 || (!BASELINK_P (fn
)
4106 && TREE_CODE (fn
) != FIELD_DECL
)
4107 || type_dependent_expression_p (fn
)
4108 || any_type_dependent_arguments_p (args
)))
4111 = build_min_nt (CALL_EXPR
, postfix_expression
,
4116 if (BASELINK_P (fn
))
4118 = (build_new_method_call
4119 (instance
, fn
, args
, NULL_TREE
,
4120 (idk
== CP_ID_KIND_QUALIFIED
4121 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
4124 = finish_call_expr (postfix_expression
, args
,
4125 /*disallow_virtual=*/false,
4126 /*koenig_p=*/false);
4128 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4129 || TREE_CODE (postfix_expression
) == MEMBER_REF
4130 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4131 postfix_expression
= (build_offset_ref_call_from_tree
4132 (postfix_expression
, args
));
4133 else if (idk
== CP_ID_KIND_QUALIFIED
)
4134 /* A call to a static class member, or a namespace-scope
4137 = finish_call_expr (postfix_expression
, args
,
4138 /*disallow_virtual=*/true,
4141 /* All other function calls. */
4143 = finish_call_expr (postfix_expression
, args
,
4144 /*disallow_virtual=*/false,
4147 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4148 idk
= CP_ID_KIND_NONE
;
4154 /* postfix-expression . template [opt] id-expression
4155 postfix-expression . pseudo-destructor-name
4156 postfix-expression -> template [opt] id-expression
4157 postfix-expression -> pseudo-destructor-name */
4159 /* Consume the `.' or `->' operator. */
4160 cp_lexer_consume_token (parser
->lexer
);
4163 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4169 /* postfix-expression ++ */
4170 /* Consume the `++' token. */
4171 cp_lexer_consume_token (parser
->lexer
);
4172 /* Generate a representation for the complete expression. */
4174 = finish_increment_expr (postfix_expression
,
4175 POSTINCREMENT_EXPR
);
4176 /* Increments may not appear in constant-expressions. */
4177 if (cp_parser_non_integral_constant_expression (parser
,
4179 postfix_expression
= error_mark_node
;
4180 idk
= CP_ID_KIND_NONE
;
4183 case CPP_MINUS_MINUS
:
4184 /* postfix-expression -- */
4185 /* Consume the `--' token. */
4186 cp_lexer_consume_token (parser
->lexer
);
4187 /* Generate a representation for the complete expression. */
4189 = finish_increment_expr (postfix_expression
,
4190 POSTDECREMENT_EXPR
);
4191 /* Decrements may not appear in constant-expressions. */
4192 if (cp_parser_non_integral_constant_expression (parser
,
4194 postfix_expression
= error_mark_node
;
4195 idk
= CP_ID_KIND_NONE
;
4199 return postfix_expression
;
4203 /* We should never get here. */
4205 return error_mark_node
;
4208 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4209 by cp_parser_builtin_offsetof. We're looking for
4211 postfix-expression [ expression ]
4213 FOR_OFFSETOF is set if we're being called in that context, which
4214 changes how we deal with integer constant expressions. */
4217 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4218 tree postfix_expression
,
4223 /* Consume the `[' token. */
4224 cp_lexer_consume_token (parser
->lexer
);
4226 /* Parse the index expression. */
4227 /* ??? For offsetof, there is a question of what to allow here. If
4228 offsetof is not being used in an integral constant expression context,
4229 then we *could* get the right answer by computing the value at runtime.
4230 If we are in an integral constant expression context, then we might
4231 could accept any constant expression; hard to say without analysis.
4232 Rather than open the barn door too wide right away, allow only integer
4233 constant expressions here. */
4235 index
= cp_parser_constant_expression (parser
, false, NULL
);
4237 index
= cp_parser_expression (parser
);
4239 /* Look for the closing `]'. */
4240 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4242 /* Build the ARRAY_REF. */
4243 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4245 /* When not doing offsetof, array references are not permitted in
4246 constant-expressions. */
4248 && (cp_parser_non_integral_constant_expression
4249 (parser
, "an array reference")))
4250 postfix_expression
= error_mark_node
;
4252 return postfix_expression
;
4255 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4256 by cp_parser_builtin_offsetof. We're looking for
4258 postfix-expression . template [opt] id-expression
4259 postfix-expression . pseudo-destructor-name
4260 postfix-expression -> template [opt] id-expression
4261 postfix-expression -> pseudo-destructor-name
4263 FOR_OFFSETOF is set if we're being called in that context. That sorta
4264 limits what of the above we'll actually accept, but nevermind.
4265 TOKEN_TYPE is the "." or "->" token, which will already have been
4266 removed from the stream. */
4269 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4270 enum cpp_ttype token_type
,
4271 tree postfix_expression
,
4272 bool for_offsetof
, cp_id_kind
*idk
)
4277 bool pseudo_destructor_p
;
4278 tree scope
= NULL_TREE
;
4280 /* If this is a `->' operator, dereference the pointer. */
4281 if (token_type
== CPP_DEREF
)
4282 postfix_expression
= build_x_arrow (postfix_expression
);
4283 /* Check to see whether or not the expression is type-dependent. */
4284 dependent_p
= type_dependent_expression_p (postfix_expression
);
4285 /* The identifier following the `->' or `.' is not qualified. */
4286 parser
->scope
= NULL_TREE
;
4287 parser
->qualifying_scope
= NULL_TREE
;
4288 parser
->object_scope
= NULL_TREE
;
4289 *idk
= CP_ID_KIND_NONE
;
4290 /* Enter the scope corresponding to the type of the object
4291 given by the POSTFIX_EXPRESSION. */
4292 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4294 scope
= TREE_TYPE (postfix_expression
);
4295 /* According to the standard, no expression should ever have
4296 reference type. Unfortunately, we do not currently match
4297 the standard in this respect in that our internal representation
4298 of an expression may have reference type even when the standard
4299 says it does not. Therefore, we have to manually obtain the
4300 underlying type here. */
4301 scope
= non_reference (scope
);
4302 /* The type of the POSTFIX_EXPRESSION must be complete. */
4303 scope
= complete_type_or_else (scope
, NULL_TREE
);
4304 /* Let the name lookup machinery know that we are processing a
4305 class member access expression. */
4306 parser
->context
->object_type
= scope
;
4307 /* If something went wrong, we want to be able to discern that case,
4308 as opposed to the case where there was no SCOPE due to the type
4309 of expression being dependent. */
4311 scope
= error_mark_node
;
4312 /* If the SCOPE was erroneous, make the various semantic analysis
4313 functions exit quickly -- and without issuing additional error
4315 if (scope
== error_mark_node
)
4316 postfix_expression
= error_mark_node
;
4319 /* Assume this expression is not a pseudo-destructor access. */
4320 pseudo_destructor_p
= false;
4322 /* If the SCOPE is a scalar type, then, if this is a valid program,
4323 we must be looking at a pseudo-destructor-name. */
4324 if (scope
&& SCALAR_TYPE_P (scope
))
4329 cp_parser_parse_tentatively (parser
);
4330 /* Parse the pseudo-destructor-name. */
4332 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4333 if (cp_parser_parse_definitely (parser
))
4335 pseudo_destructor_p
= true;
4337 = finish_pseudo_destructor_expr (postfix_expression
,
4338 s
, TREE_TYPE (type
));
4342 if (!pseudo_destructor_p
)
4344 /* If the SCOPE is not a scalar type, we are looking at an
4345 ordinary class member access expression, rather than a
4346 pseudo-destructor-name. */
4347 template_p
= cp_parser_optional_template_keyword (parser
);
4348 /* Parse the id-expression. */
4349 name
= cp_parser_id_expression (parser
, template_p
,
4350 /*check_dependency_p=*/true,
4351 /*template_p=*/NULL
,
4352 /*declarator_p=*/false);
4353 /* In general, build a SCOPE_REF if the member name is qualified.
4354 However, if the name was not dependent and has already been
4355 resolved; there is no need to build the SCOPE_REF. For example;
4357 struct X { void f(); };
4358 template <typename T> void f(T* t) { t->X::f(); }
4360 Even though "t" is dependent, "X::f" is not and has been resolved
4361 to a BASELINK; there is no need to include scope information. */
4363 /* But we do need to remember that there was an explicit scope for
4364 virtual function calls. */
4366 *idk
= CP_ID_KIND_QUALIFIED
;
4368 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4370 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
4371 parser
->scope
= NULL_TREE
;
4372 parser
->qualifying_scope
= NULL_TREE
;
4373 parser
->object_scope
= NULL_TREE
;
4375 if (scope
&& name
&& BASELINK_P (name
))
4376 adjust_result_of_qualified_name_lookup
4377 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4379 = finish_class_member_access_expr (postfix_expression
, name
);
4382 /* We no longer need to look up names in the scope of the object on
4383 the left-hand side of the `.' or `->' operator. */
4384 parser
->context
->object_type
= NULL_TREE
;
4386 /* Outside of offsetof, these operators may not appear in
4387 constant-expressions. */
4389 && (cp_parser_non_integral_constant_expression
4390 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4391 postfix_expression
= error_mark_node
;
4393 return postfix_expression
;
4396 /* Parse a parenthesized expression-list.
4399 assignment-expression
4400 expression-list, assignment-expression
4405 identifier, expression-list
4407 Returns a TREE_LIST. The TREE_VALUE of each node is a
4408 representation of an assignment-expression. Note that a TREE_LIST
4409 is returned even if there is only a single expression in the list.
4410 error_mark_node is returned if the ( and or ) are
4411 missing. NULL_TREE is returned on no expressions. The parentheses
4412 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4413 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4414 indicates whether or not all of the expressions in the list were
4418 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4419 bool is_attribute_list
,
4420 bool *non_constant_p
)
4422 tree expression_list
= NULL_TREE
;
4423 bool fold_expr_p
= is_attribute_list
;
4424 tree identifier
= NULL_TREE
;
4426 /* Assume all the expressions will be constant. */
4428 *non_constant_p
= false;
4430 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4431 return error_mark_node
;
4433 /* Consume expressions until there are no more. */
4434 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4439 /* At the beginning of attribute lists, check to see if the
4440 next token is an identifier. */
4441 if (is_attribute_list
4442 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4446 /* Consume the identifier. */
4447 token
= cp_lexer_consume_token (parser
->lexer
);
4448 /* Save the identifier. */
4449 identifier
= token
->value
;
4453 /* Parse the next assignment-expression. */
4456 bool expr_non_constant_p
;
4457 expr
= (cp_parser_constant_expression
4458 (parser
, /*allow_non_constant_p=*/true,
4459 &expr_non_constant_p
));
4460 if (expr_non_constant_p
)
4461 *non_constant_p
= true;
4464 expr
= cp_parser_assignment_expression (parser
);
4467 expr
= fold_non_dependent_expr (expr
);
4469 /* Add it to the list. We add error_mark_node
4470 expressions to the list, so that we can still tell if
4471 the correct form for a parenthesized expression-list
4472 is found. That gives better errors. */
4473 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4475 if (expr
== error_mark_node
)
4479 /* After the first item, attribute lists look the same as
4480 expression lists. */
4481 is_attribute_list
= false;
4484 /* If the next token isn't a `,', then we are done. */
4485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4488 /* Otherwise, consume the `,' and keep going. */
4489 cp_lexer_consume_token (parser
->lexer
);
4492 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4497 /* We try and resync to an unnested comma, as that will give the
4498 user better diagnostics. */
4499 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4500 /*recovering=*/true,
4502 /*consume_paren=*/true);
4506 return error_mark_node
;
4509 /* We built up the list in reverse order so we must reverse it now. */
4510 expression_list
= nreverse (expression_list
);
4512 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4514 return expression_list
;
4517 /* Parse a pseudo-destructor-name.
4519 pseudo-destructor-name:
4520 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4521 :: [opt] nested-name-specifier template template-id :: ~ type-name
4522 :: [opt] nested-name-specifier [opt] ~ type-name
4524 If either of the first two productions is used, sets *SCOPE to the
4525 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4526 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4527 or ERROR_MARK_NODE if the parse fails. */
4530 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4534 bool nested_name_specifier_p
;
4536 /* Assume that things will not work out. */
4537 *type
= error_mark_node
;
4539 /* Look for the optional `::' operator. */
4540 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4541 /* Look for the optional nested-name-specifier. */
4542 nested_name_specifier_p
4543 = (cp_parser_nested_name_specifier_opt (parser
,
4544 /*typename_keyword_p=*/false,
4545 /*check_dependency_p=*/true,
4547 /*is_declaration=*/true)
4549 /* Now, if we saw a nested-name-specifier, we might be doing the
4550 second production. */
4551 if (nested_name_specifier_p
4552 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4554 /* Consume the `template' keyword. */
4555 cp_lexer_consume_token (parser
->lexer
);
4556 /* Parse the template-id. */
4557 cp_parser_template_id (parser
,
4558 /*template_keyword_p=*/true,
4559 /*check_dependency_p=*/false,
4560 /*is_declaration=*/true);
4561 /* Look for the `::' token. */
4562 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4564 /* If the next token is not a `~', then there might be some
4565 additional qualification. */
4566 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4568 /* Look for the type-name. */
4569 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4571 if (*scope
== error_mark_node
)
4574 /* If we don't have ::~, then something has gone wrong. Since
4575 the only caller of this function is looking for something
4576 after `.' or `->' after a scalar type, most likely the
4577 program is trying to get a member of a non-aggregate
4579 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4580 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4582 cp_parser_error (parser
, "request for member of non-aggregate type");
4586 /* Look for the `::' token. */
4587 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4592 /* Look for the `~'. */
4593 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4594 /* Look for the type-name again. We are not responsible for
4595 checking that it matches the first type-name. */
4596 *type
= cp_parser_type_name (parser
);
4599 /* Parse a unary-expression.
4605 unary-operator cast-expression
4606 sizeof unary-expression
4614 __extension__ cast-expression
4615 __alignof__ unary-expression
4616 __alignof__ ( type-id )
4617 __real__ cast-expression
4618 __imag__ cast-expression
4621 ADDRESS_P is true iff the unary-expression is appearing as the
4622 operand of the `&' operator.
4624 Returns a representation of the expression. */
4627 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4630 enum tree_code unary_operator
;
4632 /* Peek at the next token. */
4633 token
= cp_lexer_peek_token (parser
->lexer
);
4634 /* Some keywords give away the kind of expression. */
4635 if (token
->type
== CPP_KEYWORD
)
4637 enum rid keyword
= token
->keyword
;
4647 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4648 /* Consume the token. */
4649 cp_lexer_consume_token (parser
->lexer
);
4650 /* Parse the operand. */
4651 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4653 if (TYPE_P (operand
))
4654 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4656 return cxx_sizeof_or_alignof_expr (operand
, op
);
4660 return cp_parser_new_expression (parser
);
4663 return cp_parser_delete_expression (parser
);
4667 /* The saved value of the PEDANTIC flag. */
4671 /* Save away the PEDANTIC flag. */
4672 cp_parser_extension_opt (parser
, &saved_pedantic
);
4673 /* Parse the cast-expression. */
4674 expr
= cp_parser_simple_cast_expression (parser
);
4675 /* Restore the PEDANTIC flag. */
4676 pedantic
= saved_pedantic
;
4686 /* Consume the `__real__' or `__imag__' token. */
4687 cp_lexer_consume_token (parser
->lexer
);
4688 /* Parse the cast-expression. */
4689 expression
= cp_parser_simple_cast_expression (parser
);
4690 /* Create the complete representation. */
4691 return build_x_unary_op ((keyword
== RID_REALPART
4692 ? REALPART_EXPR
: IMAGPART_EXPR
),
4702 /* Look for the `:: new' and `:: delete', which also signal the
4703 beginning of a new-expression, or delete-expression,
4704 respectively. If the next token is `::', then it might be one of
4706 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4710 /* See if the token after the `::' is one of the keywords in
4711 which we're interested. */
4712 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4713 /* If it's `new', we have a new-expression. */
4714 if (keyword
== RID_NEW
)
4715 return cp_parser_new_expression (parser
);
4716 /* Similarly, for `delete'. */
4717 else if (keyword
== RID_DELETE
)
4718 return cp_parser_delete_expression (parser
);
4721 /* Look for a unary operator. */
4722 unary_operator
= cp_parser_unary_operator (token
);
4723 /* The `++' and `--' operators can be handled similarly, even though
4724 they are not technically unary-operators in the grammar. */
4725 if (unary_operator
== ERROR_MARK
)
4727 if (token
->type
== CPP_PLUS_PLUS
)
4728 unary_operator
= PREINCREMENT_EXPR
;
4729 else if (token
->type
== CPP_MINUS_MINUS
)
4730 unary_operator
= PREDECREMENT_EXPR
;
4731 /* Handle the GNU address-of-label extension. */
4732 else if (cp_parser_allow_gnu_extensions_p (parser
)
4733 && token
->type
== CPP_AND_AND
)
4737 /* Consume the '&&' token. */
4738 cp_lexer_consume_token (parser
->lexer
);
4739 /* Look for the identifier. */
4740 identifier
= cp_parser_identifier (parser
);
4741 /* Create an expression representing the address. */
4742 return finish_label_address_expr (identifier
);
4745 if (unary_operator
!= ERROR_MARK
)
4747 tree cast_expression
;
4748 tree expression
= error_mark_node
;
4749 const char *non_constant_p
= NULL
;
4751 /* Consume the operator token. */
4752 token
= cp_lexer_consume_token (parser
->lexer
);
4753 /* Parse the cast-expression. */
4755 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4756 /* Now, build an appropriate representation. */
4757 switch (unary_operator
)
4760 non_constant_p
= "`*'";
4761 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4765 non_constant_p
= "`&'";
4768 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4771 case PREINCREMENT_EXPR
:
4772 case PREDECREMENT_EXPR
:
4773 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4778 case TRUTH_NOT_EXPR
:
4779 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4787 && cp_parser_non_integral_constant_expression (parser
,
4789 expression
= error_mark_node
;
4794 return cp_parser_postfix_expression (parser
, address_p
);
4797 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4798 unary-operator, the corresponding tree code is returned. */
4800 static enum tree_code
4801 cp_parser_unary_operator (cp_token
* token
)
4803 switch (token
->type
)
4806 return INDIRECT_REF
;
4812 return CONVERT_EXPR
;
4818 return TRUTH_NOT_EXPR
;
4821 return BIT_NOT_EXPR
;
4828 /* Parse a new-expression.
4831 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4832 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4834 Returns a representation of the expression. */
4837 cp_parser_new_expression (cp_parser
* parser
)
4839 bool global_scope_p
;
4845 /* Look for the optional `::' operator. */
4847 = (cp_parser_global_scope_opt (parser
,
4848 /*current_scope_valid_p=*/false)
4850 /* Look for the `new' operator. */
4851 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4852 /* There's no easy way to tell a new-placement from the
4853 `( type-id )' construct. */
4854 cp_parser_parse_tentatively (parser
);
4855 /* Look for a new-placement. */
4856 placement
= cp_parser_new_placement (parser
);
4857 /* If that didn't work out, there's no new-placement. */
4858 if (!cp_parser_parse_definitely (parser
))
4859 placement
= NULL_TREE
;
4861 /* If the next token is a `(', then we have a parenthesized
4863 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4865 /* Consume the `('. */
4866 cp_lexer_consume_token (parser
->lexer
);
4867 /* Parse the type-id. */
4868 type
= cp_parser_type_id (parser
);
4869 /* Look for the closing `)'. */
4870 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4871 /* There should not be a direct-new-declarator in this production,
4872 but GCC used to allowed this, so we check and emit a sensible error
4873 message for this case. */
4874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4876 error ("array bound forbidden after parenthesized type-id");
4877 inform ("try removing the parentheses around the type-id");
4878 cp_parser_direct_new_declarator (parser
);
4882 /* Otherwise, there must be a new-type-id. */
4884 type
= cp_parser_new_type_id (parser
, &nelts
);
4886 /* If the next token is a `(', then we have a new-initializer. */
4887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4888 initializer
= cp_parser_new_initializer (parser
);
4890 initializer
= NULL_TREE
;
4892 /* A new-expression may not appear in an integral constant
4894 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
4895 return error_mark_node
;
4897 /* Create a representation of the new-expression. */
4898 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
4901 /* Parse a new-placement.
4906 Returns the same representation as for an expression-list. */
4909 cp_parser_new_placement (cp_parser
* parser
)
4911 tree expression_list
;
4913 /* Parse the expression-list. */
4914 expression_list
= (cp_parser_parenthesized_expression_list
4915 (parser
, false, /*non_constant_p=*/NULL
));
4917 return expression_list
;
4920 /* Parse a new-type-id.
4923 type-specifier-seq new-declarator [opt]
4925 Returns the TYPE allocated. If the new-type-id indicates an array
4926 type, *NELTS is set to the number of elements in the last array
4927 bound; the TYPE will not include the last array bound. */
4930 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
4932 cp_decl_specifier_seq type_specifier_seq
;
4933 cp_declarator
*new_declarator
;
4934 cp_declarator
*declarator
;
4935 cp_declarator
*outer_declarator
;
4936 const char *saved_message
;
4939 /* The type-specifier sequence must not contain type definitions.
4940 (It cannot contain declarations of new types either, but if they
4941 are not definitions we will catch that because they are not
4943 saved_message
= parser
->type_definition_forbidden_message
;
4944 parser
->type_definition_forbidden_message
4945 = "types may not be defined in a new-type-id";
4946 /* Parse the type-specifier-seq. */
4947 cp_parser_type_specifier_seq (parser
, &type_specifier_seq
);
4948 /* Restore the old message. */
4949 parser
->type_definition_forbidden_message
= saved_message
;
4950 /* Parse the new-declarator. */
4951 new_declarator
= cp_parser_new_declarator_opt (parser
);
4953 /* Determine the number of elements in the last array dimension, if
4956 /* Skip down to the last array dimension. */
4957 declarator
= new_declarator
;
4958 outer_declarator
= NULL
;
4959 while (declarator
&& (declarator
->kind
== cdk_pointer
4960 || declarator
->kind
== cdk_ptrmem
))
4962 outer_declarator
= declarator
;
4963 declarator
= declarator
->declarator
;
4966 && declarator
->kind
== cdk_array
4967 && declarator
->declarator
4968 && declarator
->declarator
->kind
== cdk_array
)
4970 outer_declarator
= declarator
;
4971 declarator
= declarator
->declarator
;
4974 if (declarator
&& declarator
->kind
== cdk_array
)
4976 *nelts
= declarator
->u
.array
.bounds
;
4977 if (*nelts
== error_mark_node
)
4978 *nelts
= integer_one_node
;
4979 else if (!processing_template_decl
)
4981 if (!build_expr_type_conversion (WANT_INT
| WANT_ENUM
, *nelts
,
4983 pedwarn ("size in array new must have integral type");
4984 *nelts
= save_expr (cp_convert (sizetype
, *nelts
));
4985 if (*nelts
== integer_zero_node
)
4986 warning ("zero size array reserves no space");
4988 if (outer_declarator
)
4989 outer_declarator
->declarator
= declarator
->declarator
;
4991 new_declarator
= NULL
;
4994 type
= groktypename (&type_specifier_seq
, new_declarator
);
4995 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
4997 *nelts
= array_type_nelts_top (type
);
4998 type
= TREE_TYPE (type
);
5003 /* Parse an (optional) new-declarator.
5006 ptr-operator new-declarator [opt]
5007 direct-new-declarator
5009 Returns the declarator. */
5011 static cp_declarator
*
5012 cp_parser_new_declarator_opt (cp_parser
* parser
)
5014 enum tree_code code
;
5016 cp_cv_quals cv_quals
;
5018 /* We don't know if there's a ptr-operator next, or not. */
5019 cp_parser_parse_tentatively (parser
);
5020 /* Look for a ptr-operator. */
5021 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5022 /* If that worked, look for more new-declarators. */
5023 if (cp_parser_parse_definitely (parser
))
5025 cp_declarator
*declarator
;
5027 /* Parse another optional declarator. */
5028 declarator
= cp_parser_new_declarator_opt (parser
);
5030 /* Create the representation of the declarator. */
5032 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5033 else if (code
== INDIRECT_REF
)
5034 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5036 declarator
= make_reference_declarator (cv_quals
, declarator
);
5041 /* If the next token is a `[', there is a direct-new-declarator. */
5042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5043 return cp_parser_direct_new_declarator (parser
);
5048 /* Parse a direct-new-declarator.
5050 direct-new-declarator:
5052 direct-new-declarator [constant-expression]
5056 static cp_declarator
*
5057 cp_parser_direct_new_declarator (cp_parser
* parser
)
5059 cp_declarator
*declarator
= NULL
;
5065 /* Look for the opening `['. */
5066 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5067 /* The first expression is not required to be constant. */
5070 expression
= cp_parser_expression (parser
);
5071 /* The standard requires that the expression have integral
5072 type. DR 74 adds enumeration types. We believe that the
5073 real intent is that these expressions be handled like the
5074 expression in a `switch' condition, which also allows
5075 classes with a single conversion to integral or
5076 enumeration type. */
5077 if (!processing_template_decl
)
5080 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5085 error ("expression in new-declarator must have integral "
5086 "or enumeration type");
5087 expression
= error_mark_node
;
5091 /* But all the other expressions must be. */
5094 = cp_parser_constant_expression (parser
,
5095 /*allow_non_constant=*/false,
5097 /* Look for the closing `]'. */
5098 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5100 /* Add this bound to the declarator. */
5101 declarator
= make_array_declarator (declarator
, expression
);
5103 /* If the next token is not a `[', then there are no more
5105 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5112 /* Parse a new-initializer.
5115 ( expression-list [opt] )
5117 Returns a representation of the expression-list. If there is no
5118 expression-list, VOID_ZERO_NODE is returned. */
5121 cp_parser_new_initializer (cp_parser
* parser
)
5123 tree expression_list
;
5125 expression_list
= (cp_parser_parenthesized_expression_list
5126 (parser
, false, /*non_constant_p=*/NULL
));
5127 if (!expression_list
)
5128 expression_list
= void_zero_node
;
5130 return expression_list
;
5133 /* Parse a delete-expression.
5136 :: [opt] delete cast-expression
5137 :: [opt] delete [ ] cast-expression
5139 Returns a representation of the expression. */
5142 cp_parser_delete_expression (cp_parser
* parser
)
5144 bool global_scope_p
;
5148 /* Look for the optional `::' operator. */
5150 = (cp_parser_global_scope_opt (parser
,
5151 /*current_scope_valid_p=*/false)
5153 /* Look for the `delete' keyword. */
5154 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5155 /* See if the array syntax is in use. */
5156 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5158 /* Consume the `[' token. */
5159 cp_lexer_consume_token (parser
->lexer
);
5160 /* Look for the `]' token. */
5161 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5162 /* Remember that this is the `[]' construct. */
5168 /* Parse the cast-expression. */
5169 expression
= cp_parser_simple_cast_expression (parser
);
5171 /* A delete-expression may not appear in an integral constant
5173 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5174 return error_mark_node
;
5176 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5179 /* Parse a cast-expression.
5183 ( type-id ) cast-expression
5185 Returns a representation of the expression. */
5188 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
5190 /* If it's a `(', then we might be looking at a cast. */
5191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5193 tree type
= NULL_TREE
;
5194 tree expr
= NULL_TREE
;
5195 bool compound_literal_p
;
5196 const char *saved_message
;
5198 /* There's no way to know yet whether or not this is a cast.
5199 For example, `(int (3))' is a unary-expression, while `(int)
5200 3' is a cast. So, we resort to parsing tentatively. */
5201 cp_parser_parse_tentatively (parser
);
5202 /* Types may not be defined in a cast. */
5203 saved_message
= parser
->type_definition_forbidden_message
;
5204 parser
->type_definition_forbidden_message
5205 = "types may not be defined in casts";
5206 /* Consume the `('. */
5207 cp_lexer_consume_token (parser
->lexer
);
5208 /* A very tricky bit is that `(struct S) { 3 }' is a
5209 compound-literal (which we permit in C++ as an extension).
5210 But, that construct is not a cast-expression -- it is a
5211 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5212 is legal; if the compound-literal were a cast-expression,
5213 you'd need an extra set of parentheses.) But, if we parse
5214 the type-id, and it happens to be a class-specifier, then we
5215 will commit to the parse at that point, because we cannot
5216 undo the action that is done when creating a new class. So,
5217 then we cannot back up and do a postfix-expression.
5219 Therefore, we scan ahead to the closing `)', and check to see
5220 if the token after the `)' is a `{'. If so, we are not
5221 looking at a cast-expression.
5223 Save tokens so that we can put them back. */
5224 cp_lexer_save_tokens (parser
->lexer
);
5225 /* Skip tokens until the next token is a closing parenthesis.
5226 If we find the closing `)', and the next token is a `{', then
5227 we are looking at a compound-literal. */
5229 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5230 /*consume_paren=*/true)
5231 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5232 /* Roll back the tokens we skipped. */
5233 cp_lexer_rollback_tokens (parser
->lexer
);
5234 /* If we were looking at a compound-literal, simulate an error
5235 so that the call to cp_parser_parse_definitely below will
5237 if (compound_literal_p
)
5238 cp_parser_simulate_error (parser
);
5241 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5242 parser
->in_type_id_in_expr_p
= true;
5243 /* Look for the type-id. */
5244 type
= cp_parser_type_id (parser
);
5245 /* Look for the closing `)'. */
5246 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5247 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5250 /* Restore the saved message. */
5251 parser
->type_definition_forbidden_message
= saved_message
;
5253 /* If ok so far, parse the dependent expression. We cannot be
5254 sure it is a cast. Consider `(T ())'. It is a parenthesized
5255 ctor of T, but looks like a cast to function returning T
5256 without a dependent expression. */
5257 if (!cp_parser_error_occurred (parser
))
5258 expr
= cp_parser_simple_cast_expression (parser
);
5260 if (cp_parser_parse_definitely (parser
))
5262 /* Warn about old-style casts, if so requested. */
5263 if (warn_old_style_cast
5264 && !in_system_header
5265 && !VOID_TYPE_P (type
)
5266 && current_lang_name
!= lang_name_c
)
5267 warning ("use of old-style cast");
5269 /* Only type conversions to integral or enumeration types
5270 can be used in constant-expressions. */
5271 if (parser
->integral_constant_expression_p
5272 && !dependent_type_p (type
)
5273 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
5274 && (cp_parser_non_integral_constant_expression
5276 "a cast to a type other than an integral or "
5277 "enumeration type")))
5278 return error_mark_node
;
5280 /* Perform the cast. */
5281 expr
= build_c_cast (type
, expr
);
5286 /* If we get here, then it's not a cast, so it must be a
5287 unary-expression. */
5288 return cp_parser_unary_expression (parser
, address_p
);
5291 /* Parse a binary expression of the general form:
5295 pm-expression .* cast-expression
5296 pm-expression ->* cast-expression
5298 multiplicative-expression:
5300 multiplicative-expression * pm-expression
5301 multiplicative-expression / pm-expression
5302 multiplicative-expression % pm-expression
5304 additive-expression:
5305 multiplicative-expression
5306 additive-expression + multiplicative-expression
5307 additive-expression - multiplicative-expression
5311 shift-expression << additive-expression
5312 shift-expression >> additive-expression
5314 relational-expression:
5316 relational-expression < shift-expression
5317 relational-expression > shift-expression
5318 relational-expression <= shift-expression
5319 relational-expression >= shift-expression
5323 relational-expression:
5324 relational-expression <? shift-expression
5325 relational-expression >? shift-expression
5327 equality-expression:
5328 relational-expression
5329 equality-expression == relational-expression
5330 equality-expression != relational-expression
5334 and-expression & equality-expression
5336 exclusive-or-expression:
5338 exclusive-or-expression ^ and-expression
5340 inclusive-or-expression:
5341 exclusive-or-expression
5342 inclusive-or-expression | exclusive-or-expression
5344 logical-and-expression:
5345 inclusive-or-expression
5346 logical-and-expression && inclusive-or-expression
5348 logical-or-expression:
5349 logical-and-expression
5350 logical-or-expression || logical-and-expression
5352 All these are implemented with a single function like:
5355 simple-cast-expression
5356 binary-expression <token> binary-expression
5358 The binops_by_token map is used to get the tree codes for each <token> type.
5359 binary-expressions are associated according to a precedence table. */
5361 #define TOKEN_PRECEDENCE(token) \
5362 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5363 ? PREC_NOT_OPERATOR \
5364 : binops_by_token[token->type].prec)
5367 cp_parser_binary_expression (cp_parser
* parser
)
5369 cp_parser_expression_stack stack
;
5370 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5373 enum tree_code tree_type
;
5374 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5377 /* Parse the first expression. */
5378 lhs
= cp_parser_simple_cast_expression (parser
);
5382 /* Get an operator token. */
5383 token
= cp_lexer_peek_token (parser
->lexer
);
5384 new_prec
= TOKEN_PRECEDENCE (token
);
5386 /* Popping an entry off the stack means we completed a subexpression:
5387 - either we found a token which is not an operator (`>' where it is not
5388 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5389 will happen repeatedly;
5390 - or, we found an operator which has lower priority. This is the case
5391 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5393 if (new_prec
<= prec
)
5402 tree_type
= binops_by_token
[token
->type
].tree_type
;
5404 /* We used the operator token. */
5405 cp_lexer_consume_token (parser
->lexer
);
5407 /* Extract another operand. It may be the RHS of this expression
5408 or the LHS of a new, higher priority expression. */
5409 rhs
= cp_parser_simple_cast_expression (parser
);
5411 /* Get another operator token. Look up its precedence to avoid
5412 building a useless (immediately popped) stack entry for common
5413 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5414 token
= cp_lexer_peek_token (parser
->lexer
);
5415 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5416 if (lookahead_prec
> new_prec
)
5418 /* ... and prepare to parse the RHS of the new, higher priority
5419 expression. Since precedence levels on the stack are
5420 monotonically increasing, we do not have to care about
5423 sp
->tree_type
= tree_type
;
5428 new_prec
= lookahead_prec
;
5432 /* If the stack is not empty, we have parsed into LHS the right side
5433 (`4' in the example above) of an expression we had suspended.
5434 We can use the information on the stack to recover the LHS (`3')
5435 from the stack together with the tree code (`MULT_EXPR'), and
5436 the precedence of the higher level subexpression
5437 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5438 which will be used to actually build the additive expression. */
5441 tree_type
= sp
->tree_type
;
5446 overloaded_p
= false;
5447 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5449 /* If the binary operator required the use of an overloaded operator,
5450 then this expression cannot be an integral constant-expression.
5451 An overloaded operator can be used even if both operands are
5452 otherwise permissible in an integral constant-expression if at
5453 least one of the operands is of enumeration type. */
5456 && (cp_parser_non_integral_constant_expression
5457 (parser
, "calls to overloaded operators")))
5458 return error_mark_node
;
5465 /* Parse the `? expression : assignment-expression' part of a
5466 conditional-expression. The LOGICAL_OR_EXPR is the
5467 logical-or-expression that started the conditional-expression.
5468 Returns a representation of the entire conditional-expression.
5470 This routine is used by cp_parser_assignment_expression.
5472 ? expression : assignment-expression
5476 ? : assignment-expression */
5479 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5482 tree assignment_expr
;
5484 /* Consume the `?' token. */
5485 cp_lexer_consume_token (parser
->lexer
);
5486 if (cp_parser_allow_gnu_extensions_p (parser
)
5487 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5488 /* Implicit true clause. */
5491 /* Parse the expression. */
5492 expr
= cp_parser_expression (parser
);
5494 /* The next token should be a `:'. */
5495 cp_parser_require (parser
, CPP_COLON
, "`:'");
5496 /* Parse the assignment-expression. */
5497 assignment_expr
= cp_parser_assignment_expression (parser
);
5499 /* Build the conditional-expression. */
5500 return build_x_conditional_expr (logical_or_expr
,
5505 /* Parse an assignment-expression.
5507 assignment-expression:
5508 conditional-expression
5509 logical-or-expression assignment-operator assignment_expression
5512 Returns a representation for the expression. */
5515 cp_parser_assignment_expression (cp_parser
* parser
)
5519 /* If the next token is the `throw' keyword, then we're looking at
5520 a throw-expression. */
5521 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5522 expr
= cp_parser_throw_expression (parser
);
5523 /* Otherwise, it must be that we are looking at a
5524 logical-or-expression. */
5527 /* Parse the binary expressions (logical-or-expression). */
5528 expr
= cp_parser_binary_expression (parser
);
5529 /* If the next token is a `?' then we're actually looking at a
5530 conditional-expression. */
5531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5532 return cp_parser_question_colon_clause (parser
, expr
);
5535 enum tree_code assignment_operator
;
5537 /* If it's an assignment-operator, we're using the second
5540 = cp_parser_assignment_operator_opt (parser
);
5541 if (assignment_operator
!= ERROR_MARK
)
5545 /* Parse the right-hand side of the assignment. */
5546 rhs
= cp_parser_assignment_expression (parser
);
5547 /* An assignment may not appear in a
5548 constant-expression. */
5549 if (cp_parser_non_integral_constant_expression (parser
,
5551 return error_mark_node
;
5552 /* Build the assignment expression. */
5553 expr
= build_x_modify_expr (expr
,
5554 assignment_operator
,
5563 /* Parse an (optional) assignment-operator.
5565 assignment-operator: one of
5566 = *= /= %= += -= >>= <<= &= ^= |=
5570 assignment-operator: one of
5573 If the next token is an assignment operator, the corresponding tree
5574 code is returned, and the token is consumed. For example, for
5575 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5576 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5577 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5578 operator, ERROR_MARK is returned. */
5580 static enum tree_code
5581 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5586 /* Peek at the next toen. */
5587 token
= cp_lexer_peek_token (parser
->lexer
);
5589 switch (token
->type
)
5600 op
= TRUNC_DIV_EXPR
;
5604 op
= TRUNC_MOD_EXPR
;
5644 /* Nothing else is an assignment operator. */
5648 /* If it was an assignment operator, consume it. */
5649 if (op
!= ERROR_MARK
)
5650 cp_lexer_consume_token (parser
->lexer
);
5655 /* Parse an expression.
5658 assignment-expression
5659 expression , assignment-expression
5661 Returns a representation of the expression. */
5664 cp_parser_expression (cp_parser
* parser
)
5666 tree expression
= NULL_TREE
;
5670 tree assignment_expression
;
5672 /* Parse the next assignment-expression. */
5673 assignment_expression
5674 = cp_parser_assignment_expression (parser
);
5675 /* If this is the first assignment-expression, we can just
5678 expression
= assignment_expression
;
5680 expression
= build_x_compound_expr (expression
,
5681 assignment_expression
);
5682 /* If the next token is not a comma, then we are done with the
5684 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5686 /* Consume the `,'. */
5687 cp_lexer_consume_token (parser
->lexer
);
5688 /* A comma operator cannot appear in a constant-expression. */
5689 if (cp_parser_non_integral_constant_expression (parser
,
5690 "a comma operator"))
5691 expression
= error_mark_node
;
5697 /* Parse a constant-expression.
5699 constant-expression:
5700 conditional-expression
5702 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5703 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5704 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5705 is false, NON_CONSTANT_P should be NULL. */
5708 cp_parser_constant_expression (cp_parser
* parser
,
5709 bool allow_non_constant_p
,
5710 bool *non_constant_p
)
5712 bool saved_integral_constant_expression_p
;
5713 bool saved_allow_non_integral_constant_expression_p
;
5714 bool saved_non_integral_constant_expression_p
;
5717 /* It might seem that we could simply parse the
5718 conditional-expression, and then check to see if it were
5719 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5720 one that the compiler can figure out is constant, possibly after
5721 doing some simplifications or optimizations. The standard has a
5722 precise definition of constant-expression, and we must honor
5723 that, even though it is somewhat more restrictive.
5729 is not a legal declaration, because `(2, 3)' is not a
5730 constant-expression. The `,' operator is forbidden in a
5731 constant-expression. However, GCC's constant-folding machinery
5732 will fold this operation to an INTEGER_CST for `3'. */
5734 /* Save the old settings. */
5735 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5736 saved_allow_non_integral_constant_expression_p
5737 = parser
->allow_non_integral_constant_expression_p
;
5738 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5739 /* We are now parsing a constant-expression. */
5740 parser
->integral_constant_expression_p
= true;
5741 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5742 parser
->non_integral_constant_expression_p
= false;
5743 /* Although the grammar says "conditional-expression", we parse an
5744 "assignment-expression", which also permits "throw-expression"
5745 and the use of assignment operators. In the case that
5746 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5747 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5748 actually essential that we look for an assignment-expression.
5749 For example, cp_parser_initializer_clauses uses this function to
5750 determine whether a particular assignment-expression is in fact
5752 expression
= cp_parser_assignment_expression (parser
);
5753 /* Restore the old settings. */
5754 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
5755 parser
->allow_non_integral_constant_expression_p
5756 = saved_allow_non_integral_constant_expression_p
;
5757 if (allow_non_constant_p
)
5758 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5759 parser
->non_integral_constant_expression_p
= saved_non_integral_constant_expression_p
;
5764 /* Parse __builtin_offsetof.
5766 offsetof-expression:
5767 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5769 offsetof-member-designator:
5771 | offsetof-member-designator "." id-expression
5772 | offsetof-member-designator "[" expression "]"
5776 cp_parser_builtin_offsetof (cp_parser
*parser
)
5778 int save_ice_p
, save_non_ice_p
;
5782 /* We're about to accept non-integral-constant things, but will
5783 definitely yield an integral constant expression. Save and
5784 restore these values around our local parsing. */
5785 save_ice_p
= parser
->integral_constant_expression_p
;
5786 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
5788 /* Consume the "__builtin_offsetof" token. */
5789 cp_lexer_consume_token (parser
->lexer
);
5790 /* Consume the opening `('. */
5791 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5792 /* Parse the type-id. */
5793 type
= cp_parser_type_id (parser
);
5794 /* Look for the `,'. */
5795 cp_parser_require (parser
, CPP_COMMA
, "`,'");
5797 /* Build the (type *)null that begins the traditional offsetof macro. */
5798 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
5800 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5801 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
5805 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5806 switch (token
->type
)
5808 case CPP_OPEN_SQUARE
:
5809 /* offsetof-member-designator "[" expression "]" */
5810 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
5814 /* offsetof-member-designator "." identifier */
5815 cp_lexer_consume_token (parser
->lexer
);
5816 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
5820 case CPP_CLOSE_PAREN
:
5821 /* Consume the ")" token. */
5822 cp_lexer_consume_token (parser
->lexer
);
5826 /* Error. We know the following require will fail, but
5827 that gives the proper error message. */
5828 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5829 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
5830 expr
= error_mark_node
;
5836 /* If we're processing a template, we can't finish the semantics yet.
5837 Otherwise we can fold the entire expression now. */
5838 if (processing_template_decl
)
5839 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
5841 expr
= fold_offsetof (expr
);
5844 parser
->integral_constant_expression_p
= save_ice_p
;
5845 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
5850 /* Statements [gram.stmt.stmt] */
5852 /* Parse a statement.
5856 expression-statement
5861 declaration-statement
5865 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
)
5869 location_t statement_location
;
5871 /* There is no statement yet. */
5872 statement
= NULL_TREE
;
5873 /* Peek at the next token. */
5874 token
= cp_lexer_peek_token (parser
->lexer
);
5875 /* Remember the location of the first token in the statement. */
5876 statement_location
= token
->location
;
5877 /* If this is a keyword, then that will often determine what kind of
5878 statement we have. */
5879 if (token
->type
== CPP_KEYWORD
)
5881 enum rid keyword
= token
->keyword
;
5887 statement
= cp_parser_labeled_statement (parser
,
5893 statement
= cp_parser_selection_statement (parser
);
5899 statement
= cp_parser_iteration_statement (parser
);
5906 statement
= cp_parser_jump_statement (parser
);
5910 statement
= cp_parser_try_block (parser
);
5914 /* It might be a keyword like `int' that can start a
5915 declaration-statement. */
5919 else if (token
->type
== CPP_NAME
)
5921 /* If the next token is a `:', then we are looking at a
5922 labeled-statement. */
5923 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5924 if (token
->type
== CPP_COLON
)
5925 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
);
5927 /* Anything that starts with a `{' must be a compound-statement. */
5928 else if (token
->type
== CPP_OPEN_BRACE
)
5929 statement
= cp_parser_compound_statement (parser
, NULL
, false);
5930 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5931 a statement all its own. */
5932 else if (token
->type
== CPP_PRAGMA
)
5934 cp_lexer_handle_pragma (parser
->lexer
);
5938 /* Everything else must be a declaration-statement or an
5939 expression-statement. Try for the declaration-statement
5940 first, unless we are looking at a `;', in which case we know that
5941 we have an expression-statement. */
5944 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5946 cp_parser_parse_tentatively (parser
);
5947 /* Try to parse the declaration-statement. */
5948 cp_parser_declaration_statement (parser
);
5949 /* If that worked, we're done. */
5950 if (cp_parser_parse_definitely (parser
))
5953 /* Look for an expression-statement instead. */
5954 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
5957 /* Set the line number for the statement. */
5958 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5959 SET_EXPR_LOCATION (statement
, statement_location
);
5962 /* Parse a labeled-statement.
5965 identifier : statement
5966 case constant-expression : statement
5972 case constant-expression ... constant-expression : statement
5974 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5975 For an ordinary label, returns a LABEL_EXPR. */
5978 cp_parser_labeled_statement (cp_parser
* parser
, tree in_statement_expr
)
5981 tree statement
= error_mark_node
;
5983 /* The next token should be an identifier. */
5984 token
= cp_lexer_peek_token (parser
->lexer
);
5985 if (token
->type
!= CPP_NAME
5986 && token
->type
!= CPP_KEYWORD
)
5988 cp_parser_error (parser
, "expected labeled-statement");
5989 return error_mark_node
;
5992 switch (token
->keyword
)
5999 /* Consume the `case' token. */
6000 cp_lexer_consume_token (parser
->lexer
);
6001 /* Parse the constant-expression. */
6002 expr
= cp_parser_constant_expression (parser
,
6003 /*allow_non_constant_p=*/false,
6006 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6007 if (ellipsis
->type
== CPP_ELLIPSIS
)
6009 /* Consume the `...' token. */
6010 cp_lexer_consume_token (parser
->lexer
);
6012 cp_parser_constant_expression (parser
,
6013 /*allow_non_constant_p=*/false,
6015 /* We don't need to emit warnings here, as the common code
6016 will do this for us. */
6019 expr_hi
= NULL_TREE
;
6021 if (!parser
->in_switch_statement_p
)
6022 error ("case label %qE not within a switch statement", expr
);
6024 statement
= finish_case_label (expr
, expr_hi
);
6029 /* Consume the `default' token. */
6030 cp_lexer_consume_token (parser
->lexer
);
6031 if (!parser
->in_switch_statement_p
)
6032 error ("case label not within a switch statement");
6034 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
6038 /* Anything else must be an ordinary label. */
6039 statement
= finish_label_stmt (cp_parser_identifier (parser
));
6043 /* Require the `:' token. */
6044 cp_parser_require (parser
, CPP_COLON
, "`:'");
6045 /* Parse the labeled statement. */
6046 cp_parser_statement (parser
, in_statement_expr
);
6048 /* Return the label, in the case of a `case' or `default' label. */
6052 /* Parse an expression-statement.
6054 expression-statement:
6057 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6058 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6059 indicates whether this expression-statement is part of an
6060 expression statement. */
6063 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6065 tree statement
= NULL_TREE
;
6067 /* If the next token is a ';', then there is no expression
6069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6070 statement
= cp_parser_expression (parser
);
6072 /* Consume the final `;'. */
6073 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6075 if (in_statement_expr
6076 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6078 /* This is the final expression statement of a statement
6080 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6083 statement
= finish_expr_stmt (statement
);
6090 /* Parse a compound-statement.
6093 { statement-seq [opt] }
6095 Returns a tree representing the statement. */
6098 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6103 /* Consume the `{'. */
6104 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6105 return error_mark_node
;
6106 /* Begin the compound-statement. */
6107 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6108 /* Parse an (optional) statement-seq. */
6109 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6110 /* Finish the compound-statement. */
6111 finish_compound_stmt (compound_stmt
);
6112 /* Consume the `}'. */
6113 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6115 return compound_stmt
;
6118 /* Parse an (optional) statement-seq.
6122 statement-seq [opt] statement */
6125 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6127 /* Scan statements until there aren't any more. */
6130 /* If we're looking at a `}', then we've run out of statements. */
6131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
6132 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
6135 /* Parse the statement. */
6136 cp_parser_statement (parser
, in_statement_expr
);
6140 /* Parse a selection-statement.
6142 selection-statement:
6143 if ( condition ) statement
6144 if ( condition ) statement else statement
6145 switch ( condition ) statement
6147 Returns the new IF_STMT or SWITCH_STMT. */
6150 cp_parser_selection_statement (cp_parser
* parser
)
6155 /* Peek at the next token. */
6156 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6158 /* See what kind of keyword it is. */
6159 keyword
= token
->keyword
;
6168 /* Look for the `('. */
6169 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6171 cp_parser_skip_to_end_of_statement (parser
);
6172 return error_mark_node
;
6175 /* Begin the selection-statement. */
6176 if (keyword
== RID_IF
)
6177 statement
= begin_if_stmt ();
6179 statement
= begin_switch_stmt ();
6181 /* Parse the condition. */
6182 condition
= cp_parser_condition (parser
);
6183 /* Look for the `)'. */
6184 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6185 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6186 /*consume_paren=*/true);
6188 if (keyword
== RID_IF
)
6190 /* Add the condition. */
6191 finish_if_stmt_cond (condition
, statement
);
6193 /* Parse the then-clause. */
6194 cp_parser_implicitly_scoped_statement (parser
);
6195 finish_then_clause (statement
);
6197 /* If the next token is `else', parse the else-clause. */
6198 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6201 /* Consume the `else' keyword. */
6202 cp_lexer_consume_token (parser
->lexer
);
6203 begin_else_clause (statement
);
6204 /* Parse the else-clause. */
6205 cp_parser_implicitly_scoped_statement (parser
);
6206 finish_else_clause (statement
);
6209 /* Now we're all done with the if-statement. */
6210 finish_if_stmt (statement
);
6214 bool in_switch_statement_p
;
6216 /* Add the condition. */
6217 finish_switch_cond (condition
, statement
);
6219 /* Parse the body of the switch-statement. */
6220 in_switch_statement_p
= parser
->in_switch_statement_p
;
6221 parser
->in_switch_statement_p
= true;
6222 cp_parser_implicitly_scoped_statement (parser
);
6223 parser
->in_switch_statement_p
= in_switch_statement_p
;
6225 /* Now we're all done with the switch-statement. */
6226 finish_switch_stmt (statement
);
6234 cp_parser_error (parser
, "expected selection-statement");
6235 return error_mark_node
;
6239 /* Parse a condition.
6243 type-specifier-seq declarator = assignment-expression
6248 type-specifier-seq declarator asm-specification [opt]
6249 attributes [opt] = assignment-expression
6251 Returns the expression that should be tested. */
6254 cp_parser_condition (cp_parser
* parser
)
6256 cp_decl_specifier_seq type_specifiers
;
6257 const char *saved_message
;
6259 /* Try the declaration first. */
6260 cp_parser_parse_tentatively (parser
);
6261 /* New types are not allowed in the type-specifier-seq for a
6263 saved_message
= parser
->type_definition_forbidden_message
;
6264 parser
->type_definition_forbidden_message
6265 = "types may not be defined in conditions";
6266 /* Parse the type-specifier-seq. */
6267 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
6268 /* Restore the saved message. */
6269 parser
->type_definition_forbidden_message
= saved_message
;
6270 /* If all is well, we might be looking at a declaration. */
6271 if (!cp_parser_error_occurred (parser
))
6274 tree asm_specification
;
6276 cp_declarator
*declarator
;
6277 tree initializer
= NULL_TREE
;
6279 /* Parse the declarator. */
6280 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6281 /*ctor_dtor_or_conv_p=*/NULL
,
6282 /*parenthesized_p=*/NULL
,
6283 /*member_p=*/false);
6284 /* Parse the attributes. */
6285 attributes
= cp_parser_attributes_opt (parser
);
6286 /* Parse the asm-specification. */
6287 asm_specification
= cp_parser_asm_specification_opt (parser
);
6288 /* If the next token is not an `=', then we might still be
6289 looking at an expression. For example:
6293 looks like a decl-specifier-seq and a declarator -- but then
6294 there is no `=', so this is an expression. */
6295 cp_parser_require (parser
, CPP_EQ
, "`='");
6296 /* If we did see an `=', then we are looking at a declaration
6298 if (cp_parser_parse_definitely (parser
))
6302 /* Create the declaration. */
6303 decl
= start_decl (declarator
, &type_specifiers
,
6304 /*initialized_p=*/true,
6305 attributes
, /*prefix_attributes=*/NULL_TREE
,
6307 /* Parse the assignment-expression. */
6308 initializer
= cp_parser_assignment_expression (parser
);
6310 /* Process the initializer. */
6311 cp_finish_decl (decl
,
6314 LOOKUP_ONLYCONVERTING
);
6317 pop_scope (DECL_CONTEXT (decl
));
6319 return convert_from_reference (decl
);
6322 /* If we didn't even get past the declarator successfully, we are
6323 definitely not looking at a declaration. */
6325 cp_parser_abort_tentative_parse (parser
);
6327 /* Otherwise, we are looking at an expression. */
6328 return cp_parser_expression (parser
);
6331 /* Parse an iteration-statement.
6333 iteration-statement:
6334 while ( condition ) statement
6335 do statement while ( expression ) ;
6336 for ( for-init-statement condition [opt] ; expression [opt] )
6339 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6342 cp_parser_iteration_statement (cp_parser
* parser
)
6347 bool in_iteration_statement_p
;
6350 /* Peek at the next token. */
6351 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6353 return error_mark_node
;
6355 /* Remember whether or not we are already within an iteration
6357 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
6359 /* See what kind of keyword it is. */
6360 keyword
= token
->keyword
;
6367 /* Begin the while-statement. */
6368 statement
= begin_while_stmt ();
6369 /* Look for the `('. */
6370 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6371 /* Parse the condition. */
6372 condition
= cp_parser_condition (parser
);
6373 finish_while_stmt_cond (condition
, statement
);
6374 /* Look for the `)'. */
6375 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6376 /* Parse the dependent statement. */
6377 parser
->in_iteration_statement_p
= true;
6378 cp_parser_already_scoped_statement (parser
);
6379 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6380 /* We're done with the while-statement. */
6381 finish_while_stmt (statement
);
6389 /* Begin the do-statement. */
6390 statement
= begin_do_stmt ();
6391 /* Parse the body of the do-statement. */
6392 parser
->in_iteration_statement_p
= true;
6393 cp_parser_implicitly_scoped_statement (parser
);
6394 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6395 finish_do_body (statement
);
6396 /* Look for the `while' keyword. */
6397 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6398 /* Look for the `('. */
6399 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6400 /* Parse the expression. */
6401 expression
= cp_parser_expression (parser
);
6402 /* We're done with the do-statement. */
6403 finish_do_stmt (expression
, statement
);
6404 /* Look for the `)'. */
6405 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6406 /* Look for the `;'. */
6407 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6413 tree condition
= NULL_TREE
;
6414 tree expression
= NULL_TREE
;
6416 /* Begin the for-statement. */
6417 statement
= begin_for_stmt ();
6418 /* Look for the `('. */
6419 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6420 /* Parse the initialization. */
6421 cp_parser_for_init_statement (parser
);
6422 finish_for_init_stmt (statement
);
6424 /* If there's a condition, process it. */
6425 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6426 condition
= cp_parser_condition (parser
);
6427 finish_for_cond (condition
, statement
);
6428 /* Look for the `;'. */
6429 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6431 /* If there's an expression, process it. */
6432 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6433 expression
= cp_parser_expression (parser
);
6434 finish_for_expr (expression
, statement
);
6435 /* Look for the `)'. */
6436 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6438 /* Parse the body of the for-statement. */
6439 parser
->in_iteration_statement_p
= true;
6440 cp_parser_already_scoped_statement (parser
);
6441 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6443 /* We're done with the for-statement. */
6444 finish_for_stmt (statement
);
6449 cp_parser_error (parser
, "expected iteration-statement");
6450 statement
= error_mark_node
;
6457 /* Parse a for-init-statement.
6460 expression-statement
6461 simple-declaration */
6464 cp_parser_for_init_statement (cp_parser
* parser
)
6466 /* If the next token is a `;', then we have an empty
6467 expression-statement. Grammatically, this is also a
6468 simple-declaration, but an invalid one, because it does not
6469 declare anything. Therefore, if we did not handle this case
6470 specially, we would issue an error message about an invalid
6472 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6474 /* We're going to speculatively look for a declaration, falling back
6475 to an expression, if necessary. */
6476 cp_parser_parse_tentatively (parser
);
6477 /* Parse the declaration. */
6478 cp_parser_simple_declaration (parser
,
6479 /*function_definition_allowed_p=*/false);
6480 /* If the tentative parse failed, then we shall need to look for an
6481 expression-statement. */
6482 if (cp_parser_parse_definitely (parser
))
6486 cp_parser_expression_statement (parser
, false);
6489 /* Parse a jump-statement.
6494 return expression [opt] ;
6502 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6505 cp_parser_jump_statement (cp_parser
* parser
)
6507 tree statement
= error_mark_node
;
6511 /* Peek at the next token. */
6512 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6514 return error_mark_node
;
6516 /* See what kind of keyword it is. */
6517 keyword
= token
->keyword
;
6521 if (!parser
->in_switch_statement_p
6522 && !parser
->in_iteration_statement_p
)
6524 error ("break statement not within loop or switch");
6525 statement
= error_mark_node
;
6528 statement
= finish_break_stmt ();
6529 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6533 if (!parser
->in_iteration_statement_p
)
6535 error ("continue statement not within a loop");
6536 statement
= error_mark_node
;
6539 statement
= finish_continue_stmt ();
6540 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6547 /* If the next token is a `;', then there is no
6549 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6550 expr
= cp_parser_expression (parser
);
6553 /* Build the return-statement. */
6554 statement
= finish_return_stmt (expr
);
6555 /* Look for the final `;'. */
6556 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6561 /* Create the goto-statement. */
6562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6564 /* Issue a warning about this use of a GNU extension. */
6566 pedwarn ("ISO C++ forbids computed gotos");
6567 /* Consume the '*' token. */
6568 cp_lexer_consume_token (parser
->lexer
);
6569 /* Parse the dependent expression. */
6570 finish_goto_stmt (cp_parser_expression (parser
));
6573 finish_goto_stmt (cp_parser_identifier (parser
));
6574 /* Look for the final `;'. */
6575 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6579 cp_parser_error (parser
, "expected jump-statement");
6586 /* Parse a declaration-statement.
6588 declaration-statement:
6589 block-declaration */
6592 cp_parser_declaration_statement (cp_parser
* parser
)
6596 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6597 p
= obstack_alloc (&declarator_obstack
, 0);
6599 /* Parse the block-declaration. */
6600 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6602 /* Free any declarators allocated. */
6603 obstack_free (&declarator_obstack
, p
);
6605 /* Finish off the statement. */
6609 /* Some dependent statements (like `if (cond) statement'), are
6610 implicitly in their own scope. In other words, if the statement is
6611 a single statement (as opposed to a compound-statement), it is
6612 none-the-less treated as if it were enclosed in braces. Any
6613 declarations appearing in the dependent statement are out of scope
6614 after control passes that point. This function parses a statement,
6615 but ensures that is in its own scope, even if it is not a
6618 Returns the new statement. */
6621 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6625 /* If the token is not a `{', then we must take special action. */
6626 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6628 /* Create a compound-statement. */
6629 statement
= begin_compound_stmt (0);
6630 /* Parse the dependent-statement. */
6631 cp_parser_statement (parser
, false);
6632 /* Finish the dummy compound-statement. */
6633 finish_compound_stmt (statement
);
6635 /* Otherwise, we simply parse the statement directly. */
6637 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6639 /* Return the statement. */
6643 /* For some dependent statements (like `while (cond) statement'), we
6644 have already created a scope. Therefore, even if the dependent
6645 statement is a compound-statement, we do not want to create another
6649 cp_parser_already_scoped_statement (cp_parser
* parser
)
6651 /* If the token is a `{', then we must take special action. */
6652 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6653 cp_parser_statement (parser
, false);
6656 /* Avoid calling cp_parser_compound_statement, so that we
6657 don't create a new scope. Do everything else by hand. */
6658 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6659 cp_parser_statement_seq_opt (parser
, false);
6660 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6664 /* Declarations [gram.dcl.dcl] */
6666 /* Parse an optional declaration-sequence.
6670 declaration-seq declaration */
6673 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6679 token
= cp_lexer_peek_token (parser
->lexer
);
6681 if (token
->type
== CPP_CLOSE_BRACE
6682 || token
->type
== CPP_EOF
)
6685 if (token
->type
== CPP_SEMICOLON
)
6687 /* A declaration consisting of a single semicolon is
6688 invalid. Allow it unless we're being pedantic. */
6689 cp_lexer_consume_token (parser
->lexer
);
6690 if (pedantic
&& !in_system_header
)
6691 pedwarn ("extra %<;%>");
6695 /* If we're entering or exiting a region that's implicitly
6696 extern "C", modify the lang context appropriately. */
6697 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
6699 push_lang_context (lang_name_c
);
6700 parser
->implicit_extern_c
= true;
6702 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
6704 pop_lang_context ();
6705 parser
->implicit_extern_c
= false;
6708 if (token
->type
== CPP_PRAGMA
)
6710 /* A top-level declaration can consist solely of a #pragma.
6711 A nested declaration cannot, so this is done here and not
6712 in cp_parser_declaration. (A #pragma at block scope is
6713 handled in cp_parser_statement.) */
6714 cp_lexer_handle_pragma (parser
->lexer
);
6718 /* Parse the declaration itself. */
6719 cp_parser_declaration (parser
);
6723 /* Parse a declaration.
6728 template-declaration
6729 explicit-instantiation
6730 explicit-specialization
6731 linkage-specification
6732 namespace-definition
6737 __extension__ declaration */
6740 cp_parser_declaration (cp_parser
* parser
)
6747 /* Check for the `__extension__' keyword. */
6748 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6750 /* Parse the qualified declaration. */
6751 cp_parser_declaration (parser
);
6752 /* Restore the PEDANTIC flag. */
6753 pedantic
= saved_pedantic
;
6758 /* Try to figure out what kind of declaration is present. */
6759 token1
= *cp_lexer_peek_token (parser
->lexer
);
6761 if (token1
.type
!= CPP_EOF
)
6762 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6764 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6765 p
= obstack_alloc (&declarator_obstack
, 0);
6767 /* If the next token is `extern' and the following token is a string
6768 literal, then we have a linkage specification. */
6769 if (token1
.keyword
== RID_EXTERN
6770 && cp_parser_is_string_literal (&token2
))
6771 cp_parser_linkage_specification (parser
);
6772 /* If the next token is `template', then we have either a template
6773 declaration, an explicit instantiation, or an explicit
6775 else if (token1
.keyword
== RID_TEMPLATE
)
6777 /* `template <>' indicates a template specialization. */
6778 if (token2
.type
== CPP_LESS
6779 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6780 cp_parser_explicit_specialization (parser
);
6781 /* `template <' indicates a template declaration. */
6782 else if (token2
.type
== CPP_LESS
)
6783 cp_parser_template_declaration (parser
, /*member_p=*/false);
6784 /* Anything else must be an explicit instantiation. */
6786 cp_parser_explicit_instantiation (parser
);
6788 /* If the next token is `export', then we have a template
6790 else if (token1
.keyword
== RID_EXPORT
)
6791 cp_parser_template_declaration (parser
, /*member_p=*/false);
6792 /* If the next token is `extern', 'static' or 'inline' and the one
6793 after that is `template', we have a GNU extended explicit
6794 instantiation directive. */
6795 else if (cp_parser_allow_gnu_extensions_p (parser
)
6796 && (token1
.keyword
== RID_EXTERN
6797 || token1
.keyword
== RID_STATIC
6798 || token1
.keyword
== RID_INLINE
)
6799 && token2
.keyword
== RID_TEMPLATE
)
6800 cp_parser_explicit_instantiation (parser
);
6801 /* If the next token is `namespace', check for a named or unnamed
6802 namespace definition. */
6803 else if (token1
.keyword
== RID_NAMESPACE
6804 && (/* A named namespace definition. */
6805 (token2
.type
== CPP_NAME
6806 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6808 /* An unnamed namespace definition. */
6809 || token2
.type
== CPP_OPEN_BRACE
))
6810 cp_parser_namespace_definition (parser
);
6811 /* We must have either a block declaration or a function
6814 /* Try to parse a block-declaration, or a function-definition. */
6815 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6817 /* Free any declarators allocated. */
6818 obstack_free (&declarator_obstack
, p
);
6821 /* Parse a block-declaration.
6826 namespace-alias-definition
6833 __extension__ block-declaration
6836 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6837 part of a declaration-statement. */
6840 cp_parser_block_declaration (cp_parser
*parser
,
6846 /* Check for the `__extension__' keyword. */
6847 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6849 /* Parse the qualified declaration. */
6850 cp_parser_block_declaration (parser
, statement_p
);
6851 /* Restore the PEDANTIC flag. */
6852 pedantic
= saved_pedantic
;
6857 /* Peek at the next token to figure out which kind of declaration is
6859 token1
= cp_lexer_peek_token (parser
->lexer
);
6861 /* If the next keyword is `asm', we have an asm-definition. */
6862 if (token1
->keyword
== RID_ASM
)
6865 cp_parser_commit_to_tentative_parse (parser
);
6866 cp_parser_asm_definition (parser
);
6868 /* If the next keyword is `namespace', we have a
6869 namespace-alias-definition. */
6870 else if (token1
->keyword
== RID_NAMESPACE
)
6871 cp_parser_namespace_alias_definition (parser
);
6872 /* If the next keyword is `using', we have either a
6873 using-declaration or a using-directive. */
6874 else if (token1
->keyword
== RID_USING
)
6879 cp_parser_commit_to_tentative_parse (parser
);
6880 /* If the token after `using' is `namespace', then we have a
6882 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6883 if (token2
->keyword
== RID_NAMESPACE
)
6884 cp_parser_using_directive (parser
);
6885 /* Otherwise, it's a using-declaration. */
6887 cp_parser_using_declaration (parser
);
6889 /* If the next keyword is `__label__' we have a label declaration. */
6890 else if (token1
->keyword
== RID_LABEL
)
6893 cp_parser_commit_to_tentative_parse (parser
);
6894 cp_parser_label_declaration (parser
);
6896 /* Anything else must be a simple-declaration. */
6898 cp_parser_simple_declaration (parser
, !statement_p
);
6901 /* Parse a simple-declaration.
6904 decl-specifier-seq [opt] init-declarator-list [opt] ;
6906 init-declarator-list:
6908 init-declarator-list , init-declarator
6910 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6911 function-definition as a simple-declaration. */
6914 cp_parser_simple_declaration (cp_parser
* parser
,
6915 bool function_definition_allowed_p
)
6917 cp_decl_specifier_seq decl_specifiers
;
6918 int declares_class_or_enum
;
6919 bool saw_declarator
;
6921 /* Defer access checks until we know what is being declared; the
6922 checks for names appearing in the decl-specifier-seq should be
6923 done as if we were in the scope of the thing being declared. */
6924 push_deferring_access_checks (dk_deferred
);
6926 /* Parse the decl-specifier-seq. We have to keep track of whether
6927 or not the decl-specifier-seq declares a named class or
6928 enumeration type, since that is the only case in which the
6929 init-declarator-list is allowed to be empty.
6933 In a simple-declaration, the optional init-declarator-list can be
6934 omitted only when declaring a class or enumeration, that is when
6935 the decl-specifier-seq contains either a class-specifier, an
6936 elaborated-type-specifier, or an enum-specifier. */
6937 cp_parser_decl_specifier_seq (parser
,
6938 CP_PARSER_FLAGS_OPTIONAL
,
6940 &declares_class_or_enum
);
6941 /* We no longer need to defer access checks. */
6942 stop_deferring_access_checks ();
6944 /* In a block scope, a valid declaration must always have a
6945 decl-specifier-seq. By not trying to parse declarators, we can
6946 resolve the declaration/expression ambiguity more quickly. */
6947 if (!function_definition_allowed_p
6948 && !decl_specifiers
.any_specifiers_p
)
6950 cp_parser_error (parser
, "expected declaration");
6954 /* If the next two tokens are both identifiers, the code is
6955 erroneous. The usual cause of this situation is code like:
6959 where "T" should name a type -- but does not. */
6960 if (!decl_specifiers
.type
6961 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
6963 /* If parsing tentatively, we should commit; we really are
6964 looking at a declaration. */
6965 cp_parser_commit_to_tentative_parse (parser
);
6970 /* If we have seen at least one decl-specifier, and the next token
6971 is not a parenthesis, then we must be looking at a declaration.
6972 (After "int (" we might be looking at a functional cast.) */
6973 if (decl_specifiers
.any_specifiers_p
6974 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6975 cp_parser_commit_to_tentative_parse (parser
);
6977 /* Keep going until we hit the `;' at the end of the simple
6979 saw_declarator
= false;
6980 while (cp_lexer_next_token_is_not (parser
->lexer
,
6984 bool function_definition_p
;
6987 saw_declarator
= true;
6988 /* Parse the init-declarator. */
6989 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
6990 function_definition_allowed_p
,
6992 declares_class_or_enum
,
6993 &function_definition_p
);
6994 /* If an error occurred while parsing tentatively, exit quickly.
6995 (That usually happens when in the body of a function; each
6996 statement is treated as a declaration-statement until proven
6998 if (cp_parser_error_occurred (parser
))
7000 /* Handle function definitions specially. */
7001 if (function_definition_p
)
7003 /* If the next token is a `,', then we are probably
7004 processing something like:
7008 which is erroneous. */
7009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7010 error ("mixing declarations and function-definitions is forbidden");
7011 /* Otherwise, we're done with the list of declarators. */
7014 pop_deferring_access_checks ();
7018 /* The next token should be either a `,' or a `;'. */
7019 token
= cp_lexer_peek_token (parser
->lexer
);
7020 /* If it's a `,', there are more declarators to come. */
7021 if (token
->type
== CPP_COMMA
)
7022 cp_lexer_consume_token (parser
->lexer
);
7023 /* If it's a `;', we are done. */
7024 else if (token
->type
== CPP_SEMICOLON
)
7026 /* Anything else is an error. */
7029 /* If we have already issued an error message we don't need
7030 to issue another one. */
7031 if (decl
!= error_mark_node
7032 || (cp_parser_parsing_tentatively (parser
)
7033 && !cp_parser_committed_to_tentative_parse (parser
)))
7034 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7035 /* Skip tokens until we reach the end of the statement. */
7036 cp_parser_skip_to_end_of_statement (parser
);
7037 /* If the next token is now a `;', consume it. */
7038 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7039 cp_lexer_consume_token (parser
->lexer
);
7042 /* After the first time around, a function-definition is not
7043 allowed -- even if it was OK at first. For example:
7048 function_definition_allowed_p
= false;
7051 /* Issue an error message if no declarators are present, and the
7052 decl-specifier-seq does not itself declare a class or
7054 if (!saw_declarator
)
7056 if (cp_parser_declares_only_class_p (parser
))
7057 shadow_tag (&decl_specifiers
);
7058 /* Perform any deferred access checks. */
7059 perform_deferred_access_checks ();
7062 /* Consume the `;'. */
7063 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7066 pop_deferring_access_checks ();
7069 /* Parse a decl-specifier-seq.
7072 decl-specifier-seq [opt] decl-specifier
7075 storage-class-specifier
7086 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7088 The parser flags FLAGS is used to control type-specifier parsing.
7090 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7093 1: one of the decl-specifiers is an elaborated-type-specifier
7094 (i.e., a type declaration)
7095 2: one of the decl-specifiers is an enum-specifier or a
7096 class-specifier (i.e., a type definition)
7101 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7102 cp_parser_flags flags
,
7103 cp_decl_specifier_seq
*decl_specs
,
7104 int* declares_class_or_enum
)
7106 bool constructor_possible_p
= !parser
->in_declarator_p
;
7108 /* Clear DECL_SPECS. */
7109 clear_decl_specs (decl_specs
);
7111 /* Assume no class or enumeration type is declared. */
7112 *declares_class_or_enum
= 0;
7114 /* Keep reading specifiers until there are no more to read. */
7118 bool found_decl_spec
;
7121 /* Peek at the next token. */
7122 token
= cp_lexer_peek_token (parser
->lexer
);
7123 /* Handle attributes. */
7124 if (token
->keyword
== RID_ATTRIBUTE
)
7126 /* Parse the attributes. */
7127 decl_specs
->attributes
7128 = chainon (decl_specs
->attributes
,
7129 cp_parser_attributes_opt (parser
));
7132 /* Assume we will find a decl-specifier keyword. */
7133 found_decl_spec
= true;
7134 /* If the next token is an appropriate keyword, we can simply
7135 add it to the list. */
7136 switch (token
->keyword
)
7141 if (decl_specs
->specs
[(int) ds_friend
]++)
7142 error ("duplicate %<friend%>");
7143 /* Consume the token. */
7144 cp_lexer_consume_token (parser
->lexer
);
7147 /* function-specifier:
7154 cp_parser_function_specifier_opt (parser
, decl_specs
);
7160 ++decl_specs
->specs
[(int) ds_typedef
];
7161 /* Consume the token. */
7162 cp_lexer_consume_token (parser
->lexer
);
7163 /* A constructor declarator cannot appear in a typedef. */
7164 constructor_possible_p
= false;
7165 /* The "typedef" keyword can only occur in a declaration; we
7166 may as well commit at this point. */
7167 cp_parser_commit_to_tentative_parse (parser
);
7170 /* storage-class-specifier:
7180 /* Consume the token. */
7181 cp_lexer_consume_token (parser
->lexer
);
7182 cp_parser_set_storage_class (decl_specs
, sc_auto
);
7185 /* Consume the token. */
7186 cp_lexer_consume_token (parser
->lexer
);
7187 cp_parser_set_storage_class (decl_specs
, sc_register
);
7190 /* Consume the token. */
7191 cp_lexer_consume_token (parser
->lexer
);
7192 if (decl_specs
->specs
[(int) ds_thread
])
7194 error ("%<__thread%> before %<static%>");
7195 decl_specs
->specs
[(int) ds_thread
] = 0;
7197 cp_parser_set_storage_class (decl_specs
, sc_static
);
7200 /* Consume the token. */
7201 cp_lexer_consume_token (parser
->lexer
);
7202 if (decl_specs
->specs
[(int) ds_thread
])
7204 error ("%<__thread%> before %<extern%>");
7205 decl_specs
->specs
[(int) ds_thread
] = 0;
7207 cp_parser_set_storage_class (decl_specs
, sc_extern
);
7210 /* Consume the token. */
7211 cp_lexer_consume_token (parser
->lexer
);
7212 cp_parser_set_storage_class (decl_specs
, sc_mutable
);
7215 /* Consume the token. */
7216 cp_lexer_consume_token (parser
->lexer
);
7217 ++decl_specs
->specs
[(int) ds_thread
];
7221 /* We did not yet find a decl-specifier yet. */
7222 found_decl_spec
= false;
7226 /* Constructors are a special case. The `S' in `S()' is not a
7227 decl-specifier; it is the beginning of the declarator. */
7230 && constructor_possible_p
7231 && (cp_parser_constructor_declarator_p
7232 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7234 /* If we don't have a DECL_SPEC yet, then we must be looking at
7235 a type-specifier. */
7236 if (!found_decl_spec
&& !constructor_p
)
7238 int decl_spec_declares_class_or_enum
;
7239 bool is_cv_qualifier
;
7243 = cp_parser_type_specifier (parser
, flags
,
7245 /*is_declaration=*/true,
7246 &decl_spec_declares_class_or_enum
,
7249 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7251 /* If this type-specifier referenced a user-defined type
7252 (a typedef, class-name, etc.), then we can't allow any
7253 more such type-specifiers henceforth.
7257 The longest sequence of decl-specifiers that could
7258 possibly be a type name is taken as the
7259 decl-specifier-seq of a declaration. The sequence shall
7260 be self-consistent as described below.
7264 As a general rule, at most one type-specifier is allowed
7265 in the complete decl-specifier-seq of a declaration. The
7266 only exceptions are the following:
7268 -- const or volatile can be combined with any other
7271 -- signed or unsigned can be combined with char, long,
7279 void g (const int Pc);
7281 Here, Pc is *not* part of the decl-specifier seq; it's
7282 the declarator. Therefore, once we see a type-specifier
7283 (other than a cv-qualifier), we forbid any additional
7284 user-defined types. We *do* still allow things like `int
7285 int' to be considered a decl-specifier-seq, and issue the
7286 error message later. */
7287 if (type_spec
&& !is_cv_qualifier
)
7288 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7289 /* A constructor declarator cannot follow a type-specifier. */
7292 constructor_possible_p
= false;
7293 found_decl_spec
= true;
7297 /* If we still do not have a DECL_SPEC, then there are no more
7299 if (!found_decl_spec
)
7302 decl_specs
->any_specifiers_p
= true;
7303 /* After we see one decl-specifier, further decl-specifiers are
7305 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7308 /* Don't allow a friend specifier with a class definition. */
7309 if (decl_specs
->specs
[(int) ds_friend
] != 0
7310 && (*declares_class_or_enum
& 2))
7311 error ("class definition may not be declared a friend");
7314 /* Parse an (optional) storage-class-specifier.
7316 storage-class-specifier:
7325 storage-class-specifier:
7328 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7331 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7333 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7341 /* Consume the token. */
7342 return cp_lexer_consume_token (parser
->lexer
)->value
;
7349 /* Parse an (optional) function-specifier.
7356 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7357 Updates DECL_SPECS, if it is non-NULL. */
7360 cp_parser_function_specifier_opt (cp_parser
* parser
,
7361 cp_decl_specifier_seq
*decl_specs
)
7363 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7367 ++decl_specs
->specs
[(int) ds_inline
];
7372 ++decl_specs
->specs
[(int) ds_virtual
];
7377 ++decl_specs
->specs
[(int) ds_explicit
];
7384 /* Consume the token. */
7385 return cp_lexer_consume_token (parser
->lexer
)->value
;
7388 /* Parse a linkage-specification.
7390 linkage-specification:
7391 extern string-literal { declaration-seq [opt] }
7392 extern string-literal declaration */
7395 cp_parser_linkage_specification (cp_parser
* parser
)
7399 /* Look for the `extern' keyword. */
7400 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7402 /* Look for the string-literal. */
7403 linkage
= cp_parser_string_literal (parser
, false, false);
7405 /* Transform the literal into an identifier. If the literal is a
7406 wide-character string, or contains embedded NULs, then we can't
7407 handle it as the user wants. */
7408 if (strlen (TREE_STRING_POINTER (linkage
))
7409 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7411 cp_parser_error (parser
, "invalid linkage-specification");
7412 /* Assume C++ linkage. */
7413 linkage
= lang_name_cplusplus
;
7416 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7418 /* We're now using the new linkage. */
7419 push_lang_context (linkage
);
7421 /* If the next token is a `{', then we're using the first
7423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7425 /* Consume the `{' token. */
7426 cp_lexer_consume_token (parser
->lexer
);
7427 /* Parse the declarations. */
7428 cp_parser_declaration_seq_opt (parser
);
7429 /* Look for the closing `}'. */
7430 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7432 /* Otherwise, there's just one declaration. */
7435 bool saved_in_unbraced_linkage_specification_p
;
7437 saved_in_unbraced_linkage_specification_p
7438 = parser
->in_unbraced_linkage_specification_p
;
7439 parser
->in_unbraced_linkage_specification_p
= true;
7440 have_extern_spec
= true;
7441 cp_parser_declaration (parser
);
7442 have_extern_spec
= false;
7443 parser
->in_unbraced_linkage_specification_p
7444 = saved_in_unbraced_linkage_specification_p
;
7447 /* We're done with the linkage-specification. */
7448 pop_lang_context ();
7451 /* Special member functions [gram.special] */
7453 /* Parse a conversion-function-id.
7455 conversion-function-id:
7456 operator conversion-type-id
7458 Returns an IDENTIFIER_NODE representing the operator. */
7461 cp_parser_conversion_function_id (cp_parser
* parser
)
7465 tree saved_qualifying_scope
;
7466 tree saved_object_scope
;
7469 /* Look for the `operator' token. */
7470 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7471 return error_mark_node
;
7472 /* When we parse the conversion-type-id, the current scope will be
7473 reset. However, we need that information in able to look up the
7474 conversion function later, so we save it here. */
7475 saved_scope
= parser
->scope
;
7476 saved_qualifying_scope
= parser
->qualifying_scope
;
7477 saved_object_scope
= parser
->object_scope
;
7478 /* We must enter the scope of the class so that the names of
7479 entities declared within the class are available in the
7480 conversion-type-id. For example, consider:
7487 S::operator I() { ... }
7489 In order to see that `I' is a type-name in the definition, we
7490 must be in the scope of `S'. */
7492 pop_p
= push_scope (saved_scope
);
7493 /* Parse the conversion-type-id. */
7494 type
= cp_parser_conversion_type_id (parser
);
7495 /* Leave the scope of the class, if any. */
7497 pop_scope (saved_scope
);
7498 /* Restore the saved scope. */
7499 parser
->scope
= saved_scope
;
7500 parser
->qualifying_scope
= saved_qualifying_scope
;
7501 parser
->object_scope
= saved_object_scope
;
7502 /* If the TYPE is invalid, indicate failure. */
7503 if (type
== error_mark_node
)
7504 return error_mark_node
;
7505 return mangle_conv_op_name_for_type (type
);
7508 /* Parse a conversion-type-id:
7511 type-specifier-seq conversion-declarator [opt]
7513 Returns the TYPE specified. */
7516 cp_parser_conversion_type_id (cp_parser
* parser
)
7519 cp_decl_specifier_seq type_specifiers
;
7520 cp_declarator
*declarator
;
7521 tree type_specified
;
7523 /* Parse the attributes. */
7524 attributes
= cp_parser_attributes_opt (parser
);
7525 /* Parse the type-specifiers. */
7526 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
7527 /* If that didn't work, stop. */
7528 if (type_specifiers
.type
== error_mark_node
)
7529 return error_mark_node
;
7530 /* Parse the conversion-declarator. */
7531 declarator
= cp_parser_conversion_declarator_opt (parser
);
7533 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7534 /*initialized=*/0, &attributes
);
7536 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7537 return type_specified
;
7540 /* Parse an (optional) conversion-declarator.
7542 conversion-declarator:
7543 ptr-operator conversion-declarator [opt]
7547 static cp_declarator
*
7548 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7550 enum tree_code code
;
7552 cp_cv_quals cv_quals
;
7554 /* We don't know if there's a ptr-operator next, or not. */
7555 cp_parser_parse_tentatively (parser
);
7556 /* Try the ptr-operator. */
7557 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7558 /* If it worked, look for more conversion-declarators. */
7559 if (cp_parser_parse_definitely (parser
))
7561 cp_declarator
*declarator
;
7563 /* Parse another optional declarator. */
7564 declarator
= cp_parser_conversion_declarator_opt (parser
);
7566 /* Create the representation of the declarator. */
7568 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7570 else if (code
== INDIRECT_REF
)
7571 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7573 declarator
= make_reference_declarator (cv_quals
, declarator
);
7581 /* Parse an (optional) ctor-initializer.
7584 : mem-initializer-list
7586 Returns TRUE iff the ctor-initializer was actually present. */
7589 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7591 /* If the next token is not a `:', then there is no
7592 ctor-initializer. */
7593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7595 /* Do default initialization of any bases and members. */
7596 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7597 finish_mem_initializers (NULL_TREE
);
7602 /* Consume the `:' token. */
7603 cp_lexer_consume_token (parser
->lexer
);
7604 /* And the mem-initializer-list. */
7605 cp_parser_mem_initializer_list (parser
);
7610 /* Parse a mem-initializer-list.
7612 mem-initializer-list:
7614 mem-initializer , mem-initializer-list */
7617 cp_parser_mem_initializer_list (cp_parser
* parser
)
7619 tree mem_initializer_list
= NULL_TREE
;
7621 /* Let the semantic analysis code know that we are starting the
7622 mem-initializer-list. */
7623 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7624 error ("only constructors take base initializers");
7626 /* Loop through the list. */
7629 tree mem_initializer
;
7631 /* Parse the mem-initializer. */
7632 mem_initializer
= cp_parser_mem_initializer (parser
);
7633 /* Add it to the list, unless it was erroneous. */
7634 if (mem_initializer
)
7636 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7637 mem_initializer_list
= mem_initializer
;
7639 /* If the next token is not a `,', we're done. */
7640 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7642 /* Consume the `,' token. */
7643 cp_lexer_consume_token (parser
->lexer
);
7646 /* Perform semantic analysis. */
7647 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7648 finish_mem_initializers (mem_initializer_list
);
7651 /* Parse a mem-initializer.
7654 mem-initializer-id ( expression-list [opt] )
7659 ( expression-list [opt] )
7661 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7662 class) or FIELD_DECL (for a non-static data member) to initialize;
7663 the TREE_VALUE is the expression-list. */
7666 cp_parser_mem_initializer (cp_parser
* parser
)
7668 tree mem_initializer_id
;
7669 tree expression_list
;
7672 /* Find out what is being initialized. */
7673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7675 pedwarn ("anachronistic old-style base class initializer");
7676 mem_initializer_id
= NULL_TREE
;
7679 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7680 member
= expand_member_init (mem_initializer_id
);
7681 if (member
&& !DECL_P (member
))
7682 in_base_initializer
= 1;
7685 = cp_parser_parenthesized_expression_list (parser
, false,
7686 /*non_constant_p=*/NULL
);
7687 if (!expression_list
)
7688 expression_list
= void_type_node
;
7690 in_base_initializer
= 0;
7692 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7695 /* Parse a mem-initializer-id.
7698 :: [opt] nested-name-specifier [opt] class-name
7701 Returns a TYPE indicating the class to be initializer for the first
7702 production. Returns an IDENTIFIER_NODE indicating the data member
7703 to be initialized for the second production. */
7706 cp_parser_mem_initializer_id (cp_parser
* parser
)
7708 bool global_scope_p
;
7709 bool nested_name_specifier_p
;
7710 bool template_p
= false;
7713 /* `typename' is not allowed in this context ([temp.res]). */
7714 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
7716 error ("keyword %<typename%> not allowed in this context (a qualified "
7717 "member initializer is implicitly a type)");
7718 cp_lexer_consume_token (parser
->lexer
);
7720 /* Look for the optional `::' operator. */
7722 = (cp_parser_global_scope_opt (parser
,
7723 /*current_scope_valid_p=*/false)
7725 /* Look for the optional nested-name-specifier. The simplest way to
7730 The keyword `typename' is not permitted in a base-specifier or
7731 mem-initializer; in these contexts a qualified name that
7732 depends on a template-parameter is implicitly assumed to be a
7735 is to assume that we have seen the `typename' keyword at this
7737 nested_name_specifier_p
7738 = (cp_parser_nested_name_specifier_opt (parser
,
7739 /*typename_keyword_p=*/true,
7740 /*check_dependency_p=*/true,
7742 /*is_declaration=*/true)
7744 if (nested_name_specifier_p
)
7745 template_p
= cp_parser_optional_template_keyword (parser
);
7746 /* If there is a `::' operator or a nested-name-specifier, then we
7747 are definitely looking for a class-name. */
7748 if (global_scope_p
|| nested_name_specifier_p
)
7749 return cp_parser_class_name (parser
,
7750 /*typename_keyword_p=*/true,
7751 /*template_keyword_p=*/template_p
,
7753 /*check_dependency_p=*/true,
7754 /*class_head_p=*/false,
7755 /*is_declaration=*/true);
7756 /* Otherwise, we could also be looking for an ordinary identifier. */
7757 cp_parser_parse_tentatively (parser
);
7758 /* Try a class-name. */
7759 id
= cp_parser_class_name (parser
,
7760 /*typename_keyword_p=*/true,
7761 /*template_keyword_p=*/false,
7763 /*check_dependency_p=*/true,
7764 /*class_head_p=*/false,
7765 /*is_declaration=*/true);
7766 /* If we found one, we're done. */
7767 if (cp_parser_parse_definitely (parser
))
7769 /* Otherwise, look for an ordinary identifier. */
7770 return cp_parser_identifier (parser
);
7773 /* Overloading [gram.over] */
7775 /* Parse an operator-function-id.
7777 operator-function-id:
7780 Returns an IDENTIFIER_NODE for the operator which is a
7781 human-readable spelling of the identifier, e.g., `operator +'. */
7784 cp_parser_operator_function_id (cp_parser
* parser
)
7786 /* Look for the `operator' keyword. */
7787 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7788 return error_mark_node
;
7789 /* And then the name of the operator itself. */
7790 return cp_parser_operator (parser
);
7793 /* Parse an operator.
7796 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7797 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7798 || ++ -- , ->* -> () []
7805 Returns an IDENTIFIER_NODE for the operator which is a
7806 human-readable spelling of the identifier, e.g., `operator +'. */
7809 cp_parser_operator (cp_parser
* parser
)
7811 tree id
= NULL_TREE
;
7814 /* Peek at the next token. */
7815 token
= cp_lexer_peek_token (parser
->lexer
);
7816 /* Figure out which operator we have. */
7817 switch (token
->type
)
7823 /* The keyword should be either `new' or `delete'. */
7824 if (token
->keyword
== RID_NEW
)
7826 else if (token
->keyword
== RID_DELETE
)
7831 /* Consume the `new' or `delete' token. */
7832 cp_lexer_consume_token (parser
->lexer
);
7834 /* Peek at the next token. */
7835 token
= cp_lexer_peek_token (parser
->lexer
);
7836 /* If it's a `[' token then this is the array variant of the
7838 if (token
->type
== CPP_OPEN_SQUARE
)
7840 /* Consume the `[' token. */
7841 cp_lexer_consume_token (parser
->lexer
);
7842 /* Look for the `]' token. */
7843 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7844 id
= ansi_opname (op
== NEW_EXPR
7845 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7847 /* Otherwise, we have the non-array variant. */
7849 id
= ansi_opname (op
);
7855 id
= ansi_opname (PLUS_EXPR
);
7859 id
= ansi_opname (MINUS_EXPR
);
7863 id
= ansi_opname (MULT_EXPR
);
7867 id
= ansi_opname (TRUNC_DIV_EXPR
);
7871 id
= ansi_opname (TRUNC_MOD_EXPR
);
7875 id
= ansi_opname (BIT_XOR_EXPR
);
7879 id
= ansi_opname (BIT_AND_EXPR
);
7883 id
= ansi_opname (BIT_IOR_EXPR
);
7887 id
= ansi_opname (BIT_NOT_EXPR
);
7891 id
= ansi_opname (TRUTH_NOT_EXPR
);
7895 id
= ansi_assopname (NOP_EXPR
);
7899 id
= ansi_opname (LT_EXPR
);
7903 id
= ansi_opname (GT_EXPR
);
7907 id
= ansi_assopname (PLUS_EXPR
);
7911 id
= ansi_assopname (MINUS_EXPR
);
7915 id
= ansi_assopname (MULT_EXPR
);
7919 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7923 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7927 id
= ansi_assopname (BIT_XOR_EXPR
);
7931 id
= ansi_assopname (BIT_AND_EXPR
);
7935 id
= ansi_assopname (BIT_IOR_EXPR
);
7939 id
= ansi_opname (LSHIFT_EXPR
);
7943 id
= ansi_opname (RSHIFT_EXPR
);
7947 id
= ansi_assopname (LSHIFT_EXPR
);
7951 id
= ansi_assopname (RSHIFT_EXPR
);
7955 id
= ansi_opname (EQ_EXPR
);
7959 id
= ansi_opname (NE_EXPR
);
7963 id
= ansi_opname (LE_EXPR
);
7966 case CPP_GREATER_EQ
:
7967 id
= ansi_opname (GE_EXPR
);
7971 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7975 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7979 id
= ansi_opname (POSTINCREMENT_EXPR
);
7982 case CPP_MINUS_MINUS
:
7983 id
= ansi_opname (PREDECREMENT_EXPR
);
7987 id
= ansi_opname (COMPOUND_EXPR
);
7990 case CPP_DEREF_STAR
:
7991 id
= ansi_opname (MEMBER_REF
);
7995 id
= ansi_opname (COMPONENT_REF
);
7998 case CPP_OPEN_PAREN
:
7999 /* Consume the `('. */
8000 cp_lexer_consume_token (parser
->lexer
);
8001 /* Look for the matching `)'. */
8002 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8003 return ansi_opname (CALL_EXPR
);
8005 case CPP_OPEN_SQUARE
:
8006 /* Consume the `['. */
8007 cp_lexer_consume_token (parser
->lexer
);
8008 /* Look for the matching `]'. */
8009 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8010 return ansi_opname (ARRAY_REF
);
8014 id
= ansi_opname (MIN_EXPR
);
8018 id
= ansi_opname (MAX_EXPR
);
8022 id
= ansi_assopname (MIN_EXPR
);
8026 id
= ansi_assopname (MAX_EXPR
);
8030 /* Anything else is an error. */
8034 /* If we have selected an identifier, we need to consume the
8037 cp_lexer_consume_token (parser
->lexer
);
8038 /* Otherwise, no valid operator name was present. */
8041 cp_parser_error (parser
, "expected operator");
8042 id
= error_mark_node
;
8048 /* Parse a template-declaration.
8050 template-declaration:
8051 export [opt] template < template-parameter-list > declaration
8053 If MEMBER_P is TRUE, this template-declaration occurs within a
8056 The grammar rule given by the standard isn't correct. What
8059 template-declaration:
8060 export [opt] template-parameter-list-seq
8061 decl-specifier-seq [opt] init-declarator [opt] ;
8062 export [opt] template-parameter-list-seq
8065 template-parameter-list-seq:
8066 template-parameter-list-seq [opt]
8067 template < template-parameter-list > */
8070 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8072 /* Check for `export'. */
8073 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8075 /* Consume the `export' token. */
8076 cp_lexer_consume_token (parser
->lexer
);
8077 /* Warn that we do not support `export'. */
8078 warning ("keyword %<export%> not implemented, and will be ignored");
8081 cp_parser_template_declaration_after_export (parser
, member_p
);
8084 /* Parse a template-parameter-list.
8086 template-parameter-list:
8088 template-parameter-list , template-parameter
8090 Returns a TREE_LIST. Each node represents a template parameter.
8091 The nodes are connected via their TREE_CHAINs. */
8094 cp_parser_template_parameter_list (cp_parser
* parser
)
8096 tree parameter_list
= NULL_TREE
;
8104 /* Parse the template-parameter. */
8105 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8106 /* Add it to the list. */
8107 parameter_list
= process_template_parm (parameter_list
,
8110 /* Peek at the next token. */
8111 token
= cp_lexer_peek_token (parser
->lexer
);
8112 /* If it's not a `,', we're done. */
8113 if (token
->type
!= CPP_COMMA
)
8115 /* Otherwise, consume the `,' token. */
8116 cp_lexer_consume_token (parser
->lexer
);
8119 return parameter_list
;
8122 /* Parse a template-parameter.
8126 parameter-declaration
8128 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8129 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8130 true iff this parameter is a non-type parameter. */
8133 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8136 cp_parameter_declarator
*parameter_declarator
;
8138 /* Assume it is a type parameter or a template parameter. */
8139 *is_non_type
= false;
8140 /* Peek at the next token. */
8141 token
= cp_lexer_peek_token (parser
->lexer
);
8142 /* If it is `class' or `template', we have a type-parameter. */
8143 if (token
->keyword
== RID_TEMPLATE
)
8144 return cp_parser_type_parameter (parser
);
8145 /* If it is `class' or `typename' we do not know yet whether it is a
8146 type parameter or a non-type parameter. Consider:
8148 template <typename T, typename T::X X> ...
8152 template <class C, class D*> ...
8154 Here, the first parameter is a type parameter, and the second is
8155 a non-type parameter. We can tell by looking at the token after
8156 the identifier -- if it is a `,', `=', or `>' then we have a type
8158 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8160 /* Peek at the token after `class' or `typename'. */
8161 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8162 /* If it's an identifier, skip it. */
8163 if (token
->type
== CPP_NAME
)
8164 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8165 /* Now, see if the token looks like the end of a template
8167 if (token
->type
== CPP_COMMA
8168 || token
->type
== CPP_EQ
8169 || token
->type
== CPP_GREATER
)
8170 return cp_parser_type_parameter (parser
);
8173 /* Otherwise, it is a non-type parameter.
8177 When parsing a default template-argument for a non-type
8178 template-parameter, the first non-nested `>' is taken as the end
8179 of the template parameter-list rather than a greater-than
8181 *is_non_type
= true;
8182 parameter_declarator
8183 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8184 /*parenthesized_p=*/NULL
);
8185 return (build_tree_list
8186 (parameter_declarator
->default_argument
,
8187 grokdeclarator (parameter_declarator
->declarator
,
8188 ¶meter_declarator
->decl_specifiers
,
8189 PARM
, /*initialized=*/0,
8190 /*attrlist=*/NULL
)));
8193 /* Parse a type-parameter.
8196 class identifier [opt]
8197 class identifier [opt] = type-id
8198 typename identifier [opt]
8199 typename identifier [opt] = type-id
8200 template < template-parameter-list > class identifier [opt]
8201 template < template-parameter-list > class identifier [opt]
8204 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8205 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8206 the declaration of the parameter. */
8209 cp_parser_type_parameter (cp_parser
* parser
)
8214 /* Look for a keyword to tell us what kind of parameter this is. */
8215 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8216 "`class', `typename', or `template'");
8218 return error_mark_node
;
8220 switch (token
->keyword
)
8226 tree default_argument
;
8228 /* If the next token is an identifier, then it names the
8230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8231 identifier
= cp_parser_identifier (parser
);
8233 identifier
= NULL_TREE
;
8235 /* Create the parameter. */
8236 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8238 /* If the next token is an `=', we have a default argument. */
8239 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8241 /* Consume the `=' token. */
8242 cp_lexer_consume_token (parser
->lexer
);
8243 /* Parse the default-argument. */
8244 default_argument
= cp_parser_type_id (parser
);
8247 default_argument
= NULL_TREE
;
8249 /* Create the combined representation of the parameter and the
8250 default argument. */
8251 parameter
= build_tree_list (default_argument
, parameter
);
8257 tree parameter_list
;
8259 tree default_argument
;
8261 /* Look for the `<'. */
8262 cp_parser_require (parser
, CPP_LESS
, "`<'");
8263 /* Parse the template-parameter-list. */
8264 begin_template_parm_list ();
8266 = cp_parser_template_parameter_list (parser
);
8267 parameter_list
= end_template_parm_list (parameter_list
);
8268 /* Look for the `>'. */
8269 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8270 /* Look for the `class' keyword. */
8271 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8272 /* If the next token is an `=', then there is a
8273 default-argument. If the next token is a `>', we are at
8274 the end of the parameter-list. If the next token is a `,',
8275 then we are at the end of this parameter. */
8276 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8277 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8278 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8279 identifier
= cp_parser_identifier (parser
);
8281 identifier
= NULL_TREE
;
8282 /* Create the template parameter. */
8283 parameter
= finish_template_template_parm (class_type_node
,
8286 /* If the next token is an `=', then there is a
8287 default-argument. */
8288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8292 /* Consume the `='. */
8293 cp_lexer_consume_token (parser
->lexer
);
8294 /* Parse the id-expression. */
8296 = cp_parser_id_expression (parser
,
8297 /*template_keyword_p=*/false,
8298 /*check_dependency_p=*/true,
8299 /*template_p=*/&is_template
,
8300 /*declarator_p=*/false);
8301 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8302 /* If the id-expression was a template-id that refers to
8303 a template-class, we already have the declaration here,
8304 so no further lookup is needed. */
8307 /* Look up the name. */
8309 = cp_parser_lookup_name (parser
, default_argument
,
8311 /*is_template=*/is_template
,
8312 /*is_namespace=*/false,
8313 /*check_dependency=*/true,
8314 /*ambiguous_p=*/NULL
);
8315 /* See if the default argument is valid. */
8317 = check_template_template_default_arg (default_argument
);
8320 default_argument
= NULL_TREE
;
8322 /* Create the combined representation of the parameter and the
8323 default argument. */
8324 parameter
= build_tree_list (default_argument
, parameter
);
8329 /* Anything else is an error. */
8330 cp_parser_error (parser
,
8331 "expected %<class%>, %<typename%>, or %<template%>");
8332 parameter
= error_mark_node
;
8338 /* Parse a template-id.
8341 template-name < template-argument-list [opt] >
8343 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8344 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8345 returned. Otherwise, if the template-name names a function, or set
8346 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8347 names a class, returns a TYPE_DECL for the specialization.
8349 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8350 uninstantiated templates. */
8353 cp_parser_template_id (cp_parser
*parser
,
8354 bool template_keyword_p
,
8355 bool check_dependency_p
,
8356 bool is_declaration
)
8361 ptrdiff_t start_of_id
;
8362 tree access_check
= NULL_TREE
;
8363 cp_token
*next_token
, *next_token_2
;
8366 /* If the next token corresponds to a template-id, there is no need
8368 next_token
= cp_lexer_peek_token (parser
->lexer
);
8369 if (next_token
->type
== CPP_TEMPLATE_ID
)
8374 /* Get the stored value. */
8375 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8376 /* Perform any access checks that were deferred. */
8377 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8378 perform_or_defer_access_check (TREE_PURPOSE (check
),
8379 TREE_VALUE (check
));
8380 /* Return the stored value. */
8381 return TREE_VALUE (value
);
8384 /* Avoid performing name lookup if there is no possibility of
8385 finding a template-id. */
8386 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8387 || (next_token
->type
== CPP_NAME
8388 && !cp_parser_nth_token_starts_template_argument_list_p
8391 cp_parser_error (parser
, "expected template-id");
8392 return error_mark_node
;
8395 /* Remember where the template-id starts. */
8396 if (cp_parser_parsing_tentatively (parser
)
8397 && !cp_parser_committed_to_tentative_parse (parser
))
8399 next_token
= cp_lexer_peek_token (parser
->lexer
);
8400 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
8401 parser
->lexer
->buffer
,
8407 push_deferring_access_checks (dk_deferred
);
8409 /* Parse the template-name. */
8410 is_identifier
= false;
8411 template = cp_parser_template_name (parser
, template_keyword_p
,
8415 if (template == error_mark_node
|| is_identifier
)
8417 pop_deferring_access_checks ();
8421 /* If we find the sequence `[:' after a template-name, it's probably
8422 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8423 parse correctly the argument list. */
8424 next_token
= cp_lexer_peek_token (parser
->lexer
);
8425 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8426 if (next_token
->type
== CPP_OPEN_SQUARE
8427 && next_token
->flags
& DIGRAPH
8428 && next_token_2
->type
== CPP_COLON
8429 && !(next_token_2
->flags
& PREV_WHITE
))
8431 cp_parser_parse_tentatively (parser
);
8432 /* Change `:' into `::'. */
8433 next_token_2
->type
= CPP_SCOPE
;
8434 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8436 cp_lexer_consume_token (parser
->lexer
);
8437 /* Parse the arguments. */
8438 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8439 if (!cp_parser_parse_definitely (parser
))
8441 /* If we couldn't parse an argument list, then we revert our changes
8442 and return simply an error. Maybe this is not a template-id
8444 next_token_2
->type
= CPP_COLON
;
8445 cp_parser_error (parser
, "expected %<<%>");
8446 pop_deferring_access_checks ();
8447 return error_mark_node
;
8449 /* Otherwise, emit an error about the invalid digraph, but continue
8450 parsing because we got our argument list. */
8451 pedwarn ("%<<::%> cannot begin a template-argument list");
8452 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8453 "between %<<%> and %<::%>");
8454 if (!flag_permissive
)
8459 inform ("(if you use -fpermissive G++ will accept your code)");
8466 /* Look for the `<' that starts the template-argument-list. */
8467 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8469 pop_deferring_access_checks ();
8470 return error_mark_node
;
8472 /* Parse the arguments. */
8473 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8476 /* Build a representation of the specialization. */
8477 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8478 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8479 else if (DECL_CLASS_TEMPLATE_P (template)
8480 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8482 = finish_template_type (template, arguments
,
8483 cp_lexer_next_token_is (parser
->lexer
,
8487 /* If it's not a class-template or a template-template, it should be
8488 a function-template. */
8489 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8490 || TREE_CODE (template) == OVERLOAD
8491 || BASELINK_P (template)));
8493 template_id
= lookup_template_function (template, arguments
);
8496 /* Retrieve any deferred checks. Do not pop this access checks yet
8497 so the memory will not be reclaimed during token replacing below. */
8498 access_check
= get_deferred_access_checks ();
8500 /* If parsing tentatively, replace the sequence of tokens that makes
8501 up the template-id with a CPP_TEMPLATE_ID token. That way,
8502 should we re-parse the token stream, we will not have to repeat
8503 the effort required to do the parse, nor will we issue duplicate
8504 error messages about problems during instantiation of the
8506 if (start_of_id
>= 0)
8510 /* Find the token that corresponds to the start of the
8512 token
= cp_lexer_advance_token (parser
->lexer
,
8513 parser
->lexer
->buffer
,
8516 /* Reset the contents of the START_OF_ID token. */
8517 token
->type
= CPP_TEMPLATE_ID
;
8518 token
->value
= build_tree_list (access_check
, template_id
);
8519 token
->keyword
= RID_MAX
;
8520 /* Purge all subsequent tokens. */
8521 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8524 pop_deferring_access_checks ();
8528 /* Parse a template-name.
8533 The standard should actually say:
8537 operator-function-id
8539 A defect report has been filed about this issue.
8541 A conversion-function-id cannot be a template name because they cannot
8542 be part of a template-id. In fact, looking at this code:
8546 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8547 It is impossible to call a templated conversion-function-id with an
8548 explicit argument list, since the only allowed template parameter is
8549 the type to which it is converting.
8551 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8552 `template' keyword, in a construction like:
8556 In that case `f' is taken to be a template-name, even though there
8557 is no way of knowing for sure.
8559 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8560 name refers to a set of overloaded functions, at least one of which
8561 is a template, or an IDENTIFIER_NODE with the name of the template,
8562 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8563 names are looked up inside uninstantiated templates. */
8566 cp_parser_template_name (cp_parser
* parser
,
8567 bool template_keyword_p
,
8568 bool check_dependency_p
,
8569 bool is_declaration
,
8570 bool *is_identifier
)
8576 /* If the next token is `operator', then we have either an
8577 operator-function-id or a conversion-function-id. */
8578 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8580 /* We don't know whether we're looking at an
8581 operator-function-id or a conversion-function-id. */
8582 cp_parser_parse_tentatively (parser
);
8583 /* Try an operator-function-id. */
8584 identifier
= cp_parser_operator_function_id (parser
);
8585 /* If that didn't work, try a conversion-function-id. */
8586 if (!cp_parser_parse_definitely (parser
))
8588 cp_parser_error (parser
, "expected template-name");
8589 return error_mark_node
;
8592 /* Look for the identifier. */
8594 identifier
= cp_parser_identifier (parser
);
8596 /* If we didn't find an identifier, we don't have a template-id. */
8597 if (identifier
== error_mark_node
)
8598 return error_mark_node
;
8600 /* If the name immediately followed the `template' keyword, then it
8601 is a template-name. However, if the next token is not `<', then
8602 we do not treat it as a template-name, since it is not being used
8603 as part of a template-id. This enables us to handle constructs
8606 template <typename T> struct S { S(); };
8607 template <typename T> S<T>::S();
8609 correctly. We would treat `S' as a template -- if it were `S<T>'
8610 -- but we do not if there is no `<'. */
8612 if (processing_template_decl
8613 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8615 /* In a declaration, in a dependent context, we pretend that the
8616 "template" keyword was present in order to improve error
8617 recovery. For example, given:
8619 template <typename T> void f(T::X<int>);
8621 we want to treat "X<int>" as a template-id. */
8623 && !template_keyword_p
8624 && parser
->scope
&& TYPE_P (parser
->scope
)
8625 && check_dependency_p
8626 && dependent_type_p (parser
->scope
)
8627 /* Do not do this for dtors (or ctors), since they never
8628 need the template keyword before their name. */
8629 && !constructor_name_p (identifier
, parser
->scope
))
8633 /* Explain what went wrong. */
8634 error ("non-template %qD used as template", identifier
);
8635 inform ("use %<%T::template %D%> to indicate that it is a template",
8636 parser
->scope
, identifier
);
8637 /* If parsing tentatively, find the location of the "<"
8639 if (cp_parser_parsing_tentatively (parser
)
8640 && !cp_parser_committed_to_tentative_parse (parser
))
8642 cp_parser_simulate_error (parser
);
8643 token
= cp_lexer_peek_token (parser
->lexer
);
8644 token
= cp_lexer_prev_token (parser
->lexer
, token
);
8645 start
= cp_lexer_token_difference (parser
->lexer
,
8646 parser
->lexer
->buffer
,
8651 /* Parse the template arguments so that we can issue error
8652 messages about them. */
8653 cp_lexer_consume_token (parser
->lexer
);
8654 cp_parser_enclosed_template_argument_list (parser
);
8655 /* Skip tokens until we find a good place from which to
8656 continue parsing. */
8657 cp_parser_skip_to_closing_parenthesis (parser
,
8658 /*recovering=*/true,
8660 /*consume_paren=*/false);
8661 /* If parsing tentatively, permanently remove the
8662 template argument list. That will prevent duplicate
8663 error messages from being issued about the missing
8664 "template" keyword. */
8667 token
= cp_lexer_advance_token (parser
->lexer
,
8668 parser
->lexer
->buffer
,
8670 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8673 *is_identifier
= true;
8677 /* If the "template" keyword is present, then there is generally
8678 no point in doing name-lookup, so we just return IDENTIFIER.
8679 But, if the qualifying scope is non-dependent then we can
8680 (and must) do name-lookup normally. */
8681 if (template_keyword_p
8683 || (TYPE_P (parser
->scope
)
8684 && dependent_type_p (parser
->scope
))))
8688 /* Look up the name. */
8689 decl
= cp_parser_lookup_name (parser
, identifier
,
8691 /*is_template=*/false,
8692 /*is_namespace=*/false,
8694 /*ambiguous_p=*/NULL
);
8695 decl
= maybe_get_template_decl_from_type_decl (decl
);
8697 /* If DECL is a template, then the name was a template-name. */
8698 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8702 /* The standard does not explicitly indicate whether a name that
8703 names a set of overloaded declarations, some of which are
8704 templates, is a template-name. However, such a name should
8705 be a template-name; otherwise, there is no way to form a
8706 template-id for the overloaded templates. */
8707 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8708 if (TREE_CODE (fns
) == OVERLOAD
)
8712 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8713 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8718 /* Otherwise, the name does not name a template. */
8719 cp_parser_error (parser
, "expected template-name");
8720 return error_mark_node
;
8724 /* If DECL is dependent, and refers to a function, then just return
8725 its name; we will look it up again during template instantiation. */
8726 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8728 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8729 if (TYPE_P (scope
) && dependent_type_p (scope
))
8736 /* Parse a template-argument-list.
8738 template-argument-list:
8740 template-argument-list , template-argument
8742 Returns a TREE_VEC containing the arguments. */
8745 cp_parser_template_argument_list (cp_parser
* parser
)
8747 tree fixed_args
[10];
8748 unsigned n_args
= 0;
8749 unsigned alloced
= 10;
8750 tree
*arg_ary
= fixed_args
;
8752 bool saved_in_template_argument_list_p
;
8754 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8755 parser
->in_template_argument_list_p
= true;
8761 /* Consume the comma. */
8762 cp_lexer_consume_token (parser
->lexer
);
8764 /* Parse the template-argument. */
8765 argument
= cp_parser_template_argument (parser
);
8766 if (n_args
== alloced
)
8770 if (arg_ary
== fixed_args
)
8772 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8773 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8776 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8778 arg_ary
[n_args
++] = argument
;
8780 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8782 vec
= make_tree_vec (n_args
);
8785 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8787 if (arg_ary
!= fixed_args
)
8789 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8793 /* Parse a template-argument.
8796 assignment-expression
8800 The representation is that of an assignment-expression, type-id, or
8801 id-expression -- except that the qualified id-expression is
8802 evaluated, so that the value returned is either a DECL or an
8805 Although the standard says "assignment-expression", it forbids
8806 throw-expressions or assignments in the template argument.
8807 Therefore, we use "conditional-expression" instead. */
8810 cp_parser_template_argument (cp_parser
* parser
)
8815 bool maybe_type_id
= false;
8818 tree qualifying_class
;
8820 /* There's really no way to know what we're looking at, so we just
8821 try each alternative in order.
8825 In a template-argument, an ambiguity between a type-id and an
8826 expression is resolved to a type-id, regardless of the form of
8827 the corresponding template-parameter.
8829 Therefore, we try a type-id first. */
8830 cp_parser_parse_tentatively (parser
);
8831 argument
= cp_parser_type_id (parser
);
8832 /* If there was no error parsing the type-id but the next token is a '>>',
8833 we probably found a typo for '> >'. But there are type-id which are
8834 also valid expressions. For instance:
8836 struct X { int operator >> (int); };
8837 template <int V> struct Foo {};
8840 Here 'X()' is a valid type-id of a function type, but the user just
8841 wanted to write the expression "X() >> 5". Thus, we remember that we
8842 found a valid type-id, but we still try to parse the argument as an
8843 expression to see what happens. */
8844 if (!cp_parser_error_occurred (parser
)
8845 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8847 maybe_type_id
= true;
8848 cp_parser_abort_tentative_parse (parser
);
8852 /* If the next token isn't a `,' or a `>', then this argument wasn't
8853 really finished. This means that the argument is not a valid
8855 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8856 cp_parser_error (parser
, "expected template-argument");
8857 /* If that worked, we're done. */
8858 if (cp_parser_parse_definitely (parser
))
8861 /* We're still not sure what the argument will be. */
8862 cp_parser_parse_tentatively (parser
);
8863 /* Try a template. */
8864 argument
= cp_parser_id_expression (parser
,
8865 /*template_keyword_p=*/false,
8866 /*check_dependency_p=*/true,
8868 /*declarator_p=*/false);
8869 /* If the next token isn't a `,' or a `>', then this argument wasn't
8871 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8872 cp_parser_error (parser
, "expected template-argument");
8873 if (!cp_parser_error_occurred (parser
))
8875 /* Figure out what is being referred to. If the id-expression
8876 was for a class template specialization, then we will have a
8877 TYPE_DECL at this point. There is no need to do name lookup
8878 at this point in that case. */
8879 if (TREE_CODE (argument
) != TYPE_DECL
)
8880 argument
= cp_parser_lookup_name (parser
, argument
,
8882 /*is_template=*/template_p
,
8883 /*is_namespace=*/false,
8884 /*check_dependency=*/true,
8885 /*ambiguous_p=*/NULL
);
8886 if (TREE_CODE (argument
) != TEMPLATE_DECL
8887 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8888 cp_parser_error (parser
, "expected template-name");
8890 if (cp_parser_parse_definitely (parser
))
8892 /* It must be a non-type argument. There permitted cases are given
8893 in [temp.arg.nontype]:
8895 -- an integral constant-expression of integral or enumeration
8898 -- the name of a non-type template-parameter; or
8900 -- the name of an object or function with external linkage...
8902 -- the address of an object or function with external linkage...
8904 -- a pointer to member... */
8905 /* Look for a non-type template parameter. */
8906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8908 cp_parser_parse_tentatively (parser
);
8909 argument
= cp_parser_primary_expression (parser
,
8912 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8913 || !cp_parser_next_token_ends_template_argument_p (parser
))
8914 cp_parser_simulate_error (parser
);
8915 if (cp_parser_parse_definitely (parser
))
8918 /* If the next token is "&", the argument must be the address of an
8919 object or function with external linkage. */
8920 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8922 cp_lexer_consume_token (parser
->lexer
);
8923 /* See if we might have an id-expression. */
8924 token
= cp_lexer_peek_token (parser
->lexer
);
8925 if (token
->type
== CPP_NAME
8926 || token
->keyword
== RID_OPERATOR
8927 || token
->type
== CPP_SCOPE
8928 || token
->type
== CPP_TEMPLATE_ID
8929 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8931 cp_parser_parse_tentatively (parser
);
8932 argument
= cp_parser_primary_expression (parser
,
8935 if (cp_parser_error_occurred (parser
)
8936 || !cp_parser_next_token_ends_template_argument_p (parser
))
8937 cp_parser_abort_tentative_parse (parser
);
8940 if (qualifying_class
)
8941 argument
= finish_qualified_id_expr (qualifying_class
,
8945 if (TREE_CODE (argument
) == VAR_DECL
)
8947 /* A variable without external linkage might still be a
8948 valid constant-expression, so no error is issued here
8949 if the external-linkage check fails. */
8950 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8951 cp_parser_simulate_error (parser
);
8953 else if (is_overloaded_fn (argument
))
8954 /* All overloaded functions are allowed; if the external
8955 linkage test does not pass, an error will be issued
8959 && (TREE_CODE (argument
) == OFFSET_REF
8960 || TREE_CODE (argument
) == SCOPE_REF
))
8961 /* A pointer-to-member. */
8964 cp_parser_simulate_error (parser
);
8966 if (cp_parser_parse_definitely (parser
))
8969 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8974 /* If the argument started with "&", there are no other valid
8975 alternatives at this point. */
8978 cp_parser_error (parser
, "invalid non-type template argument");
8979 return error_mark_node
;
8981 /* If the argument wasn't successfully parsed as a type-id followed
8982 by '>>', the argument can only be a constant expression now.
8983 Otherwise, we try parsing the constant-expression tentatively,
8984 because the argument could really be a type-id. */
8986 cp_parser_parse_tentatively (parser
);
8987 argument
= cp_parser_constant_expression (parser
,
8988 /*allow_non_constant_p=*/false,
8989 /*non_constant_p=*/NULL
);
8990 argument
= fold_non_dependent_expr (argument
);
8993 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8994 cp_parser_error (parser
, "expected template-argument");
8995 if (cp_parser_parse_definitely (parser
))
8997 /* We did our best to parse the argument as a non type-id, but that
8998 was the only alternative that matched (albeit with a '>' after
8999 it). We can assume it's just a typo from the user, and a
9000 diagnostic will then be issued. */
9001 return cp_parser_type_id (parser
);
9004 /* Parse an explicit-instantiation.
9006 explicit-instantiation:
9007 template declaration
9009 Although the standard says `declaration', what it really means is:
9011 explicit-instantiation:
9012 template decl-specifier-seq [opt] declarator [opt] ;
9014 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9015 supposed to be allowed. A defect report has been filed about this
9020 explicit-instantiation:
9021 storage-class-specifier template
9022 decl-specifier-seq [opt] declarator [opt] ;
9023 function-specifier template
9024 decl-specifier-seq [opt] declarator [opt] ; */
9027 cp_parser_explicit_instantiation (cp_parser
* parser
)
9029 int declares_class_or_enum
;
9030 cp_decl_specifier_seq decl_specifiers
;
9031 tree extension_specifier
= NULL_TREE
;
9033 /* Look for an (optional) storage-class-specifier or
9034 function-specifier. */
9035 if (cp_parser_allow_gnu_extensions_p (parser
))
9038 = cp_parser_storage_class_specifier_opt (parser
);
9039 if (!extension_specifier
)
9041 = cp_parser_function_specifier_opt (parser
,
9042 /*decl_specs=*/NULL
);
9045 /* Look for the `template' keyword. */
9046 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9047 /* Let the front end know that we are processing an explicit
9049 begin_explicit_instantiation ();
9050 /* [temp.explicit] says that we are supposed to ignore access
9051 control while processing explicit instantiation directives. */
9052 push_deferring_access_checks (dk_no_check
);
9053 /* Parse a decl-specifier-seq. */
9054 cp_parser_decl_specifier_seq (parser
,
9055 CP_PARSER_FLAGS_OPTIONAL
,
9057 &declares_class_or_enum
);
9058 /* If there was exactly one decl-specifier, and it declared a class,
9059 and there's no declarator, then we have an explicit type
9061 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9065 type
= check_tag_decl (&decl_specifiers
);
9066 /* Turn access control back on for names used during
9067 template instantiation. */
9068 pop_deferring_access_checks ();
9070 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
9074 cp_declarator
*declarator
;
9077 /* Parse the declarator. */
9079 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9080 /*ctor_dtor_or_conv_p=*/NULL
,
9081 /*parenthesized_p=*/NULL
,
9082 /*member_p=*/false);
9083 cp_parser_check_for_definition_in_return_type (declarator
,
9084 declares_class_or_enum
);
9085 if (declarator
!= cp_error_declarator
)
9087 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9089 /* Turn access control back on for names used during
9090 template instantiation. */
9091 pop_deferring_access_checks ();
9092 /* Do the explicit instantiation. */
9093 do_decl_instantiation (decl
, extension_specifier
);
9097 pop_deferring_access_checks ();
9098 /* Skip the body of the explicit instantiation. */
9099 cp_parser_skip_to_end_of_statement (parser
);
9102 /* We're done with the instantiation. */
9103 end_explicit_instantiation ();
9105 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9108 /* Parse an explicit-specialization.
9110 explicit-specialization:
9111 template < > declaration
9113 Although the standard says `declaration', what it really means is:
9115 explicit-specialization:
9116 template <> decl-specifier [opt] init-declarator [opt] ;
9117 template <> function-definition
9118 template <> explicit-specialization
9119 template <> template-declaration */
9122 cp_parser_explicit_specialization (cp_parser
* parser
)
9124 /* Look for the `template' keyword. */
9125 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9126 /* Look for the `<'. */
9127 cp_parser_require (parser
, CPP_LESS
, "`<'");
9128 /* Look for the `>'. */
9129 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9130 /* We have processed another parameter list. */
9131 ++parser
->num_template_parameter_lists
;
9132 /* Let the front end know that we are beginning a specialization. */
9133 begin_specialization ();
9135 /* If the next keyword is `template', we need to figure out whether
9136 or not we're looking a template-declaration. */
9137 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9139 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9140 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9141 cp_parser_template_declaration_after_export (parser
,
9142 /*member_p=*/false);
9144 cp_parser_explicit_specialization (parser
);
9147 /* Parse the dependent declaration. */
9148 cp_parser_single_declaration (parser
,
9152 /* We're done with the specialization. */
9153 end_specialization ();
9154 /* We're done with this parameter list. */
9155 --parser
->num_template_parameter_lists
;
9158 /* Parse a type-specifier.
9161 simple-type-specifier
9164 elaborated-type-specifier
9172 Returns a representation of the type-specifier. For a
9173 class-specifier, enum-specifier, or elaborated-type-specifier, a
9174 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9176 The parser flags FLAGS is used to control type-specifier parsing.
9178 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9179 in a decl-specifier-seq.
9181 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9182 class-specifier, enum-specifier, or elaborated-type-specifier, then
9183 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9184 if a type is declared; 2 if it is defined. Otherwise, it is set to
9187 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9188 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9192 cp_parser_type_specifier (cp_parser
* parser
,
9193 cp_parser_flags flags
,
9194 cp_decl_specifier_seq
*decl_specs
,
9195 bool is_declaration
,
9196 int* declares_class_or_enum
,
9197 bool* is_cv_qualifier
)
9199 tree type_spec
= NULL_TREE
;
9202 cp_decl_spec ds
= ds_last
;
9204 /* Assume this type-specifier does not declare a new type. */
9205 if (declares_class_or_enum
)
9206 *declares_class_or_enum
= 0;
9207 /* And that it does not specify a cv-qualifier. */
9208 if (is_cv_qualifier
)
9209 *is_cv_qualifier
= false;
9210 /* Peek at the next token. */
9211 token
= cp_lexer_peek_token (parser
->lexer
);
9213 /* If we're looking at a keyword, we can use that to guide the
9214 production we choose. */
9215 keyword
= token
->keyword
;
9219 /* 'enum' [identifier] '{' introduces an enum-specifier;
9220 'enum' <anything else> introduces an elaborated-type-specifier. */
9221 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_BRACE
9222 || (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
9223 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9226 type_spec
= cp_parser_enum_specifier (parser
);
9227 if (declares_class_or_enum
)
9228 *declares_class_or_enum
= 2;
9230 cp_parser_set_decl_spec_type (decl_specs
,
9232 /*user_defined_p=*/true);
9236 goto elaborated_type_specifier
;
9238 /* Any of these indicate either a class-specifier, or an
9239 elaborated-type-specifier. */
9243 /* Parse tentatively so that we can back up if we don't find a
9245 cp_parser_parse_tentatively (parser
);
9246 /* Look for the class-specifier. */
9247 type_spec
= cp_parser_class_specifier (parser
);
9248 /* If that worked, we're done. */
9249 if (cp_parser_parse_definitely (parser
))
9251 if (declares_class_or_enum
)
9252 *declares_class_or_enum
= 2;
9254 cp_parser_set_decl_spec_type (decl_specs
,
9256 /*user_defined_p=*/true);
9261 elaborated_type_specifier
:
9262 /* We're declaring (not defining) a class or enum. */
9263 if (declares_class_or_enum
)
9264 *declares_class_or_enum
= 1;
9268 /* Look for an elaborated-type-specifier. */
9270 = (cp_parser_elaborated_type_specifier
9272 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9275 cp_parser_set_decl_spec_type (decl_specs
,
9277 /*user_defined_p=*/true);
9282 if (is_cv_qualifier
)
9283 *is_cv_qualifier
= true;
9288 if (is_cv_qualifier
)
9289 *is_cv_qualifier
= true;
9294 if (is_cv_qualifier
)
9295 *is_cv_qualifier
= true;
9299 /* The `__complex__' keyword is a GNU extension. */
9307 /* Handle simple keywords. */
9312 ++decl_specs
->specs
[(int)ds
];
9313 decl_specs
->any_specifiers_p
= true;
9315 return cp_lexer_consume_token (parser
->lexer
)->value
;
9318 /* If we do not already have a type-specifier, assume we are looking
9319 at a simple-type-specifier. */
9320 type_spec
= cp_parser_simple_type_specifier (parser
,
9324 /* If we didn't find a type-specifier, and a type-specifier was not
9325 optional in this context, issue an error message. */
9326 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9328 cp_parser_error (parser
, "expected type specifier");
9329 return error_mark_node
;
9335 /* Parse a simple-type-specifier.
9337 simple-type-specifier:
9338 :: [opt] nested-name-specifier [opt] type-name
9339 :: [opt] nested-name-specifier template template-id
9354 simple-type-specifier:
9355 __typeof__ unary-expression
9356 __typeof__ ( type-id )
9358 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9359 appropriately updated. */
9362 cp_parser_simple_type_specifier (cp_parser
* parser
,
9363 cp_decl_specifier_seq
*decl_specs
,
9364 cp_parser_flags flags
)
9366 tree type
= NULL_TREE
;
9369 /* Peek at the next token. */
9370 token
= cp_lexer_peek_token (parser
->lexer
);
9372 /* If we're looking at a keyword, things are easy. */
9373 switch (token
->keyword
)
9377 decl_specs
->explicit_char_p
= true;
9378 type
= char_type_node
;
9381 type
= wchar_type_node
;
9384 type
= boolean_type_node
;
9388 ++decl_specs
->specs
[(int) ds_short
];
9389 type
= short_integer_type_node
;
9393 decl_specs
->explicit_int_p
= true;
9394 type
= integer_type_node
;
9398 ++decl_specs
->specs
[(int) ds_long
];
9399 type
= long_integer_type_node
;
9403 ++decl_specs
->specs
[(int) ds_signed
];
9404 type
= integer_type_node
;
9408 ++decl_specs
->specs
[(int) ds_unsigned
];
9409 type
= unsigned_type_node
;
9412 type
= float_type_node
;
9415 type
= double_type_node
;
9418 type
= void_type_node
;
9422 /* Consume the `typeof' token. */
9423 cp_lexer_consume_token (parser
->lexer
);
9424 /* Parse the operand to `typeof'. */
9425 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9426 /* If it is not already a TYPE, take its type. */
9428 type
= finish_typeof (type
);
9431 cp_parser_set_decl_spec_type (decl_specs
, type
,
9432 /*user_defined_p=*/true);
9440 /* If the type-specifier was for a built-in type, we're done. */
9445 /* Record the type. */
9447 && (token
->keyword
!= RID_SIGNED
9448 && token
->keyword
!= RID_UNSIGNED
9449 && token
->keyword
!= RID_SHORT
9450 && token
->keyword
!= RID_LONG
))
9451 cp_parser_set_decl_spec_type (decl_specs
,
9453 /*user_defined=*/false);
9455 decl_specs
->any_specifiers_p
= true;
9457 /* Consume the token. */
9458 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9460 /* There is no valid C++ program where a non-template type is
9461 followed by a "<". That usually indicates that the user thought
9462 that the type was a template. */
9463 cp_parser_check_for_invalid_template_id (parser
, type
);
9465 return TYPE_NAME (type
);
9468 /* The type-specifier must be a user-defined type. */
9469 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9474 /* Don't gobble tokens or issue error messages if this is an
9475 optional type-specifier. */
9476 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9477 cp_parser_parse_tentatively (parser
);
9479 /* Look for the optional `::' operator. */
9481 = (cp_parser_global_scope_opt (parser
,
9482 /*current_scope_valid_p=*/false)
9484 /* Look for the nested-name specifier. */
9486 = (cp_parser_nested_name_specifier_opt (parser
,
9487 /*typename_keyword_p=*/false,
9488 /*check_dependency_p=*/true,
9490 /*is_declaration=*/false)
9492 /* If we have seen a nested-name-specifier, and the next token
9493 is `template', then we are using the template-id production. */
9495 && cp_parser_optional_template_keyword (parser
))
9497 /* Look for the template-id. */
9498 type
= cp_parser_template_id (parser
,
9499 /*template_keyword_p=*/true,
9500 /*check_dependency_p=*/true,
9501 /*is_declaration=*/false);
9502 /* If the template-id did not name a type, we are out of
9504 if (TREE_CODE (type
) != TYPE_DECL
)
9506 cp_parser_error (parser
, "expected template-id for type");
9510 /* Otherwise, look for a type-name. */
9512 type
= cp_parser_type_name (parser
);
9513 /* Keep track of all name-lookups performed in class scopes. */
9517 && TREE_CODE (type
) == TYPE_DECL
9518 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9519 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9520 /* If it didn't work out, we don't have a TYPE. */
9521 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9522 && !cp_parser_parse_definitely (parser
))
9524 if (type
&& decl_specs
)
9525 cp_parser_set_decl_spec_type (decl_specs
, type
,
9526 /*user_defined=*/true);
9529 /* If we didn't get a type-name, issue an error message. */
9530 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9532 cp_parser_error (parser
, "expected type-name");
9533 return error_mark_node
;
9536 /* There is no valid C++ program where a non-template type is
9537 followed by a "<". That usually indicates that the user thought
9538 that the type was a template. */
9539 if (type
&& type
!= error_mark_node
)
9540 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9545 /* Parse a type-name.
9558 Returns a TYPE_DECL for the the type. */
9561 cp_parser_type_name (cp_parser
* parser
)
9566 /* We can't know yet whether it is a class-name or not. */
9567 cp_parser_parse_tentatively (parser
);
9568 /* Try a class-name. */
9569 type_decl
= cp_parser_class_name (parser
,
9570 /*typename_keyword_p=*/false,
9571 /*template_keyword_p=*/false,
9573 /*check_dependency_p=*/true,
9574 /*class_head_p=*/false,
9575 /*is_declaration=*/false);
9576 /* If it's not a class-name, keep looking. */
9577 if (!cp_parser_parse_definitely (parser
))
9579 /* It must be a typedef-name or an enum-name. */
9580 identifier
= cp_parser_identifier (parser
);
9581 if (identifier
== error_mark_node
)
9582 return error_mark_node
;
9584 /* Look up the type-name. */
9585 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9586 /* Issue an error if we did not find a type-name. */
9587 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9589 if (!cp_parser_simulate_error (parser
))
9590 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9592 type_decl
= error_mark_node
;
9594 /* Remember that the name was used in the definition of the
9595 current class so that we can check later to see if the
9596 meaning would have been different after the class was
9597 entirely defined. */
9598 else if (type_decl
!= error_mark_node
9600 maybe_note_name_used_in_class (identifier
, type_decl
);
9607 /* Parse an elaborated-type-specifier. Note that the grammar given
9608 here incorporates the resolution to DR68.
9610 elaborated-type-specifier:
9611 class-key :: [opt] nested-name-specifier [opt] identifier
9612 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9613 enum :: [opt] nested-name-specifier [opt] identifier
9614 typename :: [opt] nested-name-specifier identifier
9615 typename :: [opt] nested-name-specifier template [opt]
9620 elaborated-type-specifier:
9621 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9622 class-key attributes :: [opt] nested-name-specifier [opt]
9623 template [opt] template-id
9624 enum attributes :: [opt] nested-name-specifier [opt] identifier
9626 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9627 declared `friend'. If IS_DECLARATION is TRUE, then this
9628 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9629 something is being declared.
9631 Returns the TYPE specified. */
9634 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
9636 bool is_declaration
)
9638 enum tag_types tag_type
;
9640 tree type
= NULL_TREE
;
9641 tree attributes
= NULL_TREE
;
9643 /* See if we're looking at the `enum' keyword. */
9644 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
9646 /* Consume the `enum' token. */
9647 cp_lexer_consume_token (parser
->lexer
);
9648 /* Remember that it's an enumeration type. */
9649 tag_type
= enum_type
;
9650 /* Parse the attributes. */
9651 attributes
= cp_parser_attributes_opt (parser
);
9653 /* Or, it might be `typename'. */
9654 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9657 /* Consume the `typename' token. */
9658 cp_lexer_consume_token (parser
->lexer
);
9659 /* Remember that it's a `typename' type. */
9660 tag_type
= typename_type
;
9661 /* The `typename' keyword is only allowed in templates. */
9662 if (!processing_template_decl
)
9663 pedwarn ("using %<typename%> outside of template");
9665 /* Otherwise it must be a class-key. */
9668 tag_type
= cp_parser_class_key (parser
);
9669 if (tag_type
== none_type
)
9670 return error_mark_node
;
9671 /* Parse the attributes. */
9672 attributes
= cp_parser_attributes_opt (parser
);
9675 /* Look for the `::' operator. */
9676 cp_parser_global_scope_opt (parser
,
9677 /*current_scope_valid_p=*/false);
9678 /* Look for the nested-name-specifier. */
9679 if (tag_type
== typename_type
)
9681 if (cp_parser_nested_name_specifier (parser
,
9682 /*typename_keyword_p=*/true,
9683 /*check_dependency_p=*/true,
9687 return error_mark_node
;
9690 /* Even though `typename' is not present, the proposed resolution
9691 to Core Issue 180 says that in `class A<T>::B', `B' should be
9692 considered a type-name, even if `A<T>' is dependent. */
9693 cp_parser_nested_name_specifier_opt (parser
,
9694 /*typename_keyword_p=*/true,
9695 /*check_dependency_p=*/true,
9698 /* For everything but enumeration types, consider a template-id. */
9699 if (tag_type
!= enum_type
)
9701 bool template_p
= false;
9704 /* Allow the `template' keyword. */
9705 template_p
= cp_parser_optional_template_keyword (parser
);
9706 /* If we didn't see `template', we don't know if there's a
9707 template-id or not. */
9709 cp_parser_parse_tentatively (parser
);
9710 /* Parse the template-id. */
9711 decl
= cp_parser_template_id (parser
, template_p
,
9712 /*check_dependency_p=*/true,
9714 /* If we didn't find a template-id, look for an ordinary
9716 if (!template_p
&& !cp_parser_parse_definitely (parser
))
9718 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9719 in effect, then we must assume that, upon instantiation, the
9720 template will correspond to a class. */
9721 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
9722 && tag_type
== typename_type
)
9723 type
= make_typename_type (parser
->scope
, decl
,
9726 type
= TREE_TYPE (decl
);
9729 /* For an enumeration type, consider only a plain identifier. */
9732 identifier
= cp_parser_identifier (parser
);
9734 if (identifier
== error_mark_node
)
9736 parser
->scope
= NULL_TREE
;
9737 return error_mark_node
;
9740 /* For a `typename', we needn't call xref_tag. */
9741 if (tag_type
== typename_type
)
9742 return cp_parser_make_typename_type (parser
, parser
->scope
,
9744 /* Look up a qualified name in the usual way. */
9749 /* In an elaborated-type-specifier, names are assumed to name
9750 types, so we set IS_TYPE to TRUE when calling
9751 cp_parser_lookup_name. */
9752 decl
= cp_parser_lookup_name (parser
, identifier
,
9754 /*is_template=*/false,
9755 /*is_namespace=*/false,
9756 /*check_dependency=*/true,
9757 /*ambiguous_p=*/NULL
);
9759 /* If we are parsing friend declaration, DECL may be a
9760 TEMPLATE_DECL tree node here. However, we need to check
9761 whether this TEMPLATE_DECL results in valid code. Consider
9762 the following example:
9765 template <class T> class C {};
9768 template <class T> friend class N::C; // #1, valid code
9770 template <class T> class Y {
9771 friend class N::C; // #2, invalid code
9774 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9775 name lookup of `N::C'. We see that friend declaration must
9776 be template for the code to be valid. Note that
9777 processing_template_decl does not work here since it is
9778 always 1 for the above two cases. */
9780 decl
= (cp_parser_maybe_treat_template_as_class
9781 (decl
, /*tag_name_p=*/is_friend
9782 && parser
->num_template_parameter_lists
));
9784 if (TREE_CODE (decl
) != TYPE_DECL
)
9786 error ("expected type-name");
9787 return error_mark_node
;
9790 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9791 check_elaborated_type_specifier
9793 (parser
->num_template_parameter_lists
9794 || DECL_SELF_REFERENCE_P (decl
)));
9796 type
= TREE_TYPE (decl
);
9800 /* An elaborated-type-specifier sometimes introduces a new type and
9801 sometimes names an existing type. Normally, the rule is that it
9802 introduces a new type only if there is not an existing type of
9803 the same name already in scope. For example, given:
9806 void f() { struct S s; }
9808 the `struct S' in the body of `f' is the same `struct S' as in
9809 the global scope; the existing definition is used. However, if
9810 there were no global declaration, this would introduce a new
9811 local class named `S'.
9813 An exception to this rule applies to the following code:
9815 namespace N { struct S; }
9817 Here, the elaborated-type-specifier names a new type
9818 unconditionally; even if there is already an `S' in the
9819 containing scope this declaration names a new type.
9820 This exception only applies if the elaborated-type-specifier
9821 forms the complete declaration:
9825 A declaration consisting solely of `class-key identifier ;' is
9826 either a redeclaration of the name in the current scope or a
9827 forward declaration of the identifier as a class name. It
9828 introduces the name into the current scope.
9830 We are in this situation precisely when the next token is a `;'.
9832 An exception to the exception is that a `friend' declaration does
9833 *not* name a new type; i.e., given:
9835 struct S { friend struct T; };
9837 `T' is not a new type in the scope of `S'.
9839 Also, `new struct S' or `sizeof (struct S)' never results in the
9840 definition of a new type; a new type can only be declared in a
9841 declaration context. */
9843 /* Warn about attributes. They are ignored. */
9845 warning ("type attributes are honored only at type definition");
9847 type
= xref_tag (tag_type
, identifier
,
9850 || cp_lexer_next_token_is_not (parser
->lexer
,
9852 parser
->num_template_parameter_lists
);
9855 if (tag_type
!= enum_type
)
9856 cp_parser_check_class_key (tag_type
, type
);
9858 /* A "<" cannot follow an elaborated type specifier. If that
9859 happens, the user was probably trying to form a template-id. */
9860 cp_parser_check_for_invalid_template_id (parser
, type
);
9865 /* Parse an enum-specifier.
9868 enum identifier [opt] { enumerator-list [opt] }
9870 Returns an ENUM_TYPE representing the enumeration. */
9873 cp_parser_enum_specifier (cp_parser
* parser
)
9878 /* Caller guarantees that the current token is 'enum', an identifier
9879 possibly follows, and the token after that is an opening brace.
9880 If we don't have an identifier, fabricate an anonymous name for
9881 the enumeration being defined. */
9882 cp_lexer_consume_token (parser
->lexer
);
9884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9885 identifier
= cp_parser_identifier (parser
);
9887 identifier
= make_anon_name ();
9889 /* Issue an error message if type-definitions are forbidden here. */
9890 cp_parser_check_type_definition (parser
);
9892 /* Create the new type. We do this before consuming the opening brace
9893 so the enum will be recorded as being on the line of its tag (or the
9894 'enum' keyword, if there is no tag). */
9895 type
= start_enum (identifier
);
9897 /* Consume the opening brace. */
9898 cp_lexer_consume_token (parser
->lexer
);
9900 /* If the next token is not '}', then there are some enumerators. */
9901 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
9902 cp_parser_enumerator_list (parser
, type
);
9904 /* Consume the final '}'. */
9905 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9907 /* Finish up the enumeration. */
9913 /* Parse an enumerator-list. The enumerators all have the indicated
9917 enumerator-definition
9918 enumerator-list , enumerator-definition */
9921 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9925 /* Parse an enumerator-definition. */
9926 cp_parser_enumerator_definition (parser
, type
);
9928 /* If the next token is not a ',', we've reached the end of
9930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9932 /* Otherwise, consume the `,' and keep going. */
9933 cp_lexer_consume_token (parser
->lexer
);
9934 /* If the next token is a `}', there is a trailing comma. */
9935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9937 if (pedantic
&& !in_system_header
)
9938 pedwarn ("comma at end of enumerator list");
9944 /* Parse an enumerator-definition. The enumerator has the indicated
9947 enumerator-definition:
9949 enumerator = constant-expression
9955 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9960 /* Look for the identifier. */
9961 identifier
= cp_parser_identifier (parser
);
9962 if (identifier
== error_mark_node
)
9965 /* If the next token is an '=', then there is an explicit value. */
9966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9968 /* Consume the `=' token. */
9969 cp_lexer_consume_token (parser
->lexer
);
9970 /* Parse the value. */
9971 value
= cp_parser_constant_expression (parser
,
9972 /*allow_non_constant_p=*/false,
9978 /* Create the enumerator. */
9979 build_enumerator (identifier
, value
, type
);
9982 /* Parse a namespace-name.
9985 original-namespace-name
9988 Returns the NAMESPACE_DECL for the namespace. */
9991 cp_parser_namespace_name (cp_parser
* parser
)
9994 tree namespace_decl
;
9996 /* Get the name of the namespace. */
9997 identifier
= cp_parser_identifier (parser
);
9998 if (identifier
== error_mark_node
)
9999 return error_mark_node
;
10001 /* Look up the identifier in the currently active scope. Look only
10002 for namespaces, due to:
10004 [basic.lookup.udir]
10006 When looking up a namespace-name in a using-directive or alias
10007 definition, only namespace names are considered.
10011 [basic.lookup.qual]
10013 During the lookup of a name preceding the :: scope resolution
10014 operator, object, function, and enumerator names are ignored.
10016 (Note that cp_parser_class_or_namespace_name only calls this
10017 function if the token after the name is the scope resolution
10019 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10021 /*is_template=*/false,
10022 /*is_namespace=*/true,
10023 /*check_dependency=*/true,
10024 /*ambiguous_p=*/NULL
);
10025 /* If it's not a namespace, issue an error. */
10026 if (namespace_decl
== error_mark_node
10027 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10029 cp_parser_error (parser
, "expected namespace-name");
10030 namespace_decl
= error_mark_node
;
10033 return namespace_decl
;
10036 /* Parse a namespace-definition.
10038 namespace-definition:
10039 named-namespace-definition
10040 unnamed-namespace-definition
10042 named-namespace-definition:
10043 original-namespace-definition
10044 extension-namespace-definition
10046 original-namespace-definition:
10047 namespace identifier { namespace-body }
10049 extension-namespace-definition:
10050 namespace original-namespace-name { namespace-body }
10052 unnamed-namespace-definition:
10053 namespace { namespace-body } */
10056 cp_parser_namespace_definition (cp_parser
* parser
)
10060 /* Look for the `namespace' keyword. */
10061 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10063 /* Get the name of the namespace. We do not attempt to distinguish
10064 between an original-namespace-definition and an
10065 extension-namespace-definition at this point. The semantic
10066 analysis routines are responsible for that. */
10067 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10068 identifier
= cp_parser_identifier (parser
);
10070 identifier
= NULL_TREE
;
10072 /* Look for the `{' to start the namespace. */
10073 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10074 /* Start the namespace. */
10075 push_namespace (identifier
);
10076 /* Parse the body of the namespace. */
10077 cp_parser_namespace_body (parser
);
10078 /* Finish the namespace. */
10080 /* Look for the final `}'. */
10081 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10084 /* Parse a namespace-body.
10087 declaration-seq [opt] */
10090 cp_parser_namespace_body (cp_parser
* parser
)
10092 cp_parser_declaration_seq_opt (parser
);
10095 /* Parse a namespace-alias-definition.
10097 namespace-alias-definition:
10098 namespace identifier = qualified-namespace-specifier ; */
10101 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10104 tree namespace_specifier
;
10106 /* Look for the `namespace' keyword. */
10107 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10108 /* Look for the identifier. */
10109 identifier
= cp_parser_identifier (parser
);
10110 if (identifier
== error_mark_node
)
10112 /* Look for the `=' token. */
10113 cp_parser_require (parser
, CPP_EQ
, "`='");
10114 /* Look for the qualified-namespace-specifier. */
10115 namespace_specifier
10116 = cp_parser_qualified_namespace_specifier (parser
);
10117 /* Look for the `;' token. */
10118 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10120 /* Register the alias in the symbol table. */
10121 do_namespace_alias (identifier
, namespace_specifier
);
10124 /* Parse a qualified-namespace-specifier.
10126 qualified-namespace-specifier:
10127 :: [opt] nested-name-specifier [opt] namespace-name
10129 Returns a NAMESPACE_DECL corresponding to the specified
10133 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10135 /* Look for the optional `::'. */
10136 cp_parser_global_scope_opt (parser
,
10137 /*current_scope_valid_p=*/false);
10139 /* Look for the optional nested-name-specifier. */
10140 cp_parser_nested_name_specifier_opt (parser
,
10141 /*typename_keyword_p=*/false,
10142 /*check_dependency_p=*/true,
10144 /*is_declaration=*/true);
10146 return cp_parser_namespace_name (parser
);
10149 /* Parse a using-declaration.
10152 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10153 using :: unqualified-id ; */
10156 cp_parser_using_declaration (cp_parser
* parser
)
10159 bool typename_p
= false;
10160 bool global_scope_p
;
10166 /* Look for the `using' keyword. */
10167 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10169 /* Peek at the next token. */
10170 token
= cp_lexer_peek_token (parser
->lexer
);
10171 /* See if it's `typename'. */
10172 if (token
->keyword
== RID_TYPENAME
)
10174 /* Remember that we've seen it. */
10176 /* Consume the `typename' token. */
10177 cp_lexer_consume_token (parser
->lexer
);
10180 /* Look for the optional global scope qualification. */
10182 = (cp_parser_global_scope_opt (parser
,
10183 /*current_scope_valid_p=*/false)
10186 /* If we saw `typename', or didn't see `::', then there must be a
10187 nested-name-specifier present. */
10188 if (typename_p
|| !global_scope_p
)
10189 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10190 /*check_dependency_p=*/true,
10192 /*is_declaration=*/true);
10193 /* Otherwise, we could be in either of the two productions. In that
10194 case, treat the nested-name-specifier as optional. */
10196 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10197 /*typename_keyword_p=*/false,
10198 /*check_dependency_p=*/true,
10200 /*is_declaration=*/true);
10202 qscope
= global_namespace
;
10204 /* Parse the unqualified-id. */
10205 identifier
= cp_parser_unqualified_id (parser
,
10206 /*template_keyword_p=*/false,
10207 /*check_dependency_p=*/true,
10208 /*declarator_p=*/true);
10210 /* The function we call to handle a using-declaration is different
10211 depending on what scope we are in. */
10212 if (identifier
== error_mark_node
)
10214 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10215 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10216 /* [namespace.udecl]
10218 A using declaration shall not name a template-id. */
10219 error ("a template-id may not appear in a using-declaration");
10222 scope
= current_scope ();
10223 if (scope
&& TYPE_P (scope
))
10225 /* Create the USING_DECL. */
10226 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
10229 /* Add it to the list of members in this class. */
10230 finish_member_declaration (decl
);
10234 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10235 if (decl
== error_mark_node
)
10236 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10238 do_local_using_decl (decl
, qscope
, identifier
);
10240 do_toplevel_using_decl (decl
, qscope
, identifier
);
10244 /* Look for the final `;'. */
10245 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10248 /* Parse a using-directive.
10251 using namespace :: [opt] nested-name-specifier [opt]
10252 namespace-name ; */
10255 cp_parser_using_directive (cp_parser
* parser
)
10257 tree namespace_decl
;
10260 /* Look for the `using' keyword. */
10261 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10262 /* And the `namespace' keyword. */
10263 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10264 /* Look for the optional `::' operator. */
10265 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10266 /* And the optional nested-name-specifier. */
10267 cp_parser_nested_name_specifier_opt (parser
,
10268 /*typename_keyword_p=*/false,
10269 /*check_dependency_p=*/true,
10271 /*is_declaration=*/true);
10272 /* Get the namespace being used. */
10273 namespace_decl
= cp_parser_namespace_name (parser
);
10274 /* And any specified attributes. */
10275 attribs
= cp_parser_attributes_opt (parser
);
10276 /* Update the symbol table. */
10277 parse_using_directive (namespace_decl
, attribs
);
10278 /* Look for the final `;'. */
10279 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10282 /* Parse an asm-definition.
10285 asm ( string-literal ) ;
10290 asm volatile [opt] ( string-literal ) ;
10291 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10292 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10293 : asm-operand-list [opt] ) ;
10294 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10295 : asm-operand-list [opt]
10296 : asm-operand-list [opt] ) ; */
10299 cp_parser_asm_definition (cp_parser
* parser
)
10302 tree outputs
= NULL_TREE
;
10303 tree inputs
= NULL_TREE
;
10304 tree clobbers
= NULL_TREE
;
10306 bool volatile_p
= false;
10307 bool extended_p
= false;
10309 /* Look for the `asm' keyword. */
10310 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10311 /* See if the next token is `volatile'. */
10312 if (cp_parser_allow_gnu_extensions_p (parser
)
10313 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10315 /* Remember that we saw the `volatile' keyword. */
10317 /* Consume the token. */
10318 cp_lexer_consume_token (parser
->lexer
);
10320 /* Look for the opening `('. */
10321 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10323 /* Look for the string. */
10324 string
= cp_parser_string_literal (parser
, false, false);
10325 if (string
== error_mark_node
)
10327 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10328 /*consume_paren=*/true);
10332 /* If we're allowing GNU extensions, check for the extended assembly
10333 syntax. Unfortunately, the `:' tokens need not be separated by
10334 a space in C, and so, for compatibility, we tolerate that here
10335 too. Doing that means that we have to treat the `::' operator as
10337 if (cp_parser_allow_gnu_extensions_p (parser
)
10338 && at_function_scope_p ()
10339 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10340 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10342 bool inputs_p
= false;
10343 bool clobbers_p
= false;
10345 /* The extended syntax was used. */
10348 /* Look for outputs. */
10349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10351 /* Consume the `:'. */
10352 cp_lexer_consume_token (parser
->lexer
);
10353 /* Parse the output-operands. */
10354 if (cp_lexer_next_token_is_not (parser
->lexer
,
10356 && cp_lexer_next_token_is_not (parser
->lexer
,
10358 && cp_lexer_next_token_is_not (parser
->lexer
,
10360 outputs
= cp_parser_asm_operand_list (parser
);
10362 /* If the next token is `::', there are no outputs, and the
10363 next token is the beginning of the inputs. */
10364 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10365 /* The inputs are coming next. */
10368 /* Look for inputs. */
10370 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10372 /* Consume the `:' or `::'. */
10373 cp_lexer_consume_token (parser
->lexer
);
10374 /* Parse the output-operands. */
10375 if (cp_lexer_next_token_is_not (parser
->lexer
,
10377 && cp_lexer_next_token_is_not (parser
->lexer
,
10379 inputs
= cp_parser_asm_operand_list (parser
);
10381 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10382 /* The clobbers are coming next. */
10385 /* Look for clobbers. */
10387 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10389 /* Consume the `:' or `::'. */
10390 cp_lexer_consume_token (parser
->lexer
);
10391 /* Parse the clobbers. */
10392 if (cp_lexer_next_token_is_not (parser
->lexer
,
10394 clobbers
= cp_parser_asm_clobber_list (parser
);
10397 /* Look for the closing `)'. */
10398 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10399 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10400 /*consume_paren=*/true);
10401 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10403 /* Create the ASM_EXPR. */
10404 if (at_function_scope_p ())
10406 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10408 /* If the extended syntax was not used, mark the ASM_EXPR. */
10411 tree temp
= asm_stmt
;
10412 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10413 temp
= TREE_OPERAND (temp
, 0);
10415 ASM_INPUT_P (temp
) = 1;
10419 assemble_asm (string
);
10422 /* Declarators [gram.dcl.decl] */
10424 /* Parse an init-declarator.
10427 declarator initializer [opt]
10432 declarator asm-specification [opt] attributes [opt] initializer [opt]
10434 function-definition:
10435 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10437 decl-specifier-seq [opt] declarator function-try-block
10441 function-definition:
10442 __extension__ function-definition
10444 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10445 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10446 then this declarator appears in a class scope. The new DECL created
10447 by this declarator is returned.
10449 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10450 for a function-definition here as well. If the declarator is a
10451 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10452 be TRUE upon return. By that point, the function-definition will
10453 have been completely parsed.
10455 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10459 cp_parser_init_declarator (cp_parser
* parser
,
10460 cp_decl_specifier_seq
*decl_specifiers
,
10461 bool function_definition_allowed_p
,
10463 int declares_class_or_enum
,
10464 bool* function_definition_p
)
10467 cp_declarator
*declarator
;
10468 tree prefix_attributes
;
10470 tree asm_specification
;
10472 tree decl
= NULL_TREE
;
10474 bool is_initialized
;
10475 bool is_parenthesized_init
;
10476 bool is_non_constant_init
;
10477 int ctor_dtor_or_conv_p
;
10479 bool pop_p
= false;
10481 /* Gather the attributes that were provided with the
10482 decl-specifiers. */
10483 prefix_attributes
= decl_specifiers
->attributes
;
10485 /* Assume that this is not the declarator for a function
10487 if (function_definition_p
)
10488 *function_definition_p
= false;
10490 /* Defer access checks while parsing the declarator; we cannot know
10491 what names are accessible until we know what is being
10493 resume_deferring_access_checks ();
10495 /* Parse the declarator. */
10497 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10498 &ctor_dtor_or_conv_p
,
10499 /*parenthesized_p=*/NULL
,
10500 /*member_p=*/false);
10501 /* Gather up the deferred checks. */
10502 stop_deferring_access_checks ();
10504 /* If the DECLARATOR was erroneous, there's no need to go
10506 if (declarator
== cp_error_declarator
)
10507 return error_mark_node
;
10509 cp_parser_check_for_definition_in_return_type (declarator
,
10510 declares_class_or_enum
);
10512 /* Figure out what scope the entity declared by the DECLARATOR is
10513 located in. `grokdeclarator' sometimes changes the scope, so
10514 we compute it now. */
10515 scope
= get_scope_of_declarator (declarator
);
10517 /* If we're allowing GNU extensions, look for an asm-specification
10519 if (cp_parser_allow_gnu_extensions_p (parser
))
10521 /* Look for an asm-specification. */
10522 asm_specification
= cp_parser_asm_specification_opt (parser
);
10523 /* And attributes. */
10524 attributes
= cp_parser_attributes_opt (parser
);
10528 asm_specification
= NULL_TREE
;
10529 attributes
= NULL_TREE
;
10532 /* Peek at the next token. */
10533 token
= cp_lexer_peek_token (parser
->lexer
);
10534 /* Check to see if the token indicates the start of a
10535 function-definition. */
10536 if (cp_parser_token_starts_function_definition_p (token
))
10538 if (!function_definition_allowed_p
)
10540 /* If a function-definition should not appear here, issue an
10542 cp_parser_error (parser
,
10543 "a function-definition is not allowed here");
10544 return error_mark_node
;
10548 /* Neither attributes nor an asm-specification are allowed
10549 on a function-definition. */
10550 if (asm_specification
)
10551 error ("an asm-specification is not allowed on a function-definition");
10553 error ("attributes are not allowed on a function-definition");
10554 /* This is a function-definition. */
10555 *function_definition_p
= true;
10557 /* Parse the function definition. */
10559 decl
= cp_parser_save_member_function_body (parser
,
10562 prefix_attributes
);
10565 = (cp_parser_function_definition_from_specifiers_and_declarator
10566 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
10574 Only in function declarations for constructors, destructors, and
10575 type conversions can the decl-specifier-seq be omitted.
10577 We explicitly postpone this check past the point where we handle
10578 function-definitions because we tolerate function-definitions
10579 that are missing their return types in some modes. */
10580 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
10582 cp_parser_error (parser
,
10583 "expected constructor, destructor, or type conversion");
10584 return error_mark_node
;
10587 /* An `=' or an `(' indicates an initializer. */
10588 is_initialized
= (token
->type
== CPP_EQ
10589 || token
->type
== CPP_OPEN_PAREN
);
10590 /* If the init-declarator isn't initialized and isn't followed by a
10591 `,' or `;', it's not a valid init-declarator. */
10592 if (!is_initialized
10593 && token
->type
!= CPP_COMMA
10594 && token
->type
!= CPP_SEMICOLON
)
10596 cp_parser_error (parser
, "expected initializer");
10597 return error_mark_node
;
10600 /* Because start_decl has side-effects, we should only call it if we
10601 know we're going ahead. By this point, we know that we cannot
10602 possibly be looking at any other construct. */
10603 cp_parser_commit_to_tentative_parse (parser
);
10605 /* If the decl specifiers were bad, issue an error now that we're
10606 sure this was intended to be a declarator. Then continue
10607 declaring the variable(s), as int, to try to cut down on further
10609 if (decl_specifiers
->any_specifiers_p
10610 && decl_specifiers
->type
== error_mark_node
)
10612 cp_parser_error (parser
, "invalid type in declaration");
10613 decl_specifiers
->type
= integer_type_node
;
10616 /* Check to see whether or not this declaration is a friend. */
10617 friend_p
= cp_parser_friend_p (decl_specifiers
);
10619 /* Check that the number of template-parameter-lists is OK. */
10620 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
10621 return error_mark_node
;
10623 /* Enter the newly declared entry in the symbol table. If we're
10624 processing a declaration in a class-specifier, we wait until
10625 after processing the initializer. */
10628 if (parser
->in_unbraced_linkage_specification_p
)
10630 decl_specifiers
->storage_class
= sc_extern
;
10631 have_extern_spec
= false;
10633 decl
= start_decl (declarator
, decl_specifiers
,
10634 is_initialized
, attributes
, prefix_attributes
,
10638 /* Enter the SCOPE. That way unqualified names appearing in the
10639 initializer will be looked up in SCOPE. */
10640 pop_p
= push_scope (scope
);
10642 /* Perform deferred access control checks, now that we know in which
10643 SCOPE the declared entity resides. */
10644 if (!member_p
&& decl
)
10646 tree saved_current_function_decl
= NULL_TREE
;
10648 /* If the entity being declared is a function, pretend that we
10649 are in its scope. If it is a `friend', it may have access to
10650 things that would not otherwise be accessible. */
10651 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10653 saved_current_function_decl
= current_function_decl
;
10654 current_function_decl
= decl
;
10657 /* Perform the access control checks for the declarator and the
10658 the decl-specifiers. */
10659 perform_deferred_access_checks ();
10661 /* Restore the saved value. */
10662 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10663 current_function_decl
= saved_current_function_decl
;
10666 /* Parse the initializer. */
10667 if (is_initialized
)
10668 initializer
= cp_parser_initializer (parser
,
10669 &is_parenthesized_init
,
10670 &is_non_constant_init
);
10673 initializer
= NULL_TREE
;
10674 is_parenthesized_init
= false;
10675 is_non_constant_init
= true;
10678 /* The old parser allows attributes to appear after a parenthesized
10679 initializer. Mark Mitchell proposed removing this functionality
10680 on the GCC mailing lists on 2002-08-13. This parser accepts the
10681 attributes -- but ignores them. */
10682 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
10683 if (cp_parser_attributes_opt (parser
))
10684 warning ("attributes after parenthesized initializer ignored");
10686 /* For an in-class declaration, use `grokfield' to create the
10692 decl
= grokfield (declarator
, decl_specifiers
,
10693 initializer
, /*asmspec=*/NULL_TREE
,
10694 /*attributes=*/NULL_TREE
);
10695 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
10696 cp_parser_save_default_args (parser
, decl
);
10699 /* Finish processing the declaration. But, skip friend
10701 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
10703 cp_finish_decl (decl
,
10706 /* If the initializer is in parentheses, then this is
10707 a direct-initialization, which means that an
10708 `explicit' constructor is OK. Otherwise, an
10709 `explicit' constructor cannot be used. */
10710 ((is_parenthesized_init
|| !is_initialized
)
10711 ? 0 : LOOKUP_ONLYCONVERTING
));
10713 pop_scope (DECL_CONTEXT (decl
));
10716 /* Remember whether or not variables were initialized by
10717 constant-expressions. */
10718 if (decl
&& TREE_CODE (decl
) == VAR_DECL
10719 && is_initialized
&& !is_non_constant_init
)
10720 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
10725 /* Parse a declarator.
10729 ptr-operator declarator
10731 abstract-declarator:
10732 ptr-operator abstract-declarator [opt]
10733 direct-abstract-declarator
10738 attributes [opt] direct-declarator
10739 attributes [opt] ptr-operator declarator
10741 abstract-declarator:
10742 attributes [opt] ptr-operator abstract-declarator [opt]
10743 attributes [opt] direct-abstract-declarator
10745 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10746 detect constructor, destructor or conversion operators. It is set
10747 to -1 if the declarator is a name, and +1 if it is a
10748 function. Otherwise it is set to zero. Usually you just want to
10749 test for >0, but internally the negative value is used.
10751 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10752 a decl-specifier-seq unless it declares a constructor, destructor,
10753 or conversion. It might seem that we could check this condition in
10754 semantic analysis, rather than parsing, but that makes it difficult
10755 to handle something like `f()'. We want to notice that there are
10756 no decl-specifiers, and therefore realize that this is an
10757 expression, not a declaration.)
10759 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10760 the declarator is a direct-declarator of the form "(...)".
10762 MEMBER_P is true iff this declarator is a member-declarator. */
10764 static cp_declarator
*
10765 cp_parser_declarator (cp_parser
* parser
,
10766 cp_parser_declarator_kind dcl_kind
,
10767 int* ctor_dtor_or_conv_p
,
10768 bool* parenthesized_p
,
10772 cp_declarator
*declarator
;
10773 enum tree_code code
;
10774 cp_cv_quals cv_quals
;
10776 tree attributes
= NULL_TREE
;
10778 /* Assume this is not a constructor, destructor, or type-conversion
10780 if (ctor_dtor_or_conv_p
)
10781 *ctor_dtor_or_conv_p
= 0;
10783 if (cp_parser_allow_gnu_extensions_p (parser
))
10784 attributes
= cp_parser_attributes_opt (parser
);
10786 /* Peek at the next token. */
10787 token
= cp_lexer_peek_token (parser
->lexer
);
10789 /* Check for the ptr-operator production. */
10790 cp_parser_parse_tentatively (parser
);
10791 /* Parse the ptr-operator. */
10792 code
= cp_parser_ptr_operator (parser
,
10795 /* If that worked, then we have a ptr-operator. */
10796 if (cp_parser_parse_definitely (parser
))
10798 /* If a ptr-operator was found, then this declarator was not
10800 if (parenthesized_p
)
10801 *parenthesized_p
= true;
10802 /* The dependent declarator is optional if we are parsing an
10803 abstract-declarator. */
10804 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10805 cp_parser_parse_tentatively (parser
);
10807 /* Parse the dependent declarator. */
10808 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10809 /*ctor_dtor_or_conv_p=*/NULL
,
10810 /*parenthesized_p=*/NULL
,
10811 /*member_p=*/false);
10813 /* If we are parsing an abstract-declarator, we must handle the
10814 case where the dependent declarator is absent. */
10815 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10816 && !cp_parser_parse_definitely (parser
))
10819 /* Build the representation of the ptr-operator. */
10821 declarator
= make_ptrmem_declarator (cv_quals
,
10824 else if (code
== INDIRECT_REF
)
10825 declarator
= make_pointer_declarator (cv_quals
, declarator
);
10827 declarator
= make_reference_declarator (cv_quals
, declarator
);
10829 /* Everything else is a direct-declarator. */
10832 if (parenthesized_p
)
10833 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10835 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10836 ctor_dtor_or_conv_p
,
10840 if (attributes
&& declarator
!= cp_error_declarator
)
10841 declarator
->attributes
= attributes
;
10846 /* Parse a direct-declarator or direct-abstract-declarator.
10850 direct-declarator ( parameter-declaration-clause )
10851 cv-qualifier-seq [opt]
10852 exception-specification [opt]
10853 direct-declarator [ constant-expression [opt] ]
10856 direct-abstract-declarator:
10857 direct-abstract-declarator [opt]
10858 ( parameter-declaration-clause )
10859 cv-qualifier-seq [opt]
10860 exception-specification [opt]
10861 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10862 ( abstract-declarator )
10864 Returns a representation of the declarator. DCL_KIND is
10865 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10866 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10867 we are parsing a direct-declarator. It is
10868 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10869 of ambiguity we prefer an abstract declarator, as per
10870 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10871 cp_parser_declarator. */
10873 static cp_declarator
*
10874 cp_parser_direct_declarator (cp_parser
* parser
,
10875 cp_parser_declarator_kind dcl_kind
,
10876 int* ctor_dtor_or_conv_p
,
10880 cp_declarator
*declarator
= NULL
;
10881 tree scope
= NULL_TREE
;
10882 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10883 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10885 bool pop_p
= false;
10889 /* Peek at the next token. */
10890 token
= cp_lexer_peek_token (parser
->lexer
);
10891 if (token
->type
== CPP_OPEN_PAREN
)
10893 /* This is either a parameter-declaration-clause, or a
10894 parenthesized declarator. When we know we are parsing a
10895 named declarator, it must be a parenthesized declarator
10896 if FIRST is true. For instance, `(int)' is a
10897 parameter-declaration-clause, with an omitted
10898 direct-abstract-declarator. But `((*))', is a
10899 parenthesized abstract declarator. Finally, when T is a
10900 template parameter `(T)' is a
10901 parameter-declaration-clause, and not a parenthesized
10904 We first try and parse a parameter-declaration-clause,
10905 and then try a nested declarator (if FIRST is true).
10907 It is not an error for it not to be a
10908 parameter-declaration-clause, even when FIRST is
10914 The first is the declaration of a function while the
10915 second is a the definition of a variable, including its
10918 Having seen only the parenthesis, we cannot know which of
10919 these two alternatives should be selected. Even more
10920 complex are examples like:
10925 The former is a function-declaration; the latter is a
10926 variable initialization.
10928 Thus again, we try a parameter-declaration-clause, and if
10929 that fails, we back out and return. */
10931 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10933 cp_parameter_declarator
*params
;
10934 unsigned saved_num_template_parameter_lists
;
10936 /* In a member-declarator, the only valid interpretation
10937 of a parenthesis is the start of a
10938 parameter-declaration-clause. (It is invalid to
10939 initialize a static data member with a parenthesized
10940 initializer; only the "=" form of initialization is
10943 cp_parser_parse_tentatively (parser
);
10945 /* Consume the `('. */
10946 cp_lexer_consume_token (parser
->lexer
);
10949 /* If this is going to be an abstract declarator, we're
10950 in a declarator and we can't have default args. */
10951 parser
->default_arg_ok_p
= false;
10952 parser
->in_declarator_p
= true;
10955 /* Inside the function parameter list, surrounding
10956 template-parameter-lists do not apply. */
10957 saved_num_template_parameter_lists
10958 = parser
->num_template_parameter_lists
;
10959 parser
->num_template_parameter_lists
= 0;
10961 /* Parse the parameter-declaration-clause. */
10962 params
= cp_parser_parameter_declaration_clause (parser
);
10964 parser
->num_template_parameter_lists
10965 = saved_num_template_parameter_lists
;
10967 /* If all went well, parse the cv-qualifier-seq and the
10968 exception-specification. */
10969 if (member_p
|| cp_parser_parse_definitely (parser
))
10971 cp_cv_quals cv_quals
;
10972 tree exception_specification
;
10974 if (ctor_dtor_or_conv_p
)
10975 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
10977 /* Consume the `)'. */
10978 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
10980 /* Parse the cv-qualifier-seq. */
10981 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
10982 /* And the exception-specification. */
10983 exception_specification
10984 = cp_parser_exception_specification_opt (parser
);
10986 /* Create the function-declarator. */
10987 declarator
= make_call_declarator (declarator
,
10990 exception_specification
);
10991 /* Any subsequent parameter lists are to do with
10992 return type, so are not those of the declared
10994 parser
->default_arg_ok_p
= false;
10996 /* Repeat the main loop. */
11001 /* If this is the first, we can try a parenthesized
11005 bool saved_in_type_id_in_expr_p
;
11007 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11008 parser
->in_declarator_p
= saved_in_declarator_p
;
11010 /* Consume the `('. */
11011 cp_lexer_consume_token (parser
->lexer
);
11012 /* Parse the nested declarator. */
11013 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11014 parser
->in_type_id_in_expr_p
= true;
11016 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11017 /*parenthesized_p=*/NULL
,
11019 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11021 /* Expect a `)'. */
11022 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11023 declarator
= cp_error_declarator
;
11024 if (declarator
== cp_error_declarator
)
11027 goto handle_declarator
;
11029 /* Otherwise, we must be done. */
11033 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11034 && token
->type
== CPP_OPEN_SQUARE
)
11036 /* Parse an array-declarator. */
11039 if (ctor_dtor_or_conv_p
)
11040 *ctor_dtor_or_conv_p
= 0;
11043 parser
->default_arg_ok_p
= false;
11044 parser
->in_declarator_p
= true;
11045 /* Consume the `['. */
11046 cp_lexer_consume_token (parser
->lexer
);
11047 /* Peek at the next token. */
11048 token
= cp_lexer_peek_token (parser
->lexer
);
11049 /* If the next token is `]', then there is no
11050 constant-expression. */
11051 if (token
->type
!= CPP_CLOSE_SQUARE
)
11053 bool non_constant_p
;
11056 = cp_parser_constant_expression (parser
,
11057 /*allow_non_constant=*/true,
11059 if (!non_constant_p
)
11060 bounds
= fold_non_dependent_expr (bounds
);
11063 bounds
= NULL_TREE
;
11064 /* Look for the closing `]'. */
11065 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11067 declarator
= cp_error_declarator
;
11071 declarator
= make_array_declarator (declarator
, bounds
);
11073 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11077 /* Parse a declarator-id */
11078 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11079 cp_parser_parse_tentatively (parser
);
11080 id
= cp_parser_declarator_id (parser
);
11081 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11083 if (!cp_parser_parse_definitely (parser
))
11084 id
= error_mark_node
;
11085 else if (TREE_CODE (id
) != IDENTIFIER_NODE
)
11087 cp_parser_error (parser
, "expected unqualified-id");
11088 id
= error_mark_node
;
11092 if (id
== error_mark_node
)
11094 declarator
= cp_error_declarator
;
11098 if (TREE_CODE (id
) == SCOPE_REF
&& !current_scope ())
11100 tree scope
= TREE_OPERAND (id
, 0);
11102 /* In the declaration of a member of a template class
11103 outside of the class itself, the SCOPE will sometimes
11104 be a TYPENAME_TYPE. For example, given:
11106 template <typename T>
11107 int S<T>::R::i = 3;
11109 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11110 this context, we must resolve S<T>::R to an ordinary
11111 type, rather than a typename type.
11113 The reason we normally avoid resolving TYPENAME_TYPEs
11114 is that a specialization of `S' might render
11115 `S<T>::R' not a type. However, if `S' is
11116 specialized, then this `i' will not be used, so there
11117 is no harm in resolving the types here. */
11118 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
11122 /* Resolve the TYPENAME_TYPE. */
11123 type
= resolve_typename_type (scope
,
11124 /*only_current_p=*/false);
11125 /* If that failed, the declarator is invalid. */
11126 if (type
== error_mark_node
)
11127 error ("%<%T::%D%> is not a type",
11128 TYPE_CONTEXT (scope
),
11129 TYPE_IDENTIFIER (scope
));
11130 /* Build a new DECLARATOR. */
11131 id
= build_nt (SCOPE_REF
, type
, TREE_OPERAND (id
, 1));
11135 declarator
= make_id_declarator (id
);
11139 tree unqualified_name
;
11141 if (TREE_CODE (id
) == SCOPE_REF
11142 && CLASS_TYPE_P (TREE_OPERAND (id
, 0)))
11144 class_type
= TREE_OPERAND (id
, 0);
11145 unqualified_name
= TREE_OPERAND (id
, 1);
11149 class_type
= current_class_type
;
11150 unqualified_name
= id
;
11155 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11156 declarator
->u
.id
.sfk
= sfk_destructor
;
11157 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11158 declarator
->u
.id
.sfk
= sfk_conversion
;
11159 else if (constructor_name_p (unqualified_name
,
11161 || (TREE_CODE (unqualified_name
) == TYPE_DECL
11162 && same_type_p (TREE_TYPE (unqualified_name
),
11164 declarator
->u
.id
.sfk
= sfk_constructor
;
11166 if (ctor_dtor_or_conv_p
&& declarator
->u
.id
.sfk
!= sfk_none
)
11167 *ctor_dtor_or_conv_p
= -1;
11168 if (TREE_CODE (id
) == SCOPE_REF
11169 && TREE_CODE (unqualified_name
) == TYPE_DECL
11170 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name
)))
11172 error ("invalid use of constructor as a template");
11173 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11174 "the constructor in a qualified name",
11176 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11177 class_type
, class_type
);
11182 handle_declarator
:;
11183 scope
= get_scope_of_declarator (declarator
);
11185 /* Any names that appear after the declarator-id for a
11186 member are looked up in the containing scope. */
11187 pop_p
= push_scope (scope
);
11188 parser
->in_declarator_p
= true;
11189 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11190 || (declarator
&& declarator
->kind
== cdk_id
))
11191 /* Default args are only allowed on function
11193 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11195 parser
->default_arg_ok_p
= false;
11204 /* For an abstract declarator, we might wind up with nothing at this
11205 point. That's an error; the declarator is not optional. */
11207 cp_parser_error (parser
, "expected declarator");
11209 /* If we entered a scope, we must exit it now. */
11213 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11214 parser
->in_declarator_p
= saved_in_declarator_p
;
11219 /* Parse a ptr-operator.
11222 * cv-qualifier-seq [opt]
11224 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11229 & cv-qualifier-seq [opt]
11231 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11232 Returns ADDR_EXPR if a reference was used. In the case of a
11233 pointer-to-member, *TYPE is filled in with the TYPE containing the
11234 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11235 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11236 ERROR_MARK if an error occurred. */
11238 static enum tree_code
11239 cp_parser_ptr_operator (cp_parser
* parser
,
11241 cp_cv_quals
*cv_quals
)
11243 enum tree_code code
= ERROR_MARK
;
11246 /* Assume that it's not a pointer-to-member. */
11248 /* And that there are no cv-qualifiers. */
11249 *cv_quals
= TYPE_UNQUALIFIED
;
11251 /* Peek at the next token. */
11252 token
= cp_lexer_peek_token (parser
->lexer
);
11253 /* If it's a `*' or `&' we have a pointer or reference. */
11254 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11256 /* Remember which ptr-operator we were processing. */
11257 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11259 /* Consume the `*' or `&'. */
11260 cp_lexer_consume_token (parser
->lexer
);
11262 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11263 `&', if we are allowing GNU extensions. (The only qualifier
11264 that can legally appear after `&' is `restrict', but that is
11265 enforced during semantic analysis. */
11266 if (code
== INDIRECT_REF
11267 || cp_parser_allow_gnu_extensions_p (parser
))
11268 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11272 /* Try the pointer-to-member case. */
11273 cp_parser_parse_tentatively (parser
);
11274 /* Look for the optional `::' operator. */
11275 cp_parser_global_scope_opt (parser
,
11276 /*current_scope_valid_p=*/false);
11277 /* Look for the nested-name specifier. */
11278 cp_parser_nested_name_specifier (parser
,
11279 /*typename_keyword_p=*/false,
11280 /*check_dependency_p=*/true,
11282 /*is_declaration=*/false);
11283 /* If we found it, and the next token is a `*', then we are
11284 indeed looking at a pointer-to-member operator. */
11285 if (!cp_parser_error_occurred (parser
)
11286 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11288 /* The type of which the member is a member is given by the
11290 *type
= parser
->scope
;
11291 /* The next name will not be qualified. */
11292 parser
->scope
= NULL_TREE
;
11293 parser
->qualifying_scope
= NULL_TREE
;
11294 parser
->object_scope
= NULL_TREE
;
11295 /* Indicate that the `*' operator was used. */
11296 code
= INDIRECT_REF
;
11297 /* Look for the optional cv-qualifier-seq. */
11298 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11300 /* If that didn't work we don't have a ptr-operator. */
11301 if (!cp_parser_parse_definitely (parser
))
11302 cp_parser_error (parser
, "expected ptr-operator");
11308 /* Parse an (optional) cv-qualifier-seq.
11311 cv-qualifier cv-qualifier-seq [opt]
11322 Returns a bitmask representing the cv-qualifiers. */
11325 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11327 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11332 cp_cv_quals cv_qualifier
;
11334 /* Peek at the next token. */
11335 token
= cp_lexer_peek_token (parser
->lexer
);
11336 /* See if it's a cv-qualifier. */
11337 switch (token
->keyword
)
11340 cv_qualifier
= TYPE_QUAL_CONST
;
11344 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11348 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11352 cv_qualifier
= TYPE_UNQUALIFIED
;
11359 if (cv_quals
& cv_qualifier
)
11361 error ("duplicate cv-qualifier");
11362 cp_lexer_purge_token (parser
->lexer
);
11366 cp_lexer_consume_token (parser
->lexer
);
11367 cv_quals
|= cv_qualifier
;
11374 /* Parse a declarator-id.
11378 :: [opt] nested-name-specifier [opt] type-name
11380 In the `id-expression' case, the value returned is as for
11381 cp_parser_id_expression if the id-expression was an unqualified-id.
11382 If the id-expression was a qualified-id, then a SCOPE_REF is
11383 returned. The first operand is the scope (either a NAMESPACE_DECL
11384 or TREE_TYPE), but the second is still just a representation of an
11388 cp_parser_declarator_id (cp_parser
* parser
)
11390 tree id_expression
;
11392 /* The expression must be an id-expression. Assume that qualified
11393 names are the names of types so that:
11396 int S<T>::R::i = 3;
11398 will work; we must treat `S<T>::R' as the name of a type.
11399 Similarly, assume that qualified names are templates, where
11403 int S<T>::R<T>::i = 3;
11406 id_expression
= cp_parser_id_expression (parser
,
11407 /*template_keyword_p=*/false,
11408 /*check_dependency_p=*/false,
11409 /*template_p=*/NULL
,
11410 /*declarator_p=*/true);
11411 /* If the name was qualified, create a SCOPE_REF to represent
11415 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
11416 parser
->scope
= NULL_TREE
;
11419 return id_expression
;
11422 /* Parse a type-id.
11425 type-specifier-seq abstract-declarator [opt]
11427 Returns the TYPE specified. */
11430 cp_parser_type_id (cp_parser
* parser
)
11432 cp_decl_specifier_seq type_specifier_seq
;
11433 cp_declarator
*abstract_declarator
;
11435 /* Parse the type-specifier-seq. */
11436 cp_parser_type_specifier_seq (parser
, &type_specifier_seq
);
11437 if (type_specifier_seq
.type
== error_mark_node
)
11438 return error_mark_node
;
11440 /* There might or might not be an abstract declarator. */
11441 cp_parser_parse_tentatively (parser
);
11442 /* Look for the declarator. */
11443 abstract_declarator
11444 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11445 /*parenthesized_p=*/NULL
,
11446 /*member_p=*/false);
11447 /* Check to see if there really was a declarator. */
11448 if (!cp_parser_parse_definitely (parser
))
11449 abstract_declarator
= NULL
;
11451 return groktypename (&type_specifier_seq
, abstract_declarator
);
11454 /* Parse a type-specifier-seq.
11456 type-specifier-seq:
11457 type-specifier type-specifier-seq [opt]
11461 type-specifier-seq:
11462 attributes type-specifier-seq [opt]
11464 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11467 cp_parser_type_specifier_seq (cp_parser
* parser
,
11468 cp_decl_specifier_seq
*type_specifier_seq
)
11470 bool seen_type_specifier
= false;
11472 /* Clear the TYPE_SPECIFIER_SEQ. */
11473 clear_decl_specs (type_specifier_seq
);
11475 /* Parse the type-specifiers and attributes. */
11478 tree type_specifier
;
11480 /* Check for attributes first. */
11481 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
11483 type_specifier_seq
->attributes
=
11484 chainon (type_specifier_seq
->attributes
,
11485 cp_parser_attributes_opt (parser
));
11489 /* Look for the type-specifier. */
11490 type_specifier
= cp_parser_type_specifier (parser
,
11491 CP_PARSER_FLAGS_OPTIONAL
,
11492 type_specifier_seq
,
11493 /*is_declaration=*/false,
11496 /* If the first type-specifier could not be found, this is not a
11497 type-specifier-seq at all. */
11498 if (!seen_type_specifier
&& !type_specifier
)
11500 cp_parser_error (parser
, "expected type-specifier");
11501 type_specifier_seq
->type
= error_mark_node
;
11504 /* If subsequent type-specifiers could not be found, the
11505 type-specifier-seq is complete. */
11506 else if (seen_type_specifier
&& !type_specifier
)
11509 seen_type_specifier
= true;
11515 /* Parse a parameter-declaration-clause.
11517 parameter-declaration-clause:
11518 parameter-declaration-list [opt] ... [opt]
11519 parameter-declaration-list , ...
11521 Returns a representation for the parameter declarations. A return
11522 value of NULL indicates a parameter-declaration-clause consisting
11523 only of an ellipsis. */
11525 static cp_parameter_declarator
*
11526 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
11528 cp_parameter_declarator
*parameters
;
11533 /* Peek at the next token. */
11534 token
= cp_lexer_peek_token (parser
->lexer
);
11535 /* Check for trivial parameter-declaration-clauses. */
11536 if (token
->type
== CPP_ELLIPSIS
)
11538 /* Consume the `...' token. */
11539 cp_lexer_consume_token (parser
->lexer
);
11542 else if (token
->type
== CPP_CLOSE_PAREN
)
11543 /* There are no parameters. */
11545 #ifndef NO_IMPLICIT_EXTERN_C
11546 if (in_system_header
&& current_class_type
== NULL
11547 && current_lang_name
== lang_name_c
)
11551 return no_parameters
;
11553 /* Check for `(void)', too, which is a special case. */
11554 else if (token
->keyword
== RID_VOID
11555 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11556 == CPP_CLOSE_PAREN
))
11558 /* Consume the `void' token. */
11559 cp_lexer_consume_token (parser
->lexer
);
11560 /* There are no parameters. */
11561 return no_parameters
;
11564 /* Parse the parameter-declaration-list. */
11565 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
11566 /* If a parse error occurred while parsing the
11567 parameter-declaration-list, then the entire
11568 parameter-declaration-clause is erroneous. */
11572 /* Peek at the next token. */
11573 token
= cp_lexer_peek_token (parser
->lexer
);
11574 /* If it's a `,', the clause should terminate with an ellipsis. */
11575 if (token
->type
== CPP_COMMA
)
11577 /* Consume the `,'. */
11578 cp_lexer_consume_token (parser
->lexer
);
11579 /* Expect an ellipsis. */
11581 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
11583 /* It might also be `...' if the optional trailing `,' was
11585 else if (token
->type
== CPP_ELLIPSIS
)
11587 /* Consume the `...' token. */
11588 cp_lexer_consume_token (parser
->lexer
);
11589 /* And remember that we saw it. */
11593 ellipsis_p
= false;
11595 /* Finish the parameter list. */
11596 if (parameters
&& ellipsis_p
)
11597 parameters
->ellipsis_p
= true;
11602 /* Parse a parameter-declaration-list.
11604 parameter-declaration-list:
11605 parameter-declaration
11606 parameter-declaration-list , parameter-declaration
11608 Returns a representation of the parameter-declaration-list, as for
11609 cp_parser_parameter_declaration_clause. However, the
11610 `void_list_node' is never appended to the list. Upon return,
11611 *IS_ERROR will be true iff an error occurred. */
11613 static cp_parameter_declarator
*
11614 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
11616 cp_parameter_declarator
*parameters
= NULL
;
11617 cp_parameter_declarator
**tail
= ¶meters
;
11619 /* Assume all will go well. */
11622 /* Look for more parameters. */
11625 cp_parameter_declarator
*parameter
;
11626 bool parenthesized_p
;
11627 /* Parse the parameter. */
11629 = cp_parser_parameter_declaration (parser
,
11630 /*template_parm_p=*/false,
11633 /* If a parse error occurred parsing the parameter declaration,
11634 then the entire parameter-declaration-list is erroneous. */
11641 /* Add the new parameter to the list. */
11643 tail
= ¶meter
->next
;
11645 /* Peek at the next token. */
11646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
11647 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11648 /* The parameter-declaration-list is complete. */
11650 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11654 /* Peek at the next token. */
11655 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11656 /* If it's an ellipsis, then the list is complete. */
11657 if (token
->type
== CPP_ELLIPSIS
)
11659 /* Otherwise, there must be more parameters. Consume the
11661 cp_lexer_consume_token (parser
->lexer
);
11662 /* When parsing something like:
11664 int i(float f, double d)
11666 we can tell after seeing the declaration for "f" that we
11667 are not looking at an initialization of a variable "i",
11668 but rather at the declaration of a function "i".
11670 Due to the fact that the parsing of template arguments
11671 (as specified to a template-id) requires backtracking we
11672 cannot use this technique when inside a template argument
11674 if (!parser
->in_template_argument_list_p
11675 && !parser
->in_type_id_in_expr_p
11676 && cp_parser_parsing_tentatively (parser
)
11677 && !cp_parser_committed_to_tentative_parse (parser
)
11678 /* However, a parameter-declaration of the form
11679 "foat(f)" (which is a valid declaration of a
11680 parameter "f") can also be interpreted as an
11681 expression (the conversion of "f" to "float"). */
11682 && !parenthesized_p
)
11683 cp_parser_commit_to_tentative_parse (parser
);
11687 cp_parser_error (parser
, "expected %<,%> or %<...%>");
11688 if (!cp_parser_parsing_tentatively (parser
)
11689 || cp_parser_committed_to_tentative_parse (parser
))
11690 cp_parser_skip_to_closing_parenthesis (parser
,
11691 /*recovering=*/true,
11692 /*or_comma=*/false,
11693 /*consume_paren=*/false);
11701 /* Parse a parameter declaration.
11703 parameter-declaration:
11704 decl-specifier-seq declarator
11705 decl-specifier-seq declarator = assignment-expression
11706 decl-specifier-seq abstract-declarator [opt]
11707 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11709 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11710 declares a template parameter. (In that case, a non-nested `>'
11711 token encountered during the parsing of the assignment-expression
11712 is not interpreted as a greater-than operator.)
11714 Returns a representation of the parameter, or NULL if an error
11715 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11716 true iff the declarator is of the form "(p)". */
11718 static cp_parameter_declarator
*
11719 cp_parser_parameter_declaration (cp_parser
*parser
,
11720 bool template_parm_p
,
11721 bool *parenthesized_p
)
11723 int declares_class_or_enum
;
11724 bool greater_than_is_operator_p
;
11725 cp_decl_specifier_seq decl_specifiers
;
11726 cp_declarator
*declarator
;
11727 tree default_argument
;
11729 const char *saved_message
;
11731 /* In a template parameter, `>' is not an operator.
11735 When parsing a default template-argument for a non-type
11736 template-parameter, the first non-nested `>' is taken as the end
11737 of the template parameter-list rather than a greater-than
11739 greater_than_is_operator_p
= !template_parm_p
;
11741 /* Type definitions may not appear in parameter types. */
11742 saved_message
= parser
->type_definition_forbidden_message
;
11743 parser
->type_definition_forbidden_message
11744 = "types may not be defined in parameter types";
11746 /* Parse the declaration-specifiers. */
11747 cp_parser_decl_specifier_seq (parser
,
11748 CP_PARSER_FLAGS_NONE
,
11750 &declares_class_or_enum
);
11751 /* If an error occurred, there's no reason to attempt to parse the
11752 rest of the declaration. */
11753 if (cp_parser_error_occurred (parser
))
11755 parser
->type_definition_forbidden_message
= saved_message
;
11759 /* Peek at the next token. */
11760 token
= cp_lexer_peek_token (parser
->lexer
);
11761 /* If the next token is a `)', `,', `=', `>', or `...', then there
11762 is no declarator. */
11763 if (token
->type
== CPP_CLOSE_PAREN
11764 || token
->type
== CPP_COMMA
11765 || token
->type
== CPP_EQ
11766 || token
->type
== CPP_ELLIPSIS
11767 || token
->type
== CPP_GREATER
)
11770 if (parenthesized_p
)
11771 *parenthesized_p
= false;
11773 /* Otherwise, there should be a declarator. */
11776 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11777 parser
->default_arg_ok_p
= false;
11779 /* After seeing a decl-specifier-seq, if the next token is not a
11780 "(", there is no possibility that the code is a valid
11781 expression. Therefore, if parsing tentatively, we commit at
11783 if (!parser
->in_template_argument_list_p
11784 /* In an expression context, having seen:
11788 we cannot be sure whether we are looking at a
11789 function-type (taking a "char" as a parameter) or a cast
11790 of some object of type "char" to "int". */
11791 && !parser
->in_type_id_in_expr_p
11792 && cp_parser_parsing_tentatively (parser
)
11793 && !cp_parser_committed_to_tentative_parse (parser
)
11794 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11795 cp_parser_commit_to_tentative_parse (parser
);
11796 /* Parse the declarator. */
11797 declarator
= cp_parser_declarator (parser
,
11798 CP_PARSER_DECLARATOR_EITHER
,
11799 /*ctor_dtor_or_conv_p=*/NULL
,
11801 /*member_p=*/false);
11802 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11803 /* After the declarator, allow more attributes. */
11804 decl_specifiers
.attributes
11805 = chainon (decl_specifiers
.attributes
,
11806 cp_parser_attributes_opt (parser
));
11809 /* The restriction on defining new types applies only to the type
11810 of the parameter, not to the default argument. */
11811 parser
->type_definition_forbidden_message
= saved_message
;
11813 /* If the next token is `=', then process a default argument. */
11814 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11816 bool saved_greater_than_is_operator_p
;
11817 /* Consume the `='. */
11818 cp_lexer_consume_token (parser
->lexer
);
11820 /* If we are defining a class, then the tokens that make up the
11821 default argument must be saved and processed later. */
11822 if (!template_parm_p
&& at_class_scope_p ()
11823 && TYPE_BEING_DEFINED (current_class_type
))
11825 unsigned depth
= 0;
11826 cp_token
*first_token
;
11829 /* Add tokens until we have processed the entire default
11830 argument. We add the range [first_token, token). */
11831 first_token
= cp_lexer_peek_token (parser
->lexer
);
11836 /* Peek at the next token. */
11837 token
= cp_lexer_peek_token (parser
->lexer
);
11838 /* What we do depends on what token we have. */
11839 switch (token
->type
)
11841 /* In valid code, a default argument must be
11842 immediately followed by a `,' `)', or `...'. */
11844 case CPP_CLOSE_PAREN
:
11846 /* If we run into a non-nested `;', `}', or `]',
11847 then the code is invalid -- but the default
11848 argument is certainly over. */
11849 case CPP_SEMICOLON
:
11850 case CPP_CLOSE_BRACE
:
11851 case CPP_CLOSE_SQUARE
:
11854 /* Update DEPTH, if necessary. */
11855 else if (token
->type
== CPP_CLOSE_PAREN
11856 || token
->type
== CPP_CLOSE_BRACE
11857 || token
->type
== CPP_CLOSE_SQUARE
)
11861 case CPP_OPEN_PAREN
:
11862 case CPP_OPEN_SQUARE
:
11863 case CPP_OPEN_BRACE
:
11868 /* If we see a non-nested `>', and `>' is not an
11869 operator, then it marks the end of the default
11871 if (!depth
&& !greater_than_is_operator_p
)
11875 /* If we run out of tokens, issue an error message. */
11877 error ("file ends in default argument");
11883 /* In these cases, we should look for template-ids.
11884 For example, if the default argument is
11885 `X<int, double>()', we need to do name lookup to
11886 figure out whether or not `X' is a template; if
11887 so, the `,' does not end the default argument.
11889 That is not yet done. */
11896 /* If we've reached the end, stop. */
11900 /* Add the token to the token block. */
11901 token
= cp_lexer_consume_token (parser
->lexer
);
11904 /* Create a DEFAULT_ARG to represented the unparsed default
11906 default_argument
= make_node (DEFAULT_ARG
);
11907 DEFARG_TOKENS (default_argument
)
11908 = cp_token_cache_new (first_token
, token
);
11910 /* Outside of a class definition, we can just parse the
11911 assignment-expression. */
11914 bool saved_local_variables_forbidden_p
;
11916 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11918 saved_greater_than_is_operator_p
11919 = parser
->greater_than_is_operator_p
;
11920 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11921 /* Local variable names (and the `this' keyword) may not
11922 appear in a default argument. */
11923 saved_local_variables_forbidden_p
11924 = parser
->local_variables_forbidden_p
;
11925 parser
->local_variables_forbidden_p
= true;
11926 /* Parse the assignment-expression. */
11927 default_argument
= cp_parser_assignment_expression (parser
);
11928 /* Restore saved state. */
11929 parser
->greater_than_is_operator_p
11930 = saved_greater_than_is_operator_p
;
11931 parser
->local_variables_forbidden_p
11932 = saved_local_variables_forbidden_p
;
11934 if (!parser
->default_arg_ok_p
)
11936 if (!flag_pedantic_errors
)
11937 warning ("deprecated use of default argument for parameter of non-function");
11940 error ("default arguments are only permitted for function parameters");
11941 default_argument
= NULL_TREE
;
11946 default_argument
= NULL_TREE
;
11948 return make_parameter_declarator (&decl_specifiers
,
11953 /* Parse a function-body.
11956 compound_statement */
11959 cp_parser_function_body (cp_parser
*parser
)
11961 cp_parser_compound_statement (parser
, NULL
, false);
11964 /* Parse a ctor-initializer-opt followed by a function-body. Return
11965 true if a ctor-initializer was present. */
11968 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11971 bool ctor_initializer_p
;
11973 /* Begin the function body. */
11974 body
= begin_function_body ();
11975 /* Parse the optional ctor-initializer. */
11976 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11977 /* Parse the function-body. */
11978 cp_parser_function_body (parser
);
11979 /* Finish the function body. */
11980 finish_function_body (body
);
11982 return ctor_initializer_p
;
11985 /* Parse an initializer.
11988 = initializer-clause
11989 ( expression-list )
11991 Returns a expression representing the initializer. If no
11992 initializer is present, NULL_TREE is returned.
11994 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11995 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11996 set to FALSE if there is no initializer present. If there is an
11997 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11998 is set to true; otherwise it is set to false. */
12001 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12002 bool* non_constant_p
)
12007 /* Peek at the next token. */
12008 token
= cp_lexer_peek_token (parser
->lexer
);
12010 /* Let our caller know whether or not this initializer was
12012 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12013 /* Assume that the initializer is constant. */
12014 *non_constant_p
= false;
12016 if (token
->type
== CPP_EQ
)
12018 /* Consume the `='. */
12019 cp_lexer_consume_token (parser
->lexer
);
12020 /* Parse the initializer-clause. */
12021 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12023 else if (token
->type
== CPP_OPEN_PAREN
)
12024 init
= cp_parser_parenthesized_expression_list (parser
, false,
12028 /* Anything else is an error. */
12029 cp_parser_error (parser
, "expected initializer");
12030 init
= error_mark_node
;
12036 /* Parse an initializer-clause.
12038 initializer-clause:
12039 assignment-expression
12040 { initializer-list , [opt] }
12043 Returns an expression representing the initializer.
12045 If the `assignment-expression' production is used the value
12046 returned is simply a representation for the expression.
12048 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12049 the elements of the initializer-list (or NULL_TREE, if the last
12050 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12051 NULL_TREE. There is no way to detect whether or not the optional
12052 trailing `,' was provided. NON_CONSTANT_P is as for
12053 cp_parser_initializer. */
12056 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12060 /* If it is not a `{', then we are looking at an
12061 assignment-expression. */
12062 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12065 = cp_parser_constant_expression (parser
,
12066 /*allow_non_constant_p=*/true,
12068 if (!*non_constant_p
)
12069 initializer
= fold_non_dependent_expr (initializer
);
12073 /* Consume the `{' token. */
12074 cp_lexer_consume_token (parser
->lexer
);
12075 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12076 initializer
= make_node (CONSTRUCTOR
);
12077 /* If it's not a `}', then there is a non-trivial initializer. */
12078 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12080 /* Parse the initializer list. */
12081 CONSTRUCTOR_ELTS (initializer
)
12082 = cp_parser_initializer_list (parser
, non_constant_p
);
12083 /* A trailing `,' token is allowed. */
12084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12085 cp_lexer_consume_token (parser
->lexer
);
12087 /* Now, there should be a trailing `}'. */
12088 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12091 return initializer
;
12094 /* Parse an initializer-list.
12098 initializer-list , initializer-clause
12103 identifier : initializer-clause
12104 initializer-list, identifier : initializer-clause
12106 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12107 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12108 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12109 as for cp_parser_initializer. */
12112 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12114 tree initializers
= NULL_TREE
;
12116 /* Assume all of the expressions are constant. */
12117 *non_constant_p
= false;
12119 /* Parse the rest of the list. */
12125 bool clause_non_constant_p
;
12127 /* If the next token is an identifier and the following one is a
12128 colon, we are looking at the GNU designated-initializer
12130 if (cp_parser_allow_gnu_extensions_p (parser
)
12131 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12132 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12134 /* Consume the identifier. */
12135 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12136 /* Consume the `:'. */
12137 cp_lexer_consume_token (parser
->lexer
);
12140 identifier
= NULL_TREE
;
12142 /* Parse the initializer. */
12143 initializer
= cp_parser_initializer_clause (parser
,
12144 &clause_non_constant_p
);
12145 /* If any clause is non-constant, so is the entire initializer. */
12146 if (clause_non_constant_p
)
12147 *non_constant_p
= true;
12148 /* Add it to the list. */
12149 initializers
= tree_cons (identifier
, initializer
, initializers
);
12151 /* If the next token is not a comma, we have reached the end of
12153 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12156 /* Peek at the next token. */
12157 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12158 /* If the next token is a `}', then we're still done. An
12159 initializer-clause can have a trailing `,' after the
12160 initializer-list and before the closing `}'. */
12161 if (token
->type
== CPP_CLOSE_BRACE
)
12164 /* Consume the `,' token. */
12165 cp_lexer_consume_token (parser
->lexer
);
12168 /* The initializers were built up in reverse order, so we need to
12169 reverse them now. */
12170 return nreverse (initializers
);
12173 /* Classes [gram.class] */
12175 /* Parse a class-name.
12181 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12182 to indicate that names looked up in dependent types should be
12183 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12184 keyword has been used to indicate that the name that appears next
12185 is a template. TYPE_P is true iff the next name should be treated
12186 as class-name, even if it is declared to be some other kind of name
12187 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12188 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12189 being defined in a class-head.
12191 Returns the TYPE_DECL representing the class. */
12194 cp_parser_class_name (cp_parser
*parser
,
12195 bool typename_keyword_p
,
12196 bool template_keyword_p
,
12198 bool check_dependency_p
,
12200 bool is_declaration
)
12207 /* All class-names start with an identifier. */
12208 token
= cp_lexer_peek_token (parser
->lexer
);
12209 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12211 cp_parser_error (parser
, "expected class-name");
12212 return error_mark_node
;
12215 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12216 to a template-id, so we save it here. */
12217 scope
= parser
->scope
;
12218 if (scope
== error_mark_node
)
12219 return error_mark_node
;
12221 /* Any name names a type if we're following the `typename' keyword
12222 in a qualified name where the enclosing scope is type-dependent. */
12223 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12224 && dependent_type_p (scope
));
12225 /* Handle the common case (an identifier, but not a template-id)
12227 if (token
->type
== CPP_NAME
12228 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12232 /* Look for the identifier. */
12233 identifier
= cp_parser_identifier (parser
);
12234 /* If the next token isn't an identifier, we are certainly not
12235 looking at a class-name. */
12236 if (identifier
== error_mark_node
)
12237 decl
= error_mark_node
;
12238 /* If we know this is a type-name, there's no need to look it
12240 else if (typename_p
)
12244 /* If the next token is a `::', then the name must be a type
12247 [basic.lookup.qual]
12249 During the lookup for a name preceding the :: scope
12250 resolution operator, object, function, and enumerator
12251 names are ignored. */
12252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12254 /* Look up the name. */
12255 decl
= cp_parser_lookup_name (parser
, identifier
,
12257 /*is_template=*/false,
12258 /*is_namespace=*/false,
12259 check_dependency_p
,
12260 /*ambiguous_p=*/NULL
);
12265 /* Try a template-id. */
12266 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12267 check_dependency_p
,
12269 if (decl
== error_mark_node
)
12270 return error_mark_node
;
12273 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12275 /* If this is a typename, create a TYPENAME_TYPE. */
12276 if (typename_p
&& decl
!= error_mark_node
)
12278 decl
= make_typename_type (scope
, decl
, /*complain=*/1);
12279 if (decl
!= error_mark_node
)
12280 decl
= TYPE_NAME (decl
);
12283 /* Check to see that it is really the name of a class. */
12284 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12285 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12286 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12287 /* Situations like this:
12289 template <typename T> struct A {
12290 typename T::template X<int>::I i;
12293 are problematic. Is `T::template X<int>' a class-name? The
12294 standard does not seem to be definitive, but there is no other
12295 valid interpretation of the following `::'. Therefore, those
12296 names are considered class-names. */
12297 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
12298 else if (decl
== error_mark_node
12299 || TREE_CODE (decl
) != TYPE_DECL
12300 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12302 cp_parser_error (parser
, "expected class-name");
12303 return error_mark_node
;
12309 /* Parse a class-specifier.
12312 class-head { member-specification [opt] }
12314 Returns the TREE_TYPE representing the class. */
12317 cp_parser_class_specifier (cp_parser
* parser
)
12321 tree attributes
= NULL_TREE
;
12322 int has_trailing_semicolon
;
12323 bool nested_name_specifier_p
;
12324 unsigned saved_num_template_parameter_lists
;
12325 bool pop_p
= false;
12326 tree scope
= NULL_TREE
;
12328 push_deferring_access_checks (dk_no_deferred
);
12330 /* Parse the class-head. */
12331 type
= cp_parser_class_head (parser
,
12332 &nested_name_specifier_p
,
12334 /* If the class-head was a semantic disaster, skip the entire body
12338 cp_parser_skip_to_end_of_block_or_statement (parser
);
12339 pop_deferring_access_checks ();
12340 return error_mark_node
;
12343 /* Look for the `{'. */
12344 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12346 pop_deferring_access_checks ();
12347 return error_mark_node
;
12350 /* Issue an error message if type-definitions are forbidden here. */
12351 cp_parser_check_type_definition (parser
);
12352 /* Remember that we are defining one more class. */
12353 ++parser
->num_classes_being_defined
;
12354 /* Inside the class, surrounding template-parameter-lists do not
12356 saved_num_template_parameter_lists
12357 = parser
->num_template_parameter_lists
;
12358 parser
->num_template_parameter_lists
= 0;
12360 /* Start the class. */
12361 if (nested_name_specifier_p
)
12363 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12364 pop_p
= push_scope (scope
);
12366 type
= begin_class_definition (type
);
12368 if (type
== error_mark_node
)
12369 /* If the type is erroneous, skip the entire body of the class. */
12370 cp_parser_skip_to_closing_brace (parser
);
12372 /* Parse the member-specification. */
12373 cp_parser_member_specification_opt (parser
);
12375 /* Look for the trailing `}'. */
12376 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12377 /* We get better error messages by noticing a common problem: a
12378 missing trailing `;'. */
12379 token
= cp_lexer_peek_token (parser
->lexer
);
12380 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12381 /* Look for trailing attributes to apply to this class. */
12382 if (cp_parser_allow_gnu_extensions_p (parser
))
12384 tree sub_attr
= cp_parser_attributes_opt (parser
);
12385 attributes
= chainon (attributes
, sub_attr
);
12387 if (type
!= error_mark_node
)
12388 type
= finish_struct (type
, attributes
);
12391 /* If this class is not itself within the scope of another class,
12392 then we need to parse the bodies of all of the queued function
12393 definitions. Note that the queued functions defined in a class
12394 are not always processed immediately following the
12395 class-specifier for that class. Consider:
12398 struct B { void f() { sizeof (A); } };
12401 If `f' were processed before the processing of `A' were
12402 completed, there would be no way to compute the size of `A'.
12403 Note that the nesting we are interested in here is lexical --
12404 not the semantic nesting given by TYPE_CONTEXT. In particular,
12407 struct A { struct B; };
12408 struct A::B { void f() { } };
12410 there is no need to delay the parsing of `A::B::f'. */
12411 if (--parser
->num_classes_being_defined
== 0)
12418 /* In a first pass, parse default arguments to the functions.
12419 Then, in a second pass, parse the bodies of the functions.
12420 This two-phased approach handles cases like:
12428 class_type
= NULL_TREE
;
12430 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
12431 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
12432 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
12433 TREE_PURPOSE (parser
->unparsed_functions_queues
)
12434 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
12436 fn
= TREE_VALUE (queue_entry
);
12437 /* If there are default arguments that have not yet been processed,
12438 take care of them now. */
12439 if (class_type
!= TREE_PURPOSE (queue_entry
))
12442 pop_scope (class_type
);
12443 class_type
= TREE_PURPOSE (queue_entry
);
12444 pop_p
= push_scope (class_type
);
12446 /* Make sure that any template parameters are in scope. */
12447 maybe_begin_member_template_processing (fn
);
12448 /* Parse the default argument expressions. */
12449 cp_parser_late_parsing_default_args (parser
, fn
);
12450 /* Remove any template parameters from the symbol table. */
12451 maybe_end_member_template_processing ();
12454 pop_scope (class_type
);
12455 /* Now parse the body of the functions. */
12456 for (TREE_VALUE (parser
->unparsed_functions_queues
)
12457 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
12458 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
12459 TREE_VALUE (parser
->unparsed_functions_queues
)
12460 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
12462 /* Figure out which function we need to process. */
12463 fn
= TREE_VALUE (queue_entry
);
12465 /* A hack to prevent garbage collection. */
12468 /* Parse the function. */
12469 cp_parser_late_parsing_for_member (parser
, fn
);
12474 /* Put back any saved access checks. */
12475 pop_deferring_access_checks ();
12477 /* Restore the count of active template-parameter-lists. */
12478 parser
->num_template_parameter_lists
12479 = saved_num_template_parameter_lists
;
12484 /* Parse a class-head.
12487 class-key identifier [opt] base-clause [opt]
12488 class-key nested-name-specifier identifier base-clause [opt]
12489 class-key nested-name-specifier [opt] template-id
12493 class-key attributes identifier [opt] base-clause [opt]
12494 class-key attributes nested-name-specifier identifier base-clause [opt]
12495 class-key attributes nested-name-specifier [opt] template-id
12498 Returns the TYPE of the indicated class. Sets
12499 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12500 involving a nested-name-specifier was used, and FALSE otherwise.
12502 Returns NULL_TREE if the class-head is syntactically valid, but
12503 semantically invalid in a way that means we should skip the entire
12504 body of the class. */
12507 cp_parser_class_head (cp_parser
* parser
,
12508 bool* nested_name_specifier_p
,
12509 tree
*attributes_p
)
12511 tree nested_name_specifier
;
12512 enum tag_types class_key
;
12513 tree id
= NULL_TREE
;
12514 tree type
= NULL_TREE
;
12516 bool template_id_p
= false;
12517 bool qualified_p
= false;
12518 bool invalid_nested_name_p
= false;
12519 bool invalid_explicit_specialization_p
= false;
12520 bool pop_p
= false;
12521 unsigned num_templates
;
12524 /* Assume no nested-name-specifier will be present. */
12525 *nested_name_specifier_p
= false;
12526 /* Assume no template parameter lists will be used in defining the
12530 /* Look for the class-key. */
12531 class_key
= cp_parser_class_key (parser
);
12532 if (class_key
== none_type
)
12533 return error_mark_node
;
12535 /* Parse the attributes. */
12536 attributes
= cp_parser_attributes_opt (parser
);
12538 /* If the next token is `::', that is invalid -- but sometimes
12539 people do try to write:
12543 Handle this gracefully by accepting the extra qualifier, and then
12544 issuing an error about it later if this really is a
12545 class-head. If it turns out just to be an elaborated type
12546 specifier, remain silent. */
12547 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
12548 qualified_p
= true;
12550 push_deferring_access_checks (dk_no_check
);
12552 /* Determine the name of the class. Begin by looking for an
12553 optional nested-name-specifier. */
12554 nested_name_specifier
12555 = cp_parser_nested_name_specifier_opt (parser
,
12556 /*typename_keyword_p=*/false,
12557 /*check_dependency_p=*/false,
12559 /*is_declaration=*/false);
12560 /* If there was a nested-name-specifier, then there *must* be an
12562 if (nested_name_specifier
)
12564 /* Although the grammar says `identifier', it really means
12565 `class-name' or `template-name'. You are only allowed to
12566 define a class that has already been declared with this
12569 The proposed resolution for Core Issue 180 says that whever
12570 you see `class T::X' you should treat `X' as a type-name.
12572 It is OK to define an inaccessible class; for example:
12574 class A { class B; };
12577 We do not know if we will see a class-name, or a
12578 template-name. We look for a class-name first, in case the
12579 class-name is a template-id; if we looked for the
12580 template-name first we would stop after the template-name. */
12581 cp_parser_parse_tentatively (parser
);
12582 type
= cp_parser_class_name (parser
,
12583 /*typename_keyword_p=*/false,
12584 /*template_keyword_p=*/false,
12586 /*check_dependency_p=*/false,
12587 /*class_head_p=*/true,
12588 /*is_declaration=*/false);
12589 /* If that didn't work, ignore the nested-name-specifier. */
12590 if (!cp_parser_parse_definitely (parser
))
12592 invalid_nested_name_p
= true;
12593 id
= cp_parser_identifier (parser
);
12594 if (id
== error_mark_node
)
12597 /* If we could not find a corresponding TYPE, treat this
12598 declaration like an unqualified declaration. */
12599 if (type
== error_mark_node
)
12600 nested_name_specifier
= NULL_TREE
;
12601 /* Otherwise, count the number of templates used in TYPE and its
12602 containing scopes. */
12607 for (scope
= TREE_TYPE (type
);
12608 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
12609 scope
= (TYPE_P (scope
)
12610 ? TYPE_CONTEXT (scope
)
12611 : DECL_CONTEXT (scope
)))
12613 && CLASS_TYPE_P (scope
)
12614 && CLASSTYPE_TEMPLATE_INFO (scope
)
12615 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
12616 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
12620 /* Otherwise, the identifier is optional. */
12623 /* We don't know whether what comes next is a template-id,
12624 an identifier, or nothing at all. */
12625 cp_parser_parse_tentatively (parser
);
12626 /* Check for a template-id. */
12627 id
= cp_parser_template_id (parser
,
12628 /*template_keyword_p=*/false,
12629 /*check_dependency_p=*/true,
12630 /*is_declaration=*/true);
12631 /* If that didn't work, it could still be an identifier. */
12632 if (!cp_parser_parse_definitely (parser
))
12634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12635 id
= cp_parser_identifier (parser
);
12641 template_id_p
= true;
12646 pop_deferring_access_checks ();
12649 cp_parser_check_for_invalid_template_id (parser
, id
);
12651 /* If it's not a `:' or a `{' then we can't really be looking at a
12652 class-head, since a class-head only appears as part of a
12653 class-specifier. We have to detect this situation before calling
12654 xref_tag, since that has irreversible side-effects. */
12655 if (!cp_parser_next_token_starts_class_definition_p (parser
))
12657 cp_parser_error (parser
, "expected %<{%> or %<:%>");
12658 return error_mark_node
;
12661 /* At this point, we're going ahead with the class-specifier, even
12662 if some other problem occurs. */
12663 cp_parser_commit_to_tentative_parse (parser
);
12664 /* Issue the error about the overly-qualified name now. */
12666 cp_parser_error (parser
,
12667 "global qualification of class name is invalid");
12668 else if (invalid_nested_name_p
)
12669 cp_parser_error (parser
,
12670 "qualified name does not name a class");
12671 else if (nested_name_specifier
)
12674 /* Figure out in what scope the declaration is being placed. */
12675 scope
= current_scope ();
12677 scope
= current_namespace
;
12678 /* If that scope does not contain the scope in which the
12679 class was originally declared, the program is invalid. */
12680 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
12682 error ("declaration of %qD in %qD which does not enclose %qD",
12683 type
, scope
, nested_name_specifier
);
12689 A declarator-id shall not be qualified exception of the
12690 definition of a ... nested class outside of its class
12691 ... [or] a the definition or explicit instantiation of a
12692 class member of a namespace outside of its namespace. */
12693 if (scope
== nested_name_specifier
)
12695 pedwarn ("extra qualification ignored");
12696 nested_name_specifier
= NULL_TREE
;
12700 /* An explicit-specialization must be preceded by "template <>". If
12701 it is not, try to recover gracefully. */
12702 if (at_namespace_scope_p ()
12703 && parser
->num_template_parameter_lists
== 0
12706 error ("an explicit specialization must be preceded by %<template <>%>");
12707 invalid_explicit_specialization_p
= true;
12708 /* Take the same action that would have been taken by
12709 cp_parser_explicit_specialization. */
12710 ++parser
->num_template_parameter_lists
;
12711 begin_specialization ();
12713 /* There must be no "return" statements between this point and the
12714 end of this function; set "type "to the correct return value and
12715 use "goto done;" to return. */
12716 /* Make sure that the right number of template parameters were
12718 if (!cp_parser_check_template_parameters (parser
, num_templates
))
12720 /* If something went wrong, there is no point in even trying to
12721 process the class-definition. */
12726 /* Look up the type. */
12729 type
= TREE_TYPE (id
);
12730 maybe_process_partial_specialization (type
);
12732 else if (!nested_name_specifier
)
12734 /* If the class was unnamed, create a dummy name. */
12736 id
= make_anon_name ();
12737 type
= xref_tag (class_key
, id
, /*globalize=*/false,
12738 parser
->num_template_parameter_lists
);
12743 bool pop_p
= false;
12747 template <typename T> struct S { struct T };
12748 template <typename T> struct S<T>::T { };
12750 we will get a TYPENAME_TYPE when processing the definition of
12751 `S::T'. We need to resolve it to the actual type before we
12752 try to define it. */
12753 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
12755 class_type
= resolve_typename_type (TREE_TYPE (type
),
12756 /*only_current_p=*/false);
12757 if (class_type
!= error_mark_node
)
12758 type
= TYPE_NAME (class_type
);
12761 cp_parser_error (parser
, "could not resolve typename type");
12762 type
= error_mark_node
;
12766 maybe_process_partial_specialization (TREE_TYPE (type
));
12767 class_type
= current_class_type
;
12768 /* Enter the scope indicated by the nested-name-specifier. */
12769 if (nested_name_specifier
)
12770 pop_p
= push_scope (nested_name_specifier
);
12771 /* Get the canonical version of this type. */
12772 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
12773 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12774 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
12775 type
= push_template_decl (type
);
12776 type
= TREE_TYPE (type
);
12777 if (nested_name_specifier
)
12779 *nested_name_specifier_p
= true;
12781 pop_scope (nested_name_specifier
);
12784 /* Indicate whether this class was declared as a `class' or as a
12786 if (TREE_CODE (type
) == RECORD_TYPE
)
12787 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
12788 cp_parser_check_class_key (class_key
, type
);
12790 /* Enter the scope containing the class; the names of base classes
12791 should be looked up in that context. For example, given:
12793 struct A { struct B {}; struct C; };
12794 struct A::C : B {};
12797 if (nested_name_specifier
)
12798 pop_p
= push_scope (nested_name_specifier
);
12802 /* Get the list of base-classes, if there is one. */
12803 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12804 bases
= cp_parser_base_clause (parser
);
12806 /* Process the base classes. */
12807 xref_basetypes (type
, bases
);
12809 /* Leave the scope given by the nested-name-specifier. We will
12810 enter the class scope itself while processing the members. */
12812 pop_scope (nested_name_specifier
);
12815 if (invalid_explicit_specialization_p
)
12817 end_specialization ();
12818 --parser
->num_template_parameter_lists
;
12820 *attributes_p
= attributes
;
12824 /* Parse a class-key.
12831 Returns the kind of class-key specified, or none_type to indicate
12834 static enum tag_types
12835 cp_parser_class_key (cp_parser
* parser
)
12838 enum tag_types tag_type
;
12840 /* Look for the class-key. */
12841 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12845 /* Check to see if the TOKEN is a class-key. */
12846 tag_type
= cp_parser_token_is_class_key (token
);
12848 cp_parser_error (parser
, "expected class-key");
12852 /* Parse an (optional) member-specification.
12854 member-specification:
12855 member-declaration member-specification [opt]
12856 access-specifier : member-specification [opt] */
12859 cp_parser_member_specification_opt (cp_parser
* parser
)
12866 /* Peek at the next token. */
12867 token
= cp_lexer_peek_token (parser
->lexer
);
12868 /* If it's a `}', or EOF then we've seen all the members. */
12869 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12872 /* See if this token is a keyword. */
12873 keyword
= token
->keyword
;
12877 case RID_PROTECTED
:
12879 /* Consume the access-specifier. */
12880 cp_lexer_consume_token (parser
->lexer
);
12881 /* Remember which access-specifier is active. */
12882 current_access_specifier
= token
->value
;
12883 /* Look for the `:'. */
12884 cp_parser_require (parser
, CPP_COLON
, "`:'");
12888 /* Accept #pragmas at class scope. */
12889 if (token
->type
== CPP_PRAGMA
)
12891 cp_lexer_handle_pragma (parser
->lexer
);
12895 /* Otherwise, the next construction must be a
12896 member-declaration. */
12897 cp_parser_member_declaration (parser
);
12902 /* Parse a member-declaration.
12904 member-declaration:
12905 decl-specifier-seq [opt] member-declarator-list [opt] ;
12906 function-definition ; [opt]
12907 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12909 template-declaration
12911 member-declarator-list:
12913 member-declarator-list , member-declarator
12916 declarator pure-specifier [opt]
12917 declarator constant-initializer [opt]
12918 identifier [opt] : constant-expression
12922 member-declaration:
12923 __extension__ member-declaration
12926 declarator attributes [opt] pure-specifier [opt]
12927 declarator attributes [opt] constant-initializer [opt]
12928 identifier [opt] attributes [opt] : constant-expression */
12931 cp_parser_member_declaration (cp_parser
* parser
)
12933 cp_decl_specifier_seq decl_specifiers
;
12934 tree prefix_attributes
;
12936 int declares_class_or_enum
;
12939 int saved_pedantic
;
12941 /* Check for the `__extension__' keyword. */
12942 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12945 cp_parser_member_declaration (parser
);
12946 /* Restore the old value of the PEDANTIC flag. */
12947 pedantic
= saved_pedantic
;
12952 /* Check for a template-declaration. */
12953 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12955 /* Parse the template-declaration. */
12956 cp_parser_template_declaration (parser
, /*member_p=*/true);
12961 /* Check for a using-declaration. */
12962 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
12964 /* Parse the using-declaration. */
12965 cp_parser_using_declaration (parser
);
12970 /* Parse the decl-specifier-seq. */
12971 cp_parser_decl_specifier_seq (parser
,
12972 CP_PARSER_FLAGS_OPTIONAL
,
12974 &declares_class_or_enum
);
12975 prefix_attributes
= decl_specifiers
.attributes
;
12976 decl_specifiers
.attributes
= NULL_TREE
;
12977 /* Check for an invalid type-name. */
12978 if (!decl_specifiers
.type
12979 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12981 /* If there is no declarator, then the decl-specifier-seq should
12983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12985 /* If there was no decl-specifier-seq, and the next token is a
12986 `;', then we have something like:
12992 Each member-declaration shall declare at least one member
12993 name of the class. */
12994 if (!decl_specifiers
.any_specifiers_p
)
12996 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12997 if (pedantic
&& !token
->in_system_header
)
12998 pedwarn ("%Hextra %<;%>", &token
->location
);
13004 /* See if this declaration is a friend. */
13005 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13006 /* If there were decl-specifiers, check to see if there was
13007 a class-declaration. */
13008 type
= check_tag_decl (&decl_specifiers
);
13009 /* Nested classes have already been added to the class, but
13010 a `friend' needs to be explicitly registered. */
13013 /* If the `friend' keyword was present, the friend must
13014 be introduced with a class-key. */
13015 if (!declares_class_or_enum
)
13016 error ("a class-key must be used when declaring a friend");
13019 template <typename T> struct A {
13020 friend struct A<T>::B;
13023 A<T>::B will be represented by a TYPENAME_TYPE, and
13024 therefore not recognized by check_tag_decl. */
13026 && decl_specifiers
.type
13027 && TYPE_P (decl_specifiers
.type
))
13028 type
= decl_specifiers
.type
;
13029 if (!type
|| !TYPE_P (type
))
13030 error ("friend declaration does not name a class or "
13033 make_friend_class (current_class_type
, type
,
13034 /*complain=*/true);
13036 /* If there is no TYPE, an error message will already have
13038 else if (!type
|| type
== error_mark_node
)
13040 /* An anonymous aggregate has to be handled specially; such
13041 a declaration really declares a data member (with a
13042 particular type), as opposed to a nested class. */
13043 else if (ANON_AGGR_TYPE_P (type
))
13045 /* Remove constructors and such from TYPE, now that we
13046 know it is an anonymous aggregate. */
13047 fixup_anonymous_aggr (type
);
13048 /* And make the corresponding data member. */
13049 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13050 /* Add it to the class. */
13051 finish_member_declaration (decl
);
13054 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13059 /* See if these declarations will be friends. */
13060 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13062 /* Keep going until we hit the `;' at the end of the
13064 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13066 tree attributes
= NULL_TREE
;
13067 tree first_attribute
;
13069 /* Peek at the next token. */
13070 token
= cp_lexer_peek_token (parser
->lexer
);
13072 /* Check for a bitfield declaration. */
13073 if (token
->type
== CPP_COLON
13074 || (token
->type
== CPP_NAME
13075 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13081 /* Get the name of the bitfield. Note that we cannot just
13082 check TOKEN here because it may have been invalidated by
13083 the call to cp_lexer_peek_nth_token above. */
13084 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13085 identifier
= cp_parser_identifier (parser
);
13087 identifier
= NULL_TREE
;
13089 /* Consume the `:' token. */
13090 cp_lexer_consume_token (parser
->lexer
);
13091 /* Get the width of the bitfield. */
13093 = cp_parser_constant_expression (parser
,
13094 /*allow_non_constant=*/false,
13097 /* Look for attributes that apply to the bitfield. */
13098 attributes
= cp_parser_attributes_opt (parser
);
13099 /* Remember which attributes are prefix attributes and
13101 first_attribute
= attributes
;
13102 /* Combine the attributes. */
13103 attributes
= chainon (prefix_attributes
, attributes
);
13105 /* Create the bitfield declaration. */
13106 decl
= grokbitfield (identifier
13107 ? make_id_declarator (identifier
)
13111 /* Apply the attributes. */
13112 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13116 cp_declarator
*declarator
;
13118 tree asm_specification
;
13119 int ctor_dtor_or_conv_p
;
13121 /* Parse the declarator. */
13123 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13124 &ctor_dtor_or_conv_p
,
13125 /*parenthesized_p=*/NULL
,
13126 /*member_p=*/true);
13128 /* If something went wrong parsing the declarator, make sure
13129 that we at least consume some tokens. */
13130 if (declarator
== cp_error_declarator
)
13132 /* Skip to the end of the statement. */
13133 cp_parser_skip_to_end_of_statement (parser
);
13134 /* If the next token is not a semicolon, that is
13135 probably because we just skipped over the body of
13136 a function. So, we consume a semicolon if
13137 present, but do not issue an error message if it
13139 if (cp_lexer_next_token_is (parser
->lexer
,
13141 cp_lexer_consume_token (parser
->lexer
);
13145 cp_parser_check_for_definition_in_return_type
13146 (declarator
, declares_class_or_enum
);
13148 /* Look for an asm-specification. */
13149 asm_specification
= cp_parser_asm_specification_opt (parser
);
13150 /* Look for attributes that apply to the declaration. */
13151 attributes
= cp_parser_attributes_opt (parser
);
13152 /* Remember which attributes are prefix attributes and
13154 first_attribute
= attributes
;
13155 /* Combine the attributes. */
13156 attributes
= chainon (prefix_attributes
, attributes
);
13158 /* If it's an `=', then we have a constant-initializer or a
13159 pure-specifier. It is not correct to parse the
13160 initializer before registering the member declaration
13161 since the member declaration should be in scope while
13162 its initializer is processed. However, the rest of the
13163 front end does not yet provide an interface that allows
13164 us to handle this correctly. */
13165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13169 A pure-specifier shall be used only in the declaration of
13170 a virtual function.
13172 A member-declarator can contain a constant-initializer
13173 only if it declares a static member of integral or
13176 Therefore, if the DECLARATOR is for a function, we look
13177 for a pure-specifier; otherwise, we look for a
13178 constant-initializer. When we call `grokfield', it will
13179 perform more stringent semantics checks. */
13180 if (declarator
->kind
== cdk_function
)
13181 initializer
= cp_parser_pure_specifier (parser
);
13183 /* Parse the initializer. */
13184 initializer
= cp_parser_constant_initializer (parser
);
13186 /* Otherwise, there is no initializer. */
13188 initializer
= NULL_TREE
;
13190 /* See if we are probably looking at a function
13191 definition. We are certainly not looking at at a
13192 member-declarator. Calling `grokfield' has
13193 side-effects, so we must not do it unless we are sure
13194 that we are looking at a member-declarator. */
13195 if (cp_parser_token_starts_function_definition_p
13196 (cp_lexer_peek_token (parser
->lexer
)))
13198 /* The grammar does not allow a pure-specifier to be
13199 used when a member function is defined. (It is
13200 possible that this fact is an oversight in the
13201 standard, since a pure function may be defined
13202 outside of the class-specifier. */
13204 error ("pure-specifier on function-definition");
13205 decl
= cp_parser_save_member_function_body (parser
,
13209 /* If the member was not a friend, declare it here. */
13211 finish_member_declaration (decl
);
13212 /* Peek at the next token. */
13213 token
= cp_lexer_peek_token (parser
->lexer
);
13214 /* If the next token is a semicolon, consume it. */
13215 if (token
->type
== CPP_SEMICOLON
)
13216 cp_lexer_consume_token (parser
->lexer
);
13221 /* Create the declaration. */
13222 decl
= grokfield (declarator
, &decl_specifiers
,
13223 initializer
, asm_specification
,
13225 /* Any initialization must have been from a
13226 constant-expression. */
13227 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
13228 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
13232 /* Reset PREFIX_ATTRIBUTES. */
13233 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13234 attributes
= TREE_CHAIN (attributes
);
13236 TREE_CHAIN (attributes
) = NULL_TREE
;
13238 /* If there is any qualification still in effect, clear it
13239 now; we will be starting fresh with the next declarator. */
13240 parser
->scope
= NULL_TREE
;
13241 parser
->qualifying_scope
= NULL_TREE
;
13242 parser
->object_scope
= NULL_TREE
;
13243 /* If it's a `,', then there are more declarators. */
13244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13245 cp_lexer_consume_token (parser
->lexer
);
13246 /* If the next token isn't a `;', then we have a parse error. */
13247 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13250 cp_parser_error (parser
, "expected %<;%>");
13251 /* Skip tokens until we find a `;'. */
13252 cp_parser_skip_to_end_of_statement (parser
);
13259 /* Add DECL to the list of members. */
13261 finish_member_declaration (decl
);
13263 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13264 cp_parser_save_default_args (parser
, decl
);
13269 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13272 /* Parse a pure-specifier.
13277 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13278 Otherwise, ERROR_MARK_NODE is returned. */
13281 cp_parser_pure_specifier (cp_parser
* parser
)
13285 /* Look for the `=' token. */
13286 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13287 return error_mark_node
;
13288 /* Look for the `0' token. */
13289 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
13290 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13291 to get information from the lexer about how the number was
13292 spelled in order to fix this problem. */
13293 if (!token
|| !integer_zerop (token
->value
))
13294 return error_mark_node
;
13296 return integer_zero_node
;
13299 /* Parse a constant-initializer.
13301 constant-initializer:
13302 = constant-expression
13304 Returns a representation of the constant-expression. */
13307 cp_parser_constant_initializer (cp_parser
* parser
)
13309 /* Look for the `=' token. */
13310 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13311 return error_mark_node
;
13313 /* It is invalid to write:
13315 struct S { static const int i = { 7 }; };
13318 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13320 cp_parser_error (parser
,
13321 "a brace-enclosed initializer is not allowed here");
13322 /* Consume the opening brace. */
13323 cp_lexer_consume_token (parser
->lexer
);
13324 /* Skip the initializer. */
13325 cp_parser_skip_to_closing_brace (parser
);
13326 /* Look for the trailing `}'. */
13327 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13329 return error_mark_node
;
13332 return cp_parser_constant_expression (parser
,
13333 /*allow_non_constant=*/false,
13337 /* Derived classes [gram.class.derived] */
13339 /* Parse a base-clause.
13342 : base-specifier-list
13344 base-specifier-list:
13346 base-specifier-list , base-specifier
13348 Returns a TREE_LIST representing the base-classes, in the order in
13349 which they were declared. The representation of each node is as
13350 described by cp_parser_base_specifier.
13352 In the case that no bases are specified, this function will return
13353 NULL_TREE, not ERROR_MARK_NODE. */
13356 cp_parser_base_clause (cp_parser
* parser
)
13358 tree bases
= NULL_TREE
;
13360 /* Look for the `:' that begins the list. */
13361 cp_parser_require (parser
, CPP_COLON
, "`:'");
13363 /* Scan the base-specifier-list. */
13369 /* Look for the base-specifier. */
13370 base
= cp_parser_base_specifier (parser
);
13371 /* Add BASE to the front of the list. */
13372 if (base
!= error_mark_node
)
13374 TREE_CHAIN (base
) = bases
;
13377 /* Peek at the next token. */
13378 token
= cp_lexer_peek_token (parser
->lexer
);
13379 /* If it's not a comma, then the list is complete. */
13380 if (token
->type
!= CPP_COMMA
)
13382 /* Consume the `,'. */
13383 cp_lexer_consume_token (parser
->lexer
);
13386 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13387 base class had a qualified name. However, the next name that
13388 appears is certainly not qualified. */
13389 parser
->scope
= NULL_TREE
;
13390 parser
->qualifying_scope
= NULL_TREE
;
13391 parser
->object_scope
= NULL_TREE
;
13393 return nreverse (bases
);
13396 /* Parse a base-specifier.
13399 :: [opt] nested-name-specifier [opt] class-name
13400 virtual access-specifier [opt] :: [opt] nested-name-specifier
13402 access-specifier virtual [opt] :: [opt] nested-name-specifier
13405 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13406 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13407 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13408 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13411 cp_parser_base_specifier (cp_parser
* parser
)
13415 bool virtual_p
= false;
13416 bool duplicate_virtual_error_issued_p
= false;
13417 bool duplicate_access_error_issued_p
= false;
13418 bool class_scope_p
, template_p
;
13419 tree access
= access_default_node
;
13422 /* Process the optional `virtual' and `access-specifier'. */
13425 /* Peek at the next token. */
13426 token
= cp_lexer_peek_token (parser
->lexer
);
13427 /* Process `virtual'. */
13428 switch (token
->keyword
)
13431 /* If `virtual' appears more than once, issue an error. */
13432 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
13434 cp_parser_error (parser
,
13435 "%<virtual%> specified more than once in base-specified");
13436 duplicate_virtual_error_issued_p
= true;
13441 /* Consume the `virtual' token. */
13442 cp_lexer_consume_token (parser
->lexer
);
13447 case RID_PROTECTED
:
13449 /* If more than one access specifier appears, issue an
13451 if (access
!= access_default_node
13452 && !duplicate_access_error_issued_p
)
13454 cp_parser_error (parser
,
13455 "more than one access specifier in base-specified");
13456 duplicate_access_error_issued_p
= true;
13459 access
= ridpointers
[(int) token
->keyword
];
13461 /* Consume the access-specifier. */
13462 cp_lexer_consume_token (parser
->lexer
);
13471 /* It is not uncommon to see programs mechanically, erroneously, use
13472 the 'typename' keyword to denote (dependent) qualified types
13473 as base classes. */
13474 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13476 if (!processing_template_decl
)
13477 error ("keyword %<typename%> not allowed outside of templates");
13479 error ("keyword %<typename%> not allowed in this context "
13480 "(the base class is implicitly a type)");
13481 cp_lexer_consume_token (parser
->lexer
);
13484 /* Look for the optional `::' operator. */
13485 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13486 /* Look for the nested-name-specifier. The simplest way to
13491 The keyword `typename' is not permitted in a base-specifier or
13492 mem-initializer; in these contexts a qualified name that
13493 depends on a template-parameter is implicitly assumed to be a
13496 is to pretend that we have seen the `typename' keyword at this
13498 cp_parser_nested_name_specifier_opt (parser
,
13499 /*typename_keyword_p=*/true,
13500 /*check_dependency_p=*/true,
13502 /*is_declaration=*/true);
13503 /* If the base class is given by a qualified name, assume that names
13504 we see are type names or templates, as appropriate. */
13505 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
13506 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
13508 /* Finally, look for the class-name. */
13509 type
= cp_parser_class_name (parser
,
13513 /*check_dependency_p=*/true,
13514 /*class_head_p=*/false,
13515 /*is_declaration=*/true);
13517 if (type
== error_mark_node
)
13518 return error_mark_node
;
13520 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
13523 /* Exception handling [gram.exception] */
13525 /* Parse an (optional) exception-specification.
13527 exception-specification:
13528 throw ( type-id-list [opt] )
13530 Returns a TREE_LIST representing the exception-specification. The
13531 TREE_VALUE of each node is a type. */
13534 cp_parser_exception_specification_opt (cp_parser
* parser
)
13539 /* Peek at the next token. */
13540 token
= cp_lexer_peek_token (parser
->lexer
);
13541 /* If it's not `throw', then there's no exception-specification. */
13542 if (!cp_parser_is_keyword (token
, RID_THROW
))
13545 /* Consume the `throw'. */
13546 cp_lexer_consume_token (parser
->lexer
);
13548 /* Look for the `('. */
13549 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13551 /* Peek at the next token. */
13552 token
= cp_lexer_peek_token (parser
->lexer
);
13553 /* If it's not a `)', then there is a type-id-list. */
13554 if (token
->type
!= CPP_CLOSE_PAREN
)
13556 const char *saved_message
;
13558 /* Types may not be defined in an exception-specification. */
13559 saved_message
= parser
->type_definition_forbidden_message
;
13560 parser
->type_definition_forbidden_message
13561 = "types may not be defined in an exception-specification";
13562 /* Parse the type-id-list. */
13563 type_id_list
= cp_parser_type_id_list (parser
);
13564 /* Restore the saved message. */
13565 parser
->type_definition_forbidden_message
= saved_message
;
13568 type_id_list
= empty_except_spec
;
13570 /* Look for the `)'. */
13571 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13573 return type_id_list
;
13576 /* Parse an (optional) type-id-list.
13580 type-id-list , type-id
13582 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13583 in the order that the types were presented. */
13586 cp_parser_type_id_list (cp_parser
* parser
)
13588 tree types
= NULL_TREE
;
13595 /* Get the next type-id. */
13596 type
= cp_parser_type_id (parser
);
13597 /* Add it to the list. */
13598 types
= add_exception_specifier (types
, type
, /*complain=*/1);
13599 /* Peek at the next token. */
13600 token
= cp_lexer_peek_token (parser
->lexer
);
13601 /* If it is not a `,', we are done. */
13602 if (token
->type
!= CPP_COMMA
)
13604 /* Consume the `,'. */
13605 cp_lexer_consume_token (parser
->lexer
);
13608 return nreverse (types
);
13611 /* Parse a try-block.
13614 try compound-statement handler-seq */
13617 cp_parser_try_block (cp_parser
* parser
)
13621 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
13622 try_block
= begin_try_block ();
13623 cp_parser_compound_statement (parser
, NULL
, true);
13624 finish_try_block (try_block
);
13625 cp_parser_handler_seq (parser
);
13626 finish_handler_sequence (try_block
);
13631 /* Parse a function-try-block.
13633 function-try-block:
13634 try ctor-initializer [opt] function-body handler-seq */
13637 cp_parser_function_try_block (cp_parser
* parser
)
13640 bool ctor_initializer_p
;
13642 /* Look for the `try' keyword. */
13643 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
13645 /* Let the rest of the front-end know where we are. */
13646 try_block
= begin_function_try_block ();
13647 /* Parse the function-body. */
13649 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13650 /* We're done with the `try' part. */
13651 finish_function_try_block (try_block
);
13652 /* Parse the handlers. */
13653 cp_parser_handler_seq (parser
);
13654 /* We're done with the handlers. */
13655 finish_function_handler_sequence (try_block
);
13657 return ctor_initializer_p
;
13660 /* Parse a handler-seq.
13663 handler handler-seq [opt] */
13666 cp_parser_handler_seq (cp_parser
* parser
)
13672 /* Parse the handler. */
13673 cp_parser_handler (parser
);
13674 /* Peek at the next token. */
13675 token
= cp_lexer_peek_token (parser
->lexer
);
13676 /* If it's not `catch' then there are no more handlers. */
13677 if (!cp_parser_is_keyword (token
, RID_CATCH
))
13682 /* Parse a handler.
13685 catch ( exception-declaration ) compound-statement */
13688 cp_parser_handler (cp_parser
* parser
)
13693 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
13694 handler
= begin_handler ();
13695 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13696 declaration
= cp_parser_exception_declaration (parser
);
13697 finish_handler_parms (declaration
, handler
);
13698 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13699 cp_parser_compound_statement (parser
, NULL
, false);
13700 finish_handler (handler
);
13703 /* Parse an exception-declaration.
13705 exception-declaration:
13706 type-specifier-seq declarator
13707 type-specifier-seq abstract-declarator
13711 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13712 ellipsis variant is used. */
13715 cp_parser_exception_declaration (cp_parser
* parser
)
13718 cp_decl_specifier_seq type_specifiers
;
13719 cp_declarator
*declarator
;
13720 const char *saved_message
;
13722 /* If it's an ellipsis, it's easy to handle. */
13723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13725 /* Consume the `...' token. */
13726 cp_lexer_consume_token (parser
->lexer
);
13730 /* Types may not be defined in exception-declarations. */
13731 saved_message
= parser
->type_definition_forbidden_message
;
13732 parser
->type_definition_forbidden_message
13733 = "types may not be defined in exception-declarations";
13735 /* Parse the type-specifier-seq. */
13736 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
13737 /* If it's a `)', then there is no declarator. */
13738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
13741 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
13742 /*ctor_dtor_or_conv_p=*/NULL
,
13743 /*parenthesized_p=*/NULL
,
13744 /*member_p=*/false);
13746 /* Restore the saved message. */
13747 parser
->type_definition_forbidden_message
= saved_message
;
13749 if (type_specifiers
.any_specifiers_p
)
13751 decl
= grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
13752 if (decl
== NULL_TREE
)
13753 error ("invalid catch parameter");
13761 /* Parse a throw-expression.
13764 throw assignment-expression [opt]
13766 Returns a THROW_EXPR representing the throw-expression. */
13769 cp_parser_throw_expression (cp_parser
* parser
)
13774 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
13775 token
= cp_lexer_peek_token (parser
->lexer
);
13776 /* Figure out whether or not there is an assignment-expression
13777 following the "throw" keyword. */
13778 if (token
->type
== CPP_COMMA
13779 || token
->type
== CPP_SEMICOLON
13780 || token
->type
== CPP_CLOSE_PAREN
13781 || token
->type
== CPP_CLOSE_SQUARE
13782 || token
->type
== CPP_CLOSE_BRACE
13783 || token
->type
== CPP_COLON
)
13784 expression
= NULL_TREE
;
13786 expression
= cp_parser_assignment_expression (parser
);
13788 return build_throw (expression
);
13791 /* GNU Extensions */
13793 /* Parse an (optional) asm-specification.
13796 asm ( string-literal )
13798 If the asm-specification is present, returns a STRING_CST
13799 corresponding to the string-literal. Otherwise, returns
13803 cp_parser_asm_specification_opt (cp_parser
* parser
)
13806 tree asm_specification
;
13808 /* Peek at the next token. */
13809 token
= cp_lexer_peek_token (parser
->lexer
);
13810 /* If the next token isn't the `asm' keyword, then there's no
13811 asm-specification. */
13812 if (!cp_parser_is_keyword (token
, RID_ASM
))
13815 /* Consume the `asm' token. */
13816 cp_lexer_consume_token (parser
->lexer
);
13817 /* Look for the `('. */
13818 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13820 /* Look for the string-literal. */
13821 asm_specification
= cp_parser_string_literal (parser
, false, false);
13823 /* Look for the `)'. */
13824 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13826 return asm_specification
;
13829 /* Parse an asm-operand-list.
13833 asm-operand-list , asm-operand
13836 string-literal ( expression )
13837 [ string-literal ] string-literal ( expression )
13839 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13840 each node is the expression. The TREE_PURPOSE is itself a
13841 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13842 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13843 is a STRING_CST for the string literal before the parenthesis. */
13846 cp_parser_asm_operand_list (cp_parser
* parser
)
13848 tree asm_operands
= NULL_TREE
;
13852 tree string_literal
;
13856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13858 /* Consume the `[' token. */
13859 cp_lexer_consume_token (parser
->lexer
);
13860 /* Read the operand name. */
13861 name
= cp_parser_identifier (parser
);
13862 if (name
!= error_mark_node
)
13863 name
= build_string (IDENTIFIER_LENGTH (name
),
13864 IDENTIFIER_POINTER (name
));
13865 /* Look for the closing `]'. */
13866 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13870 /* Look for the string-literal. */
13871 string_literal
= cp_parser_string_literal (parser
, false, false);
13873 /* Look for the `('. */
13874 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13875 /* Parse the expression. */
13876 expression
= cp_parser_expression (parser
);
13877 /* Look for the `)'. */
13878 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13880 /* Add this operand to the list. */
13881 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
13884 /* If the next token is not a `,', there are no more
13886 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13888 /* Consume the `,'. */
13889 cp_lexer_consume_token (parser
->lexer
);
13892 return nreverse (asm_operands
);
13895 /* Parse an asm-clobber-list.
13899 asm-clobber-list , string-literal
13901 Returns a TREE_LIST, indicating the clobbers in the order that they
13902 appeared. The TREE_VALUE of each node is a STRING_CST. */
13905 cp_parser_asm_clobber_list (cp_parser
* parser
)
13907 tree clobbers
= NULL_TREE
;
13911 tree string_literal
;
13913 /* Look for the string literal. */
13914 string_literal
= cp_parser_string_literal (parser
, false, false);
13915 /* Add it to the list. */
13916 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13917 /* If the next token is not a `,', then the list is
13919 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13921 /* Consume the `,' token. */
13922 cp_lexer_consume_token (parser
->lexer
);
13928 /* Parse an (optional) series of attributes.
13931 attributes attribute
13934 __attribute__ (( attribute-list [opt] ))
13936 The return value is as for cp_parser_attribute_list. */
13939 cp_parser_attributes_opt (cp_parser
* parser
)
13941 tree attributes
= NULL_TREE
;
13946 tree attribute_list
;
13948 /* Peek at the next token. */
13949 token
= cp_lexer_peek_token (parser
->lexer
);
13950 /* If it's not `__attribute__', then we're done. */
13951 if (token
->keyword
!= RID_ATTRIBUTE
)
13954 /* Consume the `__attribute__' keyword. */
13955 cp_lexer_consume_token (parser
->lexer
);
13956 /* Look for the two `(' tokens. */
13957 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13958 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13960 /* Peek at the next token. */
13961 token
= cp_lexer_peek_token (parser
->lexer
);
13962 if (token
->type
!= CPP_CLOSE_PAREN
)
13963 /* Parse the attribute-list. */
13964 attribute_list
= cp_parser_attribute_list (parser
);
13966 /* If the next token is a `)', then there is no attribute
13968 attribute_list
= NULL
;
13970 /* Look for the two `)' tokens. */
13971 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13972 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13974 /* Add these new attributes to the list. */
13975 attributes
= chainon (attributes
, attribute_list
);
13981 /* Parse an attribute-list.
13985 attribute-list , attribute
13989 identifier ( identifier )
13990 identifier ( identifier , expression-list )
13991 identifier ( expression-list )
13993 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13994 TREE_PURPOSE of each node is the identifier indicating which
13995 attribute is in use. The TREE_VALUE represents the arguments, if
13999 cp_parser_attribute_list (cp_parser
* parser
)
14001 tree attribute_list
= NULL_TREE
;
14002 bool save_translate_strings_p
= parser
->translate_strings_p
;
14004 parser
->translate_strings_p
= false;
14011 /* Look for the identifier. We also allow keywords here; for
14012 example `__attribute__ ((const))' is legal. */
14013 token
= cp_lexer_peek_token (parser
->lexer
);
14014 if (token
->type
!= CPP_NAME
14015 && token
->type
!= CPP_KEYWORD
)
14016 return error_mark_node
;
14017 /* Consume the token. */
14018 token
= cp_lexer_consume_token (parser
->lexer
);
14020 /* Save away the identifier that indicates which attribute this is. */
14021 identifier
= token
->value
;
14022 attribute
= build_tree_list (identifier
, NULL_TREE
);
14024 /* Peek at the next token. */
14025 token
= cp_lexer_peek_token (parser
->lexer
);
14026 /* If it's an `(', then parse the attribute arguments. */
14027 if (token
->type
== CPP_OPEN_PAREN
)
14031 arguments
= (cp_parser_parenthesized_expression_list
14032 (parser
, true, /*non_constant_p=*/NULL
));
14033 /* Save the identifier and arguments away. */
14034 TREE_VALUE (attribute
) = arguments
;
14037 /* Add this attribute to the list. */
14038 TREE_CHAIN (attribute
) = attribute_list
;
14039 attribute_list
= attribute
;
14041 /* Now, look for more attributes. */
14042 token
= cp_lexer_peek_token (parser
->lexer
);
14043 /* If the next token isn't a `,', we're done. */
14044 if (token
->type
!= CPP_COMMA
)
14047 /* Consume the comma and keep going. */
14048 cp_lexer_consume_token (parser
->lexer
);
14050 parser
->translate_strings_p
= save_translate_strings_p
;
14052 /* We built up the list in reverse order. */
14053 return nreverse (attribute_list
);
14056 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14057 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14058 current value of the PEDANTIC flag, regardless of whether or not
14059 the `__extension__' keyword is present. The caller is responsible
14060 for restoring the value of the PEDANTIC flag. */
14063 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14065 /* Save the old value of the PEDANTIC flag. */
14066 *saved_pedantic
= pedantic
;
14068 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14070 /* Consume the `__extension__' token. */
14071 cp_lexer_consume_token (parser
->lexer
);
14072 /* We're not being pedantic while the `__extension__' keyword is
14082 /* Parse a label declaration.
14085 __label__ label-declarator-seq ;
14087 label-declarator-seq:
14088 identifier , label-declarator-seq
14092 cp_parser_label_declaration (cp_parser
* parser
)
14094 /* Look for the `__label__' keyword. */
14095 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14101 /* Look for an identifier. */
14102 identifier
= cp_parser_identifier (parser
);
14103 /* Declare it as a lobel. */
14104 finish_label_decl (identifier
);
14105 /* If the next token is a `;', stop. */
14106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14108 /* Look for the `,' separating the label declarations. */
14109 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14112 /* Look for the final `;'. */
14113 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14116 /* Support Functions */
14118 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14119 NAME should have one of the representations used for an
14120 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14121 is returned. If PARSER->SCOPE is a dependent type, then a
14122 SCOPE_REF is returned.
14124 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14125 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14126 was formed. Abstractly, such entities should not be passed to this
14127 function, because they do not need to be looked up, but it is
14128 simpler to check for this special case here, rather than at the
14131 In cases not explicitly covered above, this function returns a
14132 DECL, OVERLOAD, or baselink representing the result of the lookup.
14133 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14136 If IS_TYPE is TRUE, bindings that do not refer to types are
14139 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14142 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14145 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14148 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14149 results in an ambiguity, and false otherwise. */
14152 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14153 bool is_type
, bool is_template
, bool is_namespace
,
14154 bool check_dependency
,
14158 tree object_type
= parser
->context
->object_type
;
14160 /* Assume that the lookup will be unambiguous. */
14162 *ambiguous_p
= false;
14164 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14165 no longer valid. Note that if we are parsing tentatively, and
14166 the parse fails, OBJECT_TYPE will be automatically restored. */
14167 parser
->context
->object_type
= NULL_TREE
;
14169 if (name
== error_mark_node
)
14170 return error_mark_node
;
14172 /* A template-id has already been resolved; there is no lookup to
14174 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14176 if (BASELINK_P (name
))
14178 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14179 == TEMPLATE_ID_EXPR
);
14183 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14184 it should already have been checked to make sure that the name
14185 used matches the type being destroyed. */
14186 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14190 /* Figure out to which type this destructor applies. */
14192 type
= parser
->scope
;
14193 else if (object_type
)
14194 type
= object_type
;
14196 type
= current_class_type
;
14197 /* If that's not a class type, there is no destructor. */
14198 if (!type
|| !CLASS_TYPE_P (type
))
14199 return error_mark_node
;
14200 if (!CLASSTYPE_DESTRUCTORS (type
))
14201 return error_mark_node
;
14202 /* If it was a class type, return the destructor. */
14203 return CLASSTYPE_DESTRUCTORS (type
);
14206 /* By this point, the NAME should be an ordinary identifier. If
14207 the id-expression was a qualified name, the qualifying scope is
14208 stored in PARSER->SCOPE at this point. */
14209 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14211 /* Perform the lookup. */
14216 if (parser
->scope
== error_mark_node
)
14217 return error_mark_node
;
14219 /* If the SCOPE is dependent, the lookup must be deferred until
14220 the template is instantiated -- unless we are explicitly
14221 looking up names in uninstantiated templates. Even then, we
14222 cannot look up the name if the scope is not a class type; it
14223 might, for example, be a template type parameter. */
14224 dependent_p
= (TYPE_P (parser
->scope
)
14225 && !(parser
->in_declarator_p
14226 && currently_open_class (parser
->scope
))
14227 && dependent_type_p (parser
->scope
));
14228 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14232 /* The resolution to Core Issue 180 says that `struct A::B'
14233 should be considered a type-name, even if `A' is
14235 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
14238 else if (is_template
)
14239 decl
= make_unbound_class_template (parser
->scope
,
14243 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
14247 bool pop_p
= false;
14249 /* If PARSER->SCOPE is a dependent type, then it must be a
14250 class type, and we must not be checking dependencies;
14251 otherwise, we would have processed this lookup above. So
14252 that PARSER->SCOPE is not considered a dependent base by
14253 lookup_member, we must enter the scope here. */
14255 pop_p
= push_scope (parser
->scope
);
14256 /* If the PARSER->SCOPE is a a template specialization, it
14257 may be instantiated during name lookup. In that case,
14258 errors may be issued. Even if we rollback the current
14259 tentative parse, those errors are valid. */
14260 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
14261 /*complain=*/true);
14263 pop_scope (parser
->scope
);
14265 parser
->qualifying_scope
= parser
->scope
;
14266 parser
->object_scope
= NULL_TREE
;
14268 else if (object_type
)
14270 tree object_decl
= NULL_TREE
;
14271 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14272 OBJECT_TYPE is not a class. */
14273 if (CLASS_TYPE_P (object_type
))
14274 /* If the OBJECT_TYPE is a template specialization, it may
14275 be instantiated during name lookup. In that case, errors
14276 may be issued. Even if we rollback the current tentative
14277 parse, those errors are valid. */
14278 object_decl
= lookup_member (object_type
,
14280 /*protect=*/0, is_type
);
14281 /* Look it up in the enclosing context, too. */
14282 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
14283 /*block_p=*/true, is_namespace
,
14285 parser
->object_scope
= object_type
;
14286 parser
->qualifying_scope
= NULL_TREE
;
14288 decl
= object_decl
;
14292 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
14293 /*block_p=*/true, is_namespace
,
14295 parser
->qualifying_scope
= NULL_TREE
;
14296 parser
->object_scope
= NULL_TREE
;
14299 /* If the lookup failed, let our caller know. */
14301 || decl
== error_mark_node
14302 || (TREE_CODE (decl
) == FUNCTION_DECL
14303 && DECL_ANTICIPATED (decl
)))
14304 return error_mark_node
;
14306 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14307 if (TREE_CODE (decl
) == TREE_LIST
)
14310 *ambiguous_p
= true;
14311 /* The error message we have to print is too complicated for
14312 cp_parser_error, so we incorporate its actions directly. */
14313 if (!cp_parser_simulate_error (parser
))
14315 error ("reference to %qD is ambiguous", name
);
14316 print_candidates (decl
);
14318 return error_mark_node
;
14321 gcc_assert (DECL_P (decl
)
14322 || TREE_CODE (decl
) == OVERLOAD
14323 || TREE_CODE (decl
) == SCOPE_REF
14324 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14325 || BASELINK_P (decl
));
14327 /* If we have resolved the name of a member declaration, check to
14328 see if the declaration is accessible. When the name resolves to
14329 set of overloaded functions, accessibility is checked when
14330 overload resolution is done.
14332 During an explicit instantiation, access is not checked at all,
14333 as per [temp.explicit]. */
14335 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
14340 /* Like cp_parser_lookup_name, but for use in the typical case where
14341 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14342 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14345 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
14347 return cp_parser_lookup_name (parser
, name
,
14349 /*is_template=*/false,
14350 /*is_namespace=*/false,
14351 /*check_dependency=*/true,
14352 /*ambiguous_p=*/NULL
);
14355 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14356 the current context, return the TYPE_DECL. If TAG_NAME_P is
14357 true, the DECL indicates the class being defined in a class-head,
14358 or declared in an elaborated-type-specifier.
14360 Otherwise, return DECL. */
14363 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
14365 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14366 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14369 template <typename T> struct B;
14372 template <typename T> struct A::B {};
14374 Similarly, in a elaborated-type-specifier:
14376 namespace N { struct X{}; }
14379 template <typename T> friend struct N::X;
14382 However, if the DECL refers to a class type, and we are in
14383 the scope of the class, then the name lookup automatically
14384 finds the TYPE_DECL created by build_self_reference rather
14385 than a TEMPLATE_DECL. For example, in:
14387 template <class T> struct S {
14391 there is no need to handle such case. */
14393 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
14394 return DECL_TEMPLATE_RESULT (decl
);
14399 /* If too many, or too few, template-parameter lists apply to the
14400 declarator, issue an error message. Returns TRUE if all went well,
14401 and FALSE otherwise. */
14404 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
14405 cp_declarator
*declarator
)
14407 unsigned num_templates
;
14409 /* We haven't seen any classes that involve template parameters yet. */
14412 switch (declarator
->kind
)
14415 if (TREE_CODE (declarator
->u
.id
.name
) == SCOPE_REF
)
14420 scope
= TREE_OPERAND (declarator
->u
.id
.name
, 0);
14421 member
= TREE_OPERAND (declarator
->u
.id
.name
, 1);
14423 while (scope
&& CLASS_TYPE_P (scope
))
14425 /* You're supposed to have one `template <...>'
14426 for every template class, but you don't need one
14427 for a full specialization. For example:
14429 template <class T> struct S{};
14430 template <> struct S<int> { void f(); };
14431 void S<int>::f () {}
14433 is correct; there shouldn't be a `template <>' for
14434 the definition of `S<int>::f'. */
14435 if (CLASSTYPE_TEMPLATE_INFO (scope
)
14436 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
14437 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
14438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
14441 scope
= TYPE_CONTEXT (scope
);
14445 /* If the DECLARATOR has the form `X<y>' then it uses one
14446 additional level of template parameters. */
14447 if (TREE_CODE (declarator
->u
.id
.name
) == TEMPLATE_ID_EXPR
)
14450 return cp_parser_check_template_parameters (parser
,
14456 case cdk_reference
:
14458 return (cp_parser_check_declarator_template_parameters
14459 (parser
, declarator
->declarator
));
14465 gcc_unreachable ();
14470 /* NUM_TEMPLATES were used in the current declaration. If that is
14471 invalid, return FALSE and issue an error messages. Otherwise,
14475 cp_parser_check_template_parameters (cp_parser
* parser
,
14476 unsigned num_templates
)
14478 /* If there are more template classes than parameter lists, we have
14481 template <class T> void S<T>::R<T>::f (); */
14482 if (parser
->num_template_parameter_lists
< num_templates
)
14484 error ("too few template-parameter-lists");
14487 /* If there are the same number of template classes and parameter
14488 lists, that's OK. */
14489 if (parser
->num_template_parameter_lists
== num_templates
)
14491 /* If there are more, but only one more, then we are referring to a
14492 member template. That's OK too. */
14493 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
14495 /* Otherwise, there are too many template parameter lists. We have
14498 template <class T> template <class U> void S::f(); */
14499 error ("too many template-parameter-lists");
14503 /* Parse an optional `::' token indicating that the following name is
14504 from the global namespace. If so, PARSER->SCOPE is set to the
14505 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14506 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14507 Returns the new value of PARSER->SCOPE, if the `::' token is
14508 present, and NULL_TREE otherwise. */
14511 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
14515 /* Peek at the next token. */
14516 token
= cp_lexer_peek_token (parser
->lexer
);
14517 /* If we're looking at a `::' token then we're starting from the
14518 global namespace, not our current location. */
14519 if (token
->type
== CPP_SCOPE
)
14521 /* Consume the `::' token. */
14522 cp_lexer_consume_token (parser
->lexer
);
14523 /* Set the SCOPE so that we know where to start the lookup. */
14524 parser
->scope
= global_namespace
;
14525 parser
->qualifying_scope
= global_namespace
;
14526 parser
->object_scope
= NULL_TREE
;
14528 return parser
->scope
;
14530 else if (!current_scope_valid_p
)
14532 parser
->scope
= NULL_TREE
;
14533 parser
->qualifying_scope
= NULL_TREE
;
14534 parser
->object_scope
= NULL_TREE
;
14540 /* Returns TRUE if the upcoming token sequence is the start of a
14541 constructor declarator. If FRIEND_P is true, the declarator is
14542 preceded by the `friend' specifier. */
14545 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
14547 bool constructor_p
;
14548 tree type_decl
= NULL_TREE
;
14549 bool nested_name_p
;
14550 cp_token
*next_token
;
14552 /* The common case is that this is not a constructor declarator, so
14553 try to avoid doing lots of work if at all possible. It's not
14554 valid declare a constructor at function scope. */
14555 if (at_function_scope_p ())
14557 /* And only certain tokens can begin a constructor declarator. */
14558 next_token
= cp_lexer_peek_token (parser
->lexer
);
14559 if (next_token
->type
!= CPP_NAME
14560 && next_token
->type
!= CPP_SCOPE
14561 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
14562 && next_token
->type
!= CPP_TEMPLATE_ID
)
14565 /* Parse tentatively; we are going to roll back all of the tokens
14567 cp_parser_parse_tentatively (parser
);
14568 /* Assume that we are looking at a constructor declarator. */
14569 constructor_p
= true;
14571 /* Look for the optional `::' operator. */
14572 cp_parser_global_scope_opt (parser
,
14573 /*current_scope_valid_p=*/false);
14574 /* Look for the nested-name-specifier. */
14576 = (cp_parser_nested_name_specifier_opt (parser
,
14577 /*typename_keyword_p=*/false,
14578 /*check_dependency_p=*/false,
14580 /*is_declaration=*/false)
14582 /* Outside of a class-specifier, there must be a
14583 nested-name-specifier. */
14584 if (!nested_name_p
&&
14585 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
14587 constructor_p
= false;
14588 /* If we still think that this might be a constructor-declarator,
14589 look for a class-name. */
14594 template <typename T> struct S { S(); };
14595 template <typename T> S<T>::S ();
14597 we must recognize that the nested `S' names a class.
14600 template <typename T> S<T>::S<T> ();
14602 we must recognize that the nested `S' names a template. */
14603 type_decl
= cp_parser_class_name (parser
,
14604 /*typename_keyword_p=*/false,
14605 /*template_keyword_p=*/false,
14607 /*check_dependency_p=*/false,
14608 /*class_head_p=*/false,
14609 /*is_declaration=*/false);
14610 /* If there was no class-name, then this is not a constructor. */
14611 constructor_p
= !cp_parser_error_occurred (parser
);
14614 /* If we're still considering a constructor, we have to see a `(',
14615 to begin the parameter-declaration-clause, followed by either a
14616 `)', an `...', or a decl-specifier. We need to check for a
14617 type-specifier to avoid being fooled into thinking that:
14621 is a constructor. (It is actually a function named `f' that
14622 takes one parameter (of type `int') and returns a value of type
14625 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14627 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
14628 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
14629 /* A parameter declaration begins with a decl-specifier,
14630 which is either the "attribute" keyword, a storage class
14631 specifier, or (usually) a type-specifier. */
14632 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
14633 && !cp_parser_storage_class_specifier_opt (parser
))
14636 bool pop_p
= false;
14637 unsigned saved_num_template_parameter_lists
;
14639 /* Names appearing in the type-specifier should be looked up
14640 in the scope of the class. */
14641 if (current_class_type
)
14645 type
= TREE_TYPE (type_decl
);
14646 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14648 type
= resolve_typename_type (type
,
14649 /*only_current_p=*/false);
14650 if (type
== error_mark_node
)
14652 cp_parser_abort_tentative_parse (parser
);
14656 pop_p
= push_scope (type
);
14659 /* Inside the constructor parameter list, surrounding
14660 template-parameter-lists do not apply. */
14661 saved_num_template_parameter_lists
14662 = parser
->num_template_parameter_lists
;
14663 parser
->num_template_parameter_lists
= 0;
14665 /* Look for the type-specifier. */
14666 cp_parser_type_specifier (parser
,
14667 CP_PARSER_FLAGS_NONE
,
14668 /*decl_specs=*/NULL
,
14669 /*is_declarator=*/true,
14670 /*declares_class_or_enum=*/NULL
,
14671 /*is_cv_qualifier=*/NULL
);
14673 parser
->num_template_parameter_lists
14674 = saved_num_template_parameter_lists
;
14676 /* Leave the scope of the class. */
14680 constructor_p
= !cp_parser_error_occurred (parser
);
14684 constructor_p
= false;
14685 /* We did not really want to consume any tokens. */
14686 cp_parser_abort_tentative_parse (parser
);
14688 return constructor_p
;
14691 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14692 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14693 they must be performed once we are in the scope of the function.
14695 Returns the function defined. */
14698 cp_parser_function_definition_from_specifiers_and_declarator
14699 (cp_parser
* parser
,
14700 cp_decl_specifier_seq
*decl_specifiers
,
14702 const cp_declarator
*declarator
)
14707 /* Begin the function-definition. */
14708 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
14710 /* The things we're about to see are not directly qualified by any
14711 template headers we've seen thus far. */
14712 reset_specialization ();
14714 /* If there were names looked up in the decl-specifier-seq that we
14715 did not check, check them now. We must wait until we are in the
14716 scope of the function to perform the checks, since the function
14717 might be a friend. */
14718 perform_deferred_access_checks ();
14722 /* Skip the entire function. */
14723 error ("invalid function declaration");
14724 cp_parser_skip_to_end_of_block_or_statement (parser
);
14725 fn
= error_mark_node
;
14728 fn
= cp_parser_function_definition_after_declarator (parser
,
14729 /*inline_p=*/false);
14734 /* Parse the part of a function-definition that follows the
14735 declarator. INLINE_P is TRUE iff this function is an inline
14736 function defined with a class-specifier.
14738 Returns the function defined. */
14741 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
14745 bool ctor_initializer_p
= false;
14746 bool saved_in_unbraced_linkage_specification_p
;
14747 unsigned saved_num_template_parameter_lists
;
14749 /* If the next token is `return', then the code may be trying to
14750 make use of the "named return value" extension that G++ used to
14752 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
14754 /* Consume the `return' keyword. */
14755 cp_lexer_consume_token (parser
->lexer
);
14756 /* Look for the identifier that indicates what value is to be
14758 cp_parser_identifier (parser
);
14759 /* Issue an error message. */
14760 error ("named return values are no longer supported");
14761 /* Skip tokens until we reach the start of the function body. */
14762 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
14763 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
14764 cp_lexer_consume_token (parser
->lexer
);
14766 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14767 anything declared inside `f'. */
14768 saved_in_unbraced_linkage_specification_p
14769 = parser
->in_unbraced_linkage_specification_p
;
14770 parser
->in_unbraced_linkage_specification_p
= false;
14771 /* Inside the function, surrounding template-parameter-lists do not
14773 saved_num_template_parameter_lists
14774 = parser
->num_template_parameter_lists
;
14775 parser
->num_template_parameter_lists
= 0;
14776 /* If the next token is `try', then we are looking at a
14777 function-try-block. */
14778 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14779 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14780 /* A function-try-block includes the function-body, so we only do
14781 this next part if we're not processing a function-try-block. */
14784 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14786 /* Finish the function. */
14787 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14788 (inline_p
? 2 : 0));
14789 /* Generate code for it, if necessary. */
14790 expand_or_defer_fn (fn
);
14791 /* Restore the saved values. */
14792 parser
->in_unbraced_linkage_specification_p
14793 = saved_in_unbraced_linkage_specification_p
;
14794 parser
->num_template_parameter_lists
14795 = saved_num_template_parameter_lists
;
14800 /* Parse a template-declaration, assuming that the `export' (and
14801 `extern') keywords, if present, has already been scanned. MEMBER_P
14802 is as for cp_parser_template_declaration. */
14805 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14807 tree decl
= NULL_TREE
;
14808 tree parameter_list
;
14809 bool friend_p
= false;
14811 /* Look for the `template' keyword. */
14812 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14816 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14819 /* If the next token is `>', then we have an invalid
14820 specialization. Rather than complain about an invalid template
14821 parameter, issue an error message here. */
14822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14824 cp_parser_error (parser
, "invalid explicit specialization");
14825 begin_specialization ();
14826 parameter_list
= NULL_TREE
;
14830 /* Parse the template parameters. */
14831 begin_template_parm_list ();
14832 parameter_list
= cp_parser_template_parameter_list (parser
);
14833 parameter_list
= end_template_parm_list (parameter_list
);
14836 /* Look for the `>'. */
14837 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14838 /* We just processed one more parameter list. */
14839 ++parser
->num_template_parameter_lists
;
14840 /* If the next token is `template', there are more template
14842 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14844 cp_parser_template_declaration_after_export (parser
, member_p
);
14847 /* There are no access checks when parsing a template, as we do not
14848 know if a specialization will be a friend. */
14849 push_deferring_access_checks (dk_no_check
);
14851 decl
= cp_parser_single_declaration (parser
,
14855 pop_deferring_access_checks ();
14857 /* If this is a member template declaration, let the front
14859 if (member_p
&& !friend_p
&& decl
)
14861 if (TREE_CODE (decl
) == TYPE_DECL
)
14862 cp_parser_check_access_in_redeclaration (decl
);
14864 decl
= finish_member_template_decl (decl
);
14866 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14867 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14868 /*complain=*/true);
14870 /* We are done with the current parameter list. */
14871 --parser
->num_template_parameter_lists
;
14874 finish_template_decl (parameter_list
);
14876 /* Register member declarations. */
14877 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14878 finish_member_declaration (decl
);
14880 /* If DECL is a function template, we must return to parse it later.
14881 (Even though there is no definition, there might be default
14882 arguments that need handling.) */
14883 if (member_p
&& decl
14884 && (TREE_CODE (decl
) == FUNCTION_DECL
14885 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14886 TREE_VALUE (parser
->unparsed_functions_queues
)
14887 = tree_cons (NULL_TREE
, decl
,
14888 TREE_VALUE (parser
->unparsed_functions_queues
));
14891 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14892 `function-definition' sequence. MEMBER_P is true, this declaration
14893 appears in a class scope.
14895 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14896 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14899 cp_parser_single_declaration (cp_parser
* parser
,
14903 int declares_class_or_enum
;
14904 tree decl
= NULL_TREE
;
14905 cp_decl_specifier_seq decl_specifiers
;
14906 bool function_definition_p
= false;
14908 /* Defer access checks until we know what is being declared. */
14909 push_deferring_access_checks (dk_deferred
);
14911 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14913 cp_parser_decl_specifier_seq (parser
,
14914 CP_PARSER_FLAGS_OPTIONAL
,
14916 &declares_class_or_enum
);
14918 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
14919 /* Gather up the access checks that occurred the
14920 decl-specifier-seq. */
14921 stop_deferring_access_checks ();
14923 /* Check for the declaration of a template class. */
14924 if (declares_class_or_enum
)
14926 if (cp_parser_declares_only_class_p (parser
))
14928 decl
= shadow_tag (&decl_specifiers
);
14929 if (decl
&& decl
!= error_mark_node
)
14930 decl
= TYPE_NAME (decl
);
14932 decl
= error_mark_node
;
14937 /* If it's not a template class, try for a template function. If
14938 the next token is a `;', then this declaration does not declare
14939 anything. But, if there were errors in the decl-specifiers, then
14940 the error might well have come from an attempted class-specifier.
14941 In that case, there's no need to warn about a missing declarator. */
14943 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
14944 || decl_specifiers
.type
!= error_mark_node
))
14945 decl
= cp_parser_init_declarator (parser
,
14947 /*function_definition_allowed_p=*/true,
14949 declares_class_or_enum
,
14950 &function_definition_p
);
14952 pop_deferring_access_checks ();
14954 /* Clear any current qualification; whatever comes next is the start
14955 of something new. */
14956 parser
->scope
= NULL_TREE
;
14957 parser
->qualifying_scope
= NULL_TREE
;
14958 parser
->object_scope
= NULL_TREE
;
14959 /* Look for a trailing `;' after the declaration. */
14960 if (!function_definition_p
14961 && !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
14962 cp_parser_skip_to_end_of_block_or_statement (parser
);
14967 /* Parse a cast-expression that is not the operand of a unary "&". */
14970 cp_parser_simple_cast_expression (cp_parser
*parser
)
14972 return cp_parser_cast_expression (parser
, /*address_p=*/false);
14975 /* Parse a functional cast to TYPE. Returns an expression
14976 representing the cast. */
14979 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14981 tree expression_list
;
14985 = cp_parser_parenthesized_expression_list (parser
, false,
14986 /*non_constant_p=*/NULL
);
14988 cast
= build_functional_cast (type
, expression_list
);
14989 /* [expr.const]/1: In an integral constant expression "only type
14990 conversions to integral or enumeration type can be used". */
14991 if (cast
!= error_mark_node
&& !type_dependent_expression_p (type
)
14992 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type
)))
14994 if (cp_parser_non_integral_constant_expression
14995 (parser
, "a call to a constructor"))
14996 return error_mark_node
;
15001 /* Save the tokens that make up the body of a member function defined
15002 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15003 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15004 specifiers applied to the declaration. Returns the FUNCTION_DECL
15005 for the member function. */
15008 cp_parser_save_member_function_body (cp_parser
* parser
,
15009 cp_decl_specifier_seq
*decl_specifiers
,
15010 cp_declarator
*declarator
,
15017 /* Create the function-declaration. */
15018 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15019 /* If something went badly wrong, bail out now. */
15020 if (fn
== error_mark_node
)
15022 /* If there's a function-body, skip it. */
15023 if (cp_parser_token_starts_function_definition_p
15024 (cp_lexer_peek_token (parser
->lexer
)))
15025 cp_parser_skip_to_end_of_block_or_statement (parser
);
15026 return error_mark_node
;
15029 /* Remember it, if there default args to post process. */
15030 cp_parser_save_default_args (parser
, fn
);
15032 /* Save away the tokens that make up the body of the
15034 first
= parser
->lexer
->next_token
;
15035 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15036 /* Handle function try blocks. */
15037 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15038 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15039 last
= parser
->lexer
->next_token
;
15041 /* Save away the inline definition; we will process it when the
15042 class is complete. */
15043 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15044 DECL_PENDING_INLINE_P (fn
) = 1;
15046 /* We need to know that this was defined in the class, so that
15047 friend templates are handled correctly. */
15048 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15050 /* We're done with the inline definition. */
15051 finish_method (fn
);
15053 /* Add FN to the queue of functions to be parsed later. */
15054 TREE_VALUE (parser
->unparsed_functions_queues
)
15055 = tree_cons (NULL_TREE
, fn
,
15056 TREE_VALUE (parser
->unparsed_functions_queues
));
15061 /* Parse a template-argument-list, as well as the trailing ">" (but
15062 not the opening ">"). See cp_parser_template_argument_list for the
15066 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15070 tree saved_qualifying_scope
;
15071 tree saved_object_scope
;
15072 bool saved_greater_than_is_operator_p
;
15076 When parsing a template-id, the first non-nested `>' is taken as
15077 the end of the template-argument-list rather than a greater-than
15079 saved_greater_than_is_operator_p
15080 = parser
->greater_than_is_operator_p
;
15081 parser
->greater_than_is_operator_p
= false;
15082 /* Parsing the argument list may modify SCOPE, so we save it
15084 saved_scope
= parser
->scope
;
15085 saved_qualifying_scope
= parser
->qualifying_scope
;
15086 saved_object_scope
= parser
->object_scope
;
15087 /* Parse the template-argument-list itself. */
15088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15089 arguments
= NULL_TREE
;
15091 arguments
= cp_parser_template_argument_list (parser
);
15092 /* Look for the `>' that ends the template-argument-list. If we find
15093 a '>>' instead, it's probably just a typo. */
15094 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15096 if (!saved_greater_than_is_operator_p
)
15098 /* If we're in a nested template argument list, the '>>' has
15099 to be a typo for '> >'. We emit the error message, but we
15100 continue parsing and we push a '>' as next token, so that
15101 the argument list will be parsed correctly. Note that the
15102 global source location is still on the token before the
15103 '>>', so we need to say explicitly where we want it. */
15104 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15105 error ("%H%<>>%> should be %<> >%> "
15106 "within a nested template argument list",
15109 /* ??? Proper recovery should terminate two levels of
15110 template argument list here. */
15111 token
->type
= CPP_GREATER
;
15115 /* If this is not a nested template argument list, the '>>'
15116 is a typo for '>'. Emit an error message and continue.
15117 Same deal about the token location, but here we can get it
15118 right by consuming the '>>' before issuing the diagnostic. */
15119 cp_lexer_consume_token (parser
->lexer
);
15120 error ("spurious %<>>%>, use %<>%> to terminate "
15121 "a template argument list");
15124 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15125 error ("missing %<>%> to terminate the template argument list");
15127 /* It's what we want, a '>'; consume it. */
15128 cp_lexer_consume_token (parser
->lexer
);
15129 /* The `>' token might be a greater-than operator again now. */
15130 parser
->greater_than_is_operator_p
15131 = saved_greater_than_is_operator_p
;
15132 /* Restore the SAVED_SCOPE. */
15133 parser
->scope
= saved_scope
;
15134 parser
->qualifying_scope
= saved_qualifying_scope
;
15135 parser
->object_scope
= saved_object_scope
;
15140 /* MEMBER_FUNCTION is a member function, or a friend. If default
15141 arguments, or the body of the function have not yet been parsed,
15145 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15147 /* If this member is a template, get the underlying
15149 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15150 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15152 /* There should not be any class definitions in progress at this
15153 point; the bodies of members are only parsed outside of all class
15155 gcc_assert (parser
->num_classes_being_defined
== 0);
15156 /* While we're parsing the member functions we might encounter more
15157 classes. We want to handle them right away, but we don't want
15158 them getting mixed up with functions that are currently in the
15160 parser
->unparsed_functions_queues
15161 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15163 /* Make sure that any template parameters are in scope. */
15164 maybe_begin_member_template_processing (member_function
);
15166 /* If the body of the function has not yet been parsed, parse it
15168 if (DECL_PENDING_INLINE_P (member_function
))
15170 tree function_scope
;
15171 cp_token_cache
*tokens
;
15173 /* The function is no longer pending; we are processing it. */
15174 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15175 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15176 DECL_PENDING_INLINE_P (member_function
) = 0;
15177 /* If this was an inline function in a local class, enter the scope
15178 of the containing function. */
15179 function_scope
= decl_function_context (member_function
);
15180 if (function_scope
)
15181 push_function_context_to (function_scope
);
15183 /* Push the body of the function onto the lexer stack. */
15184 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15186 /* Let the front end know that we going to be defining this
15188 start_preparsed_function (member_function
, NULL_TREE
,
15189 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15191 /* Now, parse the body of the function. */
15192 cp_parser_function_definition_after_declarator (parser
,
15193 /*inline_p=*/true);
15195 /* Leave the scope of the containing function. */
15196 if (function_scope
)
15197 pop_function_context_from (function_scope
);
15198 cp_parser_pop_lexer (parser
);
15201 /* Remove any template parameters from the symbol table. */
15202 maybe_end_member_template_processing ();
15204 /* Restore the queue. */
15205 parser
->unparsed_functions_queues
15206 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15209 /* If DECL contains any default args, remember it on the unparsed
15210 functions queue. */
15213 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15217 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15219 probe
= TREE_CHAIN (probe
))
15220 if (TREE_PURPOSE (probe
))
15222 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15223 = tree_cons (current_class_type
, decl
,
15224 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15230 /* FN is a FUNCTION_DECL which may contains a parameter with an
15231 unparsed DEFAULT_ARG. Parse the default args now. This function
15232 assumes that the current scope is the scope in which the default
15233 argument should be processed. */
15236 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15238 bool saved_local_variables_forbidden_p
;
15241 /* While we're parsing the default args, we might (due to the
15242 statement expression extension) encounter more classes. We want
15243 to handle them right away, but we don't want them getting mixed
15244 up with default args that are currently in the queue. */
15245 parser
->unparsed_functions_queues
15246 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15248 /* Local variable names (and the `this' keyword) may not appear
15249 in a default argument. */
15250 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15251 parser
->local_variables_forbidden_p
= true;
15253 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
15255 parm
= TREE_CHAIN (parm
))
15257 cp_token_cache
*tokens
;
15259 if (!TREE_PURPOSE (parm
)
15260 || TREE_CODE (TREE_PURPOSE (parm
)) != DEFAULT_ARG
)
15263 /* Push the saved tokens for the default argument onto the parser's
15265 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parm
));
15266 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15268 /* Parse the assignment-expression. */
15269 TREE_PURPOSE (parm
) = cp_parser_assignment_expression (parser
);
15271 /* If the token stream has not been completely used up, then
15272 there was extra junk after the end of the default
15274 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15275 cp_parser_error (parser
, "expected %<,%>");
15277 /* Revert to the main lexer. */
15278 cp_parser_pop_lexer (parser
);
15281 /* Restore the state of local_variables_forbidden_p. */
15282 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
15284 /* Restore the queue. */
15285 parser
->unparsed_functions_queues
15286 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15289 /* Parse the operand of `sizeof' (or a similar operator). Returns
15290 either a TYPE or an expression, depending on the form of the
15291 input. The KEYWORD indicates which kind of expression we have
15295 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
15297 static const char *format
;
15298 tree expr
= NULL_TREE
;
15299 const char *saved_message
;
15300 bool saved_integral_constant_expression_p
;
15302 /* Initialize FORMAT the first time we get here. */
15304 format
= "types may not be defined in `%s' expressions";
15306 /* Types cannot be defined in a `sizeof' expression. Save away the
15308 saved_message
= parser
->type_definition_forbidden_message
;
15309 /* And create the new one. */
15310 parser
->type_definition_forbidden_message
15311 = xmalloc (strlen (format
)
15312 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
15314 sprintf ((char *) parser
->type_definition_forbidden_message
,
15315 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
15317 /* The restrictions on constant-expressions do not apply inside
15318 sizeof expressions. */
15319 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
15320 parser
->integral_constant_expression_p
= false;
15322 /* Do not actually evaluate the expression. */
15324 /* If it's a `(', then we might be looking at the type-id
15326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15329 bool saved_in_type_id_in_expr_p
;
15331 /* We can't be sure yet whether we're looking at a type-id or an
15333 cp_parser_parse_tentatively (parser
);
15334 /* Consume the `('. */
15335 cp_lexer_consume_token (parser
->lexer
);
15336 /* Parse the type-id. */
15337 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
15338 parser
->in_type_id_in_expr_p
= true;
15339 type
= cp_parser_type_id (parser
);
15340 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
15341 /* Now, look for the trailing `)'. */
15342 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15343 /* If all went well, then we're done. */
15344 if (cp_parser_parse_definitely (parser
))
15346 cp_decl_specifier_seq decl_specs
;
15348 /* Build a trivial decl-specifier-seq. */
15349 clear_decl_specs (&decl_specs
);
15350 decl_specs
.type
= type
;
15352 /* Call grokdeclarator to figure out what type this is. */
15353 expr
= grokdeclarator (NULL
,
15357 /*attrlist=*/NULL
);
15361 /* If the type-id production did not work out, then we must be
15362 looking at the unary-expression production. */
15364 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
15365 /* Go back to evaluating expressions. */
15368 /* Free the message we created. */
15369 free ((char *) parser
->type_definition_forbidden_message
);
15370 /* And restore the old one. */
15371 parser
->type_definition_forbidden_message
= saved_message
;
15372 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
15377 /* If the current declaration has no declarator, return true. */
15380 cp_parser_declares_only_class_p (cp_parser
*parser
)
15382 /* If the next token is a `;' or a `,' then there is no
15384 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15385 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15388 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15391 cp_parser_set_storage_class (cp_decl_specifier_seq
*decl_specs
,
15392 cp_storage_class storage_class
)
15394 if (decl_specs
->storage_class
!= sc_none
)
15395 decl_specs
->multiple_storage_classes_p
= true;
15397 decl_specs
->storage_class
= storage_class
;
15400 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15401 is true, the type is a user-defined type; otherwise it is a
15402 built-in type specified by a keyword. */
15405 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
15407 bool user_defined_p
)
15409 decl_specs
->any_specifiers_p
= true;
15411 /* If the user tries to redeclare a built-in type (with, for example,
15412 in "typedef int wchar_t;") we remember that this is what
15413 happened. In system headers, we ignore these declarations so
15414 that G++ can work with system headers that are not C++-safe. */
15415 if (decl_specs
->specs
[(int) ds_typedef
]
15417 && (decl_specs
->type
15418 || decl_specs
->specs
[(int) ds_long
]
15419 || decl_specs
->specs
[(int) ds_short
]
15420 || decl_specs
->specs
[(int) ds_unsigned
]
15421 || decl_specs
->specs
[(int) ds_signed
]))
15423 decl_specs
->redefined_builtin_type
= type_spec
;
15424 if (!decl_specs
->type
)
15426 decl_specs
->type
= type_spec
;
15427 decl_specs
->user_defined_type_p
= false;
15430 else if (decl_specs
->type
)
15431 decl_specs
->multiple_types_p
= true;
15434 decl_specs
->type
= type_spec
;
15435 decl_specs
->user_defined_type_p
= user_defined_p
;
15436 decl_specs
->redefined_builtin_type
= NULL_TREE
;
15440 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15441 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15444 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
15446 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
15449 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15450 issue an error message indicating that TOKEN_DESC was expected.
15452 Returns the token consumed, if the token had the appropriate type.
15453 Otherwise, returns NULL. */
15456 cp_parser_require (cp_parser
* parser
,
15457 enum cpp_ttype type
,
15458 const char* token_desc
)
15460 if (cp_lexer_next_token_is (parser
->lexer
, type
))
15461 return cp_lexer_consume_token (parser
->lexer
);
15464 /* Output the MESSAGE -- unless we're parsing tentatively. */
15465 if (!cp_parser_simulate_error (parser
))
15467 char *message
= concat ("expected ", token_desc
, NULL
);
15468 cp_parser_error (parser
, message
);
15475 /* Like cp_parser_require, except that tokens will be skipped until
15476 the desired token is found. An error message is still produced if
15477 the next token is not as expected. */
15480 cp_parser_skip_until_found (cp_parser
* parser
,
15481 enum cpp_ttype type
,
15482 const char* token_desc
)
15485 unsigned nesting_depth
= 0;
15487 if (cp_parser_require (parser
, type
, token_desc
))
15490 /* Skip tokens until the desired token is found. */
15493 /* Peek at the next token. */
15494 token
= cp_lexer_peek_token (parser
->lexer
);
15495 /* If we've reached the token we want, consume it and
15497 if (token
->type
== type
&& !nesting_depth
)
15499 cp_lexer_consume_token (parser
->lexer
);
15502 /* If we've run out of tokens, stop. */
15503 if (token
->type
== CPP_EOF
)
15505 if (token
->type
== CPP_OPEN_BRACE
15506 || token
->type
== CPP_OPEN_PAREN
15507 || token
->type
== CPP_OPEN_SQUARE
)
15509 else if (token
->type
== CPP_CLOSE_BRACE
15510 || token
->type
== CPP_CLOSE_PAREN
15511 || token
->type
== CPP_CLOSE_SQUARE
)
15513 if (nesting_depth
-- == 0)
15516 /* Consume this token. */
15517 cp_lexer_consume_token (parser
->lexer
);
15521 /* If the next token is the indicated keyword, consume it. Otherwise,
15522 issue an error message indicating that TOKEN_DESC was expected.
15524 Returns the token consumed, if the token had the appropriate type.
15525 Otherwise, returns NULL. */
15528 cp_parser_require_keyword (cp_parser
* parser
,
15530 const char* token_desc
)
15532 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
15534 if (token
&& token
->keyword
!= keyword
)
15536 dyn_string_t error_msg
;
15538 /* Format the error message. */
15539 error_msg
= dyn_string_new (0);
15540 dyn_string_append_cstr (error_msg
, "expected ");
15541 dyn_string_append_cstr (error_msg
, token_desc
);
15542 cp_parser_error (parser
, error_msg
->s
);
15543 dyn_string_delete (error_msg
);
15550 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15551 function-definition. */
15554 cp_parser_token_starts_function_definition_p (cp_token
* token
)
15556 return (/* An ordinary function-body begins with an `{'. */
15557 token
->type
== CPP_OPEN_BRACE
15558 /* A ctor-initializer begins with a `:'. */
15559 || token
->type
== CPP_COLON
15560 /* A function-try-block begins with `try'. */
15561 || token
->keyword
== RID_TRY
15562 /* The named return value extension begins with `return'. */
15563 || token
->keyword
== RID_RETURN
);
15566 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15570 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
15574 token
= cp_lexer_peek_token (parser
->lexer
);
15575 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
15578 /* Returns TRUE iff the next token is the "," or ">" ending a
15579 template-argument. ">>" is also accepted (after the full
15580 argument was parsed) because it's probably a typo for "> >",
15581 and there is a specific diagnostic for this. */
15584 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
15588 token
= cp_lexer_peek_token (parser
->lexer
);
15589 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
15590 || token
->type
== CPP_RSHIFT
);
15593 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15594 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15597 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
15602 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
15603 if (token
->type
== CPP_LESS
)
15605 /* Check for the sequence `<::' in the original code. It would be lexed as
15606 `[:', where `[' is a digraph, and there is no whitespace before
15608 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
15611 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
15612 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
15618 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15619 or none_type otherwise. */
15621 static enum tag_types
15622 cp_parser_token_is_class_key (cp_token
* token
)
15624 switch (token
->keyword
)
15629 return record_type
;
15638 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15641 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
15643 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
15644 pedwarn ("%qs tag used in naming %q#T",
15645 class_key
== union_type
? "union"
15646 : class_key
== record_type
? "struct" : "class",
15650 /* Issue an error message if DECL is redeclared with different
15651 access than its original declaration [class.access.spec/3].
15652 This applies to nested classes and nested class templates.
15656 cp_parser_check_access_in_redeclaration (tree decl
)
15658 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15661 if ((TREE_PRIVATE (decl
)
15662 != (current_access_specifier
== access_private_node
))
15663 || (TREE_PROTECTED (decl
)
15664 != (current_access_specifier
== access_protected_node
)))
15665 error ("%qD redeclared with different access", decl
);
15668 /* Look for the `template' keyword, as a syntactic disambiguator.
15669 Return TRUE iff it is present, in which case it will be
15673 cp_parser_optional_template_keyword (cp_parser
*parser
)
15675 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15677 /* The `template' keyword can only be used within templates;
15678 outside templates the parser can always figure out what is a
15679 template and what is not. */
15680 if (!processing_template_decl
)
15682 error ("%<template%> (as a disambiguator) is only allowed "
15683 "within templates");
15684 /* If this part of the token stream is rescanned, the same
15685 error message would be generated. So, we purge the token
15686 from the stream. */
15687 cp_lexer_purge_token (parser
->lexer
);
15692 /* Consume the `template' keyword. */
15693 cp_lexer_consume_token (parser
->lexer
);
15701 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15702 set PARSER->SCOPE, and perform other related actions. */
15705 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
15710 /* Get the stored value. */
15711 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
15712 /* Perform any access checks that were deferred. */
15713 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
15714 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
15715 /* Set the scope from the stored value. */
15716 parser
->scope
= TREE_VALUE (value
);
15717 parser
->qualifying_scope
= TREE_TYPE (value
);
15718 parser
->object_scope
= NULL_TREE
;
15721 /* Consume tokens up through a non-nested END token. */
15724 cp_parser_cache_group (cp_parser
*parser
,
15725 enum cpp_ttype end
,
15732 /* Abort a parenthesized expression if we encounter a brace. */
15733 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
15734 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15736 /* If we've reached the end of the file, stop. */
15737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15739 /* Consume the next token. */
15740 token
= cp_lexer_consume_token (parser
->lexer
);
15741 /* See if it starts a new group. */
15742 if (token
->type
== CPP_OPEN_BRACE
)
15744 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
15748 else if (token
->type
== CPP_OPEN_PAREN
)
15749 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
15750 else if (token
->type
== end
)
15755 /* Begin parsing tentatively. We always save tokens while parsing
15756 tentatively so that if the tentative parsing fails we can restore the
15760 cp_parser_parse_tentatively (cp_parser
* parser
)
15762 /* Enter a new parsing context. */
15763 parser
->context
= cp_parser_context_new (parser
->context
);
15764 /* Begin saving tokens. */
15765 cp_lexer_save_tokens (parser
->lexer
);
15766 /* In order to avoid repetitive access control error messages,
15767 access checks are queued up until we are no longer parsing
15769 push_deferring_access_checks (dk_deferred
);
15772 /* Commit to the currently active tentative parse. */
15775 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
15777 cp_parser_context
*context
;
15780 /* Mark all of the levels as committed. */
15781 lexer
= parser
->lexer
;
15782 for (context
= parser
->context
; context
->next
; context
= context
->next
)
15784 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
15786 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
15787 while (!cp_lexer_saving_tokens (lexer
))
15788 lexer
= lexer
->next
;
15789 cp_lexer_commit_tokens (lexer
);
15793 /* Abort the currently active tentative parse. All consumed tokens
15794 will be rolled back, and no diagnostics will be issued. */
15797 cp_parser_abort_tentative_parse (cp_parser
* parser
)
15799 cp_parser_simulate_error (parser
);
15800 /* Now, pretend that we want to see if the construct was
15801 successfully parsed. */
15802 cp_parser_parse_definitely (parser
);
15805 /* Stop parsing tentatively. If a parse error has occurred, restore the
15806 token stream. Otherwise, commit to the tokens we have consumed.
15807 Returns true if no error occurred; false otherwise. */
15810 cp_parser_parse_definitely (cp_parser
* parser
)
15812 bool error_occurred
;
15813 cp_parser_context
*context
;
15815 /* Remember whether or not an error occurred, since we are about to
15816 destroy that information. */
15817 error_occurred
= cp_parser_error_occurred (parser
);
15818 /* Remove the topmost context from the stack. */
15819 context
= parser
->context
;
15820 parser
->context
= context
->next
;
15821 /* If no parse errors occurred, commit to the tentative parse. */
15822 if (!error_occurred
)
15824 /* Commit to the tokens read tentatively, unless that was
15826 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
15827 cp_lexer_commit_tokens (parser
->lexer
);
15829 pop_to_parent_deferring_access_checks ();
15831 /* Otherwise, if errors occurred, roll back our state so that things
15832 are just as they were before we began the tentative parse. */
15835 cp_lexer_rollback_tokens (parser
->lexer
);
15836 pop_deferring_access_checks ();
15838 /* Add the context to the front of the free list. */
15839 context
->next
= cp_parser_context_free_list
;
15840 cp_parser_context_free_list
= context
;
15842 return !error_occurred
;
15845 /* Returns true if we are parsing tentatively -- but have decided that
15846 we will stick with this tentative parse, even if errors occur. */
15849 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
15851 return (cp_parser_parsing_tentatively (parser
)
15852 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
15855 /* Returns nonzero iff an error has occurred during the most recent
15856 tentative parse. */
15859 cp_parser_error_occurred (cp_parser
* parser
)
15861 return (cp_parser_parsing_tentatively (parser
)
15862 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
15865 /* Returns nonzero if GNU extensions are allowed. */
15868 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
15870 return parser
->allow_gnu_extensions_p
;
15876 static GTY (()) cp_parser
*the_parser
;
15878 /* External interface. */
15880 /* Parse one entire translation unit. */
15883 c_parse_file (void)
15885 bool error_occurred
;
15886 static bool already_called
= false;
15888 if (already_called
)
15890 sorry ("inter-module optimizations not implemented for C++");
15893 already_called
= true;
15895 the_parser
= cp_parser_new ();
15896 push_deferring_access_checks (flag_access_control
15897 ? dk_no_deferred
: dk_no_check
);
15898 error_occurred
= cp_parser_translation_unit (the_parser
);
15902 /* This variable must be provided by every front end. */
15906 #include "gt-cp-parser.h"