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"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token
GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype
) type
: 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid
) keyword
: 8;
75 /* The value associated with this token, if any. */
77 /* The location at which this token was found. */
81 /* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
86 /* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
92 This somewhat ungainly data structure (as opposed to, say, a
93 variable-length array), is used due to constraints imposed by the
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
97 typedef struct cp_token_block
GTY (())
100 cp_token tokens
[CP_TOKEN_BLOCK_NUM_TOKENS
];
101 /* The number of tokens in this block. */
103 /* The next token block in the chain. */
104 struct cp_token_block
*next
;
105 /* The previous block in the chain. */
106 struct cp_token_block
*prev
;
109 typedef struct cp_token_cache
GTY (())
111 /* The first block in the cache. NULL if there are no tokens in the
113 cp_token_block
*first
;
114 /* The last block in the cache. NULL If there are no tokens in the
116 cp_token_block
*last
;
121 static cp_token_cache
*cp_token_cache_new
123 static void cp_token_cache_push_token
124 (cp_token_cache
*, cp_token
*);
126 /* Create a new cp_token_cache. */
128 static cp_token_cache
*
129 cp_token_cache_new (void)
131 return ggc_alloc_cleared (sizeof (cp_token_cache
));
134 /* Add *TOKEN to *CACHE. */
137 cp_token_cache_push_token (cp_token_cache
*cache
,
140 cp_token_block
*b
= cache
->last
;
142 /* See if we need to allocate a new token block. */
143 if (!b
|| b
->num_tokens
== CP_TOKEN_BLOCK_NUM_TOKENS
)
145 b
= ggc_alloc_cleared (sizeof (cp_token_block
));
146 b
->prev
= cache
->last
;
149 cache
->last
->next
= b
;
153 cache
->first
= cache
->last
= b
;
155 /* Add this token to the current token block. */
156 b
->tokens
[b
->num_tokens
++] = *token
;
159 /* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
163 typedef struct cp_lexer
GTY (())
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token
* GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer
;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token
* GTY ((skip (""))) buffer_end
;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token
* GTY ((skip (""))) first_token
;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token
* GTY ((skip (""))) next_token
;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token
* GTY ((skip (""))) last_token
;
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens
;
197 /* The STRING_CST tokens encountered while processing the current
199 varray_type string_tokens
;
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
205 /* True if we should output debugging information. */
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer
*next
;
214 static cp_lexer
*cp_lexer_new_main
216 static cp_lexer
*cp_lexer_new_from_tokens
217 (struct cp_token_cache
*);
218 static int cp_lexer_saving_tokens
220 static cp_token
*cp_lexer_next_token
221 (cp_lexer
*, cp_token
*);
222 static cp_token
*cp_lexer_prev_token
223 (cp_lexer
*, cp_token
*);
224 static ptrdiff_t cp_lexer_token_difference
225 (cp_lexer
*, cp_token
*, cp_token
*);
226 static cp_token
*cp_lexer_read_token
228 static void cp_lexer_maybe_grow_buffer
230 static void cp_lexer_get_preprocessor_token
231 (cp_lexer
*, cp_token
*);
232 static cp_token
*cp_lexer_peek_token
234 static cp_token
*cp_lexer_peek_nth_token
235 (cp_lexer
*, size_t);
236 static inline bool cp_lexer_next_token_is
237 (cp_lexer
*, enum cpp_ttype
);
238 static bool cp_lexer_next_token_is_not
239 (cp_lexer
*, enum cpp_ttype
);
240 static bool cp_lexer_next_token_is_keyword
241 (cp_lexer
*, enum rid
);
242 static cp_token
*cp_lexer_consume_token
244 static void cp_lexer_purge_token
246 static void cp_lexer_purge_tokens_after
247 (cp_lexer
*, cp_token
*);
248 static void cp_lexer_save_tokens
250 static void cp_lexer_commit_tokens
252 static void cp_lexer_rollback_tokens
254 static inline void cp_lexer_set_source_position_from_token
255 (cp_lexer
*, const cp_token
*);
256 static void cp_lexer_print_token
257 (FILE *, cp_token
*);
258 static inline bool cp_lexer_debugging_p
260 static void cp_lexer_start_debugging
261 (cp_lexer
*) ATTRIBUTE_UNUSED
;
262 static void cp_lexer_stop_debugging
263 (cp_lexer
*) ATTRIBUTE_UNUSED
;
265 /* Manifest constants. */
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
270 /* A token type for keywords, as opposed to ordinary identifiers. */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
273 /* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
279 /* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
286 /* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292 /* The stream to which debugging output should be written. */
293 static FILE *cp_lexer_debug_stream
;
295 /* Create a new main C++ lexer, the lexer that gets tokens from the
299 cp_lexer_new_main (void)
302 cp_token first_token
;
304 /* It's possible that lexing the first token will load a PCH file,
305 which is a GC collection point. So we have to grab the first
306 token before allocating any memory. */
307 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
308 c_common_no_more_pch ();
310 /* Allocate the memory. */
311 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
313 /* Create the circular buffer. */
314 lexer
->buffer
= ggc_calloc (CP_TOKEN_BUFFER_SIZE
, sizeof (cp_token
));
315 lexer
->buffer_end
= lexer
->buffer
+ CP_TOKEN_BUFFER_SIZE
;
317 /* There is one token in the buffer. */
318 lexer
->last_token
= lexer
->buffer
+ 1;
319 lexer
->first_token
= lexer
->buffer
;
320 lexer
->next_token
= lexer
->buffer
;
321 memcpy (lexer
->buffer
, &first_token
, sizeof (cp_token
));
323 /* This lexer obtains more tokens by calling c_lex. */
324 lexer
->main_lexer_p
= true;
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
332 /* Assume we are not debugging. */
333 lexer
->debugging_p
= false;
338 /* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
342 cp_lexer_new_from_tokens (cp_token_cache
*tokens
)
346 cp_token_block
*block
;
347 ptrdiff_t num_tokens
;
349 /* Allocate the memory. */
350 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
352 /* Create a new buffer, appropriately sized. */
354 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
355 num_tokens
+= block
->num_tokens
;
356 lexer
->buffer
= ggc_alloc (num_tokens
* sizeof (cp_token
));
357 lexer
->buffer_end
= lexer
->buffer
+ num_tokens
;
359 /* Install the tokens. */
360 token
= lexer
->buffer
;
361 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
363 memcpy (token
, block
->tokens
, block
->num_tokens
* sizeof (cp_token
));
364 token
+= block
->num_tokens
;
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer
->first_token
= lexer
->buffer
;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer
->next_token
= lexer
->buffer
;
371 /* The buffer is full. */
372 lexer
->last_token
= lexer
->first_token
;
374 /* This lexer doesn't obtain more tokens. */
375 lexer
->main_lexer_p
= false;
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
383 /* Assume we are not debugging. */
384 lexer
->debugging_p
= false;
389 /* Returns nonzero if debugging information should be output. */
392 cp_lexer_debugging_p (cp_lexer
*lexer
)
394 return lexer
->debugging_p
;
397 /* Set the current source position from the information stored in
401 cp_lexer_set_source_position_from_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
402 const cp_token
*token
)
404 /* Ideally, the source position information would not be a global
405 variable, but it is. */
407 /* Update the line number. */
408 if (token
->type
!= CPP_EOF
)
409 input_location
= token
->location
;
412 /* TOKEN points into the circular token buffer. Return a pointer to
413 the next token in the buffer. */
415 static inline cp_token
*
416 cp_lexer_next_token (cp_lexer
* lexer
, cp_token
* token
)
419 if (token
== lexer
->buffer_end
)
420 token
= lexer
->buffer
;
424 /* TOKEN points into the circular token buffer. Return a pointer to
425 the previous token in the buffer. */
427 static inline cp_token
*
428 cp_lexer_prev_token (cp_lexer
* lexer
, cp_token
* token
)
430 if (token
== lexer
->buffer
)
431 token
= lexer
->buffer_end
;
435 /* nonzero if we are presently saving tokens. */
438 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
440 return VARRAY_ACTIVE_SIZE (lexer
->saved_tokens
) != 0;
443 /* Return a pointer to the token that is N tokens beyond TOKEN in the
447 cp_lexer_advance_token (cp_lexer
*lexer
, cp_token
*token
, ptrdiff_t n
)
450 if (token
>= lexer
->buffer_end
)
451 token
= lexer
->buffer
+ (token
- lexer
->buffer_end
);
455 /* Returns the number of times that START would have to be incremented
456 to reach FINISH. If START and FINISH are the same, returns zero. */
459 cp_lexer_token_difference (cp_lexer
* lexer
, cp_token
* start
, cp_token
* finish
)
462 return finish
- start
;
464 return ((lexer
->buffer_end
- lexer
->buffer
)
468 /* Obtain another token from the C preprocessor and add it to the
469 token buffer. Returns the newly read token. */
472 cp_lexer_read_token (cp_lexer
* lexer
)
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer
);
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer
->first_token
)
481 lexer
->first_token
= lexer
->last_token
;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer
->next_token
)
484 lexer
->next_token
= lexer
->last_token
;
486 /* Figure out where we're going to store the new token. */
487 token
= lexer
->last_token
;
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer
, token
);
492 /* Increment LAST_TOKEN. */
493 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
495 /* Strings should have type `const char []'. Right now, we will
496 have an ARRAY_TYPE that is constant rather than an array of
498 FIXME: Make fix_string_type get this right in the first place. */
499 if ((token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
)
500 && flag_const_strings
)
504 /* Get the current type. It will be an ARRAY_TYPE. */
505 type
= TREE_TYPE (token
->value
);
506 /* Use build_cplus_array_type to rebuild the array, thereby
507 getting the right type. */
508 type
= build_cplus_array_type (TREE_TYPE (type
), TYPE_DOMAIN (type
));
509 /* Reset the type of the token. */
510 TREE_TYPE (token
->value
) = type
;
516 /* If the circular buffer is full, make it bigger. */
519 cp_lexer_maybe_grow_buffer (cp_lexer
* lexer
)
521 /* If the buffer is full, enlarge it. */
522 if (lexer
->last_token
== lexer
->first_token
)
524 cp_token
*new_buffer
;
525 cp_token
*old_buffer
;
526 cp_token
*new_first_token
;
527 ptrdiff_t buffer_length
;
528 size_t num_tokens_to_copy
;
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
533 old_buffer
= lexer
->buffer
;
534 /* Compute the current buffer size. */
535 buffer_length
= lexer
->buffer_end
- lexer
->buffer
;
536 /* Allocate a buffer twice as big. */
537 new_buffer
= ggc_realloc (lexer
->buffer
,
538 2 * buffer_length
* sizeof (cp_token
));
540 /* Because the buffer is circular, logically consecutive tokens
541 are not necessarily placed consecutively in memory.
542 Therefore, we must keep move the tokens that were before
543 FIRST_TOKEN to the second half of the newly allocated
545 num_tokens_to_copy
= (lexer
->first_token
- old_buffer
);
546 memcpy (new_buffer
+ buffer_length
,
548 num_tokens_to_copy
* sizeof (cp_token
));
549 /* Clear the rest of the buffer. We never look at this storage,
550 but the garbage collector may. */
551 memset (new_buffer
+ buffer_length
+ num_tokens_to_copy
, 0,
552 (buffer_length
- num_tokens_to_copy
) * sizeof (cp_token
));
554 /* Now recompute all of the buffer pointers. */
556 = new_buffer
+ (lexer
->first_token
- old_buffer
);
557 if (lexer
->next_token
!= NULL
)
559 ptrdiff_t next_token_delta
;
561 if (lexer
->next_token
> lexer
->first_token
)
562 next_token_delta
= lexer
->next_token
- lexer
->first_token
;
565 buffer_length
- (lexer
->first_token
- lexer
->next_token
);
566 lexer
->next_token
= new_first_token
+ next_token_delta
;
568 lexer
->last_token
= new_first_token
+ buffer_length
;
569 lexer
->buffer
= new_buffer
;
570 lexer
->buffer_end
= new_buffer
+ buffer_length
* 2;
571 lexer
->first_token
= new_first_token
;
575 /* Store the next token from the preprocessor in *TOKEN. */
578 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
583 /* If this not the main lexer, return a terminating CPP_EOF token. */
584 if (lexer
!= NULL
&& !lexer
->main_lexer_p
)
586 token
->type
= CPP_EOF
;
587 token
->location
.line
= 0;
588 token
->location
.file
= NULL
;
589 token
->value
= NULL_TREE
;
590 token
->keyword
= RID_MAX
;
596 /* Keep going until we get a token we like. */
599 /* Get a new token from the preprocessor. */
600 token
->type
= c_lex (&token
->value
);
601 /* Issue messages about tokens we cannot process. */
607 error ("invalid token");
611 /* This is a good token, so we exit the loop. */
616 /* Now we've got our token. */
617 token
->location
= input_location
;
619 /* Check to see if this token is a keyword. */
620 if (token
->type
== CPP_NAME
621 && C_IS_RESERVED_WORD (token
->value
))
623 /* Mark this token as a keyword. */
624 token
->type
= CPP_KEYWORD
;
625 /* Record which keyword. */
626 token
->keyword
= C_RID_CODE (token
->value
);
627 /* Update the value. Some keywords are mapped to particular
628 entities, rather than simply having the value of the
629 corresponding IDENTIFIER_NODE. For example, `__const' is
630 mapped to `const'. */
631 token
->value
= ridpointers
[token
->keyword
];
634 token
->keyword
= RID_MAX
;
637 /* Return a pointer to the next token in the token stream, but do not
641 cp_lexer_peek_token (cp_lexer
* lexer
)
645 /* If there are no tokens, read one now. */
646 if (!lexer
->next_token
)
647 cp_lexer_read_token (lexer
);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer
))
652 fprintf (cp_lexer_debug_stream
, "cp_lexer: peeking at token: ");
653 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
654 fprintf (cp_lexer_debug_stream
, "\n");
657 token
= lexer
->next_token
;
658 cp_lexer_set_source_position_from_token (lexer
, token
);
662 /* Return true if the next token has the indicated TYPE. */
665 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
669 /* Peek at the next token. */
670 token
= cp_lexer_peek_token (lexer
);
671 /* Check to see if it has the indicated TYPE. */
672 return token
->type
== type
;
675 /* Return true if the next token does not have the indicated TYPE. */
678 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
680 return !cp_lexer_next_token_is (lexer
, type
);
683 /* Return true if the next token is the indicated KEYWORD. */
686 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
690 /* Peek at the next token. */
691 token
= cp_lexer_peek_token (lexer
);
692 /* Check to see if it is the indicated keyword. */
693 return token
->keyword
== keyword
;
696 /* Return a pointer to the Nth token in the token stream. If N is 1,
697 then this is precisely equivalent to cp_lexer_peek_token. */
700 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
704 /* N is 1-based, not zero-based. */
705 my_friendly_assert (n
> 0, 20000224);
707 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
708 token
= lexer
->next_token
;
709 /* If there are no tokens in the buffer, get one now. */
712 cp_lexer_read_token (lexer
);
713 token
= lexer
->next_token
;
716 /* Now, read tokens until we have enough. */
719 /* Advance to the next token. */
720 token
= cp_lexer_next_token (lexer
, token
);
721 /* If that's all the tokens we have, read a new one. */
722 if (token
== lexer
->last_token
)
723 token
= cp_lexer_read_token (lexer
);
729 /* Consume the next token. The pointer returned is valid only until
730 another token is read. Callers should preserve copy the token
731 explicitly if they will need its value for a longer period of
735 cp_lexer_consume_token (cp_lexer
* lexer
)
739 /* If there are no tokens, read one now. */
740 if (!lexer
->next_token
)
741 cp_lexer_read_token (lexer
);
743 /* Remember the token we'll be returning. */
744 token
= lexer
->next_token
;
746 /* Increment NEXT_TOKEN. */
747 lexer
->next_token
= cp_lexer_next_token (lexer
,
749 /* Check to see if we're all out of tokens. */
750 if (lexer
->next_token
== lexer
->last_token
)
751 lexer
->next_token
= NULL
;
753 /* If we're not saving tokens, then move FIRST_TOKEN too. */
754 if (!cp_lexer_saving_tokens (lexer
))
756 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
757 if (!lexer
->next_token
)
758 lexer
->first_token
= NULL
;
760 lexer
->first_token
= lexer
->next_token
;
763 /* Provide debugging output. */
764 if (cp_lexer_debugging_p (lexer
))
766 fprintf (cp_lexer_debug_stream
, "cp_lexer: consuming token: ");
767 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
768 fprintf (cp_lexer_debug_stream
, "\n");
774 /* Permanently remove the next token from the token stream. There
775 must be a valid next token already; this token never reads
776 additional tokens from the preprocessor. */
779 cp_lexer_purge_token (cp_lexer
*lexer
)
782 cp_token
*next_token
;
784 token
= lexer
->next_token
;
787 next_token
= cp_lexer_next_token (lexer
, token
);
788 if (next_token
== lexer
->last_token
)
790 *token
= *next_token
;
794 lexer
->last_token
= token
;
795 /* The token purged may have been the only token remaining; if so,
797 if (lexer
->next_token
== token
)
798 lexer
->next_token
= NULL
;
801 /* Permanently remove all tokens after TOKEN, up to, but not
802 including, the token that will be returned next by
803 cp_lexer_peek_token. */
806 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*token
)
812 if (lexer
->next_token
)
814 /* Copy the tokens that have not yet been read to the location
815 immediately following TOKEN. */
816 t1
= cp_lexer_next_token (lexer
, token
);
817 t2
= peek
= cp_lexer_peek_token (lexer
);
818 /* Move tokens into the vacant area between TOKEN and PEEK. */
819 while (t2
!= lexer
->last_token
)
822 t1
= cp_lexer_next_token (lexer
, t1
);
823 t2
= cp_lexer_next_token (lexer
, t2
);
825 /* Now, the next available token is right after TOKEN. */
826 lexer
->next_token
= cp_lexer_next_token (lexer
, token
);
827 /* And the last token is wherever we ended up. */
828 lexer
->last_token
= t1
;
832 /* There are no tokens in the buffer, so there is nothing to
833 copy. The last token in the buffer is TOKEN itself. */
834 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
838 /* Begin saving tokens. All tokens consumed after this point will be
842 cp_lexer_save_tokens (cp_lexer
* lexer
)
844 /* Provide debugging output. */
845 if (cp_lexer_debugging_p (lexer
))
846 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
848 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
849 restore the tokens if required. */
850 if (!lexer
->next_token
)
851 cp_lexer_read_token (lexer
);
853 VARRAY_PUSH_INT (lexer
->saved_tokens
,
854 cp_lexer_token_difference (lexer
,
859 /* Commit to the portion of the token stream most recently saved. */
862 cp_lexer_commit_tokens (cp_lexer
* lexer
)
864 /* Provide debugging output. */
865 if (cp_lexer_debugging_p (lexer
))
866 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
868 VARRAY_POP (lexer
->saved_tokens
);
871 /* Return all tokens saved since the last call to cp_lexer_save_tokens
872 to the token stream. Stop saving tokens. */
875 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
879 /* Provide debugging output. */
880 if (cp_lexer_debugging_p (lexer
))
881 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
883 /* Find the token that was the NEXT_TOKEN when we started saving
885 delta
= VARRAY_TOP_INT(lexer
->saved_tokens
);
886 /* Make it the next token again now. */
887 lexer
->next_token
= cp_lexer_advance_token (lexer
,
890 /* It might be the case that there were no tokens when we started
891 saving tokens, but that there are some tokens now. */
892 if (!lexer
->next_token
&& lexer
->first_token
)
893 lexer
->next_token
= lexer
->first_token
;
895 /* Stop saving tokens. */
896 VARRAY_POP (lexer
->saved_tokens
);
899 /* Print a representation of the TOKEN on the STREAM. */
902 cp_lexer_print_token (FILE * stream
, cp_token
* token
)
904 const char *token_type
= NULL
;
906 /* Figure out what kind of token this is. */
914 token_type
= "COMMA";
918 token_type
= "OPEN_PAREN";
921 case CPP_CLOSE_PAREN
:
922 token_type
= "CLOSE_PAREN";
926 token_type
= "OPEN_BRACE";
929 case CPP_CLOSE_BRACE
:
930 token_type
= "CLOSE_BRACE";
934 token_type
= "SEMICOLON";
946 token_type
= "keyword";
949 /* This is not a token that we know how to handle yet. */
954 /* If we have a name for the token, print it out. Otherwise, we
955 simply give the numeric code. */
957 fprintf (stream
, "%s", token_type
);
959 fprintf (stream
, "%d", token
->type
);
960 /* And, for an identifier, print the identifier name. */
961 if (token
->type
== CPP_NAME
962 /* Some keywords have a value that is not an IDENTIFIER_NODE.
963 For example, `struct' is mapped to an INTEGER_CST. */
964 || (token
->type
== CPP_KEYWORD
965 && TREE_CODE (token
->value
) == IDENTIFIER_NODE
))
966 fprintf (stream
, " %s", IDENTIFIER_POINTER (token
->value
));
969 /* Start emitting debugging information. */
972 cp_lexer_start_debugging (cp_lexer
* lexer
)
974 ++lexer
->debugging_p
;
977 /* Stop emitting debugging information. */
980 cp_lexer_stop_debugging (cp_lexer
* lexer
)
982 --lexer
->debugging_p
;
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1035 The performance of the parser could probably be improved
1036 substantially. Some possible improvements include:
1038 - The expression parser recurses through the various levels of
1039 precedence as specified in the grammar, rather than using an
1040 operator-precedence technique. Therefore, parsing a simple
1041 identifier requires multiple recursive calls.
1043 - We could often eliminate the need to parse tentatively by
1044 looking ahead a little bit. In some places, this approach
1045 might not entirely eliminate the need to parse tentatively, but
1046 it might still speed up the average case. */
1048 /* Flags that are passed to some parsing functions. These values can
1049 be bitwise-ored together. */
1051 typedef enum cp_parser_flags
1054 CP_PARSER_FLAGS_NONE
= 0x0,
1055 /* The construct is optional. If it is not present, then no error
1056 should be issued. */
1057 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1058 /* When parsing a type-specifier, do not allow user-defined types. */
1059 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1062 /* The different kinds of declarators we want to parse. */
1064 typedef enum cp_parser_declarator_kind
1066 /* We want an abstract declartor. */
1067 CP_PARSER_DECLARATOR_ABSTRACT
,
1068 /* We want a named declarator. */
1069 CP_PARSER_DECLARATOR_NAMED
,
1070 /* We don't mind, but the name must be an unqualified-id. */
1071 CP_PARSER_DECLARATOR_EITHER
1072 } cp_parser_declarator_kind
;
1074 /* A mapping from a token type to a corresponding tree node type. */
1076 typedef struct cp_parser_token_tree_map_node
1078 /* The token type. */
1079 ENUM_BITFIELD (cpp_ttype
) token_type
: 8;
1080 /* The corresponding tree code. */
1081 ENUM_BITFIELD (tree_code
) tree_type
: 8;
1082 } cp_parser_token_tree_map_node
;
1084 /* A complete map consists of several ordinary entries, followed by a
1085 terminator. The terminating entry has a token_type of CPP_EOF. */
1087 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map
[];
1089 /* The status of a tentative parse. */
1091 typedef enum cp_parser_status_kind
1093 /* No errors have occurred. */
1094 CP_PARSER_STATUS_KIND_NO_ERROR
,
1095 /* An error has occurred. */
1096 CP_PARSER_STATUS_KIND_ERROR
,
1097 /* We are committed to this tentative parse, whether or not an error
1099 CP_PARSER_STATUS_KIND_COMMITTED
1100 } cp_parser_status_kind
;
1102 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context
GTY (())
1106 /* If this is a tentative parsing context, the status of the
1108 enum cp_parser_status_kind status
;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context
*next
;
1116 } cp_parser_context
;
1120 /* Constructors and destructors. */
1122 static cp_parser_context
*cp_parser_context_new
1123 (cp_parser_context
*);
1125 /* Class variables. */
1127 static GTY((deletable (""))) cp_parser_context
* cp_parser_context_free_list
;
1129 /* Constructors and destructors. */
1131 /* Construct a new context. The context below this one on the stack
1132 is given by NEXT. */
1134 static cp_parser_context
*
1135 cp_parser_context_new (cp_parser_context
* next
)
1137 cp_parser_context
*context
;
1139 /* Allocate the storage. */
1140 if (cp_parser_context_free_list
!= NULL
)
1142 /* Pull the first entry from the free list. */
1143 context
= cp_parser_context_free_list
;
1144 cp_parser_context_free_list
= context
->next
;
1145 memset (context
, 0, sizeof (*context
));
1148 context
= ggc_alloc_cleared (sizeof (cp_parser_context
));
1149 /* No errors have occurred yet in this context. */
1150 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1151 /* If this is not the bottomost context, copy information that we
1152 need from the previous context. */
1155 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1156 expression, then we are parsing one in this context, too. */
1157 context
->object_type
= next
->object_type
;
1158 /* Thread the stack. */
1159 context
->next
= next
;
1165 /* The cp_parser structure represents the C++ parser. */
1167 typedef struct cp_parser
GTY(())
1169 /* The lexer from which we are obtaining tokens. */
1172 /* The scope in which names should be looked up. If NULL_TREE, then
1173 we look up names in the scope that is currently open in the
1174 source program. If non-NULL, this is either a TYPE or
1175 NAMESPACE_DECL for the scope in which we should look.
1177 This value is not cleared automatically after a name is looked
1178 up, so we must be careful to clear it before starting a new look
1179 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1180 will look up `Z' in the scope of `X', rather than the current
1181 scope.) Unfortunately, it is difficult to tell when name lookup
1182 is complete, because we sometimes peek at a token, look it up,
1183 and then decide not to consume it. */
1186 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1187 last lookup took place. OBJECT_SCOPE is used if an expression
1188 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1189 respectively. QUALIFYING_SCOPE is used for an expression of the
1190 form "X::Y"; it refers to X. */
1192 tree qualifying_scope
;
1194 /* A stack of parsing contexts. All but the bottom entry on the
1195 stack will be tentative contexts.
1197 We parse tentatively in order to determine which construct is in
1198 use in some situations. For example, in order to determine
1199 whether a statement is an expression-statement or a
1200 declaration-statement we parse it tentatively as a
1201 declaration-statement. If that fails, we then reparse the same
1202 token stream as an expression-statement. */
1203 cp_parser_context
*context
;
1205 /* True if we are parsing GNU C++. If this flag is not set, then
1206 GNU extensions are not recognized. */
1207 bool allow_gnu_extensions_p
;
1209 /* TRUE if the `>' token should be interpreted as the greater-than
1210 operator. FALSE if it is the end of a template-id or
1211 template-parameter-list. */
1212 bool greater_than_is_operator_p
;
1214 /* TRUE if default arguments are allowed within a parameter list
1215 that starts at this point. FALSE if only a gnu extension makes
1216 them permissible. */
1217 bool default_arg_ok_p
;
1219 /* TRUE if we are parsing an integral constant-expression. See
1220 [expr.const] for a precise definition. */
1221 bool integral_constant_expression_p
;
1223 /* TRUE if we are parsing an integral constant-expression -- but a
1224 non-constant expression should be permitted as well. This flag
1225 is used when parsing an array bound so that GNU variable-length
1226 arrays are tolerated. */
1227 bool allow_non_integral_constant_expression_p
;
1229 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1230 been seen that makes the expression non-constant. */
1231 bool non_integral_constant_expression_p
;
1233 /* TRUE if we are parsing the argument to "__offsetof__". */
1236 /* TRUE if local variable names and `this' are forbidden in the
1238 bool local_variables_forbidden_p
;
1240 /* TRUE if the declaration we are parsing is part of a
1241 linkage-specification of the form `extern string-literal
1243 bool in_unbraced_linkage_specification_p
;
1245 /* TRUE if we are presently parsing a declarator, after the
1246 direct-declarator. */
1247 bool in_declarator_p
;
1249 /* TRUE if we are presently parsing a template-argument-list. */
1250 bool in_template_argument_list_p
;
1252 /* TRUE if we are presently parsing the body of an
1253 iteration-statement. */
1254 bool in_iteration_statement_p
;
1256 /* TRUE if we are presently parsing the body of a switch
1258 bool in_switch_statement_p
;
1260 /* TRUE if we are parsing a type-id in an expression context. In
1261 such a situation, both "type (expr)" and "type (type)" are valid
1263 bool in_type_id_in_expr_p
;
1265 /* If non-NULL, then we are parsing a construct where new type
1266 definitions are not permitted. The string stored here will be
1267 issued as an error message if a type is defined. */
1268 const char *type_definition_forbidden_message
;
1270 /* A list of lists. The outer list is a stack, used for member
1271 functions of local classes. At each level there are two sub-list,
1272 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1273 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1274 TREE_VALUE's. The functions are chained in reverse declaration
1277 The TREE_PURPOSE sublist contains those functions with default
1278 arguments that need post processing, and the TREE_VALUE sublist
1279 contains those functions with definitions that need post
1282 These lists can only be processed once the outermost class being
1283 defined is complete. */
1284 tree unparsed_functions_queues
;
1286 /* The number of classes whose definitions are currently in
1288 unsigned num_classes_being_defined
;
1290 /* The number of template parameter lists that apply directly to the
1291 current declaration. */
1292 unsigned num_template_parameter_lists
;
1295 /* The type of a function that parses some kind of expression. */
1296 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1300 /* Constructors and destructors. */
1302 static cp_parser
*cp_parser_new
1305 /* Routines to parse various constructs.
1307 Those that return `tree' will return the error_mark_node (rather
1308 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1309 Sometimes, they will return an ordinary node if error-recovery was
1310 attempted, even though a parse error occurred. So, to check
1311 whether or not a parse error occurred, you should always use
1312 cp_parser_error_occurred. If the construct is optional (indicated
1313 either by an `_opt' in the name of the function that does the
1314 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1315 the construct is not present. */
1317 /* Lexical conventions [gram.lex] */
1319 static tree cp_parser_identifier
1322 /* Basic concepts [gram.basic] */
1324 static bool cp_parser_translation_unit
1327 /* Expressions [gram.expr] */
1329 static tree cp_parser_primary_expression
1330 (cp_parser
*, cp_id_kind
*, tree
*);
1331 static tree cp_parser_id_expression
1332 (cp_parser
*, bool, bool, bool *, bool);
1333 static tree cp_parser_unqualified_id
1334 (cp_parser
*, bool, bool, bool);
1335 static tree cp_parser_nested_name_specifier_opt
1336 (cp_parser
*, bool, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier
1338 (cp_parser
*, bool, bool, bool, bool);
1339 static tree cp_parser_class_or_namespace_name
1340 (cp_parser
*, bool, bool, bool, bool, bool);
1341 static tree cp_parser_postfix_expression
1342 (cp_parser
*, bool);
1343 static tree cp_parser_parenthesized_expression_list
1344 (cp_parser
*, bool, bool *);
1345 static void cp_parser_pseudo_destructor_name
1346 (cp_parser
*, tree
*, tree
*);
1347 static tree cp_parser_unary_expression
1348 (cp_parser
*, bool);
1349 static enum tree_code cp_parser_unary_operator
1351 static tree cp_parser_new_expression
1353 static tree cp_parser_new_placement
1355 static tree cp_parser_new_type_id
1357 static tree cp_parser_new_declarator_opt
1359 static tree cp_parser_direct_new_declarator
1361 static tree cp_parser_new_initializer
1363 static tree cp_parser_delete_expression
1365 static tree cp_parser_cast_expression
1366 (cp_parser
*, bool);
1367 static tree cp_parser_pm_expression
1369 static tree cp_parser_multiplicative_expression
1371 static tree cp_parser_additive_expression
1373 static tree cp_parser_shift_expression
1375 static tree cp_parser_relational_expression
1377 static tree cp_parser_equality_expression
1379 static tree cp_parser_and_expression
1381 static tree cp_parser_exclusive_or_expression
1383 static tree cp_parser_inclusive_or_expression
1385 static tree cp_parser_logical_and_expression
1387 static tree cp_parser_logical_or_expression
1389 static tree cp_parser_question_colon_clause
1390 (cp_parser
*, tree
);
1391 static tree cp_parser_assignment_expression
1393 static enum tree_code cp_parser_assignment_operator_opt
1395 static tree cp_parser_expression
1397 static tree cp_parser_constant_expression
1398 (cp_parser
*, bool, bool *);
1400 /* Statements [gram.stmt.stmt] */
1402 static void cp_parser_statement
1403 (cp_parser
*, bool);
1404 static tree cp_parser_labeled_statement
1405 (cp_parser
*, bool);
1406 static tree cp_parser_expression_statement
1407 (cp_parser
*, bool);
1408 static tree cp_parser_compound_statement
1409 (cp_parser
*, bool);
1410 static void cp_parser_statement_seq_opt
1411 (cp_parser
*, bool);
1412 static tree cp_parser_selection_statement
1414 static tree cp_parser_condition
1416 static tree cp_parser_iteration_statement
1418 static void cp_parser_for_init_statement
1420 static tree cp_parser_jump_statement
1422 static void cp_parser_declaration_statement
1425 static tree cp_parser_implicitly_scoped_statement
1427 static void cp_parser_already_scoped_statement
1430 /* Declarations [gram.dcl.dcl] */
1432 static void cp_parser_declaration_seq_opt
1434 static void cp_parser_declaration
1436 static void cp_parser_block_declaration
1437 (cp_parser
*, bool);
1438 static void cp_parser_simple_declaration
1439 (cp_parser
*, bool);
1440 static tree cp_parser_decl_specifier_seq
1441 (cp_parser
*, cp_parser_flags
, tree
*, int *);
1442 static tree cp_parser_storage_class_specifier_opt
1444 static tree cp_parser_function_specifier_opt
1446 static tree cp_parser_type_specifier
1447 (cp_parser
*, cp_parser_flags
, bool, bool, int *, bool *);
1448 static tree cp_parser_simple_type_specifier
1449 (cp_parser
*, cp_parser_flags
, bool);
1450 static tree cp_parser_type_name
1452 static tree cp_parser_elaborated_type_specifier
1453 (cp_parser
*, bool, bool);
1454 static tree cp_parser_enum_specifier
1456 static void cp_parser_enumerator_list
1457 (cp_parser
*, tree
);
1458 static void cp_parser_enumerator_definition
1459 (cp_parser
*, tree
);
1460 static tree cp_parser_namespace_name
1462 static void cp_parser_namespace_definition
1464 static void cp_parser_namespace_body
1466 static tree cp_parser_qualified_namespace_specifier
1468 static void cp_parser_namespace_alias_definition
1470 static void cp_parser_using_declaration
1472 static void cp_parser_using_directive
1474 static void cp_parser_asm_definition
1476 static void cp_parser_linkage_specification
1479 /* Declarators [gram.dcl.decl] */
1481 static tree cp_parser_init_declarator
1482 (cp_parser
*, tree
, tree
, bool, bool, int, bool *);
1483 static tree cp_parser_declarator
1484 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *);
1485 static tree cp_parser_direct_declarator
1486 (cp_parser
*, cp_parser_declarator_kind
, int *);
1487 static enum tree_code cp_parser_ptr_operator
1488 (cp_parser
*, tree
*, tree
*);
1489 static tree cp_parser_cv_qualifier_seq_opt
1491 static tree cp_parser_cv_qualifier_opt
1493 static tree cp_parser_declarator_id
1495 static tree cp_parser_type_id
1497 static tree cp_parser_type_specifier_seq
1499 static tree cp_parser_parameter_declaration_clause
1501 static tree cp_parser_parameter_declaration_list
1503 static tree cp_parser_parameter_declaration
1504 (cp_parser
*, bool, bool *);
1505 static void cp_parser_function_body
1507 static tree cp_parser_initializer
1508 (cp_parser
*, bool *, bool *);
1509 static tree cp_parser_initializer_clause
1510 (cp_parser
*, bool *);
1511 static tree cp_parser_initializer_list
1512 (cp_parser
*, bool *);
1514 static bool cp_parser_ctor_initializer_opt_and_function_body
1517 /* Classes [gram.class] */
1519 static tree cp_parser_class_name
1520 (cp_parser
*, bool, bool, bool, bool, bool, bool);
1521 static tree cp_parser_class_specifier
1523 static tree cp_parser_class_head
1524 (cp_parser
*, bool *);
1525 static enum tag_types cp_parser_class_key
1527 static void cp_parser_member_specification_opt
1529 static void cp_parser_member_declaration
1531 static tree cp_parser_pure_specifier
1533 static tree cp_parser_constant_initializer
1536 /* Derived classes [gram.class.derived] */
1538 static tree cp_parser_base_clause
1540 static tree cp_parser_base_specifier
1543 /* Special member functions [gram.special] */
1545 static tree cp_parser_conversion_function_id
1547 static tree cp_parser_conversion_type_id
1549 static tree cp_parser_conversion_declarator_opt
1551 static bool cp_parser_ctor_initializer_opt
1553 static void cp_parser_mem_initializer_list
1555 static tree cp_parser_mem_initializer
1557 static tree cp_parser_mem_initializer_id
1560 /* Overloading [gram.over] */
1562 static tree cp_parser_operator_function_id
1564 static tree cp_parser_operator
1567 /* Templates [gram.temp] */
1569 static void cp_parser_template_declaration
1570 (cp_parser
*, bool);
1571 static tree cp_parser_template_parameter_list
1573 static tree cp_parser_template_parameter
1575 static tree cp_parser_type_parameter
1577 static tree cp_parser_template_id
1578 (cp_parser
*, bool, bool, bool);
1579 static tree cp_parser_template_name
1580 (cp_parser
*, bool, bool, bool, bool *);
1581 static tree cp_parser_template_argument_list
1583 static tree cp_parser_template_argument
1585 static void cp_parser_explicit_instantiation
1587 static void cp_parser_explicit_specialization
1590 /* Exception handling [gram.exception] */
1592 static tree cp_parser_try_block
1594 static bool cp_parser_function_try_block
1596 static void cp_parser_handler_seq
1598 static void cp_parser_handler
1600 static tree cp_parser_exception_declaration
1602 static tree cp_parser_throw_expression
1604 static tree cp_parser_exception_specification_opt
1606 static tree cp_parser_type_id_list
1609 /* GNU Extensions */
1611 static tree cp_parser_asm_specification_opt
1613 static tree cp_parser_asm_operand_list
1615 static tree cp_parser_asm_clobber_list
1617 static tree cp_parser_attributes_opt
1619 static tree cp_parser_attribute_list
1621 static bool cp_parser_extension_opt
1622 (cp_parser
*, int *);
1623 static void cp_parser_label_declaration
1626 /* Utility Routines */
1628 static tree cp_parser_lookup_name
1629 (cp_parser
*, tree
, bool, bool, bool, bool);
1630 static tree cp_parser_lookup_name_simple
1631 (cp_parser
*, tree
);
1632 static tree cp_parser_maybe_treat_template_as_class
1634 static bool cp_parser_check_declarator_template_parameters
1635 (cp_parser
*, tree
);
1636 static bool cp_parser_check_template_parameters
1637 (cp_parser
*, unsigned);
1638 static tree cp_parser_simple_cast_expression
1640 static tree cp_parser_binary_expression
1641 (cp_parser
*, const cp_parser_token_tree_map
, cp_parser_expression_fn
);
1642 static tree cp_parser_global_scope_opt
1643 (cp_parser
*, bool);
1644 static bool cp_parser_constructor_declarator_p
1645 (cp_parser
*, bool);
1646 static tree cp_parser_function_definition_from_specifiers_and_declarator
1647 (cp_parser
*, tree
, tree
, tree
);
1648 static tree cp_parser_function_definition_after_declarator
1649 (cp_parser
*, bool);
1650 static void cp_parser_template_declaration_after_export
1651 (cp_parser
*, bool);
1652 static tree cp_parser_single_declaration
1653 (cp_parser
*, bool, bool *);
1654 static tree cp_parser_functional_cast
1655 (cp_parser
*, tree
);
1656 static tree cp_parser_save_member_function_body
1657 (cp_parser
*, tree
, tree
, tree
);
1658 static tree cp_parser_enclosed_template_argument_list
1660 static void cp_parser_save_default_args
1661 (cp_parser
*, tree
);
1662 static void cp_parser_late_parsing_for_member
1663 (cp_parser
*, tree
);
1664 static void cp_parser_late_parsing_default_args
1665 (cp_parser
*, tree
);
1666 static tree cp_parser_sizeof_operand
1667 (cp_parser
*, enum rid
);
1668 static bool cp_parser_declares_only_class_p
1670 static tree cp_parser_fold_non_dependent_expr
1672 static bool cp_parser_friend_p
1674 static cp_token
*cp_parser_require
1675 (cp_parser
*, enum cpp_ttype
, const char *);
1676 static cp_token
*cp_parser_require_keyword
1677 (cp_parser
*, enum rid
, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1680 static bool cp_parser_next_token_starts_class_definition_p
1682 static bool cp_parser_next_token_ends_template_argument_p
1684 static enum tag_types cp_parser_token_is_class_key
1686 static void cp_parser_check_class_key
1687 (enum tag_types
, tree type
);
1688 static void cp_parser_check_access_in_redeclaration
1690 static bool cp_parser_optional_template_keyword
1692 static void cp_parser_pre_parsed_nested_name_specifier
1694 static void cp_parser_cache_group
1695 (cp_parser
*, cp_token_cache
*, enum cpp_ttype
, unsigned);
1696 static void cp_parser_parse_tentatively
1698 static void cp_parser_commit_to_tentative_parse
1700 static void cp_parser_abort_tentative_parse
1702 static bool cp_parser_parse_definitely
1704 static inline bool cp_parser_parsing_tentatively
1706 static bool cp_parser_committed_to_tentative_parse
1708 static void cp_parser_error
1709 (cp_parser
*, const char *);
1710 static void cp_parser_name_lookup_error
1711 (cp_parser
*, tree
, tree
, const char *);
1712 static bool cp_parser_simulate_error
1714 static void cp_parser_check_type_definition
1716 static void cp_parser_check_for_definition_in_return_type
1718 static void cp_parser_check_for_invalid_template_id
1719 (cp_parser
*, tree
);
1720 static tree cp_parser_non_integral_constant_expression
1722 static bool cp_parser_diagnose_invalid_type_name
1724 static int cp_parser_skip_to_closing_parenthesis
1725 (cp_parser
*, bool, bool, bool);
1726 static void cp_parser_skip_to_end_of_statement
1728 static void cp_parser_consume_semicolon_at_end_of_statement
1730 static void cp_parser_skip_to_end_of_block_or_statement
1732 static void cp_parser_skip_to_closing_brace
1734 static void cp_parser_skip_until_found
1735 (cp_parser
*, enum cpp_ttype
, const char *);
1736 static bool cp_parser_error_occurred
1738 static bool cp_parser_allow_gnu_extensions_p
1740 static bool cp_parser_is_string_literal
1742 static bool cp_parser_is_keyword
1743 (cp_token
*, enum rid
);
1745 /* Returns nonzero if we are parsing tentatively. */
1748 cp_parser_parsing_tentatively (cp_parser
* parser
)
1750 return parser
->context
->next
!= NULL
;
1753 /* Returns nonzero if TOKEN is a string literal. */
1756 cp_parser_is_string_literal (cp_token
* token
)
1758 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1761 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1764 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1766 return token
->keyword
== keyword
;
1769 /* Issue the indicated error MESSAGE. */
1772 cp_parser_error (cp_parser
* parser
, const char* message
)
1774 /* Output the MESSAGE -- unless we're parsing tentatively. */
1775 if (!cp_parser_simulate_error (parser
))
1778 token
= cp_lexer_peek_token (parser
->lexer
);
1779 c_parse_error (message
,
1780 /* Because c_parser_error does not understand
1781 CPP_KEYWORD, keywords are treated like
1783 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1788 /* Issue an error about name-lookup failing. NAME is the
1789 IDENTIFIER_NODE DECL is the result of
1790 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1791 the thing that we hoped to find. */
1794 cp_parser_name_lookup_error (cp_parser
* parser
,
1797 const char* desired
)
1799 /* If name lookup completely failed, tell the user that NAME was not
1801 if (decl
== error_mark_node
)
1803 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1804 error ("`%D::%D' has not been declared",
1805 parser
->scope
, name
);
1806 else if (parser
->scope
== global_namespace
)
1807 error ("`::%D' has not been declared", name
);
1809 error ("`%D' has not been declared", name
);
1811 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1812 error ("`%D::%D' %s", parser
->scope
, name
, desired
);
1813 else if (parser
->scope
== global_namespace
)
1814 error ("`::%D' %s", name
, desired
);
1816 error ("`%D' %s", name
, desired
);
1819 /* If we are parsing tentatively, remember that an error has occurred
1820 during this tentative parse. Returns true if the error was
1821 simulated; false if a messgae should be issued by the caller. */
1824 cp_parser_simulate_error (cp_parser
* parser
)
1826 if (cp_parser_parsing_tentatively (parser
)
1827 && !cp_parser_committed_to_tentative_parse (parser
))
1829 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1835 /* This function is called when a type is defined. If type
1836 definitions are forbidden at this point, an error message is
1840 cp_parser_check_type_definition (cp_parser
* parser
)
1842 /* If types are forbidden here, issue a message. */
1843 if (parser
->type_definition_forbidden_message
)
1844 /* Use `%s' to print the string in case there are any escape
1845 characters in the message. */
1846 error ("%s", parser
->type_definition_forbidden_message
);
1849 /* This function is called when a declaration is parsed. If
1850 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1851 indicates that a type was defined in the decl-specifiers for DECL,
1852 then an error is issued. */
1855 cp_parser_check_for_definition_in_return_type (tree declarator
,
1856 int declares_class_or_enum
)
1858 /* [dcl.fct] forbids type definitions in return types.
1859 Unfortunately, it's not easy to know whether or not we are
1860 processing a return type until after the fact. */
1862 && (TREE_CODE (declarator
) == INDIRECT_REF
1863 || TREE_CODE (declarator
) == ADDR_EXPR
))
1864 declarator
= TREE_OPERAND (declarator
, 0);
1866 && TREE_CODE (declarator
) == CALL_EXPR
1867 && declares_class_or_enum
& 2)
1868 error ("new types may not be defined in a return type");
1871 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1872 "<" in any valid C++ program. If the next token is indeed "<",
1873 issue a message warning the user about what appears to be an
1874 invalid attempt to form a template-id. */
1877 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1883 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1886 error ("`%T' is not a template", type
);
1887 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1888 error ("`%s' is not a template", IDENTIFIER_POINTER (type
));
1890 error ("invalid template-id");
1891 /* Remember the location of the invalid "<". */
1892 if (cp_parser_parsing_tentatively (parser
)
1893 && !cp_parser_committed_to_tentative_parse (parser
))
1895 token
= cp_lexer_peek_token (parser
->lexer
);
1896 token
= cp_lexer_prev_token (parser
->lexer
, token
);
1897 start
= cp_lexer_token_difference (parser
->lexer
,
1898 parser
->lexer
->first_token
,
1903 /* Consume the "<". */
1904 cp_lexer_consume_token (parser
->lexer
);
1905 /* Parse the template arguments. */
1906 cp_parser_enclosed_template_argument_list (parser
);
1907 /* Permanently remove the invalid template arguments so that
1908 this error message is not issued again. */
1911 token
= cp_lexer_advance_token (parser
->lexer
,
1912 parser
->lexer
->first_token
,
1914 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
1919 /* Issue an error message about the fact that THING appeared in a
1920 constant-expression. Returns ERROR_MARK_NODE. */
1923 cp_parser_non_integral_constant_expression (const char *thing
)
1925 error ("%s cannot appear in a constant-expression", thing
);
1926 return error_mark_node
;
1929 /* Check for a common situation where a type-name should be present,
1930 but is not, and issue a sensible error message. Returns true if an
1931 invalid type-name was detected. */
1934 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
)
1936 /* If the next two tokens are both identifiers, the code is
1937 erroneous. The usual cause of this situation is code like:
1941 where "T" should name a type -- but does not. */
1942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
1943 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
1947 /* If parsing tentatively, we should commit; we really are
1948 looking at a declaration. */
1949 /* Consume the first identifier. */
1950 name
= cp_lexer_consume_token (parser
->lexer
)->value
;
1951 /* Issue an error message. */
1952 error ("`%s' does not name a type", IDENTIFIER_POINTER (name
));
1953 /* If we're in a template class, it's possible that the user was
1954 referring to a type from a base class. For example:
1956 template <typename T> struct A { typedef T X; };
1957 template <typename T> struct B : public A<T> { X x; };
1959 The user should have said "typename A<T>::X". */
1960 if (processing_template_decl
&& current_class_type
)
1964 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1968 tree base_type
= BINFO_TYPE (b
);
1969 if (CLASS_TYPE_P (base_type
)
1970 && dependent_type_p (base_type
))
1973 /* Go from a particular instantiation of the
1974 template (which will have an empty TYPE_FIELDs),
1975 to the main version. */
1976 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1977 for (field
= TYPE_FIELDS (base_type
);
1979 field
= TREE_CHAIN (field
))
1980 if (TREE_CODE (field
) == TYPE_DECL
1981 && DECL_NAME (field
) == name
)
1983 error ("(perhaps `typename %T::%s' was intended)",
1984 BINFO_TYPE (b
), IDENTIFIER_POINTER (name
));
1992 /* Skip to the end of the declaration; there's no point in
1993 trying to process it. */
1994 cp_parser_skip_to_end_of_statement (parser
);
2002 /* Consume tokens up to, and including, the next non-nested closing `)'.
2003 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2004 are doing error recovery. Returns -1 if OR_COMMA is true and we
2005 found an unnested comma. */
2008 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2013 unsigned paren_depth
= 0;
2014 unsigned brace_depth
= 0;
2016 if (recovering
&& !or_comma
&& cp_parser_parsing_tentatively (parser
)
2017 && !cp_parser_committed_to_tentative_parse (parser
))
2024 /* If we've run out of tokens, then there is no closing `)'. */
2025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2028 token
= cp_lexer_peek_token (parser
->lexer
);
2030 /* This matches the processing in skip_to_end_of_statement. */
2031 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2033 if (token
->type
== CPP_OPEN_BRACE
)
2035 if (token
->type
== CPP_CLOSE_BRACE
)
2040 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2041 && !brace_depth
&& !paren_depth
)
2046 /* If it is an `(', we have entered another level of nesting. */
2047 if (token
->type
== CPP_OPEN_PAREN
)
2049 /* If it is a `)', then we might be done. */
2050 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2053 cp_lexer_consume_token (parser
->lexer
);
2058 /* Consume the token. */
2059 cp_lexer_consume_token (parser
->lexer
);
2063 /* Consume tokens until we reach the end of the current statement.
2064 Normally, that will be just before consuming a `;'. However, if a
2065 non-nested `}' comes first, then we stop before consuming that. */
2068 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2070 unsigned nesting_depth
= 0;
2076 /* Peek at the next token. */
2077 token
= cp_lexer_peek_token (parser
->lexer
);
2078 /* If we've run out of tokens, stop. */
2079 if (token
->type
== CPP_EOF
)
2081 /* If the next token is a `;', we have reached the end of the
2083 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2085 /* If the next token is a non-nested `}', then we have reached
2086 the end of the current block. */
2087 if (token
->type
== CPP_CLOSE_BRACE
)
2089 /* If this is a non-nested `}', stop before consuming it.
2090 That way, when confronted with something like:
2094 we stop before consuming the closing `}', even though we
2095 have not yet reached a `;'. */
2096 if (nesting_depth
== 0)
2098 /* If it is the closing `}' for a block that we have
2099 scanned, stop -- but only after consuming the token.
2105 we will stop after the body of the erroneously declared
2106 function, but before consuming the following `typedef'
2108 if (--nesting_depth
== 0)
2110 cp_lexer_consume_token (parser
->lexer
);
2114 /* If it the next token is a `{', then we are entering a new
2115 block. Consume the entire block. */
2116 else if (token
->type
== CPP_OPEN_BRACE
)
2118 /* Consume the token. */
2119 cp_lexer_consume_token (parser
->lexer
);
2123 /* This function is called at the end of a statement or declaration.
2124 If the next token is a semicolon, it is consumed; otherwise, error
2125 recovery is attempted. */
2128 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2130 /* Look for the trailing `;'. */
2131 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2133 /* If there is additional (erroneous) input, skip to the end of
2135 cp_parser_skip_to_end_of_statement (parser
);
2136 /* If the next token is now a `;', consume it. */
2137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2138 cp_lexer_consume_token (parser
->lexer
);
2142 /* Skip tokens until we have consumed an entire block, or until we
2143 have consumed a non-nested `;'. */
2146 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2148 unsigned nesting_depth
= 0;
2154 /* Peek at the next token. */
2155 token
= cp_lexer_peek_token (parser
->lexer
);
2156 /* If we've run out of tokens, stop. */
2157 if (token
->type
== CPP_EOF
)
2159 /* If the next token is a `;', we have reached the end of the
2161 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2163 /* Consume the `;'. */
2164 cp_lexer_consume_token (parser
->lexer
);
2167 /* Consume the token. */
2168 token
= cp_lexer_consume_token (parser
->lexer
);
2169 /* If the next token is a non-nested `}', then we have reached
2170 the end of the current block. */
2171 if (token
->type
== CPP_CLOSE_BRACE
2172 && (nesting_depth
== 0 || --nesting_depth
== 0))
2174 /* If it the next token is a `{', then we are entering a new
2175 block. Consume the entire block. */
2176 if (token
->type
== CPP_OPEN_BRACE
)
2181 /* Skip tokens until a non-nested closing curly brace is the next
2185 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2187 unsigned nesting_depth
= 0;
2193 /* Peek at the next token. */
2194 token
= cp_lexer_peek_token (parser
->lexer
);
2195 /* If we've run out of tokens, stop. */
2196 if (token
->type
== CPP_EOF
)
2198 /* If the next token is a non-nested `}', then we have reached
2199 the end of the current block. */
2200 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2202 /* If it the next token is a `{', then we are entering a new
2203 block. Consume the entire block. */
2204 else if (token
->type
== CPP_OPEN_BRACE
)
2206 /* Consume the token. */
2207 cp_lexer_consume_token (parser
->lexer
);
2211 /* Create a new C++ parser. */
2214 cp_parser_new (void)
2219 /* cp_lexer_new_main is called before calling ggc_alloc because
2220 cp_lexer_new_main might load a PCH file. */
2221 lexer
= cp_lexer_new_main ();
2223 parser
= ggc_alloc_cleared (sizeof (cp_parser
));
2224 parser
->lexer
= lexer
;
2225 parser
->context
= cp_parser_context_new (NULL
);
2227 /* For now, we always accept GNU extensions. */
2228 parser
->allow_gnu_extensions_p
= 1;
2230 /* The `>' token is a greater-than operator, not the end of a
2232 parser
->greater_than_is_operator_p
= true;
2234 parser
->default_arg_ok_p
= true;
2236 /* We are not parsing a constant-expression. */
2237 parser
->integral_constant_expression_p
= false;
2238 parser
->allow_non_integral_constant_expression_p
= false;
2239 parser
->non_integral_constant_expression_p
= false;
2241 /* We are not parsing offsetof. */
2242 parser
->in_offsetof_p
= false;
2244 /* Local variable names are not forbidden. */
2245 parser
->local_variables_forbidden_p
= false;
2247 /* We are not processing an `extern "C"' declaration. */
2248 parser
->in_unbraced_linkage_specification_p
= false;
2250 /* We are not processing a declarator. */
2251 parser
->in_declarator_p
= false;
2253 /* We are not processing a template-argument-list. */
2254 parser
->in_template_argument_list_p
= false;
2256 /* We are not in an iteration statement. */
2257 parser
->in_iteration_statement_p
= false;
2259 /* We are not in a switch statement. */
2260 parser
->in_switch_statement_p
= false;
2262 /* We are not parsing a type-id inside an expression. */
2263 parser
->in_type_id_in_expr_p
= false;
2265 /* The unparsed function queue is empty. */
2266 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2268 /* There are no classes being defined. */
2269 parser
->num_classes_being_defined
= 0;
2271 /* No template parameters apply. */
2272 parser
->num_template_parameter_lists
= 0;
2277 /* Lexical conventions [gram.lex] */
2279 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2283 cp_parser_identifier (cp_parser
* parser
)
2287 /* Look for the identifier. */
2288 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2289 /* Return the value. */
2290 return token
? token
->value
: error_mark_node
;
2293 /* Basic concepts [gram.basic] */
2295 /* Parse a translation-unit.
2298 declaration-seq [opt]
2300 Returns TRUE if all went well. */
2303 cp_parser_translation_unit (cp_parser
* parser
)
2307 cp_parser_declaration_seq_opt (parser
);
2309 /* If there are no tokens left then all went well. */
2310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2313 /* Otherwise, issue an error message. */
2314 cp_parser_error (parser
, "expected declaration");
2318 /* Consume the EOF token. */
2319 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2322 finish_translation_unit ();
2324 /* All went well. */
2328 /* Expressions [gram.expr] */
2330 /* Parse a primary-expression.
2341 ( compound-statement )
2342 __builtin_va_arg ( assignment-expression , type-id )
2347 Returns a representation of the expression.
2349 *IDK indicates what kind of id-expression (if any) was present.
2351 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2352 used as the operand of a pointer-to-member. In that case,
2353 *QUALIFYING_CLASS gives the class that is used as the qualifying
2354 class in the pointer-to-member. */
2357 cp_parser_primary_expression (cp_parser
*parser
,
2359 tree
*qualifying_class
)
2363 /* Assume the primary expression is not an id-expression. */
2364 *idk
= CP_ID_KIND_NONE
;
2365 /* And that it cannot be used as pointer-to-member. */
2366 *qualifying_class
= NULL_TREE
;
2368 /* Peek at the next token. */
2369 token
= cp_lexer_peek_token (parser
->lexer
);
2370 switch (token
->type
)
2383 token
= cp_lexer_consume_token (parser
->lexer
);
2384 return token
->value
;
2386 case CPP_OPEN_PAREN
:
2389 bool saved_greater_than_is_operator_p
;
2391 /* Consume the `('. */
2392 cp_lexer_consume_token (parser
->lexer
);
2393 /* Within a parenthesized expression, a `>' token is always
2394 the greater-than operator. */
2395 saved_greater_than_is_operator_p
2396 = parser
->greater_than_is_operator_p
;
2397 parser
->greater_than_is_operator_p
= true;
2398 /* If we see `( { ' then we are looking at the beginning of
2399 a GNU statement-expression. */
2400 if (cp_parser_allow_gnu_extensions_p (parser
)
2401 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2403 /* Statement-expressions are not allowed by the standard. */
2405 pedwarn ("ISO C++ forbids braced-groups within expressions");
2407 /* And they're not allowed outside of a function-body; you
2408 cannot, for example, write:
2410 int i = ({ int j = 3; j + 1; });
2412 at class or namespace scope. */
2413 if (!at_function_scope_p ())
2414 error ("statement-expressions are allowed only inside functions");
2415 /* Start the statement-expression. */
2416 expr
= begin_stmt_expr ();
2417 /* Parse the compound-statement. */
2418 cp_parser_compound_statement (parser
, true);
2420 expr
= finish_stmt_expr (expr
, false);
2424 /* Parse the parenthesized expression. */
2425 expr
= cp_parser_expression (parser
);
2426 /* Let the front end know that this expression was
2427 enclosed in parentheses. This matters in case, for
2428 example, the expression is of the form `A::B', since
2429 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2431 finish_parenthesized_expr (expr
);
2433 /* The `>' token might be the end of a template-id or
2434 template-parameter-list now. */
2435 parser
->greater_than_is_operator_p
2436 = saved_greater_than_is_operator_p
;
2437 /* Consume the `)'. */
2438 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2439 cp_parser_skip_to_end_of_statement (parser
);
2445 switch (token
->keyword
)
2447 /* These two are the boolean literals. */
2449 cp_lexer_consume_token (parser
->lexer
);
2450 return boolean_true_node
;
2452 cp_lexer_consume_token (parser
->lexer
);
2453 return boolean_false_node
;
2455 /* The `__null' literal. */
2457 cp_lexer_consume_token (parser
->lexer
);
2460 /* Recognize the `this' keyword. */
2462 cp_lexer_consume_token (parser
->lexer
);
2463 if (parser
->local_variables_forbidden_p
)
2465 error ("`this' may not be used in this context");
2466 return error_mark_node
;
2468 /* Pointers cannot appear in constant-expressions. */
2469 if (parser
->integral_constant_expression_p
)
2471 if (!parser
->allow_non_integral_constant_expression_p
)
2472 return cp_parser_non_integral_constant_expression ("`this'");
2473 parser
->non_integral_constant_expression_p
= true;
2475 return finish_this_expr ();
2477 /* The `operator' keyword can be the beginning of an
2482 case RID_FUNCTION_NAME
:
2483 case RID_PRETTY_FUNCTION_NAME
:
2484 case RID_C99_FUNCTION_NAME
:
2485 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2486 __func__ are the names of variables -- but they are
2487 treated specially. Therefore, they are handled here,
2488 rather than relying on the generic id-expression logic
2489 below. Grammatically, these names are id-expressions.
2491 Consume the token. */
2492 token
= cp_lexer_consume_token (parser
->lexer
);
2493 /* Look up the name. */
2494 return finish_fname (token
->value
);
2501 /* The `__builtin_va_arg' construct is used to handle
2502 `va_arg'. Consume the `__builtin_va_arg' token. */
2503 cp_lexer_consume_token (parser
->lexer
);
2504 /* Look for the opening `('. */
2505 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2506 /* Now, parse the assignment-expression. */
2507 expression
= cp_parser_assignment_expression (parser
);
2508 /* Look for the `,'. */
2509 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2510 /* Parse the type-id. */
2511 type
= cp_parser_type_id (parser
);
2512 /* Look for the closing `)'. */
2513 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2514 /* Using `va_arg' in a constant-expression is not
2516 if (parser
->integral_constant_expression_p
)
2518 if (!parser
->allow_non_integral_constant_expression_p
)
2519 return cp_parser_non_integral_constant_expression ("`va_arg'");
2520 parser
->non_integral_constant_expression_p
= true;
2522 return build_x_va_arg (expression
, type
);
2528 bool saved_in_offsetof_p
;
2530 /* Consume the "__offsetof__" token. */
2531 cp_lexer_consume_token (parser
->lexer
);
2532 /* Consume the opening `('. */
2533 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2534 /* Parse the parenthesized (almost) constant-expression. */
2535 saved_in_offsetof_p
= parser
->in_offsetof_p
;
2536 parser
->in_offsetof_p
= true;
2538 = cp_parser_constant_expression (parser
,
2539 /*allow_non_constant_p=*/false,
2540 /*non_constant_p=*/NULL
);
2541 parser
->in_offsetof_p
= saved_in_offsetof_p
;
2542 /* Consume the closing ')'. */
2543 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2549 cp_parser_error (parser
, "expected primary-expression");
2550 return error_mark_node
;
2553 /* An id-expression can start with either an identifier, a
2554 `::' as the beginning of a qualified-id, or the "operator"
2558 case CPP_TEMPLATE_ID
:
2559 case CPP_NESTED_NAME_SPECIFIER
:
2563 const char *error_msg
;
2566 /* Parse the id-expression. */
2568 = cp_parser_id_expression (parser
,
2569 /*template_keyword_p=*/false,
2570 /*check_dependency_p=*/true,
2571 /*template_p=*/NULL
,
2572 /*declarator_p=*/false);
2573 if (id_expression
== error_mark_node
)
2574 return error_mark_node
;
2575 /* If we have a template-id, then no further lookup is
2576 required. If the template-id was for a template-class, we
2577 will sometimes have a TYPE_DECL at this point. */
2578 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2579 || TREE_CODE (id_expression
) == TYPE_DECL
)
2580 decl
= id_expression
;
2581 /* Look up the name. */
2584 decl
= cp_parser_lookup_name_simple (parser
, id_expression
);
2585 /* If name lookup gives us a SCOPE_REF, then the
2586 qualifying scope was dependent. Just propagate the
2588 if (TREE_CODE (decl
) == SCOPE_REF
)
2590 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2591 *qualifying_class
= TREE_OPERAND (decl
, 0);
2594 /* Check to see if DECL is a local variable in a context
2595 where that is forbidden. */
2596 if (parser
->local_variables_forbidden_p
2597 && local_variable_p (decl
))
2599 /* It might be that we only found DECL because we are
2600 trying to be generous with pre-ISO scoping rules.
2601 For example, consider:
2605 for (int i = 0; i < 10; ++i) {}
2606 extern void f(int j = i);
2609 Here, name look up will originally find the out
2610 of scope `i'. We need to issue a warning message,
2611 but then use the global `i'. */
2612 decl
= check_for_out_of_scope_variable (decl
);
2613 if (local_variable_p (decl
))
2615 error ("local variable `%D' may not appear in this context",
2617 return error_mark_node
;
2622 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2623 idk
, qualifying_class
,
2624 parser
->integral_constant_expression_p
,
2625 parser
->allow_non_integral_constant_expression_p
,
2626 &parser
->non_integral_constant_expression_p
,
2629 cp_parser_error (parser
, error_msg
);
2633 /* Anything else is an error. */
2635 cp_parser_error (parser
, "expected primary-expression");
2636 return error_mark_node
;
2640 /* Parse an id-expression.
2647 :: [opt] nested-name-specifier template [opt] unqualified-id
2649 :: operator-function-id
2652 Return a representation of the unqualified portion of the
2653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2654 a `::' or nested-name-specifier.
2656 Often, if the id-expression was a qualified-id, the caller will
2657 want to make a SCOPE_REF to represent the qualified-id. This
2658 function does not do this in order to avoid wastefully creating
2659 SCOPE_REFs when they are not required.
2661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2665 uninstantiated templates.
2667 If *TEMPLATE_P is non-NULL, it is set to true iff the
2668 `template' keyword is used to explicitly indicate that the entity
2669 named is a template.
2671 If DECLARATOR_P is true, the id-expression is appearing as part of
2672 a declarator, rather than as part of an expression. */
2675 cp_parser_id_expression (cp_parser
*parser
,
2676 bool template_keyword_p
,
2677 bool check_dependency_p
,
2681 bool global_scope_p
;
2682 bool nested_name_specifier_p
;
2684 /* Assume the `template' keyword was not used. */
2686 *template_p
= false;
2688 /* Look for the optional `::' operator. */
2690 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2692 /* Look for the optional nested-name-specifier. */
2693 nested_name_specifier_p
2694 = (cp_parser_nested_name_specifier_opt (parser
,
2695 /*typename_keyword_p=*/false,
2698 /*is_declarator=*/false)
2700 /* If there is a nested-name-specifier, then we are looking at
2701 the first qualified-id production. */
2702 if (nested_name_specifier_p
)
2705 tree saved_object_scope
;
2706 tree saved_qualifying_scope
;
2707 tree unqualified_id
;
2710 /* See if the next token is the `template' keyword. */
2712 template_p
= &is_template
;
2713 *template_p
= cp_parser_optional_template_keyword (parser
);
2714 /* Name lookup we do during the processing of the
2715 unqualified-id might obliterate SCOPE. */
2716 saved_scope
= parser
->scope
;
2717 saved_object_scope
= parser
->object_scope
;
2718 saved_qualifying_scope
= parser
->qualifying_scope
;
2719 /* Process the final unqualified-id. */
2720 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2723 /* Restore the SAVED_SCOPE for our caller. */
2724 parser
->scope
= saved_scope
;
2725 parser
->object_scope
= saved_object_scope
;
2726 parser
->qualifying_scope
= saved_qualifying_scope
;
2728 return unqualified_id
;
2730 /* Otherwise, if we are in global scope, then we are looking at one
2731 of the other qualified-id productions. */
2732 else if (global_scope_p
)
2737 /* Peek at the next token. */
2738 token
= cp_lexer_peek_token (parser
->lexer
);
2740 /* If it's an identifier, and the next token is not a "<", then
2741 we can avoid the template-id case. This is an optimization
2742 for this common case. */
2743 if (token
->type
== CPP_NAME
2744 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
2745 return cp_parser_identifier (parser
);
2747 cp_parser_parse_tentatively (parser
);
2748 /* Try a template-id. */
2749 id
= cp_parser_template_id (parser
,
2750 /*template_keyword_p=*/false,
2751 /*check_dependency_p=*/true,
2753 /* If that worked, we're done. */
2754 if (cp_parser_parse_definitely (parser
))
2757 /* Peek at the next token. (Changes in the token buffer may
2758 have invalidated the pointer obtained above.) */
2759 token
= cp_lexer_peek_token (parser
->lexer
);
2761 switch (token
->type
)
2764 return cp_parser_identifier (parser
);
2767 if (token
->keyword
== RID_OPERATOR
)
2768 return cp_parser_operator_function_id (parser
);
2772 cp_parser_error (parser
, "expected id-expression");
2773 return error_mark_node
;
2777 return cp_parser_unqualified_id (parser
, template_keyword_p
,
2778 /*check_dependency_p=*/true,
2782 /* Parse an unqualified-id.
2786 operator-function-id
2787 conversion-function-id
2791 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2792 keyword, in a construct like `A::template ...'.
2794 Returns a representation of unqualified-id. For the `identifier'
2795 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2796 production a BIT_NOT_EXPR is returned; the operand of the
2797 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2798 other productions, see the documentation accompanying the
2799 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2800 names are looked up in uninstantiated templates. If DECLARATOR_P
2801 is true, the unqualified-id is appearing as part of a declarator,
2802 rather than as part of an expression. */
2805 cp_parser_unqualified_id (cp_parser
* parser
,
2806 bool template_keyword_p
,
2807 bool check_dependency_p
,
2812 /* Peek at the next token. */
2813 token
= cp_lexer_peek_token (parser
->lexer
);
2815 switch (token
->type
)
2821 /* We don't know yet whether or not this will be a
2823 cp_parser_parse_tentatively (parser
);
2824 /* Try a template-id. */
2825 id
= cp_parser_template_id (parser
, template_keyword_p
,
2828 /* If it worked, we're done. */
2829 if (cp_parser_parse_definitely (parser
))
2831 /* Otherwise, it's an ordinary identifier. */
2832 return cp_parser_identifier (parser
);
2835 case CPP_TEMPLATE_ID
:
2836 return cp_parser_template_id (parser
, template_keyword_p
,
2843 tree qualifying_scope
;
2847 /* Consume the `~' token. */
2848 cp_lexer_consume_token (parser
->lexer
);
2849 /* Parse the class-name. The standard, as written, seems to
2852 template <typename T> struct S { ~S (); };
2853 template <typename T> S<T>::~S() {}
2855 is invalid, since `~' must be followed by a class-name, but
2856 `S<T>' is dependent, and so not known to be a class.
2857 That's not right; we need to look in uninstantiated
2858 templates. A further complication arises from:
2860 template <typename T> void f(T t) {
2864 Here, it is not possible to look up `T' in the scope of `T'
2865 itself. We must look in both the current scope, and the
2866 scope of the containing complete expression.
2868 Yet another issue is:
2877 The standard does not seem to say that the `S' in `~S'
2878 should refer to the type `S' and not the data member
2881 /* DR 244 says that we look up the name after the "~" in the
2882 same scope as we looked up the qualifying name. That idea
2883 isn't fully worked out; it's more complicated than that. */
2884 scope
= parser
->scope
;
2885 object_scope
= parser
->object_scope
;
2886 qualifying_scope
= parser
->qualifying_scope
;
2888 /* If the name is of the form "X::~X" it's OK. */
2889 if (scope
&& TYPE_P (scope
)
2890 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2891 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
2893 && (cp_lexer_peek_token (parser
->lexer
)->value
2894 == TYPE_IDENTIFIER (scope
)))
2896 cp_lexer_consume_token (parser
->lexer
);
2897 return build_nt (BIT_NOT_EXPR
, scope
);
2900 /* If there was an explicit qualification (S::~T), first look
2901 in the scope given by the qualification (i.e., S). */
2904 cp_parser_parse_tentatively (parser
);
2905 type_decl
= cp_parser_class_name (parser
,
2906 /*typename_keyword_p=*/false,
2907 /*template_keyword_p=*/false,
2909 /*check_dependency=*/false,
2910 /*class_head_p=*/false,
2912 if (cp_parser_parse_definitely (parser
))
2913 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2915 /* In "N::S::~S", look in "N" as well. */
2916 if (scope
&& qualifying_scope
)
2918 cp_parser_parse_tentatively (parser
);
2919 parser
->scope
= qualifying_scope
;
2920 parser
->object_scope
= NULL_TREE
;
2921 parser
->qualifying_scope
= NULL_TREE
;
2923 = cp_parser_class_name (parser
,
2924 /*typename_keyword_p=*/false,
2925 /*template_keyword_p=*/false,
2927 /*check_dependency=*/false,
2928 /*class_head_p=*/false,
2930 if (cp_parser_parse_definitely (parser
))
2931 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2933 /* In "p->S::~T", look in the scope given by "*p" as well. */
2934 else if (object_scope
)
2936 cp_parser_parse_tentatively (parser
);
2937 parser
->scope
= object_scope
;
2938 parser
->object_scope
= NULL_TREE
;
2939 parser
->qualifying_scope
= NULL_TREE
;
2941 = cp_parser_class_name (parser
,
2942 /*typename_keyword_p=*/false,
2943 /*template_keyword_p=*/false,
2945 /*check_dependency=*/false,
2946 /*class_head_p=*/false,
2948 if (cp_parser_parse_definitely (parser
))
2949 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2951 /* Look in the surrounding context. */
2952 parser
->scope
= NULL_TREE
;
2953 parser
->object_scope
= NULL_TREE
;
2954 parser
->qualifying_scope
= NULL_TREE
;
2956 = cp_parser_class_name (parser
,
2957 /*typename_keyword_p=*/false,
2958 /*template_keyword_p=*/false,
2960 /*check_dependency=*/false,
2961 /*class_head_p=*/false,
2963 /* If an error occurred, assume that the name of the
2964 destructor is the same as the name of the qualifying
2965 class. That allows us to keep parsing after running
2966 into ill-formed destructor names. */
2967 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
2968 return build_nt (BIT_NOT_EXPR
, scope
);
2969 else if (type_decl
== error_mark_node
)
2970 return error_mark_node
;
2974 A typedef-name that names a class shall not be used as the
2975 identifier in the declarator for a destructor declaration. */
2977 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
2978 && !DECL_SELF_REFERENCE_P (type_decl
))
2979 error ("typedef-name `%D' used as destructor declarator",
2982 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2986 if (token
->keyword
== RID_OPERATOR
)
2990 /* This could be a template-id, so we try that first. */
2991 cp_parser_parse_tentatively (parser
);
2992 /* Try a template-id. */
2993 id
= cp_parser_template_id (parser
, template_keyword_p
,
2994 /*check_dependency_p=*/true,
2996 /* If that worked, we're done. */
2997 if (cp_parser_parse_definitely (parser
))
2999 /* We still don't know whether we're looking at an
3000 operator-function-id or a conversion-function-id. */
3001 cp_parser_parse_tentatively (parser
);
3002 /* Try an operator-function-id. */
3003 id
= cp_parser_operator_function_id (parser
);
3004 /* If that didn't work, try a conversion-function-id. */
3005 if (!cp_parser_parse_definitely (parser
))
3006 id
= cp_parser_conversion_function_id (parser
);
3013 cp_parser_error (parser
, "expected unqualified-id");
3014 return error_mark_node
;
3018 /* Parse an (optional) nested-name-specifier.
3020 nested-name-specifier:
3021 class-or-namespace-name :: nested-name-specifier [opt]
3022 class-or-namespace-name :: template nested-name-specifier [opt]
3024 PARSER->SCOPE should be set appropriately before this function is
3025 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3026 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3029 Sets PARSER->SCOPE to the class (TYPE) or namespace
3030 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3031 it unchanged if there is no nested-name-specifier. Returns the new
3032 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3034 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3035 part of a declaration and/or decl-specifier. */
3038 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3039 bool typename_keyword_p
,
3040 bool check_dependency_p
,
3042 bool is_declaration
)
3044 bool success
= false;
3045 tree access_check
= NULL_TREE
;
3049 /* If the next token corresponds to a nested name specifier, there
3050 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3051 false, it may have been true before, in which case something
3052 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3053 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3054 CHECK_DEPENDENCY_P is false, we have to fall through into the
3056 if (check_dependency_p
3057 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3059 cp_parser_pre_parsed_nested_name_specifier (parser
);
3060 return parser
->scope
;
3063 /* Remember where the nested-name-specifier starts. */
3064 if (cp_parser_parsing_tentatively (parser
)
3065 && !cp_parser_committed_to_tentative_parse (parser
))
3067 token
= cp_lexer_peek_token (parser
->lexer
);
3068 start
= cp_lexer_token_difference (parser
->lexer
,
3069 parser
->lexer
->first_token
,
3075 push_deferring_access_checks (dk_deferred
);
3081 tree saved_qualifying_scope
;
3082 bool template_keyword_p
;
3084 /* Spot cases that cannot be the beginning of a
3085 nested-name-specifier. */
3086 token
= cp_lexer_peek_token (parser
->lexer
);
3088 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3089 the already parsed nested-name-specifier. */
3090 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3092 /* Grab the nested-name-specifier and continue the loop. */
3093 cp_parser_pre_parsed_nested_name_specifier (parser
);
3098 /* Spot cases that cannot be the beginning of a
3099 nested-name-specifier. On the second and subsequent times
3100 through the loop, we look for the `template' keyword. */
3101 if (success
&& token
->keyword
== RID_TEMPLATE
)
3103 /* A template-id can start a nested-name-specifier. */
3104 else if (token
->type
== CPP_TEMPLATE_ID
)
3108 /* If the next token is not an identifier, then it is
3109 definitely not a class-or-namespace-name. */
3110 if (token
->type
!= CPP_NAME
)
3112 /* If the following token is neither a `<' (to begin a
3113 template-id), nor a `::', then we are not looking at a
3114 nested-name-specifier. */
3115 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3116 if (token
->type
!= CPP_LESS
&& token
->type
!= CPP_SCOPE
)
3120 /* The nested-name-specifier is optional, so we parse
3122 cp_parser_parse_tentatively (parser
);
3124 /* Look for the optional `template' keyword, if this isn't the
3125 first time through the loop. */
3127 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3129 template_keyword_p
= false;
3131 /* Save the old scope since the name lookup we are about to do
3132 might destroy it. */
3133 old_scope
= parser
->scope
;
3134 saved_qualifying_scope
= parser
->qualifying_scope
;
3135 /* Parse the qualifying entity. */
3137 = cp_parser_class_or_namespace_name (parser
,
3143 /* Look for the `::' token. */
3144 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3146 /* If we found what we wanted, we keep going; otherwise, we're
3148 if (!cp_parser_parse_definitely (parser
))
3150 bool error_p
= false;
3152 /* Restore the OLD_SCOPE since it was valid before the
3153 failed attempt at finding the last
3154 class-or-namespace-name. */
3155 parser
->scope
= old_scope
;
3156 parser
->qualifying_scope
= saved_qualifying_scope
;
3157 /* If the next token is an identifier, and the one after
3158 that is a `::', then any valid interpretation would have
3159 found a class-or-namespace-name. */
3160 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3161 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3163 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3166 token
= cp_lexer_consume_token (parser
->lexer
);
3171 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3172 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3173 error ("`%D' used without template parameters",
3176 cp_parser_name_lookup_error
3177 (parser
, token
->value
, decl
,
3178 "is not a class or namespace");
3179 parser
->scope
= NULL_TREE
;
3181 /* Treat this as a successful nested-name-specifier
3186 If the name found is not a class-name (clause
3187 _class_) or namespace-name (_namespace.def_), the
3188 program is ill-formed. */
3191 cp_lexer_consume_token (parser
->lexer
);
3196 /* We've found one valid nested-name-specifier. */
3198 /* Make sure we look in the right scope the next time through
3200 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3201 ? TREE_TYPE (new_scope
)
3203 /* If it is a class scope, try to complete it; we are about to
3204 be looking up names inside the class. */
3205 if (TYPE_P (parser
->scope
)
3206 /* Since checking types for dependency can be expensive,
3207 avoid doing it if the type is already complete. */
3208 && !COMPLETE_TYPE_P (parser
->scope
)
3209 /* Do not try to complete dependent types. */
3210 && !dependent_type_p (parser
->scope
))
3211 complete_type (parser
->scope
);
3214 /* Retrieve any deferred checks. Do not pop this access checks yet
3215 so the memory will not be reclaimed during token replacing below. */
3216 access_check
= get_deferred_access_checks ();
3218 /* If parsing tentatively, replace the sequence of tokens that makes
3219 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3220 token. That way, should we re-parse the token stream, we will
3221 not have to repeat the effort required to do the parse, nor will
3222 we issue duplicate error messages. */
3223 if (success
&& start
>= 0)
3225 /* Find the token that corresponds to the start of the
3227 token
= cp_lexer_advance_token (parser
->lexer
,
3228 parser
->lexer
->first_token
,
3231 /* Reset the contents of the START token. */
3232 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3233 token
->value
= build_tree_list (access_check
, parser
->scope
);
3234 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3235 token
->keyword
= RID_MAX
;
3236 /* Purge all subsequent tokens. */
3237 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3240 pop_deferring_access_checks ();
3241 return success
? parser
->scope
: NULL_TREE
;
3244 /* Parse a nested-name-specifier. See
3245 cp_parser_nested_name_specifier_opt for details. This function
3246 behaves identically, except that it will an issue an error if no
3247 nested-name-specifier is present, and it will return
3248 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3252 cp_parser_nested_name_specifier (cp_parser
*parser
,
3253 bool typename_keyword_p
,
3254 bool check_dependency_p
,
3256 bool is_declaration
)
3260 /* Look for the nested-name-specifier. */
3261 scope
= cp_parser_nested_name_specifier_opt (parser
,
3266 /* If it was not present, issue an error message. */
3269 cp_parser_error (parser
, "expected nested-name-specifier");
3270 parser
->scope
= NULL_TREE
;
3271 return error_mark_node
;
3277 /* Parse a class-or-namespace-name.
3279 class-or-namespace-name:
3283 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3284 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3285 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3286 TYPE_P is TRUE iff the next name should be taken as a class-name,
3287 even the same name is declared to be another entity in the same
3290 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3291 specified by the class-or-namespace-name. If neither is found the
3292 ERROR_MARK_NODE is returned. */
3295 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3296 bool typename_keyword_p
,
3297 bool template_keyword_p
,
3298 bool check_dependency_p
,
3300 bool is_declaration
)
3303 tree saved_qualifying_scope
;
3304 tree saved_object_scope
;
3308 /* Before we try to parse the class-name, we must save away the
3309 current PARSER->SCOPE since cp_parser_class_name will destroy
3311 saved_scope
= parser
->scope
;
3312 saved_qualifying_scope
= parser
->qualifying_scope
;
3313 saved_object_scope
= parser
->object_scope
;
3314 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3315 there is no need to look for a namespace-name. */
3316 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3318 cp_parser_parse_tentatively (parser
);
3319 scope
= cp_parser_class_name (parser
,
3324 /*class_head_p=*/false,
3326 /* If that didn't work, try for a namespace-name. */
3327 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3329 /* Restore the saved scope. */
3330 parser
->scope
= saved_scope
;
3331 parser
->qualifying_scope
= saved_qualifying_scope
;
3332 parser
->object_scope
= saved_object_scope
;
3333 /* If we are not looking at an identifier followed by the scope
3334 resolution operator, then this is not part of a
3335 nested-name-specifier. (Note that this function is only used
3336 to parse the components of a nested-name-specifier.) */
3337 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3338 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3339 return error_mark_node
;
3340 scope
= cp_parser_namespace_name (parser
);
3346 /* Parse a postfix-expression.
3350 postfix-expression [ expression ]
3351 postfix-expression ( expression-list [opt] )
3352 simple-type-specifier ( expression-list [opt] )
3353 typename :: [opt] nested-name-specifier identifier
3354 ( expression-list [opt] )
3355 typename :: [opt] nested-name-specifier template [opt] template-id
3356 ( expression-list [opt] )
3357 postfix-expression . template [opt] id-expression
3358 postfix-expression -> template [opt] id-expression
3359 postfix-expression . pseudo-destructor-name
3360 postfix-expression -> pseudo-destructor-name
3361 postfix-expression ++
3362 postfix-expression --
3363 dynamic_cast < type-id > ( expression )
3364 static_cast < type-id > ( expression )
3365 reinterpret_cast < type-id > ( expression )
3366 const_cast < type-id > ( expression )
3367 typeid ( expression )
3373 ( type-id ) { initializer-list , [opt] }
3375 This extension is a GNU version of the C99 compound-literal
3376 construct. (The C99 grammar uses `type-name' instead of `type-id',
3377 but they are essentially the same concept.)
3379 If ADDRESS_P is true, the postfix expression is the operand of the
3382 Returns a representation of the expression. */
3385 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3389 cp_id_kind idk
= CP_ID_KIND_NONE
;
3390 tree postfix_expression
= NULL_TREE
;
3391 /* Non-NULL only if the current postfix-expression can be used to
3392 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3393 class used to qualify the member. */
3394 tree qualifying_class
= NULL_TREE
;
3396 /* Peek at the next token. */
3397 token
= cp_lexer_peek_token (parser
->lexer
);
3398 /* Some of the productions are determined by keywords. */
3399 keyword
= token
->keyword
;
3409 const char *saved_message
;
3411 /* All of these can be handled in the same way from the point
3412 of view of parsing. Begin by consuming the token
3413 identifying the cast. */
3414 cp_lexer_consume_token (parser
->lexer
);
3416 /* New types cannot be defined in the cast. */
3417 saved_message
= parser
->type_definition_forbidden_message
;
3418 parser
->type_definition_forbidden_message
3419 = "types may not be defined in casts";
3421 /* Look for the opening `<'. */
3422 cp_parser_require (parser
, CPP_LESS
, "`<'");
3423 /* Parse the type to which we are casting. */
3424 type
= cp_parser_type_id (parser
);
3425 /* Look for the closing `>'. */
3426 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3427 /* Restore the old message. */
3428 parser
->type_definition_forbidden_message
= saved_message
;
3430 /* And the expression which is being cast. */
3431 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3432 expression
= cp_parser_expression (parser
);
3433 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3435 /* Only type conversions to integral or enumeration types
3436 can be used in constant-expressions. */
3437 if (parser
->integral_constant_expression_p
3438 && !dependent_type_p (type
)
3439 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3440 /* A cast to pointer or reference type is allowed in the
3441 implementation of "offsetof". */
3442 && !(parser
->in_offsetof_p
&& POINTER_TYPE_P (type
)))
3444 if (!parser
->allow_non_integral_constant_expression_p
)
3445 return (cp_parser_non_integral_constant_expression
3446 ("a cast to a type other than an integral or "
3447 "enumeration type"));
3448 parser
->non_integral_constant_expression_p
= true;
3455 = build_dynamic_cast (type
, expression
);
3459 = build_static_cast (type
, expression
);
3463 = build_reinterpret_cast (type
, expression
);
3467 = build_const_cast (type
, expression
);
3478 const char *saved_message
;
3479 bool saved_in_type_id_in_expr_p
;
3481 /* Consume the `typeid' token. */
3482 cp_lexer_consume_token (parser
->lexer
);
3483 /* Look for the `(' token. */
3484 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3485 /* Types cannot be defined in a `typeid' expression. */
3486 saved_message
= parser
->type_definition_forbidden_message
;
3487 parser
->type_definition_forbidden_message
3488 = "types may not be defined in a `typeid\' expression";
3489 /* We can't be sure yet whether we're looking at a type-id or an
3491 cp_parser_parse_tentatively (parser
);
3492 /* Try a type-id first. */
3493 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3494 parser
->in_type_id_in_expr_p
= true;
3495 type
= cp_parser_type_id (parser
);
3496 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3497 /* Look for the `)' token. Otherwise, we can't be sure that
3498 we're not looking at an expression: consider `typeid (int
3499 (3))', for example. */
3500 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3501 /* If all went well, simply lookup the type-id. */
3502 if (cp_parser_parse_definitely (parser
))
3503 postfix_expression
= get_typeid (type
);
3504 /* Otherwise, fall back to the expression variant. */
3509 /* Look for an expression. */
3510 expression
= cp_parser_expression (parser
);
3511 /* Compute its typeid. */
3512 postfix_expression
= build_typeid (expression
);
3513 /* Look for the `)' token. */
3514 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3517 /* Restore the saved message. */
3518 parser
->type_definition_forbidden_message
= saved_message
;
3524 bool template_p
= false;
3528 /* Consume the `typename' token. */
3529 cp_lexer_consume_token (parser
->lexer
);
3530 /* Look for the optional `::' operator. */
3531 cp_parser_global_scope_opt (parser
,
3532 /*current_scope_valid_p=*/false);
3533 /* Look for the nested-name-specifier. */
3534 cp_parser_nested_name_specifier (parser
,
3535 /*typename_keyword_p=*/true,
3536 /*check_dependency_p=*/true,
3538 /*is_declaration=*/true);
3539 /* Look for the optional `template' keyword. */
3540 template_p
= cp_parser_optional_template_keyword (parser
);
3541 /* We don't know whether we're looking at a template-id or an
3543 cp_parser_parse_tentatively (parser
);
3544 /* Try a template-id. */
3545 id
= cp_parser_template_id (parser
, template_p
,
3546 /*check_dependency_p=*/true,
3547 /*is_declaration=*/true);
3548 /* If that didn't work, try an identifier. */
3549 if (!cp_parser_parse_definitely (parser
))
3550 id
= cp_parser_identifier (parser
);
3551 /* Create a TYPENAME_TYPE to represent the type to which the
3552 functional cast is being performed. */
3553 type
= make_typename_type (parser
->scope
, id
,
3556 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3564 /* If the next thing is a simple-type-specifier, we may be
3565 looking at a functional cast. We could also be looking at
3566 an id-expression. So, we try the functional cast, and if
3567 that doesn't work we fall back to the primary-expression. */
3568 cp_parser_parse_tentatively (parser
);
3569 /* Look for the simple-type-specifier. */
3570 type
= cp_parser_simple_type_specifier (parser
,
3571 CP_PARSER_FLAGS_NONE
,
3572 /*identifier_p=*/false);
3573 /* Parse the cast itself. */
3574 if (!cp_parser_error_occurred (parser
))
3576 = cp_parser_functional_cast (parser
, type
);
3577 /* If that worked, we're done. */
3578 if (cp_parser_parse_definitely (parser
))
3581 /* If the functional-cast didn't work out, try a
3582 compound-literal. */
3583 if (cp_parser_allow_gnu_extensions_p (parser
)
3584 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3586 tree initializer_list
= NULL_TREE
;
3587 bool saved_in_type_id_in_expr_p
;
3589 cp_parser_parse_tentatively (parser
);
3590 /* Consume the `('. */
3591 cp_lexer_consume_token (parser
->lexer
);
3592 /* Parse the type. */
3593 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3594 parser
->in_type_id_in_expr_p
= true;
3595 type
= cp_parser_type_id (parser
);
3596 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3597 /* Look for the `)'. */
3598 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3599 /* Look for the `{'. */
3600 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3601 /* If things aren't going well, there's no need to
3603 if (!cp_parser_error_occurred (parser
))
3605 bool non_constant_p
;
3606 /* Parse the initializer-list. */
3608 = cp_parser_initializer_list (parser
, &non_constant_p
);
3609 /* Allow a trailing `,'. */
3610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3611 cp_lexer_consume_token (parser
->lexer
);
3612 /* Look for the final `}'. */
3613 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3615 /* If that worked, we're definitely looking at a
3616 compound-literal expression. */
3617 if (cp_parser_parse_definitely (parser
))
3619 /* Warn the user that a compound literal is not
3620 allowed in standard C++. */
3622 pedwarn ("ISO C++ forbids compound-literals");
3623 /* Form the representation of the compound-literal. */
3625 = finish_compound_literal (type
, initializer_list
);
3630 /* It must be a primary-expression. */
3631 postfix_expression
= cp_parser_primary_expression (parser
,
3638 /* If we were avoiding committing to the processing of a
3639 qualified-id until we knew whether or not we had a
3640 pointer-to-member, we now know. */
3641 if (qualifying_class
)
3645 /* Peek at the next token. */
3646 token
= cp_lexer_peek_token (parser
->lexer
);
3647 done
= (token
->type
!= CPP_OPEN_SQUARE
3648 && token
->type
!= CPP_OPEN_PAREN
3649 && token
->type
!= CPP_DOT
3650 && token
->type
!= CPP_DEREF
3651 && token
->type
!= CPP_PLUS_PLUS
3652 && token
->type
!= CPP_MINUS_MINUS
);
3654 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3659 return postfix_expression
;
3662 /* Keep looping until the postfix-expression is complete. */
3665 if (idk
== CP_ID_KIND_UNQUALIFIED
3666 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3667 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3668 /* It is not a Koenig lookup function call. */
3670 = unqualified_name_lookup_error (postfix_expression
);
3672 /* Peek at the next token. */
3673 token
= cp_lexer_peek_token (parser
->lexer
);
3675 switch (token
->type
)
3677 case CPP_OPEN_SQUARE
:
3678 /* postfix-expression [ expression ] */
3682 /* Consume the `[' token. */
3683 cp_lexer_consume_token (parser
->lexer
);
3684 /* Parse the index expression. */
3685 index
= cp_parser_expression (parser
);
3686 /* Look for the closing `]'. */
3687 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
3689 /* Build the ARRAY_REF. */
3691 = grok_array_decl (postfix_expression
, index
);
3692 idk
= CP_ID_KIND_NONE
;
3693 /* Array references are not permitted in
3694 constant-expressions. */
3695 if (parser
->integral_constant_expression_p
)
3697 if (!parser
->allow_non_integral_constant_expression_p
)
3699 = cp_parser_non_integral_constant_expression ("an array reference");
3700 parser
->non_integral_constant_expression_p
= true;
3705 case CPP_OPEN_PAREN
:
3706 /* postfix-expression ( expression-list [opt] ) */
3709 tree args
= (cp_parser_parenthesized_expression_list
3710 (parser
, false, /*non_constant_p=*/NULL
));
3712 if (args
== error_mark_node
)
3714 postfix_expression
= error_mark_node
;
3718 /* Function calls are not permitted in
3719 constant-expressions. */
3720 if (parser
->integral_constant_expression_p
)
3722 if (!parser
->allow_non_integral_constant_expression_p
)
3725 = cp_parser_non_integral_constant_expression ("a function call");
3728 parser
->non_integral_constant_expression_p
= true;
3732 if (idk
== CP_ID_KIND_UNQUALIFIED
)
3735 && (is_overloaded_fn (postfix_expression
)
3736 || DECL_P (postfix_expression
)
3737 || TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
))
3741 = perform_koenig_lookup (postfix_expression
, args
);
3743 else if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3745 = unqualified_fn_lookup_error (postfix_expression
);
3748 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
3750 tree instance
= TREE_OPERAND (postfix_expression
, 0);
3751 tree fn
= TREE_OPERAND (postfix_expression
, 1);
3753 if (processing_template_decl
3754 && (type_dependent_expression_p (instance
)
3755 || (!BASELINK_P (fn
)
3756 && TREE_CODE (fn
) != FIELD_DECL
)
3757 || type_dependent_expression_p (fn
)
3758 || any_type_dependent_arguments_p (args
)))
3761 = build_min_nt (CALL_EXPR
, postfix_expression
, args
);
3766 = (build_new_method_call
3767 (instance
, fn
, args
, NULL_TREE
,
3768 (idk
== CP_ID_KIND_QUALIFIED
3769 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3771 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
3772 || TREE_CODE (postfix_expression
) == MEMBER_REF
3773 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
3774 postfix_expression
= (build_offset_ref_call_from_tree
3775 (postfix_expression
, args
));
3776 else if (idk
== CP_ID_KIND_QUALIFIED
)
3777 /* A call to a static class member, or a namespace-scope
3780 = finish_call_expr (postfix_expression
, args
,
3781 /*disallow_virtual=*/true,
3784 /* All other function calls. */
3786 = finish_call_expr (postfix_expression
, args
,
3787 /*disallow_virtual=*/false,
3790 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3791 idk
= CP_ID_KIND_NONE
;
3797 /* postfix-expression . template [opt] id-expression
3798 postfix-expression . pseudo-destructor-name
3799 postfix-expression -> template [opt] id-expression
3800 postfix-expression -> pseudo-destructor-name */
3805 tree scope
= NULL_TREE
;
3806 enum cpp_ttype token_type
= token
->type
;
3808 /* If this is a `->' operator, dereference the pointer. */
3809 if (token
->type
== CPP_DEREF
)
3810 postfix_expression
= build_x_arrow (postfix_expression
);
3811 /* Check to see whether or not the expression is
3813 dependent_p
= type_dependent_expression_p (postfix_expression
);
3814 /* The identifier following the `->' or `.' is not
3816 parser
->scope
= NULL_TREE
;
3817 parser
->qualifying_scope
= NULL_TREE
;
3818 parser
->object_scope
= NULL_TREE
;
3819 idk
= CP_ID_KIND_NONE
;
3820 /* Enter the scope corresponding to the type of the object
3821 given by the POSTFIX_EXPRESSION. */
3823 && TREE_TYPE (postfix_expression
) != NULL_TREE
)
3825 scope
= TREE_TYPE (postfix_expression
);
3826 /* According to the standard, no expression should
3827 ever have reference type. Unfortunately, we do not
3828 currently match the standard in this respect in
3829 that our internal representation of an expression
3830 may have reference type even when the standard says
3831 it does not. Therefore, we have to manually obtain
3832 the underlying type here. */
3833 scope
= non_reference (scope
);
3834 /* The type of the POSTFIX_EXPRESSION must be
3836 scope
= complete_type_or_else (scope
, NULL_TREE
);
3837 /* Let the name lookup machinery know that we are
3838 processing a class member access expression. */
3839 parser
->context
->object_type
= scope
;
3840 /* If something went wrong, we want to be able to
3841 discern that case, as opposed to the case where
3842 there was no SCOPE due to the type of expression
3845 scope
= error_mark_node
;
3848 /* Consume the `.' or `->' operator. */
3849 cp_lexer_consume_token (parser
->lexer
);
3850 /* If the SCOPE is not a scalar type, we are looking at an
3851 ordinary class member access expression, rather than a
3852 pseudo-destructor-name. */
3853 if (!scope
|| !SCALAR_TYPE_P (scope
))
3855 template_p
= cp_parser_optional_template_keyword (parser
);
3856 /* Parse the id-expression. */
3857 name
= cp_parser_id_expression (parser
,
3859 /*check_dependency_p=*/true,
3860 /*template_p=*/NULL
,
3861 /*declarator_p=*/false);
3862 /* In general, build a SCOPE_REF if the member name is
3863 qualified. However, if the name was not dependent
3864 and has already been resolved; there is no need to
3865 build the SCOPE_REF. For example;
3867 struct X { void f(); };
3868 template <typename T> void f(T* t) { t->X::f(); }
3870 Even though "t" is dependent, "X::f" is not and has
3871 been resolved to a BASELINK; there is no need to
3872 include scope information. */
3874 /* But we do need to remember that there was an explicit
3875 scope for virtual function calls. */
3877 idk
= CP_ID_KIND_QUALIFIED
;
3879 if (name
!= error_mark_node
3880 && !BASELINK_P (name
)
3883 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
3884 parser
->scope
= NULL_TREE
;
3885 parser
->qualifying_scope
= NULL_TREE
;
3886 parser
->object_scope
= NULL_TREE
;
3889 = finish_class_member_access_expr (postfix_expression
, name
);
3891 /* Otherwise, try the pseudo-destructor-name production. */
3897 /* Parse the pseudo-destructor-name. */
3898 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
3899 /* Form the call. */
3901 = finish_pseudo_destructor_expr (postfix_expression
,
3902 s
, TREE_TYPE (type
));
3905 /* We no longer need to look up names in the scope of the
3906 object on the left-hand side of the `.' or `->'
3908 parser
->context
->object_type
= NULL_TREE
;
3909 /* These operators may not appear in constant-expressions. */
3910 if (parser
->integral_constant_expression_p
3911 /* The "->" operator is allowed in the implementation
3912 of "offsetof". The "." operator may appear in the
3913 name of the member. */
3914 && !parser
->in_offsetof_p
)
3916 if (!parser
->allow_non_integral_constant_expression_p
)
3918 = (cp_parser_non_integral_constant_expression
3919 (token_type
== CPP_DEREF
? "'->'" : "`.'"));
3920 parser
->non_integral_constant_expression_p
= true;
3926 /* postfix-expression ++ */
3927 /* Consume the `++' token. */
3928 cp_lexer_consume_token (parser
->lexer
);
3929 /* Generate a representation for the complete expression. */
3931 = finish_increment_expr (postfix_expression
,
3932 POSTINCREMENT_EXPR
);
3933 /* Increments may not appear in constant-expressions. */
3934 if (parser
->integral_constant_expression_p
)
3936 if (!parser
->allow_non_integral_constant_expression_p
)
3938 = cp_parser_non_integral_constant_expression ("an increment");
3939 parser
->non_integral_constant_expression_p
= true;
3941 idk
= CP_ID_KIND_NONE
;
3944 case CPP_MINUS_MINUS
:
3945 /* postfix-expression -- */
3946 /* Consume the `--' token. */
3947 cp_lexer_consume_token (parser
->lexer
);
3948 /* Generate a representation for the complete expression. */
3950 = finish_increment_expr (postfix_expression
,
3951 POSTDECREMENT_EXPR
);
3952 /* Decrements may not appear in constant-expressions. */
3953 if (parser
->integral_constant_expression_p
)
3955 if (!parser
->allow_non_integral_constant_expression_p
)
3957 = cp_parser_non_integral_constant_expression ("a decrement");
3958 parser
->non_integral_constant_expression_p
= true;
3960 idk
= CP_ID_KIND_NONE
;
3964 return postfix_expression
;
3968 /* We should never get here. */
3970 return error_mark_node
;
3973 /* Parse a parenthesized expression-list.
3976 assignment-expression
3977 expression-list, assignment-expression
3982 identifier, expression-list
3984 Returns a TREE_LIST. The TREE_VALUE of each node is a
3985 representation of an assignment-expression. Note that a TREE_LIST
3986 is returned even if there is only a single expression in the list.
3987 error_mark_node is returned if the ( and or ) are
3988 missing. NULL_TREE is returned on no expressions. The parentheses
3989 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3990 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3991 indicates whether or not all of the expressions in the list were
3995 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
3996 bool is_attribute_list
,
3997 bool *non_constant_p
)
3999 tree expression_list
= NULL_TREE
;
4000 tree identifier
= NULL_TREE
;
4002 /* Assume all the expressions will be constant. */
4004 *non_constant_p
= false;
4006 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4007 return error_mark_node
;
4009 /* Consume expressions until there are no more. */
4010 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4015 /* At the beginning of attribute lists, check to see if the
4016 next token is an identifier. */
4017 if (is_attribute_list
4018 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4022 /* Consume the identifier. */
4023 token
= cp_lexer_consume_token (parser
->lexer
);
4024 /* Save the identifier. */
4025 identifier
= token
->value
;
4029 /* Parse the next assignment-expression. */
4032 bool expr_non_constant_p
;
4033 expr
= (cp_parser_constant_expression
4034 (parser
, /*allow_non_constant_p=*/true,
4035 &expr_non_constant_p
));
4036 if (expr_non_constant_p
)
4037 *non_constant_p
= true;
4040 expr
= cp_parser_assignment_expression (parser
);
4042 /* Add it to the list. We add error_mark_node
4043 expressions to the list, so that we can still tell if
4044 the correct form for a parenthesized expression-list
4045 is found. That gives better errors. */
4046 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4048 if (expr
== error_mark_node
)
4052 /* After the first item, attribute lists look the same as
4053 expression lists. */
4054 is_attribute_list
= false;
4057 /* If the next token isn't a `,', then we are done. */
4058 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4061 /* Otherwise, consume the `,' and keep going. */
4062 cp_lexer_consume_token (parser
->lexer
);
4065 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4070 /* We try and resync to an unnested comma, as that will give the
4071 user better diagnostics. */
4072 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4073 /*recovering=*/true,
4075 /*consume_paren=*/true);
4079 return error_mark_node
;
4082 /* We built up the list in reverse order so we must reverse it now. */
4083 expression_list
= nreverse (expression_list
);
4085 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4087 return expression_list
;
4090 /* Parse a pseudo-destructor-name.
4092 pseudo-destructor-name:
4093 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4094 :: [opt] nested-name-specifier template template-id :: ~ type-name
4095 :: [opt] nested-name-specifier [opt] ~ type-name
4097 If either of the first two productions is used, sets *SCOPE to the
4098 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4099 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4100 or ERROR_MARK_NODE if no type-name is present. */
4103 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4107 bool nested_name_specifier_p
;
4109 /* Look for the optional `::' operator. */
4110 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4111 /* Look for the optional nested-name-specifier. */
4112 nested_name_specifier_p
4113 = (cp_parser_nested_name_specifier_opt (parser
,
4114 /*typename_keyword_p=*/false,
4115 /*check_dependency_p=*/true,
4117 /*is_declaration=*/true)
4119 /* Now, if we saw a nested-name-specifier, we might be doing the
4120 second production. */
4121 if (nested_name_specifier_p
4122 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4124 /* Consume the `template' keyword. */
4125 cp_lexer_consume_token (parser
->lexer
);
4126 /* Parse the template-id. */
4127 cp_parser_template_id (parser
,
4128 /*template_keyword_p=*/true,
4129 /*check_dependency_p=*/false,
4130 /*is_declaration=*/true);
4131 /* Look for the `::' token. */
4132 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4134 /* If the next token is not a `~', then there might be some
4135 additional qualification. */
4136 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4138 /* Look for the type-name. */
4139 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4140 /* Look for the `::' token. */
4141 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4146 /* Look for the `~'. */
4147 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4148 /* Look for the type-name again. We are not responsible for
4149 checking that it matches the first type-name. */
4150 *type
= cp_parser_type_name (parser
);
4153 /* Parse a unary-expression.
4159 unary-operator cast-expression
4160 sizeof unary-expression
4168 __extension__ cast-expression
4169 __alignof__ unary-expression
4170 __alignof__ ( type-id )
4171 __real__ cast-expression
4172 __imag__ cast-expression
4175 ADDRESS_P is true iff the unary-expression is appearing as the
4176 operand of the `&' operator.
4178 Returns a representation of the expression. */
4181 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4184 enum tree_code unary_operator
;
4186 /* Peek at the next token. */
4187 token
= cp_lexer_peek_token (parser
->lexer
);
4188 /* Some keywords give away the kind of expression. */
4189 if (token
->type
== CPP_KEYWORD
)
4191 enum rid keyword
= token
->keyword
;
4201 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4202 /* Consume the token. */
4203 cp_lexer_consume_token (parser
->lexer
);
4204 /* Parse the operand. */
4205 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4207 if (TYPE_P (operand
))
4208 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4210 return cxx_sizeof_or_alignof_expr (operand
, op
);
4214 return cp_parser_new_expression (parser
);
4217 return cp_parser_delete_expression (parser
);
4221 /* The saved value of the PEDANTIC flag. */
4225 /* Save away the PEDANTIC flag. */
4226 cp_parser_extension_opt (parser
, &saved_pedantic
);
4227 /* Parse the cast-expression. */
4228 expr
= cp_parser_simple_cast_expression (parser
);
4229 /* Restore the PEDANTIC flag. */
4230 pedantic
= saved_pedantic
;
4240 /* Consume the `__real__' or `__imag__' token. */
4241 cp_lexer_consume_token (parser
->lexer
);
4242 /* Parse the cast-expression. */
4243 expression
= cp_parser_simple_cast_expression (parser
);
4244 /* Create the complete representation. */
4245 return build_x_unary_op ((keyword
== RID_REALPART
4246 ? REALPART_EXPR
: IMAGPART_EXPR
),
4256 /* Look for the `:: new' and `:: delete', which also signal the
4257 beginning of a new-expression, or delete-expression,
4258 respectively. If the next token is `::', then it might be one of
4260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4264 /* See if the token after the `::' is one of the keywords in
4265 which we're interested. */
4266 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4267 /* If it's `new', we have a new-expression. */
4268 if (keyword
== RID_NEW
)
4269 return cp_parser_new_expression (parser
);
4270 /* Similarly, for `delete'. */
4271 else if (keyword
== RID_DELETE
)
4272 return cp_parser_delete_expression (parser
);
4275 /* Look for a unary operator. */
4276 unary_operator
= cp_parser_unary_operator (token
);
4277 /* The `++' and `--' operators can be handled similarly, even though
4278 they are not technically unary-operators in the grammar. */
4279 if (unary_operator
== ERROR_MARK
)
4281 if (token
->type
== CPP_PLUS_PLUS
)
4282 unary_operator
= PREINCREMENT_EXPR
;
4283 else if (token
->type
== CPP_MINUS_MINUS
)
4284 unary_operator
= PREDECREMENT_EXPR
;
4285 /* Handle the GNU address-of-label extension. */
4286 else if (cp_parser_allow_gnu_extensions_p (parser
)
4287 && token
->type
== CPP_AND_AND
)
4291 /* Consume the '&&' token. */
4292 cp_lexer_consume_token (parser
->lexer
);
4293 /* Look for the identifier. */
4294 identifier
= cp_parser_identifier (parser
);
4295 /* Create an expression representing the address. */
4296 return finish_label_address_expr (identifier
);
4299 if (unary_operator
!= ERROR_MARK
)
4301 tree cast_expression
;
4302 tree expression
= error_mark_node
;
4303 const char *non_constant_p
= NULL
;
4305 /* Consume the operator token. */
4306 token
= cp_lexer_consume_token (parser
->lexer
);
4307 /* Parse the cast-expression. */
4309 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4310 /* Now, build an appropriate representation. */
4311 switch (unary_operator
)
4314 non_constant_p
= "`*'";
4315 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4319 /* The "&" operator is allowed in the implementation of
4321 if (!parser
->in_offsetof_p
)
4322 non_constant_p
= "`&'";
4325 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4328 case PREINCREMENT_EXPR
:
4329 case PREDECREMENT_EXPR
:
4330 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4335 case TRUTH_NOT_EXPR
:
4336 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4343 if (non_constant_p
&& parser
->integral_constant_expression_p
)
4345 if (!parser
->allow_non_integral_constant_expression_p
)
4346 return cp_parser_non_integral_constant_expression (non_constant_p
);
4347 parser
->non_integral_constant_expression_p
= true;
4353 return cp_parser_postfix_expression (parser
, address_p
);
4356 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4357 unary-operator, the corresponding tree code is returned. */
4359 static enum tree_code
4360 cp_parser_unary_operator (cp_token
* token
)
4362 switch (token
->type
)
4365 return INDIRECT_REF
;
4371 return CONVERT_EXPR
;
4377 return TRUTH_NOT_EXPR
;
4380 return BIT_NOT_EXPR
;
4387 /* Parse a new-expression.
4390 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4391 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4393 Returns a representation of the expression. */
4396 cp_parser_new_expression (cp_parser
* parser
)
4398 bool global_scope_p
;
4403 /* Look for the optional `::' operator. */
4405 = (cp_parser_global_scope_opt (parser
,
4406 /*current_scope_valid_p=*/false)
4408 /* Look for the `new' operator. */
4409 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4410 /* There's no easy way to tell a new-placement from the
4411 `( type-id )' construct. */
4412 cp_parser_parse_tentatively (parser
);
4413 /* Look for a new-placement. */
4414 placement
= cp_parser_new_placement (parser
);
4415 /* If that didn't work out, there's no new-placement. */
4416 if (!cp_parser_parse_definitely (parser
))
4417 placement
= NULL_TREE
;
4419 /* If the next token is a `(', then we have a parenthesized
4421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4423 /* Consume the `('. */
4424 cp_lexer_consume_token (parser
->lexer
);
4425 /* Parse the type-id. */
4426 type
= cp_parser_type_id (parser
);
4427 /* Look for the closing `)'. */
4428 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4430 /* Otherwise, there must be a new-type-id. */
4432 type
= cp_parser_new_type_id (parser
);
4434 /* If the next token is a `(', then we have a new-initializer. */
4435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4436 initializer
= cp_parser_new_initializer (parser
);
4438 initializer
= NULL_TREE
;
4440 /* Create a representation of the new-expression. */
4441 return build_new (placement
, type
, initializer
, global_scope_p
);
4444 /* Parse a new-placement.
4449 Returns the same representation as for an expression-list. */
4452 cp_parser_new_placement (cp_parser
* parser
)
4454 tree expression_list
;
4456 /* Parse the expression-list. */
4457 expression_list
= (cp_parser_parenthesized_expression_list
4458 (parser
, false, /*non_constant_p=*/NULL
));
4460 return expression_list
;
4463 /* Parse a new-type-id.
4466 type-specifier-seq new-declarator [opt]
4468 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4469 and whose TREE_VALUE is the new-declarator. */
4472 cp_parser_new_type_id (cp_parser
* parser
)
4474 tree type_specifier_seq
;
4476 const char *saved_message
;
4478 /* The type-specifier sequence must not contain type definitions.
4479 (It cannot contain declarations of new types either, but if they
4480 are not definitions we will catch that because they are not
4482 saved_message
= parser
->type_definition_forbidden_message
;
4483 parser
->type_definition_forbidden_message
4484 = "types may not be defined in a new-type-id";
4485 /* Parse the type-specifier-seq. */
4486 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4487 /* Restore the old message. */
4488 parser
->type_definition_forbidden_message
= saved_message
;
4489 /* Parse the new-declarator. */
4490 declarator
= cp_parser_new_declarator_opt (parser
);
4492 return build_tree_list (type_specifier_seq
, declarator
);
4495 /* Parse an (optional) new-declarator.
4498 ptr-operator new-declarator [opt]
4499 direct-new-declarator
4501 Returns a representation of the declarator. See
4502 cp_parser_declarator for the representations used. */
4505 cp_parser_new_declarator_opt (cp_parser
* parser
)
4507 enum tree_code code
;
4509 tree cv_qualifier_seq
;
4511 /* We don't know if there's a ptr-operator next, or not. */
4512 cp_parser_parse_tentatively (parser
);
4513 /* Look for a ptr-operator. */
4514 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4515 /* If that worked, look for more new-declarators. */
4516 if (cp_parser_parse_definitely (parser
))
4520 /* Parse another optional declarator. */
4521 declarator
= cp_parser_new_declarator_opt (parser
);
4523 /* Create the representation of the declarator. */
4524 if (code
== INDIRECT_REF
)
4525 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4528 declarator
= make_reference_declarator (cv_qualifier_seq
,
4531 /* Handle the pointer-to-member case. */
4533 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4538 /* If the next token is a `[', there is a direct-new-declarator. */
4539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4540 return cp_parser_direct_new_declarator (parser
);
4545 /* Parse a direct-new-declarator.
4547 direct-new-declarator:
4549 direct-new-declarator [constant-expression]
4551 Returns an ARRAY_REF, following the same conventions as are
4552 documented for cp_parser_direct_declarator. */
4555 cp_parser_direct_new_declarator (cp_parser
* parser
)
4557 tree declarator
= NULL_TREE
;
4563 /* Look for the opening `['. */
4564 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4565 /* The first expression is not required to be constant. */
4568 expression
= cp_parser_expression (parser
);
4569 /* The standard requires that the expression have integral
4570 type. DR 74 adds enumeration types. We believe that the
4571 real intent is that these expressions be handled like the
4572 expression in a `switch' condition, which also allows
4573 classes with a single conversion to integral or
4574 enumeration type. */
4575 if (!processing_template_decl
)
4578 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4583 error ("expression in new-declarator must have integral or enumeration type");
4584 expression
= error_mark_node
;
4588 /* But all the other expressions must be. */
4591 = cp_parser_constant_expression (parser
,
4592 /*allow_non_constant=*/false,
4594 /* Look for the closing `]'. */
4595 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4597 /* Add this bound to the declarator. */
4598 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4600 /* If the next token is not a `[', then there are no more
4602 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4609 /* Parse a new-initializer.
4612 ( expression-list [opt] )
4614 Returns a representation of the expression-list. If there is no
4615 expression-list, VOID_ZERO_NODE is returned. */
4618 cp_parser_new_initializer (cp_parser
* parser
)
4620 tree expression_list
;
4622 expression_list
= (cp_parser_parenthesized_expression_list
4623 (parser
, false, /*non_constant_p=*/NULL
));
4624 if (!expression_list
)
4625 expression_list
= void_zero_node
;
4627 return expression_list
;
4630 /* Parse a delete-expression.
4633 :: [opt] delete cast-expression
4634 :: [opt] delete [ ] cast-expression
4636 Returns a representation of the expression. */
4639 cp_parser_delete_expression (cp_parser
* parser
)
4641 bool global_scope_p
;
4645 /* Look for the optional `::' operator. */
4647 = (cp_parser_global_scope_opt (parser
,
4648 /*current_scope_valid_p=*/false)
4650 /* Look for the `delete' keyword. */
4651 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4652 /* See if the array syntax is in use. */
4653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4655 /* Consume the `[' token. */
4656 cp_lexer_consume_token (parser
->lexer
);
4657 /* Look for the `]' token. */
4658 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4659 /* Remember that this is the `[]' construct. */
4665 /* Parse the cast-expression. */
4666 expression
= cp_parser_simple_cast_expression (parser
);
4668 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4671 /* Parse a cast-expression.
4675 ( type-id ) cast-expression
4677 Returns a representation of the expression. */
4680 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4682 /* If it's a `(', then we might be looking at a cast. */
4683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4685 tree type
= NULL_TREE
;
4686 tree expr
= NULL_TREE
;
4687 bool compound_literal_p
;
4688 const char *saved_message
;
4690 /* There's no way to know yet whether or not this is a cast.
4691 For example, `(int (3))' is a unary-expression, while `(int)
4692 3' is a cast. So, we resort to parsing tentatively. */
4693 cp_parser_parse_tentatively (parser
);
4694 /* Types may not be defined in a cast. */
4695 saved_message
= parser
->type_definition_forbidden_message
;
4696 parser
->type_definition_forbidden_message
4697 = "types may not be defined in casts";
4698 /* Consume the `('. */
4699 cp_lexer_consume_token (parser
->lexer
);
4700 /* A very tricky bit is that `(struct S) { 3 }' is a
4701 compound-literal (which we permit in C++ as an extension).
4702 But, that construct is not a cast-expression -- it is a
4703 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4704 is legal; if the compound-literal were a cast-expression,
4705 you'd need an extra set of parentheses.) But, if we parse
4706 the type-id, and it happens to be a class-specifier, then we
4707 will commit to the parse at that point, because we cannot
4708 undo the action that is done when creating a new class. So,
4709 then we cannot back up and do a postfix-expression.
4711 Therefore, we scan ahead to the closing `)', and check to see
4712 if the token after the `)' is a `{'. If so, we are not
4713 looking at a cast-expression.
4715 Save tokens so that we can put them back. */
4716 cp_lexer_save_tokens (parser
->lexer
);
4717 /* Skip tokens until the next token is a closing parenthesis.
4718 If we find the closing `)', and the next token is a `{', then
4719 we are looking at a compound-literal. */
4721 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
4722 /*consume_paren=*/true)
4723 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4724 /* Roll back the tokens we skipped. */
4725 cp_lexer_rollback_tokens (parser
->lexer
);
4726 /* If we were looking at a compound-literal, simulate an error
4727 so that the call to cp_parser_parse_definitely below will
4729 if (compound_literal_p
)
4730 cp_parser_simulate_error (parser
);
4733 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4734 parser
->in_type_id_in_expr_p
= true;
4735 /* Look for the type-id. */
4736 type
= cp_parser_type_id (parser
);
4737 /* Look for the closing `)'. */
4738 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4739 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4742 /* Restore the saved message. */
4743 parser
->type_definition_forbidden_message
= saved_message
;
4745 /* If ok so far, parse the dependent expression. We cannot be
4746 sure it is a cast. Consider `(T ())'. It is a parenthesized
4747 ctor of T, but looks like a cast to function returning T
4748 without a dependent expression. */
4749 if (!cp_parser_error_occurred (parser
))
4750 expr
= cp_parser_simple_cast_expression (parser
);
4752 if (cp_parser_parse_definitely (parser
))
4754 /* Warn about old-style casts, if so requested. */
4755 if (warn_old_style_cast
4756 && !in_system_header
4757 && !VOID_TYPE_P (type
)
4758 && current_lang_name
!= lang_name_c
)
4759 warning ("use of old-style cast");
4761 /* Only type conversions to integral or enumeration types
4762 can be used in constant-expressions. */
4763 if (parser
->integral_constant_expression_p
4764 && !dependent_type_p (type
)
4765 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4767 if (!parser
->allow_non_integral_constant_expression_p
)
4768 return (cp_parser_non_integral_constant_expression
4769 ("a casts to a type other than an integral or "
4770 "enumeration type"));
4771 parser
->non_integral_constant_expression_p
= true;
4773 /* Perform the cast. */
4774 expr
= build_c_cast (type
, expr
);
4779 /* If we get here, then it's not a cast, so it must be a
4780 unary-expression. */
4781 return cp_parser_unary_expression (parser
, address_p
);
4784 /* Parse a pm-expression.
4788 pm-expression .* cast-expression
4789 pm-expression ->* cast-expression
4791 Returns a representation of the expression. */
4794 cp_parser_pm_expression (cp_parser
* parser
)
4796 static const cp_parser_token_tree_map map
= {
4797 { CPP_DEREF_STAR
, MEMBER_REF
},
4798 { CPP_DOT_STAR
, DOTSTAR_EXPR
},
4799 { CPP_EOF
, ERROR_MARK
}
4802 return cp_parser_binary_expression (parser
, map
,
4803 cp_parser_simple_cast_expression
);
4806 /* Parse a multiplicative-expression.
4808 mulitplicative-expression:
4810 multiplicative-expression * pm-expression
4811 multiplicative-expression / pm-expression
4812 multiplicative-expression % pm-expression
4814 Returns a representation of the expression. */
4817 cp_parser_multiplicative_expression (cp_parser
* parser
)
4819 static const cp_parser_token_tree_map map
= {
4820 { CPP_MULT
, MULT_EXPR
},
4821 { CPP_DIV
, TRUNC_DIV_EXPR
},
4822 { CPP_MOD
, TRUNC_MOD_EXPR
},
4823 { CPP_EOF
, ERROR_MARK
}
4826 return cp_parser_binary_expression (parser
,
4828 cp_parser_pm_expression
);
4831 /* Parse an additive-expression.
4833 additive-expression:
4834 multiplicative-expression
4835 additive-expression + multiplicative-expression
4836 additive-expression - multiplicative-expression
4838 Returns a representation of the expression. */
4841 cp_parser_additive_expression (cp_parser
* parser
)
4843 static const cp_parser_token_tree_map map
= {
4844 { CPP_PLUS
, PLUS_EXPR
},
4845 { CPP_MINUS
, MINUS_EXPR
},
4846 { CPP_EOF
, ERROR_MARK
}
4849 return cp_parser_binary_expression (parser
,
4851 cp_parser_multiplicative_expression
);
4854 /* Parse a shift-expression.
4858 shift-expression << additive-expression
4859 shift-expression >> additive-expression
4861 Returns a representation of the expression. */
4864 cp_parser_shift_expression (cp_parser
* parser
)
4866 static const cp_parser_token_tree_map map
= {
4867 { CPP_LSHIFT
, LSHIFT_EXPR
},
4868 { CPP_RSHIFT
, RSHIFT_EXPR
},
4869 { CPP_EOF
, ERROR_MARK
}
4872 return cp_parser_binary_expression (parser
,
4874 cp_parser_additive_expression
);
4877 /* Parse a relational-expression.
4879 relational-expression:
4881 relational-expression < shift-expression
4882 relational-expression > shift-expression
4883 relational-expression <= shift-expression
4884 relational-expression >= shift-expression
4888 relational-expression:
4889 relational-expression <? shift-expression
4890 relational-expression >? shift-expression
4892 Returns a representation of the expression. */
4895 cp_parser_relational_expression (cp_parser
* parser
)
4897 static const cp_parser_token_tree_map map
= {
4898 { CPP_LESS
, LT_EXPR
},
4899 { CPP_GREATER
, GT_EXPR
},
4900 { CPP_LESS_EQ
, LE_EXPR
},
4901 { CPP_GREATER_EQ
, GE_EXPR
},
4902 { CPP_MIN
, MIN_EXPR
},
4903 { CPP_MAX
, MAX_EXPR
},
4904 { CPP_EOF
, ERROR_MARK
}
4907 return cp_parser_binary_expression (parser
,
4909 cp_parser_shift_expression
);
4912 /* Parse an equality-expression.
4914 equality-expression:
4915 relational-expression
4916 equality-expression == relational-expression
4917 equality-expression != relational-expression
4919 Returns a representation of the expression. */
4922 cp_parser_equality_expression (cp_parser
* parser
)
4924 static const cp_parser_token_tree_map map
= {
4925 { CPP_EQ_EQ
, EQ_EXPR
},
4926 { CPP_NOT_EQ
, NE_EXPR
},
4927 { CPP_EOF
, ERROR_MARK
}
4930 return cp_parser_binary_expression (parser
,
4932 cp_parser_relational_expression
);
4935 /* Parse an and-expression.
4939 and-expression & equality-expression
4941 Returns a representation of the expression. */
4944 cp_parser_and_expression (cp_parser
* parser
)
4946 static const cp_parser_token_tree_map map
= {
4947 { CPP_AND
, BIT_AND_EXPR
},
4948 { CPP_EOF
, ERROR_MARK
}
4951 return cp_parser_binary_expression (parser
,
4953 cp_parser_equality_expression
);
4956 /* Parse an exclusive-or-expression.
4958 exclusive-or-expression:
4960 exclusive-or-expression ^ and-expression
4962 Returns a representation of the expression. */
4965 cp_parser_exclusive_or_expression (cp_parser
* parser
)
4967 static const cp_parser_token_tree_map map
= {
4968 { CPP_XOR
, BIT_XOR_EXPR
},
4969 { CPP_EOF
, ERROR_MARK
}
4972 return cp_parser_binary_expression (parser
,
4974 cp_parser_and_expression
);
4978 /* Parse an inclusive-or-expression.
4980 inclusive-or-expression:
4981 exclusive-or-expression
4982 inclusive-or-expression | exclusive-or-expression
4984 Returns a representation of the expression. */
4987 cp_parser_inclusive_or_expression (cp_parser
* parser
)
4989 static const cp_parser_token_tree_map map
= {
4990 { CPP_OR
, BIT_IOR_EXPR
},
4991 { CPP_EOF
, ERROR_MARK
}
4994 return cp_parser_binary_expression (parser
,
4996 cp_parser_exclusive_or_expression
);
4999 /* Parse a logical-and-expression.
5001 logical-and-expression:
5002 inclusive-or-expression
5003 logical-and-expression && inclusive-or-expression
5005 Returns a representation of the expression. */
5008 cp_parser_logical_and_expression (cp_parser
* parser
)
5010 static const cp_parser_token_tree_map map
= {
5011 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
5012 { CPP_EOF
, ERROR_MARK
}
5015 return cp_parser_binary_expression (parser
,
5017 cp_parser_inclusive_or_expression
);
5020 /* Parse a logical-or-expression.
5022 logical-or-expression:
5023 logical-and-expression
5024 logical-or-expression || logical-and-expression
5026 Returns a representation of the expression. */
5029 cp_parser_logical_or_expression (cp_parser
* parser
)
5031 static const cp_parser_token_tree_map map
= {
5032 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
5033 { CPP_EOF
, ERROR_MARK
}
5036 return cp_parser_binary_expression (parser
,
5038 cp_parser_logical_and_expression
);
5041 /* Parse the `? expression : assignment-expression' part of a
5042 conditional-expression. The LOGICAL_OR_EXPR is the
5043 logical-or-expression that started the conditional-expression.
5044 Returns a representation of the entire conditional-expression.
5046 This routine is used by cp_parser_assignment_expression.
5048 ? expression : assignment-expression
5052 ? : assignment-expression */
5055 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5058 tree assignment_expr
;
5060 /* Consume the `?' token. */
5061 cp_lexer_consume_token (parser
->lexer
);
5062 if (cp_parser_allow_gnu_extensions_p (parser
)
5063 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5064 /* Implicit true clause. */
5067 /* Parse the expression. */
5068 expr
= cp_parser_expression (parser
);
5070 /* The next token should be a `:'. */
5071 cp_parser_require (parser
, CPP_COLON
, "`:'");
5072 /* Parse the assignment-expression. */
5073 assignment_expr
= cp_parser_assignment_expression (parser
);
5075 /* Build the conditional-expression. */
5076 return build_x_conditional_expr (logical_or_expr
,
5081 /* Parse an assignment-expression.
5083 assignment-expression:
5084 conditional-expression
5085 logical-or-expression assignment-operator assignment_expression
5088 Returns a representation for the expression. */
5091 cp_parser_assignment_expression (cp_parser
* parser
)
5095 /* If the next token is the `throw' keyword, then we're looking at
5096 a throw-expression. */
5097 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5098 expr
= cp_parser_throw_expression (parser
);
5099 /* Otherwise, it must be that we are looking at a
5100 logical-or-expression. */
5103 /* Parse the logical-or-expression. */
5104 expr
= cp_parser_logical_or_expression (parser
);
5105 /* If the next token is a `?' then we're actually looking at a
5106 conditional-expression. */
5107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5108 return cp_parser_question_colon_clause (parser
, expr
);
5111 enum tree_code assignment_operator
;
5113 /* If it's an assignment-operator, we're using the second
5116 = cp_parser_assignment_operator_opt (parser
);
5117 if (assignment_operator
!= ERROR_MARK
)
5121 /* Parse the right-hand side of the assignment. */
5122 rhs
= cp_parser_assignment_expression (parser
);
5123 /* An assignment may not appear in a
5124 constant-expression. */
5125 if (parser
->integral_constant_expression_p
)
5127 if (!parser
->allow_non_integral_constant_expression_p
)
5128 return cp_parser_non_integral_constant_expression ("an assignment");
5129 parser
->non_integral_constant_expression_p
= true;
5131 /* Build the assignment expression. */
5132 expr
= build_x_modify_expr (expr
,
5133 assignment_operator
,
5142 /* Parse an (optional) assignment-operator.
5144 assignment-operator: one of
5145 = *= /= %= += -= >>= <<= &= ^= |=
5149 assignment-operator: one of
5152 If the next token is an assignment operator, the corresponding tree
5153 code is returned, and the token is consumed. For example, for
5154 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5155 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5156 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5157 operator, ERROR_MARK is returned. */
5159 static enum tree_code
5160 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5165 /* Peek at the next toen. */
5166 token
= cp_lexer_peek_token (parser
->lexer
);
5168 switch (token
->type
)
5179 op
= TRUNC_DIV_EXPR
;
5183 op
= TRUNC_MOD_EXPR
;
5223 /* Nothing else is an assignment operator. */
5227 /* If it was an assignment operator, consume it. */
5228 if (op
!= ERROR_MARK
)
5229 cp_lexer_consume_token (parser
->lexer
);
5234 /* Parse an expression.
5237 assignment-expression
5238 expression , assignment-expression
5240 Returns a representation of the expression. */
5243 cp_parser_expression (cp_parser
* parser
)
5245 tree expression
= NULL_TREE
;
5249 tree assignment_expression
;
5251 /* Parse the next assignment-expression. */
5252 assignment_expression
5253 = cp_parser_assignment_expression (parser
);
5254 /* If this is the first assignment-expression, we can just
5257 expression
= assignment_expression
;
5259 expression
= build_x_compound_expr (expression
,
5260 assignment_expression
);
5261 /* If the next token is not a comma, then we are done with the
5263 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5265 /* Consume the `,'. */
5266 cp_lexer_consume_token (parser
->lexer
);
5267 /* A comma operator cannot appear in a constant-expression. */
5268 if (parser
->integral_constant_expression_p
)
5270 if (!parser
->allow_non_integral_constant_expression_p
)
5272 = cp_parser_non_integral_constant_expression ("a comma operator");
5273 parser
->non_integral_constant_expression_p
= true;
5280 /* Parse a constant-expression.
5282 constant-expression:
5283 conditional-expression
5285 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5286 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5287 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5288 is false, NON_CONSTANT_P should be NULL. */
5291 cp_parser_constant_expression (cp_parser
* parser
,
5292 bool allow_non_constant_p
,
5293 bool *non_constant_p
)
5295 bool saved_integral_constant_expression_p
;
5296 bool saved_allow_non_integral_constant_expression_p
;
5297 bool saved_non_integral_constant_expression_p
;
5300 /* It might seem that we could simply parse the
5301 conditional-expression, and then check to see if it were
5302 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5303 one that the compiler can figure out is constant, possibly after
5304 doing some simplifications or optimizations. The standard has a
5305 precise definition of constant-expression, and we must honor
5306 that, even though it is somewhat more restrictive.
5312 is not a legal declaration, because `(2, 3)' is not a
5313 constant-expression. The `,' operator is forbidden in a
5314 constant-expression. However, GCC's constant-folding machinery
5315 will fold this operation to an INTEGER_CST for `3'. */
5317 /* Save the old settings. */
5318 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5319 saved_allow_non_integral_constant_expression_p
5320 = parser
->allow_non_integral_constant_expression_p
;
5321 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5322 /* We are now parsing a constant-expression. */
5323 parser
->integral_constant_expression_p
= true;
5324 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5325 parser
->non_integral_constant_expression_p
= false;
5326 /* Although the grammar says "conditional-expression", we parse an
5327 "assignment-expression", which also permits "throw-expression"
5328 and the use of assignment operators. In the case that
5329 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5330 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5331 actually essential that we look for an assignment-expression.
5332 For example, cp_parser_initializer_clauses uses this function to
5333 determine whether a particular assignment-expression is in fact
5335 expression
= cp_parser_assignment_expression (parser
);
5336 /* Restore the old settings. */
5337 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
5338 parser
->allow_non_integral_constant_expression_p
5339 = saved_allow_non_integral_constant_expression_p
;
5340 if (allow_non_constant_p
)
5341 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5342 parser
->non_integral_constant_expression_p
= saved_non_integral_constant_expression_p
;
5347 /* Statements [gram.stmt.stmt] */
5349 /* Parse a statement.
5353 expression-statement
5358 declaration-statement
5362 cp_parser_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5366 int statement_line_number
;
5368 /* There is no statement yet. */
5369 statement
= NULL_TREE
;
5370 /* Peek at the next token. */
5371 token
= cp_lexer_peek_token (parser
->lexer
);
5372 /* Remember the line number of the first token in the statement. */
5373 statement_line_number
= token
->location
.line
;
5374 /* If this is a keyword, then that will often determine what kind of
5375 statement we have. */
5376 if (token
->type
== CPP_KEYWORD
)
5378 enum rid keyword
= token
->keyword
;
5384 statement
= cp_parser_labeled_statement (parser
,
5385 in_statement_expr_p
);
5390 statement
= cp_parser_selection_statement (parser
);
5396 statement
= cp_parser_iteration_statement (parser
);
5403 statement
= cp_parser_jump_statement (parser
);
5407 statement
= cp_parser_try_block (parser
);
5411 /* It might be a keyword like `int' that can start a
5412 declaration-statement. */
5416 else if (token
->type
== CPP_NAME
)
5418 /* If the next token is a `:', then we are looking at a
5419 labeled-statement. */
5420 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5421 if (token
->type
== CPP_COLON
)
5422 statement
= cp_parser_labeled_statement (parser
, in_statement_expr_p
);
5424 /* Anything that starts with a `{' must be a compound-statement. */
5425 else if (token
->type
== CPP_OPEN_BRACE
)
5426 statement
= cp_parser_compound_statement (parser
, false);
5428 /* Everything else must be a declaration-statement or an
5429 expression-statement. Try for the declaration-statement
5430 first, unless we are looking at a `;', in which case we know that
5431 we have an expression-statement. */
5434 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5436 cp_parser_parse_tentatively (parser
);
5437 /* Try to parse the declaration-statement. */
5438 cp_parser_declaration_statement (parser
);
5439 /* If that worked, we're done. */
5440 if (cp_parser_parse_definitely (parser
))
5443 /* Look for an expression-statement instead. */
5444 statement
= cp_parser_expression_statement (parser
, in_statement_expr_p
);
5447 /* Set the line number for the statement. */
5448 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5449 STMT_LINENO (statement
) = statement_line_number
;
5452 /* Parse a labeled-statement.
5455 identifier : statement
5456 case constant-expression : statement
5459 Returns the new CASE_LABEL, for a `case' or `default' label. For
5460 an ordinary label, returns a LABEL_STMT. */
5463 cp_parser_labeled_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5466 tree statement
= error_mark_node
;
5468 /* The next token should be an identifier. */
5469 token
= cp_lexer_peek_token (parser
->lexer
);
5470 if (token
->type
!= CPP_NAME
5471 && token
->type
!= CPP_KEYWORD
)
5473 cp_parser_error (parser
, "expected labeled-statement");
5474 return error_mark_node
;
5477 switch (token
->keyword
)
5483 /* Consume the `case' token. */
5484 cp_lexer_consume_token (parser
->lexer
);
5485 /* Parse the constant-expression. */
5486 expr
= cp_parser_constant_expression (parser
,
5487 /*allow_non_constant_p=*/false,
5489 if (!parser
->in_switch_statement_p
)
5490 error ("case label `%E' not within a switch statement", expr
);
5492 statement
= finish_case_label (expr
, NULL_TREE
);
5497 /* Consume the `default' token. */
5498 cp_lexer_consume_token (parser
->lexer
);
5499 if (!parser
->in_switch_statement_p
)
5500 error ("case label not within a switch statement");
5502 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5506 /* Anything else must be an ordinary label. */
5507 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5511 /* Require the `:' token. */
5512 cp_parser_require (parser
, CPP_COLON
, "`:'");
5513 /* Parse the labeled statement. */
5514 cp_parser_statement (parser
, in_statement_expr_p
);
5516 /* Return the label, in the case of a `case' or `default' label. */
5520 /* Parse an expression-statement.
5522 expression-statement:
5525 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5526 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5527 indicates whether this expression-statement is part of an
5528 expression statement. */
5531 cp_parser_expression_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5533 tree statement
= NULL_TREE
;
5535 /* If the next token is a ';', then there is no expression
5537 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5538 statement
= cp_parser_expression (parser
);
5540 /* Consume the final `;'. */
5541 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5543 if (in_statement_expr_p
5544 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
5546 /* This is the final expression statement of a statement
5548 statement
= finish_stmt_expr_expr (statement
);
5551 statement
= finish_expr_stmt (statement
);
5558 /* Parse a compound-statement.
5561 { statement-seq [opt] }
5563 Returns a COMPOUND_STMT representing the statement. */
5566 cp_parser_compound_statement (cp_parser
*parser
, bool in_statement_expr_p
)
5570 /* Consume the `{'. */
5571 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5572 return error_mark_node
;
5573 /* Begin the compound-statement. */
5574 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/false);
5575 /* Parse an (optional) statement-seq. */
5576 cp_parser_statement_seq_opt (parser
, in_statement_expr_p
);
5577 /* Finish the compound-statement. */
5578 finish_compound_stmt (compound_stmt
);
5579 /* Consume the `}'. */
5580 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5582 return compound_stmt
;
5585 /* Parse an (optional) statement-seq.
5589 statement-seq [opt] statement */
5592 cp_parser_statement_seq_opt (cp_parser
* parser
, bool in_statement_expr_p
)
5594 /* Scan statements until there aren't any more. */
5597 /* If we're looking at a `}', then we've run out of statements. */
5598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5599 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5602 /* Parse the statement. */
5603 cp_parser_statement (parser
, in_statement_expr_p
);
5607 /* Parse a selection-statement.
5609 selection-statement:
5610 if ( condition ) statement
5611 if ( condition ) statement else statement
5612 switch ( condition ) statement
5614 Returns the new IF_STMT or SWITCH_STMT. */
5617 cp_parser_selection_statement (cp_parser
* parser
)
5622 /* Peek at the next token. */
5623 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5625 /* See what kind of keyword it is. */
5626 keyword
= token
->keyword
;
5635 /* Look for the `('. */
5636 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5638 cp_parser_skip_to_end_of_statement (parser
);
5639 return error_mark_node
;
5642 /* Begin the selection-statement. */
5643 if (keyword
== RID_IF
)
5644 statement
= begin_if_stmt ();
5646 statement
= begin_switch_stmt ();
5648 /* Parse the condition. */
5649 condition
= cp_parser_condition (parser
);
5650 /* Look for the `)'. */
5651 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5652 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
5653 /*consume_paren=*/true);
5655 if (keyword
== RID_IF
)
5659 /* Add the condition. */
5660 finish_if_stmt_cond (condition
, statement
);
5662 /* Parse the then-clause. */
5663 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5664 finish_then_clause (statement
);
5666 /* If the next token is `else', parse the else-clause. */
5667 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5672 /* Consume the `else' keyword. */
5673 cp_lexer_consume_token (parser
->lexer
);
5674 /* Parse the else-clause. */
5676 = cp_parser_implicitly_scoped_statement (parser
);
5677 finish_else_clause (statement
);
5680 /* Now we're all done with the if-statement. */
5686 bool in_switch_statement_p
;
5688 /* Add the condition. */
5689 finish_switch_cond (condition
, statement
);
5691 /* Parse the body of the switch-statement. */
5692 in_switch_statement_p
= parser
->in_switch_statement_p
;
5693 parser
->in_switch_statement_p
= true;
5694 body
= cp_parser_implicitly_scoped_statement (parser
);
5695 parser
->in_switch_statement_p
= in_switch_statement_p
;
5697 /* Now we're all done with the switch-statement. */
5698 finish_switch_stmt (statement
);
5706 cp_parser_error (parser
, "expected selection-statement");
5707 return error_mark_node
;
5711 /* Parse a condition.
5715 type-specifier-seq declarator = assignment-expression
5720 type-specifier-seq declarator asm-specification [opt]
5721 attributes [opt] = assignment-expression
5723 Returns the expression that should be tested. */
5726 cp_parser_condition (cp_parser
* parser
)
5728 tree type_specifiers
;
5729 const char *saved_message
;
5731 /* Try the declaration first. */
5732 cp_parser_parse_tentatively (parser
);
5733 /* New types are not allowed in the type-specifier-seq for a
5735 saved_message
= parser
->type_definition_forbidden_message
;
5736 parser
->type_definition_forbidden_message
5737 = "types may not be defined in conditions";
5738 /* Parse the type-specifier-seq. */
5739 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5740 /* Restore the saved message. */
5741 parser
->type_definition_forbidden_message
= saved_message
;
5742 /* If all is well, we might be looking at a declaration. */
5743 if (!cp_parser_error_occurred (parser
))
5746 tree asm_specification
;
5749 tree initializer
= NULL_TREE
;
5751 /* Parse the declarator. */
5752 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5753 /*ctor_dtor_or_conv_p=*/NULL
,
5754 /*parenthesized_p=*/NULL
);
5755 /* Parse the attributes. */
5756 attributes
= cp_parser_attributes_opt (parser
);
5757 /* Parse the asm-specification. */
5758 asm_specification
= cp_parser_asm_specification_opt (parser
);
5759 /* If the next token is not an `=', then we might still be
5760 looking at an expression. For example:
5764 looks like a decl-specifier-seq and a declarator -- but then
5765 there is no `=', so this is an expression. */
5766 cp_parser_require (parser
, CPP_EQ
, "`='");
5767 /* If we did see an `=', then we are looking at a declaration
5769 if (cp_parser_parse_definitely (parser
))
5771 /* Create the declaration. */
5772 decl
= start_decl (declarator
, type_specifiers
,
5773 /*initialized_p=*/true,
5774 attributes
, /*prefix_attributes=*/NULL_TREE
);
5775 /* Parse the assignment-expression. */
5776 initializer
= cp_parser_assignment_expression (parser
);
5778 /* Process the initializer. */
5779 cp_finish_decl (decl
,
5782 LOOKUP_ONLYCONVERTING
);
5784 return convert_from_reference (decl
);
5787 /* If we didn't even get past the declarator successfully, we are
5788 definitely not looking at a declaration. */
5790 cp_parser_abort_tentative_parse (parser
);
5792 /* Otherwise, we are looking at an expression. */
5793 return cp_parser_expression (parser
);
5796 /* Parse an iteration-statement.
5798 iteration-statement:
5799 while ( condition ) statement
5800 do statement while ( expression ) ;
5801 for ( for-init-statement condition [opt] ; expression [opt] )
5804 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5807 cp_parser_iteration_statement (cp_parser
* parser
)
5812 bool in_iteration_statement_p
;
5815 /* Peek at the next token. */
5816 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
5818 return error_mark_node
;
5820 /* Remember whether or not we are already within an iteration
5822 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
5824 /* See what kind of keyword it is. */
5825 keyword
= token
->keyword
;
5832 /* Begin the while-statement. */
5833 statement
= begin_while_stmt ();
5834 /* Look for the `('. */
5835 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5836 /* Parse the condition. */
5837 condition
= cp_parser_condition (parser
);
5838 finish_while_stmt_cond (condition
, statement
);
5839 /* Look for the `)'. */
5840 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5841 /* Parse the dependent statement. */
5842 parser
->in_iteration_statement_p
= true;
5843 cp_parser_already_scoped_statement (parser
);
5844 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5845 /* We're done with the while-statement. */
5846 finish_while_stmt (statement
);
5854 /* Begin the do-statement. */
5855 statement
= begin_do_stmt ();
5856 /* Parse the body of the do-statement. */
5857 parser
->in_iteration_statement_p
= true;
5858 cp_parser_implicitly_scoped_statement (parser
);
5859 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5860 finish_do_body (statement
);
5861 /* Look for the `while' keyword. */
5862 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
5863 /* Look for the `('. */
5864 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5865 /* Parse the expression. */
5866 expression
= cp_parser_expression (parser
);
5867 /* We're done with the do-statement. */
5868 finish_do_stmt (expression
, statement
);
5869 /* Look for the `)'. */
5870 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5871 /* Look for the `;'. */
5872 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5878 tree condition
= NULL_TREE
;
5879 tree expression
= NULL_TREE
;
5881 /* Begin the for-statement. */
5882 statement
= begin_for_stmt ();
5883 /* Look for the `('. */
5884 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5885 /* Parse the initialization. */
5886 cp_parser_for_init_statement (parser
);
5887 finish_for_init_stmt (statement
);
5889 /* If there's a condition, process it. */
5890 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5891 condition
= cp_parser_condition (parser
);
5892 finish_for_cond (condition
, statement
);
5893 /* Look for the `;'. */
5894 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5896 /* If there's an expression, process it. */
5897 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5898 expression
= cp_parser_expression (parser
);
5899 finish_for_expr (expression
, statement
);
5900 /* Look for the `)'. */
5901 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`;'");
5903 /* Parse the body of the for-statement. */
5904 parser
->in_iteration_statement_p
= true;
5905 cp_parser_already_scoped_statement (parser
);
5906 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5908 /* We're done with the for-statement. */
5909 finish_for_stmt (statement
);
5914 cp_parser_error (parser
, "expected iteration-statement");
5915 statement
= error_mark_node
;
5922 /* Parse a for-init-statement.
5925 expression-statement
5926 simple-declaration */
5929 cp_parser_for_init_statement (cp_parser
* parser
)
5931 /* If the next token is a `;', then we have an empty
5932 expression-statement. Grammatically, this is also a
5933 simple-declaration, but an invalid one, because it does not
5934 declare anything. Therefore, if we did not handle this case
5935 specially, we would issue an error message about an invalid
5937 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5939 /* We're going to speculatively look for a declaration, falling back
5940 to an expression, if necessary. */
5941 cp_parser_parse_tentatively (parser
);
5942 /* Parse the declaration. */
5943 cp_parser_simple_declaration (parser
,
5944 /*function_definition_allowed_p=*/false);
5945 /* If the tentative parse failed, then we shall need to look for an
5946 expression-statement. */
5947 if (cp_parser_parse_definitely (parser
))
5951 cp_parser_expression_statement (parser
, false);
5954 /* Parse a jump-statement.
5959 return expression [opt] ;
5967 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5971 cp_parser_jump_statement (cp_parser
* parser
)
5973 tree statement
= error_mark_node
;
5977 /* Peek at the next token. */
5978 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
5980 return error_mark_node
;
5982 /* See what kind of keyword it is. */
5983 keyword
= token
->keyword
;
5987 if (!parser
->in_switch_statement_p
5988 && !parser
->in_iteration_statement_p
)
5990 error ("break statement not within loop or switch");
5991 statement
= error_mark_node
;
5994 statement
= finish_break_stmt ();
5995 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5999 if (!parser
->in_iteration_statement_p
)
6001 error ("continue statement not within a loop");
6002 statement
= error_mark_node
;
6005 statement
= finish_continue_stmt ();
6006 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6013 /* If the next token is a `;', then there is no
6015 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6016 expr
= cp_parser_expression (parser
);
6019 /* Build the return-statement. */
6020 statement
= finish_return_stmt (expr
);
6021 /* Look for the final `;'. */
6022 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6027 /* Create the goto-statement. */
6028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6030 /* Issue a warning about this use of a GNU extension. */
6032 pedwarn ("ISO C++ forbids computed gotos");
6033 /* Consume the '*' token. */
6034 cp_lexer_consume_token (parser
->lexer
);
6035 /* Parse the dependent expression. */
6036 finish_goto_stmt (cp_parser_expression (parser
));
6039 finish_goto_stmt (cp_parser_identifier (parser
));
6040 /* Look for the final `;'. */
6041 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6045 cp_parser_error (parser
, "expected jump-statement");
6052 /* Parse a declaration-statement.
6054 declaration-statement:
6055 block-declaration */
6058 cp_parser_declaration_statement (cp_parser
* parser
)
6060 /* Parse the block-declaration. */
6061 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6063 /* Finish off the statement. */
6067 /* Some dependent statements (like `if (cond) statement'), are
6068 implicitly in their own scope. In other words, if the statement is
6069 a single statement (as opposed to a compound-statement), it is
6070 none-the-less treated as if it were enclosed in braces. Any
6071 declarations appearing in the dependent statement are out of scope
6072 after control passes that point. This function parses a statement,
6073 but ensures that is in its own scope, even if it is not a
6076 Returns the new statement. */
6079 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6083 /* If the token is not a `{', then we must take special action. */
6084 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6086 /* Create a compound-statement. */
6087 statement
= begin_compound_stmt (/*has_no_scope=*/false);
6088 /* Parse the dependent-statement. */
6089 cp_parser_statement (parser
, false);
6090 /* Finish the dummy compound-statement. */
6091 finish_compound_stmt (statement
);
6093 /* Otherwise, we simply parse the statement directly. */
6095 statement
= cp_parser_compound_statement (parser
, false);
6097 /* Return the statement. */
6101 /* For some dependent statements (like `while (cond) statement'), we
6102 have already created a scope. Therefore, even if the dependent
6103 statement is a compound-statement, we do not want to create another
6107 cp_parser_already_scoped_statement (cp_parser
* parser
)
6109 /* If the token is not a `{', then we must take special action. */
6110 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
6114 /* Create a compound-statement. */
6115 statement
= begin_compound_stmt (/*has_no_scope=*/true);
6116 /* Parse the dependent-statement. */
6117 cp_parser_statement (parser
, false);
6118 /* Finish the dummy compound-statement. */
6119 finish_compound_stmt (statement
);
6121 /* Otherwise, we simply parse the statement directly. */
6123 cp_parser_statement (parser
, false);
6126 /* Declarations [gram.dcl.dcl] */
6128 /* Parse an optional declaration-sequence.
6132 declaration-seq declaration */
6135 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6141 token
= cp_lexer_peek_token (parser
->lexer
);
6143 if (token
->type
== CPP_CLOSE_BRACE
6144 || token
->type
== CPP_EOF
)
6147 if (token
->type
== CPP_SEMICOLON
)
6149 /* A declaration consisting of a single semicolon is
6150 invalid. Allow it unless we're being pedantic. */
6151 if (pedantic
&& !in_system_header
)
6152 pedwarn ("extra `;'");
6153 cp_lexer_consume_token (parser
->lexer
);
6157 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6158 parser to enter or exit implicit `extern "C"' blocks. */
6159 while (pending_lang_change
> 0)
6161 push_lang_context (lang_name_c
);
6162 --pending_lang_change
;
6164 while (pending_lang_change
< 0)
6166 pop_lang_context ();
6167 ++pending_lang_change
;
6170 /* Parse the declaration itself. */
6171 cp_parser_declaration (parser
);
6175 /* Parse a declaration.
6180 template-declaration
6181 explicit-instantiation
6182 explicit-specialization
6183 linkage-specification
6184 namespace-definition
6189 __extension__ declaration */
6192 cp_parser_declaration (cp_parser
* parser
)
6198 /* Check for the `__extension__' keyword. */
6199 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6201 /* Parse the qualified declaration. */
6202 cp_parser_declaration (parser
);
6203 /* Restore the PEDANTIC flag. */
6204 pedantic
= saved_pedantic
;
6209 /* Try to figure out what kind of declaration is present. */
6210 token1
= *cp_lexer_peek_token (parser
->lexer
);
6211 if (token1
.type
!= CPP_EOF
)
6212 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6214 /* If the next token is `extern' and the following token is a string
6215 literal, then we have a linkage specification. */
6216 if (token1
.keyword
== RID_EXTERN
6217 && cp_parser_is_string_literal (&token2
))
6218 cp_parser_linkage_specification (parser
);
6219 /* If the next token is `template', then we have either a template
6220 declaration, an explicit instantiation, or an explicit
6222 else if (token1
.keyword
== RID_TEMPLATE
)
6224 /* `template <>' indicates a template specialization. */
6225 if (token2
.type
== CPP_LESS
6226 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6227 cp_parser_explicit_specialization (parser
);
6228 /* `template <' indicates a template declaration. */
6229 else if (token2
.type
== CPP_LESS
)
6230 cp_parser_template_declaration (parser
, /*member_p=*/false);
6231 /* Anything else must be an explicit instantiation. */
6233 cp_parser_explicit_instantiation (parser
);
6235 /* If the next token is `export', then we have a template
6237 else if (token1
.keyword
== RID_EXPORT
)
6238 cp_parser_template_declaration (parser
, /*member_p=*/false);
6239 /* If the next token is `extern', 'static' or 'inline' and the one
6240 after that is `template', we have a GNU extended explicit
6241 instantiation directive. */
6242 else if (cp_parser_allow_gnu_extensions_p (parser
)
6243 && (token1
.keyword
== RID_EXTERN
6244 || token1
.keyword
== RID_STATIC
6245 || token1
.keyword
== RID_INLINE
)
6246 && token2
.keyword
== RID_TEMPLATE
)
6247 cp_parser_explicit_instantiation (parser
);
6248 /* If the next token is `namespace', check for a named or unnamed
6249 namespace definition. */
6250 else if (token1
.keyword
== RID_NAMESPACE
6251 && (/* A named namespace definition. */
6252 (token2
.type
== CPP_NAME
6253 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6255 /* An unnamed namespace definition. */
6256 || token2
.type
== CPP_OPEN_BRACE
))
6257 cp_parser_namespace_definition (parser
);
6258 /* We must have either a block declaration or a function
6261 /* Try to parse a block-declaration, or a function-definition. */
6262 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6265 /* Parse a block-declaration.
6270 namespace-alias-definition
6277 __extension__ block-declaration
6280 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6281 part of a declaration-statement. */
6284 cp_parser_block_declaration (cp_parser
*parser
,
6290 /* Check for the `__extension__' keyword. */
6291 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6293 /* Parse the qualified declaration. */
6294 cp_parser_block_declaration (parser
, statement_p
);
6295 /* Restore the PEDANTIC flag. */
6296 pedantic
= saved_pedantic
;
6301 /* Peek at the next token to figure out which kind of declaration is
6303 token1
= cp_lexer_peek_token (parser
->lexer
);
6305 /* If the next keyword is `asm', we have an asm-definition. */
6306 if (token1
->keyword
== RID_ASM
)
6309 cp_parser_commit_to_tentative_parse (parser
);
6310 cp_parser_asm_definition (parser
);
6312 /* If the next keyword is `namespace', we have a
6313 namespace-alias-definition. */
6314 else if (token1
->keyword
== RID_NAMESPACE
)
6315 cp_parser_namespace_alias_definition (parser
);
6316 /* If the next keyword is `using', we have either a
6317 using-declaration or a using-directive. */
6318 else if (token1
->keyword
== RID_USING
)
6323 cp_parser_commit_to_tentative_parse (parser
);
6324 /* If the token after `using' is `namespace', then we have a
6326 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6327 if (token2
->keyword
== RID_NAMESPACE
)
6328 cp_parser_using_directive (parser
);
6329 /* Otherwise, it's a using-declaration. */
6331 cp_parser_using_declaration (parser
);
6333 /* If the next keyword is `__label__' we have a label declaration. */
6334 else if (token1
->keyword
== RID_LABEL
)
6337 cp_parser_commit_to_tentative_parse (parser
);
6338 cp_parser_label_declaration (parser
);
6340 /* Anything else must be a simple-declaration. */
6342 cp_parser_simple_declaration (parser
, !statement_p
);
6345 /* Parse a simple-declaration.
6348 decl-specifier-seq [opt] init-declarator-list [opt] ;
6350 init-declarator-list:
6352 init-declarator-list , init-declarator
6354 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6355 function-definition as a simple-declaration. */
6358 cp_parser_simple_declaration (cp_parser
* parser
,
6359 bool function_definition_allowed_p
)
6361 tree decl_specifiers
;
6363 int declares_class_or_enum
;
6364 bool saw_declarator
;
6366 /* Defer access checks until we know what is being declared; the
6367 checks for names appearing in the decl-specifier-seq should be
6368 done as if we were in the scope of the thing being declared. */
6369 push_deferring_access_checks (dk_deferred
);
6371 /* Parse the decl-specifier-seq. We have to keep track of whether
6372 or not the decl-specifier-seq declares a named class or
6373 enumeration type, since that is the only case in which the
6374 init-declarator-list is allowed to be empty.
6378 In a simple-declaration, the optional init-declarator-list can be
6379 omitted only when declaring a class or enumeration, that is when
6380 the decl-specifier-seq contains either a class-specifier, an
6381 elaborated-type-specifier, or an enum-specifier. */
6383 = cp_parser_decl_specifier_seq (parser
,
6384 CP_PARSER_FLAGS_OPTIONAL
,
6386 &declares_class_or_enum
);
6387 /* We no longer need to defer access checks. */
6388 stop_deferring_access_checks ();
6390 /* In a block scope, a valid declaration must always have a
6391 decl-specifier-seq. By not trying to parse declarators, we can
6392 resolve the declaration/expression ambiguity more quickly. */
6393 if (!function_definition_allowed_p
&& !decl_specifiers
)
6395 cp_parser_error (parser
, "expected declaration");
6399 /* If the next two tokens are both identifiers, the code is
6400 erroneous. The usual cause of this situation is code like:
6404 where "T" should name a type -- but does not. */
6405 if (cp_parser_diagnose_invalid_type_name (parser
))
6407 /* If parsing tentatively, we should commit; we really are
6408 looking at a declaration. */
6409 cp_parser_commit_to_tentative_parse (parser
);
6414 /* Keep going until we hit the `;' at the end of the simple
6416 saw_declarator
= false;
6417 while (cp_lexer_next_token_is_not (parser
->lexer
,
6421 bool function_definition_p
;
6424 saw_declarator
= true;
6425 /* Parse the init-declarator. */
6426 decl
= cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6427 function_definition_allowed_p
,
6429 declares_class_or_enum
,
6430 &function_definition_p
);
6431 /* If an error occurred while parsing tentatively, exit quickly.
6432 (That usually happens when in the body of a function; each
6433 statement is treated as a declaration-statement until proven
6435 if (cp_parser_error_occurred (parser
))
6437 /* Handle function definitions specially. */
6438 if (function_definition_p
)
6440 /* If the next token is a `,', then we are probably
6441 processing something like:
6445 which is erroneous. */
6446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6447 error ("mixing declarations and function-definitions is forbidden");
6448 /* Otherwise, we're done with the list of declarators. */
6451 pop_deferring_access_checks ();
6455 /* The next token should be either a `,' or a `;'. */
6456 token
= cp_lexer_peek_token (parser
->lexer
);
6457 /* If it's a `,', there are more declarators to come. */
6458 if (token
->type
== CPP_COMMA
)
6459 cp_lexer_consume_token (parser
->lexer
);
6460 /* If it's a `;', we are done. */
6461 else if (token
->type
== CPP_SEMICOLON
)
6463 /* Anything else is an error. */
6466 cp_parser_error (parser
, "expected `,' or `;'");
6467 /* Skip tokens until we reach the end of the statement. */
6468 cp_parser_skip_to_end_of_statement (parser
);
6471 /* After the first time around, a function-definition is not
6472 allowed -- even if it was OK at first. For example:
6477 function_definition_allowed_p
= false;
6480 /* Issue an error message if no declarators are present, and the
6481 decl-specifier-seq does not itself declare a class or
6483 if (!saw_declarator
)
6485 if (cp_parser_declares_only_class_p (parser
))
6486 shadow_tag (decl_specifiers
);
6487 /* Perform any deferred access checks. */
6488 perform_deferred_access_checks ();
6491 /* Consume the `;'. */
6492 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6495 pop_deferring_access_checks ();
6498 /* Parse a decl-specifier-seq.
6501 decl-specifier-seq [opt] decl-specifier
6504 storage-class-specifier
6513 decl-specifier-seq [opt] attributes
6515 Returns a TREE_LIST, giving the decl-specifiers in the order they
6516 appear in the source code. The TREE_VALUE of each node is the
6517 decl-specifier. For a keyword (such as `auto' or `friend'), the
6518 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6519 representation of a type-specifier, see cp_parser_type_specifier.
6521 If there are attributes, they will be stored in *ATTRIBUTES,
6522 represented as described above cp_parser_attributes.
6524 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6525 appears, and the entity that will be a friend is not going to be a
6526 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6527 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6528 friendship is granted might not be a class.
6530 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6533 1: one of the decl-specifiers is an elaborated-type-specifier
6534 (i.e., a type declaration)
6535 2: one of the decl-specifiers is an enum-specifier or a
6536 class-specifier (i.e., a type definition)
6541 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6542 cp_parser_flags flags
,
6544 int* declares_class_or_enum
)
6546 tree decl_specs
= NULL_TREE
;
6547 bool friend_p
= false;
6548 bool constructor_possible_p
= !parser
->in_declarator_p
;
6550 /* Assume no class or enumeration type is declared. */
6551 *declares_class_or_enum
= 0;
6553 /* Assume there are no attributes. */
6554 *attributes
= NULL_TREE
;
6556 /* Keep reading specifiers until there are no more to read. */
6559 tree decl_spec
= NULL_TREE
;
6563 /* Peek at the next token. */
6564 token
= cp_lexer_peek_token (parser
->lexer
);
6565 /* Handle attributes. */
6566 if (token
->keyword
== RID_ATTRIBUTE
)
6568 /* Parse the attributes. */
6569 decl_spec
= cp_parser_attributes_opt (parser
);
6570 /* Add them to the list. */
6571 *attributes
= chainon (*attributes
, decl_spec
);
6574 /* If the next token is an appropriate keyword, we can simply
6575 add it to the list. */
6576 switch (token
->keyword
)
6582 error ("duplicate `friend'");
6585 /* The representation of the specifier is simply the
6586 appropriate TREE_IDENTIFIER node. */
6587 decl_spec
= token
->value
;
6588 /* Consume the token. */
6589 cp_lexer_consume_token (parser
->lexer
);
6592 /* function-specifier:
6599 decl_spec
= cp_parser_function_specifier_opt (parser
);
6605 /* The representation of the specifier is simply the
6606 appropriate TREE_IDENTIFIER node. */
6607 decl_spec
= token
->value
;
6608 /* Consume the token. */
6609 cp_lexer_consume_token (parser
->lexer
);
6610 /* A constructor declarator cannot appear in a typedef. */
6611 constructor_possible_p
= false;
6612 /* The "typedef" keyword can only occur in a declaration; we
6613 may as well commit at this point. */
6614 cp_parser_commit_to_tentative_parse (parser
);
6617 /* storage-class-specifier:
6632 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6639 /* Constructors are a special case. The `S' in `S()' is not a
6640 decl-specifier; it is the beginning of the declarator. */
6641 constructor_p
= (!decl_spec
6642 && constructor_possible_p
6643 && cp_parser_constructor_declarator_p (parser
,
6646 /* If we don't have a DECL_SPEC yet, then we must be looking at
6647 a type-specifier. */
6648 if (!decl_spec
&& !constructor_p
)
6650 int decl_spec_declares_class_or_enum
;
6651 bool is_cv_qualifier
;
6654 = cp_parser_type_specifier (parser
, flags
,
6656 /*is_declaration=*/true,
6657 &decl_spec_declares_class_or_enum
,
6660 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6662 /* If this type-specifier referenced a user-defined type
6663 (a typedef, class-name, etc.), then we can't allow any
6664 more such type-specifiers henceforth.
6668 The longest sequence of decl-specifiers that could
6669 possibly be a type name is taken as the
6670 decl-specifier-seq of a declaration. The sequence shall
6671 be self-consistent as described below.
6675 As a general rule, at most one type-specifier is allowed
6676 in the complete decl-specifier-seq of a declaration. The
6677 only exceptions are the following:
6679 -- const or volatile can be combined with any other
6682 -- signed or unsigned can be combined with char, long,
6690 void g (const int Pc);
6692 Here, Pc is *not* part of the decl-specifier seq; it's
6693 the declarator. Therefore, once we see a type-specifier
6694 (other than a cv-qualifier), we forbid any additional
6695 user-defined types. We *do* still allow things like `int
6696 int' to be considered a decl-specifier-seq, and issue the
6697 error message later. */
6698 if (decl_spec
&& !is_cv_qualifier
)
6699 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6700 /* A constructor declarator cannot follow a type-specifier. */
6702 constructor_possible_p
= false;
6705 /* If we still do not have a DECL_SPEC, then there are no more
6709 /* Issue an error message, unless the entire construct was
6711 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6713 cp_parser_error (parser
, "expected decl specifier");
6714 return error_mark_node
;
6720 /* Add the DECL_SPEC to the list of specifiers. */
6721 if (decl_specs
== NULL
|| TREE_VALUE (decl_specs
) != error_mark_node
)
6722 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6724 /* After we see one decl-specifier, further decl-specifiers are
6726 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6729 /* Don't allow a friend specifier with a class definition. */
6730 if (friend_p
&& (*declares_class_or_enum
& 2))
6731 error ("class definition may not be declared a friend");
6733 /* We have built up the DECL_SPECS in reverse order. Return them in
6734 the correct order. */
6735 return nreverse (decl_specs
);
6738 /* Parse an (optional) storage-class-specifier.
6740 storage-class-specifier:
6749 storage-class-specifier:
6752 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6755 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
6757 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6765 /* Consume the token. */
6766 return cp_lexer_consume_token (parser
->lexer
)->value
;
6773 /* Parse an (optional) function-specifier.
6780 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6783 cp_parser_function_specifier_opt (cp_parser
* parser
)
6785 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6790 /* Consume the token. */
6791 return cp_lexer_consume_token (parser
->lexer
)->value
;
6798 /* Parse a linkage-specification.
6800 linkage-specification:
6801 extern string-literal { declaration-seq [opt] }
6802 extern string-literal declaration */
6805 cp_parser_linkage_specification (cp_parser
* parser
)
6810 /* Look for the `extern' keyword. */
6811 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
6813 /* Peek at the next token. */
6814 token
= cp_lexer_peek_token (parser
->lexer
);
6815 /* If it's not a string-literal, then there's a problem. */
6816 if (!cp_parser_is_string_literal (token
))
6818 cp_parser_error (parser
, "expected language-name");
6821 /* Consume the token. */
6822 cp_lexer_consume_token (parser
->lexer
);
6824 /* Transform the literal into an identifier. If the literal is a
6825 wide-character string, or contains embedded NULs, then we can't
6826 handle it as the user wants. */
6827 if (token
->type
== CPP_WSTRING
6828 || (strlen (TREE_STRING_POINTER (token
->value
))
6829 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
6831 cp_parser_error (parser
, "invalid linkage-specification");
6832 /* Assume C++ linkage. */
6833 linkage
= get_identifier ("c++");
6835 /* If it's a simple string constant, things are easier. */
6837 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
6839 /* We're now using the new linkage. */
6840 push_lang_context (linkage
);
6842 /* If the next token is a `{', then we're using the first
6844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6846 /* Consume the `{' token. */
6847 cp_lexer_consume_token (parser
->lexer
);
6848 /* Parse the declarations. */
6849 cp_parser_declaration_seq_opt (parser
);
6850 /* Look for the closing `}'. */
6851 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6853 /* Otherwise, there's just one declaration. */
6856 bool saved_in_unbraced_linkage_specification_p
;
6858 saved_in_unbraced_linkage_specification_p
6859 = parser
->in_unbraced_linkage_specification_p
;
6860 parser
->in_unbraced_linkage_specification_p
= true;
6861 have_extern_spec
= true;
6862 cp_parser_declaration (parser
);
6863 have_extern_spec
= false;
6864 parser
->in_unbraced_linkage_specification_p
6865 = saved_in_unbraced_linkage_specification_p
;
6868 /* We're done with the linkage-specification. */
6869 pop_lang_context ();
6872 /* Special member functions [gram.special] */
6874 /* Parse a conversion-function-id.
6876 conversion-function-id:
6877 operator conversion-type-id
6879 Returns an IDENTIFIER_NODE representing the operator. */
6882 cp_parser_conversion_function_id (cp_parser
* parser
)
6886 tree saved_qualifying_scope
;
6887 tree saved_object_scope
;
6889 /* Look for the `operator' token. */
6890 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
6891 return error_mark_node
;
6892 /* When we parse the conversion-type-id, the current scope will be
6893 reset. However, we need that information in able to look up the
6894 conversion function later, so we save it here. */
6895 saved_scope
= parser
->scope
;
6896 saved_qualifying_scope
= parser
->qualifying_scope
;
6897 saved_object_scope
= parser
->object_scope
;
6898 /* We must enter the scope of the class so that the names of
6899 entities declared within the class are available in the
6900 conversion-type-id. For example, consider:
6907 S::operator I() { ... }
6909 In order to see that `I' is a type-name in the definition, we
6910 must be in the scope of `S'. */
6912 push_scope (saved_scope
);
6913 /* Parse the conversion-type-id. */
6914 type
= cp_parser_conversion_type_id (parser
);
6915 /* Leave the scope of the class, if any. */
6917 pop_scope (saved_scope
);
6918 /* Restore the saved scope. */
6919 parser
->scope
= saved_scope
;
6920 parser
->qualifying_scope
= saved_qualifying_scope
;
6921 parser
->object_scope
= saved_object_scope
;
6922 /* If the TYPE is invalid, indicate failure. */
6923 if (type
== error_mark_node
)
6924 return error_mark_node
;
6925 return mangle_conv_op_name_for_type (type
);
6928 /* Parse a conversion-type-id:
6931 type-specifier-seq conversion-declarator [opt]
6933 Returns the TYPE specified. */
6936 cp_parser_conversion_type_id (cp_parser
* parser
)
6939 tree type_specifiers
;
6942 /* Parse the attributes. */
6943 attributes
= cp_parser_attributes_opt (parser
);
6944 /* Parse the type-specifiers. */
6945 type_specifiers
= cp_parser_type_specifier_seq (parser
);
6946 /* If that didn't work, stop. */
6947 if (type_specifiers
== error_mark_node
)
6948 return error_mark_node
;
6949 /* Parse the conversion-declarator. */
6950 declarator
= cp_parser_conversion_declarator_opt (parser
);
6952 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
6953 /*initialized=*/0, &attributes
);
6956 /* Parse an (optional) conversion-declarator.
6958 conversion-declarator:
6959 ptr-operator conversion-declarator [opt]
6961 Returns a representation of the declarator. See
6962 cp_parser_declarator for details. */
6965 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
6967 enum tree_code code
;
6969 tree cv_qualifier_seq
;
6971 /* We don't know if there's a ptr-operator next, or not. */
6972 cp_parser_parse_tentatively (parser
);
6973 /* Try the ptr-operator. */
6974 code
= cp_parser_ptr_operator (parser
, &class_type
,
6976 /* If it worked, look for more conversion-declarators. */
6977 if (cp_parser_parse_definitely (parser
))
6981 /* Parse another optional declarator. */
6982 declarator
= cp_parser_conversion_declarator_opt (parser
);
6984 /* Create the representation of the declarator. */
6985 if (code
== INDIRECT_REF
)
6986 declarator
= make_pointer_declarator (cv_qualifier_seq
,
6989 declarator
= make_reference_declarator (cv_qualifier_seq
,
6992 /* Handle the pointer-to-member case. */
6994 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
7002 /* Parse an (optional) ctor-initializer.
7005 : mem-initializer-list
7007 Returns TRUE iff the ctor-initializer was actually present. */
7010 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7012 /* If the next token is not a `:', then there is no
7013 ctor-initializer. */
7014 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7016 /* Do default initialization of any bases and members. */
7017 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7018 finish_mem_initializers (NULL_TREE
);
7023 /* Consume the `:' token. */
7024 cp_lexer_consume_token (parser
->lexer
);
7025 /* And the mem-initializer-list. */
7026 cp_parser_mem_initializer_list (parser
);
7031 /* Parse a mem-initializer-list.
7033 mem-initializer-list:
7035 mem-initializer , mem-initializer-list */
7038 cp_parser_mem_initializer_list (cp_parser
* parser
)
7040 tree mem_initializer_list
= NULL_TREE
;
7042 /* Let the semantic analysis code know that we are starting the
7043 mem-initializer-list. */
7044 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7045 error ("only constructors take base initializers");
7047 /* Loop through the list. */
7050 tree mem_initializer
;
7052 /* Parse the mem-initializer. */
7053 mem_initializer
= cp_parser_mem_initializer (parser
);
7054 /* Add it to the list, unless it was erroneous. */
7055 if (mem_initializer
)
7057 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7058 mem_initializer_list
= mem_initializer
;
7060 /* If the next token is not a `,', we're done. */
7061 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7063 /* Consume the `,' token. */
7064 cp_lexer_consume_token (parser
->lexer
);
7067 /* Perform semantic analysis. */
7068 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7069 finish_mem_initializers (mem_initializer_list
);
7072 /* Parse a mem-initializer.
7075 mem-initializer-id ( expression-list [opt] )
7080 ( expression-list [opt] )
7082 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7083 class) or FIELD_DECL (for a non-static data member) to initialize;
7084 the TREE_VALUE is the expression-list. */
7087 cp_parser_mem_initializer (cp_parser
* parser
)
7089 tree mem_initializer_id
;
7090 tree expression_list
;
7093 /* Find out what is being initialized. */
7094 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7096 pedwarn ("anachronistic old-style base class initializer");
7097 mem_initializer_id
= NULL_TREE
;
7100 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7101 member
= expand_member_init (mem_initializer_id
);
7102 if (member
&& !DECL_P (member
))
7103 in_base_initializer
= 1;
7106 = cp_parser_parenthesized_expression_list (parser
, false,
7107 /*non_constant_p=*/NULL
);
7108 if (!expression_list
)
7109 expression_list
= void_type_node
;
7111 in_base_initializer
= 0;
7113 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7116 /* Parse a mem-initializer-id.
7119 :: [opt] nested-name-specifier [opt] class-name
7122 Returns a TYPE indicating the class to be initializer for the first
7123 production. Returns an IDENTIFIER_NODE indicating the data member
7124 to be initialized for the second production. */
7127 cp_parser_mem_initializer_id (cp_parser
* parser
)
7129 bool global_scope_p
;
7130 bool nested_name_specifier_p
;
7133 /* Look for the optional `::' operator. */
7135 = (cp_parser_global_scope_opt (parser
,
7136 /*current_scope_valid_p=*/false)
7138 /* Look for the optional nested-name-specifier. The simplest way to
7143 The keyword `typename' is not permitted in a base-specifier or
7144 mem-initializer; in these contexts a qualified name that
7145 depends on a template-parameter is implicitly assumed to be a
7148 is to assume that we have seen the `typename' keyword at this
7150 nested_name_specifier_p
7151 = (cp_parser_nested_name_specifier_opt (parser
,
7152 /*typename_keyword_p=*/true,
7153 /*check_dependency_p=*/true,
7155 /*is_declaration=*/true)
7157 /* If there is a `::' operator or a nested-name-specifier, then we
7158 are definitely looking for a class-name. */
7159 if (global_scope_p
|| nested_name_specifier_p
)
7160 return cp_parser_class_name (parser
,
7161 /*typename_keyword_p=*/true,
7162 /*template_keyword_p=*/false,
7164 /*check_dependency_p=*/true,
7165 /*class_head_p=*/false,
7166 /*is_declaration=*/true);
7167 /* Otherwise, we could also be looking for an ordinary identifier. */
7168 cp_parser_parse_tentatively (parser
);
7169 /* Try a class-name. */
7170 id
= cp_parser_class_name (parser
,
7171 /*typename_keyword_p=*/true,
7172 /*template_keyword_p=*/false,
7174 /*check_dependency_p=*/true,
7175 /*class_head_p=*/false,
7176 /*is_declaration=*/true);
7177 /* If we found one, we're done. */
7178 if (cp_parser_parse_definitely (parser
))
7180 /* Otherwise, look for an ordinary identifier. */
7181 return cp_parser_identifier (parser
);
7184 /* Overloading [gram.over] */
7186 /* Parse an operator-function-id.
7188 operator-function-id:
7191 Returns an IDENTIFIER_NODE for the operator which is a
7192 human-readable spelling of the identifier, e.g., `operator +'. */
7195 cp_parser_operator_function_id (cp_parser
* parser
)
7197 /* Look for the `operator' keyword. */
7198 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7199 return error_mark_node
;
7200 /* And then the name of the operator itself. */
7201 return cp_parser_operator (parser
);
7204 /* Parse an operator.
7207 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7208 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7209 || ++ -- , ->* -> () []
7216 Returns an IDENTIFIER_NODE for the operator which is a
7217 human-readable spelling of the identifier, e.g., `operator +'. */
7220 cp_parser_operator (cp_parser
* parser
)
7222 tree id
= NULL_TREE
;
7225 /* Peek at the next token. */
7226 token
= cp_lexer_peek_token (parser
->lexer
);
7227 /* Figure out which operator we have. */
7228 switch (token
->type
)
7234 /* The keyword should be either `new' or `delete'. */
7235 if (token
->keyword
== RID_NEW
)
7237 else if (token
->keyword
== RID_DELETE
)
7242 /* Consume the `new' or `delete' token. */
7243 cp_lexer_consume_token (parser
->lexer
);
7245 /* Peek at the next token. */
7246 token
= cp_lexer_peek_token (parser
->lexer
);
7247 /* If it's a `[' token then this is the array variant of the
7249 if (token
->type
== CPP_OPEN_SQUARE
)
7251 /* Consume the `[' token. */
7252 cp_lexer_consume_token (parser
->lexer
);
7253 /* Look for the `]' token. */
7254 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7255 id
= ansi_opname (op
== NEW_EXPR
7256 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7258 /* Otherwise, we have the non-array variant. */
7260 id
= ansi_opname (op
);
7266 id
= ansi_opname (PLUS_EXPR
);
7270 id
= ansi_opname (MINUS_EXPR
);
7274 id
= ansi_opname (MULT_EXPR
);
7278 id
= ansi_opname (TRUNC_DIV_EXPR
);
7282 id
= ansi_opname (TRUNC_MOD_EXPR
);
7286 id
= ansi_opname (BIT_XOR_EXPR
);
7290 id
= ansi_opname (BIT_AND_EXPR
);
7294 id
= ansi_opname (BIT_IOR_EXPR
);
7298 id
= ansi_opname (BIT_NOT_EXPR
);
7302 id
= ansi_opname (TRUTH_NOT_EXPR
);
7306 id
= ansi_assopname (NOP_EXPR
);
7310 id
= ansi_opname (LT_EXPR
);
7314 id
= ansi_opname (GT_EXPR
);
7318 id
= ansi_assopname (PLUS_EXPR
);
7322 id
= ansi_assopname (MINUS_EXPR
);
7326 id
= ansi_assopname (MULT_EXPR
);
7330 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7334 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7338 id
= ansi_assopname (BIT_XOR_EXPR
);
7342 id
= ansi_assopname (BIT_AND_EXPR
);
7346 id
= ansi_assopname (BIT_IOR_EXPR
);
7350 id
= ansi_opname (LSHIFT_EXPR
);
7354 id
= ansi_opname (RSHIFT_EXPR
);
7358 id
= ansi_assopname (LSHIFT_EXPR
);
7362 id
= ansi_assopname (RSHIFT_EXPR
);
7366 id
= ansi_opname (EQ_EXPR
);
7370 id
= ansi_opname (NE_EXPR
);
7374 id
= ansi_opname (LE_EXPR
);
7377 case CPP_GREATER_EQ
:
7378 id
= ansi_opname (GE_EXPR
);
7382 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7386 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7390 id
= ansi_opname (POSTINCREMENT_EXPR
);
7393 case CPP_MINUS_MINUS
:
7394 id
= ansi_opname (PREDECREMENT_EXPR
);
7398 id
= ansi_opname (COMPOUND_EXPR
);
7401 case CPP_DEREF_STAR
:
7402 id
= ansi_opname (MEMBER_REF
);
7406 id
= ansi_opname (COMPONENT_REF
);
7409 case CPP_OPEN_PAREN
:
7410 /* Consume the `('. */
7411 cp_lexer_consume_token (parser
->lexer
);
7412 /* Look for the matching `)'. */
7413 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7414 return ansi_opname (CALL_EXPR
);
7416 case CPP_OPEN_SQUARE
:
7417 /* Consume the `['. */
7418 cp_lexer_consume_token (parser
->lexer
);
7419 /* Look for the matching `]'. */
7420 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7421 return ansi_opname (ARRAY_REF
);
7425 id
= ansi_opname (MIN_EXPR
);
7429 id
= ansi_opname (MAX_EXPR
);
7433 id
= ansi_assopname (MIN_EXPR
);
7437 id
= ansi_assopname (MAX_EXPR
);
7441 /* Anything else is an error. */
7445 /* If we have selected an identifier, we need to consume the
7448 cp_lexer_consume_token (parser
->lexer
);
7449 /* Otherwise, no valid operator name was present. */
7452 cp_parser_error (parser
, "expected operator");
7453 id
= error_mark_node
;
7459 /* Parse a template-declaration.
7461 template-declaration:
7462 export [opt] template < template-parameter-list > declaration
7464 If MEMBER_P is TRUE, this template-declaration occurs within a
7467 The grammar rule given by the standard isn't correct. What
7470 template-declaration:
7471 export [opt] template-parameter-list-seq
7472 decl-specifier-seq [opt] init-declarator [opt] ;
7473 export [opt] template-parameter-list-seq
7476 template-parameter-list-seq:
7477 template-parameter-list-seq [opt]
7478 template < template-parameter-list > */
7481 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7483 /* Check for `export'. */
7484 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7486 /* Consume the `export' token. */
7487 cp_lexer_consume_token (parser
->lexer
);
7488 /* Warn that we do not support `export'. */
7489 warning ("keyword `export' not implemented, and will be ignored");
7492 cp_parser_template_declaration_after_export (parser
, member_p
);
7495 /* Parse a template-parameter-list.
7497 template-parameter-list:
7499 template-parameter-list , template-parameter
7501 Returns a TREE_LIST. Each node represents a template parameter.
7502 The nodes are connected via their TREE_CHAINs. */
7505 cp_parser_template_parameter_list (cp_parser
* parser
)
7507 tree parameter_list
= NULL_TREE
;
7514 /* Parse the template-parameter. */
7515 parameter
= cp_parser_template_parameter (parser
);
7516 /* Add it to the list. */
7517 parameter_list
= process_template_parm (parameter_list
,
7520 /* Peek at the next token. */
7521 token
= cp_lexer_peek_token (parser
->lexer
);
7522 /* If it's not a `,', we're done. */
7523 if (token
->type
!= CPP_COMMA
)
7525 /* Otherwise, consume the `,' token. */
7526 cp_lexer_consume_token (parser
->lexer
);
7529 return parameter_list
;
7532 /* Parse a template-parameter.
7536 parameter-declaration
7538 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7539 TREE_PURPOSE is the default value, if any. */
7542 cp_parser_template_parameter (cp_parser
* parser
)
7546 /* Peek at the next token. */
7547 token
= cp_lexer_peek_token (parser
->lexer
);
7548 /* If it is `class' or `template', we have a type-parameter. */
7549 if (token
->keyword
== RID_TEMPLATE
)
7550 return cp_parser_type_parameter (parser
);
7551 /* If it is `class' or `typename' we do not know yet whether it is a
7552 type parameter or a non-type parameter. Consider:
7554 template <typename T, typename T::X X> ...
7558 template <class C, class D*> ...
7560 Here, the first parameter is a type parameter, and the second is
7561 a non-type parameter. We can tell by looking at the token after
7562 the identifier -- if it is a `,', `=', or `>' then we have a type
7564 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7566 /* Peek at the token after `class' or `typename'. */
7567 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7568 /* If it's an identifier, skip it. */
7569 if (token
->type
== CPP_NAME
)
7570 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7571 /* Now, see if the token looks like the end of a template
7573 if (token
->type
== CPP_COMMA
7574 || token
->type
== CPP_EQ
7575 || token
->type
== CPP_GREATER
)
7576 return cp_parser_type_parameter (parser
);
7579 /* Otherwise, it is a non-type parameter.
7583 When parsing a default template-argument for a non-type
7584 template-parameter, the first non-nested `>' is taken as the end
7585 of the template parameter-list rather than a greater-than
7588 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
7589 /*parenthesized_p=*/NULL
);
7592 /* Parse a type-parameter.
7595 class identifier [opt]
7596 class identifier [opt] = type-id
7597 typename identifier [opt]
7598 typename identifier [opt] = type-id
7599 template < template-parameter-list > class identifier [opt]
7600 template < template-parameter-list > class identifier [opt]
7603 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7604 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7605 the declaration of the parameter. */
7608 cp_parser_type_parameter (cp_parser
* parser
)
7613 /* Look for a keyword to tell us what kind of parameter this is. */
7614 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7615 "`class', `typename', or `template'");
7617 return error_mark_node
;
7619 switch (token
->keyword
)
7625 tree default_argument
;
7627 /* If the next token is an identifier, then it names the
7629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7630 identifier
= cp_parser_identifier (parser
);
7632 identifier
= NULL_TREE
;
7634 /* Create the parameter. */
7635 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7637 /* If the next token is an `=', we have a default argument. */
7638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7640 /* Consume the `=' token. */
7641 cp_lexer_consume_token (parser
->lexer
);
7642 /* Parse the default-argument. */
7643 default_argument
= cp_parser_type_id (parser
);
7646 default_argument
= NULL_TREE
;
7648 /* Create the combined representation of the parameter and the
7649 default argument. */
7650 parameter
= build_tree_list (default_argument
, parameter
);
7656 tree parameter_list
;
7658 tree default_argument
;
7660 /* Look for the `<'. */
7661 cp_parser_require (parser
, CPP_LESS
, "`<'");
7662 /* Parse the template-parameter-list. */
7663 begin_template_parm_list ();
7665 = cp_parser_template_parameter_list (parser
);
7666 parameter_list
= end_template_parm_list (parameter_list
);
7667 /* Look for the `>'. */
7668 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7669 /* Look for the `class' keyword. */
7670 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7671 /* If the next token is an `=', then there is a
7672 default-argument. If the next token is a `>', we are at
7673 the end of the parameter-list. If the next token is a `,',
7674 then we are at the end of this parameter. */
7675 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7676 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7677 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7678 identifier
= cp_parser_identifier (parser
);
7680 identifier
= NULL_TREE
;
7681 /* Create the template parameter. */
7682 parameter
= finish_template_template_parm (class_type_node
,
7685 /* If the next token is an `=', then there is a
7686 default-argument. */
7687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7691 /* Consume the `='. */
7692 cp_lexer_consume_token (parser
->lexer
);
7693 /* Parse the id-expression. */
7695 = cp_parser_id_expression (parser
,
7696 /*template_keyword_p=*/false,
7697 /*check_dependency_p=*/true,
7698 /*template_p=*/&is_template
,
7699 /*declarator_p=*/false);
7700 /* Look up the name. */
7702 = cp_parser_lookup_name (parser
, default_argument
,
7704 /*is_template=*/is_template
,
7705 /*is_namespace=*/false,
7706 /*check_dependency=*/true);
7707 /* See if the default argument is valid. */
7709 = check_template_template_default_arg (default_argument
);
7712 default_argument
= NULL_TREE
;
7714 /* Create the combined representation of the parameter and the
7715 default argument. */
7716 parameter
= build_tree_list (default_argument
, parameter
);
7721 /* Anything else is an error. */
7722 cp_parser_error (parser
,
7723 "expected `class', `typename', or `template'");
7724 parameter
= error_mark_node
;
7730 /* Parse a template-id.
7733 template-name < template-argument-list [opt] >
7735 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7736 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7737 returned. Otherwise, if the template-name names a function, or set
7738 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7739 names a class, returns a TYPE_DECL for the specialization.
7741 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7742 uninstantiated templates. */
7745 cp_parser_template_id (cp_parser
*parser
,
7746 bool template_keyword_p
,
7747 bool check_dependency_p
,
7748 bool is_declaration
)
7753 ptrdiff_t start_of_id
;
7754 tree access_check
= NULL_TREE
;
7755 cp_token
*next_token
;
7758 /* If the next token corresponds to a template-id, there is no need
7760 next_token
= cp_lexer_peek_token (parser
->lexer
);
7761 if (next_token
->type
== CPP_TEMPLATE_ID
)
7766 /* Get the stored value. */
7767 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
7768 /* Perform any access checks that were deferred. */
7769 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
7770 perform_or_defer_access_check (TREE_PURPOSE (check
),
7771 TREE_VALUE (check
));
7772 /* Return the stored value. */
7773 return TREE_VALUE (value
);
7776 /* Avoid performing name lookup if there is no possibility of
7777 finding a template-id. */
7778 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
7779 || (next_token
->type
== CPP_NAME
7780 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
))
7782 cp_parser_error (parser
, "expected template-id");
7783 return error_mark_node
;
7786 /* Remember where the template-id starts. */
7787 if (cp_parser_parsing_tentatively (parser
)
7788 && !cp_parser_committed_to_tentative_parse (parser
))
7790 next_token
= cp_lexer_peek_token (parser
->lexer
);
7791 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
7792 parser
->lexer
->first_token
,
7798 push_deferring_access_checks (dk_deferred
);
7800 /* Parse the template-name. */
7801 is_identifier
= false;
7802 template = cp_parser_template_name (parser
, template_keyword_p
,
7806 if (template == error_mark_node
|| is_identifier
)
7808 pop_deferring_access_checks ();
7812 /* Look for the `<' that starts the template-argument-list. */
7813 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
7815 pop_deferring_access_checks ();
7816 return error_mark_node
;
7819 /* Parse the arguments. */
7820 arguments
= cp_parser_enclosed_template_argument_list (parser
);
7822 /* Build a representation of the specialization. */
7823 if (TREE_CODE (template) == IDENTIFIER_NODE
)
7824 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
7825 else if (DECL_CLASS_TEMPLATE_P (template)
7826 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7828 = finish_template_type (template, arguments
,
7829 cp_lexer_next_token_is (parser
->lexer
,
7833 /* If it's not a class-template or a template-template, it should be
7834 a function-template. */
7835 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7836 || TREE_CODE (template) == OVERLOAD
7837 || BASELINK_P (template)),
7840 template_id
= lookup_template_function (template, arguments
);
7843 /* Retrieve any deferred checks. Do not pop this access checks yet
7844 so the memory will not be reclaimed during token replacing below. */
7845 access_check
= get_deferred_access_checks ();
7847 /* If parsing tentatively, replace the sequence of tokens that makes
7848 up the template-id with a CPP_TEMPLATE_ID token. That way,
7849 should we re-parse the token stream, we will not have to repeat
7850 the effort required to do the parse, nor will we issue duplicate
7851 error messages about problems during instantiation of the
7853 if (start_of_id
>= 0)
7857 /* Find the token that corresponds to the start of the
7859 token
= cp_lexer_advance_token (parser
->lexer
,
7860 parser
->lexer
->first_token
,
7863 /* Reset the contents of the START_OF_ID token. */
7864 token
->type
= CPP_TEMPLATE_ID
;
7865 token
->value
= build_tree_list (access_check
, template_id
);
7866 token
->keyword
= RID_MAX
;
7867 /* Purge all subsequent tokens. */
7868 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
7871 pop_deferring_access_checks ();
7875 /* Parse a template-name.
7880 The standard should actually say:
7884 operator-function-id
7885 conversion-function-id
7887 A defect report has been filed about this issue.
7889 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7890 `template' keyword, in a construction like:
7894 In that case `f' is taken to be a template-name, even though there
7895 is no way of knowing for sure.
7897 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7898 name refers to a set of overloaded functions, at least one of which
7899 is a template, or an IDENTIFIER_NODE with the name of the template,
7900 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7901 names are looked up inside uninstantiated templates. */
7904 cp_parser_template_name (cp_parser
* parser
,
7905 bool template_keyword_p
,
7906 bool check_dependency_p
,
7907 bool is_declaration
,
7908 bool *is_identifier
)
7914 /* If the next token is `operator', then we have either an
7915 operator-function-id or a conversion-function-id. */
7916 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
7918 /* We don't know whether we're looking at an
7919 operator-function-id or a conversion-function-id. */
7920 cp_parser_parse_tentatively (parser
);
7921 /* Try an operator-function-id. */
7922 identifier
= cp_parser_operator_function_id (parser
);
7923 /* If that didn't work, try a conversion-function-id. */
7924 if (!cp_parser_parse_definitely (parser
))
7925 identifier
= cp_parser_conversion_function_id (parser
);
7927 /* Look for the identifier. */
7929 identifier
= cp_parser_identifier (parser
);
7931 /* If we didn't find an identifier, we don't have a template-id. */
7932 if (identifier
== error_mark_node
)
7933 return error_mark_node
;
7935 /* If the name immediately followed the `template' keyword, then it
7936 is a template-name. However, if the next token is not `<', then
7937 we do not treat it as a template-name, since it is not being used
7938 as part of a template-id. This enables us to handle constructs
7941 template <typename T> struct S { S(); };
7942 template <typename T> S<T>::S();
7944 correctly. We would treat `S' as a template -- if it were `S<T>'
7945 -- but we do not if there is no `<'. */
7947 if (processing_template_decl
7948 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
7950 /* In a declaration, in a dependent context, we pretend that the
7951 "template" keyword was present in order to improve error
7952 recovery. For example, given:
7954 template <typename T> void f(T::X<int>);
7956 we want to treat "X<int>" as a template-id. */
7958 && !template_keyword_p
7959 && parser
->scope
&& TYPE_P (parser
->scope
)
7960 && dependent_type_p (parser
->scope
))
7964 /* Explain what went wrong. */
7965 error ("non-template `%D' used as template", identifier
);
7966 error ("(use `%T::template %D' to indicate that it is a template)",
7967 parser
->scope
, identifier
);
7968 /* If parsing tentatively, find the location of the "<"
7970 if (cp_parser_parsing_tentatively (parser
)
7971 && !cp_parser_committed_to_tentative_parse (parser
))
7973 cp_parser_simulate_error (parser
);
7974 token
= cp_lexer_peek_token (parser
->lexer
);
7975 token
= cp_lexer_prev_token (parser
->lexer
, token
);
7976 start
= cp_lexer_token_difference (parser
->lexer
,
7977 parser
->lexer
->first_token
,
7982 /* Parse the template arguments so that we can issue error
7983 messages about them. */
7984 cp_lexer_consume_token (parser
->lexer
);
7985 cp_parser_enclosed_template_argument_list (parser
);
7986 /* Skip tokens until we find a good place from which to
7987 continue parsing. */
7988 cp_parser_skip_to_closing_parenthesis (parser
,
7989 /*recovering=*/true,
7991 /*consume_paren=*/false);
7992 /* If parsing tentatively, permanently remove the
7993 template argument list. That will prevent duplicate
7994 error messages from being issued about the missing
7995 "template" keyword. */
7998 token
= cp_lexer_advance_token (parser
->lexer
,
7999 parser
->lexer
->first_token
,
8001 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8004 *is_identifier
= true;
8007 if (template_keyword_p
)
8011 /* Look up the name. */
8012 decl
= cp_parser_lookup_name (parser
, identifier
,
8014 /*is_template=*/false,
8015 /*is_namespace=*/false,
8016 check_dependency_p
);
8017 decl
= maybe_get_template_decl_from_type_decl (decl
);
8019 /* If DECL is a template, then the name was a template-name. */
8020 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8024 /* The standard does not explicitly indicate whether a name that
8025 names a set of overloaded declarations, some of which are
8026 templates, is a template-name. However, such a name should
8027 be a template-name; otherwise, there is no way to form a
8028 template-id for the overloaded templates. */
8029 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8030 if (TREE_CODE (fns
) == OVERLOAD
)
8034 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8035 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8040 /* Otherwise, the name does not name a template. */
8041 cp_parser_error (parser
, "expected template-name");
8042 return error_mark_node
;
8046 /* If DECL is dependent, and refers to a function, then just return
8047 its name; we will look it up again during template instantiation. */
8048 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8050 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8051 if (TYPE_P (scope
) && dependent_type_p (scope
))
8058 /* Parse a template-argument-list.
8060 template-argument-list:
8062 template-argument-list , template-argument
8064 Returns a TREE_VEC containing the arguments. */
8067 cp_parser_template_argument_list (cp_parser
* parser
)
8069 tree fixed_args
[10];
8070 unsigned n_args
= 0;
8071 unsigned alloced
= 10;
8072 tree
*arg_ary
= fixed_args
;
8074 bool saved_in_template_argument_list_p
;
8076 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8077 parser
->in_template_argument_list_p
= true;
8083 /* Consume the comma. */
8084 cp_lexer_consume_token (parser
->lexer
);
8086 /* Parse the template-argument. */
8087 argument
= cp_parser_template_argument (parser
);
8088 if (n_args
== alloced
)
8092 if (arg_ary
== fixed_args
)
8094 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8095 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8098 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8100 arg_ary
[n_args
++] = argument
;
8102 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8104 vec
= make_tree_vec (n_args
);
8107 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8109 if (arg_ary
!= fixed_args
)
8111 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8115 /* Parse a template-argument.
8118 assignment-expression
8122 The representation is that of an assignment-expression, type-id, or
8123 id-expression -- except that the qualified id-expression is
8124 evaluated, so that the value returned is either a DECL or an
8127 Although the standard says "assignment-expression", it forbids
8128 throw-expressions or assignments in the template argument.
8129 Therefore, we use "conditional-expression" instead. */
8132 cp_parser_template_argument (cp_parser
* parser
)
8137 bool maybe_type_id
= false;
8140 tree qualifying_class
;
8142 /* There's really no way to know what we're looking at, so we just
8143 try each alternative in order.
8147 In a template-argument, an ambiguity between a type-id and an
8148 expression is resolved to a type-id, regardless of the form of
8149 the corresponding template-parameter.
8151 Therefore, we try a type-id first. */
8152 cp_parser_parse_tentatively (parser
);
8153 argument
= cp_parser_type_id (parser
);
8154 /* If there was no error parsing the type-id but the next token is a '>>',
8155 we probably found a typo for '> >'. But there are type-id which are
8156 also valid expressions. For instance:
8158 struct X { int operator >> (int); };
8159 template <int V> struct Foo {};
8162 Here 'X()' is a valid type-id of a function type, but the user just
8163 wanted to write the expression "X() >> 5". Thus, we remember that we
8164 found a valid type-id, but we still try to parse the argument as an
8165 expression to see what happens. */
8166 if (!cp_parser_error_occurred (parser
)
8167 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8169 maybe_type_id
= true;
8170 cp_parser_abort_tentative_parse (parser
);
8174 /* If the next token isn't a `,' or a `>', then this argument wasn't
8175 really finished. This means that the argument is not a valid
8177 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8178 cp_parser_error (parser
, "expected template-argument");
8179 /* If that worked, we're done. */
8180 if (cp_parser_parse_definitely (parser
))
8183 /* We're still not sure what the argument will be. */
8184 cp_parser_parse_tentatively (parser
);
8185 /* Try a template. */
8186 argument
= cp_parser_id_expression (parser
,
8187 /*template_keyword_p=*/false,
8188 /*check_dependency_p=*/true,
8190 /*declarator_p=*/false);
8191 /* If the next token isn't a `,' or a `>', then this argument wasn't
8193 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8194 cp_parser_error (parser
, "expected template-argument");
8195 if (!cp_parser_error_occurred (parser
))
8197 /* Figure out what is being referred to. */
8198 argument
= cp_parser_lookup_name (parser
, argument
,
8200 /*is_template=*/template_p
,
8201 /*is_namespace=*/false,
8202 /*check_dependency=*/true);
8203 if (TREE_CODE (argument
) != TEMPLATE_DECL
8204 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8205 cp_parser_error (parser
, "expected template-name");
8207 if (cp_parser_parse_definitely (parser
))
8209 /* It must be a non-type argument. There permitted cases are given
8210 in [temp.arg.nontype]:
8212 -- an integral constant-expression of integral or enumeration
8215 -- the name of a non-type template-parameter; or
8217 -- the name of an object or function with external linkage...
8219 -- the address of an object or function with external linkage...
8221 -- a pointer to member... */
8222 /* Look for a non-type template parameter. */
8223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8225 cp_parser_parse_tentatively (parser
);
8226 argument
= cp_parser_primary_expression (parser
,
8229 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8230 || !cp_parser_next_token_ends_template_argument_p (parser
))
8231 cp_parser_simulate_error (parser
);
8232 if (cp_parser_parse_definitely (parser
))
8235 /* If the next token is "&", the argument must be the address of an
8236 object or function with external linkage. */
8237 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8239 cp_lexer_consume_token (parser
->lexer
);
8240 /* See if we might have an id-expression. */
8241 token
= cp_lexer_peek_token (parser
->lexer
);
8242 if (token
->type
== CPP_NAME
8243 || token
->keyword
== RID_OPERATOR
8244 || token
->type
== CPP_SCOPE
8245 || token
->type
== CPP_TEMPLATE_ID
8246 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8248 cp_parser_parse_tentatively (parser
);
8249 argument
= cp_parser_primary_expression (parser
,
8252 if (cp_parser_error_occurred (parser
)
8253 || !cp_parser_next_token_ends_template_argument_p (parser
))
8254 cp_parser_abort_tentative_parse (parser
);
8257 if (qualifying_class
)
8258 argument
= finish_qualified_id_expr (qualifying_class
,
8262 if (TREE_CODE (argument
) == VAR_DECL
)
8264 /* A variable without external linkage might still be a
8265 valid constant-expression, so no error is issued here
8266 if the external-linkage check fails. */
8267 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8268 cp_parser_simulate_error (parser
);
8270 else if (is_overloaded_fn (argument
))
8271 /* All overloaded functions are allowed; if the external
8272 linkage test does not pass, an error will be issued
8276 && (TREE_CODE (argument
) == OFFSET_REF
8277 || TREE_CODE (argument
) == SCOPE_REF
))
8278 /* A pointer-to-member. */
8281 cp_parser_simulate_error (parser
);
8283 if (cp_parser_parse_definitely (parser
))
8286 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8291 /* If the argument started with "&", there are no other valid
8292 alternatives at this point. */
8295 cp_parser_error (parser
, "invalid non-type template argument");
8296 return error_mark_node
;
8298 /* If the argument wasn't successfully parsed as a type-id followed
8299 by '>>', the argument can only be a constant expression now.
8300 Otherwise, we try parsing the constant-expression tentatively,
8301 because the argument could really be a type-id. */
8303 cp_parser_parse_tentatively (parser
);
8304 argument
= cp_parser_constant_expression (parser
,
8305 /*allow_non_constant_p=*/false,
8306 /*non_constant_p=*/NULL
);
8307 argument
= cp_parser_fold_non_dependent_expr (argument
);
8310 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8311 cp_parser_error (parser
, "expected template-argument");
8312 if (cp_parser_parse_definitely (parser
))
8314 /* We did our best to parse the argument as a non type-id, but that
8315 was the only alternative that matched (albeit with a '>' after
8316 it). We can assume it's just a typo from the user, and a
8317 diagnostic will then be issued. */
8318 return cp_parser_type_id (parser
);
8321 /* Parse an explicit-instantiation.
8323 explicit-instantiation:
8324 template declaration
8326 Although the standard says `declaration', what it really means is:
8328 explicit-instantiation:
8329 template decl-specifier-seq [opt] declarator [opt] ;
8331 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8332 supposed to be allowed. A defect report has been filed about this
8337 explicit-instantiation:
8338 storage-class-specifier template
8339 decl-specifier-seq [opt] declarator [opt] ;
8340 function-specifier template
8341 decl-specifier-seq [opt] declarator [opt] ; */
8344 cp_parser_explicit_instantiation (cp_parser
* parser
)
8346 int declares_class_or_enum
;
8347 tree decl_specifiers
;
8349 tree extension_specifier
= NULL_TREE
;
8351 /* Look for an (optional) storage-class-specifier or
8352 function-specifier. */
8353 if (cp_parser_allow_gnu_extensions_p (parser
))
8356 = cp_parser_storage_class_specifier_opt (parser
);
8357 if (!extension_specifier
)
8358 extension_specifier
= cp_parser_function_specifier_opt (parser
);
8361 /* Look for the `template' keyword. */
8362 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8363 /* Let the front end know that we are processing an explicit
8365 begin_explicit_instantiation ();
8366 /* [temp.explicit] says that we are supposed to ignore access
8367 control while processing explicit instantiation directives. */
8368 push_deferring_access_checks (dk_no_check
);
8369 /* Parse a decl-specifier-seq. */
8371 = cp_parser_decl_specifier_seq (parser
,
8372 CP_PARSER_FLAGS_OPTIONAL
,
8374 &declares_class_or_enum
);
8375 /* If there was exactly one decl-specifier, and it declared a class,
8376 and there's no declarator, then we have an explicit type
8378 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8382 type
= check_tag_decl (decl_specifiers
);
8383 /* Turn access control back on for names used during
8384 template instantiation. */
8385 pop_deferring_access_checks ();
8387 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8394 /* Parse the declarator. */
8396 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8397 /*ctor_dtor_or_conv_p=*/NULL
,
8398 /*parenthesized_p=*/NULL
);
8399 cp_parser_check_for_definition_in_return_type (declarator
,
8400 declares_class_or_enum
);
8401 if (declarator
!= error_mark_node
)
8403 decl
= grokdeclarator (declarator
, decl_specifiers
,
8405 /* Turn access control back on for names used during
8406 template instantiation. */
8407 pop_deferring_access_checks ();
8408 /* Do the explicit instantiation. */
8409 do_decl_instantiation (decl
, extension_specifier
);
8413 pop_deferring_access_checks ();
8414 /* Skip the body of the explicit instantiation. */
8415 cp_parser_skip_to_end_of_statement (parser
);
8418 /* We're done with the instantiation. */
8419 end_explicit_instantiation ();
8421 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8424 /* Parse an explicit-specialization.
8426 explicit-specialization:
8427 template < > declaration
8429 Although the standard says `declaration', what it really means is:
8431 explicit-specialization:
8432 template <> decl-specifier [opt] init-declarator [opt] ;
8433 template <> function-definition
8434 template <> explicit-specialization
8435 template <> template-declaration */
8438 cp_parser_explicit_specialization (cp_parser
* parser
)
8440 /* Look for the `template' keyword. */
8441 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8442 /* Look for the `<'. */
8443 cp_parser_require (parser
, CPP_LESS
, "`<'");
8444 /* Look for the `>'. */
8445 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8446 /* We have processed another parameter list. */
8447 ++parser
->num_template_parameter_lists
;
8448 /* Let the front end know that we are beginning a specialization. */
8449 begin_specialization ();
8451 /* If the next keyword is `template', we need to figure out whether
8452 or not we're looking a template-declaration. */
8453 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8455 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8456 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8457 cp_parser_template_declaration_after_export (parser
,
8458 /*member_p=*/false);
8460 cp_parser_explicit_specialization (parser
);
8463 /* Parse the dependent declaration. */
8464 cp_parser_single_declaration (parser
,
8468 /* We're done with the specialization. */
8469 end_specialization ();
8470 /* We're done with this parameter list. */
8471 --parser
->num_template_parameter_lists
;
8474 /* Parse a type-specifier.
8477 simple-type-specifier
8480 elaborated-type-specifier
8488 Returns a representation of the type-specifier. If the
8489 type-specifier is a keyword (like `int' or `const', or
8490 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8491 For a class-specifier, enum-specifier, or elaborated-type-specifier
8492 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8494 If IS_FRIEND is TRUE then this type-specifier is being declared a
8495 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8496 appearing in a decl-specifier-seq.
8498 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8499 class-specifier, enum-specifier, or elaborated-type-specifier, then
8500 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8501 if a type is declared; 2 if it is defined. Otherwise, it is set to
8504 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8505 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8509 cp_parser_type_specifier (cp_parser
* parser
,
8510 cp_parser_flags flags
,
8512 bool is_declaration
,
8513 int* declares_class_or_enum
,
8514 bool* is_cv_qualifier
)
8516 tree type_spec
= NULL_TREE
;
8520 /* Assume this type-specifier does not declare a new type. */
8521 if (declares_class_or_enum
)
8522 *declares_class_or_enum
= 0;
8523 /* And that it does not specify a cv-qualifier. */
8524 if (is_cv_qualifier
)
8525 *is_cv_qualifier
= false;
8526 /* Peek at the next token. */
8527 token
= cp_lexer_peek_token (parser
->lexer
);
8529 /* If we're looking at a keyword, we can use that to guide the
8530 production we choose. */
8531 keyword
= token
->keyword
;
8534 /* Any of these indicate either a class-specifier, or an
8535 elaborated-type-specifier. */
8540 /* Parse tentatively so that we can back up if we don't find a
8541 class-specifier or enum-specifier. */
8542 cp_parser_parse_tentatively (parser
);
8543 /* Look for the class-specifier or enum-specifier. */
8544 if (keyword
== RID_ENUM
)
8545 type_spec
= cp_parser_enum_specifier (parser
);
8547 type_spec
= cp_parser_class_specifier (parser
);
8549 /* If that worked, we're done. */
8550 if (cp_parser_parse_definitely (parser
))
8552 if (declares_class_or_enum
)
8553 *declares_class_or_enum
= 2;
8560 /* Look for an elaborated-type-specifier. */
8561 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8564 /* We're declaring a class or enum -- unless we're using
8566 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8567 *declares_class_or_enum
= 1;
8573 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8574 /* Even though we call a routine that looks for an optional
8575 qualifier, we know that there should be one. */
8576 my_friendly_assert (type_spec
!= NULL
, 20000328);
8577 /* This type-specifier was a cv-qualified. */
8578 if (is_cv_qualifier
)
8579 *is_cv_qualifier
= true;
8584 /* The `__complex__' keyword is a GNU extension. */
8585 return cp_lexer_consume_token (parser
->lexer
)->value
;
8591 /* If we do not already have a type-specifier, assume we are looking
8592 at a simple-type-specifier. */
8593 type_spec
= cp_parser_simple_type_specifier (parser
, flags
,
8594 /*identifier_p=*/true);
8596 /* If we didn't find a type-specifier, and a type-specifier was not
8597 optional in this context, issue an error message. */
8598 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8600 cp_parser_error (parser
, "expected type specifier");
8601 return error_mark_node
;
8607 /* Parse a simple-type-specifier.
8609 simple-type-specifier:
8610 :: [opt] nested-name-specifier [opt] type-name
8611 :: [opt] nested-name-specifier template template-id
8626 simple-type-specifier:
8627 __typeof__ unary-expression
8628 __typeof__ ( type-id )
8630 For the various keywords, the value returned is simply the
8631 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8632 For the first two productions, and if IDENTIFIER_P is false, the
8633 value returned is the indicated TYPE_DECL. */
8636 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
,
8639 tree type
= NULL_TREE
;
8642 /* Peek at the next token. */
8643 token
= cp_lexer_peek_token (parser
->lexer
);
8645 /* If we're looking at a keyword, things are easy. */
8646 switch (token
->keyword
)
8649 type
= char_type_node
;
8652 type
= wchar_type_node
;
8655 type
= boolean_type_node
;
8658 type
= short_integer_type_node
;
8661 type
= integer_type_node
;
8664 type
= long_integer_type_node
;
8667 type
= integer_type_node
;
8670 type
= unsigned_type_node
;
8673 type
= float_type_node
;
8676 type
= double_type_node
;
8679 type
= void_type_node
;
8686 /* Consume the `typeof' token. */
8687 cp_lexer_consume_token (parser
->lexer
);
8688 /* Parse the operand to `typeof'. */
8689 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
8690 /* If it is not already a TYPE, take its type. */
8691 if (!TYPE_P (operand
))
8692 operand
= finish_typeof (operand
);
8701 /* If the type-specifier was for a built-in type, we're done. */
8706 /* Consume the token. */
8707 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
8708 return identifier_p
? id
: TYPE_NAME (type
);
8711 /* The type-specifier must be a user-defined type. */
8712 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
8714 /* Don't gobble tokens or issue error messages if this is an
8715 optional type-specifier. */
8716 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
8717 cp_parser_parse_tentatively (parser
);
8719 /* Look for the optional `::' operator. */
8720 cp_parser_global_scope_opt (parser
,
8721 /*current_scope_valid_p=*/false);
8722 /* Look for the nested-name specifier. */
8723 cp_parser_nested_name_specifier_opt (parser
,
8724 /*typename_keyword_p=*/false,
8725 /*check_dependency_p=*/true,
8727 /*is_declaration=*/false);
8728 /* If we have seen a nested-name-specifier, and the next token
8729 is `template', then we are using the template-id production. */
8731 && cp_parser_optional_template_keyword (parser
))
8733 /* Look for the template-id. */
8734 type
= cp_parser_template_id (parser
,
8735 /*template_keyword_p=*/true,
8736 /*check_dependency_p=*/true,
8737 /*is_declaration=*/false);
8738 /* If the template-id did not name a type, we are out of
8740 if (TREE_CODE (type
) != TYPE_DECL
)
8742 cp_parser_error (parser
, "expected template-id for type");
8746 /* Otherwise, look for a type-name. */
8748 type
= cp_parser_type_name (parser
);
8749 /* If it didn't work out, we don't have a TYPE. */
8750 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
8751 && !cp_parser_parse_definitely (parser
))
8755 /* If we didn't get a type-name, issue an error message. */
8756 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8758 cp_parser_error (parser
, "expected type-name");
8759 return error_mark_node
;
8762 /* There is no valid C++ program where a non-template type is
8763 followed by a "<". That usually indicates that the user thought
8764 that the type was a template. */
8765 if (type
&& type
!= error_mark_node
)
8766 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
8771 /* Parse a type-name.
8784 Returns a TYPE_DECL for the the type. */
8787 cp_parser_type_name (cp_parser
* parser
)
8792 /* We can't know yet whether it is a class-name or not. */
8793 cp_parser_parse_tentatively (parser
);
8794 /* Try a class-name. */
8795 type_decl
= cp_parser_class_name (parser
,
8796 /*typename_keyword_p=*/false,
8797 /*template_keyword_p=*/false,
8799 /*check_dependency_p=*/true,
8800 /*class_head_p=*/false,
8801 /*is_declaration=*/false);
8802 /* If it's not a class-name, keep looking. */
8803 if (!cp_parser_parse_definitely (parser
))
8805 /* It must be a typedef-name or an enum-name. */
8806 identifier
= cp_parser_identifier (parser
);
8807 if (identifier
== error_mark_node
)
8808 return error_mark_node
;
8810 /* Look up the type-name. */
8811 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
8812 /* Issue an error if we did not find a type-name. */
8813 if (TREE_CODE (type_decl
) != TYPE_DECL
)
8815 if (!cp_parser_simulate_error (parser
))
8816 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
8818 type_decl
= error_mark_node
;
8820 /* Remember that the name was used in the definition of the
8821 current class so that we can check later to see if the
8822 meaning would have been different after the class was
8823 entirely defined. */
8824 else if (type_decl
!= error_mark_node
8826 maybe_note_name_used_in_class (identifier
, type_decl
);
8833 /* Parse an elaborated-type-specifier. Note that the grammar given
8834 here incorporates the resolution to DR68.
8836 elaborated-type-specifier:
8837 class-key :: [opt] nested-name-specifier [opt] identifier
8838 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8839 enum :: [opt] nested-name-specifier [opt] identifier
8840 typename :: [opt] nested-name-specifier identifier
8841 typename :: [opt] nested-name-specifier template [opt]
8846 elaborated-type-specifier:
8847 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8848 class-key attributes :: [opt] nested-name-specifier [opt]
8849 template [opt] template-id
8850 enum attributes :: [opt] nested-name-specifier [opt] identifier
8852 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8853 declared `friend'. If IS_DECLARATION is TRUE, then this
8854 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8855 something is being declared.
8857 Returns the TYPE specified. */
8860 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
8862 bool is_declaration
)
8864 enum tag_types tag_type
;
8866 tree type
= NULL_TREE
;
8867 tree attributes
= NULL_TREE
;
8869 /* See if we're looking at the `enum' keyword. */
8870 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
8872 /* Consume the `enum' token. */
8873 cp_lexer_consume_token (parser
->lexer
);
8874 /* Remember that it's an enumeration type. */
8875 tag_type
= enum_type
;
8876 /* Parse the attributes. */
8877 attributes
= cp_parser_attributes_opt (parser
);
8879 /* Or, it might be `typename'. */
8880 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8883 /* Consume the `typename' token. */
8884 cp_lexer_consume_token (parser
->lexer
);
8885 /* Remember that it's a `typename' type. */
8886 tag_type
= typename_type
;
8887 /* The `typename' keyword is only allowed in templates. */
8888 if (!processing_template_decl
)
8889 pedwarn ("using `typename' outside of template");
8891 /* Otherwise it must be a class-key. */
8894 tag_type
= cp_parser_class_key (parser
);
8895 if (tag_type
== none_type
)
8896 return error_mark_node
;
8897 /* Parse the attributes. */
8898 attributes
= cp_parser_attributes_opt (parser
);
8901 /* Look for the `::' operator. */
8902 cp_parser_global_scope_opt (parser
,
8903 /*current_scope_valid_p=*/false);
8904 /* Look for the nested-name-specifier. */
8905 if (tag_type
== typename_type
)
8907 if (cp_parser_nested_name_specifier (parser
,
8908 /*typename_keyword_p=*/true,
8909 /*check_dependency_p=*/true,
8913 return error_mark_node
;
8916 /* Even though `typename' is not present, the proposed resolution
8917 to Core Issue 180 says that in `class A<T>::B', `B' should be
8918 considered a type-name, even if `A<T>' is dependent. */
8919 cp_parser_nested_name_specifier_opt (parser
,
8920 /*typename_keyword_p=*/true,
8921 /*check_dependency_p=*/true,
8924 /* For everything but enumeration types, consider a template-id. */
8925 if (tag_type
!= enum_type
)
8927 bool template_p
= false;
8930 /* Allow the `template' keyword. */
8931 template_p
= cp_parser_optional_template_keyword (parser
);
8932 /* If we didn't see `template', we don't know if there's a
8933 template-id or not. */
8935 cp_parser_parse_tentatively (parser
);
8936 /* Parse the template-id. */
8937 decl
= cp_parser_template_id (parser
, template_p
,
8938 /*check_dependency_p=*/true,
8940 /* If we didn't find a template-id, look for an ordinary
8942 if (!template_p
&& !cp_parser_parse_definitely (parser
))
8944 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8945 in effect, then we must assume that, upon instantiation, the
8946 template will correspond to a class. */
8947 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
8948 && tag_type
== typename_type
)
8949 type
= make_typename_type (parser
->scope
, decl
,
8952 type
= TREE_TYPE (decl
);
8955 /* For an enumeration type, consider only a plain identifier. */
8958 identifier
= cp_parser_identifier (parser
);
8960 if (identifier
== error_mark_node
)
8962 parser
->scope
= NULL_TREE
;
8963 return error_mark_node
;
8966 /* For a `typename', we needn't call xref_tag. */
8967 if (tag_type
== typename_type
)
8968 return make_typename_type (parser
->scope
, identifier
,
8970 /* Look up a qualified name in the usual way. */
8975 /* In an elaborated-type-specifier, names are assumed to name
8976 types, so we set IS_TYPE to TRUE when calling
8977 cp_parser_lookup_name. */
8978 decl
= cp_parser_lookup_name (parser
, identifier
,
8980 /*is_template=*/false,
8981 /*is_namespace=*/false,
8982 /*check_dependency=*/true);
8984 /* If we are parsing friend declaration, DECL may be a
8985 TEMPLATE_DECL tree node here. However, we need to check
8986 whether this TEMPLATE_DECL results in valid code. Consider
8987 the following example:
8990 template <class T> class C {};
8993 template <class T> friend class N::C; // #1, valid code
8995 template <class T> class Y {
8996 friend class N::C; // #2, invalid code
8999 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9000 name lookup of `N::C'. We see that friend declaration must
9001 be template for the code to be valid. Note that
9002 processing_template_decl does not work here since it is
9003 always 1 for the above two cases. */
9005 decl
= (cp_parser_maybe_treat_template_as_class
9006 (decl
, /*tag_name_p=*/is_friend
9007 && parser
->num_template_parameter_lists
));
9009 if (TREE_CODE (decl
) != TYPE_DECL
)
9011 error ("expected type-name");
9012 return error_mark_node
;
9015 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9016 check_elaborated_type_specifier
9018 (parser
->num_template_parameter_lists
9019 || DECL_SELF_REFERENCE_P (decl
)));
9021 type
= TREE_TYPE (decl
);
9025 /* An elaborated-type-specifier sometimes introduces a new type and
9026 sometimes names an existing type. Normally, the rule is that it
9027 introduces a new type only if there is not an existing type of
9028 the same name already in scope. For example, given:
9031 void f() { struct S s; }
9033 the `struct S' in the body of `f' is the same `struct S' as in
9034 the global scope; the existing definition is used. However, if
9035 there were no global declaration, this would introduce a new
9036 local class named `S'.
9038 An exception to this rule applies to the following code:
9040 namespace N { struct S; }
9042 Here, the elaborated-type-specifier names a new type
9043 unconditionally; even if there is already an `S' in the
9044 containing scope this declaration names a new type.
9045 This exception only applies if the elaborated-type-specifier
9046 forms the complete declaration:
9050 A declaration consisting solely of `class-key identifier ;' is
9051 either a redeclaration of the name in the current scope or a
9052 forward declaration of the identifier as a class name. It
9053 introduces the name into the current scope.
9055 We are in this situation precisely when the next token is a `;'.
9057 An exception to the exception is that a `friend' declaration does
9058 *not* name a new type; i.e., given:
9060 struct S { friend struct T; };
9062 `T' is not a new type in the scope of `S'.
9064 Also, `new struct S' or `sizeof (struct S)' never results in the
9065 definition of a new type; a new type can only be declared in a
9066 declaration context. */
9068 type
= xref_tag (tag_type
, identifier
,
9072 || cp_lexer_next_token_is_not (parser
->lexer
,
9074 parser
->num_template_parameter_lists
);
9077 if (tag_type
!= enum_type
)
9078 cp_parser_check_class_key (tag_type
, type
);
9080 /* A "<" cannot follow an elaborated type specifier. If that
9081 happens, the user was probably trying to form a template-id. */
9082 cp_parser_check_for_invalid_template_id (parser
, type
);
9087 /* Parse an enum-specifier.
9090 enum identifier [opt] { enumerator-list [opt] }
9092 Returns an ENUM_TYPE representing the enumeration. */
9095 cp_parser_enum_specifier (cp_parser
* parser
)
9098 tree identifier
= NULL_TREE
;
9101 /* Look for the `enum' keyword. */
9102 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
9103 return error_mark_node
;
9104 /* Peek at the next token. */
9105 token
= cp_lexer_peek_token (parser
->lexer
);
9107 /* See if it is an identifier. */
9108 if (token
->type
== CPP_NAME
)
9109 identifier
= cp_parser_identifier (parser
);
9111 /* Look for the `{'. */
9112 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
9113 return error_mark_node
;
9115 /* At this point, we're going ahead with the enum-specifier, even
9116 if some other problem occurs. */
9117 cp_parser_commit_to_tentative_parse (parser
);
9119 /* Issue an error message if type-definitions are forbidden here. */
9120 cp_parser_check_type_definition (parser
);
9122 /* Create the new type. */
9123 type
= start_enum (identifier
? identifier
: make_anon_name ());
9125 /* Peek at the next token. */
9126 token
= cp_lexer_peek_token (parser
->lexer
);
9127 /* If it's not a `}', then there are some enumerators. */
9128 if (token
->type
!= CPP_CLOSE_BRACE
)
9129 cp_parser_enumerator_list (parser
, type
);
9130 /* Look for the `}'. */
9131 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9133 /* Finish up the enumeration. */
9139 /* Parse an enumerator-list. The enumerators all have the indicated
9143 enumerator-definition
9144 enumerator-list , enumerator-definition */
9147 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9153 /* Parse an enumerator-definition. */
9154 cp_parser_enumerator_definition (parser
, type
);
9155 /* Peek at the next token. */
9156 token
= cp_lexer_peek_token (parser
->lexer
);
9157 /* If it's not a `,', then we've reached the end of the
9159 if (token
->type
!= CPP_COMMA
)
9161 /* Otherwise, consume the `,' and keep going. */
9162 cp_lexer_consume_token (parser
->lexer
);
9163 /* If the next token is a `}', there is a trailing comma. */
9164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9166 if (pedantic
&& !in_system_header
)
9167 pedwarn ("comma at end of enumerator list");
9173 /* Parse an enumerator-definition. The enumerator has the indicated
9176 enumerator-definition:
9178 enumerator = constant-expression
9184 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9190 /* Look for the identifier. */
9191 identifier
= cp_parser_identifier (parser
);
9192 if (identifier
== error_mark_node
)
9195 /* Peek at the next token. */
9196 token
= cp_lexer_peek_token (parser
->lexer
);
9197 /* If it's an `=', then there's an explicit value. */
9198 if (token
->type
== CPP_EQ
)
9200 /* Consume the `=' token. */
9201 cp_lexer_consume_token (parser
->lexer
);
9202 /* Parse the value. */
9203 value
= cp_parser_constant_expression (parser
,
9204 /*allow_non_constant_p=*/false,
9210 /* Create the enumerator. */
9211 build_enumerator (identifier
, value
, type
);
9214 /* Parse a namespace-name.
9217 original-namespace-name
9220 Returns the NAMESPACE_DECL for the namespace. */
9223 cp_parser_namespace_name (cp_parser
* parser
)
9226 tree namespace_decl
;
9228 /* Get the name of the namespace. */
9229 identifier
= cp_parser_identifier (parser
);
9230 if (identifier
== error_mark_node
)
9231 return error_mark_node
;
9233 /* Look up the identifier in the currently active scope. Look only
9234 for namespaces, due to:
9238 When looking up a namespace-name in a using-directive or alias
9239 definition, only namespace names are considered.
9245 During the lookup of a name preceding the :: scope resolution
9246 operator, object, function, and enumerator names are ignored.
9248 (Note that cp_parser_class_or_namespace_name only calls this
9249 function if the token after the name is the scope resolution
9251 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
9253 /*is_template=*/false,
9254 /*is_namespace=*/true,
9255 /*check_dependency=*/true);
9256 /* If it's not a namespace, issue an error. */
9257 if (namespace_decl
== error_mark_node
9258 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
9260 cp_parser_error (parser
, "expected namespace-name");
9261 namespace_decl
= error_mark_node
;
9264 return namespace_decl
;
9267 /* Parse a namespace-definition.
9269 namespace-definition:
9270 named-namespace-definition
9271 unnamed-namespace-definition
9273 named-namespace-definition:
9274 original-namespace-definition
9275 extension-namespace-definition
9277 original-namespace-definition:
9278 namespace identifier { namespace-body }
9280 extension-namespace-definition:
9281 namespace original-namespace-name { namespace-body }
9283 unnamed-namespace-definition:
9284 namespace { namespace-body } */
9287 cp_parser_namespace_definition (cp_parser
* parser
)
9291 /* Look for the `namespace' keyword. */
9292 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9294 /* Get the name of the namespace. We do not attempt to distinguish
9295 between an original-namespace-definition and an
9296 extension-namespace-definition at this point. The semantic
9297 analysis routines are responsible for that. */
9298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9299 identifier
= cp_parser_identifier (parser
);
9301 identifier
= NULL_TREE
;
9303 /* Look for the `{' to start the namespace. */
9304 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
9305 /* Start the namespace. */
9306 push_namespace (identifier
);
9307 /* Parse the body of the namespace. */
9308 cp_parser_namespace_body (parser
);
9309 /* Finish the namespace. */
9311 /* Look for the final `}'. */
9312 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9315 /* Parse a namespace-body.
9318 declaration-seq [opt] */
9321 cp_parser_namespace_body (cp_parser
* parser
)
9323 cp_parser_declaration_seq_opt (parser
);
9326 /* Parse a namespace-alias-definition.
9328 namespace-alias-definition:
9329 namespace identifier = qualified-namespace-specifier ; */
9332 cp_parser_namespace_alias_definition (cp_parser
* parser
)
9335 tree namespace_specifier
;
9337 /* Look for the `namespace' keyword. */
9338 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9339 /* Look for the identifier. */
9340 identifier
= cp_parser_identifier (parser
);
9341 if (identifier
== error_mark_node
)
9343 /* Look for the `=' token. */
9344 cp_parser_require (parser
, CPP_EQ
, "`='");
9345 /* Look for the qualified-namespace-specifier. */
9347 = cp_parser_qualified_namespace_specifier (parser
);
9348 /* Look for the `;' token. */
9349 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9351 /* Register the alias in the symbol table. */
9352 do_namespace_alias (identifier
, namespace_specifier
);
9355 /* Parse a qualified-namespace-specifier.
9357 qualified-namespace-specifier:
9358 :: [opt] nested-name-specifier [opt] namespace-name
9360 Returns a NAMESPACE_DECL corresponding to the specified
9364 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
9366 /* Look for the optional `::'. */
9367 cp_parser_global_scope_opt (parser
,
9368 /*current_scope_valid_p=*/false);
9370 /* Look for the optional nested-name-specifier. */
9371 cp_parser_nested_name_specifier_opt (parser
,
9372 /*typename_keyword_p=*/false,
9373 /*check_dependency_p=*/true,
9375 /*is_declaration=*/true);
9377 return cp_parser_namespace_name (parser
);
9380 /* Parse a using-declaration.
9383 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9384 using :: unqualified-id ; */
9387 cp_parser_using_declaration (cp_parser
* parser
)
9390 bool typename_p
= false;
9391 bool global_scope_p
;
9396 /* Look for the `using' keyword. */
9397 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9399 /* Peek at the next token. */
9400 token
= cp_lexer_peek_token (parser
->lexer
);
9401 /* See if it's `typename'. */
9402 if (token
->keyword
== RID_TYPENAME
)
9404 /* Remember that we've seen it. */
9406 /* Consume the `typename' token. */
9407 cp_lexer_consume_token (parser
->lexer
);
9410 /* Look for the optional global scope qualification. */
9412 = (cp_parser_global_scope_opt (parser
,
9413 /*current_scope_valid_p=*/false)
9416 /* If we saw `typename', or didn't see `::', then there must be a
9417 nested-name-specifier present. */
9418 if (typename_p
|| !global_scope_p
)
9419 cp_parser_nested_name_specifier (parser
, typename_p
,
9420 /*check_dependency_p=*/true,
9422 /*is_declaration=*/true);
9423 /* Otherwise, we could be in either of the two productions. In that
9424 case, treat the nested-name-specifier as optional. */
9426 cp_parser_nested_name_specifier_opt (parser
,
9427 /*typename_keyword_p=*/false,
9428 /*check_dependency_p=*/true,
9430 /*is_declaration=*/true);
9432 /* Parse the unqualified-id. */
9433 identifier
= cp_parser_unqualified_id (parser
,
9434 /*template_keyword_p=*/false,
9435 /*check_dependency_p=*/true,
9436 /*declarator_p=*/true);
9438 /* The function we call to handle a using-declaration is different
9439 depending on what scope we are in. */
9440 if (identifier
== error_mark_node
)
9442 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
9443 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
9444 /* [namespace.udecl]
9446 A using declaration shall not name a template-id. */
9447 error ("a template-id may not appear in a using-declaration");
9450 scope
= current_scope ();
9451 if (scope
&& TYPE_P (scope
))
9453 /* Create the USING_DECL. */
9454 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9457 /* Add it to the list of members in this class. */
9458 finish_member_declaration (decl
);
9462 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9463 if (decl
== error_mark_node
)
9464 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
9466 do_local_using_decl (decl
);
9468 do_toplevel_using_decl (decl
);
9472 /* Look for the final `;'. */
9473 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9476 /* Parse a using-directive.
9479 using namespace :: [opt] nested-name-specifier [opt]
9483 cp_parser_using_directive (cp_parser
* parser
)
9485 tree namespace_decl
;
9488 /* Look for the `using' keyword. */
9489 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9490 /* And the `namespace' keyword. */
9491 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9492 /* Look for the optional `::' operator. */
9493 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9494 /* And the optional nested-name-specifier. */
9495 cp_parser_nested_name_specifier_opt (parser
,
9496 /*typename_keyword_p=*/false,
9497 /*check_dependency_p=*/true,
9499 /*is_declaration=*/true);
9500 /* Get the namespace being used. */
9501 namespace_decl
= cp_parser_namespace_name (parser
);
9502 /* And any specified attributes. */
9503 attribs
= cp_parser_attributes_opt (parser
);
9504 /* Update the symbol table. */
9505 parse_using_directive (namespace_decl
, attribs
);
9506 /* Look for the final `;'. */
9507 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9510 /* Parse an asm-definition.
9513 asm ( string-literal ) ;
9518 asm volatile [opt] ( string-literal ) ;
9519 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9520 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9521 : asm-operand-list [opt] ) ;
9522 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9523 : asm-operand-list [opt]
9524 : asm-operand-list [opt] ) ; */
9527 cp_parser_asm_definition (cp_parser
* parser
)
9531 tree outputs
= NULL_TREE
;
9532 tree inputs
= NULL_TREE
;
9533 tree clobbers
= NULL_TREE
;
9535 bool volatile_p
= false;
9536 bool extended_p
= false;
9538 /* Look for the `asm' keyword. */
9539 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9540 /* See if the next token is `volatile'. */
9541 if (cp_parser_allow_gnu_extensions_p (parser
)
9542 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9544 /* Remember that we saw the `volatile' keyword. */
9546 /* Consume the token. */
9547 cp_lexer_consume_token (parser
->lexer
);
9549 /* Look for the opening `('. */
9550 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9551 /* Look for the string. */
9552 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9555 string
= token
->value
;
9556 /* If we're allowing GNU extensions, check for the extended assembly
9557 syntax. Unfortunately, the `:' tokens need not be separated by
9558 a space in C, and so, for compatibility, we tolerate that here
9559 too. Doing that means that we have to treat the `::' operator as
9561 if (cp_parser_allow_gnu_extensions_p (parser
)
9562 && at_function_scope_p ()
9563 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9564 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9566 bool inputs_p
= false;
9567 bool clobbers_p
= false;
9569 /* The extended syntax was used. */
9572 /* Look for outputs. */
9573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9575 /* Consume the `:'. */
9576 cp_lexer_consume_token (parser
->lexer
);
9577 /* Parse the output-operands. */
9578 if (cp_lexer_next_token_is_not (parser
->lexer
,
9580 && cp_lexer_next_token_is_not (parser
->lexer
,
9582 && cp_lexer_next_token_is_not (parser
->lexer
,
9584 outputs
= cp_parser_asm_operand_list (parser
);
9586 /* If the next token is `::', there are no outputs, and the
9587 next token is the beginning of the inputs. */
9588 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9590 /* Consume the `::' token. */
9591 cp_lexer_consume_token (parser
->lexer
);
9592 /* The inputs are coming next. */
9596 /* Look for inputs. */
9598 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9601 /* Consume the `:'. */
9602 cp_lexer_consume_token (parser
->lexer
);
9603 /* Parse the output-operands. */
9604 if (cp_lexer_next_token_is_not (parser
->lexer
,
9606 && cp_lexer_next_token_is_not (parser
->lexer
,
9608 && cp_lexer_next_token_is_not (parser
->lexer
,
9610 inputs
= cp_parser_asm_operand_list (parser
);
9612 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9613 /* The clobbers are coming next. */
9616 /* Look for clobbers. */
9618 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9621 /* Consume the `:'. */
9622 cp_lexer_consume_token (parser
->lexer
);
9623 /* Parse the clobbers. */
9624 if (cp_lexer_next_token_is_not (parser
->lexer
,
9626 clobbers
= cp_parser_asm_clobber_list (parser
);
9629 /* Look for the closing `)'. */
9630 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9631 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9632 /*consume_paren=*/true);
9633 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9635 /* Create the ASM_STMT. */
9636 if (at_function_scope_p ())
9639 finish_asm_stmt (volatile_p
9640 ? ridpointers
[(int) RID_VOLATILE
] : NULL_TREE
,
9641 string
, outputs
, inputs
, clobbers
);
9642 /* If the extended syntax was not used, mark the ASM_STMT. */
9644 ASM_INPUT_P (asm_stmt
) = 1;
9647 assemble_asm (string
);
9650 /* Declarators [gram.dcl.decl] */
9652 /* Parse an init-declarator.
9655 declarator initializer [opt]
9660 declarator asm-specification [opt] attributes [opt] initializer [opt]
9662 function-definition:
9663 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9665 decl-specifier-seq [opt] declarator function-try-block
9669 function-definition:
9670 __extension__ function-definition
9672 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9673 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9674 then this declarator appears in a class scope. The new DECL created
9675 by this declarator is returned.
9677 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9678 for a function-definition here as well. If the declarator is a
9679 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9680 be TRUE upon return. By that point, the function-definition will
9681 have been completely parsed.
9683 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9687 cp_parser_init_declarator (cp_parser
* parser
,
9688 tree decl_specifiers
,
9689 tree prefix_attributes
,
9690 bool function_definition_allowed_p
,
9692 int declares_class_or_enum
,
9693 bool* function_definition_p
)
9698 tree asm_specification
;
9700 tree decl
= NULL_TREE
;
9702 bool is_initialized
;
9703 bool is_parenthesized_init
;
9704 bool is_non_constant_init
;
9705 int ctor_dtor_or_conv_p
;
9708 /* Assume that this is not the declarator for a function
9710 if (function_definition_p
)
9711 *function_definition_p
= false;
9713 /* Defer access checks while parsing the declarator; we cannot know
9714 what names are accessible until we know what is being
9716 resume_deferring_access_checks ();
9718 /* Parse the declarator. */
9720 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9721 &ctor_dtor_or_conv_p
,
9722 /*parenthesized_p=*/NULL
);
9723 /* Gather up the deferred checks. */
9724 stop_deferring_access_checks ();
9726 /* If the DECLARATOR was erroneous, there's no need to go
9728 if (declarator
== error_mark_node
)
9729 return error_mark_node
;
9731 cp_parser_check_for_definition_in_return_type (declarator
,
9732 declares_class_or_enum
);
9734 /* Figure out what scope the entity declared by the DECLARATOR is
9735 located in. `grokdeclarator' sometimes changes the scope, so
9736 we compute it now. */
9737 scope
= get_scope_of_declarator (declarator
);
9739 /* If we're allowing GNU extensions, look for an asm-specification
9741 if (cp_parser_allow_gnu_extensions_p (parser
))
9743 /* Look for an asm-specification. */
9744 asm_specification
= cp_parser_asm_specification_opt (parser
);
9745 /* And attributes. */
9746 attributes
= cp_parser_attributes_opt (parser
);
9750 asm_specification
= NULL_TREE
;
9751 attributes
= NULL_TREE
;
9754 /* Peek at the next token. */
9755 token
= cp_lexer_peek_token (parser
->lexer
);
9756 /* Check to see if the token indicates the start of a
9757 function-definition. */
9758 if (cp_parser_token_starts_function_definition_p (token
))
9760 if (!function_definition_allowed_p
)
9762 /* If a function-definition should not appear here, issue an
9764 cp_parser_error (parser
,
9765 "a function-definition is not allowed here");
9766 return error_mark_node
;
9770 /* Neither attributes nor an asm-specification are allowed
9771 on a function-definition. */
9772 if (asm_specification
)
9773 error ("an asm-specification is not allowed on a function-definition");
9775 error ("attributes are not allowed on a function-definition");
9776 /* This is a function-definition. */
9777 *function_definition_p
= true;
9779 /* Parse the function definition. */
9781 decl
= cp_parser_save_member_function_body (parser
,
9787 = (cp_parser_function_definition_from_specifiers_and_declarator
9788 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
9796 Only in function declarations for constructors, destructors, and
9797 type conversions can the decl-specifier-seq be omitted.
9799 We explicitly postpone this check past the point where we handle
9800 function-definitions because we tolerate function-definitions
9801 that are missing their return types in some modes. */
9802 if (!decl_specifiers
&& ctor_dtor_or_conv_p
<= 0)
9804 cp_parser_error (parser
,
9805 "expected constructor, destructor, or type conversion");
9806 return error_mark_node
;
9809 /* An `=' or an `(' indicates an initializer. */
9810 is_initialized
= (token
->type
== CPP_EQ
9811 || token
->type
== CPP_OPEN_PAREN
);
9812 /* If the init-declarator isn't initialized and isn't followed by a
9813 `,' or `;', it's not a valid init-declarator. */
9815 && token
->type
!= CPP_COMMA
9816 && token
->type
!= CPP_SEMICOLON
)
9818 cp_parser_error (parser
, "expected init-declarator");
9819 return error_mark_node
;
9822 /* Because start_decl has side-effects, we should only call it if we
9823 know we're going ahead. By this point, we know that we cannot
9824 possibly be looking at any other construct. */
9825 cp_parser_commit_to_tentative_parse (parser
);
9827 /* If the decl specifiers were bad, issue an error now that we're
9828 sure this was intended to be a declarator. Then continue
9829 declaring the variable(s), as int, to try to cut down on further
9831 if (decl_specifiers
!= NULL
9832 && TREE_VALUE (decl_specifiers
) == error_mark_node
)
9834 cp_parser_error (parser
, "invalid type in declaration");
9835 TREE_VALUE (decl_specifiers
) = integer_type_node
;
9838 /* Check to see whether or not this declaration is a friend. */
9839 friend_p
= cp_parser_friend_p (decl_specifiers
);
9841 /* Check that the number of template-parameter-lists is OK. */
9842 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
9843 return error_mark_node
;
9845 /* Enter the newly declared entry in the symbol table. If we're
9846 processing a declaration in a class-specifier, we wait until
9847 after processing the initializer. */
9850 if (parser
->in_unbraced_linkage_specification_p
)
9852 decl_specifiers
= tree_cons (error_mark_node
,
9853 get_identifier ("extern"),
9855 have_extern_spec
= false;
9857 decl
= start_decl (declarator
, decl_specifiers
,
9858 is_initialized
, attributes
, prefix_attributes
);
9861 /* Enter the SCOPE. That way unqualified names appearing in the
9862 initializer will be looked up in SCOPE. */
9866 /* Perform deferred access control checks, now that we know in which
9867 SCOPE the declared entity resides. */
9868 if (!member_p
&& decl
)
9870 tree saved_current_function_decl
= NULL_TREE
;
9872 /* If the entity being declared is a function, pretend that we
9873 are in its scope. If it is a `friend', it may have access to
9874 things that would not otherwise be accessible. */
9875 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9877 saved_current_function_decl
= current_function_decl
;
9878 current_function_decl
= decl
;
9881 /* Perform the access control checks for the declarator and the
9882 the decl-specifiers. */
9883 perform_deferred_access_checks ();
9885 /* Restore the saved value. */
9886 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9887 current_function_decl
= saved_current_function_decl
;
9890 /* Parse the initializer. */
9892 initializer
= cp_parser_initializer (parser
,
9893 &is_parenthesized_init
,
9894 &is_non_constant_init
);
9897 initializer
= NULL_TREE
;
9898 is_parenthesized_init
= false;
9899 is_non_constant_init
= true;
9902 /* The old parser allows attributes to appear after a parenthesized
9903 initializer. Mark Mitchell proposed removing this functionality
9904 on the GCC mailing lists on 2002-08-13. This parser accepts the
9905 attributes -- but ignores them. */
9906 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
9907 if (cp_parser_attributes_opt (parser
))
9908 warning ("attributes after parenthesized initializer ignored");
9910 /* Leave the SCOPE, now that we have processed the initializer. It
9911 is important to do this before calling cp_finish_decl because it
9912 makes decisions about whether to create DECL_STMTs or not based
9913 on the current scope. */
9917 /* For an in-class declaration, use `grokfield' to create the
9921 decl
= grokfield (declarator
, decl_specifiers
,
9922 initializer
, /*asmspec=*/NULL_TREE
,
9923 /*attributes=*/NULL_TREE
);
9924 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
9925 cp_parser_save_default_args (parser
, decl
);
9928 /* Finish processing the declaration. But, skip friend
9930 if (!friend_p
&& decl
)
9931 cp_finish_decl (decl
,
9934 /* If the initializer is in parentheses, then this is
9935 a direct-initialization, which means that an
9936 `explicit' constructor is OK. Otherwise, an
9937 `explicit' constructor cannot be used. */
9938 ((is_parenthesized_init
|| !is_initialized
)
9939 ? 0 : LOOKUP_ONLYCONVERTING
));
9941 /* Remember whether or not variables were initialized by
9942 constant-expressions. */
9943 if (decl
&& TREE_CODE (decl
) == VAR_DECL
9944 && is_initialized
&& !is_non_constant_init
)
9945 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
9950 /* Parse a declarator.
9954 ptr-operator declarator
9956 abstract-declarator:
9957 ptr-operator abstract-declarator [opt]
9958 direct-abstract-declarator
9963 attributes [opt] direct-declarator
9964 attributes [opt] ptr-operator declarator
9966 abstract-declarator:
9967 attributes [opt] ptr-operator abstract-declarator [opt]
9968 attributes [opt] direct-abstract-declarator
9970 Returns a representation of the declarator. If the declarator has
9971 the form `* declarator', then an INDIRECT_REF is returned, whose
9972 only operand is the sub-declarator. Analogously, `& declarator' is
9973 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9974 used. The first operand is the TYPE for `X'. The second operand
9975 is an INDIRECT_REF whose operand is the sub-declarator.
9977 Otherwise, the representation is as for a direct-declarator.
9979 (It would be better to define a structure type to represent
9980 declarators, rather than abusing `tree' nodes to represent
9981 declarators. That would be much clearer and save some memory.
9982 There is no reason for declarators to be garbage-collected, for
9983 example; they are created during parser and no longer needed after
9984 `grokdeclarator' has been called.)
9986 For a ptr-operator that has the optional cv-qualifier-seq,
9987 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9990 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9991 detect constructor, destructor or conversion operators. It is set
9992 to -1 if the declarator is a name, and +1 if it is a
9993 function. Otherwise it is set to zero. Usually you just want to
9994 test for >0, but internally the negative value is used.
9996 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9997 a decl-specifier-seq unless it declares a constructor, destructor,
9998 or conversion. It might seem that we could check this condition in
9999 semantic analysis, rather than parsing, but that makes it difficult
10000 to handle something like `f()'. We want to notice that there are
10001 no decl-specifiers, and therefore realize that this is an
10002 expression, not a declaration.)
10004 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10005 the declarator is a direct-declarator of the form "(...)". */
10008 cp_parser_declarator (cp_parser
* parser
,
10009 cp_parser_declarator_kind dcl_kind
,
10010 int* ctor_dtor_or_conv_p
,
10011 bool* parenthesized_p
)
10015 enum tree_code code
;
10016 tree cv_qualifier_seq
;
10018 tree attributes
= NULL_TREE
;
10020 /* Assume this is not a constructor, destructor, or type-conversion
10022 if (ctor_dtor_or_conv_p
)
10023 *ctor_dtor_or_conv_p
= 0;
10025 if (cp_parser_allow_gnu_extensions_p (parser
))
10026 attributes
= cp_parser_attributes_opt (parser
);
10028 /* Peek at the next token. */
10029 token
= cp_lexer_peek_token (parser
->lexer
);
10031 /* Check for the ptr-operator production. */
10032 cp_parser_parse_tentatively (parser
);
10033 /* Parse the ptr-operator. */
10034 code
= cp_parser_ptr_operator (parser
,
10036 &cv_qualifier_seq
);
10037 /* If that worked, then we have a ptr-operator. */
10038 if (cp_parser_parse_definitely (parser
))
10040 /* If a ptr-operator was found, then this declarator was not
10042 if (parenthesized_p
)
10043 *parenthesized_p
= true;
10044 /* The dependent declarator is optional if we are parsing an
10045 abstract-declarator. */
10046 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10047 cp_parser_parse_tentatively (parser
);
10049 /* Parse the dependent declarator. */
10050 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10051 /*ctor_dtor_or_conv_p=*/NULL
,
10052 /*parenthesized_p=*/NULL
);
10054 /* If we are parsing an abstract-declarator, we must handle the
10055 case where the dependent declarator is absent. */
10056 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10057 && !cp_parser_parse_definitely (parser
))
10058 declarator
= NULL_TREE
;
10060 /* Build the representation of the ptr-operator. */
10061 if (code
== INDIRECT_REF
)
10062 declarator
= make_pointer_declarator (cv_qualifier_seq
,
10065 declarator
= make_reference_declarator (cv_qualifier_seq
,
10067 /* Handle the pointer-to-member case. */
10069 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
10071 /* Everything else is a direct-declarator. */
10074 if (parenthesized_p
)
10075 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10077 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10078 ctor_dtor_or_conv_p
);
10081 if (attributes
&& declarator
!= error_mark_node
)
10082 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
10087 /* Parse a direct-declarator or direct-abstract-declarator.
10091 direct-declarator ( parameter-declaration-clause )
10092 cv-qualifier-seq [opt]
10093 exception-specification [opt]
10094 direct-declarator [ constant-expression [opt] ]
10097 direct-abstract-declarator:
10098 direct-abstract-declarator [opt]
10099 ( parameter-declaration-clause )
10100 cv-qualifier-seq [opt]
10101 exception-specification [opt]
10102 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10103 ( abstract-declarator )
10105 Returns a representation of the declarator. DCL_KIND is
10106 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10107 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10108 we are parsing a direct-declarator. It is
10109 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10110 of ambiguity we prefer an abstract declarator, as per
10111 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10112 cp_parser_declarator.
10114 For the declarator-id production, the representation is as for an
10115 id-expression, except that a qualified name is represented as a
10116 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10117 see the documentation of the FUNCTION_DECLARATOR_* macros for
10118 information about how to find the various declarator components.
10119 An array-declarator is represented as an ARRAY_REF. The
10120 direct-declarator is the first operand; the constant-expression
10121 indicating the size of the array is the second operand. */
10124 cp_parser_direct_declarator (cp_parser
* parser
,
10125 cp_parser_declarator_kind dcl_kind
,
10126 int* ctor_dtor_or_conv_p
)
10129 tree declarator
= NULL_TREE
;
10130 tree scope
= NULL_TREE
;
10131 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10132 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10137 /* Peek at the next token. */
10138 token
= cp_lexer_peek_token (parser
->lexer
);
10139 if (token
->type
== CPP_OPEN_PAREN
)
10141 /* This is either a parameter-declaration-clause, or a
10142 parenthesized declarator. When we know we are parsing a
10143 named declarator, it must be a parenthesized declarator
10144 if FIRST is true. For instance, `(int)' is a
10145 parameter-declaration-clause, with an omitted
10146 direct-abstract-declarator. But `((*))', is a
10147 parenthesized abstract declarator. Finally, when T is a
10148 template parameter `(T)' is a
10149 parameter-declaration-clause, and not a parenthesized
10152 We first try and parse a parameter-declaration-clause,
10153 and then try a nested declarator (if FIRST is true).
10155 It is not an error for it not to be a
10156 parameter-declaration-clause, even when FIRST is
10162 The first is the declaration of a function while the
10163 second is a the definition of a variable, including its
10166 Having seen only the parenthesis, we cannot know which of
10167 these two alternatives should be selected. Even more
10168 complex are examples like:
10173 The former is a function-declaration; the latter is a
10174 variable initialization.
10176 Thus again, we try a parameter-declaration-clause, and if
10177 that fails, we back out and return. */
10179 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10182 unsigned saved_num_template_parameter_lists
;
10184 cp_parser_parse_tentatively (parser
);
10186 /* Consume the `('. */
10187 cp_lexer_consume_token (parser
->lexer
);
10190 /* If this is going to be an abstract declarator, we're
10191 in a declarator and we can't have default args. */
10192 parser
->default_arg_ok_p
= false;
10193 parser
->in_declarator_p
= true;
10196 /* Inside the function parameter list, surrounding
10197 template-parameter-lists do not apply. */
10198 saved_num_template_parameter_lists
10199 = parser
->num_template_parameter_lists
;
10200 parser
->num_template_parameter_lists
= 0;
10202 /* Parse the parameter-declaration-clause. */
10203 params
= cp_parser_parameter_declaration_clause (parser
);
10205 parser
->num_template_parameter_lists
10206 = saved_num_template_parameter_lists
;
10208 /* If all went well, parse the cv-qualifier-seq and the
10209 exception-specification. */
10210 if (cp_parser_parse_definitely (parser
))
10212 tree cv_qualifiers
;
10213 tree exception_specification
;
10215 if (ctor_dtor_or_conv_p
)
10216 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
10218 /* Consume the `)'. */
10219 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
10221 /* Parse the cv-qualifier-seq. */
10222 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
10223 /* And the exception-specification. */
10224 exception_specification
10225 = cp_parser_exception_specification_opt (parser
);
10227 /* Create the function-declarator. */
10228 declarator
= make_call_declarator (declarator
,
10231 exception_specification
);
10232 /* Any subsequent parameter lists are to do with
10233 return type, so are not those of the declared
10235 parser
->default_arg_ok_p
= false;
10237 /* Repeat the main loop. */
10242 /* If this is the first, we can try a parenthesized
10246 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10247 parser
->in_declarator_p
= saved_in_declarator_p
;
10249 /* Consume the `('. */
10250 cp_lexer_consume_token (parser
->lexer
);
10251 /* Parse the nested declarator. */
10253 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
10254 /*parenthesized_p=*/NULL
);
10256 /* Expect a `)'. */
10257 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10258 declarator
= error_mark_node
;
10259 if (declarator
== error_mark_node
)
10262 goto handle_declarator
;
10264 /* Otherwise, we must be done. */
10268 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10269 && token
->type
== CPP_OPEN_SQUARE
)
10271 /* Parse an array-declarator. */
10274 if (ctor_dtor_or_conv_p
)
10275 *ctor_dtor_or_conv_p
= 0;
10278 parser
->default_arg_ok_p
= false;
10279 parser
->in_declarator_p
= true;
10280 /* Consume the `['. */
10281 cp_lexer_consume_token (parser
->lexer
);
10282 /* Peek at the next token. */
10283 token
= cp_lexer_peek_token (parser
->lexer
);
10284 /* If the next token is `]', then there is no
10285 constant-expression. */
10286 if (token
->type
!= CPP_CLOSE_SQUARE
)
10288 bool non_constant_p
;
10291 = cp_parser_constant_expression (parser
,
10292 /*allow_non_constant=*/true,
10294 if (!non_constant_p
)
10295 bounds
= cp_parser_fold_non_dependent_expr (bounds
);
10298 bounds
= NULL_TREE
;
10299 /* Look for the closing `]'. */
10300 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
10302 declarator
= error_mark_node
;
10306 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
10308 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
10310 /* Parse a declarator-id */
10311 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10312 cp_parser_parse_tentatively (parser
);
10313 declarator
= cp_parser_declarator_id (parser
);
10314 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10316 if (!cp_parser_parse_definitely (parser
))
10317 declarator
= error_mark_node
;
10318 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
10320 cp_parser_error (parser
, "expected unqualified-id");
10321 declarator
= error_mark_node
;
10325 if (declarator
== error_mark_node
)
10328 if (TREE_CODE (declarator
) == SCOPE_REF
10329 && !current_scope ())
10331 tree scope
= TREE_OPERAND (declarator
, 0);
10333 /* In the declaration of a member of a template class
10334 outside of the class itself, the SCOPE will sometimes
10335 be a TYPENAME_TYPE. For example, given:
10337 template <typename T>
10338 int S<T>::R::i = 3;
10340 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10341 this context, we must resolve S<T>::R to an ordinary
10342 type, rather than a typename type.
10344 The reason we normally avoid resolving TYPENAME_TYPEs
10345 is that a specialization of `S' might render
10346 `S<T>::R' not a type. However, if `S' is
10347 specialized, then this `i' will not be used, so there
10348 is no harm in resolving the types here. */
10349 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
10353 /* Resolve the TYPENAME_TYPE. */
10354 type
= resolve_typename_type (scope
,
10355 /*only_current_p=*/false);
10356 /* If that failed, the declarator is invalid. */
10357 if (type
!= error_mark_node
)
10359 /* Build a new DECLARATOR. */
10360 declarator
= build_nt (SCOPE_REF
,
10362 TREE_OPERAND (declarator
, 1));
10366 /* Check to see whether the declarator-id names a constructor,
10367 destructor, or conversion. */
10368 if (declarator
&& ctor_dtor_or_conv_p
10369 && ((TREE_CODE (declarator
) == SCOPE_REF
10370 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
10371 || (TREE_CODE (declarator
) != SCOPE_REF
10372 && at_class_scope_p ())))
10374 tree unqualified_name
;
10377 /* Get the unqualified part of the name. */
10378 if (TREE_CODE (declarator
) == SCOPE_REF
)
10380 class_type
= TREE_OPERAND (declarator
, 0);
10381 unqualified_name
= TREE_OPERAND (declarator
, 1);
10385 class_type
= current_class_type
;
10386 unqualified_name
= declarator
;
10389 /* See if it names ctor, dtor or conv. */
10390 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
10391 || IDENTIFIER_TYPENAME_P (unqualified_name
)
10392 || constructor_name_p (unqualified_name
, class_type
))
10393 *ctor_dtor_or_conv_p
= -1;
10396 handle_declarator
:;
10397 scope
= get_scope_of_declarator (declarator
);
10399 /* Any names that appear after the declarator-id for a member
10400 are looked up in the containing scope. */
10401 push_scope (scope
);
10402 parser
->in_declarator_p
= true;
10403 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
10405 && (TREE_CODE (declarator
) == SCOPE_REF
10406 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
10407 /* Default args are only allowed on function
10409 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10411 parser
->default_arg_ok_p
= false;
10420 /* For an abstract declarator, we might wind up with nothing at this
10421 point. That's an error; the declarator is not optional. */
10423 cp_parser_error (parser
, "expected declarator");
10425 /* If we entered a scope, we must exit it now. */
10429 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10430 parser
->in_declarator_p
= saved_in_declarator_p
;
10435 /* Parse a ptr-operator.
10438 * cv-qualifier-seq [opt]
10440 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10445 & cv-qualifier-seq [opt]
10447 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10448 used. Returns ADDR_EXPR if a reference was used. In the
10449 case of a pointer-to-member, *TYPE is filled in with the
10450 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10451 with the cv-qualifier-seq, or NULL_TREE, if there are no
10452 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10454 static enum tree_code
10455 cp_parser_ptr_operator (cp_parser
* parser
,
10457 tree
* cv_qualifier_seq
)
10459 enum tree_code code
= ERROR_MARK
;
10462 /* Assume that it's not a pointer-to-member. */
10464 /* And that there are no cv-qualifiers. */
10465 *cv_qualifier_seq
= NULL_TREE
;
10467 /* Peek at the next token. */
10468 token
= cp_lexer_peek_token (parser
->lexer
);
10469 /* If it's a `*' or `&' we have a pointer or reference. */
10470 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10472 /* Remember which ptr-operator we were processing. */
10473 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10475 /* Consume the `*' or `&'. */
10476 cp_lexer_consume_token (parser
->lexer
);
10478 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10479 `&', if we are allowing GNU extensions. (The only qualifier
10480 that can legally appear after `&' is `restrict', but that is
10481 enforced during semantic analysis. */
10482 if (code
== INDIRECT_REF
10483 || cp_parser_allow_gnu_extensions_p (parser
))
10484 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10488 /* Try the pointer-to-member case. */
10489 cp_parser_parse_tentatively (parser
);
10490 /* Look for the optional `::' operator. */
10491 cp_parser_global_scope_opt (parser
,
10492 /*current_scope_valid_p=*/false);
10493 /* Look for the nested-name specifier. */
10494 cp_parser_nested_name_specifier (parser
,
10495 /*typename_keyword_p=*/false,
10496 /*check_dependency_p=*/true,
10498 /*is_declaration=*/false);
10499 /* If we found it, and the next token is a `*', then we are
10500 indeed looking at a pointer-to-member operator. */
10501 if (!cp_parser_error_occurred (parser
)
10502 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10504 /* The type of which the member is a member is given by the
10506 *type
= parser
->scope
;
10507 /* The next name will not be qualified. */
10508 parser
->scope
= NULL_TREE
;
10509 parser
->qualifying_scope
= NULL_TREE
;
10510 parser
->object_scope
= NULL_TREE
;
10511 /* Indicate that the `*' operator was used. */
10512 code
= INDIRECT_REF
;
10513 /* Look for the optional cv-qualifier-seq. */
10514 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10516 /* If that didn't work we don't have a ptr-operator. */
10517 if (!cp_parser_parse_definitely (parser
))
10518 cp_parser_error (parser
, "expected ptr-operator");
10524 /* Parse an (optional) cv-qualifier-seq.
10527 cv-qualifier cv-qualifier-seq [opt]
10529 Returns a TREE_LIST. The TREE_VALUE of each node is the
10530 representation of a cv-qualifier. */
10533 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10535 tree cv_qualifiers
= NULL_TREE
;
10541 /* Look for the next cv-qualifier. */
10542 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10543 /* If we didn't find one, we're done. */
10547 /* Add this cv-qualifier to the list. */
10549 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10552 /* We built up the list in reverse order. */
10553 return nreverse (cv_qualifiers
);
10556 /* Parse an (optional) cv-qualifier.
10568 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10571 tree cv_qualifier
= NULL_TREE
;
10573 /* Peek at the next token. */
10574 token
= cp_lexer_peek_token (parser
->lexer
);
10575 /* See if it's a cv-qualifier. */
10576 switch (token
->keyword
)
10581 /* Save the value of the token. */
10582 cv_qualifier
= token
->value
;
10583 /* Consume the token. */
10584 cp_lexer_consume_token (parser
->lexer
);
10591 return cv_qualifier
;
10594 /* Parse a declarator-id.
10598 :: [opt] nested-name-specifier [opt] type-name
10600 In the `id-expression' case, the value returned is as for
10601 cp_parser_id_expression if the id-expression was an unqualified-id.
10602 If the id-expression was a qualified-id, then a SCOPE_REF is
10603 returned. The first operand is the scope (either a NAMESPACE_DECL
10604 or TREE_TYPE), but the second is still just a representation of an
10608 cp_parser_declarator_id (cp_parser
* parser
)
10610 tree id_expression
;
10612 /* The expression must be an id-expression. Assume that qualified
10613 names are the names of types so that:
10616 int S<T>::R::i = 3;
10618 will work; we must treat `S<T>::R' as the name of a type.
10619 Similarly, assume that qualified names are templates, where
10623 int S<T>::R<T>::i = 3;
10626 id_expression
= cp_parser_id_expression (parser
,
10627 /*template_keyword_p=*/false,
10628 /*check_dependency_p=*/false,
10629 /*template_p=*/NULL
,
10630 /*declarator_p=*/true);
10631 /* If the name was qualified, create a SCOPE_REF to represent
10635 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
10636 parser
->scope
= NULL_TREE
;
10639 return id_expression
;
10642 /* Parse a type-id.
10645 type-specifier-seq abstract-declarator [opt]
10647 Returns the TYPE specified. */
10650 cp_parser_type_id (cp_parser
* parser
)
10652 tree type_specifier_seq
;
10653 tree abstract_declarator
;
10655 /* Parse the type-specifier-seq. */
10657 = cp_parser_type_specifier_seq (parser
);
10658 if (type_specifier_seq
== error_mark_node
)
10659 return error_mark_node
;
10661 /* There might or might not be an abstract declarator. */
10662 cp_parser_parse_tentatively (parser
);
10663 /* Look for the declarator. */
10664 abstract_declarator
10665 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
10666 /*parenthesized_p=*/NULL
);
10667 /* Check to see if there really was a declarator. */
10668 if (!cp_parser_parse_definitely (parser
))
10669 abstract_declarator
= NULL_TREE
;
10671 return groktypename (build_tree_list (type_specifier_seq
,
10672 abstract_declarator
));
10675 /* Parse a type-specifier-seq.
10677 type-specifier-seq:
10678 type-specifier type-specifier-seq [opt]
10682 type-specifier-seq:
10683 attributes type-specifier-seq [opt]
10685 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10686 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10689 cp_parser_type_specifier_seq (cp_parser
* parser
)
10691 bool seen_type_specifier
= false;
10692 tree type_specifier_seq
= NULL_TREE
;
10694 /* Parse the type-specifiers and attributes. */
10697 tree type_specifier
;
10699 /* Check for attributes first. */
10700 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
10702 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
10704 type_specifier_seq
);
10708 /* After the first type-specifier, others are optional. */
10709 if (seen_type_specifier
)
10710 cp_parser_parse_tentatively (parser
);
10711 /* Look for the type-specifier. */
10712 type_specifier
= cp_parser_type_specifier (parser
,
10713 CP_PARSER_FLAGS_NONE
,
10714 /*is_friend=*/false,
10715 /*is_declaration=*/false,
10718 /* If the first type-specifier could not be found, this is not a
10719 type-specifier-seq at all. */
10720 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
10721 return error_mark_node
;
10722 /* If subsequent type-specifiers could not be found, the
10723 type-specifier-seq is complete. */
10724 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
10727 /* Add the new type-specifier to the list. */
10729 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
10730 seen_type_specifier
= true;
10733 /* We built up the list in reverse order. */
10734 return nreverse (type_specifier_seq
);
10737 /* Parse a parameter-declaration-clause.
10739 parameter-declaration-clause:
10740 parameter-declaration-list [opt] ... [opt]
10741 parameter-declaration-list , ...
10743 Returns a representation for the parameter declarations. Each node
10744 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10745 representation.) If the parameter-declaration-clause ends with an
10746 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10747 list. A return value of NULL_TREE indicates a
10748 parameter-declaration-clause consisting only of an ellipsis. */
10751 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
10757 /* Peek at the next token. */
10758 token
= cp_lexer_peek_token (parser
->lexer
);
10759 /* Check for trivial parameter-declaration-clauses. */
10760 if (token
->type
== CPP_ELLIPSIS
)
10762 /* Consume the `...' token. */
10763 cp_lexer_consume_token (parser
->lexer
);
10766 else if (token
->type
== CPP_CLOSE_PAREN
)
10767 /* There are no parameters. */
10769 #ifndef NO_IMPLICIT_EXTERN_C
10770 if (in_system_header
&& current_class_type
== NULL
10771 && current_lang_name
== lang_name_c
)
10775 return void_list_node
;
10777 /* Check for `(void)', too, which is a special case. */
10778 else if (token
->keyword
== RID_VOID
10779 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
10780 == CPP_CLOSE_PAREN
))
10782 /* Consume the `void' token. */
10783 cp_lexer_consume_token (parser
->lexer
);
10784 /* There are no parameters. */
10785 return void_list_node
;
10788 /* Parse the parameter-declaration-list. */
10789 parameters
= cp_parser_parameter_declaration_list (parser
);
10790 /* If a parse error occurred while parsing the
10791 parameter-declaration-list, then the entire
10792 parameter-declaration-clause is erroneous. */
10793 if (parameters
== error_mark_node
)
10794 return error_mark_node
;
10796 /* Peek at the next token. */
10797 token
= cp_lexer_peek_token (parser
->lexer
);
10798 /* If it's a `,', the clause should terminate with an ellipsis. */
10799 if (token
->type
== CPP_COMMA
)
10801 /* Consume the `,'. */
10802 cp_lexer_consume_token (parser
->lexer
);
10803 /* Expect an ellipsis. */
10805 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
10807 /* It might also be `...' if the optional trailing `,' was
10809 else if (token
->type
== CPP_ELLIPSIS
)
10811 /* Consume the `...' token. */
10812 cp_lexer_consume_token (parser
->lexer
);
10813 /* And remember that we saw it. */
10817 ellipsis_p
= false;
10819 /* Finish the parameter list. */
10820 return finish_parmlist (parameters
, ellipsis_p
);
10823 /* Parse a parameter-declaration-list.
10825 parameter-declaration-list:
10826 parameter-declaration
10827 parameter-declaration-list , parameter-declaration
10829 Returns a representation of the parameter-declaration-list, as for
10830 cp_parser_parameter_declaration_clause. However, the
10831 `void_list_node' is never appended to the list. */
10834 cp_parser_parameter_declaration_list (cp_parser
* parser
)
10836 tree parameters
= NULL_TREE
;
10838 /* Look for more parameters. */
10842 bool parenthesized_p
;
10843 /* Parse the parameter. */
10845 = cp_parser_parameter_declaration (parser
,
10846 /*template_parm_p=*/false,
10849 /* If a parse error occurred parsing the parameter declaration,
10850 then the entire parameter-declaration-list is erroneous. */
10851 if (parameter
== error_mark_node
)
10853 parameters
= error_mark_node
;
10856 /* Add the new parameter to the list. */
10857 TREE_CHAIN (parameter
) = parameters
;
10858 parameters
= parameter
;
10860 /* Peek at the next token. */
10861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
10862 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10863 /* The parameter-declaration-list is complete. */
10865 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10869 /* Peek at the next token. */
10870 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10871 /* If it's an ellipsis, then the list is complete. */
10872 if (token
->type
== CPP_ELLIPSIS
)
10874 /* Otherwise, there must be more parameters. Consume the
10876 cp_lexer_consume_token (parser
->lexer
);
10877 /* When parsing something like:
10879 int i(float f, double d)
10881 we can tell after seeing the declaration for "f" that we
10882 are not looking at an initialization of a variable "i",
10883 but rather at the declaration of a function "i".
10885 Due to the fact that the parsing of template arguments
10886 (as specified to a template-id) requires backtracking we
10887 cannot use this technique when inside a template argument
10889 if (!parser
->in_template_argument_list_p
10890 && cp_parser_parsing_tentatively (parser
)
10891 && !cp_parser_committed_to_tentative_parse (parser
)
10892 /* However, a parameter-declaration of the form
10893 "foat(f)" (which is a valid declaration of a
10894 parameter "f") can also be interpreted as an
10895 expression (the conversion of "f" to "float"). */
10896 && !parenthesized_p
)
10897 cp_parser_commit_to_tentative_parse (parser
);
10901 cp_parser_error (parser
, "expected `,' or `...'");
10902 if (!cp_parser_parsing_tentatively (parser
)
10903 || cp_parser_committed_to_tentative_parse (parser
))
10904 cp_parser_skip_to_closing_parenthesis (parser
,
10905 /*recovering=*/true,
10906 /*or_comma=*/false,
10907 /*consume_paren=*/false);
10912 /* We built up the list in reverse order; straighten it out now. */
10913 return nreverse (parameters
);
10916 /* Parse a parameter declaration.
10918 parameter-declaration:
10919 decl-specifier-seq declarator
10920 decl-specifier-seq declarator = assignment-expression
10921 decl-specifier-seq abstract-declarator [opt]
10922 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10924 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10925 declares a template parameter. (In that case, a non-nested `>'
10926 token encountered during the parsing of the assignment-expression
10927 is not interpreted as a greater-than operator.)
10929 Returns a TREE_LIST representing the parameter-declaration. The
10930 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10931 there is no default argument. The TREE_VALUE is a representation
10932 of the decl-specifier-seq and declarator. In particular, the
10933 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10934 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10935 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10936 the declarator is of the form "(p)". */
10939 cp_parser_parameter_declaration (cp_parser
*parser
,
10940 bool template_parm_p
,
10941 bool *parenthesized_p
)
10943 int declares_class_or_enum
;
10944 bool greater_than_is_operator_p
;
10945 tree decl_specifiers
;
10948 tree default_argument
;
10951 const char *saved_message
;
10953 /* In a template parameter, `>' is not an operator.
10957 When parsing a default template-argument for a non-type
10958 template-parameter, the first non-nested `>' is taken as the end
10959 of the template parameter-list rather than a greater-than
10961 greater_than_is_operator_p
= !template_parm_p
;
10963 /* Type definitions may not appear in parameter types. */
10964 saved_message
= parser
->type_definition_forbidden_message
;
10965 parser
->type_definition_forbidden_message
10966 = "types may not be defined in parameter types";
10968 /* Parse the declaration-specifiers. */
10970 = cp_parser_decl_specifier_seq (parser
,
10971 CP_PARSER_FLAGS_NONE
,
10973 &declares_class_or_enum
);
10974 /* If an error occurred, there's no reason to attempt to parse the
10975 rest of the declaration. */
10976 if (cp_parser_error_occurred (parser
))
10978 parser
->type_definition_forbidden_message
= saved_message
;
10979 return error_mark_node
;
10982 /* Peek at the next token. */
10983 token
= cp_lexer_peek_token (parser
->lexer
);
10984 /* If the next token is a `)', `,', `=', `>', or `...', then there
10985 is no declarator. */
10986 if (token
->type
== CPP_CLOSE_PAREN
10987 || token
->type
== CPP_COMMA
10988 || token
->type
== CPP_EQ
10989 || token
->type
== CPP_ELLIPSIS
10990 || token
->type
== CPP_GREATER
)
10992 declarator
= NULL_TREE
;
10993 if (parenthesized_p
)
10994 *parenthesized_p
= false;
10996 /* Otherwise, there should be a declarator. */
10999 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11000 parser
->default_arg_ok_p
= false;
11002 /* After seeing a decl-specifier-seq, if the next token is not a
11003 "(", there is no possibility that the code is a valid
11004 expression. Therefore, if parsing tentatively, we commit at
11006 if (!parser
->in_template_argument_list_p
11007 /* In an expression context, having seen:
11011 we cannot be sure whether we are looking at a
11012 function-type (taking a "char*" as a parameter) or a cast
11013 of some object of type "char*" to "int". */
11014 && !parser
->in_type_id_in_expr_p
11015 && cp_parser_parsing_tentatively (parser
)
11016 && !cp_parser_committed_to_tentative_parse (parser
)
11017 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11018 cp_parser_commit_to_tentative_parse (parser
);
11019 /* Parse the declarator. */
11020 declarator
= cp_parser_declarator (parser
,
11021 CP_PARSER_DECLARATOR_EITHER
,
11022 /*ctor_dtor_or_conv_p=*/NULL
,
11024 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11025 /* After the declarator, allow more attributes. */
11026 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
11029 /* The restriction on defining new types applies only to the type
11030 of the parameter, not to the default argument. */
11031 parser
->type_definition_forbidden_message
= saved_message
;
11033 /* If the next token is `=', then process a default argument. */
11034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11036 bool saved_greater_than_is_operator_p
;
11037 /* Consume the `='. */
11038 cp_lexer_consume_token (parser
->lexer
);
11040 /* If we are defining a class, then the tokens that make up the
11041 default argument must be saved and processed later. */
11042 if (!template_parm_p
&& at_class_scope_p ()
11043 && TYPE_BEING_DEFINED (current_class_type
))
11045 unsigned depth
= 0;
11047 /* Create a DEFAULT_ARG to represented the unparsed default
11049 default_argument
= make_node (DEFAULT_ARG
);
11050 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
11052 /* Add tokens until we have processed the entire default
11059 /* Peek at the next token. */
11060 token
= cp_lexer_peek_token (parser
->lexer
);
11061 /* What we do depends on what token we have. */
11062 switch (token
->type
)
11064 /* In valid code, a default argument must be
11065 immediately followed by a `,' `)', or `...'. */
11067 case CPP_CLOSE_PAREN
:
11069 /* If we run into a non-nested `;', `}', or `]',
11070 then the code is invalid -- but the default
11071 argument is certainly over. */
11072 case CPP_SEMICOLON
:
11073 case CPP_CLOSE_BRACE
:
11074 case CPP_CLOSE_SQUARE
:
11077 /* Update DEPTH, if necessary. */
11078 else if (token
->type
== CPP_CLOSE_PAREN
11079 || token
->type
== CPP_CLOSE_BRACE
11080 || token
->type
== CPP_CLOSE_SQUARE
)
11084 case CPP_OPEN_PAREN
:
11085 case CPP_OPEN_SQUARE
:
11086 case CPP_OPEN_BRACE
:
11091 /* If we see a non-nested `>', and `>' is not an
11092 operator, then it marks the end of the default
11094 if (!depth
&& !greater_than_is_operator_p
)
11098 /* If we run out of tokens, issue an error message. */
11100 error ("file ends in default argument");
11106 /* In these cases, we should look for template-ids.
11107 For example, if the default argument is
11108 `X<int, double>()', we need to do name lookup to
11109 figure out whether or not `X' is a template; if
11110 so, the `,' does not end the default argument.
11112 That is not yet done. */
11119 /* If we've reached the end, stop. */
11123 /* Add the token to the token block. */
11124 token
= cp_lexer_consume_token (parser
->lexer
);
11125 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
11129 /* Outside of a class definition, we can just parse the
11130 assignment-expression. */
11133 bool saved_local_variables_forbidden_p
;
11135 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11137 saved_greater_than_is_operator_p
11138 = parser
->greater_than_is_operator_p
;
11139 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11140 /* Local variable names (and the `this' keyword) may not
11141 appear in a default argument. */
11142 saved_local_variables_forbidden_p
11143 = parser
->local_variables_forbidden_p
;
11144 parser
->local_variables_forbidden_p
= true;
11145 /* Parse the assignment-expression. */
11146 default_argument
= cp_parser_assignment_expression (parser
);
11147 /* Restore saved state. */
11148 parser
->greater_than_is_operator_p
11149 = saved_greater_than_is_operator_p
;
11150 parser
->local_variables_forbidden_p
11151 = saved_local_variables_forbidden_p
;
11153 if (!parser
->default_arg_ok_p
)
11155 if (!flag_pedantic_errors
)
11156 warning ("deprecated use of default argument for parameter of non-function");
11159 error ("default arguments are only permitted for function parameters");
11160 default_argument
= NULL_TREE
;
11165 default_argument
= NULL_TREE
;
11167 /* Create the representation of the parameter. */
11169 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
11170 parameter
= build_tree_list (default_argument
,
11171 build_tree_list (decl_specifiers
,
11177 /* Parse a function-body.
11180 compound_statement */
11183 cp_parser_function_body (cp_parser
*parser
)
11185 cp_parser_compound_statement (parser
, false);
11188 /* Parse a ctor-initializer-opt followed by a function-body. Return
11189 true if a ctor-initializer was present. */
11192 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11195 bool ctor_initializer_p
;
11197 /* Begin the function body. */
11198 body
= begin_function_body ();
11199 /* Parse the optional ctor-initializer. */
11200 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11201 /* Parse the function-body. */
11202 cp_parser_function_body (parser
);
11203 /* Finish the function body. */
11204 finish_function_body (body
);
11206 return ctor_initializer_p
;
11209 /* Parse an initializer.
11212 = initializer-clause
11213 ( expression-list )
11215 Returns a expression representing the initializer. If no
11216 initializer is present, NULL_TREE is returned.
11218 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11219 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11220 set to FALSE if there is no initializer present. If there is an
11221 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11222 is set to true; otherwise it is set to false. */
11225 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
11226 bool* non_constant_p
)
11231 /* Peek at the next token. */
11232 token
= cp_lexer_peek_token (parser
->lexer
);
11234 /* Let our caller know whether or not this initializer was
11236 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
11237 /* Assume that the initializer is constant. */
11238 *non_constant_p
= false;
11240 if (token
->type
== CPP_EQ
)
11242 /* Consume the `='. */
11243 cp_lexer_consume_token (parser
->lexer
);
11244 /* Parse the initializer-clause. */
11245 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
11247 else if (token
->type
== CPP_OPEN_PAREN
)
11248 init
= cp_parser_parenthesized_expression_list (parser
, false,
11252 /* Anything else is an error. */
11253 cp_parser_error (parser
, "expected initializer");
11254 init
= error_mark_node
;
11260 /* Parse an initializer-clause.
11262 initializer-clause:
11263 assignment-expression
11264 { initializer-list , [opt] }
11267 Returns an expression representing the initializer.
11269 If the `assignment-expression' production is used the value
11270 returned is simply a representation for the expression.
11272 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11273 the elements of the initializer-list (or NULL_TREE, if the last
11274 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11275 NULL_TREE. There is no way to detect whether or not the optional
11276 trailing `,' was provided. NON_CONSTANT_P is as for
11277 cp_parser_initializer. */
11280 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
11284 /* If it is not a `{', then we are looking at an
11285 assignment-expression. */
11286 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11288 = cp_parser_constant_expression (parser
,
11289 /*allow_non_constant_p=*/true,
11293 /* Consume the `{' token. */
11294 cp_lexer_consume_token (parser
->lexer
);
11295 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11296 initializer
= make_node (CONSTRUCTOR
);
11297 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11298 necessary, but check_initializer depends upon it, for
11300 TREE_HAS_CONSTRUCTOR (initializer
) = 1;
11301 /* If it's not a `}', then there is a non-trivial initializer. */
11302 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11304 /* Parse the initializer list. */
11305 CONSTRUCTOR_ELTS (initializer
)
11306 = cp_parser_initializer_list (parser
, non_constant_p
);
11307 /* A trailing `,' token is allowed. */
11308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11309 cp_lexer_consume_token (parser
->lexer
);
11311 /* Now, there should be a trailing `}'. */
11312 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11315 return initializer
;
11318 /* Parse an initializer-list.
11322 initializer-list , initializer-clause
11327 identifier : initializer-clause
11328 initializer-list, identifier : initializer-clause
11330 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11331 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11332 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11333 as for cp_parser_initializer. */
11336 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
11338 tree initializers
= NULL_TREE
;
11340 /* Assume all of the expressions are constant. */
11341 *non_constant_p
= false;
11343 /* Parse the rest of the list. */
11349 bool clause_non_constant_p
;
11351 /* If the next token is an identifier and the following one is a
11352 colon, we are looking at the GNU designated-initializer
11354 if (cp_parser_allow_gnu_extensions_p (parser
)
11355 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11356 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11358 /* Consume the identifier. */
11359 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11360 /* Consume the `:'. */
11361 cp_lexer_consume_token (parser
->lexer
);
11364 identifier
= NULL_TREE
;
11366 /* Parse the initializer. */
11367 initializer
= cp_parser_initializer_clause (parser
,
11368 &clause_non_constant_p
);
11369 /* If any clause is non-constant, so is the entire initializer. */
11370 if (clause_non_constant_p
)
11371 *non_constant_p
= true;
11372 /* Add it to the list. */
11373 initializers
= tree_cons (identifier
, initializer
, initializers
);
11375 /* If the next token is not a comma, we have reached the end of
11377 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11380 /* Peek at the next token. */
11381 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11382 /* If the next token is a `}', then we're still done. An
11383 initializer-clause can have a trailing `,' after the
11384 initializer-list and before the closing `}'. */
11385 if (token
->type
== CPP_CLOSE_BRACE
)
11388 /* Consume the `,' token. */
11389 cp_lexer_consume_token (parser
->lexer
);
11392 /* The initializers were built up in reverse order, so we need to
11393 reverse them now. */
11394 return nreverse (initializers
);
11397 /* Classes [gram.class] */
11399 /* Parse a class-name.
11405 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11406 to indicate that names looked up in dependent types should be
11407 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11408 keyword has been used to indicate that the name that appears next
11409 is a template. TYPE_P is true iff the next name should be treated
11410 as class-name, even if it is declared to be some other kind of name
11411 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11412 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11413 being defined in a class-head.
11415 Returns the TYPE_DECL representing the class. */
11418 cp_parser_class_name (cp_parser
*parser
,
11419 bool typename_keyword_p
,
11420 bool template_keyword_p
,
11422 bool check_dependency_p
,
11424 bool is_declaration
)
11431 /* All class-names start with an identifier. */
11432 token
= cp_lexer_peek_token (parser
->lexer
);
11433 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11435 cp_parser_error (parser
, "expected class-name");
11436 return error_mark_node
;
11439 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11440 to a template-id, so we save it here. */
11441 scope
= parser
->scope
;
11442 if (scope
== error_mark_node
)
11443 return error_mark_node
;
11445 /* Any name names a type if we're following the `typename' keyword
11446 in a qualified name where the enclosing scope is type-dependent. */
11447 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11448 && dependent_type_p (scope
));
11449 /* Handle the common case (an identifier, but not a template-id)
11451 if (token
->type
== CPP_NAME
11452 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
11456 /* Look for the identifier. */
11457 identifier
= cp_parser_identifier (parser
);
11458 /* If the next token isn't an identifier, we are certainly not
11459 looking at a class-name. */
11460 if (identifier
== error_mark_node
)
11461 decl
= error_mark_node
;
11462 /* If we know this is a type-name, there's no need to look it
11464 else if (typename_p
)
11468 /* If the next token is a `::', then the name must be a type
11471 [basic.lookup.qual]
11473 During the lookup for a name preceding the :: scope
11474 resolution operator, object, function, and enumerator
11475 names are ignored. */
11476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11478 /* Look up the name. */
11479 decl
= cp_parser_lookup_name (parser
, identifier
,
11481 /*is_template=*/false,
11482 /*is_namespace=*/false,
11483 check_dependency_p
);
11488 /* Try a template-id. */
11489 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11490 check_dependency_p
,
11492 if (decl
== error_mark_node
)
11493 return error_mark_node
;
11496 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11498 /* If this is a typename, create a TYPENAME_TYPE. */
11499 if (typename_p
&& decl
!= error_mark_node
)
11500 decl
= TYPE_NAME (make_typename_type (scope
, decl
,
11503 /* Check to see that it is really the name of a class. */
11504 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11505 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11506 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11507 /* Situations like this:
11509 template <typename T> struct A {
11510 typename T::template X<int>::I i;
11513 are problematic. Is `T::template X<int>' a class-name? The
11514 standard does not seem to be definitive, but there is no other
11515 valid interpretation of the following `::'. Therefore, those
11516 names are considered class-names. */
11517 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11518 else if (decl
== error_mark_node
11519 || TREE_CODE (decl
) != TYPE_DECL
11520 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11522 cp_parser_error (parser
, "expected class-name");
11523 return error_mark_node
;
11529 /* Parse a class-specifier.
11532 class-head { member-specification [opt] }
11534 Returns the TREE_TYPE representing the class. */
11537 cp_parser_class_specifier (cp_parser
* parser
)
11541 tree attributes
= NULL_TREE
;
11542 int has_trailing_semicolon
;
11543 bool nested_name_specifier_p
;
11544 unsigned saved_num_template_parameter_lists
;
11546 push_deferring_access_checks (dk_no_deferred
);
11548 /* Parse the class-head. */
11549 type
= cp_parser_class_head (parser
,
11550 &nested_name_specifier_p
);
11551 /* If the class-head was a semantic disaster, skip the entire body
11555 cp_parser_skip_to_end_of_block_or_statement (parser
);
11556 pop_deferring_access_checks ();
11557 return error_mark_node
;
11560 /* Look for the `{'. */
11561 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11563 pop_deferring_access_checks ();
11564 return error_mark_node
;
11567 /* Issue an error message if type-definitions are forbidden here. */
11568 cp_parser_check_type_definition (parser
);
11569 /* Remember that we are defining one more class. */
11570 ++parser
->num_classes_being_defined
;
11571 /* Inside the class, surrounding template-parameter-lists do not
11573 saved_num_template_parameter_lists
11574 = parser
->num_template_parameter_lists
;
11575 parser
->num_template_parameter_lists
= 0;
11577 /* Start the class. */
11578 if (nested_name_specifier_p
)
11579 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11580 type
= begin_class_definition (type
);
11581 if (type
== error_mark_node
)
11582 /* If the type is erroneous, skip the entire body of the class. */
11583 cp_parser_skip_to_closing_brace (parser
);
11585 /* Parse the member-specification. */
11586 cp_parser_member_specification_opt (parser
);
11587 /* Look for the trailing `}'. */
11588 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11589 /* We get better error messages by noticing a common problem: a
11590 missing trailing `;'. */
11591 token
= cp_lexer_peek_token (parser
->lexer
);
11592 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11593 /* Look for attributes to apply to this class. */
11594 if (cp_parser_allow_gnu_extensions_p (parser
))
11595 attributes
= cp_parser_attributes_opt (parser
);
11596 /* If we got any attributes in class_head, xref_tag will stick them in
11597 TREE_TYPE of the type. Grab them now. */
11598 if (type
!= error_mark_node
)
11600 attributes
= chainon (TYPE_ATTRIBUTES (type
), attributes
);
11601 TYPE_ATTRIBUTES (type
) = NULL_TREE
;
11602 type
= finish_struct (type
, attributes
);
11604 if (nested_name_specifier_p
)
11605 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11606 /* If this class is not itself within the scope of another class,
11607 then we need to parse the bodies of all of the queued function
11608 definitions. Note that the queued functions defined in a class
11609 are not always processed immediately following the
11610 class-specifier for that class. Consider:
11613 struct B { void f() { sizeof (A); } };
11616 If `f' were processed before the processing of `A' were
11617 completed, there would be no way to compute the size of `A'.
11618 Note that the nesting we are interested in here is lexical --
11619 not the semantic nesting given by TYPE_CONTEXT. In particular,
11622 struct A { struct B; };
11623 struct A::B { void f() { } };
11625 there is no need to delay the parsing of `A::B::f'. */
11626 if (--parser
->num_classes_being_defined
== 0)
11631 /* In a first pass, parse default arguments to the functions.
11632 Then, in a second pass, parse the bodies of the functions.
11633 This two-phased approach handles cases like:
11641 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
11642 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
11643 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
11644 TREE_PURPOSE (parser
->unparsed_functions_queues
)
11645 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
11647 fn
= TREE_VALUE (queue_entry
);
11648 /* Make sure that any template parameters are in scope. */
11649 maybe_begin_member_template_processing (fn
);
11650 /* If there are default arguments that have not yet been processed,
11651 take care of them now. */
11652 cp_parser_late_parsing_default_args (parser
, fn
);
11653 /* Remove any template parameters from the symbol table. */
11654 maybe_end_member_template_processing ();
11656 /* Now parse the body of the functions. */
11657 for (TREE_VALUE (parser
->unparsed_functions_queues
)
11658 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
11659 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
11660 TREE_VALUE (parser
->unparsed_functions_queues
)
11661 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
11663 /* Figure out which function we need to process. */
11664 fn
= TREE_VALUE (queue_entry
);
11666 /* Parse the function. */
11667 cp_parser_late_parsing_for_member (parser
, fn
);
11672 /* Put back any saved access checks. */
11673 pop_deferring_access_checks ();
11675 /* Restore the count of active template-parameter-lists. */
11676 parser
->num_template_parameter_lists
11677 = saved_num_template_parameter_lists
;
11682 /* Parse a class-head.
11685 class-key identifier [opt] base-clause [opt]
11686 class-key nested-name-specifier identifier base-clause [opt]
11687 class-key nested-name-specifier [opt] template-id
11691 class-key attributes identifier [opt] base-clause [opt]
11692 class-key attributes nested-name-specifier identifier base-clause [opt]
11693 class-key attributes nested-name-specifier [opt] template-id
11696 Returns the TYPE of the indicated class. Sets
11697 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11698 involving a nested-name-specifier was used, and FALSE otherwise.
11700 Returns NULL_TREE if the class-head is syntactically valid, but
11701 semantically invalid in a way that means we should skip the entire
11702 body of the class. */
11705 cp_parser_class_head (cp_parser
* parser
,
11706 bool* nested_name_specifier_p
)
11709 tree nested_name_specifier
;
11710 enum tag_types class_key
;
11711 tree id
= NULL_TREE
;
11712 tree type
= NULL_TREE
;
11714 bool template_id_p
= false;
11715 bool qualified_p
= false;
11716 bool invalid_nested_name_p
= false;
11717 bool invalid_explicit_specialization_p
= false;
11718 unsigned num_templates
;
11720 /* Assume no nested-name-specifier will be present. */
11721 *nested_name_specifier_p
= false;
11722 /* Assume no template parameter lists will be used in defining the
11726 /* Look for the class-key. */
11727 class_key
= cp_parser_class_key (parser
);
11728 if (class_key
== none_type
)
11729 return error_mark_node
;
11731 /* Parse the attributes. */
11732 attributes
= cp_parser_attributes_opt (parser
);
11734 /* If the next token is `::', that is invalid -- but sometimes
11735 people do try to write:
11739 Handle this gracefully by accepting the extra qualifier, and then
11740 issuing an error about it later if this really is a
11741 class-head. If it turns out just to be an elaborated type
11742 specifier, remain silent. */
11743 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
11744 qualified_p
= true;
11746 push_deferring_access_checks (dk_no_check
);
11748 /* Determine the name of the class. Begin by looking for an
11749 optional nested-name-specifier. */
11750 nested_name_specifier
11751 = cp_parser_nested_name_specifier_opt (parser
,
11752 /*typename_keyword_p=*/false,
11753 /*check_dependency_p=*/false,
11755 /*is_declaration=*/false);
11756 /* If there was a nested-name-specifier, then there *must* be an
11758 if (nested_name_specifier
)
11760 /* Although the grammar says `identifier', it really means
11761 `class-name' or `template-name'. You are only allowed to
11762 define a class that has already been declared with this
11765 The proposed resolution for Core Issue 180 says that whever
11766 you see `class T::X' you should treat `X' as a type-name.
11768 It is OK to define an inaccessible class; for example:
11770 class A { class B; };
11773 We do not know if we will see a class-name, or a
11774 template-name. We look for a class-name first, in case the
11775 class-name is a template-id; if we looked for the
11776 template-name first we would stop after the template-name. */
11777 cp_parser_parse_tentatively (parser
);
11778 type
= cp_parser_class_name (parser
,
11779 /*typename_keyword_p=*/false,
11780 /*template_keyword_p=*/false,
11782 /*check_dependency_p=*/false,
11783 /*class_head_p=*/true,
11784 /*is_declaration=*/false);
11785 /* If that didn't work, ignore the nested-name-specifier. */
11786 if (!cp_parser_parse_definitely (parser
))
11788 invalid_nested_name_p
= true;
11789 id
= cp_parser_identifier (parser
);
11790 if (id
== error_mark_node
)
11793 /* If we could not find a corresponding TYPE, treat this
11794 declaration like an unqualified declaration. */
11795 if (type
== error_mark_node
)
11796 nested_name_specifier
= NULL_TREE
;
11797 /* Otherwise, count the number of templates used in TYPE and its
11798 containing scopes. */
11803 for (scope
= TREE_TYPE (type
);
11804 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
11805 scope
= (TYPE_P (scope
)
11806 ? TYPE_CONTEXT (scope
)
11807 : DECL_CONTEXT (scope
)))
11809 && CLASS_TYPE_P (scope
)
11810 && CLASSTYPE_TEMPLATE_INFO (scope
)
11811 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
11812 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
11816 /* Otherwise, the identifier is optional. */
11819 /* We don't know whether what comes next is a template-id,
11820 an identifier, or nothing at all. */
11821 cp_parser_parse_tentatively (parser
);
11822 /* Check for a template-id. */
11823 id
= cp_parser_template_id (parser
,
11824 /*template_keyword_p=*/false,
11825 /*check_dependency_p=*/true,
11826 /*is_declaration=*/true);
11827 /* If that didn't work, it could still be an identifier. */
11828 if (!cp_parser_parse_definitely (parser
))
11830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11831 id
= cp_parser_identifier (parser
);
11837 template_id_p
= true;
11842 pop_deferring_access_checks ();
11844 cp_parser_check_for_invalid_template_id (parser
, id
);
11846 /* If it's not a `:' or a `{' then we can't really be looking at a
11847 class-head, since a class-head only appears as part of a
11848 class-specifier. We have to detect this situation before calling
11849 xref_tag, since that has irreversible side-effects. */
11850 if (!cp_parser_next_token_starts_class_definition_p (parser
))
11852 cp_parser_error (parser
, "expected `{' or `:'");
11853 return error_mark_node
;
11856 /* At this point, we're going ahead with the class-specifier, even
11857 if some other problem occurs. */
11858 cp_parser_commit_to_tentative_parse (parser
);
11859 /* Issue the error about the overly-qualified name now. */
11861 cp_parser_error (parser
,
11862 "global qualification of class name is invalid");
11863 else if (invalid_nested_name_p
)
11864 cp_parser_error (parser
,
11865 "qualified name does not name a class");
11866 else if (nested_name_specifier
)
11869 /* Figure out in what scope the declaration is being placed. */
11870 scope
= current_scope ();
11872 scope
= current_namespace
;
11873 /* If that scope does not contain the scope in which the
11874 class was originally declared, the program is invalid. */
11875 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
11877 error ("declaration of `%D' in `%D' which does not "
11878 "enclose `%D'", type
, scope
, nested_name_specifier
);
11884 A declarator-id shall not be qualified exception of the
11885 definition of a ... nested class outside of its class
11886 ... [or] a the definition or explicit instantiation of a
11887 class member of a namespace outside of its namespace. */
11888 if (scope
== nested_name_specifier
)
11890 pedwarn ("extra qualification ignored");
11891 nested_name_specifier
= NULL_TREE
;
11895 /* An explicit-specialization must be preceded by "template <>". If
11896 it is not, try to recover gracefully. */
11897 if (at_namespace_scope_p ()
11898 && parser
->num_template_parameter_lists
== 0
11901 error ("an explicit specialization must be preceded by 'template <>'");
11902 invalid_explicit_specialization_p
= true;
11903 /* Take the same action that would have been taken by
11904 cp_parser_explicit_specialization. */
11905 ++parser
->num_template_parameter_lists
;
11906 begin_specialization ();
11908 /* There must be no "return" statements between this point and the
11909 end of this function; set "type "to the correct return value and
11910 use "goto done;" to return. */
11911 /* Make sure that the right number of template parameters were
11913 if (!cp_parser_check_template_parameters (parser
, num_templates
))
11915 /* If something went wrong, there is no point in even trying to
11916 process the class-definition. */
11921 /* Look up the type. */
11924 type
= TREE_TYPE (id
);
11925 maybe_process_partial_specialization (type
);
11927 else if (!nested_name_specifier
)
11929 /* If the class was unnamed, create a dummy name. */
11931 id
= make_anon_name ();
11932 type
= xref_tag (class_key
, id
, attributes
, /*globalize=*/false,
11933 parser
->num_template_parameter_lists
);
11941 template <typename T> struct S { struct T };
11942 template <typename T> struct S<T>::T { };
11944 we will get a TYPENAME_TYPE when processing the definition of
11945 `S::T'. We need to resolve it to the actual type before we
11946 try to define it. */
11947 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
11949 class_type
= resolve_typename_type (TREE_TYPE (type
),
11950 /*only_current_p=*/false);
11951 if (class_type
!= error_mark_node
)
11952 type
= TYPE_NAME (class_type
);
11955 cp_parser_error (parser
, "could not resolve typename type");
11956 type
= error_mark_node
;
11960 maybe_process_partial_specialization (TREE_TYPE (type
));
11961 class_type
= current_class_type
;
11962 /* Enter the scope indicated by the nested-name-specifier. */
11963 if (nested_name_specifier
)
11964 push_scope (nested_name_specifier
);
11965 /* Get the canonical version of this type. */
11966 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
11967 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11968 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
11969 type
= push_template_decl (type
);
11970 type
= TREE_TYPE (type
);
11971 if (nested_name_specifier
)
11973 *nested_name_specifier_p
= true;
11974 pop_scope (nested_name_specifier
);
11977 /* Indicate whether this class was declared as a `class' or as a
11979 if (TREE_CODE (type
) == RECORD_TYPE
)
11980 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
11981 cp_parser_check_class_key (class_key
, type
);
11983 /* Enter the scope containing the class; the names of base classes
11984 should be looked up in that context. For example, given:
11986 struct A { struct B {}; struct C; };
11987 struct A::C : B {};
11990 if (nested_name_specifier
)
11991 push_scope (nested_name_specifier
);
11992 /* Now, look for the base-clause. */
11993 token
= cp_lexer_peek_token (parser
->lexer
);
11994 if (token
->type
== CPP_COLON
)
11998 /* Get the list of base-classes. */
11999 bases
= cp_parser_base_clause (parser
);
12000 /* Process them. */
12001 xref_basetypes (type
, bases
);
12003 /* Leave the scope given by the nested-name-specifier. We will
12004 enter the class scope itself while processing the members. */
12005 if (nested_name_specifier
)
12006 pop_scope (nested_name_specifier
);
12009 if (invalid_explicit_specialization_p
)
12011 end_specialization ();
12012 --parser
->num_template_parameter_lists
;
12017 /* Parse a class-key.
12024 Returns the kind of class-key specified, or none_type to indicate
12027 static enum tag_types
12028 cp_parser_class_key (cp_parser
* parser
)
12031 enum tag_types tag_type
;
12033 /* Look for the class-key. */
12034 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12038 /* Check to see if the TOKEN is a class-key. */
12039 tag_type
= cp_parser_token_is_class_key (token
);
12041 cp_parser_error (parser
, "expected class-key");
12045 /* Parse an (optional) member-specification.
12047 member-specification:
12048 member-declaration member-specification [opt]
12049 access-specifier : member-specification [opt] */
12052 cp_parser_member_specification_opt (cp_parser
* parser
)
12059 /* Peek at the next token. */
12060 token
= cp_lexer_peek_token (parser
->lexer
);
12061 /* If it's a `}', or EOF then we've seen all the members. */
12062 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12065 /* See if this token is a keyword. */
12066 keyword
= token
->keyword
;
12070 case RID_PROTECTED
:
12072 /* Consume the access-specifier. */
12073 cp_lexer_consume_token (parser
->lexer
);
12074 /* Remember which access-specifier is active. */
12075 current_access_specifier
= token
->value
;
12076 /* Look for the `:'. */
12077 cp_parser_require (parser
, CPP_COLON
, "`:'");
12081 /* Otherwise, the next construction must be a
12082 member-declaration. */
12083 cp_parser_member_declaration (parser
);
12088 /* Parse a member-declaration.
12090 member-declaration:
12091 decl-specifier-seq [opt] member-declarator-list [opt] ;
12092 function-definition ; [opt]
12093 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12095 template-declaration
12097 member-declarator-list:
12099 member-declarator-list , member-declarator
12102 declarator pure-specifier [opt]
12103 declarator constant-initializer [opt]
12104 identifier [opt] : constant-expression
12108 member-declaration:
12109 __extension__ member-declaration
12112 declarator attributes [opt] pure-specifier [opt]
12113 declarator attributes [opt] constant-initializer [opt]
12114 identifier [opt] attributes [opt] : constant-expression */
12117 cp_parser_member_declaration (cp_parser
* parser
)
12119 tree decl_specifiers
;
12120 tree prefix_attributes
;
12122 int declares_class_or_enum
;
12125 int saved_pedantic
;
12127 /* Check for the `__extension__' keyword. */
12128 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12131 cp_parser_member_declaration (parser
);
12132 /* Restore the old value of the PEDANTIC flag. */
12133 pedantic
= saved_pedantic
;
12138 /* Check for a template-declaration. */
12139 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12141 /* Parse the template-declaration. */
12142 cp_parser_template_declaration (parser
, /*member_p=*/true);
12147 /* Check for a using-declaration. */
12148 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
12150 /* Parse the using-declaration. */
12151 cp_parser_using_declaration (parser
);
12156 /* Parse the decl-specifier-seq. */
12158 = cp_parser_decl_specifier_seq (parser
,
12159 CP_PARSER_FLAGS_OPTIONAL
,
12160 &prefix_attributes
,
12161 &declares_class_or_enum
);
12162 /* Check for an invalid type-name. */
12163 if (cp_parser_diagnose_invalid_type_name (parser
))
12165 /* If there is no declarator, then the decl-specifier-seq should
12167 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12169 /* If there was no decl-specifier-seq, and the next token is a
12170 `;', then we have something like:
12176 Each member-declaration shall declare at least one member
12177 name of the class. */
12178 if (!decl_specifiers
)
12181 pedwarn ("extra semicolon");
12187 /* See if this declaration is a friend. */
12188 friend_p
= cp_parser_friend_p (decl_specifiers
);
12189 /* If there were decl-specifiers, check to see if there was
12190 a class-declaration. */
12191 type
= check_tag_decl (decl_specifiers
);
12192 /* Nested classes have already been added to the class, but
12193 a `friend' needs to be explicitly registered. */
12196 /* If the `friend' keyword was present, the friend must
12197 be introduced with a class-key. */
12198 if (!declares_class_or_enum
)
12199 error ("a class-key must be used when declaring a friend");
12202 template <typename T> struct A {
12203 friend struct A<T>::B;
12206 A<T>::B will be represented by a TYPENAME_TYPE, and
12207 therefore not recognized by check_tag_decl. */
12212 for (specifier
= decl_specifiers
;
12214 specifier
= TREE_CHAIN (specifier
))
12216 tree s
= TREE_VALUE (specifier
);
12218 if (TREE_CODE (s
) == IDENTIFIER_NODE
)
12219 get_global_value_if_present (s
, &type
);
12220 if (TREE_CODE (s
) == TYPE_DECL
)
12230 error ("friend declaration does not name a class or "
12233 make_friend_class (current_class_type
, type
,
12234 /*complain=*/true);
12236 /* If there is no TYPE, an error message will already have
12240 /* An anonymous aggregate has to be handled specially; such
12241 a declaration really declares a data member (with a
12242 particular type), as opposed to a nested class. */
12243 else if (ANON_AGGR_TYPE_P (type
))
12245 /* Remove constructors and such from TYPE, now that we
12246 know it is an anonymous aggregate. */
12247 fixup_anonymous_aggr (type
);
12248 /* And make the corresponding data member. */
12249 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
12250 /* Add it to the class. */
12251 finish_member_declaration (decl
);
12254 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
12259 /* See if these declarations will be friends. */
12260 friend_p
= cp_parser_friend_p (decl_specifiers
);
12262 /* Keep going until we hit the `;' at the end of the
12264 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12266 tree attributes
= NULL_TREE
;
12267 tree first_attribute
;
12269 /* Peek at the next token. */
12270 token
= cp_lexer_peek_token (parser
->lexer
);
12272 /* Check for a bitfield declaration. */
12273 if (token
->type
== CPP_COLON
12274 || (token
->type
== CPP_NAME
12275 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12281 /* Get the name of the bitfield. Note that we cannot just
12282 check TOKEN here because it may have been invalidated by
12283 the call to cp_lexer_peek_nth_token above. */
12284 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
12285 identifier
= cp_parser_identifier (parser
);
12287 identifier
= NULL_TREE
;
12289 /* Consume the `:' token. */
12290 cp_lexer_consume_token (parser
->lexer
);
12291 /* Get the width of the bitfield. */
12293 = cp_parser_constant_expression (parser
,
12294 /*allow_non_constant=*/false,
12297 /* Look for attributes that apply to the bitfield. */
12298 attributes
= cp_parser_attributes_opt (parser
);
12299 /* Remember which attributes are prefix attributes and
12301 first_attribute
= attributes
;
12302 /* Combine the attributes. */
12303 attributes
= chainon (prefix_attributes
, attributes
);
12305 /* Create the bitfield declaration. */
12306 decl
= grokbitfield (identifier
,
12309 /* Apply the attributes. */
12310 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
12316 tree asm_specification
;
12317 int ctor_dtor_or_conv_p
;
12319 /* Parse the declarator. */
12321 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12322 &ctor_dtor_or_conv_p
,
12323 /*parenthesized_p=*/NULL
);
12325 /* If something went wrong parsing the declarator, make sure
12326 that we at least consume some tokens. */
12327 if (declarator
== error_mark_node
)
12329 /* Skip to the end of the statement. */
12330 cp_parser_skip_to_end_of_statement (parser
);
12331 /* If the next token is not a semicolon, that is
12332 probably because we just skipped over the body of
12333 a function. So, we consume a semicolon if
12334 present, but do not issue an error message if it
12336 if (cp_lexer_next_token_is (parser
->lexer
,
12338 cp_lexer_consume_token (parser
->lexer
);
12342 cp_parser_check_for_definition_in_return_type
12343 (declarator
, declares_class_or_enum
);
12345 /* Look for an asm-specification. */
12346 asm_specification
= cp_parser_asm_specification_opt (parser
);
12347 /* Look for attributes that apply to the declaration. */
12348 attributes
= cp_parser_attributes_opt (parser
);
12349 /* Remember which attributes are prefix attributes and
12351 first_attribute
= attributes
;
12352 /* Combine the attributes. */
12353 attributes
= chainon (prefix_attributes
, attributes
);
12355 /* If it's an `=', then we have a constant-initializer or a
12356 pure-specifier. It is not correct to parse the
12357 initializer before registering the member declaration
12358 since the member declaration should be in scope while
12359 its initializer is processed. However, the rest of the
12360 front end does not yet provide an interface that allows
12361 us to handle this correctly. */
12362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12366 A pure-specifier shall be used only in the declaration of
12367 a virtual function.
12369 A member-declarator can contain a constant-initializer
12370 only if it declares a static member of integral or
12373 Therefore, if the DECLARATOR is for a function, we look
12374 for a pure-specifier; otherwise, we look for a
12375 constant-initializer. When we call `grokfield', it will
12376 perform more stringent semantics checks. */
12377 if (TREE_CODE (declarator
) == CALL_EXPR
)
12378 initializer
= cp_parser_pure_specifier (parser
);
12380 /* Parse the initializer. */
12381 initializer
= cp_parser_constant_initializer (parser
);
12383 /* Otherwise, there is no initializer. */
12385 initializer
= NULL_TREE
;
12387 /* See if we are probably looking at a function
12388 definition. We are certainly not looking at at a
12389 member-declarator. Calling `grokfield' has
12390 side-effects, so we must not do it unless we are sure
12391 that we are looking at a member-declarator. */
12392 if (cp_parser_token_starts_function_definition_p
12393 (cp_lexer_peek_token (parser
->lexer
)))
12395 /* The grammar does not allow a pure-specifier to be
12396 used when a member function is defined. (It is
12397 possible that this fact is an oversight in the
12398 standard, since a pure function may be defined
12399 outside of the class-specifier. */
12401 error ("pure-specifier on function-definition");
12402 decl
= cp_parser_save_member_function_body (parser
,
12406 /* If the member was not a friend, declare it here. */
12408 finish_member_declaration (decl
);
12409 /* Peek at the next token. */
12410 token
= cp_lexer_peek_token (parser
->lexer
);
12411 /* If the next token is a semicolon, consume it. */
12412 if (token
->type
== CPP_SEMICOLON
)
12413 cp_lexer_consume_token (parser
->lexer
);
12418 /* Create the declaration. */
12419 decl
= grokfield (declarator
, decl_specifiers
,
12420 initializer
, asm_specification
,
12422 /* Any initialization must have been from a
12423 constant-expression. */
12424 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
12425 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
12429 /* Reset PREFIX_ATTRIBUTES. */
12430 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12431 attributes
= TREE_CHAIN (attributes
);
12433 TREE_CHAIN (attributes
) = NULL_TREE
;
12435 /* If there is any qualification still in effect, clear it
12436 now; we will be starting fresh with the next declarator. */
12437 parser
->scope
= NULL_TREE
;
12438 parser
->qualifying_scope
= NULL_TREE
;
12439 parser
->object_scope
= NULL_TREE
;
12440 /* If it's a `,', then there are more declarators. */
12441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12442 cp_lexer_consume_token (parser
->lexer
);
12443 /* If the next token isn't a `;', then we have a parse error. */
12444 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12447 cp_parser_error (parser
, "expected `;'");
12448 /* Skip tokens until we find a `;'. */
12449 cp_parser_skip_to_end_of_statement (parser
);
12456 /* Add DECL to the list of members. */
12458 finish_member_declaration (decl
);
12460 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12461 cp_parser_save_default_args (parser
, decl
);
12466 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12469 /* Parse a pure-specifier.
12474 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12475 Otherwise, ERROR_MARK_NODE is returned. */
12478 cp_parser_pure_specifier (cp_parser
* parser
)
12482 /* Look for the `=' token. */
12483 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12484 return error_mark_node
;
12485 /* Look for the `0' token. */
12486 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12487 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12488 to get information from the lexer about how the number was
12489 spelled in order to fix this problem. */
12490 if (!token
|| !integer_zerop (token
->value
))
12491 return error_mark_node
;
12493 return integer_zero_node
;
12496 /* Parse a constant-initializer.
12498 constant-initializer:
12499 = constant-expression
12501 Returns a representation of the constant-expression. */
12504 cp_parser_constant_initializer (cp_parser
* parser
)
12506 /* Look for the `=' token. */
12507 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12508 return error_mark_node
;
12510 /* It is invalid to write:
12512 struct S { static const int i = { 7 }; };
12515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12517 cp_parser_error (parser
,
12518 "a brace-enclosed initializer is not allowed here");
12519 /* Consume the opening brace. */
12520 cp_lexer_consume_token (parser
->lexer
);
12521 /* Skip the initializer. */
12522 cp_parser_skip_to_closing_brace (parser
);
12523 /* Look for the trailing `}'. */
12524 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12526 return error_mark_node
;
12529 return cp_parser_constant_expression (parser
,
12530 /*allow_non_constant=*/false,
12534 /* Derived classes [gram.class.derived] */
12536 /* Parse a base-clause.
12539 : base-specifier-list
12541 base-specifier-list:
12543 base-specifier-list , base-specifier
12545 Returns a TREE_LIST representing the base-classes, in the order in
12546 which they were declared. The representation of each node is as
12547 described by cp_parser_base_specifier.
12549 In the case that no bases are specified, this function will return
12550 NULL_TREE, not ERROR_MARK_NODE. */
12553 cp_parser_base_clause (cp_parser
* parser
)
12555 tree bases
= NULL_TREE
;
12557 /* Look for the `:' that begins the list. */
12558 cp_parser_require (parser
, CPP_COLON
, "`:'");
12560 /* Scan the base-specifier-list. */
12566 /* Look for the base-specifier. */
12567 base
= cp_parser_base_specifier (parser
);
12568 /* Add BASE to the front of the list. */
12569 if (base
!= error_mark_node
)
12571 TREE_CHAIN (base
) = bases
;
12574 /* Peek at the next token. */
12575 token
= cp_lexer_peek_token (parser
->lexer
);
12576 /* If it's not a comma, then the list is complete. */
12577 if (token
->type
!= CPP_COMMA
)
12579 /* Consume the `,'. */
12580 cp_lexer_consume_token (parser
->lexer
);
12583 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12584 base class had a qualified name. However, the next name that
12585 appears is certainly not qualified. */
12586 parser
->scope
= NULL_TREE
;
12587 parser
->qualifying_scope
= NULL_TREE
;
12588 parser
->object_scope
= NULL_TREE
;
12590 return nreverse (bases
);
12593 /* Parse a base-specifier.
12596 :: [opt] nested-name-specifier [opt] class-name
12597 virtual access-specifier [opt] :: [opt] nested-name-specifier
12599 access-specifier virtual [opt] :: [opt] nested-name-specifier
12602 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12603 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12604 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12605 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12608 cp_parser_base_specifier (cp_parser
* parser
)
12612 bool virtual_p
= false;
12613 bool duplicate_virtual_error_issued_p
= false;
12614 bool duplicate_access_error_issued_p
= false;
12615 bool class_scope_p
, template_p
;
12616 tree access
= access_default_node
;
12619 /* Process the optional `virtual' and `access-specifier'. */
12622 /* Peek at the next token. */
12623 token
= cp_lexer_peek_token (parser
->lexer
);
12624 /* Process `virtual'. */
12625 switch (token
->keyword
)
12628 /* If `virtual' appears more than once, issue an error. */
12629 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
12631 cp_parser_error (parser
,
12632 "`virtual' specified more than once in base-specified");
12633 duplicate_virtual_error_issued_p
= true;
12638 /* Consume the `virtual' token. */
12639 cp_lexer_consume_token (parser
->lexer
);
12644 case RID_PROTECTED
:
12646 /* If more than one access specifier appears, issue an
12648 if (access
!= access_default_node
12649 && !duplicate_access_error_issued_p
)
12651 cp_parser_error (parser
,
12652 "more than one access specifier in base-specified");
12653 duplicate_access_error_issued_p
= true;
12656 access
= ridpointers
[(int) token
->keyword
];
12658 /* Consume the access-specifier. */
12659 cp_lexer_consume_token (parser
->lexer
);
12669 /* Look for the optional `::' operator. */
12670 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
12671 /* Look for the nested-name-specifier. The simplest way to
12676 The keyword `typename' is not permitted in a base-specifier or
12677 mem-initializer; in these contexts a qualified name that
12678 depends on a template-parameter is implicitly assumed to be a
12681 is to pretend that we have seen the `typename' keyword at this
12683 cp_parser_nested_name_specifier_opt (parser
,
12684 /*typename_keyword_p=*/true,
12685 /*check_dependency_p=*/true,
12687 /*is_declaration=*/true);
12688 /* If the base class is given by a qualified name, assume that names
12689 we see are type names or templates, as appropriate. */
12690 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
12691 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
12693 /* Finally, look for the class-name. */
12694 type
= cp_parser_class_name (parser
,
12698 /*check_dependency_p=*/true,
12699 /*class_head_p=*/false,
12700 /*is_declaration=*/true);
12702 if (type
== error_mark_node
)
12703 return error_mark_node
;
12705 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
12708 /* Exception handling [gram.exception] */
12710 /* Parse an (optional) exception-specification.
12712 exception-specification:
12713 throw ( type-id-list [opt] )
12715 Returns a TREE_LIST representing the exception-specification. The
12716 TREE_VALUE of each node is a type. */
12719 cp_parser_exception_specification_opt (cp_parser
* parser
)
12724 /* Peek at the next token. */
12725 token
= cp_lexer_peek_token (parser
->lexer
);
12726 /* If it's not `throw', then there's no exception-specification. */
12727 if (!cp_parser_is_keyword (token
, RID_THROW
))
12730 /* Consume the `throw'. */
12731 cp_lexer_consume_token (parser
->lexer
);
12733 /* Look for the `('. */
12734 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12736 /* Peek at the next token. */
12737 token
= cp_lexer_peek_token (parser
->lexer
);
12738 /* If it's not a `)', then there is a type-id-list. */
12739 if (token
->type
!= CPP_CLOSE_PAREN
)
12741 const char *saved_message
;
12743 /* Types may not be defined in an exception-specification. */
12744 saved_message
= parser
->type_definition_forbidden_message
;
12745 parser
->type_definition_forbidden_message
12746 = "types may not be defined in an exception-specification";
12747 /* Parse the type-id-list. */
12748 type_id_list
= cp_parser_type_id_list (parser
);
12749 /* Restore the saved message. */
12750 parser
->type_definition_forbidden_message
= saved_message
;
12753 type_id_list
= empty_except_spec
;
12755 /* Look for the `)'. */
12756 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12758 return type_id_list
;
12761 /* Parse an (optional) type-id-list.
12765 type-id-list , type-id
12767 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12768 in the order that the types were presented. */
12771 cp_parser_type_id_list (cp_parser
* parser
)
12773 tree types
= NULL_TREE
;
12780 /* Get the next type-id. */
12781 type
= cp_parser_type_id (parser
);
12782 /* Add it to the list. */
12783 types
= add_exception_specifier (types
, type
, /*complain=*/1);
12784 /* Peek at the next token. */
12785 token
= cp_lexer_peek_token (parser
->lexer
);
12786 /* If it is not a `,', we are done. */
12787 if (token
->type
!= CPP_COMMA
)
12789 /* Consume the `,'. */
12790 cp_lexer_consume_token (parser
->lexer
);
12793 return nreverse (types
);
12796 /* Parse a try-block.
12799 try compound-statement handler-seq */
12802 cp_parser_try_block (cp_parser
* parser
)
12806 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
12807 try_block
= begin_try_block ();
12808 cp_parser_compound_statement (parser
, false);
12809 finish_try_block (try_block
);
12810 cp_parser_handler_seq (parser
);
12811 finish_handler_sequence (try_block
);
12816 /* Parse a function-try-block.
12818 function-try-block:
12819 try ctor-initializer [opt] function-body handler-seq */
12822 cp_parser_function_try_block (cp_parser
* parser
)
12825 bool ctor_initializer_p
;
12827 /* Look for the `try' keyword. */
12828 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
12830 /* Let the rest of the front-end know where we are. */
12831 try_block
= begin_function_try_block ();
12832 /* Parse the function-body. */
12834 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
12835 /* We're done with the `try' part. */
12836 finish_function_try_block (try_block
);
12837 /* Parse the handlers. */
12838 cp_parser_handler_seq (parser
);
12839 /* We're done with the handlers. */
12840 finish_function_handler_sequence (try_block
);
12842 return ctor_initializer_p
;
12845 /* Parse a handler-seq.
12848 handler handler-seq [opt] */
12851 cp_parser_handler_seq (cp_parser
* parser
)
12857 /* Parse the handler. */
12858 cp_parser_handler (parser
);
12859 /* Peek at the next token. */
12860 token
= cp_lexer_peek_token (parser
->lexer
);
12861 /* If it's not `catch' then there are no more handlers. */
12862 if (!cp_parser_is_keyword (token
, RID_CATCH
))
12867 /* Parse a handler.
12870 catch ( exception-declaration ) compound-statement */
12873 cp_parser_handler (cp_parser
* parser
)
12878 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
12879 handler
= begin_handler ();
12880 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12881 declaration
= cp_parser_exception_declaration (parser
);
12882 finish_handler_parms (declaration
, handler
);
12883 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12884 cp_parser_compound_statement (parser
, false);
12885 finish_handler (handler
);
12888 /* Parse an exception-declaration.
12890 exception-declaration:
12891 type-specifier-seq declarator
12892 type-specifier-seq abstract-declarator
12896 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12897 ellipsis variant is used. */
12900 cp_parser_exception_declaration (cp_parser
* parser
)
12902 tree type_specifiers
;
12904 const char *saved_message
;
12906 /* If it's an ellipsis, it's easy to handle. */
12907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12909 /* Consume the `...' token. */
12910 cp_lexer_consume_token (parser
->lexer
);
12914 /* Types may not be defined in exception-declarations. */
12915 saved_message
= parser
->type_definition_forbidden_message
;
12916 parser
->type_definition_forbidden_message
12917 = "types may not be defined in exception-declarations";
12919 /* Parse the type-specifier-seq. */
12920 type_specifiers
= cp_parser_type_specifier_seq (parser
);
12921 /* If it's a `)', then there is no declarator. */
12922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
12923 declarator
= NULL_TREE
;
12925 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
12926 /*ctor_dtor_or_conv_p=*/NULL
,
12927 /*parenthesized_p=*/NULL
);
12929 /* Restore the saved message. */
12930 parser
->type_definition_forbidden_message
= saved_message
;
12932 return start_handler_parms (type_specifiers
, declarator
);
12935 /* Parse a throw-expression.
12938 throw assignment-expression [opt]
12940 Returns a THROW_EXPR representing the throw-expression. */
12943 cp_parser_throw_expression (cp_parser
* parser
)
12948 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
12949 token
= cp_lexer_peek_token (parser
->lexer
);
12950 /* Figure out whether or not there is an assignment-expression
12951 following the "throw" keyword. */
12952 if (token
->type
== CPP_COMMA
12953 || token
->type
== CPP_SEMICOLON
12954 || token
->type
== CPP_CLOSE_PAREN
12955 || token
->type
== CPP_CLOSE_SQUARE
12956 || token
->type
== CPP_CLOSE_BRACE
12957 || token
->type
== CPP_COLON
)
12958 expression
= NULL_TREE
;
12960 expression
= cp_parser_assignment_expression (parser
);
12962 return build_throw (expression
);
12965 /* GNU Extensions */
12967 /* Parse an (optional) asm-specification.
12970 asm ( string-literal )
12972 If the asm-specification is present, returns a STRING_CST
12973 corresponding to the string-literal. Otherwise, returns
12977 cp_parser_asm_specification_opt (cp_parser
* parser
)
12980 tree asm_specification
;
12982 /* Peek at the next token. */
12983 token
= cp_lexer_peek_token (parser
->lexer
);
12984 /* If the next token isn't the `asm' keyword, then there's no
12985 asm-specification. */
12986 if (!cp_parser_is_keyword (token
, RID_ASM
))
12989 /* Consume the `asm' token. */
12990 cp_lexer_consume_token (parser
->lexer
);
12991 /* Look for the `('. */
12992 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12994 /* Look for the string-literal. */
12995 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12997 asm_specification
= token
->value
;
12999 asm_specification
= NULL_TREE
;
13001 /* Look for the `)'. */
13002 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13004 return asm_specification
;
13007 /* Parse an asm-operand-list.
13011 asm-operand-list , asm-operand
13014 string-literal ( expression )
13015 [ string-literal ] string-literal ( expression )
13017 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13018 each node is the expression. The TREE_PURPOSE is itself a
13019 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13020 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13021 is a STRING_CST for the string literal before the parenthesis. */
13024 cp_parser_asm_operand_list (cp_parser
* parser
)
13026 tree asm_operands
= NULL_TREE
;
13030 tree string_literal
;
13035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13037 /* Consume the `[' token. */
13038 cp_lexer_consume_token (parser
->lexer
);
13039 /* Read the operand name. */
13040 name
= cp_parser_identifier (parser
);
13041 if (name
!= error_mark_node
)
13042 name
= build_string (IDENTIFIER_LENGTH (name
),
13043 IDENTIFIER_POINTER (name
));
13044 /* Look for the closing `]'. */
13045 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13049 /* Look for the string-literal. */
13050 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13051 string_literal
= token
? token
->value
: error_mark_node
;
13052 /* Look for the `('. */
13053 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13054 /* Parse the expression. */
13055 expression
= cp_parser_expression (parser
);
13056 /* Look for the `)'. */
13057 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13058 /* Add this operand to the list. */
13059 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
13062 /* If the next token is not a `,', there are no more
13064 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13066 /* Consume the `,'. */
13067 cp_lexer_consume_token (parser
->lexer
);
13070 return nreverse (asm_operands
);
13073 /* Parse an asm-clobber-list.
13077 asm-clobber-list , string-literal
13079 Returns a TREE_LIST, indicating the clobbers in the order that they
13080 appeared. The TREE_VALUE of each node is a STRING_CST. */
13083 cp_parser_asm_clobber_list (cp_parser
* parser
)
13085 tree clobbers
= NULL_TREE
;
13090 tree string_literal
;
13092 /* Look for the string literal. */
13093 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13094 string_literal
= token
? token
->value
: error_mark_node
;
13095 /* Add it to the list. */
13096 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13097 /* If the next token is not a `,', then the list is
13099 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13101 /* Consume the `,' token. */
13102 cp_lexer_consume_token (parser
->lexer
);
13108 /* Parse an (optional) series of attributes.
13111 attributes attribute
13114 __attribute__ (( attribute-list [opt] ))
13116 The return value is as for cp_parser_attribute_list. */
13119 cp_parser_attributes_opt (cp_parser
* parser
)
13121 tree attributes
= NULL_TREE
;
13126 tree attribute_list
;
13128 /* Peek at the next token. */
13129 token
= cp_lexer_peek_token (parser
->lexer
);
13130 /* If it's not `__attribute__', then we're done. */
13131 if (token
->keyword
!= RID_ATTRIBUTE
)
13134 /* Consume the `__attribute__' keyword. */
13135 cp_lexer_consume_token (parser
->lexer
);
13136 /* Look for the two `(' tokens. */
13137 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13138 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13140 /* Peek at the next token. */
13141 token
= cp_lexer_peek_token (parser
->lexer
);
13142 if (token
->type
!= CPP_CLOSE_PAREN
)
13143 /* Parse the attribute-list. */
13144 attribute_list
= cp_parser_attribute_list (parser
);
13146 /* If the next token is a `)', then there is no attribute
13148 attribute_list
= NULL
;
13150 /* Look for the two `)' tokens. */
13151 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13152 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13154 /* Add these new attributes to the list. */
13155 attributes
= chainon (attributes
, attribute_list
);
13161 /* Parse an attribute-list.
13165 attribute-list , attribute
13169 identifier ( identifier )
13170 identifier ( identifier , expression-list )
13171 identifier ( expression-list )
13173 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13174 TREE_PURPOSE of each node is the identifier indicating which
13175 attribute is in use. The TREE_VALUE represents the arguments, if
13179 cp_parser_attribute_list (cp_parser
* parser
)
13181 tree attribute_list
= NULL_TREE
;
13189 /* Look for the identifier. We also allow keywords here; for
13190 example `__attribute__ ((const))' is legal. */
13191 token
= cp_lexer_peek_token (parser
->lexer
);
13192 if (token
->type
!= CPP_NAME
13193 && token
->type
!= CPP_KEYWORD
)
13194 return error_mark_node
;
13195 /* Consume the token. */
13196 token
= cp_lexer_consume_token (parser
->lexer
);
13198 /* Save away the identifier that indicates which attribute this is. */
13199 identifier
= token
->value
;
13200 attribute
= build_tree_list (identifier
, NULL_TREE
);
13202 /* Peek at the next token. */
13203 token
= cp_lexer_peek_token (parser
->lexer
);
13204 /* If it's an `(', then parse the attribute arguments. */
13205 if (token
->type
== CPP_OPEN_PAREN
)
13209 arguments
= (cp_parser_parenthesized_expression_list
13210 (parser
, true, /*non_constant_p=*/NULL
));
13211 /* Save the identifier and arguments away. */
13212 TREE_VALUE (attribute
) = arguments
;
13215 /* Add this attribute to the list. */
13216 TREE_CHAIN (attribute
) = attribute_list
;
13217 attribute_list
= attribute
;
13219 /* Now, look for more attributes. */
13220 token
= cp_lexer_peek_token (parser
->lexer
);
13221 /* If the next token isn't a `,', we're done. */
13222 if (token
->type
!= CPP_COMMA
)
13225 /* Consume the comma and keep going. */
13226 cp_lexer_consume_token (parser
->lexer
);
13229 /* We built up the list in reverse order. */
13230 return nreverse (attribute_list
);
13233 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13234 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13235 current value of the PEDANTIC flag, regardless of whether or not
13236 the `__extension__' keyword is present. The caller is responsible
13237 for restoring the value of the PEDANTIC flag. */
13240 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
13242 /* Save the old value of the PEDANTIC flag. */
13243 *saved_pedantic
= pedantic
;
13245 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
13247 /* Consume the `__extension__' token. */
13248 cp_lexer_consume_token (parser
->lexer
);
13249 /* We're not being pedantic while the `__extension__' keyword is
13259 /* Parse a label declaration.
13262 __label__ label-declarator-seq ;
13264 label-declarator-seq:
13265 identifier , label-declarator-seq
13269 cp_parser_label_declaration (cp_parser
* parser
)
13271 /* Look for the `__label__' keyword. */
13272 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
13278 /* Look for an identifier. */
13279 identifier
= cp_parser_identifier (parser
);
13280 /* Declare it as a lobel. */
13281 finish_label_decl (identifier
);
13282 /* If the next token is a `;', stop. */
13283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13285 /* Look for the `,' separating the label declarations. */
13286 cp_parser_require (parser
, CPP_COMMA
, "`,'");
13289 /* Look for the final `;'. */
13290 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13293 /* Support Functions */
13295 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13296 NAME should have one of the representations used for an
13297 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13298 is returned. If PARSER->SCOPE is a dependent type, then a
13299 SCOPE_REF is returned.
13301 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13302 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13303 was formed. Abstractly, such entities should not be passed to this
13304 function, because they do not need to be looked up, but it is
13305 simpler to check for this special case here, rather than at the
13308 In cases not explicitly covered above, this function returns a
13309 DECL, OVERLOAD, or baselink representing the result of the lookup.
13310 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13313 If IS_TYPE is TRUE, bindings that do not refer to types are
13316 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13319 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13322 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13326 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
13327 bool is_type
, bool is_template
, bool is_namespace
,
13328 bool check_dependency
)
13331 tree object_type
= parser
->context
->object_type
;
13333 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13334 no longer valid. Note that if we are parsing tentatively, and
13335 the parse fails, OBJECT_TYPE will be automatically restored. */
13336 parser
->context
->object_type
= NULL_TREE
;
13338 if (name
== error_mark_node
)
13339 return error_mark_node
;
13341 /* A template-id has already been resolved; there is no lookup to
13343 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13345 if (BASELINK_P (name
))
13347 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
13348 == TEMPLATE_ID_EXPR
),
13353 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13354 it should already have been checked to make sure that the name
13355 used matches the type being destroyed. */
13356 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13360 /* Figure out to which type this destructor applies. */
13362 type
= parser
->scope
;
13363 else if (object_type
)
13364 type
= object_type
;
13366 type
= current_class_type
;
13367 /* If that's not a class type, there is no destructor. */
13368 if (!type
|| !CLASS_TYPE_P (type
))
13369 return error_mark_node
;
13370 /* If it was a class type, return the destructor. */
13371 return CLASSTYPE_DESTRUCTORS (type
);
13374 /* By this point, the NAME should be an ordinary identifier. If
13375 the id-expression was a qualified name, the qualifying scope is
13376 stored in PARSER->SCOPE at this point. */
13377 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
13380 /* Perform the lookup. */
13385 if (parser
->scope
== error_mark_node
)
13386 return error_mark_node
;
13388 /* If the SCOPE is dependent, the lookup must be deferred until
13389 the template is instantiated -- unless we are explicitly
13390 looking up names in uninstantiated templates. Even then, we
13391 cannot look up the name if the scope is not a class type; it
13392 might, for example, be a template type parameter. */
13393 dependent_p
= (TYPE_P (parser
->scope
)
13394 && !(parser
->in_declarator_p
13395 && currently_open_class (parser
->scope
))
13396 && dependent_type_p (parser
->scope
));
13397 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
13401 /* The resolution to Core Issue 180 says that `struct A::B'
13402 should be considered a type-name, even if `A' is
13404 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
13407 else if (is_template
)
13408 decl
= make_unbound_class_template (parser
->scope
,
13412 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
13416 /* If PARSER->SCOPE is a dependent type, then it must be a
13417 class type, and we must not be checking dependencies;
13418 otherwise, we would have processed this lookup above. So
13419 that PARSER->SCOPE is not considered a dependent base by
13420 lookup_member, we must enter the scope here. */
13422 push_scope (parser
->scope
);
13423 /* If the PARSER->SCOPE is a a template specialization, it
13424 may be instantiated during name lookup. In that case,
13425 errors may be issued. Even if we rollback the current
13426 tentative parse, those errors are valid. */
13427 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
13428 /*complain=*/true);
13430 pop_scope (parser
->scope
);
13432 parser
->qualifying_scope
= parser
->scope
;
13433 parser
->object_scope
= NULL_TREE
;
13435 else if (object_type
)
13437 tree object_decl
= NULL_TREE
;
13438 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13439 OBJECT_TYPE is not a class. */
13440 if (CLASS_TYPE_P (object_type
))
13441 /* If the OBJECT_TYPE is a template specialization, it may
13442 be instantiated during name lookup. In that case, errors
13443 may be issued. Even if we rollback the current tentative
13444 parse, those errors are valid. */
13445 object_decl
= lookup_member (object_type
,
13447 /*protect=*/0, is_type
);
13448 /* Look it up in the enclosing context, too. */
13449 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13452 parser
->object_scope
= object_type
;
13453 parser
->qualifying_scope
= NULL_TREE
;
13455 decl
= object_decl
;
13459 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13462 parser
->qualifying_scope
= NULL_TREE
;
13463 parser
->object_scope
= NULL_TREE
;
13466 /* If the lookup failed, let our caller know. */
13468 || decl
== error_mark_node
13469 || (TREE_CODE (decl
) == FUNCTION_DECL
13470 && DECL_ANTICIPATED (decl
)))
13471 return error_mark_node
;
13473 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13474 if (TREE_CODE (decl
) == TREE_LIST
)
13476 /* The error message we have to print is too complicated for
13477 cp_parser_error, so we incorporate its actions directly. */
13478 if (!cp_parser_simulate_error (parser
))
13480 error ("reference to `%D' is ambiguous", name
);
13481 print_candidates (decl
);
13483 return error_mark_node
;
13486 my_friendly_assert (DECL_P (decl
)
13487 || TREE_CODE (decl
) == OVERLOAD
13488 || TREE_CODE (decl
) == SCOPE_REF
13489 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
13490 || BASELINK_P (decl
),
13493 /* If we have resolved the name of a member declaration, check to
13494 see if the declaration is accessible. When the name resolves to
13495 set of overloaded functions, accessibility is checked when
13496 overload resolution is done.
13498 During an explicit instantiation, access is not checked at all,
13499 as per [temp.explicit]. */
13501 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
13506 /* Like cp_parser_lookup_name, but for use in the typical case where
13507 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13508 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13511 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13513 return cp_parser_lookup_name (parser
, name
,
13515 /*is_template=*/false,
13516 /*is_namespace=*/false,
13517 /*check_dependency=*/true);
13520 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13521 the current context, return the TYPE_DECL. If TAG_NAME_P is
13522 true, the DECL indicates the class being defined in a class-head,
13523 or declared in an elaborated-type-specifier.
13525 Otherwise, return DECL. */
13528 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13530 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13531 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13534 template <typename T> struct B;
13537 template <typename T> struct A::B {};
13539 Similarly, in a elaborated-type-specifier:
13541 namespace N { struct X{}; }
13544 template <typename T> friend struct N::X;
13547 However, if the DECL refers to a class type, and we are in
13548 the scope of the class, then the name lookup automatically
13549 finds the TYPE_DECL created by build_self_reference rather
13550 than a TEMPLATE_DECL. For example, in:
13552 template <class T> struct S {
13556 there is no need to handle such case. */
13558 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13559 return DECL_TEMPLATE_RESULT (decl
);
13564 /* If too many, or too few, template-parameter lists apply to the
13565 declarator, issue an error message. Returns TRUE if all went well,
13566 and FALSE otherwise. */
13569 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13572 unsigned num_templates
;
13574 /* We haven't seen any classes that involve template parameters yet. */
13577 switch (TREE_CODE (declarator
))
13584 tree main_declarator
= TREE_OPERAND (declarator
, 0);
13586 cp_parser_check_declarator_template_parameters (parser
,
13595 scope
= TREE_OPERAND (declarator
, 0);
13596 member
= TREE_OPERAND (declarator
, 1);
13598 /* If this is a pointer-to-member, then we are not interested
13599 in the SCOPE, because it does not qualify the thing that is
13601 if (TREE_CODE (member
) == INDIRECT_REF
)
13602 return (cp_parser_check_declarator_template_parameters
13605 while (scope
&& CLASS_TYPE_P (scope
))
13607 /* You're supposed to have one `template <...>'
13608 for every template class, but you don't need one
13609 for a full specialization. For example:
13611 template <class T> struct S{};
13612 template <> struct S<int> { void f(); };
13613 void S<int>::f () {}
13615 is correct; there shouldn't be a `template <>' for
13616 the definition of `S<int>::f'. */
13617 if (CLASSTYPE_TEMPLATE_INFO (scope
)
13618 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
13619 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
13620 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
13623 scope
= TYPE_CONTEXT (scope
);
13627 /* Fall through. */
13630 /* If the DECLARATOR has the form `X<y>' then it uses one
13631 additional level of template parameters. */
13632 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
13635 return cp_parser_check_template_parameters (parser
,
13640 /* NUM_TEMPLATES were used in the current declaration. If that is
13641 invalid, return FALSE and issue an error messages. Otherwise,
13645 cp_parser_check_template_parameters (cp_parser
* parser
,
13646 unsigned num_templates
)
13648 /* If there are more template classes than parameter lists, we have
13651 template <class T> void S<T>::R<T>::f (); */
13652 if (parser
->num_template_parameter_lists
< num_templates
)
13654 error ("too few template-parameter-lists");
13657 /* If there are the same number of template classes and parameter
13658 lists, that's OK. */
13659 if (parser
->num_template_parameter_lists
== num_templates
)
13661 /* If there are more, but only one more, then we are referring to a
13662 member template. That's OK too. */
13663 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
13665 /* Otherwise, there are too many template parameter lists. We have
13668 template <class T> template <class U> void S::f(); */
13669 error ("too many template-parameter-lists");
13673 /* Parse a binary-expression of the general form:
13677 binary-expression <token> <expr>
13679 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13680 to parser the <expr>s. If the first production is used, then the
13681 value returned by FN is returned directly. Otherwise, a node with
13682 the indicated EXPR_TYPE is returned, with operands corresponding to
13683 the two sub-expressions. */
13686 cp_parser_binary_expression (cp_parser
* parser
,
13687 const cp_parser_token_tree_map token_tree_map
,
13688 cp_parser_expression_fn fn
)
13692 /* Parse the first expression. */
13693 lhs
= (*fn
) (parser
);
13694 /* Now, look for more expressions. */
13698 const cp_parser_token_tree_map_node
*map_node
;
13701 /* Peek at the next token. */
13702 token
= cp_lexer_peek_token (parser
->lexer
);
13703 /* If the token is `>', and that's not an operator at the
13704 moment, then we're done. */
13705 if (token
->type
== CPP_GREATER
13706 && !parser
->greater_than_is_operator_p
)
13708 /* If we find one of the tokens we want, build the corresponding
13709 tree representation. */
13710 for (map_node
= token_tree_map
;
13711 map_node
->token_type
!= CPP_EOF
;
13713 if (map_node
->token_type
== token
->type
)
13715 /* Consume the operator token. */
13716 cp_lexer_consume_token (parser
->lexer
);
13717 /* Parse the right-hand side of the expression. */
13718 rhs
= (*fn
) (parser
);
13719 /* Build the binary tree node. */
13720 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
);
13724 /* If the token wasn't one of the ones we want, we're done. */
13725 if (map_node
->token_type
== CPP_EOF
)
13732 /* Parse an optional `::' token indicating that the following name is
13733 from the global namespace. If so, PARSER->SCOPE is set to the
13734 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13735 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13736 Returns the new value of PARSER->SCOPE, if the `::' token is
13737 present, and NULL_TREE otherwise. */
13740 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
13744 /* Peek at the next token. */
13745 token
= cp_lexer_peek_token (parser
->lexer
);
13746 /* If we're looking at a `::' token then we're starting from the
13747 global namespace, not our current location. */
13748 if (token
->type
== CPP_SCOPE
)
13750 /* Consume the `::' token. */
13751 cp_lexer_consume_token (parser
->lexer
);
13752 /* Set the SCOPE so that we know where to start the lookup. */
13753 parser
->scope
= global_namespace
;
13754 parser
->qualifying_scope
= global_namespace
;
13755 parser
->object_scope
= NULL_TREE
;
13757 return parser
->scope
;
13759 else if (!current_scope_valid_p
)
13761 parser
->scope
= NULL_TREE
;
13762 parser
->qualifying_scope
= NULL_TREE
;
13763 parser
->object_scope
= NULL_TREE
;
13769 /* Returns TRUE if the upcoming token sequence is the start of a
13770 constructor declarator. If FRIEND_P is true, the declarator is
13771 preceded by the `friend' specifier. */
13774 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
13776 bool constructor_p
;
13777 tree type_decl
= NULL_TREE
;
13778 bool nested_name_p
;
13779 cp_token
*next_token
;
13781 /* The common case is that this is not a constructor declarator, so
13782 try to avoid doing lots of work if at all possible. It's not
13783 valid declare a constructor at function scope. */
13784 if (at_function_scope_p ())
13786 /* And only certain tokens can begin a constructor declarator. */
13787 next_token
= cp_lexer_peek_token (parser
->lexer
);
13788 if (next_token
->type
!= CPP_NAME
13789 && next_token
->type
!= CPP_SCOPE
13790 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
13791 && next_token
->type
!= CPP_TEMPLATE_ID
)
13794 /* Parse tentatively; we are going to roll back all of the tokens
13796 cp_parser_parse_tentatively (parser
);
13797 /* Assume that we are looking at a constructor declarator. */
13798 constructor_p
= true;
13800 /* Look for the optional `::' operator. */
13801 cp_parser_global_scope_opt (parser
,
13802 /*current_scope_valid_p=*/false);
13803 /* Look for the nested-name-specifier. */
13805 = (cp_parser_nested_name_specifier_opt (parser
,
13806 /*typename_keyword_p=*/false,
13807 /*check_dependency_p=*/false,
13809 /*is_declaration=*/false)
13811 /* Outside of a class-specifier, there must be a
13812 nested-name-specifier. */
13813 if (!nested_name_p
&&
13814 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
13816 constructor_p
= false;
13817 /* If we still think that this might be a constructor-declarator,
13818 look for a class-name. */
13823 template <typename T> struct S { S(); };
13824 template <typename T> S<T>::S ();
13826 we must recognize that the nested `S' names a class.
13829 template <typename T> S<T>::S<T> ();
13831 we must recognize that the nested `S' names a template. */
13832 type_decl
= cp_parser_class_name (parser
,
13833 /*typename_keyword_p=*/false,
13834 /*template_keyword_p=*/false,
13836 /*check_dependency_p=*/false,
13837 /*class_head_p=*/false,
13838 /*is_declaration=*/false);
13839 /* If there was no class-name, then this is not a constructor. */
13840 constructor_p
= !cp_parser_error_occurred (parser
);
13843 /* If we're still considering a constructor, we have to see a `(',
13844 to begin the parameter-declaration-clause, followed by either a
13845 `)', an `...', or a decl-specifier. We need to check for a
13846 type-specifier to avoid being fooled into thinking that:
13850 is a constructor. (It is actually a function named `f' that
13851 takes one parameter (of type `int') and returns a value of type
13854 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
13856 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
13857 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
13858 && !cp_parser_storage_class_specifier_opt (parser
))
13861 unsigned saved_num_template_parameter_lists
;
13863 /* Names appearing in the type-specifier should be looked up
13864 in the scope of the class. */
13865 if (current_class_type
)
13869 type
= TREE_TYPE (type_decl
);
13870 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13872 type
= resolve_typename_type (type
,
13873 /*only_current_p=*/false);
13874 if (type
== error_mark_node
)
13876 cp_parser_abort_tentative_parse (parser
);
13883 /* Inside the constructor parameter list, surrounding
13884 template-parameter-lists do not apply. */
13885 saved_num_template_parameter_lists
13886 = parser
->num_template_parameter_lists
;
13887 parser
->num_template_parameter_lists
= 0;
13889 /* Look for the type-specifier. */
13890 cp_parser_type_specifier (parser
,
13891 CP_PARSER_FLAGS_NONE
,
13892 /*is_friend=*/false,
13893 /*is_declarator=*/true,
13894 /*declares_class_or_enum=*/NULL
,
13895 /*is_cv_qualifier=*/NULL
);
13897 parser
->num_template_parameter_lists
13898 = saved_num_template_parameter_lists
;
13900 /* Leave the scope of the class. */
13904 constructor_p
= !cp_parser_error_occurred (parser
);
13908 constructor_p
= false;
13909 /* We did not really want to consume any tokens. */
13910 cp_parser_abort_tentative_parse (parser
);
13912 return constructor_p
;
13915 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13916 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13917 they must be performed once we are in the scope of the function.
13919 Returns the function defined. */
13922 cp_parser_function_definition_from_specifiers_and_declarator
13923 (cp_parser
* parser
,
13924 tree decl_specifiers
,
13931 /* Begin the function-definition. */
13932 success_p
= begin_function_definition (decl_specifiers
,
13936 /* If there were names looked up in the decl-specifier-seq that we
13937 did not check, check them now. We must wait until we are in the
13938 scope of the function to perform the checks, since the function
13939 might be a friend. */
13940 perform_deferred_access_checks ();
13944 /* If begin_function_definition didn't like the definition, skip
13945 the entire function. */
13946 error ("invalid function declaration");
13947 cp_parser_skip_to_end_of_block_or_statement (parser
);
13948 fn
= error_mark_node
;
13951 fn
= cp_parser_function_definition_after_declarator (parser
,
13952 /*inline_p=*/false);
13957 /* Parse the part of a function-definition that follows the
13958 declarator. INLINE_P is TRUE iff this function is an inline
13959 function defined with a class-specifier.
13961 Returns the function defined. */
13964 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
13968 bool ctor_initializer_p
= false;
13969 bool saved_in_unbraced_linkage_specification_p
;
13970 unsigned saved_num_template_parameter_lists
;
13972 /* If the next token is `return', then the code may be trying to
13973 make use of the "named return value" extension that G++ used to
13975 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
13977 /* Consume the `return' keyword. */
13978 cp_lexer_consume_token (parser
->lexer
);
13979 /* Look for the identifier that indicates what value is to be
13981 cp_parser_identifier (parser
);
13982 /* Issue an error message. */
13983 error ("named return values are no longer supported");
13984 /* Skip tokens until we reach the start of the function body. */
13985 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
13986 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
13987 cp_lexer_consume_token (parser
->lexer
);
13989 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13990 anything declared inside `f'. */
13991 saved_in_unbraced_linkage_specification_p
13992 = parser
->in_unbraced_linkage_specification_p
;
13993 parser
->in_unbraced_linkage_specification_p
= false;
13994 /* Inside the function, surrounding template-parameter-lists do not
13996 saved_num_template_parameter_lists
13997 = parser
->num_template_parameter_lists
;
13998 parser
->num_template_parameter_lists
= 0;
13999 /* If the next token is `try', then we are looking at a
14000 function-try-block. */
14001 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14002 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14003 /* A function-try-block includes the function-body, so we only do
14004 this next part if we're not processing a function-try-block. */
14007 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14009 /* Finish the function. */
14010 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14011 (inline_p
? 2 : 0));
14012 /* Generate code for it, if necessary. */
14013 expand_or_defer_fn (fn
);
14014 /* Restore the saved values. */
14015 parser
->in_unbraced_linkage_specification_p
14016 = saved_in_unbraced_linkage_specification_p
;
14017 parser
->num_template_parameter_lists
14018 = saved_num_template_parameter_lists
;
14023 /* Parse a template-declaration, assuming that the `export' (and
14024 `extern') keywords, if present, has already been scanned. MEMBER_P
14025 is as for cp_parser_template_declaration. */
14028 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14030 tree decl
= NULL_TREE
;
14031 tree parameter_list
;
14032 bool friend_p
= false;
14034 /* Look for the `template' keyword. */
14035 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14039 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14042 /* If the next token is `>', then we have an invalid
14043 specialization. Rather than complain about an invalid template
14044 parameter, issue an error message here. */
14045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14047 cp_parser_error (parser
, "invalid explicit specialization");
14048 begin_specialization ();
14049 parameter_list
= NULL_TREE
;
14053 /* Parse the template parameters. */
14054 begin_template_parm_list ();
14055 parameter_list
= cp_parser_template_parameter_list (parser
);
14056 parameter_list
= end_template_parm_list (parameter_list
);
14059 /* Look for the `>'. */
14060 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14061 /* We just processed one more parameter list. */
14062 ++parser
->num_template_parameter_lists
;
14063 /* If the next token is `template', there are more template
14065 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14067 cp_parser_template_declaration_after_export (parser
, member_p
);
14070 decl
= cp_parser_single_declaration (parser
,
14074 /* If this is a member template declaration, let the front
14076 if (member_p
&& !friend_p
&& decl
)
14078 if (TREE_CODE (decl
) == TYPE_DECL
)
14079 cp_parser_check_access_in_redeclaration (decl
);
14081 decl
= finish_member_template_decl (decl
);
14083 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14084 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14085 /*complain=*/true);
14087 /* We are done with the current parameter list. */
14088 --parser
->num_template_parameter_lists
;
14091 finish_template_decl (parameter_list
);
14093 /* Register member declarations. */
14094 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14095 finish_member_declaration (decl
);
14097 /* If DECL is a function template, we must return to parse it later.
14098 (Even though there is no definition, there might be default
14099 arguments that need handling.) */
14100 if (member_p
&& decl
14101 && (TREE_CODE (decl
) == FUNCTION_DECL
14102 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14103 TREE_VALUE (parser
->unparsed_functions_queues
)
14104 = tree_cons (NULL_TREE
, decl
,
14105 TREE_VALUE (parser
->unparsed_functions_queues
));
14108 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14109 `function-definition' sequence. MEMBER_P is true, this declaration
14110 appears in a class scope.
14112 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14113 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14116 cp_parser_single_declaration (cp_parser
* parser
,
14120 int declares_class_or_enum
;
14121 tree decl
= NULL_TREE
;
14122 tree decl_specifiers
;
14124 bool function_definition_p
= false;
14126 /* Defer access checks until we know what is being declared. */
14127 push_deferring_access_checks (dk_deferred
);
14129 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14132 = cp_parser_decl_specifier_seq (parser
,
14133 CP_PARSER_FLAGS_OPTIONAL
,
14135 &declares_class_or_enum
);
14137 *friend_p
= cp_parser_friend_p (decl_specifiers
);
14138 /* Gather up the access checks that occurred the
14139 decl-specifier-seq. */
14140 stop_deferring_access_checks ();
14142 /* Check for the declaration of a template class. */
14143 if (declares_class_or_enum
)
14145 if (cp_parser_declares_only_class_p (parser
))
14147 decl
= shadow_tag (decl_specifiers
);
14149 decl
= TYPE_NAME (decl
);
14151 decl
= error_mark_node
;
14156 /* If it's not a template class, try for a template function. If
14157 the next token is a `;', then this declaration does not declare
14158 anything. But, if there were errors in the decl-specifiers, then
14159 the error might well have come from an attempted class-specifier.
14160 In that case, there's no need to warn about a missing declarator. */
14162 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
14163 || !value_member (error_mark_node
, decl_specifiers
)))
14164 decl
= cp_parser_init_declarator (parser
,
14167 /*function_definition_allowed_p=*/true,
14169 declares_class_or_enum
,
14170 &function_definition_p
);
14172 pop_deferring_access_checks ();
14174 /* Clear any current qualification; whatever comes next is the start
14175 of something new. */
14176 parser
->scope
= NULL_TREE
;
14177 parser
->qualifying_scope
= NULL_TREE
;
14178 parser
->object_scope
= NULL_TREE
;
14179 /* Look for a trailing `;' after the declaration. */
14180 if (!function_definition_p
14181 && !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
14182 cp_parser_skip_to_end_of_block_or_statement (parser
);
14187 /* Parse a cast-expression that is not the operand of a unary "&". */
14190 cp_parser_simple_cast_expression (cp_parser
*parser
)
14192 return cp_parser_cast_expression (parser
, /*address_p=*/false);
14195 /* Parse a functional cast to TYPE. Returns an expression
14196 representing the cast. */
14199 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14201 tree expression_list
;
14204 = cp_parser_parenthesized_expression_list (parser
, false,
14205 /*non_constant_p=*/NULL
);
14207 return build_functional_cast (type
, expression_list
);
14210 /* Save the tokens that make up the body of a member function defined
14211 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14212 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14213 specifiers applied to the declaration. Returns the FUNCTION_DECL
14214 for the member function. */
14217 cp_parser_save_member_function_body (cp_parser
* parser
,
14218 tree decl_specifiers
,
14222 cp_token_cache
*cache
;
14225 /* Create the function-declaration. */
14226 fn
= start_method (decl_specifiers
, declarator
, attributes
);
14227 /* If something went badly wrong, bail out now. */
14228 if (fn
== error_mark_node
)
14230 /* If there's a function-body, skip it. */
14231 if (cp_parser_token_starts_function_definition_p
14232 (cp_lexer_peek_token (parser
->lexer
)))
14233 cp_parser_skip_to_end_of_block_or_statement (parser
);
14234 return error_mark_node
;
14237 /* Remember it, if there default args to post process. */
14238 cp_parser_save_default_args (parser
, fn
);
14240 /* Create a token cache. */
14241 cache
= cp_token_cache_new ();
14242 /* Save away the tokens that make up the body of the
14244 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14245 /* Handle function try blocks. */
14246 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
14247 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14249 /* Save away the inline definition; we will process it when the
14250 class is complete. */
14251 DECL_PENDING_INLINE_INFO (fn
) = cache
;
14252 DECL_PENDING_INLINE_P (fn
) = 1;
14254 /* We need to know that this was defined in the class, so that
14255 friend templates are handled correctly. */
14256 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
14258 /* We're done with the inline definition. */
14259 finish_method (fn
);
14261 /* Add FN to the queue of functions to be parsed later. */
14262 TREE_VALUE (parser
->unparsed_functions_queues
)
14263 = tree_cons (NULL_TREE
, fn
,
14264 TREE_VALUE (parser
->unparsed_functions_queues
));
14269 /* Parse a template-argument-list, as well as the trailing ">" (but
14270 not the opening ">"). See cp_parser_template_argument_list for the
14274 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
14278 tree saved_qualifying_scope
;
14279 tree saved_object_scope
;
14280 bool saved_greater_than_is_operator_p
;
14284 When parsing a template-id, the first non-nested `>' is taken as
14285 the end of the template-argument-list rather than a greater-than
14287 saved_greater_than_is_operator_p
14288 = parser
->greater_than_is_operator_p
;
14289 parser
->greater_than_is_operator_p
= false;
14290 /* Parsing the argument list may modify SCOPE, so we save it
14292 saved_scope
= parser
->scope
;
14293 saved_qualifying_scope
= parser
->qualifying_scope
;
14294 saved_object_scope
= parser
->object_scope
;
14295 /* Parse the template-argument-list itself. */
14296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14297 arguments
= NULL_TREE
;
14299 arguments
= cp_parser_template_argument_list (parser
);
14300 /* Look for the `>' that ends the template-argument-list. If we find
14301 a '>>' instead, it's probably just a typo. */
14302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14304 if (!saved_greater_than_is_operator_p
)
14306 /* If we're in a nested template argument list, the '>>' has to be
14307 a typo for '> >'. We emit the error message, but we continue
14308 parsing and we push a '>' as next token, so that the argument
14309 list will be parsed correctly.. */
14311 error ("`>>' should be `> >' within a nested template argument list");
14312 token
= cp_lexer_peek_token (parser
->lexer
);
14313 token
->type
= CPP_GREATER
;
14317 /* If this is not a nested template argument list, the '>>' is
14318 a typo for '>'. Emit an error message and continue. */
14319 error ("spurious `>>', use `>' to terminate a template argument list");
14320 cp_lexer_consume_token (parser
->lexer
);
14324 cp_parser_require (parser
, CPP_GREATER
, "`>'");
14325 /* The `>' token might be a greater-than operator again now. */
14326 parser
->greater_than_is_operator_p
14327 = saved_greater_than_is_operator_p
;
14328 /* Restore the SAVED_SCOPE. */
14329 parser
->scope
= saved_scope
;
14330 parser
->qualifying_scope
= saved_qualifying_scope
;
14331 parser
->object_scope
= saved_object_scope
;
14336 /* MEMBER_FUNCTION is a member function, or a friend. If default
14337 arguments, or the body of the function have not yet been parsed,
14341 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
14343 cp_lexer
*saved_lexer
;
14345 /* If this member is a template, get the underlying
14347 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
14348 member_function
= DECL_TEMPLATE_RESULT (member_function
);
14350 /* There should not be any class definitions in progress at this
14351 point; the bodies of members are only parsed outside of all class
14353 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
14354 /* While we're parsing the member functions we might encounter more
14355 classes. We want to handle them right away, but we don't want
14356 them getting mixed up with functions that are currently in the
14358 parser
->unparsed_functions_queues
14359 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14361 /* Make sure that any template parameters are in scope. */
14362 maybe_begin_member_template_processing (member_function
);
14364 /* If the body of the function has not yet been parsed, parse it
14366 if (DECL_PENDING_INLINE_P (member_function
))
14368 tree function_scope
;
14369 cp_token_cache
*tokens
;
14371 /* The function is no longer pending; we are processing it. */
14372 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
14373 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
14374 DECL_PENDING_INLINE_P (member_function
) = 0;
14375 /* If this was an inline function in a local class, enter the scope
14376 of the containing function. */
14377 function_scope
= decl_function_context (member_function
);
14378 if (function_scope
)
14379 push_function_context_to (function_scope
);
14381 /* Save away the current lexer. */
14382 saved_lexer
= parser
->lexer
;
14383 /* Make a new lexer to feed us the tokens saved for this function. */
14384 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14385 parser
->lexer
->next
= saved_lexer
;
14387 /* Set the current source position to be the location of the first
14388 token in the saved inline body. */
14389 cp_lexer_peek_token (parser
->lexer
);
14391 /* Let the front end know that we going to be defining this
14393 start_function (NULL_TREE
, member_function
, NULL_TREE
,
14394 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
14396 /* Now, parse the body of the function. */
14397 cp_parser_function_definition_after_declarator (parser
,
14398 /*inline_p=*/true);
14400 /* Leave the scope of the containing function. */
14401 if (function_scope
)
14402 pop_function_context_from (function_scope
);
14403 /* Restore the lexer. */
14404 parser
->lexer
= saved_lexer
;
14407 /* Remove any template parameters from the symbol table. */
14408 maybe_end_member_template_processing ();
14410 /* Restore the queue. */
14411 parser
->unparsed_functions_queues
14412 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14415 /* If DECL contains any default args, remember it on the unparsed
14416 functions queue. */
14419 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
14423 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14425 probe
= TREE_CHAIN (probe
))
14426 if (TREE_PURPOSE (probe
))
14428 TREE_PURPOSE (parser
->unparsed_functions_queues
)
14429 = tree_cons (NULL_TREE
, decl
,
14430 TREE_PURPOSE (parser
->unparsed_functions_queues
));
14436 /* FN is a FUNCTION_DECL which may contains a parameter with an
14437 unparsed DEFAULT_ARG. Parse the default args now. */
14440 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
14442 cp_lexer
*saved_lexer
;
14443 cp_token_cache
*tokens
;
14444 bool saved_local_variables_forbidden_p
;
14447 /* While we're parsing the default args, we might (due to the
14448 statement expression extension) encounter more classes. We want
14449 to handle them right away, but we don't want them getting mixed
14450 up with default args that are currently in the queue. */
14451 parser
->unparsed_functions_queues
14452 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14454 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14456 parameters
= TREE_CHAIN (parameters
))
14458 if (!TREE_PURPOSE (parameters
)
14459 || TREE_CODE (TREE_PURPOSE (parameters
)) != DEFAULT_ARG
)
14462 /* Save away the current lexer. */
14463 saved_lexer
= parser
->lexer
;
14464 /* Create a new one, using the tokens we have saved. */
14465 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parameters
));
14466 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14468 /* Set the current source position to be the location of the
14469 first token in the default argument. */
14470 cp_lexer_peek_token (parser
->lexer
);
14472 /* Local variable names (and the `this' keyword) may not appear
14473 in a default argument. */
14474 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14475 parser
->local_variables_forbidden_p
= true;
14476 /* Parse the assignment-expression. */
14477 if (DECL_CLASS_SCOPE_P (fn
))
14478 push_nested_class (DECL_CONTEXT (fn
));
14479 TREE_PURPOSE (parameters
) = cp_parser_assignment_expression (parser
);
14480 if (DECL_CLASS_SCOPE_P (fn
))
14481 pop_nested_class ();
14483 /* Restore saved state. */
14484 parser
->lexer
= saved_lexer
;
14485 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14488 /* Restore the queue. */
14489 parser
->unparsed_functions_queues
14490 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14493 /* Parse the operand of `sizeof' (or a similar operator). Returns
14494 either a TYPE or an expression, depending on the form of the
14495 input. The KEYWORD indicates which kind of expression we have
14499 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
14501 static const char *format
;
14502 tree expr
= NULL_TREE
;
14503 const char *saved_message
;
14504 bool saved_integral_constant_expression_p
;
14506 /* Initialize FORMAT the first time we get here. */
14508 format
= "types may not be defined in `%s' expressions";
14510 /* Types cannot be defined in a `sizeof' expression. Save away the
14512 saved_message
= parser
->type_definition_forbidden_message
;
14513 /* And create the new one. */
14514 parser
->type_definition_forbidden_message
14515 = xmalloc (strlen (format
)
14516 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
14518 sprintf ((char *) parser
->type_definition_forbidden_message
,
14519 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
14521 /* The restrictions on constant-expressions do not apply inside
14522 sizeof expressions. */
14523 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
14524 parser
->integral_constant_expression_p
= false;
14526 /* Do not actually evaluate the expression. */
14528 /* If it's a `(', then we might be looking at the type-id
14530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14533 bool saved_in_type_id_in_expr_p
;
14535 /* We can't be sure yet whether we're looking at a type-id or an
14537 cp_parser_parse_tentatively (parser
);
14538 /* Consume the `('. */
14539 cp_lexer_consume_token (parser
->lexer
);
14540 /* Parse the type-id. */
14541 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14542 parser
->in_type_id_in_expr_p
= true;
14543 type
= cp_parser_type_id (parser
);
14544 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14545 /* Now, look for the trailing `)'. */
14546 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14547 /* If all went well, then we're done. */
14548 if (cp_parser_parse_definitely (parser
))
14550 /* Build a list of decl-specifiers; right now, we have only
14551 a single type-specifier. */
14552 type
= build_tree_list (NULL_TREE
,
14555 /* Call grokdeclarator to figure out what type this is. */
14556 expr
= grokdeclarator (NULL_TREE
,
14560 /*attrlist=*/NULL
);
14564 /* If the type-id production did not work out, then we must be
14565 looking at the unary-expression production. */
14567 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
14568 /* Go back to evaluating expressions. */
14571 /* Free the message we created. */
14572 free ((char *) parser
->type_definition_forbidden_message
);
14573 /* And restore the old one. */
14574 parser
->type_definition_forbidden_message
= saved_message
;
14575 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
14580 /* If the current declaration has no declarator, return true. */
14583 cp_parser_declares_only_class_p (cp_parser
*parser
)
14585 /* If the next token is a `;' or a `,' then there is no
14587 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14588 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14591 /* Simplify EXPR if it is a non-dependent expression. Returns the
14592 (possibly simplified) expression. */
14595 cp_parser_fold_non_dependent_expr (tree expr
)
14597 /* If we're in a template, but EXPR isn't value dependent, simplify
14598 it. We're supposed to treat:
14600 template <typename T> void f(T[1 + 1]);
14601 template <typename T> void f(T[2]);
14603 as two declarations of the same function, for example. */
14604 if (processing_template_decl
14605 && !type_dependent_expression_p (expr
)
14606 && !value_dependent_expression_p (expr
))
14608 HOST_WIDE_INT saved_processing_template_decl
;
14610 saved_processing_template_decl
= processing_template_decl
;
14611 processing_template_decl
= 0;
14612 expr
= tsubst_copy_and_build (expr
,
14613 /*args=*/NULL_TREE
,
14615 /*in_decl=*/NULL_TREE
,
14616 /*function_p=*/false);
14617 processing_template_decl
= saved_processing_template_decl
;
14622 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14623 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14626 cp_parser_friend_p (tree decl_specifiers
)
14628 while (decl_specifiers
)
14630 /* See if this decl-specifier is `friend'. */
14631 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
14632 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
14635 /* Go on to the next decl-specifier. */
14636 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
14642 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14643 issue an error message indicating that TOKEN_DESC was expected.
14645 Returns the token consumed, if the token had the appropriate type.
14646 Otherwise, returns NULL. */
14649 cp_parser_require (cp_parser
* parser
,
14650 enum cpp_ttype type
,
14651 const char* token_desc
)
14653 if (cp_lexer_next_token_is (parser
->lexer
, type
))
14654 return cp_lexer_consume_token (parser
->lexer
);
14657 /* Output the MESSAGE -- unless we're parsing tentatively. */
14658 if (!cp_parser_simulate_error (parser
))
14660 char *message
= concat ("expected ", token_desc
, NULL
);
14661 cp_parser_error (parser
, message
);
14668 /* Like cp_parser_require, except that tokens will be skipped until
14669 the desired token is found. An error message is still produced if
14670 the next token is not as expected. */
14673 cp_parser_skip_until_found (cp_parser
* parser
,
14674 enum cpp_ttype type
,
14675 const char* token_desc
)
14678 unsigned nesting_depth
= 0;
14680 if (cp_parser_require (parser
, type
, token_desc
))
14683 /* Skip tokens until the desired token is found. */
14686 /* Peek at the next token. */
14687 token
= cp_lexer_peek_token (parser
->lexer
);
14688 /* If we've reached the token we want, consume it and
14690 if (token
->type
== type
&& !nesting_depth
)
14692 cp_lexer_consume_token (parser
->lexer
);
14695 /* If we've run out of tokens, stop. */
14696 if (token
->type
== CPP_EOF
)
14698 if (token
->type
== CPP_OPEN_BRACE
14699 || token
->type
== CPP_OPEN_PAREN
14700 || token
->type
== CPP_OPEN_SQUARE
)
14702 else if (token
->type
== CPP_CLOSE_BRACE
14703 || token
->type
== CPP_CLOSE_PAREN
14704 || token
->type
== CPP_CLOSE_SQUARE
)
14706 if (nesting_depth
-- == 0)
14709 /* Consume this token. */
14710 cp_lexer_consume_token (parser
->lexer
);
14714 /* If the next token is the indicated keyword, consume it. Otherwise,
14715 issue an error message indicating that TOKEN_DESC was expected.
14717 Returns the token consumed, if the token had the appropriate type.
14718 Otherwise, returns NULL. */
14721 cp_parser_require_keyword (cp_parser
* parser
,
14723 const char* token_desc
)
14725 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
14727 if (token
&& token
->keyword
!= keyword
)
14729 dyn_string_t error_msg
;
14731 /* Format the error message. */
14732 error_msg
= dyn_string_new (0);
14733 dyn_string_append_cstr (error_msg
, "expected ");
14734 dyn_string_append_cstr (error_msg
, token_desc
);
14735 cp_parser_error (parser
, error_msg
->s
);
14736 dyn_string_delete (error_msg
);
14743 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14744 function-definition. */
14747 cp_parser_token_starts_function_definition_p (cp_token
* token
)
14749 return (/* An ordinary function-body begins with an `{'. */
14750 token
->type
== CPP_OPEN_BRACE
14751 /* A ctor-initializer begins with a `:'. */
14752 || token
->type
== CPP_COLON
14753 /* A function-try-block begins with `try'. */
14754 || token
->keyword
== RID_TRY
14755 /* The named return value extension begins with `return'. */
14756 || token
->keyword
== RID_RETURN
);
14759 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14763 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
14767 token
= cp_lexer_peek_token (parser
->lexer
);
14768 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
14771 /* Returns TRUE iff the next token is the "," or ">" ending a
14772 template-argument. ">>" is also accepted (after the full
14773 argument was parsed) because it's probably a typo for "> >",
14774 and there is a specific diagnostic for this. */
14777 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
14781 token
= cp_lexer_peek_token (parser
->lexer
);
14782 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
14783 || token
->type
== CPP_RSHIFT
);
14786 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14787 or none_type otherwise. */
14789 static enum tag_types
14790 cp_parser_token_is_class_key (cp_token
* token
)
14792 switch (token
->keyword
)
14797 return record_type
;
14806 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14809 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
14811 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
14812 pedwarn ("`%s' tag used in naming `%#T'",
14813 class_key
== union_type
? "union"
14814 : class_key
== record_type
? "struct" : "class",
14818 /* Issue an error message if DECL is redeclared with different
14819 access than its original declaration [class.access.spec/3].
14820 This applies to nested classes and nested class templates.
14823 static void cp_parser_check_access_in_redeclaration (tree decl
)
14825 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
14828 if ((TREE_PRIVATE (decl
)
14829 != (current_access_specifier
== access_private_node
))
14830 || (TREE_PROTECTED (decl
)
14831 != (current_access_specifier
== access_protected_node
)))
14832 error ("%D redeclared with different access", decl
);
14835 /* Look for the `template' keyword, as a syntactic disambiguator.
14836 Return TRUE iff it is present, in which case it will be
14840 cp_parser_optional_template_keyword (cp_parser
*parser
)
14842 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14844 /* The `template' keyword can only be used within templates;
14845 outside templates the parser can always figure out what is a
14846 template and what is not. */
14847 if (!processing_template_decl
)
14849 error ("`template' (as a disambiguator) is only allowed "
14850 "within templates");
14851 /* If this part of the token stream is rescanned, the same
14852 error message would be generated. So, we purge the token
14853 from the stream. */
14854 cp_lexer_purge_token (parser
->lexer
);
14859 /* Consume the `template' keyword. */
14860 cp_lexer_consume_token (parser
->lexer
);
14868 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14869 set PARSER->SCOPE, and perform other related actions. */
14872 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
14877 /* Get the stored value. */
14878 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
14879 /* Perform any access checks that were deferred. */
14880 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
14881 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
14882 /* Set the scope from the stored value. */
14883 parser
->scope
= TREE_VALUE (value
);
14884 parser
->qualifying_scope
= TREE_TYPE (value
);
14885 parser
->object_scope
= NULL_TREE
;
14888 /* Add tokens to CACHE until an non-nested END token appears. */
14891 cp_parser_cache_group (cp_parser
*parser
,
14892 cp_token_cache
*cache
,
14893 enum cpp_ttype end
,
14900 /* Abort a parenthesized expression if we encounter a brace. */
14901 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
14902 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14904 /* Consume the next token. */
14905 token
= cp_lexer_consume_token (parser
->lexer
);
14906 /* If we've reached the end of the file, stop. */
14907 if (token
->type
== CPP_EOF
)
14909 /* Add this token to the tokens we are saving. */
14910 cp_token_cache_push_token (cache
, token
);
14911 /* See if it starts a new group. */
14912 if (token
->type
== CPP_OPEN_BRACE
)
14914 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
14918 else if (token
->type
== CPP_OPEN_PAREN
)
14919 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
14920 else if (token
->type
== end
)
14925 /* Begin parsing tentatively. We always save tokens while parsing
14926 tentatively so that if the tentative parsing fails we can restore the
14930 cp_parser_parse_tentatively (cp_parser
* parser
)
14932 /* Enter a new parsing context. */
14933 parser
->context
= cp_parser_context_new (parser
->context
);
14934 /* Begin saving tokens. */
14935 cp_lexer_save_tokens (parser
->lexer
);
14936 /* In order to avoid repetitive access control error messages,
14937 access checks are queued up until we are no longer parsing
14939 push_deferring_access_checks (dk_deferred
);
14942 /* Commit to the currently active tentative parse. */
14945 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
14947 cp_parser_context
*context
;
14950 /* Mark all of the levels as committed. */
14951 lexer
= parser
->lexer
;
14952 for (context
= parser
->context
; context
->next
; context
= context
->next
)
14954 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
14956 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
14957 while (!cp_lexer_saving_tokens (lexer
))
14958 lexer
= lexer
->next
;
14959 cp_lexer_commit_tokens (lexer
);
14963 /* Abort the currently active tentative parse. All consumed tokens
14964 will be rolled back, and no diagnostics will be issued. */
14967 cp_parser_abort_tentative_parse (cp_parser
* parser
)
14969 cp_parser_simulate_error (parser
);
14970 /* Now, pretend that we want to see if the construct was
14971 successfully parsed. */
14972 cp_parser_parse_definitely (parser
);
14975 /* Stop parsing tentatively. If a parse error has occurred, restore the
14976 token stream. Otherwise, commit to the tokens we have consumed.
14977 Returns true if no error occurred; false otherwise. */
14980 cp_parser_parse_definitely (cp_parser
* parser
)
14982 bool error_occurred
;
14983 cp_parser_context
*context
;
14985 /* Remember whether or not an error occurred, since we are about to
14986 destroy that information. */
14987 error_occurred
= cp_parser_error_occurred (parser
);
14988 /* Remove the topmost context from the stack. */
14989 context
= parser
->context
;
14990 parser
->context
= context
->next
;
14991 /* If no parse errors occurred, commit to the tentative parse. */
14992 if (!error_occurred
)
14994 /* Commit to the tokens read tentatively, unless that was
14996 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
14997 cp_lexer_commit_tokens (parser
->lexer
);
14999 pop_to_parent_deferring_access_checks ();
15001 /* Otherwise, if errors occurred, roll back our state so that things
15002 are just as they were before we began the tentative parse. */
15005 cp_lexer_rollback_tokens (parser
->lexer
);
15006 pop_deferring_access_checks ();
15008 /* Add the context to the front of the free list. */
15009 context
->next
= cp_parser_context_free_list
;
15010 cp_parser_context_free_list
= context
;
15012 return !error_occurred
;
15015 /* Returns true if we are parsing tentatively -- but have decided that
15016 we will stick with this tentative parse, even if errors occur. */
15019 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
15021 return (cp_parser_parsing_tentatively (parser
)
15022 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
15025 /* Returns nonzero iff an error has occurred during the most recent
15026 tentative parse. */
15029 cp_parser_error_occurred (cp_parser
* parser
)
15031 return (cp_parser_parsing_tentatively (parser
)
15032 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
15035 /* Returns nonzero if GNU extensions are allowed. */
15038 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
15040 return parser
->allow_gnu_extensions_p
;
15047 static GTY (()) cp_parser
*the_parser
;
15049 /* External interface. */
15051 /* Parse one entire translation unit. */
15054 c_parse_file (void)
15056 bool error_occurred
;
15058 the_parser
= cp_parser_new ();
15059 push_deferring_access_checks (flag_access_control
15060 ? dk_no_deferred
: dk_no_check
);
15061 error_occurred
= cp_parser_translation_unit (the_parser
);
15065 /* Clean up after parsing the entire translation unit. */
15068 free_parser_stacks (void)
15070 /* Nothing to do. */
15073 /* This variable must be provided by every front end. */
15077 #include "gt-cp-parser.h"