2 Copyright (C) 2000, 2001, 2002, 2003 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 /* If non-NULL, then we are parsing a construct where new type
1261 definitions are not permitted. The string stored here will be
1262 issued as an error message if a type is defined. */
1263 const char *type_definition_forbidden_message
;
1265 /* A list of lists. The outer list is a stack, used for member
1266 functions of local classes. At each level there are two sub-list,
1267 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1268 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1269 TREE_VALUE's. The functions are chained in reverse declaration
1272 The TREE_PURPOSE sublist contains those functions with default
1273 arguments that need post processing, and the TREE_VALUE sublist
1274 contains those functions with definitions that need post
1277 These lists can only be processed once the outermost class being
1278 defined is complete. */
1279 tree unparsed_functions_queues
;
1281 /* The number of classes whose definitions are currently in
1283 unsigned num_classes_being_defined
;
1285 /* The number of template parameter lists that apply directly to the
1286 current declaration. */
1287 unsigned num_template_parameter_lists
;
1290 /* The type of a function that parses some kind of expression. */
1291 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1295 /* Constructors and destructors. */
1297 static cp_parser
*cp_parser_new
1300 /* Routines to parse various constructs.
1302 Those that return `tree' will return the error_mark_node (rather
1303 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1304 Sometimes, they will return an ordinary node if error-recovery was
1305 attempted, even though a parse error occurred. So, to check
1306 whether or not a parse error occurred, you should always use
1307 cp_parser_error_occurred. If the construct is optional (indicated
1308 either by an `_opt' in the name of the function that does the
1309 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1310 the construct is not present. */
1312 /* Lexical conventions [gram.lex] */
1314 static tree cp_parser_identifier
1317 /* Basic concepts [gram.basic] */
1319 static bool cp_parser_translation_unit
1322 /* Expressions [gram.expr] */
1324 static tree cp_parser_primary_expression
1325 (cp_parser
*, cp_id_kind
*, tree
*);
1326 static tree cp_parser_id_expression
1327 (cp_parser
*, bool, bool, bool *, bool);
1328 static tree cp_parser_unqualified_id
1329 (cp_parser
*, bool, bool, bool);
1330 static tree cp_parser_nested_name_specifier_opt
1331 (cp_parser
*, bool, bool, bool, bool);
1332 static tree cp_parser_nested_name_specifier
1333 (cp_parser
*, bool, bool, bool, bool);
1334 static tree cp_parser_class_or_namespace_name
1335 (cp_parser
*, bool, bool, bool, bool, bool);
1336 static tree cp_parser_postfix_expression
1337 (cp_parser
*, bool);
1338 static tree cp_parser_parenthesized_expression_list
1339 (cp_parser
*, bool, bool *);
1340 static void cp_parser_pseudo_destructor_name
1341 (cp_parser
*, tree
*, tree
*);
1342 static tree cp_parser_unary_expression
1343 (cp_parser
*, bool);
1344 static enum tree_code cp_parser_unary_operator
1346 static tree cp_parser_new_expression
1348 static tree cp_parser_new_placement
1350 static tree cp_parser_new_type_id
1352 static tree cp_parser_new_declarator_opt
1354 static tree cp_parser_direct_new_declarator
1356 static tree cp_parser_new_initializer
1358 static tree cp_parser_delete_expression
1360 static tree cp_parser_cast_expression
1361 (cp_parser
*, bool);
1362 static tree cp_parser_pm_expression
1364 static tree cp_parser_multiplicative_expression
1366 static tree cp_parser_additive_expression
1368 static tree cp_parser_shift_expression
1370 static tree cp_parser_relational_expression
1372 static tree cp_parser_equality_expression
1374 static tree cp_parser_and_expression
1376 static tree cp_parser_exclusive_or_expression
1378 static tree cp_parser_inclusive_or_expression
1380 static tree cp_parser_logical_and_expression
1382 static tree cp_parser_logical_or_expression
1384 static tree cp_parser_question_colon_clause
1385 (cp_parser
*, tree
);
1386 static tree cp_parser_assignment_expression
1388 static enum tree_code cp_parser_assignment_operator_opt
1390 static tree cp_parser_expression
1392 static tree cp_parser_constant_expression
1393 (cp_parser
*, bool, bool *);
1395 /* Statements [gram.stmt.stmt] */
1397 static void cp_parser_statement
1398 (cp_parser
*, bool);
1399 static tree cp_parser_labeled_statement
1400 (cp_parser
*, bool);
1401 static tree cp_parser_expression_statement
1402 (cp_parser
*, bool);
1403 static tree cp_parser_compound_statement
1404 (cp_parser
*, bool);
1405 static void cp_parser_statement_seq_opt
1406 (cp_parser
*, bool);
1407 static tree cp_parser_selection_statement
1409 static tree cp_parser_condition
1411 static tree cp_parser_iteration_statement
1413 static void cp_parser_for_init_statement
1415 static tree cp_parser_jump_statement
1417 static void cp_parser_declaration_statement
1420 static tree cp_parser_implicitly_scoped_statement
1422 static void cp_parser_already_scoped_statement
1425 /* Declarations [gram.dcl.dcl] */
1427 static void cp_parser_declaration_seq_opt
1429 static void cp_parser_declaration
1431 static void cp_parser_block_declaration
1432 (cp_parser
*, bool);
1433 static void cp_parser_simple_declaration
1434 (cp_parser
*, bool);
1435 static tree cp_parser_decl_specifier_seq
1436 (cp_parser
*, cp_parser_flags
, tree
*, int *);
1437 static tree cp_parser_storage_class_specifier_opt
1439 static tree cp_parser_function_specifier_opt
1441 static tree cp_parser_type_specifier
1442 (cp_parser
*, cp_parser_flags
, bool, bool, int *, bool *);
1443 static tree cp_parser_simple_type_specifier
1444 (cp_parser
*, cp_parser_flags
, bool);
1445 static tree cp_parser_type_name
1447 static tree cp_parser_elaborated_type_specifier
1448 (cp_parser
*, bool, bool);
1449 static tree cp_parser_enum_specifier
1451 static void cp_parser_enumerator_list
1452 (cp_parser
*, tree
);
1453 static void cp_parser_enumerator_definition
1454 (cp_parser
*, tree
);
1455 static tree cp_parser_namespace_name
1457 static void cp_parser_namespace_definition
1459 static void cp_parser_namespace_body
1461 static tree cp_parser_qualified_namespace_specifier
1463 static void cp_parser_namespace_alias_definition
1465 static void cp_parser_using_declaration
1467 static void cp_parser_using_directive
1469 static void cp_parser_asm_definition
1471 static void cp_parser_linkage_specification
1474 /* Declarators [gram.dcl.decl] */
1476 static tree cp_parser_init_declarator
1477 (cp_parser
*, tree
, tree
, bool, bool, int, bool *);
1478 static tree cp_parser_declarator
1479 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *);
1480 static tree cp_parser_direct_declarator
1481 (cp_parser
*, cp_parser_declarator_kind
, int *);
1482 static enum tree_code cp_parser_ptr_operator
1483 (cp_parser
*, tree
*, tree
*);
1484 static tree cp_parser_cv_qualifier_seq_opt
1486 static tree cp_parser_cv_qualifier_opt
1488 static tree cp_parser_declarator_id
1490 static tree cp_parser_type_id
1492 static tree cp_parser_type_specifier_seq
1494 static tree cp_parser_parameter_declaration_clause
1496 static tree cp_parser_parameter_declaration_list
1498 static tree cp_parser_parameter_declaration
1499 (cp_parser
*, bool, bool *);
1500 static void cp_parser_function_body
1502 static tree cp_parser_initializer
1503 (cp_parser
*, bool *, bool *);
1504 static tree cp_parser_initializer_clause
1505 (cp_parser
*, bool *);
1506 static tree cp_parser_initializer_list
1507 (cp_parser
*, bool *);
1509 static bool cp_parser_ctor_initializer_opt_and_function_body
1512 /* Classes [gram.class] */
1514 static tree cp_parser_class_name
1515 (cp_parser
*, bool, bool, bool, bool, bool, bool);
1516 static tree cp_parser_class_specifier
1518 static tree cp_parser_class_head
1519 (cp_parser
*, bool *);
1520 static enum tag_types cp_parser_class_key
1522 static void cp_parser_member_specification_opt
1524 static void cp_parser_member_declaration
1526 static tree cp_parser_pure_specifier
1528 static tree cp_parser_constant_initializer
1531 /* Derived classes [gram.class.derived] */
1533 static tree cp_parser_base_clause
1535 static tree cp_parser_base_specifier
1538 /* Special member functions [gram.special] */
1540 static tree cp_parser_conversion_function_id
1542 static tree cp_parser_conversion_type_id
1544 static tree cp_parser_conversion_declarator_opt
1546 static bool cp_parser_ctor_initializer_opt
1548 static void cp_parser_mem_initializer_list
1550 static tree cp_parser_mem_initializer
1552 static tree cp_parser_mem_initializer_id
1555 /* Overloading [gram.over] */
1557 static tree cp_parser_operator_function_id
1559 static tree cp_parser_operator
1562 /* Templates [gram.temp] */
1564 static void cp_parser_template_declaration
1565 (cp_parser
*, bool);
1566 static tree cp_parser_template_parameter_list
1568 static tree cp_parser_template_parameter
1570 static tree cp_parser_type_parameter
1572 static tree cp_parser_template_id
1573 (cp_parser
*, bool, bool, bool);
1574 static tree cp_parser_template_name
1575 (cp_parser
*, bool, bool, bool, bool *);
1576 static tree cp_parser_template_argument_list
1578 static tree cp_parser_template_argument
1580 static void cp_parser_explicit_instantiation
1582 static void cp_parser_explicit_specialization
1585 /* Exception handling [gram.exception] */
1587 static tree cp_parser_try_block
1589 static bool cp_parser_function_try_block
1591 static void cp_parser_handler_seq
1593 static void cp_parser_handler
1595 static tree cp_parser_exception_declaration
1597 static tree cp_parser_throw_expression
1599 static tree cp_parser_exception_specification_opt
1601 static tree cp_parser_type_id_list
1604 /* GNU Extensions */
1606 static tree cp_parser_asm_specification_opt
1608 static tree cp_parser_asm_operand_list
1610 static tree cp_parser_asm_clobber_list
1612 static tree cp_parser_attributes_opt
1614 static tree cp_parser_attribute_list
1616 static bool cp_parser_extension_opt
1617 (cp_parser
*, int *);
1618 static void cp_parser_label_declaration
1621 /* Utility Routines */
1623 static tree cp_parser_lookup_name
1624 (cp_parser
*, tree
, bool, bool, bool);
1625 static tree cp_parser_lookup_name_simple
1626 (cp_parser
*, tree
);
1627 static tree cp_parser_maybe_treat_template_as_class
1629 static bool cp_parser_check_declarator_template_parameters
1630 (cp_parser
*, tree
);
1631 static bool cp_parser_check_template_parameters
1632 (cp_parser
*, unsigned);
1633 static tree cp_parser_simple_cast_expression
1635 static tree cp_parser_binary_expression
1636 (cp_parser
*, const cp_parser_token_tree_map
, cp_parser_expression_fn
);
1637 static tree cp_parser_global_scope_opt
1638 (cp_parser
*, bool);
1639 static bool cp_parser_constructor_declarator_p
1640 (cp_parser
*, bool);
1641 static tree cp_parser_function_definition_from_specifiers_and_declarator
1642 (cp_parser
*, tree
, tree
, tree
);
1643 static tree cp_parser_function_definition_after_declarator
1644 (cp_parser
*, bool);
1645 static void cp_parser_template_declaration_after_export
1646 (cp_parser
*, bool);
1647 static tree cp_parser_single_declaration
1648 (cp_parser
*, bool, bool *);
1649 static tree cp_parser_functional_cast
1650 (cp_parser
*, tree
);
1651 static tree cp_parser_save_member_function_body
1652 (cp_parser
*, tree
, tree
, tree
);
1653 static tree cp_parser_enclosed_template_argument_list
1655 static void cp_parser_save_default_args
1656 (cp_parser
*, tree
);
1657 static void cp_parser_late_parsing_for_member
1658 (cp_parser
*, tree
);
1659 static void cp_parser_late_parsing_default_args
1660 (cp_parser
*, tree
);
1661 static tree cp_parser_sizeof_operand
1662 (cp_parser
*, enum rid
);
1663 static bool cp_parser_declares_only_class_p
1665 static tree cp_parser_fold_non_dependent_expr
1667 static bool cp_parser_friend_p
1669 static cp_token
*cp_parser_require
1670 (cp_parser
*, enum cpp_ttype
, const char *);
1671 static cp_token
*cp_parser_require_keyword
1672 (cp_parser
*, enum rid
, const char *);
1673 static bool cp_parser_token_starts_function_definition_p
1675 static bool cp_parser_next_token_starts_class_definition_p
1677 static bool cp_parser_next_token_ends_template_argument_p
1679 static enum tag_types cp_parser_token_is_class_key
1681 static void cp_parser_check_class_key
1682 (enum tag_types
, tree type
);
1683 static void cp_parser_check_access_in_redeclaration
1685 static bool cp_parser_optional_template_keyword
1687 static void cp_parser_pre_parsed_nested_name_specifier
1689 static void cp_parser_cache_group
1690 (cp_parser
*, cp_token_cache
*, enum cpp_ttype
, unsigned);
1691 static void cp_parser_parse_tentatively
1693 static void cp_parser_commit_to_tentative_parse
1695 static void cp_parser_abort_tentative_parse
1697 static bool cp_parser_parse_definitely
1699 static inline bool cp_parser_parsing_tentatively
1701 static bool cp_parser_committed_to_tentative_parse
1703 static void cp_parser_error
1704 (cp_parser
*, const char *);
1705 static void cp_parser_name_lookup_error
1706 (cp_parser
*, tree
, tree
, const char *);
1707 static bool cp_parser_simulate_error
1709 static void cp_parser_check_type_definition
1711 static void cp_parser_check_for_definition_in_return_type
1713 static void cp_parser_check_for_invalid_template_id
1714 (cp_parser
*, tree
);
1715 static tree cp_parser_non_integral_constant_expression
1717 static bool cp_parser_diagnose_invalid_type_name
1719 static int cp_parser_skip_to_closing_parenthesis
1720 (cp_parser
*, bool, bool, bool);
1721 static void cp_parser_skip_to_end_of_statement
1723 static void cp_parser_consume_semicolon_at_end_of_statement
1725 static void cp_parser_skip_to_end_of_block_or_statement
1727 static void cp_parser_skip_to_closing_brace
1729 static void cp_parser_skip_until_found
1730 (cp_parser
*, enum cpp_ttype
, const char *);
1731 static bool cp_parser_error_occurred
1733 static bool cp_parser_allow_gnu_extensions_p
1735 static bool cp_parser_is_string_literal
1737 static bool cp_parser_is_keyword
1738 (cp_token
*, enum rid
);
1740 /* Returns nonzero if we are parsing tentatively. */
1743 cp_parser_parsing_tentatively (cp_parser
* parser
)
1745 return parser
->context
->next
!= NULL
;
1748 /* Returns nonzero if TOKEN is a string literal. */
1751 cp_parser_is_string_literal (cp_token
* token
)
1753 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1756 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1759 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1761 return token
->keyword
== keyword
;
1764 /* Issue the indicated error MESSAGE. */
1767 cp_parser_error (cp_parser
* parser
, const char* message
)
1769 /* Output the MESSAGE -- unless we're parsing tentatively. */
1770 if (!cp_parser_simulate_error (parser
))
1773 token
= cp_lexer_peek_token (parser
->lexer
);
1774 c_parse_error (message
,
1775 /* Because c_parser_error does not understand
1776 CPP_KEYWORD, keywords are treated like
1778 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1783 /* Issue an error about name-lookup failing. NAME is the
1784 IDENTIFIER_NODE DECL is the result of
1785 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1786 the thing that we hoped to find. */
1789 cp_parser_name_lookup_error (cp_parser
* parser
,
1792 const char* desired
)
1794 /* If name lookup completely failed, tell the user that NAME was not
1796 if (decl
== error_mark_node
)
1798 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1799 error ("`%D::%D' has not been declared",
1800 parser
->scope
, name
);
1801 else if (parser
->scope
== global_namespace
)
1802 error ("`::%D' has not been declared", name
);
1804 error ("`%D' has not been declared", name
);
1806 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1807 error ("`%D::%D' %s", parser
->scope
, name
, desired
);
1808 else if (parser
->scope
== global_namespace
)
1809 error ("`::%D' %s", name
, desired
);
1811 error ("`%D' %s", name
, desired
);
1814 /* If we are parsing tentatively, remember that an error has occurred
1815 during this tentative parse. Returns true if the error was
1816 simulated; false if a messgae should be issued by the caller. */
1819 cp_parser_simulate_error (cp_parser
* parser
)
1821 if (cp_parser_parsing_tentatively (parser
)
1822 && !cp_parser_committed_to_tentative_parse (parser
))
1824 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1830 /* This function is called when a type is defined. If type
1831 definitions are forbidden at this point, an error message is
1835 cp_parser_check_type_definition (cp_parser
* parser
)
1837 /* If types are forbidden here, issue a message. */
1838 if (parser
->type_definition_forbidden_message
)
1839 /* Use `%s' to print the string in case there are any escape
1840 characters in the message. */
1841 error ("%s", parser
->type_definition_forbidden_message
);
1844 /* This function is called when a declaration is parsed. If
1845 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1846 indicates that a type was defined in the decl-specifiers for DECL,
1847 then an error is issued. */
1850 cp_parser_check_for_definition_in_return_type (tree declarator
,
1851 int declares_class_or_enum
)
1853 /* [dcl.fct] forbids type definitions in return types.
1854 Unfortunately, it's not easy to know whether or not we are
1855 processing a return type until after the fact. */
1857 && (TREE_CODE (declarator
) == INDIRECT_REF
1858 || TREE_CODE (declarator
) == ADDR_EXPR
))
1859 declarator
= TREE_OPERAND (declarator
, 0);
1861 && TREE_CODE (declarator
) == CALL_EXPR
1862 && declares_class_or_enum
& 2)
1863 error ("new types may not be defined in a return type");
1866 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1867 "<" in any valid C++ program. If the next token is indeed "<",
1868 issue a message warning the user about what appears to be an
1869 invalid attempt to form a template-id. */
1872 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1878 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1881 error ("`%T' is not a template", type
);
1882 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1883 error ("`%s' is not a template", IDENTIFIER_POINTER (type
));
1885 error ("invalid template-id");
1886 /* Remember the location of the invalid "<". */
1887 if (cp_parser_parsing_tentatively (parser
)
1888 && !cp_parser_committed_to_tentative_parse (parser
))
1890 token
= cp_lexer_peek_token (parser
->lexer
);
1891 token
= cp_lexer_prev_token (parser
->lexer
, token
);
1892 start
= cp_lexer_token_difference (parser
->lexer
,
1893 parser
->lexer
->first_token
,
1898 /* Consume the "<". */
1899 cp_lexer_consume_token (parser
->lexer
);
1900 /* Parse the template arguments. */
1901 cp_parser_enclosed_template_argument_list (parser
);
1902 /* Permanently remove the invalid template arguments so that
1903 this error message is not issued again. */
1906 token
= cp_lexer_advance_token (parser
->lexer
,
1907 parser
->lexer
->first_token
,
1909 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
1914 /* Issue an error message about the fact that THING appeared in a
1915 constant-expression. Returns ERROR_MARK_NODE. */
1918 cp_parser_non_integral_constant_expression (const char *thing
)
1920 error ("%s cannot appear in a constant-expression", thing
);
1921 return error_mark_node
;
1924 /* Check for a common situation where a type-name should be present,
1925 but is not, and issue a sensible error message. Returns true if an
1926 invalid type-name was detected. */
1929 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
)
1931 /* If the next two tokens are both identifiers, the code is
1932 erroneous. The usual cause of this situation is code like:
1936 where "T" should name a type -- but does not. */
1937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
1938 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
1942 /* If parsing tentatively, we should commit; we really are
1943 looking at a declaration. */
1944 /* Consume the first identifier. */
1945 name
= cp_lexer_consume_token (parser
->lexer
)->value
;
1946 /* Issue an error message. */
1947 error ("`%s' does not name a type", IDENTIFIER_POINTER (name
));
1948 /* If we're in a template class, it's possible that the user was
1949 referring to a type from a base class. For example:
1951 template <typename T> struct A { typedef T X; };
1952 template <typename T> struct B : public A<T> { X x; };
1954 The user should have said "typename A<T>::X". */
1955 if (processing_template_decl
&& current_class_type
)
1959 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1963 tree base_type
= BINFO_TYPE (b
);
1964 if (CLASS_TYPE_P (base_type
)
1965 && dependent_type_p (base_type
))
1968 /* Go from a particular instantiation of the
1969 template (which will have an empty TYPE_FIELDs),
1970 to the main version. */
1971 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1972 for (field
= TYPE_FIELDS (base_type
);
1974 field
= TREE_CHAIN (field
))
1975 if (TREE_CODE (field
) == TYPE_DECL
1976 && DECL_NAME (field
) == name
)
1978 error ("(perhaps `typename %T::%s' was intended)",
1979 BINFO_TYPE (b
), IDENTIFIER_POINTER (name
));
1987 /* Skip to the end of the declaration; there's no point in
1988 trying to process it. */
1989 cp_parser_skip_to_end_of_statement (parser
);
1997 /* Consume tokens up to, and including, the next non-nested closing `)'.
1998 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1999 are doing error recovery. Returns -1 if OR_COMMA is true and we
2000 found an unnested comma. */
2003 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2008 unsigned paren_depth
= 0;
2009 unsigned brace_depth
= 0;
2011 if (recovering
&& !or_comma
&& cp_parser_parsing_tentatively (parser
)
2012 && !cp_parser_committed_to_tentative_parse (parser
))
2019 /* If we've run out of tokens, then there is no closing `)'. */
2020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2023 token
= cp_lexer_peek_token (parser
->lexer
);
2025 /* This matches the processing in skip_to_end_of_statement. */
2026 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2028 if (token
->type
== CPP_OPEN_BRACE
)
2030 if (token
->type
== CPP_CLOSE_BRACE
)
2035 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2036 && !brace_depth
&& !paren_depth
)
2041 /* If it is an `(', we have entered another level of nesting. */
2042 if (token
->type
== CPP_OPEN_PAREN
)
2044 /* If it is a `)', then we might be done. */
2045 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2048 cp_lexer_consume_token (parser
->lexer
);
2053 /* Consume the token. */
2054 cp_lexer_consume_token (parser
->lexer
);
2058 /* Consume tokens until we reach the end of the current statement.
2059 Normally, that will be just before consuming a `;'. However, if a
2060 non-nested `}' comes first, then we stop before consuming that. */
2063 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2065 unsigned nesting_depth
= 0;
2071 /* Peek at the next token. */
2072 token
= cp_lexer_peek_token (parser
->lexer
);
2073 /* If we've run out of tokens, stop. */
2074 if (token
->type
== CPP_EOF
)
2076 /* If the next token is a `;', we have reached the end of the
2078 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2080 /* If the next token is a non-nested `}', then we have reached
2081 the end of the current block. */
2082 if (token
->type
== CPP_CLOSE_BRACE
)
2084 /* If this is a non-nested `}', stop before consuming it.
2085 That way, when confronted with something like:
2089 we stop before consuming the closing `}', even though we
2090 have not yet reached a `;'. */
2091 if (nesting_depth
== 0)
2093 /* If it is the closing `}' for a block that we have
2094 scanned, stop -- but only after consuming the token.
2100 we will stop after the body of the erroneously declared
2101 function, but before consuming the following `typedef'
2103 if (--nesting_depth
== 0)
2105 cp_lexer_consume_token (parser
->lexer
);
2109 /* If it the next token is a `{', then we are entering a new
2110 block. Consume the entire block. */
2111 else if (token
->type
== CPP_OPEN_BRACE
)
2113 /* Consume the token. */
2114 cp_lexer_consume_token (parser
->lexer
);
2118 /* This function is called at the end of a statement or declaration.
2119 If the next token is a semicolon, it is consumed; otherwise, error
2120 recovery is attempted. */
2123 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2125 /* Look for the trailing `;'. */
2126 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2128 /* If there is additional (erroneous) input, skip to the end of
2130 cp_parser_skip_to_end_of_statement (parser
);
2131 /* If the next token is now a `;', consume it. */
2132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2133 cp_lexer_consume_token (parser
->lexer
);
2137 /* Skip tokens until we have consumed an entire block, or until we
2138 have consumed a non-nested `;'. */
2141 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2143 unsigned nesting_depth
= 0;
2149 /* Peek at the next token. */
2150 token
= cp_lexer_peek_token (parser
->lexer
);
2151 /* If we've run out of tokens, stop. */
2152 if (token
->type
== CPP_EOF
)
2154 /* If the next token is a `;', we have reached the end of the
2156 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2158 /* Consume the `;'. */
2159 cp_lexer_consume_token (parser
->lexer
);
2162 /* Consume the token. */
2163 token
= cp_lexer_consume_token (parser
->lexer
);
2164 /* If the next token is a non-nested `}', then we have reached
2165 the end of the current block. */
2166 if (token
->type
== CPP_CLOSE_BRACE
2167 && (nesting_depth
== 0 || --nesting_depth
== 0))
2169 /* If it the next token is a `{', then we are entering a new
2170 block. Consume the entire block. */
2171 if (token
->type
== CPP_OPEN_BRACE
)
2176 /* Skip tokens until a non-nested closing curly brace is the next
2180 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2182 unsigned nesting_depth
= 0;
2188 /* Peek at the next token. */
2189 token
= cp_lexer_peek_token (parser
->lexer
);
2190 /* If we've run out of tokens, stop. */
2191 if (token
->type
== CPP_EOF
)
2193 /* If the next token is a non-nested `}', then we have reached
2194 the end of the current block. */
2195 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2197 /* If it the next token is a `{', then we are entering a new
2198 block. Consume the entire block. */
2199 else if (token
->type
== CPP_OPEN_BRACE
)
2201 /* Consume the token. */
2202 cp_lexer_consume_token (parser
->lexer
);
2206 /* Create a new C++ parser. */
2209 cp_parser_new (void)
2214 /* cp_lexer_new_main is called before calling ggc_alloc because
2215 cp_lexer_new_main might load a PCH file. */
2216 lexer
= cp_lexer_new_main ();
2218 parser
= ggc_alloc_cleared (sizeof (cp_parser
));
2219 parser
->lexer
= lexer
;
2220 parser
->context
= cp_parser_context_new (NULL
);
2222 /* For now, we always accept GNU extensions. */
2223 parser
->allow_gnu_extensions_p
= 1;
2225 /* The `>' token is a greater-than operator, not the end of a
2227 parser
->greater_than_is_operator_p
= true;
2229 parser
->default_arg_ok_p
= true;
2231 /* We are not parsing a constant-expression. */
2232 parser
->integral_constant_expression_p
= false;
2233 parser
->allow_non_integral_constant_expression_p
= false;
2234 parser
->non_integral_constant_expression_p
= false;
2236 /* We are not parsing offsetof. */
2237 parser
->in_offsetof_p
= false;
2239 /* Local variable names are not forbidden. */
2240 parser
->local_variables_forbidden_p
= false;
2242 /* We are not processing an `extern "C"' declaration. */
2243 parser
->in_unbraced_linkage_specification_p
= false;
2245 /* We are not processing a declarator. */
2246 parser
->in_declarator_p
= false;
2248 /* We are not processing a template-argument-list. */
2249 parser
->in_template_argument_list_p
= false;
2251 /* We are not in an iteration statement. */
2252 parser
->in_iteration_statement_p
= false;
2254 /* We are not in a switch statement. */
2255 parser
->in_switch_statement_p
= false;
2257 /* The unparsed function queue is empty. */
2258 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2260 /* There are no classes being defined. */
2261 parser
->num_classes_being_defined
= 0;
2263 /* No template parameters apply. */
2264 parser
->num_template_parameter_lists
= 0;
2269 /* Lexical conventions [gram.lex] */
2271 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2275 cp_parser_identifier (cp_parser
* parser
)
2279 /* Look for the identifier. */
2280 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2281 /* Return the value. */
2282 return token
? token
->value
: error_mark_node
;
2285 /* Basic concepts [gram.basic] */
2287 /* Parse a translation-unit.
2290 declaration-seq [opt]
2292 Returns TRUE if all went well. */
2295 cp_parser_translation_unit (cp_parser
* parser
)
2299 cp_parser_declaration_seq_opt (parser
);
2301 /* If there are no tokens left then all went well. */
2302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2305 /* Otherwise, issue an error message. */
2306 cp_parser_error (parser
, "expected declaration");
2310 /* Consume the EOF token. */
2311 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2314 finish_translation_unit ();
2316 /* All went well. */
2320 /* Expressions [gram.expr] */
2322 /* Parse a primary-expression.
2333 ( compound-statement )
2334 __builtin_va_arg ( assignment-expression , type-id )
2339 Returns a representation of the expression.
2341 *IDK indicates what kind of id-expression (if any) was present.
2343 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2344 used as the operand of a pointer-to-member. In that case,
2345 *QUALIFYING_CLASS gives the class that is used as the qualifying
2346 class in the pointer-to-member. */
2349 cp_parser_primary_expression (cp_parser
*parser
,
2351 tree
*qualifying_class
)
2355 /* Assume the primary expression is not an id-expression. */
2356 *idk
= CP_ID_KIND_NONE
;
2357 /* And that it cannot be used as pointer-to-member. */
2358 *qualifying_class
= NULL_TREE
;
2360 /* Peek at the next token. */
2361 token
= cp_lexer_peek_token (parser
->lexer
);
2362 switch (token
->type
)
2375 token
= cp_lexer_consume_token (parser
->lexer
);
2376 return token
->value
;
2378 case CPP_OPEN_PAREN
:
2381 bool saved_greater_than_is_operator_p
;
2383 /* Consume the `('. */
2384 cp_lexer_consume_token (parser
->lexer
);
2385 /* Within a parenthesized expression, a `>' token is always
2386 the greater-than operator. */
2387 saved_greater_than_is_operator_p
2388 = parser
->greater_than_is_operator_p
;
2389 parser
->greater_than_is_operator_p
= true;
2390 /* If we see `( { ' then we are looking at the beginning of
2391 a GNU statement-expression. */
2392 if (cp_parser_allow_gnu_extensions_p (parser
)
2393 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2395 /* Statement-expressions are not allowed by the standard. */
2397 pedwarn ("ISO C++ forbids braced-groups within expressions");
2399 /* And they're not allowed outside of a function-body; you
2400 cannot, for example, write:
2402 int i = ({ int j = 3; j + 1; });
2404 at class or namespace scope. */
2405 if (!at_function_scope_p ())
2406 error ("statement-expressions are allowed only inside functions");
2407 /* Start the statement-expression. */
2408 expr
= begin_stmt_expr ();
2409 /* Parse the compound-statement. */
2410 cp_parser_compound_statement (parser
, true);
2412 expr
= finish_stmt_expr (expr
, false);
2416 /* Parse the parenthesized expression. */
2417 expr
= cp_parser_expression (parser
);
2418 /* Let the front end know that this expression was
2419 enclosed in parentheses. This matters in case, for
2420 example, the expression is of the form `A::B', since
2421 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2423 finish_parenthesized_expr (expr
);
2425 /* The `>' token might be the end of a template-id or
2426 template-parameter-list now. */
2427 parser
->greater_than_is_operator_p
2428 = saved_greater_than_is_operator_p
;
2429 /* Consume the `)'. */
2430 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2431 cp_parser_skip_to_end_of_statement (parser
);
2437 switch (token
->keyword
)
2439 /* These two are the boolean literals. */
2441 cp_lexer_consume_token (parser
->lexer
);
2442 return boolean_true_node
;
2444 cp_lexer_consume_token (parser
->lexer
);
2445 return boolean_false_node
;
2447 /* The `__null' literal. */
2449 cp_lexer_consume_token (parser
->lexer
);
2452 /* Recognize the `this' keyword. */
2454 cp_lexer_consume_token (parser
->lexer
);
2455 if (parser
->local_variables_forbidden_p
)
2457 error ("`this' may not be used in this context");
2458 return error_mark_node
;
2460 /* Pointers cannot appear in constant-expressions. */
2461 if (parser
->integral_constant_expression_p
)
2463 if (!parser
->allow_non_integral_constant_expression_p
)
2464 return cp_parser_non_integral_constant_expression ("`this'");
2465 parser
->non_integral_constant_expression_p
= true;
2467 return finish_this_expr ();
2469 /* The `operator' keyword can be the beginning of an
2474 case RID_FUNCTION_NAME
:
2475 case RID_PRETTY_FUNCTION_NAME
:
2476 case RID_C99_FUNCTION_NAME
:
2477 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2478 __func__ are the names of variables -- but they are
2479 treated specially. Therefore, they are handled here,
2480 rather than relying on the generic id-expression logic
2481 below. Grammatically, these names are id-expressions.
2483 Consume the token. */
2484 token
= cp_lexer_consume_token (parser
->lexer
);
2485 /* Look up the name. */
2486 return finish_fname (token
->value
);
2493 /* The `__builtin_va_arg' construct is used to handle
2494 `va_arg'. Consume the `__builtin_va_arg' token. */
2495 cp_lexer_consume_token (parser
->lexer
);
2496 /* Look for the opening `('. */
2497 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2498 /* Now, parse the assignment-expression. */
2499 expression
= cp_parser_assignment_expression (parser
);
2500 /* Look for the `,'. */
2501 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2502 /* Parse the type-id. */
2503 type
= cp_parser_type_id (parser
);
2504 /* Look for the closing `)'. */
2505 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2506 /* Using `va_arg' in a constant-expression is not
2508 if (parser
->integral_constant_expression_p
)
2510 if (!parser
->allow_non_integral_constant_expression_p
)
2511 return cp_parser_non_integral_constant_expression ("`va_arg'");
2512 parser
->non_integral_constant_expression_p
= true;
2514 return build_x_va_arg (expression
, type
);
2520 bool saved_in_offsetof_p
;
2522 /* Consume the "__offsetof__" token. */
2523 cp_lexer_consume_token (parser
->lexer
);
2524 /* Consume the opening `('. */
2525 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2526 /* Parse the parenthesized (almost) constant-expression. */
2527 saved_in_offsetof_p
= parser
->in_offsetof_p
;
2528 parser
->in_offsetof_p
= true;
2530 = cp_parser_constant_expression (parser
,
2531 /*allow_non_constant_p=*/false,
2532 /*non_constant_p=*/NULL
);
2533 parser
->in_offsetof_p
= saved_in_offsetof_p
;
2534 /* Consume the closing ')'. */
2535 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2541 cp_parser_error (parser
, "expected primary-expression");
2542 return error_mark_node
;
2545 /* An id-expression can start with either an identifier, a
2546 `::' as the beginning of a qualified-id, or the "operator"
2550 case CPP_TEMPLATE_ID
:
2551 case CPP_NESTED_NAME_SPECIFIER
:
2555 const char *error_msg
;
2558 /* Parse the id-expression. */
2560 = cp_parser_id_expression (parser
,
2561 /*template_keyword_p=*/false,
2562 /*check_dependency_p=*/true,
2563 /*template_p=*/NULL
,
2564 /*declarator_p=*/false);
2565 if (id_expression
== error_mark_node
)
2566 return error_mark_node
;
2567 /* If we have a template-id, then no further lookup is
2568 required. If the template-id was for a template-class, we
2569 will sometimes have a TYPE_DECL at this point. */
2570 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2571 || TREE_CODE (id_expression
) == TYPE_DECL
)
2572 decl
= id_expression
;
2573 /* Look up the name. */
2576 decl
= cp_parser_lookup_name_simple (parser
, id_expression
);
2577 /* If name lookup gives us a SCOPE_REF, then the
2578 qualifying scope was dependent. Just propagate the
2580 if (TREE_CODE (decl
) == SCOPE_REF
)
2582 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2583 *qualifying_class
= TREE_OPERAND (decl
, 0);
2586 /* Check to see if DECL is a local variable in a context
2587 where that is forbidden. */
2588 if (parser
->local_variables_forbidden_p
2589 && local_variable_p (decl
))
2591 /* It might be that we only found DECL because we are
2592 trying to be generous with pre-ISO scoping rules.
2593 For example, consider:
2597 for (int i = 0; i < 10; ++i) {}
2598 extern void f(int j = i);
2601 Here, name look up will originally find the out
2602 of scope `i'. We need to issue a warning message,
2603 but then use the global `i'. */
2604 decl
= check_for_out_of_scope_variable (decl
);
2605 if (local_variable_p (decl
))
2607 error ("local variable `%D' may not appear in this context",
2609 return error_mark_node
;
2614 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2615 idk
, qualifying_class
,
2616 parser
->integral_constant_expression_p
,
2617 parser
->allow_non_integral_constant_expression_p
,
2618 &parser
->non_integral_constant_expression_p
,
2621 cp_parser_error (parser
, error_msg
);
2625 /* Anything else is an error. */
2627 cp_parser_error (parser
, "expected primary-expression");
2628 return error_mark_node
;
2632 /* Parse an id-expression.
2639 :: [opt] nested-name-specifier template [opt] unqualified-id
2641 :: operator-function-id
2644 Return a representation of the unqualified portion of the
2645 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2646 a `::' or nested-name-specifier.
2648 Often, if the id-expression was a qualified-id, the caller will
2649 want to make a SCOPE_REF to represent the qualified-id. This
2650 function does not do this in order to avoid wastefully creating
2651 SCOPE_REFs when they are not required.
2653 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2656 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2657 uninstantiated templates.
2659 If *TEMPLATE_P is non-NULL, it is set to true iff the
2660 `template' keyword is used to explicitly indicate that the entity
2661 named is a template.
2663 If DECLARATOR_P is true, the id-expression is appearing as part of
2664 a declarator, rather than as part of an expression. */
2667 cp_parser_id_expression (cp_parser
*parser
,
2668 bool template_keyword_p
,
2669 bool check_dependency_p
,
2673 bool global_scope_p
;
2674 bool nested_name_specifier_p
;
2676 /* Assume the `template' keyword was not used. */
2678 *template_p
= false;
2680 /* Look for the optional `::' operator. */
2682 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2684 /* Look for the optional nested-name-specifier. */
2685 nested_name_specifier_p
2686 = (cp_parser_nested_name_specifier_opt (parser
,
2687 /*typename_keyword_p=*/false,
2690 /*is_declarator=*/false)
2692 /* If there is a nested-name-specifier, then we are looking at
2693 the first qualified-id production. */
2694 if (nested_name_specifier_p
)
2697 tree saved_object_scope
;
2698 tree saved_qualifying_scope
;
2699 tree unqualified_id
;
2702 /* See if the next token is the `template' keyword. */
2704 template_p
= &is_template
;
2705 *template_p
= cp_parser_optional_template_keyword (parser
);
2706 /* Name lookup we do during the processing of the
2707 unqualified-id might obliterate SCOPE. */
2708 saved_scope
= parser
->scope
;
2709 saved_object_scope
= parser
->object_scope
;
2710 saved_qualifying_scope
= parser
->qualifying_scope
;
2711 /* Process the final unqualified-id. */
2712 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2715 /* Restore the SAVED_SCOPE for our caller. */
2716 parser
->scope
= saved_scope
;
2717 parser
->object_scope
= saved_object_scope
;
2718 parser
->qualifying_scope
= saved_qualifying_scope
;
2720 return unqualified_id
;
2722 /* Otherwise, if we are in global scope, then we are looking at one
2723 of the other qualified-id productions. */
2724 else if (global_scope_p
)
2729 /* Peek at the next token. */
2730 token
= cp_lexer_peek_token (parser
->lexer
);
2732 /* If it's an identifier, and the next token is not a "<", then
2733 we can avoid the template-id case. This is an optimization
2734 for this common case. */
2735 if (token
->type
== CPP_NAME
2736 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
2737 return cp_parser_identifier (parser
);
2739 cp_parser_parse_tentatively (parser
);
2740 /* Try a template-id. */
2741 id
= cp_parser_template_id (parser
,
2742 /*template_keyword_p=*/false,
2743 /*check_dependency_p=*/true,
2745 /* If that worked, we're done. */
2746 if (cp_parser_parse_definitely (parser
))
2749 /* Peek at the next token. (Changes in the token buffer may
2750 have invalidated the pointer obtained above.) */
2751 token
= cp_lexer_peek_token (parser
->lexer
);
2753 switch (token
->type
)
2756 return cp_parser_identifier (parser
);
2759 if (token
->keyword
== RID_OPERATOR
)
2760 return cp_parser_operator_function_id (parser
);
2764 cp_parser_error (parser
, "expected id-expression");
2765 return error_mark_node
;
2769 return cp_parser_unqualified_id (parser
, template_keyword_p
,
2770 /*check_dependency_p=*/true,
2774 /* Parse an unqualified-id.
2778 operator-function-id
2779 conversion-function-id
2783 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2784 keyword, in a construct like `A::template ...'.
2786 Returns a representation of unqualified-id. For the `identifier'
2787 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2788 production a BIT_NOT_EXPR is returned; the operand of the
2789 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2790 other productions, see the documentation accompanying the
2791 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2792 names are looked up in uninstantiated templates. If DECLARATOR_P
2793 is true, the unqualified-id is appearing as part of a declarator,
2794 rather than as part of an expression. */
2797 cp_parser_unqualified_id (cp_parser
* parser
,
2798 bool template_keyword_p
,
2799 bool check_dependency_p
,
2804 /* Peek at the next token. */
2805 token
= cp_lexer_peek_token (parser
->lexer
);
2807 switch (token
->type
)
2813 /* We don't know yet whether or not this will be a
2815 cp_parser_parse_tentatively (parser
);
2816 /* Try a template-id. */
2817 id
= cp_parser_template_id (parser
, template_keyword_p
,
2820 /* If it worked, we're done. */
2821 if (cp_parser_parse_definitely (parser
))
2823 /* Otherwise, it's an ordinary identifier. */
2824 return cp_parser_identifier (parser
);
2827 case CPP_TEMPLATE_ID
:
2828 return cp_parser_template_id (parser
, template_keyword_p
,
2835 tree qualifying_scope
;
2839 /* Consume the `~' token. */
2840 cp_lexer_consume_token (parser
->lexer
);
2841 /* Parse the class-name. The standard, as written, seems to
2844 template <typename T> struct S { ~S (); };
2845 template <typename T> S<T>::~S() {}
2847 is invalid, since `~' must be followed by a class-name, but
2848 `S<T>' is dependent, and so not known to be a class.
2849 That's not right; we need to look in uninstantiated
2850 templates. A further complication arises from:
2852 template <typename T> void f(T t) {
2856 Here, it is not possible to look up `T' in the scope of `T'
2857 itself. We must look in both the current scope, and the
2858 scope of the containing complete expression.
2860 Yet another issue is:
2869 The standard does not seem to say that the `S' in `~S'
2870 should refer to the type `S' and not the data member
2873 /* DR 244 says that we look up the name after the "~" in the
2874 same scope as we looked up the qualifying name. That idea
2875 isn't fully worked out; it's more complicated than that. */
2876 scope
= parser
->scope
;
2877 object_scope
= parser
->object_scope
;
2878 qualifying_scope
= parser
->qualifying_scope
;
2880 /* If the name is of the form "X::~X" it's OK. */
2881 if (scope
&& TYPE_P (scope
)
2882 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2883 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
2885 && (cp_lexer_peek_token (parser
->lexer
)->value
2886 == TYPE_IDENTIFIER (scope
)))
2888 cp_lexer_consume_token (parser
->lexer
);
2889 return build_nt (BIT_NOT_EXPR
, scope
);
2892 /* If there was an explicit qualification (S::~T), first look
2893 in the scope given by the qualification (i.e., S). */
2896 cp_parser_parse_tentatively (parser
);
2897 type_decl
= cp_parser_class_name (parser
,
2898 /*typename_keyword_p=*/false,
2899 /*template_keyword_p=*/false,
2901 /*check_dependency=*/false,
2902 /*class_head_p=*/false,
2904 if (cp_parser_parse_definitely (parser
))
2905 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2907 /* In "N::S::~S", look in "N" as well. */
2908 if (scope
&& qualifying_scope
)
2910 cp_parser_parse_tentatively (parser
);
2911 parser
->scope
= qualifying_scope
;
2912 parser
->object_scope
= NULL_TREE
;
2913 parser
->qualifying_scope
= NULL_TREE
;
2915 = cp_parser_class_name (parser
,
2916 /*typename_keyword_p=*/false,
2917 /*template_keyword_p=*/false,
2919 /*check_dependency=*/false,
2920 /*class_head_p=*/false,
2922 if (cp_parser_parse_definitely (parser
))
2923 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2925 /* In "p->S::~T", look in the scope given by "*p" as well. */
2926 else if (object_scope
)
2928 cp_parser_parse_tentatively (parser
);
2929 parser
->scope
= object_scope
;
2930 parser
->object_scope
= NULL_TREE
;
2931 parser
->qualifying_scope
= NULL_TREE
;
2933 = cp_parser_class_name (parser
,
2934 /*typename_keyword_p=*/false,
2935 /*template_keyword_p=*/false,
2937 /*check_dependency=*/false,
2938 /*class_head_p=*/false,
2940 if (cp_parser_parse_definitely (parser
))
2941 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2943 /* Look in the surrounding context. */
2944 parser
->scope
= NULL_TREE
;
2945 parser
->object_scope
= NULL_TREE
;
2946 parser
->qualifying_scope
= NULL_TREE
;
2948 = cp_parser_class_name (parser
,
2949 /*typename_keyword_p=*/false,
2950 /*template_keyword_p=*/false,
2952 /*check_dependency=*/false,
2953 /*class_head_p=*/false,
2955 /* If an error occurred, assume that the name of the
2956 destructor is the same as the name of the qualifying
2957 class. That allows us to keep parsing after running
2958 into ill-formed destructor names. */
2959 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
2960 return build_nt (BIT_NOT_EXPR
, scope
);
2961 else if (type_decl
== error_mark_node
)
2962 return error_mark_node
;
2966 A typedef-name that names a class shall not be used as the
2967 identifier in the declarator for a destructor declaration. */
2969 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
2970 && !DECL_SELF_REFERENCE_P (type_decl
))
2971 error ("typedef-name `%D' used as destructor declarator",
2974 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2978 if (token
->keyword
== RID_OPERATOR
)
2982 /* This could be a template-id, so we try that first. */
2983 cp_parser_parse_tentatively (parser
);
2984 /* Try a template-id. */
2985 id
= cp_parser_template_id (parser
, template_keyword_p
,
2986 /*check_dependency_p=*/true,
2988 /* If that worked, we're done. */
2989 if (cp_parser_parse_definitely (parser
))
2991 /* We still don't know whether we're looking at an
2992 operator-function-id or a conversion-function-id. */
2993 cp_parser_parse_tentatively (parser
);
2994 /* Try an operator-function-id. */
2995 id
= cp_parser_operator_function_id (parser
);
2996 /* If that didn't work, try a conversion-function-id. */
2997 if (!cp_parser_parse_definitely (parser
))
2998 id
= cp_parser_conversion_function_id (parser
);
3005 cp_parser_error (parser
, "expected unqualified-id");
3006 return error_mark_node
;
3010 /* Parse an (optional) nested-name-specifier.
3012 nested-name-specifier:
3013 class-or-namespace-name :: nested-name-specifier [opt]
3014 class-or-namespace-name :: template nested-name-specifier [opt]
3016 PARSER->SCOPE should be set appropriately before this function is
3017 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3018 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3021 Sets PARSER->SCOPE to the class (TYPE) or namespace
3022 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3023 it unchanged if there is no nested-name-specifier. Returns the new
3024 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3026 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3027 part of a declaration and/or decl-specifier. */
3030 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3031 bool typename_keyword_p
,
3032 bool check_dependency_p
,
3034 bool is_declaration
)
3036 bool success
= false;
3037 tree access_check
= NULL_TREE
;
3041 /* If the next token corresponds to a nested name specifier, there
3042 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3043 false, it may have been true before, in which case something
3044 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3045 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3046 CHECK_DEPENDENCY_P is false, we have to fall through into the
3048 if (check_dependency_p
3049 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3051 cp_parser_pre_parsed_nested_name_specifier (parser
);
3052 return parser
->scope
;
3055 /* Remember where the nested-name-specifier starts. */
3056 if (cp_parser_parsing_tentatively (parser
)
3057 && !cp_parser_committed_to_tentative_parse (parser
))
3059 token
= cp_lexer_peek_token (parser
->lexer
);
3060 start
= cp_lexer_token_difference (parser
->lexer
,
3061 parser
->lexer
->first_token
,
3067 push_deferring_access_checks (dk_deferred
);
3073 tree saved_qualifying_scope
;
3074 bool template_keyword_p
;
3076 /* Spot cases that cannot be the beginning of a
3077 nested-name-specifier. */
3078 token
= cp_lexer_peek_token (parser
->lexer
);
3080 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3081 the already parsed nested-name-specifier. */
3082 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3084 /* Grab the nested-name-specifier and continue the loop. */
3085 cp_parser_pre_parsed_nested_name_specifier (parser
);
3090 /* Spot cases that cannot be the beginning of a
3091 nested-name-specifier. On the second and subsequent times
3092 through the loop, we look for the `template' keyword. */
3093 if (success
&& token
->keyword
== RID_TEMPLATE
)
3095 /* A template-id can start a nested-name-specifier. */
3096 else if (token
->type
== CPP_TEMPLATE_ID
)
3100 /* If the next token is not an identifier, then it is
3101 definitely not a class-or-namespace-name. */
3102 if (token
->type
!= CPP_NAME
)
3104 /* If the following token is neither a `<' (to begin a
3105 template-id), nor a `::', then we are not looking at a
3106 nested-name-specifier. */
3107 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3108 if (token
->type
!= CPP_LESS
&& token
->type
!= CPP_SCOPE
)
3112 /* The nested-name-specifier is optional, so we parse
3114 cp_parser_parse_tentatively (parser
);
3116 /* Look for the optional `template' keyword, if this isn't the
3117 first time through the loop. */
3119 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3121 template_keyword_p
= false;
3123 /* Save the old scope since the name lookup we are about to do
3124 might destroy it. */
3125 old_scope
= parser
->scope
;
3126 saved_qualifying_scope
= parser
->qualifying_scope
;
3127 /* Parse the qualifying entity. */
3129 = cp_parser_class_or_namespace_name (parser
,
3135 /* Look for the `::' token. */
3136 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3138 /* If we found what we wanted, we keep going; otherwise, we're
3140 if (!cp_parser_parse_definitely (parser
))
3142 bool error_p
= false;
3144 /* Restore the OLD_SCOPE since it was valid before the
3145 failed attempt at finding the last
3146 class-or-namespace-name. */
3147 parser
->scope
= old_scope
;
3148 parser
->qualifying_scope
= saved_qualifying_scope
;
3149 /* If the next token is an identifier, and the one after
3150 that is a `::', then any valid interpretation would have
3151 found a class-or-namespace-name. */
3152 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3153 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3155 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3158 token
= cp_lexer_consume_token (parser
->lexer
);
3163 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3164 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3165 error ("`%D' used without template parameters",
3168 cp_parser_name_lookup_error
3169 (parser
, token
->value
, decl
,
3170 "is not a class or namespace");
3171 parser
->scope
= NULL_TREE
;
3173 /* Treat this as a successful nested-name-specifier
3178 If the name found is not a class-name (clause
3179 _class_) or namespace-name (_namespace.def_), the
3180 program is ill-formed. */
3183 cp_lexer_consume_token (parser
->lexer
);
3188 /* We've found one valid nested-name-specifier. */
3190 /* Make sure we look in the right scope the next time through
3192 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3193 ? TREE_TYPE (new_scope
)
3195 /* If it is a class scope, try to complete it; we are about to
3196 be looking up names inside the class. */
3197 if (TYPE_P (parser
->scope
)
3198 /* Since checking types for dependency can be expensive,
3199 avoid doing it if the type is already complete. */
3200 && !COMPLETE_TYPE_P (parser
->scope
)
3201 /* Do not try to complete dependent types. */
3202 && !dependent_type_p (parser
->scope
))
3203 complete_type (parser
->scope
);
3206 /* Retrieve any deferred checks. Do not pop this access checks yet
3207 so the memory will not be reclaimed during token replacing below. */
3208 access_check
= get_deferred_access_checks ();
3210 /* If parsing tentatively, replace the sequence of tokens that makes
3211 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3212 token. That way, should we re-parse the token stream, we will
3213 not have to repeat the effort required to do the parse, nor will
3214 we issue duplicate error messages. */
3215 if (success
&& start
>= 0)
3217 /* Find the token that corresponds to the start of the
3219 token
= cp_lexer_advance_token (parser
->lexer
,
3220 parser
->lexer
->first_token
,
3223 /* Reset the contents of the START token. */
3224 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3225 token
->value
= build_tree_list (access_check
, parser
->scope
);
3226 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3227 token
->keyword
= RID_MAX
;
3228 /* Purge all subsequent tokens. */
3229 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3232 pop_deferring_access_checks ();
3233 return success
? parser
->scope
: NULL_TREE
;
3236 /* Parse a nested-name-specifier. See
3237 cp_parser_nested_name_specifier_opt for details. This function
3238 behaves identically, except that it will an issue an error if no
3239 nested-name-specifier is present, and it will return
3240 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3244 cp_parser_nested_name_specifier (cp_parser
*parser
,
3245 bool typename_keyword_p
,
3246 bool check_dependency_p
,
3248 bool is_declaration
)
3252 /* Look for the nested-name-specifier. */
3253 scope
= cp_parser_nested_name_specifier_opt (parser
,
3258 /* If it was not present, issue an error message. */
3261 cp_parser_error (parser
, "expected nested-name-specifier");
3262 parser
->scope
= NULL_TREE
;
3263 return error_mark_node
;
3269 /* Parse a class-or-namespace-name.
3271 class-or-namespace-name:
3275 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3276 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3277 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3278 TYPE_P is TRUE iff the next name should be taken as a class-name,
3279 even the same name is declared to be another entity in the same
3282 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3283 specified by the class-or-namespace-name. If neither is found the
3284 ERROR_MARK_NODE is returned. */
3287 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3288 bool typename_keyword_p
,
3289 bool template_keyword_p
,
3290 bool check_dependency_p
,
3292 bool is_declaration
)
3295 tree saved_qualifying_scope
;
3296 tree saved_object_scope
;
3300 /* Before we try to parse the class-name, we must save away the
3301 current PARSER->SCOPE since cp_parser_class_name will destroy
3303 saved_scope
= parser
->scope
;
3304 saved_qualifying_scope
= parser
->qualifying_scope
;
3305 saved_object_scope
= parser
->object_scope
;
3306 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3307 there is no need to look for a namespace-name. */
3308 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3310 cp_parser_parse_tentatively (parser
);
3311 scope
= cp_parser_class_name (parser
,
3316 /*class_head_p=*/false,
3318 /* If that didn't work, try for a namespace-name. */
3319 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3321 /* Restore the saved scope. */
3322 parser
->scope
= saved_scope
;
3323 parser
->qualifying_scope
= saved_qualifying_scope
;
3324 parser
->object_scope
= saved_object_scope
;
3325 /* If we are not looking at an identifier followed by the scope
3326 resolution operator, then this is not part of a
3327 nested-name-specifier. (Note that this function is only used
3328 to parse the components of a nested-name-specifier.) */
3329 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3330 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3331 return error_mark_node
;
3332 scope
= cp_parser_namespace_name (parser
);
3338 /* Parse a postfix-expression.
3342 postfix-expression [ expression ]
3343 postfix-expression ( expression-list [opt] )
3344 simple-type-specifier ( expression-list [opt] )
3345 typename :: [opt] nested-name-specifier identifier
3346 ( expression-list [opt] )
3347 typename :: [opt] nested-name-specifier template [opt] template-id
3348 ( expression-list [opt] )
3349 postfix-expression . template [opt] id-expression
3350 postfix-expression -> template [opt] id-expression
3351 postfix-expression . pseudo-destructor-name
3352 postfix-expression -> pseudo-destructor-name
3353 postfix-expression ++
3354 postfix-expression --
3355 dynamic_cast < type-id > ( expression )
3356 static_cast < type-id > ( expression )
3357 reinterpret_cast < type-id > ( expression )
3358 const_cast < type-id > ( expression )
3359 typeid ( expression )
3365 ( type-id ) { initializer-list , [opt] }
3367 This extension is a GNU version of the C99 compound-literal
3368 construct. (The C99 grammar uses `type-name' instead of `type-id',
3369 but they are essentially the same concept.)
3371 If ADDRESS_P is true, the postfix expression is the operand of the
3374 Returns a representation of the expression. */
3377 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3381 cp_id_kind idk
= CP_ID_KIND_NONE
;
3382 tree postfix_expression
= NULL_TREE
;
3383 /* Non-NULL only if the current postfix-expression can be used to
3384 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3385 class used to qualify the member. */
3386 tree qualifying_class
= NULL_TREE
;
3388 /* Peek at the next token. */
3389 token
= cp_lexer_peek_token (parser
->lexer
);
3390 /* Some of the productions are determined by keywords. */
3391 keyword
= token
->keyword
;
3401 const char *saved_message
;
3403 /* All of these can be handled in the same way from the point
3404 of view of parsing. Begin by consuming the token
3405 identifying the cast. */
3406 cp_lexer_consume_token (parser
->lexer
);
3408 /* New types cannot be defined in the cast. */
3409 saved_message
= parser
->type_definition_forbidden_message
;
3410 parser
->type_definition_forbidden_message
3411 = "types may not be defined in casts";
3413 /* Look for the opening `<'. */
3414 cp_parser_require (parser
, CPP_LESS
, "`<'");
3415 /* Parse the type to which we are casting. */
3416 type
= cp_parser_type_id (parser
);
3417 /* Look for the closing `>'. */
3418 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3419 /* Restore the old message. */
3420 parser
->type_definition_forbidden_message
= saved_message
;
3422 /* And the expression which is being cast. */
3423 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3424 expression
= cp_parser_expression (parser
);
3425 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3427 /* Only type conversions to integral or enumeration types
3428 can be used in constant-expressions. */
3429 if (parser
->integral_constant_expression_p
3430 && !dependent_type_p (type
)
3431 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3432 /* A cast to pointer or reference type is allowed in the
3433 implementation of "offsetof". */
3434 && !(parser
->in_offsetof_p
&& POINTER_TYPE_P (type
)))
3436 if (!parser
->allow_non_integral_constant_expression_p
)
3437 return (cp_parser_non_integral_constant_expression
3438 ("a cast to a type other than an integral or "
3439 "enumeration type"));
3440 parser
->non_integral_constant_expression_p
= true;
3447 = build_dynamic_cast (type
, expression
);
3451 = build_static_cast (type
, expression
);
3455 = build_reinterpret_cast (type
, expression
);
3459 = build_const_cast (type
, expression
);
3470 const char *saved_message
;
3472 /* Consume the `typeid' token. */
3473 cp_lexer_consume_token (parser
->lexer
);
3474 /* Look for the `(' token. */
3475 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3476 /* Types cannot be defined in a `typeid' expression. */
3477 saved_message
= parser
->type_definition_forbidden_message
;
3478 parser
->type_definition_forbidden_message
3479 = "types may not be defined in a `typeid\' expression";
3480 /* We can't be sure yet whether we're looking at a type-id or an
3482 cp_parser_parse_tentatively (parser
);
3483 /* Try a type-id first. */
3484 type
= cp_parser_type_id (parser
);
3485 /* Look for the `)' token. Otherwise, we can't be sure that
3486 we're not looking at an expression: consider `typeid (int
3487 (3))', for example. */
3488 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3489 /* If all went well, simply lookup the type-id. */
3490 if (cp_parser_parse_definitely (parser
))
3491 postfix_expression
= get_typeid (type
);
3492 /* Otherwise, fall back to the expression variant. */
3497 /* Look for an expression. */
3498 expression
= cp_parser_expression (parser
);
3499 /* Compute its typeid. */
3500 postfix_expression
= build_typeid (expression
);
3501 /* Look for the `)' token. */
3502 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3505 /* Restore the saved message. */
3506 parser
->type_definition_forbidden_message
= saved_message
;
3512 bool template_p
= false;
3516 /* Consume the `typename' token. */
3517 cp_lexer_consume_token (parser
->lexer
);
3518 /* Look for the optional `::' operator. */
3519 cp_parser_global_scope_opt (parser
,
3520 /*current_scope_valid_p=*/false);
3521 /* Look for the nested-name-specifier. */
3522 cp_parser_nested_name_specifier (parser
,
3523 /*typename_keyword_p=*/true,
3524 /*check_dependency_p=*/true,
3526 /*is_declaration=*/true);
3527 /* Look for the optional `template' keyword. */
3528 template_p
= cp_parser_optional_template_keyword (parser
);
3529 /* We don't know whether we're looking at a template-id or an
3531 cp_parser_parse_tentatively (parser
);
3532 /* Try a template-id. */
3533 id
= cp_parser_template_id (parser
, template_p
,
3534 /*check_dependency_p=*/true,
3535 /*is_declaration=*/true);
3536 /* If that didn't work, try an identifier. */
3537 if (!cp_parser_parse_definitely (parser
))
3538 id
= cp_parser_identifier (parser
);
3539 /* Create a TYPENAME_TYPE to represent the type to which the
3540 functional cast is being performed. */
3541 type
= make_typename_type (parser
->scope
, id
,
3544 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3552 /* If the next thing is a simple-type-specifier, we may be
3553 looking at a functional cast. We could also be looking at
3554 an id-expression. So, we try the functional cast, and if
3555 that doesn't work we fall back to the primary-expression. */
3556 cp_parser_parse_tentatively (parser
);
3557 /* Look for the simple-type-specifier. */
3558 type
= cp_parser_simple_type_specifier (parser
,
3559 CP_PARSER_FLAGS_NONE
,
3560 /*identifier_p=*/false);
3561 /* Parse the cast itself. */
3562 if (!cp_parser_error_occurred (parser
))
3564 = cp_parser_functional_cast (parser
, type
);
3565 /* If that worked, we're done. */
3566 if (cp_parser_parse_definitely (parser
))
3569 /* If the functional-cast didn't work out, try a
3570 compound-literal. */
3571 if (cp_parser_allow_gnu_extensions_p (parser
)
3572 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3574 tree initializer_list
= NULL_TREE
;
3576 cp_parser_parse_tentatively (parser
);
3577 /* Consume the `('. */
3578 cp_lexer_consume_token (parser
->lexer
);
3579 /* Parse the type. */
3580 type
= cp_parser_type_id (parser
);
3581 /* Look for the `)'. */
3582 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3583 /* Look for the `{'. */
3584 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3585 /* If things aren't going well, there's no need to
3587 if (!cp_parser_error_occurred (parser
))
3589 bool non_constant_p
;
3590 /* Parse the initializer-list. */
3592 = cp_parser_initializer_list (parser
, &non_constant_p
);
3593 /* Allow a trailing `,'. */
3594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3595 cp_lexer_consume_token (parser
->lexer
);
3596 /* Look for the final `}'. */
3597 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3599 /* If that worked, we're definitely looking at a
3600 compound-literal expression. */
3601 if (cp_parser_parse_definitely (parser
))
3603 /* Warn the user that a compound literal is not
3604 allowed in standard C++. */
3606 pedwarn ("ISO C++ forbids compound-literals");
3607 /* Form the representation of the compound-literal. */
3609 = finish_compound_literal (type
, initializer_list
);
3614 /* It must be a primary-expression. */
3615 postfix_expression
= cp_parser_primary_expression (parser
,
3622 /* If we were avoiding committing to the processing of a
3623 qualified-id until we knew whether or not we had a
3624 pointer-to-member, we now know. */
3625 if (qualifying_class
)
3629 /* Peek at the next token. */
3630 token
= cp_lexer_peek_token (parser
->lexer
);
3631 done
= (token
->type
!= CPP_OPEN_SQUARE
3632 && token
->type
!= CPP_OPEN_PAREN
3633 && token
->type
!= CPP_DOT
3634 && token
->type
!= CPP_DEREF
3635 && token
->type
!= CPP_PLUS_PLUS
3636 && token
->type
!= CPP_MINUS_MINUS
);
3638 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3643 return postfix_expression
;
3646 /* Keep looping until the postfix-expression is complete. */
3649 if (idk
== CP_ID_KIND_UNQUALIFIED
3650 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3651 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3652 /* It is not a Koenig lookup function call. */
3654 = unqualified_name_lookup_error (postfix_expression
);
3656 /* Peek at the next token. */
3657 token
= cp_lexer_peek_token (parser
->lexer
);
3659 switch (token
->type
)
3661 case CPP_OPEN_SQUARE
:
3662 /* postfix-expression [ expression ] */
3666 /* Consume the `[' token. */
3667 cp_lexer_consume_token (parser
->lexer
);
3668 /* Parse the index expression. */
3669 index
= cp_parser_expression (parser
);
3670 /* Look for the closing `]'. */
3671 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
3673 /* Build the ARRAY_REF. */
3675 = grok_array_decl (postfix_expression
, index
);
3676 idk
= CP_ID_KIND_NONE
;
3677 /* Array references are not permitted in
3678 constant-expressions. */
3679 if (parser
->integral_constant_expression_p
)
3681 if (!parser
->allow_non_integral_constant_expression_p
)
3683 = cp_parser_non_integral_constant_expression ("an array reference");
3684 parser
->non_integral_constant_expression_p
= true;
3689 case CPP_OPEN_PAREN
:
3690 /* postfix-expression ( expression-list [opt] ) */
3693 tree args
= (cp_parser_parenthesized_expression_list
3694 (parser
, false, /*non_constant_p=*/NULL
));
3696 if (args
== error_mark_node
)
3698 postfix_expression
= error_mark_node
;
3702 /* Function calls are not permitted in
3703 constant-expressions. */
3704 if (parser
->integral_constant_expression_p
)
3706 if (!parser
->allow_non_integral_constant_expression_p
)
3709 = cp_parser_non_integral_constant_expression ("a function call");
3712 parser
->non_integral_constant_expression_p
= true;
3716 if (idk
== CP_ID_KIND_UNQUALIFIED
)
3719 && (is_overloaded_fn (postfix_expression
)
3720 || DECL_P (postfix_expression
)
3721 || TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
))
3725 = perform_koenig_lookup (postfix_expression
, args
);
3727 else if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3729 = unqualified_fn_lookup_error (postfix_expression
);
3732 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
3734 tree instance
= TREE_OPERAND (postfix_expression
, 0);
3735 tree fn
= TREE_OPERAND (postfix_expression
, 1);
3737 if (processing_template_decl
3738 && (type_dependent_expression_p (instance
)
3739 || (!BASELINK_P (fn
)
3740 && TREE_CODE (fn
) != FIELD_DECL
)
3741 || type_dependent_expression_p (fn
)
3742 || any_type_dependent_arguments_p (args
)))
3745 = build_min_nt (CALL_EXPR
, postfix_expression
, args
);
3750 = (build_new_method_call
3751 (instance
, fn
, args
, NULL_TREE
,
3752 (idk
== CP_ID_KIND_QUALIFIED
3753 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3755 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
3756 || TREE_CODE (postfix_expression
) == MEMBER_REF
3757 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
3758 postfix_expression
= (build_offset_ref_call_from_tree
3759 (postfix_expression
, args
));
3760 else if (idk
== CP_ID_KIND_QUALIFIED
)
3761 /* A call to a static class member, or a namespace-scope
3764 = finish_call_expr (postfix_expression
, args
,
3765 /*disallow_virtual=*/true,
3768 /* All other function calls. */
3770 = finish_call_expr (postfix_expression
, args
,
3771 /*disallow_virtual=*/false,
3774 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3775 idk
= CP_ID_KIND_NONE
;
3781 /* postfix-expression . template [opt] id-expression
3782 postfix-expression . pseudo-destructor-name
3783 postfix-expression -> template [opt] id-expression
3784 postfix-expression -> pseudo-destructor-name */
3789 tree scope
= NULL_TREE
;
3790 enum cpp_ttype token_type
= token
->type
;
3792 /* If this is a `->' operator, dereference the pointer. */
3793 if (token
->type
== CPP_DEREF
)
3794 postfix_expression
= build_x_arrow (postfix_expression
);
3795 /* Check to see whether or not the expression is
3797 dependent_p
= type_dependent_expression_p (postfix_expression
);
3798 /* The identifier following the `->' or `.' is not
3800 parser
->scope
= NULL_TREE
;
3801 parser
->qualifying_scope
= NULL_TREE
;
3802 parser
->object_scope
= NULL_TREE
;
3803 idk
= CP_ID_KIND_NONE
;
3804 /* Enter the scope corresponding to the type of the object
3805 given by the POSTFIX_EXPRESSION. */
3807 && TREE_TYPE (postfix_expression
) != NULL_TREE
)
3809 scope
= TREE_TYPE (postfix_expression
);
3810 /* According to the standard, no expression should
3811 ever have reference type. Unfortunately, we do not
3812 currently match the standard in this respect in
3813 that our internal representation of an expression
3814 may have reference type even when the standard says
3815 it does not. Therefore, we have to manually obtain
3816 the underlying type here. */
3817 scope
= non_reference (scope
);
3818 /* The type of the POSTFIX_EXPRESSION must be
3820 scope
= complete_type_or_else (scope
, NULL_TREE
);
3821 /* Let the name lookup machinery know that we are
3822 processing a class member access expression. */
3823 parser
->context
->object_type
= scope
;
3824 /* If something went wrong, we want to be able to
3825 discern that case, as opposed to the case where
3826 there was no SCOPE due to the type of expression
3829 scope
= error_mark_node
;
3832 /* Consume the `.' or `->' operator. */
3833 cp_lexer_consume_token (parser
->lexer
);
3834 /* If the SCOPE is not a scalar type, we are looking at an
3835 ordinary class member access expression, rather than a
3836 pseudo-destructor-name. */
3837 if (!scope
|| !SCALAR_TYPE_P (scope
))
3839 template_p
= cp_parser_optional_template_keyword (parser
);
3840 /* Parse the id-expression. */
3841 name
= cp_parser_id_expression (parser
,
3843 /*check_dependency_p=*/true,
3844 /*template_p=*/NULL
,
3845 /*declarator_p=*/false);
3846 /* In general, build a SCOPE_REF if the member name is
3847 qualified. However, if the name was not dependent
3848 and has already been resolved; there is no need to
3849 build the SCOPE_REF. For example;
3851 struct X { void f(); };
3852 template <typename T> void f(T* t) { t->X::f(); }
3854 Even though "t" is dependent, "X::f" is not and has
3855 been resolved to a BASELINK; there is no need to
3856 include scope information. */
3858 /* But we do need to remember that there was an explicit
3859 scope for virtual function calls. */
3861 idk
= CP_ID_KIND_QUALIFIED
;
3863 if (name
!= error_mark_node
3864 && !BASELINK_P (name
)
3867 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
3868 parser
->scope
= NULL_TREE
;
3869 parser
->qualifying_scope
= NULL_TREE
;
3870 parser
->object_scope
= NULL_TREE
;
3873 = finish_class_member_access_expr (postfix_expression
, name
);
3875 /* Otherwise, try the pseudo-destructor-name production. */
3881 /* Parse the pseudo-destructor-name. */
3882 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
3883 /* Form the call. */
3885 = finish_pseudo_destructor_expr (postfix_expression
,
3886 s
, TREE_TYPE (type
));
3889 /* We no longer need to look up names in the scope of the
3890 object on the left-hand side of the `.' or `->'
3892 parser
->context
->object_type
= NULL_TREE
;
3893 /* These operators may not appear in constant-expressions. */
3894 if (parser
->integral_constant_expression_p
3895 /* The "->" operator is allowed in the implementation
3897 && !(parser
->in_offsetof_p
&& token_type
== CPP_DEREF
))
3899 if (!parser
->allow_non_integral_constant_expression_p
)
3901 = (cp_parser_non_integral_constant_expression
3902 (token_type
== CPP_DEREF
? "'->'" : "`.'"));
3903 parser
->non_integral_constant_expression_p
= true;
3909 /* postfix-expression ++ */
3910 /* Consume the `++' token. */
3911 cp_lexer_consume_token (parser
->lexer
);
3912 /* Generate a representation for the complete expression. */
3914 = finish_increment_expr (postfix_expression
,
3915 POSTINCREMENT_EXPR
);
3916 /* Increments may not appear in constant-expressions. */
3917 if (parser
->integral_constant_expression_p
)
3919 if (!parser
->allow_non_integral_constant_expression_p
)
3921 = cp_parser_non_integral_constant_expression ("an increment");
3922 parser
->non_integral_constant_expression_p
= true;
3924 idk
= CP_ID_KIND_NONE
;
3927 case CPP_MINUS_MINUS
:
3928 /* postfix-expression -- */
3929 /* Consume the `--' token. */
3930 cp_lexer_consume_token (parser
->lexer
);
3931 /* Generate a representation for the complete expression. */
3933 = finish_increment_expr (postfix_expression
,
3934 POSTDECREMENT_EXPR
);
3935 /* Decrements may not appear in constant-expressions. */
3936 if (parser
->integral_constant_expression_p
)
3938 if (!parser
->allow_non_integral_constant_expression_p
)
3940 = cp_parser_non_integral_constant_expression ("a decrement");
3941 parser
->non_integral_constant_expression_p
= true;
3943 idk
= CP_ID_KIND_NONE
;
3947 return postfix_expression
;
3951 /* We should never get here. */
3953 return error_mark_node
;
3956 /* Parse a parenthesized expression-list.
3959 assignment-expression
3960 expression-list, assignment-expression
3965 identifier, expression-list
3967 Returns a TREE_LIST. The TREE_VALUE of each node is a
3968 representation of an assignment-expression. Note that a TREE_LIST
3969 is returned even if there is only a single expression in the list.
3970 error_mark_node is returned if the ( and or ) are
3971 missing. NULL_TREE is returned on no expressions. The parentheses
3972 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3973 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3974 indicates whether or not all of the expressions in the list were
3978 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
3979 bool is_attribute_list
,
3980 bool *non_constant_p
)
3982 tree expression_list
= NULL_TREE
;
3983 tree identifier
= NULL_TREE
;
3985 /* Assume all the expressions will be constant. */
3987 *non_constant_p
= false;
3989 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
3990 return error_mark_node
;
3992 /* Consume expressions until there are no more. */
3993 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
3998 /* At the beginning of attribute lists, check to see if the
3999 next token is an identifier. */
4000 if (is_attribute_list
4001 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4005 /* Consume the identifier. */
4006 token
= cp_lexer_consume_token (parser
->lexer
);
4007 /* Save the identifier. */
4008 identifier
= token
->value
;
4012 /* Parse the next assignment-expression. */
4015 bool expr_non_constant_p
;
4016 expr
= (cp_parser_constant_expression
4017 (parser
, /*allow_non_constant_p=*/true,
4018 &expr_non_constant_p
));
4019 if (expr_non_constant_p
)
4020 *non_constant_p
= true;
4023 expr
= cp_parser_assignment_expression (parser
);
4025 /* Add it to the list. We add error_mark_node
4026 expressions to the list, so that we can still tell if
4027 the correct form for a parenthesized expression-list
4028 is found. That gives better errors. */
4029 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4031 if (expr
== error_mark_node
)
4035 /* After the first item, attribute lists look the same as
4036 expression lists. */
4037 is_attribute_list
= false;
4040 /* If the next token isn't a `,', then we are done. */
4041 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4044 /* Otherwise, consume the `,' and keep going. */
4045 cp_lexer_consume_token (parser
->lexer
);
4048 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4053 /* We try and resync to an unnested comma, as that will give the
4054 user better diagnostics. */
4055 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4056 /*recovering=*/true,
4058 /*consume_paren=*/true);
4062 return error_mark_node
;
4065 /* We built up the list in reverse order so we must reverse it now. */
4066 expression_list
= nreverse (expression_list
);
4068 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4070 return expression_list
;
4073 /* Parse a pseudo-destructor-name.
4075 pseudo-destructor-name:
4076 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4077 :: [opt] nested-name-specifier template template-id :: ~ type-name
4078 :: [opt] nested-name-specifier [opt] ~ type-name
4080 If either of the first two productions is used, sets *SCOPE to the
4081 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4082 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4083 or ERROR_MARK_NODE if no type-name is present. */
4086 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4090 bool nested_name_specifier_p
;
4092 /* Look for the optional `::' operator. */
4093 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4094 /* Look for the optional nested-name-specifier. */
4095 nested_name_specifier_p
4096 = (cp_parser_nested_name_specifier_opt (parser
,
4097 /*typename_keyword_p=*/false,
4098 /*check_dependency_p=*/true,
4100 /*is_declaration=*/true)
4102 /* Now, if we saw a nested-name-specifier, we might be doing the
4103 second production. */
4104 if (nested_name_specifier_p
4105 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4107 /* Consume the `template' keyword. */
4108 cp_lexer_consume_token (parser
->lexer
);
4109 /* Parse the template-id. */
4110 cp_parser_template_id (parser
,
4111 /*template_keyword_p=*/true,
4112 /*check_dependency_p=*/false,
4113 /*is_declaration=*/true);
4114 /* Look for the `::' token. */
4115 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4117 /* If the next token is not a `~', then there might be some
4118 additional qualification. */
4119 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4121 /* Look for the type-name. */
4122 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4123 /* Look for the `::' token. */
4124 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4129 /* Look for the `~'. */
4130 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4131 /* Look for the type-name again. We are not responsible for
4132 checking that it matches the first type-name. */
4133 *type
= cp_parser_type_name (parser
);
4136 /* Parse a unary-expression.
4142 unary-operator cast-expression
4143 sizeof unary-expression
4151 __extension__ cast-expression
4152 __alignof__ unary-expression
4153 __alignof__ ( type-id )
4154 __real__ cast-expression
4155 __imag__ cast-expression
4158 ADDRESS_P is true iff the unary-expression is appearing as the
4159 operand of the `&' operator.
4161 Returns a representation of the expression. */
4164 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4167 enum tree_code unary_operator
;
4169 /* Peek at the next token. */
4170 token
= cp_lexer_peek_token (parser
->lexer
);
4171 /* Some keywords give away the kind of expression. */
4172 if (token
->type
== CPP_KEYWORD
)
4174 enum rid keyword
= token
->keyword
;
4184 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4185 /* Consume the token. */
4186 cp_lexer_consume_token (parser
->lexer
);
4187 /* Parse the operand. */
4188 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4190 if (TYPE_P (operand
))
4191 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4193 return cxx_sizeof_or_alignof_expr (operand
, op
);
4197 return cp_parser_new_expression (parser
);
4200 return cp_parser_delete_expression (parser
);
4204 /* The saved value of the PEDANTIC flag. */
4208 /* Save away the PEDANTIC flag. */
4209 cp_parser_extension_opt (parser
, &saved_pedantic
);
4210 /* Parse the cast-expression. */
4211 expr
= cp_parser_simple_cast_expression (parser
);
4212 /* Restore the PEDANTIC flag. */
4213 pedantic
= saved_pedantic
;
4223 /* Consume the `__real__' or `__imag__' token. */
4224 cp_lexer_consume_token (parser
->lexer
);
4225 /* Parse the cast-expression. */
4226 expression
= cp_parser_simple_cast_expression (parser
);
4227 /* Create the complete representation. */
4228 return build_x_unary_op ((keyword
== RID_REALPART
4229 ? REALPART_EXPR
: IMAGPART_EXPR
),
4239 /* Look for the `:: new' and `:: delete', which also signal the
4240 beginning of a new-expression, or delete-expression,
4241 respectively. If the next token is `::', then it might be one of
4243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4247 /* See if the token after the `::' is one of the keywords in
4248 which we're interested. */
4249 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4250 /* If it's `new', we have a new-expression. */
4251 if (keyword
== RID_NEW
)
4252 return cp_parser_new_expression (parser
);
4253 /* Similarly, for `delete'. */
4254 else if (keyword
== RID_DELETE
)
4255 return cp_parser_delete_expression (parser
);
4258 /* Look for a unary operator. */
4259 unary_operator
= cp_parser_unary_operator (token
);
4260 /* The `++' and `--' operators can be handled similarly, even though
4261 they are not technically unary-operators in the grammar. */
4262 if (unary_operator
== ERROR_MARK
)
4264 if (token
->type
== CPP_PLUS_PLUS
)
4265 unary_operator
= PREINCREMENT_EXPR
;
4266 else if (token
->type
== CPP_MINUS_MINUS
)
4267 unary_operator
= PREDECREMENT_EXPR
;
4268 /* Handle the GNU address-of-label extension. */
4269 else if (cp_parser_allow_gnu_extensions_p (parser
)
4270 && token
->type
== CPP_AND_AND
)
4274 /* Consume the '&&' token. */
4275 cp_lexer_consume_token (parser
->lexer
);
4276 /* Look for the identifier. */
4277 identifier
= cp_parser_identifier (parser
);
4278 /* Create an expression representing the address. */
4279 return finish_label_address_expr (identifier
);
4282 if (unary_operator
!= ERROR_MARK
)
4284 tree cast_expression
;
4285 tree expression
= error_mark_node
;
4286 const char *non_constant_p
= NULL
;
4288 /* Consume the operator token. */
4289 token
= cp_lexer_consume_token (parser
->lexer
);
4290 /* Parse the cast-expression. */
4292 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4293 /* Now, build an appropriate representation. */
4294 switch (unary_operator
)
4297 non_constant_p
= "`*'";
4298 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4302 /* The "&" operator is allowed in the implementation of
4304 if (!parser
->in_offsetof_p
)
4305 non_constant_p
= "`&'";
4308 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4311 case PREINCREMENT_EXPR
:
4312 case PREDECREMENT_EXPR
:
4313 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4318 case TRUTH_NOT_EXPR
:
4319 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4326 if (non_constant_p
&& parser
->integral_constant_expression_p
)
4328 if (!parser
->allow_non_integral_constant_expression_p
)
4329 return cp_parser_non_integral_constant_expression (non_constant_p
);
4330 parser
->non_integral_constant_expression_p
= true;
4336 return cp_parser_postfix_expression (parser
, address_p
);
4339 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4340 unary-operator, the corresponding tree code is returned. */
4342 static enum tree_code
4343 cp_parser_unary_operator (cp_token
* token
)
4345 switch (token
->type
)
4348 return INDIRECT_REF
;
4354 return CONVERT_EXPR
;
4360 return TRUTH_NOT_EXPR
;
4363 return BIT_NOT_EXPR
;
4370 /* Parse a new-expression.
4373 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4374 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4376 Returns a representation of the expression. */
4379 cp_parser_new_expression (cp_parser
* parser
)
4381 bool global_scope_p
;
4386 /* Look for the optional `::' operator. */
4388 = (cp_parser_global_scope_opt (parser
,
4389 /*current_scope_valid_p=*/false)
4391 /* Look for the `new' operator. */
4392 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4393 /* There's no easy way to tell a new-placement from the
4394 `( type-id )' construct. */
4395 cp_parser_parse_tentatively (parser
);
4396 /* Look for a new-placement. */
4397 placement
= cp_parser_new_placement (parser
);
4398 /* If that didn't work out, there's no new-placement. */
4399 if (!cp_parser_parse_definitely (parser
))
4400 placement
= NULL_TREE
;
4402 /* If the next token is a `(', then we have a parenthesized
4404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4406 /* Consume the `('. */
4407 cp_lexer_consume_token (parser
->lexer
);
4408 /* Parse the type-id. */
4409 type
= cp_parser_type_id (parser
);
4410 /* Look for the closing `)'. */
4411 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4413 /* Otherwise, there must be a new-type-id. */
4415 type
= cp_parser_new_type_id (parser
);
4417 /* If the next token is a `(', then we have a new-initializer. */
4418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4419 initializer
= cp_parser_new_initializer (parser
);
4421 initializer
= NULL_TREE
;
4423 /* Create a representation of the new-expression. */
4424 return build_new (placement
, type
, initializer
, global_scope_p
);
4427 /* Parse a new-placement.
4432 Returns the same representation as for an expression-list. */
4435 cp_parser_new_placement (cp_parser
* parser
)
4437 tree expression_list
;
4439 /* Parse the expression-list. */
4440 expression_list
= (cp_parser_parenthesized_expression_list
4441 (parser
, false, /*non_constant_p=*/NULL
));
4443 return expression_list
;
4446 /* Parse a new-type-id.
4449 type-specifier-seq new-declarator [opt]
4451 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4452 and whose TREE_VALUE is the new-declarator. */
4455 cp_parser_new_type_id (cp_parser
* parser
)
4457 tree type_specifier_seq
;
4459 const char *saved_message
;
4461 /* The type-specifier sequence must not contain type definitions.
4462 (It cannot contain declarations of new types either, but if they
4463 are not definitions we will catch that because they are not
4465 saved_message
= parser
->type_definition_forbidden_message
;
4466 parser
->type_definition_forbidden_message
4467 = "types may not be defined in a new-type-id";
4468 /* Parse the type-specifier-seq. */
4469 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4470 /* Restore the old message. */
4471 parser
->type_definition_forbidden_message
= saved_message
;
4472 /* Parse the new-declarator. */
4473 declarator
= cp_parser_new_declarator_opt (parser
);
4475 return build_tree_list (type_specifier_seq
, declarator
);
4478 /* Parse an (optional) new-declarator.
4481 ptr-operator new-declarator [opt]
4482 direct-new-declarator
4484 Returns a representation of the declarator. See
4485 cp_parser_declarator for the representations used. */
4488 cp_parser_new_declarator_opt (cp_parser
* parser
)
4490 enum tree_code code
;
4492 tree cv_qualifier_seq
;
4494 /* We don't know if there's a ptr-operator next, or not. */
4495 cp_parser_parse_tentatively (parser
);
4496 /* Look for a ptr-operator. */
4497 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4498 /* If that worked, look for more new-declarators. */
4499 if (cp_parser_parse_definitely (parser
))
4503 /* Parse another optional declarator. */
4504 declarator
= cp_parser_new_declarator_opt (parser
);
4506 /* Create the representation of the declarator. */
4507 if (code
== INDIRECT_REF
)
4508 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4511 declarator
= make_reference_declarator (cv_qualifier_seq
,
4514 /* Handle the pointer-to-member case. */
4516 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4521 /* If the next token is a `[', there is a direct-new-declarator. */
4522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4523 return cp_parser_direct_new_declarator (parser
);
4528 /* Parse a direct-new-declarator.
4530 direct-new-declarator:
4532 direct-new-declarator [constant-expression]
4534 Returns an ARRAY_REF, following the same conventions as are
4535 documented for cp_parser_direct_declarator. */
4538 cp_parser_direct_new_declarator (cp_parser
* parser
)
4540 tree declarator
= NULL_TREE
;
4546 /* Look for the opening `['. */
4547 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4548 /* The first expression is not required to be constant. */
4551 expression
= cp_parser_expression (parser
);
4552 /* The standard requires that the expression have integral
4553 type. DR 74 adds enumeration types. We believe that the
4554 real intent is that these expressions be handled like the
4555 expression in a `switch' condition, which also allows
4556 classes with a single conversion to integral or
4557 enumeration type. */
4558 if (!processing_template_decl
)
4561 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4566 error ("expression in new-declarator must have integral or enumeration type");
4567 expression
= error_mark_node
;
4571 /* But all the other expressions must be. */
4574 = cp_parser_constant_expression (parser
,
4575 /*allow_non_constant=*/false,
4577 /* Look for the closing `]'. */
4578 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4580 /* Add this bound to the declarator. */
4581 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4583 /* If the next token is not a `[', then there are no more
4585 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4592 /* Parse a new-initializer.
4595 ( expression-list [opt] )
4597 Returns a representation of the expression-list. If there is no
4598 expression-list, VOID_ZERO_NODE is returned. */
4601 cp_parser_new_initializer (cp_parser
* parser
)
4603 tree expression_list
;
4605 expression_list
= (cp_parser_parenthesized_expression_list
4606 (parser
, false, /*non_constant_p=*/NULL
));
4607 if (!expression_list
)
4608 expression_list
= void_zero_node
;
4610 return expression_list
;
4613 /* Parse a delete-expression.
4616 :: [opt] delete cast-expression
4617 :: [opt] delete [ ] cast-expression
4619 Returns a representation of the expression. */
4622 cp_parser_delete_expression (cp_parser
* parser
)
4624 bool global_scope_p
;
4628 /* Look for the optional `::' operator. */
4630 = (cp_parser_global_scope_opt (parser
,
4631 /*current_scope_valid_p=*/false)
4633 /* Look for the `delete' keyword. */
4634 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4635 /* See if the array syntax is in use. */
4636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4638 /* Consume the `[' token. */
4639 cp_lexer_consume_token (parser
->lexer
);
4640 /* Look for the `]' token. */
4641 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4642 /* Remember that this is the `[]' construct. */
4648 /* Parse the cast-expression. */
4649 expression
= cp_parser_simple_cast_expression (parser
);
4651 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4654 /* Parse a cast-expression.
4658 ( type-id ) cast-expression
4660 Returns a representation of the expression. */
4663 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4665 /* If it's a `(', then we might be looking at a cast. */
4666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4668 tree type
= NULL_TREE
;
4669 tree expr
= NULL_TREE
;
4670 bool compound_literal_p
;
4671 const char *saved_message
;
4673 /* There's no way to know yet whether or not this is a cast.
4674 For example, `(int (3))' is a unary-expression, while `(int)
4675 3' is a cast. So, we resort to parsing tentatively. */
4676 cp_parser_parse_tentatively (parser
);
4677 /* Types may not be defined in a cast. */
4678 saved_message
= parser
->type_definition_forbidden_message
;
4679 parser
->type_definition_forbidden_message
4680 = "types may not be defined in casts";
4681 /* Consume the `('. */
4682 cp_lexer_consume_token (parser
->lexer
);
4683 /* A very tricky bit is that `(struct S) { 3 }' is a
4684 compound-literal (which we permit in C++ as an extension).
4685 But, that construct is not a cast-expression -- it is a
4686 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4687 is legal; if the compound-literal were a cast-expression,
4688 you'd need an extra set of parentheses.) But, if we parse
4689 the type-id, and it happens to be a class-specifier, then we
4690 will commit to the parse at that point, because we cannot
4691 undo the action that is done when creating a new class. So,
4692 then we cannot back up and do a postfix-expression.
4694 Therefore, we scan ahead to the closing `)', and check to see
4695 if the token after the `)' is a `{'. If so, we are not
4696 looking at a cast-expression.
4698 Save tokens so that we can put them back. */
4699 cp_lexer_save_tokens (parser
->lexer
);
4700 /* Skip tokens until the next token is a closing parenthesis.
4701 If we find the closing `)', and the next token is a `{', then
4702 we are looking at a compound-literal. */
4704 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
4705 /*consume_paren=*/true)
4706 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4707 /* Roll back the tokens we skipped. */
4708 cp_lexer_rollback_tokens (parser
->lexer
);
4709 /* If we were looking at a compound-literal, simulate an error
4710 so that the call to cp_parser_parse_definitely below will
4712 if (compound_literal_p
)
4713 cp_parser_simulate_error (parser
);
4716 /* Look for the type-id. */
4717 type
= cp_parser_type_id (parser
);
4718 /* Look for the closing `)'. */
4719 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4722 /* Restore the saved message. */
4723 parser
->type_definition_forbidden_message
= saved_message
;
4725 /* If ok so far, parse the dependent expression. We cannot be
4726 sure it is a cast. Consider `(T ())'. It is a parenthesized
4727 ctor of T, but looks like a cast to function returning T
4728 without a dependent expression. */
4729 if (!cp_parser_error_occurred (parser
))
4730 expr
= cp_parser_simple_cast_expression (parser
);
4732 if (cp_parser_parse_definitely (parser
))
4734 /* Warn about old-style casts, if so requested. */
4735 if (warn_old_style_cast
4736 && !in_system_header
4737 && !VOID_TYPE_P (type
)
4738 && current_lang_name
!= lang_name_c
)
4739 warning ("use of old-style cast");
4741 /* Only type conversions to integral or enumeration types
4742 can be used in constant-expressions. */
4743 if (parser
->integral_constant_expression_p
4744 && !dependent_type_p (type
)
4745 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4747 if (!parser
->allow_non_integral_constant_expression_p
)
4748 return (cp_parser_non_integral_constant_expression
4749 ("a casts to a type other than an integral or "
4750 "enumeration type"));
4751 parser
->non_integral_constant_expression_p
= true;
4753 /* Perform the cast. */
4754 expr
= build_c_cast (type
, expr
);
4759 /* If we get here, then it's not a cast, so it must be a
4760 unary-expression. */
4761 return cp_parser_unary_expression (parser
, address_p
);
4764 /* Parse a pm-expression.
4768 pm-expression .* cast-expression
4769 pm-expression ->* cast-expression
4771 Returns a representation of the expression. */
4774 cp_parser_pm_expression (cp_parser
* parser
)
4776 static const cp_parser_token_tree_map map
= {
4777 { CPP_DEREF_STAR
, MEMBER_REF
},
4778 { CPP_DOT_STAR
, DOTSTAR_EXPR
},
4779 { CPP_EOF
, ERROR_MARK
}
4782 return cp_parser_binary_expression (parser
, map
,
4783 cp_parser_simple_cast_expression
);
4786 /* Parse a multiplicative-expression.
4788 mulitplicative-expression:
4790 multiplicative-expression * pm-expression
4791 multiplicative-expression / pm-expression
4792 multiplicative-expression % pm-expression
4794 Returns a representation of the expression. */
4797 cp_parser_multiplicative_expression (cp_parser
* parser
)
4799 static const cp_parser_token_tree_map map
= {
4800 { CPP_MULT
, MULT_EXPR
},
4801 { CPP_DIV
, TRUNC_DIV_EXPR
},
4802 { CPP_MOD
, TRUNC_MOD_EXPR
},
4803 { CPP_EOF
, ERROR_MARK
}
4806 return cp_parser_binary_expression (parser
,
4808 cp_parser_pm_expression
);
4811 /* Parse an additive-expression.
4813 additive-expression:
4814 multiplicative-expression
4815 additive-expression + multiplicative-expression
4816 additive-expression - multiplicative-expression
4818 Returns a representation of the expression. */
4821 cp_parser_additive_expression (cp_parser
* parser
)
4823 static const cp_parser_token_tree_map map
= {
4824 { CPP_PLUS
, PLUS_EXPR
},
4825 { CPP_MINUS
, MINUS_EXPR
},
4826 { CPP_EOF
, ERROR_MARK
}
4829 return cp_parser_binary_expression (parser
,
4831 cp_parser_multiplicative_expression
);
4834 /* Parse a shift-expression.
4838 shift-expression << additive-expression
4839 shift-expression >> additive-expression
4841 Returns a representation of the expression. */
4844 cp_parser_shift_expression (cp_parser
* parser
)
4846 static const cp_parser_token_tree_map map
= {
4847 { CPP_LSHIFT
, LSHIFT_EXPR
},
4848 { CPP_RSHIFT
, RSHIFT_EXPR
},
4849 { CPP_EOF
, ERROR_MARK
}
4852 return cp_parser_binary_expression (parser
,
4854 cp_parser_additive_expression
);
4857 /* Parse a relational-expression.
4859 relational-expression:
4861 relational-expression < shift-expression
4862 relational-expression > shift-expression
4863 relational-expression <= shift-expression
4864 relational-expression >= shift-expression
4868 relational-expression:
4869 relational-expression <? shift-expression
4870 relational-expression >? shift-expression
4872 Returns a representation of the expression. */
4875 cp_parser_relational_expression (cp_parser
* parser
)
4877 static const cp_parser_token_tree_map map
= {
4878 { CPP_LESS
, LT_EXPR
},
4879 { CPP_GREATER
, GT_EXPR
},
4880 { CPP_LESS_EQ
, LE_EXPR
},
4881 { CPP_GREATER_EQ
, GE_EXPR
},
4882 { CPP_MIN
, MIN_EXPR
},
4883 { CPP_MAX
, MAX_EXPR
},
4884 { CPP_EOF
, ERROR_MARK
}
4887 return cp_parser_binary_expression (parser
,
4889 cp_parser_shift_expression
);
4892 /* Parse an equality-expression.
4894 equality-expression:
4895 relational-expression
4896 equality-expression == relational-expression
4897 equality-expression != relational-expression
4899 Returns a representation of the expression. */
4902 cp_parser_equality_expression (cp_parser
* parser
)
4904 static const cp_parser_token_tree_map map
= {
4905 { CPP_EQ_EQ
, EQ_EXPR
},
4906 { CPP_NOT_EQ
, NE_EXPR
},
4907 { CPP_EOF
, ERROR_MARK
}
4910 return cp_parser_binary_expression (parser
,
4912 cp_parser_relational_expression
);
4915 /* Parse an and-expression.
4919 and-expression & equality-expression
4921 Returns a representation of the expression. */
4924 cp_parser_and_expression (cp_parser
* parser
)
4926 static const cp_parser_token_tree_map map
= {
4927 { CPP_AND
, BIT_AND_EXPR
},
4928 { CPP_EOF
, ERROR_MARK
}
4931 return cp_parser_binary_expression (parser
,
4933 cp_parser_equality_expression
);
4936 /* Parse an exclusive-or-expression.
4938 exclusive-or-expression:
4940 exclusive-or-expression ^ and-expression
4942 Returns a representation of the expression. */
4945 cp_parser_exclusive_or_expression (cp_parser
* parser
)
4947 static const cp_parser_token_tree_map map
= {
4948 { CPP_XOR
, BIT_XOR_EXPR
},
4949 { CPP_EOF
, ERROR_MARK
}
4952 return cp_parser_binary_expression (parser
,
4954 cp_parser_and_expression
);
4958 /* Parse an inclusive-or-expression.
4960 inclusive-or-expression:
4961 exclusive-or-expression
4962 inclusive-or-expression | exclusive-or-expression
4964 Returns a representation of the expression. */
4967 cp_parser_inclusive_or_expression (cp_parser
* parser
)
4969 static const cp_parser_token_tree_map map
= {
4970 { CPP_OR
, BIT_IOR_EXPR
},
4971 { CPP_EOF
, ERROR_MARK
}
4974 return cp_parser_binary_expression (parser
,
4976 cp_parser_exclusive_or_expression
);
4979 /* Parse a logical-and-expression.
4981 logical-and-expression:
4982 inclusive-or-expression
4983 logical-and-expression && inclusive-or-expression
4985 Returns a representation of the expression. */
4988 cp_parser_logical_and_expression (cp_parser
* parser
)
4990 static const cp_parser_token_tree_map map
= {
4991 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
4992 { CPP_EOF
, ERROR_MARK
}
4995 return cp_parser_binary_expression (parser
,
4997 cp_parser_inclusive_or_expression
);
5000 /* Parse a logical-or-expression.
5002 logical-or-expression:
5003 logical-and-expression
5004 logical-or-expression || logical-and-expression
5006 Returns a representation of the expression. */
5009 cp_parser_logical_or_expression (cp_parser
* parser
)
5011 static const cp_parser_token_tree_map map
= {
5012 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
5013 { CPP_EOF
, ERROR_MARK
}
5016 return cp_parser_binary_expression (parser
,
5018 cp_parser_logical_and_expression
);
5021 /* Parse the `? expression : assignment-expression' part of a
5022 conditional-expression. The LOGICAL_OR_EXPR is the
5023 logical-or-expression that started the conditional-expression.
5024 Returns a representation of the entire conditional-expression.
5026 This routine is used by cp_parser_assignment_expression.
5028 ? expression : assignment-expression
5032 ? : assignment-expression */
5035 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5038 tree assignment_expr
;
5040 /* Consume the `?' token. */
5041 cp_lexer_consume_token (parser
->lexer
);
5042 if (cp_parser_allow_gnu_extensions_p (parser
)
5043 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5044 /* Implicit true clause. */
5047 /* Parse the expression. */
5048 expr
= cp_parser_expression (parser
);
5050 /* The next token should be a `:'. */
5051 cp_parser_require (parser
, CPP_COLON
, "`:'");
5052 /* Parse the assignment-expression. */
5053 assignment_expr
= cp_parser_assignment_expression (parser
);
5055 /* Build the conditional-expression. */
5056 return build_x_conditional_expr (logical_or_expr
,
5061 /* Parse an assignment-expression.
5063 assignment-expression:
5064 conditional-expression
5065 logical-or-expression assignment-operator assignment_expression
5068 Returns a representation for the expression. */
5071 cp_parser_assignment_expression (cp_parser
* parser
)
5075 /* If the next token is the `throw' keyword, then we're looking at
5076 a throw-expression. */
5077 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5078 expr
= cp_parser_throw_expression (parser
);
5079 /* Otherwise, it must be that we are looking at a
5080 logical-or-expression. */
5083 /* Parse the logical-or-expression. */
5084 expr
= cp_parser_logical_or_expression (parser
);
5085 /* If the next token is a `?' then we're actually looking at a
5086 conditional-expression. */
5087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5088 return cp_parser_question_colon_clause (parser
, expr
);
5091 enum tree_code assignment_operator
;
5093 /* If it's an assignment-operator, we're using the second
5096 = cp_parser_assignment_operator_opt (parser
);
5097 if (assignment_operator
!= ERROR_MARK
)
5101 /* Parse the right-hand side of the assignment. */
5102 rhs
= cp_parser_assignment_expression (parser
);
5103 /* An assignment may not appear in a
5104 constant-expression. */
5105 if (parser
->integral_constant_expression_p
)
5107 if (!parser
->allow_non_integral_constant_expression_p
)
5108 return cp_parser_non_integral_constant_expression ("an assignment");
5109 parser
->non_integral_constant_expression_p
= true;
5111 /* Build the assignment expression. */
5112 expr
= build_x_modify_expr (expr
,
5113 assignment_operator
,
5122 /* Parse an (optional) assignment-operator.
5124 assignment-operator: one of
5125 = *= /= %= += -= >>= <<= &= ^= |=
5129 assignment-operator: one of
5132 If the next token is an assignment operator, the corresponding tree
5133 code is returned, and the token is consumed. For example, for
5134 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5135 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5136 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5137 operator, ERROR_MARK is returned. */
5139 static enum tree_code
5140 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5145 /* Peek at the next toen. */
5146 token
= cp_lexer_peek_token (parser
->lexer
);
5148 switch (token
->type
)
5159 op
= TRUNC_DIV_EXPR
;
5163 op
= TRUNC_MOD_EXPR
;
5203 /* Nothing else is an assignment operator. */
5207 /* If it was an assignment operator, consume it. */
5208 if (op
!= ERROR_MARK
)
5209 cp_lexer_consume_token (parser
->lexer
);
5214 /* Parse an expression.
5217 assignment-expression
5218 expression , assignment-expression
5220 Returns a representation of the expression. */
5223 cp_parser_expression (cp_parser
* parser
)
5225 tree expression
= NULL_TREE
;
5229 tree assignment_expression
;
5231 /* Parse the next assignment-expression. */
5232 assignment_expression
5233 = cp_parser_assignment_expression (parser
);
5234 /* If this is the first assignment-expression, we can just
5237 expression
= assignment_expression
;
5239 expression
= build_x_compound_expr (expression
,
5240 assignment_expression
);
5241 /* If the next token is not a comma, then we are done with the
5243 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5245 /* Consume the `,'. */
5246 cp_lexer_consume_token (parser
->lexer
);
5247 /* A comma operator cannot appear in a constant-expression. */
5248 if (parser
->integral_constant_expression_p
)
5250 if (!parser
->allow_non_integral_constant_expression_p
)
5252 = cp_parser_non_integral_constant_expression ("a comma operator");
5253 parser
->non_integral_constant_expression_p
= true;
5260 /* Parse a constant-expression.
5262 constant-expression:
5263 conditional-expression
5265 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5266 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5267 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5268 is false, NON_CONSTANT_P should be NULL. */
5271 cp_parser_constant_expression (cp_parser
* parser
,
5272 bool allow_non_constant_p
,
5273 bool *non_constant_p
)
5275 bool saved_integral_constant_expression_p
;
5276 bool saved_allow_non_integral_constant_expression_p
;
5277 bool saved_non_integral_constant_expression_p
;
5280 /* It might seem that we could simply parse the
5281 conditional-expression, and then check to see if it were
5282 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5283 one that the compiler can figure out is constant, possibly after
5284 doing some simplifications or optimizations. The standard has a
5285 precise definition of constant-expression, and we must honor
5286 that, even though it is somewhat more restrictive.
5292 is not a legal declaration, because `(2, 3)' is not a
5293 constant-expression. The `,' operator is forbidden in a
5294 constant-expression. However, GCC's constant-folding machinery
5295 will fold this operation to an INTEGER_CST for `3'. */
5297 /* Save the old settings. */
5298 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5299 saved_allow_non_integral_constant_expression_p
5300 = parser
->allow_non_integral_constant_expression_p
;
5301 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5302 /* We are now parsing a constant-expression. */
5303 parser
->integral_constant_expression_p
= true;
5304 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5305 parser
->non_integral_constant_expression_p
= false;
5306 /* Although the grammar says "conditional-expression", we parse an
5307 "assignment-expression", which also permits "throw-expression"
5308 and the use of assignment operators. In the case that
5309 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5310 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5311 actually essential that we look for an assignment-expression.
5312 For example, cp_parser_initializer_clauses uses this function to
5313 determine whether a particular assignment-expression is in fact
5315 expression
= cp_parser_assignment_expression (parser
);
5316 /* Restore the old settings. */
5317 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
5318 parser
->allow_non_integral_constant_expression_p
5319 = saved_allow_non_integral_constant_expression_p
;
5320 if (allow_non_constant_p
)
5321 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5322 parser
->non_integral_constant_expression_p
= saved_non_integral_constant_expression_p
;
5327 /* Statements [gram.stmt.stmt] */
5329 /* Parse a statement.
5333 expression-statement
5338 declaration-statement
5342 cp_parser_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5346 int statement_line_number
;
5348 /* There is no statement yet. */
5349 statement
= NULL_TREE
;
5350 /* Peek at the next token. */
5351 token
= cp_lexer_peek_token (parser
->lexer
);
5352 /* Remember the line number of the first token in the statement. */
5353 statement_line_number
= token
->location
.line
;
5354 /* If this is a keyword, then that will often determine what kind of
5355 statement we have. */
5356 if (token
->type
== CPP_KEYWORD
)
5358 enum rid keyword
= token
->keyword
;
5364 statement
= cp_parser_labeled_statement (parser
,
5365 in_statement_expr_p
);
5370 statement
= cp_parser_selection_statement (parser
);
5376 statement
= cp_parser_iteration_statement (parser
);
5383 statement
= cp_parser_jump_statement (parser
);
5387 statement
= cp_parser_try_block (parser
);
5391 /* It might be a keyword like `int' that can start a
5392 declaration-statement. */
5396 else if (token
->type
== CPP_NAME
)
5398 /* If the next token is a `:', then we are looking at a
5399 labeled-statement. */
5400 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5401 if (token
->type
== CPP_COLON
)
5402 statement
= cp_parser_labeled_statement (parser
, in_statement_expr_p
);
5404 /* Anything that starts with a `{' must be a compound-statement. */
5405 else if (token
->type
== CPP_OPEN_BRACE
)
5406 statement
= cp_parser_compound_statement (parser
, false);
5408 /* Everything else must be a declaration-statement or an
5409 expression-statement. Try for the declaration-statement
5410 first, unless we are looking at a `;', in which case we know that
5411 we have an expression-statement. */
5414 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5416 cp_parser_parse_tentatively (parser
);
5417 /* Try to parse the declaration-statement. */
5418 cp_parser_declaration_statement (parser
);
5419 /* If that worked, we're done. */
5420 if (cp_parser_parse_definitely (parser
))
5423 /* Look for an expression-statement instead. */
5424 statement
= cp_parser_expression_statement (parser
, in_statement_expr_p
);
5427 /* Set the line number for the statement. */
5428 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5429 STMT_LINENO (statement
) = statement_line_number
;
5432 /* Parse a labeled-statement.
5435 identifier : statement
5436 case constant-expression : statement
5439 Returns the new CASE_LABEL, for a `case' or `default' label. For
5440 an ordinary label, returns a LABEL_STMT. */
5443 cp_parser_labeled_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5446 tree statement
= error_mark_node
;
5448 /* The next token should be an identifier. */
5449 token
= cp_lexer_peek_token (parser
->lexer
);
5450 if (token
->type
!= CPP_NAME
5451 && token
->type
!= CPP_KEYWORD
)
5453 cp_parser_error (parser
, "expected labeled-statement");
5454 return error_mark_node
;
5457 switch (token
->keyword
)
5463 /* Consume the `case' token. */
5464 cp_lexer_consume_token (parser
->lexer
);
5465 /* Parse the constant-expression. */
5466 expr
= cp_parser_constant_expression (parser
,
5467 /*allow_non_constant_p=*/false,
5469 if (!parser
->in_switch_statement_p
)
5470 error ("case label `%E' not within a switch statement", expr
);
5472 statement
= finish_case_label (expr
, NULL_TREE
);
5477 /* Consume the `default' token. */
5478 cp_lexer_consume_token (parser
->lexer
);
5479 if (!parser
->in_switch_statement_p
)
5480 error ("case label not within a switch statement");
5482 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5486 /* Anything else must be an ordinary label. */
5487 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5491 /* Require the `:' token. */
5492 cp_parser_require (parser
, CPP_COLON
, "`:'");
5493 /* Parse the labeled statement. */
5494 cp_parser_statement (parser
, in_statement_expr_p
);
5496 /* Return the label, in the case of a `case' or `default' label. */
5500 /* Parse an expression-statement.
5502 expression-statement:
5505 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5506 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5507 indicates whether this expression-statement is part of an
5508 expression statement. */
5511 cp_parser_expression_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5513 tree statement
= NULL_TREE
;
5515 /* If the next token is a ';', then there is no expression
5517 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5518 statement
= cp_parser_expression (parser
);
5520 /* Consume the final `;'. */
5521 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5523 if (in_statement_expr_p
5524 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
5526 /* This is the final expression statement of a statement
5528 statement
= finish_stmt_expr_expr (statement
);
5531 statement
= finish_expr_stmt (statement
);
5538 /* Parse a compound-statement.
5541 { statement-seq [opt] }
5543 Returns a COMPOUND_STMT representing the statement. */
5546 cp_parser_compound_statement (cp_parser
*parser
, bool in_statement_expr_p
)
5550 /* Consume the `{'. */
5551 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5552 return error_mark_node
;
5553 /* Begin the compound-statement. */
5554 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/false);
5555 /* Parse an (optional) statement-seq. */
5556 cp_parser_statement_seq_opt (parser
, in_statement_expr_p
);
5557 /* Finish the compound-statement. */
5558 finish_compound_stmt (compound_stmt
);
5559 /* Consume the `}'. */
5560 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5562 return compound_stmt
;
5565 /* Parse an (optional) statement-seq.
5569 statement-seq [opt] statement */
5572 cp_parser_statement_seq_opt (cp_parser
* parser
, bool in_statement_expr_p
)
5574 /* Scan statements until there aren't any more. */
5577 /* If we're looking at a `}', then we've run out of statements. */
5578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5579 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5582 /* Parse the statement. */
5583 cp_parser_statement (parser
, in_statement_expr_p
);
5587 /* Parse a selection-statement.
5589 selection-statement:
5590 if ( condition ) statement
5591 if ( condition ) statement else statement
5592 switch ( condition ) statement
5594 Returns the new IF_STMT or SWITCH_STMT. */
5597 cp_parser_selection_statement (cp_parser
* parser
)
5602 /* Peek at the next token. */
5603 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5605 /* See what kind of keyword it is. */
5606 keyword
= token
->keyword
;
5615 /* Look for the `('. */
5616 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5618 cp_parser_skip_to_end_of_statement (parser
);
5619 return error_mark_node
;
5622 /* Begin the selection-statement. */
5623 if (keyword
== RID_IF
)
5624 statement
= begin_if_stmt ();
5626 statement
= begin_switch_stmt ();
5628 /* Parse the condition. */
5629 condition
= cp_parser_condition (parser
);
5630 /* Look for the `)'. */
5631 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5632 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
5633 /*consume_paren=*/true);
5635 if (keyword
== RID_IF
)
5639 /* Add the condition. */
5640 finish_if_stmt_cond (condition
, statement
);
5642 /* Parse the then-clause. */
5643 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5644 finish_then_clause (statement
);
5646 /* If the next token is `else', parse the else-clause. */
5647 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5652 /* Consume the `else' keyword. */
5653 cp_lexer_consume_token (parser
->lexer
);
5654 /* Parse the else-clause. */
5656 = cp_parser_implicitly_scoped_statement (parser
);
5657 finish_else_clause (statement
);
5660 /* Now we're all done with the if-statement. */
5666 bool in_switch_statement_p
;
5668 /* Add the condition. */
5669 finish_switch_cond (condition
, statement
);
5671 /* Parse the body of the switch-statement. */
5672 in_switch_statement_p
= parser
->in_switch_statement_p
;
5673 parser
->in_switch_statement_p
= true;
5674 body
= cp_parser_implicitly_scoped_statement (parser
);
5675 parser
->in_switch_statement_p
= in_switch_statement_p
;
5677 /* Now we're all done with the switch-statement. */
5678 finish_switch_stmt (statement
);
5686 cp_parser_error (parser
, "expected selection-statement");
5687 return error_mark_node
;
5691 /* Parse a condition.
5695 type-specifier-seq declarator = assignment-expression
5700 type-specifier-seq declarator asm-specification [opt]
5701 attributes [opt] = assignment-expression
5703 Returns the expression that should be tested. */
5706 cp_parser_condition (cp_parser
* parser
)
5708 tree type_specifiers
;
5709 const char *saved_message
;
5711 /* Try the declaration first. */
5712 cp_parser_parse_tentatively (parser
);
5713 /* New types are not allowed in the type-specifier-seq for a
5715 saved_message
= parser
->type_definition_forbidden_message
;
5716 parser
->type_definition_forbidden_message
5717 = "types may not be defined in conditions";
5718 /* Parse the type-specifier-seq. */
5719 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5720 /* Restore the saved message. */
5721 parser
->type_definition_forbidden_message
= saved_message
;
5722 /* If all is well, we might be looking at a declaration. */
5723 if (!cp_parser_error_occurred (parser
))
5726 tree asm_specification
;
5729 tree initializer
= NULL_TREE
;
5731 /* Parse the declarator. */
5732 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5733 /*ctor_dtor_or_conv_p=*/NULL
,
5734 /*parenthesized_p=*/NULL
);
5735 /* Parse the attributes. */
5736 attributes
= cp_parser_attributes_opt (parser
);
5737 /* Parse the asm-specification. */
5738 asm_specification
= cp_parser_asm_specification_opt (parser
);
5739 /* If the next token is not an `=', then we might still be
5740 looking at an expression. For example:
5744 looks like a decl-specifier-seq and a declarator -- but then
5745 there is no `=', so this is an expression. */
5746 cp_parser_require (parser
, CPP_EQ
, "`='");
5747 /* If we did see an `=', then we are looking at a declaration
5749 if (cp_parser_parse_definitely (parser
))
5751 /* Create the declaration. */
5752 decl
= start_decl (declarator
, type_specifiers
,
5753 /*initialized_p=*/true,
5754 attributes
, /*prefix_attributes=*/NULL_TREE
);
5755 /* Parse the assignment-expression. */
5756 initializer
= cp_parser_assignment_expression (parser
);
5758 /* Process the initializer. */
5759 cp_finish_decl (decl
,
5762 LOOKUP_ONLYCONVERTING
);
5764 return convert_from_reference (decl
);
5767 /* If we didn't even get past the declarator successfully, we are
5768 definitely not looking at a declaration. */
5770 cp_parser_abort_tentative_parse (parser
);
5772 /* Otherwise, we are looking at an expression. */
5773 return cp_parser_expression (parser
);
5776 /* Parse an iteration-statement.
5778 iteration-statement:
5779 while ( condition ) statement
5780 do statement while ( expression ) ;
5781 for ( for-init-statement condition [opt] ; expression [opt] )
5784 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5787 cp_parser_iteration_statement (cp_parser
* parser
)
5792 bool in_iteration_statement_p
;
5795 /* Peek at the next token. */
5796 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
5798 return error_mark_node
;
5800 /* Remember whether or not we are already within an iteration
5802 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
5804 /* See what kind of keyword it is. */
5805 keyword
= token
->keyword
;
5812 /* Begin the while-statement. */
5813 statement
= begin_while_stmt ();
5814 /* Look for the `('. */
5815 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5816 /* Parse the condition. */
5817 condition
= cp_parser_condition (parser
);
5818 finish_while_stmt_cond (condition
, statement
);
5819 /* Look for the `)'. */
5820 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5821 /* Parse the dependent statement. */
5822 parser
->in_iteration_statement_p
= true;
5823 cp_parser_already_scoped_statement (parser
);
5824 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5825 /* We're done with the while-statement. */
5826 finish_while_stmt (statement
);
5834 /* Begin the do-statement. */
5835 statement
= begin_do_stmt ();
5836 /* Parse the body of the do-statement. */
5837 parser
->in_iteration_statement_p
= true;
5838 cp_parser_implicitly_scoped_statement (parser
);
5839 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5840 finish_do_body (statement
);
5841 /* Look for the `while' keyword. */
5842 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
5843 /* Look for the `('. */
5844 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5845 /* Parse the expression. */
5846 expression
= cp_parser_expression (parser
);
5847 /* We're done with the do-statement. */
5848 finish_do_stmt (expression
, statement
);
5849 /* Look for the `)'. */
5850 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5851 /* Look for the `;'. */
5852 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5858 tree condition
= NULL_TREE
;
5859 tree expression
= NULL_TREE
;
5861 /* Begin the for-statement. */
5862 statement
= begin_for_stmt ();
5863 /* Look for the `('. */
5864 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5865 /* Parse the initialization. */
5866 cp_parser_for_init_statement (parser
);
5867 finish_for_init_stmt (statement
);
5869 /* If there's a condition, process it. */
5870 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5871 condition
= cp_parser_condition (parser
);
5872 finish_for_cond (condition
, statement
);
5873 /* Look for the `;'. */
5874 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5876 /* If there's an expression, process it. */
5877 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5878 expression
= cp_parser_expression (parser
);
5879 finish_for_expr (expression
, statement
);
5880 /* Look for the `)'. */
5881 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`;'");
5883 /* Parse the body of the for-statement. */
5884 parser
->in_iteration_statement_p
= true;
5885 cp_parser_already_scoped_statement (parser
);
5886 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5888 /* We're done with the for-statement. */
5889 finish_for_stmt (statement
);
5894 cp_parser_error (parser
, "expected iteration-statement");
5895 statement
= error_mark_node
;
5902 /* Parse a for-init-statement.
5905 expression-statement
5906 simple-declaration */
5909 cp_parser_for_init_statement (cp_parser
* parser
)
5911 /* If the next token is a `;', then we have an empty
5912 expression-statement. Grammatically, this is also a
5913 simple-declaration, but an invalid one, because it does not
5914 declare anything. Therefore, if we did not handle this case
5915 specially, we would issue an error message about an invalid
5917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5919 /* We're going to speculatively look for a declaration, falling back
5920 to an expression, if necessary. */
5921 cp_parser_parse_tentatively (parser
);
5922 /* Parse the declaration. */
5923 cp_parser_simple_declaration (parser
,
5924 /*function_definition_allowed_p=*/false);
5925 /* If the tentative parse failed, then we shall need to look for an
5926 expression-statement. */
5927 if (cp_parser_parse_definitely (parser
))
5931 cp_parser_expression_statement (parser
, false);
5934 /* Parse a jump-statement.
5939 return expression [opt] ;
5947 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5951 cp_parser_jump_statement (cp_parser
* parser
)
5953 tree statement
= error_mark_node
;
5957 /* Peek at the next token. */
5958 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
5960 return error_mark_node
;
5962 /* See what kind of keyword it is. */
5963 keyword
= token
->keyword
;
5967 if (!parser
->in_switch_statement_p
5968 && !parser
->in_iteration_statement_p
)
5970 error ("break statement not within loop or switch");
5971 statement
= error_mark_node
;
5974 statement
= finish_break_stmt ();
5975 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5979 if (!parser
->in_iteration_statement_p
)
5981 error ("continue statement not within a loop");
5982 statement
= error_mark_node
;
5985 statement
= finish_continue_stmt ();
5986 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5993 /* If the next token is a `;', then there is no
5995 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5996 expr
= cp_parser_expression (parser
);
5999 /* Build the return-statement. */
6000 statement
= finish_return_stmt (expr
);
6001 /* Look for the final `;'. */
6002 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6007 /* Create the goto-statement. */
6008 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6010 /* Issue a warning about this use of a GNU extension. */
6012 pedwarn ("ISO C++ forbids computed gotos");
6013 /* Consume the '*' token. */
6014 cp_lexer_consume_token (parser
->lexer
);
6015 /* Parse the dependent expression. */
6016 finish_goto_stmt (cp_parser_expression (parser
));
6019 finish_goto_stmt (cp_parser_identifier (parser
));
6020 /* Look for the final `;'. */
6021 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6025 cp_parser_error (parser
, "expected jump-statement");
6032 /* Parse a declaration-statement.
6034 declaration-statement:
6035 block-declaration */
6038 cp_parser_declaration_statement (cp_parser
* parser
)
6040 /* Parse the block-declaration. */
6041 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6043 /* Finish off the statement. */
6047 /* Some dependent statements (like `if (cond) statement'), are
6048 implicitly in their own scope. In other words, if the statement is
6049 a single statement (as opposed to a compound-statement), it is
6050 none-the-less treated as if it were enclosed in braces. Any
6051 declarations appearing in the dependent statement are out of scope
6052 after control passes that point. This function parses a statement,
6053 but ensures that is in its own scope, even if it is not a
6056 Returns the new statement. */
6059 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6063 /* If the token is not a `{', then we must take special action. */
6064 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6066 /* Create a compound-statement. */
6067 statement
= begin_compound_stmt (/*has_no_scope=*/false);
6068 /* Parse the dependent-statement. */
6069 cp_parser_statement (parser
, false);
6070 /* Finish the dummy compound-statement. */
6071 finish_compound_stmt (statement
);
6073 /* Otherwise, we simply parse the statement directly. */
6075 statement
= cp_parser_compound_statement (parser
, false);
6077 /* Return the statement. */
6081 /* For some dependent statements (like `while (cond) statement'), we
6082 have already created a scope. Therefore, even if the dependent
6083 statement is a compound-statement, we do not want to create another
6087 cp_parser_already_scoped_statement (cp_parser
* parser
)
6089 /* If the token is not a `{', then we must take special action. */
6090 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
6094 /* Create a compound-statement. */
6095 statement
= begin_compound_stmt (/*has_no_scope=*/true);
6096 /* Parse the dependent-statement. */
6097 cp_parser_statement (parser
, false);
6098 /* Finish the dummy compound-statement. */
6099 finish_compound_stmt (statement
);
6101 /* Otherwise, we simply parse the statement directly. */
6103 cp_parser_statement (parser
, false);
6106 /* Declarations [gram.dcl.dcl] */
6108 /* Parse an optional declaration-sequence.
6112 declaration-seq declaration */
6115 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6121 token
= cp_lexer_peek_token (parser
->lexer
);
6123 if (token
->type
== CPP_CLOSE_BRACE
6124 || token
->type
== CPP_EOF
)
6127 if (token
->type
== CPP_SEMICOLON
)
6129 /* A declaration consisting of a single semicolon is
6130 invalid. Allow it unless we're being pedantic. */
6131 if (pedantic
&& !in_system_header
)
6132 pedwarn ("extra `;'");
6133 cp_lexer_consume_token (parser
->lexer
);
6137 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6138 parser to enter or exit implicit `extern "C"' blocks. */
6139 while (pending_lang_change
> 0)
6141 push_lang_context (lang_name_c
);
6142 --pending_lang_change
;
6144 while (pending_lang_change
< 0)
6146 pop_lang_context ();
6147 ++pending_lang_change
;
6150 /* Parse the declaration itself. */
6151 cp_parser_declaration (parser
);
6155 /* Parse a declaration.
6160 template-declaration
6161 explicit-instantiation
6162 explicit-specialization
6163 linkage-specification
6164 namespace-definition
6169 __extension__ declaration */
6172 cp_parser_declaration (cp_parser
* parser
)
6178 /* Check for the `__extension__' keyword. */
6179 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6181 /* Parse the qualified declaration. */
6182 cp_parser_declaration (parser
);
6183 /* Restore the PEDANTIC flag. */
6184 pedantic
= saved_pedantic
;
6189 /* Try to figure out what kind of declaration is present. */
6190 token1
= *cp_lexer_peek_token (parser
->lexer
);
6191 if (token1
.type
!= CPP_EOF
)
6192 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6194 /* If the next token is `extern' and the following token is a string
6195 literal, then we have a linkage specification. */
6196 if (token1
.keyword
== RID_EXTERN
6197 && cp_parser_is_string_literal (&token2
))
6198 cp_parser_linkage_specification (parser
);
6199 /* If the next token is `template', then we have either a template
6200 declaration, an explicit instantiation, or an explicit
6202 else if (token1
.keyword
== RID_TEMPLATE
)
6204 /* `template <>' indicates a template specialization. */
6205 if (token2
.type
== CPP_LESS
6206 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6207 cp_parser_explicit_specialization (parser
);
6208 /* `template <' indicates a template declaration. */
6209 else if (token2
.type
== CPP_LESS
)
6210 cp_parser_template_declaration (parser
, /*member_p=*/false);
6211 /* Anything else must be an explicit instantiation. */
6213 cp_parser_explicit_instantiation (parser
);
6215 /* If the next token is `export', then we have a template
6217 else if (token1
.keyword
== RID_EXPORT
)
6218 cp_parser_template_declaration (parser
, /*member_p=*/false);
6219 /* If the next token is `extern', 'static' or 'inline' and the one
6220 after that is `template', we have a GNU extended explicit
6221 instantiation directive. */
6222 else if (cp_parser_allow_gnu_extensions_p (parser
)
6223 && (token1
.keyword
== RID_EXTERN
6224 || token1
.keyword
== RID_STATIC
6225 || token1
.keyword
== RID_INLINE
)
6226 && token2
.keyword
== RID_TEMPLATE
)
6227 cp_parser_explicit_instantiation (parser
);
6228 /* If the next token is `namespace', check for a named or unnamed
6229 namespace definition. */
6230 else if (token1
.keyword
== RID_NAMESPACE
6231 && (/* A named namespace definition. */
6232 (token2
.type
== CPP_NAME
6233 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6235 /* An unnamed namespace definition. */
6236 || token2
.type
== CPP_OPEN_BRACE
))
6237 cp_parser_namespace_definition (parser
);
6238 /* We must have either a block declaration or a function
6241 /* Try to parse a block-declaration, or a function-definition. */
6242 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6245 /* Parse a block-declaration.
6250 namespace-alias-definition
6257 __extension__ block-declaration
6260 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6261 part of a declaration-statement. */
6264 cp_parser_block_declaration (cp_parser
*parser
,
6270 /* Check for the `__extension__' keyword. */
6271 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6273 /* Parse the qualified declaration. */
6274 cp_parser_block_declaration (parser
, statement_p
);
6275 /* Restore the PEDANTIC flag. */
6276 pedantic
= saved_pedantic
;
6281 /* Peek at the next token to figure out which kind of declaration is
6283 token1
= cp_lexer_peek_token (parser
->lexer
);
6285 /* If the next keyword is `asm', we have an asm-definition. */
6286 if (token1
->keyword
== RID_ASM
)
6289 cp_parser_commit_to_tentative_parse (parser
);
6290 cp_parser_asm_definition (parser
);
6292 /* If the next keyword is `namespace', we have a
6293 namespace-alias-definition. */
6294 else if (token1
->keyword
== RID_NAMESPACE
)
6295 cp_parser_namespace_alias_definition (parser
);
6296 /* If the next keyword is `using', we have either a
6297 using-declaration or a using-directive. */
6298 else if (token1
->keyword
== RID_USING
)
6303 cp_parser_commit_to_tentative_parse (parser
);
6304 /* If the token after `using' is `namespace', then we have a
6306 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6307 if (token2
->keyword
== RID_NAMESPACE
)
6308 cp_parser_using_directive (parser
);
6309 /* Otherwise, it's a using-declaration. */
6311 cp_parser_using_declaration (parser
);
6313 /* If the next keyword is `__label__' we have a label declaration. */
6314 else if (token1
->keyword
== RID_LABEL
)
6317 cp_parser_commit_to_tentative_parse (parser
);
6318 cp_parser_label_declaration (parser
);
6320 /* Anything else must be a simple-declaration. */
6322 cp_parser_simple_declaration (parser
, !statement_p
);
6325 /* Parse a simple-declaration.
6328 decl-specifier-seq [opt] init-declarator-list [opt] ;
6330 init-declarator-list:
6332 init-declarator-list , init-declarator
6334 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6335 function-definition as a simple-declaration. */
6338 cp_parser_simple_declaration (cp_parser
* parser
,
6339 bool function_definition_allowed_p
)
6341 tree decl_specifiers
;
6343 int declares_class_or_enum
;
6344 bool saw_declarator
;
6346 /* Defer access checks until we know what is being declared; the
6347 checks for names appearing in the decl-specifier-seq should be
6348 done as if we were in the scope of the thing being declared. */
6349 push_deferring_access_checks (dk_deferred
);
6351 /* Parse the decl-specifier-seq. We have to keep track of whether
6352 or not the decl-specifier-seq declares a named class or
6353 enumeration type, since that is the only case in which the
6354 init-declarator-list is allowed to be empty.
6358 In a simple-declaration, the optional init-declarator-list can be
6359 omitted only when declaring a class or enumeration, that is when
6360 the decl-specifier-seq contains either a class-specifier, an
6361 elaborated-type-specifier, or an enum-specifier. */
6363 = cp_parser_decl_specifier_seq (parser
,
6364 CP_PARSER_FLAGS_OPTIONAL
,
6366 &declares_class_or_enum
);
6367 /* We no longer need to defer access checks. */
6368 stop_deferring_access_checks ();
6370 /* In a block scope, a valid declaration must always have a
6371 decl-specifier-seq. By not trying to parse declarators, we can
6372 resolve the declaration/expression ambiguity more quickly. */
6373 if (!function_definition_allowed_p
&& !decl_specifiers
)
6375 cp_parser_error (parser
, "expected declaration");
6379 /* If the next two tokens are both identifiers, the code is
6380 erroneous. The usual cause of this situation is code like:
6384 where "T" should name a type -- but does not. */
6385 if (cp_parser_diagnose_invalid_type_name (parser
))
6387 /* If parsing tentatively, we should commit; we really are
6388 looking at a declaration. */
6389 cp_parser_commit_to_tentative_parse (parser
);
6394 /* Keep going until we hit the `;' at the end of the simple
6396 saw_declarator
= false;
6397 while (cp_lexer_next_token_is_not (parser
->lexer
,
6401 bool function_definition_p
;
6404 saw_declarator
= true;
6405 /* Parse the init-declarator. */
6406 decl
= cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6407 function_definition_allowed_p
,
6409 declares_class_or_enum
,
6410 &function_definition_p
);
6411 /* If an error occurred while parsing tentatively, exit quickly.
6412 (That usually happens when in the body of a function; each
6413 statement is treated as a declaration-statement until proven
6415 if (cp_parser_error_occurred (parser
))
6417 /* Handle function definitions specially. */
6418 if (function_definition_p
)
6420 /* If the next token is a `,', then we are probably
6421 processing something like:
6425 which is erroneous. */
6426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6427 error ("mixing declarations and function-definitions is forbidden");
6428 /* Otherwise, we're done with the list of declarators. */
6431 pop_deferring_access_checks ();
6435 /* The next token should be either a `,' or a `;'. */
6436 token
= cp_lexer_peek_token (parser
->lexer
);
6437 /* If it's a `,', there are more declarators to come. */
6438 if (token
->type
== CPP_COMMA
)
6439 cp_lexer_consume_token (parser
->lexer
);
6440 /* If it's a `;', we are done. */
6441 else if (token
->type
== CPP_SEMICOLON
)
6443 /* Anything else is an error. */
6446 cp_parser_error (parser
, "expected `,' or `;'");
6447 /* Skip tokens until we reach the end of the statement. */
6448 cp_parser_skip_to_end_of_statement (parser
);
6451 /* After the first time around, a function-definition is not
6452 allowed -- even if it was OK at first. For example:
6457 function_definition_allowed_p
= false;
6460 /* Issue an error message if no declarators are present, and the
6461 decl-specifier-seq does not itself declare a class or
6463 if (!saw_declarator
)
6465 if (cp_parser_declares_only_class_p (parser
))
6466 shadow_tag (decl_specifiers
);
6467 /* Perform any deferred access checks. */
6468 perform_deferred_access_checks ();
6471 /* Consume the `;'. */
6472 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6475 pop_deferring_access_checks ();
6478 /* Parse a decl-specifier-seq.
6481 decl-specifier-seq [opt] decl-specifier
6484 storage-class-specifier
6493 decl-specifier-seq [opt] attributes
6495 Returns a TREE_LIST, giving the decl-specifiers in the order they
6496 appear in the source code. The TREE_VALUE of each node is the
6497 decl-specifier. For a keyword (such as `auto' or `friend'), the
6498 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6499 representation of a type-specifier, see cp_parser_type_specifier.
6501 If there are attributes, they will be stored in *ATTRIBUTES,
6502 represented as described above cp_parser_attributes.
6504 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6505 appears, and the entity that will be a friend is not going to be a
6506 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6507 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6508 friendship is granted might not be a class.
6510 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6513 1: one of the decl-specifiers is an elaborated-type-specifier
6514 2: one of the decl-specifiers is an enum-specifier or a
6520 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6521 cp_parser_flags flags
,
6523 int* declares_class_or_enum
)
6525 tree decl_specs
= NULL_TREE
;
6526 bool friend_p
= false;
6527 bool constructor_possible_p
= !parser
->in_declarator_p
;
6529 /* Assume no class or enumeration type is declared. */
6530 *declares_class_or_enum
= 0;
6532 /* Assume there are no attributes. */
6533 *attributes
= NULL_TREE
;
6535 /* Keep reading specifiers until there are no more to read. */
6538 tree decl_spec
= NULL_TREE
;
6542 /* Peek at the next token. */
6543 token
= cp_lexer_peek_token (parser
->lexer
);
6544 /* Handle attributes. */
6545 if (token
->keyword
== RID_ATTRIBUTE
)
6547 /* Parse the attributes. */
6548 decl_spec
= cp_parser_attributes_opt (parser
);
6549 /* Add them to the list. */
6550 *attributes
= chainon (*attributes
, decl_spec
);
6553 /* If the next token is an appropriate keyword, we can simply
6554 add it to the list. */
6555 switch (token
->keyword
)
6561 error ("duplicate `friend'");
6564 /* The representation of the specifier is simply the
6565 appropriate TREE_IDENTIFIER node. */
6566 decl_spec
= token
->value
;
6567 /* Consume the token. */
6568 cp_lexer_consume_token (parser
->lexer
);
6571 /* function-specifier:
6578 decl_spec
= cp_parser_function_specifier_opt (parser
);
6584 /* The representation of the specifier is simply the
6585 appropriate TREE_IDENTIFIER node. */
6586 decl_spec
= token
->value
;
6587 /* Consume the token. */
6588 cp_lexer_consume_token (parser
->lexer
);
6589 /* A constructor declarator cannot appear in a typedef. */
6590 constructor_possible_p
= false;
6591 /* The "typedef" keyword can only occur in a declaration; we
6592 may as well commit at this point. */
6593 cp_parser_commit_to_tentative_parse (parser
);
6596 /* storage-class-specifier:
6611 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6618 /* Constructors are a special case. The `S' in `S()' is not a
6619 decl-specifier; it is the beginning of the declarator. */
6620 constructor_p
= (!decl_spec
6621 && constructor_possible_p
6622 && cp_parser_constructor_declarator_p (parser
,
6625 /* If we don't have a DECL_SPEC yet, then we must be looking at
6626 a type-specifier. */
6627 if (!decl_spec
&& !constructor_p
)
6629 int decl_spec_declares_class_or_enum
;
6630 bool is_cv_qualifier
;
6633 = cp_parser_type_specifier (parser
, flags
,
6635 /*is_declaration=*/true,
6636 &decl_spec_declares_class_or_enum
,
6639 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6641 /* If this type-specifier referenced a user-defined type
6642 (a typedef, class-name, etc.), then we can't allow any
6643 more such type-specifiers henceforth.
6647 The longest sequence of decl-specifiers that could
6648 possibly be a type name is taken as the
6649 decl-specifier-seq of a declaration. The sequence shall
6650 be self-consistent as described below.
6654 As a general rule, at most one type-specifier is allowed
6655 in the complete decl-specifier-seq of a declaration. The
6656 only exceptions are the following:
6658 -- const or volatile can be combined with any other
6661 -- signed or unsigned can be combined with char, long,
6669 void g (const int Pc);
6671 Here, Pc is *not* part of the decl-specifier seq; it's
6672 the declarator. Therefore, once we see a type-specifier
6673 (other than a cv-qualifier), we forbid any additional
6674 user-defined types. We *do* still allow things like `int
6675 int' to be considered a decl-specifier-seq, and issue the
6676 error message later. */
6677 if (decl_spec
&& !is_cv_qualifier
)
6678 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6679 /* A constructor declarator cannot follow a type-specifier. */
6681 constructor_possible_p
= false;
6684 /* If we still do not have a DECL_SPEC, then there are no more
6688 /* Issue an error message, unless the entire construct was
6690 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6692 cp_parser_error (parser
, "expected decl specifier");
6693 return error_mark_node
;
6699 /* Add the DECL_SPEC to the list of specifiers. */
6700 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6702 /* After we see one decl-specifier, further decl-specifiers are
6704 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6707 /* We have built up the DECL_SPECS in reverse order. Return them in
6708 the correct order. */
6709 return nreverse (decl_specs
);
6712 /* Parse an (optional) storage-class-specifier.
6714 storage-class-specifier:
6723 storage-class-specifier:
6726 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6729 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
6731 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6739 /* Consume the token. */
6740 return cp_lexer_consume_token (parser
->lexer
)->value
;
6747 /* Parse an (optional) function-specifier.
6754 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6757 cp_parser_function_specifier_opt (cp_parser
* parser
)
6759 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6764 /* Consume the token. */
6765 return cp_lexer_consume_token (parser
->lexer
)->value
;
6772 /* Parse a linkage-specification.
6774 linkage-specification:
6775 extern string-literal { declaration-seq [opt] }
6776 extern string-literal declaration */
6779 cp_parser_linkage_specification (cp_parser
* parser
)
6784 /* Look for the `extern' keyword. */
6785 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
6787 /* Peek at the next token. */
6788 token
= cp_lexer_peek_token (parser
->lexer
);
6789 /* If it's not a string-literal, then there's a problem. */
6790 if (!cp_parser_is_string_literal (token
))
6792 cp_parser_error (parser
, "expected language-name");
6795 /* Consume the token. */
6796 cp_lexer_consume_token (parser
->lexer
);
6798 /* Transform the literal into an identifier. If the literal is a
6799 wide-character string, or contains embedded NULs, then we can't
6800 handle it as the user wants. */
6801 if (token
->type
== CPP_WSTRING
6802 || (strlen (TREE_STRING_POINTER (token
->value
))
6803 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
6805 cp_parser_error (parser
, "invalid linkage-specification");
6806 /* Assume C++ linkage. */
6807 linkage
= get_identifier ("c++");
6809 /* If it's a simple string constant, things are easier. */
6811 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
6813 /* We're now using the new linkage. */
6814 push_lang_context (linkage
);
6816 /* If the next token is a `{', then we're using the first
6818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6820 /* Consume the `{' token. */
6821 cp_lexer_consume_token (parser
->lexer
);
6822 /* Parse the declarations. */
6823 cp_parser_declaration_seq_opt (parser
);
6824 /* Look for the closing `}'. */
6825 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6827 /* Otherwise, there's just one declaration. */
6830 bool saved_in_unbraced_linkage_specification_p
;
6832 saved_in_unbraced_linkage_specification_p
6833 = parser
->in_unbraced_linkage_specification_p
;
6834 parser
->in_unbraced_linkage_specification_p
= true;
6835 have_extern_spec
= true;
6836 cp_parser_declaration (parser
);
6837 have_extern_spec
= false;
6838 parser
->in_unbraced_linkage_specification_p
6839 = saved_in_unbraced_linkage_specification_p
;
6842 /* We're done with the linkage-specification. */
6843 pop_lang_context ();
6846 /* Special member functions [gram.special] */
6848 /* Parse a conversion-function-id.
6850 conversion-function-id:
6851 operator conversion-type-id
6853 Returns an IDENTIFIER_NODE representing the operator. */
6856 cp_parser_conversion_function_id (cp_parser
* parser
)
6860 tree saved_qualifying_scope
;
6861 tree saved_object_scope
;
6863 /* Look for the `operator' token. */
6864 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
6865 return error_mark_node
;
6866 /* When we parse the conversion-type-id, the current scope will be
6867 reset. However, we need that information in able to look up the
6868 conversion function later, so we save it here. */
6869 saved_scope
= parser
->scope
;
6870 saved_qualifying_scope
= parser
->qualifying_scope
;
6871 saved_object_scope
= parser
->object_scope
;
6872 /* We must enter the scope of the class so that the names of
6873 entities declared within the class are available in the
6874 conversion-type-id. For example, consider:
6881 S::operator I() { ... }
6883 In order to see that `I' is a type-name in the definition, we
6884 must be in the scope of `S'. */
6886 push_scope (saved_scope
);
6887 /* Parse the conversion-type-id. */
6888 type
= cp_parser_conversion_type_id (parser
);
6889 /* Leave the scope of the class, if any. */
6891 pop_scope (saved_scope
);
6892 /* Restore the saved scope. */
6893 parser
->scope
= saved_scope
;
6894 parser
->qualifying_scope
= saved_qualifying_scope
;
6895 parser
->object_scope
= saved_object_scope
;
6896 /* If the TYPE is invalid, indicate failure. */
6897 if (type
== error_mark_node
)
6898 return error_mark_node
;
6899 return mangle_conv_op_name_for_type (type
);
6902 /* Parse a conversion-type-id:
6905 type-specifier-seq conversion-declarator [opt]
6907 Returns the TYPE specified. */
6910 cp_parser_conversion_type_id (cp_parser
* parser
)
6913 tree type_specifiers
;
6916 /* Parse the attributes. */
6917 attributes
= cp_parser_attributes_opt (parser
);
6918 /* Parse the type-specifiers. */
6919 type_specifiers
= cp_parser_type_specifier_seq (parser
);
6920 /* If that didn't work, stop. */
6921 if (type_specifiers
== error_mark_node
)
6922 return error_mark_node
;
6923 /* Parse the conversion-declarator. */
6924 declarator
= cp_parser_conversion_declarator_opt (parser
);
6926 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
6927 /*initialized=*/0, &attributes
);
6930 /* Parse an (optional) conversion-declarator.
6932 conversion-declarator:
6933 ptr-operator conversion-declarator [opt]
6935 Returns a representation of the declarator. See
6936 cp_parser_declarator for details. */
6939 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
6941 enum tree_code code
;
6943 tree cv_qualifier_seq
;
6945 /* We don't know if there's a ptr-operator next, or not. */
6946 cp_parser_parse_tentatively (parser
);
6947 /* Try the ptr-operator. */
6948 code
= cp_parser_ptr_operator (parser
, &class_type
,
6950 /* If it worked, look for more conversion-declarators. */
6951 if (cp_parser_parse_definitely (parser
))
6955 /* Parse another optional declarator. */
6956 declarator
= cp_parser_conversion_declarator_opt (parser
);
6958 /* Create the representation of the declarator. */
6959 if (code
== INDIRECT_REF
)
6960 declarator
= make_pointer_declarator (cv_qualifier_seq
,
6963 declarator
= make_reference_declarator (cv_qualifier_seq
,
6966 /* Handle the pointer-to-member case. */
6968 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
6976 /* Parse an (optional) ctor-initializer.
6979 : mem-initializer-list
6981 Returns TRUE iff the ctor-initializer was actually present. */
6984 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
6986 /* If the next token is not a `:', then there is no
6987 ctor-initializer. */
6988 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
6990 /* Do default initialization of any bases and members. */
6991 if (DECL_CONSTRUCTOR_P (current_function_decl
))
6992 finish_mem_initializers (NULL_TREE
);
6997 /* Consume the `:' token. */
6998 cp_lexer_consume_token (parser
->lexer
);
6999 /* And the mem-initializer-list. */
7000 cp_parser_mem_initializer_list (parser
);
7005 /* Parse a mem-initializer-list.
7007 mem-initializer-list:
7009 mem-initializer , mem-initializer-list */
7012 cp_parser_mem_initializer_list (cp_parser
* parser
)
7014 tree mem_initializer_list
= NULL_TREE
;
7016 /* Let the semantic analysis code know that we are starting the
7017 mem-initializer-list. */
7018 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7019 error ("only constructors take base initializers");
7021 /* Loop through the list. */
7024 tree mem_initializer
;
7026 /* Parse the mem-initializer. */
7027 mem_initializer
= cp_parser_mem_initializer (parser
);
7028 /* Add it to the list, unless it was erroneous. */
7029 if (mem_initializer
)
7031 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7032 mem_initializer_list
= mem_initializer
;
7034 /* If the next token is not a `,', we're done. */
7035 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7037 /* Consume the `,' token. */
7038 cp_lexer_consume_token (parser
->lexer
);
7041 /* Perform semantic analysis. */
7042 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7043 finish_mem_initializers (mem_initializer_list
);
7046 /* Parse a mem-initializer.
7049 mem-initializer-id ( expression-list [opt] )
7054 ( expression-list [opt] )
7056 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7057 class) or FIELD_DECL (for a non-static data member) to initialize;
7058 the TREE_VALUE is the expression-list. */
7061 cp_parser_mem_initializer (cp_parser
* parser
)
7063 tree mem_initializer_id
;
7064 tree expression_list
;
7067 /* Find out what is being initialized. */
7068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7070 pedwarn ("anachronistic old-style base class initializer");
7071 mem_initializer_id
= NULL_TREE
;
7074 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7075 member
= expand_member_init (mem_initializer_id
);
7076 if (member
&& !DECL_P (member
))
7077 in_base_initializer
= 1;
7080 = cp_parser_parenthesized_expression_list (parser
, false,
7081 /*non_constant_p=*/NULL
);
7082 if (!expression_list
)
7083 expression_list
= void_type_node
;
7085 in_base_initializer
= 0;
7087 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7090 /* Parse a mem-initializer-id.
7093 :: [opt] nested-name-specifier [opt] class-name
7096 Returns a TYPE indicating the class to be initializer for the first
7097 production. Returns an IDENTIFIER_NODE indicating the data member
7098 to be initialized for the second production. */
7101 cp_parser_mem_initializer_id (cp_parser
* parser
)
7103 bool global_scope_p
;
7104 bool nested_name_specifier_p
;
7107 /* Look for the optional `::' operator. */
7109 = (cp_parser_global_scope_opt (parser
,
7110 /*current_scope_valid_p=*/false)
7112 /* Look for the optional nested-name-specifier. The simplest way to
7117 The keyword `typename' is not permitted in a base-specifier or
7118 mem-initializer; in these contexts a qualified name that
7119 depends on a template-parameter is implicitly assumed to be a
7122 is to assume that we have seen the `typename' keyword at this
7124 nested_name_specifier_p
7125 = (cp_parser_nested_name_specifier_opt (parser
,
7126 /*typename_keyword_p=*/true,
7127 /*check_dependency_p=*/true,
7129 /*is_declaration=*/true)
7131 /* If there is a `::' operator or a nested-name-specifier, then we
7132 are definitely looking for a class-name. */
7133 if (global_scope_p
|| nested_name_specifier_p
)
7134 return cp_parser_class_name (parser
,
7135 /*typename_keyword_p=*/true,
7136 /*template_keyword_p=*/false,
7138 /*check_dependency_p=*/true,
7139 /*class_head_p=*/false,
7140 /*is_declaration=*/true);
7141 /* Otherwise, we could also be looking for an ordinary identifier. */
7142 cp_parser_parse_tentatively (parser
);
7143 /* Try a class-name. */
7144 id
= cp_parser_class_name (parser
,
7145 /*typename_keyword_p=*/true,
7146 /*template_keyword_p=*/false,
7148 /*check_dependency_p=*/true,
7149 /*class_head_p=*/false,
7150 /*is_declaration=*/true);
7151 /* If we found one, we're done. */
7152 if (cp_parser_parse_definitely (parser
))
7154 /* Otherwise, look for an ordinary identifier. */
7155 return cp_parser_identifier (parser
);
7158 /* Overloading [gram.over] */
7160 /* Parse an operator-function-id.
7162 operator-function-id:
7165 Returns an IDENTIFIER_NODE for the operator which is a
7166 human-readable spelling of the identifier, e.g., `operator +'. */
7169 cp_parser_operator_function_id (cp_parser
* parser
)
7171 /* Look for the `operator' keyword. */
7172 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7173 return error_mark_node
;
7174 /* And then the name of the operator itself. */
7175 return cp_parser_operator (parser
);
7178 /* Parse an operator.
7181 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7182 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7183 || ++ -- , ->* -> () []
7190 Returns an IDENTIFIER_NODE for the operator which is a
7191 human-readable spelling of the identifier, e.g., `operator +'. */
7194 cp_parser_operator (cp_parser
* parser
)
7196 tree id
= NULL_TREE
;
7199 /* Peek at the next token. */
7200 token
= cp_lexer_peek_token (parser
->lexer
);
7201 /* Figure out which operator we have. */
7202 switch (token
->type
)
7208 /* The keyword should be either `new' or `delete'. */
7209 if (token
->keyword
== RID_NEW
)
7211 else if (token
->keyword
== RID_DELETE
)
7216 /* Consume the `new' or `delete' token. */
7217 cp_lexer_consume_token (parser
->lexer
);
7219 /* Peek at the next token. */
7220 token
= cp_lexer_peek_token (parser
->lexer
);
7221 /* If it's a `[' token then this is the array variant of the
7223 if (token
->type
== CPP_OPEN_SQUARE
)
7225 /* Consume the `[' token. */
7226 cp_lexer_consume_token (parser
->lexer
);
7227 /* Look for the `]' token. */
7228 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7229 id
= ansi_opname (op
== NEW_EXPR
7230 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7232 /* Otherwise, we have the non-array variant. */
7234 id
= ansi_opname (op
);
7240 id
= ansi_opname (PLUS_EXPR
);
7244 id
= ansi_opname (MINUS_EXPR
);
7248 id
= ansi_opname (MULT_EXPR
);
7252 id
= ansi_opname (TRUNC_DIV_EXPR
);
7256 id
= ansi_opname (TRUNC_MOD_EXPR
);
7260 id
= ansi_opname (BIT_XOR_EXPR
);
7264 id
= ansi_opname (BIT_AND_EXPR
);
7268 id
= ansi_opname (BIT_IOR_EXPR
);
7272 id
= ansi_opname (BIT_NOT_EXPR
);
7276 id
= ansi_opname (TRUTH_NOT_EXPR
);
7280 id
= ansi_assopname (NOP_EXPR
);
7284 id
= ansi_opname (LT_EXPR
);
7288 id
= ansi_opname (GT_EXPR
);
7292 id
= ansi_assopname (PLUS_EXPR
);
7296 id
= ansi_assopname (MINUS_EXPR
);
7300 id
= ansi_assopname (MULT_EXPR
);
7304 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7308 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7312 id
= ansi_assopname (BIT_XOR_EXPR
);
7316 id
= ansi_assopname (BIT_AND_EXPR
);
7320 id
= ansi_assopname (BIT_IOR_EXPR
);
7324 id
= ansi_opname (LSHIFT_EXPR
);
7328 id
= ansi_opname (RSHIFT_EXPR
);
7332 id
= ansi_assopname (LSHIFT_EXPR
);
7336 id
= ansi_assopname (RSHIFT_EXPR
);
7340 id
= ansi_opname (EQ_EXPR
);
7344 id
= ansi_opname (NE_EXPR
);
7348 id
= ansi_opname (LE_EXPR
);
7351 case CPP_GREATER_EQ
:
7352 id
= ansi_opname (GE_EXPR
);
7356 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7360 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7364 id
= ansi_opname (POSTINCREMENT_EXPR
);
7367 case CPP_MINUS_MINUS
:
7368 id
= ansi_opname (PREDECREMENT_EXPR
);
7372 id
= ansi_opname (COMPOUND_EXPR
);
7375 case CPP_DEREF_STAR
:
7376 id
= ansi_opname (MEMBER_REF
);
7380 id
= ansi_opname (COMPONENT_REF
);
7383 case CPP_OPEN_PAREN
:
7384 /* Consume the `('. */
7385 cp_lexer_consume_token (parser
->lexer
);
7386 /* Look for the matching `)'. */
7387 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7388 return ansi_opname (CALL_EXPR
);
7390 case CPP_OPEN_SQUARE
:
7391 /* Consume the `['. */
7392 cp_lexer_consume_token (parser
->lexer
);
7393 /* Look for the matching `]'. */
7394 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7395 return ansi_opname (ARRAY_REF
);
7399 id
= ansi_opname (MIN_EXPR
);
7403 id
= ansi_opname (MAX_EXPR
);
7407 id
= ansi_assopname (MIN_EXPR
);
7411 id
= ansi_assopname (MAX_EXPR
);
7415 /* Anything else is an error. */
7419 /* If we have selected an identifier, we need to consume the
7422 cp_lexer_consume_token (parser
->lexer
);
7423 /* Otherwise, no valid operator name was present. */
7426 cp_parser_error (parser
, "expected operator");
7427 id
= error_mark_node
;
7433 /* Parse a template-declaration.
7435 template-declaration:
7436 export [opt] template < template-parameter-list > declaration
7438 If MEMBER_P is TRUE, this template-declaration occurs within a
7441 The grammar rule given by the standard isn't correct. What
7444 template-declaration:
7445 export [opt] template-parameter-list-seq
7446 decl-specifier-seq [opt] init-declarator [opt] ;
7447 export [opt] template-parameter-list-seq
7450 template-parameter-list-seq:
7451 template-parameter-list-seq [opt]
7452 template < template-parameter-list > */
7455 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7457 /* Check for `export'. */
7458 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7460 /* Consume the `export' token. */
7461 cp_lexer_consume_token (parser
->lexer
);
7462 /* Warn that we do not support `export'. */
7463 warning ("keyword `export' not implemented, and will be ignored");
7466 cp_parser_template_declaration_after_export (parser
, member_p
);
7469 /* Parse a template-parameter-list.
7471 template-parameter-list:
7473 template-parameter-list , template-parameter
7475 Returns a TREE_LIST. Each node represents a template parameter.
7476 The nodes are connected via their TREE_CHAINs. */
7479 cp_parser_template_parameter_list (cp_parser
* parser
)
7481 tree parameter_list
= NULL_TREE
;
7488 /* Parse the template-parameter. */
7489 parameter
= cp_parser_template_parameter (parser
);
7490 /* Add it to the list. */
7491 parameter_list
= process_template_parm (parameter_list
,
7494 /* Peek at the next token. */
7495 token
= cp_lexer_peek_token (parser
->lexer
);
7496 /* If it's not a `,', we're done. */
7497 if (token
->type
!= CPP_COMMA
)
7499 /* Otherwise, consume the `,' token. */
7500 cp_lexer_consume_token (parser
->lexer
);
7503 return parameter_list
;
7506 /* Parse a template-parameter.
7510 parameter-declaration
7512 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7513 TREE_PURPOSE is the default value, if any. */
7516 cp_parser_template_parameter (cp_parser
* parser
)
7520 /* Peek at the next token. */
7521 token
= cp_lexer_peek_token (parser
->lexer
);
7522 /* If it is `class' or `template', we have a type-parameter. */
7523 if (token
->keyword
== RID_TEMPLATE
)
7524 return cp_parser_type_parameter (parser
);
7525 /* If it is `class' or `typename' we do not know yet whether it is a
7526 type parameter or a non-type parameter. Consider:
7528 template <typename T, typename T::X X> ...
7532 template <class C, class D*> ...
7534 Here, the first parameter is a type parameter, and the second is
7535 a non-type parameter. We can tell by looking at the token after
7536 the identifier -- if it is a `,', `=', or `>' then we have a type
7538 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7540 /* Peek at the token after `class' or `typename'. */
7541 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7542 /* If it's an identifier, skip it. */
7543 if (token
->type
== CPP_NAME
)
7544 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7545 /* Now, see if the token looks like the end of a template
7547 if (token
->type
== CPP_COMMA
7548 || token
->type
== CPP_EQ
7549 || token
->type
== CPP_GREATER
)
7550 return cp_parser_type_parameter (parser
);
7553 /* Otherwise, it is a non-type parameter.
7557 When parsing a default template-argument for a non-type
7558 template-parameter, the first non-nested `>' is taken as the end
7559 of the template parameter-list rather than a greater-than
7562 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
7563 /*parenthesized_p=*/NULL
);
7566 /* Parse a type-parameter.
7569 class identifier [opt]
7570 class identifier [opt] = type-id
7571 typename identifier [opt]
7572 typename identifier [opt] = type-id
7573 template < template-parameter-list > class identifier [opt]
7574 template < template-parameter-list > class identifier [opt]
7577 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7578 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7579 the declaration of the parameter. */
7582 cp_parser_type_parameter (cp_parser
* parser
)
7587 /* Look for a keyword to tell us what kind of parameter this is. */
7588 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7589 "`class', `typename', or `template'");
7591 return error_mark_node
;
7593 switch (token
->keyword
)
7599 tree default_argument
;
7601 /* If the next token is an identifier, then it names the
7603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7604 identifier
= cp_parser_identifier (parser
);
7606 identifier
= NULL_TREE
;
7608 /* Create the parameter. */
7609 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7611 /* If the next token is an `=', we have a default argument. */
7612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7614 /* Consume the `=' token. */
7615 cp_lexer_consume_token (parser
->lexer
);
7616 /* Parse the default-argument. */
7617 default_argument
= cp_parser_type_id (parser
);
7620 default_argument
= NULL_TREE
;
7622 /* Create the combined representation of the parameter and the
7623 default argument. */
7624 parameter
= build_tree_list (default_argument
, parameter
);
7630 tree parameter_list
;
7632 tree default_argument
;
7634 /* Look for the `<'. */
7635 cp_parser_require (parser
, CPP_LESS
, "`<'");
7636 /* Parse the template-parameter-list. */
7637 begin_template_parm_list ();
7639 = cp_parser_template_parameter_list (parser
);
7640 parameter_list
= end_template_parm_list (parameter_list
);
7641 /* Look for the `>'. */
7642 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7643 /* Look for the `class' keyword. */
7644 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7645 /* If the next token is an `=', then there is a
7646 default-argument. If the next token is a `>', we are at
7647 the end of the parameter-list. If the next token is a `,',
7648 then we are at the end of this parameter. */
7649 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7650 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7651 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7652 identifier
= cp_parser_identifier (parser
);
7654 identifier
= NULL_TREE
;
7655 /* Create the template parameter. */
7656 parameter
= finish_template_template_parm (class_type_node
,
7659 /* If the next token is an `=', then there is a
7660 default-argument. */
7661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7663 /* Consume the `='. */
7664 cp_lexer_consume_token (parser
->lexer
);
7665 /* Parse the id-expression. */
7667 = cp_parser_id_expression (parser
,
7668 /*template_keyword_p=*/false,
7669 /*check_dependency_p=*/true,
7670 /*template_p=*/NULL
,
7671 /*declarator_p=*/false);
7672 /* Look up the name. */
7674 = cp_parser_lookup_name_simple (parser
, default_argument
);
7675 /* See if the default argument is valid. */
7677 = check_template_template_default_arg (default_argument
);
7680 default_argument
= NULL_TREE
;
7682 /* Create the combined representation of the parameter and the
7683 default argument. */
7684 parameter
= build_tree_list (default_argument
, parameter
);
7689 /* Anything else is an error. */
7690 cp_parser_error (parser
,
7691 "expected `class', `typename', or `template'");
7692 parameter
= error_mark_node
;
7698 /* Parse a template-id.
7701 template-name < template-argument-list [opt] >
7703 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7704 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7705 returned. Otherwise, if the template-name names a function, or set
7706 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7707 names a class, returns a TYPE_DECL for the specialization.
7709 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7710 uninstantiated templates. */
7713 cp_parser_template_id (cp_parser
*parser
,
7714 bool template_keyword_p
,
7715 bool check_dependency_p
,
7716 bool is_declaration
)
7721 ptrdiff_t start_of_id
;
7722 tree access_check
= NULL_TREE
;
7723 cp_token
*next_token
;
7726 /* If the next token corresponds to a template-id, there is no need
7728 next_token
= cp_lexer_peek_token (parser
->lexer
);
7729 if (next_token
->type
== CPP_TEMPLATE_ID
)
7734 /* Get the stored value. */
7735 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
7736 /* Perform any access checks that were deferred. */
7737 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
7738 perform_or_defer_access_check (TREE_PURPOSE (check
),
7739 TREE_VALUE (check
));
7740 /* Return the stored value. */
7741 return TREE_VALUE (value
);
7744 /* Avoid performing name lookup if there is no possibility of
7745 finding a template-id. */
7746 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
7747 || (next_token
->type
== CPP_NAME
7748 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
))
7750 cp_parser_error (parser
, "expected template-id");
7751 return error_mark_node
;
7754 /* Remember where the template-id starts. */
7755 if (cp_parser_parsing_tentatively (parser
)
7756 && !cp_parser_committed_to_tentative_parse (parser
))
7758 next_token
= cp_lexer_peek_token (parser
->lexer
);
7759 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
7760 parser
->lexer
->first_token
,
7766 push_deferring_access_checks (dk_deferred
);
7768 /* Parse the template-name. */
7769 is_identifier
= false;
7770 template = cp_parser_template_name (parser
, template_keyword_p
,
7774 if (template == error_mark_node
|| is_identifier
)
7776 pop_deferring_access_checks ();
7780 /* Look for the `<' that starts the template-argument-list. */
7781 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
7783 pop_deferring_access_checks ();
7784 return error_mark_node
;
7787 /* Parse the arguments. */
7788 arguments
= cp_parser_enclosed_template_argument_list (parser
);
7790 /* Build a representation of the specialization. */
7791 if (TREE_CODE (template) == IDENTIFIER_NODE
)
7792 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
7793 else if (DECL_CLASS_TEMPLATE_P (template)
7794 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7796 = finish_template_type (template, arguments
,
7797 cp_lexer_next_token_is (parser
->lexer
,
7801 /* If it's not a class-template or a template-template, it should be
7802 a function-template. */
7803 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7804 || TREE_CODE (template) == OVERLOAD
7805 || BASELINK_P (template)),
7808 template_id
= lookup_template_function (template, arguments
);
7811 /* Retrieve any deferred checks. Do not pop this access checks yet
7812 so the memory will not be reclaimed during token replacing below. */
7813 access_check
= get_deferred_access_checks ();
7815 /* If parsing tentatively, replace the sequence of tokens that makes
7816 up the template-id with a CPP_TEMPLATE_ID token. That way,
7817 should we re-parse the token stream, we will not have to repeat
7818 the effort required to do the parse, nor will we issue duplicate
7819 error messages about problems during instantiation of the
7821 if (start_of_id
>= 0)
7825 /* Find the token that corresponds to the start of the
7827 token
= cp_lexer_advance_token (parser
->lexer
,
7828 parser
->lexer
->first_token
,
7831 /* Reset the contents of the START_OF_ID token. */
7832 token
->type
= CPP_TEMPLATE_ID
;
7833 token
->value
= build_tree_list (access_check
, template_id
);
7834 token
->keyword
= RID_MAX
;
7835 /* Purge all subsequent tokens. */
7836 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
7839 pop_deferring_access_checks ();
7843 /* Parse a template-name.
7848 The standard should actually say:
7852 operator-function-id
7853 conversion-function-id
7855 A defect report has been filed about this issue.
7857 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7858 `template' keyword, in a construction like:
7862 In that case `f' is taken to be a template-name, even though there
7863 is no way of knowing for sure.
7865 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7866 name refers to a set of overloaded functions, at least one of which
7867 is a template, or an IDENTIFIER_NODE with the name of the template,
7868 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7869 names are looked up inside uninstantiated templates. */
7872 cp_parser_template_name (cp_parser
* parser
,
7873 bool template_keyword_p
,
7874 bool check_dependency_p
,
7875 bool is_declaration
,
7876 bool *is_identifier
)
7882 /* If the next token is `operator', then we have either an
7883 operator-function-id or a conversion-function-id. */
7884 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
7886 /* We don't know whether we're looking at an
7887 operator-function-id or a conversion-function-id. */
7888 cp_parser_parse_tentatively (parser
);
7889 /* Try an operator-function-id. */
7890 identifier
= cp_parser_operator_function_id (parser
);
7891 /* If that didn't work, try a conversion-function-id. */
7892 if (!cp_parser_parse_definitely (parser
))
7893 identifier
= cp_parser_conversion_function_id (parser
);
7895 /* Look for the identifier. */
7897 identifier
= cp_parser_identifier (parser
);
7899 /* If we didn't find an identifier, we don't have a template-id. */
7900 if (identifier
== error_mark_node
)
7901 return error_mark_node
;
7903 /* If the name immediately followed the `template' keyword, then it
7904 is a template-name. However, if the next token is not `<', then
7905 we do not treat it as a template-name, since it is not being used
7906 as part of a template-id. This enables us to handle constructs
7909 template <typename T> struct S { S(); };
7910 template <typename T> S<T>::S();
7912 correctly. We would treat `S' as a template -- if it were `S<T>'
7913 -- but we do not if there is no `<'. */
7915 if (processing_template_decl
7916 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
7918 /* In a declaration, in a dependent context, we pretend that the
7919 "template" keyword was present in order to improve error
7920 recovery. For example, given:
7922 template <typename T> void f(T::X<int>);
7924 we want to treat "X<int>" as a template-id. */
7926 && !template_keyword_p
7927 && parser
->scope
&& TYPE_P (parser
->scope
)
7928 && dependent_type_p (parser
->scope
))
7932 /* Explain what went wrong. */
7933 error ("non-template `%D' used as template", identifier
);
7934 error ("(use `%T::template %D' to indicate that it is a template)",
7935 parser
->scope
, identifier
);
7936 /* If parsing tentatively, find the location of the "<"
7938 if (cp_parser_parsing_tentatively (parser
)
7939 && !cp_parser_committed_to_tentative_parse (parser
))
7941 cp_parser_simulate_error (parser
);
7942 token
= cp_lexer_peek_token (parser
->lexer
);
7943 token
= cp_lexer_prev_token (parser
->lexer
, token
);
7944 start
= cp_lexer_token_difference (parser
->lexer
,
7945 parser
->lexer
->first_token
,
7950 /* Parse the template arguments so that we can issue error
7951 messages about them. */
7952 cp_lexer_consume_token (parser
->lexer
);
7953 cp_parser_enclosed_template_argument_list (parser
);
7954 /* Skip tokens until we find a good place from which to
7955 continue parsing. */
7956 cp_parser_skip_to_closing_parenthesis (parser
,
7957 /*recovering=*/true,
7959 /*consume_paren=*/false);
7960 /* If parsing tentatively, permanently remove the
7961 template argument list. That will prevent duplicate
7962 error messages from being issued about the missing
7963 "template" keyword. */
7966 token
= cp_lexer_advance_token (parser
->lexer
,
7967 parser
->lexer
->first_token
,
7969 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
7972 *is_identifier
= true;
7975 if (template_keyword_p
)
7979 /* Look up the name. */
7980 decl
= cp_parser_lookup_name (parser
, identifier
,
7982 /*is_namespace=*/false,
7983 check_dependency_p
);
7984 decl
= maybe_get_template_decl_from_type_decl (decl
);
7986 /* If DECL is a template, then the name was a template-name. */
7987 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7991 /* The standard does not explicitly indicate whether a name that
7992 names a set of overloaded declarations, some of which are
7993 templates, is a template-name. However, such a name should
7994 be a template-name; otherwise, there is no way to form a
7995 template-id for the overloaded templates. */
7996 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
7997 if (TREE_CODE (fns
) == OVERLOAD
)
8001 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8002 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8007 /* Otherwise, the name does not name a template. */
8008 cp_parser_error (parser
, "expected template-name");
8009 return error_mark_node
;
8013 /* If DECL is dependent, and refers to a function, then just return
8014 its name; we will look it up again during template instantiation. */
8015 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8017 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8018 if (TYPE_P (scope
) && dependent_type_p (scope
))
8025 /* Parse a template-argument-list.
8027 template-argument-list:
8029 template-argument-list , template-argument
8031 Returns a TREE_VEC containing the arguments. */
8034 cp_parser_template_argument_list (cp_parser
* parser
)
8036 tree fixed_args
[10];
8037 unsigned n_args
= 0;
8038 unsigned alloced
= 10;
8039 tree
*arg_ary
= fixed_args
;
8041 bool saved_in_template_argument_list_p
;
8043 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8044 parser
->in_template_argument_list_p
= true;
8050 /* Consume the comma. */
8051 cp_lexer_consume_token (parser
->lexer
);
8053 /* Parse the template-argument. */
8054 argument
= cp_parser_template_argument (parser
);
8055 if (n_args
== alloced
)
8059 if (arg_ary
== fixed_args
)
8061 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8062 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8065 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8067 arg_ary
[n_args
++] = argument
;
8069 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8071 vec
= make_tree_vec (n_args
);
8074 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8076 if (arg_ary
!= fixed_args
)
8078 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8082 /* Parse a template-argument.
8085 assignment-expression
8089 The representation is that of an assignment-expression, type-id, or
8090 id-expression -- except that the qualified id-expression is
8091 evaluated, so that the value returned is either a DECL or an
8094 Although the standard says "assignment-expression", it forbids
8095 throw-expressions or assignments in the template argument.
8096 Therefore, we use "conditional-expression" instead. */
8099 cp_parser_template_argument (cp_parser
* parser
)
8104 bool maybe_type_id
= false;
8107 tree qualifying_class
;
8109 /* There's really no way to know what we're looking at, so we just
8110 try each alternative in order.
8114 In a template-argument, an ambiguity between a type-id and an
8115 expression is resolved to a type-id, regardless of the form of
8116 the corresponding template-parameter.
8118 Therefore, we try a type-id first. */
8119 cp_parser_parse_tentatively (parser
);
8120 argument
= cp_parser_type_id (parser
);
8121 /* If there was no error parsing the type-id but the next token is a '>>',
8122 we probably found a typo for '> >'. But there are type-id which are
8123 also valid expressions. For instance:
8125 struct X { int operator >> (int); };
8126 template <int V> struct Foo {};
8129 Here 'X()' is a valid type-id of a function type, but the user just
8130 wanted to write the expression "X() >> 5". Thus, we remember that we
8131 found a valid type-id, but we still try to parse the argument as an
8132 expression to see what happens. */
8133 if (!cp_parser_error_occurred (parser
)
8134 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8136 maybe_type_id
= true;
8137 cp_parser_abort_tentative_parse (parser
);
8141 /* If the next token isn't a `,' or a `>', then this argument wasn't
8142 really finished. This means that the argument is not a valid
8144 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8145 cp_parser_error (parser
, "expected template-argument");
8146 /* If that worked, we're done. */
8147 if (cp_parser_parse_definitely (parser
))
8150 /* We're still not sure what the argument will be. */
8151 cp_parser_parse_tentatively (parser
);
8152 /* Try a template. */
8153 argument
= cp_parser_id_expression (parser
,
8154 /*template_keyword_p=*/false,
8155 /*check_dependency_p=*/true,
8157 /*declarator_p=*/false);
8158 /* If the next token isn't a `,' or a `>', then this argument wasn't
8160 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8161 cp_parser_error (parser
, "expected template-argument");
8162 if (!cp_parser_error_occurred (parser
))
8164 /* Figure out what is being referred to. */
8165 argument
= cp_parser_lookup_name_simple (parser
, argument
);
8167 argument
= make_unbound_class_template (TREE_OPERAND (argument
, 0),
8168 TREE_OPERAND (argument
, 1),
8170 else if (TREE_CODE (argument
) != TEMPLATE_DECL
)
8171 cp_parser_error (parser
, "expected template-name");
8173 if (cp_parser_parse_definitely (parser
))
8175 /* It must be a non-type argument. There permitted cases are given
8176 in [temp.arg.nontype]:
8178 -- an integral constant-expression of integral or enumeration
8181 -- the name of a non-type template-parameter; or
8183 -- the name of an object or function with external linkage...
8185 -- the address of an object or function with external linkage...
8187 -- a pointer to member... */
8188 /* Look for a non-type template parameter. */
8189 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8191 cp_parser_parse_tentatively (parser
);
8192 argument
= cp_parser_primary_expression (parser
,
8195 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8196 || !cp_parser_next_token_ends_template_argument_p (parser
))
8197 cp_parser_simulate_error (parser
);
8198 if (cp_parser_parse_definitely (parser
))
8201 /* If the next token is "&", the argument must be the address of an
8202 object or function with external linkage. */
8203 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8205 cp_lexer_consume_token (parser
->lexer
);
8206 /* See if we might have an id-expression. */
8207 token
= cp_lexer_peek_token (parser
->lexer
);
8208 if (token
->type
== CPP_NAME
8209 || token
->keyword
== RID_OPERATOR
8210 || token
->type
== CPP_SCOPE
8211 || token
->type
== CPP_TEMPLATE_ID
8212 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8214 cp_parser_parse_tentatively (parser
);
8215 argument
= cp_parser_primary_expression (parser
,
8218 if (cp_parser_error_occurred (parser
)
8219 || !cp_parser_next_token_ends_template_argument_p (parser
))
8220 cp_parser_abort_tentative_parse (parser
);
8223 if (qualifying_class
)
8224 argument
= finish_qualified_id_expr (qualifying_class
,
8228 if (TREE_CODE (argument
) == VAR_DECL
)
8230 /* A variable without external linkage might still be a
8231 valid constant-expression, so no error is issued here
8232 if the external-linkage check fails. */
8233 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8234 cp_parser_simulate_error (parser
);
8236 else if (is_overloaded_fn (argument
))
8237 /* All overloaded functions are allowed; if the external
8238 linkage test does not pass, an error will be issued
8242 && (TREE_CODE (argument
) == OFFSET_REF
8243 || TREE_CODE (argument
) == SCOPE_REF
))
8244 /* A pointer-to-member. */
8247 cp_parser_simulate_error (parser
);
8249 if (cp_parser_parse_definitely (parser
))
8252 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8257 /* If the argument started with "&", there are no other valid
8258 alternatives at this point. */
8261 cp_parser_error (parser
, "invalid non-type template argument");
8262 return error_mark_node
;
8264 /* If the argument wasn't successfully parsed as a type-id followed
8265 by '>>', the argument can only be a constant expression now.
8266 Otherwise, we try parsing the constant-expression tentatively,
8267 because the argument could really be a type-id. */
8269 cp_parser_parse_tentatively (parser
);
8270 argument
= cp_parser_constant_expression (parser
,
8271 /*allow_non_constant_p=*/false,
8272 /*non_constant_p=*/NULL
);
8273 argument
= cp_parser_fold_non_dependent_expr (argument
);
8276 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8277 cp_parser_error (parser
, "expected template-argument");
8278 if (cp_parser_parse_definitely (parser
))
8280 /* We did our best to parse the argument as a non type-id, but that
8281 was the only alternative that matched (albeit with a '>' after
8282 it). We can assume it's just a typo from the user, and a
8283 diagnostic will then be issued. */
8284 return cp_parser_type_id (parser
);
8287 /* Parse an explicit-instantiation.
8289 explicit-instantiation:
8290 template declaration
8292 Although the standard says `declaration', what it really means is:
8294 explicit-instantiation:
8295 template decl-specifier-seq [opt] declarator [opt] ;
8297 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8298 supposed to be allowed. A defect report has been filed about this
8303 explicit-instantiation:
8304 storage-class-specifier template
8305 decl-specifier-seq [opt] declarator [opt] ;
8306 function-specifier template
8307 decl-specifier-seq [opt] declarator [opt] ; */
8310 cp_parser_explicit_instantiation (cp_parser
* parser
)
8312 int declares_class_or_enum
;
8313 tree decl_specifiers
;
8315 tree extension_specifier
= NULL_TREE
;
8317 /* Look for an (optional) storage-class-specifier or
8318 function-specifier. */
8319 if (cp_parser_allow_gnu_extensions_p (parser
))
8322 = cp_parser_storage_class_specifier_opt (parser
);
8323 if (!extension_specifier
)
8324 extension_specifier
= cp_parser_function_specifier_opt (parser
);
8327 /* Look for the `template' keyword. */
8328 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8329 /* Let the front end know that we are processing an explicit
8331 begin_explicit_instantiation ();
8332 /* [temp.explicit] says that we are supposed to ignore access
8333 control while processing explicit instantiation directives. */
8334 push_deferring_access_checks (dk_no_check
);
8335 /* Parse a decl-specifier-seq. */
8337 = cp_parser_decl_specifier_seq (parser
,
8338 CP_PARSER_FLAGS_OPTIONAL
,
8340 &declares_class_or_enum
);
8341 /* If there was exactly one decl-specifier, and it declared a class,
8342 and there's no declarator, then we have an explicit type
8344 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8348 type
= check_tag_decl (decl_specifiers
);
8349 /* Turn access control back on for names used during
8350 template instantiation. */
8351 pop_deferring_access_checks ();
8353 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8360 /* Parse the declarator. */
8362 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8363 /*ctor_dtor_or_conv_p=*/NULL
,
8364 /*parenthesized_p=*/NULL
);
8365 cp_parser_check_for_definition_in_return_type (declarator
,
8366 declares_class_or_enum
);
8367 decl
= grokdeclarator (declarator
, decl_specifiers
,
8369 /* Turn access control back on for names used during
8370 template instantiation. */
8371 pop_deferring_access_checks ();
8372 /* Do the explicit instantiation. */
8373 do_decl_instantiation (decl
, extension_specifier
);
8375 /* We're done with the instantiation. */
8376 end_explicit_instantiation ();
8378 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8381 /* Parse an explicit-specialization.
8383 explicit-specialization:
8384 template < > declaration
8386 Although the standard says `declaration', what it really means is:
8388 explicit-specialization:
8389 template <> decl-specifier [opt] init-declarator [opt] ;
8390 template <> function-definition
8391 template <> explicit-specialization
8392 template <> template-declaration */
8395 cp_parser_explicit_specialization (cp_parser
* parser
)
8397 /* Look for the `template' keyword. */
8398 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8399 /* Look for the `<'. */
8400 cp_parser_require (parser
, CPP_LESS
, "`<'");
8401 /* Look for the `>'. */
8402 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8403 /* We have processed another parameter list. */
8404 ++parser
->num_template_parameter_lists
;
8405 /* Let the front end know that we are beginning a specialization. */
8406 begin_specialization ();
8408 /* If the next keyword is `template', we need to figure out whether
8409 or not we're looking a template-declaration. */
8410 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8412 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8413 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8414 cp_parser_template_declaration_after_export (parser
,
8415 /*member_p=*/false);
8417 cp_parser_explicit_specialization (parser
);
8420 /* Parse the dependent declaration. */
8421 cp_parser_single_declaration (parser
,
8425 /* We're done with the specialization. */
8426 end_specialization ();
8427 /* We're done with this parameter list. */
8428 --parser
->num_template_parameter_lists
;
8431 /* Parse a type-specifier.
8434 simple-type-specifier
8437 elaborated-type-specifier
8445 Returns a representation of the type-specifier. If the
8446 type-specifier is a keyword (like `int' or `const', or
8447 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8448 For a class-specifier, enum-specifier, or elaborated-type-specifier
8449 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8451 If IS_FRIEND is TRUE then this type-specifier is being declared a
8452 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8453 appearing in a decl-specifier-seq.
8455 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8456 class-specifier, enum-specifier, or elaborated-type-specifier, then
8457 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8458 if a type is declared; 2 if it is defined. Otherwise, it is set to
8461 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8462 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8466 cp_parser_type_specifier (cp_parser
* parser
,
8467 cp_parser_flags flags
,
8469 bool is_declaration
,
8470 int* declares_class_or_enum
,
8471 bool* is_cv_qualifier
)
8473 tree type_spec
= NULL_TREE
;
8477 /* Assume this type-specifier does not declare a new type. */
8478 if (declares_class_or_enum
)
8479 *declares_class_or_enum
= false;
8480 /* And that it does not specify a cv-qualifier. */
8481 if (is_cv_qualifier
)
8482 *is_cv_qualifier
= false;
8483 /* Peek at the next token. */
8484 token
= cp_lexer_peek_token (parser
->lexer
);
8486 /* If we're looking at a keyword, we can use that to guide the
8487 production we choose. */
8488 keyword
= token
->keyword
;
8491 /* Any of these indicate either a class-specifier, or an
8492 elaborated-type-specifier. */
8497 /* Parse tentatively so that we can back up if we don't find a
8498 class-specifier or enum-specifier. */
8499 cp_parser_parse_tentatively (parser
);
8500 /* Look for the class-specifier or enum-specifier. */
8501 if (keyword
== RID_ENUM
)
8502 type_spec
= cp_parser_enum_specifier (parser
);
8504 type_spec
= cp_parser_class_specifier (parser
);
8506 /* If that worked, we're done. */
8507 if (cp_parser_parse_definitely (parser
))
8509 if (declares_class_or_enum
)
8510 *declares_class_or_enum
= 2;
8517 /* Look for an elaborated-type-specifier. */
8518 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8521 /* We're declaring a class or enum -- unless we're using
8523 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8524 *declares_class_or_enum
= 1;
8530 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8531 /* Even though we call a routine that looks for an optional
8532 qualifier, we know that there should be one. */
8533 my_friendly_assert (type_spec
!= NULL
, 20000328);
8534 /* This type-specifier was a cv-qualified. */
8535 if (is_cv_qualifier
)
8536 *is_cv_qualifier
= true;
8541 /* The `__complex__' keyword is a GNU extension. */
8542 return cp_lexer_consume_token (parser
->lexer
)->value
;
8548 /* If we do not already have a type-specifier, assume we are looking
8549 at a simple-type-specifier. */
8550 type_spec
= cp_parser_simple_type_specifier (parser
, flags
,
8551 /*identifier_p=*/true);
8553 /* If we didn't find a type-specifier, and a type-specifier was not
8554 optional in this context, issue an error message. */
8555 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8557 cp_parser_error (parser
, "expected type specifier");
8558 return error_mark_node
;
8564 /* Parse a simple-type-specifier.
8566 simple-type-specifier:
8567 :: [opt] nested-name-specifier [opt] type-name
8568 :: [opt] nested-name-specifier template template-id
8583 simple-type-specifier:
8584 __typeof__ unary-expression
8585 __typeof__ ( type-id )
8587 For the various keywords, the value returned is simply the
8588 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8589 For the first two productions, and if IDENTIFIER_P is false, the
8590 value returned is the indicated TYPE_DECL. */
8593 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
,
8596 tree type
= NULL_TREE
;
8599 /* Peek at the next token. */
8600 token
= cp_lexer_peek_token (parser
->lexer
);
8602 /* If we're looking at a keyword, things are easy. */
8603 switch (token
->keyword
)
8606 type
= char_type_node
;
8609 type
= wchar_type_node
;
8612 type
= boolean_type_node
;
8615 type
= short_integer_type_node
;
8618 type
= integer_type_node
;
8621 type
= long_integer_type_node
;
8624 type
= integer_type_node
;
8627 type
= unsigned_type_node
;
8630 type
= float_type_node
;
8633 type
= double_type_node
;
8636 type
= void_type_node
;
8643 /* Consume the `typeof' token. */
8644 cp_lexer_consume_token (parser
->lexer
);
8645 /* Parse the operand to `typeof'. */
8646 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
8647 /* If it is not already a TYPE, take its type. */
8648 if (!TYPE_P (operand
))
8649 operand
= finish_typeof (operand
);
8658 /* If the type-specifier was for a built-in type, we're done. */
8663 /* Consume the token. */
8664 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
8665 return identifier_p
? id
: TYPE_NAME (type
);
8668 /* The type-specifier must be a user-defined type. */
8669 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
8671 /* Don't gobble tokens or issue error messages if this is an
8672 optional type-specifier. */
8673 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
8674 cp_parser_parse_tentatively (parser
);
8676 /* Look for the optional `::' operator. */
8677 cp_parser_global_scope_opt (parser
,
8678 /*current_scope_valid_p=*/false);
8679 /* Look for the nested-name specifier. */
8680 cp_parser_nested_name_specifier_opt (parser
,
8681 /*typename_keyword_p=*/false,
8682 /*check_dependency_p=*/true,
8684 /*is_declaration=*/false);
8685 /* If we have seen a nested-name-specifier, and the next token
8686 is `template', then we are using the template-id production. */
8688 && cp_parser_optional_template_keyword (parser
))
8690 /* Look for the template-id. */
8691 type
= cp_parser_template_id (parser
,
8692 /*template_keyword_p=*/true,
8693 /*check_dependency_p=*/true,
8694 /*is_declaration=*/false);
8695 /* If the template-id did not name a type, we are out of
8697 if (TREE_CODE (type
) != TYPE_DECL
)
8699 cp_parser_error (parser
, "expected template-id for type");
8703 /* Otherwise, look for a type-name. */
8705 type
= cp_parser_type_name (parser
);
8706 /* If it didn't work out, we don't have a TYPE. */
8707 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
8708 && !cp_parser_parse_definitely (parser
))
8712 /* If we didn't get a type-name, issue an error message. */
8713 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8715 cp_parser_error (parser
, "expected type-name");
8716 return error_mark_node
;
8719 /* There is no valid C++ program where a non-template type is
8720 followed by a "<". That usually indicates that the user thought
8721 that the type was a template. */
8722 if (type
&& type
!= error_mark_node
)
8723 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
8728 /* Parse a type-name.
8741 Returns a TYPE_DECL for the the type. */
8744 cp_parser_type_name (cp_parser
* parser
)
8749 /* We can't know yet whether it is a class-name or not. */
8750 cp_parser_parse_tentatively (parser
);
8751 /* Try a class-name. */
8752 type_decl
= cp_parser_class_name (parser
,
8753 /*typename_keyword_p=*/false,
8754 /*template_keyword_p=*/false,
8756 /*check_dependency_p=*/true,
8757 /*class_head_p=*/false,
8758 /*is_declaration=*/false);
8759 /* If it's not a class-name, keep looking. */
8760 if (!cp_parser_parse_definitely (parser
))
8762 /* It must be a typedef-name or an enum-name. */
8763 identifier
= cp_parser_identifier (parser
);
8764 if (identifier
== error_mark_node
)
8765 return error_mark_node
;
8767 /* Look up the type-name. */
8768 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
8769 /* Issue an error if we did not find a type-name. */
8770 if (TREE_CODE (type_decl
) != TYPE_DECL
)
8772 if (!cp_parser_simulate_error (parser
))
8773 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
8775 type_decl
= error_mark_node
;
8777 /* Remember that the name was used in the definition of the
8778 current class so that we can check later to see if the
8779 meaning would have been different after the class was
8780 entirely defined. */
8781 else if (type_decl
!= error_mark_node
8783 maybe_note_name_used_in_class (identifier
, type_decl
);
8790 /* Parse an elaborated-type-specifier. Note that the grammar given
8791 here incorporates the resolution to DR68.
8793 elaborated-type-specifier:
8794 class-key :: [opt] nested-name-specifier [opt] identifier
8795 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8796 enum :: [opt] nested-name-specifier [opt] identifier
8797 typename :: [opt] nested-name-specifier identifier
8798 typename :: [opt] nested-name-specifier template [opt]
8803 elaborated-type-specifier:
8804 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8805 class-key attributes :: [opt] nested-name-specifier [opt]
8806 template [opt] template-id
8807 enum attributes :: [opt] nested-name-specifier [opt] identifier
8809 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8810 declared `friend'. If IS_DECLARATION is TRUE, then this
8811 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8812 something is being declared.
8814 Returns the TYPE specified. */
8817 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
8819 bool is_declaration
)
8821 enum tag_types tag_type
;
8823 tree type
= NULL_TREE
;
8824 tree attributes
= NULL_TREE
;
8826 /* See if we're looking at the `enum' keyword. */
8827 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
8829 /* Consume the `enum' token. */
8830 cp_lexer_consume_token (parser
->lexer
);
8831 /* Remember that it's an enumeration type. */
8832 tag_type
= enum_type
;
8833 /* Parse the attributes. */
8834 attributes
= cp_parser_attributes_opt (parser
);
8836 /* Or, it might be `typename'. */
8837 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8840 /* Consume the `typename' token. */
8841 cp_lexer_consume_token (parser
->lexer
);
8842 /* Remember that it's a `typename' type. */
8843 tag_type
= typename_type
;
8844 /* The `typename' keyword is only allowed in templates. */
8845 if (!processing_template_decl
)
8846 pedwarn ("using `typename' outside of template");
8848 /* Otherwise it must be a class-key. */
8851 tag_type
= cp_parser_class_key (parser
);
8852 if (tag_type
== none_type
)
8853 return error_mark_node
;
8854 /* Parse the attributes. */
8855 attributes
= cp_parser_attributes_opt (parser
);
8858 /* Look for the `::' operator. */
8859 cp_parser_global_scope_opt (parser
,
8860 /*current_scope_valid_p=*/false);
8861 /* Look for the nested-name-specifier. */
8862 if (tag_type
== typename_type
)
8864 if (cp_parser_nested_name_specifier (parser
,
8865 /*typename_keyword_p=*/true,
8866 /*check_dependency_p=*/true,
8870 return error_mark_node
;
8873 /* Even though `typename' is not present, the proposed resolution
8874 to Core Issue 180 says that in `class A<T>::B', `B' should be
8875 considered a type-name, even if `A<T>' is dependent. */
8876 cp_parser_nested_name_specifier_opt (parser
,
8877 /*typename_keyword_p=*/true,
8878 /*check_dependency_p=*/true,
8881 /* For everything but enumeration types, consider a template-id. */
8882 if (tag_type
!= enum_type
)
8884 bool template_p
= false;
8887 /* Allow the `template' keyword. */
8888 template_p
= cp_parser_optional_template_keyword (parser
);
8889 /* If we didn't see `template', we don't know if there's a
8890 template-id or not. */
8892 cp_parser_parse_tentatively (parser
);
8893 /* Parse the template-id. */
8894 decl
= cp_parser_template_id (parser
, template_p
,
8895 /*check_dependency_p=*/true,
8897 /* If we didn't find a template-id, look for an ordinary
8899 if (!template_p
&& !cp_parser_parse_definitely (parser
))
8901 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8902 in effect, then we must assume that, upon instantiation, the
8903 template will correspond to a class. */
8904 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
8905 && tag_type
== typename_type
)
8906 type
= make_typename_type (parser
->scope
, decl
,
8909 type
= TREE_TYPE (decl
);
8912 /* For an enumeration type, consider only a plain identifier. */
8915 identifier
= cp_parser_identifier (parser
);
8917 if (identifier
== error_mark_node
)
8919 parser
->scope
= NULL_TREE
;
8920 return error_mark_node
;
8923 /* For a `typename', we needn't call xref_tag. */
8924 if (tag_type
== typename_type
)
8925 return make_typename_type (parser
->scope
, identifier
,
8927 /* Look up a qualified name in the usual way. */
8932 /* In an elaborated-type-specifier, names are assumed to name
8933 types, so we set IS_TYPE to TRUE when calling
8934 cp_parser_lookup_name. */
8935 decl
= cp_parser_lookup_name (parser
, identifier
,
8937 /*is_namespace=*/false,
8938 /*check_dependency=*/true);
8940 /* If we are parsing friend declaration, DECL may be a
8941 TEMPLATE_DECL tree node here. However, we need to check
8942 whether this TEMPLATE_DECL results in valid code. Consider
8943 the following example:
8946 template <class T> class C {};
8949 template <class T> friend class N::C; // #1, valid code
8951 template <class T> class Y {
8952 friend class N::C; // #2, invalid code
8955 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8956 name lookup of `N::C'. We see that friend declaration must
8957 be template for the code to be valid. Note that
8958 processing_template_decl does not work here since it is
8959 always 1 for the above two cases. */
8961 decl
= (cp_parser_maybe_treat_template_as_class
8962 (decl
, /*tag_name_p=*/is_friend
8963 && parser
->num_template_parameter_lists
));
8965 if (TREE_CODE (decl
) != TYPE_DECL
)
8967 error ("expected type-name");
8968 return error_mark_node
;
8971 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
8972 check_elaborated_type_specifier
8974 (parser
->num_template_parameter_lists
8975 || DECL_SELF_REFERENCE_P (decl
)));
8977 type
= TREE_TYPE (decl
);
8981 /* An elaborated-type-specifier sometimes introduces a new type and
8982 sometimes names an existing type. Normally, the rule is that it
8983 introduces a new type only if there is not an existing type of
8984 the same name already in scope. For example, given:
8987 void f() { struct S s; }
8989 the `struct S' in the body of `f' is the same `struct S' as in
8990 the global scope; the existing definition is used. However, if
8991 there were no global declaration, this would introduce a new
8992 local class named `S'.
8994 An exception to this rule applies to the following code:
8996 namespace N { struct S; }
8998 Here, the elaborated-type-specifier names a new type
8999 unconditionally; even if there is already an `S' in the
9000 containing scope this declaration names a new type.
9001 This exception only applies if the elaborated-type-specifier
9002 forms the complete declaration:
9006 A declaration consisting solely of `class-key identifier ;' is
9007 either a redeclaration of the name in the current scope or a
9008 forward declaration of the identifier as a class name. It
9009 introduces the name into the current scope.
9011 We are in this situation precisely when the next token is a `;'.
9013 An exception to the exception is that a `friend' declaration does
9014 *not* name a new type; i.e., given:
9016 struct S { friend struct T; };
9018 `T' is not a new type in the scope of `S'.
9020 Also, `new struct S' or `sizeof (struct S)' never results in the
9021 definition of a new type; a new type can only be declared in a
9022 declaration context. */
9024 type
= xref_tag (tag_type
, identifier
,
9028 || cp_lexer_next_token_is_not (parser
->lexer
,
9030 parser
->num_template_parameter_lists
);
9033 if (tag_type
!= enum_type
)
9034 cp_parser_check_class_key (tag_type
, type
);
9036 /* A "<" cannot follow an elaborated type specifier. If that
9037 happens, the user was probably trying to form a template-id. */
9038 cp_parser_check_for_invalid_template_id (parser
, type
);
9043 /* Parse an enum-specifier.
9046 enum identifier [opt] { enumerator-list [opt] }
9048 Returns an ENUM_TYPE representing the enumeration. */
9051 cp_parser_enum_specifier (cp_parser
* parser
)
9054 tree identifier
= NULL_TREE
;
9057 /* Look for the `enum' keyword. */
9058 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
9059 return error_mark_node
;
9060 /* Peek at the next token. */
9061 token
= cp_lexer_peek_token (parser
->lexer
);
9063 /* See if it is an identifier. */
9064 if (token
->type
== CPP_NAME
)
9065 identifier
= cp_parser_identifier (parser
);
9067 /* Look for the `{'. */
9068 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
9069 return error_mark_node
;
9071 /* At this point, we're going ahead with the enum-specifier, even
9072 if some other problem occurs. */
9073 cp_parser_commit_to_tentative_parse (parser
);
9075 /* Issue an error message if type-definitions are forbidden here. */
9076 cp_parser_check_type_definition (parser
);
9078 /* Create the new type. */
9079 type
= start_enum (identifier
? identifier
: make_anon_name ());
9081 /* Peek at the next token. */
9082 token
= cp_lexer_peek_token (parser
->lexer
);
9083 /* If it's not a `}', then there are some enumerators. */
9084 if (token
->type
!= CPP_CLOSE_BRACE
)
9085 cp_parser_enumerator_list (parser
, type
);
9086 /* Look for the `}'. */
9087 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9089 /* Finish up the enumeration. */
9095 /* Parse an enumerator-list. The enumerators all have the indicated
9099 enumerator-definition
9100 enumerator-list , enumerator-definition */
9103 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9109 /* Parse an enumerator-definition. */
9110 cp_parser_enumerator_definition (parser
, type
);
9111 /* Peek at the next token. */
9112 token
= cp_lexer_peek_token (parser
->lexer
);
9113 /* If it's not a `,', then we've reached the end of the
9115 if (token
->type
!= CPP_COMMA
)
9117 /* Otherwise, consume the `,' and keep going. */
9118 cp_lexer_consume_token (parser
->lexer
);
9119 /* If the next token is a `}', there is a trailing comma. */
9120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9122 if (pedantic
&& !in_system_header
)
9123 pedwarn ("comma at end of enumerator list");
9129 /* Parse an enumerator-definition. The enumerator has the indicated
9132 enumerator-definition:
9134 enumerator = constant-expression
9140 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9146 /* Look for the identifier. */
9147 identifier
= cp_parser_identifier (parser
);
9148 if (identifier
== error_mark_node
)
9151 /* Peek at the next token. */
9152 token
= cp_lexer_peek_token (parser
->lexer
);
9153 /* If it's an `=', then there's an explicit value. */
9154 if (token
->type
== CPP_EQ
)
9156 /* Consume the `=' token. */
9157 cp_lexer_consume_token (parser
->lexer
);
9158 /* Parse the value. */
9159 value
= cp_parser_constant_expression (parser
,
9160 /*allow_non_constant_p=*/false,
9166 /* Create the enumerator. */
9167 build_enumerator (identifier
, value
, type
);
9170 /* Parse a namespace-name.
9173 original-namespace-name
9176 Returns the NAMESPACE_DECL for the namespace. */
9179 cp_parser_namespace_name (cp_parser
* parser
)
9182 tree namespace_decl
;
9184 /* Get the name of the namespace. */
9185 identifier
= cp_parser_identifier (parser
);
9186 if (identifier
== error_mark_node
)
9187 return error_mark_node
;
9189 /* Look up the identifier in the currently active scope. Look only
9190 for namespaces, due to:
9194 When looking up a namespace-name in a using-directive or alias
9195 definition, only namespace names are considered.
9201 During the lookup of a name preceding the :: scope resolution
9202 operator, object, function, and enumerator names are ignored.
9204 (Note that cp_parser_class_or_namespace_name only calls this
9205 function if the token after the name is the scope resolution
9207 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
9209 /*is_namespace=*/true,
9210 /*check_dependency=*/true);
9211 /* If it's not a namespace, issue an error. */
9212 if (namespace_decl
== error_mark_node
9213 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
9215 cp_parser_error (parser
, "expected namespace-name");
9216 namespace_decl
= error_mark_node
;
9219 return namespace_decl
;
9222 /* Parse a namespace-definition.
9224 namespace-definition:
9225 named-namespace-definition
9226 unnamed-namespace-definition
9228 named-namespace-definition:
9229 original-namespace-definition
9230 extension-namespace-definition
9232 original-namespace-definition:
9233 namespace identifier { namespace-body }
9235 extension-namespace-definition:
9236 namespace original-namespace-name { namespace-body }
9238 unnamed-namespace-definition:
9239 namespace { namespace-body } */
9242 cp_parser_namespace_definition (cp_parser
* parser
)
9246 /* Look for the `namespace' keyword. */
9247 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9249 /* Get the name of the namespace. We do not attempt to distinguish
9250 between an original-namespace-definition and an
9251 extension-namespace-definition at this point. The semantic
9252 analysis routines are responsible for that. */
9253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9254 identifier
= cp_parser_identifier (parser
);
9256 identifier
= NULL_TREE
;
9258 /* Look for the `{' to start the namespace. */
9259 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
9260 /* Start the namespace. */
9261 push_namespace (identifier
);
9262 /* Parse the body of the namespace. */
9263 cp_parser_namespace_body (parser
);
9264 /* Finish the namespace. */
9266 /* Look for the final `}'. */
9267 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9270 /* Parse a namespace-body.
9273 declaration-seq [opt] */
9276 cp_parser_namespace_body (cp_parser
* parser
)
9278 cp_parser_declaration_seq_opt (parser
);
9281 /* Parse a namespace-alias-definition.
9283 namespace-alias-definition:
9284 namespace identifier = qualified-namespace-specifier ; */
9287 cp_parser_namespace_alias_definition (cp_parser
* parser
)
9290 tree namespace_specifier
;
9292 /* Look for the `namespace' keyword. */
9293 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9294 /* Look for the identifier. */
9295 identifier
= cp_parser_identifier (parser
);
9296 if (identifier
== error_mark_node
)
9298 /* Look for the `=' token. */
9299 cp_parser_require (parser
, CPP_EQ
, "`='");
9300 /* Look for the qualified-namespace-specifier. */
9302 = cp_parser_qualified_namespace_specifier (parser
);
9303 /* Look for the `;' token. */
9304 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9306 /* Register the alias in the symbol table. */
9307 do_namespace_alias (identifier
, namespace_specifier
);
9310 /* Parse a qualified-namespace-specifier.
9312 qualified-namespace-specifier:
9313 :: [opt] nested-name-specifier [opt] namespace-name
9315 Returns a NAMESPACE_DECL corresponding to the specified
9319 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
9321 /* Look for the optional `::'. */
9322 cp_parser_global_scope_opt (parser
,
9323 /*current_scope_valid_p=*/false);
9325 /* Look for the optional nested-name-specifier. */
9326 cp_parser_nested_name_specifier_opt (parser
,
9327 /*typename_keyword_p=*/false,
9328 /*check_dependency_p=*/true,
9330 /*is_declaration=*/true);
9332 return cp_parser_namespace_name (parser
);
9335 /* Parse a using-declaration.
9338 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9339 using :: unqualified-id ; */
9342 cp_parser_using_declaration (cp_parser
* parser
)
9345 bool typename_p
= false;
9346 bool global_scope_p
;
9351 /* Look for the `using' keyword. */
9352 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9354 /* Peek at the next token. */
9355 token
= cp_lexer_peek_token (parser
->lexer
);
9356 /* See if it's `typename'. */
9357 if (token
->keyword
== RID_TYPENAME
)
9359 /* Remember that we've seen it. */
9361 /* Consume the `typename' token. */
9362 cp_lexer_consume_token (parser
->lexer
);
9365 /* Look for the optional global scope qualification. */
9367 = (cp_parser_global_scope_opt (parser
,
9368 /*current_scope_valid_p=*/false)
9371 /* If we saw `typename', or didn't see `::', then there must be a
9372 nested-name-specifier present. */
9373 if (typename_p
|| !global_scope_p
)
9374 cp_parser_nested_name_specifier (parser
, typename_p
,
9375 /*check_dependency_p=*/true,
9377 /*is_declaration=*/true);
9378 /* Otherwise, we could be in either of the two productions. In that
9379 case, treat the nested-name-specifier as optional. */
9381 cp_parser_nested_name_specifier_opt (parser
,
9382 /*typename_keyword_p=*/false,
9383 /*check_dependency_p=*/true,
9385 /*is_declaration=*/true);
9387 /* Parse the unqualified-id. */
9388 identifier
= cp_parser_unqualified_id (parser
,
9389 /*template_keyword_p=*/false,
9390 /*check_dependency_p=*/true,
9391 /*declarator_p=*/true);
9393 /* The function we call to handle a using-declaration is different
9394 depending on what scope we are in. */
9395 if (identifier
== error_mark_node
)
9397 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
9398 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
9399 /* [namespace.udecl]
9401 A using declaration shall not name a template-id. */
9402 error ("a template-id may not appear in a using-declaration");
9405 scope
= current_scope ();
9406 if (scope
&& TYPE_P (scope
))
9408 /* Create the USING_DECL. */
9409 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9412 /* Add it to the list of members in this class. */
9413 finish_member_declaration (decl
);
9417 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9418 if (decl
== error_mark_node
)
9419 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
9421 do_local_using_decl (decl
);
9423 do_toplevel_using_decl (decl
);
9427 /* Look for the final `;'. */
9428 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9431 /* Parse a using-directive.
9434 using namespace :: [opt] nested-name-specifier [opt]
9438 cp_parser_using_directive (cp_parser
* parser
)
9440 tree namespace_decl
;
9443 /* Look for the `using' keyword. */
9444 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9445 /* And the `namespace' keyword. */
9446 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9447 /* Look for the optional `::' operator. */
9448 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9449 /* And the optional nested-name-specifier. */
9450 cp_parser_nested_name_specifier_opt (parser
,
9451 /*typename_keyword_p=*/false,
9452 /*check_dependency_p=*/true,
9454 /*is_declaration=*/true);
9455 /* Get the namespace being used. */
9456 namespace_decl
= cp_parser_namespace_name (parser
);
9457 /* And any specified attributes. */
9458 attribs
= cp_parser_attributes_opt (parser
);
9459 /* Update the symbol table. */
9460 parse_using_directive (namespace_decl
, attribs
);
9461 /* Look for the final `;'. */
9462 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9465 /* Parse an asm-definition.
9468 asm ( string-literal ) ;
9473 asm volatile [opt] ( string-literal ) ;
9474 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9475 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9476 : asm-operand-list [opt] ) ;
9477 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9478 : asm-operand-list [opt]
9479 : asm-operand-list [opt] ) ; */
9482 cp_parser_asm_definition (cp_parser
* parser
)
9486 tree outputs
= NULL_TREE
;
9487 tree inputs
= NULL_TREE
;
9488 tree clobbers
= NULL_TREE
;
9490 bool volatile_p
= false;
9491 bool extended_p
= false;
9493 /* Look for the `asm' keyword. */
9494 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9495 /* See if the next token is `volatile'. */
9496 if (cp_parser_allow_gnu_extensions_p (parser
)
9497 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9499 /* Remember that we saw the `volatile' keyword. */
9501 /* Consume the token. */
9502 cp_lexer_consume_token (parser
->lexer
);
9504 /* Look for the opening `('. */
9505 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9506 /* Look for the string. */
9507 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9510 string
= token
->value
;
9511 /* If we're allowing GNU extensions, check for the extended assembly
9512 syntax. Unfortunately, the `:' tokens need not be separated by
9513 a space in C, and so, for compatibility, we tolerate that here
9514 too. Doing that means that we have to treat the `::' operator as
9516 if (cp_parser_allow_gnu_extensions_p (parser
)
9517 && at_function_scope_p ()
9518 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9519 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9521 bool inputs_p
= false;
9522 bool clobbers_p
= false;
9524 /* The extended syntax was used. */
9527 /* Look for outputs. */
9528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9530 /* Consume the `:'. */
9531 cp_lexer_consume_token (parser
->lexer
);
9532 /* Parse the output-operands. */
9533 if (cp_lexer_next_token_is_not (parser
->lexer
,
9535 && cp_lexer_next_token_is_not (parser
->lexer
,
9537 && cp_lexer_next_token_is_not (parser
->lexer
,
9539 outputs
= cp_parser_asm_operand_list (parser
);
9541 /* If the next token is `::', there are no outputs, and the
9542 next token is the beginning of the inputs. */
9543 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9545 /* Consume the `::' token. */
9546 cp_lexer_consume_token (parser
->lexer
);
9547 /* The inputs are coming next. */
9551 /* Look for inputs. */
9553 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9556 /* Consume the `:'. */
9557 cp_lexer_consume_token (parser
->lexer
);
9558 /* Parse the output-operands. */
9559 if (cp_lexer_next_token_is_not (parser
->lexer
,
9561 && cp_lexer_next_token_is_not (parser
->lexer
,
9563 && cp_lexer_next_token_is_not (parser
->lexer
,
9565 inputs
= cp_parser_asm_operand_list (parser
);
9567 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9568 /* The clobbers are coming next. */
9571 /* Look for clobbers. */
9573 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9576 /* Consume the `:'. */
9577 cp_lexer_consume_token (parser
->lexer
);
9578 /* Parse the clobbers. */
9579 if (cp_lexer_next_token_is_not (parser
->lexer
,
9581 clobbers
= cp_parser_asm_clobber_list (parser
);
9584 /* Look for the closing `)'. */
9585 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9586 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9587 /*consume_paren=*/true);
9588 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9590 /* Create the ASM_STMT. */
9591 if (at_function_scope_p ())
9594 finish_asm_stmt (volatile_p
9595 ? ridpointers
[(int) RID_VOLATILE
] : NULL_TREE
,
9596 string
, outputs
, inputs
, clobbers
);
9597 /* If the extended syntax was not used, mark the ASM_STMT. */
9599 ASM_INPUT_P (asm_stmt
) = 1;
9602 assemble_asm (string
);
9605 /* Declarators [gram.dcl.decl] */
9607 /* Parse an init-declarator.
9610 declarator initializer [opt]
9615 declarator asm-specification [opt] attributes [opt] initializer [opt]
9617 function-definition:
9618 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9620 decl-specifier-seq [opt] declarator function-try-block
9624 function-definition:
9625 __extension__ function-definition
9627 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9628 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9629 then this declarator appears in a class scope. The new DECL created
9630 by this declarator is returned.
9632 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9633 for a function-definition here as well. If the declarator is a
9634 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9635 be TRUE upon return. By that point, the function-definition will
9636 have been completely parsed.
9638 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9642 cp_parser_init_declarator (cp_parser
* parser
,
9643 tree decl_specifiers
,
9644 tree prefix_attributes
,
9645 bool function_definition_allowed_p
,
9647 int declares_class_or_enum
,
9648 bool* function_definition_p
)
9653 tree asm_specification
;
9655 tree decl
= NULL_TREE
;
9657 bool is_initialized
;
9658 bool is_parenthesized_init
;
9659 bool is_non_constant_init
;
9660 int ctor_dtor_or_conv_p
;
9663 /* Assume that this is not the declarator for a function
9665 if (function_definition_p
)
9666 *function_definition_p
= false;
9668 /* Defer access checks while parsing the declarator; we cannot know
9669 what names are accessible until we know what is being
9671 resume_deferring_access_checks ();
9673 /* Parse the declarator. */
9675 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9676 &ctor_dtor_or_conv_p
,
9677 /*parenthesized_p=*/NULL
);
9678 /* Gather up the deferred checks. */
9679 stop_deferring_access_checks ();
9681 /* If the DECLARATOR was erroneous, there's no need to go
9683 if (declarator
== error_mark_node
)
9684 return error_mark_node
;
9686 cp_parser_check_for_definition_in_return_type (declarator
,
9687 declares_class_or_enum
);
9689 /* Figure out what scope the entity declared by the DECLARATOR is
9690 located in. `grokdeclarator' sometimes changes the scope, so
9691 we compute it now. */
9692 scope
= get_scope_of_declarator (declarator
);
9694 /* If we're allowing GNU extensions, look for an asm-specification
9696 if (cp_parser_allow_gnu_extensions_p (parser
))
9698 /* Look for an asm-specification. */
9699 asm_specification
= cp_parser_asm_specification_opt (parser
);
9700 /* And attributes. */
9701 attributes
= cp_parser_attributes_opt (parser
);
9705 asm_specification
= NULL_TREE
;
9706 attributes
= NULL_TREE
;
9709 /* Peek at the next token. */
9710 token
= cp_lexer_peek_token (parser
->lexer
);
9711 /* Check to see if the token indicates the start of a
9712 function-definition. */
9713 if (cp_parser_token_starts_function_definition_p (token
))
9715 if (!function_definition_allowed_p
)
9717 /* If a function-definition should not appear here, issue an
9719 cp_parser_error (parser
,
9720 "a function-definition is not allowed here");
9721 return error_mark_node
;
9725 /* Neither attributes nor an asm-specification are allowed
9726 on a function-definition. */
9727 if (asm_specification
)
9728 error ("an asm-specification is not allowed on a function-definition");
9730 error ("attributes are not allowed on a function-definition");
9731 /* This is a function-definition. */
9732 *function_definition_p
= true;
9734 /* Parse the function definition. */
9736 decl
= cp_parser_save_member_function_body (parser
,
9742 = (cp_parser_function_definition_from_specifiers_and_declarator
9743 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
9751 Only in function declarations for constructors, destructors, and
9752 type conversions can the decl-specifier-seq be omitted.
9754 We explicitly postpone this check past the point where we handle
9755 function-definitions because we tolerate function-definitions
9756 that are missing their return types in some modes. */
9757 if (!decl_specifiers
&& ctor_dtor_or_conv_p
<= 0)
9759 cp_parser_error (parser
,
9760 "expected constructor, destructor, or type conversion");
9761 return error_mark_node
;
9764 /* An `=' or an `(' indicates an initializer. */
9765 is_initialized
= (token
->type
== CPP_EQ
9766 || token
->type
== CPP_OPEN_PAREN
);
9767 /* If the init-declarator isn't initialized and isn't followed by a
9768 `,' or `;', it's not a valid init-declarator. */
9770 && token
->type
!= CPP_COMMA
9771 && token
->type
!= CPP_SEMICOLON
)
9773 cp_parser_error (parser
, "expected init-declarator");
9774 return error_mark_node
;
9777 /* Because start_decl has side-effects, we should only call it if we
9778 know we're going ahead. By this point, we know that we cannot
9779 possibly be looking at any other construct. */
9780 cp_parser_commit_to_tentative_parse (parser
);
9782 /* Check to see whether or not this declaration is a friend. */
9783 friend_p
= cp_parser_friend_p (decl_specifiers
);
9785 /* Check that the number of template-parameter-lists is OK. */
9786 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
9787 return error_mark_node
;
9789 /* Enter the newly declared entry in the symbol table. If we're
9790 processing a declaration in a class-specifier, we wait until
9791 after processing the initializer. */
9794 if (parser
->in_unbraced_linkage_specification_p
)
9796 decl_specifiers
= tree_cons (error_mark_node
,
9797 get_identifier ("extern"),
9799 have_extern_spec
= false;
9801 decl
= start_decl (declarator
, decl_specifiers
,
9802 is_initialized
, attributes
, prefix_attributes
);
9805 /* Enter the SCOPE. That way unqualified names appearing in the
9806 initializer will be looked up in SCOPE. */
9810 /* Perform deferred access control checks, now that we know in which
9811 SCOPE the declared entity resides. */
9812 if (!member_p
&& decl
)
9814 tree saved_current_function_decl
= NULL_TREE
;
9816 /* If the entity being declared is a function, pretend that we
9817 are in its scope. If it is a `friend', it may have access to
9818 things that would not otherwise be accessible. */
9819 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9821 saved_current_function_decl
= current_function_decl
;
9822 current_function_decl
= decl
;
9825 /* Perform the access control checks for the declarator and the
9826 the decl-specifiers. */
9827 perform_deferred_access_checks ();
9829 /* Restore the saved value. */
9830 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9831 current_function_decl
= saved_current_function_decl
;
9834 /* Parse the initializer. */
9836 initializer
= cp_parser_initializer (parser
,
9837 &is_parenthesized_init
,
9838 &is_non_constant_init
);
9841 initializer
= NULL_TREE
;
9842 is_parenthesized_init
= false;
9843 is_non_constant_init
= true;
9846 /* The old parser allows attributes to appear after a parenthesized
9847 initializer. Mark Mitchell proposed removing this functionality
9848 on the GCC mailing lists on 2002-08-13. This parser accepts the
9849 attributes -- but ignores them. */
9850 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
9851 if (cp_parser_attributes_opt (parser
))
9852 warning ("attributes after parenthesized initializer ignored");
9854 /* Leave the SCOPE, now that we have processed the initializer. It
9855 is important to do this before calling cp_finish_decl because it
9856 makes decisions about whether to create DECL_STMTs or not based
9857 on the current scope. */
9861 /* For an in-class declaration, use `grokfield' to create the
9865 decl
= grokfield (declarator
, decl_specifiers
,
9866 initializer
, /*asmspec=*/NULL_TREE
,
9867 /*attributes=*/NULL_TREE
);
9868 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
9869 cp_parser_save_default_args (parser
, decl
);
9872 /* Finish processing the declaration. But, skip friend
9874 if (!friend_p
&& decl
)
9875 cp_finish_decl (decl
,
9878 /* If the initializer is in parentheses, then this is
9879 a direct-initialization, which means that an
9880 `explicit' constructor is OK. Otherwise, an
9881 `explicit' constructor cannot be used. */
9882 ((is_parenthesized_init
|| !is_initialized
)
9883 ? 0 : LOOKUP_ONLYCONVERTING
));
9885 /* Remember whether or not variables were initialized by
9886 constant-expressions. */
9887 if (decl
&& TREE_CODE (decl
) == VAR_DECL
9888 && is_initialized
&& !is_non_constant_init
)
9889 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
9894 /* Parse a declarator.
9898 ptr-operator declarator
9900 abstract-declarator:
9901 ptr-operator abstract-declarator [opt]
9902 direct-abstract-declarator
9907 attributes [opt] direct-declarator
9908 attributes [opt] ptr-operator declarator
9910 abstract-declarator:
9911 attributes [opt] ptr-operator abstract-declarator [opt]
9912 attributes [opt] direct-abstract-declarator
9914 Returns a representation of the declarator. If the declarator has
9915 the form `* declarator', then an INDIRECT_REF is returned, whose
9916 only operand is the sub-declarator. Analogously, `& declarator' is
9917 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9918 used. The first operand is the TYPE for `X'. The second operand
9919 is an INDIRECT_REF whose operand is the sub-declarator.
9921 Otherwise, the representation is as for a direct-declarator.
9923 (It would be better to define a structure type to represent
9924 declarators, rather than abusing `tree' nodes to represent
9925 declarators. That would be much clearer and save some memory.
9926 There is no reason for declarators to be garbage-collected, for
9927 example; they are created during parser and no longer needed after
9928 `grokdeclarator' has been called.)
9930 For a ptr-operator that has the optional cv-qualifier-seq,
9931 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9934 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9935 detect constructor, destructor or conversion operators. It is set
9936 to -1 if the declarator is a name, and +1 if it is a
9937 function. Otherwise it is set to zero. Usually you just want to
9938 test for >0, but internally the negative value is used.
9940 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9941 a decl-specifier-seq unless it declares a constructor, destructor,
9942 or conversion. It might seem that we could check this condition in
9943 semantic analysis, rather than parsing, but that makes it difficult
9944 to handle something like `f()'. We want to notice that there are
9945 no decl-specifiers, and therefore realize that this is an
9946 expression, not a declaration.)
9948 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
9949 the declarator is a direct-declarator of the form "(...)". */
9952 cp_parser_declarator (cp_parser
* parser
,
9953 cp_parser_declarator_kind dcl_kind
,
9954 int* ctor_dtor_or_conv_p
,
9955 bool* parenthesized_p
)
9959 enum tree_code code
;
9960 tree cv_qualifier_seq
;
9962 tree attributes
= NULL_TREE
;
9964 /* Assume this is not a constructor, destructor, or type-conversion
9966 if (ctor_dtor_or_conv_p
)
9967 *ctor_dtor_or_conv_p
= 0;
9969 if (cp_parser_allow_gnu_extensions_p (parser
))
9970 attributes
= cp_parser_attributes_opt (parser
);
9972 /* Peek at the next token. */
9973 token
= cp_lexer_peek_token (parser
->lexer
);
9975 /* Check for the ptr-operator production. */
9976 cp_parser_parse_tentatively (parser
);
9977 /* Parse the ptr-operator. */
9978 code
= cp_parser_ptr_operator (parser
,
9981 /* If that worked, then we have a ptr-operator. */
9982 if (cp_parser_parse_definitely (parser
))
9984 /* If a ptr-operator was found, then this declarator was not
9986 if (parenthesized_p
)
9987 *parenthesized_p
= true;
9988 /* The dependent declarator is optional if we are parsing an
9989 abstract-declarator. */
9990 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9991 cp_parser_parse_tentatively (parser
);
9993 /* Parse the dependent declarator. */
9994 declarator
= cp_parser_declarator (parser
, dcl_kind
,
9995 /*ctor_dtor_or_conv_p=*/NULL
,
9996 /*parenthesized_p=*/NULL
);
9998 /* If we are parsing an abstract-declarator, we must handle the
9999 case where the dependent declarator is absent. */
10000 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10001 && !cp_parser_parse_definitely (parser
))
10002 declarator
= NULL_TREE
;
10004 /* Build the representation of the ptr-operator. */
10005 if (code
== INDIRECT_REF
)
10006 declarator
= make_pointer_declarator (cv_qualifier_seq
,
10009 declarator
= make_reference_declarator (cv_qualifier_seq
,
10011 /* Handle the pointer-to-member case. */
10013 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
10015 /* Everything else is a direct-declarator. */
10018 if (parenthesized_p
)
10019 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10021 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10022 ctor_dtor_or_conv_p
);
10025 if (attributes
&& declarator
!= error_mark_node
)
10026 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
10031 /* Parse a direct-declarator or direct-abstract-declarator.
10035 direct-declarator ( parameter-declaration-clause )
10036 cv-qualifier-seq [opt]
10037 exception-specification [opt]
10038 direct-declarator [ constant-expression [opt] ]
10041 direct-abstract-declarator:
10042 direct-abstract-declarator [opt]
10043 ( parameter-declaration-clause )
10044 cv-qualifier-seq [opt]
10045 exception-specification [opt]
10046 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10047 ( abstract-declarator )
10049 Returns a representation of the declarator. DCL_KIND is
10050 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10051 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10052 we are parsing a direct-declarator. It is
10053 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10054 of ambiguity we prefer an abstract declarator, as per
10055 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10056 cp_parser_declarator.
10058 For the declarator-id production, the representation is as for an
10059 id-expression, except that a qualified name is represented as a
10060 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10061 see the documentation of the FUNCTION_DECLARATOR_* macros for
10062 information about how to find the various declarator components.
10063 An array-declarator is represented as an ARRAY_REF. The
10064 direct-declarator is the first operand; the constant-expression
10065 indicating the size of the array is the second operand. */
10068 cp_parser_direct_declarator (cp_parser
* parser
,
10069 cp_parser_declarator_kind dcl_kind
,
10070 int* ctor_dtor_or_conv_p
)
10073 tree declarator
= NULL_TREE
;
10074 tree scope
= NULL_TREE
;
10075 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10076 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10081 /* Peek at the next token. */
10082 token
= cp_lexer_peek_token (parser
->lexer
);
10083 if (token
->type
== CPP_OPEN_PAREN
)
10085 /* This is either a parameter-declaration-clause, or a
10086 parenthesized declarator. When we know we are parsing a
10087 named declarator, it must be a parenthesized declarator
10088 if FIRST is true. For instance, `(int)' is a
10089 parameter-declaration-clause, with an omitted
10090 direct-abstract-declarator. But `((*))', is a
10091 parenthesized abstract declarator. Finally, when T is a
10092 template parameter `(T)' is a
10093 parameter-declaration-clause, and not a parenthesized
10096 We first try and parse a parameter-declaration-clause,
10097 and then try a nested declarator (if FIRST is true).
10099 It is not an error for it not to be a
10100 parameter-declaration-clause, even when FIRST is
10106 The first is the declaration of a function while the
10107 second is a the definition of a variable, including its
10110 Having seen only the parenthesis, we cannot know which of
10111 these two alternatives should be selected. Even more
10112 complex are examples like:
10117 The former is a function-declaration; the latter is a
10118 variable initialization.
10120 Thus again, we try a parameter-declaration-clause, and if
10121 that fails, we back out and return. */
10123 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10126 unsigned saved_num_template_parameter_lists
;
10128 cp_parser_parse_tentatively (parser
);
10130 /* Consume the `('. */
10131 cp_lexer_consume_token (parser
->lexer
);
10134 /* If this is going to be an abstract declarator, we're
10135 in a declarator and we can't have default args. */
10136 parser
->default_arg_ok_p
= false;
10137 parser
->in_declarator_p
= true;
10140 /* Inside the function parameter list, surrounding
10141 template-parameter-lists do not apply. */
10142 saved_num_template_parameter_lists
10143 = parser
->num_template_parameter_lists
;
10144 parser
->num_template_parameter_lists
= 0;
10146 /* Parse the parameter-declaration-clause. */
10147 params
= cp_parser_parameter_declaration_clause (parser
);
10149 parser
->num_template_parameter_lists
10150 = saved_num_template_parameter_lists
;
10152 /* If all went well, parse the cv-qualifier-seq and the
10153 exception-specification. */
10154 if (cp_parser_parse_definitely (parser
))
10156 tree cv_qualifiers
;
10157 tree exception_specification
;
10159 if (ctor_dtor_or_conv_p
)
10160 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
10162 /* Consume the `)'. */
10163 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
10165 /* Parse the cv-qualifier-seq. */
10166 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
10167 /* And the exception-specification. */
10168 exception_specification
10169 = cp_parser_exception_specification_opt (parser
);
10171 /* Create the function-declarator. */
10172 declarator
= make_call_declarator (declarator
,
10175 exception_specification
);
10176 /* Any subsequent parameter lists are to do with
10177 return type, so are not those of the declared
10179 parser
->default_arg_ok_p
= false;
10181 /* Repeat the main loop. */
10186 /* If this is the first, we can try a parenthesized
10190 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10191 parser
->in_declarator_p
= saved_in_declarator_p
;
10193 /* Consume the `('. */
10194 cp_lexer_consume_token (parser
->lexer
);
10195 /* Parse the nested declarator. */
10197 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
10198 /*parenthesized_p=*/NULL
);
10200 /* Expect a `)'. */
10201 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10202 declarator
= error_mark_node
;
10203 if (declarator
== error_mark_node
)
10206 goto handle_declarator
;
10208 /* Otherwise, we must be done. */
10212 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10213 && token
->type
== CPP_OPEN_SQUARE
)
10215 /* Parse an array-declarator. */
10218 if (ctor_dtor_or_conv_p
)
10219 *ctor_dtor_or_conv_p
= 0;
10222 parser
->default_arg_ok_p
= false;
10223 parser
->in_declarator_p
= true;
10224 /* Consume the `['. */
10225 cp_lexer_consume_token (parser
->lexer
);
10226 /* Peek at the next token. */
10227 token
= cp_lexer_peek_token (parser
->lexer
);
10228 /* If the next token is `]', then there is no
10229 constant-expression. */
10230 if (token
->type
!= CPP_CLOSE_SQUARE
)
10232 bool non_constant_p
;
10235 = cp_parser_constant_expression (parser
,
10236 /*allow_non_constant=*/true,
10238 if (!non_constant_p
)
10239 bounds
= cp_parser_fold_non_dependent_expr (bounds
);
10242 bounds
= NULL_TREE
;
10243 /* Look for the closing `]'. */
10244 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
10246 declarator
= error_mark_node
;
10250 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
10252 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
10254 /* Parse a declarator-id */
10255 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10256 cp_parser_parse_tentatively (parser
);
10257 declarator
= cp_parser_declarator_id (parser
);
10258 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10260 if (!cp_parser_parse_definitely (parser
))
10261 declarator
= error_mark_node
;
10262 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
10264 cp_parser_error (parser
, "expected unqualified-id");
10265 declarator
= error_mark_node
;
10269 if (declarator
== error_mark_node
)
10272 if (TREE_CODE (declarator
) == SCOPE_REF
10273 && !current_scope ())
10275 tree scope
= TREE_OPERAND (declarator
, 0);
10277 /* In the declaration of a member of a template class
10278 outside of the class itself, the SCOPE will sometimes
10279 be a TYPENAME_TYPE. For example, given:
10281 template <typename T>
10282 int S<T>::R::i = 3;
10284 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10285 this context, we must resolve S<T>::R to an ordinary
10286 type, rather than a typename type.
10288 The reason we normally avoid resolving TYPENAME_TYPEs
10289 is that a specialization of `S' might render
10290 `S<T>::R' not a type. However, if `S' is
10291 specialized, then this `i' will not be used, so there
10292 is no harm in resolving the types here. */
10293 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
10297 /* Resolve the TYPENAME_TYPE. */
10298 type
= resolve_typename_type (scope
,
10299 /*only_current_p=*/false);
10300 /* If that failed, the declarator is invalid. */
10301 if (type
!= error_mark_node
)
10303 /* Build a new DECLARATOR. */
10304 declarator
= build_nt (SCOPE_REF
,
10306 TREE_OPERAND (declarator
, 1));
10310 /* Check to see whether the declarator-id names a constructor,
10311 destructor, or conversion. */
10312 if (declarator
&& ctor_dtor_or_conv_p
10313 && ((TREE_CODE (declarator
) == SCOPE_REF
10314 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
10315 || (TREE_CODE (declarator
) != SCOPE_REF
10316 && at_class_scope_p ())))
10318 tree unqualified_name
;
10321 /* Get the unqualified part of the name. */
10322 if (TREE_CODE (declarator
) == SCOPE_REF
)
10324 class_type
= TREE_OPERAND (declarator
, 0);
10325 unqualified_name
= TREE_OPERAND (declarator
, 1);
10329 class_type
= current_class_type
;
10330 unqualified_name
= declarator
;
10333 /* See if it names ctor, dtor or conv. */
10334 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
10335 || IDENTIFIER_TYPENAME_P (unqualified_name
)
10336 || constructor_name_p (unqualified_name
, class_type
))
10337 *ctor_dtor_or_conv_p
= -1;
10340 handle_declarator
:;
10341 scope
= get_scope_of_declarator (declarator
);
10343 /* Any names that appear after the declarator-id for a member
10344 are looked up in the containing scope. */
10345 push_scope (scope
);
10346 parser
->in_declarator_p
= true;
10347 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
10349 && (TREE_CODE (declarator
) == SCOPE_REF
10350 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
10351 /* Default args are only allowed on function
10353 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10355 parser
->default_arg_ok_p
= false;
10364 /* For an abstract declarator, we might wind up with nothing at this
10365 point. That's an error; the declarator is not optional. */
10367 cp_parser_error (parser
, "expected declarator");
10369 /* If we entered a scope, we must exit it now. */
10373 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10374 parser
->in_declarator_p
= saved_in_declarator_p
;
10379 /* Parse a ptr-operator.
10382 * cv-qualifier-seq [opt]
10384 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10389 & cv-qualifier-seq [opt]
10391 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10392 used. Returns ADDR_EXPR if a reference was used. In the
10393 case of a pointer-to-member, *TYPE is filled in with the
10394 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10395 with the cv-qualifier-seq, or NULL_TREE, if there are no
10396 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10398 static enum tree_code
10399 cp_parser_ptr_operator (cp_parser
* parser
,
10401 tree
* cv_qualifier_seq
)
10403 enum tree_code code
= ERROR_MARK
;
10406 /* Assume that it's not a pointer-to-member. */
10408 /* And that there are no cv-qualifiers. */
10409 *cv_qualifier_seq
= NULL_TREE
;
10411 /* Peek at the next token. */
10412 token
= cp_lexer_peek_token (parser
->lexer
);
10413 /* If it's a `*' or `&' we have a pointer or reference. */
10414 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10416 /* Remember which ptr-operator we were processing. */
10417 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10419 /* Consume the `*' or `&'. */
10420 cp_lexer_consume_token (parser
->lexer
);
10422 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10423 `&', if we are allowing GNU extensions. (The only qualifier
10424 that can legally appear after `&' is `restrict', but that is
10425 enforced during semantic analysis. */
10426 if (code
== INDIRECT_REF
10427 || cp_parser_allow_gnu_extensions_p (parser
))
10428 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10432 /* Try the pointer-to-member case. */
10433 cp_parser_parse_tentatively (parser
);
10434 /* Look for the optional `::' operator. */
10435 cp_parser_global_scope_opt (parser
,
10436 /*current_scope_valid_p=*/false);
10437 /* Look for the nested-name specifier. */
10438 cp_parser_nested_name_specifier (parser
,
10439 /*typename_keyword_p=*/false,
10440 /*check_dependency_p=*/true,
10442 /*is_declaration=*/false);
10443 /* If we found it, and the next token is a `*', then we are
10444 indeed looking at a pointer-to-member operator. */
10445 if (!cp_parser_error_occurred (parser
)
10446 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10448 /* The type of which the member is a member is given by the
10450 *type
= parser
->scope
;
10451 /* The next name will not be qualified. */
10452 parser
->scope
= NULL_TREE
;
10453 parser
->qualifying_scope
= NULL_TREE
;
10454 parser
->object_scope
= NULL_TREE
;
10455 /* Indicate that the `*' operator was used. */
10456 code
= INDIRECT_REF
;
10457 /* Look for the optional cv-qualifier-seq. */
10458 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10460 /* If that didn't work we don't have a ptr-operator. */
10461 if (!cp_parser_parse_definitely (parser
))
10462 cp_parser_error (parser
, "expected ptr-operator");
10468 /* Parse an (optional) cv-qualifier-seq.
10471 cv-qualifier cv-qualifier-seq [opt]
10473 Returns a TREE_LIST. The TREE_VALUE of each node is the
10474 representation of a cv-qualifier. */
10477 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10479 tree cv_qualifiers
= NULL_TREE
;
10485 /* Look for the next cv-qualifier. */
10486 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10487 /* If we didn't find one, we're done. */
10491 /* Add this cv-qualifier to the list. */
10493 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10496 /* We built up the list in reverse order. */
10497 return nreverse (cv_qualifiers
);
10500 /* Parse an (optional) cv-qualifier.
10512 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10515 tree cv_qualifier
= NULL_TREE
;
10517 /* Peek at the next token. */
10518 token
= cp_lexer_peek_token (parser
->lexer
);
10519 /* See if it's a cv-qualifier. */
10520 switch (token
->keyword
)
10525 /* Save the value of the token. */
10526 cv_qualifier
= token
->value
;
10527 /* Consume the token. */
10528 cp_lexer_consume_token (parser
->lexer
);
10535 return cv_qualifier
;
10538 /* Parse a declarator-id.
10542 :: [opt] nested-name-specifier [opt] type-name
10544 In the `id-expression' case, the value returned is as for
10545 cp_parser_id_expression if the id-expression was an unqualified-id.
10546 If the id-expression was a qualified-id, then a SCOPE_REF is
10547 returned. The first operand is the scope (either a NAMESPACE_DECL
10548 or TREE_TYPE), but the second is still just a representation of an
10552 cp_parser_declarator_id (cp_parser
* parser
)
10554 tree id_expression
;
10556 /* The expression must be an id-expression. Assume that qualified
10557 names are the names of types so that:
10560 int S<T>::R::i = 3;
10562 will work; we must treat `S<T>::R' as the name of a type.
10563 Similarly, assume that qualified names are templates, where
10567 int S<T>::R<T>::i = 3;
10570 id_expression
= cp_parser_id_expression (parser
,
10571 /*template_keyword_p=*/false,
10572 /*check_dependency_p=*/false,
10573 /*template_p=*/NULL
,
10574 /*declarator_p=*/true);
10575 /* If the name was qualified, create a SCOPE_REF to represent
10579 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
10580 parser
->scope
= NULL_TREE
;
10583 return id_expression
;
10586 /* Parse a type-id.
10589 type-specifier-seq abstract-declarator [opt]
10591 Returns the TYPE specified. */
10594 cp_parser_type_id (cp_parser
* parser
)
10596 tree type_specifier_seq
;
10597 tree abstract_declarator
;
10599 /* Parse the type-specifier-seq. */
10601 = cp_parser_type_specifier_seq (parser
);
10602 if (type_specifier_seq
== error_mark_node
)
10603 return error_mark_node
;
10605 /* There might or might not be an abstract declarator. */
10606 cp_parser_parse_tentatively (parser
);
10607 /* Look for the declarator. */
10608 abstract_declarator
10609 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
10610 /*parenthesized_p=*/NULL
);
10611 /* Check to see if there really was a declarator. */
10612 if (!cp_parser_parse_definitely (parser
))
10613 abstract_declarator
= NULL_TREE
;
10615 return groktypename (build_tree_list (type_specifier_seq
,
10616 abstract_declarator
));
10619 /* Parse a type-specifier-seq.
10621 type-specifier-seq:
10622 type-specifier type-specifier-seq [opt]
10626 type-specifier-seq:
10627 attributes type-specifier-seq [opt]
10629 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10630 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10633 cp_parser_type_specifier_seq (cp_parser
* parser
)
10635 bool seen_type_specifier
= false;
10636 tree type_specifier_seq
= NULL_TREE
;
10638 /* Parse the type-specifiers and attributes. */
10641 tree type_specifier
;
10643 /* Check for attributes first. */
10644 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
10646 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
10648 type_specifier_seq
);
10652 /* After the first type-specifier, others are optional. */
10653 if (seen_type_specifier
)
10654 cp_parser_parse_tentatively (parser
);
10655 /* Look for the type-specifier. */
10656 type_specifier
= cp_parser_type_specifier (parser
,
10657 CP_PARSER_FLAGS_NONE
,
10658 /*is_friend=*/false,
10659 /*is_declaration=*/false,
10662 /* If the first type-specifier could not be found, this is not a
10663 type-specifier-seq at all. */
10664 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
10665 return error_mark_node
;
10666 /* If subsequent type-specifiers could not be found, the
10667 type-specifier-seq is complete. */
10668 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
10671 /* Add the new type-specifier to the list. */
10673 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
10674 seen_type_specifier
= true;
10677 /* We built up the list in reverse order. */
10678 return nreverse (type_specifier_seq
);
10681 /* Parse a parameter-declaration-clause.
10683 parameter-declaration-clause:
10684 parameter-declaration-list [opt] ... [opt]
10685 parameter-declaration-list , ...
10687 Returns a representation for the parameter declarations. Each node
10688 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10689 representation.) If the parameter-declaration-clause ends with an
10690 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10691 list. A return value of NULL_TREE indicates a
10692 parameter-declaration-clause consisting only of an ellipsis. */
10695 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
10701 /* Peek at the next token. */
10702 token
= cp_lexer_peek_token (parser
->lexer
);
10703 /* Check for trivial parameter-declaration-clauses. */
10704 if (token
->type
== CPP_ELLIPSIS
)
10706 /* Consume the `...' token. */
10707 cp_lexer_consume_token (parser
->lexer
);
10710 else if (token
->type
== CPP_CLOSE_PAREN
)
10711 /* There are no parameters. */
10713 #ifndef NO_IMPLICIT_EXTERN_C
10714 if (in_system_header
&& current_class_type
== NULL
10715 && current_lang_name
== lang_name_c
)
10719 return void_list_node
;
10721 /* Check for `(void)', too, which is a special case. */
10722 else if (token
->keyword
== RID_VOID
10723 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
10724 == CPP_CLOSE_PAREN
))
10726 /* Consume the `void' token. */
10727 cp_lexer_consume_token (parser
->lexer
);
10728 /* There are no parameters. */
10729 return void_list_node
;
10732 /* Parse the parameter-declaration-list. */
10733 parameters
= cp_parser_parameter_declaration_list (parser
);
10734 /* If a parse error occurred while parsing the
10735 parameter-declaration-list, then the entire
10736 parameter-declaration-clause is erroneous. */
10737 if (parameters
== error_mark_node
)
10738 return error_mark_node
;
10740 /* Peek at the next token. */
10741 token
= cp_lexer_peek_token (parser
->lexer
);
10742 /* If it's a `,', the clause should terminate with an ellipsis. */
10743 if (token
->type
== CPP_COMMA
)
10745 /* Consume the `,'. */
10746 cp_lexer_consume_token (parser
->lexer
);
10747 /* Expect an ellipsis. */
10749 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
10751 /* It might also be `...' if the optional trailing `,' was
10753 else if (token
->type
== CPP_ELLIPSIS
)
10755 /* Consume the `...' token. */
10756 cp_lexer_consume_token (parser
->lexer
);
10757 /* And remember that we saw it. */
10761 ellipsis_p
= false;
10763 /* Finish the parameter list. */
10764 return finish_parmlist (parameters
, ellipsis_p
);
10767 /* Parse a parameter-declaration-list.
10769 parameter-declaration-list:
10770 parameter-declaration
10771 parameter-declaration-list , parameter-declaration
10773 Returns a representation of the parameter-declaration-list, as for
10774 cp_parser_parameter_declaration_clause. However, the
10775 `void_list_node' is never appended to the list. */
10778 cp_parser_parameter_declaration_list (cp_parser
* parser
)
10780 tree parameters
= NULL_TREE
;
10782 /* Look for more parameters. */
10786 bool parenthesized_p
;
10787 /* Parse the parameter. */
10789 = cp_parser_parameter_declaration (parser
,
10790 /*template_parm_p=*/false,
10793 /* If a parse error occurred parsing the parameter declaration,
10794 then the entire parameter-declaration-list is erroneous. */
10795 if (parameter
== error_mark_node
)
10797 parameters
= error_mark_node
;
10800 /* Add the new parameter to the list. */
10801 TREE_CHAIN (parameter
) = parameters
;
10802 parameters
= parameter
;
10804 /* Peek at the next token. */
10805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
10806 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10807 /* The parameter-declaration-list is complete. */
10809 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10813 /* Peek at the next token. */
10814 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10815 /* If it's an ellipsis, then the list is complete. */
10816 if (token
->type
== CPP_ELLIPSIS
)
10818 /* Otherwise, there must be more parameters. Consume the
10820 cp_lexer_consume_token (parser
->lexer
);
10821 /* When parsing something like:
10823 int i(float f, double d)
10825 we can tell after seeing the declaration for "f" that we
10826 are not looking at an initialization of a variable "i",
10827 but rather at the declaration of a function "i".
10829 Due to the fact that the parsing of template arguments
10830 (as specified to a template-id) requires backtracking we
10831 cannot use this technique when inside a template argument
10833 if (!parser
->in_template_argument_list_p
10834 && cp_parser_parsing_tentatively (parser
)
10835 && !cp_parser_committed_to_tentative_parse (parser
)
10836 /* However, a parameter-declaration of the form
10837 "foat(f)" (which is a valid declaration of a
10838 parameter "f") can also be interpreted as an
10839 expression (the conversion of "f" to "float"). */
10840 && !parenthesized_p
)
10841 cp_parser_commit_to_tentative_parse (parser
);
10845 cp_parser_error (parser
, "expected `,' or `...'");
10846 if (!cp_parser_parsing_tentatively (parser
)
10847 || cp_parser_committed_to_tentative_parse (parser
))
10848 cp_parser_skip_to_closing_parenthesis (parser
,
10849 /*recovering=*/true,
10850 /*or_comma=*/false,
10851 /*consume_paren=*/false);
10856 /* We built up the list in reverse order; straighten it out now. */
10857 return nreverse (parameters
);
10860 /* Parse a parameter declaration.
10862 parameter-declaration:
10863 decl-specifier-seq declarator
10864 decl-specifier-seq declarator = assignment-expression
10865 decl-specifier-seq abstract-declarator [opt]
10866 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10868 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10869 declares a template parameter. (In that case, a non-nested `>'
10870 token encountered during the parsing of the assignment-expression
10871 is not interpreted as a greater-than operator.)
10873 Returns a TREE_LIST representing the parameter-declaration. The
10874 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10875 there is no default argument. The TREE_VALUE is a representation
10876 of the decl-specifier-seq and declarator. In particular, the
10877 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10878 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10879 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10880 the declarator is of the form "(p)". */
10883 cp_parser_parameter_declaration (cp_parser
*parser
,
10884 bool template_parm_p
,
10885 bool *parenthesized_p
)
10887 int declares_class_or_enum
;
10888 bool greater_than_is_operator_p
;
10889 tree decl_specifiers
;
10892 tree default_argument
;
10895 const char *saved_message
;
10897 /* In a template parameter, `>' is not an operator.
10901 When parsing a default template-argument for a non-type
10902 template-parameter, the first non-nested `>' is taken as the end
10903 of the template parameter-list rather than a greater-than
10905 greater_than_is_operator_p
= !template_parm_p
;
10907 /* Type definitions may not appear in parameter types. */
10908 saved_message
= parser
->type_definition_forbidden_message
;
10909 parser
->type_definition_forbidden_message
10910 = "types may not be defined in parameter types";
10912 /* Parse the declaration-specifiers. */
10914 = cp_parser_decl_specifier_seq (parser
,
10915 CP_PARSER_FLAGS_NONE
,
10917 &declares_class_or_enum
);
10918 /* If an error occurred, there's no reason to attempt to parse the
10919 rest of the declaration. */
10920 if (cp_parser_error_occurred (parser
))
10922 parser
->type_definition_forbidden_message
= saved_message
;
10923 return error_mark_node
;
10926 /* Peek at the next token. */
10927 token
= cp_lexer_peek_token (parser
->lexer
);
10928 /* If the next token is a `)', `,', `=', `>', or `...', then there
10929 is no declarator. */
10930 if (token
->type
== CPP_CLOSE_PAREN
10931 || token
->type
== CPP_COMMA
10932 || token
->type
== CPP_EQ
10933 || token
->type
== CPP_ELLIPSIS
10934 || token
->type
== CPP_GREATER
)
10936 declarator
= NULL_TREE
;
10937 if (parenthesized_p
)
10938 *parenthesized_p
= false;
10940 /* Otherwise, there should be a declarator. */
10943 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10944 parser
->default_arg_ok_p
= false;
10946 /* After seeing a decl-specifier-seq, if the next token is not a
10947 "(", there is no possibility that the code is a valid
10948 expression initializer. Therefore, if parsing tentatively,
10949 we commit at this point. */
10950 if (!parser
->in_template_argument_list_p
10951 && cp_parser_parsing_tentatively (parser
)
10952 && !cp_parser_committed_to_tentative_parse (parser
)
10953 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
10954 cp_parser_commit_to_tentative_parse (parser
);
10955 /* Parse the declarator. */
10956 declarator
= cp_parser_declarator (parser
,
10957 CP_PARSER_DECLARATOR_EITHER
,
10958 /*ctor_dtor_or_conv_p=*/NULL
,
10960 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10961 /* After the declarator, allow more attributes. */
10962 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
10965 /* The restriction on defining new types applies only to the type
10966 of the parameter, not to the default argument. */
10967 parser
->type_definition_forbidden_message
= saved_message
;
10969 /* If the next token is `=', then process a default argument. */
10970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10972 bool saved_greater_than_is_operator_p
;
10973 /* Consume the `='. */
10974 cp_lexer_consume_token (parser
->lexer
);
10976 /* If we are defining a class, then the tokens that make up the
10977 default argument must be saved and processed later. */
10978 if (!template_parm_p
&& at_class_scope_p ()
10979 && TYPE_BEING_DEFINED (current_class_type
))
10981 unsigned depth
= 0;
10983 /* Create a DEFAULT_ARG to represented the unparsed default
10985 default_argument
= make_node (DEFAULT_ARG
);
10986 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
10988 /* Add tokens until we have processed the entire default
10995 /* Peek at the next token. */
10996 token
= cp_lexer_peek_token (parser
->lexer
);
10997 /* What we do depends on what token we have. */
10998 switch (token
->type
)
11000 /* In valid code, a default argument must be
11001 immediately followed by a `,' `)', or `...'. */
11003 case CPP_CLOSE_PAREN
:
11005 /* If we run into a non-nested `;', `}', or `]',
11006 then the code is invalid -- but the default
11007 argument is certainly over. */
11008 case CPP_SEMICOLON
:
11009 case CPP_CLOSE_BRACE
:
11010 case CPP_CLOSE_SQUARE
:
11013 /* Update DEPTH, if necessary. */
11014 else if (token
->type
== CPP_CLOSE_PAREN
11015 || token
->type
== CPP_CLOSE_BRACE
11016 || token
->type
== CPP_CLOSE_SQUARE
)
11020 case CPP_OPEN_PAREN
:
11021 case CPP_OPEN_SQUARE
:
11022 case CPP_OPEN_BRACE
:
11027 /* If we see a non-nested `>', and `>' is not an
11028 operator, then it marks the end of the default
11030 if (!depth
&& !greater_than_is_operator_p
)
11034 /* If we run out of tokens, issue an error message. */
11036 error ("file ends in default argument");
11042 /* In these cases, we should look for template-ids.
11043 For example, if the default argument is
11044 `X<int, double>()', we need to do name lookup to
11045 figure out whether or not `X' is a template; if
11046 so, the `,' does not end the default argument.
11048 That is not yet done. */
11055 /* If we've reached the end, stop. */
11059 /* Add the token to the token block. */
11060 token
= cp_lexer_consume_token (parser
->lexer
);
11061 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
11065 /* Outside of a class definition, we can just parse the
11066 assignment-expression. */
11069 bool saved_local_variables_forbidden_p
;
11071 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11073 saved_greater_than_is_operator_p
11074 = parser
->greater_than_is_operator_p
;
11075 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11076 /* Local variable names (and the `this' keyword) may not
11077 appear in a default argument. */
11078 saved_local_variables_forbidden_p
11079 = parser
->local_variables_forbidden_p
;
11080 parser
->local_variables_forbidden_p
= true;
11081 /* Parse the assignment-expression. */
11082 default_argument
= cp_parser_assignment_expression (parser
);
11083 /* Restore saved state. */
11084 parser
->greater_than_is_operator_p
11085 = saved_greater_than_is_operator_p
;
11086 parser
->local_variables_forbidden_p
11087 = saved_local_variables_forbidden_p
;
11089 if (!parser
->default_arg_ok_p
)
11091 if (!flag_pedantic_errors
)
11092 warning ("deprecated use of default argument for parameter of non-function");
11095 error ("default arguments are only permitted for function parameters");
11096 default_argument
= NULL_TREE
;
11101 default_argument
= NULL_TREE
;
11103 /* Create the representation of the parameter. */
11105 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
11106 parameter
= build_tree_list (default_argument
,
11107 build_tree_list (decl_specifiers
,
11113 /* Parse a function-body.
11116 compound_statement */
11119 cp_parser_function_body (cp_parser
*parser
)
11121 cp_parser_compound_statement (parser
, false);
11124 /* Parse a ctor-initializer-opt followed by a function-body. Return
11125 true if a ctor-initializer was present. */
11128 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11131 bool ctor_initializer_p
;
11133 /* Begin the function body. */
11134 body
= begin_function_body ();
11135 /* Parse the optional ctor-initializer. */
11136 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11137 /* Parse the function-body. */
11138 cp_parser_function_body (parser
);
11139 /* Finish the function body. */
11140 finish_function_body (body
);
11142 return ctor_initializer_p
;
11145 /* Parse an initializer.
11148 = initializer-clause
11149 ( expression-list )
11151 Returns a expression representing the initializer. If no
11152 initializer is present, NULL_TREE is returned.
11154 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11155 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11156 set to FALSE if there is no initializer present. If there is an
11157 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11158 is set to true; otherwise it is set to false. */
11161 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
11162 bool* non_constant_p
)
11167 /* Peek at the next token. */
11168 token
= cp_lexer_peek_token (parser
->lexer
);
11170 /* Let our caller know whether or not this initializer was
11172 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
11173 /* Assume that the initializer is constant. */
11174 *non_constant_p
= false;
11176 if (token
->type
== CPP_EQ
)
11178 /* Consume the `='. */
11179 cp_lexer_consume_token (parser
->lexer
);
11180 /* Parse the initializer-clause. */
11181 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
11183 else if (token
->type
== CPP_OPEN_PAREN
)
11184 init
= cp_parser_parenthesized_expression_list (parser
, false,
11188 /* Anything else is an error. */
11189 cp_parser_error (parser
, "expected initializer");
11190 init
= error_mark_node
;
11196 /* Parse an initializer-clause.
11198 initializer-clause:
11199 assignment-expression
11200 { initializer-list , [opt] }
11203 Returns an expression representing the initializer.
11205 If the `assignment-expression' production is used the value
11206 returned is simply a representation for the expression.
11208 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11209 the elements of the initializer-list (or NULL_TREE, if the last
11210 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11211 NULL_TREE. There is no way to detect whether or not the optional
11212 trailing `,' was provided. NON_CONSTANT_P is as for
11213 cp_parser_initializer. */
11216 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
11220 /* If it is not a `{', then we are looking at an
11221 assignment-expression. */
11222 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11224 = cp_parser_constant_expression (parser
,
11225 /*allow_non_constant_p=*/true,
11229 /* Consume the `{' token. */
11230 cp_lexer_consume_token (parser
->lexer
);
11231 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11232 initializer
= make_node (CONSTRUCTOR
);
11233 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11234 necessary, but check_initializer depends upon it, for
11236 TREE_HAS_CONSTRUCTOR (initializer
) = 1;
11237 /* If it's not a `}', then there is a non-trivial initializer. */
11238 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11240 /* Parse the initializer list. */
11241 CONSTRUCTOR_ELTS (initializer
)
11242 = cp_parser_initializer_list (parser
, non_constant_p
);
11243 /* A trailing `,' token is allowed. */
11244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11245 cp_lexer_consume_token (parser
->lexer
);
11247 /* Now, there should be a trailing `}'. */
11248 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11251 return initializer
;
11254 /* Parse an initializer-list.
11258 initializer-list , initializer-clause
11263 identifier : initializer-clause
11264 initializer-list, identifier : initializer-clause
11266 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11267 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11268 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11269 as for cp_parser_initializer. */
11272 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
11274 tree initializers
= NULL_TREE
;
11276 /* Assume all of the expressions are constant. */
11277 *non_constant_p
= false;
11279 /* Parse the rest of the list. */
11285 bool clause_non_constant_p
;
11287 /* If the next token is an identifier and the following one is a
11288 colon, we are looking at the GNU designated-initializer
11290 if (cp_parser_allow_gnu_extensions_p (parser
)
11291 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11292 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11294 /* Consume the identifier. */
11295 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11296 /* Consume the `:'. */
11297 cp_lexer_consume_token (parser
->lexer
);
11300 identifier
= NULL_TREE
;
11302 /* Parse the initializer. */
11303 initializer
= cp_parser_initializer_clause (parser
,
11304 &clause_non_constant_p
);
11305 /* If any clause is non-constant, so is the entire initializer. */
11306 if (clause_non_constant_p
)
11307 *non_constant_p
= true;
11308 /* Add it to the list. */
11309 initializers
= tree_cons (identifier
, initializer
, initializers
);
11311 /* If the next token is not a comma, we have reached the end of
11313 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11316 /* Peek at the next token. */
11317 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11318 /* If the next token is a `}', then we're still done. An
11319 initializer-clause can have a trailing `,' after the
11320 initializer-list and before the closing `}'. */
11321 if (token
->type
== CPP_CLOSE_BRACE
)
11324 /* Consume the `,' token. */
11325 cp_lexer_consume_token (parser
->lexer
);
11328 /* The initializers were built up in reverse order, so we need to
11329 reverse them now. */
11330 return nreverse (initializers
);
11333 /* Classes [gram.class] */
11335 /* Parse a class-name.
11341 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11342 to indicate that names looked up in dependent types should be
11343 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11344 keyword has been used to indicate that the name that appears next
11345 is a template. TYPE_P is true iff the next name should be treated
11346 as class-name, even if it is declared to be some other kind of name
11347 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11348 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11349 being defined in a class-head.
11351 Returns the TYPE_DECL representing the class. */
11354 cp_parser_class_name (cp_parser
*parser
,
11355 bool typename_keyword_p
,
11356 bool template_keyword_p
,
11358 bool check_dependency_p
,
11360 bool is_declaration
)
11367 /* All class-names start with an identifier. */
11368 token
= cp_lexer_peek_token (parser
->lexer
);
11369 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11371 cp_parser_error (parser
, "expected class-name");
11372 return error_mark_node
;
11375 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11376 to a template-id, so we save it here. */
11377 scope
= parser
->scope
;
11378 if (scope
== error_mark_node
)
11379 return error_mark_node
;
11381 /* Any name names a type if we're following the `typename' keyword
11382 in a qualified name where the enclosing scope is type-dependent. */
11383 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11384 && dependent_type_p (scope
));
11385 /* Handle the common case (an identifier, but not a template-id)
11387 if (token
->type
== CPP_NAME
11388 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
11392 /* Look for the identifier. */
11393 identifier
= cp_parser_identifier (parser
);
11394 /* If the next token isn't an identifier, we are certainly not
11395 looking at a class-name. */
11396 if (identifier
== error_mark_node
)
11397 decl
= error_mark_node
;
11398 /* If we know this is a type-name, there's no need to look it
11400 else if (typename_p
)
11404 /* If the next token is a `::', then the name must be a type
11407 [basic.lookup.qual]
11409 During the lookup for a name preceding the :: scope
11410 resolution operator, object, function, and enumerator
11411 names are ignored. */
11412 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11414 /* Look up the name. */
11415 decl
= cp_parser_lookup_name (parser
, identifier
,
11417 /*is_namespace=*/false,
11418 check_dependency_p
);
11423 /* Try a template-id. */
11424 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11425 check_dependency_p
,
11427 if (decl
== error_mark_node
)
11428 return error_mark_node
;
11431 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11433 /* If this is a typename, create a TYPENAME_TYPE. */
11434 if (typename_p
&& decl
!= error_mark_node
)
11435 decl
= TYPE_NAME (make_typename_type (scope
, decl
,
11438 /* Check to see that it is really the name of a class. */
11439 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11440 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11441 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11442 /* Situations like this:
11444 template <typename T> struct A {
11445 typename T::template X<int>::I i;
11448 are problematic. Is `T::template X<int>' a class-name? The
11449 standard does not seem to be definitive, but there is no other
11450 valid interpretation of the following `::'. Therefore, those
11451 names are considered class-names. */
11452 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11453 else if (decl
== error_mark_node
11454 || TREE_CODE (decl
) != TYPE_DECL
11455 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11457 cp_parser_error (parser
, "expected class-name");
11458 return error_mark_node
;
11464 /* Parse a class-specifier.
11467 class-head { member-specification [opt] }
11469 Returns the TREE_TYPE representing the class. */
11472 cp_parser_class_specifier (cp_parser
* parser
)
11476 tree attributes
= NULL_TREE
;
11477 int has_trailing_semicolon
;
11478 bool nested_name_specifier_p
;
11479 unsigned saved_num_template_parameter_lists
;
11481 push_deferring_access_checks (dk_no_deferred
);
11483 /* Parse the class-head. */
11484 type
= cp_parser_class_head (parser
,
11485 &nested_name_specifier_p
);
11486 /* If the class-head was a semantic disaster, skip the entire body
11490 cp_parser_skip_to_end_of_block_or_statement (parser
);
11491 pop_deferring_access_checks ();
11492 return error_mark_node
;
11495 /* Look for the `{'. */
11496 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11498 pop_deferring_access_checks ();
11499 return error_mark_node
;
11502 /* Issue an error message if type-definitions are forbidden here. */
11503 cp_parser_check_type_definition (parser
);
11504 /* Remember that we are defining one more class. */
11505 ++parser
->num_classes_being_defined
;
11506 /* Inside the class, surrounding template-parameter-lists do not
11508 saved_num_template_parameter_lists
11509 = parser
->num_template_parameter_lists
;
11510 parser
->num_template_parameter_lists
= 0;
11512 /* Start the class. */
11513 if (nested_name_specifier_p
)
11514 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11515 type
= begin_class_definition (type
);
11516 if (type
== error_mark_node
)
11517 /* If the type is erroneous, skip the entire body of the class. */
11518 cp_parser_skip_to_closing_brace (parser
);
11520 /* Parse the member-specification. */
11521 cp_parser_member_specification_opt (parser
);
11522 /* Look for the trailing `}'. */
11523 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11524 /* We get better error messages by noticing a common problem: a
11525 missing trailing `;'. */
11526 token
= cp_lexer_peek_token (parser
->lexer
);
11527 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11528 /* Look for attributes to apply to this class. */
11529 if (cp_parser_allow_gnu_extensions_p (parser
))
11530 attributes
= cp_parser_attributes_opt (parser
);
11531 /* If we got any attributes in class_head, xref_tag will stick them in
11532 TREE_TYPE of the type. Grab them now. */
11533 if (type
!= error_mark_node
)
11535 attributes
= chainon (TYPE_ATTRIBUTES (type
), attributes
);
11536 TYPE_ATTRIBUTES (type
) = NULL_TREE
;
11537 type
= finish_struct (type
, attributes
);
11539 if (nested_name_specifier_p
)
11540 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11541 /* If this class is not itself within the scope of another class,
11542 then we need to parse the bodies of all of the queued function
11543 definitions. Note that the queued functions defined in a class
11544 are not always processed immediately following the
11545 class-specifier for that class. Consider:
11548 struct B { void f() { sizeof (A); } };
11551 If `f' were processed before the processing of `A' were
11552 completed, there would be no way to compute the size of `A'.
11553 Note that the nesting we are interested in here is lexical --
11554 not the semantic nesting given by TYPE_CONTEXT. In particular,
11557 struct A { struct B; };
11558 struct A::B { void f() { } };
11560 there is no need to delay the parsing of `A::B::f'. */
11561 if (--parser
->num_classes_being_defined
== 0)
11566 /* In a first pass, parse default arguments to the functions.
11567 Then, in a second pass, parse the bodies of the functions.
11568 This two-phased approach handles cases like:
11576 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
11577 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
11578 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
11579 TREE_PURPOSE (parser
->unparsed_functions_queues
)
11580 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
11582 fn
= TREE_VALUE (queue_entry
);
11583 /* Make sure that any template parameters are in scope. */
11584 maybe_begin_member_template_processing (fn
);
11585 /* If there are default arguments that have not yet been processed,
11586 take care of them now. */
11587 cp_parser_late_parsing_default_args (parser
, fn
);
11588 /* Remove any template parameters from the symbol table. */
11589 maybe_end_member_template_processing ();
11591 /* Now parse the body of the functions. */
11592 for (TREE_VALUE (parser
->unparsed_functions_queues
)
11593 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
11594 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
11595 TREE_VALUE (parser
->unparsed_functions_queues
)
11596 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
11598 /* Figure out which function we need to process. */
11599 fn
= TREE_VALUE (queue_entry
);
11601 /* Parse the function. */
11602 cp_parser_late_parsing_for_member (parser
, fn
);
11607 /* Put back any saved access checks. */
11608 pop_deferring_access_checks ();
11610 /* Restore the count of active template-parameter-lists. */
11611 parser
->num_template_parameter_lists
11612 = saved_num_template_parameter_lists
;
11617 /* Parse a class-head.
11620 class-key identifier [opt] base-clause [opt]
11621 class-key nested-name-specifier identifier base-clause [opt]
11622 class-key nested-name-specifier [opt] template-id
11626 class-key attributes identifier [opt] base-clause [opt]
11627 class-key attributes nested-name-specifier identifier base-clause [opt]
11628 class-key attributes nested-name-specifier [opt] template-id
11631 Returns the TYPE of the indicated class. Sets
11632 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11633 involving a nested-name-specifier was used, and FALSE otherwise.
11635 Returns NULL_TREE if the class-head is syntactically valid, but
11636 semantically invalid in a way that means we should skip the entire
11637 body of the class. */
11640 cp_parser_class_head (cp_parser
* parser
,
11641 bool* nested_name_specifier_p
)
11644 tree nested_name_specifier
;
11645 enum tag_types class_key
;
11646 tree id
= NULL_TREE
;
11647 tree type
= NULL_TREE
;
11649 bool template_id_p
= false;
11650 bool qualified_p
= false;
11651 bool invalid_nested_name_p
= false;
11652 bool invalid_explicit_specialization_p
= false;
11653 unsigned num_templates
;
11655 /* Assume no nested-name-specifier will be present. */
11656 *nested_name_specifier_p
= false;
11657 /* Assume no template parameter lists will be used in defining the
11661 /* Look for the class-key. */
11662 class_key
= cp_parser_class_key (parser
);
11663 if (class_key
== none_type
)
11664 return error_mark_node
;
11666 /* Parse the attributes. */
11667 attributes
= cp_parser_attributes_opt (parser
);
11669 /* If the next token is `::', that is invalid -- but sometimes
11670 people do try to write:
11674 Handle this gracefully by accepting the extra qualifier, and then
11675 issuing an error about it later if this really is a
11676 class-head. If it turns out just to be an elaborated type
11677 specifier, remain silent. */
11678 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
11679 qualified_p
= true;
11681 push_deferring_access_checks (dk_no_check
);
11683 /* Determine the name of the class. Begin by looking for an
11684 optional nested-name-specifier. */
11685 nested_name_specifier
11686 = cp_parser_nested_name_specifier_opt (parser
,
11687 /*typename_keyword_p=*/false,
11688 /*check_dependency_p=*/false,
11690 /*is_declaration=*/false);
11691 /* If there was a nested-name-specifier, then there *must* be an
11693 if (nested_name_specifier
)
11695 /* Although the grammar says `identifier', it really means
11696 `class-name' or `template-name'. You are only allowed to
11697 define a class that has already been declared with this
11700 The proposed resolution for Core Issue 180 says that whever
11701 you see `class T::X' you should treat `X' as a type-name.
11703 It is OK to define an inaccessible class; for example:
11705 class A { class B; };
11708 We do not know if we will see a class-name, or a
11709 template-name. We look for a class-name first, in case the
11710 class-name is a template-id; if we looked for the
11711 template-name first we would stop after the template-name. */
11712 cp_parser_parse_tentatively (parser
);
11713 type
= cp_parser_class_name (parser
,
11714 /*typename_keyword_p=*/false,
11715 /*template_keyword_p=*/false,
11717 /*check_dependency_p=*/false,
11718 /*class_head_p=*/true,
11719 /*is_declaration=*/false);
11720 /* If that didn't work, ignore the nested-name-specifier. */
11721 if (!cp_parser_parse_definitely (parser
))
11723 invalid_nested_name_p
= true;
11724 id
= cp_parser_identifier (parser
);
11725 if (id
== error_mark_node
)
11728 /* If we could not find a corresponding TYPE, treat this
11729 declaration like an unqualified declaration. */
11730 if (type
== error_mark_node
)
11731 nested_name_specifier
= NULL_TREE
;
11732 /* Otherwise, count the number of templates used in TYPE and its
11733 containing scopes. */
11738 for (scope
= TREE_TYPE (type
);
11739 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
11740 scope
= (TYPE_P (scope
)
11741 ? TYPE_CONTEXT (scope
)
11742 : DECL_CONTEXT (scope
)))
11744 && CLASS_TYPE_P (scope
)
11745 && CLASSTYPE_TEMPLATE_INFO (scope
)
11746 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
11747 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
11751 /* Otherwise, the identifier is optional. */
11754 /* We don't know whether what comes next is a template-id,
11755 an identifier, or nothing at all. */
11756 cp_parser_parse_tentatively (parser
);
11757 /* Check for a template-id. */
11758 id
= cp_parser_template_id (parser
,
11759 /*template_keyword_p=*/false,
11760 /*check_dependency_p=*/true,
11761 /*is_declaration=*/true);
11762 /* If that didn't work, it could still be an identifier. */
11763 if (!cp_parser_parse_definitely (parser
))
11765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11766 id
= cp_parser_identifier (parser
);
11772 template_id_p
= true;
11777 pop_deferring_access_checks ();
11779 cp_parser_check_for_invalid_template_id (parser
, id
);
11781 /* If it's not a `:' or a `{' then we can't really be looking at a
11782 class-head, since a class-head only appears as part of a
11783 class-specifier. We have to detect this situation before calling
11784 xref_tag, since that has irreversible side-effects. */
11785 if (!cp_parser_next_token_starts_class_definition_p (parser
))
11787 cp_parser_error (parser
, "expected `{' or `:'");
11788 return error_mark_node
;
11791 /* At this point, we're going ahead with the class-specifier, even
11792 if some other problem occurs. */
11793 cp_parser_commit_to_tentative_parse (parser
);
11794 /* Issue the error about the overly-qualified name now. */
11796 cp_parser_error (parser
,
11797 "global qualification of class name is invalid");
11798 else if (invalid_nested_name_p
)
11799 cp_parser_error (parser
,
11800 "qualified name does not name a class");
11801 /* An explicit-specialization must be preceded by "template <>". If
11802 it is not, try to recover gracefully. */
11803 if (at_namespace_scope_p ()
11804 && parser
->num_template_parameter_lists
== 0
11807 error ("an explicit specialization must be preceded by 'template <>'");
11808 invalid_explicit_specialization_p
= true;
11809 /* Take the same action that would have been taken by
11810 cp_parser_explicit_specialization. */
11811 ++parser
->num_template_parameter_lists
;
11812 begin_specialization ();
11814 /* There must be no "return" statements between this point and the
11815 end of this function; set "type "to the correct return value and
11816 use "goto done;" to return. */
11817 /* Make sure that the right number of template parameters were
11819 if (!cp_parser_check_template_parameters (parser
, num_templates
))
11821 /* If something went wrong, there is no point in even trying to
11822 process the class-definition. */
11827 /* Look up the type. */
11830 type
= TREE_TYPE (id
);
11831 maybe_process_partial_specialization (type
);
11833 else if (!nested_name_specifier
)
11835 /* If the class was unnamed, create a dummy name. */
11837 id
= make_anon_name ();
11838 type
= xref_tag (class_key
, id
, attributes
, /*globalize=*/false,
11839 parser
->num_template_parameter_lists
);
11848 template <typename T> struct S { struct T };
11849 template <typename T> struct S<T>::T { };
11851 we will get a TYPENAME_TYPE when processing the definition of
11852 `S::T'. We need to resolve it to the actual type before we
11853 try to define it. */
11854 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
11856 class_type
= resolve_typename_type (TREE_TYPE (type
),
11857 /*only_current_p=*/false);
11858 if (class_type
!= error_mark_node
)
11859 type
= TYPE_NAME (class_type
);
11862 cp_parser_error (parser
, "could not resolve typename type");
11863 type
= error_mark_node
;
11867 /* Figure out in what scope the declaration is being placed. */
11868 scope
= current_scope ();
11870 scope
= current_namespace
;
11871 /* If that scope does not contain the scope in which the
11872 class was originally declared, the program is invalid. */
11873 if (scope
&& !is_ancestor (scope
, CP_DECL_CONTEXT (type
)))
11875 error ("declaration of `%D' in `%D' which does not "
11876 "enclose `%D'", type
, scope
, nested_name_specifier
);
11882 A declarator-id shall not be qualified exception of the
11883 definition of a ... nested class outside of its class
11884 ... [or] a the definition or explicit instantiation of a
11885 class member of a namespace outside of its namespace. */
11886 if (scope
== CP_DECL_CONTEXT (type
))
11888 pedwarn ("extra qualification ignored");
11889 nested_name_specifier
= NULL_TREE
;
11892 maybe_process_partial_specialization (TREE_TYPE (type
));
11893 class_type
= current_class_type
;
11894 /* Enter the scope indicated by the nested-name-specifier. */
11895 if (nested_name_specifier
)
11896 push_scope (nested_name_specifier
);
11897 /* Get the canonical version of this type. */
11898 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
11899 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11900 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
11901 type
= push_template_decl (type
);
11902 type
= TREE_TYPE (type
);
11903 if (nested_name_specifier
)
11905 *nested_name_specifier_p
= true;
11906 pop_scope (nested_name_specifier
);
11909 /* Indicate whether this class was declared as a `class' or as a
11911 if (TREE_CODE (type
) == RECORD_TYPE
)
11912 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
11913 cp_parser_check_class_key (class_key
, type
);
11915 /* Enter the scope containing the class; the names of base classes
11916 should be looked up in that context. For example, given:
11918 struct A { struct B {}; struct C; };
11919 struct A::C : B {};
11922 if (nested_name_specifier
)
11923 push_scope (nested_name_specifier
);
11924 /* Now, look for the base-clause. */
11925 token
= cp_lexer_peek_token (parser
->lexer
);
11926 if (token
->type
== CPP_COLON
)
11930 /* Get the list of base-classes. */
11931 bases
= cp_parser_base_clause (parser
);
11932 /* Process them. */
11933 xref_basetypes (type
, bases
);
11935 /* Leave the scope given by the nested-name-specifier. We will
11936 enter the class scope itself while processing the members. */
11937 if (nested_name_specifier
)
11938 pop_scope (nested_name_specifier
);
11941 if (invalid_explicit_specialization_p
)
11943 end_specialization ();
11944 --parser
->num_template_parameter_lists
;
11949 /* Parse a class-key.
11956 Returns the kind of class-key specified, or none_type to indicate
11959 static enum tag_types
11960 cp_parser_class_key (cp_parser
* parser
)
11963 enum tag_types tag_type
;
11965 /* Look for the class-key. */
11966 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
11970 /* Check to see if the TOKEN is a class-key. */
11971 tag_type
= cp_parser_token_is_class_key (token
);
11973 cp_parser_error (parser
, "expected class-key");
11977 /* Parse an (optional) member-specification.
11979 member-specification:
11980 member-declaration member-specification [opt]
11981 access-specifier : member-specification [opt] */
11984 cp_parser_member_specification_opt (cp_parser
* parser
)
11991 /* Peek at the next token. */
11992 token
= cp_lexer_peek_token (parser
->lexer
);
11993 /* If it's a `}', or EOF then we've seen all the members. */
11994 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
11997 /* See if this token is a keyword. */
11998 keyword
= token
->keyword
;
12002 case RID_PROTECTED
:
12004 /* Consume the access-specifier. */
12005 cp_lexer_consume_token (parser
->lexer
);
12006 /* Remember which access-specifier is active. */
12007 current_access_specifier
= token
->value
;
12008 /* Look for the `:'. */
12009 cp_parser_require (parser
, CPP_COLON
, "`:'");
12013 /* Otherwise, the next construction must be a
12014 member-declaration. */
12015 cp_parser_member_declaration (parser
);
12020 /* Parse a member-declaration.
12022 member-declaration:
12023 decl-specifier-seq [opt] member-declarator-list [opt] ;
12024 function-definition ; [opt]
12025 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12027 template-declaration
12029 member-declarator-list:
12031 member-declarator-list , member-declarator
12034 declarator pure-specifier [opt]
12035 declarator constant-initializer [opt]
12036 identifier [opt] : constant-expression
12040 member-declaration:
12041 __extension__ member-declaration
12044 declarator attributes [opt] pure-specifier [opt]
12045 declarator attributes [opt] constant-initializer [opt]
12046 identifier [opt] attributes [opt] : constant-expression */
12049 cp_parser_member_declaration (cp_parser
* parser
)
12051 tree decl_specifiers
;
12052 tree prefix_attributes
;
12054 int declares_class_or_enum
;
12057 int saved_pedantic
;
12059 /* Check for the `__extension__' keyword. */
12060 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12063 cp_parser_member_declaration (parser
);
12064 /* Restore the old value of the PEDANTIC flag. */
12065 pedantic
= saved_pedantic
;
12070 /* Check for a template-declaration. */
12071 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12073 /* Parse the template-declaration. */
12074 cp_parser_template_declaration (parser
, /*member_p=*/true);
12079 /* Check for a using-declaration. */
12080 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
12082 /* Parse the using-declaration. */
12083 cp_parser_using_declaration (parser
);
12088 /* Parse the decl-specifier-seq. */
12090 = cp_parser_decl_specifier_seq (parser
,
12091 CP_PARSER_FLAGS_OPTIONAL
,
12092 &prefix_attributes
,
12093 &declares_class_or_enum
);
12094 /* Check for an invalid type-name. */
12095 if (cp_parser_diagnose_invalid_type_name (parser
))
12097 /* If there is no declarator, then the decl-specifier-seq should
12099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12101 /* If there was no decl-specifier-seq, and the next token is a
12102 `;', then we have something like:
12108 Each member-declaration shall declare at least one member
12109 name of the class. */
12110 if (!decl_specifiers
)
12113 pedwarn ("extra semicolon");
12119 /* See if this declaration is a friend. */
12120 friend_p
= cp_parser_friend_p (decl_specifiers
);
12121 /* If there were decl-specifiers, check to see if there was
12122 a class-declaration. */
12123 type
= check_tag_decl (decl_specifiers
);
12124 /* Nested classes have already been added to the class, but
12125 a `friend' needs to be explicitly registered. */
12128 /* If the `friend' keyword was present, the friend must
12129 be introduced with a class-key. */
12130 if (!declares_class_or_enum
)
12131 error ("a class-key must be used when declaring a friend");
12134 template <typename T> struct A {
12135 friend struct A<T>::B;
12138 A<T>::B will be represented by a TYPENAME_TYPE, and
12139 therefore not recognized by check_tag_decl. */
12144 for (specifier
= decl_specifiers
;
12146 specifier
= TREE_CHAIN (specifier
))
12148 tree s
= TREE_VALUE (specifier
);
12150 if (TREE_CODE (s
) == IDENTIFIER_NODE
)
12151 get_global_value_if_present (s
, &type
);
12152 if (TREE_CODE (s
) == TYPE_DECL
)
12162 error ("friend declaration does not name a class or "
12165 make_friend_class (current_class_type
, type
,
12166 /*complain=*/true);
12168 /* If there is no TYPE, an error message will already have
12172 /* An anonymous aggregate has to be handled specially; such
12173 a declaration really declares a data member (with a
12174 particular type), as opposed to a nested class. */
12175 else if (ANON_AGGR_TYPE_P (type
))
12177 /* Remove constructors and such from TYPE, now that we
12178 know it is an anonymous aggregate. */
12179 fixup_anonymous_aggr (type
);
12180 /* And make the corresponding data member. */
12181 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
12182 /* Add it to the class. */
12183 finish_member_declaration (decl
);
12186 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
12191 /* See if these declarations will be friends. */
12192 friend_p
= cp_parser_friend_p (decl_specifiers
);
12194 /* Keep going until we hit the `;' at the end of the
12196 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12198 tree attributes
= NULL_TREE
;
12199 tree first_attribute
;
12201 /* Peek at the next token. */
12202 token
= cp_lexer_peek_token (parser
->lexer
);
12204 /* Check for a bitfield declaration. */
12205 if (token
->type
== CPP_COLON
12206 || (token
->type
== CPP_NAME
12207 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12213 /* Get the name of the bitfield. Note that we cannot just
12214 check TOKEN here because it may have been invalidated by
12215 the call to cp_lexer_peek_nth_token above. */
12216 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
12217 identifier
= cp_parser_identifier (parser
);
12219 identifier
= NULL_TREE
;
12221 /* Consume the `:' token. */
12222 cp_lexer_consume_token (parser
->lexer
);
12223 /* Get the width of the bitfield. */
12225 = cp_parser_constant_expression (parser
,
12226 /*allow_non_constant=*/false,
12229 /* Look for attributes that apply to the bitfield. */
12230 attributes
= cp_parser_attributes_opt (parser
);
12231 /* Remember which attributes are prefix attributes and
12233 first_attribute
= attributes
;
12234 /* Combine the attributes. */
12235 attributes
= chainon (prefix_attributes
, attributes
);
12237 /* Create the bitfield declaration. */
12238 decl
= grokbitfield (identifier
,
12241 /* Apply the attributes. */
12242 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
12248 tree asm_specification
;
12249 int ctor_dtor_or_conv_p
;
12251 /* Parse the declarator. */
12253 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12254 &ctor_dtor_or_conv_p
,
12255 /*parenthesized_p=*/NULL
);
12257 /* If something went wrong parsing the declarator, make sure
12258 that we at least consume some tokens. */
12259 if (declarator
== error_mark_node
)
12261 /* Skip to the end of the statement. */
12262 cp_parser_skip_to_end_of_statement (parser
);
12263 /* If the next token is not a semicolon, that is
12264 probably because we just skipped over the body of
12265 a function. So, we consume a semicolon if
12266 present, but do not issue an error message if it
12268 if (cp_lexer_next_token_is (parser
->lexer
,
12270 cp_lexer_consume_token (parser
->lexer
);
12274 cp_parser_check_for_definition_in_return_type
12275 (declarator
, declares_class_or_enum
);
12277 /* Look for an asm-specification. */
12278 asm_specification
= cp_parser_asm_specification_opt (parser
);
12279 /* Look for attributes that apply to the declaration. */
12280 attributes
= cp_parser_attributes_opt (parser
);
12281 /* Remember which attributes are prefix attributes and
12283 first_attribute
= attributes
;
12284 /* Combine the attributes. */
12285 attributes
= chainon (prefix_attributes
, attributes
);
12287 /* If it's an `=', then we have a constant-initializer or a
12288 pure-specifier. It is not correct to parse the
12289 initializer before registering the member declaration
12290 since the member declaration should be in scope while
12291 its initializer is processed. However, the rest of the
12292 front end does not yet provide an interface that allows
12293 us to handle this correctly. */
12294 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12298 A pure-specifier shall be used only in the declaration of
12299 a virtual function.
12301 A member-declarator can contain a constant-initializer
12302 only if it declares a static member of integral or
12305 Therefore, if the DECLARATOR is for a function, we look
12306 for a pure-specifier; otherwise, we look for a
12307 constant-initializer. When we call `grokfield', it will
12308 perform more stringent semantics checks. */
12309 if (TREE_CODE (declarator
) == CALL_EXPR
)
12310 initializer
= cp_parser_pure_specifier (parser
);
12312 /* Parse the initializer. */
12313 initializer
= cp_parser_constant_initializer (parser
);
12315 /* Otherwise, there is no initializer. */
12317 initializer
= NULL_TREE
;
12319 /* See if we are probably looking at a function
12320 definition. We are certainly not looking at at a
12321 member-declarator. Calling `grokfield' has
12322 side-effects, so we must not do it unless we are sure
12323 that we are looking at a member-declarator. */
12324 if (cp_parser_token_starts_function_definition_p
12325 (cp_lexer_peek_token (parser
->lexer
)))
12327 /* The grammar does not allow a pure-specifier to be
12328 used when a member function is defined. (It is
12329 possible that this fact is an oversight in the
12330 standard, since a pure function may be defined
12331 outside of the class-specifier. */
12333 error ("pure-specifier on function-definition");
12334 decl
= cp_parser_save_member_function_body (parser
,
12338 /* If the member was not a friend, declare it here. */
12340 finish_member_declaration (decl
);
12341 /* Peek at the next token. */
12342 token
= cp_lexer_peek_token (parser
->lexer
);
12343 /* If the next token is a semicolon, consume it. */
12344 if (token
->type
== CPP_SEMICOLON
)
12345 cp_lexer_consume_token (parser
->lexer
);
12350 /* Create the declaration. */
12351 decl
= grokfield (declarator
, decl_specifiers
,
12352 initializer
, asm_specification
,
12354 /* Any initialization must have been from a
12355 constant-expression. */
12356 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
12357 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
12361 /* Reset PREFIX_ATTRIBUTES. */
12362 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12363 attributes
= TREE_CHAIN (attributes
);
12365 TREE_CHAIN (attributes
) = NULL_TREE
;
12367 /* If there is any qualification still in effect, clear it
12368 now; we will be starting fresh with the next declarator. */
12369 parser
->scope
= NULL_TREE
;
12370 parser
->qualifying_scope
= NULL_TREE
;
12371 parser
->object_scope
= NULL_TREE
;
12372 /* If it's a `,', then there are more declarators. */
12373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12374 cp_lexer_consume_token (parser
->lexer
);
12375 /* If the next token isn't a `;', then we have a parse error. */
12376 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12379 cp_parser_error (parser
, "expected `;'");
12380 /* Skip tokens until we find a `;'. */
12381 cp_parser_skip_to_end_of_statement (parser
);
12388 /* Add DECL to the list of members. */
12390 finish_member_declaration (decl
);
12392 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12393 cp_parser_save_default_args (parser
, decl
);
12398 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12401 /* Parse a pure-specifier.
12406 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12407 Otherwise, ERROR_MARK_NODE is returned. */
12410 cp_parser_pure_specifier (cp_parser
* parser
)
12414 /* Look for the `=' token. */
12415 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12416 return error_mark_node
;
12417 /* Look for the `0' token. */
12418 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12419 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12420 to get information from the lexer about how the number was
12421 spelled in order to fix this problem. */
12422 if (!token
|| !integer_zerop (token
->value
))
12423 return error_mark_node
;
12425 return integer_zero_node
;
12428 /* Parse a constant-initializer.
12430 constant-initializer:
12431 = constant-expression
12433 Returns a representation of the constant-expression. */
12436 cp_parser_constant_initializer (cp_parser
* parser
)
12438 /* Look for the `=' token. */
12439 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12440 return error_mark_node
;
12442 /* It is invalid to write:
12444 struct S { static const int i = { 7 }; };
12447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12449 cp_parser_error (parser
,
12450 "a brace-enclosed initializer is not allowed here");
12451 /* Consume the opening brace. */
12452 cp_lexer_consume_token (parser
->lexer
);
12453 /* Skip the initializer. */
12454 cp_parser_skip_to_closing_brace (parser
);
12455 /* Look for the trailing `}'. */
12456 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12458 return error_mark_node
;
12461 return cp_parser_constant_expression (parser
,
12462 /*allow_non_constant=*/false,
12466 /* Derived classes [gram.class.derived] */
12468 /* Parse a base-clause.
12471 : base-specifier-list
12473 base-specifier-list:
12475 base-specifier-list , base-specifier
12477 Returns a TREE_LIST representing the base-classes, in the order in
12478 which they were declared. The representation of each node is as
12479 described by cp_parser_base_specifier.
12481 In the case that no bases are specified, this function will return
12482 NULL_TREE, not ERROR_MARK_NODE. */
12485 cp_parser_base_clause (cp_parser
* parser
)
12487 tree bases
= NULL_TREE
;
12489 /* Look for the `:' that begins the list. */
12490 cp_parser_require (parser
, CPP_COLON
, "`:'");
12492 /* Scan the base-specifier-list. */
12498 /* Look for the base-specifier. */
12499 base
= cp_parser_base_specifier (parser
);
12500 /* Add BASE to the front of the list. */
12501 if (base
!= error_mark_node
)
12503 TREE_CHAIN (base
) = bases
;
12506 /* Peek at the next token. */
12507 token
= cp_lexer_peek_token (parser
->lexer
);
12508 /* If it's not a comma, then the list is complete. */
12509 if (token
->type
!= CPP_COMMA
)
12511 /* Consume the `,'. */
12512 cp_lexer_consume_token (parser
->lexer
);
12515 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12516 base class had a qualified name. However, the next name that
12517 appears is certainly not qualified. */
12518 parser
->scope
= NULL_TREE
;
12519 parser
->qualifying_scope
= NULL_TREE
;
12520 parser
->object_scope
= NULL_TREE
;
12522 return nreverse (bases
);
12525 /* Parse a base-specifier.
12528 :: [opt] nested-name-specifier [opt] class-name
12529 virtual access-specifier [opt] :: [opt] nested-name-specifier
12531 access-specifier virtual [opt] :: [opt] nested-name-specifier
12534 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12535 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12536 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12537 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12540 cp_parser_base_specifier (cp_parser
* parser
)
12544 bool virtual_p
= false;
12545 bool duplicate_virtual_error_issued_p
= false;
12546 bool duplicate_access_error_issued_p
= false;
12547 bool class_scope_p
, template_p
;
12548 tree access
= access_default_node
;
12551 /* Process the optional `virtual' and `access-specifier'. */
12554 /* Peek at the next token. */
12555 token
= cp_lexer_peek_token (parser
->lexer
);
12556 /* Process `virtual'. */
12557 switch (token
->keyword
)
12560 /* If `virtual' appears more than once, issue an error. */
12561 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
12563 cp_parser_error (parser
,
12564 "`virtual' specified more than once in base-specified");
12565 duplicate_virtual_error_issued_p
= true;
12570 /* Consume the `virtual' token. */
12571 cp_lexer_consume_token (parser
->lexer
);
12576 case RID_PROTECTED
:
12578 /* If more than one access specifier appears, issue an
12580 if (access
!= access_default_node
12581 && !duplicate_access_error_issued_p
)
12583 cp_parser_error (parser
,
12584 "more than one access specifier in base-specified");
12585 duplicate_access_error_issued_p
= true;
12588 access
= ridpointers
[(int) token
->keyword
];
12590 /* Consume the access-specifier. */
12591 cp_lexer_consume_token (parser
->lexer
);
12601 /* Look for the optional `::' operator. */
12602 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
12603 /* Look for the nested-name-specifier. The simplest way to
12608 The keyword `typename' is not permitted in a base-specifier or
12609 mem-initializer; in these contexts a qualified name that
12610 depends on a template-parameter is implicitly assumed to be a
12613 is to pretend that we have seen the `typename' keyword at this
12615 cp_parser_nested_name_specifier_opt (parser
,
12616 /*typename_keyword_p=*/true,
12617 /*check_dependency_p=*/true,
12619 /*is_declaration=*/true);
12620 /* If the base class is given by a qualified name, assume that names
12621 we see are type names or templates, as appropriate. */
12622 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
12623 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
12625 /* Finally, look for the class-name. */
12626 type
= cp_parser_class_name (parser
,
12630 /*check_dependency_p=*/true,
12631 /*class_head_p=*/false,
12632 /*is_declaration=*/true);
12634 if (type
== error_mark_node
)
12635 return error_mark_node
;
12637 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
12640 /* Exception handling [gram.exception] */
12642 /* Parse an (optional) exception-specification.
12644 exception-specification:
12645 throw ( type-id-list [opt] )
12647 Returns a TREE_LIST representing the exception-specification. The
12648 TREE_VALUE of each node is a type. */
12651 cp_parser_exception_specification_opt (cp_parser
* parser
)
12656 /* Peek at the next token. */
12657 token
= cp_lexer_peek_token (parser
->lexer
);
12658 /* If it's not `throw', then there's no exception-specification. */
12659 if (!cp_parser_is_keyword (token
, RID_THROW
))
12662 /* Consume the `throw'. */
12663 cp_lexer_consume_token (parser
->lexer
);
12665 /* Look for the `('. */
12666 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12668 /* Peek at the next token. */
12669 token
= cp_lexer_peek_token (parser
->lexer
);
12670 /* If it's not a `)', then there is a type-id-list. */
12671 if (token
->type
!= CPP_CLOSE_PAREN
)
12673 const char *saved_message
;
12675 /* Types may not be defined in an exception-specification. */
12676 saved_message
= parser
->type_definition_forbidden_message
;
12677 parser
->type_definition_forbidden_message
12678 = "types may not be defined in an exception-specification";
12679 /* Parse the type-id-list. */
12680 type_id_list
= cp_parser_type_id_list (parser
);
12681 /* Restore the saved message. */
12682 parser
->type_definition_forbidden_message
= saved_message
;
12685 type_id_list
= empty_except_spec
;
12687 /* Look for the `)'. */
12688 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12690 return type_id_list
;
12693 /* Parse an (optional) type-id-list.
12697 type-id-list , type-id
12699 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12700 in the order that the types were presented. */
12703 cp_parser_type_id_list (cp_parser
* parser
)
12705 tree types
= NULL_TREE
;
12712 /* Get the next type-id. */
12713 type
= cp_parser_type_id (parser
);
12714 /* Add it to the list. */
12715 types
= add_exception_specifier (types
, type
, /*complain=*/1);
12716 /* Peek at the next token. */
12717 token
= cp_lexer_peek_token (parser
->lexer
);
12718 /* If it is not a `,', we are done. */
12719 if (token
->type
!= CPP_COMMA
)
12721 /* Consume the `,'. */
12722 cp_lexer_consume_token (parser
->lexer
);
12725 return nreverse (types
);
12728 /* Parse a try-block.
12731 try compound-statement handler-seq */
12734 cp_parser_try_block (cp_parser
* parser
)
12738 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
12739 try_block
= begin_try_block ();
12740 cp_parser_compound_statement (parser
, false);
12741 finish_try_block (try_block
);
12742 cp_parser_handler_seq (parser
);
12743 finish_handler_sequence (try_block
);
12748 /* Parse a function-try-block.
12750 function-try-block:
12751 try ctor-initializer [opt] function-body handler-seq */
12754 cp_parser_function_try_block (cp_parser
* parser
)
12757 bool ctor_initializer_p
;
12759 /* Look for the `try' keyword. */
12760 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
12762 /* Let the rest of the front-end know where we are. */
12763 try_block
= begin_function_try_block ();
12764 /* Parse the function-body. */
12766 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
12767 /* We're done with the `try' part. */
12768 finish_function_try_block (try_block
);
12769 /* Parse the handlers. */
12770 cp_parser_handler_seq (parser
);
12771 /* We're done with the handlers. */
12772 finish_function_handler_sequence (try_block
);
12774 return ctor_initializer_p
;
12777 /* Parse a handler-seq.
12780 handler handler-seq [opt] */
12783 cp_parser_handler_seq (cp_parser
* parser
)
12789 /* Parse the handler. */
12790 cp_parser_handler (parser
);
12791 /* Peek at the next token. */
12792 token
= cp_lexer_peek_token (parser
->lexer
);
12793 /* If it's not `catch' then there are no more handlers. */
12794 if (!cp_parser_is_keyword (token
, RID_CATCH
))
12799 /* Parse a handler.
12802 catch ( exception-declaration ) compound-statement */
12805 cp_parser_handler (cp_parser
* parser
)
12810 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
12811 handler
= begin_handler ();
12812 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12813 declaration
= cp_parser_exception_declaration (parser
);
12814 finish_handler_parms (declaration
, handler
);
12815 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12816 cp_parser_compound_statement (parser
, false);
12817 finish_handler (handler
);
12820 /* Parse an exception-declaration.
12822 exception-declaration:
12823 type-specifier-seq declarator
12824 type-specifier-seq abstract-declarator
12828 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12829 ellipsis variant is used. */
12832 cp_parser_exception_declaration (cp_parser
* parser
)
12834 tree type_specifiers
;
12836 const char *saved_message
;
12838 /* If it's an ellipsis, it's easy to handle. */
12839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12841 /* Consume the `...' token. */
12842 cp_lexer_consume_token (parser
->lexer
);
12846 /* Types may not be defined in exception-declarations. */
12847 saved_message
= parser
->type_definition_forbidden_message
;
12848 parser
->type_definition_forbidden_message
12849 = "types may not be defined in exception-declarations";
12851 /* Parse the type-specifier-seq. */
12852 type_specifiers
= cp_parser_type_specifier_seq (parser
);
12853 /* If it's a `)', then there is no declarator. */
12854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
12855 declarator
= NULL_TREE
;
12857 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
12858 /*ctor_dtor_or_conv_p=*/NULL
,
12859 /*parenthesized_p=*/NULL
);
12861 /* Restore the saved message. */
12862 parser
->type_definition_forbidden_message
= saved_message
;
12864 return start_handler_parms (type_specifiers
, declarator
);
12867 /* Parse a throw-expression.
12870 throw assignment-expression [opt]
12872 Returns a THROW_EXPR representing the throw-expression. */
12875 cp_parser_throw_expression (cp_parser
* parser
)
12880 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
12881 token
= cp_lexer_peek_token (parser
->lexer
);
12882 /* Figure out whether or not there is an assignment-expression
12883 following the "throw" keyword. */
12884 if (token
->type
== CPP_COMMA
12885 || token
->type
== CPP_SEMICOLON
12886 || token
->type
== CPP_CLOSE_PAREN
12887 || token
->type
== CPP_CLOSE_SQUARE
12888 || token
->type
== CPP_CLOSE_BRACE
12889 || token
->type
== CPP_COLON
)
12890 expression
= NULL_TREE
;
12892 expression
= cp_parser_assignment_expression (parser
);
12894 return build_throw (expression
);
12897 /* GNU Extensions */
12899 /* Parse an (optional) asm-specification.
12902 asm ( string-literal )
12904 If the asm-specification is present, returns a STRING_CST
12905 corresponding to the string-literal. Otherwise, returns
12909 cp_parser_asm_specification_opt (cp_parser
* parser
)
12912 tree asm_specification
;
12914 /* Peek at the next token. */
12915 token
= cp_lexer_peek_token (parser
->lexer
);
12916 /* If the next token isn't the `asm' keyword, then there's no
12917 asm-specification. */
12918 if (!cp_parser_is_keyword (token
, RID_ASM
))
12921 /* Consume the `asm' token. */
12922 cp_lexer_consume_token (parser
->lexer
);
12923 /* Look for the `('. */
12924 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12926 /* Look for the string-literal. */
12927 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12929 asm_specification
= token
->value
;
12931 asm_specification
= NULL_TREE
;
12933 /* Look for the `)'. */
12934 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
12936 return asm_specification
;
12939 /* Parse an asm-operand-list.
12943 asm-operand-list , asm-operand
12946 string-literal ( expression )
12947 [ string-literal ] string-literal ( expression )
12949 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12950 each node is the expression. The TREE_PURPOSE is itself a
12951 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12952 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12953 is a STRING_CST for the string literal before the parenthesis. */
12956 cp_parser_asm_operand_list (cp_parser
* parser
)
12958 tree asm_operands
= NULL_TREE
;
12962 tree string_literal
;
12967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
12969 /* Consume the `[' token. */
12970 cp_lexer_consume_token (parser
->lexer
);
12971 /* Read the operand name. */
12972 name
= cp_parser_identifier (parser
);
12973 if (name
!= error_mark_node
)
12974 name
= build_string (IDENTIFIER_LENGTH (name
),
12975 IDENTIFIER_POINTER (name
));
12976 /* Look for the closing `]'. */
12977 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
12981 /* Look for the string-literal. */
12982 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12983 string_literal
= token
? token
->value
: error_mark_node
;
12984 /* Look for the `('. */
12985 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12986 /* Parse the expression. */
12987 expression
= cp_parser_expression (parser
);
12988 /* Look for the `)'. */
12989 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12990 /* Add this operand to the list. */
12991 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
12994 /* If the next token is not a `,', there are no more
12996 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12998 /* Consume the `,'. */
12999 cp_lexer_consume_token (parser
->lexer
);
13002 return nreverse (asm_operands
);
13005 /* Parse an asm-clobber-list.
13009 asm-clobber-list , string-literal
13011 Returns a TREE_LIST, indicating the clobbers in the order that they
13012 appeared. The TREE_VALUE of each node is a STRING_CST. */
13015 cp_parser_asm_clobber_list (cp_parser
* parser
)
13017 tree clobbers
= NULL_TREE
;
13022 tree string_literal
;
13024 /* Look for the string literal. */
13025 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13026 string_literal
= token
? token
->value
: error_mark_node
;
13027 /* Add it to the list. */
13028 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13029 /* If the next token is not a `,', then the list is
13031 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13033 /* Consume the `,' token. */
13034 cp_lexer_consume_token (parser
->lexer
);
13040 /* Parse an (optional) series of attributes.
13043 attributes attribute
13046 __attribute__ (( attribute-list [opt] ))
13048 The return value is as for cp_parser_attribute_list. */
13051 cp_parser_attributes_opt (cp_parser
* parser
)
13053 tree attributes
= NULL_TREE
;
13058 tree attribute_list
;
13060 /* Peek at the next token. */
13061 token
= cp_lexer_peek_token (parser
->lexer
);
13062 /* If it's not `__attribute__', then we're done. */
13063 if (token
->keyword
!= RID_ATTRIBUTE
)
13066 /* Consume the `__attribute__' keyword. */
13067 cp_lexer_consume_token (parser
->lexer
);
13068 /* Look for the two `(' tokens. */
13069 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13070 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13072 /* Peek at the next token. */
13073 token
= cp_lexer_peek_token (parser
->lexer
);
13074 if (token
->type
!= CPP_CLOSE_PAREN
)
13075 /* Parse the attribute-list. */
13076 attribute_list
= cp_parser_attribute_list (parser
);
13078 /* If the next token is a `)', then there is no attribute
13080 attribute_list
= NULL
;
13082 /* Look for the two `)' tokens. */
13083 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13084 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13086 /* Add these new attributes to the list. */
13087 attributes
= chainon (attributes
, attribute_list
);
13093 /* Parse an attribute-list.
13097 attribute-list , attribute
13101 identifier ( identifier )
13102 identifier ( identifier , expression-list )
13103 identifier ( expression-list )
13105 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13106 TREE_PURPOSE of each node is the identifier indicating which
13107 attribute is in use. The TREE_VALUE represents the arguments, if
13111 cp_parser_attribute_list (cp_parser
* parser
)
13113 tree attribute_list
= NULL_TREE
;
13121 /* Look for the identifier. We also allow keywords here; for
13122 example `__attribute__ ((const))' is legal. */
13123 token
= cp_lexer_peek_token (parser
->lexer
);
13124 if (token
->type
!= CPP_NAME
13125 && token
->type
!= CPP_KEYWORD
)
13126 return error_mark_node
;
13127 /* Consume the token. */
13128 token
= cp_lexer_consume_token (parser
->lexer
);
13130 /* Save away the identifier that indicates which attribute this is. */
13131 identifier
= token
->value
;
13132 attribute
= build_tree_list (identifier
, NULL_TREE
);
13134 /* Peek at the next token. */
13135 token
= cp_lexer_peek_token (parser
->lexer
);
13136 /* If it's an `(', then parse the attribute arguments. */
13137 if (token
->type
== CPP_OPEN_PAREN
)
13141 arguments
= (cp_parser_parenthesized_expression_list
13142 (parser
, true, /*non_constant_p=*/NULL
));
13143 /* Save the identifier and arguments away. */
13144 TREE_VALUE (attribute
) = arguments
;
13147 /* Add this attribute to the list. */
13148 TREE_CHAIN (attribute
) = attribute_list
;
13149 attribute_list
= attribute
;
13151 /* Now, look for more attributes. */
13152 token
= cp_lexer_peek_token (parser
->lexer
);
13153 /* If the next token isn't a `,', we're done. */
13154 if (token
->type
!= CPP_COMMA
)
13157 /* Consume the comma and keep going. */
13158 cp_lexer_consume_token (parser
->lexer
);
13161 /* We built up the list in reverse order. */
13162 return nreverse (attribute_list
);
13165 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13166 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13167 current value of the PEDANTIC flag, regardless of whether or not
13168 the `__extension__' keyword is present. The caller is responsible
13169 for restoring the value of the PEDANTIC flag. */
13172 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
13174 /* Save the old value of the PEDANTIC flag. */
13175 *saved_pedantic
= pedantic
;
13177 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
13179 /* Consume the `__extension__' token. */
13180 cp_lexer_consume_token (parser
->lexer
);
13181 /* We're not being pedantic while the `__extension__' keyword is
13191 /* Parse a label declaration.
13194 __label__ label-declarator-seq ;
13196 label-declarator-seq:
13197 identifier , label-declarator-seq
13201 cp_parser_label_declaration (cp_parser
* parser
)
13203 /* Look for the `__label__' keyword. */
13204 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
13210 /* Look for an identifier. */
13211 identifier
= cp_parser_identifier (parser
);
13212 /* Declare it as a lobel. */
13213 finish_label_decl (identifier
);
13214 /* If the next token is a `;', stop. */
13215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13217 /* Look for the `,' separating the label declarations. */
13218 cp_parser_require (parser
, CPP_COMMA
, "`,'");
13221 /* Look for the final `;'. */
13222 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13225 /* Support Functions */
13227 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13228 NAME should have one of the representations used for an
13229 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13230 is returned. If PARSER->SCOPE is a dependent type, then a
13231 SCOPE_REF is returned.
13233 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13234 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13235 was formed. Abstractly, such entities should not be passed to this
13236 function, because they do not need to be looked up, but it is
13237 simpler to check for this special case here, rather than at the
13240 In cases not explicitly covered above, this function returns a
13241 DECL, OVERLOAD, or baselink representing the result of the lookup.
13242 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13245 If IS_TYPE is TRUE, bindings that do not refer to types are
13248 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13251 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13255 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
13256 bool is_type
, bool is_namespace
, bool check_dependency
)
13259 tree object_type
= parser
->context
->object_type
;
13261 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13262 no longer valid. Note that if we are parsing tentatively, and
13263 the parse fails, OBJECT_TYPE will be automatically restored. */
13264 parser
->context
->object_type
= NULL_TREE
;
13266 if (name
== error_mark_node
)
13267 return error_mark_node
;
13269 /* A template-id has already been resolved; there is no lookup to
13271 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13273 if (BASELINK_P (name
))
13275 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
13276 == TEMPLATE_ID_EXPR
),
13281 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13282 it should already have been checked to make sure that the name
13283 used matches the type being destroyed. */
13284 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13288 /* Figure out to which type this destructor applies. */
13290 type
= parser
->scope
;
13291 else if (object_type
)
13292 type
= object_type
;
13294 type
= current_class_type
;
13295 /* If that's not a class type, there is no destructor. */
13296 if (!type
|| !CLASS_TYPE_P (type
))
13297 return error_mark_node
;
13298 /* If it was a class type, return the destructor. */
13299 return CLASSTYPE_DESTRUCTORS (type
);
13302 /* By this point, the NAME should be an ordinary identifier. If
13303 the id-expression was a qualified name, the qualifying scope is
13304 stored in PARSER->SCOPE at this point. */
13305 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
13308 /* Perform the lookup. */
13313 if (parser
->scope
== error_mark_node
)
13314 return error_mark_node
;
13316 /* If the SCOPE is dependent, the lookup must be deferred until
13317 the template is instantiated -- unless we are explicitly
13318 looking up names in uninstantiated templates. Even then, we
13319 cannot look up the name if the scope is not a class type; it
13320 might, for example, be a template type parameter. */
13321 dependent_p
= (TYPE_P (parser
->scope
)
13322 && !(parser
->in_declarator_p
13323 && currently_open_class (parser
->scope
))
13324 && dependent_type_p (parser
->scope
));
13325 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
13329 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
13331 /* The resolution to Core Issue 180 says that `struct A::B'
13332 should be considered a type-name, even if `A' is
13334 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
13340 /* If PARSER->SCOPE is a dependent type, then it must be a
13341 class type, and we must not be checking dependencies;
13342 otherwise, we would have processed this lookup above. So
13343 that PARSER->SCOPE is not considered a dependent base by
13344 lookup_member, we must enter the scope here. */
13346 push_scope (parser
->scope
);
13347 /* If the PARSER->SCOPE is a a template specialization, it
13348 may be instantiated during name lookup. In that case,
13349 errors may be issued. Even if we rollback the current
13350 tentative parse, those errors are valid. */
13351 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
13352 /*complain=*/true);
13354 pop_scope (parser
->scope
);
13356 parser
->qualifying_scope
= parser
->scope
;
13357 parser
->object_scope
= NULL_TREE
;
13359 else if (object_type
)
13361 tree object_decl
= NULL_TREE
;
13362 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13363 OBJECT_TYPE is not a class. */
13364 if (CLASS_TYPE_P (object_type
))
13365 /* If the OBJECT_TYPE is a template specialization, it may
13366 be instantiated during name lookup. In that case, errors
13367 may be issued. Even if we rollback the current tentative
13368 parse, those errors are valid. */
13369 object_decl
= lookup_member (object_type
,
13371 /*protect=*/0, is_type
);
13372 /* Look it up in the enclosing context, too. */
13373 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13376 parser
->object_scope
= object_type
;
13377 parser
->qualifying_scope
= NULL_TREE
;
13379 decl
= object_decl
;
13383 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13386 parser
->qualifying_scope
= NULL_TREE
;
13387 parser
->object_scope
= NULL_TREE
;
13390 /* If the lookup failed, let our caller know. */
13392 || decl
== error_mark_node
13393 || (TREE_CODE (decl
) == FUNCTION_DECL
13394 && DECL_ANTICIPATED (decl
)))
13395 return error_mark_node
;
13397 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13398 if (TREE_CODE (decl
) == TREE_LIST
)
13400 /* The error message we have to print is too complicated for
13401 cp_parser_error, so we incorporate its actions directly. */
13402 if (!cp_parser_simulate_error (parser
))
13404 error ("reference to `%D' is ambiguous", name
);
13405 print_candidates (decl
);
13407 return error_mark_node
;
13410 my_friendly_assert (DECL_P (decl
)
13411 || TREE_CODE (decl
) == OVERLOAD
13412 || TREE_CODE (decl
) == SCOPE_REF
13413 || BASELINK_P (decl
),
13416 /* If we have resolved the name of a member declaration, check to
13417 see if the declaration is accessible. When the name resolves to
13418 set of overloaded functions, accessibility is checked when
13419 overload resolution is done.
13421 During an explicit instantiation, access is not checked at all,
13422 as per [temp.explicit]. */
13424 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
13429 /* Like cp_parser_lookup_name, but for use in the typical case where
13430 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13434 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13436 return cp_parser_lookup_name (parser
, name
,
13438 /*is_namespace=*/false,
13439 /*check_dependency=*/true);
13442 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13443 the current context, return the TYPE_DECL. If TAG_NAME_P is
13444 true, the DECL indicates the class being defined in a class-head,
13445 or declared in an elaborated-type-specifier.
13447 Otherwise, return DECL. */
13450 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13452 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13453 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13456 template <typename T> struct B;
13459 template <typename T> struct A::B {};
13461 Similarly, in a elaborated-type-specifier:
13463 namespace N { struct X{}; }
13466 template <typename T> friend struct N::X;
13469 However, if the DECL refers to a class type, and we are in
13470 the scope of the class, then the name lookup automatically
13471 finds the TYPE_DECL created by build_self_reference rather
13472 than a TEMPLATE_DECL. For example, in:
13474 template <class T> struct S {
13478 there is no need to handle such case. */
13480 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13481 return DECL_TEMPLATE_RESULT (decl
);
13486 /* If too many, or too few, template-parameter lists apply to the
13487 declarator, issue an error message. Returns TRUE if all went well,
13488 and FALSE otherwise. */
13491 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13494 unsigned num_templates
;
13496 /* We haven't seen any classes that involve template parameters yet. */
13499 switch (TREE_CODE (declarator
))
13506 tree main_declarator
= TREE_OPERAND (declarator
, 0);
13508 cp_parser_check_declarator_template_parameters (parser
,
13517 scope
= TREE_OPERAND (declarator
, 0);
13518 member
= TREE_OPERAND (declarator
, 1);
13520 /* If this is a pointer-to-member, then we are not interested
13521 in the SCOPE, because it does not qualify the thing that is
13523 if (TREE_CODE (member
) == INDIRECT_REF
)
13524 return (cp_parser_check_declarator_template_parameters
13527 while (scope
&& CLASS_TYPE_P (scope
))
13529 /* You're supposed to have one `template <...>'
13530 for every template class, but you don't need one
13531 for a full specialization. For example:
13533 template <class T> struct S{};
13534 template <> struct S<int> { void f(); };
13535 void S<int>::f () {}
13537 is correct; there shouldn't be a `template <>' for
13538 the definition of `S<int>::f'. */
13539 if (CLASSTYPE_TEMPLATE_INFO (scope
)
13540 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
13541 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
13542 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
13545 scope
= TYPE_CONTEXT (scope
);
13549 /* Fall through. */
13552 /* If the DECLARATOR has the form `X<y>' then it uses one
13553 additional level of template parameters. */
13554 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
13557 return cp_parser_check_template_parameters (parser
,
13562 /* NUM_TEMPLATES were used in the current declaration. If that is
13563 invalid, return FALSE and issue an error messages. Otherwise,
13567 cp_parser_check_template_parameters (cp_parser
* parser
,
13568 unsigned num_templates
)
13570 /* If there are more template classes than parameter lists, we have
13573 template <class T> void S<T>::R<T>::f (); */
13574 if (parser
->num_template_parameter_lists
< num_templates
)
13576 error ("too few template-parameter-lists");
13579 /* If there are the same number of template classes and parameter
13580 lists, that's OK. */
13581 if (parser
->num_template_parameter_lists
== num_templates
)
13583 /* If there are more, but only one more, then we are referring to a
13584 member template. That's OK too. */
13585 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
13587 /* Otherwise, there are too many template parameter lists. We have
13590 template <class T> template <class U> void S::f(); */
13591 error ("too many template-parameter-lists");
13595 /* Parse a binary-expression of the general form:
13599 binary-expression <token> <expr>
13601 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13602 to parser the <expr>s. If the first production is used, then the
13603 value returned by FN is returned directly. Otherwise, a node with
13604 the indicated EXPR_TYPE is returned, with operands corresponding to
13605 the two sub-expressions. */
13608 cp_parser_binary_expression (cp_parser
* parser
,
13609 const cp_parser_token_tree_map token_tree_map
,
13610 cp_parser_expression_fn fn
)
13614 /* Parse the first expression. */
13615 lhs
= (*fn
) (parser
);
13616 /* Now, look for more expressions. */
13620 const cp_parser_token_tree_map_node
*map_node
;
13623 /* Peek at the next token. */
13624 token
= cp_lexer_peek_token (parser
->lexer
);
13625 /* If the token is `>', and that's not an operator at the
13626 moment, then we're done. */
13627 if (token
->type
== CPP_GREATER
13628 && !parser
->greater_than_is_operator_p
)
13630 /* If we find one of the tokens we want, build the corresponding
13631 tree representation. */
13632 for (map_node
= token_tree_map
;
13633 map_node
->token_type
!= CPP_EOF
;
13635 if (map_node
->token_type
== token
->type
)
13637 /* Consume the operator token. */
13638 cp_lexer_consume_token (parser
->lexer
);
13639 /* Parse the right-hand side of the expression. */
13640 rhs
= (*fn
) (parser
);
13641 /* Build the binary tree node. */
13642 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
);
13646 /* If the token wasn't one of the ones we want, we're done. */
13647 if (map_node
->token_type
== CPP_EOF
)
13654 /* Parse an optional `::' token indicating that the following name is
13655 from the global namespace. If so, PARSER->SCOPE is set to the
13656 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13657 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13658 Returns the new value of PARSER->SCOPE, if the `::' token is
13659 present, and NULL_TREE otherwise. */
13662 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
13666 /* Peek at the next token. */
13667 token
= cp_lexer_peek_token (parser
->lexer
);
13668 /* If we're looking at a `::' token then we're starting from the
13669 global namespace, not our current location. */
13670 if (token
->type
== CPP_SCOPE
)
13672 /* Consume the `::' token. */
13673 cp_lexer_consume_token (parser
->lexer
);
13674 /* Set the SCOPE so that we know where to start the lookup. */
13675 parser
->scope
= global_namespace
;
13676 parser
->qualifying_scope
= global_namespace
;
13677 parser
->object_scope
= NULL_TREE
;
13679 return parser
->scope
;
13681 else if (!current_scope_valid_p
)
13683 parser
->scope
= NULL_TREE
;
13684 parser
->qualifying_scope
= NULL_TREE
;
13685 parser
->object_scope
= NULL_TREE
;
13691 /* Returns TRUE if the upcoming token sequence is the start of a
13692 constructor declarator. If FRIEND_P is true, the declarator is
13693 preceded by the `friend' specifier. */
13696 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
13698 bool constructor_p
;
13699 tree type_decl
= NULL_TREE
;
13700 bool nested_name_p
;
13701 cp_token
*next_token
;
13703 /* The common case is that this is not a constructor declarator, so
13704 try to avoid doing lots of work if at all possible. It's not
13705 valid declare a constructor at function scope. */
13706 if (at_function_scope_p ())
13708 /* And only certain tokens can begin a constructor declarator. */
13709 next_token
= cp_lexer_peek_token (parser
->lexer
);
13710 if (next_token
->type
!= CPP_NAME
13711 && next_token
->type
!= CPP_SCOPE
13712 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
13713 && next_token
->type
!= CPP_TEMPLATE_ID
)
13716 /* Parse tentatively; we are going to roll back all of the tokens
13718 cp_parser_parse_tentatively (parser
);
13719 /* Assume that we are looking at a constructor declarator. */
13720 constructor_p
= true;
13722 /* Look for the optional `::' operator. */
13723 cp_parser_global_scope_opt (parser
,
13724 /*current_scope_valid_p=*/false);
13725 /* Look for the nested-name-specifier. */
13727 = (cp_parser_nested_name_specifier_opt (parser
,
13728 /*typename_keyword_p=*/false,
13729 /*check_dependency_p=*/false,
13731 /*is_declaration=*/false)
13733 /* Outside of a class-specifier, there must be a
13734 nested-name-specifier. */
13735 if (!nested_name_p
&&
13736 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
13738 constructor_p
= false;
13739 /* If we still think that this might be a constructor-declarator,
13740 look for a class-name. */
13745 template <typename T> struct S { S(); };
13746 template <typename T> S<T>::S ();
13748 we must recognize that the nested `S' names a class.
13751 template <typename T> S<T>::S<T> ();
13753 we must recognize that the nested `S' names a template. */
13754 type_decl
= cp_parser_class_name (parser
,
13755 /*typename_keyword_p=*/false,
13756 /*template_keyword_p=*/false,
13758 /*check_dependency_p=*/false,
13759 /*class_head_p=*/false,
13760 /*is_declaration=*/false);
13761 /* If there was no class-name, then this is not a constructor. */
13762 constructor_p
= !cp_parser_error_occurred (parser
);
13765 /* If we're still considering a constructor, we have to see a `(',
13766 to begin the parameter-declaration-clause, followed by either a
13767 `)', an `...', or a decl-specifier. We need to check for a
13768 type-specifier to avoid being fooled into thinking that:
13772 is a constructor. (It is actually a function named `f' that
13773 takes one parameter (of type `int') and returns a value of type
13776 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
13778 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
13779 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
13780 && !cp_parser_storage_class_specifier_opt (parser
))
13783 unsigned saved_num_template_parameter_lists
;
13785 /* Names appearing in the type-specifier should be looked up
13786 in the scope of the class. */
13787 if (current_class_type
)
13791 type
= TREE_TYPE (type_decl
);
13792 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13794 type
= resolve_typename_type (type
,
13795 /*only_current_p=*/false);
13796 if (type
== error_mark_node
)
13798 cp_parser_abort_tentative_parse (parser
);
13805 /* Inside the constructor parameter list, surrounding
13806 template-parameter-lists do not apply. */
13807 saved_num_template_parameter_lists
13808 = parser
->num_template_parameter_lists
;
13809 parser
->num_template_parameter_lists
= 0;
13811 /* Look for the type-specifier. */
13812 cp_parser_type_specifier (parser
,
13813 CP_PARSER_FLAGS_NONE
,
13814 /*is_friend=*/false,
13815 /*is_declarator=*/true,
13816 /*declares_class_or_enum=*/NULL
,
13817 /*is_cv_qualifier=*/NULL
);
13819 parser
->num_template_parameter_lists
13820 = saved_num_template_parameter_lists
;
13822 /* Leave the scope of the class. */
13826 constructor_p
= !cp_parser_error_occurred (parser
);
13830 constructor_p
= false;
13831 /* We did not really want to consume any tokens. */
13832 cp_parser_abort_tentative_parse (parser
);
13834 return constructor_p
;
13837 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13838 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13839 they must be performed once we are in the scope of the function.
13841 Returns the function defined. */
13844 cp_parser_function_definition_from_specifiers_and_declarator
13845 (cp_parser
* parser
,
13846 tree decl_specifiers
,
13853 /* Begin the function-definition. */
13854 success_p
= begin_function_definition (decl_specifiers
,
13858 /* If there were names looked up in the decl-specifier-seq that we
13859 did not check, check them now. We must wait until we are in the
13860 scope of the function to perform the checks, since the function
13861 might be a friend. */
13862 perform_deferred_access_checks ();
13866 /* If begin_function_definition didn't like the definition, skip
13867 the entire function. */
13868 error ("invalid function declaration");
13869 cp_parser_skip_to_end_of_block_or_statement (parser
);
13870 fn
= error_mark_node
;
13873 fn
= cp_parser_function_definition_after_declarator (parser
,
13874 /*inline_p=*/false);
13879 /* Parse the part of a function-definition that follows the
13880 declarator. INLINE_P is TRUE iff this function is an inline
13881 function defined with a class-specifier.
13883 Returns the function defined. */
13886 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
13890 bool ctor_initializer_p
= false;
13891 bool saved_in_unbraced_linkage_specification_p
;
13892 unsigned saved_num_template_parameter_lists
;
13894 /* If the next token is `return', then the code may be trying to
13895 make use of the "named return value" extension that G++ used to
13897 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
13899 /* Consume the `return' keyword. */
13900 cp_lexer_consume_token (parser
->lexer
);
13901 /* Look for the identifier that indicates what value is to be
13903 cp_parser_identifier (parser
);
13904 /* Issue an error message. */
13905 error ("named return values are no longer supported");
13906 /* Skip tokens until we reach the start of the function body. */
13907 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
13908 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
13909 cp_lexer_consume_token (parser
->lexer
);
13911 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13912 anything declared inside `f'. */
13913 saved_in_unbraced_linkage_specification_p
13914 = parser
->in_unbraced_linkage_specification_p
;
13915 parser
->in_unbraced_linkage_specification_p
= false;
13916 /* Inside the function, surrounding template-parameter-lists do not
13918 saved_num_template_parameter_lists
13919 = parser
->num_template_parameter_lists
;
13920 parser
->num_template_parameter_lists
= 0;
13921 /* If the next token is `try', then we are looking at a
13922 function-try-block. */
13923 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
13924 ctor_initializer_p
= cp_parser_function_try_block (parser
);
13925 /* A function-try-block includes the function-body, so we only do
13926 this next part if we're not processing a function-try-block. */
13929 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13931 /* Finish the function. */
13932 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
13933 (inline_p
? 2 : 0));
13934 /* Generate code for it, if necessary. */
13935 expand_or_defer_fn (fn
);
13936 /* Restore the saved values. */
13937 parser
->in_unbraced_linkage_specification_p
13938 = saved_in_unbraced_linkage_specification_p
;
13939 parser
->num_template_parameter_lists
13940 = saved_num_template_parameter_lists
;
13945 /* Parse a template-declaration, assuming that the `export' (and
13946 `extern') keywords, if present, has already been scanned. MEMBER_P
13947 is as for cp_parser_template_declaration. */
13950 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
13952 tree decl
= NULL_TREE
;
13953 tree parameter_list
;
13954 bool friend_p
= false;
13956 /* Look for the `template' keyword. */
13957 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
13961 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
13964 /* Parse the template parameters. */
13965 begin_template_parm_list ();
13966 /* If the next token is `>', then we have an invalid
13967 specialization. Rather than complain about an invalid template
13968 parameter, issue an error message here. */
13969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
13971 cp_parser_error (parser
, "invalid explicit specialization");
13972 parameter_list
= NULL_TREE
;
13975 parameter_list
= cp_parser_template_parameter_list (parser
);
13976 parameter_list
= end_template_parm_list (parameter_list
);
13977 /* Look for the `>'. */
13978 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
13979 /* We just processed one more parameter list. */
13980 ++parser
->num_template_parameter_lists
;
13981 /* If the next token is `template', there are more template
13983 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13985 cp_parser_template_declaration_after_export (parser
, member_p
);
13988 decl
= cp_parser_single_declaration (parser
,
13992 /* If this is a member template declaration, let the front
13994 if (member_p
&& !friend_p
&& decl
)
13996 if (TREE_CODE (decl
) == TYPE_DECL
)
13997 cp_parser_check_access_in_redeclaration (decl
);
13999 decl
= finish_member_template_decl (decl
);
14001 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14002 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14003 /*complain=*/true);
14005 /* We are done with the current parameter list. */
14006 --parser
->num_template_parameter_lists
;
14009 finish_template_decl (parameter_list
);
14011 /* Register member declarations. */
14012 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14013 finish_member_declaration (decl
);
14015 /* If DECL is a function template, we must return to parse it later.
14016 (Even though there is no definition, there might be default
14017 arguments that need handling.) */
14018 if (member_p
&& decl
14019 && (TREE_CODE (decl
) == FUNCTION_DECL
14020 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14021 TREE_VALUE (parser
->unparsed_functions_queues
)
14022 = tree_cons (NULL_TREE
, decl
,
14023 TREE_VALUE (parser
->unparsed_functions_queues
));
14026 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14027 `function-definition' sequence. MEMBER_P is true, this declaration
14028 appears in a class scope.
14030 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14031 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14034 cp_parser_single_declaration (cp_parser
* parser
,
14038 int declares_class_or_enum
;
14039 tree decl
= NULL_TREE
;
14040 tree decl_specifiers
;
14042 bool function_definition_p
= false;
14044 /* Defer access checks until we know what is being declared. */
14045 push_deferring_access_checks (dk_deferred
);
14047 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14050 = cp_parser_decl_specifier_seq (parser
,
14051 CP_PARSER_FLAGS_OPTIONAL
,
14053 &declares_class_or_enum
);
14055 *friend_p
= cp_parser_friend_p (decl_specifiers
);
14056 /* Gather up the access checks that occurred the
14057 decl-specifier-seq. */
14058 stop_deferring_access_checks ();
14060 /* Check for the declaration of a template class. */
14061 if (declares_class_or_enum
)
14063 if (cp_parser_declares_only_class_p (parser
))
14065 decl
= shadow_tag (decl_specifiers
);
14067 decl
= TYPE_NAME (decl
);
14069 decl
= error_mark_node
;
14074 /* If it's not a template class, try for a template function. If
14075 the next token is a `;', then this declaration does not declare
14076 anything. But, if there were errors in the decl-specifiers, then
14077 the error might well have come from an attempted class-specifier.
14078 In that case, there's no need to warn about a missing declarator. */
14080 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
14081 || !value_member (error_mark_node
, decl_specifiers
)))
14082 decl
= cp_parser_init_declarator (parser
,
14085 /*function_definition_allowed_p=*/true,
14087 declares_class_or_enum
,
14088 &function_definition_p
);
14090 pop_deferring_access_checks ();
14092 /* Clear any current qualification; whatever comes next is the start
14093 of something new. */
14094 parser
->scope
= NULL_TREE
;
14095 parser
->qualifying_scope
= NULL_TREE
;
14096 parser
->object_scope
= NULL_TREE
;
14097 /* Look for a trailing `;' after the declaration. */
14098 if (!function_definition_p
14099 && !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
14100 cp_parser_skip_to_end_of_block_or_statement (parser
);
14105 /* Parse a cast-expression that is not the operand of a unary "&". */
14108 cp_parser_simple_cast_expression (cp_parser
*parser
)
14110 return cp_parser_cast_expression (parser
, /*address_p=*/false);
14113 /* Parse a functional cast to TYPE. Returns an expression
14114 representing the cast. */
14117 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14119 tree expression_list
;
14122 = cp_parser_parenthesized_expression_list (parser
, false,
14123 /*non_constant_p=*/NULL
);
14125 return build_functional_cast (type
, expression_list
);
14128 /* Save the tokens that make up the body of a member function defined
14129 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14130 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14131 specifiers applied to the declaration. Returns the FUNCTION_DECL
14132 for the member function. */
14135 cp_parser_save_member_function_body (cp_parser
* parser
,
14136 tree decl_specifiers
,
14140 cp_token_cache
*cache
;
14143 /* Create the function-declaration. */
14144 fn
= start_method (decl_specifiers
, declarator
, attributes
);
14145 /* If something went badly wrong, bail out now. */
14146 if (fn
== error_mark_node
)
14148 /* If there's a function-body, skip it. */
14149 if (cp_parser_token_starts_function_definition_p
14150 (cp_lexer_peek_token (parser
->lexer
)))
14151 cp_parser_skip_to_end_of_block_or_statement (parser
);
14152 return error_mark_node
;
14155 /* Remember it, if there default args to post process. */
14156 cp_parser_save_default_args (parser
, fn
);
14158 /* Create a token cache. */
14159 cache
= cp_token_cache_new ();
14160 /* Save away the tokens that make up the body of the
14162 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14163 /* Handle function try blocks. */
14164 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
14165 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14167 /* Save away the inline definition; we will process it when the
14168 class is complete. */
14169 DECL_PENDING_INLINE_INFO (fn
) = cache
;
14170 DECL_PENDING_INLINE_P (fn
) = 1;
14172 /* We need to know that this was defined in the class, so that
14173 friend templates are handled correctly. */
14174 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
14176 /* We're done with the inline definition. */
14177 finish_method (fn
);
14179 /* Add FN to the queue of functions to be parsed later. */
14180 TREE_VALUE (parser
->unparsed_functions_queues
)
14181 = tree_cons (NULL_TREE
, fn
,
14182 TREE_VALUE (parser
->unparsed_functions_queues
));
14187 /* Parse a template-argument-list, as well as the trailing ">" (but
14188 not the opening ">"). See cp_parser_template_argument_list for the
14192 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
14196 tree saved_qualifying_scope
;
14197 tree saved_object_scope
;
14198 bool saved_greater_than_is_operator_p
;
14202 When parsing a template-id, the first non-nested `>' is taken as
14203 the end of the template-argument-list rather than a greater-than
14205 saved_greater_than_is_operator_p
14206 = parser
->greater_than_is_operator_p
;
14207 parser
->greater_than_is_operator_p
= false;
14208 /* Parsing the argument list may modify SCOPE, so we save it
14210 saved_scope
= parser
->scope
;
14211 saved_qualifying_scope
= parser
->qualifying_scope
;
14212 saved_object_scope
= parser
->object_scope
;
14213 /* Parse the template-argument-list itself. */
14214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14215 arguments
= NULL_TREE
;
14217 arguments
= cp_parser_template_argument_list (parser
);
14218 /* Look for the `>' that ends the template-argument-list. If we find
14219 a '>>' instead, it's probably just a typo. */
14220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14222 if (!saved_greater_than_is_operator_p
)
14224 /* If we're in a nested template argument list, the '>>' has to be
14225 a typo for '> >'. We emit the error message, but we continue
14226 parsing and we push a '>' as next token, so that the argument
14227 list will be parsed correctly.. */
14229 error ("`>>' should be `> >' within a nested template argument list");
14230 token
= cp_lexer_peek_token (parser
->lexer
);
14231 token
->type
= CPP_GREATER
;
14235 /* If this is not a nested template argument list, the '>>' is
14236 a typo for '>'. Emit an error message and continue. */
14237 error ("spurious `>>', use `>' to terminate a template argument list");
14238 cp_lexer_consume_token (parser
->lexer
);
14242 cp_parser_require (parser
, CPP_GREATER
, "`>'");
14243 /* The `>' token might be a greater-than operator again now. */
14244 parser
->greater_than_is_operator_p
14245 = saved_greater_than_is_operator_p
;
14246 /* Restore the SAVED_SCOPE. */
14247 parser
->scope
= saved_scope
;
14248 parser
->qualifying_scope
= saved_qualifying_scope
;
14249 parser
->object_scope
= saved_object_scope
;
14254 /* MEMBER_FUNCTION is a member function, or a friend. If default
14255 arguments, or the body of the function have not yet been parsed,
14259 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
14261 cp_lexer
*saved_lexer
;
14263 /* If this member is a template, get the underlying
14265 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
14266 member_function
= DECL_TEMPLATE_RESULT (member_function
);
14268 /* There should not be any class definitions in progress at this
14269 point; the bodies of members are only parsed outside of all class
14271 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
14272 /* While we're parsing the member functions we might encounter more
14273 classes. We want to handle them right away, but we don't want
14274 them getting mixed up with functions that are currently in the
14276 parser
->unparsed_functions_queues
14277 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14279 /* Make sure that any template parameters are in scope. */
14280 maybe_begin_member_template_processing (member_function
);
14282 /* If the body of the function has not yet been parsed, parse it
14284 if (DECL_PENDING_INLINE_P (member_function
))
14286 tree function_scope
;
14287 cp_token_cache
*tokens
;
14289 /* The function is no longer pending; we are processing it. */
14290 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
14291 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
14292 DECL_PENDING_INLINE_P (member_function
) = 0;
14293 /* If this was an inline function in a local class, enter the scope
14294 of the containing function. */
14295 function_scope
= decl_function_context (member_function
);
14296 if (function_scope
)
14297 push_function_context_to (function_scope
);
14299 /* Save away the current lexer. */
14300 saved_lexer
= parser
->lexer
;
14301 /* Make a new lexer to feed us the tokens saved for this function. */
14302 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14303 parser
->lexer
->next
= saved_lexer
;
14305 /* Set the current source position to be the location of the first
14306 token in the saved inline body. */
14307 cp_lexer_peek_token (parser
->lexer
);
14309 /* Let the front end know that we going to be defining this
14311 start_function (NULL_TREE
, member_function
, NULL_TREE
,
14312 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
14314 /* Now, parse the body of the function. */
14315 cp_parser_function_definition_after_declarator (parser
,
14316 /*inline_p=*/true);
14318 /* Leave the scope of the containing function. */
14319 if (function_scope
)
14320 pop_function_context_from (function_scope
);
14321 /* Restore the lexer. */
14322 parser
->lexer
= saved_lexer
;
14325 /* Remove any template parameters from the symbol table. */
14326 maybe_end_member_template_processing ();
14328 /* Restore the queue. */
14329 parser
->unparsed_functions_queues
14330 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14333 /* If DECL contains any default args, remember it on the unparsed
14334 functions queue. */
14337 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
14341 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14343 probe
= TREE_CHAIN (probe
))
14344 if (TREE_PURPOSE (probe
))
14346 TREE_PURPOSE (parser
->unparsed_functions_queues
)
14347 = tree_cons (NULL_TREE
, decl
,
14348 TREE_PURPOSE (parser
->unparsed_functions_queues
));
14354 /* FN is a FUNCTION_DECL which may contains a parameter with an
14355 unparsed DEFAULT_ARG. Parse the default args now. */
14358 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
14360 cp_lexer
*saved_lexer
;
14361 cp_token_cache
*tokens
;
14362 bool saved_local_variables_forbidden_p
;
14365 /* While we're parsing the default args, we might (due to the
14366 statement expression extension) encounter more classes. We want
14367 to handle them right away, but we don't want them getting mixed
14368 up with default args that are currently in the queue. */
14369 parser
->unparsed_functions_queues
14370 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14372 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14374 parameters
= TREE_CHAIN (parameters
))
14376 if (!TREE_PURPOSE (parameters
)
14377 || TREE_CODE (TREE_PURPOSE (parameters
)) != DEFAULT_ARG
)
14380 /* Save away the current lexer. */
14381 saved_lexer
= parser
->lexer
;
14382 /* Create a new one, using the tokens we have saved. */
14383 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parameters
));
14384 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14386 /* Set the current source position to be the location of the
14387 first token in the default argument. */
14388 cp_lexer_peek_token (parser
->lexer
);
14390 /* Local variable names (and the `this' keyword) may not appear
14391 in a default argument. */
14392 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14393 parser
->local_variables_forbidden_p
= true;
14394 /* Parse the assignment-expression. */
14395 if (DECL_CLASS_SCOPE_P (fn
))
14396 push_nested_class (DECL_CONTEXT (fn
));
14397 TREE_PURPOSE (parameters
) = cp_parser_assignment_expression (parser
);
14398 if (DECL_CLASS_SCOPE_P (fn
))
14399 pop_nested_class ();
14401 /* Restore saved state. */
14402 parser
->lexer
= saved_lexer
;
14403 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14406 /* Restore the queue. */
14407 parser
->unparsed_functions_queues
14408 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14411 /* Parse the operand of `sizeof' (or a similar operator). Returns
14412 either a TYPE or an expression, depending on the form of the
14413 input. The KEYWORD indicates which kind of expression we have
14417 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
14419 static const char *format
;
14420 tree expr
= NULL_TREE
;
14421 const char *saved_message
;
14422 bool saved_integral_constant_expression_p
;
14424 /* Initialize FORMAT the first time we get here. */
14426 format
= "types may not be defined in `%s' expressions";
14428 /* Types cannot be defined in a `sizeof' expression. Save away the
14430 saved_message
= parser
->type_definition_forbidden_message
;
14431 /* And create the new one. */
14432 parser
->type_definition_forbidden_message
14433 = xmalloc (strlen (format
)
14434 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
14436 sprintf ((char *) parser
->type_definition_forbidden_message
,
14437 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
14439 /* The restrictions on constant-expressions do not apply inside
14440 sizeof expressions. */
14441 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
14442 parser
->integral_constant_expression_p
= false;
14444 /* Do not actually evaluate the expression. */
14446 /* If it's a `(', then we might be looking at the type-id
14448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14452 /* We can't be sure yet whether we're looking at a type-id or an
14454 cp_parser_parse_tentatively (parser
);
14455 /* Consume the `('. */
14456 cp_lexer_consume_token (parser
->lexer
);
14457 /* Parse the type-id. */
14458 type
= cp_parser_type_id (parser
);
14459 /* Now, look for the trailing `)'. */
14460 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14461 /* If all went well, then we're done. */
14462 if (cp_parser_parse_definitely (parser
))
14464 /* Build a list of decl-specifiers; right now, we have only
14465 a single type-specifier. */
14466 type
= build_tree_list (NULL_TREE
,
14469 /* Call grokdeclarator to figure out what type this is. */
14470 expr
= grokdeclarator (NULL_TREE
,
14474 /*attrlist=*/NULL
);
14478 /* If the type-id production did not work out, then we must be
14479 looking at the unary-expression production. */
14481 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
14482 /* Go back to evaluating expressions. */
14485 /* Free the message we created. */
14486 free ((char *) parser
->type_definition_forbidden_message
);
14487 /* And restore the old one. */
14488 parser
->type_definition_forbidden_message
= saved_message
;
14489 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
14494 /* If the current declaration has no declarator, return true. */
14497 cp_parser_declares_only_class_p (cp_parser
*parser
)
14499 /* If the next token is a `;' or a `,' then there is no
14501 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14502 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14505 /* Simplify EXPR if it is a non-dependent expression. Returns the
14506 (possibly simplified) expression. */
14509 cp_parser_fold_non_dependent_expr (tree expr
)
14511 /* If we're in a template, but EXPR isn't value dependent, simplify
14512 it. We're supposed to treat:
14514 template <typename T> void f(T[1 + 1]);
14515 template <typename T> void f(T[2]);
14517 as two declarations of the same function, for example. */
14518 if (processing_template_decl
14519 && !type_dependent_expression_p (expr
)
14520 && !value_dependent_expression_p (expr
))
14522 HOST_WIDE_INT saved_processing_template_decl
;
14524 saved_processing_template_decl
= processing_template_decl
;
14525 processing_template_decl
= 0;
14526 expr
= tsubst_copy_and_build (expr
,
14527 /*args=*/NULL_TREE
,
14529 /*in_decl=*/NULL_TREE
,
14530 /*function_p=*/false);
14531 processing_template_decl
= saved_processing_template_decl
;
14536 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14537 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14540 cp_parser_friend_p (tree decl_specifiers
)
14542 while (decl_specifiers
)
14544 /* See if this decl-specifier is `friend'. */
14545 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
14546 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
14549 /* Go on to the next decl-specifier. */
14550 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
14556 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14557 issue an error message indicating that TOKEN_DESC was expected.
14559 Returns the token consumed, if the token had the appropriate type.
14560 Otherwise, returns NULL. */
14563 cp_parser_require (cp_parser
* parser
,
14564 enum cpp_ttype type
,
14565 const char* token_desc
)
14567 if (cp_lexer_next_token_is (parser
->lexer
, type
))
14568 return cp_lexer_consume_token (parser
->lexer
);
14571 /* Output the MESSAGE -- unless we're parsing tentatively. */
14572 if (!cp_parser_simulate_error (parser
))
14573 error ("expected %s", token_desc
);
14578 /* Like cp_parser_require, except that tokens will be skipped until
14579 the desired token is found. An error message is still produced if
14580 the next token is not as expected. */
14583 cp_parser_skip_until_found (cp_parser
* parser
,
14584 enum cpp_ttype type
,
14585 const char* token_desc
)
14588 unsigned nesting_depth
= 0;
14590 if (cp_parser_require (parser
, type
, token_desc
))
14593 /* Skip tokens until the desired token is found. */
14596 /* Peek at the next token. */
14597 token
= cp_lexer_peek_token (parser
->lexer
);
14598 /* If we've reached the token we want, consume it and
14600 if (token
->type
== type
&& !nesting_depth
)
14602 cp_lexer_consume_token (parser
->lexer
);
14605 /* If we've run out of tokens, stop. */
14606 if (token
->type
== CPP_EOF
)
14608 if (token
->type
== CPP_OPEN_BRACE
14609 || token
->type
== CPP_OPEN_PAREN
14610 || token
->type
== CPP_OPEN_SQUARE
)
14612 else if (token
->type
== CPP_CLOSE_BRACE
14613 || token
->type
== CPP_CLOSE_PAREN
14614 || token
->type
== CPP_CLOSE_SQUARE
)
14616 if (nesting_depth
-- == 0)
14619 /* Consume this token. */
14620 cp_lexer_consume_token (parser
->lexer
);
14624 /* If the next token is the indicated keyword, consume it. Otherwise,
14625 issue an error message indicating that TOKEN_DESC was expected.
14627 Returns the token consumed, if the token had the appropriate type.
14628 Otherwise, returns NULL. */
14631 cp_parser_require_keyword (cp_parser
* parser
,
14633 const char* token_desc
)
14635 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
14637 if (token
&& token
->keyword
!= keyword
)
14639 dyn_string_t error_msg
;
14641 /* Format the error message. */
14642 error_msg
= dyn_string_new (0);
14643 dyn_string_append_cstr (error_msg
, "expected ");
14644 dyn_string_append_cstr (error_msg
, token_desc
);
14645 cp_parser_error (parser
, error_msg
->s
);
14646 dyn_string_delete (error_msg
);
14653 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14654 function-definition. */
14657 cp_parser_token_starts_function_definition_p (cp_token
* token
)
14659 return (/* An ordinary function-body begins with an `{'. */
14660 token
->type
== CPP_OPEN_BRACE
14661 /* A ctor-initializer begins with a `:'. */
14662 || token
->type
== CPP_COLON
14663 /* A function-try-block begins with `try'. */
14664 || token
->keyword
== RID_TRY
14665 /* The named return value extension begins with `return'. */
14666 || token
->keyword
== RID_RETURN
);
14669 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14673 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
14677 token
= cp_lexer_peek_token (parser
->lexer
);
14678 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
14681 /* Returns TRUE iff the next token is the "," or ">" ending a
14682 template-argument. ">>" is also accepted (after the full
14683 argument was parsed) because it's probably a typo for "> >",
14684 and there is a specific diagnostic for this. */
14687 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
14691 token
= cp_lexer_peek_token (parser
->lexer
);
14692 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
14693 || token
->type
== CPP_RSHIFT
);
14696 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14697 or none_type otherwise. */
14699 static enum tag_types
14700 cp_parser_token_is_class_key (cp_token
* token
)
14702 switch (token
->keyword
)
14707 return record_type
;
14716 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14719 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
14721 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
14722 pedwarn ("`%s' tag used in naming `%#T'",
14723 class_key
== union_type
? "union"
14724 : class_key
== record_type
? "struct" : "class",
14728 /* Issue an error message if DECL is redeclared with different
14729 access than its original declaration [class.access.spec/3].
14730 This applies to nested classes and nested class templates.
14733 static void cp_parser_check_access_in_redeclaration (tree decl
)
14735 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
14738 if ((TREE_PRIVATE (decl
)
14739 != (current_access_specifier
== access_private_node
))
14740 || (TREE_PROTECTED (decl
)
14741 != (current_access_specifier
== access_protected_node
)))
14742 error ("%D redeclared with different access", decl
);
14745 /* Look for the `template' keyword, as a syntactic disambiguator.
14746 Return TRUE iff it is present, in which case it will be
14750 cp_parser_optional_template_keyword (cp_parser
*parser
)
14752 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14754 /* The `template' keyword can only be used within templates;
14755 outside templates the parser can always figure out what is a
14756 template and what is not. */
14757 if (!processing_template_decl
)
14759 error ("`template' (as a disambiguator) is only allowed "
14760 "within templates");
14761 /* If this part of the token stream is rescanned, the same
14762 error message would be generated. So, we purge the token
14763 from the stream. */
14764 cp_lexer_purge_token (parser
->lexer
);
14769 /* Consume the `template' keyword. */
14770 cp_lexer_consume_token (parser
->lexer
);
14778 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14779 set PARSER->SCOPE, and perform other related actions. */
14782 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
14787 /* Get the stored value. */
14788 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
14789 /* Perform any access checks that were deferred. */
14790 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
14791 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
14792 /* Set the scope from the stored value. */
14793 parser
->scope
= TREE_VALUE (value
);
14794 parser
->qualifying_scope
= TREE_TYPE (value
);
14795 parser
->object_scope
= NULL_TREE
;
14798 /* Add tokens to CACHE until an non-nested END token appears. */
14801 cp_parser_cache_group (cp_parser
*parser
,
14802 cp_token_cache
*cache
,
14803 enum cpp_ttype end
,
14810 /* Abort a parenthesized expression if we encounter a brace. */
14811 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
14812 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14814 /* Consume the next token. */
14815 token
= cp_lexer_consume_token (parser
->lexer
);
14816 /* If we've reached the end of the file, stop. */
14817 if (token
->type
== CPP_EOF
)
14819 /* Add this token to the tokens we are saving. */
14820 cp_token_cache_push_token (cache
, token
);
14821 /* See if it starts a new group. */
14822 if (token
->type
== CPP_OPEN_BRACE
)
14824 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
14828 else if (token
->type
== CPP_OPEN_PAREN
)
14829 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
14830 else if (token
->type
== end
)
14835 /* Begin parsing tentatively. We always save tokens while parsing
14836 tentatively so that if the tentative parsing fails we can restore the
14840 cp_parser_parse_tentatively (cp_parser
* parser
)
14842 /* Enter a new parsing context. */
14843 parser
->context
= cp_parser_context_new (parser
->context
);
14844 /* Begin saving tokens. */
14845 cp_lexer_save_tokens (parser
->lexer
);
14846 /* In order to avoid repetitive access control error messages,
14847 access checks are queued up until we are no longer parsing
14849 push_deferring_access_checks (dk_deferred
);
14852 /* Commit to the currently active tentative parse. */
14855 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
14857 cp_parser_context
*context
;
14860 /* Mark all of the levels as committed. */
14861 lexer
= parser
->lexer
;
14862 for (context
= parser
->context
; context
->next
; context
= context
->next
)
14864 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
14866 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
14867 while (!cp_lexer_saving_tokens (lexer
))
14868 lexer
= lexer
->next
;
14869 cp_lexer_commit_tokens (lexer
);
14873 /* Abort the currently active tentative parse. All consumed tokens
14874 will be rolled back, and no diagnostics will be issued. */
14877 cp_parser_abort_tentative_parse (cp_parser
* parser
)
14879 cp_parser_simulate_error (parser
);
14880 /* Now, pretend that we want to see if the construct was
14881 successfully parsed. */
14882 cp_parser_parse_definitely (parser
);
14885 /* Stop parsing tentatively. If a parse error has occurred, restore the
14886 token stream. Otherwise, commit to the tokens we have consumed.
14887 Returns true if no error occurred; false otherwise. */
14890 cp_parser_parse_definitely (cp_parser
* parser
)
14892 bool error_occurred
;
14893 cp_parser_context
*context
;
14895 /* Remember whether or not an error occurred, since we are about to
14896 destroy that information. */
14897 error_occurred
= cp_parser_error_occurred (parser
);
14898 /* Remove the topmost context from the stack. */
14899 context
= parser
->context
;
14900 parser
->context
= context
->next
;
14901 /* If no parse errors occurred, commit to the tentative parse. */
14902 if (!error_occurred
)
14904 /* Commit to the tokens read tentatively, unless that was
14906 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
14907 cp_lexer_commit_tokens (parser
->lexer
);
14909 pop_to_parent_deferring_access_checks ();
14911 /* Otherwise, if errors occurred, roll back our state so that things
14912 are just as they were before we began the tentative parse. */
14915 cp_lexer_rollback_tokens (parser
->lexer
);
14916 pop_deferring_access_checks ();
14918 /* Add the context to the front of the free list. */
14919 context
->next
= cp_parser_context_free_list
;
14920 cp_parser_context_free_list
= context
;
14922 return !error_occurred
;
14925 /* Returns true if we are parsing tentatively -- but have decided that
14926 we will stick with this tentative parse, even if errors occur. */
14929 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
14931 return (cp_parser_parsing_tentatively (parser
)
14932 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
14935 /* Returns nonzero iff an error has occurred during the most recent
14936 tentative parse. */
14939 cp_parser_error_occurred (cp_parser
* parser
)
14941 return (cp_parser_parsing_tentatively (parser
)
14942 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
14945 /* Returns nonzero if GNU extensions are allowed. */
14948 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
14950 return parser
->allow_gnu_extensions_p
;
14957 static GTY (()) cp_parser
*the_parser
;
14959 /* External interface. */
14961 /* Parse one entire translation unit. */
14964 c_parse_file (void)
14966 bool error_occurred
;
14968 the_parser
= cp_parser_new ();
14969 push_deferring_access_checks (flag_access_control
14970 ? dk_no_deferred
: dk_no_check
);
14971 error_occurred
= cp_parser_translation_unit (the_parser
);
14975 /* Clean up after parsing the entire translation unit. */
14978 free_parser_stacks (void)
14980 /* Nothing to do. */
14983 /* This variable must be provided by every front end. */
14987 #include "gt-cp-parser.h"