2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
69 typedef struct cp_token
GTY (())
71 /* The kind of token. */
72 ENUM_BITFIELD (cpp_ttype
) type
: 8;
73 /* If this token is a keyword, this value indicates which keyword.
74 Otherwise, this value is RID_MAX. */
75 ENUM_BITFIELD (rid
) keyword
: 8;
78 /* The value associated with this token, if any. */
80 /* The location at which this token was found. */
84 /* The number of tokens in a single token block.
85 Computed so that cp_token_block fits in a 512B allocation unit. */
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
89 /* A group of tokens. These groups are chained together to store
90 large numbers of tokens. (For example, a token block is created
91 when the body of an inline member function is first encountered;
92 the tokens are processed later after the class definition is
95 This somewhat ungainly data structure (as opposed to, say, a
96 variable-length array), is used due to constraints imposed by the
97 current garbage-collection methodology. If it is made more
98 flexible, we could perhaps simplify the data structures involved. */
100 typedef struct cp_token_block
GTY (())
103 cp_token tokens
[CP_TOKEN_BLOCK_NUM_TOKENS
];
104 /* The number of tokens in this block. */
106 /* The next token block in the chain. */
107 struct cp_token_block
*next
;
108 /* The previous block in the chain. */
109 struct cp_token_block
*prev
;
112 typedef struct cp_token_cache
GTY (())
114 /* The first block in the cache. NULL if there are no tokens in the
116 cp_token_block
*first
;
117 /* The last block in the cache. NULL If there are no tokens in the
119 cp_token_block
*last
;
124 static cp_token_cache
*cp_token_cache_new
126 static void cp_token_cache_push_token
127 (cp_token_cache
*, cp_token
*);
129 /* Create a new cp_token_cache. */
131 static cp_token_cache
*
132 cp_token_cache_new (void)
134 return ggc_alloc_cleared (sizeof (cp_token_cache
));
137 /* Add *TOKEN to *CACHE. */
140 cp_token_cache_push_token (cp_token_cache
*cache
,
143 cp_token_block
*b
= cache
->last
;
145 /* See if we need to allocate a new token block. */
146 if (!b
|| b
->num_tokens
== CP_TOKEN_BLOCK_NUM_TOKENS
)
148 b
= ggc_alloc_cleared (sizeof (cp_token_block
));
149 b
->prev
= cache
->last
;
152 cache
->last
->next
= b
;
156 cache
->first
= cache
->last
= b
;
158 /* Add this token to the current token block. */
159 b
->tokens
[b
->num_tokens
++] = *token
;
162 /* The cp_lexer structure represents the C++ lexer. It is responsible
163 for managing the token stream from the preprocessor and supplying
166 typedef struct cp_lexer
GTY (())
168 /* The memory allocated for the buffer. Never NULL. */
169 cp_token
* GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer
;
170 /* A pointer just past the end of the memory allocated for the buffer. */
171 cp_token
* GTY ((skip (""))) buffer_end
;
172 /* The first valid token in the buffer, or NULL if none. */
173 cp_token
* GTY ((skip (""))) first_token
;
174 /* The next available token. If NEXT_TOKEN is NULL, then there are
175 no more available tokens. */
176 cp_token
* GTY ((skip (""))) next_token
;
177 /* A pointer just past the last available token. If FIRST_TOKEN is
178 NULL, however, there are no available tokens, and then this
179 location is simply the place in which the next token read will be
180 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181 When the LAST_TOKEN == BUFFER, then the last token is at the
182 highest memory address in the BUFFER. */
183 cp_token
* GTY ((skip (""))) last_token
;
185 /* A stack indicating positions at which cp_lexer_save_tokens was
186 called. The top entry is the most recent position at which we
187 began saving tokens. The entries are differences in token
188 position between FIRST_TOKEN and the first saved token.
190 If the stack is non-empty, we are saving tokens. When a token is
191 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192 pointer will not. The token stream will be preserved so that it
193 can be reexamined later.
195 If the stack is empty, then we are not saving tokens. Whenever a
196 token is consumed, the FIRST_TOKEN pointer will be moved, and the
197 consumed token will be gone forever. */
198 varray_type saved_tokens
;
200 /* The STRING_CST tokens encountered while processing the current
202 varray_type string_tokens
;
204 /* True if we should obtain more tokens from the preprocessor; false
205 if we are processing a saved token cache. */
208 /* True if we should output debugging information. */
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer
*next
;
217 static cp_lexer
*cp_lexer_new_main
219 static cp_lexer
*cp_lexer_new_from_tokens
220 (struct cp_token_cache
*);
221 static int cp_lexer_saving_tokens
223 static cp_token
*cp_lexer_next_token
224 (cp_lexer
*, cp_token
*);
225 static cp_token
*cp_lexer_prev_token
226 (cp_lexer
*, cp_token
*);
227 static ptrdiff_t cp_lexer_token_difference
228 (cp_lexer
*, cp_token
*, cp_token
*);
229 static cp_token
*cp_lexer_read_token
231 static void cp_lexer_maybe_grow_buffer
233 static void cp_lexer_get_preprocessor_token
234 (cp_lexer
*, cp_token
*);
235 static cp_token
*cp_lexer_peek_token
237 static cp_token
*cp_lexer_peek_nth_token
238 (cp_lexer
*, size_t);
239 static inline bool cp_lexer_next_token_is
240 (cp_lexer
*, enum cpp_ttype
);
241 static bool cp_lexer_next_token_is_not
242 (cp_lexer
*, enum cpp_ttype
);
243 static bool cp_lexer_next_token_is_keyword
244 (cp_lexer
*, enum rid
);
245 static cp_token
*cp_lexer_consume_token
247 static void cp_lexer_purge_token
249 static void cp_lexer_purge_tokens_after
250 (cp_lexer
*, cp_token
*);
251 static void cp_lexer_save_tokens
253 static void cp_lexer_commit_tokens
255 static void cp_lexer_rollback_tokens
257 static inline void cp_lexer_set_source_position_from_token
258 (cp_lexer
*, const cp_token
*);
259 static void cp_lexer_print_token
260 (FILE *, cp_token
*);
261 static inline bool cp_lexer_debugging_p
263 static void cp_lexer_start_debugging
264 (cp_lexer
*) ATTRIBUTE_UNUSED
;
265 static void cp_lexer_stop_debugging
266 (cp_lexer
*) ATTRIBUTE_UNUSED
;
268 /* Manifest constants. */
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
273 /* A token type for keywords, as opposed to ordinary identifiers. */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
276 /* A token type for template-ids. If a template-id is processed while
277 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278 the value of the CPP_TEMPLATE_ID is whatever was returned by
279 cp_parser_template_id. */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
282 /* A token type for nested-name-specifiers. If a
283 nested-name-specifier is processed while parsing tentatively, it is
284 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286 cp_parser_nested_name_specifier_opt. */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
289 /* A token type for tokens that are not tokens at all; these are used
290 to mark the end of a token block. */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
295 /* The stream to which debugging output should be written. */
296 static FILE *cp_lexer_debug_stream
;
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
302 cp_lexer_new_main (void)
305 cp_token first_token
;
307 /* It's possible that lexing the first token will load a PCH file,
308 which is a GC collection point. So we have to grab the first
309 token before allocating any memory. */
310 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
311 c_common_no_more_pch ();
313 /* Allocate the memory. */
314 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
316 /* Create the circular buffer. */
317 lexer
->buffer
= ggc_calloc (CP_TOKEN_BUFFER_SIZE
, sizeof (cp_token
));
318 lexer
->buffer_end
= lexer
->buffer
+ CP_TOKEN_BUFFER_SIZE
;
320 /* There is one token in the buffer. */
321 lexer
->last_token
= lexer
->buffer
+ 1;
322 lexer
->first_token
= lexer
->buffer
;
323 lexer
->next_token
= lexer
->buffer
;
324 memcpy (lexer
->buffer
, &first_token
, sizeof (cp_token
));
326 /* This lexer obtains more tokens by calling c_lex. */
327 lexer
->main_lexer_p
= true;
329 /* Create the SAVED_TOKENS stack. */
330 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
332 /* Create the STRINGS array. */
333 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
335 /* Assume we are not debugging. */
336 lexer
->debugging_p
= false;
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342 When these tokens are exhausted, no new tokens will be read. */
345 cp_lexer_new_from_tokens (cp_token_cache
*tokens
)
349 cp_token_block
*block
;
350 ptrdiff_t num_tokens
;
352 /* Allocate the memory. */
353 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
355 /* Create a new buffer, appropriately sized. */
357 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
358 num_tokens
+= block
->num_tokens
;
359 lexer
->buffer
= ggc_alloc (num_tokens
* sizeof (cp_token
));
360 lexer
->buffer_end
= lexer
->buffer
+ num_tokens
;
362 /* Install the tokens. */
363 token
= lexer
->buffer
;
364 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
366 memcpy (token
, block
->tokens
, block
->num_tokens
* sizeof (cp_token
));
367 token
+= block
->num_tokens
;
370 /* The FIRST_TOKEN is the beginning of the buffer. */
371 lexer
->first_token
= lexer
->buffer
;
372 /* The next available token is also at the beginning of the buffer. */
373 lexer
->next_token
= lexer
->buffer
;
374 /* The buffer is full. */
375 lexer
->last_token
= lexer
->first_token
;
377 /* This lexer doesn't obtain more tokens. */
378 lexer
->main_lexer_p
= false;
380 /* Create the SAVED_TOKENS stack. */
381 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
383 /* Create the STRINGS array. */
384 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
386 /* Assume we are not debugging. */
387 lexer
->debugging_p
= false;
392 /* Returns nonzero if debugging information should be output. */
395 cp_lexer_debugging_p (cp_lexer
*lexer
)
397 return lexer
->debugging_p
;
400 /* Set the current source position from the information stored in
404 cp_lexer_set_source_position_from_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
405 const cp_token
*token
)
407 /* Ideally, the source position information would not be a global
408 variable, but it is. */
410 /* Update the line number. */
411 if (token
->type
!= CPP_EOF
)
412 input_location
= token
->location
;
415 /* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
418 static inline cp_token
*
419 cp_lexer_next_token (cp_lexer
* lexer
, cp_token
* token
)
422 if (token
== lexer
->buffer_end
)
423 token
= lexer
->buffer
;
427 /* TOKEN points into the circular token buffer. Return a pointer to
428 the previous token in the buffer. */
430 static inline cp_token
*
431 cp_lexer_prev_token (cp_lexer
* lexer
, cp_token
* token
)
433 if (token
== lexer
->buffer
)
434 token
= lexer
->buffer_end
;
438 /* nonzero if we are presently saving tokens. */
441 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
443 return VARRAY_ACTIVE_SIZE (lexer
->saved_tokens
) != 0;
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
450 cp_lexer_advance_token (cp_lexer
*lexer
, cp_token
*token
, ptrdiff_t n
)
453 if (token
>= lexer
->buffer_end
)
454 token
= lexer
->buffer
+ (token
- lexer
->buffer_end
);
458 /* Returns the number of times that START would have to be incremented
459 to reach FINISH. If START and FINISH are the same, returns zero. */
462 cp_lexer_token_difference (cp_lexer
* lexer
, cp_token
* start
, cp_token
* finish
)
465 return finish
- start
;
467 return ((lexer
->buffer_end
- lexer
->buffer
)
471 /* Obtain another token from the C preprocessor and add it to the
472 token buffer. Returns the newly read token. */
475 cp_lexer_read_token (cp_lexer
* lexer
)
479 /* Make sure there is room in the buffer. */
480 cp_lexer_maybe_grow_buffer (lexer
);
482 /* If there weren't any tokens, then this one will be the first. */
483 if (!lexer
->first_token
)
484 lexer
->first_token
= lexer
->last_token
;
485 /* Similarly, if there were no available tokens, there is one now. */
486 if (!lexer
->next_token
)
487 lexer
->next_token
= lexer
->last_token
;
489 /* Figure out where we're going to store the new token. */
490 token
= lexer
->last_token
;
492 /* Get a new token from the preprocessor. */
493 cp_lexer_get_preprocessor_token (lexer
, token
);
495 /* Increment LAST_TOKEN. */
496 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
498 /* Strings should have type `const char []'. Right now, we will
499 have an ARRAY_TYPE that is constant rather than an array of
501 FIXME: Make fix_string_type get this right in the first place. */
502 if ((token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
)
503 && flag_const_strings
)
507 /* Get the current type. It will be an ARRAY_TYPE. */
508 type
= TREE_TYPE (token
->value
);
509 /* Use build_cplus_array_type to rebuild the array, thereby
510 getting the right type. */
511 type
= build_cplus_array_type (TREE_TYPE (type
), TYPE_DOMAIN (type
));
512 /* Reset the type of the token. */
513 TREE_TYPE (token
->value
) = type
;
519 /* If the circular buffer is full, make it bigger. */
522 cp_lexer_maybe_grow_buffer (cp_lexer
* lexer
)
524 /* If the buffer is full, enlarge it. */
525 if (lexer
->last_token
== lexer
->first_token
)
527 cp_token
*new_buffer
;
528 cp_token
*old_buffer
;
529 cp_token
*new_first_token
;
530 ptrdiff_t buffer_length
;
531 size_t num_tokens_to_copy
;
533 /* Remember the current buffer pointer. It will become invalid,
534 but we will need to do pointer arithmetic involving this
536 old_buffer
= lexer
->buffer
;
537 /* Compute the current buffer size. */
538 buffer_length
= lexer
->buffer_end
- lexer
->buffer
;
539 /* Allocate a buffer twice as big. */
540 new_buffer
= ggc_realloc (lexer
->buffer
,
541 2 * buffer_length
* sizeof (cp_token
));
543 /* Because the buffer is circular, logically consecutive tokens
544 are not necessarily placed consecutively in memory.
545 Therefore, we must keep move the tokens that were before
546 FIRST_TOKEN to the second half of the newly allocated
548 num_tokens_to_copy
= (lexer
->first_token
- old_buffer
);
549 memcpy (new_buffer
+ buffer_length
,
551 num_tokens_to_copy
* sizeof (cp_token
));
552 /* Clear the rest of the buffer. We never look at this storage,
553 but the garbage collector may. */
554 memset (new_buffer
+ buffer_length
+ num_tokens_to_copy
, 0,
555 (buffer_length
- num_tokens_to_copy
) * sizeof (cp_token
));
557 /* Now recompute all of the buffer pointers. */
559 = new_buffer
+ (lexer
->first_token
- old_buffer
);
560 if (lexer
->next_token
!= NULL
)
562 ptrdiff_t next_token_delta
;
564 if (lexer
->next_token
> lexer
->first_token
)
565 next_token_delta
= lexer
->next_token
- lexer
->first_token
;
568 buffer_length
- (lexer
->first_token
- lexer
->next_token
);
569 lexer
->next_token
= new_first_token
+ next_token_delta
;
571 lexer
->last_token
= new_first_token
+ buffer_length
;
572 lexer
->buffer
= new_buffer
;
573 lexer
->buffer_end
= new_buffer
+ buffer_length
* 2;
574 lexer
->first_token
= new_first_token
;
578 /* Store the next token from the preprocessor in *TOKEN. */
581 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
586 /* If this not the main lexer, return a terminating CPP_EOF token. */
587 if (lexer
!= NULL
&& !lexer
->main_lexer_p
)
589 token
->type
= CPP_EOF
;
590 token
->location
.line
= 0;
591 token
->location
.file
= NULL
;
592 token
->value
= NULL_TREE
;
593 token
->keyword
= RID_MAX
;
599 /* Keep going until we get a token we like. */
602 /* Get a new token from the preprocessor. */
603 token
->type
= c_lex_with_flags (&token
->value
, &token
->flags
);
604 /* Issue messages about tokens we cannot process. */
610 error ("invalid token");
614 /* This is a good token, so we exit the loop. */
619 /* Now we've got our token. */
620 token
->location
= input_location
;
622 /* Check to see if this token is a keyword. */
623 if (token
->type
== CPP_NAME
624 && C_IS_RESERVED_WORD (token
->value
))
626 /* Mark this token as a keyword. */
627 token
->type
= CPP_KEYWORD
;
628 /* Record which keyword. */
629 token
->keyword
= C_RID_CODE (token
->value
);
630 /* Update the value. Some keywords are mapped to particular
631 entities, rather than simply having the value of the
632 corresponding IDENTIFIER_NODE. For example, `__const' is
633 mapped to `const'. */
634 token
->value
= ridpointers
[token
->keyword
];
637 token
->keyword
= RID_MAX
;
640 /* Return a pointer to the next token in the token stream, but do not
644 cp_lexer_peek_token (cp_lexer
* lexer
)
648 /* If there are no tokens, read one now. */
649 if (!lexer
->next_token
)
650 cp_lexer_read_token (lexer
);
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer
))
655 fprintf (cp_lexer_debug_stream
, "cp_lexer: peeking at token: ");
656 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
657 fprintf (cp_lexer_debug_stream
, "\n");
660 token
= lexer
->next_token
;
661 cp_lexer_set_source_position_from_token (lexer
, token
);
665 /* Return true if the next token has the indicated TYPE. */
668 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
672 /* Peek at the next token. */
673 token
= cp_lexer_peek_token (lexer
);
674 /* Check to see if it has the indicated TYPE. */
675 return token
->type
== type
;
678 /* Return true if the next token does not have the indicated TYPE. */
681 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
683 return !cp_lexer_next_token_is (lexer
, type
);
686 /* Return true if the next token is the indicated KEYWORD. */
689 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
693 /* Peek at the next token. */
694 token
= cp_lexer_peek_token (lexer
);
695 /* Check to see if it is the indicated keyword. */
696 return token
->keyword
== keyword
;
699 /* Return a pointer to the Nth token in the token stream. If N is 1,
700 then this is precisely equivalent to cp_lexer_peek_token. */
703 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
707 /* N is 1-based, not zero-based. */
708 my_friendly_assert (n
> 0, 20000224);
710 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
711 token
= lexer
->next_token
;
712 /* If there are no tokens in the buffer, get one now. */
715 cp_lexer_read_token (lexer
);
716 token
= lexer
->next_token
;
719 /* Now, read tokens until we have enough. */
722 /* Advance to the next token. */
723 token
= cp_lexer_next_token (lexer
, token
);
724 /* If that's all the tokens we have, read a new one. */
725 if (token
== lexer
->last_token
)
726 token
= cp_lexer_read_token (lexer
);
732 /* Consume the next token. The pointer returned is valid only until
733 another token is read. Callers should preserve copy the token
734 explicitly if they will need its value for a longer period of
738 cp_lexer_consume_token (cp_lexer
* lexer
)
742 /* If there are no tokens, read one now. */
743 if (!lexer
->next_token
)
744 cp_lexer_read_token (lexer
);
746 /* Remember the token we'll be returning. */
747 token
= lexer
->next_token
;
749 /* Increment NEXT_TOKEN. */
750 lexer
->next_token
= cp_lexer_next_token (lexer
,
752 /* Check to see if we're all out of tokens. */
753 if (lexer
->next_token
== lexer
->last_token
)
754 lexer
->next_token
= NULL
;
756 /* If we're not saving tokens, then move FIRST_TOKEN too. */
757 if (!cp_lexer_saving_tokens (lexer
))
759 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
760 if (!lexer
->next_token
)
761 lexer
->first_token
= NULL
;
763 lexer
->first_token
= lexer
->next_token
;
766 /* Provide debugging output. */
767 if (cp_lexer_debugging_p (lexer
))
769 fprintf (cp_lexer_debug_stream
, "cp_lexer: consuming token: ");
770 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
771 fprintf (cp_lexer_debug_stream
, "\n");
777 /* Permanently remove the next token from the token stream. There
778 must be a valid next token already; this token never reads
779 additional tokens from the preprocessor. */
782 cp_lexer_purge_token (cp_lexer
*lexer
)
785 cp_token
*next_token
;
787 token
= lexer
->next_token
;
790 next_token
= cp_lexer_next_token (lexer
, token
);
791 if (next_token
== lexer
->last_token
)
793 *token
= *next_token
;
797 lexer
->last_token
= token
;
798 /* The token purged may have been the only token remaining; if so,
800 if (lexer
->next_token
== token
)
801 lexer
->next_token
= NULL
;
804 /* Permanently remove all tokens after TOKEN, up to, but not
805 including, the token that will be returned next by
806 cp_lexer_peek_token. */
809 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*token
)
815 if (lexer
->next_token
)
817 /* Copy the tokens that have not yet been read to the location
818 immediately following TOKEN. */
819 t1
= cp_lexer_next_token (lexer
, token
);
820 t2
= peek
= cp_lexer_peek_token (lexer
);
821 /* Move tokens into the vacant area between TOKEN and PEEK. */
822 while (t2
!= lexer
->last_token
)
825 t1
= cp_lexer_next_token (lexer
, t1
);
826 t2
= cp_lexer_next_token (lexer
, t2
);
828 /* Now, the next available token is right after TOKEN. */
829 lexer
->next_token
= cp_lexer_next_token (lexer
, token
);
830 /* And the last token is wherever we ended up. */
831 lexer
->last_token
= t1
;
835 /* There are no tokens in the buffer, so there is nothing to
836 copy. The last token in the buffer is TOKEN itself. */
837 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
841 /* Begin saving tokens. All tokens consumed after this point will be
845 cp_lexer_save_tokens (cp_lexer
* lexer
)
847 /* Provide debugging output. */
848 if (cp_lexer_debugging_p (lexer
))
849 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
851 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
852 restore the tokens if required. */
853 if (!lexer
->next_token
)
854 cp_lexer_read_token (lexer
);
856 VARRAY_PUSH_INT (lexer
->saved_tokens
,
857 cp_lexer_token_difference (lexer
,
862 /* Commit to the portion of the token stream most recently saved. */
865 cp_lexer_commit_tokens (cp_lexer
* lexer
)
867 /* Provide debugging output. */
868 if (cp_lexer_debugging_p (lexer
))
869 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
871 VARRAY_POP (lexer
->saved_tokens
);
874 /* Return all tokens saved since the last call to cp_lexer_save_tokens
875 to the token stream. Stop saving tokens. */
878 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer
))
884 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
886 /* Find the token that was the NEXT_TOKEN when we started saving
888 delta
= VARRAY_TOP_INT(lexer
->saved_tokens
);
889 /* Make it the next token again now. */
890 lexer
->next_token
= cp_lexer_advance_token (lexer
,
893 /* It might be the case that there were no tokens when we started
894 saving tokens, but that there are some tokens now. */
895 if (!lexer
->next_token
&& lexer
->first_token
)
896 lexer
->next_token
= lexer
->first_token
;
898 /* Stop saving tokens. */
899 VARRAY_POP (lexer
->saved_tokens
);
902 /* Print a representation of the TOKEN on the STREAM. */
905 cp_lexer_print_token (FILE * stream
, cp_token
* token
)
907 const char *token_type
= NULL
;
909 /* Figure out what kind of token this is. */
917 token_type
= "COMMA";
921 token_type
= "OPEN_PAREN";
924 case CPP_CLOSE_PAREN
:
925 token_type
= "CLOSE_PAREN";
929 token_type
= "OPEN_BRACE";
932 case CPP_CLOSE_BRACE
:
933 token_type
= "CLOSE_BRACE";
937 token_type
= "SEMICOLON";
949 token_type
= "keyword";
952 /* This is not a token that we know how to handle yet. */
957 /* If we have a name for the token, print it out. Otherwise, we
958 simply give the numeric code. */
960 fprintf (stream
, "%s", token_type
);
962 fprintf (stream
, "%d", token
->type
);
963 /* And, for an identifier, print the identifier name. */
964 if (token
->type
== CPP_NAME
965 /* Some keywords have a value that is not an IDENTIFIER_NODE.
966 For example, `struct' is mapped to an INTEGER_CST. */
967 || (token
->type
== CPP_KEYWORD
968 && TREE_CODE (token
->value
) == IDENTIFIER_NODE
))
969 fprintf (stream
, " %s", IDENTIFIER_POINTER (token
->value
));
972 /* Start emitting debugging information. */
975 cp_lexer_start_debugging (cp_lexer
* lexer
)
977 ++lexer
->debugging_p
;
980 /* Stop emitting debugging information. */
983 cp_lexer_stop_debugging (cp_lexer
* lexer
)
985 --lexer
->debugging_p
;
994 A cp_parser parses the token stream as specified by the C++
995 grammar. Its job is purely parsing, not semantic analysis. For
996 example, the parser breaks the token stream into declarators,
997 expressions, statements, and other similar syntactic constructs.
998 It does not check that the types of the expressions on either side
999 of an assignment-statement are compatible, or that a function is
1000 not declared with a parameter of type `void'.
1002 The parser invokes routines elsewhere in the compiler to perform
1003 semantic analysis and to build up the abstract syntax tree for the
1006 The parser (and the template instantiation code, which is, in a
1007 way, a close relative of parsing) are the only parts of the
1008 compiler that should be calling push_scope and pop_scope, or
1009 related functions. The parser (and template instantiation code)
1010 keeps track of what scope is presently active; everything else
1011 should simply honor that. (The code that generates static
1012 initializers may also need to set the scope, in order to check
1013 access control correctly when emitting the initializers.)
1018 The parser is of the standard recursive-descent variety. Upcoming
1019 tokens in the token stream are examined in order to determine which
1020 production to use when parsing a non-terminal. Some C++ constructs
1021 require arbitrary look ahead to disambiguate. For example, it is
1022 impossible, in the general case, to tell whether a statement is an
1023 expression or declaration without scanning the entire statement.
1024 Therefore, the parser is capable of "parsing tentatively." When the
1025 parser is not sure what construct comes next, it enters this mode.
1026 Then, while we attempt to parse the construct, the parser queues up
1027 error messages, rather than issuing them immediately, and saves the
1028 tokens it consumes. If the construct is parsed successfully, the
1029 parser "commits", i.e., it issues any queued error messages and
1030 the tokens that were being preserved are permanently discarded.
1031 If, however, the construct is not parsed successfully, the parser
1032 rolls back its state completely so that it can resume parsing using
1033 a different alternative.
1038 The performance of the parser could probably be improved
1039 substantially. Some possible improvements include:
1041 - The expression parser recurses through the various levels of
1042 precedence as specified in the grammar, rather than using an
1043 operator-precedence technique. Therefore, parsing a simple
1044 identifier requires multiple recursive calls.
1046 - We could often eliminate the need to parse tentatively by
1047 looking ahead a little bit. In some places, this approach
1048 might not entirely eliminate the need to parse tentatively, but
1049 it might still speed up the average case. */
1051 /* Flags that are passed to some parsing functions. These values can
1052 be bitwise-ored together. */
1054 typedef enum cp_parser_flags
1057 CP_PARSER_FLAGS_NONE
= 0x0,
1058 /* The construct is optional. If it is not present, then no error
1059 should be issued. */
1060 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1061 /* When parsing a type-specifier, do not allow user-defined types. */
1062 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1065 /* The different kinds of declarators we want to parse. */
1067 typedef enum cp_parser_declarator_kind
1069 /* We want an abstract declartor. */
1070 CP_PARSER_DECLARATOR_ABSTRACT
,
1071 /* We want a named declarator. */
1072 CP_PARSER_DECLARATOR_NAMED
,
1073 /* We don't mind, but the name must be an unqualified-id. */
1074 CP_PARSER_DECLARATOR_EITHER
1075 } cp_parser_declarator_kind
;
1077 /* A mapping from a token type to a corresponding tree node type. */
1079 typedef struct cp_parser_token_tree_map_node
1081 /* The token type. */
1082 ENUM_BITFIELD (cpp_ttype
) token_type
: 8;
1083 /* The corresponding tree code. */
1084 ENUM_BITFIELD (tree_code
) tree_type
: 8;
1085 } cp_parser_token_tree_map_node
;
1087 /* A complete map consists of several ordinary entries, followed by a
1088 terminator. The terminating entry has a token_type of CPP_EOF. */
1090 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map
[];
1092 /* The status of a tentative parse. */
1094 typedef enum cp_parser_status_kind
1096 /* No errors have occurred. */
1097 CP_PARSER_STATUS_KIND_NO_ERROR
,
1098 /* An error has occurred. */
1099 CP_PARSER_STATUS_KIND_ERROR
,
1100 /* We are committed to this tentative parse, whether or not an error
1102 CP_PARSER_STATUS_KIND_COMMITTED
1103 } cp_parser_status_kind
;
1105 /* Context that is saved and restored when parsing tentatively. */
1107 typedef struct cp_parser_context
GTY (())
1109 /* If this is a tentative parsing context, the status of the
1111 enum cp_parser_status_kind status
;
1112 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1113 that are looked up in this context must be looked up both in the
1114 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1115 the context of the containing expression. */
1117 /* The next parsing context in the stack. */
1118 struct cp_parser_context
*next
;
1119 } cp_parser_context
;
1123 /* Constructors and destructors. */
1125 static cp_parser_context
*cp_parser_context_new
1126 (cp_parser_context
*);
1128 /* Class variables. */
1130 static GTY((deletable (""))) cp_parser_context
* cp_parser_context_free_list
;
1132 /* Constructors and destructors. */
1134 /* Construct a new context. The context below this one on the stack
1135 is given by NEXT. */
1137 static cp_parser_context
*
1138 cp_parser_context_new (cp_parser_context
* next
)
1140 cp_parser_context
*context
;
1142 /* Allocate the storage. */
1143 if (cp_parser_context_free_list
!= NULL
)
1145 /* Pull the first entry from the free list. */
1146 context
= cp_parser_context_free_list
;
1147 cp_parser_context_free_list
= context
->next
;
1148 memset (context
, 0, sizeof (*context
));
1151 context
= ggc_alloc_cleared (sizeof (cp_parser_context
));
1152 /* No errors have occurred yet in this context. */
1153 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1154 /* If this is not the bottomost context, copy information that we
1155 need from the previous context. */
1158 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159 expression, then we are parsing one in this context, too. */
1160 context
->object_type
= next
->object_type
;
1161 /* Thread the stack. */
1162 context
->next
= next
;
1168 /* The cp_parser structure represents the C++ parser. */
1170 typedef struct cp_parser
GTY(())
1172 /* The lexer from which we are obtaining tokens. */
1175 /* The scope in which names should be looked up. If NULL_TREE, then
1176 we look up names in the scope that is currently open in the
1177 source program. If non-NULL, this is either a TYPE or
1178 NAMESPACE_DECL for the scope in which we should look.
1180 This value is not cleared automatically after a name is looked
1181 up, so we must be careful to clear it before starting a new look
1182 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1183 will look up `Z' in the scope of `X', rather than the current
1184 scope.) Unfortunately, it is difficult to tell when name lookup
1185 is complete, because we sometimes peek at a token, look it up,
1186 and then decide not to consume it. */
1189 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190 last lookup took place. OBJECT_SCOPE is used if an expression
1191 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192 respectively. QUALIFYING_SCOPE is used for an expression of the
1193 form "X::Y"; it refers to X. */
1195 tree qualifying_scope
;
1197 /* A stack of parsing contexts. All but the bottom entry on the
1198 stack will be tentative contexts.
1200 We parse tentatively in order to determine which construct is in
1201 use in some situations. For example, in order to determine
1202 whether a statement is an expression-statement or a
1203 declaration-statement we parse it tentatively as a
1204 declaration-statement. If that fails, we then reparse the same
1205 token stream as an expression-statement. */
1206 cp_parser_context
*context
;
1208 /* True if we are parsing GNU C++. If this flag is not set, then
1209 GNU extensions are not recognized. */
1210 bool allow_gnu_extensions_p
;
1212 /* TRUE if the `>' token should be interpreted as the greater-than
1213 operator. FALSE if it is the end of a template-id or
1214 template-parameter-list. */
1215 bool greater_than_is_operator_p
;
1217 /* TRUE if default arguments are allowed within a parameter list
1218 that starts at this point. FALSE if only a gnu extension makes
1219 them permissible. */
1220 bool default_arg_ok_p
;
1222 /* TRUE if we are parsing an integral constant-expression. See
1223 [expr.const] for a precise definition. */
1224 bool integral_constant_expression_p
;
1226 /* TRUE if we are parsing an integral constant-expression -- but a
1227 non-constant expression should be permitted as well. This flag
1228 is used when parsing an array bound so that GNU variable-length
1229 arrays are tolerated. */
1230 bool allow_non_integral_constant_expression_p
;
1232 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233 been seen that makes the expression non-constant. */
1234 bool non_integral_constant_expression_p
;
1236 /* TRUE if we are parsing the argument to "__offsetof__". */
1239 /* TRUE if local variable names and `this' are forbidden in the
1241 bool local_variables_forbidden_p
;
1243 /* TRUE if the declaration we are parsing is part of a
1244 linkage-specification of the form `extern string-literal
1246 bool in_unbraced_linkage_specification_p
;
1248 /* TRUE if we are presently parsing a declarator, after the
1249 direct-declarator. */
1250 bool in_declarator_p
;
1252 /* TRUE if we are presently parsing a template-argument-list. */
1253 bool in_template_argument_list_p
;
1255 /* TRUE if we are presently parsing the body of an
1256 iteration-statement. */
1257 bool in_iteration_statement_p
;
1259 /* TRUE if we are presently parsing the body of a switch
1261 bool in_switch_statement_p
;
1263 /* TRUE if we are parsing a type-id in an expression context. In
1264 such a situation, both "type (expr)" and "type (type)" are valid
1266 bool in_type_id_in_expr_p
;
1268 /* If non-NULL, then we are parsing a construct where new type
1269 definitions are not permitted. The string stored here will be
1270 issued as an error message if a type is defined. */
1271 const char *type_definition_forbidden_message
;
1273 /* A list of lists. The outer list is a stack, used for member
1274 functions of local classes. At each level there are two sub-list,
1275 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1276 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1277 TREE_VALUE's. The functions are chained in reverse declaration
1280 The TREE_PURPOSE sublist contains those functions with default
1281 arguments that need post processing, and the TREE_VALUE sublist
1282 contains those functions with definitions that need post
1285 These lists can only be processed once the outermost class being
1286 defined is complete. */
1287 tree unparsed_functions_queues
;
1289 /* The number of classes whose definitions are currently in
1291 unsigned num_classes_being_defined
;
1293 /* The number of template parameter lists that apply directly to the
1294 current declaration. */
1295 unsigned num_template_parameter_lists
;
1298 /* The type of a function that parses some kind of expression. */
1299 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1303 /* Constructors and destructors. */
1305 static cp_parser
*cp_parser_new
1308 /* Routines to parse various constructs.
1310 Those that return `tree' will return the error_mark_node (rather
1311 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1312 Sometimes, they will return an ordinary node if error-recovery was
1313 attempted, even though a parse error occurred. So, to check
1314 whether or not a parse error occurred, you should always use
1315 cp_parser_error_occurred. If the construct is optional (indicated
1316 either by an `_opt' in the name of the function that does the
1317 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1318 the construct is not present. */
1320 /* Lexical conventions [gram.lex] */
1322 static tree cp_parser_identifier
1325 /* Basic concepts [gram.basic] */
1327 static bool cp_parser_translation_unit
1330 /* Expressions [gram.expr] */
1332 static tree cp_parser_primary_expression
1333 (cp_parser
*, cp_id_kind
*, tree
*);
1334 static tree cp_parser_id_expression
1335 (cp_parser
*, bool, bool, bool *, bool);
1336 static tree cp_parser_unqualified_id
1337 (cp_parser
*, bool, bool, bool);
1338 static tree cp_parser_nested_name_specifier_opt
1339 (cp_parser
*, bool, bool, bool, bool);
1340 static tree cp_parser_nested_name_specifier
1341 (cp_parser
*, bool, bool, bool, bool);
1342 static tree cp_parser_class_or_namespace_name
1343 (cp_parser
*, bool, bool, bool, bool, bool);
1344 static tree cp_parser_postfix_expression
1345 (cp_parser
*, bool);
1346 static tree cp_parser_parenthesized_expression_list
1347 (cp_parser
*, bool, bool *);
1348 static void cp_parser_pseudo_destructor_name
1349 (cp_parser
*, tree
*, tree
*);
1350 static tree cp_parser_unary_expression
1351 (cp_parser
*, bool);
1352 static enum tree_code cp_parser_unary_operator
1354 static tree cp_parser_new_expression
1356 static tree cp_parser_new_placement
1358 static tree cp_parser_new_type_id
1360 static tree cp_parser_new_declarator_opt
1362 static tree cp_parser_direct_new_declarator
1364 static tree cp_parser_new_initializer
1366 static tree cp_parser_delete_expression
1368 static tree cp_parser_cast_expression
1369 (cp_parser
*, bool);
1370 static tree cp_parser_pm_expression
1372 static tree cp_parser_multiplicative_expression
1374 static tree cp_parser_additive_expression
1376 static tree cp_parser_shift_expression
1378 static tree cp_parser_relational_expression
1380 static tree cp_parser_equality_expression
1382 static tree cp_parser_and_expression
1384 static tree cp_parser_exclusive_or_expression
1386 static tree cp_parser_inclusive_or_expression
1388 static tree cp_parser_logical_and_expression
1390 static tree cp_parser_logical_or_expression
1392 static tree cp_parser_question_colon_clause
1393 (cp_parser
*, tree
);
1394 static tree cp_parser_assignment_expression
1396 static enum tree_code cp_parser_assignment_operator_opt
1398 static tree cp_parser_expression
1400 static tree cp_parser_constant_expression
1401 (cp_parser
*, bool, bool *);
1403 /* Statements [gram.stmt.stmt] */
1405 static void cp_parser_statement
1406 (cp_parser
*, bool);
1407 static tree cp_parser_labeled_statement
1408 (cp_parser
*, bool);
1409 static tree cp_parser_expression_statement
1410 (cp_parser
*, bool);
1411 static tree cp_parser_compound_statement
1412 (cp_parser
*, bool);
1413 static void cp_parser_statement_seq_opt
1414 (cp_parser
*, bool);
1415 static tree cp_parser_selection_statement
1417 static tree cp_parser_condition
1419 static tree cp_parser_iteration_statement
1421 static void cp_parser_for_init_statement
1423 static tree cp_parser_jump_statement
1425 static void cp_parser_declaration_statement
1428 static tree cp_parser_implicitly_scoped_statement
1430 static void cp_parser_already_scoped_statement
1433 /* Declarations [gram.dcl.dcl] */
1435 static void cp_parser_declaration_seq_opt
1437 static void cp_parser_declaration
1439 static void cp_parser_block_declaration
1440 (cp_parser
*, bool);
1441 static void cp_parser_simple_declaration
1442 (cp_parser
*, bool);
1443 static tree cp_parser_decl_specifier_seq
1444 (cp_parser
*, cp_parser_flags
, tree
*, int *);
1445 static tree cp_parser_storage_class_specifier_opt
1447 static tree cp_parser_function_specifier_opt
1449 static tree cp_parser_type_specifier
1450 (cp_parser
*, cp_parser_flags
, bool, bool, int *, bool *);
1451 static tree cp_parser_simple_type_specifier
1452 (cp_parser
*, cp_parser_flags
, bool);
1453 static tree cp_parser_type_name
1455 static tree cp_parser_elaborated_type_specifier
1456 (cp_parser
*, bool, bool);
1457 static tree cp_parser_enum_specifier
1459 static void cp_parser_enumerator_list
1460 (cp_parser
*, tree
);
1461 static void cp_parser_enumerator_definition
1462 (cp_parser
*, tree
);
1463 static tree cp_parser_namespace_name
1465 static void cp_parser_namespace_definition
1467 static void cp_parser_namespace_body
1469 static tree cp_parser_qualified_namespace_specifier
1471 static void cp_parser_namespace_alias_definition
1473 static void cp_parser_using_declaration
1475 static void cp_parser_using_directive
1477 static void cp_parser_asm_definition
1479 static void cp_parser_linkage_specification
1482 /* Declarators [gram.dcl.decl] */
1484 static tree cp_parser_init_declarator
1485 (cp_parser
*, tree
, tree
, bool, bool, int, bool *);
1486 static tree cp_parser_declarator
1487 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1488 static tree cp_parser_direct_declarator
1489 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1490 static enum tree_code cp_parser_ptr_operator
1491 (cp_parser
*, tree
*, tree
*);
1492 static tree cp_parser_cv_qualifier_seq_opt
1494 static tree cp_parser_cv_qualifier_opt
1496 static tree cp_parser_declarator_id
1498 static tree cp_parser_type_id
1500 static tree cp_parser_type_specifier_seq
1502 static tree cp_parser_parameter_declaration_clause
1504 static tree cp_parser_parameter_declaration_list
1506 static tree cp_parser_parameter_declaration
1507 (cp_parser
*, bool, bool *);
1508 static void cp_parser_function_body
1510 static tree cp_parser_initializer
1511 (cp_parser
*, bool *, bool *);
1512 static tree cp_parser_initializer_clause
1513 (cp_parser
*, bool *);
1514 static tree cp_parser_initializer_list
1515 (cp_parser
*, bool *);
1517 static bool cp_parser_ctor_initializer_opt_and_function_body
1520 /* Classes [gram.class] */
1522 static tree cp_parser_class_name
1523 (cp_parser
*, bool, bool, bool, bool, bool, bool);
1524 static tree cp_parser_class_specifier
1526 static tree cp_parser_class_head
1527 (cp_parser
*, bool *, tree
*);
1528 static enum tag_types cp_parser_class_key
1530 static void cp_parser_member_specification_opt
1532 static void cp_parser_member_declaration
1534 static tree cp_parser_pure_specifier
1536 static tree cp_parser_constant_initializer
1539 /* Derived classes [gram.class.derived] */
1541 static tree cp_parser_base_clause
1543 static tree cp_parser_base_specifier
1546 /* Special member functions [gram.special] */
1548 static tree cp_parser_conversion_function_id
1550 static tree cp_parser_conversion_type_id
1552 static tree cp_parser_conversion_declarator_opt
1554 static bool cp_parser_ctor_initializer_opt
1556 static void cp_parser_mem_initializer_list
1558 static tree cp_parser_mem_initializer
1560 static tree cp_parser_mem_initializer_id
1563 /* Overloading [gram.over] */
1565 static tree cp_parser_operator_function_id
1567 static tree cp_parser_operator
1570 /* Templates [gram.temp] */
1572 static void cp_parser_template_declaration
1573 (cp_parser
*, bool);
1574 static tree cp_parser_template_parameter_list
1576 static tree cp_parser_template_parameter
1578 static tree cp_parser_type_parameter
1580 static tree cp_parser_template_id
1581 (cp_parser
*, bool, bool, bool);
1582 static tree cp_parser_template_name
1583 (cp_parser
*, bool, bool, bool, bool *);
1584 static tree cp_parser_template_argument_list
1586 static tree cp_parser_template_argument
1588 static void cp_parser_explicit_instantiation
1590 static void cp_parser_explicit_specialization
1593 /* Exception handling [gram.exception] */
1595 static tree cp_parser_try_block
1597 static bool cp_parser_function_try_block
1599 static void cp_parser_handler_seq
1601 static void cp_parser_handler
1603 static tree cp_parser_exception_declaration
1605 static tree cp_parser_throw_expression
1607 static tree cp_parser_exception_specification_opt
1609 static tree cp_parser_type_id_list
1612 /* GNU Extensions */
1614 static tree cp_parser_asm_specification_opt
1616 static tree cp_parser_asm_operand_list
1618 static tree cp_parser_asm_clobber_list
1620 static tree cp_parser_attributes_opt
1622 static tree cp_parser_attribute_list
1624 static bool cp_parser_extension_opt
1625 (cp_parser
*, int *);
1626 static void cp_parser_label_declaration
1629 /* Utility Routines */
1631 static tree cp_parser_lookup_name
1632 (cp_parser
*, tree
, bool, bool, bool, bool);
1633 static tree cp_parser_lookup_name_simple
1634 (cp_parser
*, tree
);
1635 static tree cp_parser_maybe_treat_template_as_class
1637 static bool cp_parser_check_declarator_template_parameters
1638 (cp_parser
*, tree
);
1639 static bool cp_parser_check_template_parameters
1640 (cp_parser
*, unsigned);
1641 static tree cp_parser_simple_cast_expression
1643 static tree cp_parser_binary_expression
1644 (cp_parser
*, const cp_parser_token_tree_map
, cp_parser_expression_fn
);
1645 static tree cp_parser_global_scope_opt
1646 (cp_parser
*, bool);
1647 static bool cp_parser_constructor_declarator_p
1648 (cp_parser
*, bool);
1649 static tree cp_parser_function_definition_from_specifiers_and_declarator
1650 (cp_parser
*, tree
, tree
, tree
);
1651 static tree cp_parser_function_definition_after_declarator
1652 (cp_parser
*, bool);
1653 static void cp_parser_template_declaration_after_export
1654 (cp_parser
*, bool);
1655 static tree cp_parser_single_declaration
1656 (cp_parser
*, bool, bool *);
1657 static tree cp_parser_functional_cast
1658 (cp_parser
*, tree
);
1659 static tree cp_parser_save_member_function_body
1660 (cp_parser
*, tree
, tree
, tree
);
1661 static tree cp_parser_enclosed_template_argument_list
1663 static void cp_parser_save_default_args
1664 (cp_parser
*, tree
);
1665 static void cp_parser_late_parsing_for_member
1666 (cp_parser
*, tree
);
1667 static void cp_parser_late_parsing_default_args
1668 (cp_parser
*, tree
);
1669 static tree cp_parser_sizeof_operand
1670 (cp_parser
*, enum rid
);
1671 static bool cp_parser_declares_only_class_p
1673 static bool cp_parser_friend_p
1675 static bool cp_parser_typedef_p
1677 static cp_token
*cp_parser_require
1678 (cp_parser
*, enum cpp_ttype
, const char *);
1679 static cp_token
*cp_parser_require_keyword
1680 (cp_parser
*, enum rid
, const char *);
1681 static bool cp_parser_token_starts_function_definition_p
1683 static bool cp_parser_next_token_starts_class_definition_p
1685 static bool cp_parser_next_token_ends_template_argument_p
1687 static bool cp_parser_nth_token_starts_template_argument_list_p
1688 (cp_parser
*, size_t);
1689 static enum tag_types cp_parser_token_is_class_key
1691 static void cp_parser_check_class_key
1692 (enum tag_types
, tree type
);
1693 static void cp_parser_check_access_in_redeclaration
1695 static bool cp_parser_optional_template_keyword
1697 static void cp_parser_pre_parsed_nested_name_specifier
1699 static void cp_parser_cache_group
1700 (cp_parser
*, cp_token_cache
*, enum cpp_ttype
, unsigned);
1701 static void cp_parser_parse_tentatively
1703 static void cp_parser_commit_to_tentative_parse
1705 static void cp_parser_abort_tentative_parse
1707 static bool cp_parser_parse_definitely
1709 static inline bool cp_parser_parsing_tentatively
1711 static bool cp_parser_committed_to_tentative_parse
1713 static void cp_parser_error
1714 (cp_parser
*, const char *);
1715 static void cp_parser_name_lookup_error
1716 (cp_parser
*, tree
, tree
, const char *);
1717 static bool cp_parser_simulate_error
1719 static void cp_parser_check_type_definition
1721 static void cp_parser_check_for_definition_in_return_type
1723 static void cp_parser_check_for_invalid_template_id
1724 (cp_parser
*, tree
);
1725 static bool cp_parser_non_integral_constant_expression
1726 (cp_parser
*, const char *);
1727 static bool cp_parser_diagnose_invalid_type_name
1729 static int cp_parser_skip_to_closing_parenthesis
1730 (cp_parser
*, bool, bool, bool);
1731 static void cp_parser_skip_to_end_of_statement
1733 static void cp_parser_consume_semicolon_at_end_of_statement
1735 static void cp_parser_skip_to_end_of_block_or_statement
1737 static void cp_parser_skip_to_closing_brace
1739 static void cp_parser_skip_until_found
1740 (cp_parser
*, enum cpp_ttype
, const char *);
1741 static bool cp_parser_error_occurred
1743 static bool cp_parser_allow_gnu_extensions_p
1745 static bool cp_parser_is_string_literal
1747 static bool cp_parser_is_keyword
1748 (cp_token
*, enum rid
);
1750 /* Returns nonzero if we are parsing tentatively. */
1753 cp_parser_parsing_tentatively (cp_parser
* parser
)
1755 return parser
->context
->next
!= NULL
;
1758 /* Returns nonzero if TOKEN is a string literal. */
1761 cp_parser_is_string_literal (cp_token
* token
)
1763 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1766 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1769 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1771 return token
->keyword
== keyword
;
1774 /* Issue the indicated error MESSAGE. */
1777 cp_parser_error (cp_parser
* parser
, const char* message
)
1779 /* Output the MESSAGE -- unless we're parsing tentatively. */
1780 if (!cp_parser_simulate_error (parser
))
1783 token
= cp_lexer_peek_token (parser
->lexer
);
1784 c_parse_error (message
,
1785 /* Because c_parser_error does not understand
1786 CPP_KEYWORD, keywords are treated like
1788 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1793 /* Issue an error about name-lookup failing. NAME is the
1794 IDENTIFIER_NODE DECL is the result of
1795 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1796 the thing that we hoped to find. */
1799 cp_parser_name_lookup_error (cp_parser
* parser
,
1802 const char* desired
)
1804 /* If name lookup completely failed, tell the user that NAME was not
1806 if (decl
== error_mark_node
)
1808 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1809 error ("`%D::%D' has not been declared",
1810 parser
->scope
, name
);
1811 else if (parser
->scope
== global_namespace
)
1812 error ("`::%D' has not been declared", name
);
1814 error ("`%D' has not been declared", name
);
1816 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1817 error ("`%D::%D' %s", parser
->scope
, name
, desired
);
1818 else if (parser
->scope
== global_namespace
)
1819 error ("`::%D' %s", name
, desired
);
1821 error ("`%D' %s", name
, desired
);
1824 /* If we are parsing tentatively, remember that an error has occurred
1825 during this tentative parse. Returns true if the error was
1826 simulated; false if a messgae should be issued by the caller. */
1829 cp_parser_simulate_error (cp_parser
* parser
)
1831 if (cp_parser_parsing_tentatively (parser
)
1832 && !cp_parser_committed_to_tentative_parse (parser
))
1834 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1840 /* This function is called when a type is defined. If type
1841 definitions are forbidden at this point, an error message is
1845 cp_parser_check_type_definition (cp_parser
* parser
)
1847 /* If types are forbidden here, issue a message. */
1848 if (parser
->type_definition_forbidden_message
)
1849 /* Use `%s' to print the string in case there are any escape
1850 characters in the message. */
1851 error ("%s", parser
->type_definition_forbidden_message
);
1854 /* This function is called when the DECLARATOR is processed. The TYPE
1855 was a type defined in the decl-specifiers. If it is invalid to
1856 define a type in the decl-specifiers for DECLARATOR, an error is
1860 cp_parser_check_for_definition_in_return_type (tree declarator
, tree type
)
1862 /* [dcl.fct] forbids type definitions in return types.
1863 Unfortunately, it's not easy to know whether or not we are
1864 processing a return type until after the fact. */
1866 && (TREE_CODE (declarator
) == INDIRECT_REF
1867 || TREE_CODE (declarator
) == ADDR_EXPR
))
1868 declarator
= TREE_OPERAND (declarator
, 0);
1870 && TREE_CODE (declarator
) == CALL_EXPR
)
1872 error ("new types may not be defined in a return type");
1873 inform ("(perhaps a semicolon is missing after the definition of `%T')",
1878 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1879 "<" in any valid C++ program. If the next token is indeed "<",
1880 issue a message warning the user about what appears to be an
1881 invalid attempt to form a template-id. */
1884 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1893 error ("`%T' is not a template", type
);
1894 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1895 error ("`%s' is not a template", IDENTIFIER_POINTER (type
));
1897 error ("invalid template-id");
1898 /* Remember the location of the invalid "<". */
1899 if (cp_parser_parsing_tentatively (parser
)
1900 && !cp_parser_committed_to_tentative_parse (parser
))
1902 token
= cp_lexer_peek_token (parser
->lexer
);
1903 token
= cp_lexer_prev_token (parser
->lexer
, token
);
1904 start
= cp_lexer_token_difference (parser
->lexer
,
1905 parser
->lexer
->first_token
,
1910 /* Consume the "<". */
1911 cp_lexer_consume_token (parser
->lexer
);
1912 /* Parse the template arguments. */
1913 cp_parser_enclosed_template_argument_list (parser
);
1914 /* Permanently remove the invalid template arguments so that
1915 this error message is not issued again. */
1918 token
= cp_lexer_advance_token (parser
->lexer
,
1919 parser
->lexer
->first_token
,
1921 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
1926 /* If parsing an integral constant-expression, issue an error message
1927 about the fact that THING appeared and return true. Otherwise,
1928 return false, marking the current expression as non-constant. */
1931 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
1934 if (parser
->integral_constant_expression_p
)
1936 if (!parser
->allow_non_integral_constant_expression_p
)
1938 error ("%s cannot appear in a constant-expression", thing
);
1941 parser
->non_integral_constant_expression_p
= true;
1946 /* Check for a common situation where a type-name should be present,
1947 but is not, and issue a sensible error message. Returns true if an
1948 invalid type-name was detected. */
1951 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
)
1953 /* If the next two tokens are both identifiers, the code is
1954 erroneous. The usual cause of this situation is code like:
1958 where "T" should name a type -- but does not. */
1959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
1960 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
1964 /* If parsing tentatively, we should commit; we really are
1965 looking at a declaration. */
1966 /* Consume the first identifier. */
1967 name
= cp_lexer_consume_token (parser
->lexer
)->value
;
1968 /* Issue an error message. */
1969 error ("`%s' does not name a type", IDENTIFIER_POINTER (name
));
1970 /* If we're in a template class, it's possible that the user was
1971 referring to a type from a base class. For example:
1973 template <typename T> struct A { typedef T X; };
1974 template <typename T> struct B : public A<T> { X x; };
1976 The user should have said "typename A<T>::X". */
1977 if (processing_template_decl
&& current_class_type
)
1981 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1985 tree base_type
= BINFO_TYPE (b
);
1986 if (CLASS_TYPE_P (base_type
)
1987 && dependent_type_p (base_type
))
1990 /* Go from a particular instantiation of the
1991 template (which will have an empty TYPE_FIELDs),
1992 to the main version. */
1993 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1994 for (field
= TYPE_FIELDS (base_type
);
1996 field
= TREE_CHAIN (field
))
1997 if (TREE_CODE (field
) == TYPE_DECL
1998 && DECL_NAME (field
) == name
)
2000 error ("(perhaps `typename %T::%s' was intended)",
2001 BINFO_TYPE (b
), IDENTIFIER_POINTER (name
));
2009 /* Skip to the end of the declaration; there's no point in
2010 trying to process it. */
2011 cp_parser_skip_to_end_of_statement (parser
);
2019 /* Consume tokens up to, and including, the next non-nested closing `)'.
2020 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2021 are doing error recovery. Returns -1 if OR_COMMA is true and we
2022 found an unnested comma. */
2025 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2030 unsigned paren_depth
= 0;
2031 unsigned brace_depth
= 0;
2033 if (recovering
&& !or_comma
&& cp_parser_parsing_tentatively (parser
)
2034 && !cp_parser_committed_to_tentative_parse (parser
))
2041 /* If we've run out of tokens, then there is no closing `)'. */
2042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2045 token
= cp_lexer_peek_token (parser
->lexer
);
2047 /* This matches the processing in skip_to_end_of_statement. */
2048 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2050 if (token
->type
== CPP_OPEN_BRACE
)
2052 if (token
->type
== CPP_CLOSE_BRACE
)
2057 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2058 && !brace_depth
&& !paren_depth
)
2063 /* If it is an `(', we have entered another level of nesting. */
2064 if (token
->type
== CPP_OPEN_PAREN
)
2066 /* If it is a `)', then we might be done. */
2067 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2070 cp_lexer_consume_token (parser
->lexer
);
2075 /* Consume the token. */
2076 cp_lexer_consume_token (parser
->lexer
);
2080 /* Consume tokens until we reach the end of the current statement.
2081 Normally, that will be just before consuming a `;'. However, if a
2082 non-nested `}' comes first, then we stop before consuming that. */
2085 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2087 unsigned nesting_depth
= 0;
2093 /* Peek at the next token. */
2094 token
= cp_lexer_peek_token (parser
->lexer
);
2095 /* If we've run out of tokens, stop. */
2096 if (token
->type
== CPP_EOF
)
2098 /* If the next token is a `;', we have reached the end of the
2100 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2102 /* If the next token is a non-nested `}', then we have reached
2103 the end of the current block. */
2104 if (token
->type
== CPP_CLOSE_BRACE
)
2106 /* If this is a non-nested `}', stop before consuming it.
2107 That way, when confronted with something like:
2111 we stop before consuming the closing `}', even though we
2112 have not yet reached a `;'. */
2113 if (nesting_depth
== 0)
2115 /* If it is the closing `}' for a block that we have
2116 scanned, stop -- but only after consuming the token.
2122 we will stop after the body of the erroneously declared
2123 function, but before consuming the following `typedef'
2125 if (--nesting_depth
== 0)
2127 cp_lexer_consume_token (parser
->lexer
);
2131 /* If it the next token is a `{', then we are entering a new
2132 block. Consume the entire block. */
2133 else if (token
->type
== CPP_OPEN_BRACE
)
2135 /* Consume the token. */
2136 cp_lexer_consume_token (parser
->lexer
);
2140 /* This function is called at the end of a statement or declaration.
2141 If the next token is a semicolon, it is consumed; otherwise, error
2142 recovery is attempted. */
2145 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2147 /* Look for the trailing `;'. */
2148 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2150 /* If there is additional (erroneous) input, skip to the end of
2152 cp_parser_skip_to_end_of_statement (parser
);
2153 /* If the next token is now a `;', consume it. */
2154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2155 cp_lexer_consume_token (parser
->lexer
);
2159 /* Skip tokens until we have consumed an entire block, or until we
2160 have consumed a non-nested `;'. */
2163 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2165 unsigned nesting_depth
= 0;
2171 /* Peek at the next token. */
2172 token
= cp_lexer_peek_token (parser
->lexer
);
2173 /* If we've run out of tokens, stop. */
2174 if (token
->type
== CPP_EOF
)
2176 /* If the next token is a `;', we have reached the end of the
2178 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2180 /* Consume the `;'. */
2181 cp_lexer_consume_token (parser
->lexer
);
2184 /* Consume the token. */
2185 token
= cp_lexer_consume_token (parser
->lexer
);
2186 /* If the next token is a non-nested `}', then we have reached
2187 the end of the current block. */
2188 if (token
->type
== CPP_CLOSE_BRACE
2189 && (nesting_depth
== 0 || --nesting_depth
== 0))
2191 /* If it the next token is a `{', then we are entering a new
2192 block. Consume the entire block. */
2193 if (token
->type
== CPP_OPEN_BRACE
)
2198 /* Skip tokens until a non-nested closing curly brace is the next
2202 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2204 unsigned nesting_depth
= 0;
2210 /* Peek at the next token. */
2211 token
= cp_lexer_peek_token (parser
->lexer
);
2212 /* If we've run out of tokens, stop. */
2213 if (token
->type
== CPP_EOF
)
2215 /* If the next token is a non-nested `}', then we have reached
2216 the end of the current block. */
2217 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2219 /* If it the next token is a `{', then we are entering a new
2220 block. Consume the entire block. */
2221 else if (token
->type
== CPP_OPEN_BRACE
)
2223 /* Consume the token. */
2224 cp_lexer_consume_token (parser
->lexer
);
2228 /* Create a new C++ parser. */
2231 cp_parser_new (void)
2236 /* cp_lexer_new_main is called before calling ggc_alloc because
2237 cp_lexer_new_main might load a PCH file. */
2238 lexer
= cp_lexer_new_main ();
2240 parser
= ggc_alloc_cleared (sizeof (cp_parser
));
2241 parser
->lexer
= lexer
;
2242 parser
->context
= cp_parser_context_new (NULL
);
2244 /* For now, we always accept GNU extensions. */
2245 parser
->allow_gnu_extensions_p
= 1;
2247 /* The `>' token is a greater-than operator, not the end of a
2249 parser
->greater_than_is_operator_p
= true;
2251 parser
->default_arg_ok_p
= true;
2253 /* We are not parsing a constant-expression. */
2254 parser
->integral_constant_expression_p
= false;
2255 parser
->allow_non_integral_constant_expression_p
= false;
2256 parser
->non_integral_constant_expression_p
= false;
2258 /* We are not parsing offsetof. */
2259 parser
->in_offsetof_p
= false;
2261 /* Local variable names are not forbidden. */
2262 parser
->local_variables_forbidden_p
= false;
2264 /* We are not processing an `extern "C"' declaration. */
2265 parser
->in_unbraced_linkage_specification_p
= false;
2267 /* We are not processing a declarator. */
2268 parser
->in_declarator_p
= false;
2270 /* We are not processing a template-argument-list. */
2271 parser
->in_template_argument_list_p
= false;
2273 /* We are not in an iteration statement. */
2274 parser
->in_iteration_statement_p
= false;
2276 /* We are not in a switch statement. */
2277 parser
->in_switch_statement_p
= false;
2279 /* We are not parsing a type-id inside an expression. */
2280 parser
->in_type_id_in_expr_p
= false;
2282 /* The unparsed function queue is empty. */
2283 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2285 /* There are no classes being defined. */
2286 parser
->num_classes_being_defined
= 0;
2288 /* No template parameters apply. */
2289 parser
->num_template_parameter_lists
= 0;
2294 /* Lexical conventions [gram.lex] */
2296 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2300 cp_parser_identifier (cp_parser
* parser
)
2304 /* Look for the identifier. */
2305 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2306 /* Return the value. */
2307 return token
? token
->value
: error_mark_node
;
2310 /* Basic concepts [gram.basic] */
2312 /* Parse a translation-unit.
2315 declaration-seq [opt]
2317 Returns TRUE if all went well. */
2320 cp_parser_translation_unit (cp_parser
* parser
)
2324 cp_parser_declaration_seq_opt (parser
);
2326 /* If there are no tokens left then all went well. */
2327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2330 /* Otherwise, issue an error message. */
2331 cp_parser_error (parser
, "expected declaration");
2335 /* Consume the EOF token. */
2336 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2339 finish_translation_unit ();
2341 /* All went well. */
2345 /* Expressions [gram.expr] */
2347 /* Parse a primary-expression.
2358 ( compound-statement )
2359 __builtin_va_arg ( assignment-expression , type-id )
2364 Returns a representation of the expression.
2366 *IDK indicates what kind of id-expression (if any) was present.
2368 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2369 used as the operand of a pointer-to-member. In that case,
2370 *QUALIFYING_CLASS gives the class that is used as the qualifying
2371 class in the pointer-to-member. */
2374 cp_parser_primary_expression (cp_parser
*parser
,
2376 tree
*qualifying_class
)
2380 /* Assume the primary expression is not an id-expression. */
2381 *idk
= CP_ID_KIND_NONE
;
2382 /* And that it cannot be used as pointer-to-member. */
2383 *qualifying_class
= NULL_TREE
;
2385 /* Peek at the next token. */
2386 token
= cp_lexer_peek_token (parser
->lexer
);
2387 switch (token
->type
)
2400 token
= cp_lexer_consume_token (parser
->lexer
);
2401 return token
->value
;
2403 case CPP_OPEN_PAREN
:
2406 bool saved_greater_than_is_operator_p
;
2408 /* Consume the `('. */
2409 cp_lexer_consume_token (parser
->lexer
);
2410 /* Within a parenthesized expression, a `>' token is always
2411 the greater-than operator. */
2412 saved_greater_than_is_operator_p
2413 = parser
->greater_than_is_operator_p
;
2414 parser
->greater_than_is_operator_p
= true;
2415 /* If we see `( { ' then we are looking at the beginning of
2416 a GNU statement-expression. */
2417 if (cp_parser_allow_gnu_extensions_p (parser
)
2418 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2420 /* Statement-expressions are not allowed by the standard. */
2422 pedwarn ("ISO C++ forbids braced-groups within expressions");
2424 /* And they're not allowed outside of a function-body; you
2425 cannot, for example, write:
2427 int i = ({ int j = 3; j + 1; });
2429 at class or namespace scope. */
2430 if (!at_function_scope_p ())
2431 error ("statement-expressions are allowed only inside functions");
2432 /* Start the statement-expression. */
2433 expr
= begin_stmt_expr ();
2434 /* Parse the compound-statement. */
2435 cp_parser_compound_statement (parser
, true);
2437 expr
= finish_stmt_expr (expr
, false);
2441 /* Parse the parenthesized expression. */
2442 expr
= cp_parser_expression (parser
);
2443 /* Let the front end know that this expression was
2444 enclosed in parentheses. This matters in case, for
2445 example, the expression is of the form `A::B', since
2446 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2448 finish_parenthesized_expr (expr
);
2450 /* The `>' token might be the end of a template-id or
2451 template-parameter-list now. */
2452 parser
->greater_than_is_operator_p
2453 = saved_greater_than_is_operator_p
;
2454 /* Consume the `)'. */
2455 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2456 cp_parser_skip_to_end_of_statement (parser
);
2462 switch (token
->keyword
)
2464 /* These two are the boolean literals. */
2466 cp_lexer_consume_token (parser
->lexer
);
2467 return boolean_true_node
;
2469 cp_lexer_consume_token (parser
->lexer
);
2470 return boolean_false_node
;
2472 /* The `__null' literal. */
2474 cp_lexer_consume_token (parser
->lexer
);
2477 /* Recognize the `this' keyword. */
2479 cp_lexer_consume_token (parser
->lexer
);
2480 if (parser
->local_variables_forbidden_p
)
2482 error ("`this' may not be used in this context");
2483 return error_mark_node
;
2485 /* Pointers cannot appear in constant-expressions. */
2486 if (cp_parser_non_integral_constant_expression (parser
,
2488 return error_mark_node
;
2489 return finish_this_expr ();
2491 /* The `operator' keyword can be the beginning of an
2496 case RID_FUNCTION_NAME
:
2497 case RID_PRETTY_FUNCTION_NAME
:
2498 case RID_C99_FUNCTION_NAME
:
2499 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2500 __func__ are the names of variables -- but they are
2501 treated specially. Therefore, they are handled here,
2502 rather than relying on the generic id-expression logic
2503 below. Grammatically, these names are id-expressions.
2505 Consume the token. */
2506 token
= cp_lexer_consume_token (parser
->lexer
);
2507 /* Look up the name. */
2508 return finish_fname (token
->value
);
2515 /* The `__builtin_va_arg' construct is used to handle
2516 `va_arg'. Consume the `__builtin_va_arg' token. */
2517 cp_lexer_consume_token (parser
->lexer
);
2518 /* Look for the opening `('. */
2519 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2520 /* Now, parse the assignment-expression. */
2521 expression
= cp_parser_assignment_expression (parser
);
2522 /* Look for the `,'. */
2523 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2524 /* Parse the type-id. */
2525 type
= cp_parser_type_id (parser
);
2526 /* Look for the closing `)'. */
2527 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2528 /* Using `va_arg' in a constant-expression is not
2530 if (cp_parser_non_integral_constant_expression (parser
,
2532 return error_mark_node
;
2533 return build_x_va_arg (expression
, type
);
2539 bool saved_in_offsetof_p
;
2541 /* Consume the "__offsetof__" token. */
2542 cp_lexer_consume_token (parser
->lexer
);
2543 /* Consume the opening `('. */
2544 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2545 /* Parse the parenthesized (almost) constant-expression. */
2546 saved_in_offsetof_p
= parser
->in_offsetof_p
;
2547 parser
->in_offsetof_p
= true;
2549 = cp_parser_constant_expression (parser
,
2550 /*allow_non_constant_p=*/false,
2551 /*non_constant_p=*/NULL
);
2552 parser
->in_offsetof_p
= saved_in_offsetof_p
;
2553 /* Consume the closing ')'. */
2554 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2560 cp_parser_error (parser
, "expected primary-expression");
2561 return error_mark_node
;
2564 /* An id-expression can start with either an identifier, a
2565 `::' as the beginning of a qualified-id, or the "operator"
2569 case CPP_TEMPLATE_ID
:
2570 case CPP_NESTED_NAME_SPECIFIER
:
2574 const char *error_msg
;
2577 /* Parse the id-expression. */
2579 = cp_parser_id_expression (parser
,
2580 /*template_keyword_p=*/false,
2581 /*check_dependency_p=*/true,
2582 /*template_p=*/NULL
,
2583 /*declarator_p=*/false);
2584 if (id_expression
== error_mark_node
)
2585 return error_mark_node
;
2586 /* If we have a template-id, then no further lookup is
2587 required. If the template-id was for a template-class, we
2588 will sometimes have a TYPE_DECL at this point. */
2589 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2590 || TREE_CODE (id_expression
) == TYPE_DECL
)
2591 decl
= id_expression
;
2592 /* Look up the name. */
2595 decl
= cp_parser_lookup_name_simple (parser
, id_expression
);
2596 /* If name lookup gives us a SCOPE_REF, then the
2597 qualifying scope was dependent. Just propagate the
2599 if (TREE_CODE (decl
) == SCOPE_REF
)
2601 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2602 *qualifying_class
= TREE_OPERAND (decl
, 0);
2605 /* Check to see if DECL is a local variable in a context
2606 where that is forbidden. */
2607 if (parser
->local_variables_forbidden_p
2608 && local_variable_p (decl
))
2610 /* It might be that we only found DECL because we are
2611 trying to be generous with pre-ISO scoping rules.
2612 For example, consider:
2616 for (int i = 0; i < 10; ++i) {}
2617 extern void f(int j = i);
2620 Here, name look up will originally find the out
2621 of scope `i'. We need to issue a warning message,
2622 but then use the global `i'. */
2623 decl
= check_for_out_of_scope_variable (decl
);
2624 if (local_variable_p (decl
))
2626 error ("local variable `%D' may not appear in this context",
2628 return error_mark_node
;
2633 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2634 idk
, qualifying_class
,
2635 parser
->integral_constant_expression_p
,
2636 parser
->allow_non_integral_constant_expression_p
,
2637 &parser
->non_integral_constant_expression_p
,
2640 cp_parser_error (parser
, error_msg
);
2644 /* Anything else is an error. */
2646 cp_parser_error (parser
, "expected primary-expression");
2647 return error_mark_node
;
2651 /* Parse an id-expression.
2658 :: [opt] nested-name-specifier template [opt] unqualified-id
2660 :: operator-function-id
2663 Return a representation of the unqualified portion of the
2664 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2665 a `::' or nested-name-specifier.
2667 Often, if the id-expression was a qualified-id, the caller will
2668 want to make a SCOPE_REF to represent the qualified-id. This
2669 function does not do this in order to avoid wastefully creating
2670 SCOPE_REFs when they are not required.
2672 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2675 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2676 uninstantiated templates.
2678 If *TEMPLATE_P is non-NULL, it is set to true iff the
2679 `template' keyword is used to explicitly indicate that the entity
2680 named is a template.
2682 If DECLARATOR_P is true, the id-expression is appearing as part of
2683 a declarator, rather than as part of an expression. */
2686 cp_parser_id_expression (cp_parser
*parser
,
2687 bool template_keyword_p
,
2688 bool check_dependency_p
,
2692 bool global_scope_p
;
2693 bool nested_name_specifier_p
;
2695 /* Assume the `template' keyword was not used. */
2697 *template_p
= false;
2699 /* Look for the optional `::' operator. */
2701 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2703 /* Look for the optional nested-name-specifier. */
2704 nested_name_specifier_p
2705 = (cp_parser_nested_name_specifier_opt (parser
,
2706 /*typename_keyword_p=*/false,
2711 /* If there is a nested-name-specifier, then we are looking at
2712 the first qualified-id production. */
2713 if (nested_name_specifier_p
)
2716 tree saved_object_scope
;
2717 tree saved_qualifying_scope
;
2718 tree unqualified_id
;
2721 /* See if the next token is the `template' keyword. */
2723 template_p
= &is_template
;
2724 *template_p
= cp_parser_optional_template_keyword (parser
);
2725 /* Name lookup we do during the processing of the
2726 unqualified-id might obliterate SCOPE. */
2727 saved_scope
= parser
->scope
;
2728 saved_object_scope
= parser
->object_scope
;
2729 saved_qualifying_scope
= parser
->qualifying_scope
;
2730 /* Process the final unqualified-id. */
2731 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2734 /* Restore the SAVED_SCOPE for our caller. */
2735 parser
->scope
= saved_scope
;
2736 parser
->object_scope
= saved_object_scope
;
2737 parser
->qualifying_scope
= saved_qualifying_scope
;
2739 return unqualified_id
;
2741 /* Otherwise, if we are in global scope, then we are looking at one
2742 of the other qualified-id productions. */
2743 else if (global_scope_p
)
2748 /* Peek at the next token. */
2749 token
= cp_lexer_peek_token (parser
->lexer
);
2751 /* If it's an identifier, and the next token is not a "<", then
2752 we can avoid the template-id case. This is an optimization
2753 for this common case. */
2754 if (token
->type
== CPP_NAME
2755 && !cp_parser_nth_token_starts_template_argument_list_p
2757 return cp_parser_identifier (parser
);
2759 cp_parser_parse_tentatively (parser
);
2760 /* Try a template-id. */
2761 id
= cp_parser_template_id (parser
,
2762 /*template_keyword_p=*/false,
2763 /*check_dependency_p=*/true,
2765 /* If that worked, we're done. */
2766 if (cp_parser_parse_definitely (parser
))
2769 /* Peek at the next token. (Changes in the token buffer may
2770 have invalidated the pointer obtained above.) */
2771 token
= cp_lexer_peek_token (parser
->lexer
);
2773 switch (token
->type
)
2776 return cp_parser_identifier (parser
);
2779 if (token
->keyword
== RID_OPERATOR
)
2780 return cp_parser_operator_function_id (parser
);
2784 cp_parser_error (parser
, "expected id-expression");
2785 return error_mark_node
;
2789 return cp_parser_unqualified_id (parser
, template_keyword_p
,
2790 /*check_dependency_p=*/true,
2794 /* Parse an unqualified-id.
2798 operator-function-id
2799 conversion-function-id
2803 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2804 keyword, in a construct like `A::template ...'.
2806 Returns a representation of unqualified-id. For the `identifier'
2807 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2808 production a BIT_NOT_EXPR is returned; the operand of the
2809 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2810 other productions, see the documentation accompanying the
2811 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2812 names are looked up in uninstantiated templates. If DECLARATOR_P
2813 is true, the unqualified-id is appearing as part of a declarator,
2814 rather than as part of an expression. */
2817 cp_parser_unqualified_id (cp_parser
* parser
,
2818 bool template_keyword_p
,
2819 bool check_dependency_p
,
2824 /* Peek at the next token. */
2825 token
= cp_lexer_peek_token (parser
->lexer
);
2827 switch (token
->type
)
2833 /* We don't know yet whether or not this will be a
2835 cp_parser_parse_tentatively (parser
);
2836 /* Try a template-id. */
2837 id
= cp_parser_template_id (parser
, template_keyword_p
,
2840 /* If it worked, we're done. */
2841 if (cp_parser_parse_definitely (parser
))
2843 /* Otherwise, it's an ordinary identifier. */
2844 return cp_parser_identifier (parser
);
2847 case CPP_TEMPLATE_ID
:
2848 return cp_parser_template_id (parser
, template_keyword_p
,
2855 tree qualifying_scope
;
2860 /* Consume the `~' token. */
2861 cp_lexer_consume_token (parser
->lexer
);
2862 /* Parse the class-name. The standard, as written, seems to
2865 template <typename T> struct S { ~S (); };
2866 template <typename T> S<T>::~S() {}
2868 is invalid, since `~' must be followed by a class-name, but
2869 `S<T>' is dependent, and so not known to be a class.
2870 That's not right; we need to look in uninstantiated
2871 templates. A further complication arises from:
2873 template <typename T> void f(T t) {
2877 Here, it is not possible to look up `T' in the scope of `T'
2878 itself. We must look in both the current scope, and the
2879 scope of the containing complete expression.
2881 Yet another issue is:
2890 The standard does not seem to say that the `S' in `~S'
2891 should refer to the type `S' and not the data member
2894 /* DR 244 says that we look up the name after the "~" in the
2895 same scope as we looked up the qualifying name. That idea
2896 isn't fully worked out; it's more complicated than that. */
2897 scope
= parser
->scope
;
2898 object_scope
= parser
->object_scope
;
2899 qualifying_scope
= parser
->qualifying_scope
;
2901 /* If the name is of the form "X::~X" it's OK. */
2902 if (scope
&& TYPE_P (scope
)
2903 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2904 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
2906 && (cp_lexer_peek_token (parser
->lexer
)->value
2907 == TYPE_IDENTIFIER (scope
)))
2909 cp_lexer_consume_token (parser
->lexer
);
2910 return build_nt (BIT_NOT_EXPR
, scope
);
2913 /* If there was an explicit qualification (S::~T), first look
2914 in the scope given by the qualification (i.e., S). */
2916 type_decl
= NULL_TREE
;
2919 cp_parser_parse_tentatively (parser
);
2920 type_decl
= cp_parser_class_name (parser
,
2921 /*typename_keyword_p=*/false,
2922 /*template_keyword_p=*/false,
2924 /*check_dependency=*/false,
2925 /*class_head_p=*/false,
2927 if (cp_parser_parse_definitely (parser
))
2930 /* In "N::S::~S", look in "N" as well. */
2931 if (!done
&& scope
&& qualifying_scope
)
2933 cp_parser_parse_tentatively (parser
);
2934 parser
->scope
= qualifying_scope
;
2935 parser
->object_scope
= NULL_TREE
;
2936 parser
->qualifying_scope
= NULL_TREE
;
2938 = cp_parser_class_name (parser
,
2939 /*typename_keyword_p=*/false,
2940 /*template_keyword_p=*/false,
2942 /*check_dependency=*/false,
2943 /*class_head_p=*/false,
2945 if (cp_parser_parse_definitely (parser
))
2948 /* In "p->S::~T", look in the scope given by "*p" as well. */
2949 else if (!done
&& object_scope
)
2951 cp_parser_parse_tentatively (parser
);
2952 parser
->scope
= object_scope
;
2953 parser
->object_scope
= NULL_TREE
;
2954 parser
->qualifying_scope
= NULL_TREE
;
2956 = cp_parser_class_name (parser
,
2957 /*typename_keyword_p=*/false,
2958 /*template_keyword_p=*/false,
2960 /*check_dependency=*/false,
2961 /*class_head_p=*/false,
2963 if (cp_parser_parse_definitely (parser
))
2966 /* Look in the surrounding context. */
2969 parser
->scope
= NULL_TREE
;
2970 parser
->object_scope
= NULL_TREE
;
2971 parser
->qualifying_scope
= NULL_TREE
;
2973 = cp_parser_class_name (parser
,
2974 /*typename_keyword_p=*/false,
2975 /*template_keyword_p=*/false,
2977 /*check_dependency=*/false,
2978 /*class_head_p=*/false,
2981 /* If an error occurred, assume that the name of the
2982 destructor is the same as the name of the qualifying
2983 class. That allows us to keep parsing after running
2984 into ill-formed destructor names. */
2985 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
2986 return build_nt (BIT_NOT_EXPR
, scope
);
2987 else if (type_decl
== error_mark_node
)
2988 return error_mark_node
;
2992 A typedef-name that names a class shall not be used as the
2993 identifier in the declarator for a destructor declaration. */
2995 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
2996 && !DECL_SELF_REFERENCE_P (type_decl
))
2997 error ("typedef-name `%D' used as destructor declarator",
3000 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3004 if (token
->keyword
== RID_OPERATOR
)
3008 /* This could be a template-id, so we try that first. */
3009 cp_parser_parse_tentatively (parser
);
3010 /* Try a template-id. */
3011 id
= cp_parser_template_id (parser
, template_keyword_p
,
3012 /*check_dependency_p=*/true,
3014 /* If that worked, we're done. */
3015 if (cp_parser_parse_definitely (parser
))
3017 /* We still don't know whether we're looking at an
3018 operator-function-id or a conversion-function-id. */
3019 cp_parser_parse_tentatively (parser
);
3020 /* Try an operator-function-id. */
3021 id
= cp_parser_operator_function_id (parser
);
3022 /* If that didn't work, try a conversion-function-id. */
3023 if (!cp_parser_parse_definitely (parser
))
3024 id
= cp_parser_conversion_function_id (parser
);
3031 cp_parser_error (parser
, "expected unqualified-id");
3032 return error_mark_node
;
3036 /* Parse an (optional) nested-name-specifier.
3038 nested-name-specifier:
3039 class-or-namespace-name :: nested-name-specifier [opt]
3040 class-or-namespace-name :: template nested-name-specifier [opt]
3042 PARSER->SCOPE should be set appropriately before this function is
3043 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3044 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3047 Sets PARSER->SCOPE to the class (TYPE) or namespace
3048 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3049 it unchanged if there is no nested-name-specifier. Returns the new
3050 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3052 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3053 part of a declaration and/or decl-specifier. */
3056 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3057 bool typename_keyword_p
,
3058 bool check_dependency_p
,
3060 bool is_declaration
)
3062 bool success
= false;
3063 tree access_check
= NULL_TREE
;
3067 /* If the next token corresponds to a nested name specifier, there
3068 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3069 false, it may have been true before, in which case something
3070 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3071 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3072 CHECK_DEPENDENCY_P is false, we have to fall through into the
3074 if (check_dependency_p
3075 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3077 cp_parser_pre_parsed_nested_name_specifier (parser
);
3078 return parser
->scope
;
3081 /* Remember where the nested-name-specifier starts. */
3082 if (cp_parser_parsing_tentatively (parser
)
3083 && !cp_parser_committed_to_tentative_parse (parser
))
3085 token
= cp_lexer_peek_token (parser
->lexer
);
3086 start
= cp_lexer_token_difference (parser
->lexer
,
3087 parser
->lexer
->first_token
,
3093 push_deferring_access_checks (dk_deferred
);
3099 tree saved_qualifying_scope
;
3100 bool template_keyword_p
;
3102 /* Spot cases that cannot be the beginning of a
3103 nested-name-specifier. */
3104 token
= cp_lexer_peek_token (parser
->lexer
);
3106 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3107 the already parsed nested-name-specifier. */
3108 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3110 /* Grab the nested-name-specifier and continue the loop. */
3111 cp_parser_pre_parsed_nested_name_specifier (parser
);
3116 /* Spot cases that cannot be the beginning of a
3117 nested-name-specifier. On the second and subsequent times
3118 through the loop, we look for the `template' keyword. */
3119 if (success
&& token
->keyword
== RID_TEMPLATE
)
3121 /* A template-id can start a nested-name-specifier. */
3122 else if (token
->type
== CPP_TEMPLATE_ID
)
3126 /* If the next token is not an identifier, then it is
3127 definitely not a class-or-namespace-name. */
3128 if (token
->type
!= CPP_NAME
)
3130 /* If the following token is neither a `<' (to begin a
3131 template-id), nor a `::', then we are not looking at a
3132 nested-name-specifier. */
3133 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3134 if (token
->type
!= CPP_SCOPE
3135 && !cp_parser_nth_token_starts_template_argument_list_p
3140 /* The nested-name-specifier is optional, so we parse
3142 cp_parser_parse_tentatively (parser
);
3144 /* Look for the optional `template' keyword, if this isn't the
3145 first time through the loop. */
3147 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3149 template_keyword_p
= false;
3151 /* Save the old scope since the name lookup we are about to do
3152 might destroy it. */
3153 old_scope
= parser
->scope
;
3154 saved_qualifying_scope
= parser
->qualifying_scope
;
3155 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3156 look up names in "X<T>::I" in order to determine that "Y" is
3157 a template. So, if we have a typename at this point, we make
3158 an effort to look through it. */
3160 && !typename_keyword_p
3162 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3163 parser
->scope
= resolve_typename_type (parser
->scope
,
3164 /*only_current_p=*/false);
3165 /* Parse the qualifying entity. */
3167 = cp_parser_class_or_namespace_name (parser
,
3173 /* Look for the `::' token. */
3174 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3176 /* If we found what we wanted, we keep going; otherwise, we're
3178 if (!cp_parser_parse_definitely (parser
))
3180 bool error_p
= false;
3182 /* Restore the OLD_SCOPE since it was valid before the
3183 failed attempt at finding the last
3184 class-or-namespace-name. */
3185 parser
->scope
= old_scope
;
3186 parser
->qualifying_scope
= saved_qualifying_scope
;
3187 /* If the next token is an identifier, and the one after
3188 that is a `::', then any valid interpretation would have
3189 found a class-or-namespace-name. */
3190 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3191 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3193 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3196 token
= cp_lexer_consume_token (parser
->lexer
);
3201 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3202 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3203 error ("`%D' used without template parameters",
3206 cp_parser_name_lookup_error
3207 (parser
, token
->value
, decl
,
3208 "is not a class or namespace");
3209 parser
->scope
= NULL_TREE
;
3211 /* Treat this as a successful nested-name-specifier
3216 If the name found is not a class-name (clause
3217 _class_) or namespace-name (_namespace.def_), the
3218 program is ill-formed. */
3221 cp_lexer_consume_token (parser
->lexer
);
3226 /* We've found one valid nested-name-specifier. */
3228 /* Make sure we look in the right scope the next time through
3230 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3231 ? TREE_TYPE (new_scope
)
3233 /* If it is a class scope, try to complete it; we are about to
3234 be looking up names inside the class. */
3235 if (TYPE_P (parser
->scope
)
3236 /* Since checking types for dependency can be expensive,
3237 avoid doing it if the type is already complete. */
3238 && !COMPLETE_TYPE_P (parser
->scope
)
3239 /* Do not try to complete dependent types. */
3240 && !dependent_type_p (parser
->scope
))
3241 complete_type (parser
->scope
);
3244 /* Retrieve any deferred checks. Do not pop this access checks yet
3245 so the memory will not be reclaimed during token replacing below. */
3246 access_check
= get_deferred_access_checks ();
3248 /* If parsing tentatively, replace the sequence of tokens that makes
3249 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3250 token. That way, should we re-parse the token stream, we will
3251 not have to repeat the effort required to do the parse, nor will
3252 we issue duplicate error messages. */
3253 if (success
&& start
>= 0)
3255 /* Find the token that corresponds to the start of the
3257 token
= cp_lexer_advance_token (parser
->lexer
,
3258 parser
->lexer
->first_token
,
3261 /* Reset the contents of the START token. */
3262 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3263 token
->value
= build_tree_list (access_check
, parser
->scope
);
3264 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3265 token
->keyword
= RID_MAX
;
3266 /* Purge all subsequent tokens. */
3267 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3270 pop_deferring_access_checks ();
3271 return success
? parser
->scope
: NULL_TREE
;
3274 /* Parse a nested-name-specifier. See
3275 cp_parser_nested_name_specifier_opt for details. This function
3276 behaves identically, except that it will an issue an error if no
3277 nested-name-specifier is present, and it will return
3278 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3282 cp_parser_nested_name_specifier (cp_parser
*parser
,
3283 bool typename_keyword_p
,
3284 bool check_dependency_p
,
3286 bool is_declaration
)
3290 /* Look for the nested-name-specifier. */
3291 scope
= cp_parser_nested_name_specifier_opt (parser
,
3296 /* If it was not present, issue an error message. */
3299 cp_parser_error (parser
, "expected nested-name-specifier");
3300 parser
->scope
= NULL_TREE
;
3301 return error_mark_node
;
3307 /* Parse a class-or-namespace-name.
3309 class-or-namespace-name:
3313 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3314 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3315 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3316 TYPE_P is TRUE iff the next name should be taken as a class-name,
3317 even the same name is declared to be another entity in the same
3320 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3321 specified by the class-or-namespace-name. If neither is found the
3322 ERROR_MARK_NODE is returned. */
3325 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3326 bool typename_keyword_p
,
3327 bool template_keyword_p
,
3328 bool check_dependency_p
,
3330 bool is_declaration
)
3333 tree saved_qualifying_scope
;
3334 tree saved_object_scope
;
3338 /* Before we try to parse the class-name, we must save away the
3339 current PARSER->SCOPE since cp_parser_class_name will destroy
3341 saved_scope
= parser
->scope
;
3342 saved_qualifying_scope
= parser
->qualifying_scope
;
3343 saved_object_scope
= parser
->object_scope
;
3344 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3345 there is no need to look for a namespace-name. */
3346 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3348 cp_parser_parse_tentatively (parser
);
3349 scope
= cp_parser_class_name (parser
,
3354 /*class_head_p=*/false,
3356 /* If that didn't work, try for a namespace-name. */
3357 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3359 /* Restore the saved scope. */
3360 parser
->scope
= saved_scope
;
3361 parser
->qualifying_scope
= saved_qualifying_scope
;
3362 parser
->object_scope
= saved_object_scope
;
3363 /* If we are not looking at an identifier followed by the scope
3364 resolution operator, then this is not part of a
3365 nested-name-specifier. (Note that this function is only used
3366 to parse the components of a nested-name-specifier.) */
3367 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3368 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3369 return error_mark_node
;
3370 scope
= cp_parser_namespace_name (parser
);
3376 /* Parse a postfix-expression.
3380 postfix-expression [ expression ]
3381 postfix-expression ( expression-list [opt] )
3382 simple-type-specifier ( expression-list [opt] )
3383 typename :: [opt] nested-name-specifier identifier
3384 ( expression-list [opt] )
3385 typename :: [opt] nested-name-specifier template [opt] template-id
3386 ( expression-list [opt] )
3387 postfix-expression . template [opt] id-expression
3388 postfix-expression -> template [opt] id-expression
3389 postfix-expression . pseudo-destructor-name
3390 postfix-expression -> pseudo-destructor-name
3391 postfix-expression ++
3392 postfix-expression --
3393 dynamic_cast < type-id > ( expression )
3394 static_cast < type-id > ( expression )
3395 reinterpret_cast < type-id > ( expression )
3396 const_cast < type-id > ( expression )
3397 typeid ( expression )
3403 ( type-id ) { initializer-list , [opt] }
3405 This extension is a GNU version of the C99 compound-literal
3406 construct. (The C99 grammar uses `type-name' instead of `type-id',
3407 but they are essentially the same concept.)
3409 If ADDRESS_P is true, the postfix expression is the operand of the
3412 Returns a representation of the expression. */
3415 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3419 cp_id_kind idk
= CP_ID_KIND_NONE
;
3420 tree postfix_expression
= NULL_TREE
;
3421 /* Non-NULL only if the current postfix-expression can be used to
3422 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3423 class used to qualify the member. */
3424 tree qualifying_class
= NULL_TREE
;
3426 /* Peek at the next token. */
3427 token
= cp_lexer_peek_token (parser
->lexer
);
3428 /* Some of the productions are determined by keywords. */
3429 keyword
= token
->keyword
;
3439 const char *saved_message
;
3441 /* All of these can be handled in the same way from the point
3442 of view of parsing. Begin by consuming the token
3443 identifying the cast. */
3444 cp_lexer_consume_token (parser
->lexer
);
3446 /* New types cannot be defined in the cast. */
3447 saved_message
= parser
->type_definition_forbidden_message
;
3448 parser
->type_definition_forbidden_message
3449 = "types may not be defined in casts";
3451 /* Look for the opening `<'. */
3452 cp_parser_require (parser
, CPP_LESS
, "`<'");
3453 /* Parse the type to which we are casting. */
3454 type
= cp_parser_type_id (parser
);
3455 /* Look for the closing `>'. */
3456 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3457 /* Restore the old message. */
3458 parser
->type_definition_forbidden_message
= saved_message
;
3460 /* And the expression which is being cast. */
3461 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3462 expression
= cp_parser_expression (parser
);
3463 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3465 /* Only type conversions to integral or enumeration types
3466 can be used in constant-expressions. */
3467 if (parser
->integral_constant_expression_p
3468 && !dependent_type_p (type
)
3469 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3470 /* A cast to pointer or reference type is allowed in the
3471 implementation of "offsetof". */
3472 && !(parser
->in_offsetof_p
&& POINTER_TYPE_P (type
))
3473 && (cp_parser_non_integral_constant_expression
3475 "a cast to a type other than an integral or "
3476 "enumeration type")))
3477 return error_mark_node
;
3483 = build_dynamic_cast (type
, expression
);
3487 = build_static_cast (type
, expression
);
3491 = build_reinterpret_cast (type
, expression
);
3495 = build_const_cast (type
, expression
);
3506 const char *saved_message
;
3507 bool saved_in_type_id_in_expr_p
;
3509 /* Consume the `typeid' token. */
3510 cp_lexer_consume_token (parser
->lexer
);
3511 /* Look for the `(' token. */
3512 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3513 /* Types cannot be defined in a `typeid' expression. */
3514 saved_message
= parser
->type_definition_forbidden_message
;
3515 parser
->type_definition_forbidden_message
3516 = "types may not be defined in a `typeid\' expression";
3517 /* We can't be sure yet whether we're looking at a type-id or an
3519 cp_parser_parse_tentatively (parser
);
3520 /* Try a type-id first. */
3521 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3522 parser
->in_type_id_in_expr_p
= true;
3523 type
= cp_parser_type_id (parser
);
3524 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3525 /* Look for the `)' token. Otherwise, we can't be sure that
3526 we're not looking at an expression: consider `typeid (int
3527 (3))', for example. */
3528 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3529 /* If all went well, simply lookup the type-id. */
3530 if (cp_parser_parse_definitely (parser
))
3531 postfix_expression
= get_typeid (type
);
3532 /* Otherwise, fall back to the expression variant. */
3537 /* Look for an expression. */
3538 expression
= cp_parser_expression (parser
);
3539 /* Compute its typeid. */
3540 postfix_expression
= build_typeid (expression
);
3541 /* Look for the `)' token. */
3542 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3544 /* `typeid' may not appear in an integral constant expression. */
3545 if (cp_parser_non_integral_constant_expression(parser
,
3546 "`typeid' operator"))
3547 return error_mark_node
;
3548 /* Restore the saved message. */
3549 parser
->type_definition_forbidden_message
= saved_message
;
3557 /* The syntax permitted here is the same permitted for an
3558 elaborated-type-specifier. */
3559 type
= cp_parser_elaborated_type_specifier (parser
,
3560 /*is_friend=*/false,
3561 /*is_declaration=*/false);
3562 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3570 /* If the next thing is a simple-type-specifier, we may be
3571 looking at a functional cast. We could also be looking at
3572 an id-expression. So, we try the functional cast, and if
3573 that doesn't work we fall back to the primary-expression. */
3574 cp_parser_parse_tentatively (parser
);
3575 /* Look for the simple-type-specifier. */
3576 type
= cp_parser_simple_type_specifier (parser
,
3577 CP_PARSER_FLAGS_NONE
,
3578 /*identifier_p=*/false);
3579 /* Parse the cast itself. */
3580 if (!cp_parser_error_occurred (parser
))
3582 = cp_parser_functional_cast (parser
, type
);
3583 /* If that worked, we're done. */
3584 if (cp_parser_parse_definitely (parser
))
3587 /* If the functional-cast didn't work out, try a
3588 compound-literal. */
3589 if (cp_parser_allow_gnu_extensions_p (parser
)
3590 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3592 tree initializer_list
= NULL_TREE
;
3593 bool saved_in_type_id_in_expr_p
;
3595 cp_parser_parse_tentatively (parser
);
3596 /* Consume the `('. */
3597 cp_lexer_consume_token (parser
->lexer
);
3598 /* Parse the type. */
3599 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3600 parser
->in_type_id_in_expr_p
= true;
3601 type
= cp_parser_type_id (parser
);
3602 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3603 /* Look for the `)'. */
3604 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3605 /* Look for the `{'. */
3606 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3607 /* If things aren't going well, there's no need to
3609 if (!cp_parser_error_occurred (parser
))
3611 bool non_constant_p
;
3612 /* Parse the initializer-list. */
3614 = cp_parser_initializer_list (parser
, &non_constant_p
);
3615 /* Allow a trailing `,'. */
3616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3617 cp_lexer_consume_token (parser
->lexer
);
3618 /* Look for the final `}'. */
3619 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3621 /* If that worked, we're definitely looking at a
3622 compound-literal expression. */
3623 if (cp_parser_parse_definitely (parser
))
3625 /* Warn the user that a compound literal is not
3626 allowed in standard C++. */
3628 pedwarn ("ISO C++ forbids compound-literals");
3629 /* Form the representation of the compound-literal. */
3631 = finish_compound_literal (type
, initializer_list
);
3636 /* It must be a primary-expression. */
3637 postfix_expression
= cp_parser_primary_expression (parser
,
3644 /* If we were avoiding committing to the processing of a
3645 qualified-id until we knew whether or not we had a
3646 pointer-to-member, we now know. */
3647 if (qualifying_class
)
3651 /* Peek at the next token. */
3652 token
= cp_lexer_peek_token (parser
->lexer
);
3653 done
= (token
->type
!= CPP_OPEN_SQUARE
3654 && token
->type
!= CPP_OPEN_PAREN
3655 && token
->type
!= CPP_DOT
3656 && token
->type
!= CPP_DEREF
3657 && token
->type
!= CPP_PLUS_PLUS
3658 && token
->type
!= CPP_MINUS_MINUS
);
3660 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3665 return postfix_expression
;
3668 /* Keep looping until the postfix-expression is complete. */
3671 if (idk
== CP_ID_KIND_UNQUALIFIED
3672 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3673 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3674 /* It is not a Koenig lookup function call. */
3676 = unqualified_name_lookup_error (postfix_expression
);
3678 /* Peek at the next token. */
3679 token
= cp_lexer_peek_token (parser
->lexer
);
3681 switch (token
->type
)
3683 case CPP_OPEN_SQUARE
:
3684 /* postfix-expression [ expression ] */
3688 /* Consume the `[' token. */
3689 cp_lexer_consume_token (parser
->lexer
);
3690 /* Parse the index expression. */
3691 index
= cp_parser_expression (parser
);
3692 /* Look for the closing `]'. */
3693 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
3695 /* Build the ARRAY_REF. */
3697 = grok_array_decl (postfix_expression
, index
);
3698 idk
= CP_ID_KIND_NONE
;
3699 /* Array references are not permitted in
3700 constant-expressions (but they are allowed
3702 if (!parser
->in_offsetof_p
3703 && cp_parser_non_integral_constant_expression
3704 (parser
, "an array reference"))
3705 postfix_expression
= error_mark_node
;
3709 case CPP_OPEN_PAREN
:
3710 /* postfix-expression ( expression-list [opt] ) */
3713 tree args
= (cp_parser_parenthesized_expression_list
3714 (parser
, false, /*non_constant_p=*/NULL
));
3716 if (args
== error_mark_node
)
3718 postfix_expression
= error_mark_node
;
3722 /* Function calls are not permitted in
3723 constant-expressions. */
3724 if (cp_parser_non_integral_constant_expression (parser
,
3727 postfix_expression
= error_mark_node
;
3732 if (idk
== CP_ID_KIND_UNQUALIFIED
)
3734 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3740 = perform_koenig_lookup (postfix_expression
, args
);
3744 = unqualified_fn_lookup_error (postfix_expression
);
3746 /* We do not perform argument-dependent lookup if
3747 normal lookup finds a non-function, in accordance
3748 with the expected resolution of DR 218. */
3749 else if (args
&& is_overloaded_fn (postfix_expression
))
3751 tree fn
= get_first_fn (postfix_expression
);
3752 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
3753 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
3754 /* Only do argument dependent lookup if regular
3755 lookup does not find a set of member functions.
3756 [basic.lookup.koenig]/2a */
3757 if (!DECL_FUNCTION_MEMBER_P (fn
))
3761 = perform_koenig_lookup (postfix_expression
, args
);
3766 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
3768 tree instance
= TREE_OPERAND (postfix_expression
, 0);
3769 tree fn
= TREE_OPERAND (postfix_expression
, 1);
3771 if (processing_template_decl
3772 && (type_dependent_expression_p (instance
)
3773 || (!BASELINK_P (fn
)
3774 && TREE_CODE (fn
) != FIELD_DECL
)
3775 || type_dependent_expression_p (fn
)
3776 || any_type_dependent_arguments_p (args
)))
3779 = build_min_nt (CALL_EXPR
, postfix_expression
, args
);
3783 if (BASELINK_P (fn
))
3785 = (build_new_method_call
3786 (instance
, fn
, args
, NULL_TREE
,
3787 (idk
== CP_ID_KIND_QUALIFIED
3788 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3791 = finish_call_expr (postfix_expression
, args
,
3792 /*disallow_virtual=*/false,
3793 /*koenig_p=*/false);
3795 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
3796 || TREE_CODE (postfix_expression
) == MEMBER_REF
3797 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
3798 postfix_expression
= (build_offset_ref_call_from_tree
3799 (postfix_expression
, args
));
3800 else if (idk
== CP_ID_KIND_QUALIFIED
)
3801 /* A call to a static class member, or a namespace-scope
3804 = finish_call_expr (postfix_expression
, args
,
3805 /*disallow_virtual=*/true,
3808 /* All other function calls. */
3810 = finish_call_expr (postfix_expression
, args
,
3811 /*disallow_virtual=*/false,
3814 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3815 idk
= CP_ID_KIND_NONE
;
3821 /* postfix-expression . template [opt] id-expression
3822 postfix-expression . pseudo-destructor-name
3823 postfix-expression -> template [opt] id-expression
3824 postfix-expression -> pseudo-destructor-name */
3829 bool pseudo_destructor_p
;
3830 tree scope
= NULL_TREE
;
3831 enum cpp_ttype token_type
= token
->type
;
3833 /* If this is a `->' operator, dereference the pointer. */
3834 if (token
->type
== CPP_DEREF
)
3835 postfix_expression
= build_x_arrow (postfix_expression
);
3836 /* Check to see whether or not the expression is
3838 dependent_p
= type_dependent_expression_p (postfix_expression
);
3839 /* The identifier following the `->' or `.' is not
3841 parser
->scope
= NULL_TREE
;
3842 parser
->qualifying_scope
= NULL_TREE
;
3843 parser
->object_scope
= NULL_TREE
;
3844 idk
= CP_ID_KIND_NONE
;
3845 /* Enter the scope corresponding to the type of the object
3846 given by the POSTFIX_EXPRESSION. */
3848 && TREE_TYPE (postfix_expression
) != NULL_TREE
)
3850 scope
= TREE_TYPE (postfix_expression
);
3851 /* According to the standard, no expression should
3852 ever have reference type. Unfortunately, we do not
3853 currently match the standard in this respect in
3854 that our internal representation of an expression
3855 may have reference type even when the standard says
3856 it does not. Therefore, we have to manually obtain
3857 the underlying type here. */
3858 scope
= non_reference (scope
);
3859 /* The type of the POSTFIX_EXPRESSION must be
3861 scope
= complete_type_or_else (scope
, NULL_TREE
);
3862 /* Let the name lookup machinery know that we are
3863 processing a class member access expression. */
3864 parser
->context
->object_type
= scope
;
3865 /* If something went wrong, we want to be able to
3866 discern that case, as opposed to the case where
3867 there was no SCOPE due to the type of expression
3870 scope
= error_mark_node
;
3871 /* If the SCOPE was erroneous, make the various
3872 semantic analysis functions exit quickly -- and
3873 without issuing additional error messages. */
3874 if (scope
== error_mark_node
)
3875 postfix_expression
= error_mark_node
;
3878 /* Consume the `.' or `->' operator. */
3879 cp_lexer_consume_token (parser
->lexer
);
3881 /* Assume this expression is not a pseudo-destructor access. */
3882 pseudo_destructor_p
= false;
3884 /* If the SCOPE is a scalar type, then, if this is a valid program,
3885 we must be looking at a pseudo-destructor-name. */
3886 if (scope
&& SCALAR_TYPE_P (scope
))
3891 cp_parser_parse_tentatively (parser
);
3892 /* Parse the pseudo-destructor-name. */
3893 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
3894 if (cp_parser_parse_definitely (parser
))
3896 pseudo_destructor_p
= true;
3898 = finish_pseudo_destructor_expr (postfix_expression
,
3899 s
, TREE_TYPE (type
));
3903 if (!pseudo_destructor_p
)
3905 /* If the SCOPE is not a scalar type, we are looking
3906 at an ordinary class member access expression,
3907 rather than a pseudo-destructor-name. */
3908 template_p
= cp_parser_optional_template_keyword (parser
);
3909 /* Parse the id-expression. */
3910 name
= cp_parser_id_expression (parser
,
3912 /*check_dependency_p=*/true,
3913 /*template_p=*/NULL
,
3914 /*declarator_p=*/false);
3915 /* In general, build a SCOPE_REF if the member name is
3916 qualified. However, if the name was not dependent
3917 and has already been resolved; there is no need to
3918 build the SCOPE_REF. For example;
3920 struct X { void f(); };
3921 template <typename T> void f(T* t) { t->X::f(); }
3923 Even though "t" is dependent, "X::f" is not and has
3924 been resolved to a BASELINK; there is no need to
3925 include scope information. */
3927 /* But we do need to remember that there was an explicit
3928 scope for virtual function calls. */
3930 idk
= CP_ID_KIND_QUALIFIED
;
3932 /* If the name is a template-id that names a type, we will
3933 get a TYPE_DECL here. That is invalid code. */
3934 if (TREE_CODE (name
) == TYPE_DECL
)
3936 error ("invalid use of `%D'", name
);
3937 postfix_expression
= error_mark_node
;
3941 if (name
!= error_mark_node
&& !BASELINK_P (name
)
3944 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
3945 parser
->scope
= NULL_TREE
;
3946 parser
->qualifying_scope
= NULL_TREE
;
3947 parser
->object_scope
= NULL_TREE
;
3949 if (scope
&& name
&& BASELINK_P (name
))
3950 adjust_result_of_qualified_name_lookup
3951 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
3952 postfix_expression
= finish_class_member_access_expr
3953 (postfix_expression
, name
);
3957 /* We no longer need to look up names in the scope of the
3958 object on the left-hand side of the `.' or `->'
3960 parser
->context
->object_type
= NULL_TREE
;
3961 /* These operators may not appear in constant-expressions. */
3962 if (/* The "->" operator is allowed in the implementation
3963 of "offsetof". The "." operator may appear in the
3964 name of the member. */
3965 !parser
->in_offsetof_p
3966 && (cp_parser_non_integral_constant_expression
3968 token_type
== CPP_DEREF
? "'->'" : "`.'")))
3969 postfix_expression
= error_mark_node
;
3974 /* postfix-expression ++ */
3975 /* Consume the `++' token. */
3976 cp_lexer_consume_token (parser
->lexer
);
3977 /* Generate a representation for the complete expression. */
3979 = finish_increment_expr (postfix_expression
,
3980 POSTINCREMENT_EXPR
);
3981 /* Increments may not appear in constant-expressions. */
3982 if (cp_parser_non_integral_constant_expression (parser
,
3984 postfix_expression
= error_mark_node
;
3985 idk
= CP_ID_KIND_NONE
;
3988 case CPP_MINUS_MINUS
:
3989 /* postfix-expression -- */
3990 /* Consume the `--' token. */
3991 cp_lexer_consume_token (parser
->lexer
);
3992 /* Generate a representation for the complete expression. */
3994 = finish_increment_expr (postfix_expression
,
3995 POSTDECREMENT_EXPR
);
3996 /* Decrements may not appear in constant-expressions. */
3997 if (cp_parser_non_integral_constant_expression (parser
,
3999 postfix_expression
= error_mark_node
;
4000 idk
= CP_ID_KIND_NONE
;
4004 return postfix_expression
;
4008 /* We should never get here. */
4010 return error_mark_node
;
4013 /* Parse a parenthesized expression-list.
4016 assignment-expression
4017 expression-list, assignment-expression
4022 identifier, expression-list
4024 Returns a TREE_LIST. The TREE_VALUE of each node is a
4025 representation of an assignment-expression. Note that a TREE_LIST
4026 is returned even if there is only a single expression in the list.
4027 error_mark_node is returned if the ( and or ) are
4028 missing. NULL_TREE is returned on no expressions. The parentheses
4029 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4030 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4031 indicates whether or not all of the expressions in the list were
4035 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4036 bool is_attribute_list
,
4037 bool *non_constant_p
)
4039 tree expression_list
= NULL_TREE
;
4040 tree identifier
= NULL_TREE
;
4042 /* Assume all the expressions will be constant. */
4044 *non_constant_p
= false;
4046 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4047 return error_mark_node
;
4049 /* Consume expressions until there are no more. */
4050 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4055 /* At the beginning of attribute lists, check to see if the
4056 next token is an identifier. */
4057 if (is_attribute_list
4058 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4062 /* Consume the identifier. */
4063 token
= cp_lexer_consume_token (parser
->lexer
);
4064 /* Save the identifier. */
4065 identifier
= token
->value
;
4069 /* Parse the next assignment-expression. */
4072 bool expr_non_constant_p
;
4073 expr
= (cp_parser_constant_expression
4074 (parser
, /*allow_non_constant_p=*/true,
4075 &expr_non_constant_p
));
4076 if (expr_non_constant_p
)
4077 *non_constant_p
= true;
4080 expr
= cp_parser_assignment_expression (parser
);
4082 /* Add it to the list. We add error_mark_node
4083 expressions to the list, so that we can still tell if
4084 the correct form for a parenthesized expression-list
4085 is found. That gives better errors. */
4086 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4088 if (expr
== error_mark_node
)
4092 /* After the first item, attribute lists look the same as
4093 expression lists. */
4094 is_attribute_list
= false;
4097 /* If the next token isn't a `,', then we are done. */
4098 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4101 /* Otherwise, consume the `,' and keep going. */
4102 cp_lexer_consume_token (parser
->lexer
);
4105 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4110 /* We try and resync to an unnested comma, as that will give the
4111 user better diagnostics. */
4112 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4113 /*recovering=*/true,
4115 /*consume_paren=*/true);
4119 return error_mark_node
;
4122 /* We built up the list in reverse order so we must reverse it now. */
4123 expression_list
= nreverse (expression_list
);
4125 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4127 return expression_list
;
4130 /* Parse a pseudo-destructor-name.
4132 pseudo-destructor-name:
4133 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4134 :: [opt] nested-name-specifier template template-id :: ~ type-name
4135 :: [opt] nested-name-specifier [opt] ~ type-name
4137 If either of the first two productions is used, sets *SCOPE to the
4138 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4139 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4140 or ERROR_MARK_NODE if the parse fails. */
4143 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4147 bool nested_name_specifier_p
;
4149 /* Look for the optional `::' operator. */
4150 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4151 /* Look for the optional nested-name-specifier. */
4152 nested_name_specifier_p
4153 = (cp_parser_nested_name_specifier_opt (parser
,
4154 /*typename_keyword_p=*/false,
4155 /*check_dependency_p=*/true,
4157 /*is_declaration=*/true)
4159 /* Now, if we saw a nested-name-specifier, we might be doing the
4160 second production. */
4161 if (nested_name_specifier_p
4162 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4164 /* Consume the `template' keyword. */
4165 cp_lexer_consume_token (parser
->lexer
);
4166 /* Parse the template-id. */
4167 cp_parser_template_id (parser
,
4168 /*template_keyword_p=*/true,
4169 /*check_dependency_p=*/false,
4170 /*is_declaration=*/true);
4171 /* Look for the `::' token. */
4172 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4174 /* If the next token is not a `~', then there might be some
4175 additional qualification. */
4176 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4178 /* Look for the type-name. */
4179 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4181 /* If we didn't get an aggregate type, or we don't have ::~,
4182 then something has gone wrong. Since the only caller of this
4183 function is looking for something after `.' or `->' after a
4184 scalar type, most likely the program is trying to get a
4185 member of a non-aggregate type. */
4186 if (*scope
== error_mark_node
4187 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4188 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4190 cp_parser_error (parser
, "request for member of non-aggregate type");
4191 *type
= error_mark_node
;
4195 /* Look for the `::' token. */
4196 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4201 /* Look for the `~'. */
4202 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4203 /* Look for the type-name again. We are not responsible for
4204 checking that it matches the first type-name. */
4205 *type
= cp_parser_type_name (parser
);
4208 /* Parse a unary-expression.
4214 unary-operator cast-expression
4215 sizeof unary-expression
4223 __extension__ cast-expression
4224 __alignof__ unary-expression
4225 __alignof__ ( type-id )
4226 __real__ cast-expression
4227 __imag__ cast-expression
4230 ADDRESS_P is true iff the unary-expression is appearing as the
4231 operand of the `&' operator.
4233 Returns a representation of the expression. */
4236 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4239 enum tree_code unary_operator
;
4241 /* Peek at the next token. */
4242 token
= cp_lexer_peek_token (parser
->lexer
);
4243 /* Some keywords give away the kind of expression. */
4244 if (token
->type
== CPP_KEYWORD
)
4246 enum rid keyword
= token
->keyword
;
4256 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4257 /* Consume the token. */
4258 cp_lexer_consume_token (parser
->lexer
);
4259 /* Parse the operand. */
4260 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4262 if (TYPE_P (operand
))
4263 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4265 return cxx_sizeof_or_alignof_expr (operand
, op
);
4269 return cp_parser_new_expression (parser
);
4272 return cp_parser_delete_expression (parser
);
4276 /* The saved value of the PEDANTIC flag. */
4280 /* Save away the PEDANTIC flag. */
4281 cp_parser_extension_opt (parser
, &saved_pedantic
);
4282 /* Parse the cast-expression. */
4283 expr
= cp_parser_simple_cast_expression (parser
);
4284 /* Restore the PEDANTIC flag. */
4285 pedantic
= saved_pedantic
;
4295 /* Consume the `__real__' or `__imag__' token. */
4296 cp_lexer_consume_token (parser
->lexer
);
4297 /* Parse the cast-expression. */
4298 expression
= cp_parser_simple_cast_expression (parser
);
4299 /* Create the complete representation. */
4300 return build_x_unary_op ((keyword
== RID_REALPART
4301 ? REALPART_EXPR
: IMAGPART_EXPR
),
4311 /* Look for the `:: new' and `:: delete', which also signal the
4312 beginning of a new-expression, or delete-expression,
4313 respectively. If the next token is `::', then it might be one of
4315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4319 /* See if the token after the `::' is one of the keywords in
4320 which we're interested. */
4321 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4322 /* If it's `new', we have a new-expression. */
4323 if (keyword
== RID_NEW
)
4324 return cp_parser_new_expression (parser
);
4325 /* Similarly, for `delete'. */
4326 else if (keyword
== RID_DELETE
)
4327 return cp_parser_delete_expression (parser
);
4330 /* Look for a unary operator. */
4331 unary_operator
= cp_parser_unary_operator (token
);
4332 /* The `++' and `--' operators can be handled similarly, even though
4333 they are not technically unary-operators in the grammar. */
4334 if (unary_operator
== ERROR_MARK
)
4336 if (token
->type
== CPP_PLUS_PLUS
)
4337 unary_operator
= PREINCREMENT_EXPR
;
4338 else if (token
->type
== CPP_MINUS_MINUS
)
4339 unary_operator
= PREDECREMENT_EXPR
;
4340 /* Handle the GNU address-of-label extension. */
4341 else if (cp_parser_allow_gnu_extensions_p (parser
)
4342 && token
->type
== CPP_AND_AND
)
4346 /* Consume the '&&' token. */
4347 cp_lexer_consume_token (parser
->lexer
);
4348 /* Look for the identifier. */
4349 identifier
= cp_parser_identifier (parser
);
4350 /* Create an expression representing the address. */
4351 return finish_label_address_expr (identifier
);
4354 if (unary_operator
!= ERROR_MARK
)
4356 tree cast_expression
;
4357 tree expression
= error_mark_node
;
4358 const char *non_constant_p
= NULL
;
4360 /* Consume the operator token. */
4361 token
= cp_lexer_consume_token (parser
->lexer
);
4362 /* Parse the cast-expression. */
4364 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4365 /* Now, build an appropriate representation. */
4366 switch (unary_operator
)
4369 non_constant_p
= "`*'";
4370 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4374 /* The "&" operator is allowed in the implementation of
4376 if (!parser
->in_offsetof_p
)
4377 non_constant_p
= "`&'";
4380 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4383 case PREINCREMENT_EXPR
:
4384 case PREDECREMENT_EXPR
:
4385 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4390 case TRUTH_NOT_EXPR
:
4391 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4399 && cp_parser_non_integral_constant_expression (parser
,
4401 expression
= error_mark_node
;
4406 return cp_parser_postfix_expression (parser
, address_p
);
4409 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4410 unary-operator, the corresponding tree code is returned. */
4412 static enum tree_code
4413 cp_parser_unary_operator (cp_token
* token
)
4415 switch (token
->type
)
4418 return INDIRECT_REF
;
4424 return CONVERT_EXPR
;
4430 return TRUTH_NOT_EXPR
;
4433 return BIT_NOT_EXPR
;
4440 /* Parse a new-expression.
4443 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4444 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4446 Returns a representation of the expression. */
4449 cp_parser_new_expression (cp_parser
* parser
)
4451 bool global_scope_p
;
4456 /* Look for the optional `::' operator. */
4458 = (cp_parser_global_scope_opt (parser
,
4459 /*current_scope_valid_p=*/false)
4461 /* Look for the `new' operator. */
4462 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4463 /* There's no easy way to tell a new-placement from the
4464 `( type-id )' construct. */
4465 cp_parser_parse_tentatively (parser
);
4466 /* Look for a new-placement. */
4467 placement
= cp_parser_new_placement (parser
);
4468 /* If that didn't work out, there's no new-placement. */
4469 if (!cp_parser_parse_definitely (parser
))
4470 placement
= NULL_TREE
;
4472 /* If the next token is a `(', then we have a parenthesized
4474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4476 /* Consume the `('. */
4477 cp_lexer_consume_token (parser
->lexer
);
4478 /* Parse the type-id. */
4479 type
= cp_parser_type_id (parser
);
4480 /* Look for the closing `)'. */
4481 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4482 /* There should not be a direct-new-declarator in this production,
4483 but GCC used to allowed this, so we check and emit a sensible error
4484 message for this case. */
4485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4487 error ("array bound forbidden after parenthesized type-id");
4488 inform ("try removing the parentheses around the type-id");
4489 cp_parser_direct_new_declarator (parser
);
4492 /* Otherwise, there must be a new-type-id. */
4494 type
= cp_parser_new_type_id (parser
);
4496 /* If the next token is a `(', then we have a new-initializer. */
4497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4498 initializer
= cp_parser_new_initializer (parser
);
4500 initializer
= NULL_TREE
;
4502 /* A new-expression may not appear in an integral constant
4504 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
4505 return error_mark_node
;
4507 /* Create a representation of the new-expression. */
4508 return build_new (placement
, type
, initializer
, global_scope_p
);
4511 /* Parse a new-placement.
4516 Returns the same representation as for an expression-list. */
4519 cp_parser_new_placement (cp_parser
* parser
)
4521 tree expression_list
;
4523 /* Parse the expression-list. */
4524 expression_list
= (cp_parser_parenthesized_expression_list
4525 (parser
, false, /*non_constant_p=*/NULL
));
4527 return expression_list
;
4530 /* Parse a new-type-id.
4533 type-specifier-seq new-declarator [opt]
4535 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4536 and whose TREE_VALUE is the new-declarator. */
4539 cp_parser_new_type_id (cp_parser
* parser
)
4541 tree type_specifier_seq
;
4543 const char *saved_message
;
4545 /* The type-specifier sequence must not contain type definitions.
4546 (It cannot contain declarations of new types either, but if they
4547 are not definitions we will catch that because they are not
4549 saved_message
= parser
->type_definition_forbidden_message
;
4550 parser
->type_definition_forbidden_message
4551 = "types may not be defined in a new-type-id";
4552 /* Parse the type-specifier-seq. */
4553 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4554 /* Restore the old message. */
4555 parser
->type_definition_forbidden_message
= saved_message
;
4556 /* Parse the new-declarator. */
4557 declarator
= cp_parser_new_declarator_opt (parser
);
4559 return build_tree_list (type_specifier_seq
, declarator
);
4562 /* Parse an (optional) new-declarator.
4565 ptr-operator new-declarator [opt]
4566 direct-new-declarator
4568 Returns a representation of the declarator. See
4569 cp_parser_declarator for the representations used. */
4572 cp_parser_new_declarator_opt (cp_parser
* parser
)
4574 enum tree_code code
;
4576 tree cv_qualifier_seq
;
4578 /* We don't know if there's a ptr-operator next, or not. */
4579 cp_parser_parse_tentatively (parser
);
4580 /* Look for a ptr-operator. */
4581 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4582 /* If that worked, look for more new-declarators. */
4583 if (cp_parser_parse_definitely (parser
))
4587 /* Parse another optional declarator. */
4588 declarator
= cp_parser_new_declarator_opt (parser
);
4590 /* Create the representation of the declarator. */
4591 if (code
== INDIRECT_REF
)
4592 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4595 declarator
= make_reference_declarator (cv_qualifier_seq
,
4598 /* Handle the pointer-to-member case. */
4600 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4605 /* If the next token is a `[', there is a direct-new-declarator. */
4606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4607 return cp_parser_direct_new_declarator (parser
);
4612 /* Parse a direct-new-declarator.
4614 direct-new-declarator:
4616 direct-new-declarator [constant-expression]
4618 Returns an ARRAY_REF, following the same conventions as are
4619 documented for cp_parser_direct_declarator. */
4622 cp_parser_direct_new_declarator (cp_parser
* parser
)
4624 tree declarator
= NULL_TREE
;
4630 /* Look for the opening `['. */
4631 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4632 /* The first expression is not required to be constant. */
4635 expression
= cp_parser_expression (parser
);
4636 /* The standard requires that the expression have integral
4637 type. DR 74 adds enumeration types. We believe that the
4638 real intent is that these expressions be handled like the
4639 expression in a `switch' condition, which also allows
4640 classes with a single conversion to integral or
4641 enumeration type. */
4642 if (!processing_template_decl
)
4645 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4650 error ("expression in new-declarator must have integral or enumeration type");
4651 expression
= error_mark_node
;
4655 /* But all the other expressions must be. */
4658 = cp_parser_constant_expression (parser
,
4659 /*allow_non_constant=*/false,
4661 /* Look for the closing `]'. */
4662 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4664 /* Add this bound to the declarator. */
4665 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4667 /* If the next token is not a `[', then there are no more
4669 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4676 /* Parse a new-initializer.
4679 ( expression-list [opt] )
4681 Returns a representation of the expression-list. If there is no
4682 expression-list, VOID_ZERO_NODE is returned. */
4685 cp_parser_new_initializer (cp_parser
* parser
)
4687 tree expression_list
;
4689 expression_list
= (cp_parser_parenthesized_expression_list
4690 (parser
, false, /*non_constant_p=*/NULL
));
4691 if (!expression_list
)
4692 expression_list
= void_zero_node
;
4694 return expression_list
;
4697 /* Parse a delete-expression.
4700 :: [opt] delete cast-expression
4701 :: [opt] delete [ ] cast-expression
4703 Returns a representation of the expression. */
4706 cp_parser_delete_expression (cp_parser
* parser
)
4708 bool global_scope_p
;
4712 /* Look for the optional `::' operator. */
4714 = (cp_parser_global_scope_opt (parser
,
4715 /*current_scope_valid_p=*/false)
4717 /* Look for the `delete' keyword. */
4718 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4719 /* See if the array syntax is in use. */
4720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4722 /* Consume the `[' token. */
4723 cp_lexer_consume_token (parser
->lexer
);
4724 /* Look for the `]' token. */
4725 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4726 /* Remember that this is the `[]' construct. */
4732 /* Parse the cast-expression. */
4733 expression
= cp_parser_simple_cast_expression (parser
);
4735 /* A delete-expression may not appear in an integral constant
4737 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
4738 return error_mark_node
;
4740 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4743 /* Parse a cast-expression.
4747 ( type-id ) cast-expression
4749 Returns a representation of the expression. */
4752 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4754 /* If it's a `(', then we might be looking at a cast. */
4755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4757 tree type
= NULL_TREE
;
4758 tree expr
= NULL_TREE
;
4759 bool compound_literal_p
;
4760 const char *saved_message
;
4762 /* There's no way to know yet whether or not this is a cast.
4763 For example, `(int (3))' is a unary-expression, while `(int)
4764 3' is a cast. So, we resort to parsing tentatively. */
4765 cp_parser_parse_tentatively (parser
);
4766 /* Types may not be defined in a cast. */
4767 saved_message
= parser
->type_definition_forbidden_message
;
4768 parser
->type_definition_forbidden_message
4769 = "types may not be defined in casts";
4770 /* Consume the `('. */
4771 cp_lexer_consume_token (parser
->lexer
);
4772 /* A very tricky bit is that `(struct S) { 3 }' is a
4773 compound-literal (which we permit in C++ as an extension).
4774 But, that construct is not a cast-expression -- it is a
4775 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4776 is legal; if the compound-literal were a cast-expression,
4777 you'd need an extra set of parentheses.) But, if we parse
4778 the type-id, and it happens to be a class-specifier, then we
4779 will commit to the parse at that point, because we cannot
4780 undo the action that is done when creating a new class. So,
4781 then we cannot back up and do a postfix-expression.
4783 Therefore, we scan ahead to the closing `)', and check to see
4784 if the token after the `)' is a `{'. If so, we are not
4785 looking at a cast-expression.
4787 Save tokens so that we can put them back. */
4788 cp_lexer_save_tokens (parser
->lexer
);
4789 /* Skip tokens until the next token is a closing parenthesis.
4790 If we find the closing `)', and the next token is a `{', then
4791 we are looking at a compound-literal. */
4793 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
4794 /*consume_paren=*/true)
4795 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4796 /* Roll back the tokens we skipped. */
4797 cp_lexer_rollback_tokens (parser
->lexer
);
4798 /* If we were looking at a compound-literal, simulate an error
4799 so that the call to cp_parser_parse_definitely below will
4801 if (compound_literal_p
)
4802 cp_parser_simulate_error (parser
);
4805 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4806 parser
->in_type_id_in_expr_p
= true;
4807 /* Look for the type-id. */
4808 type
= cp_parser_type_id (parser
);
4809 /* Look for the closing `)'. */
4810 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4811 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4814 /* Restore the saved message. */
4815 parser
->type_definition_forbidden_message
= saved_message
;
4817 /* If ok so far, parse the dependent expression. We cannot be
4818 sure it is a cast. Consider `(T ())'. It is a parenthesized
4819 ctor of T, but looks like a cast to function returning T
4820 without a dependent expression. */
4821 if (!cp_parser_error_occurred (parser
))
4822 expr
= cp_parser_simple_cast_expression (parser
);
4824 if (cp_parser_parse_definitely (parser
))
4826 /* Warn about old-style casts, if so requested. */
4827 if (warn_old_style_cast
4828 && !in_system_header
4829 && !VOID_TYPE_P (type
)
4830 && current_lang_name
!= lang_name_c
)
4831 warning ("use of old-style cast");
4833 /* Only type conversions to integral or enumeration types
4834 can be used in constant-expressions. */
4835 if (parser
->integral_constant_expression_p
4836 && !dependent_type_p (type
)
4837 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
4838 && (cp_parser_non_integral_constant_expression
4840 "a casts to a type other than an integral or "
4841 "enumeration type")))
4842 return error_mark_node
;
4844 /* Perform the cast. */
4845 expr
= build_c_cast (type
, expr
);
4850 /* If we get here, then it's not a cast, so it must be a
4851 unary-expression. */
4852 return cp_parser_unary_expression (parser
, address_p
);
4855 /* Parse a pm-expression.
4859 pm-expression .* cast-expression
4860 pm-expression ->* cast-expression
4862 Returns a representation of the expression. */
4865 cp_parser_pm_expression (cp_parser
* parser
)
4867 static const cp_parser_token_tree_map map
= {
4868 { CPP_DEREF_STAR
, MEMBER_REF
},
4869 { CPP_DOT_STAR
, DOTSTAR_EXPR
},
4870 { CPP_EOF
, ERROR_MARK
}
4873 return cp_parser_binary_expression (parser
, map
,
4874 cp_parser_simple_cast_expression
);
4877 /* Parse a multiplicative-expression.
4879 mulitplicative-expression:
4881 multiplicative-expression * pm-expression
4882 multiplicative-expression / pm-expression
4883 multiplicative-expression % pm-expression
4885 Returns a representation of the expression. */
4888 cp_parser_multiplicative_expression (cp_parser
* parser
)
4890 static const cp_parser_token_tree_map map
= {
4891 { CPP_MULT
, MULT_EXPR
},
4892 { CPP_DIV
, TRUNC_DIV_EXPR
},
4893 { CPP_MOD
, TRUNC_MOD_EXPR
},
4894 { CPP_EOF
, ERROR_MARK
}
4897 return cp_parser_binary_expression (parser
,
4899 cp_parser_pm_expression
);
4902 /* Parse an additive-expression.
4904 additive-expression:
4905 multiplicative-expression
4906 additive-expression + multiplicative-expression
4907 additive-expression - multiplicative-expression
4909 Returns a representation of the expression. */
4912 cp_parser_additive_expression (cp_parser
* parser
)
4914 static const cp_parser_token_tree_map map
= {
4915 { CPP_PLUS
, PLUS_EXPR
},
4916 { CPP_MINUS
, MINUS_EXPR
},
4917 { CPP_EOF
, ERROR_MARK
}
4920 return cp_parser_binary_expression (parser
,
4922 cp_parser_multiplicative_expression
);
4925 /* Parse a shift-expression.
4929 shift-expression << additive-expression
4930 shift-expression >> additive-expression
4932 Returns a representation of the expression. */
4935 cp_parser_shift_expression (cp_parser
* parser
)
4937 static const cp_parser_token_tree_map map
= {
4938 { CPP_LSHIFT
, LSHIFT_EXPR
},
4939 { CPP_RSHIFT
, RSHIFT_EXPR
},
4940 { CPP_EOF
, ERROR_MARK
}
4943 return cp_parser_binary_expression (parser
,
4945 cp_parser_additive_expression
);
4948 /* Parse a relational-expression.
4950 relational-expression:
4952 relational-expression < shift-expression
4953 relational-expression > shift-expression
4954 relational-expression <= shift-expression
4955 relational-expression >= shift-expression
4959 relational-expression:
4960 relational-expression <? shift-expression
4961 relational-expression >? shift-expression
4963 Returns a representation of the expression. */
4966 cp_parser_relational_expression (cp_parser
* parser
)
4968 static const cp_parser_token_tree_map map
= {
4969 { CPP_LESS
, LT_EXPR
},
4970 { CPP_GREATER
, GT_EXPR
},
4971 { CPP_LESS_EQ
, LE_EXPR
},
4972 { CPP_GREATER_EQ
, GE_EXPR
},
4973 { CPP_MIN
, MIN_EXPR
},
4974 { CPP_MAX
, MAX_EXPR
},
4975 { CPP_EOF
, ERROR_MARK
}
4978 return cp_parser_binary_expression (parser
,
4980 cp_parser_shift_expression
);
4983 /* Parse an equality-expression.
4985 equality-expression:
4986 relational-expression
4987 equality-expression == relational-expression
4988 equality-expression != relational-expression
4990 Returns a representation of the expression. */
4993 cp_parser_equality_expression (cp_parser
* parser
)
4995 static const cp_parser_token_tree_map map
= {
4996 { CPP_EQ_EQ
, EQ_EXPR
},
4997 { CPP_NOT_EQ
, NE_EXPR
},
4998 { CPP_EOF
, ERROR_MARK
}
5001 return cp_parser_binary_expression (parser
,
5003 cp_parser_relational_expression
);
5006 /* Parse an and-expression.
5010 and-expression & equality-expression
5012 Returns a representation of the expression. */
5015 cp_parser_and_expression (cp_parser
* parser
)
5017 static const cp_parser_token_tree_map map
= {
5018 { CPP_AND
, BIT_AND_EXPR
},
5019 { CPP_EOF
, ERROR_MARK
}
5022 return cp_parser_binary_expression (parser
,
5024 cp_parser_equality_expression
);
5027 /* Parse an exclusive-or-expression.
5029 exclusive-or-expression:
5031 exclusive-or-expression ^ and-expression
5033 Returns a representation of the expression. */
5036 cp_parser_exclusive_or_expression (cp_parser
* parser
)
5038 static const cp_parser_token_tree_map map
= {
5039 { CPP_XOR
, BIT_XOR_EXPR
},
5040 { CPP_EOF
, ERROR_MARK
}
5043 return cp_parser_binary_expression (parser
,
5045 cp_parser_and_expression
);
5049 /* Parse an inclusive-or-expression.
5051 inclusive-or-expression:
5052 exclusive-or-expression
5053 inclusive-or-expression | exclusive-or-expression
5055 Returns a representation of the expression. */
5058 cp_parser_inclusive_or_expression (cp_parser
* parser
)
5060 static const cp_parser_token_tree_map map
= {
5061 { CPP_OR
, BIT_IOR_EXPR
},
5062 { CPP_EOF
, ERROR_MARK
}
5065 return cp_parser_binary_expression (parser
,
5067 cp_parser_exclusive_or_expression
);
5070 /* Parse a logical-and-expression.
5072 logical-and-expression:
5073 inclusive-or-expression
5074 logical-and-expression && inclusive-or-expression
5076 Returns a representation of the expression. */
5079 cp_parser_logical_and_expression (cp_parser
* parser
)
5081 static const cp_parser_token_tree_map map
= {
5082 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
5083 { CPP_EOF
, ERROR_MARK
}
5086 return cp_parser_binary_expression (parser
,
5088 cp_parser_inclusive_or_expression
);
5091 /* Parse a logical-or-expression.
5093 logical-or-expression:
5094 logical-and-expression
5095 logical-or-expression || logical-and-expression
5097 Returns a representation of the expression. */
5100 cp_parser_logical_or_expression (cp_parser
* parser
)
5102 static const cp_parser_token_tree_map map
= {
5103 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
5104 { CPP_EOF
, ERROR_MARK
}
5107 return cp_parser_binary_expression (parser
,
5109 cp_parser_logical_and_expression
);
5112 /* Parse the `? expression : assignment-expression' part of a
5113 conditional-expression. The LOGICAL_OR_EXPR is the
5114 logical-or-expression that started the conditional-expression.
5115 Returns a representation of the entire conditional-expression.
5117 This routine is used by cp_parser_assignment_expression.
5119 ? expression : assignment-expression
5123 ? : assignment-expression */
5126 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5129 tree assignment_expr
;
5131 /* Consume the `?' token. */
5132 cp_lexer_consume_token (parser
->lexer
);
5133 if (cp_parser_allow_gnu_extensions_p (parser
)
5134 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5135 /* Implicit true clause. */
5138 /* Parse the expression. */
5139 expr
= cp_parser_expression (parser
);
5141 /* The next token should be a `:'. */
5142 cp_parser_require (parser
, CPP_COLON
, "`:'");
5143 /* Parse the assignment-expression. */
5144 assignment_expr
= cp_parser_assignment_expression (parser
);
5146 /* Build the conditional-expression. */
5147 return build_x_conditional_expr (logical_or_expr
,
5152 /* Parse an assignment-expression.
5154 assignment-expression:
5155 conditional-expression
5156 logical-or-expression assignment-operator assignment_expression
5159 Returns a representation for the expression. */
5162 cp_parser_assignment_expression (cp_parser
* parser
)
5166 /* If the next token is the `throw' keyword, then we're looking at
5167 a throw-expression. */
5168 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5169 expr
= cp_parser_throw_expression (parser
);
5170 /* Otherwise, it must be that we are looking at a
5171 logical-or-expression. */
5174 /* Parse the logical-or-expression. */
5175 expr
= cp_parser_logical_or_expression (parser
);
5176 /* If the next token is a `?' then we're actually looking at a
5177 conditional-expression. */
5178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5179 return cp_parser_question_colon_clause (parser
, expr
);
5182 enum tree_code assignment_operator
;
5184 /* If it's an assignment-operator, we're using the second
5187 = cp_parser_assignment_operator_opt (parser
);
5188 if (assignment_operator
!= ERROR_MARK
)
5192 /* Parse the right-hand side of the assignment. */
5193 rhs
= cp_parser_assignment_expression (parser
);
5194 /* An assignment may not appear in a
5195 constant-expression. */
5196 if (cp_parser_non_integral_constant_expression (parser
,
5198 return error_mark_node
;
5199 /* Build the assignment expression. */
5200 expr
= build_x_modify_expr (expr
,
5201 assignment_operator
,
5210 /* Parse an (optional) assignment-operator.
5212 assignment-operator: one of
5213 = *= /= %= += -= >>= <<= &= ^= |=
5217 assignment-operator: one of
5220 If the next token is an assignment operator, the corresponding tree
5221 code is returned, and the token is consumed. For example, for
5222 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5223 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5224 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5225 operator, ERROR_MARK is returned. */
5227 static enum tree_code
5228 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5233 /* Peek at the next toen. */
5234 token
= cp_lexer_peek_token (parser
->lexer
);
5236 switch (token
->type
)
5247 op
= TRUNC_DIV_EXPR
;
5251 op
= TRUNC_MOD_EXPR
;
5291 /* Nothing else is an assignment operator. */
5295 /* If it was an assignment operator, consume it. */
5296 if (op
!= ERROR_MARK
)
5297 cp_lexer_consume_token (parser
->lexer
);
5302 /* Parse an expression.
5305 assignment-expression
5306 expression , assignment-expression
5308 Returns a representation of the expression. */
5311 cp_parser_expression (cp_parser
* parser
)
5313 tree expression
= NULL_TREE
;
5317 tree assignment_expression
;
5319 /* Parse the next assignment-expression. */
5320 assignment_expression
5321 = cp_parser_assignment_expression (parser
);
5322 /* If this is the first assignment-expression, we can just
5325 expression
= assignment_expression
;
5327 expression
= build_x_compound_expr (expression
,
5328 assignment_expression
);
5329 /* If the next token is not a comma, then we are done with the
5331 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5333 /* Consume the `,'. */
5334 cp_lexer_consume_token (parser
->lexer
);
5335 /* A comma operator cannot appear in a constant-expression. */
5336 if (cp_parser_non_integral_constant_expression (parser
,
5337 "a comma operator"))
5338 expression
= error_mark_node
;
5344 /* Parse a constant-expression.
5346 constant-expression:
5347 conditional-expression
5349 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5350 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5351 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5352 is false, NON_CONSTANT_P should be NULL. */
5355 cp_parser_constant_expression (cp_parser
* parser
,
5356 bool allow_non_constant_p
,
5357 bool *non_constant_p
)
5359 bool saved_integral_constant_expression_p
;
5360 bool saved_allow_non_integral_constant_expression_p
;
5361 bool saved_non_integral_constant_expression_p
;
5364 /* It might seem that we could simply parse the
5365 conditional-expression, and then check to see if it were
5366 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5367 one that the compiler can figure out is constant, possibly after
5368 doing some simplifications or optimizations. The standard has a
5369 precise definition of constant-expression, and we must honor
5370 that, even though it is somewhat more restrictive.
5376 is not a legal declaration, because `(2, 3)' is not a
5377 constant-expression. The `,' operator is forbidden in a
5378 constant-expression. However, GCC's constant-folding machinery
5379 will fold this operation to an INTEGER_CST for `3'. */
5381 /* Save the old settings. */
5382 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5383 saved_allow_non_integral_constant_expression_p
5384 = parser
->allow_non_integral_constant_expression_p
;
5385 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5386 /* We are now parsing a constant-expression. */
5387 parser
->integral_constant_expression_p
= true;
5388 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5389 parser
->non_integral_constant_expression_p
= false;
5390 /* Although the grammar says "conditional-expression", we parse an
5391 "assignment-expression", which also permits "throw-expression"
5392 and the use of assignment operators. In the case that
5393 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5394 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5395 actually essential that we look for an assignment-expression.
5396 For example, cp_parser_initializer_clauses uses this function to
5397 determine whether a particular assignment-expression is in fact
5399 expression
= cp_parser_assignment_expression (parser
);
5400 /* Restore the old settings. */
5401 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
5402 parser
->allow_non_integral_constant_expression_p
5403 = saved_allow_non_integral_constant_expression_p
;
5404 if (allow_non_constant_p
)
5405 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5406 parser
->non_integral_constant_expression_p
= saved_non_integral_constant_expression_p
;
5411 /* Statements [gram.stmt.stmt] */
5413 /* Parse a statement.
5417 expression-statement
5422 declaration-statement
5426 cp_parser_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5430 int statement_line_number
;
5432 /* There is no statement yet. */
5433 statement
= NULL_TREE
;
5434 /* Peek at the next token. */
5435 token
= cp_lexer_peek_token (parser
->lexer
);
5436 /* Remember the line number of the first token in the statement. */
5437 statement_line_number
= token
->location
.line
;
5438 /* If this is a keyword, then that will often determine what kind of
5439 statement we have. */
5440 if (token
->type
== CPP_KEYWORD
)
5442 enum rid keyword
= token
->keyword
;
5448 statement
= cp_parser_labeled_statement (parser
,
5449 in_statement_expr_p
);
5454 statement
= cp_parser_selection_statement (parser
);
5460 statement
= cp_parser_iteration_statement (parser
);
5467 statement
= cp_parser_jump_statement (parser
);
5471 statement
= cp_parser_try_block (parser
);
5475 /* It might be a keyword like `int' that can start a
5476 declaration-statement. */
5480 else if (token
->type
== CPP_NAME
)
5482 /* If the next token is a `:', then we are looking at a
5483 labeled-statement. */
5484 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5485 if (token
->type
== CPP_COLON
)
5486 statement
= cp_parser_labeled_statement (parser
, in_statement_expr_p
);
5488 /* Anything that starts with a `{' must be a compound-statement. */
5489 else if (token
->type
== CPP_OPEN_BRACE
)
5490 statement
= cp_parser_compound_statement (parser
, false);
5492 /* Everything else must be a declaration-statement or an
5493 expression-statement. Try for the declaration-statement
5494 first, unless we are looking at a `;', in which case we know that
5495 we have an expression-statement. */
5498 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5500 cp_parser_parse_tentatively (parser
);
5501 /* Try to parse the declaration-statement. */
5502 cp_parser_declaration_statement (parser
);
5503 /* If that worked, we're done. */
5504 if (cp_parser_parse_definitely (parser
))
5507 /* Look for an expression-statement instead. */
5508 statement
= cp_parser_expression_statement (parser
, in_statement_expr_p
);
5511 /* Set the line number for the statement. */
5512 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5513 STMT_LINENO (statement
) = statement_line_number
;
5516 /* Parse a labeled-statement.
5519 identifier : statement
5520 case constant-expression : statement
5526 case constant-expression ... constant-expression : statement
5528 Returns the new CASE_LABEL, for a `case' or `default' label. For
5529 an ordinary label, returns a LABEL_STMT. */
5532 cp_parser_labeled_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5535 tree statement
= error_mark_node
;
5537 /* The next token should be an identifier. */
5538 token
= cp_lexer_peek_token (parser
->lexer
);
5539 if (token
->type
!= CPP_NAME
5540 && token
->type
!= CPP_KEYWORD
)
5542 cp_parser_error (parser
, "expected labeled-statement");
5543 return error_mark_node
;
5546 switch (token
->keyword
)
5553 /* Consume the `case' token. */
5554 cp_lexer_consume_token (parser
->lexer
);
5555 /* Parse the constant-expression. */
5556 expr
= cp_parser_constant_expression (parser
,
5557 /*allow_non_constant_p=*/false,
5560 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
5561 if (ellipsis
->type
== CPP_ELLIPSIS
)
5563 /* Consume the `...' token. */
5564 cp_lexer_consume_token (parser
->lexer
);
5566 cp_parser_constant_expression (parser
,
5567 /*allow_non_constant_p=*/false,
5569 /* We don't need to emit warnings here, as the common code
5570 will do this for us. */
5573 expr_hi
= NULL_TREE
;
5575 if (!parser
->in_switch_statement_p
)
5576 error ("case label `%E' not within a switch statement", expr
);
5578 statement
= finish_case_label (expr
, expr_hi
);
5583 /* Consume the `default' token. */
5584 cp_lexer_consume_token (parser
->lexer
);
5585 if (!parser
->in_switch_statement_p
)
5586 error ("case label not within a switch statement");
5588 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5592 /* Anything else must be an ordinary label. */
5593 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5597 /* Require the `:' token. */
5598 cp_parser_require (parser
, CPP_COLON
, "`:'");
5599 /* Parse the labeled statement. */
5600 cp_parser_statement (parser
, in_statement_expr_p
);
5602 /* Return the label, in the case of a `case' or `default' label. */
5606 /* Parse an expression-statement.
5608 expression-statement:
5611 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5612 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5613 indicates whether this expression-statement is part of an
5614 expression statement. */
5617 cp_parser_expression_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5619 tree statement
= NULL_TREE
;
5621 /* If the next token is a ';', then there is no expression
5623 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5624 statement
= cp_parser_expression (parser
);
5626 /* Consume the final `;'. */
5627 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5629 if (in_statement_expr_p
5630 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
5632 /* This is the final expression statement of a statement
5634 statement
= finish_stmt_expr_expr (statement
);
5637 statement
= finish_expr_stmt (statement
);
5644 /* Parse a compound-statement.
5647 { statement-seq [opt] }
5649 Returns a COMPOUND_STMT representing the statement. */
5652 cp_parser_compound_statement (cp_parser
*parser
, bool in_statement_expr_p
)
5656 /* Consume the `{'. */
5657 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5658 return error_mark_node
;
5659 /* Begin the compound-statement. */
5660 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/false);
5661 /* Parse an (optional) statement-seq. */
5662 cp_parser_statement_seq_opt (parser
, in_statement_expr_p
);
5663 /* Finish the compound-statement. */
5664 finish_compound_stmt (compound_stmt
);
5665 /* Consume the `}'. */
5666 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5668 return compound_stmt
;
5671 /* Parse an (optional) statement-seq.
5675 statement-seq [opt] statement */
5678 cp_parser_statement_seq_opt (cp_parser
* parser
, bool in_statement_expr_p
)
5680 /* Scan statements until there aren't any more. */
5683 /* If we're looking at a `}', then we've run out of statements. */
5684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5685 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5688 /* Parse the statement. */
5689 cp_parser_statement (parser
, in_statement_expr_p
);
5693 /* Parse a selection-statement.
5695 selection-statement:
5696 if ( condition ) statement
5697 if ( condition ) statement else statement
5698 switch ( condition ) statement
5700 Returns the new IF_STMT or SWITCH_STMT. */
5703 cp_parser_selection_statement (cp_parser
* parser
)
5708 /* Peek at the next token. */
5709 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5711 /* See what kind of keyword it is. */
5712 keyword
= token
->keyword
;
5721 /* Look for the `('. */
5722 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5724 cp_parser_skip_to_end_of_statement (parser
);
5725 return error_mark_node
;
5728 /* Begin the selection-statement. */
5729 if (keyword
== RID_IF
)
5730 statement
= begin_if_stmt ();
5732 statement
= begin_switch_stmt ();
5734 /* Parse the condition. */
5735 condition
= cp_parser_condition (parser
);
5736 /* Look for the `)'. */
5737 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5738 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
5739 /*consume_paren=*/true);
5741 if (keyword
== RID_IF
)
5745 /* Add the condition. */
5746 finish_if_stmt_cond (condition
, statement
);
5748 /* Parse the then-clause. */
5749 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5750 finish_then_clause (statement
);
5752 /* If the next token is `else', parse the else-clause. */
5753 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5758 /* Consume the `else' keyword. */
5759 cp_lexer_consume_token (parser
->lexer
);
5760 /* Parse the else-clause. */
5762 = cp_parser_implicitly_scoped_statement (parser
);
5763 finish_else_clause (statement
);
5766 /* Now we're all done with the if-statement. */
5772 bool in_switch_statement_p
;
5774 /* Add the condition. */
5775 finish_switch_cond (condition
, statement
);
5777 /* Parse the body of the switch-statement. */
5778 in_switch_statement_p
= parser
->in_switch_statement_p
;
5779 parser
->in_switch_statement_p
= true;
5780 body
= cp_parser_implicitly_scoped_statement (parser
);
5781 parser
->in_switch_statement_p
= in_switch_statement_p
;
5783 /* Now we're all done with the switch-statement. */
5784 finish_switch_stmt (statement
);
5792 cp_parser_error (parser
, "expected selection-statement");
5793 return error_mark_node
;
5797 /* Parse a condition.
5801 type-specifier-seq declarator = assignment-expression
5806 type-specifier-seq declarator asm-specification [opt]
5807 attributes [opt] = assignment-expression
5809 Returns the expression that should be tested. */
5812 cp_parser_condition (cp_parser
* parser
)
5814 tree type_specifiers
;
5815 const char *saved_message
;
5817 /* Try the declaration first. */
5818 cp_parser_parse_tentatively (parser
);
5819 /* New types are not allowed in the type-specifier-seq for a
5821 saved_message
= parser
->type_definition_forbidden_message
;
5822 parser
->type_definition_forbidden_message
5823 = "types may not be defined in conditions";
5824 /* Parse the type-specifier-seq. */
5825 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5826 /* Restore the saved message. */
5827 parser
->type_definition_forbidden_message
= saved_message
;
5828 /* If all is well, we might be looking at a declaration. */
5829 if (!cp_parser_error_occurred (parser
))
5832 tree asm_specification
;
5835 tree initializer
= NULL_TREE
;
5837 /* Parse the declarator. */
5838 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5839 /*ctor_dtor_or_conv_p=*/NULL
,
5840 /*parenthesized_p=*/NULL
,
5841 /*member_p=*/false);
5842 /* Parse the attributes. */
5843 attributes
= cp_parser_attributes_opt (parser
);
5844 /* Parse the asm-specification. */
5845 asm_specification
= cp_parser_asm_specification_opt (parser
);
5846 /* If the next token is not an `=', then we might still be
5847 looking at an expression. For example:
5851 looks like a decl-specifier-seq and a declarator -- but then
5852 there is no `=', so this is an expression. */
5853 cp_parser_require (parser
, CPP_EQ
, "`='");
5854 /* If we did see an `=', then we are looking at a declaration
5856 if (cp_parser_parse_definitely (parser
))
5860 /* Create the declaration. */
5861 decl
= start_decl (declarator
, type_specifiers
,
5862 /*initialized_p=*/true,
5863 attributes
, /*prefix_attributes=*/NULL_TREE
,
5865 /* Parse the assignment-expression. */
5866 initializer
= cp_parser_assignment_expression (parser
);
5868 /* Process the initializer. */
5869 cp_finish_decl (decl
,
5872 LOOKUP_ONLYCONVERTING
);
5874 pop_scope (DECL_CONTEXT (decl
));
5876 return convert_from_reference (decl
);
5879 /* If we didn't even get past the declarator successfully, we are
5880 definitely not looking at a declaration. */
5882 cp_parser_abort_tentative_parse (parser
);
5884 /* Otherwise, we are looking at an expression. */
5885 return cp_parser_expression (parser
);
5888 /* Parse an iteration-statement.
5890 iteration-statement:
5891 while ( condition ) statement
5892 do statement while ( expression ) ;
5893 for ( for-init-statement condition [opt] ; expression [opt] )
5896 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5899 cp_parser_iteration_statement (cp_parser
* parser
)
5904 bool in_iteration_statement_p
;
5907 /* Peek at the next token. */
5908 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
5910 return error_mark_node
;
5912 /* Remember whether or not we are already within an iteration
5914 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
5916 /* See what kind of keyword it is. */
5917 keyword
= token
->keyword
;
5924 /* Begin the while-statement. */
5925 statement
= begin_while_stmt ();
5926 /* Look for the `('. */
5927 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5928 /* Parse the condition. */
5929 condition
= cp_parser_condition (parser
);
5930 finish_while_stmt_cond (condition
, statement
);
5931 /* Look for the `)'. */
5932 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5933 /* Parse the dependent statement. */
5934 parser
->in_iteration_statement_p
= true;
5935 cp_parser_already_scoped_statement (parser
);
5936 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5937 /* We're done with the while-statement. */
5938 finish_while_stmt (statement
);
5946 /* Begin the do-statement. */
5947 statement
= begin_do_stmt ();
5948 /* Parse the body of the do-statement. */
5949 parser
->in_iteration_statement_p
= true;
5950 cp_parser_implicitly_scoped_statement (parser
);
5951 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
5952 finish_do_body (statement
);
5953 /* Look for the `while' keyword. */
5954 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
5955 /* Look for the `('. */
5956 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5957 /* Parse the expression. */
5958 expression
= cp_parser_expression (parser
);
5959 /* We're done with the do-statement. */
5960 finish_do_stmt (expression
, statement
);
5961 /* Look for the `)'. */
5962 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5963 /* Look for the `;'. */
5964 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5970 tree condition
= NULL_TREE
;
5971 tree expression
= NULL_TREE
;
5973 /* Begin the for-statement. */
5974 statement
= begin_for_stmt ();
5975 /* Look for the `('. */
5976 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5977 /* Parse the initialization. */
5978 cp_parser_for_init_statement (parser
);
5979 finish_for_init_stmt (statement
);
5981 /* If there's a condition, process it. */
5982 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5983 condition
= cp_parser_condition (parser
);
5984 finish_for_cond (condition
, statement
);
5985 /* Look for the `;'. */
5986 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5988 /* If there's an expression, process it. */
5989 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5990 expression
= cp_parser_expression (parser
);
5991 finish_for_expr (expression
, statement
);
5992 /* Look for the `)'. */
5993 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5995 /* Parse the body of the for-statement. */
5996 parser
->in_iteration_statement_p
= true;
5997 cp_parser_already_scoped_statement (parser
);
5998 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6000 /* We're done with the for-statement. */
6001 finish_for_stmt (statement
);
6006 cp_parser_error (parser
, "expected iteration-statement");
6007 statement
= error_mark_node
;
6014 /* Parse a for-init-statement.
6017 expression-statement
6018 simple-declaration */
6021 cp_parser_for_init_statement (cp_parser
* parser
)
6023 /* If the next token is a `;', then we have an empty
6024 expression-statement. Grammatically, this is also a
6025 simple-declaration, but an invalid one, because it does not
6026 declare anything. Therefore, if we did not handle this case
6027 specially, we would issue an error message about an invalid
6029 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6031 /* We're going to speculatively look for a declaration, falling back
6032 to an expression, if necessary. */
6033 cp_parser_parse_tentatively (parser
);
6034 /* Parse the declaration. */
6035 cp_parser_simple_declaration (parser
,
6036 /*function_definition_allowed_p=*/false);
6037 /* If the tentative parse failed, then we shall need to look for an
6038 expression-statement. */
6039 if (cp_parser_parse_definitely (parser
))
6043 cp_parser_expression_statement (parser
, false);
6046 /* Parse a jump-statement.
6051 return expression [opt] ;
6059 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6063 cp_parser_jump_statement (cp_parser
* parser
)
6065 tree statement
= error_mark_node
;
6069 /* Peek at the next token. */
6070 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6072 return error_mark_node
;
6074 /* See what kind of keyword it is. */
6075 keyword
= token
->keyword
;
6079 if (!parser
->in_switch_statement_p
6080 && !parser
->in_iteration_statement_p
)
6082 error ("break statement not within loop or switch");
6083 statement
= error_mark_node
;
6086 statement
= finish_break_stmt ();
6087 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6091 if (!parser
->in_iteration_statement_p
)
6093 error ("continue statement not within a loop");
6094 statement
= error_mark_node
;
6097 statement
= finish_continue_stmt ();
6098 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6105 /* If the next token is a `;', then there is no
6107 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6108 expr
= cp_parser_expression (parser
);
6111 /* Build the return-statement. */
6112 statement
= finish_return_stmt (expr
);
6113 /* Look for the final `;'. */
6114 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6119 /* Create the goto-statement. */
6120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6122 /* Issue a warning about this use of a GNU extension. */
6124 pedwarn ("ISO C++ forbids computed gotos");
6125 /* Consume the '*' token. */
6126 cp_lexer_consume_token (parser
->lexer
);
6127 /* Parse the dependent expression. */
6128 finish_goto_stmt (cp_parser_expression (parser
));
6131 finish_goto_stmt (cp_parser_identifier (parser
));
6132 /* Look for the final `;'. */
6133 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6137 cp_parser_error (parser
, "expected jump-statement");
6144 /* Parse a declaration-statement.
6146 declaration-statement:
6147 block-declaration */
6150 cp_parser_declaration_statement (cp_parser
* parser
)
6152 /* Parse the block-declaration. */
6153 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6155 /* Finish off the statement. */
6159 /* Some dependent statements (like `if (cond) statement'), are
6160 implicitly in their own scope. In other words, if the statement is
6161 a single statement (as opposed to a compound-statement), it is
6162 none-the-less treated as if it were enclosed in braces. Any
6163 declarations appearing in the dependent statement are out of scope
6164 after control passes that point. This function parses a statement,
6165 but ensures that is in its own scope, even if it is not a
6168 Returns the new statement. */
6171 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6175 /* If the token is not a `{', then we must take special action. */
6176 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6178 /* Create a compound-statement. */
6179 statement
= begin_compound_stmt (/*has_no_scope=*/false);
6180 /* Parse the dependent-statement. */
6181 cp_parser_statement (parser
, false);
6182 /* Finish the dummy compound-statement. */
6183 finish_compound_stmt (statement
);
6185 /* Otherwise, we simply parse the statement directly. */
6187 statement
= cp_parser_compound_statement (parser
, false);
6189 /* Return the statement. */
6193 /* For some dependent statements (like `while (cond) statement'), we
6194 have already created a scope. Therefore, even if the dependent
6195 statement is a compound-statement, we do not want to create another
6199 cp_parser_already_scoped_statement (cp_parser
* parser
)
6201 /* If the token is not a `{', then we must take special action. */
6202 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
6206 /* Create a compound-statement. */
6207 statement
= begin_compound_stmt (/*has_no_scope=*/true);
6208 /* Parse the dependent-statement. */
6209 cp_parser_statement (parser
, false);
6210 /* Finish the dummy compound-statement. */
6211 finish_compound_stmt (statement
);
6213 /* Otherwise, we simply parse the statement directly. */
6215 cp_parser_statement (parser
, false);
6218 /* Declarations [gram.dcl.dcl] */
6220 /* Parse an optional declaration-sequence.
6224 declaration-seq declaration */
6227 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6233 token
= cp_lexer_peek_token (parser
->lexer
);
6235 if (token
->type
== CPP_CLOSE_BRACE
6236 || token
->type
== CPP_EOF
)
6239 if (token
->type
== CPP_SEMICOLON
)
6241 /* A declaration consisting of a single semicolon is
6242 invalid. Allow it unless we're being pedantic. */
6243 if (pedantic
&& !in_system_header
)
6244 pedwarn ("extra `;'");
6245 cp_lexer_consume_token (parser
->lexer
);
6249 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6250 parser to enter or exit implicit `extern "C"' blocks. */
6251 while (pending_lang_change
> 0)
6253 push_lang_context (lang_name_c
);
6254 --pending_lang_change
;
6256 while (pending_lang_change
< 0)
6258 pop_lang_context ();
6259 ++pending_lang_change
;
6262 /* Parse the declaration itself. */
6263 cp_parser_declaration (parser
);
6267 /* Parse a declaration.
6272 template-declaration
6273 explicit-instantiation
6274 explicit-specialization
6275 linkage-specification
6276 namespace-definition
6281 __extension__ declaration */
6284 cp_parser_declaration (cp_parser
* parser
)
6290 /* Check for the `__extension__' keyword. */
6291 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6293 /* Parse the qualified declaration. */
6294 cp_parser_declaration (parser
);
6295 /* Restore the PEDANTIC flag. */
6296 pedantic
= saved_pedantic
;
6301 /* Try to figure out what kind of declaration is present. */
6302 token1
= *cp_lexer_peek_token (parser
->lexer
);
6303 if (token1
.type
!= CPP_EOF
)
6304 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6306 /* If the next token is `extern' and the following token is a string
6307 literal, then we have a linkage specification. */
6308 if (token1
.keyword
== RID_EXTERN
6309 && cp_parser_is_string_literal (&token2
))
6310 cp_parser_linkage_specification (parser
);
6311 /* If the next token is `template', then we have either a template
6312 declaration, an explicit instantiation, or an explicit
6314 else if (token1
.keyword
== RID_TEMPLATE
)
6316 /* `template <>' indicates a template specialization. */
6317 if (token2
.type
== CPP_LESS
6318 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6319 cp_parser_explicit_specialization (parser
);
6320 /* `template <' indicates a template declaration. */
6321 else if (token2
.type
== CPP_LESS
)
6322 cp_parser_template_declaration (parser
, /*member_p=*/false);
6323 /* Anything else must be an explicit instantiation. */
6325 cp_parser_explicit_instantiation (parser
);
6327 /* If the next token is `export', then we have a template
6329 else if (token1
.keyword
== RID_EXPORT
)
6330 cp_parser_template_declaration (parser
, /*member_p=*/false);
6331 /* If the next token is `extern', 'static' or 'inline' and the one
6332 after that is `template', we have a GNU extended explicit
6333 instantiation directive. */
6334 else if (cp_parser_allow_gnu_extensions_p (parser
)
6335 && (token1
.keyword
== RID_EXTERN
6336 || token1
.keyword
== RID_STATIC
6337 || token1
.keyword
== RID_INLINE
)
6338 && token2
.keyword
== RID_TEMPLATE
)
6339 cp_parser_explicit_instantiation (parser
);
6340 /* If the next token is `namespace', check for a named or unnamed
6341 namespace definition. */
6342 else if (token1
.keyword
== RID_NAMESPACE
6343 && (/* A named namespace definition. */
6344 (token2
.type
== CPP_NAME
6345 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6347 /* An unnamed namespace definition. */
6348 || token2
.type
== CPP_OPEN_BRACE
))
6349 cp_parser_namespace_definition (parser
);
6350 /* We must have either a block declaration or a function
6353 /* Try to parse a block-declaration, or a function-definition. */
6354 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6357 /* Parse a block-declaration.
6362 namespace-alias-definition
6369 __extension__ block-declaration
6372 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6373 part of a declaration-statement. */
6376 cp_parser_block_declaration (cp_parser
*parser
,
6382 /* Check for the `__extension__' keyword. */
6383 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6385 /* Parse the qualified declaration. */
6386 cp_parser_block_declaration (parser
, statement_p
);
6387 /* Restore the PEDANTIC flag. */
6388 pedantic
= saved_pedantic
;
6393 /* Peek at the next token to figure out which kind of declaration is
6395 token1
= cp_lexer_peek_token (parser
->lexer
);
6397 /* If the next keyword is `asm', we have an asm-definition. */
6398 if (token1
->keyword
== RID_ASM
)
6401 cp_parser_commit_to_tentative_parse (parser
);
6402 cp_parser_asm_definition (parser
);
6404 /* If the next keyword is `namespace', we have a
6405 namespace-alias-definition. */
6406 else if (token1
->keyword
== RID_NAMESPACE
)
6407 cp_parser_namespace_alias_definition (parser
);
6408 /* If the next keyword is `using', we have either a
6409 using-declaration or a using-directive. */
6410 else if (token1
->keyword
== RID_USING
)
6415 cp_parser_commit_to_tentative_parse (parser
);
6416 /* If the token after `using' is `namespace', then we have a
6418 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6419 if (token2
->keyword
== RID_NAMESPACE
)
6420 cp_parser_using_directive (parser
);
6421 /* Otherwise, it's a using-declaration. */
6423 cp_parser_using_declaration (parser
);
6425 /* If the next keyword is `__label__' we have a label declaration. */
6426 else if (token1
->keyword
== RID_LABEL
)
6429 cp_parser_commit_to_tentative_parse (parser
);
6430 cp_parser_label_declaration (parser
);
6432 /* Anything else must be a simple-declaration. */
6434 cp_parser_simple_declaration (parser
, !statement_p
);
6437 /* Parse a simple-declaration.
6440 decl-specifier-seq [opt] init-declarator-list [opt] ;
6442 init-declarator-list:
6444 init-declarator-list , init-declarator
6446 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6447 function-definition as a simple-declaration. */
6450 cp_parser_simple_declaration (cp_parser
* parser
,
6451 bool function_definition_allowed_p
)
6453 tree decl_specifiers
;
6455 int declares_class_or_enum
;
6456 bool saw_declarator
;
6458 /* Defer access checks until we know what is being declared; the
6459 checks for names appearing in the decl-specifier-seq should be
6460 done as if we were in the scope of the thing being declared. */
6461 push_deferring_access_checks (dk_deferred
);
6463 /* Parse the decl-specifier-seq. We have to keep track of whether
6464 or not the decl-specifier-seq declares a named class or
6465 enumeration type, since that is the only case in which the
6466 init-declarator-list is allowed to be empty.
6470 In a simple-declaration, the optional init-declarator-list can be
6471 omitted only when declaring a class or enumeration, that is when
6472 the decl-specifier-seq contains either a class-specifier, an
6473 elaborated-type-specifier, or an enum-specifier. */
6475 = cp_parser_decl_specifier_seq (parser
,
6476 CP_PARSER_FLAGS_OPTIONAL
,
6478 &declares_class_or_enum
);
6479 /* We no longer need to defer access checks. */
6480 stop_deferring_access_checks ();
6482 /* In a block scope, a valid declaration must always have a
6483 decl-specifier-seq. By not trying to parse declarators, we can
6484 resolve the declaration/expression ambiguity more quickly. */
6485 if (!function_definition_allowed_p
&& !decl_specifiers
)
6487 cp_parser_error (parser
, "expected declaration");
6491 /* If the next two tokens are both identifiers, the code is
6492 erroneous. The usual cause of this situation is code like:
6496 where "T" should name a type -- but does not. */
6497 if (cp_parser_diagnose_invalid_type_name (parser
))
6499 /* If parsing tentatively, we should commit; we really are
6500 looking at a declaration. */
6501 cp_parser_commit_to_tentative_parse (parser
);
6506 /* If we have seen at least one decl-specifier, and the next token
6507 is not a parenthesis, then we must be looking at a declaration.
6508 (After "int (" we might be looking at a functional cast.) */
6510 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6511 cp_parser_commit_to_tentative_parse (parser
);
6513 /* Keep going until we hit the `;' at the end of the simple
6515 saw_declarator
= false;
6516 while (cp_lexer_next_token_is_not (parser
->lexer
,
6520 bool function_definition_p
;
6523 saw_declarator
= true;
6524 /* Parse the init-declarator. */
6525 decl
= cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6526 function_definition_allowed_p
,
6528 declares_class_or_enum
,
6529 &function_definition_p
);
6530 /* If an error occurred while parsing tentatively, exit quickly.
6531 (That usually happens when in the body of a function; each
6532 statement is treated as a declaration-statement until proven
6534 if (cp_parser_error_occurred (parser
))
6536 /* Handle function definitions specially. */
6537 if (function_definition_p
)
6539 /* If the next token is a `,', then we are probably
6540 processing something like:
6544 which is erroneous. */
6545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6546 error ("mixing declarations and function-definitions is forbidden");
6547 /* Otherwise, we're done with the list of declarators. */
6550 pop_deferring_access_checks ();
6554 /* The next token should be either a `,' or a `;'. */
6555 token
= cp_lexer_peek_token (parser
->lexer
);
6556 /* If it's a `,', there are more declarators to come. */
6557 if (token
->type
== CPP_COMMA
)
6558 cp_lexer_consume_token (parser
->lexer
);
6559 /* If it's a `;', we are done. */
6560 else if (token
->type
== CPP_SEMICOLON
)
6562 /* Anything else is an error. */
6565 /* If we have already issued an error message we don't need
6566 to issue another one. */
6567 if (decl
!= error_mark_node
6568 || (cp_parser_parsing_tentatively (parser
)
6569 && !cp_parser_committed_to_tentative_parse (parser
)))
6570 cp_parser_error (parser
, "expected `,' or `;'");
6571 /* Skip tokens until we reach the end of the statement. */
6572 cp_parser_skip_to_end_of_statement (parser
);
6573 /* If the next token is now a `;', consume it. */
6574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6575 cp_lexer_consume_token (parser
->lexer
);
6578 /* After the first time around, a function-definition is not
6579 allowed -- even if it was OK at first. For example:
6584 function_definition_allowed_p
= false;
6587 /* Issue an error message if no declarators are present, and the
6588 decl-specifier-seq does not itself declare a class or
6590 if (!saw_declarator
)
6592 if (cp_parser_declares_only_class_p (parser
))
6593 shadow_tag (decl_specifiers
);
6594 /* Perform any deferred access checks. */
6595 perform_deferred_access_checks ();
6598 /* Consume the `;'. */
6599 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6602 pop_deferring_access_checks ();
6605 /* Parse a decl-specifier-seq.
6608 decl-specifier-seq [opt] decl-specifier
6611 storage-class-specifier
6622 Returns a TREE_LIST, giving the decl-specifiers in the order they
6623 appear in the source code. The TREE_VALUE of each node is the
6624 decl-specifier. For a keyword (such as `auto' or `friend'), the
6625 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6626 representation of a type-specifier, see cp_parser_type_specifier.
6628 If there are attributes, they will be stored in *ATTRIBUTES,
6629 represented as described above cp_parser_attributes.
6631 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6632 appears, and the entity that will be a friend is not going to be a
6633 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6634 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6635 friendship is granted might not be a class.
6637 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6640 1: one of the decl-specifiers is an elaborated-type-specifier
6641 (i.e., a type declaration)
6642 2: one of the decl-specifiers is an enum-specifier or a
6643 class-specifier (i.e., a type definition)
6648 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6649 cp_parser_flags flags
,
6651 int* declares_class_or_enum
)
6653 tree decl_specs
= NULL_TREE
;
6654 bool friend_p
= false;
6655 bool constructor_possible_p
= !parser
->in_declarator_p
;
6657 /* Assume no class or enumeration type is declared. */
6658 *declares_class_or_enum
= 0;
6660 /* Assume there are no attributes. */
6661 *attributes
= NULL_TREE
;
6663 /* Keep reading specifiers until there are no more to read. */
6666 tree decl_spec
= NULL_TREE
;
6670 /* Peek at the next token. */
6671 token
= cp_lexer_peek_token (parser
->lexer
);
6672 /* Handle attributes. */
6673 if (token
->keyword
== RID_ATTRIBUTE
)
6675 /* Parse the attributes. */
6676 decl_spec
= cp_parser_attributes_opt (parser
);
6677 /* Add them to the list. */
6678 *attributes
= chainon (*attributes
, decl_spec
);
6681 /* If the next token is an appropriate keyword, we can simply
6682 add it to the list. */
6683 switch (token
->keyword
)
6689 error ("duplicate `friend'");
6692 /* The representation of the specifier is simply the
6693 appropriate TREE_IDENTIFIER node. */
6694 decl_spec
= token
->value
;
6695 /* Consume the token. */
6696 cp_lexer_consume_token (parser
->lexer
);
6699 /* function-specifier:
6706 decl_spec
= cp_parser_function_specifier_opt (parser
);
6712 /* The representation of the specifier is simply the
6713 appropriate TREE_IDENTIFIER node. */
6714 decl_spec
= token
->value
;
6715 /* Consume the token. */
6716 cp_lexer_consume_token (parser
->lexer
);
6717 /* A constructor declarator cannot appear in a typedef. */
6718 constructor_possible_p
= false;
6719 /* The "typedef" keyword can only occur in a declaration; we
6720 may as well commit at this point. */
6721 cp_parser_commit_to_tentative_parse (parser
);
6724 /* storage-class-specifier:
6739 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6746 /* Constructors are a special case. The `S' in `S()' is not a
6747 decl-specifier; it is the beginning of the declarator. */
6748 constructor_p
= (!decl_spec
6749 && constructor_possible_p
6750 && cp_parser_constructor_declarator_p (parser
,
6753 /* If we don't have a DECL_SPEC yet, then we must be looking at
6754 a type-specifier. */
6755 if (!decl_spec
&& !constructor_p
)
6757 int decl_spec_declares_class_or_enum
;
6758 bool is_cv_qualifier
;
6761 = cp_parser_type_specifier (parser
, flags
,
6763 /*is_declaration=*/true,
6764 &decl_spec_declares_class_or_enum
,
6767 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6769 /* If this type-specifier referenced a user-defined type
6770 (a typedef, class-name, etc.), then we can't allow any
6771 more such type-specifiers henceforth.
6775 The longest sequence of decl-specifiers that could
6776 possibly be a type name is taken as the
6777 decl-specifier-seq of a declaration. The sequence shall
6778 be self-consistent as described below.
6782 As a general rule, at most one type-specifier is allowed
6783 in the complete decl-specifier-seq of a declaration. The
6784 only exceptions are the following:
6786 -- const or volatile can be combined with any other
6789 -- signed or unsigned can be combined with char, long,
6797 void g (const int Pc);
6799 Here, Pc is *not* part of the decl-specifier seq; it's
6800 the declarator. Therefore, once we see a type-specifier
6801 (other than a cv-qualifier), we forbid any additional
6802 user-defined types. We *do* still allow things like `int
6803 int' to be considered a decl-specifier-seq, and issue the
6804 error message later. */
6805 if (decl_spec
&& !is_cv_qualifier
)
6806 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6807 /* A constructor declarator cannot follow a type-specifier. */
6809 constructor_possible_p
= false;
6812 /* If we still do not have a DECL_SPEC, then there are no more
6816 /* Issue an error message, unless the entire construct was
6818 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6820 cp_parser_error (parser
, "expected decl specifier");
6821 return error_mark_node
;
6827 /* Add the DECL_SPEC to the list of specifiers. */
6828 if (decl_specs
== NULL
|| TREE_VALUE (decl_specs
) != error_mark_node
)
6829 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6831 /* After we see one decl-specifier, further decl-specifiers are
6833 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6836 /* Don't allow a friend specifier with a class definition. */
6837 if (friend_p
&& (*declares_class_or_enum
& 2))
6838 error ("class definition may not be declared a friend");
6840 /* We have built up the DECL_SPECS in reverse order. Return them in
6841 the correct order. */
6842 return nreverse (decl_specs
);
6845 /* Parse an (optional) storage-class-specifier.
6847 storage-class-specifier:
6856 storage-class-specifier:
6859 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6862 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
6864 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6872 /* Consume the token. */
6873 return cp_lexer_consume_token (parser
->lexer
)->value
;
6880 /* Parse an (optional) function-specifier.
6887 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6890 cp_parser_function_specifier_opt (cp_parser
* parser
)
6892 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6897 /* Consume the token. */
6898 return cp_lexer_consume_token (parser
->lexer
)->value
;
6905 /* Parse a linkage-specification.
6907 linkage-specification:
6908 extern string-literal { declaration-seq [opt] }
6909 extern string-literal declaration */
6912 cp_parser_linkage_specification (cp_parser
* parser
)
6917 /* Look for the `extern' keyword. */
6918 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
6920 /* Peek at the next token. */
6921 token
= cp_lexer_peek_token (parser
->lexer
);
6922 /* If it's not a string-literal, then there's a problem. */
6923 if (!cp_parser_is_string_literal (token
))
6925 cp_parser_error (parser
, "expected language-name");
6928 /* Consume the token. */
6929 cp_lexer_consume_token (parser
->lexer
);
6931 /* Transform the literal into an identifier. If the literal is a
6932 wide-character string, or contains embedded NULs, then we can't
6933 handle it as the user wants. */
6934 if (token
->type
== CPP_WSTRING
6935 || (strlen (TREE_STRING_POINTER (token
->value
))
6936 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
6938 cp_parser_error (parser
, "invalid linkage-specification");
6939 /* Assume C++ linkage. */
6940 linkage
= get_identifier ("c++");
6942 /* If it's a simple string constant, things are easier. */
6944 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
6946 /* We're now using the new linkage. */
6947 push_lang_context (linkage
);
6949 /* If the next token is a `{', then we're using the first
6951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6953 /* Consume the `{' token. */
6954 cp_lexer_consume_token (parser
->lexer
);
6955 /* Parse the declarations. */
6956 cp_parser_declaration_seq_opt (parser
);
6957 /* Look for the closing `}'. */
6958 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6960 /* Otherwise, there's just one declaration. */
6963 bool saved_in_unbraced_linkage_specification_p
;
6965 saved_in_unbraced_linkage_specification_p
6966 = parser
->in_unbraced_linkage_specification_p
;
6967 parser
->in_unbraced_linkage_specification_p
= true;
6968 have_extern_spec
= true;
6969 cp_parser_declaration (parser
);
6970 have_extern_spec
= false;
6971 parser
->in_unbraced_linkage_specification_p
6972 = saved_in_unbraced_linkage_specification_p
;
6975 /* We're done with the linkage-specification. */
6976 pop_lang_context ();
6979 /* Special member functions [gram.special] */
6981 /* Parse a conversion-function-id.
6983 conversion-function-id:
6984 operator conversion-type-id
6986 Returns an IDENTIFIER_NODE representing the operator. */
6989 cp_parser_conversion_function_id (cp_parser
* parser
)
6993 tree saved_qualifying_scope
;
6994 tree saved_object_scope
;
6997 /* Look for the `operator' token. */
6998 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
6999 return error_mark_node
;
7000 /* When we parse the conversion-type-id, the current scope will be
7001 reset. However, we need that information in able to look up the
7002 conversion function later, so we save it here. */
7003 saved_scope
= parser
->scope
;
7004 saved_qualifying_scope
= parser
->qualifying_scope
;
7005 saved_object_scope
= parser
->object_scope
;
7006 /* We must enter the scope of the class so that the names of
7007 entities declared within the class are available in the
7008 conversion-type-id. For example, consider:
7015 S::operator I() { ... }
7017 In order to see that `I' is a type-name in the definition, we
7018 must be in the scope of `S'. */
7020 pop_p
= push_scope (saved_scope
);
7021 /* Parse the conversion-type-id. */
7022 type
= cp_parser_conversion_type_id (parser
);
7023 /* Leave the scope of the class, if any. */
7025 pop_scope (saved_scope
);
7026 /* Restore the saved scope. */
7027 parser
->scope
= saved_scope
;
7028 parser
->qualifying_scope
= saved_qualifying_scope
;
7029 parser
->object_scope
= saved_object_scope
;
7030 /* If the TYPE is invalid, indicate failure. */
7031 if (type
== error_mark_node
)
7032 return error_mark_node
;
7033 return mangle_conv_op_name_for_type (type
);
7036 /* Parse a conversion-type-id:
7039 type-specifier-seq conversion-declarator [opt]
7041 Returns the TYPE specified. */
7044 cp_parser_conversion_type_id (cp_parser
* parser
)
7047 tree type_specifiers
;
7050 /* Parse the attributes. */
7051 attributes
= cp_parser_attributes_opt (parser
);
7052 /* Parse the type-specifiers. */
7053 type_specifiers
= cp_parser_type_specifier_seq (parser
);
7054 /* If that didn't work, stop. */
7055 if (type_specifiers
== error_mark_node
)
7056 return error_mark_node
;
7057 /* Parse the conversion-declarator. */
7058 declarator
= cp_parser_conversion_declarator_opt (parser
);
7060 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
7061 /*initialized=*/0, &attributes
);
7064 /* Parse an (optional) conversion-declarator.
7066 conversion-declarator:
7067 ptr-operator conversion-declarator [opt]
7069 Returns a representation of the declarator. See
7070 cp_parser_declarator for details. */
7073 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7075 enum tree_code code
;
7077 tree cv_qualifier_seq
;
7079 /* We don't know if there's a ptr-operator next, or not. */
7080 cp_parser_parse_tentatively (parser
);
7081 /* Try the ptr-operator. */
7082 code
= cp_parser_ptr_operator (parser
, &class_type
,
7084 /* If it worked, look for more conversion-declarators. */
7085 if (cp_parser_parse_definitely (parser
))
7089 /* Parse another optional declarator. */
7090 declarator
= cp_parser_conversion_declarator_opt (parser
);
7092 /* Create the representation of the declarator. */
7093 if (code
== INDIRECT_REF
)
7094 declarator
= make_pointer_declarator (cv_qualifier_seq
,
7097 declarator
= make_reference_declarator (cv_qualifier_seq
,
7100 /* Handle the pointer-to-member case. */
7102 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
7110 /* Parse an (optional) ctor-initializer.
7113 : mem-initializer-list
7115 Returns TRUE iff the ctor-initializer was actually present. */
7118 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7120 /* If the next token is not a `:', then there is no
7121 ctor-initializer. */
7122 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7124 /* Do default initialization of any bases and members. */
7125 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7126 finish_mem_initializers (NULL_TREE
);
7131 /* Consume the `:' token. */
7132 cp_lexer_consume_token (parser
->lexer
);
7133 /* And the mem-initializer-list. */
7134 cp_parser_mem_initializer_list (parser
);
7139 /* Parse a mem-initializer-list.
7141 mem-initializer-list:
7143 mem-initializer , mem-initializer-list */
7146 cp_parser_mem_initializer_list (cp_parser
* parser
)
7148 tree mem_initializer_list
= NULL_TREE
;
7150 /* Let the semantic analysis code know that we are starting the
7151 mem-initializer-list. */
7152 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7153 error ("only constructors take base initializers");
7155 /* Loop through the list. */
7158 tree mem_initializer
;
7160 /* Parse the mem-initializer. */
7161 mem_initializer
= cp_parser_mem_initializer (parser
);
7162 /* Add it to the list, unless it was erroneous. */
7163 if (mem_initializer
)
7165 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7166 mem_initializer_list
= mem_initializer
;
7168 /* If the next token is not a `,', we're done. */
7169 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7171 /* Consume the `,' token. */
7172 cp_lexer_consume_token (parser
->lexer
);
7175 /* Perform semantic analysis. */
7176 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7177 finish_mem_initializers (mem_initializer_list
);
7180 /* Parse a mem-initializer.
7183 mem-initializer-id ( expression-list [opt] )
7188 ( expression-list [opt] )
7190 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7191 class) or FIELD_DECL (for a non-static data member) to initialize;
7192 the TREE_VALUE is the expression-list. */
7195 cp_parser_mem_initializer (cp_parser
* parser
)
7197 tree mem_initializer_id
;
7198 tree expression_list
;
7201 /* Find out what is being initialized. */
7202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7204 pedwarn ("anachronistic old-style base class initializer");
7205 mem_initializer_id
= NULL_TREE
;
7208 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7209 member
= expand_member_init (mem_initializer_id
);
7210 if (member
&& !DECL_P (member
))
7211 in_base_initializer
= 1;
7214 = cp_parser_parenthesized_expression_list (parser
, false,
7215 /*non_constant_p=*/NULL
);
7216 if (!expression_list
)
7217 expression_list
= void_type_node
;
7219 in_base_initializer
= 0;
7221 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7224 /* Parse a mem-initializer-id.
7227 :: [opt] nested-name-specifier [opt] class-name
7230 Returns a TYPE indicating the class to be initializer for the first
7231 production. Returns an IDENTIFIER_NODE indicating the data member
7232 to be initialized for the second production. */
7235 cp_parser_mem_initializer_id (cp_parser
* parser
)
7237 bool global_scope_p
;
7238 bool nested_name_specifier_p
;
7239 bool template_p
= false;
7242 /* `typename' is not allowed in this context ([temp.res]). */
7243 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
7245 error ("keyword `typename' not allowed in this context (a qualified "
7246 "member initializer is implicitly a type)");
7247 cp_lexer_consume_token (parser
->lexer
);
7249 /* Look for the optional `::' operator. */
7251 = (cp_parser_global_scope_opt (parser
,
7252 /*current_scope_valid_p=*/false)
7254 /* Look for the optional nested-name-specifier. The simplest way to
7259 The keyword `typename' is not permitted in a base-specifier or
7260 mem-initializer; in these contexts a qualified name that
7261 depends on a template-parameter is implicitly assumed to be a
7264 is to assume that we have seen the `typename' keyword at this
7266 nested_name_specifier_p
7267 = (cp_parser_nested_name_specifier_opt (parser
,
7268 /*typename_keyword_p=*/true,
7269 /*check_dependency_p=*/true,
7271 /*is_declaration=*/true)
7273 if (nested_name_specifier_p
)
7274 template_p
= cp_parser_optional_template_keyword (parser
);
7275 /* If there is a `::' operator or a nested-name-specifier, then we
7276 are definitely looking for a class-name. */
7277 if (global_scope_p
|| nested_name_specifier_p
)
7278 return cp_parser_class_name (parser
,
7279 /*typename_keyword_p=*/true,
7280 /*template_keyword_p=*/template_p
,
7282 /*check_dependency_p=*/true,
7283 /*class_head_p=*/false,
7284 /*is_declaration=*/true);
7285 /* Otherwise, we could also be looking for an ordinary identifier. */
7286 cp_parser_parse_tentatively (parser
);
7287 /* Try a class-name. */
7288 id
= cp_parser_class_name (parser
,
7289 /*typename_keyword_p=*/true,
7290 /*template_keyword_p=*/false,
7292 /*check_dependency_p=*/true,
7293 /*class_head_p=*/false,
7294 /*is_declaration=*/true);
7295 /* If we found one, we're done. */
7296 if (cp_parser_parse_definitely (parser
))
7298 /* Otherwise, look for an ordinary identifier. */
7299 return cp_parser_identifier (parser
);
7302 /* Overloading [gram.over] */
7304 /* Parse an operator-function-id.
7306 operator-function-id:
7309 Returns an IDENTIFIER_NODE for the operator which is a
7310 human-readable spelling of the identifier, e.g., `operator +'. */
7313 cp_parser_operator_function_id (cp_parser
* parser
)
7315 /* Look for the `operator' keyword. */
7316 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7317 return error_mark_node
;
7318 /* And then the name of the operator itself. */
7319 return cp_parser_operator (parser
);
7322 /* Parse an operator.
7325 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7326 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7327 || ++ -- , ->* -> () []
7334 Returns an IDENTIFIER_NODE for the operator which is a
7335 human-readable spelling of the identifier, e.g., `operator +'. */
7338 cp_parser_operator (cp_parser
* parser
)
7340 tree id
= NULL_TREE
;
7343 /* Peek at the next token. */
7344 token
= cp_lexer_peek_token (parser
->lexer
);
7345 /* Figure out which operator we have. */
7346 switch (token
->type
)
7352 /* The keyword should be either `new' or `delete'. */
7353 if (token
->keyword
== RID_NEW
)
7355 else if (token
->keyword
== RID_DELETE
)
7360 /* Consume the `new' or `delete' token. */
7361 cp_lexer_consume_token (parser
->lexer
);
7363 /* Peek at the next token. */
7364 token
= cp_lexer_peek_token (parser
->lexer
);
7365 /* If it's a `[' token then this is the array variant of the
7367 if (token
->type
== CPP_OPEN_SQUARE
)
7369 /* Consume the `[' token. */
7370 cp_lexer_consume_token (parser
->lexer
);
7371 /* Look for the `]' token. */
7372 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7373 id
= ansi_opname (op
== NEW_EXPR
7374 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7376 /* Otherwise, we have the non-array variant. */
7378 id
= ansi_opname (op
);
7384 id
= ansi_opname (PLUS_EXPR
);
7388 id
= ansi_opname (MINUS_EXPR
);
7392 id
= ansi_opname (MULT_EXPR
);
7396 id
= ansi_opname (TRUNC_DIV_EXPR
);
7400 id
= ansi_opname (TRUNC_MOD_EXPR
);
7404 id
= ansi_opname (BIT_XOR_EXPR
);
7408 id
= ansi_opname (BIT_AND_EXPR
);
7412 id
= ansi_opname (BIT_IOR_EXPR
);
7416 id
= ansi_opname (BIT_NOT_EXPR
);
7420 id
= ansi_opname (TRUTH_NOT_EXPR
);
7424 id
= ansi_assopname (NOP_EXPR
);
7428 id
= ansi_opname (LT_EXPR
);
7432 id
= ansi_opname (GT_EXPR
);
7436 id
= ansi_assopname (PLUS_EXPR
);
7440 id
= ansi_assopname (MINUS_EXPR
);
7444 id
= ansi_assopname (MULT_EXPR
);
7448 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7452 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7456 id
= ansi_assopname (BIT_XOR_EXPR
);
7460 id
= ansi_assopname (BIT_AND_EXPR
);
7464 id
= ansi_assopname (BIT_IOR_EXPR
);
7468 id
= ansi_opname (LSHIFT_EXPR
);
7472 id
= ansi_opname (RSHIFT_EXPR
);
7476 id
= ansi_assopname (LSHIFT_EXPR
);
7480 id
= ansi_assopname (RSHIFT_EXPR
);
7484 id
= ansi_opname (EQ_EXPR
);
7488 id
= ansi_opname (NE_EXPR
);
7492 id
= ansi_opname (LE_EXPR
);
7495 case CPP_GREATER_EQ
:
7496 id
= ansi_opname (GE_EXPR
);
7500 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7504 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7508 id
= ansi_opname (POSTINCREMENT_EXPR
);
7511 case CPP_MINUS_MINUS
:
7512 id
= ansi_opname (PREDECREMENT_EXPR
);
7516 id
= ansi_opname (COMPOUND_EXPR
);
7519 case CPP_DEREF_STAR
:
7520 id
= ansi_opname (MEMBER_REF
);
7524 id
= ansi_opname (COMPONENT_REF
);
7527 case CPP_OPEN_PAREN
:
7528 /* Consume the `('. */
7529 cp_lexer_consume_token (parser
->lexer
);
7530 /* Look for the matching `)'. */
7531 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7532 return ansi_opname (CALL_EXPR
);
7534 case CPP_OPEN_SQUARE
:
7535 /* Consume the `['. */
7536 cp_lexer_consume_token (parser
->lexer
);
7537 /* Look for the matching `]'. */
7538 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7539 return ansi_opname (ARRAY_REF
);
7543 id
= ansi_opname (MIN_EXPR
);
7547 id
= ansi_opname (MAX_EXPR
);
7551 id
= ansi_assopname (MIN_EXPR
);
7555 id
= ansi_assopname (MAX_EXPR
);
7559 /* Anything else is an error. */
7563 /* If we have selected an identifier, we need to consume the
7566 cp_lexer_consume_token (parser
->lexer
);
7567 /* Otherwise, no valid operator name was present. */
7570 cp_parser_error (parser
, "expected operator");
7571 id
= error_mark_node
;
7577 /* Parse a template-declaration.
7579 template-declaration:
7580 export [opt] template < template-parameter-list > declaration
7582 If MEMBER_P is TRUE, this template-declaration occurs within a
7585 The grammar rule given by the standard isn't correct. What
7588 template-declaration:
7589 export [opt] template-parameter-list-seq
7590 decl-specifier-seq [opt] init-declarator [opt] ;
7591 export [opt] template-parameter-list-seq
7594 template-parameter-list-seq:
7595 template-parameter-list-seq [opt]
7596 template < template-parameter-list > */
7599 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7601 /* Check for `export'. */
7602 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7604 /* Consume the `export' token. */
7605 cp_lexer_consume_token (parser
->lexer
);
7606 /* Warn that we do not support `export'. */
7607 warning ("keyword `export' not implemented, and will be ignored");
7610 cp_parser_template_declaration_after_export (parser
, member_p
);
7613 /* Parse a template-parameter-list.
7615 template-parameter-list:
7617 template-parameter-list , template-parameter
7619 Returns a TREE_LIST. Each node represents a template parameter.
7620 The nodes are connected via their TREE_CHAINs. */
7623 cp_parser_template_parameter_list (cp_parser
* parser
)
7625 tree parameter_list
= NULL_TREE
;
7632 /* Parse the template-parameter. */
7633 parameter
= cp_parser_template_parameter (parser
);
7634 /* Add it to the list. */
7635 parameter_list
= process_template_parm (parameter_list
,
7638 /* Peek at the next token. */
7639 token
= cp_lexer_peek_token (parser
->lexer
);
7640 /* If it's not a `,', we're done. */
7641 if (token
->type
!= CPP_COMMA
)
7643 /* Otherwise, consume the `,' token. */
7644 cp_lexer_consume_token (parser
->lexer
);
7647 return parameter_list
;
7650 /* Parse a template-parameter.
7654 parameter-declaration
7656 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7657 TREE_PURPOSE is the default value, if any. */
7660 cp_parser_template_parameter (cp_parser
* parser
)
7664 /* Peek at the next token. */
7665 token
= cp_lexer_peek_token (parser
->lexer
);
7666 /* If it is `class' or `template', we have a type-parameter. */
7667 if (token
->keyword
== RID_TEMPLATE
)
7668 return cp_parser_type_parameter (parser
);
7669 /* If it is `class' or `typename' we do not know yet whether it is a
7670 type parameter or a non-type parameter. Consider:
7672 template <typename T, typename T::X X> ...
7676 template <class C, class D*> ...
7678 Here, the first parameter is a type parameter, and the second is
7679 a non-type parameter. We can tell by looking at the token after
7680 the identifier -- if it is a `,', `=', or `>' then we have a type
7682 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7684 /* Peek at the token after `class' or `typename'. */
7685 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7686 /* If it's an identifier, skip it. */
7687 if (token
->type
== CPP_NAME
)
7688 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7689 /* Now, see if the token looks like the end of a template
7691 if (token
->type
== CPP_COMMA
7692 || token
->type
== CPP_EQ
7693 || token
->type
== CPP_GREATER
)
7694 return cp_parser_type_parameter (parser
);
7697 /* Otherwise, it is a non-type parameter.
7701 When parsing a default template-argument for a non-type
7702 template-parameter, the first non-nested `>' is taken as the end
7703 of the template parameter-list rather than a greater-than
7706 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
7707 /*parenthesized_p=*/NULL
);
7710 /* Parse a type-parameter.
7713 class identifier [opt]
7714 class identifier [opt] = type-id
7715 typename identifier [opt]
7716 typename identifier [opt] = type-id
7717 template < template-parameter-list > class identifier [opt]
7718 template < template-parameter-list > class identifier [opt]
7721 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7722 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7723 the declaration of the parameter. */
7726 cp_parser_type_parameter (cp_parser
* parser
)
7731 /* Look for a keyword to tell us what kind of parameter this is. */
7732 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7733 "`class', `typename', or `template'");
7735 return error_mark_node
;
7737 switch (token
->keyword
)
7743 tree default_argument
;
7745 /* If the next token is an identifier, then it names the
7747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7748 identifier
= cp_parser_identifier (parser
);
7750 identifier
= NULL_TREE
;
7752 /* Create the parameter. */
7753 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7755 /* If the next token is an `=', we have a default argument. */
7756 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7758 /* Consume the `=' token. */
7759 cp_lexer_consume_token (parser
->lexer
);
7760 /* Parse the default-argument. */
7761 default_argument
= cp_parser_type_id (parser
);
7764 default_argument
= NULL_TREE
;
7766 /* Create the combined representation of the parameter and the
7767 default argument. */
7768 parameter
= build_tree_list (default_argument
, parameter
);
7774 tree parameter_list
;
7776 tree default_argument
;
7778 /* Look for the `<'. */
7779 cp_parser_require (parser
, CPP_LESS
, "`<'");
7780 /* Parse the template-parameter-list. */
7781 begin_template_parm_list ();
7783 = cp_parser_template_parameter_list (parser
);
7784 parameter_list
= end_template_parm_list (parameter_list
);
7785 /* Look for the `>'. */
7786 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7787 /* Look for the `class' keyword. */
7788 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7789 /* If the next token is an `=', then there is a
7790 default-argument. If the next token is a `>', we are at
7791 the end of the parameter-list. If the next token is a `,',
7792 then we are at the end of this parameter. */
7793 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7794 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7795 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7797 identifier
= cp_parser_identifier (parser
);
7798 /* Treat invalid names as if the parameter were nameless. */
7799 if (identifier
== error_mark_node
)
7800 identifier
= NULL_TREE
;
7803 identifier
= NULL_TREE
;
7805 /* Create the template parameter. */
7806 parameter
= finish_template_template_parm (class_type_node
,
7809 /* If the next token is an `=', then there is a
7810 default-argument. */
7811 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7815 /* Consume the `='. */
7816 cp_lexer_consume_token (parser
->lexer
);
7817 /* Parse the id-expression. */
7819 = cp_parser_id_expression (parser
,
7820 /*template_keyword_p=*/false,
7821 /*check_dependency_p=*/true,
7822 /*template_p=*/&is_template
,
7823 /*declarator_p=*/false);
7824 if (TREE_CODE (default_argument
) == TYPE_DECL
)
7825 /* If the id-expression was a template-id that refers to
7826 a template-class, we already have the declaration here,
7827 so no further lookup is needed. */
7830 /* Look up the name. */
7832 = cp_parser_lookup_name (parser
, default_argument
,
7834 /*is_template=*/is_template
,
7835 /*is_namespace=*/false,
7836 /*check_dependency=*/true);
7837 /* See if the default argument is valid. */
7839 = check_template_template_default_arg (default_argument
);
7842 default_argument
= NULL_TREE
;
7844 /* Create the combined representation of the parameter and the
7845 default argument. */
7846 parameter
= build_tree_list (default_argument
, parameter
);
7858 /* Parse a template-id.
7861 template-name < template-argument-list [opt] >
7863 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7864 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7865 returned. Otherwise, if the template-name names a function, or set
7866 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7867 names a class, returns a TYPE_DECL for the specialization.
7869 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7870 uninstantiated templates. */
7873 cp_parser_template_id (cp_parser
*parser
,
7874 bool template_keyword_p
,
7875 bool check_dependency_p
,
7876 bool is_declaration
)
7881 ptrdiff_t start_of_id
;
7882 tree access_check
= NULL_TREE
;
7883 cp_token
*next_token
, *next_token_2
;
7886 /* If the next token corresponds to a template-id, there is no need
7888 next_token
= cp_lexer_peek_token (parser
->lexer
);
7889 if (next_token
->type
== CPP_TEMPLATE_ID
)
7894 /* Get the stored value. */
7895 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
7896 /* Perform any access checks that were deferred. */
7897 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
7898 perform_or_defer_access_check (TREE_PURPOSE (check
),
7899 TREE_VALUE (check
));
7900 /* Return the stored value. */
7901 return TREE_VALUE (value
);
7904 /* Avoid performing name lookup if there is no possibility of
7905 finding a template-id. */
7906 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
7907 || (next_token
->type
== CPP_NAME
7908 && !cp_parser_nth_token_starts_template_argument_list_p
7911 cp_parser_error (parser
, "expected template-id");
7912 return error_mark_node
;
7915 /* Remember where the template-id starts. */
7916 if (cp_parser_parsing_tentatively (parser
)
7917 && !cp_parser_committed_to_tentative_parse (parser
))
7919 next_token
= cp_lexer_peek_token (parser
->lexer
);
7920 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
7921 parser
->lexer
->first_token
,
7927 push_deferring_access_checks (dk_deferred
);
7929 /* Parse the template-name. */
7930 is_identifier
= false;
7931 template = cp_parser_template_name (parser
, template_keyword_p
,
7935 if (template == error_mark_node
|| is_identifier
)
7937 pop_deferring_access_checks ();
7941 /* If we find the sequence `[:' after a template-name, it's probably
7942 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7943 parse correctly the argument list. */
7944 next_token
= cp_lexer_peek_nth_token (parser
->lexer
, 1);
7945 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7946 if (next_token
->type
== CPP_OPEN_SQUARE
7947 && next_token
->flags
& DIGRAPH
7948 && next_token_2
->type
== CPP_COLON
7949 && !(next_token_2
->flags
& PREV_WHITE
))
7951 cp_parser_parse_tentatively (parser
);
7952 /* Change `:' into `::'. */
7953 next_token_2
->type
= CPP_SCOPE
;
7954 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7956 cp_lexer_consume_token (parser
->lexer
);
7957 /* Parse the arguments. */
7958 arguments
= cp_parser_enclosed_template_argument_list (parser
);
7959 if (!cp_parser_parse_definitely (parser
))
7961 /* If we couldn't parse an argument list, then we revert our changes
7962 and return simply an error. Maybe this is not a template-id
7964 next_token_2
->type
= CPP_COLON
;
7965 cp_parser_error (parser
, "expected `<'");
7966 pop_deferring_access_checks ();
7967 return error_mark_node
;
7969 /* Otherwise, emit an error about the invalid digraph, but continue
7970 parsing because we got our argument list. */
7971 pedwarn ("`<::' cannot begin a template-argument list");
7972 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7973 "between `<' and `::'");
7974 if (!flag_permissive
)
7979 inform ("(if you use `-fpermissive' G++ will accept your code)");
7986 /* Look for the `<' that starts the template-argument-list. */
7987 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
7989 pop_deferring_access_checks ();
7990 return error_mark_node
;
7992 /* Parse the arguments. */
7993 arguments
= cp_parser_enclosed_template_argument_list (parser
);
7996 /* Build a representation of the specialization. */
7997 if (TREE_CODE (template) == IDENTIFIER_NODE
)
7998 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
7999 else if (DECL_CLASS_TEMPLATE_P (template)
8000 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8002 = finish_template_type (template, arguments
,
8003 cp_lexer_next_token_is (parser
->lexer
,
8007 /* If it's not a class-template or a template-template, it should be
8008 a function-template. */
8009 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8010 || TREE_CODE (template) == OVERLOAD
8011 || BASELINK_P (template)),
8014 template_id
= lookup_template_function (template, arguments
);
8017 /* Retrieve any deferred checks. Do not pop this access checks yet
8018 so the memory will not be reclaimed during token replacing below. */
8019 access_check
= get_deferred_access_checks ();
8021 /* If parsing tentatively, replace the sequence of tokens that makes
8022 up the template-id with a CPP_TEMPLATE_ID token. That way,
8023 should we re-parse the token stream, we will not have to repeat
8024 the effort required to do the parse, nor will we issue duplicate
8025 error messages about problems during instantiation of the
8027 if (start_of_id
>= 0)
8031 /* Find the token that corresponds to the start of the
8033 token
= cp_lexer_advance_token (parser
->lexer
,
8034 parser
->lexer
->first_token
,
8037 /* Reset the contents of the START_OF_ID token. */
8038 token
->type
= CPP_TEMPLATE_ID
;
8039 token
->value
= build_tree_list (access_check
, template_id
);
8040 token
->keyword
= RID_MAX
;
8041 /* Purge all subsequent tokens. */
8042 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8044 /* ??? Can we actually assume that, if template_id ==
8045 error_mark_node, we will have issued a diagnostic to the
8046 user, as opposed to simply marking the tentative parse as
8048 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8049 error ("parse error in template argument list");
8052 pop_deferring_access_checks ();
8056 /* Parse a template-name.
8061 The standard should actually say:
8065 operator-function-id
8067 A defect report has been filed about this issue.
8069 A conversion-function-id cannot be a template name because they cannot
8070 be part of a template-id. In fact, looking at this code:
8074 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8075 It is impossible to call a templated conversion-function-id with an
8076 explicit argument list, since the only allowed template parameter is
8077 the type to which it is converting.
8079 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8080 `template' keyword, in a construction like:
8084 In that case `f' is taken to be a template-name, even though there
8085 is no way of knowing for sure.
8087 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8088 name refers to a set of overloaded functions, at least one of which
8089 is a template, or an IDENTIFIER_NODE with the name of the template,
8090 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8091 names are looked up inside uninstantiated templates. */
8094 cp_parser_template_name (cp_parser
* parser
,
8095 bool template_keyword_p
,
8096 bool check_dependency_p
,
8097 bool is_declaration
,
8098 bool *is_identifier
)
8104 /* If the next token is `operator', then we have either an
8105 operator-function-id or a conversion-function-id. */
8106 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8108 /* We don't know whether we're looking at an
8109 operator-function-id or a conversion-function-id. */
8110 cp_parser_parse_tentatively (parser
);
8111 /* Try an operator-function-id. */
8112 identifier
= cp_parser_operator_function_id (parser
);
8113 /* If that didn't work, try a conversion-function-id. */
8114 if (!cp_parser_parse_definitely (parser
))
8116 cp_parser_error (parser
, "expected template-name");
8117 return error_mark_node
;
8120 /* Look for the identifier. */
8122 identifier
= cp_parser_identifier (parser
);
8124 /* If we didn't find an identifier, we don't have a template-id. */
8125 if (identifier
== error_mark_node
)
8126 return error_mark_node
;
8128 /* If the name immediately followed the `template' keyword, then it
8129 is a template-name. However, if the next token is not `<', then
8130 we do not treat it as a template-name, since it is not being used
8131 as part of a template-id. This enables us to handle constructs
8134 template <typename T> struct S { S(); };
8135 template <typename T> S<T>::S();
8137 correctly. We would treat `S' as a template -- if it were `S<T>'
8138 -- but we do not if there is no `<'. */
8140 if (processing_template_decl
8141 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8143 /* In a declaration, in a dependent context, we pretend that the
8144 "template" keyword was present in order to improve error
8145 recovery. For example, given:
8147 template <typename T> void f(T::X<int>);
8149 we want to treat "X<int>" as a template-id. */
8151 && !template_keyword_p
8152 && parser
->scope
&& TYPE_P (parser
->scope
)
8153 && check_dependency_p
8154 && dependent_type_p (parser
->scope
)
8155 /* Do not do this for dtors (or ctors), since they never
8156 need the template keyword before their name. */
8157 && !constructor_name_p (identifier
, parser
->scope
))
8161 /* Explain what went wrong. */
8162 error ("non-template `%D' used as template", identifier
);
8163 inform ("use `%T::template %D' to indicate that it is a template",
8164 parser
->scope
, identifier
);
8165 /* If parsing tentatively, find the location of the "<"
8167 if (cp_parser_parsing_tentatively (parser
)
8168 && !cp_parser_committed_to_tentative_parse (parser
))
8170 cp_parser_simulate_error (parser
);
8171 token
= cp_lexer_peek_token (parser
->lexer
);
8172 token
= cp_lexer_prev_token (parser
->lexer
, token
);
8173 start
= cp_lexer_token_difference (parser
->lexer
,
8174 parser
->lexer
->first_token
,
8179 /* Parse the template arguments so that we can issue error
8180 messages about them. */
8181 cp_lexer_consume_token (parser
->lexer
);
8182 cp_parser_enclosed_template_argument_list (parser
);
8183 /* Skip tokens until we find a good place from which to
8184 continue parsing. */
8185 cp_parser_skip_to_closing_parenthesis (parser
,
8186 /*recovering=*/true,
8188 /*consume_paren=*/false);
8189 /* If parsing tentatively, permanently remove the
8190 template argument list. That will prevent duplicate
8191 error messages from being issued about the missing
8192 "template" keyword. */
8195 token
= cp_lexer_advance_token (parser
->lexer
,
8196 parser
->lexer
->first_token
,
8198 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8201 *is_identifier
= true;
8205 /* If the "template" keyword is present, then there is generally
8206 no point in doing name-lookup, so we just return IDENTIFIER.
8207 But, if the qualifying scope is non-dependent then we can
8208 (and must) do name-lookup normally. */
8209 if (template_keyword_p
8211 || (TYPE_P (parser
->scope
)
8212 && dependent_type_p (parser
->scope
))))
8216 /* Look up the name. */
8217 decl
= cp_parser_lookup_name (parser
, identifier
,
8219 /*is_template=*/false,
8220 /*is_namespace=*/false,
8221 check_dependency_p
);
8222 decl
= maybe_get_template_decl_from_type_decl (decl
);
8224 /* If DECL is a template, then the name was a template-name. */
8225 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8229 tree fn
= NULL_TREE
;
8231 /* The standard does not explicitly indicate whether a name that
8232 names a set of overloaded declarations, some of which are
8233 templates, is a template-name. However, such a name should
8234 be a template-name; otherwise, there is no way to form a
8235 template-id for the overloaded templates. */
8236 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8237 if (TREE_CODE (fns
) == OVERLOAD
)
8238 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8239 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8244 /* Otherwise, the name does not name a template. */
8245 cp_parser_error (parser
, "expected template-name");
8246 return error_mark_node
;
8250 /* If DECL is dependent, and refers to a function, then just return
8251 its name; we will look it up again during template instantiation. */
8252 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8254 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8255 if (TYPE_P (scope
) && dependent_type_p (scope
))
8262 /* Parse a template-argument-list.
8264 template-argument-list:
8266 template-argument-list , template-argument
8268 Returns a TREE_VEC containing the arguments. */
8271 cp_parser_template_argument_list (cp_parser
* parser
)
8273 tree fixed_args
[10];
8274 unsigned n_args
= 0;
8275 unsigned alloced
= 10;
8276 tree
*arg_ary
= fixed_args
;
8278 bool saved_in_template_argument_list_p
;
8280 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8281 parser
->in_template_argument_list_p
= true;
8287 /* Consume the comma. */
8288 cp_lexer_consume_token (parser
->lexer
);
8290 /* Parse the template-argument. */
8291 argument
= cp_parser_template_argument (parser
);
8292 if (n_args
== alloced
)
8296 if (arg_ary
== fixed_args
)
8298 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8299 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8302 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8304 arg_ary
[n_args
++] = argument
;
8306 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8308 vec
= make_tree_vec (n_args
);
8311 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8313 if (arg_ary
!= fixed_args
)
8315 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8319 /* Parse a template-argument.
8322 assignment-expression
8326 The representation is that of an assignment-expression, type-id, or
8327 id-expression -- except that the qualified id-expression is
8328 evaluated, so that the value returned is either a DECL or an
8331 Although the standard says "assignment-expression", it forbids
8332 throw-expressions or assignments in the template argument.
8333 Therefore, we use "conditional-expression" instead. */
8336 cp_parser_template_argument (cp_parser
* parser
)
8341 bool maybe_type_id
= false;
8344 tree qualifying_class
;
8346 /* There's really no way to know what we're looking at, so we just
8347 try each alternative in order.
8351 In a template-argument, an ambiguity between a type-id and an
8352 expression is resolved to a type-id, regardless of the form of
8353 the corresponding template-parameter.
8355 Therefore, we try a type-id first. */
8356 cp_parser_parse_tentatively (parser
);
8357 argument
= cp_parser_type_id (parser
);
8358 /* If there was no error parsing the type-id but the next token is a '>>',
8359 we probably found a typo for '> >'. But there are type-id which are
8360 also valid expressions. For instance:
8362 struct X { int operator >> (int); };
8363 template <int V> struct Foo {};
8366 Here 'X()' is a valid type-id of a function type, but the user just
8367 wanted to write the expression "X() >> 5". Thus, we remember that we
8368 found a valid type-id, but we still try to parse the argument as an
8369 expression to see what happens. */
8370 if (!cp_parser_error_occurred (parser
)
8371 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8373 maybe_type_id
= true;
8374 cp_parser_abort_tentative_parse (parser
);
8378 /* If the next token isn't a `,' or a `>', then this argument wasn't
8379 really finished. This means that the argument is not a valid
8381 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8382 cp_parser_error (parser
, "expected template-argument");
8383 /* If that worked, we're done. */
8384 if (cp_parser_parse_definitely (parser
))
8387 /* We're still not sure what the argument will be. */
8388 cp_parser_parse_tentatively (parser
);
8389 /* Try a template. */
8390 argument
= cp_parser_id_expression (parser
,
8391 /*template_keyword_p=*/false,
8392 /*check_dependency_p=*/true,
8394 /*declarator_p=*/false);
8395 /* If the next token isn't a `,' or a `>', then this argument wasn't
8397 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8398 cp_parser_error (parser
, "expected template-argument");
8399 if (!cp_parser_error_occurred (parser
))
8401 /* Figure out what is being referred to. If the id-expression
8402 was for a class template specialization, then we will have a
8403 TYPE_DECL at this point. There is no need to do name lookup
8404 at this point in that case. */
8405 if (TREE_CODE (argument
) != TYPE_DECL
)
8406 argument
= cp_parser_lookup_name (parser
, argument
,
8408 /*is_template=*/template_p
,
8409 /*is_namespace=*/false,
8410 /*check_dependency=*/true);
8411 if (TREE_CODE (argument
) != TEMPLATE_DECL
8412 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8413 cp_parser_error (parser
, "expected template-name");
8415 if (cp_parser_parse_definitely (parser
))
8417 /* It must be a non-type argument. There permitted cases are given
8418 in [temp.arg.nontype]:
8420 -- an integral constant-expression of integral or enumeration
8423 -- the name of a non-type template-parameter; or
8425 -- the name of an object or function with external linkage...
8427 -- the address of an object or function with external linkage...
8429 -- a pointer to member... */
8430 /* Look for a non-type template parameter. */
8431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8433 cp_parser_parse_tentatively (parser
);
8434 argument
= cp_parser_primary_expression (parser
,
8437 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8438 || !cp_parser_next_token_ends_template_argument_p (parser
))
8439 cp_parser_simulate_error (parser
);
8440 if (cp_parser_parse_definitely (parser
))
8443 /* If the next token is "&", the argument must be the address of an
8444 object or function with external linkage. */
8445 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8447 cp_lexer_consume_token (parser
->lexer
);
8448 /* See if we might have an id-expression. */
8449 token
= cp_lexer_peek_token (parser
->lexer
);
8450 if (token
->type
== CPP_NAME
8451 || token
->keyword
== RID_OPERATOR
8452 || token
->type
== CPP_SCOPE
8453 || token
->type
== CPP_TEMPLATE_ID
8454 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8456 cp_parser_parse_tentatively (parser
);
8457 argument
= cp_parser_primary_expression (parser
,
8460 if (cp_parser_error_occurred (parser
)
8461 || !cp_parser_next_token_ends_template_argument_p (parser
))
8462 cp_parser_abort_tentative_parse (parser
);
8465 if (qualifying_class
)
8466 argument
= finish_qualified_id_expr (qualifying_class
,
8470 if (TREE_CODE (argument
) == VAR_DECL
)
8472 /* A variable without external linkage might still be a
8473 valid constant-expression, so no error is issued here
8474 if the external-linkage check fails. */
8475 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8476 cp_parser_simulate_error (parser
);
8478 else if (is_overloaded_fn (argument
))
8479 /* All overloaded functions are allowed; if the external
8480 linkage test does not pass, an error will be issued
8484 && (TREE_CODE (argument
) == OFFSET_REF
8485 || TREE_CODE (argument
) == SCOPE_REF
))
8486 /* A pointer-to-member. */
8489 cp_parser_simulate_error (parser
);
8491 if (cp_parser_parse_definitely (parser
))
8494 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8499 /* If the argument started with "&", there are no other valid
8500 alternatives at this point. */
8503 cp_parser_error (parser
, "invalid non-type template argument");
8504 return error_mark_node
;
8506 /* If the argument wasn't successfully parsed as a type-id followed
8507 by '>>', the argument can only be a constant expression now.
8508 Otherwise, we try parsing the constant-expression tentatively,
8509 because the argument could really be a type-id. */
8511 cp_parser_parse_tentatively (parser
);
8512 argument
= cp_parser_constant_expression (parser
,
8513 /*allow_non_constant_p=*/false,
8514 /*non_constant_p=*/NULL
);
8515 argument
= fold_non_dependent_expr (argument
);
8518 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8519 cp_parser_error (parser
, "expected template-argument");
8520 if (cp_parser_parse_definitely (parser
))
8522 /* We did our best to parse the argument as a non type-id, but that
8523 was the only alternative that matched (albeit with a '>' after
8524 it). We can assume it's just a typo from the user, and a
8525 diagnostic will then be issued. */
8526 return cp_parser_type_id (parser
);
8529 /* Parse an explicit-instantiation.
8531 explicit-instantiation:
8532 template declaration
8534 Although the standard says `declaration', what it really means is:
8536 explicit-instantiation:
8537 template decl-specifier-seq [opt] declarator [opt] ;
8539 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8540 supposed to be allowed. A defect report has been filed about this
8545 explicit-instantiation:
8546 storage-class-specifier template
8547 decl-specifier-seq [opt] declarator [opt] ;
8548 function-specifier template
8549 decl-specifier-seq [opt] declarator [opt] ; */
8552 cp_parser_explicit_instantiation (cp_parser
* parser
)
8554 int declares_class_or_enum
;
8555 tree decl_specifiers
;
8557 tree extension_specifier
= NULL_TREE
;
8559 /* Look for an (optional) storage-class-specifier or
8560 function-specifier. */
8561 if (cp_parser_allow_gnu_extensions_p (parser
))
8564 = cp_parser_storage_class_specifier_opt (parser
);
8565 if (!extension_specifier
)
8566 extension_specifier
= cp_parser_function_specifier_opt (parser
);
8569 /* Look for the `template' keyword. */
8570 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8571 /* Let the front end know that we are processing an explicit
8573 begin_explicit_instantiation ();
8574 /* [temp.explicit] says that we are supposed to ignore access
8575 control while processing explicit instantiation directives. */
8576 push_deferring_access_checks (dk_no_check
);
8577 /* Parse a decl-specifier-seq. */
8579 = cp_parser_decl_specifier_seq (parser
,
8580 CP_PARSER_FLAGS_OPTIONAL
,
8582 &declares_class_or_enum
);
8583 /* If there was exactly one decl-specifier, and it declared a class,
8584 and there's no declarator, then we have an explicit type
8586 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8590 type
= check_tag_decl (decl_specifiers
);
8591 /* Turn access control back on for names used during
8592 template instantiation. */
8593 pop_deferring_access_checks ();
8595 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8602 /* Parse the declarator. */
8604 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8605 /*ctor_dtor_or_conv_p=*/NULL
,
8606 /*parenthesized_p=*/NULL
,
8607 /*member_p=*/false);
8608 if (declares_class_or_enum
& 2)
8609 cp_parser_check_for_definition_in_return_type
8610 (declarator
, TREE_VALUE (decl_specifiers
));
8611 if (declarator
!= error_mark_node
)
8613 decl
= grokdeclarator (declarator
, decl_specifiers
,
8615 /* Turn access control back on for names used during
8616 template instantiation. */
8617 pop_deferring_access_checks ();
8618 /* Do the explicit instantiation. */
8619 do_decl_instantiation (decl
, extension_specifier
);
8623 pop_deferring_access_checks ();
8624 /* Skip the body of the explicit instantiation. */
8625 cp_parser_skip_to_end_of_statement (parser
);
8628 /* We're done with the instantiation. */
8629 end_explicit_instantiation ();
8631 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8634 /* Parse an explicit-specialization.
8636 explicit-specialization:
8637 template < > declaration
8639 Although the standard says `declaration', what it really means is:
8641 explicit-specialization:
8642 template <> decl-specifier [opt] init-declarator [opt] ;
8643 template <> function-definition
8644 template <> explicit-specialization
8645 template <> template-declaration */
8648 cp_parser_explicit_specialization (cp_parser
* parser
)
8650 /* Look for the `template' keyword. */
8651 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8652 /* Look for the `<'. */
8653 cp_parser_require (parser
, CPP_LESS
, "`<'");
8654 /* Look for the `>'. */
8655 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8656 /* We have processed another parameter list. */
8657 ++parser
->num_template_parameter_lists
;
8658 /* Let the front end know that we are beginning a specialization. */
8659 begin_specialization ();
8661 /* If the next keyword is `template', we need to figure out whether
8662 or not we're looking a template-declaration. */
8663 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8665 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8666 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8667 cp_parser_template_declaration_after_export (parser
,
8668 /*member_p=*/false);
8670 cp_parser_explicit_specialization (parser
);
8673 /* Parse the dependent declaration. */
8674 cp_parser_single_declaration (parser
,
8678 /* We're done with the specialization. */
8679 end_specialization ();
8680 /* We're done with this parameter list. */
8681 --parser
->num_template_parameter_lists
;
8684 /* Parse a type-specifier.
8687 simple-type-specifier
8690 elaborated-type-specifier
8698 Returns a representation of the type-specifier. If the
8699 type-specifier is a keyword (like `int' or `const', or
8700 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8701 For a class-specifier, enum-specifier, or elaborated-type-specifier
8702 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8704 If IS_FRIEND is TRUE then this type-specifier is being declared a
8705 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8706 appearing in a decl-specifier-seq.
8708 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8709 class-specifier, enum-specifier, or elaborated-type-specifier, then
8710 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8711 if a type is declared; 2 if it is defined. Otherwise, it is set to
8714 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8715 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8719 cp_parser_type_specifier (cp_parser
* parser
,
8720 cp_parser_flags flags
,
8722 bool is_declaration
,
8723 int* declares_class_or_enum
,
8724 bool* is_cv_qualifier
)
8726 tree type_spec
= NULL_TREE
;
8730 /* Assume this type-specifier does not declare a new type. */
8731 if (declares_class_or_enum
)
8732 *declares_class_or_enum
= 0;
8733 /* And that it does not specify a cv-qualifier. */
8734 if (is_cv_qualifier
)
8735 *is_cv_qualifier
= false;
8736 /* Peek at the next token. */
8737 token
= cp_lexer_peek_token (parser
->lexer
);
8739 /* If we're looking at a keyword, we can use that to guide the
8740 production we choose. */
8741 keyword
= token
->keyword
;
8745 /* 'enum' [identifier] '{' introduces an enum-specifier;
8746 'enum' <anything else> introduces an elaborated-type-specifier. */
8747 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_BRACE
8748 || (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
8749 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
8752 if (parser
->num_template_parameter_lists
)
8754 error ("template declaration of `enum'");
8755 cp_parser_skip_to_end_of_block_or_statement (parser
);
8756 type_spec
= error_mark_node
;
8759 type_spec
= cp_parser_enum_specifier (parser
);
8761 if (declares_class_or_enum
)
8762 *declares_class_or_enum
= 2;
8766 goto elaborated_type_specifier
;
8768 /* Any of these indicate either a class-specifier, or an
8769 elaborated-type-specifier. */
8773 /* Parse tentatively so that we can back up if we don't find a
8774 class-specifier or enum-specifier. */
8775 cp_parser_parse_tentatively (parser
);
8776 /* Look for the class-specifier. */
8777 type_spec
= cp_parser_class_specifier (parser
);
8778 /* If that worked, we're done. */
8779 if (cp_parser_parse_definitely (parser
))
8781 if (declares_class_or_enum
)
8782 *declares_class_or_enum
= 2;
8789 elaborated_type_specifier
:
8790 /* Look for an elaborated-type-specifier. */
8791 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8794 /* We're declaring a class or enum -- unless we're using
8796 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8797 *declares_class_or_enum
= 1;
8803 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8804 /* Even though we call a routine that looks for an optional
8805 qualifier, we know that there should be one. */
8806 my_friendly_assert (type_spec
!= NULL
, 20000328);
8807 /* This type-specifier was a cv-qualified. */
8808 if (is_cv_qualifier
)
8809 *is_cv_qualifier
= true;
8814 /* The `__complex__' keyword is a GNU extension. */
8815 return cp_lexer_consume_token (parser
->lexer
)->value
;
8821 /* If we do not already have a type-specifier, assume we are looking
8822 at a simple-type-specifier. */
8823 type_spec
= cp_parser_simple_type_specifier (parser
, flags
,
8824 /*identifier_p=*/true);
8826 /* If we didn't find a type-specifier, and a type-specifier was not
8827 optional in this context, issue an error message. */
8828 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8830 cp_parser_error (parser
, "expected type specifier");
8831 return error_mark_node
;
8837 /* Parse a simple-type-specifier.
8839 simple-type-specifier:
8840 :: [opt] nested-name-specifier [opt] type-name
8841 :: [opt] nested-name-specifier template template-id
8856 simple-type-specifier:
8857 __typeof__ unary-expression
8858 __typeof__ ( type-id )
8860 For the various keywords, the value returned is simply the
8861 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8862 For the first two productions, and if IDENTIFIER_P is false, the
8863 value returned is the indicated TYPE_DECL. */
8866 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
,
8869 tree type
= NULL_TREE
;
8872 /* Peek at the next token. */
8873 token
= cp_lexer_peek_token (parser
->lexer
);
8875 /* If we're looking at a keyword, things are easy. */
8876 switch (token
->keyword
)
8879 type
= char_type_node
;
8882 type
= wchar_type_node
;
8885 type
= boolean_type_node
;
8888 type
= short_integer_type_node
;
8891 type
= integer_type_node
;
8894 type
= long_integer_type_node
;
8897 type
= integer_type_node
;
8900 type
= unsigned_type_node
;
8903 type
= float_type_node
;
8906 type
= double_type_node
;
8909 type
= void_type_node
;
8916 /* Consume the `typeof' token. */
8917 cp_lexer_consume_token (parser
->lexer
);
8918 /* Parse the operand to `typeof'. */
8919 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
8920 /* If it is not already a TYPE, take its type. */
8921 if (!TYPE_P (operand
))
8922 operand
= finish_typeof (operand
);
8931 /* If the type-specifier was for a built-in type, we're done. */
8936 /* Consume the token. */
8937 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
8939 /* There is no valid C++ program where a non-template type is
8940 followed by a "<". That usually indicates that the user thought
8941 that the type was a template. */
8942 cp_parser_check_for_invalid_template_id (parser
, type
);
8944 return identifier_p
? id
: TYPE_NAME (type
);
8947 /* The type-specifier must be a user-defined type. */
8948 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
8953 /* Don't gobble tokens or issue error messages if this is an
8954 optional type-specifier. */
8955 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
8956 cp_parser_parse_tentatively (parser
);
8958 /* Look for the optional `::' operator. */
8960 = (cp_parser_global_scope_opt (parser
,
8961 /*current_scope_valid_p=*/false)
8963 /* Look for the nested-name specifier. */
8965 = (cp_parser_nested_name_specifier_opt (parser
,
8966 /*typename_keyword_p=*/false,
8967 /*check_dependency_p=*/true,
8969 /*is_declaration=*/false)
8971 /* If we have seen a nested-name-specifier, and the next token
8972 is `template', then we are using the template-id production. */
8974 && cp_parser_optional_template_keyword (parser
))
8976 /* Look for the template-id. */
8977 type
= cp_parser_template_id (parser
,
8978 /*template_keyword_p=*/true,
8979 /*check_dependency_p=*/true,
8980 /*is_declaration=*/false);
8981 /* If the template-id did not name a type, we are out of
8983 if (TREE_CODE (type
) != TYPE_DECL
)
8985 cp_parser_error (parser
, "expected template-id for type");
8989 /* Otherwise, look for a type-name. */
8991 type
= cp_parser_type_name (parser
);
8992 /* Keep track of all name-lookups performed in class scopes. */
8996 && TREE_CODE (type
) == TYPE_DECL
8997 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
8998 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
8999 /* If it didn't work out, we don't have a TYPE. */
9000 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9001 && !cp_parser_parse_definitely (parser
))
9005 /* If we didn't get a type-name, issue an error message. */
9006 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9008 cp_parser_error (parser
, "expected type-name");
9009 return error_mark_node
;
9012 /* There is no valid C++ program where a non-template type is
9013 followed by a "<". That usually indicates that the user thought
9014 that the type was a template. */
9015 if (type
&& type
!= error_mark_node
)
9016 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9021 /* Parse a type-name.
9034 Returns a TYPE_DECL for the the type. */
9037 cp_parser_type_name (cp_parser
* parser
)
9042 /* We can't know yet whether it is a class-name or not. */
9043 cp_parser_parse_tentatively (parser
);
9044 /* Try a class-name. */
9045 type_decl
= cp_parser_class_name (parser
,
9046 /*typename_keyword_p=*/false,
9047 /*template_keyword_p=*/false,
9049 /*check_dependency_p=*/true,
9050 /*class_head_p=*/false,
9051 /*is_declaration=*/false);
9052 /* If it's not a class-name, keep looking. */
9053 if (!cp_parser_parse_definitely (parser
))
9055 /* It must be a typedef-name or an enum-name. */
9056 identifier
= cp_parser_identifier (parser
);
9057 if (identifier
== error_mark_node
)
9058 return error_mark_node
;
9060 /* Look up the type-name. */
9061 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9062 /* Issue an error if we did not find a type-name. */
9063 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9065 if (!cp_parser_simulate_error (parser
))
9066 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9068 type_decl
= error_mark_node
;
9070 /* Remember that the name was used in the definition of the
9071 current class so that we can check later to see if the
9072 meaning would have been different after the class was
9073 entirely defined. */
9074 else if (type_decl
!= error_mark_node
9076 maybe_note_name_used_in_class (identifier
, type_decl
);
9083 /* Parse an elaborated-type-specifier. Note that the grammar given
9084 here incorporates the resolution to DR68.
9086 elaborated-type-specifier:
9087 class-key :: [opt] nested-name-specifier [opt] identifier
9088 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9089 enum :: [opt] nested-name-specifier [opt] identifier
9090 typename :: [opt] nested-name-specifier identifier
9091 typename :: [opt] nested-name-specifier template [opt]
9096 elaborated-type-specifier:
9097 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9098 class-key attributes :: [opt] nested-name-specifier [opt]
9099 template [opt] template-id
9100 enum attributes :: [opt] nested-name-specifier [opt] identifier
9102 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9103 declared `friend'. If IS_DECLARATION is TRUE, then this
9104 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9105 something is being declared.
9107 Returns the TYPE specified. */
9110 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
9112 bool is_declaration
)
9114 enum tag_types tag_type
;
9116 tree type
= NULL_TREE
;
9117 tree attributes
= NULL_TREE
;
9119 /* See if we're looking at the `enum' keyword. */
9120 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
9122 /* Consume the `enum' token. */
9123 cp_lexer_consume_token (parser
->lexer
);
9124 /* Remember that it's an enumeration type. */
9125 tag_type
= enum_type
;
9126 /* Parse the attributes. */
9127 attributes
= cp_parser_attributes_opt (parser
);
9129 /* Or, it might be `typename'. */
9130 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9133 /* Consume the `typename' token. */
9134 cp_lexer_consume_token (parser
->lexer
);
9135 /* Remember that it's a `typename' type. */
9136 tag_type
= typename_type
;
9137 /* The `typename' keyword is only allowed in templates. */
9138 if (!processing_template_decl
)
9139 pedwarn ("using `typename' outside of template");
9141 /* Otherwise it must be a class-key. */
9144 tag_type
= cp_parser_class_key (parser
);
9145 if (tag_type
== none_type
)
9146 return error_mark_node
;
9147 /* Parse the attributes. */
9148 attributes
= cp_parser_attributes_opt (parser
);
9151 /* Look for the `::' operator. */
9152 cp_parser_global_scope_opt (parser
,
9153 /*current_scope_valid_p=*/false);
9154 /* Look for the nested-name-specifier. */
9155 if (tag_type
== typename_type
)
9157 if (cp_parser_nested_name_specifier (parser
,
9158 /*typename_keyword_p=*/true,
9159 /*check_dependency_p=*/true,
9163 return error_mark_node
;
9166 /* Even though `typename' is not present, the proposed resolution
9167 to Core Issue 180 says that in `class A<T>::B', `B' should be
9168 considered a type-name, even if `A<T>' is dependent. */
9169 cp_parser_nested_name_specifier_opt (parser
,
9170 /*typename_keyword_p=*/true,
9171 /*check_dependency_p=*/true,
9174 /* For everything but enumeration types, consider a template-id. */
9175 if (tag_type
!= enum_type
)
9177 bool template_p
= false;
9180 /* Allow the `template' keyword. */
9181 template_p
= cp_parser_optional_template_keyword (parser
);
9182 /* If we didn't see `template', we don't know if there's a
9183 template-id or not. */
9185 cp_parser_parse_tentatively (parser
);
9186 /* Parse the template-id. */
9187 decl
= cp_parser_template_id (parser
, template_p
,
9188 /*check_dependency_p=*/true,
9190 /* If we didn't find a template-id, look for an ordinary
9192 if (!template_p
&& !cp_parser_parse_definitely (parser
))
9194 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9195 in effect, then we must assume that, upon instantiation, the
9196 template will correspond to a class. */
9197 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
9198 && tag_type
== typename_type
)
9199 type
= make_typename_type (parser
->scope
, decl
,
9202 type
= TREE_TYPE (decl
);
9205 /* For an enumeration type, consider only a plain identifier. */
9208 identifier
= cp_parser_identifier (parser
);
9210 if (identifier
== error_mark_node
)
9212 parser
->scope
= NULL_TREE
;
9213 return error_mark_node
;
9216 /* For a `typename', we needn't call xref_tag. */
9217 if (tag_type
== typename_type
9218 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
9219 return make_typename_type (parser
->scope
, identifier
,
9221 /* Look up a qualified name in the usual way. */
9226 /* In an elaborated-type-specifier, names are assumed to name
9227 types, so we set IS_TYPE to TRUE when calling
9228 cp_parser_lookup_name. */
9229 decl
= cp_parser_lookup_name (parser
, identifier
,
9231 /*is_template=*/false,
9232 /*is_namespace=*/false,
9233 /*check_dependency=*/true);
9235 /* If we are parsing friend declaration, DECL may be a
9236 TEMPLATE_DECL tree node here. However, we need to check
9237 whether this TEMPLATE_DECL results in valid code. Consider
9238 the following example:
9241 template <class T> class C {};
9244 template <class T> friend class N::C; // #1, valid code
9246 template <class T> class Y {
9247 friend class N::C; // #2, invalid code
9250 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9251 name lookup of `N::C'. We see that friend declaration must
9252 be template for the code to be valid. Note that
9253 processing_template_decl does not work here since it is
9254 always 1 for the above two cases. */
9256 decl
= (cp_parser_maybe_treat_template_as_class
9257 (decl
, /*tag_name_p=*/is_friend
9258 && parser
->num_template_parameter_lists
));
9260 if (TREE_CODE (decl
) != TYPE_DECL
)
9262 cp_parser_diagnose_invalid_type_name (parser
);
9263 return error_mark_node
;
9266 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9267 check_elaborated_type_specifier
9269 (parser
->num_template_parameter_lists
9270 || DECL_SELF_REFERENCE_P (decl
)));
9272 type
= TREE_TYPE (decl
);
9276 /* An elaborated-type-specifier sometimes introduces a new type and
9277 sometimes names an existing type. Normally, the rule is that it
9278 introduces a new type only if there is not an existing type of
9279 the same name already in scope. For example, given:
9282 void f() { struct S s; }
9284 the `struct S' in the body of `f' is the same `struct S' as in
9285 the global scope; the existing definition is used. However, if
9286 there were no global declaration, this would introduce a new
9287 local class named `S'.
9289 An exception to this rule applies to the following code:
9291 namespace N { struct S; }
9293 Here, the elaborated-type-specifier names a new type
9294 unconditionally; even if there is already an `S' in the
9295 containing scope this declaration names a new type.
9296 This exception only applies if the elaborated-type-specifier
9297 forms the complete declaration:
9301 A declaration consisting solely of `class-key identifier ;' is
9302 either a redeclaration of the name in the current scope or a
9303 forward declaration of the identifier as a class name. It
9304 introduces the name into the current scope.
9306 We are in this situation precisely when the next token is a `;'.
9308 An exception to the exception is that a `friend' declaration does
9309 *not* name a new type; i.e., given:
9311 struct S { friend struct T; };
9313 `T' is not a new type in the scope of `S'.
9315 Also, `new struct S' or `sizeof (struct S)' never results in the
9316 definition of a new type; a new type can only be declared in a
9317 declaration context. */
9319 /* Warn about attributes. They are ignored. */
9321 warning ("type attributes are honored only at type definition");
9323 type
= xref_tag (tag_type
, identifier
,
9326 || cp_lexer_next_token_is_not (parser
->lexer
,
9328 parser
->num_template_parameter_lists
);
9331 if (tag_type
!= enum_type
)
9332 cp_parser_check_class_key (tag_type
, type
);
9334 /* A "<" cannot follow an elaborated type specifier. If that
9335 happens, the user was probably trying to form a template-id. */
9336 cp_parser_check_for_invalid_template_id (parser
, type
);
9341 /* Parse an enum-specifier.
9344 enum identifier [opt] { enumerator-list [opt] }
9346 Returns an ENUM_TYPE representing the enumeration. */
9349 cp_parser_enum_specifier (cp_parser
* parser
)
9352 tree identifier
= NULL_TREE
;
9355 /* Look for the `enum' keyword. */
9356 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
9357 return error_mark_node
;
9358 /* Peek at the next token. */
9359 token
= cp_lexer_peek_token (parser
->lexer
);
9361 /* See if it is an identifier. */
9362 if (token
->type
== CPP_NAME
)
9363 identifier
= cp_parser_identifier (parser
);
9365 /* Look for the `{'. */
9366 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
9367 return error_mark_node
;
9369 /* At this point, we're going ahead with the enum-specifier, even
9370 if some other problem occurs. */
9371 cp_parser_commit_to_tentative_parse (parser
);
9373 /* Issue an error message if type-definitions are forbidden here. */
9374 cp_parser_check_type_definition (parser
);
9376 /* Create the new type. */
9377 type
= start_enum (identifier
? identifier
: make_anon_name ());
9379 /* Peek at the next token. */
9380 token
= cp_lexer_peek_token (parser
->lexer
);
9381 /* If it's not a `}', then there are some enumerators. */
9382 if (token
->type
!= CPP_CLOSE_BRACE
)
9383 cp_parser_enumerator_list (parser
, type
);
9384 /* Look for the `}'. */
9385 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9387 /* Finish up the enumeration. */
9393 /* Parse an enumerator-list. The enumerators all have the indicated
9397 enumerator-definition
9398 enumerator-list , enumerator-definition */
9401 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9407 /* Parse an enumerator-definition. */
9408 cp_parser_enumerator_definition (parser
, type
);
9409 /* Peek at the next token. */
9410 token
= cp_lexer_peek_token (parser
->lexer
);
9411 /* If it's not a `,', then we've reached the end of the
9413 if (token
->type
!= CPP_COMMA
)
9415 /* Otherwise, consume the `,' and keep going. */
9416 cp_lexer_consume_token (parser
->lexer
);
9417 /* If the next token is a `}', there is a trailing comma. */
9418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9420 if (pedantic
&& !in_system_header
)
9421 pedwarn ("comma at end of enumerator list");
9427 /* Parse an enumerator-definition. The enumerator has the indicated
9430 enumerator-definition:
9432 enumerator = constant-expression
9438 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9444 /* Look for the identifier. */
9445 identifier
= cp_parser_identifier (parser
);
9446 if (identifier
== error_mark_node
)
9449 /* Peek at the next token. */
9450 token
= cp_lexer_peek_token (parser
->lexer
);
9451 /* If it's an `=', then there's an explicit value. */
9452 if (token
->type
== CPP_EQ
)
9454 /* Consume the `=' token. */
9455 cp_lexer_consume_token (parser
->lexer
);
9456 /* Parse the value. */
9457 value
= cp_parser_constant_expression (parser
,
9458 /*allow_non_constant_p=*/false,
9464 /* Create the enumerator. */
9465 build_enumerator (identifier
, value
, type
);
9468 /* Parse a namespace-name.
9471 original-namespace-name
9474 Returns the NAMESPACE_DECL for the namespace. */
9477 cp_parser_namespace_name (cp_parser
* parser
)
9480 tree namespace_decl
;
9482 /* Get the name of the namespace. */
9483 identifier
= cp_parser_identifier (parser
);
9484 if (identifier
== error_mark_node
)
9485 return error_mark_node
;
9487 /* Look up the identifier in the currently active scope. Look only
9488 for namespaces, due to:
9492 When looking up a namespace-name in a using-directive or alias
9493 definition, only namespace names are considered.
9499 During the lookup of a name preceding the :: scope resolution
9500 operator, object, function, and enumerator names are ignored.
9502 (Note that cp_parser_class_or_namespace_name only calls this
9503 function if the token after the name is the scope resolution
9505 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
9507 /*is_template=*/false,
9508 /*is_namespace=*/true,
9509 /*check_dependency=*/true);
9510 /* If it's not a namespace, issue an error. */
9511 if (namespace_decl
== error_mark_node
9512 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
9514 if (!cp_parser_parsing_tentatively (parser
)
9515 || cp_parser_committed_to_tentative_parse (parser
))
9516 error ("`%D' is not a namespace-name", identifier
);
9517 cp_parser_error (parser
, "expected namespace-name");
9518 namespace_decl
= error_mark_node
;
9521 return namespace_decl
;
9524 /* Parse a namespace-definition.
9526 namespace-definition:
9527 named-namespace-definition
9528 unnamed-namespace-definition
9530 named-namespace-definition:
9531 original-namespace-definition
9532 extension-namespace-definition
9534 original-namespace-definition:
9535 namespace identifier { namespace-body }
9537 extension-namespace-definition:
9538 namespace original-namespace-name { namespace-body }
9540 unnamed-namespace-definition:
9541 namespace { namespace-body } */
9544 cp_parser_namespace_definition (cp_parser
* parser
)
9548 /* Look for the `namespace' keyword. */
9549 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9551 /* Get the name of the namespace. We do not attempt to distinguish
9552 between an original-namespace-definition and an
9553 extension-namespace-definition at this point. The semantic
9554 analysis routines are responsible for that. */
9555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9556 identifier
= cp_parser_identifier (parser
);
9558 identifier
= NULL_TREE
;
9560 /* Look for the `{' to start the namespace. */
9561 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
9562 /* Start the namespace. */
9563 push_namespace (identifier
);
9564 /* Parse the body of the namespace. */
9565 cp_parser_namespace_body (parser
);
9566 /* Finish the namespace. */
9568 /* Look for the final `}'. */
9569 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9572 /* Parse a namespace-body.
9575 declaration-seq [opt] */
9578 cp_parser_namespace_body (cp_parser
* parser
)
9580 cp_parser_declaration_seq_opt (parser
);
9583 /* Parse a namespace-alias-definition.
9585 namespace-alias-definition:
9586 namespace identifier = qualified-namespace-specifier ; */
9589 cp_parser_namespace_alias_definition (cp_parser
* parser
)
9592 tree namespace_specifier
;
9594 /* Look for the `namespace' keyword. */
9595 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9596 /* Look for the identifier. */
9597 identifier
= cp_parser_identifier (parser
);
9598 if (identifier
== error_mark_node
)
9600 /* Look for the `=' token. */
9601 cp_parser_require (parser
, CPP_EQ
, "`='");
9602 /* Look for the qualified-namespace-specifier. */
9604 = cp_parser_qualified_namespace_specifier (parser
);
9605 /* Look for the `;' token. */
9606 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9608 /* Register the alias in the symbol table. */
9609 do_namespace_alias (identifier
, namespace_specifier
);
9612 /* Parse a qualified-namespace-specifier.
9614 qualified-namespace-specifier:
9615 :: [opt] nested-name-specifier [opt] namespace-name
9617 Returns a NAMESPACE_DECL corresponding to the specified
9621 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
9623 /* Look for the optional `::'. */
9624 cp_parser_global_scope_opt (parser
,
9625 /*current_scope_valid_p=*/false);
9627 /* Look for the optional nested-name-specifier. */
9628 cp_parser_nested_name_specifier_opt (parser
,
9629 /*typename_keyword_p=*/false,
9630 /*check_dependency_p=*/true,
9632 /*is_declaration=*/true);
9634 return cp_parser_namespace_name (parser
);
9637 /* Parse a using-declaration.
9640 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9641 using :: unqualified-id ; */
9644 cp_parser_using_declaration (cp_parser
* parser
)
9647 bool typename_p
= false;
9648 bool global_scope_p
;
9654 /* Look for the `using' keyword. */
9655 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9657 /* Peek at the next token. */
9658 token
= cp_lexer_peek_token (parser
->lexer
);
9659 /* See if it's `typename'. */
9660 if (token
->keyword
== RID_TYPENAME
)
9662 /* Remember that we've seen it. */
9664 /* Consume the `typename' token. */
9665 cp_lexer_consume_token (parser
->lexer
);
9668 /* Look for the optional global scope qualification. */
9670 = (cp_parser_global_scope_opt (parser
,
9671 /*current_scope_valid_p=*/false)
9674 /* If we saw `typename', or didn't see `::', then there must be a
9675 nested-name-specifier present. */
9676 if (typename_p
|| !global_scope_p
)
9677 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
9678 /*check_dependency_p=*/true,
9680 /*is_declaration=*/true);
9681 /* Otherwise, we could be in either of the two productions. In that
9682 case, treat the nested-name-specifier as optional. */
9684 qscope
= cp_parser_nested_name_specifier_opt (parser
,
9685 /*typename_keyword_p=*/false,
9686 /*check_dependency_p=*/true,
9688 /*is_declaration=*/true);
9690 qscope
= global_namespace
;
9692 /* Parse the unqualified-id. */
9693 identifier
= cp_parser_unqualified_id (parser
,
9694 /*template_keyword_p=*/false,
9695 /*check_dependency_p=*/true,
9696 /*declarator_p=*/true);
9698 /* The function we call to handle a using-declaration is different
9699 depending on what scope we are in. */
9700 if (identifier
== error_mark_node
)
9702 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
9703 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
9704 /* [namespace.udecl]
9706 A using declaration shall not name a template-id. */
9707 error ("a template-id may not appear in a using-declaration");
9710 scope
= current_scope ();
9711 if (scope
&& TYPE_P (scope
))
9713 /* Create the USING_DECL. */
9714 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9717 /* Add it to the list of members in this class. */
9718 finish_member_declaration (decl
);
9722 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9723 if (decl
== error_mark_node
)
9724 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
9726 do_local_using_decl (decl
, qscope
, identifier
);
9728 do_toplevel_using_decl (decl
, qscope
, identifier
);
9732 /* Look for the final `;'. */
9733 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9736 /* Parse a using-directive.
9739 using namespace :: [opt] nested-name-specifier [opt]
9743 cp_parser_using_directive (cp_parser
* parser
)
9745 tree namespace_decl
;
9748 /* Look for the `using' keyword. */
9749 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9750 /* And the `namespace' keyword. */
9751 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9752 /* Look for the optional `::' operator. */
9753 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9754 /* And the optional nested-name-specifier. */
9755 cp_parser_nested_name_specifier_opt (parser
,
9756 /*typename_keyword_p=*/false,
9757 /*check_dependency_p=*/true,
9759 /*is_declaration=*/true);
9760 /* Get the namespace being used. */
9761 namespace_decl
= cp_parser_namespace_name (parser
);
9762 /* And any specified attributes. */
9763 attribs
= cp_parser_attributes_opt (parser
);
9764 /* Update the symbol table. */
9765 parse_using_directive (namespace_decl
, attribs
);
9766 /* Look for the final `;'. */
9767 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9770 /* Parse an asm-definition.
9773 asm ( string-literal ) ;
9778 asm volatile [opt] ( string-literal ) ;
9779 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9780 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9781 : asm-operand-list [opt] ) ;
9782 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9783 : asm-operand-list [opt]
9784 : asm-operand-list [opt] ) ; */
9787 cp_parser_asm_definition (cp_parser
* parser
)
9791 tree outputs
= NULL_TREE
;
9792 tree inputs
= NULL_TREE
;
9793 tree clobbers
= NULL_TREE
;
9795 bool volatile_p
= false;
9796 bool extended_p
= false;
9798 /* Look for the `asm' keyword. */
9799 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9800 /* See if the next token is `volatile'. */
9801 if (cp_parser_allow_gnu_extensions_p (parser
)
9802 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9804 /* Remember that we saw the `volatile' keyword. */
9806 /* Consume the token. */
9807 cp_lexer_consume_token (parser
->lexer
);
9809 /* Look for the opening `('. */
9810 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9811 /* Look for the string. */
9812 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9815 string
= token
->value
;
9816 /* If we're allowing GNU extensions, check for the extended assembly
9817 syntax. Unfortunately, the `:' tokens need not be separated by
9818 a space in C, and so, for compatibility, we tolerate that here
9819 too. Doing that means that we have to treat the `::' operator as
9821 if (cp_parser_allow_gnu_extensions_p (parser
)
9822 && at_function_scope_p ()
9823 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9824 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9826 bool inputs_p
= false;
9827 bool clobbers_p
= false;
9829 /* The extended syntax was used. */
9832 /* Look for outputs. */
9833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9835 /* Consume the `:'. */
9836 cp_lexer_consume_token (parser
->lexer
);
9837 /* Parse the output-operands. */
9838 if (cp_lexer_next_token_is_not (parser
->lexer
,
9840 && cp_lexer_next_token_is_not (parser
->lexer
,
9842 && cp_lexer_next_token_is_not (parser
->lexer
,
9844 outputs
= cp_parser_asm_operand_list (parser
);
9846 /* If the next token is `::', there are no outputs, and the
9847 next token is the beginning of the inputs. */
9848 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9849 /* The inputs are coming next. */
9852 /* Look for inputs. */
9854 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9856 /* Consume the `:' or `::'. */
9857 cp_lexer_consume_token (parser
->lexer
);
9858 /* Parse the output-operands. */
9859 if (cp_lexer_next_token_is_not (parser
->lexer
,
9861 && cp_lexer_next_token_is_not (parser
->lexer
,
9863 inputs
= cp_parser_asm_operand_list (parser
);
9865 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9866 /* The clobbers are coming next. */
9869 /* Look for clobbers. */
9871 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9873 /* Consume the `:' or `::'. */
9874 cp_lexer_consume_token (parser
->lexer
);
9875 /* Parse the clobbers. */
9876 if (cp_lexer_next_token_is_not (parser
->lexer
,
9878 clobbers
= cp_parser_asm_clobber_list (parser
);
9881 /* Look for the closing `)'. */
9882 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9883 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9884 /*consume_paren=*/true);
9885 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9887 /* Create the ASM_STMT. */
9888 if (at_function_scope_p ())
9891 finish_asm_stmt (volatile_p
9892 ? ridpointers
[(int) RID_VOLATILE
] : NULL_TREE
,
9893 string
, outputs
, inputs
, clobbers
);
9894 /* If the extended syntax was not used, mark the ASM_STMT. */
9896 ASM_INPUT_P (asm_stmt
) = 1;
9899 assemble_asm (string
);
9902 /* Declarators [gram.dcl.decl] */
9904 /* Parse an init-declarator.
9907 declarator initializer [opt]
9912 declarator asm-specification [opt] attributes [opt] initializer [opt]
9914 function-definition:
9915 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9917 decl-specifier-seq [opt] declarator function-try-block
9921 function-definition:
9922 __extension__ function-definition
9924 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9925 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9926 then this declarator appears in a class scope. The new DECL created
9927 by this declarator is returned.
9929 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9930 for a function-definition here as well. If the declarator is a
9931 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9932 be TRUE upon return. By that point, the function-definition will
9933 have been completely parsed.
9935 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9939 cp_parser_init_declarator (cp_parser
* parser
,
9940 tree decl_specifiers
,
9941 tree prefix_attributes
,
9942 bool function_definition_allowed_p
,
9944 int declares_class_or_enum
,
9945 bool* function_definition_p
)
9950 tree asm_specification
;
9952 tree decl
= NULL_TREE
;
9954 bool is_initialized
;
9955 bool is_parenthesized_init
;
9956 bool is_non_constant_init
;
9957 int ctor_dtor_or_conv_p
;
9961 /* Assume that this is not the declarator for a function
9963 if (function_definition_p
)
9964 *function_definition_p
= false;
9966 /* Defer access checks while parsing the declarator; we cannot know
9967 what names are accessible until we know what is being
9969 resume_deferring_access_checks ();
9971 /* Parse the declarator. */
9973 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9974 &ctor_dtor_or_conv_p
,
9975 /*parenthesized_p=*/NULL
,
9976 /*member_p=*/false);
9977 /* Gather up the deferred checks. */
9978 stop_deferring_access_checks ();
9980 /* If the DECLARATOR was erroneous, there's no need to go
9982 if (declarator
== error_mark_node
)
9983 return error_mark_node
;
9985 if (declares_class_or_enum
& 2)
9986 cp_parser_check_for_definition_in_return_type
9987 (declarator
, TREE_VALUE (decl_specifiers
));
9989 /* Figure out what scope the entity declared by the DECLARATOR is
9990 located in. `grokdeclarator' sometimes changes the scope, so
9991 we compute it now. */
9992 scope
= get_scope_of_declarator (declarator
);
9994 /* If we're allowing GNU extensions, look for an asm-specification
9996 if (cp_parser_allow_gnu_extensions_p (parser
))
9998 /* Look for an asm-specification. */
9999 asm_specification
= cp_parser_asm_specification_opt (parser
);
10000 /* And attributes. */
10001 attributes
= cp_parser_attributes_opt (parser
);
10005 asm_specification
= NULL_TREE
;
10006 attributes
= NULL_TREE
;
10009 /* Peek at the next token. */
10010 token
= cp_lexer_peek_token (parser
->lexer
);
10011 /* Check to see if the token indicates the start of a
10012 function-definition. */
10013 if (cp_parser_token_starts_function_definition_p (token
))
10015 if (!function_definition_allowed_p
)
10017 /* If a function-definition should not appear here, issue an
10019 cp_parser_error (parser
,
10020 "a function-definition is not allowed here");
10021 return error_mark_node
;
10025 /* Neither attributes nor an asm-specification are allowed
10026 on a function-definition. */
10027 if (asm_specification
)
10028 error ("an asm-specification is not allowed on a function-definition");
10030 error ("attributes are not allowed on a function-definition");
10031 /* This is a function-definition. */
10032 *function_definition_p
= true;
10034 /* Parse the function definition. */
10036 decl
= cp_parser_save_member_function_body (parser
,
10039 prefix_attributes
);
10042 = (cp_parser_function_definition_from_specifiers_and_declarator
10043 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
10051 Only in function declarations for constructors, destructors, and
10052 type conversions can the decl-specifier-seq be omitted.
10054 We explicitly postpone this check past the point where we handle
10055 function-definitions because we tolerate function-definitions
10056 that are missing their return types in some modes. */
10057 if (!decl_specifiers
&& ctor_dtor_or_conv_p
<= 0)
10059 cp_parser_error (parser
,
10060 "expected constructor, destructor, or type conversion");
10061 return error_mark_node
;
10064 /* An `=' or an `(' indicates an initializer. */
10065 is_initialized
= (token
->type
== CPP_EQ
10066 || token
->type
== CPP_OPEN_PAREN
);
10067 /* If the init-declarator isn't initialized and isn't followed by a
10068 `,' or `;', it's not a valid init-declarator. */
10069 if (!is_initialized
10070 && token
->type
!= CPP_COMMA
10071 && token
->type
!= CPP_SEMICOLON
)
10073 cp_parser_error (parser
, "expected initializer");
10074 return error_mark_node
;
10077 /* Because start_decl has side-effects, we should only call it if we
10078 know we're going ahead. By this point, we know that we cannot
10079 possibly be looking at any other construct. */
10080 cp_parser_commit_to_tentative_parse (parser
);
10082 /* If the decl specifiers were bad, issue an error now that we're
10083 sure this was intended to be a declarator. Then continue
10084 declaring the variable(s), as int, to try to cut down on further
10086 if (decl_specifiers
!= NULL
10087 && TREE_VALUE (decl_specifiers
) == error_mark_node
)
10089 cp_parser_error (parser
, "invalid type in declaration");
10090 TREE_VALUE (decl_specifiers
) = integer_type_node
;
10093 /* Check to see whether or not this declaration is a friend. */
10094 friend_p
= cp_parser_friend_p (decl_specifiers
);
10096 /* Check that the number of template-parameter-lists is OK. */
10097 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
10098 return error_mark_node
;
10100 /* Enter the newly declared entry in the symbol table. If we're
10101 processing a declaration in a class-specifier, we wait until
10102 after processing the initializer. */
10105 if (parser
->in_unbraced_linkage_specification_p
)
10107 decl_specifiers
= tree_cons (error_mark_node
,
10108 get_identifier ("extern"),
10110 have_extern_spec
= false;
10112 decl
= start_decl (declarator
, decl_specifiers
,
10113 is_initialized
, attributes
, prefix_attributes
,
10117 /* Enter the SCOPE. That way unqualified names appearing in the
10118 initializer will be looked up in SCOPE. */
10119 pop_p
= push_scope (scope
);
10121 /* Perform deferred access control checks, now that we know in which
10122 SCOPE the declared entity resides. */
10123 if (!member_p
&& decl
)
10125 tree saved_current_function_decl
= NULL_TREE
;
10127 /* If the entity being declared is a function, pretend that we
10128 are in its scope. If it is a `friend', it may have access to
10129 things that would not otherwise be accessible. */
10130 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10132 saved_current_function_decl
= current_function_decl
;
10133 current_function_decl
= decl
;
10136 /* Perform the access control checks for the declarator and the
10137 the decl-specifiers. */
10138 perform_deferred_access_checks ();
10140 /* Restore the saved value. */
10141 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10142 current_function_decl
= saved_current_function_decl
;
10145 /* Parse the initializer. */
10146 if (is_initialized
)
10147 initializer
= cp_parser_initializer (parser
,
10148 &is_parenthesized_init
,
10149 &is_non_constant_init
);
10152 initializer
= NULL_TREE
;
10153 is_parenthesized_init
= false;
10154 is_non_constant_init
= true;
10157 /* The old parser allows attributes to appear after a parenthesized
10158 initializer. Mark Mitchell proposed removing this functionality
10159 on the GCC mailing lists on 2002-08-13. This parser accepts the
10160 attributes -- but ignores them. */
10161 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
10162 if (cp_parser_attributes_opt (parser
))
10163 warning ("attributes after parenthesized initializer ignored");
10165 /* For an in-class declaration, use `grokfield' to create the
10172 decl
= grokfield (declarator
, decl_specifiers
,
10173 initializer
, /*asmspec=*/NULL_TREE
,
10174 /*attributes=*/NULL_TREE
);
10175 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
10176 cp_parser_save_default_args (parser
, decl
);
10179 /* Finish processing the declaration. But, skip friend
10181 if (!friend_p
&& decl
)
10183 cp_finish_decl (decl
,
10186 /* If the initializer is in parentheses, then this is
10187 a direct-initialization, which means that an
10188 `explicit' constructor is OK. Otherwise, an
10189 `explicit' constructor cannot be used. */
10190 ((is_parenthesized_init
|| !is_initialized
)
10191 ? 0 : LOOKUP_ONLYCONVERTING
));
10193 pop_scope (DECL_CONTEXT (decl
));
10196 /* Remember whether or not variables were initialized by
10197 constant-expressions. */
10198 if (decl
&& TREE_CODE (decl
) == VAR_DECL
10199 && is_initialized
&& !is_non_constant_init
)
10200 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
10205 /* Parse a declarator.
10209 ptr-operator declarator
10211 abstract-declarator:
10212 ptr-operator abstract-declarator [opt]
10213 direct-abstract-declarator
10218 attributes [opt] direct-declarator
10219 attributes [opt] ptr-operator declarator
10221 abstract-declarator:
10222 attributes [opt] ptr-operator abstract-declarator [opt]
10223 attributes [opt] direct-abstract-declarator
10225 Returns a representation of the declarator. If the declarator has
10226 the form `* declarator', then an INDIRECT_REF is returned, whose
10227 only operand is the sub-declarator. Analogously, `& declarator' is
10228 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10229 used. The first operand is the TYPE for `X'. The second operand
10230 is an INDIRECT_REF whose operand is the sub-declarator.
10232 Otherwise, the representation is as for a direct-declarator.
10234 (It would be better to define a structure type to represent
10235 declarators, rather than abusing `tree' nodes to represent
10236 declarators. That would be much clearer and save some memory.
10237 There is no reason for declarators to be garbage-collected, for
10238 example; they are created during parser and no longer needed after
10239 `grokdeclarator' has been called.)
10241 For a ptr-operator that has the optional cv-qualifier-seq,
10242 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10245 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10246 detect constructor, destructor or conversion operators. It is set
10247 to -1 if the declarator is a name, and +1 if it is a
10248 function. Otherwise it is set to zero. Usually you just want to
10249 test for >0, but internally the negative value is used.
10251 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10252 a decl-specifier-seq unless it declares a constructor, destructor,
10253 or conversion. It might seem that we could check this condition in
10254 semantic analysis, rather than parsing, but that makes it difficult
10255 to handle something like `f()'. We want to notice that there are
10256 no decl-specifiers, and therefore realize that this is an
10257 expression, not a declaration.)
10259 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10260 the declarator is a direct-declarator of the form "(...)".
10262 MEMBER_P is true iff this declarator is a member-declarator. */
10265 cp_parser_declarator (cp_parser
* parser
,
10266 cp_parser_declarator_kind dcl_kind
,
10267 int* ctor_dtor_or_conv_p
,
10268 bool* parenthesized_p
,
10273 enum tree_code code
;
10274 tree cv_qualifier_seq
;
10276 tree attributes
= NULL_TREE
;
10278 /* Assume this is not a constructor, destructor, or type-conversion
10280 if (ctor_dtor_or_conv_p
)
10281 *ctor_dtor_or_conv_p
= 0;
10283 if (cp_parser_allow_gnu_extensions_p (parser
))
10284 attributes
= cp_parser_attributes_opt (parser
);
10286 /* Peek at the next token. */
10287 token
= cp_lexer_peek_token (parser
->lexer
);
10289 /* Check for the ptr-operator production. */
10290 cp_parser_parse_tentatively (parser
);
10291 /* Parse the ptr-operator. */
10292 code
= cp_parser_ptr_operator (parser
,
10294 &cv_qualifier_seq
);
10295 /* If that worked, then we have a ptr-operator. */
10296 if (cp_parser_parse_definitely (parser
))
10298 /* If a ptr-operator was found, then this declarator was not
10300 if (parenthesized_p
)
10301 *parenthesized_p
= true;
10302 /* The dependent declarator is optional if we are parsing an
10303 abstract-declarator. */
10304 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10305 cp_parser_parse_tentatively (parser
);
10307 /* Parse the dependent declarator. */
10308 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10309 /*ctor_dtor_or_conv_p=*/NULL
,
10310 /*parenthesized_p=*/NULL
,
10311 /*member_p=*/false);
10313 /* If we are parsing an abstract-declarator, we must handle the
10314 case where the dependent declarator is absent. */
10315 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10316 && !cp_parser_parse_definitely (parser
))
10317 declarator
= NULL_TREE
;
10319 /* Build the representation of the ptr-operator. */
10320 if (code
== INDIRECT_REF
)
10321 declarator
= make_pointer_declarator (cv_qualifier_seq
,
10324 declarator
= make_reference_declarator (cv_qualifier_seq
,
10326 /* Handle the pointer-to-member case. */
10328 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
10330 /* Everything else is a direct-declarator. */
10333 if (parenthesized_p
)
10334 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10336 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10337 ctor_dtor_or_conv_p
,
10341 if (attributes
&& declarator
!= error_mark_node
)
10342 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
10347 /* Parse a direct-declarator or direct-abstract-declarator.
10351 direct-declarator ( parameter-declaration-clause )
10352 cv-qualifier-seq [opt]
10353 exception-specification [opt]
10354 direct-declarator [ constant-expression [opt] ]
10357 direct-abstract-declarator:
10358 direct-abstract-declarator [opt]
10359 ( parameter-declaration-clause )
10360 cv-qualifier-seq [opt]
10361 exception-specification [opt]
10362 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10363 ( abstract-declarator )
10365 Returns a representation of the declarator. DCL_KIND is
10366 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10367 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10368 we are parsing a direct-declarator. It is
10369 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10370 of ambiguity we prefer an abstract declarator, as per
10371 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10372 cp_parser_declarator.
10374 For the declarator-id production, the representation is as for an
10375 id-expression, except that a qualified name is represented as a
10376 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10377 see the documentation of the FUNCTION_DECLARATOR_* macros for
10378 information about how to find the various declarator components.
10379 An array-declarator is represented as an ARRAY_REF. The
10380 direct-declarator is the first operand; the constant-expression
10381 indicating the size of the array is the second operand. */
10384 cp_parser_direct_declarator (cp_parser
* parser
,
10385 cp_parser_declarator_kind dcl_kind
,
10386 int* ctor_dtor_or_conv_p
,
10390 tree declarator
= NULL_TREE
;
10391 tree scope
= NULL_TREE
;
10392 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10393 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10395 bool pop_p
= false;
10399 /* Peek at the next token. */
10400 token
= cp_lexer_peek_token (parser
->lexer
);
10401 if (token
->type
== CPP_OPEN_PAREN
)
10403 /* This is either a parameter-declaration-clause, or a
10404 parenthesized declarator. When we know we are parsing a
10405 named declarator, it must be a parenthesized declarator
10406 if FIRST is true. For instance, `(int)' is a
10407 parameter-declaration-clause, with an omitted
10408 direct-abstract-declarator. But `((*))', is a
10409 parenthesized abstract declarator. Finally, when T is a
10410 template parameter `(T)' is a
10411 parameter-declaration-clause, and not a parenthesized
10414 We first try and parse a parameter-declaration-clause,
10415 and then try a nested declarator (if FIRST is true).
10417 It is not an error for it not to be a
10418 parameter-declaration-clause, even when FIRST is
10424 The first is the declaration of a function while the
10425 second is a the definition of a variable, including its
10428 Having seen only the parenthesis, we cannot know which of
10429 these two alternatives should be selected. Even more
10430 complex are examples like:
10435 The former is a function-declaration; the latter is a
10436 variable initialization.
10438 Thus again, we try a parameter-declaration-clause, and if
10439 that fails, we back out and return. */
10441 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10444 unsigned saved_num_template_parameter_lists
;
10446 /* In a member-declarator, the only valid interpretation
10447 of a parenthesis is the start of a
10448 parameter-declaration-clause. (It is invalid to
10449 initialize a static data member with a parenthesized
10450 initializer; only the "=" form of initialization is
10453 cp_parser_parse_tentatively (parser
);
10455 /* Consume the `('. */
10456 cp_lexer_consume_token (parser
->lexer
);
10459 /* If this is going to be an abstract declarator, we're
10460 in a declarator and we can't have default args. */
10461 parser
->default_arg_ok_p
= false;
10462 parser
->in_declarator_p
= true;
10465 /* Inside the function parameter list, surrounding
10466 template-parameter-lists do not apply. */
10467 saved_num_template_parameter_lists
10468 = parser
->num_template_parameter_lists
;
10469 parser
->num_template_parameter_lists
= 0;
10471 /* Parse the parameter-declaration-clause. */
10472 params
= cp_parser_parameter_declaration_clause (parser
);
10474 parser
->num_template_parameter_lists
10475 = saved_num_template_parameter_lists
;
10477 /* If all went well, parse the cv-qualifier-seq and the
10478 exception-specification. */
10479 if (member_p
|| cp_parser_parse_definitely (parser
))
10481 tree cv_qualifiers
;
10482 tree exception_specification
;
10484 if (ctor_dtor_or_conv_p
)
10485 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
10487 /* Consume the `)'. */
10488 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
10490 /* Parse the cv-qualifier-seq. */
10491 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
10492 /* And the exception-specification. */
10493 exception_specification
10494 = cp_parser_exception_specification_opt (parser
);
10496 /* Create the function-declarator. */
10497 declarator
= make_call_declarator (declarator
,
10500 exception_specification
);
10501 /* Any subsequent parameter lists are to do with
10502 return type, so are not those of the declared
10504 parser
->default_arg_ok_p
= false;
10506 /* Repeat the main loop. */
10511 /* If this is the first, we can try a parenthesized
10515 bool saved_in_type_id_in_expr_p
;
10517 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10518 parser
->in_declarator_p
= saved_in_declarator_p
;
10520 /* Consume the `('. */
10521 cp_lexer_consume_token (parser
->lexer
);
10522 /* Parse the nested declarator. */
10523 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
10524 parser
->in_type_id_in_expr_p
= true;
10526 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
10527 /*parenthesized_p=*/NULL
,
10529 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
10531 /* Expect a `)'. */
10532 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10533 declarator
= error_mark_node
;
10534 if (declarator
== error_mark_node
)
10537 goto handle_declarator
;
10539 /* Otherwise, we must be done. */
10543 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10544 && token
->type
== CPP_OPEN_SQUARE
)
10546 /* Parse an array-declarator. */
10549 if (ctor_dtor_or_conv_p
)
10550 *ctor_dtor_or_conv_p
= 0;
10553 parser
->default_arg_ok_p
= false;
10554 parser
->in_declarator_p
= true;
10555 /* Consume the `['. */
10556 cp_lexer_consume_token (parser
->lexer
);
10557 /* Peek at the next token. */
10558 token
= cp_lexer_peek_token (parser
->lexer
);
10559 /* If the next token is `]', then there is no
10560 constant-expression. */
10561 if (token
->type
!= CPP_CLOSE_SQUARE
)
10563 bool non_constant_p
;
10566 = cp_parser_constant_expression (parser
,
10567 /*allow_non_constant=*/true,
10569 if (!non_constant_p
)
10570 bounds
= fold_non_dependent_expr (bounds
);
10573 bounds
= NULL_TREE
;
10574 /* Look for the closing `]'. */
10575 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
10577 declarator
= error_mark_node
;
10581 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
10583 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
10585 /* Parse a declarator-id */
10586 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10587 cp_parser_parse_tentatively (parser
);
10588 declarator
= cp_parser_declarator_id (parser
);
10589 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10591 if (!cp_parser_parse_definitely (parser
))
10592 declarator
= error_mark_node
;
10593 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
10595 cp_parser_error (parser
, "expected unqualified-id");
10596 declarator
= error_mark_node
;
10600 if (declarator
== error_mark_node
)
10603 if (TREE_CODE (declarator
) == SCOPE_REF
10604 && !current_scope ())
10606 tree scope
= TREE_OPERAND (declarator
, 0);
10608 /* In the declaration of a member of a template class
10609 outside of the class itself, the SCOPE will sometimes
10610 be a TYPENAME_TYPE. For example, given:
10612 template <typename T>
10613 int S<T>::R::i = 3;
10615 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10616 this context, we must resolve S<T>::R to an ordinary
10617 type, rather than a typename type.
10619 The reason we normally avoid resolving TYPENAME_TYPEs
10620 is that a specialization of `S' might render
10621 `S<T>::R' not a type. However, if `S' is
10622 specialized, then this `i' will not be used, so there
10623 is no harm in resolving the types here. */
10624 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
10628 /* Resolve the TYPENAME_TYPE. */
10629 type
= resolve_typename_type (scope
,
10630 /*only_current_p=*/false);
10631 /* If that failed, the declarator is invalid. */
10632 if (type
== error_mark_node
)
10633 error ("`%T::%D' is not a type",
10634 TYPE_CONTEXT (scope
),
10635 TYPE_IDENTIFIER (scope
));
10636 /* Build a new DECLARATOR. */
10637 declarator
= build_nt (SCOPE_REF
,
10639 TREE_OPERAND (declarator
, 1));
10643 /* Check to see whether the declarator-id names a constructor,
10644 destructor, or conversion. */
10645 if (declarator
&& ctor_dtor_or_conv_p
10646 && ((TREE_CODE (declarator
) == SCOPE_REF
10647 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
10648 || (TREE_CODE (declarator
) != SCOPE_REF
10649 && at_class_scope_p ())))
10651 tree unqualified_name
;
10654 /* Get the unqualified part of the name. */
10655 if (TREE_CODE (declarator
) == SCOPE_REF
)
10657 class_type
= TREE_OPERAND (declarator
, 0);
10658 unqualified_name
= TREE_OPERAND (declarator
, 1);
10662 class_type
= current_class_type
;
10663 unqualified_name
= declarator
;
10666 /* See if it names ctor, dtor or conv. */
10667 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
10668 || IDENTIFIER_TYPENAME_P (unqualified_name
)
10669 || constructor_name_p (unqualified_name
, class_type
)
10670 || (TREE_CODE (unqualified_name
) == TYPE_DECL
10671 && same_type_p (TREE_TYPE (unqualified_name
),
10673 *ctor_dtor_or_conv_p
= -1;
10676 handle_declarator
:;
10677 scope
= get_scope_of_declarator (declarator
);
10679 /* Any names that appear after the declarator-id for a
10680 member are looked up in the containing scope. */
10681 pop_p
= push_scope (scope
);
10682 parser
->in_declarator_p
= true;
10683 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
10685 && (TREE_CODE (declarator
) == SCOPE_REF
10686 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
10687 /* Default args are only allowed on function
10689 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10691 parser
->default_arg_ok_p
= false;
10700 /* For an abstract declarator, we might wind up with nothing at this
10701 point. That's an error; the declarator is not optional. */
10703 cp_parser_error (parser
, "expected declarator");
10705 /* If we entered a scope, we must exit it now. */
10709 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10710 parser
->in_declarator_p
= saved_in_declarator_p
;
10715 /* Parse a ptr-operator.
10718 * cv-qualifier-seq [opt]
10720 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10725 & cv-qualifier-seq [opt]
10727 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10728 used. Returns ADDR_EXPR if a reference was used. In the
10729 case of a pointer-to-member, *TYPE is filled in with the
10730 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10731 with the cv-qualifier-seq, or NULL_TREE, if there are no
10732 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10734 static enum tree_code
10735 cp_parser_ptr_operator (cp_parser
* parser
,
10737 tree
* cv_qualifier_seq
)
10739 enum tree_code code
= ERROR_MARK
;
10742 /* Assume that it's not a pointer-to-member. */
10744 /* And that there are no cv-qualifiers. */
10745 *cv_qualifier_seq
= NULL_TREE
;
10747 /* Peek at the next token. */
10748 token
= cp_lexer_peek_token (parser
->lexer
);
10749 /* If it's a `*' or `&' we have a pointer or reference. */
10750 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10752 /* Remember which ptr-operator we were processing. */
10753 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10755 /* Consume the `*' or `&'. */
10756 cp_lexer_consume_token (parser
->lexer
);
10758 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10759 `&', if we are allowing GNU extensions. (The only qualifier
10760 that can legally appear after `&' is `restrict', but that is
10761 enforced during semantic analysis. */
10762 if (code
== INDIRECT_REF
10763 || cp_parser_allow_gnu_extensions_p (parser
))
10764 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10768 /* Try the pointer-to-member case. */
10769 cp_parser_parse_tentatively (parser
);
10770 /* Look for the optional `::' operator. */
10771 cp_parser_global_scope_opt (parser
,
10772 /*current_scope_valid_p=*/false);
10773 /* Look for the nested-name specifier. */
10774 cp_parser_nested_name_specifier (parser
,
10775 /*typename_keyword_p=*/false,
10776 /*check_dependency_p=*/true,
10778 /*is_declaration=*/false);
10779 /* If we found it, and the next token is a `*', then we are
10780 indeed looking at a pointer-to-member operator. */
10781 if (!cp_parser_error_occurred (parser
)
10782 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10784 /* The type of which the member is a member is given by the
10786 *type
= parser
->scope
;
10787 /* The next name will not be qualified. */
10788 parser
->scope
= NULL_TREE
;
10789 parser
->qualifying_scope
= NULL_TREE
;
10790 parser
->object_scope
= NULL_TREE
;
10791 /* Indicate that the `*' operator was used. */
10792 code
= INDIRECT_REF
;
10793 /* Look for the optional cv-qualifier-seq. */
10794 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10796 /* If that didn't work we don't have a ptr-operator. */
10797 if (!cp_parser_parse_definitely (parser
))
10798 cp_parser_error (parser
, "expected ptr-operator");
10804 /* Parse an (optional) cv-qualifier-seq.
10807 cv-qualifier cv-qualifier-seq [opt]
10809 Returns a TREE_LIST. The TREE_VALUE of each node is the
10810 representation of a cv-qualifier. */
10813 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10815 tree cv_qualifiers
= NULL_TREE
;
10821 /* Look for the next cv-qualifier. */
10822 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10823 /* If we didn't find one, we're done. */
10827 /* Add this cv-qualifier to the list. */
10829 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10832 /* We built up the list in reverse order. */
10833 return nreverse (cv_qualifiers
);
10836 /* Parse an (optional) cv-qualifier.
10848 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10851 tree cv_qualifier
= NULL_TREE
;
10853 /* Peek at the next token. */
10854 token
= cp_lexer_peek_token (parser
->lexer
);
10855 /* See if it's a cv-qualifier. */
10856 switch (token
->keyword
)
10861 /* Save the value of the token. */
10862 cv_qualifier
= token
->value
;
10863 /* Consume the token. */
10864 cp_lexer_consume_token (parser
->lexer
);
10871 return cv_qualifier
;
10874 /* Parse a declarator-id.
10878 :: [opt] nested-name-specifier [opt] type-name
10880 In the `id-expression' case, the value returned is as for
10881 cp_parser_id_expression if the id-expression was an unqualified-id.
10882 If the id-expression was a qualified-id, then a SCOPE_REF is
10883 returned. The first operand is the scope (either a NAMESPACE_DECL
10884 or TREE_TYPE), but the second is still just a representation of an
10888 cp_parser_declarator_id (cp_parser
* parser
)
10890 tree id_expression
;
10892 /* The expression must be an id-expression. Assume that qualified
10893 names are the names of types so that:
10896 int S<T>::R::i = 3;
10898 will work; we must treat `S<T>::R' as the name of a type.
10899 Similarly, assume that qualified names are templates, where
10903 int S<T>::R<T>::i = 3;
10906 id_expression
= cp_parser_id_expression (parser
,
10907 /*template_keyword_p=*/false,
10908 /*check_dependency_p=*/false,
10909 /*template_p=*/NULL
,
10910 /*declarator_p=*/true);
10911 /* If the name was qualified, create a SCOPE_REF to represent
10915 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
10916 parser
->scope
= NULL_TREE
;
10919 return id_expression
;
10922 /* Parse a type-id.
10925 type-specifier-seq abstract-declarator [opt]
10927 Returns the TYPE specified. */
10930 cp_parser_type_id (cp_parser
* parser
)
10932 tree type_specifier_seq
;
10933 tree abstract_declarator
;
10935 /* Parse the type-specifier-seq. */
10937 = cp_parser_type_specifier_seq (parser
);
10938 if (type_specifier_seq
== error_mark_node
)
10939 return error_mark_node
;
10941 /* There might or might not be an abstract declarator. */
10942 cp_parser_parse_tentatively (parser
);
10943 /* Look for the declarator. */
10944 abstract_declarator
10945 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
10946 /*parenthesized_p=*/NULL
,
10947 /*member_p=*/false);
10948 /* Check to see if there really was a declarator. */
10949 if (!cp_parser_parse_definitely (parser
))
10950 abstract_declarator
= NULL_TREE
;
10952 return groktypename (build_tree_list (type_specifier_seq
,
10953 abstract_declarator
));
10956 /* Parse a type-specifier-seq.
10958 type-specifier-seq:
10959 type-specifier type-specifier-seq [opt]
10963 type-specifier-seq:
10964 attributes type-specifier-seq [opt]
10966 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10967 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10970 cp_parser_type_specifier_seq (cp_parser
* parser
)
10972 bool seen_type_specifier
= false;
10973 tree type_specifier_seq
= NULL_TREE
;
10975 /* Parse the type-specifiers and attributes. */
10978 tree type_specifier
;
10980 /* Check for attributes first. */
10981 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
10983 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
10985 type_specifier_seq
);
10989 /* After the first type-specifier, others are optional. */
10990 if (seen_type_specifier
)
10991 cp_parser_parse_tentatively (parser
);
10992 /* Look for the type-specifier. */
10993 type_specifier
= cp_parser_type_specifier (parser
,
10994 CP_PARSER_FLAGS_NONE
,
10995 /*is_friend=*/false,
10996 /*is_declaration=*/false,
10999 /* If the first type-specifier could not be found, this is not a
11000 type-specifier-seq at all. */
11001 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
11002 return error_mark_node
;
11003 /* If subsequent type-specifiers could not be found, the
11004 type-specifier-seq is complete. */
11005 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
11008 /* Add the new type-specifier to the list. */
11010 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
11011 seen_type_specifier
= true;
11014 /* We built up the list in reverse order. */
11015 return nreverse (type_specifier_seq
);
11018 /* Parse a parameter-declaration-clause.
11020 parameter-declaration-clause:
11021 parameter-declaration-list [opt] ... [opt]
11022 parameter-declaration-list , ...
11024 Returns a representation for the parameter declarations. Each node
11025 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
11026 representation.) If the parameter-declaration-clause ends with an
11027 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11028 list. A return value of NULL_TREE indicates a
11029 parameter-declaration-clause consisting only of an ellipsis. */
11032 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
11038 /* Peek at the next token. */
11039 token
= cp_lexer_peek_token (parser
->lexer
);
11040 /* Check for trivial parameter-declaration-clauses. */
11041 if (token
->type
== CPP_ELLIPSIS
)
11043 /* Consume the `...' token. */
11044 cp_lexer_consume_token (parser
->lexer
);
11047 else if (token
->type
== CPP_CLOSE_PAREN
)
11048 /* There are no parameters. */
11050 #ifndef NO_IMPLICIT_EXTERN_C
11051 if (in_system_header
&& current_class_type
== NULL
11052 && current_lang_name
== lang_name_c
)
11056 return void_list_node
;
11058 /* Check for `(void)', too, which is a special case. */
11059 else if (token
->keyword
== RID_VOID
11060 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11061 == CPP_CLOSE_PAREN
))
11063 /* Consume the `void' token. */
11064 cp_lexer_consume_token (parser
->lexer
);
11065 /* There are no parameters. */
11066 return void_list_node
;
11069 /* Parse the parameter-declaration-list. */
11070 parameters
= cp_parser_parameter_declaration_list (parser
);
11071 /* If a parse error occurred while parsing the
11072 parameter-declaration-list, then the entire
11073 parameter-declaration-clause is erroneous. */
11074 if (parameters
== error_mark_node
)
11075 return error_mark_node
;
11077 /* Peek at the next token. */
11078 token
= cp_lexer_peek_token (parser
->lexer
);
11079 /* If it's a `,', the clause should terminate with an ellipsis. */
11080 if (token
->type
== CPP_COMMA
)
11082 /* Consume the `,'. */
11083 cp_lexer_consume_token (parser
->lexer
);
11084 /* Expect an ellipsis. */
11086 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
11088 /* It might also be `...' if the optional trailing `,' was
11090 else if (token
->type
== CPP_ELLIPSIS
)
11092 /* Consume the `...' token. */
11093 cp_lexer_consume_token (parser
->lexer
);
11094 /* And remember that we saw it. */
11098 ellipsis_p
= false;
11100 /* Finish the parameter list. */
11101 return finish_parmlist (parameters
, ellipsis_p
);
11104 /* Parse a parameter-declaration-list.
11106 parameter-declaration-list:
11107 parameter-declaration
11108 parameter-declaration-list , parameter-declaration
11110 Returns a representation of the parameter-declaration-list, as for
11111 cp_parser_parameter_declaration_clause. However, the
11112 `void_list_node' is never appended to the list. */
11115 cp_parser_parameter_declaration_list (cp_parser
* parser
)
11117 tree parameters
= NULL_TREE
;
11119 /* Look for more parameters. */
11123 bool parenthesized_p
;
11124 /* Parse the parameter. */
11126 = cp_parser_parameter_declaration (parser
,
11127 /*template_parm_p=*/false,
11130 /* If a parse error occurred parsing the parameter declaration,
11131 then the entire parameter-declaration-list is erroneous. */
11132 if (parameter
== error_mark_node
)
11134 parameters
= error_mark_node
;
11137 /* Add the new parameter to the list. */
11138 TREE_CHAIN (parameter
) = parameters
;
11139 parameters
= parameter
;
11141 /* Peek at the next token. */
11142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
11143 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11144 /* The parameter-declaration-list is complete. */
11146 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11150 /* Peek at the next token. */
11151 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11152 /* If it's an ellipsis, then the list is complete. */
11153 if (token
->type
== CPP_ELLIPSIS
)
11155 /* Otherwise, there must be more parameters. Consume the
11157 cp_lexer_consume_token (parser
->lexer
);
11158 /* When parsing something like:
11160 int i(float f, double d)
11162 we can tell after seeing the declaration for "f" that we
11163 are not looking at an initialization of a variable "i",
11164 but rather at the declaration of a function "i".
11166 Due to the fact that the parsing of template arguments
11167 (as specified to a template-id) requires backtracking we
11168 cannot use this technique when inside a template argument
11170 if (!parser
->in_template_argument_list_p
11171 && !parser
->in_type_id_in_expr_p
11172 && cp_parser_parsing_tentatively (parser
)
11173 && !cp_parser_committed_to_tentative_parse (parser
)
11174 /* However, a parameter-declaration of the form
11175 "foat(f)" (which is a valid declaration of a
11176 parameter "f") can also be interpreted as an
11177 expression (the conversion of "f" to "float"). */
11178 && !parenthesized_p
)
11179 cp_parser_commit_to_tentative_parse (parser
);
11183 cp_parser_error (parser
, "expected `,' or `...'");
11184 if (!cp_parser_parsing_tentatively (parser
)
11185 || cp_parser_committed_to_tentative_parse (parser
))
11186 cp_parser_skip_to_closing_parenthesis (parser
,
11187 /*recovering=*/true,
11188 /*or_comma=*/false,
11189 /*consume_paren=*/false);
11194 /* We built up the list in reverse order; straighten it out now. */
11195 return nreverse (parameters
);
11198 /* Parse a parameter declaration.
11200 parameter-declaration:
11201 decl-specifier-seq declarator
11202 decl-specifier-seq declarator = assignment-expression
11203 decl-specifier-seq abstract-declarator [opt]
11204 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11206 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11207 declares a template parameter. (In that case, a non-nested `>'
11208 token encountered during the parsing of the assignment-expression
11209 is not interpreted as a greater-than operator.)
11211 Returns a TREE_LIST representing the parameter-declaration. The
11212 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11213 there is no default argument. The TREE_VALUE is a representation
11214 of the decl-specifier-seq and declarator. In particular, the
11215 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11216 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11217 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11218 the declarator is of the form "(p)". */
11221 cp_parser_parameter_declaration (cp_parser
*parser
,
11222 bool template_parm_p
,
11223 bool *parenthesized_p
)
11225 int declares_class_or_enum
;
11226 bool greater_than_is_operator_p
;
11227 tree decl_specifiers
;
11230 tree default_argument
;
11233 const char *saved_message
;
11235 /* In a template parameter, `>' is not an operator.
11239 When parsing a default template-argument for a non-type
11240 template-parameter, the first non-nested `>' is taken as the end
11241 of the template parameter-list rather than a greater-than
11243 greater_than_is_operator_p
= !template_parm_p
;
11245 /* Type definitions may not appear in parameter types. */
11246 saved_message
= parser
->type_definition_forbidden_message
;
11247 parser
->type_definition_forbidden_message
11248 = "types may not be defined in parameter types";
11250 /* Parse the declaration-specifiers. */
11252 = cp_parser_decl_specifier_seq (parser
,
11253 CP_PARSER_FLAGS_NONE
,
11255 &declares_class_or_enum
);
11256 /* If an error occurred, there's no reason to attempt to parse the
11257 rest of the declaration. */
11258 if (cp_parser_error_occurred (parser
))
11260 parser
->type_definition_forbidden_message
= saved_message
;
11261 return error_mark_node
;
11264 /* Peek at the next token. */
11265 token
= cp_lexer_peek_token (parser
->lexer
);
11266 /* If the next token is a `)', `,', `=', `>', or `...', then there
11267 is no declarator. */
11268 if (token
->type
== CPP_CLOSE_PAREN
11269 || token
->type
== CPP_COMMA
11270 || token
->type
== CPP_EQ
11271 || token
->type
== CPP_ELLIPSIS
11272 || token
->type
== CPP_GREATER
)
11274 declarator
= NULL_TREE
;
11275 if (parenthesized_p
)
11276 *parenthesized_p
= false;
11278 /* Otherwise, there should be a declarator. */
11281 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11282 parser
->default_arg_ok_p
= false;
11284 /* After seeing a decl-specifier-seq, if the next token is not a
11285 "(", there is no possibility that the code is a valid
11286 expression. Therefore, if parsing tentatively, we commit at
11288 if (!parser
->in_template_argument_list_p
11289 /* In an expression context, having seen:
11293 we cannot be sure whether we are looking at a
11294 function-type (taking a "char" as a parameter) or a cast
11295 of some object of type "char" to "int". */
11296 && !parser
->in_type_id_in_expr_p
11297 && cp_parser_parsing_tentatively (parser
)
11298 && !cp_parser_committed_to_tentative_parse (parser
)
11299 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11300 cp_parser_commit_to_tentative_parse (parser
);
11301 /* Parse the declarator. */
11302 declarator
= cp_parser_declarator (parser
,
11303 CP_PARSER_DECLARATOR_EITHER
,
11304 /*ctor_dtor_or_conv_p=*/NULL
,
11306 /*member_p=*/false);
11307 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11308 /* After the declarator, allow more attributes. */
11309 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
11312 /* The restriction on defining new types applies only to the type
11313 of the parameter, not to the default argument. */
11314 parser
->type_definition_forbidden_message
= saved_message
;
11316 /* If the next token is `=', then process a default argument. */
11317 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11319 bool saved_greater_than_is_operator_p
;
11320 /* Consume the `='. */
11321 cp_lexer_consume_token (parser
->lexer
);
11323 /* If we are defining a class, then the tokens that make up the
11324 default argument must be saved and processed later. */
11325 if (!template_parm_p
&& at_class_scope_p ()
11326 && TYPE_BEING_DEFINED (current_class_type
))
11328 unsigned depth
= 0;
11330 /* Create a DEFAULT_ARG to represented the unparsed default
11332 default_argument
= make_node (DEFAULT_ARG
);
11333 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
11335 /* Add tokens until we have processed the entire default
11342 /* Peek at the next token. */
11343 token
= cp_lexer_peek_token (parser
->lexer
);
11344 /* What we do depends on what token we have. */
11345 switch (token
->type
)
11347 /* In valid code, a default argument must be
11348 immediately followed by a `,' `)', or `...'. */
11350 case CPP_CLOSE_PAREN
:
11352 /* If we run into a non-nested `;', `}', or `]',
11353 then the code is invalid -- but the default
11354 argument is certainly over. */
11355 case CPP_SEMICOLON
:
11356 case CPP_CLOSE_BRACE
:
11357 case CPP_CLOSE_SQUARE
:
11360 /* Update DEPTH, if necessary. */
11361 else if (token
->type
== CPP_CLOSE_PAREN
11362 || token
->type
== CPP_CLOSE_BRACE
11363 || token
->type
== CPP_CLOSE_SQUARE
)
11367 case CPP_OPEN_PAREN
:
11368 case CPP_OPEN_SQUARE
:
11369 case CPP_OPEN_BRACE
:
11374 /* If we see a non-nested `>', and `>' is not an
11375 operator, then it marks the end of the default
11377 if (!depth
&& !greater_than_is_operator_p
)
11381 /* If we run out of tokens, issue an error message. */
11383 error ("file ends in default argument");
11389 /* In these cases, we should look for template-ids.
11390 For example, if the default argument is
11391 `X<int, double>()', we need to do name lookup to
11392 figure out whether or not `X' is a template; if
11393 so, the `,' does not end the default argument.
11395 That is not yet done. */
11402 /* If we've reached the end, stop. */
11406 /* Add the token to the token block. */
11407 token
= cp_lexer_consume_token (parser
->lexer
);
11408 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
11412 /* Outside of a class definition, we can just parse the
11413 assignment-expression. */
11416 bool saved_local_variables_forbidden_p
;
11418 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11420 saved_greater_than_is_operator_p
11421 = parser
->greater_than_is_operator_p
;
11422 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11423 /* Local variable names (and the `this' keyword) may not
11424 appear in a default argument. */
11425 saved_local_variables_forbidden_p
11426 = parser
->local_variables_forbidden_p
;
11427 parser
->local_variables_forbidden_p
= true;
11428 /* Parse the assignment-expression. */
11429 default_argument
= cp_parser_assignment_expression (parser
);
11430 /* Restore saved state. */
11431 parser
->greater_than_is_operator_p
11432 = saved_greater_than_is_operator_p
;
11433 parser
->local_variables_forbidden_p
11434 = saved_local_variables_forbidden_p
;
11436 if (!parser
->default_arg_ok_p
)
11438 if (!flag_pedantic_errors
)
11439 warning ("deprecated use of default argument for parameter of non-function");
11442 error ("default arguments are only permitted for function parameters");
11443 default_argument
= NULL_TREE
;
11448 default_argument
= NULL_TREE
;
11450 /* Create the representation of the parameter. */
11452 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
11453 parameter
= build_tree_list (default_argument
,
11454 build_tree_list (decl_specifiers
,
11460 /* Parse a function-body.
11463 compound_statement */
11466 cp_parser_function_body (cp_parser
*parser
)
11468 cp_parser_compound_statement (parser
, false);
11471 /* Parse a ctor-initializer-opt followed by a function-body. Return
11472 true if a ctor-initializer was present. */
11475 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11478 bool ctor_initializer_p
;
11480 /* Begin the function body. */
11481 body
= begin_function_body ();
11482 /* Parse the optional ctor-initializer. */
11483 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11484 /* Parse the function-body. */
11485 cp_parser_function_body (parser
);
11486 /* Finish the function body. */
11487 finish_function_body (body
);
11489 return ctor_initializer_p
;
11492 /* Parse an initializer.
11495 = initializer-clause
11496 ( expression-list )
11498 Returns a expression representing the initializer. If no
11499 initializer is present, NULL_TREE is returned.
11501 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11502 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11503 set to FALSE if there is no initializer present. If there is an
11504 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11505 is set to true; otherwise it is set to false. */
11508 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
11509 bool* non_constant_p
)
11514 /* Peek at the next token. */
11515 token
= cp_lexer_peek_token (parser
->lexer
);
11517 /* Let our caller know whether or not this initializer was
11519 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
11520 /* Assume that the initializer is constant. */
11521 *non_constant_p
= false;
11523 if (token
->type
== CPP_EQ
)
11525 /* Consume the `='. */
11526 cp_lexer_consume_token (parser
->lexer
);
11527 /* Parse the initializer-clause. */
11528 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
11530 else if (token
->type
== CPP_OPEN_PAREN
)
11531 init
= cp_parser_parenthesized_expression_list (parser
, false,
11535 /* Anything else is an error. */
11536 cp_parser_error (parser
, "expected initializer");
11537 init
= error_mark_node
;
11543 /* Parse an initializer-clause.
11545 initializer-clause:
11546 assignment-expression
11547 { initializer-list , [opt] }
11550 Returns an expression representing the initializer.
11552 If the `assignment-expression' production is used the value
11553 returned is simply a representation for the expression.
11555 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11556 the elements of the initializer-list (or NULL_TREE, if the last
11557 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11558 NULL_TREE. There is no way to detect whether or not the optional
11559 trailing `,' was provided. NON_CONSTANT_P is as for
11560 cp_parser_initializer. */
11563 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
11565 tree initializer
= NULL_TREE
;
11567 /* Assume the expression is constant. */
11568 *non_constant_p
= false;
11570 /* If it is not a `{', then we are looking at an
11571 assignment-expression. */
11572 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11574 /* Speed up common initializers (simply a literal). */
11575 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
11576 cp_token
* token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11578 if (token2
->type
== CPP_COMMA
)
11579 switch (token
->type
)
11584 token
= cp_lexer_consume_token (parser
->lexer
);
11585 initializer
= token
->value
;
11590 token
= cp_lexer_consume_token (parser
->lexer
);
11591 if (TREE_CHAIN (token
->value
))
11592 initializer
= TREE_CHAIN (token
->value
);
11594 initializer
= token
->value
;
11601 /* Otherwise, fall back to the generic assignment expression. */
11605 = cp_parser_constant_expression (parser
,
11606 /*allow_non_constant_p=*/true,
11608 if (!*non_constant_p
)
11609 initializer
= fold_non_dependent_expr (initializer
);
11614 /* Consume the `{' token. */
11615 cp_lexer_consume_token (parser
->lexer
);
11616 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11617 initializer
= make_node (CONSTRUCTOR
);
11618 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11619 necessary, but check_initializer depends upon it, for
11621 TREE_HAS_CONSTRUCTOR (initializer
) = 1;
11622 /* If it's not a `}', then there is a non-trivial initializer. */
11623 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11625 /* Parse the initializer list. */
11626 CONSTRUCTOR_ELTS (initializer
)
11627 = cp_parser_initializer_list (parser
, non_constant_p
);
11628 /* A trailing `,' token is allowed. */
11629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11630 cp_lexer_consume_token (parser
->lexer
);
11632 /* Now, there should be a trailing `}'. */
11633 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11636 return initializer
;
11639 /* Parse an initializer-list.
11643 initializer-list , initializer-clause
11648 identifier : initializer-clause
11649 initializer-list, identifier : initializer-clause
11651 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11652 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11653 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11654 as for cp_parser_initializer. */
11657 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
11659 tree initializers
= NULL_TREE
;
11661 /* Assume all of the expressions are constant. */
11662 *non_constant_p
= false;
11664 /* Parse the rest of the list. */
11670 bool clause_non_constant_p
;
11672 /* If the next token is an identifier and the following one is a
11673 colon, we are looking at the GNU designated-initializer
11675 if (cp_parser_allow_gnu_extensions_p (parser
)
11676 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11677 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11679 /* Consume the identifier. */
11680 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11681 /* Consume the `:'. */
11682 cp_lexer_consume_token (parser
->lexer
);
11685 identifier
= NULL_TREE
;
11687 /* Parse the initializer. */
11688 initializer
= cp_parser_initializer_clause (parser
,
11689 &clause_non_constant_p
);
11690 /* If any clause is non-constant, so is the entire initializer. */
11691 if (clause_non_constant_p
)
11692 *non_constant_p
= true;
11693 /* Add it to the list. */
11694 initializers
= tree_cons (identifier
, initializer
, initializers
);
11696 /* If the next token is not a comma, we have reached the end of
11698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11701 /* Peek at the next token. */
11702 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11703 /* If the next token is a `}', then we're still done. An
11704 initializer-clause can have a trailing `,' after the
11705 initializer-list and before the closing `}'. */
11706 if (token
->type
== CPP_CLOSE_BRACE
)
11709 /* Consume the `,' token. */
11710 cp_lexer_consume_token (parser
->lexer
);
11713 /* The initializers were built up in reverse order, so we need to
11714 reverse them now. */
11715 return nreverse (initializers
);
11718 /* Classes [gram.class] */
11720 /* Parse a class-name.
11726 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11727 to indicate that names looked up in dependent types should be
11728 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11729 keyword has been used to indicate that the name that appears next
11730 is a template. TYPE_P is true iff the next name should be treated
11731 as class-name, even if it is declared to be some other kind of name
11732 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11733 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11734 being defined in a class-head.
11736 Returns the TYPE_DECL representing the class. */
11739 cp_parser_class_name (cp_parser
*parser
,
11740 bool typename_keyword_p
,
11741 bool template_keyword_p
,
11743 bool check_dependency_p
,
11745 bool is_declaration
)
11752 /* All class-names start with an identifier. */
11753 token
= cp_lexer_peek_token (parser
->lexer
);
11754 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11756 cp_parser_error (parser
, "expected class-name");
11757 return error_mark_node
;
11760 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11761 to a template-id, so we save it here. */
11762 scope
= parser
->scope
;
11763 if (scope
== error_mark_node
)
11764 return error_mark_node
;
11766 /* Any name names a type if we're following the `typename' keyword
11767 in a qualified name where the enclosing scope is type-dependent. */
11768 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11769 && dependent_type_p (scope
));
11770 /* Handle the common case (an identifier, but not a template-id)
11772 if (token
->type
== CPP_NAME
11773 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
11777 /* Look for the identifier. */
11778 identifier
= cp_parser_identifier (parser
);
11779 /* If the next token isn't an identifier, we are certainly not
11780 looking at a class-name. */
11781 if (identifier
== error_mark_node
)
11782 decl
= error_mark_node
;
11783 /* If we know this is a type-name, there's no need to look it
11785 else if (typename_p
)
11789 /* If the next token is a `::', then the name must be a type
11792 [basic.lookup.qual]
11794 During the lookup for a name preceding the :: scope
11795 resolution operator, object, function, and enumerator
11796 names are ignored. */
11797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11799 /* Look up the name. */
11800 decl
= cp_parser_lookup_name (parser
, identifier
,
11802 /*is_template=*/false,
11803 /*is_namespace=*/false,
11804 check_dependency_p
);
11809 /* Try a template-id. */
11810 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11811 check_dependency_p
,
11813 if (decl
== error_mark_node
)
11814 return error_mark_node
;
11817 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11819 /* If this is a typename, create a TYPENAME_TYPE. */
11820 if (typename_p
&& decl
!= error_mark_node
)
11822 decl
= make_typename_type (scope
, decl
, /*complain=*/1);
11823 if (decl
!= error_mark_node
)
11824 decl
= TYPE_NAME (decl
);
11827 /* Check to see that it is really the name of a class. */
11828 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11829 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11830 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11831 /* Situations like this:
11833 template <typename T> struct A {
11834 typename T::template X<int>::I i;
11837 are problematic. Is `T::template X<int>' a class-name? The
11838 standard does not seem to be definitive, but there is no other
11839 valid interpretation of the following `::'. Therefore, those
11840 names are considered class-names. */
11841 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11842 else if (decl
== error_mark_node
11843 || TREE_CODE (decl
) != TYPE_DECL
11844 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11846 cp_parser_error (parser
, "expected class-name");
11847 return error_mark_node
;
11853 /* Parse a class-specifier.
11856 class-head { member-specification [opt] }
11858 Returns the TREE_TYPE representing the class. */
11861 cp_parser_class_specifier (cp_parser
* parser
)
11866 int has_trailing_semicolon
;
11867 bool nested_name_specifier_p
;
11868 unsigned saved_num_template_parameter_lists
;
11869 bool pop_p
= false;
11870 tree scope
= NULL_TREE
;
11872 push_deferring_access_checks (dk_no_deferred
);
11874 /* Parse the class-head. */
11875 type
= cp_parser_class_head (parser
,
11876 &nested_name_specifier_p
,
11878 /* If the class-head was a semantic disaster, skip the entire body
11882 cp_parser_skip_to_end_of_block_or_statement (parser
);
11883 pop_deferring_access_checks ();
11884 return error_mark_node
;
11887 /* Look for the `{'. */
11888 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11890 pop_deferring_access_checks ();
11891 return error_mark_node
;
11894 /* Issue an error message if type-definitions are forbidden here. */
11895 cp_parser_check_type_definition (parser
);
11896 /* Remember that we are defining one more class. */
11897 ++parser
->num_classes_being_defined
;
11898 /* Inside the class, surrounding template-parameter-lists do not
11900 saved_num_template_parameter_lists
11901 = parser
->num_template_parameter_lists
;
11902 parser
->num_template_parameter_lists
= 0;
11904 /* Start the class. */
11905 if (nested_name_specifier_p
)
11907 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
11908 pop_p
= push_scope (scope
);
11910 type
= begin_class_definition (type
);
11911 if (type
== error_mark_node
)
11912 /* If the type is erroneous, skip the entire body of the class. */
11913 cp_parser_skip_to_closing_brace (parser
);
11915 /* Parse the member-specification. */
11916 cp_parser_member_specification_opt (parser
);
11917 /* Look for the trailing `}'. */
11918 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11919 /* We get better error messages by noticing a common problem: a
11920 missing trailing `;'. */
11921 token
= cp_lexer_peek_token (parser
->lexer
);
11922 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11923 /* Look for trailing attributes to apply to this class. */
11924 if (cp_parser_allow_gnu_extensions_p (parser
))
11926 tree sub_attr
= cp_parser_attributes_opt (parser
);
11927 attributes
= chainon (attributes
, sub_attr
);
11929 if (type
!= error_mark_node
)
11930 type
= finish_struct (type
, attributes
);
11933 /* If this class is not itself within the scope of another class,
11934 then we need to parse the bodies of all of the queued function
11935 definitions. Note that the queued functions defined in a class
11936 are not always processed immediately following the
11937 class-specifier for that class. Consider:
11940 struct B { void f() { sizeof (A); } };
11943 If `f' were processed before the processing of `A' were
11944 completed, there would be no way to compute the size of `A'.
11945 Note that the nesting we are interested in here is lexical --
11946 not the semantic nesting given by TYPE_CONTEXT. In particular,
11949 struct A { struct B; };
11950 struct A::B { void f() { } };
11952 there is no need to delay the parsing of `A::B::f'. */
11953 if (--parser
->num_classes_being_defined
== 0)
11958 /* In a first pass, parse default arguments to the functions.
11959 Then, in a second pass, parse the bodies of the functions.
11960 This two-phased approach handles cases like:
11968 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
11969 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
11970 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
11971 TREE_PURPOSE (parser
->unparsed_functions_queues
)
11972 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
11974 fn
= TREE_VALUE (queue_entry
);
11975 /* Make sure that any template parameters are in scope. */
11976 maybe_begin_member_template_processing (fn
);
11977 /* If there are default arguments that have not yet been processed,
11978 take care of them now. */
11979 cp_parser_late_parsing_default_args (parser
, fn
);
11980 /* Remove any template parameters from the symbol table. */
11981 maybe_end_member_template_processing ();
11983 /* Now parse the body of the functions. */
11984 for (TREE_VALUE (parser
->unparsed_functions_queues
)
11985 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
11986 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
11987 TREE_VALUE (parser
->unparsed_functions_queues
)
11988 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
11990 /* Figure out which function we need to process. */
11991 fn
= TREE_VALUE (queue_entry
);
11993 /* A hack to prevent garbage collection. */
11996 /* Parse the function. */
11997 cp_parser_late_parsing_for_member (parser
, fn
);
12003 /* Put back any saved access checks. */
12004 pop_deferring_access_checks ();
12006 /* Restore the count of active template-parameter-lists. */
12007 parser
->num_template_parameter_lists
12008 = saved_num_template_parameter_lists
;
12013 /* Parse a class-head.
12016 class-key identifier [opt] base-clause [opt]
12017 class-key nested-name-specifier identifier base-clause [opt]
12018 class-key nested-name-specifier [opt] template-id
12022 class-key attributes identifier [opt] base-clause [opt]
12023 class-key attributes nested-name-specifier identifier base-clause [opt]
12024 class-key attributes nested-name-specifier [opt] template-id
12027 Returns the TYPE of the indicated class. Sets
12028 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12029 involving a nested-name-specifier was used, and FALSE otherwise.
12031 Returns NULL_TREE if the class-head is syntactically valid, but
12032 semantically invalid in a way that means we should skip the entire
12033 body of the class. */
12036 cp_parser_class_head (cp_parser
* parser
,
12037 bool* nested_name_specifier_p
,
12038 tree
*attributes_p
)
12041 tree nested_name_specifier
;
12042 enum tag_types class_key
;
12043 tree id
= NULL_TREE
;
12044 tree type
= NULL_TREE
;
12046 bool template_id_p
= false;
12047 bool qualified_p
= false;
12048 bool invalid_nested_name_p
= false;
12049 bool invalid_explicit_specialization_p
= false;
12050 bool pop_p
= false;
12051 unsigned num_templates
;
12053 /* Assume no nested-name-specifier will be present. */
12054 *nested_name_specifier_p
= false;
12055 /* Assume no template parameter lists will be used in defining the
12059 /* Look for the class-key. */
12060 class_key
= cp_parser_class_key (parser
);
12061 if (class_key
== none_type
)
12062 return error_mark_node
;
12064 /* Parse the attributes. */
12065 attributes
= cp_parser_attributes_opt (parser
);
12067 /* If the next token is `::', that is invalid -- but sometimes
12068 people do try to write:
12072 Handle this gracefully by accepting the extra qualifier, and then
12073 issuing an error about it later if this really is a
12074 class-head. If it turns out just to be an elaborated type
12075 specifier, remain silent. */
12076 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
12077 qualified_p
= true;
12079 push_deferring_access_checks (dk_no_check
);
12081 /* Determine the name of the class. Begin by looking for an
12082 optional nested-name-specifier. */
12083 nested_name_specifier
12084 = cp_parser_nested_name_specifier_opt (parser
,
12085 /*typename_keyword_p=*/false,
12086 /*check_dependency_p=*/false,
12088 /*is_declaration=*/false);
12089 /* If there was a nested-name-specifier, then there *must* be an
12091 if (nested_name_specifier
)
12093 /* Although the grammar says `identifier', it really means
12094 `class-name' or `template-name'. You are only allowed to
12095 define a class that has already been declared with this
12098 The proposed resolution for Core Issue 180 says that whever
12099 you see `class T::X' you should treat `X' as a type-name.
12101 It is OK to define an inaccessible class; for example:
12103 class A { class B; };
12106 We do not know if we will see a class-name, or a
12107 template-name. We look for a class-name first, in case the
12108 class-name is a template-id; if we looked for the
12109 template-name first we would stop after the template-name. */
12110 cp_parser_parse_tentatively (parser
);
12111 type
= cp_parser_class_name (parser
,
12112 /*typename_keyword_p=*/false,
12113 /*template_keyword_p=*/false,
12115 /*check_dependency_p=*/false,
12116 /*class_head_p=*/true,
12117 /*is_declaration=*/false);
12118 /* If that didn't work, ignore the nested-name-specifier. */
12119 if (!cp_parser_parse_definitely (parser
))
12121 invalid_nested_name_p
= true;
12122 id
= cp_parser_identifier (parser
);
12123 if (id
== error_mark_node
)
12126 /* If we could not find a corresponding TYPE, treat this
12127 declaration like an unqualified declaration. */
12128 if (type
== error_mark_node
)
12129 nested_name_specifier
= NULL_TREE
;
12130 /* Otherwise, count the number of templates used in TYPE and its
12131 containing scopes. */
12136 for (scope
= TREE_TYPE (type
);
12137 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
12138 scope
= (TYPE_P (scope
)
12139 ? TYPE_CONTEXT (scope
)
12140 : DECL_CONTEXT (scope
)))
12142 && CLASS_TYPE_P (scope
)
12143 && CLASSTYPE_TEMPLATE_INFO (scope
)
12144 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
12145 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
12149 /* Otherwise, the identifier is optional. */
12152 /* We don't know whether what comes next is a template-id,
12153 an identifier, or nothing at all. */
12154 cp_parser_parse_tentatively (parser
);
12155 /* Check for a template-id. */
12156 id
= cp_parser_template_id (parser
,
12157 /*template_keyword_p=*/false,
12158 /*check_dependency_p=*/true,
12159 /*is_declaration=*/true);
12160 /* If that didn't work, it could still be an identifier. */
12161 if (!cp_parser_parse_definitely (parser
))
12163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12164 id
= cp_parser_identifier (parser
);
12170 template_id_p
= true;
12175 pop_deferring_access_checks ();
12178 cp_parser_check_for_invalid_template_id (parser
, id
);
12180 /* If it's not a `:' or a `{' then we can't really be looking at a
12181 class-head, since a class-head only appears as part of a
12182 class-specifier. We have to detect this situation before calling
12183 xref_tag, since that has irreversible side-effects. */
12184 if (!cp_parser_next_token_starts_class_definition_p (parser
))
12186 cp_parser_error (parser
, "expected `{' or `:'");
12187 return error_mark_node
;
12190 /* At this point, we're going ahead with the class-specifier, even
12191 if some other problem occurs. */
12192 cp_parser_commit_to_tentative_parse (parser
);
12193 /* Issue the error about the overly-qualified name now. */
12195 cp_parser_error (parser
,
12196 "global qualification of class name is invalid");
12197 else if (invalid_nested_name_p
)
12198 cp_parser_error (parser
,
12199 "qualified name does not name a class");
12200 else if (nested_name_specifier
)
12204 /* Reject typedef-names in class heads. */
12205 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
12207 error ("invalid class name in declaration of `%D'", type
);
12212 /* Figure out in what scope the declaration is being placed. */
12213 scope
= current_scope ();
12215 scope
= current_namespace
;
12216 /* If that scope does not contain the scope in which the
12217 class was originally declared, the program is invalid. */
12218 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
12220 error ("declaration of `%D' in `%D' which does not "
12221 "enclose `%D'", type
, scope
, nested_name_specifier
);
12227 A declarator-id shall not be qualified exception of the
12228 definition of a ... nested class outside of its class
12229 ... [or] a the definition or explicit instantiation of a
12230 class member of a namespace outside of its namespace. */
12231 if (scope
== nested_name_specifier
)
12233 pedwarn ("extra qualification ignored");
12234 nested_name_specifier
= NULL_TREE
;
12238 /* An explicit-specialization must be preceded by "template <>". If
12239 it is not, try to recover gracefully. */
12240 if (at_namespace_scope_p ()
12241 && parser
->num_template_parameter_lists
== 0
12244 error ("an explicit specialization must be preceded by 'template <>'");
12245 invalid_explicit_specialization_p
= true;
12246 /* Take the same action that would have been taken by
12247 cp_parser_explicit_specialization. */
12248 ++parser
->num_template_parameter_lists
;
12249 begin_specialization ();
12251 /* There must be no "return" statements between this point and the
12252 end of this function; set "type "to the correct return value and
12253 use "goto done;" to return. */
12254 /* Make sure that the right number of template parameters were
12256 if (!cp_parser_check_template_parameters (parser
, num_templates
))
12258 /* If something went wrong, there is no point in even trying to
12259 process the class-definition. */
12264 /* Look up the type. */
12267 type
= TREE_TYPE (id
);
12268 maybe_process_partial_specialization (type
);
12270 else if (!nested_name_specifier
)
12272 /* If the class was unnamed, create a dummy name. */
12274 id
= make_anon_name ();
12275 type
= xref_tag (class_key
, id
, /*globalize=*/false,
12276 parser
->num_template_parameter_lists
);
12281 bool pop_p
= false;
12285 template <typename T> struct S { struct T };
12286 template <typename T> struct S<T>::T { };
12288 we will get a TYPENAME_TYPE when processing the definition of
12289 `S::T'. We need to resolve it to the actual type before we
12290 try to define it. */
12291 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
12293 class_type
= resolve_typename_type (TREE_TYPE (type
),
12294 /*only_current_p=*/false);
12295 if (class_type
!= error_mark_node
)
12296 type
= TYPE_NAME (class_type
);
12299 cp_parser_error (parser
, "could not resolve typename type");
12300 type
= error_mark_node
;
12304 maybe_process_partial_specialization (TREE_TYPE (type
));
12305 class_type
= current_class_type
;
12306 /* Enter the scope indicated by the nested-name-specifier. */
12307 if (nested_name_specifier
)
12308 pop_p
= push_scope (nested_name_specifier
);
12309 /* Get the canonical version of this type. */
12310 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
12311 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12312 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
12313 type
= push_template_decl (type
);
12314 type
= TREE_TYPE (type
);
12315 if (nested_name_specifier
)
12317 *nested_name_specifier_p
= true;
12319 pop_scope (nested_name_specifier
);
12322 /* Indicate whether this class was declared as a `class' or as a
12324 if (TREE_CODE (type
) == RECORD_TYPE
)
12325 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
12326 cp_parser_check_class_key (class_key
, type
);
12328 /* Enter the scope containing the class; the names of base classes
12329 should be looked up in that context. For example, given:
12331 struct A { struct B {}; struct C; };
12332 struct A::C : B {};
12335 if (nested_name_specifier
)
12336 pop_p
= push_scope (nested_name_specifier
);
12337 /* Now, look for the base-clause. */
12338 token
= cp_lexer_peek_token (parser
->lexer
);
12339 if (token
->type
== CPP_COLON
)
12343 /* Get the list of base-classes. */
12344 bases
= cp_parser_base_clause (parser
);
12345 /* Process them. */
12346 xref_basetypes (type
, bases
);
12348 /* Leave the scope given by the nested-name-specifier. We will
12349 enter the class scope itself while processing the members. */
12351 pop_scope (nested_name_specifier
);
12354 if (invalid_explicit_specialization_p
)
12356 end_specialization ();
12357 --parser
->num_template_parameter_lists
;
12359 *attributes_p
= attributes
;
12363 /* Parse a class-key.
12370 Returns the kind of class-key specified, or none_type to indicate
12373 static enum tag_types
12374 cp_parser_class_key (cp_parser
* parser
)
12377 enum tag_types tag_type
;
12379 /* Look for the class-key. */
12380 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12384 /* Check to see if the TOKEN is a class-key. */
12385 tag_type
= cp_parser_token_is_class_key (token
);
12387 cp_parser_error (parser
, "expected class-key");
12391 /* Parse an (optional) member-specification.
12393 member-specification:
12394 member-declaration member-specification [opt]
12395 access-specifier : member-specification [opt] */
12398 cp_parser_member_specification_opt (cp_parser
* parser
)
12405 /* Peek at the next token. */
12406 token
= cp_lexer_peek_token (parser
->lexer
);
12407 /* If it's a `}', or EOF then we've seen all the members. */
12408 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12411 /* See if this token is a keyword. */
12412 keyword
= token
->keyword
;
12416 case RID_PROTECTED
:
12418 /* Consume the access-specifier. */
12419 cp_lexer_consume_token (parser
->lexer
);
12420 /* Remember which access-specifier is active. */
12421 current_access_specifier
= token
->value
;
12422 /* Look for the `:'. */
12423 cp_parser_require (parser
, CPP_COLON
, "`:'");
12427 /* Otherwise, the next construction must be a
12428 member-declaration. */
12429 cp_parser_member_declaration (parser
);
12434 /* Parse a member-declaration.
12436 member-declaration:
12437 decl-specifier-seq [opt] member-declarator-list [opt] ;
12438 function-definition ; [opt]
12439 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12441 template-declaration
12443 member-declarator-list:
12445 member-declarator-list , member-declarator
12448 declarator pure-specifier [opt]
12449 declarator constant-initializer [opt]
12450 identifier [opt] : constant-expression
12454 member-declaration:
12455 __extension__ member-declaration
12458 declarator attributes [opt] pure-specifier [opt]
12459 declarator attributes [opt] constant-initializer [opt]
12460 identifier [opt] attributes [opt] : constant-expression */
12463 cp_parser_member_declaration (cp_parser
* parser
)
12465 tree decl_specifiers
;
12466 tree prefix_attributes
;
12468 int declares_class_or_enum
;
12471 int saved_pedantic
;
12473 /* Check for the `__extension__' keyword. */
12474 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12477 cp_parser_member_declaration (parser
);
12478 /* Restore the old value of the PEDANTIC flag. */
12479 pedantic
= saved_pedantic
;
12484 /* Check for a template-declaration. */
12485 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12487 /* An explicit specialization here is an error condition, and we
12488 expect the specialization handler to detect and report this. */
12489 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
12490 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12491 cp_parser_explicit_specialization (parser
);
12493 cp_parser_template_declaration (parser
, /*member_p=*/true);
12498 /* Check for a using-declaration. */
12499 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
12501 /* Parse the using-declaration. */
12502 cp_parser_using_declaration (parser
);
12507 /* Parse the decl-specifier-seq. */
12509 = cp_parser_decl_specifier_seq (parser
,
12510 CP_PARSER_FLAGS_OPTIONAL
,
12511 &prefix_attributes
,
12512 &declares_class_or_enum
);
12513 /* Check for an invalid type-name. */
12514 if (cp_parser_diagnose_invalid_type_name (parser
))
12516 /* If there is no declarator, then the decl-specifier-seq should
12518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12520 /* If there was no decl-specifier-seq, and the next token is a
12521 `;', then we have something like:
12527 Each member-declaration shall declare at least one member
12528 name of the class. */
12529 if (!decl_specifiers
)
12532 pedwarn ("extra semicolon");
12538 /* See if this declaration is a friend. */
12539 friend_p
= cp_parser_friend_p (decl_specifiers
);
12540 /* If there were decl-specifiers, check to see if there was
12541 a class-declaration. */
12542 type
= check_tag_decl (decl_specifiers
);
12543 /* Nested classes have already been added to the class, but
12544 a `friend' needs to be explicitly registered. */
12547 /* If the `friend' keyword was present, the friend must
12548 be introduced with a class-key. */
12549 if (!declares_class_or_enum
)
12550 error ("a class-key must be used when declaring a friend");
12553 template <typename T> struct A {
12554 friend struct A<T>::B;
12557 A<T>::B will be represented by a TYPENAME_TYPE, and
12558 therefore not recognized by check_tag_decl. */
12563 for (specifier
= decl_specifiers
;
12565 specifier
= TREE_CHAIN (specifier
))
12567 tree s
= TREE_VALUE (specifier
);
12569 if (TREE_CODE (s
) == IDENTIFIER_NODE
)
12570 get_global_value_if_present (s
, &type
);
12571 if (TREE_CODE (s
) == TYPE_DECL
)
12580 if (!type
|| !TYPE_P (type
))
12581 error ("friend declaration does not name a class or "
12584 make_friend_class (current_class_type
, type
,
12585 /*complain=*/true);
12587 /* If there is no TYPE, an error message will already have
12591 /* An anonymous aggregate has to be handled specially; such
12592 a declaration really declares a data member (with a
12593 particular type), as opposed to a nested class. */
12594 else if (ANON_AGGR_TYPE_P (type
))
12596 /* Remove constructors and such from TYPE, now that we
12597 know it is an anonymous aggregate. */
12598 fixup_anonymous_aggr (type
);
12599 /* And make the corresponding data member. */
12600 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
12601 /* Add it to the class. */
12602 finish_member_declaration (decl
);
12605 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
12610 /* See if these declarations will be friends. */
12611 friend_p
= cp_parser_friend_p (decl_specifiers
);
12613 /* Keep going until we hit the `;' at the end of the
12615 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12617 tree attributes
= NULL_TREE
;
12618 tree first_attribute
;
12620 /* Peek at the next token. */
12621 token
= cp_lexer_peek_token (parser
->lexer
);
12623 /* Check for a bitfield declaration. */
12624 if (token
->type
== CPP_COLON
12625 || (token
->type
== CPP_NAME
12626 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12632 /* Get the name of the bitfield. Note that we cannot just
12633 check TOKEN here because it may have been invalidated by
12634 the call to cp_lexer_peek_nth_token above. */
12635 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
12636 identifier
= cp_parser_identifier (parser
);
12638 identifier
= NULL_TREE
;
12640 /* Consume the `:' token. */
12641 cp_lexer_consume_token (parser
->lexer
);
12642 /* Get the width of the bitfield. */
12644 = cp_parser_constant_expression (parser
,
12645 /*allow_non_constant=*/false,
12648 /* Look for attributes that apply to the bitfield. */
12649 attributes
= cp_parser_attributes_opt (parser
);
12650 /* Remember which attributes are prefix attributes and
12652 first_attribute
= attributes
;
12653 /* Combine the attributes. */
12654 attributes
= chainon (prefix_attributes
, attributes
);
12656 /* Create the bitfield declaration. */
12657 decl
= grokbitfield (identifier
,
12660 /* Apply the attributes. */
12661 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
12667 tree asm_specification
;
12668 int ctor_dtor_or_conv_p
;
12670 /* Parse the declarator. */
12672 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12673 &ctor_dtor_or_conv_p
,
12674 /*parenthesized_p=*/NULL
,
12675 /*member_p=*/true);
12677 /* If something went wrong parsing the declarator, make sure
12678 that we at least consume some tokens. */
12679 if (declarator
== error_mark_node
)
12681 /* Skip to the end of the statement. */
12682 cp_parser_skip_to_end_of_statement (parser
);
12683 /* If the next token is not a semicolon, that is
12684 probably because we just skipped over the body of
12685 a function. So, we consume a semicolon if
12686 present, but do not issue an error message if it
12688 if (cp_lexer_next_token_is (parser
->lexer
,
12690 cp_lexer_consume_token (parser
->lexer
);
12694 if (declares_class_or_enum
& 2)
12695 cp_parser_check_for_definition_in_return_type
12696 (declarator
, TREE_VALUE (decl_specifiers
));
12698 /* Look for an asm-specification. */
12699 asm_specification
= cp_parser_asm_specification_opt (parser
);
12700 /* Look for attributes that apply to the declaration. */
12701 attributes
= cp_parser_attributes_opt (parser
);
12702 /* Remember which attributes are prefix attributes and
12704 first_attribute
= attributes
;
12705 /* Combine the attributes. */
12706 attributes
= chainon (prefix_attributes
, attributes
);
12708 /* If it's an `=', then we have a constant-initializer or a
12709 pure-specifier. It is not correct to parse the
12710 initializer before registering the member declaration
12711 since the member declaration should be in scope while
12712 its initializer is processed. However, the rest of the
12713 front end does not yet provide an interface that allows
12714 us to handle this correctly. */
12715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12719 A pure-specifier shall be used only in the declaration of
12720 a virtual function.
12722 A member-declarator can contain a constant-initializer
12723 only if it declares a static member of integral or
12726 Therefore, if the DECLARATOR is for a function, we look
12727 for a pure-specifier; otherwise, we look for a
12728 constant-initializer. When we call `grokfield', it will
12729 perform more stringent semantics checks. */
12730 if (TREE_CODE (declarator
) == CALL_EXPR
)
12731 initializer
= cp_parser_pure_specifier (parser
);
12733 /* Parse the initializer. */
12734 initializer
= cp_parser_constant_initializer (parser
);
12736 /* Otherwise, there is no initializer. */
12738 initializer
= NULL_TREE
;
12740 /* See if we are probably looking at a function
12741 definition. We are certainly not looking at at a
12742 member-declarator. Calling `grokfield' has
12743 side-effects, so we must not do it unless we are sure
12744 that we are looking at a member-declarator. */
12745 if (cp_parser_token_starts_function_definition_p
12746 (cp_lexer_peek_token (parser
->lexer
)))
12748 /* The grammar does not allow a pure-specifier to be
12749 used when a member function is defined. (It is
12750 possible that this fact is an oversight in the
12751 standard, since a pure function may be defined
12752 outside of the class-specifier. */
12754 error ("pure-specifier on function-definition");
12755 decl
= cp_parser_save_member_function_body (parser
,
12759 /* If the member was not a friend, declare it here. */
12761 finish_member_declaration (decl
);
12762 /* Peek at the next token. */
12763 token
= cp_lexer_peek_token (parser
->lexer
);
12764 /* If the next token is a semicolon, consume it. */
12765 if (token
->type
== CPP_SEMICOLON
)
12766 cp_lexer_consume_token (parser
->lexer
);
12771 /* Create the declaration. */
12772 decl
= grokfield (declarator
, decl_specifiers
,
12773 initializer
, asm_specification
,
12775 /* Any initialization must have been from a
12776 constant-expression. */
12777 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
12778 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
12782 /* Reset PREFIX_ATTRIBUTES. */
12783 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12784 attributes
= TREE_CHAIN (attributes
);
12786 TREE_CHAIN (attributes
) = NULL_TREE
;
12788 /* If there is any qualification still in effect, clear it
12789 now; we will be starting fresh with the next declarator. */
12790 parser
->scope
= NULL_TREE
;
12791 parser
->qualifying_scope
= NULL_TREE
;
12792 parser
->object_scope
= NULL_TREE
;
12793 /* If it's a `,', then there are more declarators. */
12794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12795 cp_lexer_consume_token (parser
->lexer
);
12796 /* If the next token isn't a `;', then we have a parse error. */
12797 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12800 cp_parser_error (parser
, "expected `;'");
12801 /* Skip tokens until we find a `;'. */
12802 cp_parser_skip_to_end_of_statement (parser
);
12809 /* Add DECL to the list of members. */
12811 finish_member_declaration (decl
);
12813 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12814 cp_parser_save_default_args (parser
, decl
);
12819 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12822 /* Parse a pure-specifier.
12827 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12828 Otherwise, ERROR_MARK_NODE is returned. */
12831 cp_parser_pure_specifier (cp_parser
* parser
)
12835 /* Look for the `=' token. */
12836 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12837 return error_mark_node
;
12838 /* Look for the `0' token. */
12839 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12840 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12841 to get information from the lexer about how the number was
12842 spelled in order to fix this problem. */
12843 if (!token
|| !integer_zerop (token
->value
))
12844 return error_mark_node
;
12846 return integer_zero_node
;
12849 /* Parse a constant-initializer.
12851 constant-initializer:
12852 = constant-expression
12854 Returns a representation of the constant-expression. */
12857 cp_parser_constant_initializer (cp_parser
* parser
)
12859 /* Look for the `=' token. */
12860 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12861 return error_mark_node
;
12863 /* It is invalid to write:
12865 struct S { static const int i = { 7 }; };
12868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12870 cp_parser_error (parser
,
12871 "a brace-enclosed initializer is not allowed here");
12872 /* Consume the opening brace. */
12873 cp_lexer_consume_token (parser
->lexer
);
12874 /* Skip the initializer. */
12875 cp_parser_skip_to_closing_brace (parser
);
12876 /* Look for the trailing `}'. */
12877 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12879 return error_mark_node
;
12882 return cp_parser_constant_expression (parser
,
12883 /*allow_non_constant=*/false,
12887 /* Derived classes [gram.class.derived] */
12889 /* Parse a base-clause.
12892 : base-specifier-list
12894 base-specifier-list:
12896 base-specifier-list , base-specifier
12898 Returns a TREE_LIST representing the base-classes, in the order in
12899 which they were declared. The representation of each node is as
12900 described by cp_parser_base_specifier.
12902 In the case that no bases are specified, this function will return
12903 NULL_TREE, not ERROR_MARK_NODE. */
12906 cp_parser_base_clause (cp_parser
* parser
)
12908 tree bases
= NULL_TREE
;
12910 /* Look for the `:' that begins the list. */
12911 cp_parser_require (parser
, CPP_COLON
, "`:'");
12913 /* Scan the base-specifier-list. */
12919 /* Look for the base-specifier. */
12920 base
= cp_parser_base_specifier (parser
);
12921 /* Add BASE to the front of the list. */
12922 if (base
!= error_mark_node
)
12924 TREE_CHAIN (base
) = bases
;
12927 /* Peek at the next token. */
12928 token
= cp_lexer_peek_token (parser
->lexer
);
12929 /* If it's not a comma, then the list is complete. */
12930 if (token
->type
!= CPP_COMMA
)
12932 /* Consume the `,'. */
12933 cp_lexer_consume_token (parser
->lexer
);
12936 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12937 base class had a qualified name. However, the next name that
12938 appears is certainly not qualified. */
12939 parser
->scope
= NULL_TREE
;
12940 parser
->qualifying_scope
= NULL_TREE
;
12941 parser
->object_scope
= NULL_TREE
;
12943 return nreverse (bases
);
12946 /* Parse a base-specifier.
12949 :: [opt] nested-name-specifier [opt] class-name
12950 virtual access-specifier [opt] :: [opt] nested-name-specifier
12952 access-specifier virtual [opt] :: [opt] nested-name-specifier
12955 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12956 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12957 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12958 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12961 cp_parser_base_specifier (cp_parser
* parser
)
12965 bool virtual_p
= false;
12966 bool duplicate_virtual_error_issued_p
= false;
12967 bool duplicate_access_error_issued_p
= false;
12968 bool class_scope_p
, template_p
;
12969 tree access
= access_default_node
;
12972 /* Process the optional `virtual' and `access-specifier'. */
12975 /* Peek at the next token. */
12976 token
= cp_lexer_peek_token (parser
->lexer
);
12977 /* Process `virtual'. */
12978 switch (token
->keyword
)
12981 /* If `virtual' appears more than once, issue an error. */
12982 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
12984 cp_parser_error (parser
,
12985 "`virtual' specified more than once in base-specified");
12986 duplicate_virtual_error_issued_p
= true;
12991 /* Consume the `virtual' token. */
12992 cp_lexer_consume_token (parser
->lexer
);
12997 case RID_PROTECTED
:
12999 /* If more than one access specifier appears, issue an
13001 if (access
!= access_default_node
13002 && !duplicate_access_error_issued_p
)
13004 cp_parser_error (parser
,
13005 "more than one access specifier in base-specified");
13006 duplicate_access_error_issued_p
= true;
13009 access
= ridpointers
[(int) token
->keyword
];
13011 /* Consume the access-specifier. */
13012 cp_lexer_consume_token (parser
->lexer
);
13021 /* It is not uncommon to see programs mechanically, errouneously, use
13022 the 'typename' keyword to denote (dependent) qualified types
13023 as base classes. */
13024 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13026 if (!processing_template_decl
)
13027 error ("keyword `typename' not allowed outside of templates");
13029 error ("keyword `typename' not allowed in this context "
13030 "(the base class is implicitly a type)");
13031 cp_lexer_consume_token (parser
->lexer
);
13034 /* Look for the optional `::' operator. */
13035 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13036 /* Look for the nested-name-specifier. The simplest way to
13041 The keyword `typename' is not permitted in a base-specifier or
13042 mem-initializer; in these contexts a qualified name that
13043 depends on a template-parameter is implicitly assumed to be a
13046 is to pretend that we have seen the `typename' keyword at this
13048 cp_parser_nested_name_specifier_opt (parser
,
13049 /*typename_keyword_p=*/true,
13050 /*check_dependency_p=*/true,
13052 /*is_declaration=*/true);
13053 /* If the base class is given by a qualified name, assume that names
13054 we see are type names or templates, as appropriate. */
13055 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
13056 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
13058 /* Finally, look for the class-name. */
13059 type
= cp_parser_class_name (parser
,
13063 /*check_dependency_p=*/true,
13064 /*class_head_p=*/false,
13065 /*is_declaration=*/true);
13067 if (type
== error_mark_node
)
13068 return error_mark_node
;
13070 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
13073 /* Exception handling [gram.exception] */
13075 /* Parse an (optional) exception-specification.
13077 exception-specification:
13078 throw ( type-id-list [opt] )
13080 Returns a TREE_LIST representing the exception-specification. The
13081 TREE_VALUE of each node is a type. */
13084 cp_parser_exception_specification_opt (cp_parser
* parser
)
13089 /* Peek at the next token. */
13090 token
= cp_lexer_peek_token (parser
->lexer
);
13091 /* If it's not `throw', then there's no exception-specification. */
13092 if (!cp_parser_is_keyword (token
, RID_THROW
))
13095 /* Consume the `throw'. */
13096 cp_lexer_consume_token (parser
->lexer
);
13098 /* Look for the `('. */
13099 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13101 /* Peek at the next token. */
13102 token
= cp_lexer_peek_token (parser
->lexer
);
13103 /* If it's not a `)', then there is a type-id-list. */
13104 if (token
->type
!= CPP_CLOSE_PAREN
)
13106 const char *saved_message
;
13108 /* Types may not be defined in an exception-specification. */
13109 saved_message
= parser
->type_definition_forbidden_message
;
13110 parser
->type_definition_forbidden_message
13111 = "types may not be defined in an exception-specification";
13112 /* Parse the type-id-list. */
13113 type_id_list
= cp_parser_type_id_list (parser
);
13114 /* Restore the saved message. */
13115 parser
->type_definition_forbidden_message
= saved_message
;
13118 type_id_list
= empty_except_spec
;
13120 /* Look for the `)'. */
13121 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13123 return type_id_list
;
13126 /* Parse an (optional) type-id-list.
13130 type-id-list , type-id
13132 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13133 in the order that the types were presented. */
13136 cp_parser_type_id_list (cp_parser
* parser
)
13138 tree types
= NULL_TREE
;
13145 /* Get the next type-id. */
13146 type
= cp_parser_type_id (parser
);
13147 /* Add it to the list. */
13148 types
= add_exception_specifier (types
, type
, /*complain=*/1);
13149 /* Peek at the next token. */
13150 token
= cp_lexer_peek_token (parser
->lexer
);
13151 /* If it is not a `,', we are done. */
13152 if (token
->type
!= CPP_COMMA
)
13154 /* Consume the `,'. */
13155 cp_lexer_consume_token (parser
->lexer
);
13158 return nreverse (types
);
13161 /* Parse a try-block.
13164 try compound-statement handler-seq */
13167 cp_parser_try_block (cp_parser
* parser
)
13171 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
13172 try_block
= begin_try_block ();
13173 cp_parser_compound_statement (parser
, false);
13174 finish_try_block (try_block
);
13175 cp_parser_handler_seq (parser
);
13176 finish_handler_sequence (try_block
);
13181 /* Parse a function-try-block.
13183 function-try-block:
13184 try ctor-initializer [opt] function-body handler-seq */
13187 cp_parser_function_try_block (cp_parser
* parser
)
13190 bool ctor_initializer_p
;
13192 /* Look for the `try' keyword. */
13193 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
13195 /* Let the rest of the front-end know where we are. */
13196 try_block
= begin_function_try_block ();
13197 /* Parse the function-body. */
13199 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13200 /* We're done with the `try' part. */
13201 finish_function_try_block (try_block
);
13202 /* Parse the handlers. */
13203 cp_parser_handler_seq (parser
);
13204 /* We're done with the handlers. */
13205 finish_function_handler_sequence (try_block
);
13207 return ctor_initializer_p
;
13210 /* Parse a handler-seq.
13213 handler handler-seq [opt] */
13216 cp_parser_handler_seq (cp_parser
* parser
)
13222 /* Parse the handler. */
13223 cp_parser_handler (parser
);
13224 /* Peek at the next token. */
13225 token
= cp_lexer_peek_token (parser
->lexer
);
13226 /* If it's not `catch' then there are no more handlers. */
13227 if (!cp_parser_is_keyword (token
, RID_CATCH
))
13232 /* Parse a handler.
13235 catch ( exception-declaration ) compound-statement */
13238 cp_parser_handler (cp_parser
* parser
)
13243 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
13244 handler
= begin_handler ();
13245 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13246 declaration
= cp_parser_exception_declaration (parser
);
13247 finish_handler_parms (declaration
, handler
);
13248 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13249 cp_parser_compound_statement (parser
, false);
13250 finish_handler (handler
);
13253 /* Parse an exception-declaration.
13255 exception-declaration:
13256 type-specifier-seq declarator
13257 type-specifier-seq abstract-declarator
13261 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13262 ellipsis variant is used. */
13265 cp_parser_exception_declaration (cp_parser
* parser
)
13267 tree type_specifiers
;
13269 const char *saved_message
;
13271 /* If it's an ellipsis, it's easy to handle. */
13272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13274 /* Consume the `...' token. */
13275 cp_lexer_consume_token (parser
->lexer
);
13279 /* Types may not be defined in exception-declarations. */
13280 saved_message
= parser
->type_definition_forbidden_message
;
13281 parser
->type_definition_forbidden_message
13282 = "types may not be defined in exception-declarations";
13284 /* Parse the type-specifier-seq. */
13285 type_specifiers
= cp_parser_type_specifier_seq (parser
);
13286 /* If it's a `)', then there is no declarator. */
13287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
13288 declarator
= NULL_TREE
;
13290 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
13291 /*ctor_dtor_or_conv_p=*/NULL
,
13292 /*parenthesized_p=*/NULL
,
13293 /*member_p=*/false);
13295 /* Restore the saved message. */
13296 parser
->type_definition_forbidden_message
= saved_message
;
13298 return start_handler_parms (type_specifiers
, declarator
);
13301 /* Parse a throw-expression.
13304 throw assignment-expression [opt]
13306 Returns a THROW_EXPR representing the throw-expression. */
13309 cp_parser_throw_expression (cp_parser
* parser
)
13314 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
13315 token
= cp_lexer_peek_token (parser
->lexer
);
13316 /* Figure out whether or not there is an assignment-expression
13317 following the "throw" keyword. */
13318 if (token
->type
== CPP_COMMA
13319 || token
->type
== CPP_SEMICOLON
13320 || token
->type
== CPP_CLOSE_PAREN
13321 || token
->type
== CPP_CLOSE_SQUARE
13322 || token
->type
== CPP_CLOSE_BRACE
13323 || token
->type
== CPP_COLON
)
13324 expression
= NULL_TREE
;
13326 expression
= cp_parser_assignment_expression (parser
);
13328 return build_throw (expression
);
13331 /* GNU Extensions */
13333 /* Parse an (optional) asm-specification.
13336 asm ( string-literal )
13338 If the asm-specification is present, returns a STRING_CST
13339 corresponding to the string-literal. Otherwise, returns
13343 cp_parser_asm_specification_opt (cp_parser
* parser
)
13346 tree asm_specification
;
13348 /* Peek at the next token. */
13349 token
= cp_lexer_peek_token (parser
->lexer
);
13350 /* If the next token isn't the `asm' keyword, then there's no
13351 asm-specification. */
13352 if (!cp_parser_is_keyword (token
, RID_ASM
))
13355 /* Consume the `asm' token. */
13356 cp_lexer_consume_token (parser
->lexer
);
13357 /* Look for the `('. */
13358 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13360 /* Look for the string-literal. */
13361 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13363 asm_specification
= token
->value
;
13365 asm_specification
= NULL_TREE
;
13367 /* Look for the `)'. */
13368 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13370 return asm_specification
;
13373 /* Parse an asm-operand-list.
13377 asm-operand-list , asm-operand
13380 string-literal ( expression )
13381 [ string-literal ] string-literal ( expression )
13383 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13384 each node is the expression. The TREE_PURPOSE is itself a
13385 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13386 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13387 is a STRING_CST for the string literal before the parenthesis. */
13390 cp_parser_asm_operand_list (cp_parser
* parser
)
13392 tree asm_operands
= NULL_TREE
;
13396 tree string_literal
;
13401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13403 /* Consume the `[' token. */
13404 cp_lexer_consume_token (parser
->lexer
);
13405 /* Read the operand name. */
13406 name
= cp_parser_identifier (parser
);
13407 if (name
!= error_mark_node
)
13408 name
= build_string (IDENTIFIER_LENGTH (name
),
13409 IDENTIFIER_POINTER (name
));
13410 /* Look for the closing `]'. */
13411 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13415 /* Look for the string-literal. */
13416 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13417 string_literal
= token
? token
->value
: error_mark_node
;
13418 /* Look for the `('. */
13419 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13420 /* Parse the expression. */
13421 expression
= cp_parser_expression (parser
);
13422 /* Look for the `)'. */
13423 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13424 /* Add this operand to the list. */
13425 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
13428 /* If the next token is not a `,', there are no more
13430 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13432 /* Consume the `,'. */
13433 cp_lexer_consume_token (parser
->lexer
);
13436 return nreverse (asm_operands
);
13439 /* Parse an asm-clobber-list.
13443 asm-clobber-list , string-literal
13445 Returns a TREE_LIST, indicating the clobbers in the order that they
13446 appeared. The TREE_VALUE of each node is a STRING_CST. */
13449 cp_parser_asm_clobber_list (cp_parser
* parser
)
13451 tree clobbers
= NULL_TREE
;
13456 tree string_literal
;
13458 /* Look for the string literal. */
13459 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13460 string_literal
= token
? token
->value
: error_mark_node
;
13461 /* Add it to the list. */
13462 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13463 /* If the next token is not a `,', then the list is
13465 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13467 /* Consume the `,' token. */
13468 cp_lexer_consume_token (parser
->lexer
);
13474 /* Parse an (optional) series of attributes.
13477 attributes attribute
13480 __attribute__ (( attribute-list [opt] ))
13482 The return value is as for cp_parser_attribute_list. */
13485 cp_parser_attributes_opt (cp_parser
* parser
)
13487 tree attributes
= NULL_TREE
;
13492 tree attribute_list
;
13494 /* Peek at the next token. */
13495 token
= cp_lexer_peek_token (parser
->lexer
);
13496 /* If it's not `__attribute__', then we're done. */
13497 if (token
->keyword
!= RID_ATTRIBUTE
)
13500 /* Consume the `__attribute__' keyword. */
13501 cp_lexer_consume_token (parser
->lexer
);
13502 /* Look for the two `(' tokens. */
13503 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13504 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13506 /* Peek at the next token. */
13507 token
= cp_lexer_peek_token (parser
->lexer
);
13508 if (token
->type
!= CPP_CLOSE_PAREN
)
13509 /* Parse the attribute-list. */
13510 attribute_list
= cp_parser_attribute_list (parser
);
13512 /* If the next token is a `)', then there is no attribute
13514 attribute_list
= NULL
;
13516 /* Look for the two `)' tokens. */
13517 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13518 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13520 /* Add these new attributes to the list. */
13521 attributes
= chainon (attributes
, attribute_list
);
13527 /* Parse an attribute-list.
13531 attribute-list , attribute
13535 identifier ( identifier )
13536 identifier ( identifier , expression-list )
13537 identifier ( expression-list )
13539 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13540 TREE_PURPOSE of each node is the identifier indicating which
13541 attribute is in use. The TREE_VALUE represents the arguments, if
13545 cp_parser_attribute_list (cp_parser
* parser
)
13547 tree attribute_list
= NULL_TREE
;
13555 /* Look for the identifier. We also allow keywords here; for
13556 example `__attribute__ ((const))' is legal. */
13557 token
= cp_lexer_peek_token (parser
->lexer
);
13558 if (token
->type
== CPP_NAME
13559 || token
->type
== CPP_KEYWORD
)
13561 /* Consume the token. */
13562 token
= cp_lexer_consume_token (parser
->lexer
);
13564 /* Save away the identifier that indicates which attribute
13566 identifier
= token
->value
;
13567 attribute
= build_tree_list (identifier
, NULL_TREE
);
13569 /* Peek at the next token. */
13570 token
= cp_lexer_peek_token (parser
->lexer
);
13571 /* If it's an `(', then parse the attribute arguments. */
13572 if (token
->type
== CPP_OPEN_PAREN
)
13576 arguments
= (cp_parser_parenthesized_expression_list
13577 (parser
, true, /*non_constant_p=*/NULL
));
13578 /* Save the identifier and arguments away. */
13579 TREE_VALUE (attribute
) = arguments
;
13582 /* Add this attribute to the list. */
13583 TREE_CHAIN (attribute
) = attribute_list
;
13584 attribute_list
= attribute
;
13586 /* Now, look for more attributes. */
13587 token
= cp_lexer_peek_token (parser
->lexer
);
13589 /* Now, look for more attributes. If the next token isn't a
13590 `,', we're done. */
13591 if (token
->type
!= CPP_COMMA
)
13594 /* Consume the comma and keep going. */
13595 cp_lexer_consume_token (parser
->lexer
);
13598 /* We built up the list in reverse order. */
13599 return nreverse (attribute_list
);
13602 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13603 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13604 current value of the PEDANTIC flag, regardless of whether or not
13605 the `__extension__' keyword is present. The caller is responsible
13606 for restoring the value of the PEDANTIC flag. */
13609 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
13611 /* Save the old value of the PEDANTIC flag. */
13612 *saved_pedantic
= pedantic
;
13614 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
13616 /* Consume the `__extension__' token. */
13617 cp_lexer_consume_token (parser
->lexer
);
13618 /* We're not being pedantic while the `__extension__' keyword is
13628 /* Parse a label declaration.
13631 __label__ label-declarator-seq ;
13633 label-declarator-seq:
13634 identifier , label-declarator-seq
13638 cp_parser_label_declaration (cp_parser
* parser
)
13640 /* Look for the `__label__' keyword. */
13641 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
13647 /* Look for an identifier. */
13648 identifier
= cp_parser_identifier (parser
);
13649 /* If we failed, stop. */
13650 if (identifier
== error_mark_node
)
13652 /* Declare it as a label. */
13653 finish_label_decl (identifier
);
13654 /* If the next token is a `;', stop. */
13655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13657 /* Look for the `,' separating the label declarations. */
13658 cp_parser_require (parser
, CPP_COMMA
, "`,'");
13661 /* Look for the final `;'. */
13662 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13665 /* Support Functions */
13667 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13668 NAME should have one of the representations used for an
13669 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13670 is returned. If PARSER->SCOPE is a dependent type, then a
13671 SCOPE_REF is returned.
13673 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13674 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13675 was formed. Abstractly, such entities should not be passed to this
13676 function, because they do not need to be looked up, but it is
13677 simpler to check for this special case here, rather than at the
13680 In cases not explicitly covered above, this function returns a
13681 DECL, OVERLOAD, or baselink representing the result of the lookup.
13682 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13685 If IS_TYPE is TRUE, bindings that do not refer to types are
13688 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13691 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13694 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13698 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
13699 bool is_type
, bool is_template
, bool is_namespace
,
13700 bool check_dependency
)
13704 tree object_type
= parser
->context
->object_type
;
13706 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13707 no longer valid. Note that if we are parsing tentatively, and
13708 the parse fails, OBJECT_TYPE will be automatically restored. */
13709 parser
->context
->object_type
= NULL_TREE
;
13711 if (name
== error_mark_node
)
13712 return error_mark_node
;
13714 if (!cp_parser_parsing_tentatively (parser
)
13715 || cp_parser_committed_to_tentative_parse (parser
))
13716 flags
|= LOOKUP_COMPLAIN
;
13718 /* A template-id has already been resolved; there is no lookup to
13720 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13722 if (BASELINK_P (name
))
13724 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
13725 == TEMPLATE_ID_EXPR
),
13730 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13731 it should already have been checked to make sure that the name
13732 used matches the type being destroyed. */
13733 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13737 /* Figure out to which type this destructor applies. */
13739 type
= parser
->scope
;
13740 else if (object_type
)
13741 type
= object_type
;
13743 type
= current_class_type
;
13744 /* If that's not a class type, there is no destructor. */
13745 if (!type
|| !CLASS_TYPE_P (type
))
13746 return error_mark_node
;
13747 if (!CLASSTYPE_DESTRUCTORS (type
))
13748 return error_mark_node
;
13749 /* If it was a class type, return the destructor. */
13750 return CLASSTYPE_DESTRUCTORS (type
);
13753 /* By this point, the NAME should be an ordinary identifier. If
13754 the id-expression was a qualified name, the qualifying scope is
13755 stored in PARSER->SCOPE at this point. */
13756 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
13759 /* Perform the lookup. */
13764 if (parser
->scope
== error_mark_node
)
13765 return error_mark_node
;
13767 /* If the SCOPE is dependent, the lookup must be deferred until
13768 the template is instantiated -- unless we are explicitly
13769 looking up names in uninstantiated templates. Even then, we
13770 cannot look up the name if the scope is not a class type; it
13771 might, for example, be a template type parameter. */
13772 dependent_p
= (TYPE_P (parser
->scope
)
13773 && !(parser
->in_declarator_p
13774 && currently_open_class (parser
->scope
))
13775 && dependent_type_p (parser
->scope
));
13776 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
13780 /* The resolution to Core Issue 180 says that `struct A::B'
13781 should be considered a type-name, even if `A' is
13783 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
13786 else if (is_template
)
13787 decl
= make_unbound_class_template (parser
->scope
,
13791 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
13795 bool pop_p
= false;
13797 /* If PARSER->SCOPE is a dependent type, then it must be a
13798 class type, and we must not be checking dependencies;
13799 otherwise, we would have processed this lookup above. So
13800 that PARSER->SCOPE is not considered a dependent base by
13801 lookup_member, we must enter the scope here. */
13803 pop_p
= push_scope (parser
->scope
);
13804 /* If the PARSER->SCOPE is a a template specialization, it
13805 may be instantiated during name lookup. In that case,
13806 errors may be issued. Even if we rollback the current
13807 tentative parse, those errors are valid. */
13808 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
13809 /*complain=*/true);
13811 pop_scope (parser
->scope
);
13813 parser
->qualifying_scope
= parser
->scope
;
13814 parser
->object_scope
= NULL_TREE
;
13816 else if (object_type
)
13818 tree object_decl
= NULL_TREE
;
13819 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13820 OBJECT_TYPE is not a class. */
13821 if (CLASS_TYPE_P (object_type
))
13822 /* If the OBJECT_TYPE is a template specialization, it may
13823 be instantiated during name lookup. In that case, errors
13824 may be issued. Even if we rollback the current tentative
13825 parse, those errors are valid. */
13826 object_decl
= lookup_member (object_type
,
13828 /*protect=*/0, is_type
);
13829 /* Look it up in the enclosing context, too. */
13830 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13831 is_namespace
, flags
);
13832 parser
->object_scope
= object_type
;
13833 parser
->qualifying_scope
= NULL_TREE
;
13835 decl
= object_decl
;
13839 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13840 is_namespace
, flags
);
13841 parser
->qualifying_scope
= NULL_TREE
;
13842 parser
->object_scope
= NULL_TREE
;
13845 /* If the lookup failed, let our caller know. */
13847 || decl
== error_mark_node
13848 || (TREE_CODE (decl
) == FUNCTION_DECL
13849 && DECL_ANTICIPATED (decl
)))
13850 return error_mark_node
;
13852 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13853 if (TREE_CODE (decl
) == TREE_LIST
)
13855 /* The error message we have to print is too complicated for
13856 cp_parser_error, so we incorporate its actions directly. */
13857 if (!cp_parser_simulate_error (parser
))
13859 error ("reference to `%D' is ambiguous", name
);
13860 print_candidates (decl
);
13862 return error_mark_node
;
13865 my_friendly_assert (DECL_P (decl
)
13866 || TREE_CODE (decl
) == OVERLOAD
13867 || TREE_CODE (decl
) == SCOPE_REF
13868 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
13869 || BASELINK_P (decl
),
13872 /* If we have resolved the name of a member declaration, check to
13873 see if the declaration is accessible. When the name resolves to
13874 set of overloaded functions, accessibility is checked when
13875 overload resolution is done.
13877 During an explicit instantiation, access is not checked at all,
13878 as per [temp.explicit]. */
13880 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
13885 /* Like cp_parser_lookup_name, but for use in the typical case where
13886 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13887 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13890 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13892 return cp_parser_lookup_name (parser
, name
,
13894 /*is_template=*/false,
13895 /*is_namespace=*/false,
13896 /*check_dependency=*/true);
13899 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13900 the current context, return the TYPE_DECL. If TAG_NAME_P is
13901 true, the DECL indicates the class being defined in a class-head,
13902 or declared in an elaborated-type-specifier.
13904 Otherwise, return DECL. */
13907 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13909 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13910 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13913 template <typename T> struct B;
13916 template <typename T> struct A::B {};
13918 Similarly, in a elaborated-type-specifier:
13920 namespace N { struct X{}; }
13923 template <typename T> friend struct N::X;
13926 However, if the DECL refers to a class type, and we are in
13927 the scope of the class, then the name lookup automatically
13928 finds the TYPE_DECL created by build_self_reference rather
13929 than a TEMPLATE_DECL. For example, in:
13931 template <class T> struct S {
13935 there is no need to handle such case. */
13937 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13938 return DECL_TEMPLATE_RESULT (decl
);
13943 /* If too many, or too few, template-parameter lists apply to the
13944 declarator, issue an error message. Returns TRUE if all went well,
13945 and FALSE otherwise. */
13948 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13951 unsigned num_templates
;
13953 /* We haven't seen any classes that involve template parameters yet. */
13956 switch (TREE_CODE (declarator
))
13963 tree main_declarator
= TREE_OPERAND (declarator
, 0);
13965 cp_parser_check_declarator_template_parameters (parser
,
13974 scope
= TREE_OPERAND (declarator
, 0);
13975 member
= TREE_OPERAND (declarator
, 1);
13977 /* If this is a pointer-to-member, then we are not interested
13978 in the SCOPE, because it does not qualify the thing that is
13980 if (TREE_CODE (member
) == INDIRECT_REF
)
13981 return (cp_parser_check_declarator_template_parameters
13984 while (scope
&& CLASS_TYPE_P (scope
))
13986 /* You're supposed to have one `template <...>'
13987 for every template class, but you don't need one
13988 for a full specialization. For example:
13990 template <class T> struct S{};
13991 template <> struct S<int> { void f(); };
13992 void S<int>::f () {}
13994 is correct; there shouldn't be a `template <>' for
13995 the definition of `S<int>::f'. */
13996 if (CLASSTYPE_TEMPLATE_INFO (scope
)
13997 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
13998 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
13999 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
14002 scope
= TYPE_CONTEXT (scope
);
14006 /* Fall through. */
14009 /* If the DECLARATOR has the form `X<y>' then it uses one
14010 additional level of template parameters. */
14011 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
14014 return cp_parser_check_template_parameters (parser
,
14019 /* NUM_TEMPLATES were used in the current declaration. If that is
14020 invalid, return FALSE and issue an error messages. Otherwise,
14024 cp_parser_check_template_parameters (cp_parser
* parser
,
14025 unsigned num_templates
)
14027 /* If there are more template classes than parameter lists, we have
14030 template <class T> void S<T>::R<T>::f (); */
14031 if (parser
->num_template_parameter_lists
< num_templates
)
14033 error ("too few template-parameter-lists");
14036 /* If there are the same number of template classes and parameter
14037 lists, that's OK. */
14038 if (parser
->num_template_parameter_lists
== num_templates
)
14040 /* If there are more, but only one more, then we are referring to a
14041 member template. That's OK too. */
14042 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
14044 /* Otherwise, there are too many template parameter lists. We have
14047 template <class T> template <class U> void S::f(); */
14048 error ("too many template-parameter-lists");
14052 /* Parse a binary-expression of the general form:
14056 binary-expression <token> <expr>
14058 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14059 to parser the <expr>s. If the first production is used, then the
14060 value returned by FN is returned directly. Otherwise, a node with
14061 the indicated EXPR_TYPE is returned, with operands corresponding to
14062 the two sub-expressions. */
14065 cp_parser_binary_expression (cp_parser
* parser
,
14066 const cp_parser_token_tree_map token_tree_map
,
14067 cp_parser_expression_fn fn
)
14071 /* Parse the first expression. */
14072 lhs
= (*fn
) (parser
);
14073 /* Now, look for more expressions. */
14077 const cp_parser_token_tree_map_node
*map_node
;
14080 /* Peek at the next token. */
14081 token
= cp_lexer_peek_token (parser
->lexer
);
14082 /* If the token is `>', and that's not an operator at the
14083 moment, then we're done. */
14084 if (token
->type
== CPP_GREATER
14085 && !parser
->greater_than_is_operator_p
)
14087 /* If we find one of the tokens we want, build the corresponding
14088 tree representation. */
14089 for (map_node
= token_tree_map
;
14090 map_node
->token_type
!= CPP_EOF
;
14092 if (map_node
->token_type
== token
->type
)
14094 /* Assume that an overloaded operator will not be used. */
14095 bool overloaded_p
= false;
14097 /* Consume the operator token. */
14098 cp_lexer_consume_token (parser
->lexer
);
14099 /* Parse the right-hand side of the expression. */
14100 rhs
= (*fn
) (parser
);
14101 /* Build the binary tree node. */
14102 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
,
14104 /* If the binary operator required the use of an
14105 overloaded operator, then this expression cannot be an
14106 integral constant-expression. An overloaded operator
14107 can be used even if both operands are otherwise
14108 permissible in an integral constant-expression if at
14109 least one of the operands is of enumeration type. */
14111 && (cp_parser_non_integral_constant_expression
14112 (parser
, "calls to overloaded operators")))
14113 lhs
= error_mark_node
;
14117 /* If the token wasn't one of the ones we want, we're done. */
14118 if (map_node
->token_type
== CPP_EOF
)
14125 /* Parse an optional `::' token indicating that the following name is
14126 from the global namespace. If so, PARSER->SCOPE is set to the
14127 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14128 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14129 Returns the new value of PARSER->SCOPE, if the `::' token is
14130 present, and NULL_TREE otherwise. */
14133 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
14137 /* Peek at the next token. */
14138 token
= cp_lexer_peek_token (parser
->lexer
);
14139 /* If we're looking at a `::' token then we're starting from the
14140 global namespace, not our current location. */
14141 if (token
->type
== CPP_SCOPE
)
14143 /* Consume the `::' token. */
14144 cp_lexer_consume_token (parser
->lexer
);
14145 /* Set the SCOPE so that we know where to start the lookup. */
14146 parser
->scope
= global_namespace
;
14147 parser
->qualifying_scope
= global_namespace
;
14148 parser
->object_scope
= NULL_TREE
;
14150 return parser
->scope
;
14152 else if (!current_scope_valid_p
)
14154 parser
->scope
= NULL_TREE
;
14155 parser
->qualifying_scope
= NULL_TREE
;
14156 parser
->object_scope
= NULL_TREE
;
14162 /* Returns TRUE if the upcoming token sequence is the start of a
14163 constructor declarator. If FRIEND_P is true, the declarator is
14164 preceded by the `friend' specifier. */
14167 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
14169 bool constructor_p
;
14170 tree type_decl
= NULL_TREE
;
14171 bool nested_name_p
;
14172 cp_token
*next_token
;
14174 /* The common case is that this is not a constructor declarator, so
14175 try to avoid doing lots of work if at all possible. It's not
14176 valid declare a constructor at function scope. */
14177 if (at_function_scope_p ())
14179 /* And only certain tokens can begin a constructor declarator. */
14180 next_token
= cp_lexer_peek_token (parser
->lexer
);
14181 if (next_token
->type
!= CPP_NAME
14182 && next_token
->type
!= CPP_SCOPE
14183 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
14184 && next_token
->type
!= CPP_TEMPLATE_ID
)
14187 /* Parse tentatively; we are going to roll back all of the tokens
14189 cp_parser_parse_tentatively (parser
);
14190 /* Assume that we are looking at a constructor declarator. */
14191 constructor_p
= true;
14193 /* Look for the optional `::' operator. */
14194 cp_parser_global_scope_opt (parser
,
14195 /*current_scope_valid_p=*/false);
14196 /* Look for the nested-name-specifier. */
14198 = (cp_parser_nested_name_specifier_opt (parser
,
14199 /*typename_keyword_p=*/false,
14200 /*check_dependency_p=*/false,
14202 /*is_declaration=*/false)
14204 /* Outside of a class-specifier, there must be a
14205 nested-name-specifier. */
14206 if (!nested_name_p
&&
14207 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
14209 constructor_p
= false;
14210 /* If we still think that this might be a constructor-declarator,
14211 look for a class-name. */
14216 template <typename T> struct S { S(); };
14217 template <typename T> S<T>::S ();
14219 we must recognize that the nested `S' names a class.
14222 template <typename T> S<T>::S<T> ();
14224 we must recognize that the nested `S' names a template. */
14225 type_decl
= cp_parser_class_name (parser
,
14226 /*typename_keyword_p=*/false,
14227 /*template_keyword_p=*/false,
14229 /*check_dependency_p=*/false,
14230 /*class_head_p=*/false,
14231 /*is_declaration=*/false);
14232 /* If there was no class-name, then this is not a constructor. */
14233 constructor_p
= !cp_parser_error_occurred (parser
);
14236 /* If we're still considering a constructor, we have to see a `(',
14237 to begin the parameter-declaration-clause, followed by either a
14238 `)', an `...', or a decl-specifier. We need to check for a
14239 type-specifier to avoid being fooled into thinking that:
14243 is a constructor. (It is actually a function named `f' that
14244 takes one parameter (of type `int') and returns a value of type
14247 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14249 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
14250 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
14251 /* A parameter declaration begins with a decl-specifier,
14252 which is either the "attribute" keyword, a storage class
14253 specifier, or (usually) a type-specifier. */
14254 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
14255 && !cp_parser_storage_class_specifier_opt (parser
))
14258 bool pop_p
= false;
14259 unsigned saved_num_template_parameter_lists
;
14261 /* Names appearing in the type-specifier should be looked up
14262 in the scope of the class. */
14263 if (current_class_type
)
14267 type
= TREE_TYPE (type_decl
);
14268 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14270 type
= resolve_typename_type (type
,
14271 /*only_current_p=*/false);
14272 if (type
== error_mark_node
)
14274 cp_parser_abort_tentative_parse (parser
);
14278 pop_p
= push_scope (type
);
14281 /* Inside the constructor parameter list, surrounding
14282 template-parameter-lists do not apply. */
14283 saved_num_template_parameter_lists
14284 = parser
->num_template_parameter_lists
;
14285 parser
->num_template_parameter_lists
= 0;
14287 /* Look for the type-specifier. */
14288 cp_parser_type_specifier (parser
,
14289 CP_PARSER_FLAGS_NONE
,
14290 /*is_friend=*/false,
14291 /*is_declarator=*/true,
14292 /*declares_class_or_enum=*/NULL
,
14293 /*is_cv_qualifier=*/NULL
);
14295 parser
->num_template_parameter_lists
14296 = saved_num_template_parameter_lists
;
14298 /* Leave the scope of the class. */
14302 constructor_p
= !cp_parser_error_occurred (parser
);
14306 constructor_p
= false;
14307 /* We did not really want to consume any tokens. */
14308 cp_parser_abort_tentative_parse (parser
);
14310 return constructor_p
;
14313 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14314 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14315 they must be performed once we are in the scope of the function.
14317 Returns the function defined. */
14320 cp_parser_function_definition_from_specifiers_and_declarator
14321 (cp_parser
* parser
,
14322 tree decl_specifiers
,
14329 /* Begin the function-definition. */
14330 success_p
= begin_function_definition (decl_specifiers
,
14334 /* If there were names looked up in the decl-specifier-seq that we
14335 did not check, check them now. We must wait until we are in the
14336 scope of the function to perform the checks, since the function
14337 might be a friend. */
14338 perform_deferred_access_checks ();
14342 /* If begin_function_definition didn't like the definition, skip
14343 the entire function. */
14344 error ("invalid function declaration");
14345 cp_parser_skip_to_end_of_block_or_statement (parser
);
14346 fn
= error_mark_node
;
14349 fn
= cp_parser_function_definition_after_declarator (parser
,
14350 /*inline_p=*/false);
14355 /* Parse the part of a function-definition that follows the
14356 declarator. INLINE_P is TRUE iff this function is an inline
14357 function defined with a class-specifier.
14359 Returns the function defined. */
14362 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
14366 bool ctor_initializer_p
= false;
14367 bool saved_in_unbraced_linkage_specification_p
;
14368 unsigned saved_num_template_parameter_lists
;
14370 /* If the next token is `return', then the code may be trying to
14371 make use of the "named return value" extension that G++ used to
14373 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
14375 /* Consume the `return' keyword. */
14376 cp_lexer_consume_token (parser
->lexer
);
14377 /* Look for the identifier that indicates what value is to be
14379 cp_parser_identifier (parser
);
14380 /* Issue an error message. */
14381 error ("named return values are no longer supported");
14382 /* Skip tokens until we reach the start of the function body. */
14383 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
14384 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
14385 cp_lexer_consume_token (parser
->lexer
);
14387 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14388 anything declared inside `f'. */
14389 saved_in_unbraced_linkage_specification_p
14390 = parser
->in_unbraced_linkage_specification_p
;
14391 parser
->in_unbraced_linkage_specification_p
= false;
14392 /* Inside the function, surrounding template-parameter-lists do not
14394 saved_num_template_parameter_lists
14395 = parser
->num_template_parameter_lists
;
14396 parser
->num_template_parameter_lists
= 0;
14397 /* If the next token is `try', then we are looking at a
14398 function-try-block. */
14399 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14400 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14401 /* A function-try-block includes the function-body, so we only do
14402 this next part if we're not processing a function-try-block. */
14405 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14407 /* Finish the function. */
14408 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14409 (inline_p
? 2 : 0));
14410 /* Generate code for it, if necessary. */
14411 expand_or_defer_fn (fn
);
14412 /* Restore the saved values. */
14413 parser
->in_unbraced_linkage_specification_p
14414 = saved_in_unbraced_linkage_specification_p
;
14415 parser
->num_template_parameter_lists
14416 = saved_num_template_parameter_lists
;
14421 /* Parse a template-declaration, assuming that the `export' (and
14422 `extern') keywords, if present, has already been scanned. MEMBER_P
14423 is as for cp_parser_template_declaration. */
14426 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14428 tree decl
= NULL_TREE
;
14429 tree parameter_list
;
14430 bool friend_p
= false;
14432 /* Look for the `template' keyword. */
14433 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14437 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14440 /* If the next token is `>', then we have an invalid
14441 specialization. Rather than complain about an invalid template
14442 parameter, issue an error message here. */
14443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14445 cp_parser_error (parser
, "invalid explicit specialization");
14446 begin_specialization ();
14447 parameter_list
= NULL_TREE
;
14451 /* Parse the template parameters. */
14452 begin_template_parm_list ();
14453 parameter_list
= cp_parser_template_parameter_list (parser
);
14454 parameter_list
= end_template_parm_list (parameter_list
);
14457 /* Look for the `>'. */
14458 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14459 /* We just processed one more parameter list. */
14460 ++parser
->num_template_parameter_lists
;
14461 /* If the next token is `template', there are more template
14463 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14465 cp_parser_template_declaration_after_export (parser
, member_p
);
14468 decl
= cp_parser_single_declaration (parser
,
14472 /* If this is a member template declaration, let the front
14474 if (member_p
&& !friend_p
&& decl
)
14476 if (TREE_CODE (decl
) == TYPE_DECL
)
14477 cp_parser_check_access_in_redeclaration (decl
);
14479 decl
= finish_member_template_decl (decl
);
14481 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14482 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14483 /*complain=*/true);
14485 /* We are done with the current parameter list. */
14486 --parser
->num_template_parameter_lists
;
14489 finish_template_decl (parameter_list
);
14491 /* Register member declarations. */
14492 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14493 finish_member_declaration (decl
);
14495 /* If DECL is a function template, we must return to parse it later.
14496 (Even though there is no definition, there might be default
14497 arguments that need handling.) */
14498 if (member_p
&& decl
14499 && (TREE_CODE (decl
) == FUNCTION_DECL
14500 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14501 TREE_VALUE (parser
->unparsed_functions_queues
)
14502 = tree_cons (NULL_TREE
, decl
,
14503 TREE_VALUE (parser
->unparsed_functions_queues
));
14506 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14507 `function-definition' sequence. MEMBER_P is true, this declaration
14508 appears in a class scope.
14510 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14511 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14514 cp_parser_single_declaration (cp_parser
* parser
,
14518 int declares_class_or_enum
;
14519 tree decl
= NULL_TREE
;
14520 tree decl_specifiers
;
14522 bool function_definition_p
= false;
14524 /* This function is only used when processing a template
14526 if (innermost_scope_kind () != sk_template_parms
14527 && innermost_scope_kind () != sk_template_spec
)
14530 /* Defer access checks until we know what is being declared. */
14531 push_deferring_access_checks (dk_deferred
);
14533 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14536 = cp_parser_decl_specifier_seq (parser
,
14537 CP_PARSER_FLAGS_OPTIONAL
,
14539 &declares_class_or_enum
);
14541 *friend_p
= cp_parser_friend_p (decl_specifiers
);
14543 /* There are no template typedefs. */
14544 if (cp_parser_typedef_p (decl_specifiers
))
14546 error ("template declaration of `typedef'");
14547 decl
= error_mark_node
;
14550 /* Gather up the access checks that occurred the
14551 decl-specifier-seq. */
14552 stop_deferring_access_checks ();
14554 /* Check for the declaration of a template class. */
14555 if (declares_class_or_enum
)
14557 if (cp_parser_declares_only_class_p (parser
))
14559 decl
= shadow_tag (decl_specifiers
);
14561 decl
= TYPE_NAME (decl
);
14563 decl
= error_mark_node
;
14566 /* If it's not a template class, try for a template function. If
14567 the next token is a `;', then this declaration does not declare
14568 anything. But, if there were errors in the decl-specifiers, then
14569 the error might well have come from an attempted class-specifier.
14570 In that case, there's no need to warn about a missing declarator. */
14572 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
14573 || !value_member (error_mark_node
, decl_specifiers
)))
14574 decl
= cp_parser_init_declarator (parser
,
14577 /*function_definition_allowed_p=*/true,
14579 declares_class_or_enum
,
14580 &function_definition_p
);
14582 pop_deferring_access_checks ();
14584 /* Clear any current qualification; whatever comes next is the start
14585 of something new. */
14586 parser
->scope
= NULL_TREE
;
14587 parser
->qualifying_scope
= NULL_TREE
;
14588 parser
->object_scope
= NULL_TREE
;
14589 /* Look for a trailing `;' after the declaration. */
14590 if (!function_definition_p
14591 && (decl
== error_mark_node
14592 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
14593 cp_parser_skip_to_end_of_block_or_statement (parser
);
14598 /* Parse a cast-expression that is not the operand of a unary "&". */
14601 cp_parser_simple_cast_expression (cp_parser
*parser
)
14603 return cp_parser_cast_expression (parser
, /*address_p=*/false);
14606 /* Parse a functional cast to TYPE. Returns an expression
14607 representing the cast. */
14610 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14612 tree expression_list
;
14616 = cp_parser_parenthesized_expression_list (parser
, false,
14617 /*non_constant_p=*/NULL
);
14619 cast
= build_functional_cast (type
, expression_list
);
14620 /* [expr.const]/1: In an integral constant expression "only type
14621 conversions to integral or enumeration type can be used". */
14622 if (TREE_CODE (type
) == TYPE_DECL
)
14623 type
= TREE_TYPE (type
);
14624 if (cast
!= error_mark_node
&& !dependent_type_p (type
)
14625 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
14627 if (cp_parser_non_integral_constant_expression
14628 (parser
, "a call to a constructor"))
14629 return error_mark_node
;
14634 /* Save the tokens that make up the body of a member function defined
14635 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14636 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14637 specifiers applied to the declaration. Returns the FUNCTION_DECL
14638 for the member function. */
14641 cp_parser_save_member_function_body (cp_parser
* parser
,
14642 tree decl_specifiers
,
14646 cp_token_cache
*cache
;
14649 /* Create the function-declaration. */
14650 fn
= start_method (decl_specifiers
, declarator
, attributes
);
14651 /* If something went badly wrong, bail out now. */
14652 if (fn
== error_mark_node
)
14654 /* If there's a function-body, skip it. */
14655 if (cp_parser_token_starts_function_definition_p
14656 (cp_lexer_peek_token (parser
->lexer
)))
14657 cp_parser_skip_to_end_of_block_or_statement (parser
);
14658 return error_mark_node
;
14661 /* Remember it, if there default args to post process. */
14662 cp_parser_save_default_args (parser
, fn
);
14664 /* Create a token cache. */
14665 cache
= cp_token_cache_new ();
14666 /* Save away the tokens that make up the body of the
14668 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14669 /* Handle function try blocks. */
14670 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
14671 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14673 /* Save away the inline definition; we will process it when the
14674 class is complete. */
14675 DECL_PENDING_INLINE_INFO (fn
) = cache
;
14676 DECL_PENDING_INLINE_P (fn
) = 1;
14678 /* We need to know that this was defined in the class, so that
14679 friend templates are handled correctly. */
14680 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
14682 /* We're done with the inline definition. */
14683 finish_method (fn
);
14685 /* Add FN to the queue of functions to be parsed later. */
14686 TREE_VALUE (parser
->unparsed_functions_queues
)
14687 = tree_cons (NULL_TREE
, fn
,
14688 TREE_VALUE (parser
->unparsed_functions_queues
));
14693 /* Parse a template-argument-list, as well as the trailing ">" (but
14694 not the opening ">"). See cp_parser_template_argument_list for the
14698 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
14702 tree saved_qualifying_scope
;
14703 tree saved_object_scope
;
14704 bool saved_greater_than_is_operator_p
;
14708 When parsing a template-id, the first non-nested `>' is taken as
14709 the end of the template-argument-list rather than a greater-than
14711 saved_greater_than_is_operator_p
14712 = parser
->greater_than_is_operator_p
;
14713 parser
->greater_than_is_operator_p
= false;
14714 /* Parsing the argument list may modify SCOPE, so we save it
14716 saved_scope
= parser
->scope
;
14717 saved_qualifying_scope
= parser
->qualifying_scope
;
14718 saved_object_scope
= parser
->object_scope
;
14719 /* Parse the template-argument-list itself. */
14720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14721 arguments
= NULL_TREE
;
14723 arguments
= cp_parser_template_argument_list (parser
);
14724 /* Look for the `>' that ends the template-argument-list. If we find
14725 a '>>' instead, it's probably just a typo. */
14726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14728 if (!saved_greater_than_is_operator_p
)
14730 /* If we're in a nested template argument list, the '>>' has to be
14731 a typo for '> >'. We emit the error message, but we continue
14732 parsing and we push a '>' as next token, so that the argument
14733 list will be parsed correctly.. */
14735 error ("`>>' should be `> >' within a nested template argument list");
14736 token
= cp_lexer_peek_token (parser
->lexer
);
14737 token
->type
= CPP_GREATER
;
14741 /* If this is not a nested template argument list, the '>>' is
14742 a typo for '>'. Emit an error message and continue. */
14743 error ("spurious `>>', use `>' to terminate a template argument list");
14744 cp_lexer_consume_token (parser
->lexer
);
14748 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14749 /* The `>' token might be a greater-than operator again now. */
14750 parser
->greater_than_is_operator_p
14751 = saved_greater_than_is_operator_p
;
14752 /* Restore the SAVED_SCOPE. */
14753 parser
->scope
= saved_scope
;
14754 parser
->qualifying_scope
= saved_qualifying_scope
;
14755 parser
->object_scope
= saved_object_scope
;
14760 /* MEMBER_FUNCTION is a member function, or a friend. If default
14761 arguments, or the body of the function have not yet been parsed,
14765 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
14767 cp_lexer
*saved_lexer
;
14769 /* If this member is a template, get the underlying
14771 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
14772 member_function
= DECL_TEMPLATE_RESULT (member_function
);
14774 /* There should not be any class definitions in progress at this
14775 point; the bodies of members are only parsed outside of all class
14777 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
14778 /* While we're parsing the member functions we might encounter more
14779 classes. We want to handle them right away, but we don't want
14780 them getting mixed up with functions that are currently in the
14782 parser
->unparsed_functions_queues
14783 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14785 /* Make sure that any template parameters are in scope. */
14786 maybe_begin_member_template_processing (member_function
);
14788 /* If the body of the function has not yet been parsed, parse it
14790 if (DECL_PENDING_INLINE_P (member_function
))
14792 tree function_scope
;
14793 cp_token_cache
*tokens
;
14795 /* The function is no longer pending; we are processing it. */
14796 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
14797 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
14798 DECL_PENDING_INLINE_P (member_function
) = 0;
14800 /* If this is a local class, enter the scope of the containing
14802 function_scope
= current_function_decl
;
14803 if (function_scope
)
14804 push_function_context_to (function_scope
);
14806 /* Save away the current lexer. */
14807 saved_lexer
= parser
->lexer
;
14808 /* Make a new lexer to feed us the tokens saved for this function. */
14809 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14810 parser
->lexer
->next
= saved_lexer
;
14812 /* Set the current source position to be the location of the first
14813 token in the saved inline body. */
14814 cp_lexer_peek_token (parser
->lexer
);
14816 /* Let the front end know that we going to be defining this
14818 start_function (NULL_TREE
, member_function
, NULL_TREE
,
14819 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
14821 /* Now, parse the body of the function. */
14822 cp_parser_function_definition_after_declarator (parser
,
14823 /*inline_p=*/true);
14825 /* Leave the scope of the containing function. */
14826 if (function_scope
)
14827 pop_function_context_from (function_scope
);
14828 /* Restore the lexer. */
14829 parser
->lexer
= saved_lexer
;
14832 /* Remove any template parameters from the symbol table. */
14833 maybe_end_member_template_processing ();
14835 /* Restore the queue. */
14836 parser
->unparsed_functions_queues
14837 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14840 /* If DECL contains any default args, remember it on the unparsed
14841 functions queue. */
14844 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
14848 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14850 probe
= TREE_CHAIN (probe
))
14851 if (TREE_PURPOSE (probe
))
14853 TREE_PURPOSE (parser
->unparsed_functions_queues
)
14854 = tree_cons (NULL_TREE
, decl
,
14855 TREE_PURPOSE (parser
->unparsed_functions_queues
));
14861 /* FN is a FUNCTION_DECL which may contains a parameter with an
14862 unparsed DEFAULT_ARG. Parse the default args now. */
14865 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
14867 cp_lexer
*saved_lexer
;
14868 cp_token_cache
*tokens
;
14869 bool saved_local_variables_forbidden_p
;
14872 /* While we're parsing the default args, we might (due to the
14873 statement expression extension) encounter more classes. We want
14874 to handle them right away, but we don't want them getting mixed
14875 up with default args that are currently in the queue. */
14876 parser
->unparsed_functions_queues
14877 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14879 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14881 parameters
= TREE_CHAIN (parameters
))
14883 tree default_arg
= TREE_PURPOSE (parameters
);
14889 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
14890 /* This can happen for a friend declaration for a function
14891 already declared with default arguments. */
14894 /* Save away the current lexer. */
14895 saved_lexer
= parser
->lexer
;
14896 /* Create a new one, using the tokens we have saved. */
14897 tokens
= DEFARG_TOKENS (default_arg
);
14898 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14900 /* Set the current source position to be the location of the
14901 first token in the default argument. */
14902 cp_lexer_peek_token (parser
->lexer
);
14904 /* Local variable names (and the `this' keyword) may not appear
14905 in a default argument. */
14906 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14907 parser
->local_variables_forbidden_p
= true;
14909 /* Parse the assignment-expression. */
14910 if (DECL_FRIEND_CONTEXT (fn
))
14911 push_nested_class (DECL_FRIEND_CONTEXT (fn
));
14912 else if (DECL_CLASS_SCOPE_P (fn
))
14913 push_nested_class (DECL_CONTEXT (fn
));
14914 parsed_arg
= cp_parser_assignment_expression (parser
);
14915 if (DECL_FRIEND_CONTEXT (fn
) || DECL_CLASS_SCOPE_P (fn
))
14916 pop_nested_class ();
14918 TREE_PURPOSE (parameters
) = parsed_arg
;
14920 /* Update any instantiations we've already created. */
14921 for (default_arg
= TREE_CHAIN (default_arg
);
14923 default_arg
= TREE_CHAIN (default_arg
))
14924 TREE_PURPOSE (TREE_PURPOSE (default_arg
)) = parsed_arg
;
14926 /* If the token stream has not been completely used up, then
14927 there was extra junk after the end of the default
14929 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
14930 cp_parser_error (parser
, "expected `,'");
14932 /* Restore saved state. */
14933 parser
->lexer
= saved_lexer
;
14934 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14937 /* Restore the queue. */
14938 parser
->unparsed_functions_queues
14939 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14942 /* Parse the operand of `sizeof' (or a similar operator). Returns
14943 either a TYPE or an expression, depending on the form of the
14944 input. The KEYWORD indicates which kind of expression we have
14948 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
14950 static const char *format
;
14951 tree expr
= NULL_TREE
;
14952 const char *saved_message
;
14953 bool saved_integral_constant_expression_p
;
14955 /* Initialize FORMAT the first time we get here. */
14957 format
= "types may not be defined in `%s' expressions";
14959 /* Types cannot be defined in a `sizeof' expression. Save away the
14961 saved_message
= parser
->type_definition_forbidden_message
;
14962 /* And create the new one. */
14963 parser
->type_definition_forbidden_message
14964 = xmalloc (strlen (format
)
14965 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
14967 sprintf ((char *) parser
->type_definition_forbidden_message
,
14968 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
14970 /* The restrictions on constant-expressions do not apply inside
14971 sizeof expressions. */
14972 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
14973 parser
->integral_constant_expression_p
= false;
14975 /* Do not actually evaluate the expression. */
14977 /* If it's a `(', then we might be looking at the type-id
14979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14982 bool saved_in_type_id_in_expr_p
;
14984 /* We can't be sure yet whether we're looking at a type-id or an
14986 cp_parser_parse_tentatively (parser
);
14987 /* Consume the `('. */
14988 cp_lexer_consume_token (parser
->lexer
);
14989 /* Parse the type-id. */
14990 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14991 parser
->in_type_id_in_expr_p
= true;
14992 type
= cp_parser_type_id (parser
);
14993 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14994 /* Now, look for the trailing `)'. */
14995 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14996 /* If all went well, then we're done. */
14997 if (cp_parser_parse_definitely (parser
))
14999 /* Build a list of decl-specifiers; right now, we have only
15000 a single type-specifier. */
15001 type
= build_tree_list (NULL_TREE
,
15004 /* Call grokdeclarator to figure out what type this is. */
15005 expr
= grokdeclarator (NULL_TREE
,
15009 /*attrlist=*/NULL
);
15013 /* If the type-id production did not work out, then we must be
15014 looking at the unary-expression production. */
15016 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
15017 /* Go back to evaluating expressions. */
15020 /* Free the message we created. */
15021 free ((char *) parser
->type_definition_forbidden_message
);
15022 /* And restore the old one. */
15023 parser
->type_definition_forbidden_message
= saved_message
;
15024 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
15029 /* If the current declaration has no declarator, return true. */
15032 cp_parser_declares_only_class_p (cp_parser
*parser
)
15034 /* If the next token is a `;' or a `,' then there is no
15036 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15037 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15040 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15041 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15044 cp_parser_friend_p (tree decl_specifiers
)
15046 while (decl_specifiers
)
15048 /* See if this decl-specifier is `friend'. */
15049 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
15050 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
15053 /* Go on to the next decl-specifier. */
15054 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
15060 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15061 Returns TRUE iff `typedef' appears among the DECL_SPECIFIERS. */
15064 cp_parser_typedef_p (tree decl_specifiers
)
15066 while (decl_specifiers
)
15068 /* See if this decl-specifier is `typedef'. */
15069 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
15070 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_TYPEDEF
)
15073 /* Go on to the next decl-specifier. */
15074 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
15081 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15082 issue an error message indicating that TOKEN_DESC was expected.
15084 Returns the token consumed, if the token had the appropriate type.
15085 Otherwise, returns NULL. */
15088 cp_parser_require (cp_parser
* parser
,
15089 enum cpp_ttype type
,
15090 const char* token_desc
)
15092 if (cp_lexer_next_token_is (parser
->lexer
, type
))
15093 return cp_lexer_consume_token (parser
->lexer
);
15096 /* Output the MESSAGE -- unless we're parsing tentatively. */
15097 if (!cp_parser_simulate_error (parser
))
15099 char *message
= concat ("expected ", token_desc
, NULL
);
15100 cp_parser_error (parser
, message
);
15107 /* Like cp_parser_require, except that tokens will be skipped until
15108 the desired token is found. An error message is still produced if
15109 the next token is not as expected. */
15112 cp_parser_skip_until_found (cp_parser
* parser
,
15113 enum cpp_ttype type
,
15114 const char* token_desc
)
15117 unsigned nesting_depth
= 0;
15119 if (cp_parser_require (parser
, type
, token_desc
))
15122 /* Skip tokens until the desired token is found. */
15125 /* Peek at the next token. */
15126 token
= cp_lexer_peek_token (parser
->lexer
);
15127 /* If we've reached the token we want, consume it and
15129 if (token
->type
== type
&& !nesting_depth
)
15131 cp_lexer_consume_token (parser
->lexer
);
15134 /* If we've run out of tokens, stop. */
15135 if (token
->type
== CPP_EOF
)
15137 if (token
->type
== CPP_OPEN_BRACE
15138 || token
->type
== CPP_OPEN_PAREN
15139 || token
->type
== CPP_OPEN_SQUARE
)
15141 else if (token
->type
== CPP_CLOSE_BRACE
15142 || token
->type
== CPP_CLOSE_PAREN
15143 || token
->type
== CPP_CLOSE_SQUARE
)
15145 if (nesting_depth
-- == 0)
15148 /* Consume this token. */
15149 cp_lexer_consume_token (parser
->lexer
);
15153 /* If the next token is the indicated keyword, consume it. Otherwise,
15154 issue an error message indicating that TOKEN_DESC was expected.
15156 Returns the token consumed, if the token had the appropriate type.
15157 Otherwise, returns NULL. */
15160 cp_parser_require_keyword (cp_parser
* parser
,
15162 const char* token_desc
)
15164 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
15166 if (token
&& token
->keyword
!= keyword
)
15168 dyn_string_t error_msg
;
15170 /* Format the error message. */
15171 error_msg
= dyn_string_new (0);
15172 dyn_string_append_cstr (error_msg
, "expected ");
15173 dyn_string_append_cstr (error_msg
, token_desc
);
15174 cp_parser_error (parser
, error_msg
->s
);
15175 dyn_string_delete (error_msg
);
15182 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15183 function-definition. */
15186 cp_parser_token_starts_function_definition_p (cp_token
* token
)
15188 return (/* An ordinary function-body begins with an `{'. */
15189 token
->type
== CPP_OPEN_BRACE
15190 /* A ctor-initializer begins with a `:'. */
15191 || token
->type
== CPP_COLON
15192 /* A function-try-block begins with `try'. */
15193 || token
->keyword
== RID_TRY
15194 /* The named return value extension begins with `return'. */
15195 || token
->keyword
== RID_RETURN
);
15198 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15202 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
15206 token
= cp_lexer_peek_token (parser
->lexer
);
15207 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
15210 /* Returns TRUE iff the next token is the "," or ">" ending a
15211 template-argument. */
15214 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
15218 token
= cp_lexer_peek_token (parser
->lexer
);
15219 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
15222 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15223 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15226 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
15231 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
15232 if (token
->type
== CPP_LESS
)
15234 /* Check for the sequence `<::' in the original code. It would be lexed as
15235 `[:', where `[' is a digraph, and there is no whitespace before
15237 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
15240 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
15241 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
15247 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15248 or none_type otherwise. */
15250 static enum tag_types
15251 cp_parser_token_is_class_key (cp_token
* token
)
15253 switch (token
->keyword
)
15258 return record_type
;
15267 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15270 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
15272 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
15273 pedwarn ("`%s' tag used in naming `%#T'",
15274 class_key
== union_type
? "union"
15275 : class_key
== record_type
? "struct" : "class",
15279 /* Issue an error message if DECL is redeclared with different
15280 access than its original declaration [class.access.spec/3].
15281 This applies to nested classes and nested class templates.
15284 static void cp_parser_check_access_in_redeclaration (tree decl
)
15286 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15289 if ((TREE_PRIVATE (decl
)
15290 != (current_access_specifier
== access_private_node
))
15291 || (TREE_PROTECTED (decl
)
15292 != (current_access_specifier
== access_protected_node
)))
15293 error ("%D redeclared with different access", decl
);
15296 /* Look for the `template' keyword, as a syntactic disambiguator.
15297 Return TRUE iff it is present, in which case it will be
15301 cp_parser_optional_template_keyword (cp_parser
*parser
)
15303 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15305 /* The `template' keyword can only be used within templates;
15306 outside templates the parser can always figure out what is a
15307 template and what is not. */
15308 if (!processing_template_decl
)
15310 error ("`template' (as a disambiguator) is only allowed "
15311 "within templates");
15312 /* If this part of the token stream is rescanned, the same
15313 error message would be generated. So, we purge the token
15314 from the stream. */
15315 cp_lexer_purge_token (parser
->lexer
);
15320 /* Consume the `template' keyword. */
15321 cp_lexer_consume_token (parser
->lexer
);
15329 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15330 set PARSER->SCOPE, and perform other related actions. */
15333 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
15338 /* Get the stored value. */
15339 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
15340 /* Perform any access checks that were deferred. */
15341 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
15342 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
15343 /* Set the scope from the stored value. */
15344 parser
->scope
= TREE_VALUE (value
);
15345 parser
->qualifying_scope
= TREE_TYPE (value
);
15346 parser
->object_scope
= NULL_TREE
;
15349 /* Add tokens to CACHE until an non-nested END token appears. */
15352 cp_parser_cache_group (cp_parser
*parser
,
15353 cp_token_cache
*cache
,
15354 enum cpp_ttype end
,
15361 /* Abort a parenthesized expression if we encounter a brace. */
15362 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
15363 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15365 /* If we've reached the end of the file, stop. */
15366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15368 /* Consume the next token. */
15369 token
= cp_lexer_consume_token (parser
->lexer
);
15370 /* Add this token to the tokens we are saving. */
15371 cp_token_cache_push_token (cache
, token
);
15372 /* See if it starts a new group. */
15373 if (token
->type
== CPP_OPEN_BRACE
)
15375 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
15379 else if (token
->type
== CPP_OPEN_PAREN
)
15380 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
15381 else if (token
->type
== end
)
15386 /* Begin parsing tentatively. We always save tokens while parsing
15387 tentatively so that if the tentative parsing fails we can restore the
15391 cp_parser_parse_tentatively (cp_parser
* parser
)
15393 /* Enter a new parsing context. */
15394 parser
->context
= cp_parser_context_new (parser
->context
);
15395 /* Begin saving tokens. */
15396 cp_lexer_save_tokens (parser
->lexer
);
15397 /* In order to avoid repetitive access control error messages,
15398 access checks are queued up until we are no longer parsing
15400 push_deferring_access_checks (dk_deferred
);
15403 /* Commit to the currently active tentative parse. */
15406 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
15408 cp_parser_context
*context
;
15411 /* Mark all of the levels as committed. */
15412 lexer
= parser
->lexer
;
15413 for (context
= parser
->context
; context
->next
; context
= context
->next
)
15415 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
15417 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
15418 while (!cp_lexer_saving_tokens (lexer
))
15419 lexer
= lexer
->next
;
15420 cp_lexer_commit_tokens (lexer
);
15424 /* Abort the currently active tentative parse. All consumed tokens
15425 will be rolled back, and no diagnostics will be issued. */
15428 cp_parser_abort_tentative_parse (cp_parser
* parser
)
15430 cp_parser_simulate_error (parser
);
15431 /* Now, pretend that we want to see if the construct was
15432 successfully parsed. */
15433 cp_parser_parse_definitely (parser
);
15436 /* Stop parsing tentatively. If a parse error has occurred, restore the
15437 token stream. Otherwise, commit to the tokens we have consumed.
15438 Returns true if no error occurred; false otherwise. */
15441 cp_parser_parse_definitely (cp_parser
* parser
)
15443 bool error_occurred
;
15444 cp_parser_context
*context
;
15446 /* Remember whether or not an error occurred, since we are about to
15447 destroy that information. */
15448 error_occurred
= cp_parser_error_occurred (parser
);
15449 /* Remove the topmost context from the stack. */
15450 context
= parser
->context
;
15451 parser
->context
= context
->next
;
15452 /* If no parse errors occurred, commit to the tentative parse. */
15453 if (!error_occurred
)
15455 /* Commit to the tokens read tentatively, unless that was
15457 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
15458 cp_lexer_commit_tokens (parser
->lexer
);
15460 pop_to_parent_deferring_access_checks ();
15462 /* Otherwise, if errors occurred, roll back our state so that things
15463 are just as they were before we began the tentative parse. */
15466 cp_lexer_rollback_tokens (parser
->lexer
);
15467 pop_deferring_access_checks ();
15469 /* Add the context to the front of the free list. */
15470 context
->next
= cp_parser_context_free_list
;
15471 cp_parser_context_free_list
= context
;
15473 return !error_occurred
;
15476 /* Returns true if we are parsing tentatively -- but have decided that
15477 we will stick with this tentative parse, even if errors occur. */
15480 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
15482 return (cp_parser_parsing_tentatively (parser
)
15483 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
15486 /* Returns nonzero iff an error has occurred during the most recent
15487 tentative parse. */
15490 cp_parser_error_occurred (cp_parser
* parser
)
15492 return (cp_parser_parsing_tentatively (parser
)
15493 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
15496 /* Returns nonzero if GNU extensions are allowed. */
15499 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
15501 return parser
->allow_gnu_extensions_p
;
15508 static GTY (()) cp_parser
*the_parser
;
15510 /* External interface. */
15512 /* Parse one entire translation unit. */
15515 c_parse_file (void)
15517 bool error_occurred
;
15519 the_parser
= cp_parser_new ();
15520 push_deferring_access_checks (flag_access_control
15521 ? dk_no_deferred
: dk_no_check
);
15522 error_occurred
= cp_parser_translation_unit (the_parser
);
15526 /* This variable must be provided by every front end. */
15530 #include "gt-cp-parser.h"