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
);
3765 if (BASELINK_P (fn
))
3767 = (build_new_method_call
3768 (instance
, fn
, args
, NULL_TREE
,
3769 (idk
== CP_ID_KIND_QUALIFIED
3770 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3773 = finish_call_expr (postfix_expression
, args
,
3774 /*disallow_virtual=*/false,
3775 /*koenig_p=*/false);
3777 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
3778 || TREE_CODE (postfix_expression
) == MEMBER_REF
3779 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
3780 postfix_expression
= (build_offset_ref_call_from_tree
3781 (postfix_expression
, args
));
3782 else if (idk
== CP_ID_KIND_QUALIFIED
)
3783 /* A call to a static class member, or a namespace-scope
3786 = finish_call_expr (postfix_expression
, args
,
3787 /*disallow_virtual=*/true,
3790 /* All other function calls. */
3792 = finish_call_expr (postfix_expression
, args
,
3793 /*disallow_virtual=*/false,
3796 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3797 idk
= CP_ID_KIND_NONE
;
3803 /* postfix-expression . template [opt] id-expression
3804 postfix-expression . pseudo-destructor-name
3805 postfix-expression -> template [opt] id-expression
3806 postfix-expression -> pseudo-destructor-name */
3811 tree scope
= NULL_TREE
;
3812 enum cpp_ttype token_type
= token
->type
;
3814 /* If this is a `->' operator, dereference the pointer. */
3815 if (token
->type
== CPP_DEREF
)
3816 postfix_expression
= build_x_arrow (postfix_expression
);
3817 /* Check to see whether or not the expression is
3819 dependent_p
= type_dependent_expression_p (postfix_expression
);
3820 /* The identifier following the `->' or `.' is not
3822 parser
->scope
= NULL_TREE
;
3823 parser
->qualifying_scope
= NULL_TREE
;
3824 parser
->object_scope
= NULL_TREE
;
3825 idk
= CP_ID_KIND_NONE
;
3826 /* Enter the scope corresponding to the type of the object
3827 given by the POSTFIX_EXPRESSION. */
3829 && TREE_TYPE (postfix_expression
) != NULL_TREE
)
3831 scope
= TREE_TYPE (postfix_expression
);
3832 /* According to the standard, no expression should
3833 ever have reference type. Unfortunately, we do not
3834 currently match the standard in this respect in
3835 that our internal representation of an expression
3836 may have reference type even when the standard says
3837 it does not. Therefore, we have to manually obtain
3838 the underlying type here. */
3839 scope
= non_reference (scope
);
3840 /* The type of the POSTFIX_EXPRESSION must be
3842 scope
= complete_type_or_else (scope
, NULL_TREE
);
3843 /* Let the name lookup machinery know that we are
3844 processing a class member access expression. */
3845 parser
->context
->object_type
= scope
;
3846 /* If something went wrong, we want to be able to
3847 discern that case, as opposed to the case where
3848 there was no SCOPE due to the type of expression
3851 scope
= error_mark_node
;
3852 /* If the SCOPE was erroneous, make the various
3853 semantic analysis functions exit quickly -- and
3854 without issuing additional error messages. */
3855 if (scope
== error_mark_node
)
3856 postfix_expression
= error_mark_node
;
3859 /* Consume the `.' or `->' operator. */
3860 cp_lexer_consume_token (parser
->lexer
);
3861 /* If the SCOPE is not a scalar type, we are looking at an
3862 ordinary class member access expression, rather than a
3863 pseudo-destructor-name. */
3864 if (!scope
|| !SCALAR_TYPE_P (scope
))
3866 template_p
= cp_parser_optional_template_keyword (parser
);
3867 /* Parse the id-expression. */
3868 name
= cp_parser_id_expression (parser
,
3870 /*check_dependency_p=*/true,
3871 /*template_p=*/NULL
,
3872 /*declarator_p=*/false);
3873 /* In general, build a SCOPE_REF if the member name is
3874 qualified. However, if the name was not dependent
3875 and has already been resolved; there is no need to
3876 build the SCOPE_REF. For example;
3878 struct X { void f(); };
3879 template <typename T> void f(T* t) { t->X::f(); }
3881 Even though "t" is dependent, "X::f" is not and has
3882 been resolved to a BASELINK; there is no need to
3883 include scope information. */
3885 /* But we do need to remember that there was an explicit
3886 scope for virtual function calls. */
3888 idk
= CP_ID_KIND_QUALIFIED
;
3890 if (name
!= error_mark_node
3891 && !BASELINK_P (name
)
3894 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
3895 parser
->scope
= NULL_TREE
;
3896 parser
->qualifying_scope
= NULL_TREE
;
3897 parser
->object_scope
= NULL_TREE
;
3900 = finish_class_member_access_expr (postfix_expression
, name
);
3902 /* Otherwise, try the pseudo-destructor-name production. */
3908 /* Parse the pseudo-destructor-name. */
3909 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
3910 /* Form the call. */
3912 = finish_pseudo_destructor_expr (postfix_expression
,
3913 s
, TREE_TYPE (type
));
3916 /* We no longer need to look up names in the scope of the
3917 object on the left-hand side of the `.' or `->'
3919 parser
->context
->object_type
= NULL_TREE
;
3920 /* These operators may not appear in constant-expressions. */
3921 if (parser
->integral_constant_expression_p
3922 /* The "->" operator is allowed in the implementation
3923 of "offsetof". The "." operator may appear in the
3924 name of the member. */
3925 && !parser
->in_offsetof_p
)
3927 if (!parser
->allow_non_integral_constant_expression_p
)
3929 = (cp_parser_non_integral_constant_expression
3930 (token_type
== CPP_DEREF
? "'->'" : "`.'"));
3931 parser
->non_integral_constant_expression_p
= true;
3937 /* postfix-expression ++ */
3938 /* Consume the `++' token. */
3939 cp_lexer_consume_token (parser
->lexer
);
3940 /* Generate a representation for the complete expression. */
3942 = finish_increment_expr (postfix_expression
,
3943 POSTINCREMENT_EXPR
);
3944 /* Increments may not appear in constant-expressions. */
3945 if (parser
->integral_constant_expression_p
)
3947 if (!parser
->allow_non_integral_constant_expression_p
)
3949 = cp_parser_non_integral_constant_expression ("an increment");
3950 parser
->non_integral_constant_expression_p
= true;
3952 idk
= CP_ID_KIND_NONE
;
3955 case CPP_MINUS_MINUS
:
3956 /* postfix-expression -- */
3957 /* Consume the `--' token. */
3958 cp_lexer_consume_token (parser
->lexer
);
3959 /* Generate a representation for the complete expression. */
3961 = finish_increment_expr (postfix_expression
,
3962 POSTDECREMENT_EXPR
);
3963 /* Decrements may not appear in constant-expressions. */
3964 if (parser
->integral_constant_expression_p
)
3966 if (!parser
->allow_non_integral_constant_expression_p
)
3968 = cp_parser_non_integral_constant_expression ("a decrement");
3969 parser
->non_integral_constant_expression_p
= true;
3971 idk
= CP_ID_KIND_NONE
;
3975 return postfix_expression
;
3979 /* We should never get here. */
3981 return error_mark_node
;
3984 /* Parse a parenthesized expression-list.
3987 assignment-expression
3988 expression-list, assignment-expression
3993 identifier, expression-list
3995 Returns a TREE_LIST. The TREE_VALUE of each node is a
3996 representation of an assignment-expression. Note that a TREE_LIST
3997 is returned even if there is only a single expression in the list.
3998 error_mark_node is returned if the ( and or ) are
3999 missing. NULL_TREE is returned on no expressions. The parentheses
4000 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4001 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4002 indicates whether or not all of the expressions in the list were
4006 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4007 bool is_attribute_list
,
4008 bool *non_constant_p
)
4010 tree expression_list
= NULL_TREE
;
4011 tree identifier
= NULL_TREE
;
4013 /* Assume all the expressions will be constant. */
4015 *non_constant_p
= false;
4017 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4018 return error_mark_node
;
4020 /* Consume expressions until there are no more. */
4021 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4026 /* At the beginning of attribute lists, check to see if the
4027 next token is an identifier. */
4028 if (is_attribute_list
4029 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4033 /* Consume the identifier. */
4034 token
= cp_lexer_consume_token (parser
->lexer
);
4035 /* Save the identifier. */
4036 identifier
= token
->value
;
4040 /* Parse the next assignment-expression. */
4043 bool expr_non_constant_p
;
4044 expr
= (cp_parser_constant_expression
4045 (parser
, /*allow_non_constant_p=*/true,
4046 &expr_non_constant_p
));
4047 if (expr_non_constant_p
)
4048 *non_constant_p
= true;
4051 expr
= cp_parser_assignment_expression (parser
);
4053 /* Add it to the list. We add error_mark_node
4054 expressions to the list, so that we can still tell if
4055 the correct form for a parenthesized expression-list
4056 is found. That gives better errors. */
4057 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4059 if (expr
== error_mark_node
)
4063 /* After the first item, attribute lists look the same as
4064 expression lists. */
4065 is_attribute_list
= false;
4068 /* If the next token isn't a `,', then we are done. */
4069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4072 /* Otherwise, consume the `,' and keep going. */
4073 cp_lexer_consume_token (parser
->lexer
);
4076 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4081 /* We try and resync to an unnested comma, as that will give the
4082 user better diagnostics. */
4083 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4084 /*recovering=*/true,
4086 /*consume_paren=*/true);
4090 return error_mark_node
;
4093 /* We built up the list in reverse order so we must reverse it now. */
4094 expression_list
= nreverse (expression_list
);
4096 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4098 return expression_list
;
4101 /* Parse a pseudo-destructor-name.
4103 pseudo-destructor-name:
4104 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4105 :: [opt] nested-name-specifier template template-id :: ~ type-name
4106 :: [opt] nested-name-specifier [opt] ~ type-name
4108 If either of the first two productions is used, sets *SCOPE to the
4109 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4110 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4111 or ERROR_MARK_NODE if no type-name is present. */
4114 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4118 bool nested_name_specifier_p
;
4120 /* Look for the optional `::' operator. */
4121 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4122 /* Look for the optional nested-name-specifier. */
4123 nested_name_specifier_p
4124 = (cp_parser_nested_name_specifier_opt (parser
,
4125 /*typename_keyword_p=*/false,
4126 /*check_dependency_p=*/true,
4128 /*is_declaration=*/true)
4130 /* Now, if we saw a nested-name-specifier, we might be doing the
4131 second production. */
4132 if (nested_name_specifier_p
4133 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4135 /* Consume the `template' keyword. */
4136 cp_lexer_consume_token (parser
->lexer
);
4137 /* Parse the template-id. */
4138 cp_parser_template_id (parser
,
4139 /*template_keyword_p=*/true,
4140 /*check_dependency_p=*/false,
4141 /*is_declaration=*/true);
4142 /* Look for the `::' token. */
4143 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4145 /* If the next token is not a `~', then there might be some
4146 additional qualification. */
4147 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4149 /* Look for the type-name. */
4150 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4151 /* Look for the `::' token. */
4152 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4157 /* Look for the `~'. */
4158 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4159 /* Look for the type-name again. We are not responsible for
4160 checking that it matches the first type-name. */
4161 *type
= cp_parser_type_name (parser
);
4164 /* Parse a unary-expression.
4170 unary-operator cast-expression
4171 sizeof unary-expression
4179 __extension__ cast-expression
4180 __alignof__ unary-expression
4181 __alignof__ ( type-id )
4182 __real__ cast-expression
4183 __imag__ cast-expression
4186 ADDRESS_P is true iff the unary-expression is appearing as the
4187 operand of the `&' operator.
4189 Returns a representation of the expression. */
4192 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4195 enum tree_code unary_operator
;
4197 /* Peek at the next token. */
4198 token
= cp_lexer_peek_token (parser
->lexer
);
4199 /* Some keywords give away the kind of expression. */
4200 if (token
->type
== CPP_KEYWORD
)
4202 enum rid keyword
= token
->keyword
;
4212 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4213 /* Consume the token. */
4214 cp_lexer_consume_token (parser
->lexer
);
4215 /* Parse the operand. */
4216 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4218 if (TYPE_P (operand
))
4219 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4221 return cxx_sizeof_or_alignof_expr (operand
, op
);
4225 return cp_parser_new_expression (parser
);
4228 return cp_parser_delete_expression (parser
);
4232 /* The saved value of the PEDANTIC flag. */
4236 /* Save away the PEDANTIC flag. */
4237 cp_parser_extension_opt (parser
, &saved_pedantic
);
4238 /* Parse the cast-expression. */
4239 expr
= cp_parser_simple_cast_expression (parser
);
4240 /* Restore the PEDANTIC flag. */
4241 pedantic
= saved_pedantic
;
4251 /* Consume the `__real__' or `__imag__' token. */
4252 cp_lexer_consume_token (parser
->lexer
);
4253 /* Parse the cast-expression. */
4254 expression
= cp_parser_simple_cast_expression (parser
);
4255 /* Create the complete representation. */
4256 return build_x_unary_op ((keyword
== RID_REALPART
4257 ? REALPART_EXPR
: IMAGPART_EXPR
),
4267 /* Look for the `:: new' and `:: delete', which also signal the
4268 beginning of a new-expression, or delete-expression,
4269 respectively. If the next token is `::', then it might be one of
4271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4275 /* See if the token after the `::' is one of the keywords in
4276 which we're interested. */
4277 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4278 /* If it's `new', we have a new-expression. */
4279 if (keyword
== RID_NEW
)
4280 return cp_parser_new_expression (parser
);
4281 /* Similarly, for `delete'. */
4282 else if (keyword
== RID_DELETE
)
4283 return cp_parser_delete_expression (parser
);
4286 /* Look for a unary operator. */
4287 unary_operator
= cp_parser_unary_operator (token
);
4288 /* The `++' and `--' operators can be handled similarly, even though
4289 they are not technically unary-operators in the grammar. */
4290 if (unary_operator
== ERROR_MARK
)
4292 if (token
->type
== CPP_PLUS_PLUS
)
4293 unary_operator
= PREINCREMENT_EXPR
;
4294 else if (token
->type
== CPP_MINUS_MINUS
)
4295 unary_operator
= PREDECREMENT_EXPR
;
4296 /* Handle the GNU address-of-label extension. */
4297 else if (cp_parser_allow_gnu_extensions_p (parser
)
4298 && token
->type
== CPP_AND_AND
)
4302 /* Consume the '&&' token. */
4303 cp_lexer_consume_token (parser
->lexer
);
4304 /* Look for the identifier. */
4305 identifier
= cp_parser_identifier (parser
);
4306 /* Create an expression representing the address. */
4307 return finish_label_address_expr (identifier
);
4310 if (unary_operator
!= ERROR_MARK
)
4312 tree cast_expression
;
4313 tree expression
= error_mark_node
;
4314 const char *non_constant_p
= NULL
;
4316 /* Consume the operator token. */
4317 token
= cp_lexer_consume_token (parser
->lexer
);
4318 /* Parse the cast-expression. */
4320 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4321 /* Now, build an appropriate representation. */
4322 switch (unary_operator
)
4325 non_constant_p
= "`*'";
4326 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4330 /* The "&" operator is allowed in the implementation of
4332 if (!parser
->in_offsetof_p
)
4333 non_constant_p
= "`&'";
4336 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4339 case PREINCREMENT_EXPR
:
4340 case PREDECREMENT_EXPR
:
4341 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4346 case TRUTH_NOT_EXPR
:
4347 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4354 if (non_constant_p
&& parser
->integral_constant_expression_p
)
4356 if (!parser
->allow_non_integral_constant_expression_p
)
4357 return cp_parser_non_integral_constant_expression (non_constant_p
);
4358 parser
->non_integral_constant_expression_p
= true;
4364 return cp_parser_postfix_expression (parser
, address_p
);
4367 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4368 unary-operator, the corresponding tree code is returned. */
4370 static enum tree_code
4371 cp_parser_unary_operator (cp_token
* token
)
4373 switch (token
->type
)
4376 return INDIRECT_REF
;
4382 return CONVERT_EXPR
;
4388 return TRUTH_NOT_EXPR
;
4391 return BIT_NOT_EXPR
;
4398 /* Parse a new-expression.
4401 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4402 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4404 Returns a representation of the expression. */
4407 cp_parser_new_expression (cp_parser
* parser
)
4409 bool global_scope_p
;
4414 /* Look for the optional `::' operator. */
4416 = (cp_parser_global_scope_opt (parser
,
4417 /*current_scope_valid_p=*/false)
4419 /* Look for the `new' operator. */
4420 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4421 /* There's no easy way to tell a new-placement from the
4422 `( type-id )' construct. */
4423 cp_parser_parse_tentatively (parser
);
4424 /* Look for a new-placement. */
4425 placement
= cp_parser_new_placement (parser
);
4426 /* If that didn't work out, there's no new-placement. */
4427 if (!cp_parser_parse_definitely (parser
))
4428 placement
= NULL_TREE
;
4430 /* If the next token is a `(', then we have a parenthesized
4432 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4434 /* Consume the `('. */
4435 cp_lexer_consume_token (parser
->lexer
);
4436 /* Parse the type-id. */
4437 type
= cp_parser_type_id (parser
);
4438 /* Look for the closing `)'. */
4439 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4441 /* Otherwise, there must be a new-type-id. */
4443 type
= cp_parser_new_type_id (parser
);
4445 /* If the next token is a `(', then we have a new-initializer. */
4446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4447 initializer
= cp_parser_new_initializer (parser
);
4449 initializer
= NULL_TREE
;
4451 /* Create a representation of the new-expression. */
4452 return build_new (placement
, type
, initializer
, global_scope_p
);
4455 /* Parse a new-placement.
4460 Returns the same representation as for an expression-list. */
4463 cp_parser_new_placement (cp_parser
* parser
)
4465 tree expression_list
;
4467 /* Parse the expression-list. */
4468 expression_list
= (cp_parser_parenthesized_expression_list
4469 (parser
, false, /*non_constant_p=*/NULL
));
4471 return expression_list
;
4474 /* Parse a new-type-id.
4477 type-specifier-seq new-declarator [opt]
4479 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4480 and whose TREE_VALUE is the new-declarator. */
4483 cp_parser_new_type_id (cp_parser
* parser
)
4485 tree type_specifier_seq
;
4487 const char *saved_message
;
4489 /* The type-specifier sequence must not contain type definitions.
4490 (It cannot contain declarations of new types either, but if they
4491 are not definitions we will catch that because they are not
4493 saved_message
= parser
->type_definition_forbidden_message
;
4494 parser
->type_definition_forbidden_message
4495 = "types may not be defined in a new-type-id";
4496 /* Parse the type-specifier-seq. */
4497 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4498 /* Restore the old message. */
4499 parser
->type_definition_forbidden_message
= saved_message
;
4500 /* Parse the new-declarator. */
4501 declarator
= cp_parser_new_declarator_opt (parser
);
4503 return build_tree_list (type_specifier_seq
, declarator
);
4506 /* Parse an (optional) new-declarator.
4509 ptr-operator new-declarator [opt]
4510 direct-new-declarator
4512 Returns a representation of the declarator. See
4513 cp_parser_declarator for the representations used. */
4516 cp_parser_new_declarator_opt (cp_parser
* parser
)
4518 enum tree_code code
;
4520 tree cv_qualifier_seq
;
4522 /* We don't know if there's a ptr-operator next, or not. */
4523 cp_parser_parse_tentatively (parser
);
4524 /* Look for a ptr-operator. */
4525 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4526 /* If that worked, look for more new-declarators. */
4527 if (cp_parser_parse_definitely (parser
))
4531 /* Parse another optional declarator. */
4532 declarator
= cp_parser_new_declarator_opt (parser
);
4534 /* Create the representation of the declarator. */
4535 if (code
== INDIRECT_REF
)
4536 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4539 declarator
= make_reference_declarator (cv_qualifier_seq
,
4542 /* Handle the pointer-to-member case. */
4544 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4549 /* If the next token is a `[', there is a direct-new-declarator. */
4550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4551 return cp_parser_direct_new_declarator (parser
);
4556 /* Parse a direct-new-declarator.
4558 direct-new-declarator:
4560 direct-new-declarator [constant-expression]
4562 Returns an ARRAY_REF, following the same conventions as are
4563 documented for cp_parser_direct_declarator. */
4566 cp_parser_direct_new_declarator (cp_parser
* parser
)
4568 tree declarator
= NULL_TREE
;
4574 /* Look for the opening `['. */
4575 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4576 /* The first expression is not required to be constant. */
4579 expression
= cp_parser_expression (parser
);
4580 /* The standard requires that the expression have integral
4581 type. DR 74 adds enumeration types. We believe that the
4582 real intent is that these expressions be handled like the
4583 expression in a `switch' condition, which also allows
4584 classes with a single conversion to integral or
4585 enumeration type. */
4586 if (!processing_template_decl
)
4589 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4594 error ("expression in new-declarator must have integral or enumeration type");
4595 expression
= error_mark_node
;
4599 /* But all the other expressions must be. */
4602 = cp_parser_constant_expression (parser
,
4603 /*allow_non_constant=*/false,
4605 /* Look for the closing `]'. */
4606 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4608 /* Add this bound to the declarator. */
4609 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4611 /* If the next token is not a `[', then there are no more
4613 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4620 /* Parse a new-initializer.
4623 ( expression-list [opt] )
4625 Returns a representation of the expression-list. If there is no
4626 expression-list, VOID_ZERO_NODE is returned. */
4629 cp_parser_new_initializer (cp_parser
* parser
)
4631 tree expression_list
;
4633 expression_list
= (cp_parser_parenthesized_expression_list
4634 (parser
, false, /*non_constant_p=*/NULL
));
4635 if (!expression_list
)
4636 expression_list
= void_zero_node
;
4638 return expression_list
;
4641 /* Parse a delete-expression.
4644 :: [opt] delete cast-expression
4645 :: [opt] delete [ ] cast-expression
4647 Returns a representation of the expression. */
4650 cp_parser_delete_expression (cp_parser
* parser
)
4652 bool global_scope_p
;
4656 /* Look for the optional `::' operator. */
4658 = (cp_parser_global_scope_opt (parser
,
4659 /*current_scope_valid_p=*/false)
4661 /* Look for the `delete' keyword. */
4662 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4663 /* See if the array syntax is in use. */
4664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4666 /* Consume the `[' token. */
4667 cp_lexer_consume_token (parser
->lexer
);
4668 /* Look for the `]' token. */
4669 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4670 /* Remember that this is the `[]' construct. */
4676 /* Parse the cast-expression. */
4677 expression
= cp_parser_simple_cast_expression (parser
);
4679 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4682 /* Parse a cast-expression.
4686 ( type-id ) cast-expression
4688 Returns a representation of the expression. */
4691 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4693 /* If it's a `(', then we might be looking at a cast. */
4694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4696 tree type
= NULL_TREE
;
4697 tree expr
= NULL_TREE
;
4698 bool compound_literal_p
;
4699 const char *saved_message
;
4701 /* There's no way to know yet whether or not this is a cast.
4702 For example, `(int (3))' is a unary-expression, while `(int)
4703 3' is a cast. So, we resort to parsing tentatively. */
4704 cp_parser_parse_tentatively (parser
);
4705 /* Types may not be defined in a cast. */
4706 saved_message
= parser
->type_definition_forbidden_message
;
4707 parser
->type_definition_forbidden_message
4708 = "types may not be defined in casts";
4709 /* Consume the `('. */
4710 cp_lexer_consume_token (parser
->lexer
);
4711 /* A very tricky bit is that `(struct S) { 3 }' is a
4712 compound-literal (which we permit in C++ as an extension).
4713 But, that construct is not a cast-expression -- it is a
4714 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4715 is legal; if the compound-literal were a cast-expression,
4716 you'd need an extra set of parentheses.) But, if we parse
4717 the type-id, and it happens to be a class-specifier, then we
4718 will commit to the parse at that point, because we cannot
4719 undo the action that is done when creating a new class. So,
4720 then we cannot back up and do a postfix-expression.
4722 Therefore, we scan ahead to the closing `)', and check to see
4723 if the token after the `)' is a `{'. If so, we are not
4724 looking at a cast-expression.
4726 Save tokens so that we can put them back. */
4727 cp_lexer_save_tokens (parser
->lexer
);
4728 /* Skip tokens until the next token is a closing parenthesis.
4729 If we find the closing `)', and the next token is a `{', then
4730 we are looking at a compound-literal. */
4732 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
4733 /*consume_paren=*/true)
4734 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4735 /* Roll back the tokens we skipped. */
4736 cp_lexer_rollback_tokens (parser
->lexer
);
4737 /* If we were looking at a compound-literal, simulate an error
4738 so that the call to cp_parser_parse_definitely below will
4740 if (compound_literal_p
)
4741 cp_parser_simulate_error (parser
);
4744 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4745 parser
->in_type_id_in_expr_p
= true;
4746 /* Look for the type-id. */
4747 type
= cp_parser_type_id (parser
);
4748 /* Look for the closing `)'. */
4749 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4750 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4753 /* Restore the saved message. */
4754 parser
->type_definition_forbidden_message
= saved_message
;
4756 /* If ok so far, parse the dependent expression. We cannot be
4757 sure it is a cast. Consider `(T ())'. It is a parenthesized
4758 ctor of T, but looks like a cast to function returning T
4759 without a dependent expression. */
4760 if (!cp_parser_error_occurred (parser
))
4761 expr
= cp_parser_simple_cast_expression (parser
);
4763 if (cp_parser_parse_definitely (parser
))
4765 /* Warn about old-style casts, if so requested. */
4766 if (warn_old_style_cast
4767 && !in_system_header
4768 && !VOID_TYPE_P (type
)
4769 && current_lang_name
!= lang_name_c
)
4770 warning ("use of old-style cast");
4772 /* Only type conversions to integral or enumeration types
4773 can be used in constant-expressions. */
4774 if (parser
->integral_constant_expression_p
4775 && !dependent_type_p (type
)
4776 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4778 if (!parser
->allow_non_integral_constant_expression_p
)
4779 return (cp_parser_non_integral_constant_expression
4780 ("a casts to a type other than an integral or "
4781 "enumeration type"));
4782 parser
->non_integral_constant_expression_p
= true;
4784 /* Perform the cast. */
4785 expr
= build_c_cast (type
, expr
);
4790 /* If we get here, then it's not a cast, so it must be a
4791 unary-expression. */
4792 return cp_parser_unary_expression (parser
, address_p
);
4795 /* Parse a pm-expression.
4799 pm-expression .* cast-expression
4800 pm-expression ->* cast-expression
4802 Returns a representation of the expression. */
4805 cp_parser_pm_expression (cp_parser
* parser
)
4807 static const cp_parser_token_tree_map map
= {
4808 { CPP_DEREF_STAR
, MEMBER_REF
},
4809 { CPP_DOT_STAR
, DOTSTAR_EXPR
},
4810 { CPP_EOF
, ERROR_MARK
}
4813 return cp_parser_binary_expression (parser
, map
,
4814 cp_parser_simple_cast_expression
);
4817 /* Parse a multiplicative-expression.
4819 mulitplicative-expression:
4821 multiplicative-expression * pm-expression
4822 multiplicative-expression / pm-expression
4823 multiplicative-expression % pm-expression
4825 Returns a representation of the expression. */
4828 cp_parser_multiplicative_expression (cp_parser
* parser
)
4830 static const cp_parser_token_tree_map map
= {
4831 { CPP_MULT
, MULT_EXPR
},
4832 { CPP_DIV
, TRUNC_DIV_EXPR
},
4833 { CPP_MOD
, TRUNC_MOD_EXPR
},
4834 { CPP_EOF
, ERROR_MARK
}
4837 return cp_parser_binary_expression (parser
,
4839 cp_parser_pm_expression
);
4842 /* Parse an additive-expression.
4844 additive-expression:
4845 multiplicative-expression
4846 additive-expression + multiplicative-expression
4847 additive-expression - multiplicative-expression
4849 Returns a representation of the expression. */
4852 cp_parser_additive_expression (cp_parser
* parser
)
4854 static const cp_parser_token_tree_map map
= {
4855 { CPP_PLUS
, PLUS_EXPR
},
4856 { CPP_MINUS
, MINUS_EXPR
},
4857 { CPP_EOF
, ERROR_MARK
}
4860 return cp_parser_binary_expression (parser
,
4862 cp_parser_multiplicative_expression
);
4865 /* Parse a shift-expression.
4869 shift-expression << additive-expression
4870 shift-expression >> additive-expression
4872 Returns a representation of the expression. */
4875 cp_parser_shift_expression (cp_parser
* parser
)
4877 static const cp_parser_token_tree_map map
= {
4878 { CPP_LSHIFT
, LSHIFT_EXPR
},
4879 { CPP_RSHIFT
, RSHIFT_EXPR
},
4880 { CPP_EOF
, ERROR_MARK
}
4883 return cp_parser_binary_expression (parser
,
4885 cp_parser_additive_expression
);
4888 /* Parse a relational-expression.
4890 relational-expression:
4892 relational-expression < shift-expression
4893 relational-expression > shift-expression
4894 relational-expression <= shift-expression
4895 relational-expression >= shift-expression
4899 relational-expression:
4900 relational-expression <? shift-expression
4901 relational-expression >? shift-expression
4903 Returns a representation of the expression. */
4906 cp_parser_relational_expression (cp_parser
* parser
)
4908 static const cp_parser_token_tree_map map
= {
4909 { CPP_LESS
, LT_EXPR
},
4910 { CPP_GREATER
, GT_EXPR
},
4911 { CPP_LESS_EQ
, LE_EXPR
},
4912 { CPP_GREATER_EQ
, GE_EXPR
},
4913 { CPP_MIN
, MIN_EXPR
},
4914 { CPP_MAX
, MAX_EXPR
},
4915 { CPP_EOF
, ERROR_MARK
}
4918 return cp_parser_binary_expression (parser
,
4920 cp_parser_shift_expression
);
4923 /* Parse an equality-expression.
4925 equality-expression:
4926 relational-expression
4927 equality-expression == relational-expression
4928 equality-expression != relational-expression
4930 Returns a representation of the expression. */
4933 cp_parser_equality_expression (cp_parser
* parser
)
4935 static const cp_parser_token_tree_map map
= {
4936 { CPP_EQ_EQ
, EQ_EXPR
},
4937 { CPP_NOT_EQ
, NE_EXPR
},
4938 { CPP_EOF
, ERROR_MARK
}
4941 return cp_parser_binary_expression (parser
,
4943 cp_parser_relational_expression
);
4946 /* Parse an and-expression.
4950 and-expression & equality-expression
4952 Returns a representation of the expression. */
4955 cp_parser_and_expression (cp_parser
* parser
)
4957 static const cp_parser_token_tree_map map
= {
4958 { CPP_AND
, BIT_AND_EXPR
},
4959 { CPP_EOF
, ERROR_MARK
}
4962 return cp_parser_binary_expression (parser
,
4964 cp_parser_equality_expression
);
4967 /* Parse an exclusive-or-expression.
4969 exclusive-or-expression:
4971 exclusive-or-expression ^ and-expression
4973 Returns a representation of the expression. */
4976 cp_parser_exclusive_or_expression (cp_parser
* parser
)
4978 static const cp_parser_token_tree_map map
= {
4979 { CPP_XOR
, BIT_XOR_EXPR
},
4980 { CPP_EOF
, ERROR_MARK
}
4983 return cp_parser_binary_expression (parser
,
4985 cp_parser_and_expression
);
4989 /* Parse an inclusive-or-expression.
4991 inclusive-or-expression:
4992 exclusive-or-expression
4993 inclusive-or-expression | exclusive-or-expression
4995 Returns a representation of the expression. */
4998 cp_parser_inclusive_or_expression (cp_parser
* parser
)
5000 static const cp_parser_token_tree_map map
= {
5001 { CPP_OR
, BIT_IOR_EXPR
},
5002 { CPP_EOF
, ERROR_MARK
}
5005 return cp_parser_binary_expression (parser
,
5007 cp_parser_exclusive_or_expression
);
5010 /* Parse a logical-and-expression.
5012 logical-and-expression:
5013 inclusive-or-expression
5014 logical-and-expression && inclusive-or-expression
5016 Returns a representation of the expression. */
5019 cp_parser_logical_and_expression (cp_parser
* parser
)
5021 static const cp_parser_token_tree_map map
= {
5022 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
5023 { CPP_EOF
, ERROR_MARK
}
5026 return cp_parser_binary_expression (parser
,
5028 cp_parser_inclusive_or_expression
);
5031 /* Parse a logical-or-expression.
5033 logical-or-expression:
5034 logical-and-expression
5035 logical-or-expression || logical-and-expression
5037 Returns a representation of the expression. */
5040 cp_parser_logical_or_expression (cp_parser
* parser
)
5042 static const cp_parser_token_tree_map map
= {
5043 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
5044 { CPP_EOF
, ERROR_MARK
}
5047 return cp_parser_binary_expression (parser
,
5049 cp_parser_logical_and_expression
);
5052 /* Parse the `? expression : assignment-expression' part of a
5053 conditional-expression. The LOGICAL_OR_EXPR is the
5054 logical-or-expression that started the conditional-expression.
5055 Returns a representation of the entire conditional-expression.
5057 This routine is used by cp_parser_assignment_expression.
5059 ? expression : assignment-expression
5063 ? : assignment-expression */
5066 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5069 tree assignment_expr
;
5071 /* Consume the `?' token. */
5072 cp_lexer_consume_token (parser
->lexer
);
5073 if (cp_parser_allow_gnu_extensions_p (parser
)
5074 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5075 /* Implicit true clause. */
5078 /* Parse the expression. */
5079 expr
= cp_parser_expression (parser
);
5081 /* The next token should be a `:'. */
5082 cp_parser_require (parser
, CPP_COLON
, "`:'");
5083 /* Parse the assignment-expression. */
5084 assignment_expr
= cp_parser_assignment_expression (parser
);
5086 /* Build the conditional-expression. */
5087 return build_x_conditional_expr (logical_or_expr
,
5092 /* Parse an assignment-expression.
5094 assignment-expression:
5095 conditional-expression
5096 logical-or-expression assignment-operator assignment_expression
5099 Returns a representation for the expression. */
5102 cp_parser_assignment_expression (cp_parser
* parser
)
5106 /* If the next token is the `throw' keyword, then we're looking at
5107 a throw-expression. */
5108 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5109 expr
= cp_parser_throw_expression (parser
);
5110 /* Otherwise, it must be that we are looking at a
5111 logical-or-expression. */
5114 /* Parse the logical-or-expression. */
5115 expr
= cp_parser_logical_or_expression (parser
);
5116 /* If the next token is a `?' then we're actually looking at a
5117 conditional-expression. */
5118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5119 return cp_parser_question_colon_clause (parser
, expr
);
5122 enum tree_code assignment_operator
;
5124 /* If it's an assignment-operator, we're using the second
5127 = cp_parser_assignment_operator_opt (parser
);
5128 if (assignment_operator
!= ERROR_MARK
)
5132 /* Parse the right-hand side of the assignment. */
5133 rhs
= cp_parser_assignment_expression (parser
);
5134 /* An assignment may not appear in a
5135 constant-expression. */
5136 if (parser
->integral_constant_expression_p
)
5138 if (!parser
->allow_non_integral_constant_expression_p
)
5139 return cp_parser_non_integral_constant_expression ("an assignment");
5140 parser
->non_integral_constant_expression_p
= true;
5142 /* Build the assignment expression. */
5143 expr
= build_x_modify_expr (expr
,
5144 assignment_operator
,
5153 /* Parse an (optional) assignment-operator.
5155 assignment-operator: one of
5156 = *= /= %= += -= >>= <<= &= ^= |=
5160 assignment-operator: one of
5163 If the next token is an assignment operator, the corresponding tree
5164 code is returned, and the token is consumed. For example, for
5165 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5166 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5167 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5168 operator, ERROR_MARK is returned. */
5170 static enum tree_code
5171 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5176 /* Peek at the next toen. */
5177 token
= cp_lexer_peek_token (parser
->lexer
);
5179 switch (token
->type
)
5190 op
= TRUNC_DIV_EXPR
;
5194 op
= TRUNC_MOD_EXPR
;
5234 /* Nothing else is an assignment operator. */
5238 /* If it was an assignment operator, consume it. */
5239 if (op
!= ERROR_MARK
)
5240 cp_lexer_consume_token (parser
->lexer
);
5245 /* Parse an expression.
5248 assignment-expression
5249 expression , assignment-expression
5251 Returns a representation of the expression. */
5254 cp_parser_expression (cp_parser
* parser
)
5256 tree expression
= NULL_TREE
;
5260 tree assignment_expression
;
5262 /* Parse the next assignment-expression. */
5263 assignment_expression
5264 = cp_parser_assignment_expression (parser
);
5265 /* If this is the first assignment-expression, we can just
5268 expression
= assignment_expression
;
5270 expression
= build_x_compound_expr (expression
,
5271 assignment_expression
);
5272 /* If the next token is not a comma, then we are done with the
5274 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5276 /* Consume the `,'. */
5277 cp_lexer_consume_token (parser
->lexer
);
5278 /* A comma operator cannot appear in a constant-expression. */
5279 if (parser
->integral_constant_expression_p
)
5281 if (!parser
->allow_non_integral_constant_expression_p
)
5283 = cp_parser_non_integral_constant_expression ("a comma operator");
5284 parser
->non_integral_constant_expression_p
= true;
5291 /* Parse a constant-expression.
5293 constant-expression:
5294 conditional-expression
5296 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5297 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5298 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5299 is false, NON_CONSTANT_P should be NULL. */
5302 cp_parser_constant_expression (cp_parser
* parser
,
5303 bool allow_non_constant_p
,
5304 bool *non_constant_p
)
5306 bool saved_integral_constant_expression_p
;
5307 bool saved_allow_non_integral_constant_expression_p
;
5308 bool saved_non_integral_constant_expression_p
;
5311 /* It might seem that we could simply parse the
5312 conditional-expression, and then check to see if it were
5313 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5314 one that the compiler can figure out is constant, possibly after
5315 doing some simplifications or optimizations. The standard has a
5316 precise definition of constant-expression, and we must honor
5317 that, even though it is somewhat more restrictive.
5323 is not a legal declaration, because `(2, 3)' is not a
5324 constant-expression. The `,' operator is forbidden in a
5325 constant-expression. However, GCC's constant-folding machinery
5326 will fold this operation to an INTEGER_CST for `3'. */
5328 /* Save the old settings. */
5329 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5330 saved_allow_non_integral_constant_expression_p
5331 = parser
->allow_non_integral_constant_expression_p
;
5332 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5333 /* We are now parsing a constant-expression. */
5334 parser
->integral_constant_expression_p
= true;
5335 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5336 parser
->non_integral_constant_expression_p
= false;
5337 /* Although the grammar says "conditional-expression", we parse an
5338 "assignment-expression", which also permits "throw-expression"
5339 and the use of assignment operators. In the case that
5340 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5341 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5342 actually essential that we look for an assignment-expression.
5343 For example, cp_parser_initializer_clauses uses this function to
5344 determine whether a particular assignment-expression is in fact
5346 expression
= cp_parser_assignment_expression (parser
);
5347 /* Restore the old settings. */
5348 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
5349 parser
->allow_non_integral_constant_expression_p
5350 = saved_allow_non_integral_constant_expression_p
;
5351 if (allow_non_constant_p
)
5352 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5353 parser
->non_integral_constant_expression_p
= saved_non_integral_constant_expression_p
;
5358 /* Statements [gram.stmt.stmt] */
5360 /* Parse a statement.
5364 expression-statement
5369 declaration-statement
5373 cp_parser_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5377 int statement_line_number
;
5379 /* There is no statement yet. */
5380 statement
= NULL_TREE
;
5381 /* Peek at the next token. */
5382 token
= cp_lexer_peek_token (parser
->lexer
);
5383 /* Remember the line number of the first token in the statement. */
5384 statement_line_number
= token
->location
.line
;
5385 /* If this is a keyword, then that will often determine what kind of
5386 statement we have. */
5387 if (token
->type
== CPP_KEYWORD
)
5389 enum rid keyword
= token
->keyword
;
5395 statement
= cp_parser_labeled_statement (parser
,
5396 in_statement_expr_p
);
5401 statement
= cp_parser_selection_statement (parser
);
5407 statement
= cp_parser_iteration_statement (parser
);
5414 statement
= cp_parser_jump_statement (parser
);
5418 statement
= cp_parser_try_block (parser
);
5422 /* It might be a keyword like `int' that can start a
5423 declaration-statement. */
5427 else if (token
->type
== CPP_NAME
)
5429 /* If the next token is a `:', then we are looking at a
5430 labeled-statement. */
5431 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5432 if (token
->type
== CPP_COLON
)
5433 statement
= cp_parser_labeled_statement (parser
, in_statement_expr_p
);
5435 /* Anything that starts with a `{' must be a compound-statement. */
5436 else if (token
->type
== CPP_OPEN_BRACE
)
5437 statement
= cp_parser_compound_statement (parser
, false);
5439 /* Everything else must be a declaration-statement or an
5440 expression-statement. Try for the declaration-statement
5441 first, unless we are looking at a `;', in which case we know that
5442 we have an expression-statement. */
5445 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5447 cp_parser_parse_tentatively (parser
);
5448 /* Try to parse the declaration-statement. */
5449 cp_parser_declaration_statement (parser
);
5450 /* If that worked, we're done. */
5451 if (cp_parser_parse_definitely (parser
))
5454 /* Look for an expression-statement instead. */
5455 statement
= cp_parser_expression_statement (parser
, in_statement_expr_p
);
5458 /* Set the line number for the statement. */
5459 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5460 STMT_LINENO (statement
) = statement_line_number
;
5463 /* Parse a labeled-statement.
5466 identifier : statement
5467 case constant-expression : statement
5470 Returns the new CASE_LABEL, for a `case' or `default' label. For
5471 an ordinary label, returns a LABEL_STMT. */
5474 cp_parser_labeled_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5477 tree statement
= error_mark_node
;
5479 /* The next token should be an identifier. */
5480 token
= cp_lexer_peek_token (parser
->lexer
);
5481 if (token
->type
!= CPP_NAME
5482 && token
->type
!= CPP_KEYWORD
)
5484 cp_parser_error (parser
, "expected labeled-statement");
5485 return error_mark_node
;
5488 switch (token
->keyword
)
5494 /* Consume the `case' token. */
5495 cp_lexer_consume_token (parser
->lexer
);
5496 /* Parse the constant-expression. */
5497 expr
= cp_parser_constant_expression (parser
,
5498 /*allow_non_constant_p=*/false,
5500 if (!parser
->in_switch_statement_p
)
5501 error ("case label `%E' not within a switch statement", expr
);
5503 statement
= finish_case_label (expr
, NULL_TREE
);
5508 /* Consume the `default' token. */
5509 cp_lexer_consume_token (parser
->lexer
);
5510 if (!parser
->in_switch_statement_p
)
5511 error ("case label not within a switch statement");
5513 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5517 /* Anything else must be an ordinary label. */
5518 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5522 /* Require the `:' token. */
5523 cp_parser_require (parser
, CPP_COLON
, "`:'");
5524 /* Parse the labeled statement. */
5525 cp_parser_statement (parser
, in_statement_expr_p
);
5527 /* Return the label, in the case of a `case' or `default' label. */
5531 /* Parse an expression-statement.
5533 expression-statement:
5536 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5537 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5538 indicates whether this expression-statement is part of an
5539 expression statement. */
5542 cp_parser_expression_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5544 tree statement
= NULL_TREE
;
5546 /* If the next token is a ';', then there is no expression
5548 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5549 statement
= cp_parser_expression (parser
);
5551 /* Consume the final `;'. */
5552 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5554 if (in_statement_expr_p
5555 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
5557 /* This is the final expression statement of a statement
5559 statement
= finish_stmt_expr_expr (statement
);
5562 statement
= finish_expr_stmt (statement
);
5569 /* Parse a compound-statement.
5572 { statement-seq [opt] }
5574 Returns a COMPOUND_STMT representing the statement. */
5577 cp_parser_compound_statement (cp_parser
*parser
, bool in_statement_expr_p
)
5581 /* Consume the `{'. */
5582 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5583 return error_mark_node
;
5584 /* Begin the compound-statement. */
5585 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/false);
5586 /* Parse an (optional) statement-seq. */
5587 cp_parser_statement_seq_opt (parser
, in_statement_expr_p
);
5588 /* Finish the compound-statement. */
5589 finish_compound_stmt (compound_stmt
);
5590 /* Consume the `}'. */
5591 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5593 return compound_stmt
;
5596 /* Parse an (optional) statement-seq.
5600 statement-seq [opt] statement */
5603 cp_parser_statement_seq_opt (cp_parser
* parser
, bool in_statement_expr_p
)
5605 /* Scan statements until there aren't any more. */
5608 /* If we're looking at a `}', then we've run out of statements. */
5609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5610 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5613 /* Parse the statement. */
5614 cp_parser_statement (parser
, in_statement_expr_p
);
5618 /* Parse a selection-statement.
5620 selection-statement:
5621 if ( condition ) statement
5622 if ( condition ) statement else statement
5623 switch ( condition ) statement
5625 Returns the new IF_STMT or SWITCH_STMT. */
5628 cp_parser_selection_statement (cp_parser
* parser
)
5633 /* Peek at the next token. */
5634 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5636 /* See what kind of keyword it is. */
5637 keyword
= token
->keyword
;
5646 /* Look for the `('. */
5647 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5649 cp_parser_skip_to_end_of_statement (parser
);
5650 return error_mark_node
;
5653 /* Begin the selection-statement. */
5654 if (keyword
== RID_IF
)
5655 statement
= begin_if_stmt ();
5657 statement
= begin_switch_stmt ();
5659 /* Parse the condition. */
5660 condition
= cp_parser_condition (parser
);
5661 /* Look for the `)'. */
5662 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5663 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
5664 /*consume_paren=*/true);
5666 if (keyword
== RID_IF
)
5670 /* Add the condition. */
5671 finish_if_stmt_cond (condition
, statement
);
5673 /* Parse the then-clause. */
5674 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5675 finish_then_clause (statement
);
5677 /* If the next token is `else', parse the else-clause. */
5678 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5683 /* Consume the `else' keyword. */
5684 cp_lexer_consume_token (parser
->lexer
);
5685 /* Parse the else-clause. */
5687 = cp_parser_implicitly_scoped_statement (parser
);
5688 finish_else_clause (statement
);
5691 /* Now we're all done with the if-statement. */
5697 bool in_switch_statement_p
;
5699 /* Add the condition. */
5700 finish_switch_cond (condition
, statement
);
5702 /* Parse the body of the switch-statement. */
5703 in_switch_statement_p
= parser
->in_switch_statement_p
;
5704 parser
->in_switch_statement_p
= true;
5705 body
= cp_parser_implicitly_scoped_statement (parser
);
5706 parser
->in_switch_statement_p
= in_switch_statement_p
;
5708 /* Now we're all done with the switch-statement. */
5709 finish_switch_stmt (statement
);
5717 cp_parser_error (parser
, "expected selection-statement");
5718 return error_mark_node
;
5722 /* Parse a condition.
5726 type-specifier-seq declarator = assignment-expression
5731 type-specifier-seq declarator asm-specification [opt]
5732 attributes [opt] = assignment-expression
5734 Returns the expression that should be tested. */
5737 cp_parser_condition (cp_parser
* parser
)
5739 tree type_specifiers
;
5740 const char *saved_message
;
5742 /* Try the declaration first. */
5743 cp_parser_parse_tentatively (parser
);
5744 /* New types are not allowed in the type-specifier-seq for a
5746 saved_message
= parser
->type_definition_forbidden_message
;
5747 parser
->type_definition_forbidden_message
5748 = "types may not be defined in conditions";
5749 /* Parse the type-specifier-seq. */
5750 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5751 /* Restore the saved message. */
5752 parser
->type_definition_forbidden_message
= saved_message
;
5753 /* If all is well, we might be looking at a declaration. */
5754 if (!cp_parser_error_occurred (parser
))
5757 tree asm_specification
;
5760 tree initializer
= NULL_TREE
;
5762 /* Parse the declarator. */
5763 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5764 /*ctor_dtor_or_conv_p=*/NULL
,
5765 /*parenthesized_p=*/NULL
);
5766 /* Parse the attributes. */
5767 attributes
= cp_parser_attributes_opt (parser
);
5768 /* Parse the asm-specification. */
5769 asm_specification
= cp_parser_asm_specification_opt (parser
);
5770 /* If the next token is not an `=', then we might still be
5771 looking at an expression. For example:
5775 looks like a decl-specifier-seq and a declarator -- but then
5776 there is no `=', so this is an expression. */
5777 cp_parser_require (parser
, CPP_EQ
, "`='");
5778 /* If we did see an `=', then we are looking at a declaration
5780 if (cp_parser_parse_definitely (parser
))
5782 /* Create the declaration. */
5783 decl
= start_decl (declarator
, type_specifiers
,
5784 /*initialized_p=*/true,
5785 attributes
, /*prefix_attributes=*/NULL_TREE
);
5786 /* Parse the assignment-expression. */
5787 initializer
= cp_parser_assignment_expression (parser
);
5789 /* Process the initializer. */
5790 cp_finish_decl (decl
,
5793 LOOKUP_ONLYCONVERTING
);
5795 return convert_from_reference (decl
);
5798 /* If we didn't even get past the declarator successfully, we are
5799 definitely not looking at a declaration. */
5801 cp_parser_abort_tentative_parse (parser
);
5803 /* Otherwise, we are looking at an expression. */
5804 return cp_parser_expression (parser
);
5807 /* Parse an iteration-statement.
5809 iteration-statement:
5810 while ( condition ) statement
5811 do statement while ( expression ) ;
5812 for ( for-init-statement condition [opt] ; expression [opt] )
5815 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5818 cp_parser_iteration_statement (cp_parser
* parser
)
5823 bool in_iteration_statement_p
;
5826 /* Peek at the next token. */
5827 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
5829 return error_mark_node
;
5831 /* Remember whether or not we are already within an iteration
5833 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
5835 /* See what kind of keyword it is. */
5836 keyword
= token
->keyword
;
5843 /* Begin the while-statement. */
5844 statement
= begin_while_stmt ();
5845 /* Look for the `('. */
5846 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5847 /* Parse the condition. */
5848 condition
= cp_parser_condition (parser
);
5849 finish_while_stmt_cond (condition
, statement
);
5850 /* Look for the `)'. */
5851 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5852 /* Parse the dependent statement. */
5853 parser
->in_iteration_statement_p
= true;
5854 cp_parser_already_scoped_statement (parser
);
5855 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5856 /* We're done with the while-statement. */
5857 finish_while_stmt (statement
);
5865 /* Begin the do-statement. */
5866 statement
= begin_do_stmt ();
5867 /* Parse the body of the do-statement. */
5868 parser
->in_iteration_statement_p
= true;
5869 cp_parser_implicitly_scoped_statement (parser
);
5870 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5871 finish_do_body (statement
);
5872 /* Look for the `while' keyword. */
5873 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
5874 /* Look for the `('. */
5875 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5876 /* Parse the expression. */
5877 expression
= cp_parser_expression (parser
);
5878 /* We're done with the do-statement. */
5879 finish_do_stmt (expression
, statement
);
5880 /* Look for the `)'. */
5881 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5882 /* Look for the `;'. */
5883 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5889 tree condition
= NULL_TREE
;
5890 tree expression
= NULL_TREE
;
5892 /* Begin the for-statement. */
5893 statement
= begin_for_stmt ();
5894 /* Look for the `('. */
5895 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5896 /* Parse the initialization. */
5897 cp_parser_for_init_statement (parser
);
5898 finish_for_init_stmt (statement
);
5900 /* If there's a condition, process it. */
5901 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5902 condition
= cp_parser_condition (parser
);
5903 finish_for_cond (condition
, statement
);
5904 /* Look for the `;'. */
5905 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5907 /* If there's an expression, process it. */
5908 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5909 expression
= cp_parser_expression (parser
);
5910 finish_for_expr (expression
, statement
);
5911 /* Look for the `)'. */
5912 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`;'");
5914 /* Parse the body of the for-statement. */
5915 parser
->in_iteration_statement_p
= true;
5916 cp_parser_already_scoped_statement (parser
);
5917 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5919 /* We're done with the for-statement. */
5920 finish_for_stmt (statement
);
5925 cp_parser_error (parser
, "expected iteration-statement");
5926 statement
= error_mark_node
;
5933 /* Parse a for-init-statement.
5936 expression-statement
5937 simple-declaration */
5940 cp_parser_for_init_statement (cp_parser
* parser
)
5942 /* If the next token is a `;', then we have an empty
5943 expression-statement. Grammatically, this is also a
5944 simple-declaration, but an invalid one, because it does not
5945 declare anything. Therefore, if we did not handle this case
5946 specially, we would issue an error message about an invalid
5948 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5950 /* We're going to speculatively look for a declaration, falling back
5951 to an expression, if necessary. */
5952 cp_parser_parse_tentatively (parser
);
5953 /* Parse the declaration. */
5954 cp_parser_simple_declaration (parser
,
5955 /*function_definition_allowed_p=*/false);
5956 /* If the tentative parse failed, then we shall need to look for an
5957 expression-statement. */
5958 if (cp_parser_parse_definitely (parser
))
5962 cp_parser_expression_statement (parser
, false);
5965 /* Parse a jump-statement.
5970 return expression [opt] ;
5978 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5982 cp_parser_jump_statement (cp_parser
* parser
)
5984 tree statement
= error_mark_node
;
5988 /* Peek at the next token. */
5989 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
5991 return error_mark_node
;
5993 /* See what kind of keyword it is. */
5994 keyword
= token
->keyword
;
5998 if (!parser
->in_switch_statement_p
5999 && !parser
->in_iteration_statement_p
)
6001 error ("break statement not within loop or switch");
6002 statement
= error_mark_node
;
6005 statement
= finish_break_stmt ();
6006 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6010 if (!parser
->in_iteration_statement_p
)
6012 error ("continue statement not within a loop");
6013 statement
= error_mark_node
;
6016 statement
= finish_continue_stmt ();
6017 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6024 /* If the next token is a `;', then there is no
6026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6027 expr
= cp_parser_expression (parser
);
6030 /* Build the return-statement. */
6031 statement
= finish_return_stmt (expr
);
6032 /* Look for the final `;'. */
6033 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6038 /* Create the goto-statement. */
6039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6041 /* Issue a warning about this use of a GNU extension. */
6043 pedwarn ("ISO C++ forbids computed gotos");
6044 /* Consume the '*' token. */
6045 cp_lexer_consume_token (parser
->lexer
);
6046 /* Parse the dependent expression. */
6047 finish_goto_stmt (cp_parser_expression (parser
));
6050 finish_goto_stmt (cp_parser_identifier (parser
));
6051 /* Look for the final `;'. */
6052 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6056 cp_parser_error (parser
, "expected jump-statement");
6063 /* Parse a declaration-statement.
6065 declaration-statement:
6066 block-declaration */
6069 cp_parser_declaration_statement (cp_parser
* parser
)
6071 /* Parse the block-declaration. */
6072 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6074 /* Finish off the statement. */
6078 /* Some dependent statements (like `if (cond) statement'), are
6079 implicitly in their own scope. In other words, if the statement is
6080 a single statement (as opposed to a compound-statement), it is
6081 none-the-less treated as if it were enclosed in braces. Any
6082 declarations appearing in the dependent statement are out of scope
6083 after control passes that point. This function parses a statement,
6084 but ensures that is in its own scope, even if it is not a
6087 Returns the new statement. */
6090 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6094 /* If the token is not a `{', then we must take special action. */
6095 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6097 /* Create a compound-statement. */
6098 statement
= begin_compound_stmt (/*has_no_scope=*/false);
6099 /* Parse the dependent-statement. */
6100 cp_parser_statement (parser
, false);
6101 /* Finish the dummy compound-statement. */
6102 finish_compound_stmt (statement
);
6104 /* Otherwise, we simply parse the statement directly. */
6106 statement
= cp_parser_compound_statement (parser
, false);
6108 /* Return the statement. */
6112 /* For some dependent statements (like `while (cond) statement'), we
6113 have already created a scope. Therefore, even if the dependent
6114 statement is a compound-statement, we do not want to create another
6118 cp_parser_already_scoped_statement (cp_parser
* parser
)
6120 /* If the token is not a `{', then we must take special action. */
6121 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
6125 /* Create a compound-statement. */
6126 statement
= begin_compound_stmt (/*has_no_scope=*/true);
6127 /* Parse the dependent-statement. */
6128 cp_parser_statement (parser
, false);
6129 /* Finish the dummy compound-statement. */
6130 finish_compound_stmt (statement
);
6132 /* Otherwise, we simply parse the statement directly. */
6134 cp_parser_statement (parser
, false);
6137 /* Declarations [gram.dcl.dcl] */
6139 /* Parse an optional declaration-sequence.
6143 declaration-seq declaration */
6146 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6152 token
= cp_lexer_peek_token (parser
->lexer
);
6154 if (token
->type
== CPP_CLOSE_BRACE
6155 || token
->type
== CPP_EOF
)
6158 if (token
->type
== CPP_SEMICOLON
)
6160 /* A declaration consisting of a single semicolon is
6161 invalid. Allow it unless we're being pedantic. */
6162 if (pedantic
&& !in_system_header
)
6163 pedwarn ("extra `;'");
6164 cp_lexer_consume_token (parser
->lexer
);
6168 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6169 parser to enter or exit implicit `extern "C"' blocks. */
6170 while (pending_lang_change
> 0)
6172 push_lang_context (lang_name_c
);
6173 --pending_lang_change
;
6175 while (pending_lang_change
< 0)
6177 pop_lang_context ();
6178 ++pending_lang_change
;
6181 /* Parse the declaration itself. */
6182 cp_parser_declaration (parser
);
6186 /* Parse a declaration.
6191 template-declaration
6192 explicit-instantiation
6193 explicit-specialization
6194 linkage-specification
6195 namespace-definition
6200 __extension__ declaration */
6203 cp_parser_declaration (cp_parser
* parser
)
6209 /* Check for the `__extension__' keyword. */
6210 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6212 /* Parse the qualified declaration. */
6213 cp_parser_declaration (parser
);
6214 /* Restore the PEDANTIC flag. */
6215 pedantic
= saved_pedantic
;
6220 /* Try to figure out what kind of declaration is present. */
6221 token1
= *cp_lexer_peek_token (parser
->lexer
);
6222 if (token1
.type
!= CPP_EOF
)
6223 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6225 /* If the next token is `extern' and the following token is a string
6226 literal, then we have a linkage specification. */
6227 if (token1
.keyword
== RID_EXTERN
6228 && cp_parser_is_string_literal (&token2
))
6229 cp_parser_linkage_specification (parser
);
6230 /* If the next token is `template', then we have either a template
6231 declaration, an explicit instantiation, or an explicit
6233 else if (token1
.keyword
== RID_TEMPLATE
)
6235 /* `template <>' indicates a template specialization. */
6236 if (token2
.type
== CPP_LESS
6237 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6238 cp_parser_explicit_specialization (parser
);
6239 /* `template <' indicates a template declaration. */
6240 else if (token2
.type
== CPP_LESS
)
6241 cp_parser_template_declaration (parser
, /*member_p=*/false);
6242 /* Anything else must be an explicit instantiation. */
6244 cp_parser_explicit_instantiation (parser
);
6246 /* If the next token is `export', then we have a template
6248 else if (token1
.keyword
== RID_EXPORT
)
6249 cp_parser_template_declaration (parser
, /*member_p=*/false);
6250 /* If the next token is `extern', 'static' or 'inline' and the one
6251 after that is `template', we have a GNU extended explicit
6252 instantiation directive. */
6253 else if (cp_parser_allow_gnu_extensions_p (parser
)
6254 && (token1
.keyword
== RID_EXTERN
6255 || token1
.keyword
== RID_STATIC
6256 || token1
.keyword
== RID_INLINE
)
6257 && token2
.keyword
== RID_TEMPLATE
)
6258 cp_parser_explicit_instantiation (parser
);
6259 /* If the next token is `namespace', check for a named or unnamed
6260 namespace definition. */
6261 else if (token1
.keyword
== RID_NAMESPACE
6262 && (/* A named namespace definition. */
6263 (token2
.type
== CPP_NAME
6264 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6266 /* An unnamed namespace definition. */
6267 || token2
.type
== CPP_OPEN_BRACE
))
6268 cp_parser_namespace_definition (parser
);
6269 /* We must have either a block declaration or a function
6272 /* Try to parse a block-declaration, or a function-definition. */
6273 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6276 /* Parse a block-declaration.
6281 namespace-alias-definition
6288 __extension__ block-declaration
6291 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6292 part of a declaration-statement. */
6295 cp_parser_block_declaration (cp_parser
*parser
,
6301 /* Check for the `__extension__' keyword. */
6302 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6304 /* Parse the qualified declaration. */
6305 cp_parser_block_declaration (parser
, statement_p
);
6306 /* Restore the PEDANTIC flag. */
6307 pedantic
= saved_pedantic
;
6312 /* Peek at the next token to figure out which kind of declaration is
6314 token1
= cp_lexer_peek_token (parser
->lexer
);
6316 /* If the next keyword is `asm', we have an asm-definition. */
6317 if (token1
->keyword
== RID_ASM
)
6320 cp_parser_commit_to_tentative_parse (parser
);
6321 cp_parser_asm_definition (parser
);
6323 /* If the next keyword is `namespace', we have a
6324 namespace-alias-definition. */
6325 else if (token1
->keyword
== RID_NAMESPACE
)
6326 cp_parser_namespace_alias_definition (parser
);
6327 /* If the next keyword is `using', we have either a
6328 using-declaration or a using-directive. */
6329 else if (token1
->keyword
== RID_USING
)
6334 cp_parser_commit_to_tentative_parse (parser
);
6335 /* If the token after `using' is `namespace', then we have a
6337 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6338 if (token2
->keyword
== RID_NAMESPACE
)
6339 cp_parser_using_directive (parser
);
6340 /* Otherwise, it's a using-declaration. */
6342 cp_parser_using_declaration (parser
);
6344 /* If the next keyword is `__label__' we have a label declaration. */
6345 else if (token1
->keyword
== RID_LABEL
)
6348 cp_parser_commit_to_tentative_parse (parser
);
6349 cp_parser_label_declaration (parser
);
6351 /* Anything else must be a simple-declaration. */
6353 cp_parser_simple_declaration (parser
, !statement_p
);
6356 /* Parse a simple-declaration.
6359 decl-specifier-seq [opt] init-declarator-list [opt] ;
6361 init-declarator-list:
6363 init-declarator-list , init-declarator
6365 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6366 function-definition as a simple-declaration. */
6369 cp_parser_simple_declaration (cp_parser
* parser
,
6370 bool function_definition_allowed_p
)
6372 tree decl_specifiers
;
6374 int declares_class_or_enum
;
6375 bool saw_declarator
;
6377 /* Defer access checks until we know what is being declared; the
6378 checks for names appearing in the decl-specifier-seq should be
6379 done as if we were in the scope of the thing being declared. */
6380 push_deferring_access_checks (dk_deferred
);
6382 /* Parse the decl-specifier-seq. We have to keep track of whether
6383 or not the decl-specifier-seq declares a named class or
6384 enumeration type, since that is the only case in which the
6385 init-declarator-list is allowed to be empty.
6389 In a simple-declaration, the optional init-declarator-list can be
6390 omitted only when declaring a class or enumeration, that is when
6391 the decl-specifier-seq contains either a class-specifier, an
6392 elaborated-type-specifier, or an enum-specifier. */
6394 = cp_parser_decl_specifier_seq (parser
,
6395 CP_PARSER_FLAGS_OPTIONAL
,
6397 &declares_class_or_enum
);
6398 /* We no longer need to defer access checks. */
6399 stop_deferring_access_checks ();
6401 /* In a block scope, a valid declaration must always have a
6402 decl-specifier-seq. By not trying to parse declarators, we can
6403 resolve the declaration/expression ambiguity more quickly. */
6404 if (!function_definition_allowed_p
&& !decl_specifiers
)
6406 cp_parser_error (parser
, "expected declaration");
6410 /* If the next two tokens are both identifiers, the code is
6411 erroneous. The usual cause of this situation is code like:
6415 where "T" should name a type -- but does not. */
6416 if (cp_parser_diagnose_invalid_type_name (parser
))
6418 /* If parsing tentatively, we should commit; we really are
6419 looking at a declaration. */
6420 cp_parser_commit_to_tentative_parse (parser
);
6425 /* Keep going until we hit the `;' at the end of the simple
6427 saw_declarator
= false;
6428 while (cp_lexer_next_token_is_not (parser
->lexer
,
6432 bool function_definition_p
;
6435 saw_declarator
= true;
6436 /* Parse the init-declarator. */
6437 decl
= cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6438 function_definition_allowed_p
,
6440 declares_class_or_enum
,
6441 &function_definition_p
);
6442 /* If an error occurred while parsing tentatively, exit quickly.
6443 (That usually happens when in the body of a function; each
6444 statement is treated as a declaration-statement until proven
6446 if (cp_parser_error_occurred (parser
))
6448 /* Handle function definitions specially. */
6449 if (function_definition_p
)
6451 /* If the next token is a `,', then we are probably
6452 processing something like:
6456 which is erroneous. */
6457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6458 error ("mixing declarations and function-definitions is forbidden");
6459 /* Otherwise, we're done with the list of declarators. */
6462 pop_deferring_access_checks ();
6466 /* The next token should be either a `,' or a `;'. */
6467 token
= cp_lexer_peek_token (parser
->lexer
);
6468 /* If it's a `,', there are more declarators to come. */
6469 if (token
->type
== CPP_COMMA
)
6470 cp_lexer_consume_token (parser
->lexer
);
6471 /* If it's a `;', we are done. */
6472 else if (token
->type
== CPP_SEMICOLON
)
6474 /* Anything else is an error. */
6477 cp_parser_error (parser
, "expected `,' or `;'");
6478 /* Skip tokens until we reach the end of the statement. */
6479 cp_parser_skip_to_end_of_statement (parser
);
6482 /* After the first time around, a function-definition is not
6483 allowed -- even if it was OK at first. For example:
6488 function_definition_allowed_p
= false;
6491 /* Issue an error message if no declarators are present, and the
6492 decl-specifier-seq does not itself declare a class or
6494 if (!saw_declarator
)
6496 if (cp_parser_declares_only_class_p (parser
))
6497 shadow_tag (decl_specifiers
);
6498 /* Perform any deferred access checks. */
6499 perform_deferred_access_checks ();
6502 /* Consume the `;'. */
6503 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6506 pop_deferring_access_checks ();
6509 /* Parse a decl-specifier-seq.
6512 decl-specifier-seq [opt] decl-specifier
6515 storage-class-specifier
6524 decl-specifier-seq [opt] attributes
6526 Returns a TREE_LIST, giving the decl-specifiers in the order they
6527 appear in the source code. The TREE_VALUE of each node is the
6528 decl-specifier. For a keyword (such as `auto' or `friend'), the
6529 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6530 representation of a type-specifier, see cp_parser_type_specifier.
6532 If there are attributes, they will be stored in *ATTRIBUTES,
6533 represented as described above cp_parser_attributes.
6535 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6536 appears, and the entity that will be a friend is not going to be a
6537 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6538 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6539 friendship is granted might not be a class.
6541 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6544 1: one of the decl-specifiers is an elaborated-type-specifier
6545 (i.e., a type declaration)
6546 2: one of the decl-specifiers is an enum-specifier or a
6547 class-specifier (i.e., a type definition)
6552 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6553 cp_parser_flags flags
,
6555 int* declares_class_or_enum
)
6557 tree decl_specs
= NULL_TREE
;
6558 bool friend_p
= false;
6559 bool constructor_possible_p
= !parser
->in_declarator_p
;
6561 /* Assume no class or enumeration type is declared. */
6562 *declares_class_or_enum
= 0;
6564 /* Assume there are no attributes. */
6565 *attributes
= NULL_TREE
;
6567 /* Keep reading specifiers until there are no more to read. */
6570 tree decl_spec
= NULL_TREE
;
6574 /* Peek at the next token. */
6575 token
= cp_lexer_peek_token (parser
->lexer
);
6576 /* Handle attributes. */
6577 if (token
->keyword
== RID_ATTRIBUTE
)
6579 /* Parse the attributes. */
6580 decl_spec
= cp_parser_attributes_opt (parser
);
6581 /* Add them to the list. */
6582 *attributes
= chainon (*attributes
, decl_spec
);
6585 /* If the next token is an appropriate keyword, we can simply
6586 add it to the list. */
6587 switch (token
->keyword
)
6593 error ("duplicate `friend'");
6596 /* The representation of the specifier is simply the
6597 appropriate TREE_IDENTIFIER node. */
6598 decl_spec
= token
->value
;
6599 /* Consume the token. */
6600 cp_lexer_consume_token (parser
->lexer
);
6603 /* function-specifier:
6610 decl_spec
= cp_parser_function_specifier_opt (parser
);
6616 /* The representation of the specifier is simply the
6617 appropriate TREE_IDENTIFIER node. */
6618 decl_spec
= token
->value
;
6619 /* Consume the token. */
6620 cp_lexer_consume_token (parser
->lexer
);
6621 /* A constructor declarator cannot appear in a typedef. */
6622 constructor_possible_p
= false;
6623 /* The "typedef" keyword can only occur in a declaration; we
6624 may as well commit at this point. */
6625 cp_parser_commit_to_tentative_parse (parser
);
6628 /* storage-class-specifier:
6643 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6650 /* Constructors are a special case. The `S' in `S()' is not a
6651 decl-specifier; it is the beginning of the declarator. */
6652 constructor_p
= (!decl_spec
6653 && constructor_possible_p
6654 && cp_parser_constructor_declarator_p (parser
,
6657 /* If we don't have a DECL_SPEC yet, then we must be looking at
6658 a type-specifier. */
6659 if (!decl_spec
&& !constructor_p
)
6661 int decl_spec_declares_class_or_enum
;
6662 bool is_cv_qualifier
;
6665 = cp_parser_type_specifier (parser
, flags
,
6667 /*is_declaration=*/true,
6668 &decl_spec_declares_class_or_enum
,
6671 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6673 /* If this type-specifier referenced a user-defined type
6674 (a typedef, class-name, etc.), then we can't allow any
6675 more such type-specifiers henceforth.
6679 The longest sequence of decl-specifiers that could
6680 possibly be a type name is taken as the
6681 decl-specifier-seq of a declaration. The sequence shall
6682 be self-consistent as described below.
6686 As a general rule, at most one type-specifier is allowed
6687 in the complete decl-specifier-seq of a declaration. The
6688 only exceptions are the following:
6690 -- const or volatile can be combined with any other
6693 -- signed or unsigned can be combined with char, long,
6701 void g (const int Pc);
6703 Here, Pc is *not* part of the decl-specifier seq; it's
6704 the declarator. Therefore, once we see a type-specifier
6705 (other than a cv-qualifier), we forbid any additional
6706 user-defined types. We *do* still allow things like `int
6707 int' to be considered a decl-specifier-seq, and issue the
6708 error message later. */
6709 if (decl_spec
&& !is_cv_qualifier
)
6710 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6711 /* A constructor declarator cannot follow a type-specifier. */
6713 constructor_possible_p
= false;
6716 /* If we still do not have a DECL_SPEC, then there are no more
6720 /* Issue an error message, unless the entire construct was
6722 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6724 cp_parser_error (parser
, "expected decl specifier");
6725 return error_mark_node
;
6731 /* Add the DECL_SPEC to the list of specifiers. */
6732 if (decl_specs
== NULL
|| TREE_VALUE (decl_specs
) != error_mark_node
)
6733 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6735 /* After we see one decl-specifier, further decl-specifiers are
6737 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6740 /* Don't allow a friend specifier with a class definition. */
6741 if (friend_p
&& (*declares_class_or_enum
& 2))
6742 error ("class definition may not be declared a friend");
6744 /* We have built up the DECL_SPECS in reverse order. Return them in
6745 the correct order. */
6746 return nreverse (decl_specs
);
6749 /* Parse an (optional) storage-class-specifier.
6751 storage-class-specifier:
6760 storage-class-specifier:
6763 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6766 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
6768 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6776 /* Consume the token. */
6777 return cp_lexer_consume_token (parser
->lexer
)->value
;
6784 /* Parse an (optional) function-specifier.
6791 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6794 cp_parser_function_specifier_opt (cp_parser
* parser
)
6796 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6801 /* Consume the token. */
6802 return cp_lexer_consume_token (parser
->lexer
)->value
;
6809 /* Parse a linkage-specification.
6811 linkage-specification:
6812 extern string-literal { declaration-seq [opt] }
6813 extern string-literal declaration */
6816 cp_parser_linkage_specification (cp_parser
* parser
)
6821 /* Look for the `extern' keyword. */
6822 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
6824 /* Peek at the next token. */
6825 token
= cp_lexer_peek_token (parser
->lexer
);
6826 /* If it's not a string-literal, then there's a problem. */
6827 if (!cp_parser_is_string_literal (token
))
6829 cp_parser_error (parser
, "expected language-name");
6832 /* Consume the token. */
6833 cp_lexer_consume_token (parser
->lexer
);
6835 /* Transform the literal into an identifier. If the literal is a
6836 wide-character string, or contains embedded NULs, then we can't
6837 handle it as the user wants. */
6838 if (token
->type
== CPP_WSTRING
6839 || (strlen (TREE_STRING_POINTER (token
->value
))
6840 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
6842 cp_parser_error (parser
, "invalid linkage-specification");
6843 /* Assume C++ linkage. */
6844 linkage
= get_identifier ("c++");
6846 /* If it's a simple string constant, things are easier. */
6848 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
6850 /* We're now using the new linkage. */
6851 push_lang_context (linkage
);
6853 /* If the next token is a `{', then we're using the first
6855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6857 /* Consume the `{' token. */
6858 cp_lexer_consume_token (parser
->lexer
);
6859 /* Parse the declarations. */
6860 cp_parser_declaration_seq_opt (parser
);
6861 /* Look for the closing `}'. */
6862 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6864 /* Otherwise, there's just one declaration. */
6867 bool saved_in_unbraced_linkage_specification_p
;
6869 saved_in_unbraced_linkage_specification_p
6870 = parser
->in_unbraced_linkage_specification_p
;
6871 parser
->in_unbraced_linkage_specification_p
= true;
6872 have_extern_spec
= true;
6873 cp_parser_declaration (parser
);
6874 have_extern_spec
= false;
6875 parser
->in_unbraced_linkage_specification_p
6876 = saved_in_unbraced_linkage_specification_p
;
6879 /* We're done with the linkage-specification. */
6880 pop_lang_context ();
6883 /* Special member functions [gram.special] */
6885 /* Parse a conversion-function-id.
6887 conversion-function-id:
6888 operator conversion-type-id
6890 Returns an IDENTIFIER_NODE representing the operator. */
6893 cp_parser_conversion_function_id (cp_parser
* parser
)
6897 tree saved_qualifying_scope
;
6898 tree saved_object_scope
;
6900 /* Look for the `operator' token. */
6901 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
6902 return error_mark_node
;
6903 /* When we parse the conversion-type-id, the current scope will be
6904 reset. However, we need that information in able to look up the
6905 conversion function later, so we save it here. */
6906 saved_scope
= parser
->scope
;
6907 saved_qualifying_scope
= parser
->qualifying_scope
;
6908 saved_object_scope
= parser
->object_scope
;
6909 /* We must enter the scope of the class so that the names of
6910 entities declared within the class are available in the
6911 conversion-type-id. For example, consider:
6918 S::operator I() { ... }
6920 In order to see that `I' is a type-name in the definition, we
6921 must be in the scope of `S'. */
6923 push_scope (saved_scope
);
6924 /* Parse the conversion-type-id. */
6925 type
= cp_parser_conversion_type_id (parser
);
6926 /* Leave the scope of the class, if any. */
6928 pop_scope (saved_scope
);
6929 /* Restore the saved scope. */
6930 parser
->scope
= saved_scope
;
6931 parser
->qualifying_scope
= saved_qualifying_scope
;
6932 parser
->object_scope
= saved_object_scope
;
6933 /* If the TYPE is invalid, indicate failure. */
6934 if (type
== error_mark_node
)
6935 return error_mark_node
;
6936 return mangle_conv_op_name_for_type (type
);
6939 /* Parse a conversion-type-id:
6942 type-specifier-seq conversion-declarator [opt]
6944 Returns the TYPE specified. */
6947 cp_parser_conversion_type_id (cp_parser
* parser
)
6950 tree type_specifiers
;
6953 /* Parse the attributes. */
6954 attributes
= cp_parser_attributes_opt (parser
);
6955 /* Parse the type-specifiers. */
6956 type_specifiers
= cp_parser_type_specifier_seq (parser
);
6957 /* If that didn't work, stop. */
6958 if (type_specifiers
== error_mark_node
)
6959 return error_mark_node
;
6960 /* Parse the conversion-declarator. */
6961 declarator
= cp_parser_conversion_declarator_opt (parser
);
6963 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
6964 /*initialized=*/0, &attributes
);
6967 /* Parse an (optional) conversion-declarator.
6969 conversion-declarator:
6970 ptr-operator conversion-declarator [opt]
6972 Returns a representation of the declarator. See
6973 cp_parser_declarator for details. */
6976 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
6978 enum tree_code code
;
6980 tree cv_qualifier_seq
;
6982 /* We don't know if there's a ptr-operator next, or not. */
6983 cp_parser_parse_tentatively (parser
);
6984 /* Try the ptr-operator. */
6985 code
= cp_parser_ptr_operator (parser
, &class_type
,
6987 /* If it worked, look for more conversion-declarators. */
6988 if (cp_parser_parse_definitely (parser
))
6992 /* Parse another optional declarator. */
6993 declarator
= cp_parser_conversion_declarator_opt (parser
);
6995 /* Create the representation of the declarator. */
6996 if (code
== INDIRECT_REF
)
6997 declarator
= make_pointer_declarator (cv_qualifier_seq
,
7000 declarator
= make_reference_declarator (cv_qualifier_seq
,
7003 /* Handle the pointer-to-member case. */
7005 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
7013 /* Parse an (optional) ctor-initializer.
7016 : mem-initializer-list
7018 Returns TRUE iff the ctor-initializer was actually present. */
7021 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7023 /* If the next token is not a `:', then there is no
7024 ctor-initializer. */
7025 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7027 /* Do default initialization of any bases and members. */
7028 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7029 finish_mem_initializers (NULL_TREE
);
7034 /* Consume the `:' token. */
7035 cp_lexer_consume_token (parser
->lexer
);
7036 /* And the mem-initializer-list. */
7037 cp_parser_mem_initializer_list (parser
);
7042 /* Parse a mem-initializer-list.
7044 mem-initializer-list:
7046 mem-initializer , mem-initializer-list */
7049 cp_parser_mem_initializer_list (cp_parser
* parser
)
7051 tree mem_initializer_list
= NULL_TREE
;
7053 /* Let the semantic analysis code know that we are starting the
7054 mem-initializer-list. */
7055 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7056 error ("only constructors take base initializers");
7058 /* Loop through the list. */
7061 tree mem_initializer
;
7063 /* Parse the mem-initializer. */
7064 mem_initializer
= cp_parser_mem_initializer (parser
);
7065 /* Add it to the list, unless it was erroneous. */
7066 if (mem_initializer
)
7068 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7069 mem_initializer_list
= mem_initializer
;
7071 /* If the next token is not a `,', we're done. */
7072 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7074 /* Consume the `,' token. */
7075 cp_lexer_consume_token (parser
->lexer
);
7078 /* Perform semantic analysis. */
7079 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7080 finish_mem_initializers (mem_initializer_list
);
7083 /* Parse a mem-initializer.
7086 mem-initializer-id ( expression-list [opt] )
7091 ( expression-list [opt] )
7093 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7094 class) or FIELD_DECL (for a non-static data member) to initialize;
7095 the TREE_VALUE is the expression-list. */
7098 cp_parser_mem_initializer (cp_parser
* parser
)
7100 tree mem_initializer_id
;
7101 tree expression_list
;
7104 /* Find out what is being initialized. */
7105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7107 pedwarn ("anachronistic old-style base class initializer");
7108 mem_initializer_id
= NULL_TREE
;
7111 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7112 member
= expand_member_init (mem_initializer_id
);
7113 if (member
&& !DECL_P (member
))
7114 in_base_initializer
= 1;
7117 = cp_parser_parenthesized_expression_list (parser
, false,
7118 /*non_constant_p=*/NULL
);
7119 if (!expression_list
)
7120 expression_list
= void_type_node
;
7122 in_base_initializer
= 0;
7124 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7127 /* Parse a mem-initializer-id.
7130 :: [opt] nested-name-specifier [opt] class-name
7133 Returns a TYPE indicating the class to be initializer for the first
7134 production. Returns an IDENTIFIER_NODE indicating the data member
7135 to be initialized for the second production. */
7138 cp_parser_mem_initializer_id (cp_parser
* parser
)
7140 bool global_scope_p
;
7141 bool nested_name_specifier_p
;
7144 /* Look for the optional `::' operator. */
7146 = (cp_parser_global_scope_opt (parser
,
7147 /*current_scope_valid_p=*/false)
7149 /* Look for the optional nested-name-specifier. The simplest way to
7154 The keyword `typename' is not permitted in a base-specifier or
7155 mem-initializer; in these contexts a qualified name that
7156 depends on a template-parameter is implicitly assumed to be a
7159 is to assume that we have seen the `typename' keyword at this
7161 nested_name_specifier_p
7162 = (cp_parser_nested_name_specifier_opt (parser
,
7163 /*typename_keyword_p=*/true,
7164 /*check_dependency_p=*/true,
7166 /*is_declaration=*/true)
7168 /* If there is a `::' operator or a nested-name-specifier, then we
7169 are definitely looking for a class-name. */
7170 if (global_scope_p
|| nested_name_specifier_p
)
7171 return cp_parser_class_name (parser
,
7172 /*typename_keyword_p=*/true,
7173 /*template_keyword_p=*/false,
7175 /*check_dependency_p=*/true,
7176 /*class_head_p=*/false,
7177 /*is_declaration=*/true);
7178 /* Otherwise, we could also be looking for an ordinary identifier. */
7179 cp_parser_parse_tentatively (parser
);
7180 /* Try a class-name. */
7181 id
= cp_parser_class_name (parser
,
7182 /*typename_keyword_p=*/true,
7183 /*template_keyword_p=*/false,
7185 /*check_dependency_p=*/true,
7186 /*class_head_p=*/false,
7187 /*is_declaration=*/true);
7188 /* If we found one, we're done. */
7189 if (cp_parser_parse_definitely (parser
))
7191 /* Otherwise, look for an ordinary identifier. */
7192 return cp_parser_identifier (parser
);
7195 /* Overloading [gram.over] */
7197 /* Parse an operator-function-id.
7199 operator-function-id:
7202 Returns an IDENTIFIER_NODE for the operator which is a
7203 human-readable spelling of the identifier, e.g., `operator +'. */
7206 cp_parser_operator_function_id (cp_parser
* parser
)
7208 /* Look for the `operator' keyword. */
7209 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7210 return error_mark_node
;
7211 /* And then the name of the operator itself. */
7212 return cp_parser_operator (parser
);
7215 /* Parse an operator.
7218 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7219 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7220 || ++ -- , ->* -> () []
7227 Returns an IDENTIFIER_NODE for the operator which is a
7228 human-readable spelling of the identifier, e.g., `operator +'. */
7231 cp_parser_operator (cp_parser
* parser
)
7233 tree id
= NULL_TREE
;
7236 /* Peek at the next token. */
7237 token
= cp_lexer_peek_token (parser
->lexer
);
7238 /* Figure out which operator we have. */
7239 switch (token
->type
)
7245 /* The keyword should be either `new' or `delete'. */
7246 if (token
->keyword
== RID_NEW
)
7248 else if (token
->keyword
== RID_DELETE
)
7253 /* Consume the `new' or `delete' token. */
7254 cp_lexer_consume_token (parser
->lexer
);
7256 /* Peek at the next token. */
7257 token
= cp_lexer_peek_token (parser
->lexer
);
7258 /* If it's a `[' token then this is the array variant of the
7260 if (token
->type
== CPP_OPEN_SQUARE
)
7262 /* Consume the `[' token. */
7263 cp_lexer_consume_token (parser
->lexer
);
7264 /* Look for the `]' token. */
7265 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7266 id
= ansi_opname (op
== NEW_EXPR
7267 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7269 /* Otherwise, we have the non-array variant. */
7271 id
= ansi_opname (op
);
7277 id
= ansi_opname (PLUS_EXPR
);
7281 id
= ansi_opname (MINUS_EXPR
);
7285 id
= ansi_opname (MULT_EXPR
);
7289 id
= ansi_opname (TRUNC_DIV_EXPR
);
7293 id
= ansi_opname (TRUNC_MOD_EXPR
);
7297 id
= ansi_opname (BIT_XOR_EXPR
);
7301 id
= ansi_opname (BIT_AND_EXPR
);
7305 id
= ansi_opname (BIT_IOR_EXPR
);
7309 id
= ansi_opname (BIT_NOT_EXPR
);
7313 id
= ansi_opname (TRUTH_NOT_EXPR
);
7317 id
= ansi_assopname (NOP_EXPR
);
7321 id
= ansi_opname (LT_EXPR
);
7325 id
= ansi_opname (GT_EXPR
);
7329 id
= ansi_assopname (PLUS_EXPR
);
7333 id
= ansi_assopname (MINUS_EXPR
);
7337 id
= ansi_assopname (MULT_EXPR
);
7341 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7345 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7349 id
= ansi_assopname (BIT_XOR_EXPR
);
7353 id
= ansi_assopname (BIT_AND_EXPR
);
7357 id
= ansi_assopname (BIT_IOR_EXPR
);
7361 id
= ansi_opname (LSHIFT_EXPR
);
7365 id
= ansi_opname (RSHIFT_EXPR
);
7369 id
= ansi_assopname (LSHIFT_EXPR
);
7373 id
= ansi_assopname (RSHIFT_EXPR
);
7377 id
= ansi_opname (EQ_EXPR
);
7381 id
= ansi_opname (NE_EXPR
);
7385 id
= ansi_opname (LE_EXPR
);
7388 case CPP_GREATER_EQ
:
7389 id
= ansi_opname (GE_EXPR
);
7393 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7397 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7401 id
= ansi_opname (POSTINCREMENT_EXPR
);
7404 case CPP_MINUS_MINUS
:
7405 id
= ansi_opname (PREDECREMENT_EXPR
);
7409 id
= ansi_opname (COMPOUND_EXPR
);
7412 case CPP_DEREF_STAR
:
7413 id
= ansi_opname (MEMBER_REF
);
7417 id
= ansi_opname (COMPONENT_REF
);
7420 case CPP_OPEN_PAREN
:
7421 /* Consume the `('. */
7422 cp_lexer_consume_token (parser
->lexer
);
7423 /* Look for the matching `)'. */
7424 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7425 return ansi_opname (CALL_EXPR
);
7427 case CPP_OPEN_SQUARE
:
7428 /* Consume the `['. */
7429 cp_lexer_consume_token (parser
->lexer
);
7430 /* Look for the matching `]'. */
7431 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7432 return ansi_opname (ARRAY_REF
);
7436 id
= ansi_opname (MIN_EXPR
);
7440 id
= ansi_opname (MAX_EXPR
);
7444 id
= ansi_assopname (MIN_EXPR
);
7448 id
= ansi_assopname (MAX_EXPR
);
7452 /* Anything else is an error. */
7456 /* If we have selected an identifier, we need to consume the
7459 cp_lexer_consume_token (parser
->lexer
);
7460 /* Otherwise, no valid operator name was present. */
7463 cp_parser_error (parser
, "expected operator");
7464 id
= error_mark_node
;
7470 /* Parse a template-declaration.
7472 template-declaration:
7473 export [opt] template < template-parameter-list > declaration
7475 If MEMBER_P is TRUE, this template-declaration occurs within a
7478 The grammar rule given by the standard isn't correct. What
7481 template-declaration:
7482 export [opt] template-parameter-list-seq
7483 decl-specifier-seq [opt] init-declarator [opt] ;
7484 export [opt] template-parameter-list-seq
7487 template-parameter-list-seq:
7488 template-parameter-list-seq [opt]
7489 template < template-parameter-list > */
7492 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7494 /* Check for `export'. */
7495 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7497 /* Consume the `export' token. */
7498 cp_lexer_consume_token (parser
->lexer
);
7499 /* Warn that we do not support `export'. */
7500 warning ("keyword `export' not implemented, and will be ignored");
7503 cp_parser_template_declaration_after_export (parser
, member_p
);
7506 /* Parse a template-parameter-list.
7508 template-parameter-list:
7510 template-parameter-list , template-parameter
7512 Returns a TREE_LIST. Each node represents a template parameter.
7513 The nodes are connected via their TREE_CHAINs. */
7516 cp_parser_template_parameter_list (cp_parser
* parser
)
7518 tree parameter_list
= NULL_TREE
;
7525 /* Parse the template-parameter. */
7526 parameter
= cp_parser_template_parameter (parser
);
7527 /* Add it to the list. */
7528 parameter_list
= process_template_parm (parameter_list
,
7531 /* Peek at the next token. */
7532 token
= cp_lexer_peek_token (parser
->lexer
);
7533 /* If it's not a `,', we're done. */
7534 if (token
->type
!= CPP_COMMA
)
7536 /* Otherwise, consume the `,' token. */
7537 cp_lexer_consume_token (parser
->lexer
);
7540 return parameter_list
;
7543 /* Parse a template-parameter.
7547 parameter-declaration
7549 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7550 TREE_PURPOSE is the default value, if any. */
7553 cp_parser_template_parameter (cp_parser
* parser
)
7557 /* Peek at the next token. */
7558 token
= cp_lexer_peek_token (parser
->lexer
);
7559 /* If it is `class' or `template', we have a type-parameter. */
7560 if (token
->keyword
== RID_TEMPLATE
)
7561 return cp_parser_type_parameter (parser
);
7562 /* If it is `class' or `typename' we do not know yet whether it is a
7563 type parameter or a non-type parameter. Consider:
7565 template <typename T, typename T::X X> ...
7569 template <class C, class D*> ...
7571 Here, the first parameter is a type parameter, and the second is
7572 a non-type parameter. We can tell by looking at the token after
7573 the identifier -- if it is a `,', `=', or `>' then we have a type
7575 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7577 /* Peek at the token after `class' or `typename'. */
7578 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7579 /* If it's an identifier, skip it. */
7580 if (token
->type
== CPP_NAME
)
7581 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7582 /* Now, see if the token looks like the end of a template
7584 if (token
->type
== CPP_COMMA
7585 || token
->type
== CPP_EQ
7586 || token
->type
== CPP_GREATER
)
7587 return cp_parser_type_parameter (parser
);
7590 /* Otherwise, it is a non-type parameter.
7594 When parsing a default template-argument for a non-type
7595 template-parameter, the first non-nested `>' is taken as the end
7596 of the template parameter-list rather than a greater-than
7599 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
7600 /*parenthesized_p=*/NULL
);
7603 /* Parse a type-parameter.
7606 class identifier [opt]
7607 class identifier [opt] = type-id
7608 typename identifier [opt]
7609 typename identifier [opt] = type-id
7610 template < template-parameter-list > class identifier [opt]
7611 template < template-parameter-list > class identifier [opt]
7614 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7615 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7616 the declaration of the parameter. */
7619 cp_parser_type_parameter (cp_parser
* parser
)
7624 /* Look for a keyword to tell us what kind of parameter this is. */
7625 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7626 "`class', `typename', or `template'");
7628 return error_mark_node
;
7630 switch (token
->keyword
)
7636 tree default_argument
;
7638 /* If the next token is an identifier, then it names the
7640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7641 identifier
= cp_parser_identifier (parser
);
7643 identifier
= NULL_TREE
;
7645 /* Create the parameter. */
7646 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7648 /* If the next token is an `=', we have a default argument. */
7649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7651 /* Consume the `=' token. */
7652 cp_lexer_consume_token (parser
->lexer
);
7653 /* Parse the default-argument. */
7654 default_argument
= cp_parser_type_id (parser
);
7657 default_argument
= NULL_TREE
;
7659 /* Create the combined representation of the parameter and the
7660 default argument. */
7661 parameter
= build_tree_list (default_argument
, parameter
);
7667 tree parameter_list
;
7669 tree default_argument
;
7671 /* Look for the `<'. */
7672 cp_parser_require (parser
, CPP_LESS
, "`<'");
7673 /* Parse the template-parameter-list. */
7674 begin_template_parm_list ();
7676 = cp_parser_template_parameter_list (parser
);
7677 parameter_list
= end_template_parm_list (parameter_list
);
7678 /* Look for the `>'. */
7679 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7680 /* Look for the `class' keyword. */
7681 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7682 /* If the next token is an `=', then there is a
7683 default-argument. If the next token is a `>', we are at
7684 the end of the parameter-list. If the next token is a `,',
7685 then we are at the end of this parameter. */
7686 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7687 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7688 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7689 identifier
= cp_parser_identifier (parser
);
7691 identifier
= NULL_TREE
;
7692 /* Create the template parameter. */
7693 parameter
= finish_template_template_parm (class_type_node
,
7696 /* If the next token is an `=', then there is a
7697 default-argument. */
7698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7702 /* Consume the `='. */
7703 cp_lexer_consume_token (parser
->lexer
);
7704 /* Parse the id-expression. */
7706 = cp_parser_id_expression (parser
,
7707 /*template_keyword_p=*/false,
7708 /*check_dependency_p=*/true,
7709 /*template_p=*/&is_template
,
7710 /*declarator_p=*/false);
7711 if (TREE_CODE (default_argument
) == TYPE_DECL
)
7712 /* If the id-expression was a template-id that refers to
7713 a template-class, we already have the declaration here,
7714 so no further lookup is needed. */
7717 /* Look up the name. */
7719 = cp_parser_lookup_name (parser
, default_argument
,
7721 /*is_template=*/is_template
,
7722 /*is_namespace=*/false,
7723 /*check_dependency=*/true);
7724 /* See if the default argument is valid. */
7726 = check_template_template_default_arg (default_argument
);
7729 default_argument
= NULL_TREE
;
7731 /* Create the combined representation of the parameter and the
7732 default argument. */
7733 parameter
= build_tree_list (default_argument
, parameter
);
7738 /* Anything else is an error. */
7739 cp_parser_error (parser
,
7740 "expected `class', `typename', or `template'");
7741 parameter
= error_mark_node
;
7747 /* Parse a template-id.
7750 template-name < template-argument-list [opt] >
7752 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7753 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7754 returned. Otherwise, if the template-name names a function, or set
7755 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7756 names a class, returns a TYPE_DECL for the specialization.
7758 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7759 uninstantiated templates. */
7762 cp_parser_template_id (cp_parser
*parser
,
7763 bool template_keyword_p
,
7764 bool check_dependency_p
,
7765 bool is_declaration
)
7770 ptrdiff_t start_of_id
;
7771 tree access_check
= NULL_TREE
;
7772 cp_token
*next_token
;
7775 /* If the next token corresponds to a template-id, there is no need
7777 next_token
= cp_lexer_peek_token (parser
->lexer
);
7778 if (next_token
->type
== CPP_TEMPLATE_ID
)
7783 /* Get the stored value. */
7784 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
7785 /* Perform any access checks that were deferred. */
7786 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
7787 perform_or_defer_access_check (TREE_PURPOSE (check
),
7788 TREE_VALUE (check
));
7789 /* Return the stored value. */
7790 return TREE_VALUE (value
);
7793 /* Avoid performing name lookup if there is no possibility of
7794 finding a template-id. */
7795 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
7796 || (next_token
->type
== CPP_NAME
7797 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
))
7799 cp_parser_error (parser
, "expected template-id");
7800 return error_mark_node
;
7803 /* Remember where the template-id starts. */
7804 if (cp_parser_parsing_tentatively (parser
)
7805 && !cp_parser_committed_to_tentative_parse (parser
))
7807 next_token
= cp_lexer_peek_token (parser
->lexer
);
7808 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
7809 parser
->lexer
->first_token
,
7815 push_deferring_access_checks (dk_deferred
);
7817 /* Parse the template-name. */
7818 is_identifier
= false;
7819 template = cp_parser_template_name (parser
, template_keyword_p
,
7823 if (template == error_mark_node
|| is_identifier
)
7825 pop_deferring_access_checks ();
7829 /* Look for the `<' that starts the template-argument-list. */
7830 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
7832 pop_deferring_access_checks ();
7833 return error_mark_node
;
7836 /* Parse the arguments. */
7837 arguments
= cp_parser_enclosed_template_argument_list (parser
);
7839 /* Build a representation of the specialization. */
7840 if (TREE_CODE (template) == IDENTIFIER_NODE
)
7841 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
7842 else if (DECL_CLASS_TEMPLATE_P (template)
7843 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7845 = finish_template_type (template, arguments
,
7846 cp_lexer_next_token_is (parser
->lexer
,
7850 /* If it's not a class-template or a template-template, it should be
7851 a function-template. */
7852 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7853 || TREE_CODE (template) == OVERLOAD
7854 || BASELINK_P (template)),
7857 template_id
= lookup_template_function (template, arguments
);
7860 /* Retrieve any deferred checks. Do not pop this access checks yet
7861 so the memory will not be reclaimed during token replacing below. */
7862 access_check
= get_deferred_access_checks ();
7864 /* If parsing tentatively, replace the sequence of tokens that makes
7865 up the template-id with a CPP_TEMPLATE_ID token. That way,
7866 should we re-parse the token stream, we will not have to repeat
7867 the effort required to do the parse, nor will we issue duplicate
7868 error messages about problems during instantiation of the
7870 if (start_of_id
>= 0)
7874 /* Find the token that corresponds to the start of the
7876 token
= cp_lexer_advance_token (parser
->lexer
,
7877 parser
->lexer
->first_token
,
7880 /* Reset the contents of the START_OF_ID token. */
7881 token
->type
= CPP_TEMPLATE_ID
;
7882 token
->value
= build_tree_list (access_check
, template_id
);
7883 token
->keyword
= RID_MAX
;
7884 /* Purge all subsequent tokens. */
7885 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
7888 pop_deferring_access_checks ();
7892 /* Parse a template-name.
7897 The standard should actually say:
7901 operator-function-id
7903 A defect report has been filed about this issue.
7905 A conversion-function-id cannot be a template name because they cannot
7906 be part of a template-id. In fact, looking at this code:
7910 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
7911 It is impossible to call a templated conversion-function-id with an
7912 explicit argument list, since the only allowed template parameter is
7913 the type to which it is converting.
7915 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7916 `template' keyword, in a construction like:
7920 In that case `f' is taken to be a template-name, even though there
7921 is no way of knowing for sure.
7923 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7924 name refers to a set of overloaded functions, at least one of which
7925 is a template, or an IDENTIFIER_NODE with the name of the template,
7926 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7927 names are looked up inside uninstantiated templates. */
7930 cp_parser_template_name (cp_parser
* parser
,
7931 bool template_keyword_p
,
7932 bool check_dependency_p
,
7933 bool is_declaration
,
7934 bool *is_identifier
)
7940 /* If the next token is `operator', then we have either an
7941 operator-function-id or a conversion-function-id. */
7942 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
7944 /* We don't know whether we're looking at an
7945 operator-function-id or a conversion-function-id. */
7946 cp_parser_parse_tentatively (parser
);
7947 /* Try an operator-function-id. */
7948 identifier
= cp_parser_operator_function_id (parser
);
7949 /* If that didn't work, try a conversion-function-id. */
7950 if (!cp_parser_parse_definitely (parser
))
7952 cp_parser_error (parser
, "expected template-name");
7953 return error_mark_node
;
7956 /* Look for the identifier. */
7958 identifier
= cp_parser_identifier (parser
);
7960 /* If we didn't find an identifier, we don't have a template-id. */
7961 if (identifier
== error_mark_node
)
7962 return error_mark_node
;
7964 /* If the name immediately followed the `template' keyword, then it
7965 is a template-name. However, if the next token is not `<', then
7966 we do not treat it as a template-name, since it is not being used
7967 as part of a template-id. This enables us to handle constructs
7970 template <typename T> struct S { S(); };
7971 template <typename T> S<T>::S();
7973 correctly. We would treat `S' as a template -- if it were `S<T>'
7974 -- but we do not if there is no `<'. */
7976 if (processing_template_decl
7977 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
7979 /* In a declaration, in a dependent context, we pretend that the
7980 "template" keyword was present in order to improve error
7981 recovery. For example, given:
7983 template <typename T> void f(T::X<int>);
7985 we want to treat "X<int>" as a template-id. */
7987 && !template_keyword_p
7988 && parser
->scope
&& TYPE_P (parser
->scope
)
7989 && dependent_type_p (parser
->scope
))
7993 /* Explain what went wrong. */
7994 error ("non-template `%D' used as template", identifier
);
7995 error ("(use `%T::template %D' to indicate that it is a template)",
7996 parser
->scope
, identifier
);
7997 /* If parsing tentatively, find the location of the "<"
7999 if (cp_parser_parsing_tentatively (parser
)
8000 && !cp_parser_committed_to_tentative_parse (parser
))
8002 cp_parser_simulate_error (parser
);
8003 token
= cp_lexer_peek_token (parser
->lexer
);
8004 token
= cp_lexer_prev_token (parser
->lexer
, token
);
8005 start
= cp_lexer_token_difference (parser
->lexer
,
8006 parser
->lexer
->first_token
,
8011 /* Parse the template arguments so that we can issue error
8012 messages about them. */
8013 cp_lexer_consume_token (parser
->lexer
);
8014 cp_parser_enclosed_template_argument_list (parser
);
8015 /* Skip tokens until we find a good place from which to
8016 continue parsing. */
8017 cp_parser_skip_to_closing_parenthesis (parser
,
8018 /*recovering=*/true,
8020 /*consume_paren=*/false);
8021 /* If parsing tentatively, permanently remove the
8022 template argument list. That will prevent duplicate
8023 error messages from being issued about the missing
8024 "template" keyword. */
8027 token
= cp_lexer_advance_token (parser
->lexer
,
8028 parser
->lexer
->first_token
,
8030 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8033 *is_identifier
= true;
8036 if (template_keyword_p
)
8040 /* Look up the name. */
8041 decl
= cp_parser_lookup_name (parser
, identifier
,
8043 /*is_template=*/false,
8044 /*is_namespace=*/false,
8045 check_dependency_p
);
8046 decl
= maybe_get_template_decl_from_type_decl (decl
);
8048 /* If DECL is a template, then the name was a template-name. */
8049 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8053 /* The standard does not explicitly indicate whether a name that
8054 names a set of overloaded declarations, some of which are
8055 templates, is a template-name. However, such a name should
8056 be a template-name; otherwise, there is no way to form a
8057 template-id for the overloaded templates. */
8058 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8059 if (TREE_CODE (fns
) == OVERLOAD
)
8063 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8064 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8069 /* Otherwise, the name does not name a template. */
8070 cp_parser_error (parser
, "expected template-name");
8071 return error_mark_node
;
8075 /* If DECL is dependent, and refers to a function, then just return
8076 its name; we will look it up again during template instantiation. */
8077 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8079 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8080 if (TYPE_P (scope
) && dependent_type_p (scope
))
8087 /* Parse a template-argument-list.
8089 template-argument-list:
8091 template-argument-list , template-argument
8093 Returns a TREE_VEC containing the arguments. */
8096 cp_parser_template_argument_list (cp_parser
* parser
)
8098 tree fixed_args
[10];
8099 unsigned n_args
= 0;
8100 unsigned alloced
= 10;
8101 tree
*arg_ary
= fixed_args
;
8103 bool saved_in_template_argument_list_p
;
8105 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8106 parser
->in_template_argument_list_p
= true;
8112 /* Consume the comma. */
8113 cp_lexer_consume_token (parser
->lexer
);
8115 /* Parse the template-argument. */
8116 argument
= cp_parser_template_argument (parser
);
8117 if (n_args
== alloced
)
8121 if (arg_ary
== fixed_args
)
8123 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8124 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8127 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8129 arg_ary
[n_args
++] = argument
;
8131 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8133 vec
= make_tree_vec (n_args
);
8136 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8138 if (arg_ary
!= fixed_args
)
8140 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8144 /* Parse a template-argument.
8147 assignment-expression
8151 The representation is that of an assignment-expression, type-id, or
8152 id-expression -- except that the qualified id-expression is
8153 evaluated, so that the value returned is either a DECL or an
8156 Although the standard says "assignment-expression", it forbids
8157 throw-expressions or assignments in the template argument.
8158 Therefore, we use "conditional-expression" instead. */
8161 cp_parser_template_argument (cp_parser
* parser
)
8166 bool maybe_type_id
= false;
8169 tree qualifying_class
;
8171 /* There's really no way to know what we're looking at, so we just
8172 try each alternative in order.
8176 In a template-argument, an ambiguity between a type-id and an
8177 expression is resolved to a type-id, regardless of the form of
8178 the corresponding template-parameter.
8180 Therefore, we try a type-id first. */
8181 cp_parser_parse_tentatively (parser
);
8182 argument
= cp_parser_type_id (parser
);
8183 /* If there was no error parsing the type-id but the next token is a '>>',
8184 we probably found a typo for '> >'. But there are type-id which are
8185 also valid expressions. For instance:
8187 struct X { int operator >> (int); };
8188 template <int V> struct Foo {};
8191 Here 'X()' is a valid type-id of a function type, but the user just
8192 wanted to write the expression "X() >> 5". Thus, we remember that we
8193 found a valid type-id, but we still try to parse the argument as an
8194 expression to see what happens. */
8195 if (!cp_parser_error_occurred (parser
)
8196 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8198 maybe_type_id
= true;
8199 cp_parser_abort_tentative_parse (parser
);
8203 /* If the next token isn't a `,' or a `>', then this argument wasn't
8204 really finished. This means that the argument is not a valid
8206 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8207 cp_parser_error (parser
, "expected template-argument");
8208 /* If that worked, we're done. */
8209 if (cp_parser_parse_definitely (parser
))
8212 /* We're still not sure what the argument will be. */
8213 cp_parser_parse_tentatively (parser
);
8214 /* Try a template. */
8215 argument
= cp_parser_id_expression (parser
,
8216 /*template_keyword_p=*/false,
8217 /*check_dependency_p=*/true,
8219 /*declarator_p=*/false);
8220 /* If the next token isn't a `,' or a `>', then this argument wasn't
8222 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8223 cp_parser_error (parser
, "expected template-argument");
8224 if (!cp_parser_error_occurred (parser
))
8226 /* Figure out what is being referred to. */
8227 argument
= cp_parser_lookup_name (parser
, argument
,
8229 /*is_template=*/template_p
,
8230 /*is_namespace=*/false,
8231 /*check_dependency=*/true);
8232 if (TREE_CODE (argument
) != TEMPLATE_DECL
8233 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8234 cp_parser_error (parser
, "expected template-name");
8236 if (cp_parser_parse_definitely (parser
))
8238 /* It must be a non-type argument. There permitted cases are given
8239 in [temp.arg.nontype]:
8241 -- an integral constant-expression of integral or enumeration
8244 -- the name of a non-type template-parameter; or
8246 -- the name of an object or function with external linkage...
8248 -- the address of an object or function with external linkage...
8250 -- a pointer to member... */
8251 /* Look for a non-type template parameter. */
8252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8254 cp_parser_parse_tentatively (parser
);
8255 argument
= cp_parser_primary_expression (parser
,
8258 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8259 || !cp_parser_next_token_ends_template_argument_p (parser
))
8260 cp_parser_simulate_error (parser
);
8261 if (cp_parser_parse_definitely (parser
))
8264 /* If the next token is "&", the argument must be the address of an
8265 object or function with external linkage. */
8266 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8268 cp_lexer_consume_token (parser
->lexer
);
8269 /* See if we might have an id-expression. */
8270 token
= cp_lexer_peek_token (parser
->lexer
);
8271 if (token
->type
== CPP_NAME
8272 || token
->keyword
== RID_OPERATOR
8273 || token
->type
== CPP_SCOPE
8274 || token
->type
== CPP_TEMPLATE_ID
8275 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8277 cp_parser_parse_tentatively (parser
);
8278 argument
= cp_parser_primary_expression (parser
,
8281 if (cp_parser_error_occurred (parser
)
8282 || !cp_parser_next_token_ends_template_argument_p (parser
))
8283 cp_parser_abort_tentative_parse (parser
);
8286 if (qualifying_class
)
8287 argument
= finish_qualified_id_expr (qualifying_class
,
8291 if (TREE_CODE (argument
) == VAR_DECL
)
8293 /* A variable without external linkage might still be a
8294 valid constant-expression, so no error is issued here
8295 if the external-linkage check fails. */
8296 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8297 cp_parser_simulate_error (parser
);
8299 else if (is_overloaded_fn (argument
))
8300 /* All overloaded functions are allowed; if the external
8301 linkage test does not pass, an error will be issued
8305 && (TREE_CODE (argument
) == OFFSET_REF
8306 || TREE_CODE (argument
) == SCOPE_REF
))
8307 /* A pointer-to-member. */
8310 cp_parser_simulate_error (parser
);
8312 if (cp_parser_parse_definitely (parser
))
8315 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8320 /* If the argument started with "&", there are no other valid
8321 alternatives at this point. */
8324 cp_parser_error (parser
, "invalid non-type template argument");
8325 return error_mark_node
;
8327 /* If the argument wasn't successfully parsed as a type-id followed
8328 by '>>', the argument can only be a constant expression now.
8329 Otherwise, we try parsing the constant-expression tentatively,
8330 because the argument could really be a type-id. */
8332 cp_parser_parse_tentatively (parser
);
8333 argument
= cp_parser_constant_expression (parser
,
8334 /*allow_non_constant_p=*/false,
8335 /*non_constant_p=*/NULL
);
8336 argument
= cp_parser_fold_non_dependent_expr (argument
);
8339 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8340 cp_parser_error (parser
, "expected template-argument");
8341 if (cp_parser_parse_definitely (parser
))
8343 /* We did our best to parse the argument as a non type-id, but that
8344 was the only alternative that matched (albeit with a '>' after
8345 it). We can assume it's just a typo from the user, and a
8346 diagnostic will then be issued. */
8347 return cp_parser_type_id (parser
);
8350 /* Parse an explicit-instantiation.
8352 explicit-instantiation:
8353 template declaration
8355 Although the standard says `declaration', what it really means is:
8357 explicit-instantiation:
8358 template decl-specifier-seq [opt] declarator [opt] ;
8360 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8361 supposed to be allowed. A defect report has been filed about this
8366 explicit-instantiation:
8367 storage-class-specifier template
8368 decl-specifier-seq [opt] declarator [opt] ;
8369 function-specifier template
8370 decl-specifier-seq [opt] declarator [opt] ; */
8373 cp_parser_explicit_instantiation (cp_parser
* parser
)
8375 int declares_class_or_enum
;
8376 tree decl_specifiers
;
8378 tree extension_specifier
= NULL_TREE
;
8380 /* Look for an (optional) storage-class-specifier or
8381 function-specifier. */
8382 if (cp_parser_allow_gnu_extensions_p (parser
))
8385 = cp_parser_storage_class_specifier_opt (parser
);
8386 if (!extension_specifier
)
8387 extension_specifier
= cp_parser_function_specifier_opt (parser
);
8390 /* Look for the `template' keyword. */
8391 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8392 /* Let the front end know that we are processing an explicit
8394 begin_explicit_instantiation ();
8395 /* [temp.explicit] says that we are supposed to ignore access
8396 control while processing explicit instantiation directives. */
8397 push_deferring_access_checks (dk_no_check
);
8398 /* Parse a decl-specifier-seq. */
8400 = cp_parser_decl_specifier_seq (parser
,
8401 CP_PARSER_FLAGS_OPTIONAL
,
8403 &declares_class_or_enum
);
8404 /* If there was exactly one decl-specifier, and it declared a class,
8405 and there's no declarator, then we have an explicit type
8407 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8411 type
= check_tag_decl (decl_specifiers
);
8412 /* Turn access control back on for names used during
8413 template instantiation. */
8414 pop_deferring_access_checks ();
8416 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8423 /* Parse the declarator. */
8425 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8426 /*ctor_dtor_or_conv_p=*/NULL
,
8427 /*parenthesized_p=*/NULL
);
8428 cp_parser_check_for_definition_in_return_type (declarator
,
8429 declares_class_or_enum
);
8430 if (declarator
!= error_mark_node
)
8432 decl
= grokdeclarator (declarator
, decl_specifiers
,
8434 /* Turn access control back on for names used during
8435 template instantiation. */
8436 pop_deferring_access_checks ();
8437 /* Do the explicit instantiation. */
8438 do_decl_instantiation (decl
, extension_specifier
);
8442 pop_deferring_access_checks ();
8443 /* Skip the body of the explicit instantiation. */
8444 cp_parser_skip_to_end_of_statement (parser
);
8447 /* We're done with the instantiation. */
8448 end_explicit_instantiation ();
8450 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8453 /* Parse an explicit-specialization.
8455 explicit-specialization:
8456 template < > declaration
8458 Although the standard says `declaration', what it really means is:
8460 explicit-specialization:
8461 template <> decl-specifier [opt] init-declarator [opt] ;
8462 template <> function-definition
8463 template <> explicit-specialization
8464 template <> template-declaration */
8467 cp_parser_explicit_specialization (cp_parser
* parser
)
8469 /* Look for the `template' keyword. */
8470 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8471 /* Look for the `<'. */
8472 cp_parser_require (parser
, CPP_LESS
, "`<'");
8473 /* Look for the `>'. */
8474 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8475 /* We have processed another parameter list. */
8476 ++parser
->num_template_parameter_lists
;
8477 /* Let the front end know that we are beginning a specialization. */
8478 begin_specialization ();
8480 /* If the next keyword is `template', we need to figure out whether
8481 or not we're looking a template-declaration. */
8482 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8484 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8485 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8486 cp_parser_template_declaration_after_export (parser
,
8487 /*member_p=*/false);
8489 cp_parser_explicit_specialization (parser
);
8492 /* Parse the dependent declaration. */
8493 cp_parser_single_declaration (parser
,
8497 /* We're done with the specialization. */
8498 end_specialization ();
8499 /* We're done with this parameter list. */
8500 --parser
->num_template_parameter_lists
;
8503 /* Parse a type-specifier.
8506 simple-type-specifier
8509 elaborated-type-specifier
8517 Returns a representation of the type-specifier. If the
8518 type-specifier is a keyword (like `int' or `const', or
8519 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8520 For a class-specifier, enum-specifier, or elaborated-type-specifier
8521 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8523 If IS_FRIEND is TRUE then this type-specifier is being declared a
8524 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8525 appearing in a decl-specifier-seq.
8527 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8528 class-specifier, enum-specifier, or elaborated-type-specifier, then
8529 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8530 if a type is declared; 2 if it is defined. Otherwise, it is set to
8533 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8534 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8538 cp_parser_type_specifier (cp_parser
* parser
,
8539 cp_parser_flags flags
,
8541 bool is_declaration
,
8542 int* declares_class_or_enum
,
8543 bool* is_cv_qualifier
)
8545 tree type_spec
= NULL_TREE
;
8549 /* Assume this type-specifier does not declare a new type. */
8550 if (declares_class_or_enum
)
8551 *declares_class_or_enum
= 0;
8552 /* And that it does not specify a cv-qualifier. */
8553 if (is_cv_qualifier
)
8554 *is_cv_qualifier
= false;
8555 /* Peek at the next token. */
8556 token
= cp_lexer_peek_token (parser
->lexer
);
8558 /* If we're looking at a keyword, we can use that to guide the
8559 production we choose. */
8560 keyword
= token
->keyword
;
8563 /* Any of these indicate either a class-specifier, or an
8564 elaborated-type-specifier. */
8569 /* Parse tentatively so that we can back up if we don't find a
8570 class-specifier or enum-specifier. */
8571 cp_parser_parse_tentatively (parser
);
8572 /* Look for the class-specifier or enum-specifier. */
8573 if (keyword
== RID_ENUM
)
8574 type_spec
= cp_parser_enum_specifier (parser
);
8576 type_spec
= cp_parser_class_specifier (parser
);
8578 /* If that worked, we're done. */
8579 if (cp_parser_parse_definitely (parser
))
8581 if (declares_class_or_enum
)
8582 *declares_class_or_enum
= 2;
8589 /* Look for an elaborated-type-specifier. */
8590 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8593 /* We're declaring a class or enum -- unless we're using
8595 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8596 *declares_class_or_enum
= 1;
8602 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8603 /* Even though we call a routine that looks for an optional
8604 qualifier, we know that there should be one. */
8605 my_friendly_assert (type_spec
!= NULL
, 20000328);
8606 /* This type-specifier was a cv-qualified. */
8607 if (is_cv_qualifier
)
8608 *is_cv_qualifier
= true;
8613 /* The `__complex__' keyword is a GNU extension. */
8614 return cp_lexer_consume_token (parser
->lexer
)->value
;
8620 /* If we do not already have a type-specifier, assume we are looking
8621 at a simple-type-specifier. */
8622 type_spec
= cp_parser_simple_type_specifier (parser
, flags
,
8623 /*identifier_p=*/true);
8625 /* If we didn't find a type-specifier, and a type-specifier was not
8626 optional in this context, issue an error message. */
8627 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8629 cp_parser_error (parser
, "expected type specifier");
8630 return error_mark_node
;
8636 /* Parse a simple-type-specifier.
8638 simple-type-specifier:
8639 :: [opt] nested-name-specifier [opt] type-name
8640 :: [opt] nested-name-specifier template template-id
8655 simple-type-specifier:
8656 __typeof__ unary-expression
8657 __typeof__ ( type-id )
8659 For the various keywords, the value returned is simply the
8660 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8661 For the first two productions, and if IDENTIFIER_P is false, the
8662 value returned is the indicated TYPE_DECL. */
8665 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
,
8668 tree type
= NULL_TREE
;
8671 /* Peek at the next token. */
8672 token
= cp_lexer_peek_token (parser
->lexer
);
8674 /* If we're looking at a keyword, things are easy. */
8675 switch (token
->keyword
)
8678 type
= char_type_node
;
8681 type
= wchar_type_node
;
8684 type
= boolean_type_node
;
8687 type
= short_integer_type_node
;
8690 type
= integer_type_node
;
8693 type
= long_integer_type_node
;
8696 type
= integer_type_node
;
8699 type
= unsigned_type_node
;
8702 type
= float_type_node
;
8705 type
= double_type_node
;
8708 type
= void_type_node
;
8715 /* Consume the `typeof' token. */
8716 cp_lexer_consume_token (parser
->lexer
);
8717 /* Parse the operand to `typeof'. */
8718 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
8719 /* If it is not already a TYPE, take its type. */
8720 if (!TYPE_P (operand
))
8721 operand
= finish_typeof (operand
);
8730 /* If the type-specifier was for a built-in type, we're done. */
8735 /* Consume the token. */
8736 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
8738 /* There is no valid C++ program where a non-template type is
8739 followed by a "<". That usually indicates that the user thought
8740 that the type was a template. */
8741 cp_parser_check_for_invalid_template_id (parser
, type
);
8743 return identifier_p
? id
: TYPE_NAME (type
);
8746 /* The type-specifier must be a user-defined type. */
8747 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
8749 /* Don't gobble tokens or issue error messages if this is an
8750 optional type-specifier. */
8751 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
8752 cp_parser_parse_tentatively (parser
);
8754 /* Look for the optional `::' operator. */
8755 cp_parser_global_scope_opt (parser
,
8756 /*current_scope_valid_p=*/false);
8757 /* Look for the nested-name specifier. */
8758 cp_parser_nested_name_specifier_opt (parser
,
8759 /*typename_keyword_p=*/false,
8760 /*check_dependency_p=*/true,
8762 /*is_declaration=*/false);
8763 /* If we have seen a nested-name-specifier, and the next token
8764 is `template', then we are using the template-id production. */
8766 && cp_parser_optional_template_keyword (parser
))
8768 /* Look for the template-id. */
8769 type
= cp_parser_template_id (parser
,
8770 /*template_keyword_p=*/true,
8771 /*check_dependency_p=*/true,
8772 /*is_declaration=*/false);
8773 /* If the template-id did not name a type, we are out of
8775 if (TREE_CODE (type
) != TYPE_DECL
)
8777 cp_parser_error (parser
, "expected template-id for type");
8781 /* Otherwise, look for a type-name. */
8783 type
= cp_parser_type_name (parser
);
8784 /* If it didn't work out, we don't have a TYPE. */
8785 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
8786 && !cp_parser_parse_definitely (parser
))
8790 /* If we didn't get a type-name, issue an error message. */
8791 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8793 cp_parser_error (parser
, "expected type-name");
8794 return error_mark_node
;
8797 /* There is no valid C++ program where a non-template type is
8798 followed by a "<". That usually indicates that the user thought
8799 that the type was a template. */
8800 if (type
&& type
!= error_mark_node
)
8801 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
8806 /* Parse a type-name.
8819 Returns a TYPE_DECL for the the type. */
8822 cp_parser_type_name (cp_parser
* parser
)
8827 /* We can't know yet whether it is a class-name or not. */
8828 cp_parser_parse_tentatively (parser
);
8829 /* Try a class-name. */
8830 type_decl
= cp_parser_class_name (parser
,
8831 /*typename_keyword_p=*/false,
8832 /*template_keyword_p=*/false,
8834 /*check_dependency_p=*/true,
8835 /*class_head_p=*/false,
8836 /*is_declaration=*/false);
8837 /* If it's not a class-name, keep looking. */
8838 if (!cp_parser_parse_definitely (parser
))
8840 /* It must be a typedef-name or an enum-name. */
8841 identifier
= cp_parser_identifier (parser
);
8842 if (identifier
== error_mark_node
)
8843 return error_mark_node
;
8845 /* Look up the type-name. */
8846 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
8847 /* Issue an error if we did not find a type-name. */
8848 if (TREE_CODE (type_decl
) != TYPE_DECL
)
8850 if (!cp_parser_simulate_error (parser
))
8851 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
8853 type_decl
= error_mark_node
;
8855 /* Remember that the name was used in the definition of the
8856 current class so that we can check later to see if the
8857 meaning would have been different after the class was
8858 entirely defined. */
8859 else if (type_decl
!= error_mark_node
8861 maybe_note_name_used_in_class (identifier
, type_decl
);
8868 /* Parse an elaborated-type-specifier. Note that the grammar given
8869 here incorporates the resolution to DR68.
8871 elaborated-type-specifier:
8872 class-key :: [opt] nested-name-specifier [opt] identifier
8873 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8874 enum :: [opt] nested-name-specifier [opt] identifier
8875 typename :: [opt] nested-name-specifier identifier
8876 typename :: [opt] nested-name-specifier template [opt]
8881 elaborated-type-specifier:
8882 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8883 class-key attributes :: [opt] nested-name-specifier [opt]
8884 template [opt] template-id
8885 enum attributes :: [opt] nested-name-specifier [opt] identifier
8887 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8888 declared `friend'. If IS_DECLARATION is TRUE, then this
8889 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8890 something is being declared.
8892 Returns the TYPE specified. */
8895 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
8897 bool is_declaration
)
8899 enum tag_types tag_type
;
8901 tree type
= NULL_TREE
;
8902 tree attributes
= NULL_TREE
;
8904 /* See if we're looking at the `enum' keyword. */
8905 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
8907 /* Consume the `enum' token. */
8908 cp_lexer_consume_token (parser
->lexer
);
8909 /* Remember that it's an enumeration type. */
8910 tag_type
= enum_type
;
8911 /* Parse the attributes. */
8912 attributes
= cp_parser_attributes_opt (parser
);
8914 /* Or, it might be `typename'. */
8915 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8918 /* Consume the `typename' token. */
8919 cp_lexer_consume_token (parser
->lexer
);
8920 /* Remember that it's a `typename' type. */
8921 tag_type
= typename_type
;
8922 /* The `typename' keyword is only allowed in templates. */
8923 if (!processing_template_decl
)
8924 pedwarn ("using `typename' outside of template");
8926 /* Otherwise it must be a class-key. */
8929 tag_type
= cp_parser_class_key (parser
);
8930 if (tag_type
== none_type
)
8931 return error_mark_node
;
8932 /* Parse the attributes. */
8933 attributes
= cp_parser_attributes_opt (parser
);
8936 /* Look for the `::' operator. */
8937 cp_parser_global_scope_opt (parser
,
8938 /*current_scope_valid_p=*/false);
8939 /* Look for the nested-name-specifier. */
8940 if (tag_type
== typename_type
)
8942 if (cp_parser_nested_name_specifier (parser
,
8943 /*typename_keyword_p=*/true,
8944 /*check_dependency_p=*/true,
8948 return error_mark_node
;
8951 /* Even though `typename' is not present, the proposed resolution
8952 to Core Issue 180 says that in `class A<T>::B', `B' should be
8953 considered a type-name, even if `A<T>' is dependent. */
8954 cp_parser_nested_name_specifier_opt (parser
,
8955 /*typename_keyword_p=*/true,
8956 /*check_dependency_p=*/true,
8959 /* For everything but enumeration types, consider a template-id. */
8960 if (tag_type
!= enum_type
)
8962 bool template_p
= false;
8965 /* Allow the `template' keyword. */
8966 template_p
= cp_parser_optional_template_keyword (parser
);
8967 /* If we didn't see `template', we don't know if there's a
8968 template-id or not. */
8970 cp_parser_parse_tentatively (parser
);
8971 /* Parse the template-id. */
8972 decl
= cp_parser_template_id (parser
, template_p
,
8973 /*check_dependency_p=*/true,
8975 /* If we didn't find a template-id, look for an ordinary
8977 if (!template_p
&& !cp_parser_parse_definitely (parser
))
8979 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8980 in effect, then we must assume that, upon instantiation, the
8981 template will correspond to a class. */
8982 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
8983 && tag_type
== typename_type
)
8984 type
= make_typename_type (parser
->scope
, decl
,
8987 type
= TREE_TYPE (decl
);
8990 /* For an enumeration type, consider only a plain identifier. */
8993 identifier
= cp_parser_identifier (parser
);
8995 if (identifier
== error_mark_node
)
8997 parser
->scope
= NULL_TREE
;
8998 return error_mark_node
;
9001 /* For a `typename', we needn't call xref_tag. */
9002 if (tag_type
== typename_type
)
9003 return make_typename_type (parser
->scope
, identifier
,
9005 /* Look up a qualified name in the usual way. */
9010 /* In an elaborated-type-specifier, names are assumed to name
9011 types, so we set IS_TYPE to TRUE when calling
9012 cp_parser_lookup_name. */
9013 decl
= cp_parser_lookup_name (parser
, identifier
,
9015 /*is_template=*/false,
9016 /*is_namespace=*/false,
9017 /*check_dependency=*/true);
9019 /* If we are parsing friend declaration, DECL may be a
9020 TEMPLATE_DECL tree node here. However, we need to check
9021 whether this TEMPLATE_DECL results in valid code. Consider
9022 the following example:
9025 template <class T> class C {};
9028 template <class T> friend class N::C; // #1, valid code
9030 template <class T> class Y {
9031 friend class N::C; // #2, invalid code
9034 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9035 name lookup of `N::C'. We see that friend declaration must
9036 be template for the code to be valid. Note that
9037 processing_template_decl does not work here since it is
9038 always 1 for the above two cases. */
9040 decl
= (cp_parser_maybe_treat_template_as_class
9041 (decl
, /*tag_name_p=*/is_friend
9042 && parser
->num_template_parameter_lists
));
9044 if (TREE_CODE (decl
) != TYPE_DECL
)
9046 error ("expected type-name");
9047 return error_mark_node
;
9050 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9051 check_elaborated_type_specifier
9053 (parser
->num_template_parameter_lists
9054 || DECL_SELF_REFERENCE_P (decl
)));
9056 type
= TREE_TYPE (decl
);
9060 /* An elaborated-type-specifier sometimes introduces a new type and
9061 sometimes names an existing type. Normally, the rule is that it
9062 introduces a new type only if there is not an existing type of
9063 the same name already in scope. For example, given:
9066 void f() { struct S s; }
9068 the `struct S' in the body of `f' is the same `struct S' as in
9069 the global scope; the existing definition is used. However, if
9070 there were no global declaration, this would introduce a new
9071 local class named `S'.
9073 An exception to this rule applies to the following code:
9075 namespace N { struct S; }
9077 Here, the elaborated-type-specifier names a new type
9078 unconditionally; even if there is already an `S' in the
9079 containing scope this declaration names a new type.
9080 This exception only applies if the elaborated-type-specifier
9081 forms the complete declaration:
9085 A declaration consisting solely of `class-key identifier ;' is
9086 either a redeclaration of the name in the current scope or a
9087 forward declaration of the identifier as a class name. It
9088 introduces the name into the current scope.
9090 We are in this situation precisely when the next token is a `;'.
9092 An exception to the exception is that a `friend' declaration does
9093 *not* name a new type; i.e., given:
9095 struct S { friend struct T; };
9097 `T' is not a new type in the scope of `S'.
9099 Also, `new struct S' or `sizeof (struct S)' never results in the
9100 definition of a new type; a new type can only be declared in a
9101 declaration context. */
9103 /* Warn about attributes. They are ignored. */
9105 warning ("type attributes are honored only at type definition");
9107 type
= xref_tag (tag_type
, identifier
,
9108 /*attributes=*/NULL_TREE
,
9111 || cp_lexer_next_token_is_not (parser
->lexer
,
9113 parser
->num_template_parameter_lists
);
9116 if (tag_type
!= enum_type
)
9117 cp_parser_check_class_key (tag_type
, type
);
9119 /* A "<" cannot follow an elaborated type specifier. If that
9120 happens, the user was probably trying to form a template-id. */
9121 cp_parser_check_for_invalid_template_id (parser
, type
);
9126 /* Parse an enum-specifier.
9129 enum identifier [opt] { enumerator-list [opt] }
9131 Returns an ENUM_TYPE representing the enumeration. */
9134 cp_parser_enum_specifier (cp_parser
* parser
)
9137 tree identifier
= NULL_TREE
;
9140 /* Look for the `enum' keyword. */
9141 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
9142 return error_mark_node
;
9143 /* Peek at the next token. */
9144 token
= cp_lexer_peek_token (parser
->lexer
);
9146 /* See if it is an identifier. */
9147 if (token
->type
== CPP_NAME
)
9148 identifier
= cp_parser_identifier (parser
);
9150 /* Look for the `{'. */
9151 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
9152 return error_mark_node
;
9154 /* At this point, we're going ahead with the enum-specifier, even
9155 if some other problem occurs. */
9156 cp_parser_commit_to_tentative_parse (parser
);
9158 /* Issue an error message if type-definitions are forbidden here. */
9159 cp_parser_check_type_definition (parser
);
9161 /* Create the new type. */
9162 type
= start_enum (identifier
? identifier
: make_anon_name ());
9164 /* Peek at the next token. */
9165 token
= cp_lexer_peek_token (parser
->lexer
);
9166 /* If it's not a `}', then there are some enumerators. */
9167 if (token
->type
!= CPP_CLOSE_BRACE
)
9168 cp_parser_enumerator_list (parser
, type
);
9169 /* Look for the `}'. */
9170 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9172 /* Finish up the enumeration. */
9178 /* Parse an enumerator-list. The enumerators all have the indicated
9182 enumerator-definition
9183 enumerator-list , enumerator-definition */
9186 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9192 /* Parse an enumerator-definition. */
9193 cp_parser_enumerator_definition (parser
, type
);
9194 /* Peek at the next token. */
9195 token
= cp_lexer_peek_token (parser
->lexer
);
9196 /* If it's not a `,', then we've reached the end of the
9198 if (token
->type
!= CPP_COMMA
)
9200 /* Otherwise, consume the `,' and keep going. */
9201 cp_lexer_consume_token (parser
->lexer
);
9202 /* If the next token is a `}', there is a trailing comma. */
9203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9205 if (pedantic
&& !in_system_header
)
9206 pedwarn ("comma at end of enumerator list");
9212 /* Parse an enumerator-definition. The enumerator has the indicated
9215 enumerator-definition:
9217 enumerator = constant-expression
9223 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9229 /* Look for the identifier. */
9230 identifier
= cp_parser_identifier (parser
);
9231 if (identifier
== error_mark_node
)
9234 /* Peek at the next token. */
9235 token
= cp_lexer_peek_token (parser
->lexer
);
9236 /* If it's an `=', then there's an explicit value. */
9237 if (token
->type
== CPP_EQ
)
9239 /* Consume the `=' token. */
9240 cp_lexer_consume_token (parser
->lexer
);
9241 /* Parse the value. */
9242 value
= cp_parser_constant_expression (parser
,
9243 /*allow_non_constant_p=*/false,
9249 /* Create the enumerator. */
9250 build_enumerator (identifier
, value
, type
);
9253 /* Parse a namespace-name.
9256 original-namespace-name
9259 Returns the NAMESPACE_DECL for the namespace. */
9262 cp_parser_namespace_name (cp_parser
* parser
)
9265 tree namespace_decl
;
9267 /* Get the name of the namespace. */
9268 identifier
= cp_parser_identifier (parser
);
9269 if (identifier
== error_mark_node
)
9270 return error_mark_node
;
9272 /* Look up the identifier in the currently active scope. Look only
9273 for namespaces, due to:
9277 When looking up a namespace-name in a using-directive or alias
9278 definition, only namespace names are considered.
9284 During the lookup of a name preceding the :: scope resolution
9285 operator, object, function, and enumerator names are ignored.
9287 (Note that cp_parser_class_or_namespace_name only calls this
9288 function if the token after the name is the scope resolution
9290 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
9292 /*is_template=*/false,
9293 /*is_namespace=*/true,
9294 /*check_dependency=*/true);
9295 /* If it's not a namespace, issue an error. */
9296 if (namespace_decl
== error_mark_node
9297 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
9299 cp_parser_error (parser
, "expected namespace-name");
9300 namespace_decl
= error_mark_node
;
9303 return namespace_decl
;
9306 /* Parse a namespace-definition.
9308 namespace-definition:
9309 named-namespace-definition
9310 unnamed-namespace-definition
9312 named-namespace-definition:
9313 original-namespace-definition
9314 extension-namespace-definition
9316 original-namespace-definition:
9317 namespace identifier { namespace-body }
9319 extension-namespace-definition:
9320 namespace original-namespace-name { namespace-body }
9322 unnamed-namespace-definition:
9323 namespace { namespace-body } */
9326 cp_parser_namespace_definition (cp_parser
* parser
)
9330 /* Look for the `namespace' keyword. */
9331 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9333 /* Get the name of the namespace. We do not attempt to distinguish
9334 between an original-namespace-definition and an
9335 extension-namespace-definition at this point. The semantic
9336 analysis routines are responsible for that. */
9337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9338 identifier
= cp_parser_identifier (parser
);
9340 identifier
= NULL_TREE
;
9342 /* Look for the `{' to start the namespace. */
9343 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
9344 /* Start the namespace. */
9345 push_namespace (identifier
);
9346 /* Parse the body of the namespace. */
9347 cp_parser_namespace_body (parser
);
9348 /* Finish the namespace. */
9350 /* Look for the final `}'. */
9351 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9354 /* Parse a namespace-body.
9357 declaration-seq [opt] */
9360 cp_parser_namespace_body (cp_parser
* parser
)
9362 cp_parser_declaration_seq_opt (parser
);
9365 /* Parse a namespace-alias-definition.
9367 namespace-alias-definition:
9368 namespace identifier = qualified-namespace-specifier ; */
9371 cp_parser_namespace_alias_definition (cp_parser
* parser
)
9374 tree namespace_specifier
;
9376 /* Look for the `namespace' keyword. */
9377 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9378 /* Look for the identifier. */
9379 identifier
= cp_parser_identifier (parser
);
9380 if (identifier
== error_mark_node
)
9382 /* Look for the `=' token. */
9383 cp_parser_require (parser
, CPP_EQ
, "`='");
9384 /* Look for the qualified-namespace-specifier. */
9386 = cp_parser_qualified_namespace_specifier (parser
);
9387 /* Look for the `;' token. */
9388 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9390 /* Register the alias in the symbol table. */
9391 do_namespace_alias (identifier
, namespace_specifier
);
9394 /* Parse a qualified-namespace-specifier.
9396 qualified-namespace-specifier:
9397 :: [opt] nested-name-specifier [opt] namespace-name
9399 Returns a NAMESPACE_DECL corresponding to the specified
9403 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
9405 /* Look for the optional `::'. */
9406 cp_parser_global_scope_opt (parser
,
9407 /*current_scope_valid_p=*/false);
9409 /* Look for the optional nested-name-specifier. */
9410 cp_parser_nested_name_specifier_opt (parser
,
9411 /*typename_keyword_p=*/false,
9412 /*check_dependency_p=*/true,
9414 /*is_declaration=*/true);
9416 return cp_parser_namespace_name (parser
);
9419 /* Parse a using-declaration.
9422 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9423 using :: unqualified-id ; */
9426 cp_parser_using_declaration (cp_parser
* parser
)
9429 bool typename_p
= false;
9430 bool global_scope_p
;
9436 /* Look for the `using' keyword. */
9437 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9439 /* Peek at the next token. */
9440 token
= cp_lexer_peek_token (parser
->lexer
);
9441 /* See if it's `typename'. */
9442 if (token
->keyword
== RID_TYPENAME
)
9444 /* Remember that we've seen it. */
9446 /* Consume the `typename' token. */
9447 cp_lexer_consume_token (parser
->lexer
);
9450 /* Look for the optional global scope qualification. */
9452 = (cp_parser_global_scope_opt (parser
,
9453 /*current_scope_valid_p=*/false)
9456 /* If we saw `typename', or didn't see `::', then there must be a
9457 nested-name-specifier present. */
9458 if (typename_p
|| !global_scope_p
)
9459 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
9460 /*check_dependency_p=*/true,
9462 /*is_declaration=*/true);
9463 /* Otherwise, we could be in either of the two productions. In that
9464 case, treat the nested-name-specifier as optional. */
9466 qscope
= cp_parser_nested_name_specifier_opt (parser
,
9467 /*typename_keyword_p=*/false,
9468 /*check_dependency_p=*/true,
9470 /*is_declaration=*/true);
9472 qscope
= global_namespace
;
9474 /* Parse the unqualified-id. */
9475 identifier
= cp_parser_unqualified_id (parser
,
9476 /*template_keyword_p=*/false,
9477 /*check_dependency_p=*/true,
9478 /*declarator_p=*/true);
9480 /* The function we call to handle a using-declaration is different
9481 depending on what scope we are in. */
9482 if (identifier
== error_mark_node
)
9484 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
9485 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
9486 /* [namespace.udecl]
9488 A using declaration shall not name a template-id. */
9489 error ("a template-id may not appear in a using-declaration");
9492 scope
= current_scope ();
9493 if (scope
&& TYPE_P (scope
))
9495 /* Create the USING_DECL. */
9496 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9499 /* Add it to the list of members in this class. */
9500 finish_member_declaration (decl
);
9504 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9505 if (decl
== error_mark_node
)
9506 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
9508 do_local_using_decl (decl
, qscope
, identifier
);
9510 do_toplevel_using_decl (decl
, qscope
, identifier
);
9514 /* Look for the final `;'. */
9515 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9518 /* Parse a using-directive.
9521 using namespace :: [opt] nested-name-specifier [opt]
9525 cp_parser_using_directive (cp_parser
* parser
)
9527 tree namespace_decl
;
9530 /* Look for the `using' keyword. */
9531 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9532 /* And the `namespace' keyword. */
9533 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9534 /* Look for the optional `::' operator. */
9535 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9536 /* And the optional nested-name-specifier. */
9537 cp_parser_nested_name_specifier_opt (parser
,
9538 /*typename_keyword_p=*/false,
9539 /*check_dependency_p=*/true,
9541 /*is_declaration=*/true);
9542 /* Get the namespace being used. */
9543 namespace_decl
= cp_parser_namespace_name (parser
);
9544 /* And any specified attributes. */
9545 attribs
= cp_parser_attributes_opt (parser
);
9546 /* Update the symbol table. */
9547 parse_using_directive (namespace_decl
, attribs
);
9548 /* Look for the final `;'. */
9549 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9552 /* Parse an asm-definition.
9555 asm ( string-literal ) ;
9560 asm volatile [opt] ( string-literal ) ;
9561 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9562 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9563 : asm-operand-list [opt] ) ;
9564 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9565 : asm-operand-list [opt]
9566 : asm-operand-list [opt] ) ; */
9569 cp_parser_asm_definition (cp_parser
* parser
)
9573 tree outputs
= NULL_TREE
;
9574 tree inputs
= NULL_TREE
;
9575 tree clobbers
= NULL_TREE
;
9577 bool volatile_p
= false;
9578 bool extended_p
= false;
9580 /* Look for the `asm' keyword. */
9581 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9582 /* See if the next token is `volatile'. */
9583 if (cp_parser_allow_gnu_extensions_p (parser
)
9584 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9586 /* Remember that we saw the `volatile' keyword. */
9588 /* Consume the token. */
9589 cp_lexer_consume_token (parser
->lexer
);
9591 /* Look for the opening `('. */
9592 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9593 /* Look for the string. */
9594 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9597 string
= token
->value
;
9598 /* If we're allowing GNU extensions, check for the extended assembly
9599 syntax. Unfortunately, the `:' tokens need not be separated by
9600 a space in C, and so, for compatibility, we tolerate that here
9601 too. Doing that means that we have to treat the `::' operator as
9603 if (cp_parser_allow_gnu_extensions_p (parser
)
9604 && at_function_scope_p ()
9605 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9606 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9608 bool inputs_p
= false;
9609 bool clobbers_p
= false;
9611 /* The extended syntax was used. */
9614 /* Look for outputs. */
9615 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9617 /* Consume the `:'. */
9618 cp_lexer_consume_token (parser
->lexer
);
9619 /* Parse the output-operands. */
9620 if (cp_lexer_next_token_is_not (parser
->lexer
,
9622 && cp_lexer_next_token_is_not (parser
->lexer
,
9624 && cp_lexer_next_token_is_not (parser
->lexer
,
9626 outputs
= cp_parser_asm_operand_list (parser
);
9628 /* If the next token is `::', there are no outputs, and the
9629 next token is the beginning of the inputs. */
9630 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9632 /* Consume the `::' token. */
9633 cp_lexer_consume_token (parser
->lexer
);
9634 /* The inputs are coming next. */
9638 /* Look for inputs. */
9640 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9643 /* Consume the `:'. */
9644 cp_lexer_consume_token (parser
->lexer
);
9645 /* Parse the output-operands. */
9646 if (cp_lexer_next_token_is_not (parser
->lexer
,
9648 && cp_lexer_next_token_is_not (parser
->lexer
,
9650 && cp_lexer_next_token_is_not (parser
->lexer
,
9652 inputs
= cp_parser_asm_operand_list (parser
);
9654 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9655 /* The clobbers are coming next. */
9658 /* Look for clobbers. */
9660 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9663 /* Consume the `:'. */
9664 cp_lexer_consume_token (parser
->lexer
);
9665 /* Parse the clobbers. */
9666 if (cp_lexer_next_token_is_not (parser
->lexer
,
9668 clobbers
= cp_parser_asm_clobber_list (parser
);
9671 /* Look for the closing `)'. */
9672 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9673 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9674 /*consume_paren=*/true);
9675 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9677 /* Create the ASM_STMT. */
9678 if (at_function_scope_p ())
9681 finish_asm_stmt (volatile_p
9682 ? ridpointers
[(int) RID_VOLATILE
] : NULL_TREE
,
9683 string
, outputs
, inputs
, clobbers
);
9684 /* If the extended syntax was not used, mark the ASM_STMT. */
9686 ASM_INPUT_P (asm_stmt
) = 1;
9689 assemble_asm (string
);
9692 /* Declarators [gram.dcl.decl] */
9694 /* Parse an init-declarator.
9697 declarator initializer [opt]
9702 declarator asm-specification [opt] attributes [opt] initializer [opt]
9704 function-definition:
9705 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9707 decl-specifier-seq [opt] declarator function-try-block
9711 function-definition:
9712 __extension__ function-definition
9714 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9715 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9716 then this declarator appears in a class scope. The new DECL created
9717 by this declarator is returned.
9719 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9720 for a function-definition here as well. If the declarator is a
9721 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9722 be TRUE upon return. By that point, the function-definition will
9723 have been completely parsed.
9725 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9729 cp_parser_init_declarator (cp_parser
* parser
,
9730 tree decl_specifiers
,
9731 tree prefix_attributes
,
9732 bool function_definition_allowed_p
,
9734 int declares_class_or_enum
,
9735 bool* function_definition_p
)
9740 tree asm_specification
;
9742 tree decl
= NULL_TREE
;
9744 bool is_initialized
;
9745 bool is_parenthesized_init
;
9746 bool is_non_constant_init
;
9747 int ctor_dtor_or_conv_p
;
9750 /* Assume that this is not the declarator for a function
9752 if (function_definition_p
)
9753 *function_definition_p
= false;
9755 /* Defer access checks while parsing the declarator; we cannot know
9756 what names are accessible until we know what is being
9758 resume_deferring_access_checks ();
9760 /* Parse the declarator. */
9762 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9763 &ctor_dtor_or_conv_p
,
9764 /*parenthesized_p=*/NULL
);
9765 /* Gather up the deferred checks. */
9766 stop_deferring_access_checks ();
9768 /* If the DECLARATOR was erroneous, there's no need to go
9770 if (declarator
== error_mark_node
)
9771 return error_mark_node
;
9773 cp_parser_check_for_definition_in_return_type (declarator
,
9774 declares_class_or_enum
);
9776 /* Figure out what scope the entity declared by the DECLARATOR is
9777 located in. `grokdeclarator' sometimes changes the scope, so
9778 we compute it now. */
9779 scope
= get_scope_of_declarator (declarator
);
9781 /* If we're allowing GNU extensions, look for an asm-specification
9783 if (cp_parser_allow_gnu_extensions_p (parser
))
9785 /* Look for an asm-specification. */
9786 asm_specification
= cp_parser_asm_specification_opt (parser
);
9787 /* And attributes. */
9788 attributes
= cp_parser_attributes_opt (parser
);
9792 asm_specification
= NULL_TREE
;
9793 attributes
= NULL_TREE
;
9796 /* Peek at the next token. */
9797 token
= cp_lexer_peek_token (parser
->lexer
);
9798 /* Check to see if the token indicates the start of a
9799 function-definition. */
9800 if (cp_parser_token_starts_function_definition_p (token
))
9802 if (!function_definition_allowed_p
)
9804 /* If a function-definition should not appear here, issue an
9806 cp_parser_error (parser
,
9807 "a function-definition is not allowed here");
9808 return error_mark_node
;
9812 /* Neither attributes nor an asm-specification are allowed
9813 on a function-definition. */
9814 if (asm_specification
)
9815 error ("an asm-specification is not allowed on a function-definition");
9817 error ("attributes are not allowed on a function-definition");
9818 /* This is a function-definition. */
9819 *function_definition_p
= true;
9821 /* Parse the function definition. */
9823 decl
= cp_parser_save_member_function_body (parser
,
9829 = (cp_parser_function_definition_from_specifiers_and_declarator
9830 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
9838 Only in function declarations for constructors, destructors, and
9839 type conversions can the decl-specifier-seq be omitted.
9841 We explicitly postpone this check past the point where we handle
9842 function-definitions because we tolerate function-definitions
9843 that are missing their return types in some modes. */
9844 if (!decl_specifiers
&& ctor_dtor_or_conv_p
<= 0)
9846 cp_parser_error (parser
,
9847 "expected constructor, destructor, or type conversion");
9848 return error_mark_node
;
9851 /* An `=' or an `(' indicates an initializer. */
9852 is_initialized
= (token
->type
== CPP_EQ
9853 || token
->type
== CPP_OPEN_PAREN
);
9854 /* If the init-declarator isn't initialized and isn't followed by a
9855 `,' or `;', it's not a valid init-declarator. */
9857 && token
->type
!= CPP_COMMA
9858 && token
->type
!= CPP_SEMICOLON
)
9860 cp_parser_error (parser
, "expected init-declarator");
9861 return error_mark_node
;
9864 /* Because start_decl has side-effects, we should only call it if we
9865 know we're going ahead. By this point, we know that we cannot
9866 possibly be looking at any other construct. */
9867 cp_parser_commit_to_tentative_parse (parser
);
9869 /* If the decl specifiers were bad, issue an error now that we're
9870 sure this was intended to be a declarator. Then continue
9871 declaring the variable(s), as int, to try to cut down on further
9873 if (decl_specifiers
!= NULL
9874 && TREE_VALUE (decl_specifiers
) == error_mark_node
)
9876 cp_parser_error (parser
, "invalid type in declaration");
9877 TREE_VALUE (decl_specifiers
) = integer_type_node
;
9880 /* Check to see whether or not this declaration is a friend. */
9881 friend_p
= cp_parser_friend_p (decl_specifiers
);
9883 /* Check that the number of template-parameter-lists is OK. */
9884 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
9885 return error_mark_node
;
9887 /* Enter the newly declared entry in the symbol table. If we're
9888 processing a declaration in a class-specifier, we wait until
9889 after processing the initializer. */
9892 if (parser
->in_unbraced_linkage_specification_p
)
9894 decl_specifiers
= tree_cons (error_mark_node
,
9895 get_identifier ("extern"),
9897 have_extern_spec
= false;
9899 decl
= start_decl (declarator
, decl_specifiers
,
9900 is_initialized
, attributes
, prefix_attributes
);
9903 /* Enter the SCOPE. That way unqualified names appearing in the
9904 initializer will be looked up in SCOPE. */
9908 /* Perform deferred access control checks, now that we know in which
9909 SCOPE the declared entity resides. */
9910 if (!member_p
&& decl
)
9912 tree saved_current_function_decl
= NULL_TREE
;
9914 /* If the entity being declared is a function, pretend that we
9915 are in its scope. If it is a `friend', it may have access to
9916 things that would not otherwise be accessible. */
9917 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9919 saved_current_function_decl
= current_function_decl
;
9920 current_function_decl
= decl
;
9923 /* Perform the access control checks for the declarator and the
9924 the decl-specifiers. */
9925 perform_deferred_access_checks ();
9927 /* Restore the saved value. */
9928 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9929 current_function_decl
= saved_current_function_decl
;
9932 /* Parse the initializer. */
9934 initializer
= cp_parser_initializer (parser
,
9935 &is_parenthesized_init
,
9936 &is_non_constant_init
);
9939 initializer
= NULL_TREE
;
9940 is_parenthesized_init
= false;
9941 is_non_constant_init
= true;
9944 /* The old parser allows attributes to appear after a parenthesized
9945 initializer. Mark Mitchell proposed removing this functionality
9946 on the GCC mailing lists on 2002-08-13. This parser accepts the
9947 attributes -- but ignores them. */
9948 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
9949 if (cp_parser_attributes_opt (parser
))
9950 warning ("attributes after parenthesized initializer ignored");
9952 /* Leave the SCOPE, now that we have processed the initializer. It
9953 is important to do this before calling cp_finish_decl because it
9954 makes decisions about whether to create DECL_STMTs or not based
9955 on the current scope. */
9959 /* For an in-class declaration, use `grokfield' to create the
9963 decl
= grokfield (declarator
, decl_specifiers
,
9964 initializer
, /*asmspec=*/NULL_TREE
,
9965 /*attributes=*/NULL_TREE
);
9966 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
9967 cp_parser_save_default_args (parser
, decl
);
9970 /* Finish processing the declaration. But, skip friend
9972 if (!friend_p
&& decl
)
9973 cp_finish_decl (decl
,
9976 /* If the initializer is in parentheses, then this is
9977 a direct-initialization, which means that an
9978 `explicit' constructor is OK. Otherwise, an
9979 `explicit' constructor cannot be used. */
9980 ((is_parenthesized_init
|| !is_initialized
)
9981 ? 0 : LOOKUP_ONLYCONVERTING
));
9983 /* Remember whether or not variables were initialized by
9984 constant-expressions. */
9985 if (decl
&& TREE_CODE (decl
) == VAR_DECL
9986 && is_initialized
&& !is_non_constant_init
)
9987 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
9992 /* Parse a declarator.
9996 ptr-operator declarator
9998 abstract-declarator:
9999 ptr-operator abstract-declarator [opt]
10000 direct-abstract-declarator
10005 attributes [opt] direct-declarator
10006 attributes [opt] ptr-operator declarator
10008 abstract-declarator:
10009 attributes [opt] ptr-operator abstract-declarator [opt]
10010 attributes [opt] direct-abstract-declarator
10012 Returns a representation of the declarator. If the declarator has
10013 the form `* declarator', then an INDIRECT_REF is returned, whose
10014 only operand is the sub-declarator. Analogously, `& declarator' is
10015 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10016 used. The first operand is the TYPE for `X'. The second operand
10017 is an INDIRECT_REF whose operand is the sub-declarator.
10019 Otherwise, the representation is as for a direct-declarator.
10021 (It would be better to define a structure type to represent
10022 declarators, rather than abusing `tree' nodes to represent
10023 declarators. That would be much clearer and save some memory.
10024 There is no reason for declarators to be garbage-collected, for
10025 example; they are created during parser and no longer needed after
10026 `grokdeclarator' has been called.)
10028 For a ptr-operator that has the optional cv-qualifier-seq,
10029 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10032 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10033 detect constructor, destructor or conversion operators. It is set
10034 to -1 if the declarator is a name, and +1 if it is a
10035 function. Otherwise it is set to zero. Usually you just want to
10036 test for >0, but internally the negative value is used.
10038 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10039 a decl-specifier-seq unless it declares a constructor, destructor,
10040 or conversion. It might seem that we could check this condition in
10041 semantic analysis, rather than parsing, but that makes it difficult
10042 to handle something like `f()'. We want to notice that there are
10043 no decl-specifiers, and therefore realize that this is an
10044 expression, not a declaration.)
10046 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10047 the declarator is a direct-declarator of the form "(...)". */
10050 cp_parser_declarator (cp_parser
* parser
,
10051 cp_parser_declarator_kind dcl_kind
,
10052 int* ctor_dtor_or_conv_p
,
10053 bool* parenthesized_p
)
10057 enum tree_code code
;
10058 tree cv_qualifier_seq
;
10060 tree attributes
= NULL_TREE
;
10062 /* Assume this is not a constructor, destructor, or type-conversion
10064 if (ctor_dtor_or_conv_p
)
10065 *ctor_dtor_or_conv_p
= 0;
10067 if (cp_parser_allow_gnu_extensions_p (parser
))
10068 attributes
= cp_parser_attributes_opt (parser
);
10070 /* Peek at the next token. */
10071 token
= cp_lexer_peek_token (parser
->lexer
);
10073 /* Check for the ptr-operator production. */
10074 cp_parser_parse_tentatively (parser
);
10075 /* Parse the ptr-operator. */
10076 code
= cp_parser_ptr_operator (parser
,
10078 &cv_qualifier_seq
);
10079 /* If that worked, then we have a ptr-operator. */
10080 if (cp_parser_parse_definitely (parser
))
10082 /* If a ptr-operator was found, then this declarator was not
10084 if (parenthesized_p
)
10085 *parenthesized_p
= true;
10086 /* The dependent declarator is optional if we are parsing an
10087 abstract-declarator. */
10088 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10089 cp_parser_parse_tentatively (parser
);
10091 /* Parse the dependent declarator. */
10092 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10093 /*ctor_dtor_or_conv_p=*/NULL
,
10094 /*parenthesized_p=*/NULL
);
10096 /* If we are parsing an abstract-declarator, we must handle the
10097 case where the dependent declarator is absent. */
10098 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10099 && !cp_parser_parse_definitely (parser
))
10100 declarator
= NULL_TREE
;
10102 /* Build the representation of the ptr-operator. */
10103 if (code
== INDIRECT_REF
)
10104 declarator
= make_pointer_declarator (cv_qualifier_seq
,
10107 declarator
= make_reference_declarator (cv_qualifier_seq
,
10109 /* Handle the pointer-to-member case. */
10111 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
10113 /* Everything else is a direct-declarator. */
10116 if (parenthesized_p
)
10117 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10119 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10120 ctor_dtor_or_conv_p
);
10123 if (attributes
&& declarator
!= error_mark_node
)
10124 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
10129 /* Parse a direct-declarator or direct-abstract-declarator.
10133 direct-declarator ( parameter-declaration-clause )
10134 cv-qualifier-seq [opt]
10135 exception-specification [opt]
10136 direct-declarator [ constant-expression [opt] ]
10139 direct-abstract-declarator:
10140 direct-abstract-declarator [opt]
10141 ( parameter-declaration-clause )
10142 cv-qualifier-seq [opt]
10143 exception-specification [opt]
10144 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10145 ( abstract-declarator )
10147 Returns a representation of the declarator. DCL_KIND is
10148 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10149 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10150 we are parsing a direct-declarator. It is
10151 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10152 of ambiguity we prefer an abstract declarator, as per
10153 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10154 cp_parser_declarator.
10156 For the declarator-id production, the representation is as for an
10157 id-expression, except that a qualified name is represented as a
10158 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10159 see the documentation of the FUNCTION_DECLARATOR_* macros for
10160 information about how to find the various declarator components.
10161 An array-declarator is represented as an ARRAY_REF. The
10162 direct-declarator is the first operand; the constant-expression
10163 indicating the size of the array is the second operand. */
10166 cp_parser_direct_declarator (cp_parser
* parser
,
10167 cp_parser_declarator_kind dcl_kind
,
10168 int* ctor_dtor_or_conv_p
)
10171 tree declarator
= NULL_TREE
;
10172 tree scope
= NULL_TREE
;
10173 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10174 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10179 /* Peek at the next token. */
10180 token
= cp_lexer_peek_token (parser
->lexer
);
10181 if (token
->type
== CPP_OPEN_PAREN
)
10183 /* This is either a parameter-declaration-clause, or a
10184 parenthesized declarator. When we know we are parsing a
10185 named declarator, it must be a parenthesized declarator
10186 if FIRST is true. For instance, `(int)' is a
10187 parameter-declaration-clause, with an omitted
10188 direct-abstract-declarator. But `((*))', is a
10189 parenthesized abstract declarator. Finally, when T is a
10190 template parameter `(T)' is a
10191 parameter-declaration-clause, and not a parenthesized
10194 We first try and parse a parameter-declaration-clause,
10195 and then try a nested declarator (if FIRST is true).
10197 It is not an error for it not to be a
10198 parameter-declaration-clause, even when FIRST is
10204 The first is the declaration of a function while the
10205 second is a the definition of a variable, including its
10208 Having seen only the parenthesis, we cannot know which of
10209 these two alternatives should be selected. Even more
10210 complex are examples like:
10215 The former is a function-declaration; the latter is a
10216 variable initialization.
10218 Thus again, we try a parameter-declaration-clause, and if
10219 that fails, we back out and return. */
10221 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10224 unsigned saved_num_template_parameter_lists
;
10226 cp_parser_parse_tentatively (parser
);
10228 /* Consume the `('. */
10229 cp_lexer_consume_token (parser
->lexer
);
10232 /* If this is going to be an abstract declarator, we're
10233 in a declarator and we can't have default args. */
10234 parser
->default_arg_ok_p
= false;
10235 parser
->in_declarator_p
= true;
10238 /* Inside the function parameter list, surrounding
10239 template-parameter-lists do not apply. */
10240 saved_num_template_parameter_lists
10241 = parser
->num_template_parameter_lists
;
10242 parser
->num_template_parameter_lists
= 0;
10244 /* Parse the parameter-declaration-clause. */
10245 params
= cp_parser_parameter_declaration_clause (parser
);
10247 parser
->num_template_parameter_lists
10248 = saved_num_template_parameter_lists
;
10250 /* If all went well, parse the cv-qualifier-seq and the
10251 exception-specification. */
10252 if (cp_parser_parse_definitely (parser
))
10254 tree cv_qualifiers
;
10255 tree exception_specification
;
10257 if (ctor_dtor_or_conv_p
)
10258 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
10260 /* Consume the `)'. */
10261 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
10263 /* Parse the cv-qualifier-seq. */
10264 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
10265 /* And the exception-specification. */
10266 exception_specification
10267 = cp_parser_exception_specification_opt (parser
);
10269 /* Create the function-declarator. */
10270 declarator
= make_call_declarator (declarator
,
10273 exception_specification
);
10274 /* Any subsequent parameter lists are to do with
10275 return type, so are not those of the declared
10277 parser
->default_arg_ok_p
= false;
10279 /* Repeat the main loop. */
10284 /* If this is the first, we can try a parenthesized
10288 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10289 parser
->in_declarator_p
= saved_in_declarator_p
;
10291 /* Consume the `('. */
10292 cp_lexer_consume_token (parser
->lexer
);
10293 /* Parse the nested declarator. */
10295 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
10296 /*parenthesized_p=*/NULL
);
10298 /* Expect a `)'. */
10299 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10300 declarator
= error_mark_node
;
10301 if (declarator
== error_mark_node
)
10304 goto handle_declarator
;
10306 /* Otherwise, we must be done. */
10310 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10311 && token
->type
== CPP_OPEN_SQUARE
)
10313 /* Parse an array-declarator. */
10316 if (ctor_dtor_or_conv_p
)
10317 *ctor_dtor_or_conv_p
= 0;
10320 parser
->default_arg_ok_p
= false;
10321 parser
->in_declarator_p
= true;
10322 /* Consume the `['. */
10323 cp_lexer_consume_token (parser
->lexer
);
10324 /* Peek at the next token. */
10325 token
= cp_lexer_peek_token (parser
->lexer
);
10326 /* If the next token is `]', then there is no
10327 constant-expression. */
10328 if (token
->type
!= CPP_CLOSE_SQUARE
)
10330 bool non_constant_p
;
10333 = cp_parser_constant_expression (parser
,
10334 /*allow_non_constant=*/true,
10336 if (!non_constant_p
)
10337 bounds
= cp_parser_fold_non_dependent_expr (bounds
);
10340 bounds
= NULL_TREE
;
10341 /* Look for the closing `]'. */
10342 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
10344 declarator
= error_mark_node
;
10348 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
10350 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
10352 /* Parse a declarator-id */
10353 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10354 cp_parser_parse_tentatively (parser
);
10355 declarator
= cp_parser_declarator_id (parser
);
10356 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10358 if (!cp_parser_parse_definitely (parser
))
10359 declarator
= error_mark_node
;
10360 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
10362 cp_parser_error (parser
, "expected unqualified-id");
10363 declarator
= error_mark_node
;
10367 if (declarator
== error_mark_node
)
10370 if (TREE_CODE (declarator
) == SCOPE_REF
10371 && !current_scope ())
10373 tree scope
= TREE_OPERAND (declarator
, 0);
10375 /* In the declaration of a member of a template class
10376 outside of the class itself, the SCOPE will sometimes
10377 be a TYPENAME_TYPE. For example, given:
10379 template <typename T>
10380 int S<T>::R::i = 3;
10382 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10383 this context, we must resolve S<T>::R to an ordinary
10384 type, rather than a typename type.
10386 The reason we normally avoid resolving TYPENAME_TYPEs
10387 is that a specialization of `S' might render
10388 `S<T>::R' not a type. However, if `S' is
10389 specialized, then this `i' will not be used, so there
10390 is no harm in resolving the types here. */
10391 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
10395 /* Resolve the TYPENAME_TYPE. */
10396 type
= resolve_typename_type (scope
,
10397 /*only_current_p=*/false);
10398 /* If that failed, the declarator is invalid. */
10399 if (type
!= error_mark_node
)
10401 /* Build a new DECLARATOR. */
10402 declarator
= build_nt (SCOPE_REF
,
10404 TREE_OPERAND (declarator
, 1));
10408 /* Check to see whether the declarator-id names a constructor,
10409 destructor, or conversion. */
10410 if (declarator
&& ctor_dtor_or_conv_p
10411 && ((TREE_CODE (declarator
) == SCOPE_REF
10412 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
10413 || (TREE_CODE (declarator
) != SCOPE_REF
10414 && at_class_scope_p ())))
10416 tree unqualified_name
;
10419 /* Get the unqualified part of the name. */
10420 if (TREE_CODE (declarator
) == SCOPE_REF
)
10422 class_type
= TREE_OPERAND (declarator
, 0);
10423 unqualified_name
= TREE_OPERAND (declarator
, 1);
10427 class_type
= current_class_type
;
10428 unqualified_name
= declarator
;
10431 /* See if it names ctor, dtor or conv. */
10432 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
10433 || IDENTIFIER_TYPENAME_P (unqualified_name
)
10434 || constructor_name_p (unqualified_name
, class_type
))
10435 *ctor_dtor_or_conv_p
= -1;
10438 handle_declarator
:;
10439 scope
= get_scope_of_declarator (declarator
);
10441 /* Any names that appear after the declarator-id for a member
10442 are looked up in the containing scope. */
10443 push_scope (scope
);
10444 parser
->in_declarator_p
= true;
10445 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
10447 && (TREE_CODE (declarator
) == SCOPE_REF
10448 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
10449 /* Default args are only allowed on function
10451 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10453 parser
->default_arg_ok_p
= false;
10462 /* For an abstract declarator, we might wind up with nothing at this
10463 point. That's an error; the declarator is not optional. */
10465 cp_parser_error (parser
, "expected declarator");
10467 /* If we entered a scope, we must exit it now. */
10471 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10472 parser
->in_declarator_p
= saved_in_declarator_p
;
10477 /* Parse a ptr-operator.
10480 * cv-qualifier-seq [opt]
10482 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10487 & cv-qualifier-seq [opt]
10489 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10490 used. Returns ADDR_EXPR if a reference was used. In the
10491 case of a pointer-to-member, *TYPE is filled in with the
10492 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10493 with the cv-qualifier-seq, or NULL_TREE, if there are no
10494 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10496 static enum tree_code
10497 cp_parser_ptr_operator (cp_parser
* parser
,
10499 tree
* cv_qualifier_seq
)
10501 enum tree_code code
= ERROR_MARK
;
10504 /* Assume that it's not a pointer-to-member. */
10506 /* And that there are no cv-qualifiers. */
10507 *cv_qualifier_seq
= NULL_TREE
;
10509 /* Peek at the next token. */
10510 token
= cp_lexer_peek_token (parser
->lexer
);
10511 /* If it's a `*' or `&' we have a pointer or reference. */
10512 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10514 /* Remember which ptr-operator we were processing. */
10515 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10517 /* Consume the `*' or `&'. */
10518 cp_lexer_consume_token (parser
->lexer
);
10520 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10521 `&', if we are allowing GNU extensions. (The only qualifier
10522 that can legally appear after `&' is `restrict', but that is
10523 enforced during semantic analysis. */
10524 if (code
== INDIRECT_REF
10525 || cp_parser_allow_gnu_extensions_p (parser
))
10526 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10530 /* Try the pointer-to-member case. */
10531 cp_parser_parse_tentatively (parser
);
10532 /* Look for the optional `::' operator. */
10533 cp_parser_global_scope_opt (parser
,
10534 /*current_scope_valid_p=*/false);
10535 /* Look for the nested-name specifier. */
10536 cp_parser_nested_name_specifier (parser
,
10537 /*typename_keyword_p=*/false,
10538 /*check_dependency_p=*/true,
10540 /*is_declaration=*/false);
10541 /* If we found it, and the next token is a `*', then we are
10542 indeed looking at a pointer-to-member operator. */
10543 if (!cp_parser_error_occurred (parser
)
10544 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10546 /* The type of which the member is a member is given by the
10548 *type
= parser
->scope
;
10549 /* The next name will not be qualified. */
10550 parser
->scope
= NULL_TREE
;
10551 parser
->qualifying_scope
= NULL_TREE
;
10552 parser
->object_scope
= NULL_TREE
;
10553 /* Indicate that the `*' operator was used. */
10554 code
= INDIRECT_REF
;
10555 /* Look for the optional cv-qualifier-seq. */
10556 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10558 /* If that didn't work we don't have a ptr-operator. */
10559 if (!cp_parser_parse_definitely (parser
))
10560 cp_parser_error (parser
, "expected ptr-operator");
10566 /* Parse an (optional) cv-qualifier-seq.
10569 cv-qualifier cv-qualifier-seq [opt]
10571 Returns a TREE_LIST. The TREE_VALUE of each node is the
10572 representation of a cv-qualifier. */
10575 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10577 tree cv_qualifiers
= NULL_TREE
;
10583 /* Look for the next cv-qualifier. */
10584 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10585 /* If we didn't find one, we're done. */
10589 /* Add this cv-qualifier to the list. */
10591 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10594 /* We built up the list in reverse order. */
10595 return nreverse (cv_qualifiers
);
10598 /* Parse an (optional) cv-qualifier.
10610 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10613 tree cv_qualifier
= NULL_TREE
;
10615 /* Peek at the next token. */
10616 token
= cp_lexer_peek_token (parser
->lexer
);
10617 /* See if it's a cv-qualifier. */
10618 switch (token
->keyword
)
10623 /* Save the value of the token. */
10624 cv_qualifier
= token
->value
;
10625 /* Consume the token. */
10626 cp_lexer_consume_token (parser
->lexer
);
10633 return cv_qualifier
;
10636 /* Parse a declarator-id.
10640 :: [opt] nested-name-specifier [opt] type-name
10642 In the `id-expression' case, the value returned is as for
10643 cp_parser_id_expression if the id-expression was an unqualified-id.
10644 If the id-expression was a qualified-id, then a SCOPE_REF is
10645 returned. The first operand is the scope (either a NAMESPACE_DECL
10646 or TREE_TYPE), but the second is still just a representation of an
10650 cp_parser_declarator_id (cp_parser
* parser
)
10652 tree id_expression
;
10654 /* The expression must be an id-expression. Assume that qualified
10655 names are the names of types so that:
10658 int S<T>::R::i = 3;
10660 will work; we must treat `S<T>::R' as the name of a type.
10661 Similarly, assume that qualified names are templates, where
10665 int S<T>::R<T>::i = 3;
10668 id_expression
= cp_parser_id_expression (parser
,
10669 /*template_keyword_p=*/false,
10670 /*check_dependency_p=*/false,
10671 /*template_p=*/NULL
,
10672 /*declarator_p=*/true);
10673 /* If the name was qualified, create a SCOPE_REF to represent
10677 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
10678 parser
->scope
= NULL_TREE
;
10681 return id_expression
;
10684 /* Parse a type-id.
10687 type-specifier-seq abstract-declarator [opt]
10689 Returns the TYPE specified. */
10692 cp_parser_type_id (cp_parser
* parser
)
10694 tree type_specifier_seq
;
10695 tree abstract_declarator
;
10697 /* Parse the type-specifier-seq. */
10699 = cp_parser_type_specifier_seq (parser
);
10700 if (type_specifier_seq
== error_mark_node
)
10701 return error_mark_node
;
10703 /* There might or might not be an abstract declarator. */
10704 cp_parser_parse_tentatively (parser
);
10705 /* Look for the declarator. */
10706 abstract_declarator
10707 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
10708 /*parenthesized_p=*/NULL
);
10709 /* Check to see if there really was a declarator. */
10710 if (!cp_parser_parse_definitely (parser
))
10711 abstract_declarator
= NULL_TREE
;
10713 return groktypename (build_tree_list (type_specifier_seq
,
10714 abstract_declarator
));
10717 /* Parse a type-specifier-seq.
10719 type-specifier-seq:
10720 type-specifier type-specifier-seq [opt]
10724 type-specifier-seq:
10725 attributes type-specifier-seq [opt]
10727 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10728 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10731 cp_parser_type_specifier_seq (cp_parser
* parser
)
10733 bool seen_type_specifier
= false;
10734 tree type_specifier_seq
= NULL_TREE
;
10736 /* Parse the type-specifiers and attributes. */
10739 tree type_specifier
;
10741 /* Check for attributes first. */
10742 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
10744 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
10746 type_specifier_seq
);
10750 /* After the first type-specifier, others are optional. */
10751 if (seen_type_specifier
)
10752 cp_parser_parse_tentatively (parser
);
10753 /* Look for the type-specifier. */
10754 type_specifier
= cp_parser_type_specifier (parser
,
10755 CP_PARSER_FLAGS_NONE
,
10756 /*is_friend=*/false,
10757 /*is_declaration=*/false,
10760 /* If the first type-specifier could not be found, this is not a
10761 type-specifier-seq at all. */
10762 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
10763 return error_mark_node
;
10764 /* If subsequent type-specifiers could not be found, the
10765 type-specifier-seq is complete. */
10766 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
10769 /* Add the new type-specifier to the list. */
10771 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
10772 seen_type_specifier
= true;
10775 /* We built up the list in reverse order. */
10776 return nreverse (type_specifier_seq
);
10779 /* Parse a parameter-declaration-clause.
10781 parameter-declaration-clause:
10782 parameter-declaration-list [opt] ... [opt]
10783 parameter-declaration-list , ...
10785 Returns a representation for the parameter declarations. Each node
10786 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10787 representation.) If the parameter-declaration-clause ends with an
10788 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10789 list. A return value of NULL_TREE indicates a
10790 parameter-declaration-clause consisting only of an ellipsis. */
10793 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
10799 /* Peek at the next token. */
10800 token
= cp_lexer_peek_token (parser
->lexer
);
10801 /* Check for trivial parameter-declaration-clauses. */
10802 if (token
->type
== CPP_ELLIPSIS
)
10804 /* Consume the `...' token. */
10805 cp_lexer_consume_token (parser
->lexer
);
10808 else if (token
->type
== CPP_CLOSE_PAREN
)
10809 /* There are no parameters. */
10811 #ifndef NO_IMPLICIT_EXTERN_C
10812 if (in_system_header
&& current_class_type
== NULL
10813 && current_lang_name
== lang_name_c
)
10817 return void_list_node
;
10819 /* Check for `(void)', too, which is a special case. */
10820 else if (token
->keyword
== RID_VOID
10821 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
10822 == CPP_CLOSE_PAREN
))
10824 /* Consume the `void' token. */
10825 cp_lexer_consume_token (parser
->lexer
);
10826 /* There are no parameters. */
10827 return void_list_node
;
10830 /* Parse the parameter-declaration-list. */
10831 parameters
= cp_parser_parameter_declaration_list (parser
);
10832 /* If a parse error occurred while parsing the
10833 parameter-declaration-list, then the entire
10834 parameter-declaration-clause is erroneous. */
10835 if (parameters
== error_mark_node
)
10836 return error_mark_node
;
10838 /* Peek at the next token. */
10839 token
= cp_lexer_peek_token (parser
->lexer
);
10840 /* If it's a `,', the clause should terminate with an ellipsis. */
10841 if (token
->type
== CPP_COMMA
)
10843 /* Consume the `,'. */
10844 cp_lexer_consume_token (parser
->lexer
);
10845 /* Expect an ellipsis. */
10847 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
10849 /* It might also be `...' if the optional trailing `,' was
10851 else if (token
->type
== CPP_ELLIPSIS
)
10853 /* Consume the `...' token. */
10854 cp_lexer_consume_token (parser
->lexer
);
10855 /* And remember that we saw it. */
10859 ellipsis_p
= false;
10861 /* Finish the parameter list. */
10862 return finish_parmlist (parameters
, ellipsis_p
);
10865 /* Parse a parameter-declaration-list.
10867 parameter-declaration-list:
10868 parameter-declaration
10869 parameter-declaration-list , parameter-declaration
10871 Returns a representation of the parameter-declaration-list, as for
10872 cp_parser_parameter_declaration_clause. However, the
10873 `void_list_node' is never appended to the list. */
10876 cp_parser_parameter_declaration_list (cp_parser
* parser
)
10878 tree parameters
= NULL_TREE
;
10880 /* Look for more parameters. */
10884 bool parenthesized_p
;
10885 /* Parse the parameter. */
10887 = cp_parser_parameter_declaration (parser
,
10888 /*template_parm_p=*/false,
10891 /* If a parse error occurred parsing the parameter declaration,
10892 then the entire parameter-declaration-list is erroneous. */
10893 if (parameter
== error_mark_node
)
10895 parameters
= error_mark_node
;
10898 /* Add the new parameter to the list. */
10899 TREE_CHAIN (parameter
) = parameters
;
10900 parameters
= parameter
;
10902 /* Peek at the next token. */
10903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
10904 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10905 /* The parameter-declaration-list is complete. */
10907 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10911 /* Peek at the next token. */
10912 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10913 /* If it's an ellipsis, then the list is complete. */
10914 if (token
->type
== CPP_ELLIPSIS
)
10916 /* Otherwise, there must be more parameters. Consume the
10918 cp_lexer_consume_token (parser
->lexer
);
10919 /* When parsing something like:
10921 int i(float f, double d)
10923 we can tell after seeing the declaration for "f" that we
10924 are not looking at an initialization of a variable "i",
10925 but rather at the declaration of a function "i".
10927 Due to the fact that the parsing of template arguments
10928 (as specified to a template-id) requires backtracking we
10929 cannot use this technique when inside a template argument
10931 if (!parser
->in_template_argument_list_p
10932 && cp_parser_parsing_tentatively (parser
)
10933 && !cp_parser_committed_to_tentative_parse (parser
)
10934 /* However, a parameter-declaration of the form
10935 "foat(f)" (which is a valid declaration of a
10936 parameter "f") can also be interpreted as an
10937 expression (the conversion of "f" to "float"). */
10938 && !parenthesized_p
)
10939 cp_parser_commit_to_tentative_parse (parser
);
10943 cp_parser_error (parser
, "expected `,' or `...'");
10944 if (!cp_parser_parsing_tentatively (parser
)
10945 || cp_parser_committed_to_tentative_parse (parser
))
10946 cp_parser_skip_to_closing_parenthesis (parser
,
10947 /*recovering=*/true,
10948 /*or_comma=*/false,
10949 /*consume_paren=*/false);
10954 /* We built up the list in reverse order; straighten it out now. */
10955 return nreverse (parameters
);
10958 /* Parse a parameter declaration.
10960 parameter-declaration:
10961 decl-specifier-seq declarator
10962 decl-specifier-seq declarator = assignment-expression
10963 decl-specifier-seq abstract-declarator [opt]
10964 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10966 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10967 declares a template parameter. (In that case, a non-nested `>'
10968 token encountered during the parsing of the assignment-expression
10969 is not interpreted as a greater-than operator.)
10971 Returns a TREE_LIST representing the parameter-declaration. The
10972 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10973 there is no default argument. The TREE_VALUE is a representation
10974 of the decl-specifier-seq and declarator. In particular, the
10975 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10976 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10977 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10978 the declarator is of the form "(p)". */
10981 cp_parser_parameter_declaration (cp_parser
*parser
,
10982 bool template_parm_p
,
10983 bool *parenthesized_p
)
10985 int declares_class_or_enum
;
10986 bool greater_than_is_operator_p
;
10987 tree decl_specifiers
;
10990 tree default_argument
;
10993 const char *saved_message
;
10995 /* In a template parameter, `>' is not an operator.
10999 When parsing a default template-argument for a non-type
11000 template-parameter, the first non-nested `>' is taken as the end
11001 of the template parameter-list rather than a greater-than
11003 greater_than_is_operator_p
= !template_parm_p
;
11005 /* Type definitions may not appear in parameter types. */
11006 saved_message
= parser
->type_definition_forbidden_message
;
11007 parser
->type_definition_forbidden_message
11008 = "types may not be defined in parameter types";
11010 /* Parse the declaration-specifiers. */
11012 = cp_parser_decl_specifier_seq (parser
,
11013 CP_PARSER_FLAGS_NONE
,
11015 &declares_class_or_enum
);
11016 /* If an error occurred, there's no reason to attempt to parse the
11017 rest of the declaration. */
11018 if (cp_parser_error_occurred (parser
))
11020 parser
->type_definition_forbidden_message
= saved_message
;
11021 return error_mark_node
;
11024 /* Peek at the next token. */
11025 token
= cp_lexer_peek_token (parser
->lexer
);
11026 /* If the next token is a `)', `,', `=', `>', or `...', then there
11027 is no declarator. */
11028 if (token
->type
== CPP_CLOSE_PAREN
11029 || token
->type
== CPP_COMMA
11030 || token
->type
== CPP_EQ
11031 || token
->type
== CPP_ELLIPSIS
11032 || token
->type
== CPP_GREATER
)
11034 declarator
= NULL_TREE
;
11035 if (parenthesized_p
)
11036 *parenthesized_p
= false;
11038 /* Otherwise, there should be a declarator. */
11041 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11042 parser
->default_arg_ok_p
= false;
11044 /* After seeing a decl-specifier-seq, if the next token is not a
11045 "(", there is no possibility that the code is a valid
11046 expression. Therefore, if parsing tentatively, we commit at
11048 if (!parser
->in_template_argument_list_p
11049 /* In an expression context, having seen:
11053 we cannot be sure whether we are looking at a
11054 function-type (taking a "char*" as a parameter) or a cast
11055 of some object of type "char*" to "int". */
11056 && !parser
->in_type_id_in_expr_p
11057 && cp_parser_parsing_tentatively (parser
)
11058 && !cp_parser_committed_to_tentative_parse (parser
)
11059 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11060 cp_parser_commit_to_tentative_parse (parser
);
11061 /* Parse the declarator. */
11062 declarator
= cp_parser_declarator (parser
,
11063 CP_PARSER_DECLARATOR_EITHER
,
11064 /*ctor_dtor_or_conv_p=*/NULL
,
11066 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11067 /* After the declarator, allow more attributes. */
11068 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
11071 /* The restriction on defining new types applies only to the type
11072 of the parameter, not to the default argument. */
11073 parser
->type_definition_forbidden_message
= saved_message
;
11075 /* If the next token is `=', then process a default argument. */
11076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11078 bool saved_greater_than_is_operator_p
;
11079 /* Consume the `='. */
11080 cp_lexer_consume_token (parser
->lexer
);
11082 /* If we are defining a class, then the tokens that make up the
11083 default argument must be saved and processed later. */
11084 if (!template_parm_p
&& at_class_scope_p ()
11085 && TYPE_BEING_DEFINED (current_class_type
))
11087 unsigned depth
= 0;
11089 /* Create a DEFAULT_ARG to represented the unparsed default
11091 default_argument
= make_node (DEFAULT_ARG
);
11092 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
11094 /* Add tokens until we have processed the entire default
11101 /* Peek at the next token. */
11102 token
= cp_lexer_peek_token (parser
->lexer
);
11103 /* What we do depends on what token we have. */
11104 switch (token
->type
)
11106 /* In valid code, a default argument must be
11107 immediately followed by a `,' `)', or `...'. */
11109 case CPP_CLOSE_PAREN
:
11111 /* If we run into a non-nested `;', `}', or `]',
11112 then the code is invalid -- but the default
11113 argument is certainly over. */
11114 case CPP_SEMICOLON
:
11115 case CPP_CLOSE_BRACE
:
11116 case CPP_CLOSE_SQUARE
:
11119 /* Update DEPTH, if necessary. */
11120 else if (token
->type
== CPP_CLOSE_PAREN
11121 || token
->type
== CPP_CLOSE_BRACE
11122 || token
->type
== CPP_CLOSE_SQUARE
)
11126 case CPP_OPEN_PAREN
:
11127 case CPP_OPEN_SQUARE
:
11128 case CPP_OPEN_BRACE
:
11133 /* If we see a non-nested `>', and `>' is not an
11134 operator, then it marks the end of the default
11136 if (!depth
&& !greater_than_is_operator_p
)
11140 /* If we run out of tokens, issue an error message. */
11142 error ("file ends in default argument");
11148 /* In these cases, we should look for template-ids.
11149 For example, if the default argument is
11150 `X<int, double>()', we need to do name lookup to
11151 figure out whether or not `X' is a template; if
11152 so, the `,' does not end the default argument.
11154 That is not yet done. */
11161 /* If we've reached the end, stop. */
11165 /* Add the token to the token block. */
11166 token
= cp_lexer_consume_token (parser
->lexer
);
11167 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
11171 /* Outside of a class definition, we can just parse the
11172 assignment-expression. */
11175 bool saved_local_variables_forbidden_p
;
11177 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11179 saved_greater_than_is_operator_p
11180 = parser
->greater_than_is_operator_p
;
11181 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11182 /* Local variable names (and the `this' keyword) may not
11183 appear in a default argument. */
11184 saved_local_variables_forbidden_p
11185 = parser
->local_variables_forbidden_p
;
11186 parser
->local_variables_forbidden_p
= true;
11187 /* Parse the assignment-expression. */
11188 default_argument
= cp_parser_assignment_expression (parser
);
11189 /* Restore saved state. */
11190 parser
->greater_than_is_operator_p
11191 = saved_greater_than_is_operator_p
;
11192 parser
->local_variables_forbidden_p
11193 = saved_local_variables_forbidden_p
;
11195 if (!parser
->default_arg_ok_p
)
11197 if (!flag_pedantic_errors
)
11198 warning ("deprecated use of default argument for parameter of non-function");
11201 error ("default arguments are only permitted for function parameters");
11202 default_argument
= NULL_TREE
;
11207 default_argument
= NULL_TREE
;
11209 /* Create the representation of the parameter. */
11211 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
11212 parameter
= build_tree_list (default_argument
,
11213 build_tree_list (decl_specifiers
,
11219 /* Parse a function-body.
11222 compound_statement */
11225 cp_parser_function_body (cp_parser
*parser
)
11227 cp_parser_compound_statement (parser
, false);
11230 /* Parse a ctor-initializer-opt followed by a function-body. Return
11231 true if a ctor-initializer was present. */
11234 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11237 bool ctor_initializer_p
;
11239 /* Begin the function body. */
11240 body
= begin_function_body ();
11241 /* Parse the optional ctor-initializer. */
11242 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11243 /* Parse the function-body. */
11244 cp_parser_function_body (parser
);
11245 /* Finish the function body. */
11246 finish_function_body (body
);
11248 return ctor_initializer_p
;
11251 /* Parse an initializer.
11254 = initializer-clause
11255 ( expression-list )
11257 Returns a expression representing the initializer. If no
11258 initializer is present, NULL_TREE is returned.
11260 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11261 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11262 set to FALSE if there is no initializer present. If there is an
11263 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11264 is set to true; otherwise it is set to false. */
11267 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
11268 bool* non_constant_p
)
11273 /* Peek at the next token. */
11274 token
= cp_lexer_peek_token (parser
->lexer
);
11276 /* Let our caller know whether or not this initializer was
11278 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
11279 /* Assume that the initializer is constant. */
11280 *non_constant_p
= false;
11282 if (token
->type
== CPP_EQ
)
11284 /* Consume the `='. */
11285 cp_lexer_consume_token (parser
->lexer
);
11286 /* Parse the initializer-clause. */
11287 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
11289 else if (token
->type
== CPP_OPEN_PAREN
)
11290 init
= cp_parser_parenthesized_expression_list (parser
, false,
11294 /* Anything else is an error. */
11295 cp_parser_error (parser
, "expected initializer");
11296 init
= error_mark_node
;
11302 /* Parse an initializer-clause.
11304 initializer-clause:
11305 assignment-expression
11306 { initializer-list , [opt] }
11309 Returns an expression representing the initializer.
11311 If the `assignment-expression' production is used the value
11312 returned is simply a representation for the expression.
11314 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11315 the elements of the initializer-list (or NULL_TREE, if the last
11316 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11317 NULL_TREE. There is no way to detect whether or not the optional
11318 trailing `,' was provided. NON_CONSTANT_P is as for
11319 cp_parser_initializer. */
11322 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
11326 /* If it is not a `{', then we are looking at an
11327 assignment-expression. */
11328 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11330 = cp_parser_constant_expression (parser
,
11331 /*allow_non_constant_p=*/true,
11335 /* Consume the `{' token. */
11336 cp_lexer_consume_token (parser
->lexer
);
11337 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11338 initializer
= make_node (CONSTRUCTOR
);
11339 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11340 necessary, but check_initializer depends upon it, for
11342 TREE_HAS_CONSTRUCTOR (initializer
) = 1;
11343 /* If it's not a `}', then there is a non-trivial initializer. */
11344 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11346 /* Parse the initializer list. */
11347 CONSTRUCTOR_ELTS (initializer
)
11348 = cp_parser_initializer_list (parser
, non_constant_p
);
11349 /* A trailing `,' token is allowed. */
11350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11351 cp_lexer_consume_token (parser
->lexer
);
11353 /* Now, there should be a trailing `}'. */
11354 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11357 return initializer
;
11360 /* Parse an initializer-list.
11364 initializer-list , initializer-clause
11369 identifier : initializer-clause
11370 initializer-list, identifier : initializer-clause
11372 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11373 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11374 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11375 as for cp_parser_initializer. */
11378 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
11380 tree initializers
= NULL_TREE
;
11382 /* Assume all of the expressions are constant. */
11383 *non_constant_p
= false;
11385 /* Parse the rest of the list. */
11391 bool clause_non_constant_p
;
11393 /* If the next token is an identifier and the following one is a
11394 colon, we are looking at the GNU designated-initializer
11396 if (cp_parser_allow_gnu_extensions_p (parser
)
11397 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11398 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11400 /* Consume the identifier. */
11401 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11402 /* Consume the `:'. */
11403 cp_lexer_consume_token (parser
->lexer
);
11406 identifier
= NULL_TREE
;
11408 /* Parse the initializer. */
11409 initializer
= cp_parser_initializer_clause (parser
,
11410 &clause_non_constant_p
);
11411 /* If any clause is non-constant, so is the entire initializer. */
11412 if (clause_non_constant_p
)
11413 *non_constant_p
= true;
11414 /* Add it to the list. */
11415 initializers
= tree_cons (identifier
, initializer
, initializers
);
11417 /* If the next token is not a comma, we have reached the end of
11419 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11422 /* Peek at the next token. */
11423 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11424 /* If the next token is a `}', then we're still done. An
11425 initializer-clause can have a trailing `,' after the
11426 initializer-list and before the closing `}'. */
11427 if (token
->type
== CPP_CLOSE_BRACE
)
11430 /* Consume the `,' token. */
11431 cp_lexer_consume_token (parser
->lexer
);
11434 /* The initializers were built up in reverse order, so we need to
11435 reverse them now. */
11436 return nreverse (initializers
);
11439 /* Classes [gram.class] */
11441 /* Parse a class-name.
11447 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11448 to indicate that names looked up in dependent types should be
11449 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11450 keyword has been used to indicate that the name that appears next
11451 is a template. TYPE_P is true iff the next name should be treated
11452 as class-name, even if it is declared to be some other kind of name
11453 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11454 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11455 being defined in a class-head.
11457 Returns the TYPE_DECL representing the class. */
11460 cp_parser_class_name (cp_parser
*parser
,
11461 bool typename_keyword_p
,
11462 bool template_keyword_p
,
11464 bool check_dependency_p
,
11466 bool is_declaration
)
11473 /* All class-names start with an identifier. */
11474 token
= cp_lexer_peek_token (parser
->lexer
);
11475 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11477 cp_parser_error (parser
, "expected class-name");
11478 return error_mark_node
;
11481 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11482 to a template-id, so we save it here. */
11483 scope
= parser
->scope
;
11484 if (scope
== error_mark_node
)
11485 return error_mark_node
;
11487 /* Any name names a type if we're following the `typename' keyword
11488 in a qualified name where the enclosing scope is type-dependent. */
11489 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11490 && dependent_type_p (scope
));
11491 /* Handle the common case (an identifier, but not a template-id)
11493 if (token
->type
== CPP_NAME
11494 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
11498 /* Look for the identifier. */
11499 identifier
= cp_parser_identifier (parser
);
11500 /* If the next token isn't an identifier, we are certainly not
11501 looking at a class-name. */
11502 if (identifier
== error_mark_node
)
11503 decl
= error_mark_node
;
11504 /* If we know this is a type-name, there's no need to look it
11506 else if (typename_p
)
11510 /* If the next token is a `::', then the name must be a type
11513 [basic.lookup.qual]
11515 During the lookup for a name preceding the :: scope
11516 resolution operator, object, function, and enumerator
11517 names are ignored. */
11518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11520 /* Look up the name. */
11521 decl
= cp_parser_lookup_name (parser
, identifier
,
11523 /*is_template=*/false,
11524 /*is_namespace=*/false,
11525 check_dependency_p
);
11530 /* Try a template-id. */
11531 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11532 check_dependency_p
,
11534 if (decl
== error_mark_node
)
11535 return error_mark_node
;
11538 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11540 /* If this is a typename, create a TYPENAME_TYPE. */
11541 if (typename_p
&& decl
!= error_mark_node
)
11542 decl
= TYPE_NAME (make_typename_type (scope
, decl
,
11545 /* Check to see that it is really the name of a class. */
11546 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11547 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11548 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11549 /* Situations like this:
11551 template <typename T> struct A {
11552 typename T::template X<int>::I i;
11555 are problematic. Is `T::template X<int>' a class-name? The
11556 standard does not seem to be definitive, but there is no other
11557 valid interpretation of the following `::'. Therefore, those
11558 names are considered class-names. */
11559 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11560 else if (decl
== error_mark_node
11561 || TREE_CODE (decl
) != TYPE_DECL
11562 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11564 cp_parser_error (parser
, "expected class-name");
11565 return error_mark_node
;
11571 /* Parse a class-specifier.
11574 class-head { member-specification [opt] }
11576 Returns the TREE_TYPE representing the class. */
11579 cp_parser_class_specifier (cp_parser
* parser
)
11583 tree attributes
= NULL_TREE
;
11584 int has_trailing_semicolon
;
11585 bool nested_name_specifier_p
;
11586 unsigned saved_num_template_parameter_lists
;
11588 push_deferring_access_checks (dk_no_deferred
);
11590 /* Parse the class-head. */
11591 type
= cp_parser_class_head (parser
,
11592 &nested_name_specifier_p
);
11593 /* If the class-head was a semantic disaster, skip the entire body
11597 cp_parser_skip_to_end_of_block_or_statement (parser
);
11598 pop_deferring_access_checks ();
11599 return error_mark_node
;
11602 /* Look for the `{'. */
11603 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11605 pop_deferring_access_checks ();
11606 return error_mark_node
;
11609 /* Issue an error message if type-definitions are forbidden here. */
11610 cp_parser_check_type_definition (parser
);
11611 /* Remember that we are defining one more class. */
11612 ++parser
->num_classes_being_defined
;
11613 /* Inside the class, surrounding template-parameter-lists do not
11615 saved_num_template_parameter_lists
11616 = parser
->num_template_parameter_lists
;
11617 parser
->num_template_parameter_lists
= 0;
11619 /* Start the class. */
11620 if (nested_name_specifier_p
)
11621 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11622 type
= begin_class_definition (type
);
11623 if (type
== error_mark_node
)
11624 /* If the type is erroneous, skip the entire body of the class. */
11625 cp_parser_skip_to_closing_brace (parser
);
11627 /* Parse the member-specification. */
11628 cp_parser_member_specification_opt (parser
);
11629 /* Look for the trailing `}'. */
11630 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11631 /* We get better error messages by noticing a common problem: a
11632 missing trailing `;'. */
11633 token
= cp_lexer_peek_token (parser
->lexer
);
11634 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11635 /* Look for attributes to apply to this class. */
11636 if (cp_parser_allow_gnu_extensions_p (parser
))
11637 attributes
= cp_parser_attributes_opt (parser
);
11638 /* If we got any attributes in class_head, xref_tag will stick them in
11639 TREE_TYPE of the type. Grab them now. */
11640 if (type
!= error_mark_node
)
11642 attributes
= chainon (TYPE_ATTRIBUTES (type
), attributes
);
11643 TYPE_ATTRIBUTES (type
) = NULL_TREE
;
11644 type
= finish_struct (type
, attributes
);
11646 if (nested_name_specifier_p
)
11647 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11648 /* If this class is not itself within the scope of another class,
11649 then we need to parse the bodies of all of the queued function
11650 definitions. Note that the queued functions defined in a class
11651 are not always processed immediately following the
11652 class-specifier for that class. Consider:
11655 struct B { void f() { sizeof (A); } };
11658 If `f' were processed before the processing of `A' were
11659 completed, there would be no way to compute the size of `A'.
11660 Note that the nesting we are interested in here is lexical --
11661 not the semantic nesting given by TYPE_CONTEXT. In particular,
11664 struct A { struct B; };
11665 struct A::B { void f() { } };
11667 there is no need to delay the parsing of `A::B::f'. */
11668 if (--parser
->num_classes_being_defined
== 0)
11673 /* In a first pass, parse default arguments to the functions.
11674 Then, in a second pass, parse the bodies of the functions.
11675 This two-phased approach handles cases like:
11683 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
11684 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
11685 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
11686 TREE_PURPOSE (parser
->unparsed_functions_queues
)
11687 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
11689 fn
= TREE_VALUE (queue_entry
);
11690 /* Make sure that any template parameters are in scope. */
11691 maybe_begin_member_template_processing (fn
);
11692 /* If there are default arguments that have not yet been processed,
11693 take care of them now. */
11694 cp_parser_late_parsing_default_args (parser
, fn
);
11695 /* Remove any template parameters from the symbol table. */
11696 maybe_end_member_template_processing ();
11698 /* Now parse the body of the functions. */
11699 for (TREE_VALUE (parser
->unparsed_functions_queues
)
11700 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
11701 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
11702 TREE_VALUE (parser
->unparsed_functions_queues
)
11703 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
11705 /* Figure out which function we need to process. */
11706 fn
= TREE_VALUE (queue_entry
);
11708 /* A hack to prevent garbage collection. */
11711 /* Parse the function. */
11712 cp_parser_late_parsing_for_member (parser
, fn
);
11718 /* Put back any saved access checks. */
11719 pop_deferring_access_checks ();
11721 /* Restore the count of active template-parameter-lists. */
11722 parser
->num_template_parameter_lists
11723 = saved_num_template_parameter_lists
;
11728 /* Parse a class-head.
11731 class-key identifier [opt] base-clause [opt]
11732 class-key nested-name-specifier identifier base-clause [opt]
11733 class-key nested-name-specifier [opt] template-id
11737 class-key attributes identifier [opt] base-clause [opt]
11738 class-key attributes nested-name-specifier identifier base-clause [opt]
11739 class-key attributes nested-name-specifier [opt] template-id
11742 Returns the TYPE of the indicated class. Sets
11743 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11744 involving a nested-name-specifier was used, and FALSE otherwise.
11746 Returns NULL_TREE if the class-head is syntactically valid, but
11747 semantically invalid in a way that means we should skip the entire
11748 body of the class. */
11751 cp_parser_class_head (cp_parser
* parser
,
11752 bool* nested_name_specifier_p
)
11755 tree nested_name_specifier
;
11756 enum tag_types class_key
;
11757 tree id
= NULL_TREE
;
11758 tree type
= NULL_TREE
;
11760 bool template_id_p
= false;
11761 bool qualified_p
= false;
11762 bool invalid_nested_name_p
= false;
11763 bool invalid_explicit_specialization_p
= false;
11764 unsigned num_templates
;
11766 /* Assume no nested-name-specifier will be present. */
11767 *nested_name_specifier_p
= false;
11768 /* Assume no template parameter lists will be used in defining the
11772 /* Look for the class-key. */
11773 class_key
= cp_parser_class_key (parser
);
11774 if (class_key
== none_type
)
11775 return error_mark_node
;
11777 /* Parse the attributes. */
11778 attributes
= cp_parser_attributes_opt (parser
);
11780 /* If the next token is `::', that is invalid -- but sometimes
11781 people do try to write:
11785 Handle this gracefully by accepting the extra qualifier, and then
11786 issuing an error about it later if this really is a
11787 class-head. If it turns out just to be an elaborated type
11788 specifier, remain silent. */
11789 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
11790 qualified_p
= true;
11792 push_deferring_access_checks (dk_no_check
);
11794 /* Determine the name of the class. Begin by looking for an
11795 optional nested-name-specifier. */
11796 nested_name_specifier
11797 = cp_parser_nested_name_specifier_opt (parser
,
11798 /*typename_keyword_p=*/false,
11799 /*check_dependency_p=*/false,
11801 /*is_declaration=*/false);
11802 /* If there was a nested-name-specifier, then there *must* be an
11804 if (nested_name_specifier
)
11806 /* Although the grammar says `identifier', it really means
11807 `class-name' or `template-name'. You are only allowed to
11808 define a class that has already been declared with this
11811 The proposed resolution for Core Issue 180 says that whever
11812 you see `class T::X' you should treat `X' as a type-name.
11814 It is OK to define an inaccessible class; for example:
11816 class A { class B; };
11819 We do not know if we will see a class-name, or a
11820 template-name. We look for a class-name first, in case the
11821 class-name is a template-id; if we looked for the
11822 template-name first we would stop after the template-name. */
11823 cp_parser_parse_tentatively (parser
);
11824 type
= cp_parser_class_name (parser
,
11825 /*typename_keyword_p=*/false,
11826 /*template_keyword_p=*/false,
11828 /*check_dependency_p=*/false,
11829 /*class_head_p=*/true,
11830 /*is_declaration=*/false);
11831 /* If that didn't work, ignore the nested-name-specifier. */
11832 if (!cp_parser_parse_definitely (parser
))
11834 invalid_nested_name_p
= true;
11835 id
= cp_parser_identifier (parser
);
11836 if (id
== error_mark_node
)
11839 /* If we could not find a corresponding TYPE, treat this
11840 declaration like an unqualified declaration. */
11841 if (type
== error_mark_node
)
11842 nested_name_specifier
= NULL_TREE
;
11843 /* Otherwise, count the number of templates used in TYPE and its
11844 containing scopes. */
11849 for (scope
= TREE_TYPE (type
);
11850 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
11851 scope
= (TYPE_P (scope
)
11852 ? TYPE_CONTEXT (scope
)
11853 : DECL_CONTEXT (scope
)))
11855 && CLASS_TYPE_P (scope
)
11856 && CLASSTYPE_TEMPLATE_INFO (scope
)
11857 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
11858 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
11862 /* Otherwise, the identifier is optional. */
11865 /* We don't know whether what comes next is a template-id,
11866 an identifier, or nothing at all. */
11867 cp_parser_parse_tentatively (parser
);
11868 /* Check for a template-id. */
11869 id
= cp_parser_template_id (parser
,
11870 /*template_keyword_p=*/false,
11871 /*check_dependency_p=*/true,
11872 /*is_declaration=*/true);
11873 /* If that didn't work, it could still be an identifier. */
11874 if (!cp_parser_parse_definitely (parser
))
11876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11877 id
= cp_parser_identifier (parser
);
11883 template_id_p
= true;
11888 pop_deferring_access_checks ();
11890 cp_parser_check_for_invalid_template_id (parser
, id
);
11892 /* If it's not a `:' or a `{' then we can't really be looking at a
11893 class-head, since a class-head only appears as part of a
11894 class-specifier. We have to detect this situation before calling
11895 xref_tag, since that has irreversible side-effects. */
11896 if (!cp_parser_next_token_starts_class_definition_p (parser
))
11898 cp_parser_error (parser
, "expected `{' or `:'");
11899 return error_mark_node
;
11902 /* At this point, we're going ahead with the class-specifier, even
11903 if some other problem occurs. */
11904 cp_parser_commit_to_tentative_parse (parser
);
11905 /* Issue the error about the overly-qualified name now. */
11907 cp_parser_error (parser
,
11908 "global qualification of class name is invalid");
11909 else if (invalid_nested_name_p
)
11910 cp_parser_error (parser
,
11911 "qualified name does not name a class");
11912 else if (nested_name_specifier
)
11915 /* Figure out in what scope the declaration is being placed. */
11916 scope
= current_scope ();
11918 scope
= current_namespace
;
11919 /* If that scope does not contain the scope in which the
11920 class was originally declared, the program is invalid. */
11921 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
11923 error ("declaration of `%D' in `%D' which does not "
11924 "enclose `%D'", type
, scope
, nested_name_specifier
);
11930 A declarator-id shall not be qualified exception of the
11931 definition of a ... nested class outside of its class
11932 ... [or] a the definition or explicit instantiation of a
11933 class member of a namespace outside of its namespace. */
11934 if (scope
== nested_name_specifier
)
11936 pedwarn ("extra qualification ignored");
11937 nested_name_specifier
= NULL_TREE
;
11941 /* An explicit-specialization must be preceded by "template <>". If
11942 it is not, try to recover gracefully. */
11943 if (at_namespace_scope_p ()
11944 && parser
->num_template_parameter_lists
== 0
11947 error ("an explicit specialization must be preceded by 'template <>'");
11948 invalid_explicit_specialization_p
= true;
11949 /* Take the same action that would have been taken by
11950 cp_parser_explicit_specialization. */
11951 ++parser
->num_template_parameter_lists
;
11952 begin_specialization ();
11954 /* There must be no "return" statements between this point and the
11955 end of this function; set "type "to the correct return value and
11956 use "goto done;" to return. */
11957 /* Make sure that the right number of template parameters were
11959 if (!cp_parser_check_template_parameters (parser
, num_templates
))
11961 /* If something went wrong, there is no point in even trying to
11962 process the class-definition. */
11967 /* Look up the type. */
11970 type
= TREE_TYPE (id
);
11971 maybe_process_partial_specialization (type
);
11973 else if (!nested_name_specifier
)
11975 /* If the class was unnamed, create a dummy name. */
11977 id
= make_anon_name ();
11978 type
= xref_tag (class_key
, id
, attributes
, /*globalize=*/false,
11979 parser
->num_template_parameter_lists
);
11987 template <typename T> struct S { struct T };
11988 template <typename T> struct S<T>::T { };
11990 we will get a TYPENAME_TYPE when processing the definition of
11991 `S::T'. We need to resolve it to the actual type before we
11992 try to define it. */
11993 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
11995 class_type
= resolve_typename_type (TREE_TYPE (type
),
11996 /*only_current_p=*/false);
11997 if (class_type
!= error_mark_node
)
11998 type
= TYPE_NAME (class_type
);
12001 cp_parser_error (parser
, "could not resolve typename type");
12002 type
= error_mark_node
;
12006 maybe_process_partial_specialization (TREE_TYPE (type
));
12007 class_type
= current_class_type
;
12008 /* Enter the scope indicated by the nested-name-specifier. */
12009 if (nested_name_specifier
)
12010 push_scope (nested_name_specifier
);
12011 /* Get the canonical version of this type. */
12012 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
12013 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12014 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
12015 type
= push_template_decl (type
);
12016 type
= TREE_TYPE (type
);
12017 if (nested_name_specifier
)
12019 *nested_name_specifier_p
= true;
12020 pop_scope (nested_name_specifier
);
12023 /* Indicate whether this class was declared as a `class' or as a
12025 if (TREE_CODE (type
) == RECORD_TYPE
)
12026 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
12027 cp_parser_check_class_key (class_key
, type
);
12029 /* Enter the scope containing the class; the names of base classes
12030 should be looked up in that context. For example, given:
12032 struct A { struct B {}; struct C; };
12033 struct A::C : B {};
12036 if (nested_name_specifier
)
12037 push_scope (nested_name_specifier
);
12038 /* Now, look for the base-clause. */
12039 token
= cp_lexer_peek_token (parser
->lexer
);
12040 if (token
->type
== CPP_COLON
)
12044 /* Get the list of base-classes. */
12045 bases
= cp_parser_base_clause (parser
);
12046 /* Process them. */
12047 xref_basetypes (type
, bases
);
12049 /* Leave the scope given by the nested-name-specifier. We will
12050 enter the class scope itself while processing the members. */
12051 if (nested_name_specifier
)
12052 pop_scope (nested_name_specifier
);
12055 if (invalid_explicit_specialization_p
)
12057 end_specialization ();
12058 --parser
->num_template_parameter_lists
;
12063 /* Parse a class-key.
12070 Returns the kind of class-key specified, or none_type to indicate
12073 static enum tag_types
12074 cp_parser_class_key (cp_parser
* parser
)
12077 enum tag_types tag_type
;
12079 /* Look for the class-key. */
12080 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12084 /* Check to see if the TOKEN is a class-key. */
12085 tag_type
= cp_parser_token_is_class_key (token
);
12087 cp_parser_error (parser
, "expected class-key");
12091 /* Parse an (optional) member-specification.
12093 member-specification:
12094 member-declaration member-specification [opt]
12095 access-specifier : member-specification [opt] */
12098 cp_parser_member_specification_opt (cp_parser
* parser
)
12105 /* Peek at the next token. */
12106 token
= cp_lexer_peek_token (parser
->lexer
);
12107 /* If it's a `}', or EOF then we've seen all the members. */
12108 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12111 /* See if this token is a keyword. */
12112 keyword
= token
->keyword
;
12116 case RID_PROTECTED
:
12118 /* Consume the access-specifier. */
12119 cp_lexer_consume_token (parser
->lexer
);
12120 /* Remember which access-specifier is active. */
12121 current_access_specifier
= token
->value
;
12122 /* Look for the `:'. */
12123 cp_parser_require (parser
, CPP_COLON
, "`:'");
12127 /* Otherwise, the next construction must be a
12128 member-declaration. */
12129 cp_parser_member_declaration (parser
);
12134 /* Parse a member-declaration.
12136 member-declaration:
12137 decl-specifier-seq [opt] member-declarator-list [opt] ;
12138 function-definition ; [opt]
12139 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12141 template-declaration
12143 member-declarator-list:
12145 member-declarator-list , member-declarator
12148 declarator pure-specifier [opt]
12149 declarator constant-initializer [opt]
12150 identifier [opt] : constant-expression
12154 member-declaration:
12155 __extension__ member-declaration
12158 declarator attributes [opt] pure-specifier [opt]
12159 declarator attributes [opt] constant-initializer [opt]
12160 identifier [opt] attributes [opt] : constant-expression */
12163 cp_parser_member_declaration (cp_parser
* parser
)
12165 tree decl_specifiers
;
12166 tree prefix_attributes
;
12168 int declares_class_or_enum
;
12171 int saved_pedantic
;
12173 /* Check for the `__extension__' keyword. */
12174 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12177 cp_parser_member_declaration (parser
);
12178 /* Restore the old value of the PEDANTIC flag. */
12179 pedantic
= saved_pedantic
;
12184 /* Check for a template-declaration. */
12185 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12187 /* Parse the template-declaration. */
12188 cp_parser_template_declaration (parser
, /*member_p=*/true);
12193 /* Check for a using-declaration. */
12194 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
12196 /* Parse the using-declaration. */
12197 cp_parser_using_declaration (parser
);
12202 /* Parse the decl-specifier-seq. */
12204 = cp_parser_decl_specifier_seq (parser
,
12205 CP_PARSER_FLAGS_OPTIONAL
,
12206 &prefix_attributes
,
12207 &declares_class_or_enum
);
12208 /* Check for an invalid type-name. */
12209 if (cp_parser_diagnose_invalid_type_name (parser
))
12211 /* If there is no declarator, then the decl-specifier-seq should
12213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12215 /* If there was no decl-specifier-seq, and the next token is a
12216 `;', then we have something like:
12222 Each member-declaration shall declare at least one member
12223 name of the class. */
12224 if (!decl_specifiers
)
12227 pedwarn ("extra semicolon");
12233 /* See if this declaration is a friend. */
12234 friend_p
= cp_parser_friend_p (decl_specifiers
);
12235 /* If there were decl-specifiers, check to see if there was
12236 a class-declaration. */
12237 type
= check_tag_decl (decl_specifiers
);
12238 /* Nested classes have already been added to the class, but
12239 a `friend' needs to be explicitly registered. */
12242 /* If the `friend' keyword was present, the friend must
12243 be introduced with a class-key. */
12244 if (!declares_class_or_enum
)
12245 error ("a class-key must be used when declaring a friend");
12248 template <typename T> struct A {
12249 friend struct A<T>::B;
12252 A<T>::B will be represented by a TYPENAME_TYPE, and
12253 therefore not recognized by check_tag_decl. */
12258 for (specifier
= decl_specifiers
;
12260 specifier
= TREE_CHAIN (specifier
))
12262 tree s
= TREE_VALUE (specifier
);
12264 if (TREE_CODE (s
) == IDENTIFIER_NODE
)
12265 get_global_value_if_present (s
, &type
);
12266 if (TREE_CODE (s
) == TYPE_DECL
)
12275 if (!type
|| !TYPE_P (type
))
12276 error ("friend declaration does not name a class or "
12279 make_friend_class (current_class_type
, type
,
12280 /*complain=*/true);
12282 /* If there is no TYPE, an error message will already have
12286 /* An anonymous aggregate has to be handled specially; such
12287 a declaration really declares a data member (with a
12288 particular type), as opposed to a nested class. */
12289 else if (ANON_AGGR_TYPE_P (type
))
12291 /* Remove constructors and such from TYPE, now that we
12292 know it is an anonymous aggregate. */
12293 fixup_anonymous_aggr (type
);
12294 /* And make the corresponding data member. */
12295 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
12296 /* Add it to the class. */
12297 finish_member_declaration (decl
);
12300 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
12305 /* See if these declarations will be friends. */
12306 friend_p
= cp_parser_friend_p (decl_specifiers
);
12308 /* Keep going until we hit the `;' at the end of the
12310 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12312 tree attributes
= NULL_TREE
;
12313 tree first_attribute
;
12315 /* Peek at the next token. */
12316 token
= cp_lexer_peek_token (parser
->lexer
);
12318 /* Check for a bitfield declaration. */
12319 if (token
->type
== CPP_COLON
12320 || (token
->type
== CPP_NAME
12321 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12327 /* Get the name of the bitfield. Note that we cannot just
12328 check TOKEN here because it may have been invalidated by
12329 the call to cp_lexer_peek_nth_token above. */
12330 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
12331 identifier
= cp_parser_identifier (parser
);
12333 identifier
= NULL_TREE
;
12335 /* Consume the `:' token. */
12336 cp_lexer_consume_token (parser
->lexer
);
12337 /* Get the width of the bitfield. */
12339 = cp_parser_constant_expression (parser
,
12340 /*allow_non_constant=*/false,
12343 /* Look for attributes that apply to the bitfield. */
12344 attributes
= cp_parser_attributes_opt (parser
);
12345 /* Remember which attributes are prefix attributes and
12347 first_attribute
= attributes
;
12348 /* Combine the attributes. */
12349 attributes
= chainon (prefix_attributes
, attributes
);
12351 /* Create the bitfield declaration. */
12352 decl
= grokbitfield (identifier
,
12355 /* Apply the attributes. */
12356 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
12362 tree asm_specification
;
12363 int ctor_dtor_or_conv_p
;
12365 /* Parse the declarator. */
12367 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12368 &ctor_dtor_or_conv_p
,
12369 /*parenthesized_p=*/NULL
);
12371 /* If something went wrong parsing the declarator, make sure
12372 that we at least consume some tokens. */
12373 if (declarator
== error_mark_node
)
12375 /* Skip to the end of the statement. */
12376 cp_parser_skip_to_end_of_statement (parser
);
12377 /* If the next token is not a semicolon, that is
12378 probably because we just skipped over the body of
12379 a function. So, we consume a semicolon if
12380 present, but do not issue an error message if it
12382 if (cp_lexer_next_token_is (parser
->lexer
,
12384 cp_lexer_consume_token (parser
->lexer
);
12388 cp_parser_check_for_definition_in_return_type
12389 (declarator
, declares_class_or_enum
);
12391 /* Look for an asm-specification. */
12392 asm_specification
= cp_parser_asm_specification_opt (parser
);
12393 /* Look for attributes that apply to the declaration. */
12394 attributes
= cp_parser_attributes_opt (parser
);
12395 /* Remember which attributes are prefix attributes and
12397 first_attribute
= attributes
;
12398 /* Combine the attributes. */
12399 attributes
= chainon (prefix_attributes
, attributes
);
12401 /* If it's an `=', then we have a constant-initializer or a
12402 pure-specifier. It is not correct to parse the
12403 initializer before registering the member declaration
12404 since the member declaration should be in scope while
12405 its initializer is processed. However, the rest of the
12406 front end does not yet provide an interface that allows
12407 us to handle this correctly. */
12408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12412 A pure-specifier shall be used only in the declaration of
12413 a virtual function.
12415 A member-declarator can contain a constant-initializer
12416 only if it declares a static member of integral or
12419 Therefore, if the DECLARATOR is for a function, we look
12420 for a pure-specifier; otherwise, we look for a
12421 constant-initializer. When we call `grokfield', it will
12422 perform more stringent semantics checks. */
12423 if (TREE_CODE (declarator
) == CALL_EXPR
)
12424 initializer
= cp_parser_pure_specifier (parser
);
12426 /* Parse the initializer. */
12427 initializer
= cp_parser_constant_initializer (parser
);
12429 /* Otherwise, there is no initializer. */
12431 initializer
= NULL_TREE
;
12433 /* See if we are probably looking at a function
12434 definition. We are certainly not looking at at a
12435 member-declarator. Calling `grokfield' has
12436 side-effects, so we must not do it unless we are sure
12437 that we are looking at a member-declarator. */
12438 if (cp_parser_token_starts_function_definition_p
12439 (cp_lexer_peek_token (parser
->lexer
)))
12441 /* The grammar does not allow a pure-specifier to be
12442 used when a member function is defined. (It is
12443 possible that this fact is an oversight in the
12444 standard, since a pure function may be defined
12445 outside of the class-specifier. */
12447 error ("pure-specifier on function-definition");
12448 decl
= cp_parser_save_member_function_body (parser
,
12452 /* If the member was not a friend, declare it here. */
12454 finish_member_declaration (decl
);
12455 /* Peek at the next token. */
12456 token
= cp_lexer_peek_token (parser
->lexer
);
12457 /* If the next token is a semicolon, consume it. */
12458 if (token
->type
== CPP_SEMICOLON
)
12459 cp_lexer_consume_token (parser
->lexer
);
12464 /* Create the declaration. */
12465 decl
= grokfield (declarator
, decl_specifiers
,
12466 initializer
, asm_specification
,
12468 /* Any initialization must have been from a
12469 constant-expression. */
12470 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
12471 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
12475 /* Reset PREFIX_ATTRIBUTES. */
12476 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12477 attributes
= TREE_CHAIN (attributes
);
12479 TREE_CHAIN (attributes
) = NULL_TREE
;
12481 /* If there is any qualification still in effect, clear it
12482 now; we will be starting fresh with the next declarator. */
12483 parser
->scope
= NULL_TREE
;
12484 parser
->qualifying_scope
= NULL_TREE
;
12485 parser
->object_scope
= NULL_TREE
;
12486 /* If it's a `,', then there are more declarators. */
12487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12488 cp_lexer_consume_token (parser
->lexer
);
12489 /* If the next token isn't a `;', then we have a parse error. */
12490 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12493 cp_parser_error (parser
, "expected `;'");
12494 /* Skip tokens until we find a `;'. */
12495 cp_parser_skip_to_end_of_statement (parser
);
12502 /* Add DECL to the list of members. */
12504 finish_member_declaration (decl
);
12506 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12507 cp_parser_save_default_args (parser
, decl
);
12512 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12515 /* Parse a pure-specifier.
12520 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12521 Otherwise, ERROR_MARK_NODE is returned. */
12524 cp_parser_pure_specifier (cp_parser
* parser
)
12528 /* Look for the `=' token. */
12529 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12530 return error_mark_node
;
12531 /* Look for the `0' token. */
12532 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12533 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12534 to get information from the lexer about how the number was
12535 spelled in order to fix this problem. */
12536 if (!token
|| !integer_zerop (token
->value
))
12537 return error_mark_node
;
12539 return integer_zero_node
;
12542 /* Parse a constant-initializer.
12544 constant-initializer:
12545 = constant-expression
12547 Returns a representation of the constant-expression. */
12550 cp_parser_constant_initializer (cp_parser
* parser
)
12552 /* Look for the `=' token. */
12553 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12554 return error_mark_node
;
12556 /* It is invalid to write:
12558 struct S { static const int i = { 7 }; };
12561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12563 cp_parser_error (parser
,
12564 "a brace-enclosed initializer is not allowed here");
12565 /* Consume the opening brace. */
12566 cp_lexer_consume_token (parser
->lexer
);
12567 /* Skip the initializer. */
12568 cp_parser_skip_to_closing_brace (parser
);
12569 /* Look for the trailing `}'. */
12570 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12572 return error_mark_node
;
12575 return cp_parser_constant_expression (parser
,
12576 /*allow_non_constant=*/false,
12580 /* Derived classes [gram.class.derived] */
12582 /* Parse a base-clause.
12585 : base-specifier-list
12587 base-specifier-list:
12589 base-specifier-list , base-specifier
12591 Returns a TREE_LIST representing the base-classes, in the order in
12592 which they were declared. The representation of each node is as
12593 described by cp_parser_base_specifier.
12595 In the case that no bases are specified, this function will return
12596 NULL_TREE, not ERROR_MARK_NODE. */
12599 cp_parser_base_clause (cp_parser
* parser
)
12601 tree bases
= NULL_TREE
;
12603 /* Look for the `:' that begins the list. */
12604 cp_parser_require (parser
, CPP_COLON
, "`:'");
12606 /* Scan the base-specifier-list. */
12612 /* Look for the base-specifier. */
12613 base
= cp_parser_base_specifier (parser
);
12614 /* Add BASE to the front of the list. */
12615 if (base
!= error_mark_node
)
12617 TREE_CHAIN (base
) = bases
;
12620 /* Peek at the next token. */
12621 token
= cp_lexer_peek_token (parser
->lexer
);
12622 /* If it's not a comma, then the list is complete. */
12623 if (token
->type
!= CPP_COMMA
)
12625 /* Consume the `,'. */
12626 cp_lexer_consume_token (parser
->lexer
);
12629 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12630 base class had a qualified name. However, the next name that
12631 appears is certainly not qualified. */
12632 parser
->scope
= NULL_TREE
;
12633 parser
->qualifying_scope
= NULL_TREE
;
12634 parser
->object_scope
= NULL_TREE
;
12636 return nreverse (bases
);
12639 /* Parse a base-specifier.
12642 :: [opt] nested-name-specifier [opt] class-name
12643 virtual access-specifier [opt] :: [opt] nested-name-specifier
12645 access-specifier virtual [opt] :: [opt] nested-name-specifier
12648 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12649 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12650 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12651 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12654 cp_parser_base_specifier (cp_parser
* parser
)
12658 bool virtual_p
= false;
12659 bool duplicate_virtual_error_issued_p
= false;
12660 bool duplicate_access_error_issued_p
= false;
12661 bool class_scope_p
, template_p
;
12662 tree access
= access_default_node
;
12665 /* Process the optional `virtual' and `access-specifier'. */
12668 /* Peek at the next token. */
12669 token
= cp_lexer_peek_token (parser
->lexer
);
12670 /* Process `virtual'. */
12671 switch (token
->keyword
)
12674 /* If `virtual' appears more than once, issue an error. */
12675 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
12677 cp_parser_error (parser
,
12678 "`virtual' specified more than once in base-specified");
12679 duplicate_virtual_error_issued_p
= true;
12684 /* Consume the `virtual' token. */
12685 cp_lexer_consume_token (parser
->lexer
);
12690 case RID_PROTECTED
:
12692 /* If more than one access specifier appears, issue an
12694 if (access
!= access_default_node
12695 && !duplicate_access_error_issued_p
)
12697 cp_parser_error (parser
,
12698 "more than one access specifier in base-specified");
12699 duplicate_access_error_issued_p
= true;
12702 access
= ridpointers
[(int) token
->keyword
];
12704 /* Consume the access-specifier. */
12705 cp_lexer_consume_token (parser
->lexer
);
12714 /* It is not uncommon to see programs mechanically, errouneously, use
12715 the 'typename' keyword to denote (dependent) qualified types
12716 as base classes. */
12717 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12719 if (!processing_template_decl
)
12720 error ("keyword `typename' not allowed outside of templates");
12722 error ("keyword `typename' not allowed in this context "
12723 "(the base class is implicitly a type)");
12724 cp_lexer_consume_token (parser
->lexer
);
12727 /* Look for the optional `::' operator. */
12728 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
12729 /* Look for the nested-name-specifier. The simplest way to
12734 The keyword `typename' is not permitted in a base-specifier or
12735 mem-initializer; in these contexts a qualified name that
12736 depends on a template-parameter is implicitly assumed to be a
12739 is to pretend that we have seen the `typename' keyword at this
12741 cp_parser_nested_name_specifier_opt (parser
,
12742 /*typename_keyword_p=*/true,
12743 /*check_dependency_p=*/true,
12745 /*is_declaration=*/true);
12746 /* If the base class is given by a qualified name, assume that names
12747 we see are type names or templates, as appropriate. */
12748 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
12749 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
12751 /* Finally, look for the class-name. */
12752 type
= cp_parser_class_name (parser
,
12756 /*check_dependency_p=*/true,
12757 /*class_head_p=*/false,
12758 /*is_declaration=*/true);
12760 if (type
== error_mark_node
)
12761 return error_mark_node
;
12763 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
12766 /* Exception handling [gram.exception] */
12768 /* Parse an (optional) exception-specification.
12770 exception-specification:
12771 throw ( type-id-list [opt] )
12773 Returns a TREE_LIST representing the exception-specification. The
12774 TREE_VALUE of each node is a type. */
12777 cp_parser_exception_specification_opt (cp_parser
* parser
)
12782 /* Peek at the next token. */
12783 token
= cp_lexer_peek_token (parser
->lexer
);
12784 /* If it's not `throw', then there's no exception-specification. */
12785 if (!cp_parser_is_keyword (token
, RID_THROW
))
12788 /* Consume the `throw'. */
12789 cp_lexer_consume_token (parser
->lexer
);
12791 /* Look for the `('. */
12792 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12794 /* Peek at the next token. */
12795 token
= cp_lexer_peek_token (parser
->lexer
);
12796 /* If it's not a `)', then there is a type-id-list. */
12797 if (token
->type
!= CPP_CLOSE_PAREN
)
12799 const char *saved_message
;
12801 /* Types may not be defined in an exception-specification. */
12802 saved_message
= parser
->type_definition_forbidden_message
;
12803 parser
->type_definition_forbidden_message
12804 = "types may not be defined in an exception-specification";
12805 /* Parse the type-id-list. */
12806 type_id_list
= cp_parser_type_id_list (parser
);
12807 /* Restore the saved message. */
12808 parser
->type_definition_forbidden_message
= saved_message
;
12811 type_id_list
= empty_except_spec
;
12813 /* Look for the `)'. */
12814 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12816 return type_id_list
;
12819 /* Parse an (optional) type-id-list.
12823 type-id-list , type-id
12825 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12826 in the order that the types were presented. */
12829 cp_parser_type_id_list (cp_parser
* parser
)
12831 tree types
= NULL_TREE
;
12838 /* Get the next type-id. */
12839 type
= cp_parser_type_id (parser
);
12840 /* Add it to the list. */
12841 types
= add_exception_specifier (types
, type
, /*complain=*/1);
12842 /* Peek at the next token. */
12843 token
= cp_lexer_peek_token (parser
->lexer
);
12844 /* If it is not a `,', we are done. */
12845 if (token
->type
!= CPP_COMMA
)
12847 /* Consume the `,'. */
12848 cp_lexer_consume_token (parser
->lexer
);
12851 return nreverse (types
);
12854 /* Parse a try-block.
12857 try compound-statement handler-seq */
12860 cp_parser_try_block (cp_parser
* parser
)
12864 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
12865 try_block
= begin_try_block ();
12866 cp_parser_compound_statement (parser
, false);
12867 finish_try_block (try_block
);
12868 cp_parser_handler_seq (parser
);
12869 finish_handler_sequence (try_block
);
12874 /* Parse a function-try-block.
12876 function-try-block:
12877 try ctor-initializer [opt] function-body handler-seq */
12880 cp_parser_function_try_block (cp_parser
* parser
)
12883 bool ctor_initializer_p
;
12885 /* Look for the `try' keyword. */
12886 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
12888 /* Let the rest of the front-end know where we are. */
12889 try_block
= begin_function_try_block ();
12890 /* Parse the function-body. */
12892 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
12893 /* We're done with the `try' part. */
12894 finish_function_try_block (try_block
);
12895 /* Parse the handlers. */
12896 cp_parser_handler_seq (parser
);
12897 /* We're done with the handlers. */
12898 finish_function_handler_sequence (try_block
);
12900 return ctor_initializer_p
;
12903 /* Parse a handler-seq.
12906 handler handler-seq [opt] */
12909 cp_parser_handler_seq (cp_parser
* parser
)
12915 /* Parse the handler. */
12916 cp_parser_handler (parser
);
12917 /* Peek at the next token. */
12918 token
= cp_lexer_peek_token (parser
->lexer
);
12919 /* If it's not `catch' then there are no more handlers. */
12920 if (!cp_parser_is_keyword (token
, RID_CATCH
))
12925 /* Parse a handler.
12928 catch ( exception-declaration ) compound-statement */
12931 cp_parser_handler (cp_parser
* parser
)
12936 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
12937 handler
= begin_handler ();
12938 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12939 declaration
= cp_parser_exception_declaration (parser
);
12940 finish_handler_parms (declaration
, handler
);
12941 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12942 cp_parser_compound_statement (parser
, false);
12943 finish_handler (handler
);
12946 /* Parse an exception-declaration.
12948 exception-declaration:
12949 type-specifier-seq declarator
12950 type-specifier-seq abstract-declarator
12954 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12955 ellipsis variant is used. */
12958 cp_parser_exception_declaration (cp_parser
* parser
)
12960 tree type_specifiers
;
12962 const char *saved_message
;
12964 /* If it's an ellipsis, it's easy to handle. */
12965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12967 /* Consume the `...' token. */
12968 cp_lexer_consume_token (parser
->lexer
);
12972 /* Types may not be defined in exception-declarations. */
12973 saved_message
= parser
->type_definition_forbidden_message
;
12974 parser
->type_definition_forbidden_message
12975 = "types may not be defined in exception-declarations";
12977 /* Parse the type-specifier-seq. */
12978 type_specifiers
= cp_parser_type_specifier_seq (parser
);
12979 /* If it's a `)', then there is no declarator. */
12980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
12981 declarator
= NULL_TREE
;
12983 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
12984 /*ctor_dtor_or_conv_p=*/NULL
,
12985 /*parenthesized_p=*/NULL
);
12987 /* Restore the saved message. */
12988 parser
->type_definition_forbidden_message
= saved_message
;
12990 return start_handler_parms (type_specifiers
, declarator
);
12993 /* Parse a throw-expression.
12996 throw assignment-expression [opt]
12998 Returns a THROW_EXPR representing the throw-expression. */
13001 cp_parser_throw_expression (cp_parser
* parser
)
13006 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
13007 token
= cp_lexer_peek_token (parser
->lexer
);
13008 /* Figure out whether or not there is an assignment-expression
13009 following the "throw" keyword. */
13010 if (token
->type
== CPP_COMMA
13011 || token
->type
== CPP_SEMICOLON
13012 || token
->type
== CPP_CLOSE_PAREN
13013 || token
->type
== CPP_CLOSE_SQUARE
13014 || token
->type
== CPP_CLOSE_BRACE
13015 || token
->type
== CPP_COLON
)
13016 expression
= NULL_TREE
;
13018 expression
= cp_parser_assignment_expression (parser
);
13020 return build_throw (expression
);
13023 /* GNU Extensions */
13025 /* Parse an (optional) asm-specification.
13028 asm ( string-literal )
13030 If the asm-specification is present, returns a STRING_CST
13031 corresponding to the string-literal. Otherwise, returns
13035 cp_parser_asm_specification_opt (cp_parser
* parser
)
13038 tree asm_specification
;
13040 /* Peek at the next token. */
13041 token
= cp_lexer_peek_token (parser
->lexer
);
13042 /* If the next token isn't the `asm' keyword, then there's no
13043 asm-specification. */
13044 if (!cp_parser_is_keyword (token
, RID_ASM
))
13047 /* Consume the `asm' token. */
13048 cp_lexer_consume_token (parser
->lexer
);
13049 /* Look for the `('. */
13050 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13052 /* Look for the string-literal. */
13053 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13055 asm_specification
= token
->value
;
13057 asm_specification
= NULL_TREE
;
13059 /* Look for the `)'. */
13060 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13062 return asm_specification
;
13065 /* Parse an asm-operand-list.
13069 asm-operand-list , asm-operand
13072 string-literal ( expression )
13073 [ string-literal ] string-literal ( expression )
13075 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13076 each node is the expression. The TREE_PURPOSE is itself a
13077 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13078 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13079 is a STRING_CST for the string literal before the parenthesis. */
13082 cp_parser_asm_operand_list (cp_parser
* parser
)
13084 tree asm_operands
= NULL_TREE
;
13088 tree string_literal
;
13093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13095 /* Consume the `[' token. */
13096 cp_lexer_consume_token (parser
->lexer
);
13097 /* Read the operand name. */
13098 name
= cp_parser_identifier (parser
);
13099 if (name
!= error_mark_node
)
13100 name
= build_string (IDENTIFIER_LENGTH (name
),
13101 IDENTIFIER_POINTER (name
));
13102 /* Look for the closing `]'. */
13103 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13107 /* Look for the string-literal. */
13108 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13109 string_literal
= token
? token
->value
: error_mark_node
;
13110 /* Look for the `('. */
13111 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13112 /* Parse the expression. */
13113 expression
= cp_parser_expression (parser
);
13114 /* Look for the `)'. */
13115 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13116 /* Add this operand to the list. */
13117 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
13120 /* If the next token is not a `,', there are no more
13122 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13124 /* Consume the `,'. */
13125 cp_lexer_consume_token (parser
->lexer
);
13128 return nreverse (asm_operands
);
13131 /* Parse an asm-clobber-list.
13135 asm-clobber-list , string-literal
13137 Returns a TREE_LIST, indicating the clobbers in the order that they
13138 appeared. The TREE_VALUE of each node is a STRING_CST. */
13141 cp_parser_asm_clobber_list (cp_parser
* parser
)
13143 tree clobbers
= NULL_TREE
;
13148 tree string_literal
;
13150 /* Look for the string literal. */
13151 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13152 string_literal
= token
? token
->value
: error_mark_node
;
13153 /* Add it to the list. */
13154 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13155 /* If the next token is not a `,', then the list is
13157 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13159 /* Consume the `,' token. */
13160 cp_lexer_consume_token (parser
->lexer
);
13166 /* Parse an (optional) series of attributes.
13169 attributes attribute
13172 __attribute__ (( attribute-list [opt] ))
13174 The return value is as for cp_parser_attribute_list. */
13177 cp_parser_attributes_opt (cp_parser
* parser
)
13179 tree attributes
= NULL_TREE
;
13184 tree attribute_list
;
13186 /* Peek at the next token. */
13187 token
= cp_lexer_peek_token (parser
->lexer
);
13188 /* If it's not `__attribute__', then we're done. */
13189 if (token
->keyword
!= RID_ATTRIBUTE
)
13192 /* Consume the `__attribute__' keyword. */
13193 cp_lexer_consume_token (parser
->lexer
);
13194 /* Look for the two `(' tokens. */
13195 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13196 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13198 /* Peek at the next token. */
13199 token
= cp_lexer_peek_token (parser
->lexer
);
13200 if (token
->type
!= CPP_CLOSE_PAREN
)
13201 /* Parse the attribute-list. */
13202 attribute_list
= cp_parser_attribute_list (parser
);
13204 /* If the next token is a `)', then there is no attribute
13206 attribute_list
= NULL
;
13208 /* Look for the two `)' tokens. */
13209 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13210 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13212 /* Add these new attributes to the list. */
13213 attributes
= chainon (attributes
, attribute_list
);
13219 /* Parse an attribute-list.
13223 attribute-list , attribute
13227 identifier ( identifier )
13228 identifier ( identifier , expression-list )
13229 identifier ( expression-list )
13231 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13232 TREE_PURPOSE of each node is the identifier indicating which
13233 attribute is in use. The TREE_VALUE represents the arguments, if
13237 cp_parser_attribute_list (cp_parser
* parser
)
13239 tree attribute_list
= NULL_TREE
;
13247 /* Look for the identifier. We also allow keywords here; for
13248 example `__attribute__ ((const))' is legal. */
13249 token
= cp_lexer_peek_token (parser
->lexer
);
13250 if (token
->type
!= CPP_NAME
13251 && token
->type
!= CPP_KEYWORD
)
13252 return error_mark_node
;
13253 /* Consume the token. */
13254 token
= cp_lexer_consume_token (parser
->lexer
);
13256 /* Save away the identifier that indicates which attribute this is. */
13257 identifier
= token
->value
;
13258 attribute
= build_tree_list (identifier
, NULL_TREE
);
13260 /* Peek at the next token. */
13261 token
= cp_lexer_peek_token (parser
->lexer
);
13262 /* If it's an `(', then parse the attribute arguments. */
13263 if (token
->type
== CPP_OPEN_PAREN
)
13267 arguments
= (cp_parser_parenthesized_expression_list
13268 (parser
, true, /*non_constant_p=*/NULL
));
13269 /* Save the identifier and arguments away. */
13270 TREE_VALUE (attribute
) = arguments
;
13273 /* Add this attribute to the list. */
13274 TREE_CHAIN (attribute
) = attribute_list
;
13275 attribute_list
= attribute
;
13277 /* Now, look for more attributes. */
13278 token
= cp_lexer_peek_token (parser
->lexer
);
13279 /* If the next token isn't a `,', we're done. */
13280 if (token
->type
!= CPP_COMMA
)
13283 /* Consume the comma and keep going. */
13284 cp_lexer_consume_token (parser
->lexer
);
13287 /* We built up the list in reverse order. */
13288 return nreverse (attribute_list
);
13291 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13292 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13293 current value of the PEDANTIC flag, regardless of whether or not
13294 the `__extension__' keyword is present. The caller is responsible
13295 for restoring the value of the PEDANTIC flag. */
13298 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
13300 /* Save the old value of the PEDANTIC flag. */
13301 *saved_pedantic
= pedantic
;
13303 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
13305 /* Consume the `__extension__' token. */
13306 cp_lexer_consume_token (parser
->lexer
);
13307 /* We're not being pedantic while the `__extension__' keyword is
13317 /* Parse a label declaration.
13320 __label__ label-declarator-seq ;
13322 label-declarator-seq:
13323 identifier , label-declarator-seq
13327 cp_parser_label_declaration (cp_parser
* parser
)
13329 /* Look for the `__label__' keyword. */
13330 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
13336 /* Look for an identifier. */
13337 identifier
= cp_parser_identifier (parser
);
13338 /* Declare it as a lobel. */
13339 finish_label_decl (identifier
);
13340 /* If the next token is a `;', stop. */
13341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13343 /* Look for the `,' separating the label declarations. */
13344 cp_parser_require (parser
, CPP_COMMA
, "`,'");
13347 /* Look for the final `;'. */
13348 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13351 /* Support Functions */
13353 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13354 NAME should have one of the representations used for an
13355 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13356 is returned. If PARSER->SCOPE is a dependent type, then a
13357 SCOPE_REF is returned.
13359 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13360 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13361 was formed. Abstractly, such entities should not be passed to this
13362 function, because they do not need to be looked up, but it is
13363 simpler to check for this special case here, rather than at the
13366 In cases not explicitly covered above, this function returns a
13367 DECL, OVERLOAD, or baselink representing the result of the lookup.
13368 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13371 If IS_TYPE is TRUE, bindings that do not refer to types are
13374 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13377 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13380 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13384 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
13385 bool is_type
, bool is_template
, bool is_namespace
,
13386 bool check_dependency
)
13389 tree object_type
= parser
->context
->object_type
;
13391 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13392 no longer valid. Note that if we are parsing tentatively, and
13393 the parse fails, OBJECT_TYPE will be automatically restored. */
13394 parser
->context
->object_type
= NULL_TREE
;
13396 if (name
== error_mark_node
)
13397 return error_mark_node
;
13399 /* A template-id has already been resolved; there is no lookup to
13401 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13403 if (BASELINK_P (name
))
13405 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
13406 == TEMPLATE_ID_EXPR
),
13411 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13412 it should already have been checked to make sure that the name
13413 used matches the type being destroyed. */
13414 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13418 /* Figure out to which type this destructor applies. */
13420 type
= parser
->scope
;
13421 else if (object_type
)
13422 type
= object_type
;
13424 type
= current_class_type
;
13425 /* If that's not a class type, there is no destructor. */
13426 if (!type
|| !CLASS_TYPE_P (type
))
13427 return error_mark_node
;
13428 if (!CLASSTYPE_DESTRUCTORS (type
))
13429 return error_mark_node
;
13430 /* If it was a class type, return the destructor. */
13431 return CLASSTYPE_DESTRUCTORS (type
);
13434 /* By this point, the NAME should be an ordinary identifier. If
13435 the id-expression was a qualified name, the qualifying scope is
13436 stored in PARSER->SCOPE at this point. */
13437 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
13440 /* Perform the lookup. */
13445 if (parser
->scope
== error_mark_node
)
13446 return error_mark_node
;
13448 /* If the SCOPE is dependent, the lookup must be deferred until
13449 the template is instantiated -- unless we are explicitly
13450 looking up names in uninstantiated templates. Even then, we
13451 cannot look up the name if the scope is not a class type; it
13452 might, for example, be a template type parameter. */
13453 dependent_p
= (TYPE_P (parser
->scope
)
13454 && !(parser
->in_declarator_p
13455 && currently_open_class (parser
->scope
))
13456 && dependent_type_p (parser
->scope
));
13457 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
13461 /* The resolution to Core Issue 180 says that `struct A::B'
13462 should be considered a type-name, even if `A' is
13464 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
13467 else if (is_template
)
13468 decl
= make_unbound_class_template (parser
->scope
,
13472 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
13476 /* If PARSER->SCOPE is a dependent type, then it must be a
13477 class type, and we must not be checking dependencies;
13478 otherwise, we would have processed this lookup above. So
13479 that PARSER->SCOPE is not considered a dependent base by
13480 lookup_member, we must enter the scope here. */
13482 push_scope (parser
->scope
);
13483 /* If the PARSER->SCOPE is a a template specialization, it
13484 may be instantiated during name lookup. In that case,
13485 errors may be issued. Even if we rollback the current
13486 tentative parse, those errors are valid. */
13487 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
13488 /*complain=*/true);
13490 pop_scope (parser
->scope
);
13492 parser
->qualifying_scope
= parser
->scope
;
13493 parser
->object_scope
= NULL_TREE
;
13495 else if (object_type
)
13497 tree object_decl
= NULL_TREE
;
13498 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13499 OBJECT_TYPE is not a class. */
13500 if (CLASS_TYPE_P (object_type
))
13501 /* If the OBJECT_TYPE is a template specialization, it may
13502 be instantiated during name lookup. In that case, errors
13503 may be issued. Even if we rollback the current tentative
13504 parse, those errors are valid. */
13505 object_decl
= lookup_member (object_type
,
13507 /*protect=*/0, is_type
);
13508 /* Look it up in the enclosing context, too. */
13509 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13512 parser
->object_scope
= object_type
;
13513 parser
->qualifying_scope
= NULL_TREE
;
13515 decl
= object_decl
;
13519 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13522 parser
->qualifying_scope
= NULL_TREE
;
13523 parser
->object_scope
= NULL_TREE
;
13526 /* If the lookup failed, let our caller know. */
13528 || decl
== error_mark_node
13529 || (TREE_CODE (decl
) == FUNCTION_DECL
13530 && DECL_ANTICIPATED (decl
)))
13531 return error_mark_node
;
13533 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13534 if (TREE_CODE (decl
) == TREE_LIST
)
13536 /* The error message we have to print is too complicated for
13537 cp_parser_error, so we incorporate its actions directly. */
13538 if (!cp_parser_simulate_error (parser
))
13540 error ("reference to `%D' is ambiguous", name
);
13541 print_candidates (decl
);
13543 return error_mark_node
;
13546 my_friendly_assert (DECL_P (decl
)
13547 || TREE_CODE (decl
) == OVERLOAD
13548 || TREE_CODE (decl
) == SCOPE_REF
13549 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
13550 || BASELINK_P (decl
),
13553 /* If we have resolved the name of a member declaration, check to
13554 see if the declaration is accessible. When the name resolves to
13555 set of overloaded functions, accessibility is checked when
13556 overload resolution is done.
13558 During an explicit instantiation, access is not checked at all,
13559 as per [temp.explicit]. */
13561 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
13566 /* Like cp_parser_lookup_name, but for use in the typical case where
13567 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13568 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13571 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13573 return cp_parser_lookup_name (parser
, name
,
13575 /*is_template=*/false,
13576 /*is_namespace=*/false,
13577 /*check_dependency=*/true);
13580 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13581 the current context, return the TYPE_DECL. If TAG_NAME_P is
13582 true, the DECL indicates the class being defined in a class-head,
13583 or declared in an elaborated-type-specifier.
13585 Otherwise, return DECL. */
13588 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13590 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13591 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13594 template <typename T> struct B;
13597 template <typename T> struct A::B {};
13599 Similarly, in a elaborated-type-specifier:
13601 namespace N { struct X{}; }
13604 template <typename T> friend struct N::X;
13607 However, if the DECL refers to a class type, and we are in
13608 the scope of the class, then the name lookup automatically
13609 finds the TYPE_DECL created by build_self_reference rather
13610 than a TEMPLATE_DECL. For example, in:
13612 template <class T> struct S {
13616 there is no need to handle such case. */
13618 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13619 return DECL_TEMPLATE_RESULT (decl
);
13624 /* If too many, or too few, template-parameter lists apply to the
13625 declarator, issue an error message. Returns TRUE if all went well,
13626 and FALSE otherwise. */
13629 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13632 unsigned num_templates
;
13634 /* We haven't seen any classes that involve template parameters yet. */
13637 switch (TREE_CODE (declarator
))
13644 tree main_declarator
= TREE_OPERAND (declarator
, 0);
13646 cp_parser_check_declarator_template_parameters (parser
,
13655 scope
= TREE_OPERAND (declarator
, 0);
13656 member
= TREE_OPERAND (declarator
, 1);
13658 /* If this is a pointer-to-member, then we are not interested
13659 in the SCOPE, because it does not qualify the thing that is
13661 if (TREE_CODE (member
) == INDIRECT_REF
)
13662 return (cp_parser_check_declarator_template_parameters
13665 while (scope
&& CLASS_TYPE_P (scope
))
13667 /* You're supposed to have one `template <...>'
13668 for every template class, but you don't need one
13669 for a full specialization. For example:
13671 template <class T> struct S{};
13672 template <> struct S<int> { void f(); };
13673 void S<int>::f () {}
13675 is correct; there shouldn't be a `template <>' for
13676 the definition of `S<int>::f'. */
13677 if (CLASSTYPE_TEMPLATE_INFO (scope
)
13678 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
13679 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
13680 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
13683 scope
= TYPE_CONTEXT (scope
);
13687 /* Fall through. */
13690 /* If the DECLARATOR has the form `X<y>' then it uses one
13691 additional level of template parameters. */
13692 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
13695 return cp_parser_check_template_parameters (parser
,
13700 /* NUM_TEMPLATES were used in the current declaration. If that is
13701 invalid, return FALSE and issue an error messages. Otherwise,
13705 cp_parser_check_template_parameters (cp_parser
* parser
,
13706 unsigned num_templates
)
13708 /* If there are more template classes than parameter lists, we have
13711 template <class T> void S<T>::R<T>::f (); */
13712 if (parser
->num_template_parameter_lists
< num_templates
)
13714 error ("too few template-parameter-lists");
13717 /* If there are the same number of template classes and parameter
13718 lists, that's OK. */
13719 if (parser
->num_template_parameter_lists
== num_templates
)
13721 /* If there are more, but only one more, then we are referring to a
13722 member template. That's OK too. */
13723 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
13725 /* Otherwise, there are too many template parameter lists. We have
13728 template <class T> template <class U> void S::f(); */
13729 error ("too many template-parameter-lists");
13733 /* Parse a binary-expression of the general form:
13737 binary-expression <token> <expr>
13739 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13740 to parser the <expr>s. If the first production is used, then the
13741 value returned by FN is returned directly. Otherwise, a node with
13742 the indicated EXPR_TYPE is returned, with operands corresponding to
13743 the two sub-expressions. */
13746 cp_parser_binary_expression (cp_parser
* parser
,
13747 const cp_parser_token_tree_map token_tree_map
,
13748 cp_parser_expression_fn fn
)
13752 /* Parse the first expression. */
13753 lhs
= (*fn
) (parser
);
13754 /* Now, look for more expressions. */
13758 const cp_parser_token_tree_map_node
*map_node
;
13761 /* Peek at the next token. */
13762 token
= cp_lexer_peek_token (parser
->lexer
);
13763 /* If the token is `>', and that's not an operator at the
13764 moment, then we're done. */
13765 if (token
->type
== CPP_GREATER
13766 && !parser
->greater_than_is_operator_p
)
13768 /* If we find one of the tokens we want, build the corresponding
13769 tree representation. */
13770 for (map_node
= token_tree_map
;
13771 map_node
->token_type
!= CPP_EOF
;
13773 if (map_node
->token_type
== token
->type
)
13775 /* Consume the operator token. */
13776 cp_lexer_consume_token (parser
->lexer
);
13777 /* Parse the right-hand side of the expression. */
13778 rhs
= (*fn
) (parser
);
13779 /* Build the binary tree node. */
13780 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
);
13784 /* If the token wasn't one of the ones we want, we're done. */
13785 if (map_node
->token_type
== CPP_EOF
)
13792 /* Parse an optional `::' token indicating that the following name is
13793 from the global namespace. If so, PARSER->SCOPE is set to the
13794 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13795 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13796 Returns the new value of PARSER->SCOPE, if the `::' token is
13797 present, and NULL_TREE otherwise. */
13800 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
13804 /* Peek at the next token. */
13805 token
= cp_lexer_peek_token (parser
->lexer
);
13806 /* If we're looking at a `::' token then we're starting from the
13807 global namespace, not our current location. */
13808 if (token
->type
== CPP_SCOPE
)
13810 /* Consume the `::' token. */
13811 cp_lexer_consume_token (parser
->lexer
);
13812 /* Set the SCOPE so that we know where to start the lookup. */
13813 parser
->scope
= global_namespace
;
13814 parser
->qualifying_scope
= global_namespace
;
13815 parser
->object_scope
= NULL_TREE
;
13817 return parser
->scope
;
13819 else if (!current_scope_valid_p
)
13821 parser
->scope
= NULL_TREE
;
13822 parser
->qualifying_scope
= NULL_TREE
;
13823 parser
->object_scope
= NULL_TREE
;
13829 /* Returns TRUE if the upcoming token sequence is the start of a
13830 constructor declarator. If FRIEND_P is true, the declarator is
13831 preceded by the `friend' specifier. */
13834 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
13836 bool constructor_p
;
13837 tree type_decl
= NULL_TREE
;
13838 bool nested_name_p
;
13839 cp_token
*next_token
;
13841 /* The common case is that this is not a constructor declarator, so
13842 try to avoid doing lots of work if at all possible. It's not
13843 valid declare a constructor at function scope. */
13844 if (at_function_scope_p ())
13846 /* And only certain tokens can begin a constructor declarator. */
13847 next_token
= cp_lexer_peek_token (parser
->lexer
);
13848 if (next_token
->type
!= CPP_NAME
13849 && next_token
->type
!= CPP_SCOPE
13850 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
13851 && next_token
->type
!= CPP_TEMPLATE_ID
)
13854 /* Parse tentatively; we are going to roll back all of the tokens
13856 cp_parser_parse_tentatively (parser
);
13857 /* Assume that we are looking at a constructor declarator. */
13858 constructor_p
= true;
13860 /* Look for the optional `::' operator. */
13861 cp_parser_global_scope_opt (parser
,
13862 /*current_scope_valid_p=*/false);
13863 /* Look for the nested-name-specifier. */
13865 = (cp_parser_nested_name_specifier_opt (parser
,
13866 /*typename_keyword_p=*/false,
13867 /*check_dependency_p=*/false,
13869 /*is_declaration=*/false)
13871 /* Outside of a class-specifier, there must be a
13872 nested-name-specifier. */
13873 if (!nested_name_p
&&
13874 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
13876 constructor_p
= false;
13877 /* If we still think that this might be a constructor-declarator,
13878 look for a class-name. */
13883 template <typename T> struct S { S(); };
13884 template <typename T> S<T>::S ();
13886 we must recognize that the nested `S' names a class.
13889 template <typename T> S<T>::S<T> ();
13891 we must recognize that the nested `S' names a template. */
13892 type_decl
= cp_parser_class_name (parser
,
13893 /*typename_keyword_p=*/false,
13894 /*template_keyword_p=*/false,
13896 /*check_dependency_p=*/false,
13897 /*class_head_p=*/false,
13898 /*is_declaration=*/false);
13899 /* If there was no class-name, then this is not a constructor. */
13900 constructor_p
= !cp_parser_error_occurred (parser
);
13903 /* If we're still considering a constructor, we have to see a `(',
13904 to begin the parameter-declaration-clause, followed by either a
13905 `)', an `...', or a decl-specifier. We need to check for a
13906 type-specifier to avoid being fooled into thinking that:
13910 is a constructor. (It is actually a function named `f' that
13911 takes one parameter (of type `int') and returns a value of type
13914 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
13916 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
13917 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
13918 && !cp_parser_storage_class_specifier_opt (parser
))
13921 unsigned saved_num_template_parameter_lists
;
13923 /* Names appearing in the type-specifier should be looked up
13924 in the scope of the class. */
13925 if (current_class_type
)
13929 type
= TREE_TYPE (type_decl
);
13930 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13932 type
= resolve_typename_type (type
,
13933 /*only_current_p=*/false);
13934 if (type
== error_mark_node
)
13936 cp_parser_abort_tentative_parse (parser
);
13943 /* Inside the constructor parameter list, surrounding
13944 template-parameter-lists do not apply. */
13945 saved_num_template_parameter_lists
13946 = parser
->num_template_parameter_lists
;
13947 parser
->num_template_parameter_lists
= 0;
13949 /* Look for the type-specifier. */
13950 cp_parser_type_specifier (parser
,
13951 CP_PARSER_FLAGS_NONE
,
13952 /*is_friend=*/false,
13953 /*is_declarator=*/true,
13954 /*declares_class_or_enum=*/NULL
,
13955 /*is_cv_qualifier=*/NULL
);
13957 parser
->num_template_parameter_lists
13958 = saved_num_template_parameter_lists
;
13960 /* Leave the scope of the class. */
13964 constructor_p
= !cp_parser_error_occurred (parser
);
13968 constructor_p
= false;
13969 /* We did not really want to consume any tokens. */
13970 cp_parser_abort_tentative_parse (parser
);
13972 return constructor_p
;
13975 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13976 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13977 they must be performed once we are in the scope of the function.
13979 Returns the function defined. */
13982 cp_parser_function_definition_from_specifiers_and_declarator
13983 (cp_parser
* parser
,
13984 tree decl_specifiers
,
13991 /* Begin the function-definition. */
13992 success_p
= begin_function_definition (decl_specifiers
,
13996 /* If there were names looked up in the decl-specifier-seq that we
13997 did not check, check them now. We must wait until we are in the
13998 scope of the function to perform the checks, since the function
13999 might be a friend. */
14000 perform_deferred_access_checks ();
14004 /* If begin_function_definition didn't like the definition, skip
14005 the entire function. */
14006 error ("invalid function declaration");
14007 cp_parser_skip_to_end_of_block_or_statement (parser
);
14008 fn
= error_mark_node
;
14011 fn
= cp_parser_function_definition_after_declarator (parser
,
14012 /*inline_p=*/false);
14017 /* Parse the part of a function-definition that follows the
14018 declarator. INLINE_P is TRUE iff this function is an inline
14019 function defined with a class-specifier.
14021 Returns the function defined. */
14024 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
14028 bool ctor_initializer_p
= false;
14029 bool saved_in_unbraced_linkage_specification_p
;
14030 unsigned saved_num_template_parameter_lists
;
14032 /* If the next token is `return', then the code may be trying to
14033 make use of the "named return value" extension that G++ used to
14035 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
14037 /* Consume the `return' keyword. */
14038 cp_lexer_consume_token (parser
->lexer
);
14039 /* Look for the identifier that indicates what value is to be
14041 cp_parser_identifier (parser
);
14042 /* Issue an error message. */
14043 error ("named return values are no longer supported");
14044 /* Skip tokens until we reach the start of the function body. */
14045 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
14046 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
14047 cp_lexer_consume_token (parser
->lexer
);
14049 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14050 anything declared inside `f'. */
14051 saved_in_unbraced_linkage_specification_p
14052 = parser
->in_unbraced_linkage_specification_p
;
14053 parser
->in_unbraced_linkage_specification_p
= false;
14054 /* Inside the function, surrounding template-parameter-lists do not
14056 saved_num_template_parameter_lists
14057 = parser
->num_template_parameter_lists
;
14058 parser
->num_template_parameter_lists
= 0;
14059 /* If the next token is `try', then we are looking at a
14060 function-try-block. */
14061 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14062 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14063 /* A function-try-block includes the function-body, so we only do
14064 this next part if we're not processing a function-try-block. */
14067 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14069 /* Finish the function. */
14070 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14071 (inline_p
? 2 : 0));
14072 /* Generate code for it, if necessary. */
14073 expand_or_defer_fn (fn
);
14074 /* Restore the saved values. */
14075 parser
->in_unbraced_linkage_specification_p
14076 = saved_in_unbraced_linkage_specification_p
;
14077 parser
->num_template_parameter_lists
14078 = saved_num_template_parameter_lists
;
14083 /* Parse a template-declaration, assuming that the `export' (and
14084 `extern') keywords, if present, has already been scanned. MEMBER_P
14085 is as for cp_parser_template_declaration. */
14088 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14090 tree decl
= NULL_TREE
;
14091 tree parameter_list
;
14092 bool friend_p
= false;
14094 /* Look for the `template' keyword. */
14095 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14099 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14102 /* If the next token is `>', then we have an invalid
14103 specialization. Rather than complain about an invalid template
14104 parameter, issue an error message here. */
14105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14107 cp_parser_error (parser
, "invalid explicit specialization");
14108 begin_specialization ();
14109 parameter_list
= NULL_TREE
;
14113 /* Parse the template parameters. */
14114 begin_template_parm_list ();
14115 parameter_list
= cp_parser_template_parameter_list (parser
);
14116 parameter_list
= end_template_parm_list (parameter_list
);
14119 /* Look for the `>'. */
14120 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14121 /* We just processed one more parameter list. */
14122 ++parser
->num_template_parameter_lists
;
14123 /* If the next token is `template', there are more template
14125 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14127 cp_parser_template_declaration_after_export (parser
, member_p
);
14130 decl
= cp_parser_single_declaration (parser
,
14134 /* If this is a member template declaration, let the front
14136 if (member_p
&& !friend_p
&& decl
)
14138 if (TREE_CODE (decl
) == TYPE_DECL
)
14139 cp_parser_check_access_in_redeclaration (decl
);
14141 decl
= finish_member_template_decl (decl
);
14143 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14144 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14145 /*complain=*/true);
14147 /* We are done with the current parameter list. */
14148 --parser
->num_template_parameter_lists
;
14151 finish_template_decl (parameter_list
);
14153 /* Register member declarations. */
14154 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14155 finish_member_declaration (decl
);
14157 /* If DECL is a function template, we must return to parse it later.
14158 (Even though there is no definition, there might be default
14159 arguments that need handling.) */
14160 if (member_p
&& decl
14161 && (TREE_CODE (decl
) == FUNCTION_DECL
14162 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14163 TREE_VALUE (parser
->unparsed_functions_queues
)
14164 = tree_cons (NULL_TREE
, decl
,
14165 TREE_VALUE (parser
->unparsed_functions_queues
));
14168 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14169 `function-definition' sequence. MEMBER_P is true, this declaration
14170 appears in a class scope.
14172 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14173 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14176 cp_parser_single_declaration (cp_parser
* parser
,
14180 int declares_class_or_enum
;
14181 tree decl
= NULL_TREE
;
14182 tree decl_specifiers
;
14184 bool function_definition_p
= false;
14186 /* Defer access checks until we know what is being declared. */
14187 push_deferring_access_checks (dk_deferred
);
14189 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14192 = cp_parser_decl_specifier_seq (parser
,
14193 CP_PARSER_FLAGS_OPTIONAL
,
14195 &declares_class_or_enum
);
14197 *friend_p
= cp_parser_friend_p (decl_specifiers
);
14198 /* Gather up the access checks that occurred the
14199 decl-specifier-seq. */
14200 stop_deferring_access_checks ();
14202 /* Check for the declaration of a template class. */
14203 if (declares_class_or_enum
)
14205 if (cp_parser_declares_only_class_p (parser
))
14207 decl
= shadow_tag (decl_specifiers
);
14209 decl
= TYPE_NAME (decl
);
14211 decl
= error_mark_node
;
14216 /* If it's not a template class, try for a template function. If
14217 the next token is a `;', then this declaration does not declare
14218 anything. But, if there were errors in the decl-specifiers, then
14219 the error might well have come from an attempted class-specifier.
14220 In that case, there's no need to warn about a missing declarator. */
14222 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
14223 || !value_member (error_mark_node
, decl_specifiers
)))
14224 decl
= cp_parser_init_declarator (parser
,
14227 /*function_definition_allowed_p=*/true,
14229 declares_class_or_enum
,
14230 &function_definition_p
);
14232 pop_deferring_access_checks ();
14234 /* Clear any current qualification; whatever comes next is the start
14235 of something new. */
14236 parser
->scope
= NULL_TREE
;
14237 parser
->qualifying_scope
= NULL_TREE
;
14238 parser
->object_scope
= NULL_TREE
;
14239 /* Look for a trailing `;' after the declaration. */
14240 if (!function_definition_p
14241 && !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
14242 cp_parser_skip_to_end_of_block_or_statement (parser
);
14247 /* Parse a cast-expression that is not the operand of a unary "&". */
14250 cp_parser_simple_cast_expression (cp_parser
*parser
)
14252 return cp_parser_cast_expression (parser
, /*address_p=*/false);
14255 /* Parse a functional cast to TYPE. Returns an expression
14256 representing the cast. */
14259 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14261 tree expression_list
;
14264 = cp_parser_parenthesized_expression_list (parser
, false,
14265 /*non_constant_p=*/NULL
);
14267 return build_functional_cast (type
, expression_list
);
14270 /* Save the tokens that make up the body of a member function defined
14271 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14272 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14273 specifiers applied to the declaration. Returns the FUNCTION_DECL
14274 for the member function. */
14277 cp_parser_save_member_function_body (cp_parser
* parser
,
14278 tree decl_specifiers
,
14282 cp_token_cache
*cache
;
14285 /* Create the function-declaration. */
14286 fn
= start_method (decl_specifiers
, declarator
, attributes
);
14287 /* If something went badly wrong, bail out now. */
14288 if (fn
== error_mark_node
)
14290 /* If there's a function-body, skip it. */
14291 if (cp_parser_token_starts_function_definition_p
14292 (cp_lexer_peek_token (parser
->lexer
)))
14293 cp_parser_skip_to_end_of_block_or_statement (parser
);
14294 return error_mark_node
;
14297 /* Remember it, if there default args to post process. */
14298 cp_parser_save_default_args (parser
, fn
);
14300 /* Create a token cache. */
14301 cache
= cp_token_cache_new ();
14302 /* Save away the tokens that make up the body of the
14304 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14305 /* Handle function try blocks. */
14306 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
14307 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14309 /* Save away the inline definition; we will process it when the
14310 class is complete. */
14311 DECL_PENDING_INLINE_INFO (fn
) = cache
;
14312 DECL_PENDING_INLINE_P (fn
) = 1;
14314 /* We need to know that this was defined in the class, so that
14315 friend templates are handled correctly. */
14316 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
14318 /* We're done with the inline definition. */
14319 finish_method (fn
);
14321 /* Add FN to the queue of functions to be parsed later. */
14322 TREE_VALUE (parser
->unparsed_functions_queues
)
14323 = tree_cons (NULL_TREE
, fn
,
14324 TREE_VALUE (parser
->unparsed_functions_queues
));
14329 /* Parse a template-argument-list, as well as the trailing ">" (but
14330 not the opening ">"). See cp_parser_template_argument_list for the
14334 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
14338 tree saved_qualifying_scope
;
14339 tree saved_object_scope
;
14340 bool saved_greater_than_is_operator_p
;
14344 When parsing a template-id, the first non-nested `>' is taken as
14345 the end of the template-argument-list rather than a greater-than
14347 saved_greater_than_is_operator_p
14348 = parser
->greater_than_is_operator_p
;
14349 parser
->greater_than_is_operator_p
= false;
14350 /* Parsing the argument list may modify SCOPE, so we save it
14352 saved_scope
= parser
->scope
;
14353 saved_qualifying_scope
= parser
->qualifying_scope
;
14354 saved_object_scope
= parser
->object_scope
;
14355 /* Parse the template-argument-list itself. */
14356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14357 arguments
= NULL_TREE
;
14359 arguments
= cp_parser_template_argument_list (parser
);
14360 /* Look for the `>' that ends the template-argument-list. If we find
14361 a '>>' instead, it's probably just a typo. */
14362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14364 if (!saved_greater_than_is_operator_p
)
14366 /* If we're in a nested template argument list, the '>>' has to be
14367 a typo for '> >'. We emit the error message, but we continue
14368 parsing and we push a '>' as next token, so that the argument
14369 list will be parsed correctly.. */
14371 error ("`>>' should be `> >' within a nested template argument list");
14372 token
= cp_lexer_peek_token (parser
->lexer
);
14373 token
->type
= CPP_GREATER
;
14377 /* If this is not a nested template argument list, the '>>' is
14378 a typo for '>'. Emit an error message and continue. */
14379 error ("spurious `>>', use `>' to terminate a template argument list");
14380 cp_lexer_consume_token (parser
->lexer
);
14384 cp_parser_require (parser
, CPP_GREATER
, "`>'");
14385 /* The `>' token might be a greater-than operator again now. */
14386 parser
->greater_than_is_operator_p
14387 = saved_greater_than_is_operator_p
;
14388 /* Restore the SAVED_SCOPE. */
14389 parser
->scope
= saved_scope
;
14390 parser
->qualifying_scope
= saved_qualifying_scope
;
14391 parser
->object_scope
= saved_object_scope
;
14396 /* MEMBER_FUNCTION is a member function, or a friend. If default
14397 arguments, or the body of the function have not yet been parsed,
14401 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
14403 cp_lexer
*saved_lexer
;
14405 /* If this member is a template, get the underlying
14407 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
14408 member_function
= DECL_TEMPLATE_RESULT (member_function
);
14410 /* There should not be any class definitions in progress at this
14411 point; the bodies of members are only parsed outside of all class
14413 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
14414 /* While we're parsing the member functions we might encounter more
14415 classes. We want to handle them right away, but we don't want
14416 them getting mixed up with functions that are currently in the
14418 parser
->unparsed_functions_queues
14419 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14421 /* Make sure that any template parameters are in scope. */
14422 maybe_begin_member_template_processing (member_function
);
14424 /* If the body of the function has not yet been parsed, parse it
14426 if (DECL_PENDING_INLINE_P (member_function
))
14428 tree function_scope
;
14429 cp_token_cache
*tokens
;
14431 /* The function is no longer pending; we are processing it. */
14432 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
14433 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
14434 DECL_PENDING_INLINE_P (member_function
) = 0;
14435 /* If this was an inline function in a local class, enter the scope
14436 of the containing function. */
14437 function_scope
= decl_function_context (member_function
);
14438 if (function_scope
)
14439 push_function_context_to (function_scope
);
14441 /* Save away the current lexer. */
14442 saved_lexer
= parser
->lexer
;
14443 /* Make a new lexer to feed us the tokens saved for this function. */
14444 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14445 parser
->lexer
->next
= saved_lexer
;
14447 /* Set the current source position to be the location of the first
14448 token in the saved inline body. */
14449 cp_lexer_peek_token (parser
->lexer
);
14451 /* Let the front end know that we going to be defining this
14453 start_function (NULL_TREE
, member_function
, NULL_TREE
,
14454 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
14456 /* Now, parse the body of the function. */
14457 cp_parser_function_definition_after_declarator (parser
,
14458 /*inline_p=*/true);
14460 /* Leave the scope of the containing function. */
14461 if (function_scope
)
14462 pop_function_context_from (function_scope
);
14463 /* Restore the lexer. */
14464 parser
->lexer
= saved_lexer
;
14467 /* Remove any template parameters from the symbol table. */
14468 maybe_end_member_template_processing ();
14470 /* Restore the queue. */
14471 parser
->unparsed_functions_queues
14472 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14475 /* If DECL contains any default args, remember it on the unparsed
14476 functions queue. */
14479 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
14483 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14485 probe
= TREE_CHAIN (probe
))
14486 if (TREE_PURPOSE (probe
))
14488 TREE_PURPOSE (parser
->unparsed_functions_queues
)
14489 = tree_cons (NULL_TREE
, decl
,
14490 TREE_PURPOSE (parser
->unparsed_functions_queues
));
14496 /* FN is a FUNCTION_DECL which may contains a parameter with an
14497 unparsed DEFAULT_ARG. Parse the default args now. */
14500 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
14502 cp_lexer
*saved_lexer
;
14503 cp_token_cache
*tokens
;
14504 bool saved_local_variables_forbidden_p
;
14507 /* While we're parsing the default args, we might (due to the
14508 statement expression extension) encounter more classes. We want
14509 to handle them right away, but we don't want them getting mixed
14510 up with default args that are currently in the queue. */
14511 parser
->unparsed_functions_queues
14512 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14514 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14516 parameters
= TREE_CHAIN (parameters
))
14518 if (!TREE_PURPOSE (parameters
)
14519 || TREE_CODE (TREE_PURPOSE (parameters
)) != DEFAULT_ARG
)
14522 /* Save away the current lexer. */
14523 saved_lexer
= parser
->lexer
;
14524 /* Create a new one, using the tokens we have saved. */
14525 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parameters
));
14526 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14528 /* Set the current source position to be the location of the
14529 first token in the default argument. */
14530 cp_lexer_peek_token (parser
->lexer
);
14532 /* Local variable names (and the `this' keyword) may not appear
14533 in a default argument. */
14534 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14535 parser
->local_variables_forbidden_p
= true;
14536 /* Parse the assignment-expression. */
14537 if (DECL_CLASS_SCOPE_P (fn
))
14538 push_nested_class (DECL_CONTEXT (fn
));
14539 TREE_PURPOSE (parameters
) = cp_parser_assignment_expression (parser
);
14540 if (DECL_CLASS_SCOPE_P (fn
))
14541 pop_nested_class ();
14543 /* Restore saved state. */
14544 parser
->lexer
= saved_lexer
;
14545 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14548 /* Restore the queue. */
14549 parser
->unparsed_functions_queues
14550 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14553 /* Parse the operand of `sizeof' (or a similar operator). Returns
14554 either a TYPE or an expression, depending on the form of the
14555 input. The KEYWORD indicates which kind of expression we have
14559 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
14561 static const char *format
;
14562 tree expr
= NULL_TREE
;
14563 const char *saved_message
;
14564 bool saved_integral_constant_expression_p
;
14566 /* Initialize FORMAT the first time we get here. */
14568 format
= "types may not be defined in `%s' expressions";
14570 /* Types cannot be defined in a `sizeof' expression. Save away the
14572 saved_message
= parser
->type_definition_forbidden_message
;
14573 /* And create the new one. */
14574 parser
->type_definition_forbidden_message
14575 = xmalloc (strlen (format
)
14576 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
14578 sprintf ((char *) parser
->type_definition_forbidden_message
,
14579 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
14581 /* The restrictions on constant-expressions do not apply inside
14582 sizeof expressions. */
14583 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
14584 parser
->integral_constant_expression_p
= false;
14586 /* Do not actually evaluate the expression. */
14588 /* If it's a `(', then we might be looking at the type-id
14590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14593 bool saved_in_type_id_in_expr_p
;
14595 /* We can't be sure yet whether we're looking at a type-id or an
14597 cp_parser_parse_tentatively (parser
);
14598 /* Consume the `('. */
14599 cp_lexer_consume_token (parser
->lexer
);
14600 /* Parse the type-id. */
14601 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14602 parser
->in_type_id_in_expr_p
= true;
14603 type
= cp_parser_type_id (parser
);
14604 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14605 /* Now, look for the trailing `)'. */
14606 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14607 /* If all went well, then we're done. */
14608 if (cp_parser_parse_definitely (parser
))
14610 /* Build a list of decl-specifiers; right now, we have only
14611 a single type-specifier. */
14612 type
= build_tree_list (NULL_TREE
,
14615 /* Call grokdeclarator to figure out what type this is. */
14616 expr
= grokdeclarator (NULL_TREE
,
14620 /*attrlist=*/NULL
);
14624 /* If the type-id production did not work out, then we must be
14625 looking at the unary-expression production. */
14627 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
14628 /* Go back to evaluating expressions. */
14631 /* Free the message we created. */
14632 free ((char *) parser
->type_definition_forbidden_message
);
14633 /* And restore the old one. */
14634 parser
->type_definition_forbidden_message
= saved_message
;
14635 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
14640 /* If the current declaration has no declarator, return true. */
14643 cp_parser_declares_only_class_p (cp_parser
*parser
)
14645 /* If the next token is a `;' or a `,' then there is no
14647 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14648 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14651 /* Simplify EXPR if it is a non-dependent expression. Returns the
14652 (possibly simplified) expression. */
14655 cp_parser_fold_non_dependent_expr (tree expr
)
14657 /* If we're in a template, but EXPR isn't value dependent, simplify
14658 it. We're supposed to treat:
14660 template <typename T> void f(T[1 + 1]);
14661 template <typename T> void f(T[2]);
14663 as two declarations of the same function, for example. */
14664 if (processing_template_decl
14665 && !type_dependent_expression_p (expr
)
14666 && !value_dependent_expression_p (expr
))
14668 HOST_WIDE_INT saved_processing_template_decl
;
14670 saved_processing_template_decl
= processing_template_decl
;
14671 processing_template_decl
= 0;
14672 expr
= tsubst_copy_and_build (expr
,
14673 /*args=*/NULL_TREE
,
14675 /*in_decl=*/NULL_TREE
,
14676 /*function_p=*/false);
14677 processing_template_decl
= saved_processing_template_decl
;
14682 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14683 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14686 cp_parser_friend_p (tree decl_specifiers
)
14688 while (decl_specifiers
)
14690 /* See if this decl-specifier is `friend'. */
14691 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
14692 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
14695 /* Go on to the next decl-specifier. */
14696 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
14702 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14703 issue an error message indicating that TOKEN_DESC was expected.
14705 Returns the token consumed, if the token had the appropriate type.
14706 Otherwise, returns NULL. */
14709 cp_parser_require (cp_parser
* parser
,
14710 enum cpp_ttype type
,
14711 const char* token_desc
)
14713 if (cp_lexer_next_token_is (parser
->lexer
, type
))
14714 return cp_lexer_consume_token (parser
->lexer
);
14717 /* Output the MESSAGE -- unless we're parsing tentatively. */
14718 if (!cp_parser_simulate_error (parser
))
14720 char *message
= concat ("expected ", token_desc
, NULL
);
14721 cp_parser_error (parser
, message
);
14728 /* Like cp_parser_require, except that tokens will be skipped until
14729 the desired token is found. An error message is still produced if
14730 the next token is not as expected. */
14733 cp_parser_skip_until_found (cp_parser
* parser
,
14734 enum cpp_ttype type
,
14735 const char* token_desc
)
14738 unsigned nesting_depth
= 0;
14740 if (cp_parser_require (parser
, type
, token_desc
))
14743 /* Skip tokens until the desired token is found. */
14746 /* Peek at the next token. */
14747 token
= cp_lexer_peek_token (parser
->lexer
);
14748 /* If we've reached the token we want, consume it and
14750 if (token
->type
== type
&& !nesting_depth
)
14752 cp_lexer_consume_token (parser
->lexer
);
14755 /* If we've run out of tokens, stop. */
14756 if (token
->type
== CPP_EOF
)
14758 if (token
->type
== CPP_OPEN_BRACE
14759 || token
->type
== CPP_OPEN_PAREN
14760 || token
->type
== CPP_OPEN_SQUARE
)
14762 else if (token
->type
== CPP_CLOSE_BRACE
14763 || token
->type
== CPP_CLOSE_PAREN
14764 || token
->type
== CPP_CLOSE_SQUARE
)
14766 if (nesting_depth
-- == 0)
14769 /* Consume this token. */
14770 cp_lexer_consume_token (parser
->lexer
);
14774 /* If the next token is the indicated keyword, consume it. Otherwise,
14775 issue an error message indicating that TOKEN_DESC was expected.
14777 Returns the token consumed, if the token had the appropriate type.
14778 Otherwise, returns NULL. */
14781 cp_parser_require_keyword (cp_parser
* parser
,
14783 const char* token_desc
)
14785 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
14787 if (token
&& token
->keyword
!= keyword
)
14789 dyn_string_t error_msg
;
14791 /* Format the error message. */
14792 error_msg
= dyn_string_new (0);
14793 dyn_string_append_cstr (error_msg
, "expected ");
14794 dyn_string_append_cstr (error_msg
, token_desc
);
14795 cp_parser_error (parser
, error_msg
->s
);
14796 dyn_string_delete (error_msg
);
14803 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14804 function-definition. */
14807 cp_parser_token_starts_function_definition_p (cp_token
* token
)
14809 return (/* An ordinary function-body begins with an `{'. */
14810 token
->type
== CPP_OPEN_BRACE
14811 /* A ctor-initializer begins with a `:'. */
14812 || token
->type
== CPP_COLON
14813 /* A function-try-block begins with `try'. */
14814 || token
->keyword
== RID_TRY
14815 /* The named return value extension begins with `return'. */
14816 || token
->keyword
== RID_RETURN
);
14819 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14823 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
14827 token
= cp_lexer_peek_token (parser
->lexer
);
14828 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
14831 /* Returns TRUE iff the next token is the "," or ">" ending a
14832 template-argument. ">>" is also accepted (after the full
14833 argument was parsed) because it's probably a typo for "> >",
14834 and there is a specific diagnostic for this. */
14837 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
14841 token
= cp_lexer_peek_token (parser
->lexer
);
14842 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
14843 || token
->type
== CPP_RSHIFT
);
14846 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14847 or none_type otherwise. */
14849 static enum tag_types
14850 cp_parser_token_is_class_key (cp_token
* token
)
14852 switch (token
->keyword
)
14857 return record_type
;
14866 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14869 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
14871 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
14872 pedwarn ("`%s' tag used in naming `%#T'",
14873 class_key
== union_type
? "union"
14874 : class_key
== record_type
? "struct" : "class",
14878 /* Issue an error message if DECL is redeclared with different
14879 access than its original declaration [class.access.spec/3].
14880 This applies to nested classes and nested class templates.
14883 static void cp_parser_check_access_in_redeclaration (tree decl
)
14885 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
14888 if ((TREE_PRIVATE (decl
)
14889 != (current_access_specifier
== access_private_node
))
14890 || (TREE_PROTECTED (decl
)
14891 != (current_access_specifier
== access_protected_node
)))
14892 error ("%D redeclared with different access", decl
);
14895 /* Look for the `template' keyword, as a syntactic disambiguator.
14896 Return TRUE iff it is present, in which case it will be
14900 cp_parser_optional_template_keyword (cp_parser
*parser
)
14902 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14904 /* The `template' keyword can only be used within templates;
14905 outside templates the parser can always figure out what is a
14906 template and what is not. */
14907 if (!processing_template_decl
)
14909 error ("`template' (as a disambiguator) is only allowed "
14910 "within templates");
14911 /* If this part of the token stream is rescanned, the same
14912 error message would be generated. So, we purge the token
14913 from the stream. */
14914 cp_lexer_purge_token (parser
->lexer
);
14919 /* Consume the `template' keyword. */
14920 cp_lexer_consume_token (parser
->lexer
);
14928 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14929 set PARSER->SCOPE, and perform other related actions. */
14932 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
14937 /* Get the stored value. */
14938 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
14939 /* Perform any access checks that were deferred. */
14940 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
14941 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
14942 /* Set the scope from the stored value. */
14943 parser
->scope
= TREE_VALUE (value
);
14944 parser
->qualifying_scope
= TREE_TYPE (value
);
14945 parser
->object_scope
= NULL_TREE
;
14948 /* Add tokens to CACHE until an non-nested END token appears. */
14951 cp_parser_cache_group (cp_parser
*parser
,
14952 cp_token_cache
*cache
,
14953 enum cpp_ttype end
,
14960 /* Abort a parenthesized expression if we encounter a brace. */
14961 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
14962 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14964 /* Consume the next token. */
14965 token
= cp_lexer_consume_token (parser
->lexer
);
14966 /* If we've reached the end of the file, stop. */
14967 if (token
->type
== CPP_EOF
)
14969 /* Add this token to the tokens we are saving. */
14970 cp_token_cache_push_token (cache
, token
);
14971 /* See if it starts a new group. */
14972 if (token
->type
== CPP_OPEN_BRACE
)
14974 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
14978 else if (token
->type
== CPP_OPEN_PAREN
)
14979 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
14980 else if (token
->type
== end
)
14985 /* Begin parsing tentatively. We always save tokens while parsing
14986 tentatively so that if the tentative parsing fails we can restore the
14990 cp_parser_parse_tentatively (cp_parser
* parser
)
14992 /* Enter a new parsing context. */
14993 parser
->context
= cp_parser_context_new (parser
->context
);
14994 /* Begin saving tokens. */
14995 cp_lexer_save_tokens (parser
->lexer
);
14996 /* In order to avoid repetitive access control error messages,
14997 access checks are queued up until we are no longer parsing
14999 push_deferring_access_checks (dk_deferred
);
15002 /* Commit to the currently active tentative parse. */
15005 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
15007 cp_parser_context
*context
;
15010 /* Mark all of the levels as committed. */
15011 lexer
= parser
->lexer
;
15012 for (context
= parser
->context
; context
->next
; context
= context
->next
)
15014 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
15016 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
15017 while (!cp_lexer_saving_tokens (lexer
))
15018 lexer
= lexer
->next
;
15019 cp_lexer_commit_tokens (lexer
);
15023 /* Abort the currently active tentative parse. All consumed tokens
15024 will be rolled back, and no diagnostics will be issued. */
15027 cp_parser_abort_tentative_parse (cp_parser
* parser
)
15029 cp_parser_simulate_error (parser
);
15030 /* Now, pretend that we want to see if the construct was
15031 successfully parsed. */
15032 cp_parser_parse_definitely (parser
);
15035 /* Stop parsing tentatively. If a parse error has occurred, restore the
15036 token stream. Otherwise, commit to the tokens we have consumed.
15037 Returns true if no error occurred; false otherwise. */
15040 cp_parser_parse_definitely (cp_parser
* parser
)
15042 bool error_occurred
;
15043 cp_parser_context
*context
;
15045 /* Remember whether or not an error occurred, since we are about to
15046 destroy that information. */
15047 error_occurred
= cp_parser_error_occurred (parser
);
15048 /* Remove the topmost context from the stack. */
15049 context
= parser
->context
;
15050 parser
->context
= context
->next
;
15051 /* If no parse errors occurred, commit to the tentative parse. */
15052 if (!error_occurred
)
15054 /* Commit to the tokens read tentatively, unless that was
15056 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
15057 cp_lexer_commit_tokens (parser
->lexer
);
15059 pop_to_parent_deferring_access_checks ();
15061 /* Otherwise, if errors occurred, roll back our state so that things
15062 are just as they were before we began the tentative parse. */
15065 cp_lexer_rollback_tokens (parser
->lexer
);
15066 pop_deferring_access_checks ();
15068 /* Add the context to the front of the free list. */
15069 context
->next
= cp_parser_context_free_list
;
15070 cp_parser_context_free_list
= context
;
15072 return !error_occurred
;
15075 /* Returns true if we are parsing tentatively -- but have decided that
15076 we will stick with this tentative parse, even if errors occur. */
15079 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
15081 return (cp_parser_parsing_tentatively (parser
)
15082 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
15085 /* Returns nonzero iff an error has occurred during the most recent
15086 tentative parse. */
15089 cp_parser_error_occurred (cp_parser
* parser
)
15091 return (cp_parser_parsing_tentatively (parser
)
15092 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
15095 /* Returns nonzero if GNU extensions are allowed. */
15098 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
15100 return parser
->allow_gnu_extensions_p
;
15107 static GTY (()) cp_parser
*the_parser
;
15109 /* External interface. */
15111 /* Parse one entire translation unit. */
15114 c_parse_file (void)
15116 bool error_occurred
;
15118 the_parser
= cp_parser_new ();
15119 push_deferring_access_checks (flag_access_control
15120 ? dk_no_deferred
: dk_no_check
);
15121 error_occurred
= cp_parser_translation_unit (the_parser
);
15125 /* Clean up after parsing the entire translation unit. */
15128 free_parser_stacks (void)
15130 /* Nothing to do. */
15133 /* This variable must be provided by every front end. */
15137 #include "gt-cp-parser.h"