2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token
GTY (())
70 /* The kind of token. */
71 enum cpp_ttype type
: 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
75 /* The value associated with this token, if any. */
77 /* The location at which this token was found. */
81 /* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
86 /* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
92 This somewhat ungainly data structure (as opposed to, say, a
93 variable-length array), is used due to constraints imposed by the
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
97 typedef struct cp_token_block
GTY (())
100 cp_token tokens
[CP_TOKEN_BLOCK_NUM_TOKENS
];
101 /* The number of tokens in this block. */
103 /* The next token block in the chain. */
104 struct cp_token_block
*next
;
105 /* The previous block in the chain. */
106 struct cp_token_block
*prev
;
109 typedef struct cp_token_cache
GTY (())
111 /* The first block in the cache. NULL if there are no tokens in the
113 cp_token_block
*first
;
114 /* The last block in the cache. NULL If there are no tokens in the
116 cp_token_block
*last
;
121 static cp_token_cache
*cp_token_cache_new
123 static void cp_token_cache_push_token
124 (cp_token_cache
*, cp_token
*);
126 /* Create a new cp_token_cache. */
128 static cp_token_cache
*
129 cp_token_cache_new ()
131 return ggc_alloc_cleared (sizeof (cp_token_cache
));
134 /* Add *TOKEN to *CACHE. */
137 cp_token_cache_push_token (cp_token_cache
*cache
,
140 cp_token_block
*b
= cache
->last
;
142 /* See if we need to allocate a new token block. */
143 if (!b
|| b
->num_tokens
== CP_TOKEN_BLOCK_NUM_TOKENS
)
145 b
= ggc_alloc_cleared (sizeof (cp_token_block
));
146 b
->prev
= cache
->last
;
149 cache
->last
->next
= b
;
153 cache
->first
= cache
->last
= b
;
155 /* Add this token to the current token block. */
156 b
->tokens
[b
->num_tokens
++] = *token
;
159 /* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
163 typedef struct cp_lexer
GTY (())
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token
* GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer
;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token
* GTY ((skip (""))) buffer_end
;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token
* GTY ((skip (""))) first_token
;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token
* GTY ((skip (""))) next_token
;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token
* GTY ((skip (""))) last_token
;
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens
;
197 /* The STRING_CST tokens encountered while processing the current
199 varray_type string_tokens
;
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
205 /* True if we should output debugging information. */
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer
*next
;
214 static cp_lexer
*cp_lexer_new_main
216 static cp_lexer
*cp_lexer_new_from_tokens
217 (struct cp_token_cache
*);
218 static int cp_lexer_saving_tokens
220 static cp_token
*cp_lexer_next_token
221 (cp_lexer
*, cp_token
*);
222 static ptrdiff_t cp_lexer_token_difference
223 (cp_lexer
*, cp_token
*, cp_token
*);
224 static cp_token
*cp_lexer_read_token
226 static void cp_lexer_maybe_grow_buffer
228 static void cp_lexer_get_preprocessor_token
229 (cp_lexer
*, cp_token
*);
230 static cp_token
*cp_lexer_peek_token
232 static cp_token
*cp_lexer_peek_nth_token
233 (cp_lexer
*, size_t);
234 static inline bool cp_lexer_next_token_is
235 (cp_lexer
*, enum cpp_ttype
);
236 static bool cp_lexer_next_token_is_not
237 (cp_lexer
*, enum cpp_ttype
);
238 static bool cp_lexer_next_token_is_keyword
239 (cp_lexer
*, enum rid
);
240 static cp_token
*cp_lexer_consume_token
242 static void cp_lexer_purge_token
244 static void cp_lexer_purge_tokens_after
245 (cp_lexer
*, cp_token
*);
246 static void cp_lexer_save_tokens
248 static void cp_lexer_commit_tokens
250 static void cp_lexer_rollback_tokens
252 static inline void cp_lexer_set_source_position_from_token
253 (cp_lexer
*, const cp_token
*);
254 static void cp_lexer_print_token
255 (FILE *, cp_token
*);
256 static inline bool cp_lexer_debugging_p
258 static void cp_lexer_start_debugging
259 (cp_lexer
*) ATTRIBUTE_UNUSED
;
260 static void cp_lexer_stop_debugging
261 (cp_lexer
*) ATTRIBUTE_UNUSED
;
263 /* Manifest constants. */
265 #define CP_TOKEN_BUFFER_SIZE 5
266 #define CP_SAVED_TOKENS_SIZE 5
268 /* A token type for keywords, as opposed to ordinary identifiers. */
269 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
271 /* A token type for template-ids. If a template-id is processed while
272 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
273 the value of the CPP_TEMPLATE_ID is whatever was returned by
274 cp_parser_template_id. */
275 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
277 /* A token type for nested-name-specifiers. If a
278 nested-name-specifier is processed while parsing tentatively, it is
279 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
280 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
281 cp_parser_nested_name_specifier_opt. */
282 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
284 /* A token type for tokens that are not tokens at all; these are used
285 to mark the end of a token block. */
286 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
290 /* The stream to which debugging output should be written. */
291 static FILE *cp_lexer_debug_stream
;
293 /* Create a new main C++ lexer, the lexer that gets tokens from the
297 cp_lexer_new_main (void)
300 cp_token first_token
;
302 /* It's possible that lexing the first token will load a PCH file,
303 which is a GC collection point. So we have to grab the first
304 token before allocating any memory. */
305 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
306 c_common_no_more_pch ();
308 /* Allocate the memory. */
309 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
311 /* Create the circular buffer. */
312 lexer
->buffer
= ggc_calloc (CP_TOKEN_BUFFER_SIZE
, sizeof (cp_token
));
313 lexer
->buffer_end
= lexer
->buffer
+ CP_TOKEN_BUFFER_SIZE
;
315 /* There is one token in the buffer. */
316 lexer
->last_token
= lexer
->buffer
+ 1;
317 lexer
->first_token
= lexer
->buffer
;
318 lexer
->next_token
= lexer
->buffer
;
319 memcpy (lexer
->buffer
, &first_token
, sizeof (cp_token
));
321 /* This lexer obtains more tokens by calling c_lex. */
322 lexer
->main_lexer_p
= true;
324 /* Create the SAVED_TOKENS stack. */
325 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
327 /* Create the STRINGS array. */
328 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
330 /* Assume we are not debugging. */
331 lexer
->debugging_p
= false;
336 /* Create a new lexer whose token stream is primed with the TOKENS.
337 When these tokens are exhausted, no new tokens will be read. */
340 cp_lexer_new_from_tokens (cp_token_cache
*tokens
)
344 cp_token_block
*block
;
345 ptrdiff_t num_tokens
;
347 /* Allocate the memory. */
348 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
350 /* Create a new buffer, appropriately sized. */
352 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
353 num_tokens
+= block
->num_tokens
;
354 lexer
->buffer
= ggc_alloc (num_tokens
* sizeof (cp_token
));
355 lexer
->buffer_end
= lexer
->buffer
+ num_tokens
;
357 /* Install the tokens. */
358 token
= lexer
->buffer
;
359 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
361 memcpy (token
, block
->tokens
, block
->num_tokens
* sizeof (cp_token
));
362 token
+= block
->num_tokens
;
365 /* The FIRST_TOKEN is the beginning of the buffer. */
366 lexer
->first_token
= lexer
->buffer
;
367 /* The next available token is also at the beginning of the buffer. */
368 lexer
->next_token
= lexer
->buffer
;
369 /* The buffer is full. */
370 lexer
->last_token
= lexer
->first_token
;
372 /* This lexer doesn't obtain more tokens. */
373 lexer
->main_lexer_p
= false;
375 /* Create the SAVED_TOKENS stack. */
376 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
378 /* Create the STRINGS array. */
379 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
381 /* Assume we are not debugging. */
382 lexer
->debugging_p
= false;
387 /* Returns nonzero if debugging information should be output. */
390 cp_lexer_debugging_p (cp_lexer
*lexer
)
392 return lexer
->debugging_p
;
395 /* Set the current source position from the information stored in
399 cp_lexer_set_source_position_from_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
400 const cp_token
*token
)
402 /* Ideally, the source position information would not be a global
403 variable, but it is. */
405 /* Update the line number. */
406 if (token
->type
!= CPP_EOF
)
407 input_location
= token
->location
;
410 /* TOKEN points into the circular token buffer. Return a pointer to
411 the next token in the buffer. */
413 static inline cp_token
*
414 cp_lexer_next_token (cp_lexer
* lexer
, cp_token
* token
)
417 if (token
== lexer
->buffer_end
)
418 token
= lexer
->buffer
;
422 /* nonzero if we are presently saving tokens. */
425 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
427 return VARRAY_ACTIVE_SIZE (lexer
->saved_tokens
) != 0;
430 /* Return a pointer to the token that is N tokens beyond TOKEN in the
434 cp_lexer_advance_token (cp_lexer
*lexer
, cp_token
*token
, ptrdiff_t n
)
437 if (token
>= lexer
->buffer_end
)
438 token
= lexer
->buffer
+ (token
- lexer
->buffer_end
);
442 /* Returns the number of times that START would have to be incremented
443 to reach FINISH. If START and FINISH are the same, returns zero. */
446 cp_lexer_token_difference (cp_lexer
* lexer
, cp_token
* start
, cp_token
* finish
)
449 return finish
- start
;
451 return ((lexer
->buffer_end
- lexer
->buffer
)
455 /* Obtain another token from the C preprocessor and add it to the
456 token buffer. Returns the newly read token. */
459 cp_lexer_read_token (cp_lexer
* lexer
)
463 /* Make sure there is room in the buffer. */
464 cp_lexer_maybe_grow_buffer (lexer
);
466 /* If there weren't any tokens, then this one will be the first. */
467 if (!lexer
->first_token
)
468 lexer
->first_token
= lexer
->last_token
;
469 /* Similarly, if there were no available tokens, there is one now. */
470 if (!lexer
->next_token
)
471 lexer
->next_token
= lexer
->last_token
;
473 /* Figure out where we're going to store the new token. */
474 token
= lexer
->last_token
;
476 /* Get a new token from the preprocessor. */
477 cp_lexer_get_preprocessor_token (lexer
, token
);
479 /* Increment LAST_TOKEN. */
480 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
482 /* Strings should have type `const char []'. Right now, we will
483 have an ARRAY_TYPE that is constant rather than an array of
485 FIXME: Make fix_string_type get this right in the first place. */
486 if ((token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
)
487 && flag_const_strings
)
491 /* Get the current type. It will be an ARRAY_TYPE. */
492 type
= TREE_TYPE (token
->value
);
493 /* Use build_cplus_array_type to rebuild the array, thereby
494 getting the right type. */
495 type
= build_cplus_array_type (TREE_TYPE (type
), TYPE_DOMAIN (type
));
496 /* Reset the type of the token. */
497 TREE_TYPE (token
->value
) = type
;
503 /* If the circular buffer is full, make it bigger. */
506 cp_lexer_maybe_grow_buffer (cp_lexer
* lexer
)
508 /* If the buffer is full, enlarge it. */
509 if (lexer
->last_token
== lexer
->first_token
)
511 cp_token
*new_buffer
;
512 cp_token
*old_buffer
;
513 cp_token
*new_first_token
;
514 ptrdiff_t buffer_length
;
515 size_t num_tokens_to_copy
;
517 /* Remember the current buffer pointer. It will become invalid,
518 but we will need to do pointer arithmetic involving this
520 old_buffer
= lexer
->buffer
;
521 /* Compute the current buffer size. */
522 buffer_length
= lexer
->buffer_end
- lexer
->buffer
;
523 /* Allocate a buffer twice as big. */
524 new_buffer
= ggc_realloc (lexer
->buffer
,
525 2 * buffer_length
* sizeof (cp_token
));
527 /* Because the buffer is circular, logically consecutive tokens
528 are not necessarily placed consecutively in memory.
529 Therefore, we must keep move the tokens that were before
530 FIRST_TOKEN to the second half of the newly allocated
532 num_tokens_to_copy
= (lexer
->first_token
- old_buffer
);
533 memcpy (new_buffer
+ buffer_length
,
535 num_tokens_to_copy
* sizeof (cp_token
));
536 /* Clear the rest of the buffer. We never look at this storage,
537 but the garbage collector may. */
538 memset (new_buffer
+ buffer_length
+ num_tokens_to_copy
, 0,
539 (buffer_length
- num_tokens_to_copy
) * sizeof (cp_token
));
541 /* Now recompute all of the buffer pointers. */
543 = new_buffer
+ (lexer
->first_token
- old_buffer
);
544 if (lexer
->next_token
!= NULL
)
546 ptrdiff_t next_token_delta
;
548 if (lexer
->next_token
> lexer
->first_token
)
549 next_token_delta
= lexer
->next_token
- lexer
->first_token
;
552 buffer_length
- (lexer
->first_token
- lexer
->next_token
);
553 lexer
->next_token
= new_first_token
+ next_token_delta
;
555 lexer
->last_token
= new_first_token
+ buffer_length
;
556 lexer
->buffer
= new_buffer
;
557 lexer
->buffer_end
= new_buffer
+ buffer_length
* 2;
558 lexer
->first_token
= new_first_token
;
562 /* Store the next token from the preprocessor in *TOKEN. */
565 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
570 /* If this not the main lexer, return a terminating CPP_EOF token. */
571 if (lexer
!= NULL
&& !lexer
->main_lexer_p
)
573 token
->type
= CPP_EOF
;
574 token
->location
.line
= 0;
575 token
->location
.file
= NULL
;
576 token
->value
= NULL_TREE
;
577 token
->keyword
= RID_MAX
;
583 /* Keep going until we get a token we like. */
586 /* Get a new token from the preprocessor. */
587 token
->type
= c_lex (&token
->value
);
588 /* Issue messages about tokens we cannot process. */
594 error ("invalid token");
598 /* This is a good token, so we exit the loop. */
603 /* Now we've got our token. */
604 token
->location
= input_location
;
606 /* Check to see if this token is a keyword. */
607 if (token
->type
== CPP_NAME
608 && C_IS_RESERVED_WORD (token
->value
))
610 /* Mark this token as a keyword. */
611 token
->type
= CPP_KEYWORD
;
612 /* Record which keyword. */
613 token
->keyword
= C_RID_CODE (token
->value
);
614 /* Update the value. Some keywords are mapped to particular
615 entities, rather than simply having the value of the
616 corresponding IDENTIFIER_NODE. For example, `__const' is
617 mapped to `const'. */
618 token
->value
= ridpointers
[token
->keyword
];
621 token
->keyword
= RID_MAX
;
624 /* Return a pointer to the next token in the token stream, but do not
628 cp_lexer_peek_token (cp_lexer
* lexer
)
632 /* If there are no tokens, read one now. */
633 if (!lexer
->next_token
)
634 cp_lexer_read_token (lexer
);
636 /* Provide debugging output. */
637 if (cp_lexer_debugging_p (lexer
))
639 fprintf (cp_lexer_debug_stream
, "cp_lexer: peeking at token: ");
640 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
641 fprintf (cp_lexer_debug_stream
, "\n");
644 token
= lexer
->next_token
;
645 cp_lexer_set_source_position_from_token (lexer
, token
);
649 /* Return true if the next token has the indicated TYPE. */
652 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
656 /* Peek at the next token. */
657 token
= cp_lexer_peek_token (lexer
);
658 /* Check to see if it has the indicated TYPE. */
659 return token
->type
== type
;
662 /* Return true if the next token does not have the indicated TYPE. */
665 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
667 return !cp_lexer_next_token_is (lexer
, type
);
670 /* Return true if the next token is the indicated KEYWORD. */
673 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
677 /* Peek at the next token. */
678 token
= cp_lexer_peek_token (lexer
);
679 /* Check to see if it is the indicated keyword. */
680 return token
->keyword
== keyword
;
683 /* Return a pointer to the Nth token in the token stream. If N is 1,
684 then this is precisely equivalent to cp_lexer_peek_token. */
687 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
691 /* N is 1-based, not zero-based. */
692 my_friendly_assert (n
> 0, 20000224);
694 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
695 token
= lexer
->next_token
;
696 /* If there are no tokens in the buffer, get one now. */
699 cp_lexer_read_token (lexer
);
700 token
= lexer
->next_token
;
703 /* Now, read tokens until we have enough. */
706 /* Advance to the next token. */
707 token
= cp_lexer_next_token (lexer
, token
);
708 /* If that's all the tokens we have, read a new one. */
709 if (token
== lexer
->last_token
)
710 token
= cp_lexer_read_token (lexer
);
716 /* Consume the next token. The pointer returned is valid only until
717 another token is read. Callers should preserve copy the token
718 explicitly if they will need its value for a longer period of
722 cp_lexer_consume_token (cp_lexer
* lexer
)
726 /* If there are no tokens, read one now. */
727 if (!lexer
->next_token
)
728 cp_lexer_read_token (lexer
);
730 /* Remember the token we'll be returning. */
731 token
= lexer
->next_token
;
733 /* Increment NEXT_TOKEN. */
734 lexer
->next_token
= cp_lexer_next_token (lexer
,
736 /* Check to see if we're all out of tokens. */
737 if (lexer
->next_token
== lexer
->last_token
)
738 lexer
->next_token
= NULL
;
740 /* If we're not saving tokens, then move FIRST_TOKEN too. */
741 if (!cp_lexer_saving_tokens (lexer
))
743 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
744 if (!lexer
->next_token
)
745 lexer
->first_token
= NULL
;
747 lexer
->first_token
= lexer
->next_token
;
750 /* Provide debugging output. */
751 if (cp_lexer_debugging_p (lexer
))
753 fprintf (cp_lexer_debug_stream
, "cp_lexer: consuming token: ");
754 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
755 fprintf (cp_lexer_debug_stream
, "\n");
761 /* Permanently remove the next token from the token stream. There
762 must be a valid next token already; this token never reads
763 additional tokens from the preprocessor. */
766 cp_lexer_purge_token (cp_lexer
*lexer
)
769 cp_token
*next_token
;
771 token
= lexer
->next_token
;
774 next_token
= cp_lexer_next_token (lexer
, token
);
775 if (next_token
== lexer
->last_token
)
777 *token
= *next_token
;
781 lexer
->last_token
= token
;
782 /* The token purged may have been the only token remaining; if so,
784 if (lexer
->next_token
== token
)
785 lexer
->next_token
= NULL
;
788 /* Permanently remove all tokens after TOKEN, up to, but not
789 including, the token that will be returned next by
790 cp_lexer_peek_token. */
793 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*token
)
799 if (lexer
->next_token
)
801 /* Copy the tokens that have not yet been read to the location
802 immediately following TOKEN. */
803 t1
= cp_lexer_next_token (lexer
, token
);
804 t2
= peek
= cp_lexer_peek_token (lexer
);
805 /* Move tokens into the vacant area between TOKEN and PEEK. */
806 while (t2
!= lexer
->last_token
)
809 t1
= cp_lexer_next_token (lexer
, t1
);
810 t2
= cp_lexer_next_token (lexer
, t2
);
812 /* Now, the next available token is right after TOKEN. */
813 lexer
->next_token
= cp_lexer_next_token (lexer
, token
);
814 /* And the last token is wherever we ended up. */
815 lexer
->last_token
= t1
;
819 /* There are no tokens in the buffer, so there is nothing to
820 copy. The last token in the buffer is TOKEN itself. */
821 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
825 /* Begin saving tokens. All tokens consumed after this point will be
829 cp_lexer_save_tokens (cp_lexer
* lexer
)
831 /* Provide debugging output. */
832 if (cp_lexer_debugging_p (lexer
))
833 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
835 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
836 restore the tokens if required. */
837 if (!lexer
->next_token
)
838 cp_lexer_read_token (lexer
);
840 VARRAY_PUSH_INT (lexer
->saved_tokens
,
841 cp_lexer_token_difference (lexer
,
846 /* Commit to the portion of the token stream most recently saved. */
849 cp_lexer_commit_tokens (cp_lexer
* lexer
)
851 /* Provide debugging output. */
852 if (cp_lexer_debugging_p (lexer
))
853 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
855 VARRAY_POP (lexer
->saved_tokens
);
858 /* Return all tokens saved since the last call to cp_lexer_save_tokens
859 to the token stream. Stop saving tokens. */
862 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer
))
868 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
870 /* Find the token that was the NEXT_TOKEN when we started saving
872 delta
= VARRAY_TOP_INT(lexer
->saved_tokens
);
873 /* Make it the next token again now. */
874 lexer
->next_token
= cp_lexer_advance_token (lexer
,
877 /* It might be the case that there were no tokens when we started
878 saving tokens, but that there are some tokens now. */
879 if (!lexer
->next_token
&& lexer
->first_token
)
880 lexer
->next_token
= lexer
->first_token
;
882 /* Stop saving tokens. */
883 VARRAY_POP (lexer
->saved_tokens
);
886 /* Print a representation of the TOKEN on the STREAM. */
889 cp_lexer_print_token (FILE * stream
, cp_token
* token
)
891 const char *token_type
= NULL
;
893 /* Figure out what kind of token this is. */
901 token_type
= "COMMA";
905 token_type
= "OPEN_PAREN";
908 case CPP_CLOSE_PAREN
:
909 token_type
= "CLOSE_PAREN";
913 token_type
= "OPEN_BRACE";
916 case CPP_CLOSE_BRACE
:
917 token_type
= "CLOSE_BRACE";
921 token_type
= "SEMICOLON";
933 token_type
= "keyword";
936 /* This is not a token that we know how to handle yet. */
941 /* If we have a name for the token, print it out. Otherwise, we
942 simply give the numeric code. */
944 fprintf (stream
, "%s", token_type
);
946 fprintf (stream
, "%d", token
->type
);
947 /* And, for an identifier, print the identifier name. */
948 if (token
->type
== CPP_NAME
949 /* Some keywords have a value that is not an IDENTIFIER_NODE.
950 For example, `struct' is mapped to an INTEGER_CST. */
951 || (token
->type
== CPP_KEYWORD
952 && TREE_CODE (token
->value
) == IDENTIFIER_NODE
))
953 fprintf (stream
, " %s", IDENTIFIER_POINTER (token
->value
));
956 /* Start emitting debugging information. */
959 cp_lexer_start_debugging (cp_lexer
* lexer
)
961 ++lexer
->debugging_p
;
964 /* Stop emitting debugging information. */
967 cp_lexer_stop_debugging (cp_lexer
* lexer
)
969 --lexer
->debugging_p
;
978 A cp_parser parses the token stream as specified by the C++
979 grammar. Its job is purely parsing, not semantic analysis. For
980 example, the parser breaks the token stream into declarators,
981 expressions, statements, and other similar syntactic constructs.
982 It does not check that the types of the expressions on either side
983 of an assignment-statement are compatible, or that a function is
984 not declared with a parameter of type `void'.
986 The parser invokes routines elsewhere in the compiler to perform
987 semantic analysis and to build up the abstract syntax tree for the
990 The parser (and the template instantiation code, which is, in a
991 way, a close relative of parsing) are the only parts of the
992 compiler that should be calling push_scope and pop_scope, or
993 related functions. The parser (and template instantiation code)
994 keeps track of what scope is presently active; everything else
995 should simply honor that. (The code that generates static
996 initializers may also need to set the scope, in order to check
997 access control correctly when emitting the initializers.)
1002 The parser is of the standard recursive-descent variety. Upcoming
1003 tokens in the token stream are examined in order to determine which
1004 production to use when parsing a non-terminal. Some C++ constructs
1005 require arbitrary look ahead to disambiguate. For example, it is
1006 impossible, in the general case, to tell whether a statement is an
1007 expression or declaration without scanning the entire statement.
1008 Therefore, the parser is capable of "parsing tentatively." When the
1009 parser is not sure what construct comes next, it enters this mode.
1010 Then, while we attempt to parse the construct, the parser queues up
1011 error messages, rather than issuing them immediately, and saves the
1012 tokens it consumes. If the construct is parsed successfully, the
1013 parser "commits", i.e., it issues any queued error messages and
1014 the tokens that were being preserved are permanently discarded.
1015 If, however, the construct is not parsed successfully, the parser
1016 rolls back its state completely so that it can resume parsing using
1017 a different alternative.
1022 The performance of the parser could probably be improved
1023 substantially. Some possible improvements include:
1025 - The expression parser recurses through the various levels of
1026 precedence as specified in the grammar, rather than using an
1027 operator-precedence technique. Therefore, parsing a simple
1028 identifier requires multiple recursive calls.
1030 - We could often eliminate the need to parse tentatively by
1031 looking ahead a little bit. In some places, this approach
1032 might not entirely eliminate the need to parse tentatively, but
1033 it might still speed up the average case. */
1035 /* Flags that are passed to some parsing functions. These values can
1036 be bitwise-ored together. */
1038 typedef enum cp_parser_flags
1041 CP_PARSER_FLAGS_NONE
= 0x0,
1042 /* The construct is optional. If it is not present, then no error
1043 should be issued. */
1044 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1045 /* When parsing a type-specifier, do not allow user-defined types. */
1046 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1049 /* The different kinds of declarators we want to parse. */
1051 typedef enum cp_parser_declarator_kind
1053 /* We want an abstract declartor. */
1054 CP_PARSER_DECLARATOR_ABSTRACT
,
1055 /* We want a named declarator. */
1056 CP_PARSER_DECLARATOR_NAMED
,
1057 /* We don't mind, but the name must be an unqualified-id */
1058 CP_PARSER_DECLARATOR_EITHER
1059 } cp_parser_declarator_kind
;
1061 /* A mapping from a token type to a corresponding tree node type. */
1063 typedef struct cp_parser_token_tree_map_node
1065 /* The token type. */
1066 enum cpp_ttype token_type
: 8;
1067 /* The corresponding tree code. */
1068 enum tree_code tree_type
: 8;
1069 } cp_parser_token_tree_map_node
;
1071 /* A complete map consists of several ordinary entries, followed by a
1072 terminator. The terminating entry has a token_type of CPP_EOF. */
1074 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map
[];
1076 /* The status of a tentative parse. */
1078 typedef enum cp_parser_status_kind
1080 /* No errors have occurred. */
1081 CP_PARSER_STATUS_KIND_NO_ERROR
,
1082 /* An error has occurred. */
1083 CP_PARSER_STATUS_KIND_ERROR
,
1084 /* We are committed to this tentative parse, whether or not an error
1086 CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind
;
1089 /* Context that is saved and restored when parsing tentatively. */
1091 typedef struct cp_parser_context
GTY (())
1093 /* If this is a tentative parsing context, the status of the
1095 enum cp_parser_status_kind status
;
1096 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1097 that are looked up in this context must be looked up both in the
1098 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1099 the context of the containing expression. */
1101 /* The next parsing context in the stack. */
1102 struct cp_parser_context
*next
;
1103 } cp_parser_context
;
1107 /* Constructors and destructors. */
1109 static cp_parser_context
*cp_parser_context_new
1110 (cp_parser_context
*);
1112 /* Class variables. */
1114 static GTY((deletable (""))) cp_parser_context
* cp_parser_context_free_list
;
1116 /* Constructors and destructors. */
1118 /* Construct a new context. The context below this one on the stack
1119 is given by NEXT. */
1121 static cp_parser_context
*
1122 cp_parser_context_new (cp_parser_context
* next
)
1124 cp_parser_context
*context
;
1126 /* Allocate the storage. */
1127 if (cp_parser_context_free_list
!= NULL
)
1129 /* Pull the first entry from the free list. */
1130 context
= cp_parser_context_free_list
;
1131 cp_parser_context_free_list
= context
->next
;
1132 memset (context
, 0, sizeof (*context
));
1135 context
= ggc_alloc_cleared (sizeof (cp_parser_context
));
1136 /* No errors have occurred yet in this context. */
1137 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1138 /* If this is not the bottomost context, copy information that we
1139 need from the previous context. */
1142 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1143 expression, then we are parsing one in this context, too. */
1144 context
->object_type
= next
->object_type
;
1145 /* Thread the stack. */
1146 context
->next
= next
;
1152 /* The cp_parser structure represents the C++ parser. */
1154 typedef struct cp_parser
GTY(())
1156 /* The lexer from which we are obtaining tokens. */
1159 /* The scope in which names should be looked up. If NULL_TREE, then
1160 we look up names in the scope that is currently open in the
1161 source program. If non-NULL, this is either a TYPE or
1162 NAMESPACE_DECL for the scope in which we should look.
1164 This value is not cleared automatically after a name is looked
1165 up, so we must be careful to clear it before starting a new look
1166 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1167 will look up `Z' in the scope of `X', rather than the current
1168 scope.) Unfortunately, it is difficult to tell when name lookup
1169 is complete, because we sometimes peek at a token, look it up,
1170 and then decide not to consume it. */
1173 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1174 last lookup took place. OBJECT_SCOPE is used if an expression
1175 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1176 respectively. QUALIFYING_SCOPE is used for an expression of the
1177 form "X::Y"; it refers to X. */
1179 tree qualifying_scope
;
1181 /* A stack of parsing contexts. All but the bottom entry on the
1182 stack will be tentative contexts.
1184 We parse tentatively in order to determine which construct is in
1185 use in some situations. For example, in order to determine
1186 whether a statement is an expression-statement or a
1187 declaration-statement we parse it tentatively as a
1188 declaration-statement. If that fails, we then reparse the same
1189 token stream as an expression-statement. */
1190 cp_parser_context
*context
;
1192 /* True if we are parsing GNU C++. If this flag is not set, then
1193 GNU extensions are not recognized. */
1194 bool allow_gnu_extensions_p
;
1196 /* TRUE if the `>' token should be interpreted as the greater-than
1197 operator. FALSE if it is the end of a template-id or
1198 template-parameter-list. */
1199 bool greater_than_is_operator_p
;
1201 /* TRUE if default arguments are allowed within a parameter list
1202 that starts at this point. FALSE if only a gnu extension makes
1203 them permissable. */
1204 bool default_arg_ok_p
;
1206 /* TRUE if we are parsing an integral constant-expression. See
1207 [expr.const] for a precise definition. */
1208 bool constant_expression_p
;
1210 /* TRUE if we are parsing an integral constant-expression -- but a
1211 non-constant expression should be permitted as well. This flag
1212 is used when parsing an array bound so that GNU variable-length
1213 arrays are tolerated. */
1214 bool allow_non_constant_expression_p
;
1216 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1217 been seen that makes the expression non-constant. */
1218 bool non_constant_expression_p
;
1220 /* TRUE if local variable names and `this' are forbidden in the
1222 bool local_variables_forbidden_p
;
1224 /* TRUE if the declaration we are parsing is part of a
1225 linkage-specification of the form `extern string-literal
1227 bool in_unbraced_linkage_specification_p
;
1229 /* TRUE if we are presently parsing a declarator, after the
1230 direct-declarator. */
1231 bool in_declarator_p
;
1233 /* If non-NULL, then we are parsing a construct where new type
1234 definitions are not permitted. The string stored here will be
1235 issued as an error message if a type is defined. */
1236 const char *type_definition_forbidden_message
;
1238 /* A list of lists. The outer list is a stack, used for member
1239 functions of local classes. At each level there are two sub-list,
1240 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1241 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1242 TREE_VALUE's. The functions are chained in reverse declaration
1245 The TREE_PURPOSE sublist contains those functions with default
1246 arguments that need post processing, and the TREE_VALUE sublist
1247 contains those functions with definitions that need post
1250 These lists can only be processed once the outermost class being
1251 defined is complete. */
1252 tree unparsed_functions_queues
;
1254 /* The number of classes whose definitions are currently in
1256 unsigned num_classes_being_defined
;
1258 /* The number of template parameter lists that apply directly to the
1259 current declaration. */
1260 unsigned num_template_parameter_lists
;
1263 /* The type of a function that parses some kind of expression */
1264 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1268 /* Constructors and destructors. */
1270 static cp_parser
*cp_parser_new
1273 /* Routines to parse various constructs.
1275 Those that return `tree' will return the error_mark_node (rather
1276 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1277 Sometimes, they will return an ordinary node if error-recovery was
1278 attempted, even though a parse error occurred. So, to check
1279 whether or not a parse error occurred, you should always use
1280 cp_parser_error_occurred. If the construct is optional (indicated
1281 either by an `_opt' in the name of the function that does the
1282 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1283 the construct is not present. */
1285 /* Lexical conventions [gram.lex] */
1287 static tree cp_parser_identifier
1290 /* Basic concepts [gram.basic] */
1292 static bool cp_parser_translation_unit
1295 /* Expressions [gram.expr] */
1297 static tree cp_parser_primary_expression
1298 (cp_parser
*, cp_id_kind
*, tree
*);
1299 static tree cp_parser_id_expression
1300 (cp_parser
*, bool, bool, bool *, bool);
1301 static tree cp_parser_unqualified_id
1302 (cp_parser
*, bool, bool, bool);
1303 static tree cp_parser_nested_name_specifier_opt
1304 (cp_parser
*, bool, bool, bool);
1305 static tree cp_parser_nested_name_specifier
1306 (cp_parser
*, bool, bool, bool);
1307 static tree cp_parser_class_or_namespace_name
1308 (cp_parser
*, bool, bool, bool, bool);
1309 static tree cp_parser_postfix_expression
1310 (cp_parser
*, bool);
1311 static tree cp_parser_parenthesized_expression_list
1312 (cp_parser
*, bool, bool *);
1313 static void cp_parser_pseudo_destructor_name
1314 (cp_parser
*, tree
*, tree
*);
1315 static tree cp_parser_unary_expression
1316 (cp_parser
*, bool);
1317 static enum tree_code cp_parser_unary_operator
1319 static tree cp_parser_new_expression
1321 static tree cp_parser_new_placement
1323 static tree cp_parser_new_type_id
1325 static tree cp_parser_new_declarator_opt
1327 static tree cp_parser_direct_new_declarator
1329 static tree cp_parser_new_initializer
1331 static tree cp_parser_delete_expression
1333 static tree cp_parser_cast_expression
1334 (cp_parser
*, bool);
1335 static tree cp_parser_pm_expression
1337 static tree cp_parser_multiplicative_expression
1339 static tree cp_parser_additive_expression
1341 static tree cp_parser_shift_expression
1343 static tree cp_parser_relational_expression
1345 static tree cp_parser_equality_expression
1347 static tree cp_parser_and_expression
1349 static tree cp_parser_exclusive_or_expression
1351 static tree cp_parser_inclusive_or_expression
1353 static tree cp_parser_logical_and_expression
1355 static tree cp_parser_logical_or_expression
1357 static tree cp_parser_question_colon_clause
1358 (cp_parser
*, tree
);
1359 static tree cp_parser_assignment_expression
1361 static enum tree_code cp_parser_assignment_operator_opt
1363 static tree cp_parser_expression
1365 static tree cp_parser_constant_expression
1366 (cp_parser
*, bool, bool *);
1368 /* Statements [gram.stmt.stmt] */
1370 static void cp_parser_statement
1371 (cp_parser
*, bool);
1372 static tree cp_parser_labeled_statement
1373 (cp_parser
*, bool);
1374 static tree cp_parser_expression_statement
1375 (cp_parser
*, bool);
1376 static tree cp_parser_compound_statement
1377 (cp_parser
*, bool);
1378 static void cp_parser_statement_seq_opt
1379 (cp_parser
*, bool);
1380 static tree cp_parser_selection_statement
1382 static tree cp_parser_condition
1384 static tree cp_parser_iteration_statement
1386 static void cp_parser_for_init_statement
1388 static tree cp_parser_jump_statement
1390 static void cp_parser_declaration_statement
1393 static tree cp_parser_implicitly_scoped_statement
1395 static void cp_parser_already_scoped_statement
1398 /* Declarations [gram.dcl.dcl] */
1400 static void cp_parser_declaration_seq_opt
1402 static void cp_parser_declaration
1404 static void cp_parser_block_declaration
1405 (cp_parser
*, bool);
1406 static void cp_parser_simple_declaration
1407 (cp_parser
*, bool);
1408 static tree cp_parser_decl_specifier_seq
1409 (cp_parser
*, cp_parser_flags
, tree
*, int *);
1410 static tree cp_parser_storage_class_specifier_opt
1412 static tree cp_parser_function_specifier_opt
1414 static tree cp_parser_type_specifier
1415 (cp_parser
*, cp_parser_flags
, bool, bool, int *, bool *);
1416 static tree cp_parser_simple_type_specifier
1417 (cp_parser
*, cp_parser_flags
, bool);
1418 static tree cp_parser_type_name
1420 static tree cp_parser_elaborated_type_specifier
1421 (cp_parser
*, bool, bool);
1422 static tree cp_parser_enum_specifier
1424 static void cp_parser_enumerator_list
1425 (cp_parser
*, tree
);
1426 static void cp_parser_enumerator_definition
1427 (cp_parser
*, tree
);
1428 static tree cp_parser_namespace_name
1430 static void cp_parser_namespace_definition
1432 static void cp_parser_namespace_body
1434 static tree cp_parser_qualified_namespace_specifier
1436 static void cp_parser_namespace_alias_definition
1438 static void cp_parser_using_declaration
1440 static void cp_parser_using_directive
1442 static void cp_parser_asm_definition
1444 static void cp_parser_linkage_specification
1447 /* Declarators [gram.dcl.decl] */
1449 static tree cp_parser_init_declarator
1450 (cp_parser
*, tree
, tree
, bool, bool, int, bool *);
1451 static tree cp_parser_declarator
1452 (cp_parser
*, cp_parser_declarator_kind
, int *);
1453 static tree cp_parser_direct_declarator
1454 (cp_parser
*, cp_parser_declarator_kind
, int *);
1455 static enum tree_code cp_parser_ptr_operator
1456 (cp_parser
*, tree
*, tree
*);
1457 static tree cp_parser_cv_qualifier_seq_opt
1459 static tree cp_parser_cv_qualifier_opt
1461 static tree cp_parser_declarator_id
1463 static tree cp_parser_type_id
1465 static tree cp_parser_type_specifier_seq
1467 static tree cp_parser_parameter_declaration_clause
1469 static tree cp_parser_parameter_declaration_list
1471 static tree cp_parser_parameter_declaration
1472 (cp_parser
*, bool);
1473 static tree cp_parser_function_definition
1474 (cp_parser
*, bool *);
1475 static void cp_parser_function_body
1477 static tree cp_parser_initializer
1478 (cp_parser
*, bool *, bool *);
1479 static tree cp_parser_initializer_clause
1480 (cp_parser
*, bool *);
1481 static tree cp_parser_initializer_list
1482 (cp_parser
*, bool *);
1484 static bool cp_parser_ctor_initializer_opt_and_function_body
1487 /* Classes [gram.class] */
1489 static tree cp_parser_class_name
1490 (cp_parser
*, bool, bool, bool, bool, bool);
1491 static tree cp_parser_class_specifier
1493 static tree cp_parser_class_head
1494 (cp_parser
*, bool *);
1495 static enum tag_types cp_parser_class_key
1497 static void cp_parser_member_specification_opt
1499 static void cp_parser_member_declaration
1501 static tree cp_parser_pure_specifier
1503 static tree cp_parser_constant_initializer
1506 /* Derived classes [gram.class.derived] */
1508 static tree cp_parser_base_clause
1510 static tree cp_parser_base_specifier
1513 /* Special member functions [gram.special] */
1515 static tree cp_parser_conversion_function_id
1517 static tree cp_parser_conversion_type_id
1519 static tree cp_parser_conversion_declarator_opt
1521 static bool cp_parser_ctor_initializer_opt
1523 static void cp_parser_mem_initializer_list
1525 static tree cp_parser_mem_initializer
1527 static tree cp_parser_mem_initializer_id
1530 /* Overloading [gram.over] */
1532 static tree cp_parser_operator_function_id
1534 static tree cp_parser_operator
1537 /* Templates [gram.temp] */
1539 static void cp_parser_template_declaration
1540 (cp_parser
*, bool);
1541 static tree cp_parser_template_parameter_list
1543 static tree cp_parser_template_parameter
1545 static tree cp_parser_type_parameter
1547 static tree cp_parser_template_id
1548 (cp_parser
*, bool, bool);
1549 static tree cp_parser_template_name
1550 (cp_parser
*, bool, bool);
1551 static tree cp_parser_template_argument_list
1553 static tree cp_parser_template_argument
1555 static void cp_parser_explicit_instantiation
1557 static void cp_parser_explicit_specialization
1560 /* Exception handling [gram.exception] */
1562 static tree cp_parser_try_block
1564 static bool cp_parser_function_try_block
1566 static void cp_parser_handler_seq
1568 static void cp_parser_handler
1570 static tree cp_parser_exception_declaration
1572 static tree cp_parser_throw_expression
1574 static tree cp_parser_exception_specification_opt
1576 static tree cp_parser_type_id_list
1579 /* GNU Extensions */
1581 static tree cp_parser_asm_specification_opt
1583 static tree cp_parser_asm_operand_list
1585 static tree cp_parser_asm_clobber_list
1587 static tree cp_parser_attributes_opt
1589 static tree cp_parser_attribute_list
1591 static bool cp_parser_extension_opt
1592 (cp_parser
*, int *);
1593 static void cp_parser_label_declaration
1596 /* Utility Routines */
1598 static tree cp_parser_lookup_name
1599 (cp_parser
*, tree
, bool, bool, bool);
1600 static tree cp_parser_lookup_name_simple
1601 (cp_parser
*, tree
);
1602 static tree cp_parser_maybe_treat_template_as_class
1604 static bool cp_parser_check_declarator_template_parameters
1605 (cp_parser
*, tree
);
1606 static bool cp_parser_check_template_parameters
1607 (cp_parser
*, unsigned);
1608 static tree cp_parser_simple_cast_expression
1610 static tree cp_parser_binary_expression
1611 (cp_parser
*, const cp_parser_token_tree_map
, cp_parser_expression_fn
);
1612 static tree cp_parser_global_scope_opt
1613 (cp_parser
*, bool);
1614 static bool cp_parser_constructor_declarator_p
1615 (cp_parser
*, bool);
1616 static tree cp_parser_function_definition_from_specifiers_and_declarator
1617 (cp_parser
*, tree
, tree
, tree
);
1618 static tree cp_parser_function_definition_after_declarator
1619 (cp_parser
*, bool);
1620 static void cp_parser_template_declaration_after_export
1621 (cp_parser
*, bool);
1622 static tree cp_parser_single_declaration
1623 (cp_parser
*, bool, bool *);
1624 static tree cp_parser_functional_cast
1625 (cp_parser
*, tree
);
1626 static void cp_parser_save_default_args
1627 (cp_parser
*, tree
);
1628 static void cp_parser_late_parsing_for_member
1629 (cp_parser
*, tree
);
1630 static void cp_parser_late_parsing_default_args
1631 (cp_parser
*, tree
);
1632 static tree cp_parser_sizeof_operand
1633 (cp_parser
*, enum rid
);
1634 static bool cp_parser_declares_only_class_p
1636 static tree cp_parser_fold_non_dependent_expr
1638 static bool cp_parser_friend_p
1640 static cp_token
*cp_parser_require
1641 (cp_parser
*, enum cpp_ttype
, const char *);
1642 static cp_token
*cp_parser_require_keyword
1643 (cp_parser
*, enum rid
, const char *);
1644 static bool cp_parser_token_starts_function_definition_p
1646 static bool cp_parser_next_token_starts_class_definition_p
1648 static bool cp_parser_next_token_ends_template_argument_p
1650 static enum tag_types cp_parser_token_is_class_key
1652 static void cp_parser_check_class_key
1653 (enum tag_types
, tree type
);
1654 static bool cp_parser_optional_template_keyword
1656 static void cp_parser_pre_parsed_nested_name_specifier
1658 static void cp_parser_cache_group
1659 (cp_parser
*, cp_token_cache
*, enum cpp_ttype
, unsigned);
1660 static void cp_parser_parse_tentatively
1662 static void cp_parser_commit_to_tentative_parse
1664 static void cp_parser_abort_tentative_parse
1666 static bool cp_parser_parse_definitely
1668 static inline bool cp_parser_parsing_tentatively
1670 static bool cp_parser_committed_to_tentative_parse
1672 static void cp_parser_error
1673 (cp_parser
*, const char *);
1674 static bool cp_parser_simulate_error
1676 static void cp_parser_check_type_definition
1678 static void cp_parser_check_for_definition_in_return_type
1680 static tree cp_parser_non_constant_expression
1682 static bool cp_parser_diagnose_invalid_type_name
1684 static int cp_parser_skip_to_closing_parenthesis
1685 (cp_parser
*, bool, bool);
1686 static void cp_parser_skip_to_end_of_statement
1688 static void cp_parser_consume_semicolon_at_end_of_statement
1690 static void cp_parser_skip_to_end_of_block_or_statement
1692 static void cp_parser_skip_to_closing_brace
1694 static void cp_parser_skip_until_found
1695 (cp_parser
*, enum cpp_ttype
, const char *);
1696 static bool cp_parser_error_occurred
1698 static bool cp_parser_allow_gnu_extensions_p
1700 static bool cp_parser_is_string_literal
1702 static bool cp_parser_is_keyword
1703 (cp_token
*, enum rid
);
1705 /* Returns nonzero if we are parsing tentatively. */
1708 cp_parser_parsing_tentatively (cp_parser
* parser
)
1710 return parser
->context
->next
!= NULL
;
1713 /* Returns nonzero if TOKEN is a string literal. */
1716 cp_parser_is_string_literal (cp_token
* token
)
1718 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1721 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1724 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1726 return token
->keyword
== keyword
;
1729 /* Issue the indicated error MESSAGE. */
1732 cp_parser_error (cp_parser
* parser
, const char* message
)
1734 /* Output the MESSAGE -- unless we're parsing tentatively. */
1735 if (!cp_parser_simulate_error (parser
))
1739 /* If we are parsing tentatively, remember that an error has occurred
1740 during this tentative parse. Returns true if the error was
1741 simulated; false if a messgae should be issued by the caller. */
1744 cp_parser_simulate_error (cp_parser
* parser
)
1746 if (cp_parser_parsing_tentatively (parser
)
1747 && !cp_parser_committed_to_tentative_parse (parser
))
1749 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1755 /* This function is called when a type is defined. If type
1756 definitions are forbidden at this point, an error message is
1760 cp_parser_check_type_definition (cp_parser
* parser
)
1762 /* If types are forbidden here, issue a message. */
1763 if (parser
->type_definition_forbidden_message
)
1764 /* Use `%s' to print the string in case there are any escape
1765 characters in the message. */
1766 error ("%s", parser
->type_definition_forbidden_message
);
1769 /* This function is called when a declaration is parsed. If
1770 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1771 indicates that a type was defined in the decl-specifiers for DECL,
1772 then an error is issued. */
1775 cp_parser_check_for_definition_in_return_type (tree declarator
,
1776 int declares_class_or_enum
)
1778 /* [dcl.fct] forbids type definitions in return types.
1779 Unfortunately, it's not easy to know whether or not we are
1780 processing a return type until after the fact. */
1782 && (TREE_CODE (declarator
) == INDIRECT_REF
1783 || TREE_CODE (declarator
) == ADDR_EXPR
))
1784 declarator
= TREE_OPERAND (declarator
, 0);
1786 && TREE_CODE (declarator
) == CALL_EXPR
1787 && declares_class_or_enum
& 2)
1788 error ("new types may not be defined in a return type");
1791 /* Issue an eror message about the fact that THING appeared in a
1792 constant-expression. Returns ERROR_MARK_NODE. */
1795 cp_parser_non_constant_expression (const char *thing
)
1797 error ("%s cannot appear in a constant-expression", thing
);
1798 return error_mark_node
;
1801 /* Check for a common situation where a type-name should be present,
1802 but is not, and issue a sensible error message. Returns true if an
1803 invalid type-name was detected. */
1806 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
)
1808 /* If the next two tokens are both identifiers, the code is
1809 erroneous. The usual cause of this situation is code like:
1813 where "T" should name a type -- but does not. */
1814 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
1815 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
1819 /* If parsing tentatively, we should commit; we really are
1820 looking at a declaration. */
1821 /* Consume the first identifier. */
1822 name
= cp_lexer_consume_token (parser
->lexer
)->value
;
1823 /* Issue an error message. */
1824 error ("`%s' does not name a type", IDENTIFIER_POINTER (name
));
1825 /* If we're in a template class, it's possible that the user was
1826 referring to a type from a base class. For example:
1828 template <typename T> struct A { typedef T X; };
1829 template <typename T> struct B : public A<T> { X x; };
1831 The user should have said "typename A<T>::X". */
1832 if (processing_template_decl
&& current_class_type
)
1836 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1840 tree base_type
= BINFO_TYPE (b
);
1841 if (CLASS_TYPE_P (base_type
)
1842 && dependent_type_p (base_type
))
1845 /* Go from a particular instantiation of the
1846 template (which will have an empty TYPE_FIELDs),
1847 to the main version. */
1848 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1849 for (field
= TYPE_FIELDS (base_type
);
1851 field
= TREE_CHAIN (field
))
1852 if (TREE_CODE (field
) == TYPE_DECL
1853 && DECL_NAME (field
) == name
)
1855 error ("(perhaps `typename %T::%s' was intended)",
1856 BINFO_TYPE (b
), IDENTIFIER_POINTER (name
));
1864 /* Skip to the end of the declaration; there's no point in
1865 trying to process it. */
1866 cp_parser_skip_to_end_of_statement (parser
);
1874 /* Consume tokens up to, and including, the next non-nested closing `)'.
1875 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1876 are doing error recovery. Returns -1 if OR_COMMA is true and we
1877 found an unnested comma. */
1880 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
1881 bool recovering
, bool or_comma
)
1883 unsigned paren_depth
= 0;
1884 unsigned brace_depth
= 0;
1886 if (recovering
&& !or_comma
&& cp_parser_parsing_tentatively (parser
)
1887 && !cp_parser_committed_to_tentative_parse (parser
))
1894 /* If we've run out of tokens, then there is no closing `)'. */
1895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
1900 token
= cp_lexer_peek_token (parser
->lexer
);
1902 /* This matches the processing in skip_to_end_of_statement */
1903 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
1905 if (token
->type
== CPP_OPEN_BRACE
)
1907 if (token
->type
== CPP_CLOSE_BRACE
)
1912 if (or_comma
&& token
->type
== CPP_COMMA
1913 && !brace_depth
&& !paren_depth
)
1917 /* Consume the token. */
1918 token
= cp_lexer_consume_token (parser
->lexer
);
1922 /* If it is an `(', we have entered another level of nesting. */
1923 if (token
->type
== CPP_OPEN_PAREN
)
1925 /* If it is a `)', then we might be done. */
1926 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
1932 /* Consume tokens until we reach the end of the current statement.
1933 Normally, that will be just before consuming a `;'. However, if a
1934 non-nested `}' comes first, then we stop before consuming that. */
1937 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
1939 unsigned nesting_depth
= 0;
1945 /* Peek at the next token. */
1946 token
= cp_lexer_peek_token (parser
->lexer
);
1947 /* If we've run out of tokens, stop. */
1948 if (token
->type
== CPP_EOF
)
1950 /* If the next token is a `;', we have reached the end of the
1952 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
1954 /* If the next token is a non-nested `}', then we have reached
1955 the end of the current block. */
1956 if (token
->type
== CPP_CLOSE_BRACE
)
1958 /* If this is a non-nested `}', stop before consuming it.
1959 That way, when confronted with something like:
1963 we stop before consuming the closing `}', even though we
1964 have not yet reached a `;'. */
1965 if (nesting_depth
== 0)
1967 /* If it is the closing `}' for a block that we have
1968 scanned, stop -- but only after consuming the token.
1974 we will stop after the body of the erroneously declared
1975 function, but before consuming the following `typedef'
1977 if (--nesting_depth
== 0)
1979 cp_lexer_consume_token (parser
->lexer
);
1983 /* If it the next token is a `{', then we are entering a new
1984 block. Consume the entire block. */
1985 else if (token
->type
== CPP_OPEN_BRACE
)
1987 /* Consume the token. */
1988 cp_lexer_consume_token (parser
->lexer
);
1992 /* This function is called at the end of a statement or declaration.
1993 If the next token is a semicolon, it is consumed; otherwise, error
1994 recovery is attempted. */
1997 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
1999 /* Look for the trailing `;'. */
2000 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2002 /* If there is additional (erroneous) input, skip to the end of
2004 cp_parser_skip_to_end_of_statement (parser
);
2005 /* If the next token is now a `;', consume it. */
2006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2007 cp_lexer_consume_token (parser
->lexer
);
2011 /* Skip tokens until we have consumed an entire block, or until we
2012 have consumed a non-nested `;'. */
2015 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2017 unsigned nesting_depth
= 0;
2023 /* Peek at the next token. */
2024 token
= cp_lexer_peek_token (parser
->lexer
);
2025 /* If we've run out of tokens, stop. */
2026 if (token
->type
== CPP_EOF
)
2028 /* If the next token is a `;', we have reached the end of the
2030 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2032 /* Consume the `;'. */
2033 cp_lexer_consume_token (parser
->lexer
);
2036 /* Consume the token. */
2037 token
= cp_lexer_consume_token (parser
->lexer
);
2038 /* If the next token is a non-nested `}', then we have reached
2039 the end of the current block. */
2040 if (token
->type
== CPP_CLOSE_BRACE
2041 && (nesting_depth
== 0 || --nesting_depth
== 0))
2043 /* If it the next token is a `{', then we are entering a new
2044 block. Consume the entire block. */
2045 if (token
->type
== CPP_OPEN_BRACE
)
2050 /* Skip tokens until a non-nested closing curly brace is the next
2054 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2056 unsigned nesting_depth
= 0;
2062 /* Peek at the next token. */
2063 token
= cp_lexer_peek_token (parser
->lexer
);
2064 /* If we've run out of tokens, stop. */
2065 if (token
->type
== CPP_EOF
)
2067 /* If the next token is a non-nested `}', then we have reached
2068 the end of the current block. */
2069 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2071 /* If it the next token is a `{', then we are entering a new
2072 block. Consume the entire block. */
2073 else if (token
->type
== CPP_OPEN_BRACE
)
2075 /* Consume the token. */
2076 cp_lexer_consume_token (parser
->lexer
);
2080 /* Create a new C++ parser. */
2083 cp_parser_new (void)
2088 /* cp_lexer_new_main is called before calling ggc_alloc because
2089 cp_lexer_new_main might load a PCH file. */
2090 lexer
= cp_lexer_new_main ();
2092 parser
= ggc_alloc_cleared (sizeof (cp_parser
));
2093 parser
->lexer
= lexer
;
2094 parser
->context
= cp_parser_context_new (NULL
);
2096 /* For now, we always accept GNU extensions. */
2097 parser
->allow_gnu_extensions_p
= 1;
2099 /* The `>' token is a greater-than operator, not the end of a
2101 parser
->greater_than_is_operator_p
= true;
2103 parser
->default_arg_ok_p
= true;
2105 /* We are not parsing a constant-expression. */
2106 parser
->constant_expression_p
= false;
2107 parser
->allow_non_constant_expression_p
= false;
2108 parser
->non_constant_expression_p
= false;
2110 /* Local variable names are not forbidden. */
2111 parser
->local_variables_forbidden_p
= false;
2113 /* We are not processing an `extern "C"' declaration. */
2114 parser
->in_unbraced_linkage_specification_p
= false;
2116 /* We are not processing a declarator. */
2117 parser
->in_declarator_p
= false;
2119 /* The unparsed function queue is empty. */
2120 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2122 /* There are no classes being defined. */
2123 parser
->num_classes_being_defined
= 0;
2125 /* No template parameters apply. */
2126 parser
->num_template_parameter_lists
= 0;
2131 /* Lexical conventions [gram.lex] */
2133 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2137 cp_parser_identifier (cp_parser
* parser
)
2141 /* Look for the identifier. */
2142 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2143 /* Return the value. */
2144 return token
? token
->value
: error_mark_node
;
2147 /* Basic concepts [gram.basic] */
2149 /* Parse a translation-unit.
2152 declaration-seq [opt]
2154 Returns TRUE if all went well. */
2157 cp_parser_translation_unit (cp_parser
* parser
)
2161 cp_parser_declaration_seq_opt (parser
);
2163 /* If there are no tokens left then all went well. */
2164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2167 /* Otherwise, issue an error message. */
2168 cp_parser_error (parser
, "expected declaration");
2172 /* Consume the EOF token. */
2173 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2176 finish_translation_unit ();
2178 /* All went well. */
2182 /* Expressions [gram.expr] */
2184 /* Parse a primary-expression.
2195 ( compound-statement )
2196 __builtin_va_arg ( assignment-expression , type-id )
2201 Returns a representation of the expression.
2203 *IDK indicates what kind of id-expression (if any) was present.
2205 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2206 used as the operand of a pointer-to-member. In that case,
2207 *QUALIFYING_CLASS gives the class that is used as the qualifying
2208 class in the pointer-to-member. */
2211 cp_parser_primary_expression (cp_parser
*parser
,
2213 tree
*qualifying_class
)
2217 /* Assume the primary expression is not an id-expression. */
2218 *idk
= CP_ID_KIND_NONE
;
2219 /* And that it cannot be used as pointer-to-member. */
2220 *qualifying_class
= NULL_TREE
;
2222 /* Peek at the next token. */
2223 token
= cp_lexer_peek_token (parser
->lexer
);
2224 switch (token
->type
)
2237 token
= cp_lexer_consume_token (parser
->lexer
);
2238 return token
->value
;
2240 case CPP_OPEN_PAREN
:
2243 bool saved_greater_than_is_operator_p
;
2245 /* Consume the `('. */
2246 cp_lexer_consume_token (parser
->lexer
);
2247 /* Within a parenthesized expression, a `>' token is always
2248 the greater-than operator. */
2249 saved_greater_than_is_operator_p
2250 = parser
->greater_than_is_operator_p
;
2251 parser
->greater_than_is_operator_p
= true;
2252 /* If we see `( { ' then we are looking at the beginning of
2253 a GNU statement-expression. */
2254 if (cp_parser_allow_gnu_extensions_p (parser
)
2255 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2257 /* Statement-expressions are not allowed by the standard. */
2259 pedwarn ("ISO C++ forbids braced-groups within expressions");
2261 /* And they're not allowed outside of a function-body; you
2262 cannot, for example, write:
2264 int i = ({ int j = 3; j + 1; });
2266 at class or namespace scope. */
2267 if (!at_function_scope_p ())
2268 error ("statement-expressions are allowed only inside functions");
2269 /* Start the statement-expression. */
2270 expr
= begin_stmt_expr ();
2271 /* Parse the compound-statement. */
2272 cp_parser_compound_statement (parser
, true);
2274 expr
= finish_stmt_expr (expr
, false);
2278 /* Parse the parenthesized expression. */
2279 expr
= cp_parser_expression (parser
);
2280 /* Let the front end know that this expression was
2281 enclosed in parentheses. This matters in case, for
2282 example, the expression is of the form `A::B', since
2283 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2285 finish_parenthesized_expr (expr
);
2287 /* The `>' token might be the end of a template-id or
2288 template-parameter-list now. */
2289 parser
->greater_than_is_operator_p
2290 = saved_greater_than_is_operator_p
;
2291 /* Consume the `)'. */
2292 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2293 cp_parser_skip_to_end_of_statement (parser
);
2299 switch (token
->keyword
)
2301 /* These two are the boolean literals. */
2303 cp_lexer_consume_token (parser
->lexer
);
2304 return boolean_true_node
;
2306 cp_lexer_consume_token (parser
->lexer
);
2307 return boolean_false_node
;
2309 /* The `__null' literal. */
2311 cp_lexer_consume_token (parser
->lexer
);
2314 /* Recognize the `this' keyword. */
2316 cp_lexer_consume_token (parser
->lexer
);
2317 if (parser
->local_variables_forbidden_p
)
2319 error ("`this' may not be used in this context");
2320 return error_mark_node
;
2322 /* Pointers cannot appear in constant-expressions. */
2323 if (parser
->constant_expression_p
)
2325 if (!parser
->allow_non_constant_expression_p
)
2326 return cp_parser_non_constant_expression ("`this'");
2327 parser
->non_constant_expression_p
= true;
2329 return finish_this_expr ();
2331 /* The `operator' keyword can be the beginning of an
2336 case RID_FUNCTION_NAME
:
2337 case RID_PRETTY_FUNCTION_NAME
:
2338 case RID_C99_FUNCTION_NAME
:
2339 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2340 __func__ are the names of variables -- but they are
2341 treated specially. Therefore, they are handled here,
2342 rather than relying on the generic id-expression logic
2343 below. Grammatically, these names are id-expressions.
2345 Consume the token. */
2346 token
= cp_lexer_consume_token (parser
->lexer
);
2347 /* Look up the name. */
2348 return finish_fname (token
->value
);
2355 /* The `__builtin_va_arg' construct is used to handle
2356 `va_arg'. Consume the `__builtin_va_arg' token. */
2357 cp_lexer_consume_token (parser
->lexer
);
2358 /* Look for the opening `('. */
2359 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2360 /* Now, parse the assignment-expression. */
2361 expression
= cp_parser_assignment_expression (parser
);
2362 /* Look for the `,'. */
2363 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2364 /* Parse the type-id. */
2365 type
= cp_parser_type_id (parser
);
2366 /* Look for the closing `)'. */
2367 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2368 /* Using `va_arg' in a constant-expression is not
2370 if (parser
->constant_expression_p
)
2372 if (!parser
->allow_non_constant_expression_p
)
2373 return cp_parser_non_constant_expression ("`va_arg'");
2374 parser
->non_constant_expression_p
= true;
2376 return build_x_va_arg (expression
, type
);
2380 cp_parser_error (parser
, "expected primary-expression");
2381 return error_mark_node
;
2384 /* An id-expression can start with either an identifier, a
2385 `::' as the beginning of a qualified-id, or the "operator"
2389 case CPP_TEMPLATE_ID
:
2390 case CPP_NESTED_NAME_SPECIFIER
:
2394 const char *error_msg
;
2397 /* Parse the id-expression. */
2399 = cp_parser_id_expression (parser
,
2400 /*template_keyword_p=*/false,
2401 /*check_dependency_p=*/true,
2402 /*template_p=*/NULL
,
2403 /*declarator_p=*/false);
2404 if (id_expression
== error_mark_node
)
2405 return error_mark_node
;
2406 /* If we have a template-id, then no further lookup is
2407 required. If the template-id was for a template-class, we
2408 will sometimes have a TYPE_DECL at this point. */
2409 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2410 || TREE_CODE (id_expression
) == TYPE_DECL
)
2411 decl
= id_expression
;
2412 /* Look up the name. */
2415 decl
= cp_parser_lookup_name_simple (parser
, id_expression
);
2416 /* If name lookup gives us a SCOPE_REF, then the
2417 qualifying scope was dependent. Just propagate the
2419 if (TREE_CODE (decl
) == SCOPE_REF
)
2421 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2422 *qualifying_class
= TREE_OPERAND (decl
, 0);
2425 /* Check to see if DECL is a local variable in a context
2426 where that is forbidden. */
2427 if (parser
->local_variables_forbidden_p
2428 && local_variable_p (decl
))
2430 /* It might be that we only found DECL because we are
2431 trying to be generous with pre-ISO scoping rules.
2432 For example, consider:
2436 for (int i = 0; i < 10; ++i) {}
2437 extern void f(int j = i);
2440 Here, name look up will originally find the out
2441 of scope `i'. We need to issue a warning message,
2442 but then use the global `i'. */
2443 decl
= check_for_out_of_scope_variable (decl
);
2444 if (local_variable_p (decl
))
2446 error ("local variable `%D' may not appear in this context",
2448 return error_mark_node
;
2453 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2454 idk
, qualifying_class
,
2455 parser
->constant_expression_p
,
2456 parser
->allow_non_constant_expression_p
,
2457 &parser
->non_constant_expression_p
,
2460 cp_parser_error (parser
, error_msg
);
2464 /* Anything else is an error. */
2466 cp_parser_error (parser
, "expected primary-expression");
2467 return error_mark_node
;
2471 /* Parse an id-expression.
2478 :: [opt] nested-name-specifier template [opt] unqualified-id
2480 :: operator-function-id
2483 Return a representation of the unqualified portion of the
2484 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2485 a `::' or nested-name-specifier.
2487 Often, if the id-expression was a qualified-id, the caller will
2488 want to make a SCOPE_REF to represent the qualified-id. This
2489 function does not do this in order to avoid wastefully creating
2490 SCOPE_REFs when they are not required.
2492 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2495 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2496 uninstantiated templates.
2498 If *TEMPLATE_P is non-NULL, it is set to true iff the
2499 `template' keyword is used to explicitly indicate that the entity
2500 named is a template.
2502 If DECLARATOR_P is true, the id-expression is appearing as part of
2503 a declarator, rather than as part of an exprsesion. */
2506 cp_parser_id_expression (cp_parser
*parser
,
2507 bool template_keyword_p
,
2508 bool check_dependency_p
,
2512 bool global_scope_p
;
2513 bool nested_name_specifier_p
;
2515 /* Assume the `template' keyword was not used. */
2517 *template_p
= false;
2519 /* Look for the optional `::' operator. */
2521 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2523 /* Look for the optional nested-name-specifier. */
2524 nested_name_specifier_p
2525 = (cp_parser_nested_name_specifier_opt (parser
,
2526 /*typename_keyword_p=*/false,
2530 /* If there is a nested-name-specifier, then we are looking at
2531 the first qualified-id production. */
2532 if (nested_name_specifier_p
)
2535 tree saved_object_scope
;
2536 tree saved_qualifying_scope
;
2537 tree unqualified_id
;
2540 /* See if the next token is the `template' keyword. */
2542 template_p
= &is_template
;
2543 *template_p
= cp_parser_optional_template_keyword (parser
);
2544 /* Name lookup we do during the processing of the
2545 unqualified-id might obliterate SCOPE. */
2546 saved_scope
= parser
->scope
;
2547 saved_object_scope
= parser
->object_scope
;
2548 saved_qualifying_scope
= parser
->qualifying_scope
;
2549 /* Process the final unqualified-id. */
2550 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2553 /* Restore the SAVED_SCOPE for our caller. */
2554 parser
->scope
= saved_scope
;
2555 parser
->object_scope
= saved_object_scope
;
2556 parser
->qualifying_scope
= saved_qualifying_scope
;
2558 return unqualified_id
;
2560 /* Otherwise, if we are in global scope, then we are looking at one
2561 of the other qualified-id productions. */
2562 else if (global_scope_p
)
2567 /* Peek at the next token. */
2568 token
= cp_lexer_peek_token (parser
->lexer
);
2570 /* If it's an identifier, and the next token is not a "<", then
2571 we can avoid the template-id case. This is an optimization
2572 for this common case. */
2573 if (token
->type
== CPP_NAME
2574 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
2575 return cp_parser_identifier (parser
);
2577 cp_parser_parse_tentatively (parser
);
2578 /* Try a template-id. */
2579 id
= cp_parser_template_id (parser
,
2580 /*template_keyword_p=*/false,
2581 /*check_dependency_p=*/true);
2582 /* If that worked, we're done. */
2583 if (cp_parser_parse_definitely (parser
))
2586 /* Peek at the next token. (Changes in the token buffer may
2587 have invalidated the pointer obtained above.) */
2588 token
= cp_lexer_peek_token (parser
->lexer
);
2590 switch (token
->type
)
2593 return cp_parser_identifier (parser
);
2596 if (token
->keyword
== RID_OPERATOR
)
2597 return cp_parser_operator_function_id (parser
);
2601 cp_parser_error (parser
, "expected id-expression");
2602 return error_mark_node
;
2606 return cp_parser_unqualified_id (parser
, template_keyword_p
,
2607 /*check_dependency_p=*/true,
2611 /* Parse an unqualified-id.
2615 operator-function-id
2616 conversion-function-id
2620 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2621 keyword, in a construct like `A::template ...'.
2623 Returns a representation of unqualified-id. For the `identifier'
2624 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2625 production a BIT_NOT_EXPR is returned; the operand of the
2626 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2627 other productions, see the documentation accompanying the
2628 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2629 names are looked up in uninstantiated templates. If DECLARATOR_P
2630 is true, the unqualified-id is appearing as part of a declarator,
2631 rather than as part of an expression. */
2634 cp_parser_unqualified_id (cp_parser
* parser
,
2635 bool template_keyword_p
,
2636 bool check_dependency_p
,
2641 /* Peek at the next token. */
2642 token
= cp_lexer_peek_token (parser
->lexer
);
2644 switch (token
->type
)
2650 /* We don't know yet whether or not this will be a
2652 cp_parser_parse_tentatively (parser
);
2653 /* Try a template-id. */
2654 id
= cp_parser_template_id (parser
, template_keyword_p
,
2655 check_dependency_p
);
2656 /* If it worked, we're done. */
2657 if (cp_parser_parse_definitely (parser
))
2659 /* Otherwise, it's an ordinary identifier. */
2660 return cp_parser_identifier (parser
);
2663 case CPP_TEMPLATE_ID
:
2664 return cp_parser_template_id (parser
, template_keyword_p
,
2665 check_dependency_p
);
2670 tree qualifying_scope
;
2674 /* Consume the `~' token. */
2675 cp_lexer_consume_token (parser
->lexer
);
2676 /* Parse the class-name. The standard, as written, seems to
2679 template <typename T> struct S { ~S (); };
2680 template <typename T> S<T>::~S() {}
2682 is invalid, since `~' must be followed by a class-name, but
2683 `S<T>' is dependent, and so not known to be a class.
2684 That's not right; we need to look in uninstantiated
2685 templates. A further complication arises from:
2687 template <typename T> void f(T t) {
2691 Here, it is not possible to look up `T' in the scope of `T'
2692 itself. We must look in both the current scope, and the
2693 scope of the containing complete expression.
2695 Yet another issue is:
2704 The standard does not seem to say that the `S' in `~S'
2705 should refer to the type `S' and not the data member
2708 /* DR 244 says that we look up the name after the "~" in the
2709 same scope as we looked up the qualifying name. That idea
2710 isn't fully worked out; it's more complicated than that. */
2711 scope
= parser
->scope
;
2712 object_scope
= parser
->object_scope
;
2713 qualifying_scope
= parser
->qualifying_scope
;
2715 /* If the name is of the form "X::~X" it's OK. */
2716 if (scope
&& TYPE_P (scope
)
2717 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2718 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
2720 && (cp_lexer_peek_token (parser
->lexer
)->value
2721 == TYPE_IDENTIFIER (scope
)))
2723 cp_lexer_consume_token (parser
->lexer
);
2724 return build_nt (BIT_NOT_EXPR
, scope
);
2727 /* If there was an explicit qualification (S::~T), first look
2728 in the scope given by the qualification (i.e., S). */
2731 cp_parser_parse_tentatively (parser
);
2732 type_decl
= cp_parser_class_name (parser
,
2733 /*typename_keyword_p=*/false,
2734 /*template_keyword_p=*/false,
2736 /*check_dependency=*/false,
2737 /*class_head_p=*/false);
2738 if (cp_parser_parse_definitely (parser
))
2739 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2741 /* In "N::S::~S", look in "N" as well. */
2742 if (scope
&& qualifying_scope
)
2744 cp_parser_parse_tentatively (parser
);
2745 parser
->scope
= qualifying_scope
;
2746 parser
->object_scope
= NULL_TREE
;
2747 parser
->qualifying_scope
= NULL_TREE
;
2749 = cp_parser_class_name (parser
,
2750 /*typename_keyword_p=*/false,
2751 /*template_keyword_p=*/false,
2753 /*check_dependency=*/false,
2754 /*class_head_p=*/false);
2755 if (cp_parser_parse_definitely (parser
))
2756 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2758 /* In "p->S::~T", look in the scope given by "*p" as well. */
2759 else if (object_scope
)
2761 cp_parser_parse_tentatively (parser
);
2762 parser
->scope
= object_scope
;
2763 parser
->object_scope
= NULL_TREE
;
2764 parser
->qualifying_scope
= NULL_TREE
;
2766 = cp_parser_class_name (parser
,
2767 /*typename_keyword_p=*/false,
2768 /*template_keyword_p=*/false,
2770 /*check_dependency=*/false,
2771 /*class_head_p=*/false);
2772 if (cp_parser_parse_definitely (parser
))
2773 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2775 /* Look in the surrounding context. */
2776 parser
->scope
= NULL_TREE
;
2777 parser
->object_scope
= NULL_TREE
;
2778 parser
->qualifying_scope
= NULL_TREE
;
2780 = cp_parser_class_name (parser
,
2781 /*typename_keyword_p=*/false,
2782 /*template_keyword_p=*/false,
2784 /*check_dependency=*/false,
2785 /*class_head_p=*/false);
2786 /* If an error occurred, assume that the name of the
2787 destructor is the same as the name of the qualifying
2788 class. That allows us to keep parsing after running
2789 into ill-formed destructor names. */
2790 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
2791 return build_nt (BIT_NOT_EXPR
, scope
);
2792 else if (type_decl
== error_mark_node
)
2793 return error_mark_node
;
2797 A typedef-name that names a class shall not be used as the
2798 identifier in the declarator for a destructor declaration. */
2800 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
2801 && !DECL_SELF_REFERENCE_P (type_decl
))
2802 error ("typedef-name `%D' used as destructor declarator",
2805 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2809 if (token
->keyword
== RID_OPERATOR
)
2813 /* This could be a template-id, so we try that first. */
2814 cp_parser_parse_tentatively (parser
);
2815 /* Try a template-id. */
2816 id
= cp_parser_template_id (parser
, template_keyword_p
,
2817 /*check_dependency_p=*/true);
2818 /* If that worked, we're done. */
2819 if (cp_parser_parse_definitely (parser
))
2821 /* We still don't know whether we're looking at an
2822 operator-function-id or a conversion-function-id. */
2823 cp_parser_parse_tentatively (parser
);
2824 /* Try an operator-function-id. */
2825 id
= cp_parser_operator_function_id (parser
);
2826 /* If that didn't work, try a conversion-function-id. */
2827 if (!cp_parser_parse_definitely (parser
))
2828 id
= cp_parser_conversion_function_id (parser
);
2835 cp_parser_error (parser
, "expected unqualified-id");
2836 return error_mark_node
;
2840 /* Parse an (optional) nested-name-specifier.
2842 nested-name-specifier:
2843 class-or-namespace-name :: nested-name-specifier [opt]
2844 class-or-namespace-name :: template nested-name-specifier [opt]
2846 PARSER->SCOPE should be set appropriately before this function is
2847 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2848 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2851 Sets PARSER->SCOPE to the class (TYPE) or namespace
2852 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2853 it unchanged if there is no nested-name-specifier. Returns the new
2854 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2857 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
2858 bool typename_keyword_p
,
2859 bool check_dependency_p
,
2862 bool success
= false;
2863 tree access_check
= NULL_TREE
;
2867 /* If the next token corresponds to a nested name specifier, there
2868 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2869 false, it may have been true before, in which case something
2870 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2871 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2872 CHECK_DEPENDENCY_P is false, we have to fall through into the
2874 if (check_dependency_p
2875 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
2877 cp_parser_pre_parsed_nested_name_specifier (parser
);
2878 return parser
->scope
;
2881 /* Remember where the nested-name-specifier starts. */
2882 if (cp_parser_parsing_tentatively (parser
)
2883 && !cp_parser_committed_to_tentative_parse (parser
))
2885 token
= cp_lexer_peek_token (parser
->lexer
);
2886 start
= cp_lexer_token_difference (parser
->lexer
,
2887 parser
->lexer
->first_token
,
2893 push_deferring_access_checks (dk_deferred
);
2899 tree saved_qualifying_scope
;
2900 bool template_keyword_p
;
2902 /* Spot cases that cannot be the beginning of a
2903 nested-name-specifier. */
2904 token
= cp_lexer_peek_token (parser
->lexer
);
2906 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2907 the already parsed nested-name-specifier. */
2908 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2910 /* Grab the nested-name-specifier and continue the loop. */
2911 cp_parser_pre_parsed_nested_name_specifier (parser
);
2916 /* Spot cases that cannot be the beginning of a
2917 nested-name-specifier. On the second and subsequent times
2918 through the loop, we look for the `template' keyword. */
2919 if (success
&& token
->keyword
== RID_TEMPLATE
)
2921 /* A template-id can start a nested-name-specifier. */
2922 else if (token
->type
== CPP_TEMPLATE_ID
)
2926 /* If the next token is not an identifier, then it is
2927 definitely not a class-or-namespace-name. */
2928 if (token
->type
!= CPP_NAME
)
2930 /* If the following token is neither a `<' (to begin a
2931 template-id), nor a `::', then we are not looking at a
2932 nested-name-specifier. */
2933 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2934 if (token
->type
!= CPP_LESS
&& token
->type
!= CPP_SCOPE
)
2938 /* The nested-name-specifier is optional, so we parse
2940 cp_parser_parse_tentatively (parser
);
2942 /* Look for the optional `template' keyword, if this isn't the
2943 first time through the loop. */
2945 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
2947 template_keyword_p
= false;
2949 /* Save the old scope since the name lookup we are about to do
2950 might destroy it. */
2951 old_scope
= parser
->scope
;
2952 saved_qualifying_scope
= parser
->qualifying_scope
;
2953 /* Parse the qualifying entity. */
2955 = cp_parser_class_or_namespace_name (parser
,
2960 /* Look for the `::' token. */
2961 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
2963 /* If we found what we wanted, we keep going; otherwise, we're
2965 if (!cp_parser_parse_definitely (parser
))
2967 bool error_p
= false;
2969 /* Restore the OLD_SCOPE since it was valid before the
2970 failed attempt at finding the last
2971 class-or-namespace-name. */
2972 parser
->scope
= old_scope
;
2973 parser
->qualifying_scope
= saved_qualifying_scope
;
2974 /* If the next token is an identifier, and the one after
2975 that is a `::', then any valid interpretation would have
2976 found a class-or-namespace-name. */
2977 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2978 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
2980 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
2983 token
= cp_lexer_consume_token (parser
->lexer
);
2988 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
2989 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2990 error ("`%D' used without template parameters",
2992 else if (parser
->scope
)
2994 if (TYPE_P (parser
->scope
))
2995 error ("`%T::%D' is not a class-name or "
2997 parser
->scope
, token
->value
);
2999 error ("`%D::%D' is not a class-name or "
3001 parser
->scope
, token
->value
);
3004 error ("`%D' is not a class-name or namespace-name",
3006 parser
->scope
= NULL_TREE
;
3008 /* Treat this as a successful nested-name-specifier
3013 If the name found is not a class-name (clause
3014 _class_) or namespace-name (_namespace.def_), the
3015 program is ill-formed. */
3018 cp_lexer_consume_token (parser
->lexer
);
3023 /* We've found one valid nested-name-specifier. */
3025 /* Make sure we look in the right scope the next time through
3027 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3028 ? TREE_TYPE (new_scope
)
3030 /* If it is a class scope, try to complete it; we are about to
3031 be looking up names inside the class. */
3032 if (TYPE_P (parser
->scope
)
3033 /* Since checking types for dependency can be expensive,
3034 avoid doing it if the type is already complete. */
3035 && !COMPLETE_TYPE_P (parser
->scope
)
3036 /* Do not try to complete dependent types. */
3037 && !dependent_type_p (parser
->scope
))
3038 complete_type (parser
->scope
);
3041 /* Retrieve any deferred checks. Do not pop this access checks yet
3042 so the memory will not be reclaimed during token replacing below. */
3043 access_check
= get_deferred_access_checks ();
3045 /* If parsing tentatively, replace the sequence of tokens that makes
3046 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3047 token. That way, should we re-parse the token stream, we will
3048 not have to repeat the effort required to do the parse, nor will
3049 we issue duplicate error messages. */
3050 if (success
&& start
>= 0)
3052 /* Find the token that corresponds to the start of the
3054 token
= cp_lexer_advance_token (parser
->lexer
,
3055 parser
->lexer
->first_token
,
3058 /* Reset the contents of the START token. */
3059 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3060 token
->value
= build_tree_list (access_check
, parser
->scope
);
3061 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3062 token
->keyword
= RID_MAX
;
3063 /* Purge all subsequent tokens. */
3064 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3067 pop_deferring_access_checks ();
3068 return success
? parser
->scope
: NULL_TREE
;
3071 /* Parse a nested-name-specifier. See
3072 cp_parser_nested_name_specifier_opt for details. This function
3073 behaves identically, except that it will an issue an error if no
3074 nested-name-specifier is present, and it will return
3075 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3079 cp_parser_nested_name_specifier (cp_parser
*parser
,
3080 bool typename_keyword_p
,
3081 bool check_dependency_p
,
3086 /* Look for the nested-name-specifier. */
3087 scope
= cp_parser_nested_name_specifier_opt (parser
,
3091 /* If it was not present, issue an error message. */
3094 cp_parser_error (parser
, "expected nested-name-specifier");
3095 parser
->scope
= NULL_TREE
;
3096 return error_mark_node
;
3102 /* Parse a class-or-namespace-name.
3104 class-or-namespace-name:
3108 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3109 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3110 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3111 TYPE_P is TRUE iff the next name should be taken as a class-name,
3112 even the same name is declared to be another entity in the same
3115 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3116 specified by the class-or-namespace-name. If neither is found the
3117 ERROR_MARK_NODE is returned. */
3120 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3121 bool typename_keyword_p
,
3122 bool template_keyword_p
,
3123 bool check_dependency_p
,
3127 tree saved_qualifying_scope
;
3128 tree saved_object_scope
;
3132 /* Before we try to parse the class-name, we must save away the
3133 current PARSER->SCOPE since cp_parser_class_name will destroy
3135 saved_scope
= parser
->scope
;
3136 saved_qualifying_scope
= parser
->qualifying_scope
;
3137 saved_object_scope
= parser
->object_scope
;
3138 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3139 there is no need to look for a namespace-name. */
3140 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3142 cp_parser_parse_tentatively (parser
);
3143 scope
= cp_parser_class_name (parser
,
3148 /*class_head_p=*/false);
3149 /* If that didn't work, try for a namespace-name. */
3150 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3152 /* Restore the saved scope. */
3153 parser
->scope
= saved_scope
;
3154 parser
->qualifying_scope
= saved_qualifying_scope
;
3155 parser
->object_scope
= saved_object_scope
;
3156 /* If we are not looking at an identifier followed by the scope
3157 resolution operator, then this is not part of a
3158 nested-name-specifier. (Note that this function is only used
3159 to parse the components of a nested-name-specifier.) */
3160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3161 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3162 return error_mark_node
;
3163 scope
= cp_parser_namespace_name (parser
);
3169 /* Parse a postfix-expression.
3173 postfix-expression [ expression ]
3174 postfix-expression ( expression-list [opt] )
3175 simple-type-specifier ( expression-list [opt] )
3176 typename :: [opt] nested-name-specifier identifier
3177 ( expression-list [opt] )
3178 typename :: [opt] nested-name-specifier template [opt] template-id
3179 ( expression-list [opt] )
3180 postfix-expression . template [opt] id-expression
3181 postfix-expression -> template [opt] id-expression
3182 postfix-expression . pseudo-destructor-name
3183 postfix-expression -> pseudo-destructor-name
3184 postfix-expression ++
3185 postfix-expression --
3186 dynamic_cast < type-id > ( expression )
3187 static_cast < type-id > ( expression )
3188 reinterpret_cast < type-id > ( expression )
3189 const_cast < type-id > ( expression )
3190 typeid ( expression )
3196 ( type-id ) { initializer-list , [opt] }
3198 This extension is a GNU version of the C99 compound-literal
3199 construct. (The C99 grammar uses `type-name' instead of `type-id',
3200 but they are essentially the same concept.)
3202 If ADDRESS_P is true, the postfix expression is the operand of the
3205 Returns a representation of the expression. */
3208 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3212 cp_id_kind idk
= CP_ID_KIND_NONE
;
3213 tree postfix_expression
= NULL_TREE
;
3214 /* Non-NULL only if the current postfix-expression can be used to
3215 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3216 class used to qualify the member. */
3217 tree qualifying_class
= NULL_TREE
;
3219 /* Peek at the next token. */
3220 token
= cp_lexer_peek_token (parser
->lexer
);
3221 /* Some of the productions are determined by keywords. */
3222 keyword
= token
->keyword
;
3232 const char *saved_message
;
3234 /* All of these can be handled in the same way from the point
3235 of view of parsing. Begin by consuming the token
3236 identifying the cast. */
3237 cp_lexer_consume_token (parser
->lexer
);
3239 /* New types cannot be defined in the cast. */
3240 saved_message
= parser
->type_definition_forbidden_message
;
3241 parser
->type_definition_forbidden_message
3242 = "types may not be defined in casts";
3244 /* Look for the opening `<'. */
3245 cp_parser_require (parser
, CPP_LESS
, "`<'");
3246 /* Parse the type to which we are casting. */
3247 type
= cp_parser_type_id (parser
);
3248 /* Look for the closing `>'. */
3249 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3250 /* Restore the old message. */
3251 parser
->type_definition_forbidden_message
= saved_message
;
3253 /* And the expression which is being cast. */
3254 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3255 expression
= cp_parser_expression (parser
);
3256 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3258 /* Only type conversions to integral or enumeration types
3259 can be used in constant-expressions. */
3260 if (parser
->constant_expression_p
3261 && !dependent_type_p (type
)
3262 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3264 if (!parser
->allow_non_constant_expression_p
)
3265 return (cp_parser_non_constant_expression
3266 ("a cast to a type other than an integral or "
3267 "enumeration type"));
3268 parser
->non_constant_expression_p
= true;
3275 = build_dynamic_cast (type
, expression
);
3279 = build_static_cast (type
, expression
);
3283 = build_reinterpret_cast (type
, expression
);
3287 = build_const_cast (type
, expression
);
3298 const char *saved_message
;
3300 /* Consume the `typeid' token. */
3301 cp_lexer_consume_token (parser
->lexer
);
3302 /* Look for the `(' token. */
3303 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3304 /* Types cannot be defined in a `typeid' expression. */
3305 saved_message
= parser
->type_definition_forbidden_message
;
3306 parser
->type_definition_forbidden_message
3307 = "types may not be defined in a `typeid\' expression";
3308 /* We can't be sure yet whether we're looking at a type-id or an
3310 cp_parser_parse_tentatively (parser
);
3311 /* Try a type-id first. */
3312 type
= cp_parser_type_id (parser
);
3313 /* Look for the `)' token. Otherwise, we can't be sure that
3314 we're not looking at an expression: consider `typeid (int
3315 (3))', for example. */
3316 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3317 /* If all went well, simply lookup the type-id. */
3318 if (cp_parser_parse_definitely (parser
))
3319 postfix_expression
= get_typeid (type
);
3320 /* Otherwise, fall back to the expression variant. */
3325 /* Look for an expression. */
3326 expression
= cp_parser_expression (parser
);
3327 /* Compute its typeid. */
3328 postfix_expression
= build_typeid (expression
);
3329 /* Look for the `)' token. */
3330 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3333 /* Restore the saved message. */
3334 parser
->type_definition_forbidden_message
= saved_message
;
3340 bool template_p
= false;
3344 /* Consume the `typename' token. */
3345 cp_lexer_consume_token (parser
->lexer
);
3346 /* Look for the optional `::' operator. */
3347 cp_parser_global_scope_opt (parser
,
3348 /*current_scope_valid_p=*/false);
3349 /* Look for the nested-name-specifier. */
3350 cp_parser_nested_name_specifier (parser
,
3351 /*typename_keyword_p=*/true,
3352 /*check_dependency_p=*/true,
3354 /* Look for the optional `template' keyword. */
3355 template_p
= cp_parser_optional_template_keyword (parser
);
3356 /* We don't know whether we're looking at a template-id or an
3358 cp_parser_parse_tentatively (parser
);
3359 /* Try a template-id. */
3360 id
= cp_parser_template_id (parser
, template_p
,
3361 /*check_dependency_p=*/true);
3362 /* If that didn't work, try an identifier. */
3363 if (!cp_parser_parse_definitely (parser
))
3364 id
= cp_parser_identifier (parser
);
3365 /* Create a TYPENAME_TYPE to represent the type to which the
3366 functional cast is being performed. */
3367 type
= make_typename_type (parser
->scope
, id
,
3370 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3378 /* If the next thing is a simple-type-specifier, we may be
3379 looking at a functional cast. We could also be looking at
3380 an id-expression. So, we try the functional cast, and if
3381 that doesn't work we fall back to the primary-expression. */
3382 cp_parser_parse_tentatively (parser
);
3383 /* Look for the simple-type-specifier. */
3384 type
= cp_parser_simple_type_specifier (parser
,
3385 CP_PARSER_FLAGS_NONE
,
3386 /*identifier_p=*/false);
3387 /* Parse the cast itself. */
3388 if (!cp_parser_error_occurred (parser
))
3390 = cp_parser_functional_cast (parser
, type
);
3391 /* If that worked, we're done. */
3392 if (cp_parser_parse_definitely (parser
))
3395 /* If the functional-cast didn't work out, try a
3396 compound-literal. */
3397 if (cp_parser_allow_gnu_extensions_p (parser
)
3398 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3400 tree initializer_list
= NULL_TREE
;
3402 cp_parser_parse_tentatively (parser
);
3403 /* Consume the `('. */
3404 cp_lexer_consume_token (parser
->lexer
);
3405 /* Parse the type. */
3406 type
= cp_parser_type_id (parser
);
3407 /* Look for the `)'. */
3408 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3409 /* Look for the `{'. */
3410 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3411 /* If things aren't going well, there's no need to
3413 if (!cp_parser_error_occurred (parser
))
3415 bool non_constant_p
;
3416 /* Parse the initializer-list. */
3418 = cp_parser_initializer_list (parser
, &non_constant_p
);
3419 /* Allow a trailing `,'. */
3420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3421 cp_lexer_consume_token (parser
->lexer
);
3422 /* Look for the final `}'. */
3423 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3425 /* If that worked, we're definitely looking at a
3426 compound-literal expression. */
3427 if (cp_parser_parse_definitely (parser
))
3429 /* Warn the user that a compound literal is not
3430 allowed in standard C++. */
3432 pedwarn ("ISO C++ forbids compound-literals");
3433 /* Form the representation of the compound-literal. */
3435 = finish_compound_literal (type
, initializer_list
);
3440 /* It must be a primary-expression. */
3441 postfix_expression
= cp_parser_primary_expression (parser
,
3448 /* If we were avoiding committing to the processing of a
3449 qualified-id until we knew whether or not we had a
3450 pointer-to-member, we now know. */
3451 if (qualifying_class
)
3455 /* Peek at the next token. */
3456 token
= cp_lexer_peek_token (parser
->lexer
);
3457 done
= (token
->type
!= CPP_OPEN_SQUARE
3458 && token
->type
!= CPP_OPEN_PAREN
3459 && token
->type
!= CPP_DOT
3460 && token
->type
!= CPP_DEREF
3461 && token
->type
!= CPP_PLUS_PLUS
3462 && token
->type
!= CPP_MINUS_MINUS
);
3464 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3469 return postfix_expression
;
3472 /* Keep looping until the postfix-expression is complete. */
3475 if (idk
== CP_ID_KIND_UNQUALIFIED
3476 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3477 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3478 /* It is not a Koenig lookup function call. */
3480 = unqualified_name_lookup_error (postfix_expression
);
3482 /* Peek at the next token. */
3483 token
= cp_lexer_peek_token (parser
->lexer
);
3485 switch (token
->type
)
3487 case CPP_OPEN_SQUARE
:
3488 /* postfix-expression [ expression ] */
3492 /* Consume the `[' token. */
3493 cp_lexer_consume_token (parser
->lexer
);
3494 /* Parse the index expression. */
3495 index
= cp_parser_expression (parser
);
3496 /* Look for the closing `]'. */
3497 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
3499 /* Build the ARRAY_REF. */
3501 = grok_array_decl (postfix_expression
, index
);
3502 idk
= CP_ID_KIND_NONE
;
3506 case CPP_OPEN_PAREN
:
3507 /* postfix-expression ( expression-list [opt] ) */
3509 tree args
= (cp_parser_parenthesized_expression_list
3510 (parser
, false, /*non_constant_p=*/NULL
));
3512 if (args
== error_mark_node
)
3514 postfix_expression
= error_mark_node
;
3518 /* Function calls are not permitted in
3519 constant-expressions. */
3520 if (parser
->constant_expression_p
)
3522 if (!parser
->allow_non_constant_expression_p
)
3523 return cp_parser_non_constant_expression ("a function call");
3524 parser
->non_constant_expression_p
= true;
3527 if (idk
== CP_ID_KIND_UNQUALIFIED
)
3530 && (is_overloaded_fn (postfix_expression
)
3531 || DECL_P (postfix_expression
)
3532 || TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
))
3534 = perform_koenig_lookup (postfix_expression
, args
);
3535 else if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3537 = unqualified_fn_lookup_error (postfix_expression
);
3540 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
3542 tree instance
= TREE_OPERAND (postfix_expression
, 0);
3543 tree fn
= TREE_OPERAND (postfix_expression
, 1);
3545 if (processing_template_decl
3546 && (type_dependent_expression_p (instance
)
3547 || (!BASELINK_P (fn
)
3548 && TREE_CODE (fn
) != FIELD_DECL
)
3549 || type_dependent_expression_p (fn
)
3550 || any_type_dependent_arguments_p (args
)))
3553 = build_min_nt (CALL_EXPR
, postfix_expression
, args
);
3558 = (build_new_method_call
3559 (instance
, fn
, args
, NULL_TREE
,
3560 (idk
== CP_ID_KIND_QUALIFIED
3561 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3563 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
3564 || TREE_CODE (postfix_expression
) == MEMBER_REF
3565 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
3566 postfix_expression
= (build_offset_ref_call_from_tree
3567 (postfix_expression
, args
));
3568 else if (idk
== CP_ID_KIND_QUALIFIED
)
3569 /* A call to a static class member, or a namespace-scope
3572 = finish_call_expr (postfix_expression
, args
,
3573 /*disallow_virtual=*/true);
3575 /* All other function calls. */
3577 = finish_call_expr (postfix_expression
, args
,
3578 /*disallow_virtual=*/false);
3580 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3581 idk
= CP_ID_KIND_NONE
;
3587 /* postfix-expression . template [opt] id-expression
3588 postfix-expression . pseudo-destructor-name
3589 postfix-expression -> template [opt] id-expression
3590 postfix-expression -> pseudo-destructor-name */
3595 tree scope
= NULL_TREE
;
3597 /* If this is a `->' operator, dereference the pointer. */
3598 if (token
->type
== CPP_DEREF
)
3599 postfix_expression
= build_x_arrow (postfix_expression
);
3600 /* Check to see whether or not the expression is
3602 dependent_p
= type_dependent_expression_p (postfix_expression
);
3603 /* The identifier following the `->' or `.' is not
3605 parser
->scope
= NULL_TREE
;
3606 parser
->qualifying_scope
= NULL_TREE
;
3607 parser
->object_scope
= NULL_TREE
;
3608 idk
= CP_ID_KIND_NONE
;
3609 /* Enter the scope corresponding to the type of the object
3610 given by the POSTFIX_EXPRESSION. */
3612 && TREE_TYPE (postfix_expression
) != NULL_TREE
)
3614 scope
= TREE_TYPE (postfix_expression
);
3615 /* According to the standard, no expression should
3616 ever have reference type. Unfortunately, we do not
3617 currently match the standard in this respect in
3618 that our internal representation of an expression
3619 may have reference type even when the standard says
3620 it does not. Therefore, we have to manually obtain
3621 the underlying type here. */
3622 scope
= non_reference (scope
);
3623 /* The type of the POSTFIX_EXPRESSION must be
3625 scope
= complete_type_or_else (scope
, NULL_TREE
);
3626 /* Let the name lookup machinery know that we are
3627 processing a class member access expression. */
3628 parser
->context
->object_type
= scope
;
3629 /* If something went wrong, we want to be able to
3630 discern that case, as opposed to the case where
3631 there was no SCOPE due to the type of expression
3634 scope
= error_mark_node
;
3637 /* Consume the `.' or `->' operator. */
3638 cp_lexer_consume_token (parser
->lexer
);
3639 /* If the SCOPE is not a scalar type, we are looking at an
3640 ordinary class member access expression, rather than a
3641 pseudo-destructor-name. */
3642 if (!scope
|| !SCALAR_TYPE_P (scope
))
3644 template_p
= cp_parser_optional_template_keyword (parser
);
3645 /* Parse the id-expression. */
3646 name
= cp_parser_id_expression (parser
,
3648 /*check_dependency_p=*/true,
3649 /*template_p=*/NULL
,
3650 /*declarator_p=*/false);
3651 /* In general, build a SCOPE_REF if the member name is
3652 qualified. However, if the name was not dependent
3653 and has already been resolved; there is no need to
3654 build the SCOPE_REF. For example;
3656 struct X { void f(); };
3657 template <typename T> void f(T* t) { t->X::f(); }
3659 Even though "t" is dependent, "X::f" is not and has
3660 been resolved to a BASELINK; there is no need to
3661 include scope information. */
3663 /* But we do need to remember that there was an explicit
3664 scope for virtual function calls. */
3666 idk
= CP_ID_KIND_QUALIFIED
;
3668 if (name
!= error_mark_node
3669 && !BASELINK_P (name
)
3672 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
3673 parser
->scope
= NULL_TREE
;
3674 parser
->qualifying_scope
= NULL_TREE
;
3675 parser
->object_scope
= NULL_TREE
;
3678 = finish_class_member_access_expr (postfix_expression
, name
);
3680 /* Otherwise, try the pseudo-destructor-name production. */
3686 /* Parse the pseudo-destructor-name. */
3687 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
3688 /* Form the call. */
3690 = finish_pseudo_destructor_expr (postfix_expression
,
3691 s
, TREE_TYPE (type
));
3694 /* We no longer need to look up names in the scope of the
3695 object on the left-hand side of the `.' or `->'
3697 parser
->context
->object_type
= NULL_TREE
;
3702 /* postfix-expression ++ */
3703 /* Consume the `++' token. */
3704 cp_lexer_consume_token (parser
->lexer
);
3705 /* Increments may not appear in constant-expressions. */
3706 if (parser
->constant_expression_p
)
3708 if (!parser
->allow_non_constant_expression_p
)
3709 return cp_parser_non_constant_expression ("an increment");
3710 parser
->non_constant_expression_p
= true;
3712 /* Generate a representation for the complete expression. */
3714 = finish_increment_expr (postfix_expression
,
3715 POSTINCREMENT_EXPR
);
3716 idk
= CP_ID_KIND_NONE
;
3719 case CPP_MINUS_MINUS
:
3720 /* postfix-expression -- */
3721 /* Consume the `--' token. */
3722 cp_lexer_consume_token (parser
->lexer
);
3723 /* Decrements may not appear in constant-expressions. */
3724 if (parser
->constant_expression_p
)
3726 if (!parser
->allow_non_constant_expression_p
)
3727 return cp_parser_non_constant_expression ("a decrement");
3728 parser
->non_constant_expression_p
= true;
3730 /* Generate a representation for the complete expression. */
3732 = finish_increment_expr (postfix_expression
,
3733 POSTDECREMENT_EXPR
);
3734 idk
= CP_ID_KIND_NONE
;
3738 return postfix_expression
;
3742 /* We should never get here. */
3744 return error_mark_node
;
3747 /* Parse a parenthesized expression-list.
3750 assignment-expression
3751 expression-list, assignment-expression
3756 identifier, expression-list
3758 Returns a TREE_LIST. The TREE_VALUE of each node is a
3759 representation of an assignment-expression. Note that a TREE_LIST
3760 is returned even if there is only a single expression in the list.
3761 error_mark_node is returned if the ( and or ) are
3762 missing. NULL_TREE is returned on no expressions. The parentheses
3763 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3764 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3765 indicates whether or not all of the expressions in the list were
3769 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
3770 bool is_attribute_list
,
3771 bool *non_constant_p
)
3773 tree expression_list
= NULL_TREE
;
3774 tree identifier
= NULL_TREE
;
3776 /* Assume all the expressions will be constant. */
3778 *non_constant_p
= false;
3780 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
3781 return error_mark_node
;
3783 /* Consume expressions until there are no more. */
3784 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
3789 /* At the beginning of attribute lists, check to see if the
3790 next token is an identifier. */
3791 if (is_attribute_list
3792 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
3796 /* Consume the identifier. */
3797 token
= cp_lexer_consume_token (parser
->lexer
);
3798 /* Save the identifier. */
3799 identifier
= token
->value
;
3803 /* Parse the next assignment-expression. */
3806 bool expr_non_constant_p
;
3807 expr
= (cp_parser_constant_expression
3808 (parser
, /*allow_non_constant_p=*/true,
3809 &expr_non_constant_p
));
3810 if (expr_non_constant_p
)
3811 *non_constant_p
= true;
3814 expr
= cp_parser_assignment_expression (parser
);
3816 /* Add it to the list. We add error_mark_node
3817 expressions to the list, so that we can still tell if
3818 the correct form for a parenthesized expression-list
3819 is found. That gives better errors. */
3820 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
3822 if (expr
== error_mark_node
)
3826 /* After the first item, attribute lists look the same as
3827 expression lists. */
3828 is_attribute_list
= false;
3831 /* If the next token isn't a `,', then we are done. */
3832 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
3835 /* Otherwise, consume the `,' and keep going. */
3836 cp_lexer_consume_token (parser
->lexer
);
3839 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3844 /* We try and resync to an unnested comma, as that will give the
3845 user better diagnostics. */
3846 ending
= cp_parser_skip_to_closing_parenthesis (parser
, true, true);
3850 return error_mark_node
;
3853 /* We built up the list in reverse order so we must reverse it now. */
3854 expression_list
= nreverse (expression_list
);
3856 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
3858 return expression_list
;
3861 /* Parse a pseudo-destructor-name.
3863 pseudo-destructor-name:
3864 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3865 :: [opt] nested-name-specifier template template-id :: ~ type-name
3866 :: [opt] nested-name-specifier [opt] ~ type-name
3868 If either of the first two productions is used, sets *SCOPE to the
3869 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3870 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3871 or ERROR_MARK_NODE if no type-name is present. */
3874 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
3878 bool nested_name_specifier_p
;
3880 /* Look for the optional `::' operator. */
3881 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
3882 /* Look for the optional nested-name-specifier. */
3883 nested_name_specifier_p
3884 = (cp_parser_nested_name_specifier_opt (parser
,
3885 /*typename_keyword_p=*/false,
3886 /*check_dependency_p=*/true,
3889 /* Now, if we saw a nested-name-specifier, we might be doing the
3890 second production. */
3891 if (nested_name_specifier_p
3892 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
3894 /* Consume the `template' keyword. */
3895 cp_lexer_consume_token (parser
->lexer
);
3896 /* Parse the template-id. */
3897 cp_parser_template_id (parser
,
3898 /*template_keyword_p=*/true,
3899 /*check_dependency_p=*/false);
3900 /* Look for the `::' token. */
3901 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3903 /* If the next token is not a `~', then there might be some
3904 additional qualification. */
3905 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
3907 /* Look for the type-name. */
3908 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
3909 /* Look for the `::' token. */
3910 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3915 /* Look for the `~'. */
3916 cp_parser_require (parser
, CPP_COMPL
, "`~'");
3917 /* Look for the type-name again. We are not responsible for
3918 checking that it matches the first type-name. */
3919 *type
= cp_parser_type_name (parser
);
3922 /* Parse a unary-expression.
3928 unary-operator cast-expression
3929 sizeof unary-expression
3937 __extension__ cast-expression
3938 __alignof__ unary-expression
3939 __alignof__ ( type-id )
3940 __real__ cast-expression
3941 __imag__ cast-expression
3944 ADDRESS_P is true iff the unary-expression is appearing as the
3945 operand of the `&' operator.
3947 Returns a representation of the expression. */
3950 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
3953 enum tree_code unary_operator
;
3955 /* Peek at the next token. */
3956 token
= cp_lexer_peek_token (parser
->lexer
);
3957 /* Some keywords give away the kind of expression. */
3958 if (token
->type
== CPP_KEYWORD
)
3960 enum rid keyword
= token
->keyword
;
3966 /* Consume the `alignof' token. */
3967 cp_lexer_consume_token (parser
->lexer
);
3968 /* Parse the operand. */
3969 return finish_alignof (cp_parser_sizeof_operand
3977 /* Consume the `sizeof' token. */
3978 cp_lexer_consume_token (parser
->lexer
);
3979 /* Parse the operand. */
3980 operand
= cp_parser_sizeof_operand (parser
, keyword
);
3982 /* If the type of the operand cannot be determined build a
3984 if (TYPE_P (operand
)
3985 ? dependent_type_p (operand
)
3986 : type_dependent_expression_p (operand
))
3987 return build_min (SIZEOF_EXPR
, size_type_node
, operand
);
3988 /* Otherwise, compute the constant value. */
3990 return finish_sizeof (operand
);
3994 return cp_parser_new_expression (parser
);
3997 return cp_parser_delete_expression (parser
);
4001 /* The saved value of the PEDANTIC flag. */
4005 /* Save away the PEDANTIC flag. */
4006 cp_parser_extension_opt (parser
, &saved_pedantic
);
4007 /* Parse the cast-expression. */
4008 expr
= cp_parser_simple_cast_expression (parser
);
4009 /* Restore the PEDANTIC flag. */
4010 pedantic
= saved_pedantic
;
4020 /* Consume the `__real__' or `__imag__' token. */
4021 cp_lexer_consume_token (parser
->lexer
);
4022 /* Parse the cast-expression. */
4023 expression
= cp_parser_simple_cast_expression (parser
);
4024 /* Create the complete representation. */
4025 return build_x_unary_op ((keyword
== RID_REALPART
4026 ? REALPART_EXPR
: IMAGPART_EXPR
),
4036 /* Look for the `:: new' and `:: delete', which also signal the
4037 beginning of a new-expression, or delete-expression,
4038 respectively. If the next token is `::', then it might be one of
4040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4044 /* See if the token after the `::' is one of the keywords in
4045 which we're interested. */
4046 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4047 /* If it's `new', we have a new-expression. */
4048 if (keyword
== RID_NEW
)
4049 return cp_parser_new_expression (parser
);
4050 /* Similarly, for `delete'. */
4051 else if (keyword
== RID_DELETE
)
4052 return cp_parser_delete_expression (parser
);
4055 /* Look for a unary operator. */
4056 unary_operator
= cp_parser_unary_operator (token
);
4057 /* The `++' and `--' operators can be handled similarly, even though
4058 they are not technically unary-operators in the grammar. */
4059 if (unary_operator
== ERROR_MARK
)
4061 if (token
->type
== CPP_PLUS_PLUS
)
4062 unary_operator
= PREINCREMENT_EXPR
;
4063 else if (token
->type
== CPP_MINUS_MINUS
)
4064 unary_operator
= PREDECREMENT_EXPR
;
4065 /* Handle the GNU address-of-label extension. */
4066 else if (cp_parser_allow_gnu_extensions_p (parser
)
4067 && token
->type
== CPP_AND_AND
)
4071 /* Consume the '&&' token. */
4072 cp_lexer_consume_token (parser
->lexer
);
4073 /* Look for the identifier. */
4074 identifier
= cp_parser_identifier (parser
);
4075 /* Create an expression representing the address. */
4076 return finish_label_address_expr (identifier
);
4079 if (unary_operator
!= ERROR_MARK
)
4081 tree cast_expression
;
4083 /* Consume the operator token. */
4084 token
= cp_lexer_consume_token (parser
->lexer
);
4085 /* Parse the cast-expression. */
4087 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4088 /* Now, build an appropriate representation. */
4089 switch (unary_operator
)
4092 return build_x_indirect_ref (cast_expression
, "unary *");
4096 return build_x_unary_op (unary_operator
, cast_expression
);
4098 case PREINCREMENT_EXPR
:
4099 case PREDECREMENT_EXPR
:
4100 if (parser
->constant_expression_p
)
4102 if (!parser
->allow_non_constant_expression_p
)
4103 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4106 parser
->non_constant_expression_p
= true;
4111 case TRUTH_NOT_EXPR
:
4112 return finish_unary_op_expr (unary_operator
, cast_expression
);
4116 return error_mark_node
;
4120 return cp_parser_postfix_expression (parser
, address_p
);
4123 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4124 unary-operator, the corresponding tree code is returned. */
4126 static enum tree_code
4127 cp_parser_unary_operator (cp_token
* token
)
4129 switch (token
->type
)
4132 return INDIRECT_REF
;
4138 return CONVERT_EXPR
;
4144 return TRUTH_NOT_EXPR
;
4147 return BIT_NOT_EXPR
;
4154 /* Parse a new-expression.
4157 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4158 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4160 Returns a representation of the expression. */
4163 cp_parser_new_expression (cp_parser
* parser
)
4165 bool global_scope_p
;
4170 /* Look for the optional `::' operator. */
4172 = (cp_parser_global_scope_opt (parser
,
4173 /*current_scope_valid_p=*/false)
4175 /* Look for the `new' operator. */
4176 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4177 /* There's no easy way to tell a new-placement from the
4178 `( type-id )' construct. */
4179 cp_parser_parse_tentatively (parser
);
4180 /* Look for a new-placement. */
4181 placement
= cp_parser_new_placement (parser
);
4182 /* If that didn't work out, there's no new-placement. */
4183 if (!cp_parser_parse_definitely (parser
))
4184 placement
= NULL_TREE
;
4186 /* If the next token is a `(', then we have a parenthesized
4188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4190 /* Consume the `('. */
4191 cp_lexer_consume_token (parser
->lexer
);
4192 /* Parse the type-id. */
4193 type
= cp_parser_type_id (parser
);
4194 /* Look for the closing `)'. */
4195 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4197 /* Otherwise, there must be a new-type-id. */
4199 type
= cp_parser_new_type_id (parser
);
4201 /* If the next token is a `(', then we have a new-initializer. */
4202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4203 initializer
= cp_parser_new_initializer (parser
);
4205 initializer
= NULL_TREE
;
4207 /* Create a representation of the new-expression. */
4208 return build_new (placement
, type
, initializer
, global_scope_p
);
4211 /* Parse a new-placement.
4216 Returns the same representation as for an expression-list. */
4219 cp_parser_new_placement (cp_parser
* parser
)
4221 tree expression_list
;
4223 /* Parse the expression-list. */
4224 expression_list
= (cp_parser_parenthesized_expression_list
4225 (parser
, false, /*non_constant_p=*/NULL
));
4227 return expression_list
;
4230 /* Parse a new-type-id.
4233 type-specifier-seq new-declarator [opt]
4235 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4236 and whose TREE_VALUE is the new-declarator. */
4239 cp_parser_new_type_id (cp_parser
* parser
)
4241 tree type_specifier_seq
;
4243 const char *saved_message
;
4245 /* The type-specifier sequence must not contain type definitions.
4246 (It cannot contain declarations of new types either, but if they
4247 are not definitions we will catch that because they are not
4249 saved_message
= parser
->type_definition_forbidden_message
;
4250 parser
->type_definition_forbidden_message
4251 = "types may not be defined in a new-type-id";
4252 /* Parse the type-specifier-seq. */
4253 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4254 /* Restore the old message. */
4255 parser
->type_definition_forbidden_message
= saved_message
;
4256 /* Parse the new-declarator. */
4257 declarator
= cp_parser_new_declarator_opt (parser
);
4259 return build_tree_list (type_specifier_seq
, declarator
);
4262 /* Parse an (optional) new-declarator.
4265 ptr-operator new-declarator [opt]
4266 direct-new-declarator
4268 Returns a representation of the declarator. See
4269 cp_parser_declarator for the representations used. */
4272 cp_parser_new_declarator_opt (cp_parser
* parser
)
4274 enum tree_code code
;
4276 tree cv_qualifier_seq
;
4278 /* We don't know if there's a ptr-operator next, or not. */
4279 cp_parser_parse_tentatively (parser
);
4280 /* Look for a ptr-operator. */
4281 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4282 /* If that worked, look for more new-declarators. */
4283 if (cp_parser_parse_definitely (parser
))
4287 /* Parse another optional declarator. */
4288 declarator
= cp_parser_new_declarator_opt (parser
);
4290 /* Create the representation of the declarator. */
4291 if (code
== INDIRECT_REF
)
4292 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4295 declarator
= make_reference_declarator (cv_qualifier_seq
,
4298 /* Handle the pointer-to-member case. */
4300 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4305 /* If the next token is a `[', there is a direct-new-declarator. */
4306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4307 return cp_parser_direct_new_declarator (parser
);
4312 /* Parse a direct-new-declarator.
4314 direct-new-declarator:
4316 direct-new-declarator [constant-expression]
4318 Returns an ARRAY_REF, following the same conventions as are
4319 documented for cp_parser_direct_declarator. */
4322 cp_parser_direct_new_declarator (cp_parser
* parser
)
4324 tree declarator
= NULL_TREE
;
4330 /* Look for the opening `['. */
4331 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4332 /* The first expression is not required to be constant. */
4335 expression
= cp_parser_expression (parser
);
4336 /* The standard requires that the expression have integral
4337 type. DR 74 adds enumeration types. We believe that the
4338 real intent is that these expressions be handled like the
4339 expression in a `switch' condition, which also allows
4340 classes with a single conversion to integral or
4341 enumeration type. */
4342 if (!processing_template_decl
)
4345 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4350 error ("expression in new-declarator must have integral or enumeration type");
4351 expression
= error_mark_node
;
4355 /* But all the other expressions must be. */
4358 = cp_parser_constant_expression (parser
,
4359 /*allow_non_constant=*/false,
4361 /* Look for the closing `]'. */
4362 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4364 /* Add this bound to the declarator. */
4365 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4367 /* If the next token is not a `[', then there are no more
4369 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4376 /* Parse a new-initializer.
4379 ( expression-list [opt] )
4381 Returns a representation of the expression-list. If there is no
4382 expression-list, VOID_ZERO_NODE is returned. */
4385 cp_parser_new_initializer (cp_parser
* parser
)
4387 tree expression_list
;
4389 expression_list
= (cp_parser_parenthesized_expression_list
4390 (parser
, false, /*non_constant_p=*/NULL
));
4391 if (!expression_list
)
4392 expression_list
= void_zero_node
;
4394 return expression_list
;
4397 /* Parse a delete-expression.
4400 :: [opt] delete cast-expression
4401 :: [opt] delete [ ] cast-expression
4403 Returns a representation of the expression. */
4406 cp_parser_delete_expression (cp_parser
* parser
)
4408 bool global_scope_p
;
4412 /* Look for the optional `::' operator. */
4414 = (cp_parser_global_scope_opt (parser
,
4415 /*current_scope_valid_p=*/false)
4417 /* Look for the `delete' keyword. */
4418 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4419 /* See if the array syntax is in use. */
4420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4422 /* Consume the `[' token. */
4423 cp_lexer_consume_token (parser
->lexer
);
4424 /* Look for the `]' token. */
4425 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4426 /* Remember that this is the `[]' construct. */
4432 /* Parse the cast-expression. */
4433 expression
= cp_parser_simple_cast_expression (parser
);
4435 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4438 /* Parse a cast-expression.
4442 ( type-id ) cast-expression
4444 Returns a representation of the expression. */
4447 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4449 /* If it's a `(', then we might be looking at a cast. */
4450 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4452 tree type
= NULL_TREE
;
4453 tree expr
= NULL_TREE
;
4454 bool compound_literal_p
;
4455 const char *saved_message
;
4457 /* There's no way to know yet whether or not this is a cast.
4458 For example, `(int (3))' is a unary-expression, while `(int)
4459 3' is a cast. So, we resort to parsing tentatively. */
4460 cp_parser_parse_tentatively (parser
);
4461 /* Types may not be defined in a cast. */
4462 saved_message
= parser
->type_definition_forbidden_message
;
4463 parser
->type_definition_forbidden_message
4464 = "types may not be defined in casts";
4465 /* Consume the `('. */
4466 cp_lexer_consume_token (parser
->lexer
);
4467 /* A very tricky bit is that `(struct S) { 3 }' is a
4468 compound-literal (which we permit in C++ as an extension).
4469 But, that construct is not a cast-expression -- it is a
4470 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4471 is legal; if the compound-literal were a cast-expression,
4472 you'd need an extra set of parentheses.) But, if we parse
4473 the type-id, and it happens to be a class-specifier, then we
4474 will commit to the parse at that point, because we cannot
4475 undo the action that is done when creating a new class. So,
4476 then we cannot back up and do a postfix-expression.
4478 Therefore, we scan ahead to the closing `)', and check to see
4479 if the token after the `)' is a `{'. If so, we are not
4480 looking at a cast-expression.
4482 Save tokens so that we can put them back. */
4483 cp_lexer_save_tokens (parser
->lexer
);
4484 /* Skip tokens until the next token is a closing parenthesis.
4485 If we find the closing `)', and the next token is a `{', then
4486 we are looking at a compound-literal. */
4488 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false)
4489 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4490 /* Roll back the tokens we skipped. */
4491 cp_lexer_rollback_tokens (parser
->lexer
);
4492 /* If we were looking at a compound-literal, simulate an error
4493 so that the call to cp_parser_parse_definitely below will
4495 if (compound_literal_p
)
4496 cp_parser_simulate_error (parser
);
4499 /* Look for the type-id. */
4500 type
= cp_parser_type_id (parser
);
4501 /* Look for the closing `)'. */
4502 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4505 /* Restore the saved message. */
4506 parser
->type_definition_forbidden_message
= saved_message
;
4508 /* If ok so far, parse the dependent expression. We cannot be
4509 sure it is a cast. Consider `(T ())'. It is a parenthesized
4510 ctor of T, but looks like a cast to function returning T
4511 without a dependent expression. */
4512 if (!cp_parser_error_occurred (parser
))
4513 expr
= cp_parser_simple_cast_expression (parser
);
4515 if (cp_parser_parse_definitely (parser
))
4517 /* Warn about old-style casts, if so requested. */
4518 if (warn_old_style_cast
4519 && !in_system_header
4520 && !VOID_TYPE_P (type
)
4521 && current_lang_name
!= lang_name_c
)
4522 warning ("use of old-style cast");
4524 /* Only type conversions to integral or enumeration types
4525 can be used in constant-expressions. */
4526 if (parser
->constant_expression_p
4527 && !dependent_type_p (type
)
4528 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4530 if (!parser
->allow_non_constant_expression_p
)
4531 return (cp_parser_non_constant_expression
4532 ("a casts to a type other than an integral or "
4533 "enumeration type"));
4534 parser
->non_constant_expression_p
= true;
4536 /* Perform the cast. */
4537 expr
= build_c_cast (type
, expr
);
4542 /* If we get here, then it's not a cast, so it must be a
4543 unary-expression. */
4544 return cp_parser_unary_expression (parser
, address_p
);
4547 /* Parse a pm-expression.
4551 pm-expression .* cast-expression
4552 pm-expression ->* cast-expression
4554 Returns a representation of the expression. */
4557 cp_parser_pm_expression (cp_parser
* parser
)
4559 static const cp_parser_token_tree_map map
= {
4560 { CPP_DEREF_STAR
, MEMBER_REF
},
4561 { CPP_DOT_STAR
, DOTSTAR_EXPR
},
4562 { CPP_EOF
, ERROR_MARK
}
4565 return cp_parser_binary_expression (parser
, map
,
4566 cp_parser_simple_cast_expression
);
4569 /* Parse a multiplicative-expression.
4571 mulitplicative-expression:
4573 multiplicative-expression * pm-expression
4574 multiplicative-expression / pm-expression
4575 multiplicative-expression % pm-expression
4577 Returns a representation of the expression. */
4580 cp_parser_multiplicative_expression (cp_parser
* parser
)
4582 static const cp_parser_token_tree_map map
= {
4583 { CPP_MULT
, MULT_EXPR
},
4584 { CPP_DIV
, TRUNC_DIV_EXPR
},
4585 { CPP_MOD
, TRUNC_MOD_EXPR
},
4586 { CPP_EOF
, ERROR_MARK
}
4589 return cp_parser_binary_expression (parser
,
4591 cp_parser_pm_expression
);
4594 /* Parse an additive-expression.
4596 additive-expression:
4597 multiplicative-expression
4598 additive-expression + multiplicative-expression
4599 additive-expression - multiplicative-expression
4601 Returns a representation of the expression. */
4604 cp_parser_additive_expression (cp_parser
* parser
)
4606 static const cp_parser_token_tree_map map
= {
4607 { CPP_PLUS
, PLUS_EXPR
},
4608 { CPP_MINUS
, MINUS_EXPR
},
4609 { CPP_EOF
, ERROR_MARK
}
4612 return cp_parser_binary_expression (parser
,
4614 cp_parser_multiplicative_expression
);
4617 /* Parse a shift-expression.
4621 shift-expression << additive-expression
4622 shift-expression >> additive-expression
4624 Returns a representation of the expression. */
4627 cp_parser_shift_expression (cp_parser
* parser
)
4629 static const cp_parser_token_tree_map map
= {
4630 { CPP_LSHIFT
, LSHIFT_EXPR
},
4631 { CPP_RSHIFT
, RSHIFT_EXPR
},
4632 { CPP_EOF
, ERROR_MARK
}
4635 return cp_parser_binary_expression (parser
,
4637 cp_parser_additive_expression
);
4640 /* Parse a relational-expression.
4642 relational-expression:
4644 relational-expression < shift-expression
4645 relational-expression > shift-expression
4646 relational-expression <= shift-expression
4647 relational-expression >= shift-expression
4651 relational-expression:
4652 relational-expression <? shift-expression
4653 relational-expression >? shift-expression
4655 Returns a representation of the expression. */
4658 cp_parser_relational_expression (cp_parser
* parser
)
4660 static const cp_parser_token_tree_map map
= {
4661 { CPP_LESS
, LT_EXPR
},
4662 { CPP_GREATER
, GT_EXPR
},
4663 { CPP_LESS_EQ
, LE_EXPR
},
4664 { CPP_GREATER_EQ
, GE_EXPR
},
4665 { CPP_MIN
, MIN_EXPR
},
4666 { CPP_MAX
, MAX_EXPR
},
4667 { CPP_EOF
, ERROR_MARK
}
4670 return cp_parser_binary_expression (parser
,
4672 cp_parser_shift_expression
);
4675 /* Parse an equality-expression.
4677 equality-expression:
4678 relational-expression
4679 equality-expression == relational-expression
4680 equality-expression != relational-expression
4682 Returns a representation of the expression. */
4685 cp_parser_equality_expression (cp_parser
* parser
)
4687 static const cp_parser_token_tree_map map
= {
4688 { CPP_EQ_EQ
, EQ_EXPR
},
4689 { CPP_NOT_EQ
, NE_EXPR
},
4690 { CPP_EOF
, ERROR_MARK
}
4693 return cp_parser_binary_expression (parser
,
4695 cp_parser_relational_expression
);
4698 /* Parse an and-expression.
4702 and-expression & equality-expression
4704 Returns a representation of the expression. */
4707 cp_parser_and_expression (cp_parser
* parser
)
4709 static const cp_parser_token_tree_map map
= {
4710 { CPP_AND
, BIT_AND_EXPR
},
4711 { CPP_EOF
, ERROR_MARK
}
4714 return cp_parser_binary_expression (parser
,
4716 cp_parser_equality_expression
);
4719 /* Parse an exclusive-or-expression.
4721 exclusive-or-expression:
4723 exclusive-or-expression ^ and-expression
4725 Returns a representation of the expression. */
4728 cp_parser_exclusive_or_expression (cp_parser
* parser
)
4730 static const cp_parser_token_tree_map map
= {
4731 { CPP_XOR
, BIT_XOR_EXPR
},
4732 { CPP_EOF
, ERROR_MARK
}
4735 return cp_parser_binary_expression (parser
,
4737 cp_parser_and_expression
);
4741 /* Parse an inclusive-or-expression.
4743 inclusive-or-expression:
4744 exclusive-or-expression
4745 inclusive-or-expression | exclusive-or-expression
4747 Returns a representation of the expression. */
4750 cp_parser_inclusive_or_expression (cp_parser
* parser
)
4752 static const cp_parser_token_tree_map map
= {
4753 { CPP_OR
, BIT_IOR_EXPR
},
4754 { CPP_EOF
, ERROR_MARK
}
4757 return cp_parser_binary_expression (parser
,
4759 cp_parser_exclusive_or_expression
);
4762 /* Parse a logical-and-expression.
4764 logical-and-expression:
4765 inclusive-or-expression
4766 logical-and-expression && inclusive-or-expression
4768 Returns a representation of the expression. */
4771 cp_parser_logical_and_expression (cp_parser
* parser
)
4773 static const cp_parser_token_tree_map map
= {
4774 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
4775 { CPP_EOF
, ERROR_MARK
}
4778 return cp_parser_binary_expression (parser
,
4780 cp_parser_inclusive_or_expression
);
4783 /* Parse a logical-or-expression.
4785 logical-or-expression:
4786 logical-and-expression
4787 logical-or-expression || logical-and-expression
4789 Returns a representation of the expression. */
4792 cp_parser_logical_or_expression (cp_parser
* parser
)
4794 static const cp_parser_token_tree_map map
= {
4795 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
4796 { CPP_EOF
, ERROR_MARK
}
4799 return cp_parser_binary_expression (parser
,
4801 cp_parser_logical_and_expression
);
4804 /* Parse the `? expression : assignment-expression' part of a
4805 conditional-expression. The LOGICAL_OR_EXPR is the
4806 logical-or-expression that started the conditional-expression.
4807 Returns a representation of the entire conditional-expression.
4809 This routine is used by cp_parser_assignment_expression.
4811 ? expression : assignment-expression
4815 ? : assignment-expression */
4818 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
4821 tree assignment_expr
;
4823 /* Consume the `?' token. */
4824 cp_lexer_consume_token (parser
->lexer
);
4825 if (cp_parser_allow_gnu_extensions_p (parser
)
4826 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
4827 /* Implicit true clause. */
4830 /* Parse the expression. */
4831 expr
= cp_parser_expression (parser
);
4833 /* The next token should be a `:'. */
4834 cp_parser_require (parser
, CPP_COLON
, "`:'");
4835 /* Parse the assignment-expression. */
4836 assignment_expr
= cp_parser_assignment_expression (parser
);
4838 /* Build the conditional-expression. */
4839 return build_x_conditional_expr (logical_or_expr
,
4844 /* Parse an assignment-expression.
4846 assignment-expression:
4847 conditional-expression
4848 logical-or-expression assignment-operator assignment_expression
4851 Returns a representation for the expression. */
4854 cp_parser_assignment_expression (cp_parser
* parser
)
4858 /* If the next token is the `throw' keyword, then we're looking at
4859 a throw-expression. */
4860 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
4861 expr
= cp_parser_throw_expression (parser
);
4862 /* Otherwise, it must be that we are looking at a
4863 logical-or-expression. */
4866 /* Parse the logical-or-expression. */
4867 expr
= cp_parser_logical_or_expression (parser
);
4868 /* If the next token is a `?' then we're actually looking at a
4869 conditional-expression. */
4870 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
4871 return cp_parser_question_colon_clause (parser
, expr
);
4874 enum tree_code assignment_operator
;
4876 /* If it's an assignment-operator, we're using the second
4879 = cp_parser_assignment_operator_opt (parser
);
4880 if (assignment_operator
!= ERROR_MARK
)
4884 /* Parse the right-hand side of the assignment. */
4885 rhs
= cp_parser_assignment_expression (parser
);
4886 /* An assignment may not appear in a
4887 constant-expression. */
4888 if (parser
->constant_expression_p
)
4890 if (!parser
->allow_non_constant_expression_p
)
4891 return cp_parser_non_constant_expression ("an assignment");
4892 parser
->non_constant_expression_p
= true;
4894 /* Build the assignment expression. */
4895 expr
= build_x_modify_expr (expr
,
4896 assignment_operator
,
4905 /* Parse an (optional) assignment-operator.
4907 assignment-operator: one of
4908 = *= /= %= += -= >>= <<= &= ^= |=
4912 assignment-operator: one of
4915 If the next token is an assignment operator, the corresponding tree
4916 code is returned, and the token is consumed. For example, for
4917 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4918 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4919 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4920 operator, ERROR_MARK is returned. */
4922 static enum tree_code
4923 cp_parser_assignment_operator_opt (cp_parser
* parser
)
4928 /* Peek at the next toen. */
4929 token
= cp_lexer_peek_token (parser
->lexer
);
4931 switch (token
->type
)
4942 op
= TRUNC_DIV_EXPR
;
4946 op
= TRUNC_MOD_EXPR
;
4986 /* Nothing else is an assignment operator. */
4990 /* If it was an assignment operator, consume it. */
4991 if (op
!= ERROR_MARK
)
4992 cp_lexer_consume_token (parser
->lexer
);
4997 /* Parse an expression.
5000 assignment-expression
5001 expression , assignment-expression
5003 Returns a representation of the expression. */
5006 cp_parser_expression (cp_parser
* parser
)
5008 tree expression
= NULL_TREE
;
5012 tree assignment_expression
;
5014 /* Parse the next assignment-expression. */
5015 assignment_expression
5016 = cp_parser_assignment_expression (parser
);
5017 /* If this is the first assignment-expression, we can just
5020 expression
= assignment_expression
;
5022 expression
= build_x_compound_expr (expression
,
5023 assignment_expression
);
5024 /* If the next token is not a comma, then we are done with the
5026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5028 /* Consume the `,'. */
5029 cp_lexer_consume_token (parser
->lexer
);
5030 /* A comma operator cannot appear in a constant-expression. */
5031 if (parser
->constant_expression_p
)
5033 if (!parser
->allow_non_constant_expression_p
)
5035 = cp_parser_non_constant_expression ("a comma operator");
5036 parser
->non_constant_expression_p
= true;
5043 /* Parse a constant-expression.
5045 constant-expression:
5046 conditional-expression
5048 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5049 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5050 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5051 is false, NON_CONSTANT_P should be NULL. */
5054 cp_parser_constant_expression (cp_parser
* parser
,
5055 bool allow_non_constant_p
,
5056 bool *non_constant_p
)
5058 bool saved_constant_expression_p
;
5059 bool saved_allow_non_constant_expression_p
;
5060 bool saved_non_constant_expression_p
;
5063 /* It might seem that we could simply parse the
5064 conditional-expression, and then check to see if it were
5065 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5066 one that the compiler can figure out is constant, possibly after
5067 doing some simplifications or optimizations. The standard has a
5068 precise definition of constant-expression, and we must honor
5069 that, even though it is somewhat more restrictive.
5075 is not a legal declaration, because `(2, 3)' is not a
5076 constant-expression. The `,' operator is forbidden in a
5077 constant-expression. However, GCC's constant-folding machinery
5078 will fold this operation to an INTEGER_CST for `3'. */
5080 /* Save the old settings. */
5081 saved_constant_expression_p
= parser
->constant_expression_p
;
5082 saved_allow_non_constant_expression_p
5083 = parser
->allow_non_constant_expression_p
;
5084 saved_non_constant_expression_p
= parser
->non_constant_expression_p
;
5085 /* We are now parsing a constant-expression. */
5086 parser
->constant_expression_p
= true;
5087 parser
->allow_non_constant_expression_p
= allow_non_constant_p
;
5088 parser
->non_constant_expression_p
= false;
5089 /* Although the grammar says "conditional-expression", we parse an
5090 "assignment-expression", which also permits "throw-expression"
5091 and the use of assignment operators. In the case that
5092 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5093 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5094 actually essential that we look for an assignment-expression.
5095 For example, cp_parser_initializer_clauses uses this function to
5096 determine whether a particular assignment-expression is in fact
5098 expression
= cp_parser_assignment_expression (parser
);
5099 /* Restore the old settings. */
5100 parser
->constant_expression_p
= saved_constant_expression_p
;
5101 parser
->allow_non_constant_expression_p
5102 = saved_allow_non_constant_expression_p
;
5103 if (allow_non_constant_p
)
5104 *non_constant_p
= parser
->non_constant_expression_p
;
5105 parser
->non_constant_expression_p
= saved_non_constant_expression_p
;
5110 /* Statements [gram.stmt.stmt] */
5112 /* Parse a statement.
5116 expression-statement
5121 declaration-statement
5125 cp_parser_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5129 int statement_line_number
;
5131 /* There is no statement yet. */
5132 statement
= NULL_TREE
;
5133 /* Peek at the next token. */
5134 token
= cp_lexer_peek_token (parser
->lexer
);
5135 /* Remember the line number of the first token in the statement. */
5136 statement_line_number
= token
->location
.line
;
5137 /* If this is a keyword, then that will often determine what kind of
5138 statement we have. */
5139 if (token
->type
== CPP_KEYWORD
)
5141 enum rid keyword
= token
->keyword
;
5147 statement
= cp_parser_labeled_statement (parser
,
5148 in_statement_expr_p
);
5153 statement
= cp_parser_selection_statement (parser
);
5159 statement
= cp_parser_iteration_statement (parser
);
5166 statement
= cp_parser_jump_statement (parser
);
5170 statement
= cp_parser_try_block (parser
);
5174 /* It might be a keyword like `int' that can start a
5175 declaration-statement. */
5179 else if (token
->type
== CPP_NAME
)
5181 /* If the next token is a `:', then we are looking at a
5182 labeled-statement. */
5183 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5184 if (token
->type
== CPP_COLON
)
5185 statement
= cp_parser_labeled_statement (parser
, in_statement_expr_p
);
5187 /* Anything that starts with a `{' must be a compound-statement. */
5188 else if (token
->type
== CPP_OPEN_BRACE
)
5189 statement
= cp_parser_compound_statement (parser
, false);
5191 /* Everything else must be a declaration-statement or an
5192 expression-statement. Try for the declaration-statement
5193 first, unless we are looking at a `;', in which case we know that
5194 we have an expression-statement. */
5197 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5199 cp_parser_parse_tentatively (parser
);
5200 /* Try to parse the declaration-statement. */
5201 cp_parser_declaration_statement (parser
);
5202 /* If that worked, we're done. */
5203 if (cp_parser_parse_definitely (parser
))
5206 /* Look for an expression-statement instead. */
5207 statement
= cp_parser_expression_statement (parser
, in_statement_expr_p
);
5210 /* Set the line number for the statement. */
5211 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5212 STMT_LINENO (statement
) = statement_line_number
;
5215 /* Parse a labeled-statement.
5218 identifier : statement
5219 case constant-expression : statement
5222 Returns the new CASE_LABEL, for a `case' or `default' label. For
5223 an ordinary label, returns a LABEL_STMT. */
5226 cp_parser_labeled_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5229 tree statement
= NULL_TREE
;
5231 /* The next token should be an identifier. */
5232 token
= cp_lexer_peek_token (parser
->lexer
);
5233 if (token
->type
!= CPP_NAME
5234 && token
->type
!= CPP_KEYWORD
)
5236 cp_parser_error (parser
, "expected labeled-statement");
5237 return error_mark_node
;
5240 switch (token
->keyword
)
5246 /* Consume the `case' token. */
5247 cp_lexer_consume_token (parser
->lexer
);
5248 /* Parse the constant-expression. */
5249 expr
= cp_parser_constant_expression (parser
,
5250 /*allow_non_constant_p=*/false,
5252 /* Create the label. */
5253 statement
= finish_case_label (expr
, NULL_TREE
);
5258 /* Consume the `default' token. */
5259 cp_lexer_consume_token (parser
->lexer
);
5260 /* Create the label. */
5261 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5265 /* Anything else must be an ordinary label. */
5266 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5270 /* Require the `:' token. */
5271 cp_parser_require (parser
, CPP_COLON
, "`:'");
5272 /* Parse the labeled statement. */
5273 cp_parser_statement (parser
, in_statement_expr_p
);
5275 /* Return the label, in the case of a `case' or `default' label. */
5279 /* Parse an expression-statement.
5281 expression-statement:
5284 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5285 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5286 indicates whether this expression-statement is part of an
5287 expression statement. */
5290 cp_parser_expression_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5292 tree statement
= NULL_TREE
;
5294 /* If the next token is a ';', then there is no expression
5296 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5297 statement
= cp_parser_expression (parser
);
5299 /* Consume the final `;'. */
5300 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5302 if (in_statement_expr_p
5303 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
5305 /* This is the final expression statement of a statement
5307 statement
= finish_stmt_expr_expr (statement
);
5310 statement
= finish_expr_stmt (statement
);
5317 /* Parse a compound-statement.
5320 { statement-seq [opt] }
5322 Returns a COMPOUND_STMT representing the statement. */
5325 cp_parser_compound_statement (cp_parser
*parser
, bool in_statement_expr_p
)
5329 /* Consume the `{'. */
5330 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5331 return error_mark_node
;
5332 /* Begin the compound-statement. */
5333 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/false);
5334 /* Parse an (optional) statement-seq. */
5335 cp_parser_statement_seq_opt (parser
, in_statement_expr_p
);
5336 /* Finish the compound-statement. */
5337 finish_compound_stmt (compound_stmt
);
5338 /* Consume the `}'. */
5339 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5341 return compound_stmt
;
5344 /* Parse an (optional) statement-seq.
5348 statement-seq [opt] statement */
5351 cp_parser_statement_seq_opt (cp_parser
* parser
, bool in_statement_expr_p
)
5353 /* Scan statements until there aren't any more. */
5356 /* If we're looking at a `}', then we've run out of statements. */
5357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5358 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5361 /* Parse the statement. */
5362 cp_parser_statement (parser
, in_statement_expr_p
);
5366 /* Parse a selection-statement.
5368 selection-statement:
5369 if ( condition ) statement
5370 if ( condition ) statement else statement
5371 switch ( condition ) statement
5373 Returns the new IF_STMT or SWITCH_STMT. */
5376 cp_parser_selection_statement (cp_parser
* parser
)
5381 /* Peek at the next token. */
5382 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5384 /* See what kind of keyword it is. */
5385 keyword
= token
->keyword
;
5394 /* Look for the `('. */
5395 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5397 cp_parser_skip_to_end_of_statement (parser
);
5398 return error_mark_node
;
5401 /* Begin the selection-statement. */
5402 if (keyword
== RID_IF
)
5403 statement
= begin_if_stmt ();
5405 statement
= begin_switch_stmt ();
5407 /* Parse the condition. */
5408 condition
= cp_parser_condition (parser
);
5409 /* Look for the `)'. */
5410 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5411 cp_parser_skip_to_closing_parenthesis (parser
, true, false);
5413 if (keyword
== RID_IF
)
5417 /* Add the condition. */
5418 finish_if_stmt_cond (condition
, statement
);
5420 /* Parse the then-clause. */
5421 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5422 finish_then_clause (statement
);
5424 /* If the next token is `else', parse the else-clause. */
5425 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5430 /* Consume the `else' keyword. */
5431 cp_lexer_consume_token (parser
->lexer
);
5432 /* Parse the else-clause. */
5434 = cp_parser_implicitly_scoped_statement (parser
);
5435 finish_else_clause (statement
);
5438 /* Now we're all done with the if-statement. */
5445 /* Add the condition. */
5446 finish_switch_cond (condition
, statement
);
5448 /* Parse the body of the switch-statement. */
5449 body
= cp_parser_implicitly_scoped_statement (parser
);
5451 /* Now we're all done with the switch-statement. */
5452 finish_switch_stmt (statement
);
5460 cp_parser_error (parser
, "expected selection-statement");
5461 return error_mark_node
;
5465 /* Parse a condition.
5469 type-specifier-seq declarator = assignment-expression
5474 type-specifier-seq declarator asm-specification [opt]
5475 attributes [opt] = assignment-expression
5477 Returns the expression that should be tested. */
5480 cp_parser_condition (cp_parser
* parser
)
5482 tree type_specifiers
;
5483 const char *saved_message
;
5485 /* Try the declaration first. */
5486 cp_parser_parse_tentatively (parser
);
5487 /* New types are not allowed in the type-specifier-seq for a
5489 saved_message
= parser
->type_definition_forbidden_message
;
5490 parser
->type_definition_forbidden_message
5491 = "types may not be defined in conditions";
5492 /* Parse the type-specifier-seq. */
5493 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5494 /* Restore the saved message. */
5495 parser
->type_definition_forbidden_message
= saved_message
;
5496 /* If all is well, we might be looking at a declaration. */
5497 if (!cp_parser_error_occurred (parser
))
5500 tree asm_specification
;
5503 tree initializer
= NULL_TREE
;
5505 /* Parse the declarator. */
5506 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5507 /*ctor_dtor_or_conv_p=*/NULL
);
5508 /* Parse the attributes. */
5509 attributes
= cp_parser_attributes_opt (parser
);
5510 /* Parse the asm-specification. */
5511 asm_specification
= cp_parser_asm_specification_opt (parser
);
5512 /* If the next token is not an `=', then we might still be
5513 looking at an expression. For example:
5517 looks like a decl-specifier-seq and a declarator -- but then
5518 there is no `=', so this is an expression. */
5519 cp_parser_require (parser
, CPP_EQ
, "`='");
5520 /* If we did see an `=', then we are looking at a declaration
5522 if (cp_parser_parse_definitely (parser
))
5524 /* Create the declaration. */
5525 decl
= start_decl (declarator
, type_specifiers
,
5526 /*initialized_p=*/true,
5527 attributes
, /*prefix_attributes=*/NULL_TREE
);
5528 /* Parse the assignment-expression. */
5529 initializer
= cp_parser_assignment_expression (parser
);
5531 /* Process the initializer. */
5532 cp_finish_decl (decl
,
5535 LOOKUP_ONLYCONVERTING
);
5537 return convert_from_reference (decl
);
5540 /* If we didn't even get past the declarator successfully, we are
5541 definitely not looking at a declaration. */
5543 cp_parser_abort_tentative_parse (parser
);
5545 /* Otherwise, we are looking at an expression. */
5546 return cp_parser_expression (parser
);
5549 /* Parse an iteration-statement.
5551 iteration-statement:
5552 while ( condition ) statement
5553 do statement while ( expression ) ;
5554 for ( for-init-statement condition [opt] ; expression [opt] )
5557 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5560 cp_parser_iteration_statement (cp_parser
* parser
)
5566 /* Peek at the next token. */
5567 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
5569 return error_mark_node
;
5571 /* See what kind of keyword it is. */
5572 keyword
= token
->keyword
;
5579 /* Begin the while-statement. */
5580 statement
= begin_while_stmt ();
5581 /* Look for the `('. */
5582 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5583 /* Parse the condition. */
5584 condition
= cp_parser_condition (parser
);
5585 finish_while_stmt_cond (condition
, statement
);
5586 /* Look for the `)'. */
5587 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5588 /* Parse the dependent statement. */
5589 cp_parser_already_scoped_statement (parser
);
5590 /* We're done with the while-statement. */
5591 finish_while_stmt (statement
);
5599 /* Begin the do-statement. */
5600 statement
= begin_do_stmt ();
5601 /* Parse the body of the do-statement. */
5602 cp_parser_implicitly_scoped_statement (parser
);
5603 finish_do_body (statement
);
5604 /* Look for the `while' keyword. */
5605 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
5606 /* Look for the `('. */
5607 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5608 /* Parse the expression. */
5609 expression
= cp_parser_expression (parser
);
5610 /* We're done with the do-statement. */
5611 finish_do_stmt (expression
, statement
);
5612 /* Look for the `)'. */
5613 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5614 /* Look for the `;'. */
5615 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5621 tree condition
= NULL_TREE
;
5622 tree expression
= NULL_TREE
;
5624 /* Begin the for-statement. */
5625 statement
= begin_for_stmt ();
5626 /* Look for the `('. */
5627 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5628 /* Parse the initialization. */
5629 cp_parser_for_init_statement (parser
);
5630 finish_for_init_stmt (statement
);
5632 /* If there's a condition, process it. */
5633 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5634 condition
= cp_parser_condition (parser
);
5635 finish_for_cond (condition
, statement
);
5636 /* Look for the `;'. */
5637 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5639 /* If there's an expression, process it. */
5640 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5641 expression
= cp_parser_expression (parser
);
5642 finish_for_expr (expression
, statement
);
5643 /* Look for the `)'. */
5644 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`;'");
5646 /* Parse the body of the for-statement. */
5647 cp_parser_already_scoped_statement (parser
);
5649 /* We're done with the for-statement. */
5650 finish_for_stmt (statement
);
5655 cp_parser_error (parser
, "expected iteration-statement");
5656 statement
= error_mark_node
;
5663 /* Parse a for-init-statement.
5666 expression-statement
5667 simple-declaration */
5670 cp_parser_for_init_statement (cp_parser
* parser
)
5672 /* If the next token is a `;', then we have an empty
5673 expression-statement. Grammatically, this is also a
5674 simple-declaration, but an invalid one, because it does not
5675 declare anything. Therefore, if we did not handle this case
5676 specially, we would issue an error message about an invalid
5678 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5680 /* We're going to speculatively look for a declaration, falling back
5681 to an expression, if necessary. */
5682 cp_parser_parse_tentatively (parser
);
5683 /* Parse the declaration. */
5684 cp_parser_simple_declaration (parser
,
5685 /*function_definition_allowed_p=*/false);
5686 /* If the tentative parse failed, then we shall need to look for an
5687 expression-statement. */
5688 if (cp_parser_parse_definitely (parser
))
5692 cp_parser_expression_statement (parser
, false);
5695 /* Parse a jump-statement.
5700 return expression [opt] ;
5708 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5712 cp_parser_jump_statement (cp_parser
* parser
)
5714 tree statement
= error_mark_node
;
5718 /* Peek at the next token. */
5719 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
5721 return error_mark_node
;
5723 /* See what kind of keyword it is. */
5724 keyword
= token
->keyword
;
5728 statement
= finish_break_stmt ();
5729 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5733 statement
= finish_continue_stmt ();
5734 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5741 /* If the next token is a `;', then there is no
5743 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5744 expr
= cp_parser_expression (parser
);
5747 /* Build the return-statement. */
5748 statement
= finish_return_stmt (expr
);
5749 /* Look for the final `;'. */
5750 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5755 /* Create the goto-statement. */
5756 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
5758 /* Issue a warning about this use of a GNU extension. */
5760 pedwarn ("ISO C++ forbids computed gotos");
5761 /* Consume the '*' token. */
5762 cp_lexer_consume_token (parser
->lexer
);
5763 /* Parse the dependent expression. */
5764 finish_goto_stmt (cp_parser_expression (parser
));
5767 finish_goto_stmt (cp_parser_identifier (parser
));
5768 /* Look for the final `;'. */
5769 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5773 cp_parser_error (parser
, "expected jump-statement");
5780 /* Parse a declaration-statement.
5782 declaration-statement:
5783 block-declaration */
5786 cp_parser_declaration_statement (cp_parser
* parser
)
5788 /* Parse the block-declaration. */
5789 cp_parser_block_declaration (parser
, /*statement_p=*/true);
5791 /* Finish off the statement. */
5795 /* Some dependent statements (like `if (cond) statement'), are
5796 implicitly in their own scope. In other words, if the statement is
5797 a single statement (as opposed to a compound-statement), it is
5798 none-the-less treated as if it were enclosed in braces. Any
5799 declarations appearing in the dependent statement are out of scope
5800 after control passes that point. This function parses a statement,
5801 but ensures that is in its own scope, even if it is not a
5804 Returns the new statement. */
5807 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
5811 /* If the token is not a `{', then we must take special action. */
5812 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
5814 /* Create a compound-statement. */
5815 statement
= begin_compound_stmt (/*has_no_scope=*/false);
5816 /* Parse the dependent-statement. */
5817 cp_parser_statement (parser
, false);
5818 /* Finish the dummy compound-statement. */
5819 finish_compound_stmt (statement
);
5821 /* Otherwise, we simply parse the statement directly. */
5823 statement
= cp_parser_compound_statement (parser
, false);
5825 /* Return the statement. */
5829 /* For some dependent statements (like `while (cond) statement'), we
5830 have already created a scope. Therefore, even if the dependent
5831 statement is a compound-statement, we do not want to create another
5835 cp_parser_already_scoped_statement (cp_parser
* parser
)
5837 /* If the token is not a `{', then we must take special action. */
5838 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
5842 /* Create a compound-statement. */
5843 statement
= begin_compound_stmt (/*has_no_scope=*/true);
5844 /* Parse the dependent-statement. */
5845 cp_parser_statement (parser
, false);
5846 /* Finish the dummy compound-statement. */
5847 finish_compound_stmt (statement
);
5849 /* Otherwise, we simply parse the statement directly. */
5851 cp_parser_statement (parser
, false);
5854 /* Declarations [gram.dcl.dcl] */
5856 /* Parse an optional declaration-sequence.
5860 declaration-seq declaration */
5863 cp_parser_declaration_seq_opt (cp_parser
* parser
)
5869 token
= cp_lexer_peek_token (parser
->lexer
);
5871 if (token
->type
== CPP_CLOSE_BRACE
5872 || token
->type
== CPP_EOF
)
5875 if (token
->type
== CPP_SEMICOLON
)
5877 /* A declaration consisting of a single semicolon is
5878 invalid. Allow it unless we're being pedantic. */
5880 pedwarn ("extra `;'");
5881 cp_lexer_consume_token (parser
->lexer
);
5885 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
5886 parser to enter or exit implicit `extern "C"' blocks. */
5887 while (pending_lang_change
> 0)
5889 push_lang_context (lang_name_c
);
5890 --pending_lang_change
;
5892 while (pending_lang_change
< 0)
5894 pop_lang_context ();
5895 ++pending_lang_change
;
5898 /* Parse the declaration itself. */
5899 cp_parser_declaration (parser
);
5903 /* Parse a declaration.
5908 template-declaration
5909 explicit-instantiation
5910 explicit-specialization
5911 linkage-specification
5912 namespace-definition
5917 __extension__ declaration */
5920 cp_parser_declaration (cp_parser
* parser
)
5926 /* Check for the `__extension__' keyword. */
5927 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
5929 /* Parse the qualified declaration. */
5930 cp_parser_declaration (parser
);
5931 /* Restore the PEDANTIC flag. */
5932 pedantic
= saved_pedantic
;
5937 /* Try to figure out what kind of declaration is present. */
5938 token1
= *cp_lexer_peek_token (parser
->lexer
);
5939 if (token1
.type
!= CPP_EOF
)
5940 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
5942 /* If the next token is `extern' and the following token is a string
5943 literal, then we have a linkage specification. */
5944 if (token1
.keyword
== RID_EXTERN
5945 && cp_parser_is_string_literal (&token2
))
5946 cp_parser_linkage_specification (parser
);
5947 /* If the next token is `template', then we have either a template
5948 declaration, an explicit instantiation, or an explicit
5950 else if (token1
.keyword
== RID_TEMPLATE
)
5952 /* `template <>' indicates a template specialization. */
5953 if (token2
.type
== CPP_LESS
5954 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
5955 cp_parser_explicit_specialization (parser
);
5956 /* `template <' indicates a template declaration. */
5957 else if (token2
.type
== CPP_LESS
)
5958 cp_parser_template_declaration (parser
, /*member_p=*/false);
5959 /* Anything else must be an explicit instantiation. */
5961 cp_parser_explicit_instantiation (parser
);
5963 /* If the next token is `export', then we have a template
5965 else if (token1
.keyword
== RID_EXPORT
)
5966 cp_parser_template_declaration (parser
, /*member_p=*/false);
5967 /* If the next token is `extern', 'static' or 'inline' and the one
5968 after that is `template', we have a GNU extended explicit
5969 instantiation directive. */
5970 else if (cp_parser_allow_gnu_extensions_p (parser
)
5971 && (token1
.keyword
== RID_EXTERN
5972 || token1
.keyword
== RID_STATIC
5973 || token1
.keyword
== RID_INLINE
)
5974 && token2
.keyword
== RID_TEMPLATE
)
5975 cp_parser_explicit_instantiation (parser
);
5976 /* If the next token is `namespace', check for a named or unnamed
5977 namespace definition. */
5978 else if (token1
.keyword
== RID_NAMESPACE
5979 && (/* A named namespace definition. */
5980 (token2
.type
== CPP_NAME
5981 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5983 /* An unnamed namespace definition. */
5984 || token2
.type
== CPP_OPEN_BRACE
))
5985 cp_parser_namespace_definition (parser
);
5986 /* We must have either a block declaration or a function
5989 /* Try to parse a block-declaration, or a function-definition. */
5990 cp_parser_block_declaration (parser
, /*statement_p=*/false);
5993 /* Parse a block-declaration.
5998 namespace-alias-definition
6005 __extension__ block-declaration
6008 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6009 part of a declaration-statement. */
6012 cp_parser_block_declaration (cp_parser
*parser
,
6018 /* Check for the `__extension__' keyword. */
6019 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6021 /* Parse the qualified declaration. */
6022 cp_parser_block_declaration (parser
, statement_p
);
6023 /* Restore the PEDANTIC flag. */
6024 pedantic
= saved_pedantic
;
6029 /* Peek at the next token to figure out which kind of declaration is
6031 token1
= cp_lexer_peek_token (parser
->lexer
);
6033 /* If the next keyword is `asm', we have an asm-definition. */
6034 if (token1
->keyword
== RID_ASM
)
6037 cp_parser_commit_to_tentative_parse (parser
);
6038 cp_parser_asm_definition (parser
);
6040 /* If the next keyword is `namespace', we have a
6041 namespace-alias-definition. */
6042 else if (token1
->keyword
== RID_NAMESPACE
)
6043 cp_parser_namespace_alias_definition (parser
);
6044 /* If the next keyword is `using', we have either a
6045 using-declaration or a using-directive. */
6046 else if (token1
->keyword
== RID_USING
)
6051 cp_parser_commit_to_tentative_parse (parser
);
6052 /* If the token after `using' is `namespace', then we have a
6054 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6055 if (token2
->keyword
== RID_NAMESPACE
)
6056 cp_parser_using_directive (parser
);
6057 /* Otherwise, it's a using-declaration. */
6059 cp_parser_using_declaration (parser
);
6061 /* If the next keyword is `__label__' we have a label declaration. */
6062 else if (token1
->keyword
== RID_LABEL
)
6065 cp_parser_commit_to_tentative_parse (parser
);
6066 cp_parser_label_declaration (parser
);
6068 /* Anything else must be a simple-declaration. */
6070 cp_parser_simple_declaration (parser
, !statement_p
);
6073 /* Parse a simple-declaration.
6076 decl-specifier-seq [opt] init-declarator-list [opt] ;
6078 init-declarator-list:
6080 init-declarator-list , init-declarator
6082 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6083 function-definition as a simple-declaration. */
6086 cp_parser_simple_declaration (cp_parser
* parser
,
6087 bool function_definition_allowed_p
)
6089 tree decl_specifiers
;
6091 int declares_class_or_enum
;
6092 bool saw_declarator
;
6094 /* Defer access checks until we know what is being declared; the
6095 checks for names appearing in the decl-specifier-seq should be
6096 done as if we were in the scope of the thing being declared. */
6097 push_deferring_access_checks (dk_deferred
);
6099 /* Parse the decl-specifier-seq. We have to keep track of whether
6100 or not the decl-specifier-seq declares a named class or
6101 enumeration type, since that is the only case in which the
6102 init-declarator-list is allowed to be empty.
6106 In a simple-declaration, the optional init-declarator-list can be
6107 omitted only when declaring a class or enumeration, that is when
6108 the decl-specifier-seq contains either a class-specifier, an
6109 elaborated-type-specifier, or an enum-specifier. */
6111 = cp_parser_decl_specifier_seq (parser
,
6112 CP_PARSER_FLAGS_OPTIONAL
,
6114 &declares_class_or_enum
);
6115 /* We no longer need to defer access checks. */
6116 stop_deferring_access_checks ();
6118 /* In a block scope, a valid declaration must always have a
6119 decl-specifier-seq. By not trying to parse declarators, we can
6120 resolve the declaration/expression ambiguity more quickly. */
6121 if (!function_definition_allowed_p
&& !decl_specifiers
)
6123 cp_parser_error (parser
, "expected declaration");
6127 /* If the next two tokens are both identifiers, the code is
6128 erroneous. The usual cause of this situation is code like:
6132 where "T" should name a type -- but does not. */
6133 if (cp_parser_diagnose_invalid_type_name (parser
))
6135 /* If parsing tentatively, we should commit; we really are
6136 looking at a declaration. */
6137 cp_parser_commit_to_tentative_parse (parser
);
6142 /* Keep going until we hit the `;' at the end of the simple
6144 saw_declarator
= false;
6145 while (cp_lexer_next_token_is_not (parser
->lexer
,
6149 bool function_definition_p
;
6152 saw_declarator
= true;
6153 /* Parse the init-declarator. */
6154 decl
= cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6155 function_definition_allowed_p
,
6157 declares_class_or_enum
,
6158 &function_definition_p
);
6159 /* If an error occurred while parsing tentatively, exit quickly.
6160 (That usually happens when in the body of a function; each
6161 statement is treated as a declaration-statement until proven
6163 if (cp_parser_error_occurred (parser
))
6165 /* Handle function definitions specially. */
6166 if (function_definition_p
)
6168 /* If the next token is a `,', then we are probably
6169 processing something like:
6173 which is erroneous. */
6174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6175 error ("mixing declarations and function-definitions is forbidden");
6176 /* Otherwise, we're done with the list of declarators. */
6179 pop_deferring_access_checks ();
6183 /* The next token should be either a `,' or a `;'. */
6184 token
= cp_lexer_peek_token (parser
->lexer
);
6185 /* If it's a `,', there are more declarators to come. */
6186 if (token
->type
== CPP_COMMA
)
6187 cp_lexer_consume_token (parser
->lexer
);
6188 /* If it's a `;', we are done. */
6189 else if (token
->type
== CPP_SEMICOLON
)
6191 /* Anything else is an error. */
6194 cp_parser_error (parser
, "expected `,' or `;'");
6195 /* Skip tokens until we reach the end of the statement. */
6196 cp_parser_skip_to_end_of_statement (parser
);
6199 /* After the first time around, a function-definition is not
6200 allowed -- even if it was OK at first. For example:
6205 function_definition_allowed_p
= false;
6208 /* Issue an error message if no declarators are present, and the
6209 decl-specifier-seq does not itself declare a class or
6211 if (!saw_declarator
)
6213 if (cp_parser_declares_only_class_p (parser
))
6214 shadow_tag (decl_specifiers
);
6215 /* Perform any deferred access checks. */
6216 perform_deferred_access_checks ();
6219 /* Consume the `;'. */
6220 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6223 pop_deferring_access_checks ();
6226 /* Parse a decl-specifier-seq.
6229 decl-specifier-seq [opt] decl-specifier
6232 storage-class-specifier
6241 decl-specifier-seq [opt] attributes
6243 Returns a TREE_LIST, giving the decl-specifiers in the order they
6244 appear in the source code. The TREE_VALUE of each node is the
6245 decl-specifier. For a keyword (such as `auto' or `friend'), the
6246 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6247 representation of a type-specifier, see cp_parser_type_specifier.
6249 If there are attributes, they will be stored in *ATTRIBUTES,
6250 represented as described above cp_parser_attributes.
6252 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6253 appears, and the entity that will be a friend is not going to be a
6254 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6255 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6256 friendship is granted might not be a class.
6258 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6261 1: one of the decl-specifiers is an elaborated-type-specifier
6262 2: one of the decl-specifiers is an enum-specifier or a
6268 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6269 cp_parser_flags flags
,
6271 int* declares_class_or_enum
)
6273 tree decl_specs
= NULL_TREE
;
6274 bool friend_p
= false;
6275 bool constructor_possible_p
= !parser
->in_declarator_p
;
6277 /* Assume no class or enumeration type is declared. */
6278 *declares_class_or_enum
= 0;
6280 /* Assume there are no attributes. */
6281 *attributes
= NULL_TREE
;
6283 /* Keep reading specifiers until there are no more to read. */
6286 tree decl_spec
= NULL_TREE
;
6290 /* Peek at the next token. */
6291 token
= cp_lexer_peek_token (parser
->lexer
);
6292 /* Handle attributes. */
6293 if (token
->keyword
== RID_ATTRIBUTE
)
6295 /* Parse the attributes. */
6296 decl_spec
= cp_parser_attributes_opt (parser
);
6297 /* Add them to the list. */
6298 *attributes
= chainon (*attributes
, decl_spec
);
6301 /* If the next token is an appropriate keyword, we can simply
6302 add it to the list. */
6303 switch (token
->keyword
)
6309 /* The representation of the specifier is simply the
6310 appropriate TREE_IDENTIFIER node. */
6311 decl_spec
= token
->value
;
6312 /* Consume the token. */
6313 cp_lexer_consume_token (parser
->lexer
);
6316 /* function-specifier:
6323 decl_spec
= cp_parser_function_specifier_opt (parser
);
6329 /* The representation of the specifier is simply the
6330 appropriate TREE_IDENTIFIER node. */
6331 decl_spec
= token
->value
;
6332 /* Consume the token. */
6333 cp_lexer_consume_token (parser
->lexer
);
6334 /* A constructor declarator cannot appear in a typedef. */
6335 constructor_possible_p
= false;
6336 /* The "typedef" keyword can only occur in a declaration; we
6337 may as well commit at this point. */
6338 cp_parser_commit_to_tentative_parse (parser
);
6341 /* storage-class-specifier:
6356 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6363 /* Constructors are a special case. The `S' in `S()' is not a
6364 decl-specifier; it is the beginning of the declarator. */
6365 constructor_p
= (!decl_spec
6366 && constructor_possible_p
6367 && cp_parser_constructor_declarator_p (parser
,
6370 /* If we don't have a DECL_SPEC yet, then we must be looking at
6371 a type-specifier. */
6372 if (!decl_spec
&& !constructor_p
)
6374 int decl_spec_declares_class_or_enum
;
6375 bool is_cv_qualifier
;
6378 = cp_parser_type_specifier (parser
, flags
,
6380 /*is_declaration=*/true,
6381 &decl_spec_declares_class_or_enum
,
6384 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6386 /* If this type-specifier referenced a user-defined type
6387 (a typedef, class-name, etc.), then we can't allow any
6388 more such type-specifiers henceforth.
6392 The longest sequence of decl-specifiers that could
6393 possibly be a type name is taken as the
6394 decl-specifier-seq of a declaration. The sequence shall
6395 be self-consistent as described below.
6399 As a general rule, at most one type-specifier is allowed
6400 in the complete decl-specifier-seq of a declaration. The
6401 only exceptions are the following:
6403 -- const or volatile can be combined with any other
6406 -- signed or unsigned can be combined with char, long,
6414 void g (const int Pc);
6416 Here, Pc is *not* part of the decl-specifier seq; it's
6417 the declarator. Therefore, once we see a type-specifier
6418 (other than a cv-qualifier), we forbid any additional
6419 user-defined types. We *do* still allow things like `int
6420 int' to be considered a decl-specifier-seq, and issue the
6421 error message later. */
6422 if (decl_spec
&& !is_cv_qualifier
)
6423 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6424 /* A constructor declarator cannot follow a type-specifier. */
6426 constructor_possible_p
= false;
6429 /* If we still do not have a DECL_SPEC, then there are no more
6433 /* Issue an error message, unless the entire construct was
6435 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6437 cp_parser_error (parser
, "expected decl specifier");
6438 return error_mark_node
;
6444 /* Add the DECL_SPEC to the list of specifiers. */
6445 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6447 /* After we see one decl-specifier, further decl-specifiers are
6449 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6452 /* We have built up the DECL_SPECS in reverse order. Return them in
6453 the correct order. */
6454 return nreverse (decl_specs
);
6457 /* Parse an (optional) storage-class-specifier.
6459 storage-class-specifier:
6468 storage-class-specifier:
6471 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6474 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
6476 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6484 /* Consume the token. */
6485 return cp_lexer_consume_token (parser
->lexer
)->value
;
6492 /* Parse an (optional) function-specifier.
6499 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6502 cp_parser_function_specifier_opt (cp_parser
* parser
)
6504 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6509 /* Consume the token. */
6510 return cp_lexer_consume_token (parser
->lexer
)->value
;
6517 /* Parse a linkage-specification.
6519 linkage-specification:
6520 extern string-literal { declaration-seq [opt] }
6521 extern string-literal declaration */
6524 cp_parser_linkage_specification (cp_parser
* parser
)
6529 /* Look for the `extern' keyword. */
6530 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
6532 /* Peek at the next token. */
6533 token
= cp_lexer_peek_token (parser
->lexer
);
6534 /* If it's not a string-literal, then there's a problem. */
6535 if (!cp_parser_is_string_literal (token
))
6537 cp_parser_error (parser
, "expected language-name");
6540 /* Consume the token. */
6541 cp_lexer_consume_token (parser
->lexer
);
6543 /* Transform the literal into an identifier. If the literal is a
6544 wide-character string, or contains embedded NULs, then we can't
6545 handle it as the user wants. */
6546 if (token
->type
== CPP_WSTRING
6547 || (strlen (TREE_STRING_POINTER (token
->value
))
6548 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
6550 cp_parser_error (parser
, "invalid linkage-specification");
6551 /* Assume C++ linkage. */
6552 linkage
= get_identifier ("c++");
6554 /* If it's a simple string constant, things are easier. */
6556 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
6558 /* We're now using the new linkage. */
6559 push_lang_context (linkage
);
6561 /* If the next token is a `{', then we're using the first
6563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6565 /* Consume the `{' token. */
6566 cp_lexer_consume_token (parser
->lexer
);
6567 /* Parse the declarations. */
6568 cp_parser_declaration_seq_opt (parser
);
6569 /* Look for the closing `}'. */
6570 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6572 /* Otherwise, there's just one declaration. */
6575 bool saved_in_unbraced_linkage_specification_p
;
6577 saved_in_unbraced_linkage_specification_p
6578 = parser
->in_unbraced_linkage_specification_p
;
6579 parser
->in_unbraced_linkage_specification_p
= true;
6580 have_extern_spec
= true;
6581 cp_parser_declaration (parser
);
6582 have_extern_spec
= false;
6583 parser
->in_unbraced_linkage_specification_p
6584 = saved_in_unbraced_linkage_specification_p
;
6587 /* We're done with the linkage-specification. */
6588 pop_lang_context ();
6591 /* Special member functions [gram.special] */
6593 /* Parse a conversion-function-id.
6595 conversion-function-id:
6596 operator conversion-type-id
6598 Returns an IDENTIFIER_NODE representing the operator. */
6601 cp_parser_conversion_function_id (cp_parser
* parser
)
6605 tree saved_qualifying_scope
;
6606 tree saved_object_scope
;
6608 /* Look for the `operator' token. */
6609 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
6610 return error_mark_node
;
6611 /* When we parse the conversion-type-id, the current scope will be
6612 reset. However, we need that information in able to look up the
6613 conversion function later, so we save it here. */
6614 saved_scope
= parser
->scope
;
6615 saved_qualifying_scope
= parser
->qualifying_scope
;
6616 saved_object_scope
= parser
->object_scope
;
6617 /* We must enter the scope of the class so that the names of
6618 entities declared within the class are available in the
6619 conversion-type-id. For example, consider:
6626 S::operator I() { ... }
6628 In order to see that `I' is a type-name in the definition, we
6629 must be in the scope of `S'. */
6631 push_scope (saved_scope
);
6632 /* Parse the conversion-type-id. */
6633 type
= cp_parser_conversion_type_id (parser
);
6634 /* Leave the scope of the class, if any. */
6636 pop_scope (saved_scope
);
6637 /* Restore the saved scope. */
6638 parser
->scope
= saved_scope
;
6639 parser
->qualifying_scope
= saved_qualifying_scope
;
6640 parser
->object_scope
= saved_object_scope
;
6641 /* If the TYPE is invalid, indicate failure. */
6642 if (type
== error_mark_node
)
6643 return error_mark_node
;
6644 return mangle_conv_op_name_for_type (type
);
6647 /* Parse a conversion-type-id:
6650 type-specifier-seq conversion-declarator [opt]
6652 Returns the TYPE specified. */
6655 cp_parser_conversion_type_id (cp_parser
* parser
)
6658 tree type_specifiers
;
6661 /* Parse the attributes. */
6662 attributes
= cp_parser_attributes_opt (parser
);
6663 /* Parse the type-specifiers. */
6664 type_specifiers
= cp_parser_type_specifier_seq (parser
);
6665 /* If that didn't work, stop. */
6666 if (type_specifiers
== error_mark_node
)
6667 return error_mark_node
;
6668 /* Parse the conversion-declarator. */
6669 declarator
= cp_parser_conversion_declarator_opt (parser
);
6671 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
6672 /*initialized=*/0, &attributes
);
6675 /* Parse an (optional) conversion-declarator.
6677 conversion-declarator:
6678 ptr-operator conversion-declarator [opt]
6680 Returns a representation of the declarator. See
6681 cp_parser_declarator for details. */
6684 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
6686 enum tree_code code
;
6688 tree cv_qualifier_seq
;
6690 /* We don't know if there's a ptr-operator next, or not. */
6691 cp_parser_parse_tentatively (parser
);
6692 /* Try the ptr-operator. */
6693 code
= cp_parser_ptr_operator (parser
, &class_type
,
6695 /* If it worked, look for more conversion-declarators. */
6696 if (cp_parser_parse_definitely (parser
))
6700 /* Parse another optional declarator. */
6701 declarator
= cp_parser_conversion_declarator_opt (parser
);
6703 /* Create the representation of the declarator. */
6704 if (code
== INDIRECT_REF
)
6705 declarator
= make_pointer_declarator (cv_qualifier_seq
,
6708 declarator
= make_reference_declarator (cv_qualifier_seq
,
6711 /* Handle the pointer-to-member case. */
6713 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
6721 /* Parse an (optional) ctor-initializer.
6724 : mem-initializer-list
6726 Returns TRUE iff the ctor-initializer was actually present. */
6729 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
6731 /* If the next token is not a `:', then there is no
6732 ctor-initializer. */
6733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
6735 /* Do default initialization of any bases and members. */
6736 if (DECL_CONSTRUCTOR_P (current_function_decl
))
6737 finish_mem_initializers (NULL_TREE
);
6742 /* Consume the `:' token. */
6743 cp_lexer_consume_token (parser
->lexer
);
6744 /* And the mem-initializer-list. */
6745 cp_parser_mem_initializer_list (parser
);
6750 /* Parse a mem-initializer-list.
6752 mem-initializer-list:
6754 mem-initializer , mem-initializer-list */
6757 cp_parser_mem_initializer_list (cp_parser
* parser
)
6759 tree mem_initializer_list
= NULL_TREE
;
6761 /* Let the semantic analysis code know that we are starting the
6762 mem-initializer-list. */
6763 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
6764 error ("only constructors take base initializers");
6766 /* Loop through the list. */
6769 tree mem_initializer
;
6771 /* Parse the mem-initializer. */
6772 mem_initializer
= cp_parser_mem_initializer (parser
);
6773 /* Add it to the list, unless it was erroneous. */
6774 if (mem_initializer
)
6776 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
6777 mem_initializer_list
= mem_initializer
;
6779 /* If the next token is not a `,', we're done. */
6780 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6782 /* Consume the `,' token. */
6783 cp_lexer_consume_token (parser
->lexer
);
6786 /* Perform semantic analysis. */
6787 if (DECL_CONSTRUCTOR_P (current_function_decl
))
6788 finish_mem_initializers (mem_initializer_list
);
6791 /* Parse a mem-initializer.
6794 mem-initializer-id ( expression-list [opt] )
6799 ( expression-list [opt] )
6801 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6802 class) or FIELD_DECL (for a non-static data member) to initialize;
6803 the TREE_VALUE is the expression-list. */
6806 cp_parser_mem_initializer (cp_parser
* parser
)
6808 tree mem_initializer_id
;
6809 tree expression_list
;
6812 /* Find out what is being initialized. */
6813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6815 pedwarn ("anachronistic old-style base class initializer");
6816 mem_initializer_id
= NULL_TREE
;
6819 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
6820 member
= expand_member_init (mem_initializer_id
);
6821 if (member
&& !DECL_P (member
))
6822 in_base_initializer
= 1;
6825 = cp_parser_parenthesized_expression_list (parser
, false,
6826 /*non_constant_p=*/NULL
);
6827 if (!expression_list
)
6828 expression_list
= void_type_node
;
6830 in_base_initializer
= 0;
6832 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
6835 /* Parse a mem-initializer-id.
6838 :: [opt] nested-name-specifier [opt] class-name
6841 Returns a TYPE indicating the class to be initializer for the first
6842 production. Returns an IDENTIFIER_NODE indicating the data member
6843 to be initialized for the second production. */
6846 cp_parser_mem_initializer_id (cp_parser
* parser
)
6848 bool global_scope_p
;
6849 bool nested_name_specifier_p
;
6852 /* Look for the optional `::' operator. */
6854 = (cp_parser_global_scope_opt (parser
,
6855 /*current_scope_valid_p=*/false)
6857 /* Look for the optional nested-name-specifier. The simplest way to
6862 The keyword `typename' is not permitted in a base-specifier or
6863 mem-initializer; in these contexts a qualified name that
6864 depends on a template-parameter is implicitly assumed to be a
6867 is to assume that we have seen the `typename' keyword at this
6869 nested_name_specifier_p
6870 = (cp_parser_nested_name_specifier_opt (parser
,
6871 /*typename_keyword_p=*/true,
6872 /*check_dependency_p=*/true,
6875 /* If there is a `::' operator or a nested-name-specifier, then we
6876 are definitely looking for a class-name. */
6877 if (global_scope_p
|| nested_name_specifier_p
)
6878 return cp_parser_class_name (parser
,
6879 /*typename_keyword_p=*/true,
6880 /*template_keyword_p=*/false,
6882 /*check_dependency_p=*/true,
6883 /*class_head_p=*/false);
6884 /* Otherwise, we could also be looking for an ordinary identifier. */
6885 cp_parser_parse_tentatively (parser
);
6886 /* Try a class-name. */
6887 id
= cp_parser_class_name (parser
,
6888 /*typename_keyword_p=*/true,
6889 /*template_keyword_p=*/false,
6891 /*check_dependency_p=*/true,
6892 /*class_head_p=*/false);
6893 /* If we found one, we're done. */
6894 if (cp_parser_parse_definitely (parser
))
6896 /* Otherwise, look for an ordinary identifier. */
6897 return cp_parser_identifier (parser
);
6900 /* Overloading [gram.over] */
6902 /* Parse an operator-function-id.
6904 operator-function-id:
6907 Returns an IDENTIFIER_NODE for the operator which is a
6908 human-readable spelling of the identifier, e.g., `operator +'. */
6911 cp_parser_operator_function_id (cp_parser
* parser
)
6913 /* Look for the `operator' keyword. */
6914 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
6915 return error_mark_node
;
6916 /* And then the name of the operator itself. */
6917 return cp_parser_operator (parser
);
6920 /* Parse an operator.
6923 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6924 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6925 || ++ -- , ->* -> () []
6932 Returns an IDENTIFIER_NODE for the operator which is a
6933 human-readable spelling of the identifier, e.g., `operator +'. */
6936 cp_parser_operator (cp_parser
* parser
)
6938 tree id
= NULL_TREE
;
6941 /* Peek at the next token. */
6942 token
= cp_lexer_peek_token (parser
->lexer
);
6943 /* Figure out which operator we have. */
6944 switch (token
->type
)
6950 /* The keyword should be either `new' or `delete'. */
6951 if (token
->keyword
== RID_NEW
)
6953 else if (token
->keyword
== RID_DELETE
)
6958 /* Consume the `new' or `delete' token. */
6959 cp_lexer_consume_token (parser
->lexer
);
6961 /* Peek at the next token. */
6962 token
= cp_lexer_peek_token (parser
->lexer
);
6963 /* If it's a `[' token then this is the array variant of the
6965 if (token
->type
== CPP_OPEN_SQUARE
)
6967 /* Consume the `[' token. */
6968 cp_lexer_consume_token (parser
->lexer
);
6969 /* Look for the `]' token. */
6970 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
6971 id
= ansi_opname (op
== NEW_EXPR
6972 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
6974 /* Otherwise, we have the non-array variant. */
6976 id
= ansi_opname (op
);
6982 id
= ansi_opname (PLUS_EXPR
);
6986 id
= ansi_opname (MINUS_EXPR
);
6990 id
= ansi_opname (MULT_EXPR
);
6994 id
= ansi_opname (TRUNC_DIV_EXPR
);
6998 id
= ansi_opname (TRUNC_MOD_EXPR
);
7002 id
= ansi_opname (BIT_XOR_EXPR
);
7006 id
= ansi_opname (BIT_AND_EXPR
);
7010 id
= ansi_opname (BIT_IOR_EXPR
);
7014 id
= ansi_opname (BIT_NOT_EXPR
);
7018 id
= ansi_opname (TRUTH_NOT_EXPR
);
7022 id
= ansi_assopname (NOP_EXPR
);
7026 id
= ansi_opname (LT_EXPR
);
7030 id
= ansi_opname (GT_EXPR
);
7034 id
= ansi_assopname (PLUS_EXPR
);
7038 id
= ansi_assopname (MINUS_EXPR
);
7042 id
= ansi_assopname (MULT_EXPR
);
7046 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7050 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7054 id
= ansi_assopname (BIT_XOR_EXPR
);
7058 id
= ansi_assopname (BIT_AND_EXPR
);
7062 id
= ansi_assopname (BIT_IOR_EXPR
);
7066 id
= ansi_opname (LSHIFT_EXPR
);
7070 id
= ansi_opname (RSHIFT_EXPR
);
7074 id
= ansi_assopname (LSHIFT_EXPR
);
7078 id
= ansi_assopname (RSHIFT_EXPR
);
7082 id
= ansi_opname (EQ_EXPR
);
7086 id
= ansi_opname (NE_EXPR
);
7090 id
= ansi_opname (LE_EXPR
);
7093 case CPP_GREATER_EQ
:
7094 id
= ansi_opname (GE_EXPR
);
7098 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7102 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7106 id
= ansi_opname (POSTINCREMENT_EXPR
);
7109 case CPP_MINUS_MINUS
:
7110 id
= ansi_opname (PREDECREMENT_EXPR
);
7114 id
= ansi_opname (COMPOUND_EXPR
);
7117 case CPP_DEREF_STAR
:
7118 id
= ansi_opname (MEMBER_REF
);
7122 id
= ansi_opname (COMPONENT_REF
);
7125 case CPP_OPEN_PAREN
:
7126 /* Consume the `('. */
7127 cp_lexer_consume_token (parser
->lexer
);
7128 /* Look for the matching `)'. */
7129 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7130 return ansi_opname (CALL_EXPR
);
7132 case CPP_OPEN_SQUARE
:
7133 /* Consume the `['. */
7134 cp_lexer_consume_token (parser
->lexer
);
7135 /* Look for the matching `]'. */
7136 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7137 return ansi_opname (ARRAY_REF
);
7141 id
= ansi_opname (MIN_EXPR
);
7145 id
= ansi_opname (MAX_EXPR
);
7149 id
= ansi_assopname (MIN_EXPR
);
7153 id
= ansi_assopname (MAX_EXPR
);
7157 /* Anything else is an error. */
7161 /* If we have selected an identifier, we need to consume the
7164 cp_lexer_consume_token (parser
->lexer
);
7165 /* Otherwise, no valid operator name was present. */
7168 cp_parser_error (parser
, "expected operator");
7169 id
= error_mark_node
;
7175 /* Parse a template-declaration.
7177 template-declaration:
7178 export [opt] template < template-parameter-list > declaration
7180 If MEMBER_P is TRUE, this template-declaration occurs within a
7183 The grammar rule given by the standard isn't correct. What
7186 template-declaration:
7187 export [opt] template-parameter-list-seq
7188 decl-specifier-seq [opt] init-declarator [opt] ;
7189 export [opt] template-parameter-list-seq
7192 template-parameter-list-seq:
7193 template-parameter-list-seq [opt]
7194 template < template-parameter-list > */
7197 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7199 /* Check for `export'. */
7200 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7202 /* Consume the `export' token. */
7203 cp_lexer_consume_token (parser
->lexer
);
7204 /* Warn that we do not support `export'. */
7205 warning ("keyword `export' not implemented, and will be ignored");
7208 cp_parser_template_declaration_after_export (parser
, member_p
);
7211 /* Parse a template-parameter-list.
7213 template-parameter-list:
7215 template-parameter-list , template-parameter
7217 Returns a TREE_LIST. Each node represents a template parameter.
7218 The nodes are connected via their TREE_CHAINs. */
7221 cp_parser_template_parameter_list (cp_parser
* parser
)
7223 tree parameter_list
= NULL_TREE
;
7230 /* Parse the template-parameter. */
7231 parameter
= cp_parser_template_parameter (parser
);
7232 /* Add it to the list. */
7233 parameter_list
= process_template_parm (parameter_list
,
7236 /* Peek at the next token. */
7237 token
= cp_lexer_peek_token (parser
->lexer
);
7238 /* If it's not a `,', we're done. */
7239 if (token
->type
!= CPP_COMMA
)
7241 /* Otherwise, consume the `,' token. */
7242 cp_lexer_consume_token (parser
->lexer
);
7245 return parameter_list
;
7248 /* Parse a template-parameter.
7252 parameter-declaration
7254 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7255 TREE_PURPOSE is the default value, if any. */
7258 cp_parser_template_parameter (cp_parser
* parser
)
7262 /* Peek at the next token. */
7263 token
= cp_lexer_peek_token (parser
->lexer
);
7264 /* If it is `class' or `template', we have a type-parameter. */
7265 if (token
->keyword
== RID_TEMPLATE
)
7266 return cp_parser_type_parameter (parser
);
7267 /* If it is `class' or `typename' we do not know yet whether it is a
7268 type parameter or a non-type parameter. Consider:
7270 template <typename T, typename T::X X> ...
7274 template <class C, class D*> ...
7276 Here, the first parameter is a type parameter, and the second is
7277 a non-type parameter. We can tell by looking at the token after
7278 the identifier -- if it is a `,', `=', or `>' then we have a type
7280 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7282 /* Peek at the token after `class' or `typename'. */
7283 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7284 /* If it's an identifier, skip it. */
7285 if (token
->type
== CPP_NAME
)
7286 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7287 /* Now, see if the token looks like the end of a template
7289 if (token
->type
== CPP_COMMA
7290 || token
->type
== CPP_EQ
7291 || token
->type
== CPP_GREATER
)
7292 return cp_parser_type_parameter (parser
);
7295 /* Otherwise, it is a non-type parameter.
7299 When parsing a default template-argument for a non-type
7300 template-parameter, the first non-nested `>' is taken as the end
7301 of the template parameter-list rather than a greater-than
7304 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true);
7307 /* Parse a type-parameter.
7310 class identifier [opt]
7311 class identifier [opt] = type-id
7312 typename identifier [opt]
7313 typename identifier [opt] = type-id
7314 template < template-parameter-list > class identifier [opt]
7315 template < template-parameter-list > class identifier [opt]
7318 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7319 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7320 the declaration of the parameter. */
7323 cp_parser_type_parameter (cp_parser
* parser
)
7328 /* Look for a keyword to tell us what kind of parameter this is. */
7329 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7330 "`class', `typename', or `template'");
7332 return error_mark_node
;
7334 switch (token
->keyword
)
7340 tree default_argument
;
7342 /* If the next token is an identifier, then it names the
7344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7345 identifier
= cp_parser_identifier (parser
);
7347 identifier
= NULL_TREE
;
7349 /* Create the parameter. */
7350 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7352 /* If the next token is an `=', we have a default argument. */
7353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7355 /* Consume the `=' token. */
7356 cp_lexer_consume_token (parser
->lexer
);
7357 /* Parse the default-argument. */
7358 default_argument
= cp_parser_type_id (parser
);
7361 default_argument
= NULL_TREE
;
7363 /* Create the combined representation of the parameter and the
7364 default argument. */
7365 parameter
= build_tree_list (default_argument
, parameter
);
7371 tree parameter_list
;
7373 tree default_argument
;
7375 /* Look for the `<'. */
7376 cp_parser_require (parser
, CPP_LESS
, "`<'");
7377 /* Parse the template-parameter-list. */
7378 begin_template_parm_list ();
7380 = cp_parser_template_parameter_list (parser
);
7381 parameter_list
= end_template_parm_list (parameter_list
);
7382 /* Look for the `>'. */
7383 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7384 /* Look for the `class' keyword. */
7385 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7386 /* If the next token is an `=', then there is a
7387 default-argument. If the next token is a `>', we are at
7388 the end of the parameter-list. If the next token is a `,',
7389 then we are at the end of this parameter. */
7390 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7391 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7392 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7393 identifier
= cp_parser_identifier (parser
);
7395 identifier
= NULL_TREE
;
7396 /* Create the template parameter. */
7397 parameter
= finish_template_template_parm (class_type_node
,
7400 /* If the next token is an `=', then there is a
7401 default-argument. */
7402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7404 /* Consume the `='. */
7405 cp_lexer_consume_token (parser
->lexer
);
7406 /* Parse the id-expression. */
7408 = cp_parser_id_expression (parser
,
7409 /*template_keyword_p=*/false,
7410 /*check_dependency_p=*/true,
7411 /*template_p=*/NULL
,
7412 /*declarator_p=*/false);
7413 /* Look up the name. */
7415 = cp_parser_lookup_name_simple (parser
, default_argument
);
7416 /* See if the default argument is valid. */
7418 = check_template_template_default_arg (default_argument
);
7421 default_argument
= NULL_TREE
;
7423 /* Create the combined representation of the parameter and the
7424 default argument. */
7425 parameter
= build_tree_list (default_argument
, parameter
);
7430 /* Anything else is an error. */
7431 cp_parser_error (parser
,
7432 "expected `class', `typename', or `template'");
7433 parameter
= error_mark_node
;
7439 /* Parse a template-id.
7442 template-name < template-argument-list [opt] >
7444 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7445 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7446 returned. Otherwise, if the template-name names a function, or set
7447 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7448 names a class, returns a TYPE_DECL for the specialization.
7450 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7451 uninstantiated templates. */
7454 cp_parser_template_id (cp_parser
*parser
,
7455 bool template_keyword_p
,
7456 bool check_dependency_p
)
7461 tree saved_qualifying_scope
;
7462 tree saved_object_scope
;
7464 bool saved_greater_than_is_operator_p
;
7465 ptrdiff_t start_of_id
;
7466 tree access_check
= NULL_TREE
;
7467 cp_token
*next_token
;
7469 /* If the next token corresponds to a template-id, there is no need
7471 next_token
= cp_lexer_peek_token (parser
->lexer
);
7472 if (next_token
->type
== CPP_TEMPLATE_ID
)
7477 /* Get the stored value. */
7478 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
7479 /* Perform any access checks that were deferred. */
7480 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
7481 perform_or_defer_access_check (TREE_PURPOSE (check
),
7482 TREE_VALUE (check
));
7483 /* Return the stored value. */
7484 return TREE_VALUE (value
);
7487 /* Avoid performing name lookup if there is no possibility of
7488 finding a template-id. */
7489 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
7490 || (next_token
->type
== CPP_NAME
7491 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
))
7493 cp_parser_error (parser
, "expected template-id");
7494 return error_mark_node
;
7497 /* Remember where the template-id starts. */
7498 if (cp_parser_parsing_tentatively (parser
)
7499 && !cp_parser_committed_to_tentative_parse (parser
))
7501 next_token
= cp_lexer_peek_token (parser
->lexer
);
7502 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
7503 parser
->lexer
->first_token
,
7509 push_deferring_access_checks (dk_deferred
);
7511 /* Parse the template-name. */
7512 template = cp_parser_template_name (parser
, template_keyword_p
,
7513 check_dependency_p
);
7514 if (template == error_mark_node
)
7516 pop_deferring_access_checks ();
7517 return error_mark_node
;
7520 /* Look for the `<' that starts the template-argument-list. */
7521 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
7523 pop_deferring_access_checks ();
7524 return error_mark_node
;
7529 When parsing a template-id, the first non-nested `>' is taken as
7530 the end of the template-argument-list rather than a greater-than
7532 saved_greater_than_is_operator_p
7533 = parser
->greater_than_is_operator_p
;
7534 parser
->greater_than_is_operator_p
= false;
7535 /* Parsing the argument list may modify SCOPE, so we save it
7537 saved_scope
= parser
->scope
;
7538 saved_qualifying_scope
= parser
->qualifying_scope
;
7539 saved_object_scope
= parser
->object_scope
;
7540 /* Parse the template-argument-list itself. */
7541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
7542 arguments
= NULL_TREE
;
7544 arguments
= cp_parser_template_argument_list (parser
);
7545 /* Look for the `>' that ends the template-argument-list. */
7546 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7547 /* The `>' token might be a greater-than operator again now. */
7548 parser
->greater_than_is_operator_p
7549 = saved_greater_than_is_operator_p
;
7550 /* Restore the SAVED_SCOPE. */
7551 parser
->scope
= saved_scope
;
7552 parser
->qualifying_scope
= saved_qualifying_scope
;
7553 parser
->object_scope
= saved_object_scope
;
7555 /* Build a representation of the specialization. */
7556 if (TREE_CODE (template) == IDENTIFIER_NODE
)
7557 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
7558 else if (DECL_CLASS_TEMPLATE_P (template)
7559 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7561 = finish_template_type (template, arguments
,
7562 cp_lexer_next_token_is (parser
->lexer
,
7566 /* If it's not a class-template or a template-template, it should be
7567 a function-template. */
7568 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7569 || TREE_CODE (template) == OVERLOAD
7570 || BASELINK_P (template)),
7573 template_id
= lookup_template_function (template, arguments
);
7576 /* Retrieve any deferred checks. Do not pop this access checks yet
7577 so the memory will not be reclaimed during token replacing below. */
7578 access_check
= get_deferred_access_checks ();
7580 /* If parsing tentatively, replace the sequence of tokens that makes
7581 up the template-id with a CPP_TEMPLATE_ID token. That way,
7582 should we re-parse the token stream, we will not have to repeat
7583 the effort required to do the parse, nor will we issue duplicate
7584 error messages about problems during instantiation of the
7586 if (start_of_id
>= 0)
7590 /* Find the token that corresponds to the start of the
7592 token
= cp_lexer_advance_token (parser
->lexer
,
7593 parser
->lexer
->first_token
,
7596 /* Reset the contents of the START_OF_ID token. */
7597 token
->type
= CPP_TEMPLATE_ID
;
7598 token
->value
= build_tree_list (access_check
, template_id
);
7599 token
->keyword
= RID_MAX
;
7600 /* Purge all subsequent tokens. */
7601 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
7604 pop_deferring_access_checks ();
7608 /* Parse a template-name.
7613 The standard should actually say:
7617 operator-function-id
7618 conversion-function-id
7620 A defect report has been filed about this issue.
7622 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7623 `template' keyword, in a construction like:
7627 In that case `f' is taken to be a template-name, even though there
7628 is no way of knowing for sure.
7630 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7631 name refers to a set of overloaded functions, at least one of which
7632 is a template, or an IDENTIFIER_NODE with the name of the template,
7633 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7634 names are looked up inside uninstantiated templates. */
7637 cp_parser_template_name (cp_parser
* parser
,
7638 bool template_keyword_p
,
7639 bool check_dependency_p
)
7645 /* If the next token is `operator', then we have either an
7646 operator-function-id or a conversion-function-id. */
7647 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
7649 /* We don't know whether we're looking at an
7650 operator-function-id or a conversion-function-id. */
7651 cp_parser_parse_tentatively (parser
);
7652 /* Try an operator-function-id. */
7653 identifier
= cp_parser_operator_function_id (parser
);
7654 /* If that didn't work, try a conversion-function-id. */
7655 if (!cp_parser_parse_definitely (parser
))
7656 identifier
= cp_parser_conversion_function_id (parser
);
7658 /* Look for the identifier. */
7660 identifier
= cp_parser_identifier (parser
);
7662 /* If we didn't find an identifier, we don't have a template-id. */
7663 if (identifier
== error_mark_node
)
7664 return error_mark_node
;
7666 /* If the name immediately followed the `template' keyword, then it
7667 is a template-name. However, if the next token is not `<', then
7668 we do not treat it as a template-name, since it is not being used
7669 as part of a template-id. This enables us to handle constructs
7672 template <typename T> struct S { S(); };
7673 template <typename T> S<T>::S();
7675 correctly. We would treat `S' as a template -- if it were `S<T>'
7676 -- but we do not if there is no `<'. */
7677 if (template_keyword_p
&& processing_template_decl
7678 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
7681 /* Look up the name. */
7682 decl
= cp_parser_lookup_name (parser
, identifier
,
7684 /*is_namespace=*/false,
7685 check_dependency_p
);
7686 decl
= maybe_get_template_decl_from_type_decl (decl
);
7688 /* If DECL is a template, then the name was a template-name. */
7689 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7693 /* The standard does not explicitly indicate whether a name that
7694 names a set of overloaded declarations, some of which are
7695 templates, is a template-name. However, such a name should
7696 be a template-name; otherwise, there is no way to form a
7697 template-id for the overloaded templates. */
7698 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
7699 if (TREE_CODE (fns
) == OVERLOAD
)
7703 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
7704 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
7709 /* Otherwise, the name does not name a template. */
7710 cp_parser_error (parser
, "expected template-name");
7711 return error_mark_node
;
7715 /* If DECL is dependent, and refers to a function, then just return
7716 its name; we will look it up again during template instantiation. */
7717 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
7719 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
7720 if (TYPE_P (scope
) && dependent_type_p (scope
))
7727 /* Parse a template-argument-list.
7729 template-argument-list:
7731 template-argument-list , template-argument
7733 Returns a TREE_VEC containing the arguments. */
7736 cp_parser_template_argument_list (cp_parser
* parser
)
7738 tree fixed_args
[10];
7739 unsigned n_args
= 0;
7740 unsigned alloced
= 10;
7741 tree
*arg_ary
= fixed_args
;
7749 /* Consume the comma. */
7750 cp_lexer_consume_token (parser
->lexer
);
7752 /* Parse the template-argument. */
7753 argument
= cp_parser_template_argument (parser
);
7754 if (n_args
== alloced
)
7758 if (arg_ary
== fixed_args
)
7760 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
7761 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
7764 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
7766 arg_ary
[n_args
++] = argument
;
7768 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
7770 vec
= make_tree_vec (n_args
);
7773 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
7775 if (arg_ary
!= fixed_args
)
7780 /* Parse a template-argument.
7783 assignment-expression
7787 The representation is that of an assignment-expression, type-id, or
7788 id-expression -- except that the qualified id-expression is
7789 evaluated, so that the value returned is either a DECL or an
7792 Although the standard says "assignment-expression", it forbids
7793 throw-expressions or assignments in the template argument.
7794 Therefore, we use "conditional-expression" instead. */
7797 cp_parser_template_argument (cp_parser
* parser
)
7804 tree qualifying_class
;
7806 /* There's really no way to know what we're looking at, so we just
7807 try each alternative in order.
7811 In a template-argument, an ambiguity between a type-id and an
7812 expression is resolved to a type-id, regardless of the form of
7813 the corresponding template-parameter.
7815 Therefore, we try a type-id first. */
7816 cp_parser_parse_tentatively (parser
);
7817 argument
= cp_parser_type_id (parser
);
7818 /* If the next token isn't a `,' or a `>', then this argument wasn't
7820 if (!cp_parser_next_token_ends_template_argument_p (parser
))
7821 cp_parser_error (parser
, "expected template-argument");
7822 /* If that worked, we're done. */
7823 if (cp_parser_parse_definitely (parser
))
7825 /* We're still not sure what the argument will be. */
7826 cp_parser_parse_tentatively (parser
);
7827 /* Try a template. */
7828 argument
= cp_parser_id_expression (parser
,
7829 /*template_keyword_p=*/false,
7830 /*check_dependency_p=*/true,
7832 /*declarator_p=*/false);
7833 /* If the next token isn't a `,' or a `>', then this argument wasn't
7835 if (!cp_parser_next_token_ends_template_argument_p (parser
))
7836 cp_parser_error (parser
, "expected template-argument");
7837 if (!cp_parser_error_occurred (parser
))
7839 /* Figure out what is being referred to. */
7840 argument
= cp_parser_lookup_name_simple (parser
, argument
);
7842 argument
= make_unbound_class_template (TREE_OPERAND (argument
, 0),
7843 TREE_OPERAND (argument
, 1),
7845 else if (TREE_CODE (argument
) != TEMPLATE_DECL
)
7846 cp_parser_error (parser
, "expected template-name");
7848 if (cp_parser_parse_definitely (parser
))
7850 /* It must be a non-type argument. There permitted cases are given
7851 in [temp.arg.nontype]:
7853 -- an integral constant-expression of integral or enumeration
7856 -- the name of a non-type template-parameter; or
7858 -- the name of an object or function with external linkage...
7860 -- the address of an object or function with external linkage...
7862 -- a pointer to member... */
7863 /* Look for a non-type template parameter. */
7864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7866 cp_parser_parse_tentatively (parser
);
7867 argument
= cp_parser_primary_expression (parser
,
7870 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
7871 || !cp_parser_next_token_ends_template_argument_p (parser
))
7872 cp_parser_simulate_error (parser
);
7873 if (cp_parser_parse_definitely (parser
))
7876 /* If the next token is "&", the argument must be the address of an
7877 object or function with external linkage. */
7878 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
7880 cp_lexer_consume_token (parser
->lexer
);
7881 /* See if we might have an id-expression. */
7882 token
= cp_lexer_peek_token (parser
->lexer
);
7883 if (token
->type
== CPP_NAME
7884 || token
->keyword
== RID_OPERATOR
7885 || token
->type
== CPP_SCOPE
7886 || token
->type
== CPP_TEMPLATE_ID
7887 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
7889 cp_parser_parse_tentatively (parser
);
7890 argument
= cp_parser_primary_expression (parser
,
7893 if (cp_parser_error_occurred (parser
)
7894 || !cp_parser_next_token_ends_template_argument_p (parser
))
7895 cp_parser_abort_tentative_parse (parser
);
7898 if (qualifying_class
)
7899 argument
= finish_qualified_id_expr (qualifying_class
,
7903 if (TREE_CODE (argument
) == VAR_DECL
)
7905 /* A variable without external linkage might still be a
7906 valid constant-expression, so no error is issued here
7907 if the external-linkage check fails. */
7908 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
7909 cp_parser_simulate_error (parser
);
7911 else if (is_overloaded_fn (argument
))
7912 /* All overloaded functions are allowed; if the external
7913 linkage test does not pass, an error will be issued
7917 && (TREE_CODE (argument
) == OFFSET_REF
7918 || TREE_CODE (argument
) == SCOPE_REF
))
7919 /* A pointer-to-member. */
7922 cp_parser_simulate_error (parser
);
7924 if (cp_parser_parse_definitely (parser
))
7927 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
7932 /* If the argument started with "&", there are no other valid
7933 alternatives at this point. */
7936 cp_parser_error (parser
, "invalid non-type template argument");
7937 return error_mark_node
;
7939 /* The argument must be a constant-expression. */
7940 argument
= cp_parser_constant_expression (parser
,
7941 /*allow_non_constant_p=*/false,
7942 /*non_constant_p=*/NULL
);
7943 /* If it's non-dependent, simplify it. */
7944 return cp_parser_fold_non_dependent_expr (argument
);
7947 /* Parse an explicit-instantiation.
7949 explicit-instantiation:
7950 template declaration
7952 Although the standard says `declaration', what it really means is:
7954 explicit-instantiation:
7955 template decl-specifier-seq [opt] declarator [opt] ;
7957 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7958 supposed to be allowed. A defect report has been filed about this
7963 explicit-instantiation:
7964 storage-class-specifier template
7965 decl-specifier-seq [opt] declarator [opt] ;
7966 function-specifier template
7967 decl-specifier-seq [opt] declarator [opt] ; */
7970 cp_parser_explicit_instantiation (cp_parser
* parser
)
7972 int declares_class_or_enum
;
7973 tree decl_specifiers
;
7975 tree extension_specifier
= NULL_TREE
;
7977 /* Look for an (optional) storage-class-specifier or
7978 function-specifier. */
7979 if (cp_parser_allow_gnu_extensions_p (parser
))
7982 = cp_parser_storage_class_specifier_opt (parser
);
7983 if (!extension_specifier
)
7984 extension_specifier
= cp_parser_function_specifier_opt (parser
);
7987 /* Look for the `template' keyword. */
7988 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
7989 /* Let the front end know that we are processing an explicit
7991 begin_explicit_instantiation ();
7992 /* [temp.explicit] says that we are supposed to ignore access
7993 control while processing explicit instantiation directives. */
7994 push_deferring_access_checks (dk_no_check
);
7995 /* Parse a decl-specifier-seq. */
7997 = cp_parser_decl_specifier_seq (parser
,
7998 CP_PARSER_FLAGS_OPTIONAL
,
8000 &declares_class_or_enum
);
8001 /* If there was exactly one decl-specifier, and it declared a class,
8002 and there's no declarator, then we have an explicit type
8004 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8008 type
= check_tag_decl (decl_specifiers
);
8009 /* Turn access control back on for names used during
8010 template instantiation. */
8011 pop_deferring_access_checks ();
8013 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8020 /* Parse the declarator. */
8022 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8023 /*ctor_dtor_or_conv_p=*/NULL
);
8024 cp_parser_check_for_definition_in_return_type (declarator
,
8025 declares_class_or_enum
);
8026 decl
= grokdeclarator (declarator
, decl_specifiers
,
8028 /* Turn access control back on for names used during
8029 template instantiation. */
8030 pop_deferring_access_checks ();
8031 /* Do the explicit instantiation. */
8032 do_decl_instantiation (decl
, extension_specifier
);
8034 /* We're done with the instantiation. */
8035 end_explicit_instantiation ();
8037 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8040 /* Parse an explicit-specialization.
8042 explicit-specialization:
8043 template < > declaration
8045 Although the standard says `declaration', what it really means is:
8047 explicit-specialization:
8048 template <> decl-specifier [opt] init-declarator [opt] ;
8049 template <> function-definition
8050 template <> explicit-specialization
8051 template <> template-declaration */
8054 cp_parser_explicit_specialization (cp_parser
* parser
)
8056 /* Look for the `template' keyword. */
8057 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8058 /* Look for the `<'. */
8059 cp_parser_require (parser
, CPP_LESS
, "`<'");
8060 /* Look for the `>'. */
8061 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8062 /* We have processed another parameter list. */
8063 ++parser
->num_template_parameter_lists
;
8064 /* Let the front end know that we are beginning a specialization. */
8065 begin_specialization ();
8067 /* If the next keyword is `template', we need to figure out whether
8068 or not we're looking a template-declaration. */
8069 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8071 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8072 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8073 cp_parser_template_declaration_after_export (parser
,
8074 /*member_p=*/false);
8076 cp_parser_explicit_specialization (parser
);
8079 /* Parse the dependent declaration. */
8080 cp_parser_single_declaration (parser
,
8084 /* We're done with the specialization. */
8085 end_specialization ();
8086 /* We're done with this parameter list. */
8087 --parser
->num_template_parameter_lists
;
8090 /* Parse a type-specifier.
8093 simple-type-specifier
8096 elaborated-type-specifier
8104 Returns a representation of the type-specifier. If the
8105 type-specifier is a keyword (like `int' or `const', or
8106 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8107 For a class-specifier, enum-specifier, or elaborated-type-specifier
8108 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8110 If IS_FRIEND is TRUE then this type-specifier is being declared a
8111 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8112 appearing in a decl-specifier-seq.
8114 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8115 class-specifier, enum-specifier, or elaborated-type-specifier, then
8116 *DECLARES_CLASS_OR_ENUM is set to a non-zero value. The value is 1
8117 if a type is declared; 2 if it is defined. Otherwise, it is set to
8120 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8121 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8125 cp_parser_type_specifier (cp_parser
* parser
,
8126 cp_parser_flags flags
,
8128 bool is_declaration
,
8129 int* declares_class_or_enum
,
8130 bool* is_cv_qualifier
)
8132 tree type_spec
= NULL_TREE
;
8136 /* Assume this type-specifier does not declare a new type. */
8137 if (declares_class_or_enum
)
8138 *declares_class_or_enum
= false;
8139 /* And that it does not specify a cv-qualifier. */
8140 if (is_cv_qualifier
)
8141 *is_cv_qualifier
= false;
8142 /* Peek at the next token. */
8143 token
= cp_lexer_peek_token (parser
->lexer
);
8145 /* If we're looking at a keyword, we can use that to guide the
8146 production we choose. */
8147 keyword
= token
->keyword
;
8150 /* Any of these indicate either a class-specifier, or an
8151 elaborated-type-specifier. */
8156 /* Parse tentatively so that we can back up if we don't find a
8157 class-specifier or enum-specifier. */
8158 cp_parser_parse_tentatively (parser
);
8159 /* Look for the class-specifier or enum-specifier. */
8160 if (keyword
== RID_ENUM
)
8161 type_spec
= cp_parser_enum_specifier (parser
);
8163 type_spec
= cp_parser_class_specifier (parser
);
8165 /* If that worked, we're done. */
8166 if (cp_parser_parse_definitely (parser
))
8168 if (declares_class_or_enum
)
8169 *declares_class_or_enum
= 2;
8176 /* Look for an elaborated-type-specifier. */
8177 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8180 /* We're declaring a class or enum -- unless we're using
8182 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8183 *declares_class_or_enum
= 1;
8189 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8190 /* Even though we call a routine that looks for an optional
8191 qualifier, we know that there should be one. */
8192 my_friendly_assert (type_spec
!= NULL
, 20000328);
8193 /* This type-specifier was a cv-qualified. */
8194 if (is_cv_qualifier
)
8195 *is_cv_qualifier
= true;
8200 /* The `__complex__' keyword is a GNU extension. */
8201 return cp_lexer_consume_token (parser
->lexer
)->value
;
8207 /* If we do not already have a type-specifier, assume we are looking
8208 at a simple-type-specifier. */
8209 type_spec
= cp_parser_simple_type_specifier (parser
, flags
,
8210 /*identifier_p=*/true);
8212 /* If we didn't find a type-specifier, and a type-specifier was not
8213 optional in this context, issue an error message. */
8214 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8216 cp_parser_error (parser
, "expected type specifier");
8217 return error_mark_node
;
8223 /* Parse a simple-type-specifier.
8225 simple-type-specifier:
8226 :: [opt] nested-name-specifier [opt] type-name
8227 :: [opt] nested-name-specifier template template-id
8242 simple-type-specifier:
8243 __typeof__ unary-expression
8244 __typeof__ ( type-id )
8246 For the various keywords, the value returned is simply the
8247 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8248 For the first two productions, and if IDENTIFIER_P is false, the
8249 value returned is the indicated TYPE_DECL. */
8252 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
,
8255 tree type
= NULL_TREE
;
8258 /* Peek at the next token. */
8259 token
= cp_lexer_peek_token (parser
->lexer
);
8261 /* If we're looking at a keyword, things are easy. */
8262 switch (token
->keyword
)
8265 type
= char_type_node
;
8268 type
= wchar_type_node
;
8271 type
= boolean_type_node
;
8274 type
= short_integer_type_node
;
8277 type
= integer_type_node
;
8280 type
= long_integer_type_node
;
8283 type
= integer_type_node
;
8286 type
= unsigned_type_node
;
8289 type
= float_type_node
;
8292 type
= double_type_node
;
8295 type
= void_type_node
;
8302 /* Consume the `typeof' token. */
8303 cp_lexer_consume_token (parser
->lexer
);
8304 /* Parse the operand to `typeof' */
8305 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
8306 /* If it is not already a TYPE, take its type. */
8307 if (!TYPE_P (operand
))
8308 operand
= finish_typeof (operand
);
8317 /* If the type-specifier was for a built-in type, we're done. */
8322 /* Consume the token. */
8323 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
8324 return identifier_p
? id
: TYPE_NAME (type
);
8327 /* The type-specifier must be a user-defined type. */
8328 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
8330 /* Don't gobble tokens or issue error messages if this is an
8331 optional type-specifier. */
8332 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
8333 cp_parser_parse_tentatively (parser
);
8335 /* Look for the optional `::' operator. */
8336 cp_parser_global_scope_opt (parser
,
8337 /*current_scope_valid_p=*/false);
8338 /* Look for the nested-name specifier. */
8339 cp_parser_nested_name_specifier_opt (parser
,
8340 /*typename_keyword_p=*/false,
8341 /*check_dependency_p=*/true,
8343 /* If we have seen a nested-name-specifier, and the next token
8344 is `template', then we are using the template-id production. */
8346 && cp_parser_optional_template_keyword (parser
))
8348 /* Look for the template-id. */
8349 type
= cp_parser_template_id (parser
,
8350 /*template_keyword_p=*/true,
8351 /*check_dependency_p=*/true);
8352 /* If the template-id did not name a type, we are out of
8354 if (TREE_CODE (type
) != TYPE_DECL
)
8356 cp_parser_error (parser
, "expected template-id for type");
8360 /* Otherwise, look for a type-name. */
8363 type
= cp_parser_type_name (parser
);
8364 if (type
== error_mark_node
)
8368 /* If it didn't work out, we don't have a TYPE. */
8369 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
8370 && !cp_parser_parse_definitely (parser
))
8374 /* If we didn't get a type-name, issue an error message. */
8375 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8377 cp_parser_error (parser
, "expected type-name");
8378 return error_mark_node
;
8384 /* Parse a type-name.
8397 Returns a TYPE_DECL for the the type. */
8400 cp_parser_type_name (cp_parser
* parser
)
8405 /* We can't know yet whether it is a class-name or not. */
8406 cp_parser_parse_tentatively (parser
);
8407 /* Try a class-name. */
8408 type_decl
= cp_parser_class_name (parser
,
8409 /*typename_keyword_p=*/false,
8410 /*template_keyword_p=*/false,
8412 /*check_dependency_p=*/true,
8413 /*class_head_p=*/false);
8414 /* If it's not a class-name, keep looking. */
8415 if (!cp_parser_parse_definitely (parser
))
8417 /* It must be a typedef-name or an enum-name. */
8418 identifier
= cp_parser_identifier (parser
);
8419 if (identifier
== error_mark_node
)
8420 return error_mark_node
;
8422 /* Look up the type-name. */
8423 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
8424 /* Issue an error if we did not find a type-name. */
8425 if (TREE_CODE (type_decl
) != TYPE_DECL
)
8427 cp_parser_error (parser
, "expected type-name");
8428 type_decl
= error_mark_node
;
8430 /* Remember that the name was used in the definition of the
8431 current class so that we can check later to see if the
8432 meaning would have been different after the class was
8433 entirely defined. */
8434 else if (type_decl
!= error_mark_node
8436 maybe_note_name_used_in_class (identifier
, type_decl
);
8443 /* Parse an elaborated-type-specifier. Note that the grammar given
8444 here incorporates the resolution to DR68.
8446 elaborated-type-specifier:
8447 class-key :: [opt] nested-name-specifier [opt] identifier
8448 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8449 enum :: [opt] nested-name-specifier [opt] identifier
8450 typename :: [opt] nested-name-specifier identifier
8451 typename :: [opt] nested-name-specifier template [opt]
8454 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8455 declared `friend'. If IS_DECLARATION is TRUE, then this
8456 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8457 something is being declared.
8459 Returns the TYPE specified. */
8462 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
8464 bool is_declaration
)
8466 enum tag_types tag_type
;
8468 tree type
= NULL_TREE
;
8470 /* See if we're looking at the `enum' keyword. */
8471 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
8473 /* Consume the `enum' token. */
8474 cp_lexer_consume_token (parser
->lexer
);
8475 /* Remember that it's an enumeration type. */
8476 tag_type
= enum_type
;
8478 /* Or, it might be `typename'. */
8479 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8482 /* Consume the `typename' token. */
8483 cp_lexer_consume_token (parser
->lexer
);
8484 /* Remember that it's a `typename' type. */
8485 tag_type
= typename_type
;
8486 /* The `typename' keyword is only allowed in templates. */
8487 if (!processing_template_decl
)
8488 pedwarn ("using `typename' outside of template");
8490 /* Otherwise it must be a class-key. */
8493 tag_type
= cp_parser_class_key (parser
);
8494 if (tag_type
== none_type
)
8495 return error_mark_node
;
8498 /* Look for the `::' operator. */
8499 cp_parser_global_scope_opt (parser
,
8500 /*current_scope_valid_p=*/false);
8501 /* Look for the nested-name-specifier. */
8502 if (tag_type
== typename_type
)
8504 if (cp_parser_nested_name_specifier (parser
,
8505 /*typename_keyword_p=*/true,
8506 /*check_dependency_p=*/true,
8509 return error_mark_node
;
8512 /* Even though `typename' is not present, the proposed resolution
8513 to Core Issue 180 says that in `class A<T>::B', `B' should be
8514 considered a type-name, even if `A<T>' is dependent. */
8515 cp_parser_nested_name_specifier_opt (parser
,
8516 /*typename_keyword_p=*/true,
8517 /*check_dependency_p=*/true,
8519 /* For everything but enumeration types, consider a template-id. */
8520 if (tag_type
!= enum_type
)
8522 bool template_p
= false;
8525 /* Allow the `template' keyword. */
8526 template_p
= cp_parser_optional_template_keyword (parser
);
8527 /* If we didn't see `template', we don't know if there's a
8528 template-id or not. */
8530 cp_parser_parse_tentatively (parser
);
8531 /* Parse the template-id. */
8532 decl
= cp_parser_template_id (parser
, template_p
,
8533 /*check_dependency_p=*/true);
8534 /* If we didn't find a template-id, look for an ordinary
8536 if (!template_p
&& !cp_parser_parse_definitely (parser
))
8538 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8539 in effect, then we must assume that, upon instantiation, the
8540 template will correspond to a class. */
8541 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
8542 && tag_type
== typename_type
)
8543 type
= make_typename_type (parser
->scope
, decl
,
8546 type
= TREE_TYPE (decl
);
8549 /* For an enumeration type, consider only a plain identifier. */
8552 identifier
= cp_parser_identifier (parser
);
8554 if (identifier
== error_mark_node
)
8556 parser
->scope
= NULL_TREE
;
8557 return error_mark_node
;
8560 /* For a `typename', we needn't call xref_tag. */
8561 if (tag_type
== typename_type
)
8562 return make_typename_type (parser
->scope
, identifier
,
8564 /* Look up a qualified name in the usual way. */
8569 /* In an elaborated-type-specifier, names are assumed to name
8570 types, so we set IS_TYPE to TRUE when calling
8571 cp_parser_lookup_name. */
8572 decl
= cp_parser_lookup_name (parser
, identifier
,
8574 /*is_namespace=*/false,
8575 /*check_dependency=*/true);
8577 /* If we are parsing friend declaration, DECL may be a
8578 TEMPLATE_DECL tree node here. However, we need to check
8579 whether this TEMPLATE_DECL results in valid code. Consider
8580 the following example:
8583 template <class T> class C {};
8586 template <class T> friend class N::C; // #1, valid code
8588 template <class T> class Y {
8589 friend class N::C; // #2, invalid code
8592 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8593 name lookup of `N::C'. We see that friend declaration must
8594 be template for the code to be valid. Note that
8595 processing_template_decl does not work here since it is
8596 always 1 for the above two cases. */
8598 decl
= (cp_parser_maybe_treat_template_as_class
8599 (decl
, /*tag_name_p=*/is_friend
8600 && parser
->num_template_parameter_lists
));
8602 if (TREE_CODE (decl
) != TYPE_DECL
)
8604 error ("expected type-name");
8605 return error_mark_node
;
8608 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
8609 check_elaborated_type_specifier
8611 (parser
->num_template_parameter_lists
8612 || DECL_SELF_REFERENCE_P (decl
)));
8614 type
= TREE_TYPE (decl
);
8618 /* An elaborated-type-specifier sometimes introduces a new type and
8619 sometimes names an existing type. Normally, the rule is that it
8620 introduces a new type only if there is not an existing type of
8621 the same name already in scope. For example, given:
8624 void f() { struct S s; }
8626 the `struct S' in the body of `f' is the same `struct S' as in
8627 the global scope; the existing definition is used. However, if
8628 there were no global declaration, this would introduce a new
8629 local class named `S'.
8631 An exception to this rule applies to the following code:
8633 namespace N { struct S; }
8635 Here, the elaborated-type-specifier names a new type
8636 unconditionally; even if there is already an `S' in the
8637 containing scope this declaration names a new type.
8638 This exception only applies if the elaborated-type-specifier
8639 forms the complete declaration:
8643 A declaration consisting solely of `class-key identifier ;' is
8644 either a redeclaration of the name in the current scope or a
8645 forward declaration of the identifier as a class name. It
8646 introduces the name into the current scope.
8648 We are in this situation precisely when the next token is a `;'.
8650 An exception to the exception is that a `friend' declaration does
8651 *not* name a new type; i.e., given:
8653 struct S { friend struct T; };
8655 `T' is not a new type in the scope of `S'.
8657 Also, `new struct S' or `sizeof (struct S)' never results in the
8658 definition of a new type; a new type can only be declared in a
8659 declaration context. */
8661 type
= xref_tag (tag_type
, identifier
,
8662 /*attributes=*/NULL_TREE
,
8665 || cp_lexer_next_token_is_not (parser
->lexer
,
8667 parser
->num_template_parameter_lists
);
8670 if (tag_type
!= enum_type
)
8671 cp_parser_check_class_key (tag_type
, type
);
8675 /* Parse an enum-specifier.
8678 enum identifier [opt] { enumerator-list [opt] }
8680 Returns an ENUM_TYPE representing the enumeration. */
8683 cp_parser_enum_specifier (cp_parser
* parser
)
8686 tree identifier
= NULL_TREE
;
8689 /* Look for the `enum' keyword. */
8690 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
8691 return error_mark_node
;
8692 /* Peek at the next token. */
8693 token
= cp_lexer_peek_token (parser
->lexer
);
8695 /* See if it is an identifier. */
8696 if (token
->type
== CPP_NAME
)
8697 identifier
= cp_parser_identifier (parser
);
8699 /* Look for the `{'. */
8700 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
8701 return error_mark_node
;
8703 /* At this point, we're going ahead with the enum-specifier, even
8704 if some other problem occurs. */
8705 cp_parser_commit_to_tentative_parse (parser
);
8707 /* Issue an error message if type-definitions are forbidden here. */
8708 cp_parser_check_type_definition (parser
);
8710 /* Create the new type. */
8711 type
= start_enum (identifier
? identifier
: make_anon_name ());
8713 /* Peek at the next token. */
8714 token
= cp_lexer_peek_token (parser
->lexer
);
8715 /* If it's not a `}', then there are some enumerators. */
8716 if (token
->type
!= CPP_CLOSE_BRACE
)
8717 cp_parser_enumerator_list (parser
, type
);
8718 /* Look for the `}'. */
8719 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8721 /* Finish up the enumeration. */
8727 /* Parse an enumerator-list. The enumerators all have the indicated
8731 enumerator-definition
8732 enumerator-list , enumerator-definition */
8735 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
8741 /* Parse an enumerator-definition. */
8742 cp_parser_enumerator_definition (parser
, type
);
8743 /* Peek at the next token. */
8744 token
= cp_lexer_peek_token (parser
->lexer
);
8745 /* If it's not a `,', then we've reached the end of the
8747 if (token
->type
!= CPP_COMMA
)
8749 /* Otherwise, consume the `,' and keep going. */
8750 cp_lexer_consume_token (parser
->lexer
);
8751 /* If the next token is a `}', there is a trailing comma. */
8752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
8754 if (pedantic
&& !in_system_header
)
8755 pedwarn ("comma at end of enumerator list");
8761 /* Parse an enumerator-definition. The enumerator has the indicated
8764 enumerator-definition:
8766 enumerator = constant-expression
8772 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
8778 /* Look for the identifier. */
8779 identifier
= cp_parser_identifier (parser
);
8780 if (identifier
== error_mark_node
)
8783 /* Peek at the next token. */
8784 token
= cp_lexer_peek_token (parser
->lexer
);
8785 /* If it's an `=', then there's an explicit value. */
8786 if (token
->type
== CPP_EQ
)
8788 /* Consume the `=' token. */
8789 cp_lexer_consume_token (parser
->lexer
);
8790 /* Parse the value. */
8791 value
= cp_parser_constant_expression (parser
,
8792 /*allow_non_constant_p=*/false,
8798 /* Create the enumerator. */
8799 build_enumerator (identifier
, value
, type
);
8802 /* Parse a namespace-name.
8805 original-namespace-name
8808 Returns the NAMESPACE_DECL for the namespace. */
8811 cp_parser_namespace_name (cp_parser
* parser
)
8814 tree namespace_decl
;
8816 /* Get the name of the namespace. */
8817 identifier
= cp_parser_identifier (parser
);
8818 if (identifier
== error_mark_node
)
8819 return error_mark_node
;
8821 /* Look up the identifier in the currently active scope. Look only
8822 for namespaces, due to:
8826 When looking up a namespace-name in a using-directive or alias
8827 definition, only namespace names are considered.
8833 During the lookup of a name preceding the :: scope resolution
8834 operator, object, function, and enumerator names are ignored.
8836 (Note that cp_parser_class_or_namespace_name only calls this
8837 function if the token after the name is the scope resolution
8839 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
8841 /*is_namespace=*/true,
8842 /*check_dependency=*/true);
8843 /* If it's not a namespace, issue an error. */
8844 if (namespace_decl
== error_mark_node
8845 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
8847 cp_parser_error (parser
, "expected namespace-name");
8848 namespace_decl
= error_mark_node
;
8851 return namespace_decl
;
8854 /* Parse a namespace-definition.
8856 namespace-definition:
8857 named-namespace-definition
8858 unnamed-namespace-definition
8860 named-namespace-definition:
8861 original-namespace-definition
8862 extension-namespace-definition
8864 original-namespace-definition:
8865 namespace identifier { namespace-body }
8867 extension-namespace-definition:
8868 namespace original-namespace-name { namespace-body }
8870 unnamed-namespace-definition:
8871 namespace { namespace-body } */
8874 cp_parser_namespace_definition (cp_parser
* parser
)
8878 /* Look for the `namespace' keyword. */
8879 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
8881 /* Get the name of the namespace. We do not attempt to distinguish
8882 between an original-namespace-definition and an
8883 extension-namespace-definition at this point. The semantic
8884 analysis routines are responsible for that. */
8885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8886 identifier
= cp_parser_identifier (parser
);
8888 identifier
= NULL_TREE
;
8890 /* Look for the `{' to start the namespace. */
8891 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
8892 /* Start the namespace. */
8893 push_namespace (identifier
);
8894 /* Parse the body of the namespace. */
8895 cp_parser_namespace_body (parser
);
8896 /* Finish the namespace. */
8898 /* Look for the final `}'. */
8899 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8902 /* Parse a namespace-body.
8905 declaration-seq [opt] */
8908 cp_parser_namespace_body (cp_parser
* parser
)
8910 cp_parser_declaration_seq_opt (parser
);
8913 /* Parse a namespace-alias-definition.
8915 namespace-alias-definition:
8916 namespace identifier = qualified-namespace-specifier ; */
8919 cp_parser_namespace_alias_definition (cp_parser
* parser
)
8922 tree namespace_specifier
;
8924 /* Look for the `namespace' keyword. */
8925 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
8926 /* Look for the identifier. */
8927 identifier
= cp_parser_identifier (parser
);
8928 if (identifier
== error_mark_node
)
8930 /* Look for the `=' token. */
8931 cp_parser_require (parser
, CPP_EQ
, "`='");
8932 /* Look for the qualified-namespace-specifier. */
8934 = cp_parser_qualified_namespace_specifier (parser
);
8935 /* Look for the `;' token. */
8936 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8938 /* Register the alias in the symbol table. */
8939 do_namespace_alias (identifier
, namespace_specifier
);
8942 /* Parse a qualified-namespace-specifier.
8944 qualified-namespace-specifier:
8945 :: [opt] nested-name-specifier [opt] namespace-name
8947 Returns a NAMESPACE_DECL corresponding to the specified
8951 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
8953 /* Look for the optional `::'. */
8954 cp_parser_global_scope_opt (parser
,
8955 /*current_scope_valid_p=*/false);
8957 /* Look for the optional nested-name-specifier. */
8958 cp_parser_nested_name_specifier_opt (parser
,
8959 /*typename_keyword_p=*/false,
8960 /*check_dependency_p=*/true,
8963 return cp_parser_namespace_name (parser
);
8966 /* Parse a using-declaration.
8969 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8970 using :: unqualified-id ; */
8973 cp_parser_using_declaration (cp_parser
* parser
)
8976 bool typename_p
= false;
8977 bool global_scope_p
;
8982 /* Look for the `using' keyword. */
8983 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
8985 /* Peek at the next token. */
8986 token
= cp_lexer_peek_token (parser
->lexer
);
8987 /* See if it's `typename'. */
8988 if (token
->keyword
== RID_TYPENAME
)
8990 /* Remember that we've seen it. */
8992 /* Consume the `typename' token. */
8993 cp_lexer_consume_token (parser
->lexer
);
8996 /* Look for the optional global scope qualification. */
8998 = (cp_parser_global_scope_opt (parser
,
8999 /*current_scope_valid_p=*/false)
9002 /* If we saw `typename', or didn't see `::', then there must be a
9003 nested-name-specifier present. */
9004 if (typename_p
|| !global_scope_p
)
9005 cp_parser_nested_name_specifier (parser
, typename_p
,
9006 /*check_dependency_p=*/true,
9008 /* Otherwise, we could be in either of the two productions. In that
9009 case, treat the nested-name-specifier as optional. */
9011 cp_parser_nested_name_specifier_opt (parser
,
9012 /*typename_keyword_p=*/false,
9013 /*check_dependency_p=*/true,
9016 /* Parse the unqualified-id. */
9017 identifier
= cp_parser_unqualified_id (parser
,
9018 /*template_keyword_p=*/false,
9019 /*check_dependency_p=*/true,
9020 /*declarator_p=*/true);
9022 /* The function we call to handle a using-declaration is different
9023 depending on what scope we are in. */
9024 if (identifier
== error_mark_node
)
9026 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
9027 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
9028 /* [namespace.udecl]
9030 A using declaration shall not name a template-id. */
9031 error ("a template-id may not appear in a using-declaration");
9034 scope
= current_scope ();
9035 if (scope
&& TYPE_P (scope
))
9037 /* Create the USING_DECL. */
9038 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9041 /* Add it to the list of members in this class. */
9042 finish_member_declaration (decl
);
9046 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9047 if (decl
== error_mark_node
)
9049 if (parser
->scope
&& parser
->scope
!= global_namespace
)
9050 error ("`%D::%D' has not been declared",
9051 parser
->scope
, identifier
);
9053 error ("`::%D' has not been declared", identifier
);
9056 do_local_using_decl (decl
);
9058 do_toplevel_using_decl (decl
);
9062 /* Look for the final `;'. */
9063 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9066 /* Parse a using-directive.
9069 using namespace :: [opt] nested-name-specifier [opt]
9073 cp_parser_using_directive (cp_parser
* parser
)
9075 tree namespace_decl
;
9077 /* Look for the `using' keyword. */
9078 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9079 /* And the `namespace' keyword. */
9080 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9081 /* Look for the optional `::' operator. */
9082 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9083 /* And the optional nested-name-specifier. */
9084 cp_parser_nested_name_specifier_opt (parser
,
9085 /*typename_keyword_p=*/false,
9086 /*check_dependency_p=*/true,
9088 /* Get the namespace being used. */
9089 namespace_decl
= cp_parser_namespace_name (parser
);
9090 /* Update the symbol table. */
9091 do_using_directive (namespace_decl
);
9092 /* Look for the final `;'. */
9093 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9096 /* Parse an asm-definition.
9099 asm ( string-literal ) ;
9104 asm volatile [opt] ( string-literal ) ;
9105 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9106 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9107 : asm-operand-list [opt] ) ;
9108 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9109 : asm-operand-list [opt]
9110 : asm-operand-list [opt] ) ; */
9113 cp_parser_asm_definition (cp_parser
* parser
)
9117 tree outputs
= NULL_TREE
;
9118 tree inputs
= NULL_TREE
;
9119 tree clobbers
= NULL_TREE
;
9121 bool volatile_p
= false;
9122 bool extended_p
= false;
9124 /* Look for the `asm' keyword. */
9125 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9126 /* See if the next token is `volatile'. */
9127 if (cp_parser_allow_gnu_extensions_p (parser
)
9128 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9130 /* Remember that we saw the `volatile' keyword. */
9132 /* Consume the token. */
9133 cp_lexer_consume_token (parser
->lexer
);
9135 /* Look for the opening `('. */
9136 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9137 /* Look for the string. */
9138 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9141 string
= token
->value
;
9142 /* If we're allowing GNU extensions, check for the extended assembly
9143 syntax. Unfortunately, the `:' tokens need not be separated by
9144 a space in C, and so, for compatibility, we tolerate that here
9145 too. Doing that means that we have to treat the `::' operator as
9147 if (cp_parser_allow_gnu_extensions_p (parser
)
9148 && at_function_scope_p ()
9149 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9150 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9152 bool inputs_p
= false;
9153 bool clobbers_p
= false;
9155 /* The extended syntax was used. */
9158 /* Look for outputs. */
9159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9161 /* Consume the `:'. */
9162 cp_lexer_consume_token (parser
->lexer
);
9163 /* Parse the output-operands. */
9164 if (cp_lexer_next_token_is_not (parser
->lexer
,
9166 && cp_lexer_next_token_is_not (parser
->lexer
,
9168 && cp_lexer_next_token_is_not (parser
->lexer
,
9170 outputs
= cp_parser_asm_operand_list (parser
);
9172 /* If the next token is `::', there are no outputs, and the
9173 next token is the beginning of the inputs. */
9174 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9176 /* Consume the `::' token. */
9177 cp_lexer_consume_token (parser
->lexer
);
9178 /* The inputs are coming next. */
9182 /* Look for inputs. */
9184 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9187 /* Consume the `:'. */
9188 cp_lexer_consume_token (parser
->lexer
);
9189 /* Parse the output-operands. */
9190 if (cp_lexer_next_token_is_not (parser
->lexer
,
9192 && cp_lexer_next_token_is_not (parser
->lexer
,
9194 && cp_lexer_next_token_is_not (parser
->lexer
,
9196 inputs
= cp_parser_asm_operand_list (parser
);
9198 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9199 /* The clobbers are coming next. */
9202 /* Look for clobbers. */
9204 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9207 /* Consume the `:'. */
9208 cp_lexer_consume_token (parser
->lexer
);
9209 /* Parse the clobbers. */
9210 if (cp_lexer_next_token_is_not (parser
->lexer
,
9212 clobbers
= cp_parser_asm_clobber_list (parser
);
9215 /* Look for the closing `)'. */
9216 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9217 cp_parser_skip_to_closing_parenthesis (parser
, true, false);
9218 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9220 /* Create the ASM_STMT. */
9221 if (at_function_scope_p ())
9224 finish_asm_stmt (volatile_p
9225 ? ridpointers
[(int) RID_VOLATILE
] : NULL_TREE
,
9226 string
, outputs
, inputs
, clobbers
);
9227 /* If the extended syntax was not used, mark the ASM_STMT. */
9229 ASM_INPUT_P (asm_stmt
) = 1;
9232 assemble_asm (string
);
9235 /* Declarators [gram.dcl.decl] */
9237 /* Parse an init-declarator.
9240 declarator initializer [opt]
9245 declarator asm-specification [opt] attributes [opt] initializer [opt]
9247 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9248 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9249 then this declarator appears in a class scope. The new DECL created
9250 by this declarator is returned.
9252 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9253 for a function-definition here as well. If the declarator is a
9254 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9255 be TRUE upon return. By that point, the function-definition will
9256 have been completely parsed.
9258 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9262 cp_parser_init_declarator (cp_parser
* parser
,
9263 tree decl_specifiers
,
9264 tree prefix_attributes
,
9265 bool function_definition_allowed_p
,
9267 int declares_class_or_enum
,
9268 bool* function_definition_p
)
9273 tree asm_specification
;
9275 tree decl
= NULL_TREE
;
9277 bool is_initialized
;
9278 bool is_parenthesized_init
;
9279 bool is_non_constant_init
;
9280 int ctor_dtor_or_conv_p
;
9283 /* Assume that this is not the declarator for a function
9285 if (function_definition_p
)
9286 *function_definition_p
= false;
9288 /* Defer access checks while parsing the declarator; we cannot know
9289 what names are accessible until we know what is being
9291 resume_deferring_access_checks ();
9293 /* Parse the declarator. */
9295 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9296 &ctor_dtor_or_conv_p
);
9297 /* Gather up the deferred checks. */
9298 stop_deferring_access_checks ();
9300 /* If the DECLARATOR was erroneous, there's no need to go
9302 if (declarator
== error_mark_node
)
9303 return error_mark_node
;
9305 cp_parser_check_for_definition_in_return_type (declarator
,
9306 declares_class_or_enum
);
9308 /* Figure out what scope the entity declared by the DECLARATOR is
9309 located in. `grokdeclarator' sometimes changes the scope, so
9310 we compute it now. */
9311 scope
= get_scope_of_declarator (declarator
);
9313 /* If we're allowing GNU extensions, look for an asm-specification
9315 if (cp_parser_allow_gnu_extensions_p (parser
))
9317 /* Look for an asm-specification. */
9318 asm_specification
= cp_parser_asm_specification_opt (parser
);
9319 /* And attributes. */
9320 attributes
= cp_parser_attributes_opt (parser
);
9324 asm_specification
= NULL_TREE
;
9325 attributes
= NULL_TREE
;
9328 /* Peek at the next token. */
9329 token
= cp_lexer_peek_token (parser
->lexer
);
9330 /* Check to see if the token indicates the start of a
9331 function-definition. */
9332 if (cp_parser_token_starts_function_definition_p (token
))
9334 if (!function_definition_allowed_p
)
9336 /* If a function-definition should not appear here, issue an
9338 cp_parser_error (parser
,
9339 "a function-definition is not allowed here");
9340 return error_mark_node
;
9344 /* Neither attributes nor an asm-specification are allowed
9345 on a function-definition. */
9346 if (asm_specification
)
9347 error ("an asm-specification is not allowed on a function-definition");
9349 error ("attributes are not allowed on a function-definition");
9350 /* This is a function-definition. */
9351 *function_definition_p
= true;
9353 /* Parse the function definition. */
9354 decl
= (cp_parser_function_definition_from_specifiers_and_declarator
9355 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
9363 Only in function declarations for constructors, destructors, and
9364 type conversions can the decl-specifier-seq be omitted.
9366 We explicitly postpone this check past the point where we handle
9367 function-definitions because we tolerate function-definitions
9368 that are missing their return types in some modes. */
9369 if (!decl_specifiers
&& ctor_dtor_or_conv_p
<= 0)
9371 cp_parser_error (parser
,
9372 "expected constructor, destructor, or type conversion");
9373 return error_mark_node
;
9376 /* An `=' or an `(' indicates an initializer. */
9377 is_initialized
= (token
->type
== CPP_EQ
9378 || token
->type
== CPP_OPEN_PAREN
);
9379 /* If the init-declarator isn't initialized and isn't followed by a
9380 `,' or `;', it's not a valid init-declarator. */
9382 && token
->type
!= CPP_COMMA
9383 && token
->type
!= CPP_SEMICOLON
)
9385 cp_parser_error (parser
, "expected init-declarator");
9386 return error_mark_node
;
9389 /* Because start_decl has side-effects, we should only call it if we
9390 know we're going ahead. By this point, we know that we cannot
9391 possibly be looking at any other construct. */
9392 cp_parser_commit_to_tentative_parse (parser
);
9394 /* Check to see whether or not this declaration is a friend. */
9395 friend_p
= cp_parser_friend_p (decl_specifiers
);
9397 /* Check that the number of template-parameter-lists is OK. */
9398 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
9399 return error_mark_node
;
9401 /* Enter the newly declared entry in the symbol table. If we're
9402 processing a declaration in a class-specifier, we wait until
9403 after processing the initializer. */
9406 if (parser
->in_unbraced_linkage_specification_p
)
9408 decl_specifiers
= tree_cons (error_mark_node
,
9409 get_identifier ("extern"),
9411 have_extern_spec
= false;
9413 decl
= start_decl (declarator
, decl_specifiers
,
9414 is_initialized
, attributes
, prefix_attributes
);
9417 /* Enter the SCOPE. That way unqualified names appearing in the
9418 initializer will be looked up in SCOPE. */
9422 /* Perform deferred access control checks, now that we know in which
9423 SCOPE the declared entity resides. */
9424 if (!member_p
&& decl
)
9426 tree saved_current_function_decl
= NULL_TREE
;
9428 /* If the entity being declared is a function, pretend that we
9429 are in its scope. If it is a `friend', it may have access to
9430 things that would not otherwise be accessible. */
9431 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9433 saved_current_function_decl
= current_function_decl
;
9434 current_function_decl
= decl
;
9437 /* Perform the access control checks for the declarator and the
9438 the decl-specifiers. */
9439 perform_deferred_access_checks ();
9441 /* Restore the saved value. */
9442 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9443 current_function_decl
= saved_current_function_decl
;
9446 /* Parse the initializer. */
9448 initializer
= cp_parser_initializer (parser
,
9449 &is_parenthesized_init
,
9450 &is_non_constant_init
);
9453 initializer
= NULL_TREE
;
9454 is_parenthesized_init
= false;
9455 is_non_constant_init
= true;
9458 /* The old parser allows attributes to appear after a parenthesized
9459 initializer. Mark Mitchell proposed removing this functionality
9460 on the GCC mailing lists on 2002-08-13. This parser accepts the
9461 attributes -- but ignores them. */
9462 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
9463 if (cp_parser_attributes_opt (parser
))
9464 warning ("attributes after parenthesized initializer ignored");
9466 /* Leave the SCOPE, now that we have processed the initializer. It
9467 is important to do this before calling cp_finish_decl because it
9468 makes decisions about whether to create DECL_STMTs or not based
9469 on the current scope. */
9473 /* For an in-class declaration, use `grokfield' to create the
9477 decl
= grokfield (declarator
, decl_specifiers
,
9478 initializer
, /*asmspec=*/NULL_TREE
,
9479 /*attributes=*/NULL_TREE
);
9480 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
9481 cp_parser_save_default_args (parser
, decl
);
9484 /* Finish processing the declaration. But, skip friend
9486 if (!friend_p
&& decl
)
9487 cp_finish_decl (decl
,
9490 /* If the initializer is in parentheses, then this is
9491 a direct-initialization, which means that an
9492 `explicit' constructor is OK. Otherwise, an
9493 `explicit' constructor cannot be used. */
9494 ((is_parenthesized_init
|| !is_initialized
)
9495 ? 0 : LOOKUP_ONLYCONVERTING
));
9497 /* Remember whether or not variables were initialized by
9498 constant-expressions. */
9499 if (decl
&& TREE_CODE (decl
) == VAR_DECL
9500 && is_initialized
&& !is_non_constant_init
)
9501 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
9506 /* Parse a declarator.
9510 ptr-operator declarator
9512 abstract-declarator:
9513 ptr-operator abstract-declarator [opt]
9514 direct-abstract-declarator
9519 attributes [opt] direct-declarator
9520 attributes [opt] ptr-operator declarator
9522 abstract-declarator:
9523 attributes [opt] ptr-operator abstract-declarator [opt]
9524 attributes [opt] direct-abstract-declarator
9526 Returns a representation of the declarator. If the declarator has
9527 the form `* declarator', then an INDIRECT_REF is returned, whose
9528 only operand is the sub-declarator. Analogously, `& declarator' is
9529 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9530 used. The first operand is the TYPE for `X'. The second operand
9531 is an INDIRECT_REF whose operand is the sub-declarator.
9533 Otherwise, the representation is as for a direct-declarator.
9535 (It would be better to define a structure type to represent
9536 declarators, rather than abusing `tree' nodes to represent
9537 declarators. That would be much clearer and save some memory.
9538 There is no reason for declarators to be garbage-collected, for
9539 example; they are created during parser and no longer needed after
9540 `grokdeclarator' has been called.)
9542 For a ptr-operator that has the optional cv-qualifier-seq,
9543 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9546 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9547 detect constructor, destructor or conversion operators. It is set
9548 to -1 if the declarator is a name, and +1 if it is a
9549 function. Otherwise it is set to zero. Usually you just want to
9550 test for >0, but internally the negative value is used.
9552 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9553 a decl-specifier-seq unless it declares a constructor, destructor,
9554 or conversion. It might seem that we could check this condition in
9555 semantic analysis, rather than parsing, but that makes it difficult
9556 to handle something like `f()'. We want to notice that there are
9557 no decl-specifiers, and therefore realize that this is an
9558 expression, not a declaration.) */
9561 cp_parser_declarator (cp_parser
* parser
,
9562 cp_parser_declarator_kind dcl_kind
,
9563 int* ctor_dtor_or_conv_p
)
9567 enum tree_code code
;
9568 tree cv_qualifier_seq
;
9570 tree attributes
= NULL_TREE
;
9572 /* Assume this is not a constructor, destructor, or type-conversion
9574 if (ctor_dtor_or_conv_p
)
9575 *ctor_dtor_or_conv_p
= 0;
9577 if (cp_parser_allow_gnu_extensions_p (parser
))
9578 attributes
= cp_parser_attributes_opt (parser
);
9580 /* Peek at the next token. */
9581 token
= cp_lexer_peek_token (parser
->lexer
);
9583 /* Check for the ptr-operator production. */
9584 cp_parser_parse_tentatively (parser
);
9585 /* Parse the ptr-operator. */
9586 code
= cp_parser_ptr_operator (parser
,
9589 /* If that worked, then we have a ptr-operator. */
9590 if (cp_parser_parse_definitely (parser
))
9592 /* The dependent declarator is optional if we are parsing an
9593 abstract-declarator. */
9594 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9595 cp_parser_parse_tentatively (parser
);
9597 /* Parse the dependent declarator. */
9598 declarator
= cp_parser_declarator (parser
, dcl_kind
,
9599 /*ctor_dtor_or_conv_p=*/NULL
);
9601 /* If we are parsing an abstract-declarator, we must handle the
9602 case where the dependent declarator is absent. */
9603 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
9604 && !cp_parser_parse_definitely (parser
))
9605 declarator
= NULL_TREE
;
9607 /* Build the representation of the ptr-operator. */
9608 if (code
== INDIRECT_REF
)
9609 declarator
= make_pointer_declarator (cv_qualifier_seq
,
9612 declarator
= make_reference_declarator (cv_qualifier_seq
,
9614 /* Handle the pointer-to-member case. */
9616 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
9618 /* Everything else is a direct-declarator. */
9620 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
9621 ctor_dtor_or_conv_p
);
9623 if (attributes
&& declarator
!= error_mark_node
)
9624 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
9629 /* Parse a direct-declarator or direct-abstract-declarator.
9633 direct-declarator ( parameter-declaration-clause )
9634 cv-qualifier-seq [opt]
9635 exception-specification [opt]
9636 direct-declarator [ constant-expression [opt] ]
9639 direct-abstract-declarator:
9640 direct-abstract-declarator [opt]
9641 ( parameter-declaration-clause )
9642 cv-qualifier-seq [opt]
9643 exception-specification [opt]
9644 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9645 ( abstract-declarator )
9647 Returns a representation of the declarator. DCL_KIND is
9648 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9649 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9650 we are parsing a direct-declarator. It is
9651 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9652 of ambiguity we prefer an abstract declarator, as per
9653 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9654 cp_parser_declarator.
9656 For the declarator-id production, the representation is as for an
9657 id-expression, except that a qualified name is represented as a
9658 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9659 see the documentation of the FUNCTION_DECLARATOR_* macros for
9660 information about how to find the various declarator components.
9661 An array-declarator is represented as an ARRAY_REF. The
9662 direct-declarator is the first operand; the constant-expression
9663 indicating the size of the array is the second operand. */
9666 cp_parser_direct_declarator (cp_parser
* parser
,
9667 cp_parser_declarator_kind dcl_kind
,
9668 int* ctor_dtor_or_conv_p
)
9671 tree declarator
= NULL_TREE
;
9672 tree scope
= NULL_TREE
;
9673 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
9674 bool saved_in_declarator_p
= parser
->in_declarator_p
;
9679 /* Peek at the next token. */
9680 token
= cp_lexer_peek_token (parser
->lexer
);
9681 if (token
->type
== CPP_OPEN_PAREN
)
9683 /* This is either a parameter-declaration-clause, or a
9684 parenthesized declarator. When we know we are parsing a
9685 named declarator, it must be a parenthesized declarator
9686 if FIRST is true. For instance, `(int)' is a
9687 parameter-declaration-clause, with an omitted
9688 direct-abstract-declarator. But `((*))', is a
9689 parenthesized abstract declarator. Finally, when T is a
9690 template parameter `(T)' is a
9691 parameter-declaration-clause, and not a parenthesized
9694 We first try and parse a parameter-declaration-clause,
9695 and then try a nested declarator (if FIRST is true).
9697 It is not an error for it not to be a
9698 parameter-declaration-clause, even when FIRST is
9704 The first is the declaration of a function while the
9705 second is a the definition of a variable, including its
9708 Having seen only the parenthesis, we cannot know which of
9709 these two alternatives should be selected. Even more
9710 complex are examples like:
9715 The former is a function-declaration; the latter is a
9716 variable initialization.
9718 Thus again, we try a parameter-declaration-clause, and if
9719 that fails, we back out and return. */
9721 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9725 cp_parser_parse_tentatively (parser
);
9727 /* Consume the `('. */
9728 cp_lexer_consume_token (parser
->lexer
);
9731 /* If this is going to be an abstract declarator, we're
9732 in a declarator and we can't have default args. */
9733 parser
->default_arg_ok_p
= false;
9734 parser
->in_declarator_p
= true;
9737 /* Parse the parameter-declaration-clause. */
9738 params
= cp_parser_parameter_declaration_clause (parser
);
9740 /* If all went well, parse the cv-qualifier-seq and the
9741 exception-specification. */
9742 if (cp_parser_parse_definitely (parser
))
9745 tree exception_specification
;
9747 if (ctor_dtor_or_conv_p
)
9748 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
9750 /* Consume the `)'. */
9751 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
9753 /* Parse the cv-qualifier-seq. */
9754 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
9755 /* And the exception-specification. */
9756 exception_specification
9757 = cp_parser_exception_specification_opt (parser
);
9759 /* Create the function-declarator. */
9760 declarator
= make_call_declarator (declarator
,
9763 exception_specification
);
9764 /* Any subsequent parameter lists are to do with
9765 return type, so are not those of the declared
9767 parser
->default_arg_ok_p
= false;
9769 /* Repeat the main loop. */
9774 /* If this is the first, we can try a parenthesized
9778 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
9779 parser
->in_declarator_p
= saved_in_declarator_p
;
9781 /* Consume the `('. */
9782 cp_lexer_consume_token (parser
->lexer
);
9783 /* Parse the nested declarator. */
9785 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
);
9788 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9789 declarator
= error_mark_node
;
9790 if (declarator
== error_mark_node
)
9793 goto handle_declarator
;
9795 /* Otherwise, we must be done. */
9799 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9800 && token
->type
== CPP_OPEN_SQUARE
)
9802 /* Parse an array-declarator. */
9805 if (ctor_dtor_or_conv_p
)
9806 *ctor_dtor_or_conv_p
= 0;
9809 parser
->default_arg_ok_p
= false;
9810 parser
->in_declarator_p
= true;
9811 /* Consume the `['. */
9812 cp_lexer_consume_token (parser
->lexer
);
9813 /* Peek at the next token. */
9814 token
= cp_lexer_peek_token (parser
->lexer
);
9815 /* If the next token is `]', then there is no
9816 constant-expression. */
9817 if (token
->type
!= CPP_CLOSE_SQUARE
)
9819 bool non_constant_p
;
9822 = cp_parser_constant_expression (parser
,
9823 /*allow_non_constant=*/true,
9825 if (!non_constant_p
)
9826 bounds
= cp_parser_fold_non_dependent_expr (bounds
);
9830 /* Look for the closing `]'. */
9831 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
9833 declarator
= error_mark_node
;
9837 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
9839 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
9841 /* Parse a declarator_id */
9842 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
9843 cp_parser_parse_tentatively (parser
);
9844 declarator
= cp_parser_declarator_id (parser
);
9845 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
9847 if (!cp_parser_parse_definitely (parser
))
9848 declarator
= error_mark_node
;
9849 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
9851 cp_parser_error (parser
, "expected unqualified-id");
9852 declarator
= error_mark_node
;
9856 if (declarator
== error_mark_node
)
9859 if (TREE_CODE (declarator
) == SCOPE_REF
)
9861 tree scope
= TREE_OPERAND (declarator
, 0);
9863 /* In the declaration of a member of a template class
9864 outside of the class itself, the SCOPE will sometimes
9865 be a TYPENAME_TYPE. For example, given:
9867 template <typename T>
9870 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9871 this context, we must resolve S<T>::R to an ordinary
9872 type, rather than a typename type.
9874 The reason we normally avoid resolving TYPENAME_TYPEs
9875 is that a specialization of `S' might render
9876 `S<T>::R' not a type. However, if `S' is
9877 specialized, then this `i' will not be used, so there
9878 is no harm in resolving the types here. */
9879 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
9883 /* Resolve the TYPENAME_TYPE. */
9884 type
= resolve_typename_type (scope
,
9885 /*only_current_p=*/false);
9886 /* If that failed, the declarator is invalid. */
9887 if (type
!= error_mark_node
)
9889 /* Build a new DECLARATOR. */
9890 declarator
= build_nt (SCOPE_REF
,
9892 TREE_OPERAND (declarator
, 1));
9896 /* Check to see whether the declarator-id names a constructor,
9897 destructor, or conversion. */
9898 if (declarator
&& ctor_dtor_or_conv_p
9899 && ((TREE_CODE (declarator
) == SCOPE_REF
9900 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
9901 || (TREE_CODE (declarator
) != SCOPE_REF
9902 && at_class_scope_p ())))
9904 tree unqualified_name
;
9907 /* Get the unqualified part of the name. */
9908 if (TREE_CODE (declarator
) == SCOPE_REF
)
9910 class_type
= TREE_OPERAND (declarator
, 0);
9911 unqualified_name
= TREE_OPERAND (declarator
, 1);
9915 class_type
= current_class_type
;
9916 unqualified_name
= declarator
;
9919 /* See if it names ctor, dtor or conv. */
9920 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
9921 || IDENTIFIER_TYPENAME_P (unqualified_name
)
9922 || constructor_name_p (unqualified_name
, class_type
))
9923 *ctor_dtor_or_conv_p
= -1;
9927 scope
= get_scope_of_declarator (declarator
);
9929 /* Any names that appear after the declarator-id for a member
9930 are looked up in the containing scope. */
9932 parser
->in_declarator_p
= true;
9933 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
9935 && (TREE_CODE (declarator
) == SCOPE_REF
9936 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
9937 /* Default args are only allowed on function
9939 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
9941 parser
->default_arg_ok_p
= false;
9950 /* For an abstract declarator, we might wind up with nothing at this
9951 point. That's an error; the declarator is not optional. */
9953 cp_parser_error (parser
, "expected declarator");
9955 /* If we entered a scope, we must exit it now. */
9959 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
9960 parser
->in_declarator_p
= saved_in_declarator_p
;
9965 /* Parse a ptr-operator.
9968 * cv-qualifier-seq [opt]
9970 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9975 & cv-qualifier-seq [opt]
9977 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9978 used. Returns ADDR_EXPR if a reference was used. In the
9979 case of a pointer-to-member, *TYPE is filled in with the
9980 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9981 with the cv-qualifier-seq, or NULL_TREE, if there are no
9982 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9984 static enum tree_code
9985 cp_parser_ptr_operator (cp_parser
* parser
,
9987 tree
* cv_qualifier_seq
)
9989 enum tree_code code
= ERROR_MARK
;
9992 /* Assume that it's not a pointer-to-member. */
9994 /* And that there are no cv-qualifiers. */
9995 *cv_qualifier_seq
= NULL_TREE
;
9997 /* Peek at the next token. */
9998 token
= cp_lexer_peek_token (parser
->lexer
);
9999 /* If it's a `*' or `&' we have a pointer or reference. */
10000 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10002 /* Remember which ptr-operator we were processing. */
10003 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10005 /* Consume the `*' or `&'. */
10006 cp_lexer_consume_token (parser
->lexer
);
10008 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10009 `&', if we are allowing GNU extensions. (The only qualifier
10010 that can legally appear after `&' is `restrict', but that is
10011 enforced during semantic analysis. */
10012 if (code
== INDIRECT_REF
10013 || cp_parser_allow_gnu_extensions_p (parser
))
10014 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10018 /* Try the pointer-to-member case. */
10019 cp_parser_parse_tentatively (parser
);
10020 /* Look for the optional `::' operator. */
10021 cp_parser_global_scope_opt (parser
,
10022 /*current_scope_valid_p=*/false);
10023 /* Look for the nested-name specifier. */
10024 cp_parser_nested_name_specifier (parser
,
10025 /*typename_keyword_p=*/false,
10026 /*check_dependency_p=*/true,
10028 /* If we found it, and the next token is a `*', then we are
10029 indeed looking at a pointer-to-member operator. */
10030 if (!cp_parser_error_occurred (parser
)
10031 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10033 /* The type of which the member is a member is given by the
10035 *type
= parser
->scope
;
10036 /* The next name will not be qualified. */
10037 parser
->scope
= NULL_TREE
;
10038 parser
->qualifying_scope
= NULL_TREE
;
10039 parser
->object_scope
= NULL_TREE
;
10040 /* Indicate that the `*' operator was used. */
10041 code
= INDIRECT_REF
;
10042 /* Look for the optional cv-qualifier-seq. */
10043 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10045 /* If that didn't work we don't have a ptr-operator. */
10046 if (!cp_parser_parse_definitely (parser
))
10047 cp_parser_error (parser
, "expected ptr-operator");
10053 /* Parse an (optional) cv-qualifier-seq.
10056 cv-qualifier cv-qualifier-seq [opt]
10058 Returns a TREE_LIST. The TREE_VALUE of each node is the
10059 representation of a cv-qualifier. */
10062 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10064 tree cv_qualifiers
= NULL_TREE
;
10070 /* Look for the next cv-qualifier. */
10071 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10072 /* If we didn't find one, we're done. */
10076 /* Add this cv-qualifier to the list. */
10078 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10081 /* We built up the list in reverse order. */
10082 return nreverse (cv_qualifiers
);
10085 /* Parse an (optional) cv-qualifier.
10097 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10100 tree cv_qualifier
= NULL_TREE
;
10102 /* Peek at the next token. */
10103 token
= cp_lexer_peek_token (parser
->lexer
);
10104 /* See if it's a cv-qualifier. */
10105 switch (token
->keyword
)
10110 /* Save the value of the token. */
10111 cv_qualifier
= token
->value
;
10112 /* Consume the token. */
10113 cp_lexer_consume_token (parser
->lexer
);
10120 return cv_qualifier
;
10123 /* Parse a declarator-id.
10127 :: [opt] nested-name-specifier [opt] type-name
10129 In the `id-expression' case, the value returned is as for
10130 cp_parser_id_expression if the id-expression was an unqualified-id.
10131 If the id-expression was a qualified-id, then a SCOPE_REF is
10132 returned. The first operand is the scope (either a NAMESPACE_DECL
10133 or TREE_TYPE), but the second is still just a representation of an
10137 cp_parser_declarator_id (cp_parser
* parser
)
10139 tree id_expression
;
10141 /* The expression must be an id-expression. Assume that qualified
10142 names are the names of types so that:
10145 int S<T>::R::i = 3;
10147 will work; we must treat `S<T>::R' as the name of a type.
10148 Similarly, assume that qualified names are templates, where
10152 int S<T>::R<T>::i = 3;
10155 id_expression
= cp_parser_id_expression (parser
,
10156 /*template_keyword_p=*/false,
10157 /*check_dependency_p=*/false,
10158 /*template_p=*/NULL
,
10159 /*declarator_p=*/true);
10160 /* If the name was qualified, create a SCOPE_REF to represent
10164 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
10165 parser
->scope
= NULL_TREE
;
10168 return id_expression
;
10171 /* Parse a type-id.
10174 type-specifier-seq abstract-declarator [opt]
10176 Returns the TYPE specified. */
10179 cp_parser_type_id (cp_parser
* parser
)
10181 tree type_specifier_seq
;
10182 tree abstract_declarator
;
10184 /* Parse the type-specifier-seq. */
10186 = cp_parser_type_specifier_seq (parser
);
10187 if (type_specifier_seq
== error_mark_node
)
10188 return error_mark_node
;
10190 /* There might or might not be an abstract declarator. */
10191 cp_parser_parse_tentatively (parser
);
10192 /* Look for the declarator. */
10193 abstract_declarator
10194 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
);
10195 /* Check to see if there really was a declarator. */
10196 if (!cp_parser_parse_definitely (parser
))
10197 abstract_declarator
= NULL_TREE
;
10199 return groktypename (build_tree_list (type_specifier_seq
,
10200 abstract_declarator
));
10203 /* Parse a type-specifier-seq.
10205 type-specifier-seq:
10206 type-specifier type-specifier-seq [opt]
10210 type-specifier-seq:
10211 attributes type-specifier-seq [opt]
10213 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10214 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10217 cp_parser_type_specifier_seq (cp_parser
* parser
)
10219 bool seen_type_specifier
= false;
10220 tree type_specifier_seq
= NULL_TREE
;
10222 /* Parse the type-specifiers and attributes. */
10225 tree type_specifier
;
10227 /* Check for attributes first. */
10228 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
10230 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
10232 type_specifier_seq
);
10236 /* After the first type-specifier, others are optional. */
10237 if (seen_type_specifier
)
10238 cp_parser_parse_tentatively (parser
);
10239 /* Look for the type-specifier. */
10240 type_specifier
= cp_parser_type_specifier (parser
,
10241 CP_PARSER_FLAGS_NONE
,
10242 /*is_friend=*/false,
10243 /*is_declaration=*/false,
10246 /* If the first type-specifier could not be found, this is not a
10247 type-specifier-seq at all. */
10248 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
10249 return error_mark_node
;
10250 /* If subsequent type-specifiers could not be found, the
10251 type-specifier-seq is complete. */
10252 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
10255 /* Add the new type-specifier to the list. */
10257 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
10258 seen_type_specifier
= true;
10261 /* We built up the list in reverse order. */
10262 return nreverse (type_specifier_seq
);
10265 /* Parse a parameter-declaration-clause.
10267 parameter-declaration-clause:
10268 parameter-declaration-list [opt] ... [opt]
10269 parameter-declaration-list , ...
10271 Returns a representation for the parameter declarations. Each node
10272 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10273 representation.) If the parameter-declaration-clause ends with an
10274 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10275 list. A return value of NULL_TREE indicates a
10276 parameter-declaration-clause consisting only of an ellipsis. */
10279 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
10285 /* Peek at the next token. */
10286 token
= cp_lexer_peek_token (parser
->lexer
);
10287 /* Check for trivial parameter-declaration-clauses. */
10288 if (token
->type
== CPP_ELLIPSIS
)
10290 /* Consume the `...' token. */
10291 cp_lexer_consume_token (parser
->lexer
);
10294 else if (token
->type
== CPP_CLOSE_PAREN
)
10295 /* There are no parameters. */
10297 #ifndef NO_IMPLICIT_EXTERN_C
10298 if (in_system_header
&& current_class_type
== NULL
10299 && current_lang_name
== lang_name_c
)
10303 return void_list_node
;
10305 /* Check for `(void)', too, which is a special case. */
10306 else if (token
->keyword
== RID_VOID
10307 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
10308 == CPP_CLOSE_PAREN
))
10310 /* Consume the `void' token. */
10311 cp_lexer_consume_token (parser
->lexer
);
10312 /* There are no parameters. */
10313 return void_list_node
;
10316 /* Parse the parameter-declaration-list. */
10317 parameters
= cp_parser_parameter_declaration_list (parser
);
10318 /* If a parse error occurred while parsing the
10319 parameter-declaration-list, then the entire
10320 parameter-declaration-clause is erroneous. */
10321 if (parameters
== error_mark_node
)
10322 return error_mark_node
;
10324 /* Peek at the next token. */
10325 token
= cp_lexer_peek_token (parser
->lexer
);
10326 /* If it's a `,', the clause should terminate with an ellipsis. */
10327 if (token
->type
== CPP_COMMA
)
10329 /* Consume the `,'. */
10330 cp_lexer_consume_token (parser
->lexer
);
10331 /* Expect an ellipsis. */
10333 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
10335 /* It might also be `...' if the optional trailing `,' was
10337 else if (token
->type
== CPP_ELLIPSIS
)
10339 /* Consume the `...' token. */
10340 cp_lexer_consume_token (parser
->lexer
);
10341 /* And remember that we saw it. */
10345 ellipsis_p
= false;
10347 /* Finish the parameter list. */
10348 return finish_parmlist (parameters
, ellipsis_p
);
10351 /* Parse a parameter-declaration-list.
10353 parameter-declaration-list:
10354 parameter-declaration
10355 parameter-declaration-list , parameter-declaration
10357 Returns a representation of the parameter-declaration-list, as for
10358 cp_parser_parameter_declaration_clause. However, the
10359 `void_list_node' is never appended to the list. */
10362 cp_parser_parameter_declaration_list (cp_parser
* parser
)
10364 tree parameters
= NULL_TREE
;
10366 /* Look for more parameters. */
10370 /* Parse the parameter. */
10372 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/false);
10374 /* If a parse error occurred parsing the parameter declaration,
10375 then the entire parameter-declaration-list is erroneous. */
10376 if (parameter
== error_mark_node
)
10378 parameters
= error_mark_node
;
10381 /* Add the new parameter to the list. */
10382 TREE_CHAIN (parameter
) = parameters
;
10383 parameters
= parameter
;
10385 /* Peek at the next token. */
10386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
10387 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10388 /* The parameter-declaration-list is complete. */
10390 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10394 /* Peek at the next token. */
10395 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10396 /* If it's an ellipsis, then the list is complete. */
10397 if (token
->type
== CPP_ELLIPSIS
)
10399 /* Otherwise, there must be more parameters. Consume the
10401 cp_lexer_consume_token (parser
->lexer
);
10405 cp_parser_error (parser
, "expected `,' or `...'");
10410 /* We built up the list in reverse order; straighten it out now. */
10411 return nreverse (parameters
);
10414 /* Parse a parameter declaration.
10416 parameter-declaration:
10417 decl-specifier-seq declarator
10418 decl-specifier-seq declarator = assignment-expression
10419 decl-specifier-seq abstract-declarator [opt]
10420 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10422 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10423 declares a template parameter. (In that case, a non-nested `>'
10424 token encountered during the parsing of the assignment-expression
10425 is not interpreted as a greater-than operator.)
10427 Returns a TREE_LIST representing the parameter-declaration. The
10428 TREE_VALUE is a representation of the decl-specifier-seq and
10429 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10430 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10431 TREE_VALUE represents the declarator. */
10434 cp_parser_parameter_declaration (cp_parser
*parser
,
10435 bool template_parm_p
)
10437 int declares_class_or_enum
;
10438 bool greater_than_is_operator_p
;
10439 tree decl_specifiers
;
10442 tree default_argument
;
10445 const char *saved_message
;
10447 /* In a template parameter, `>' is not an operator.
10451 When parsing a default template-argument for a non-type
10452 template-parameter, the first non-nested `>' is taken as the end
10453 of the template parameter-list rather than a greater-than
10455 greater_than_is_operator_p
= !template_parm_p
;
10457 /* Type definitions may not appear in parameter types. */
10458 saved_message
= parser
->type_definition_forbidden_message
;
10459 parser
->type_definition_forbidden_message
10460 = "types may not be defined in parameter types";
10462 /* Parse the declaration-specifiers. */
10464 = cp_parser_decl_specifier_seq (parser
,
10465 CP_PARSER_FLAGS_NONE
,
10467 &declares_class_or_enum
);
10468 /* If an error occurred, there's no reason to attempt to parse the
10469 rest of the declaration. */
10470 if (cp_parser_error_occurred (parser
))
10472 parser
->type_definition_forbidden_message
= saved_message
;
10473 return error_mark_node
;
10476 /* Peek at the next token. */
10477 token
= cp_lexer_peek_token (parser
->lexer
);
10478 /* If the next token is a `)', `,', `=', `>', or `...', then there
10479 is no declarator. */
10480 if (token
->type
== CPP_CLOSE_PAREN
10481 || token
->type
== CPP_COMMA
10482 || token
->type
== CPP_EQ
10483 || token
->type
== CPP_ELLIPSIS
10484 || token
->type
== CPP_GREATER
)
10485 declarator
= NULL_TREE
;
10486 /* Otherwise, there should be a declarator. */
10489 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10490 parser
->default_arg_ok_p
= false;
10492 declarator
= cp_parser_declarator (parser
,
10493 CP_PARSER_DECLARATOR_EITHER
,
10494 /*ctor_dtor_or_conv_p=*/NULL
);
10495 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10496 /* After the declarator, allow more attributes. */
10497 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
10500 /* The restriction on defining new types applies only to the type
10501 of the parameter, not to the default argument. */
10502 parser
->type_definition_forbidden_message
= saved_message
;
10504 /* If the next token is `=', then process a default argument. */
10505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10507 bool saved_greater_than_is_operator_p
;
10508 /* Consume the `='. */
10509 cp_lexer_consume_token (parser
->lexer
);
10511 /* If we are defining a class, then the tokens that make up the
10512 default argument must be saved and processed later. */
10513 if (!template_parm_p
&& at_class_scope_p ()
10514 && TYPE_BEING_DEFINED (current_class_type
))
10516 unsigned depth
= 0;
10518 /* Create a DEFAULT_ARG to represented the unparsed default
10520 default_argument
= make_node (DEFAULT_ARG
);
10521 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
10523 /* Add tokens until we have processed the entire default
10530 /* Peek at the next token. */
10531 token
= cp_lexer_peek_token (parser
->lexer
);
10532 /* What we do depends on what token we have. */
10533 switch (token
->type
)
10535 /* In valid code, a default argument must be
10536 immediately followed by a `,' `)', or `...'. */
10538 case CPP_CLOSE_PAREN
:
10540 /* If we run into a non-nested `;', `}', or `]',
10541 then the code is invalid -- but the default
10542 argument is certainly over. */
10543 case CPP_SEMICOLON
:
10544 case CPP_CLOSE_BRACE
:
10545 case CPP_CLOSE_SQUARE
:
10548 /* Update DEPTH, if necessary. */
10549 else if (token
->type
== CPP_CLOSE_PAREN
10550 || token
->type
== CPP_CLOSE_BRACE
10551 || token
->type
== CPP_CLOSE_SQUARE
)
10555 case CPP_OPEN_PAREN
:
10556 case CPP_OPEN_SQUARE
:
10557 case CPP_OPEN_BRACE
:
10562 /* If we see a non-nested `>', and `>' is not an
10563 operator, then it marks the end of the default
10565 if (!depth
&& !greater_than_is_operator_p
)
10569 /* If we run out of tokens, issue an error message. */
10571 error ("file ends in default argument");
10577 /* In these cases, we should look for template-ids.
10578 For example, if the default argument is
10579 `X<int, double>()', we need to do name lookup to
10580 figure out whether or not `X' is a template; if
10581 so, the `,' does not end the default argument.
10583 That is not yet done. */
10590 /* If we've reached the end, stop. */
10594 /* Add the token to the token block. */
10595 token
= cp_lexer_consume_token (parser
->lexer
);
10596 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
10600 /* Outside of a class definition, we can just parse the
10601 assignment-expression. */
10604 bool saved_local_variables_forbidden_p
;
10606 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10608 saved_greater_than_is_operator_p
10609 = parser
->greater_than_is_operator_p
;
10610 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
10611 /* Local variable names (and the `this' keyword) may not
10612 appear in a default argument. */
10613 saved_local_variables_forbidden_p
10614 = parser
->local_variables_forbidden_p
;
10615 parser
->local_variables_forbidden_p
= true;
10616 /* Parse the assignment-expression. */
10617 default_argument
= cp_parser_assignment_expression (parser
);
10618 /* Restore saved state. */
10619 parser
->greater_than_is_operator_p
10620 = saved_greater_than_is_operator_p
;
10621 parser
->local_variables_forbidden_p
10622 = saved_local_variables_forbidden_p
;
10624 if (!parser
->default_arg_ok_p
)
10626 if (!flag_pedantic_errors
)
10627 warning ("deprecated use of default argument for parameter of non-function");
10630 error ("default arguments are only permitted for function parameters");
10631 default_argument
= NULL_TREE
;
10636 default_argument
= NULL_TREE
;
10638 /* Create the representation of the parameter. */
10640 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
10641 parameter
= build_tree_list (default_argument
,
10642 build_tree_list (decl_specifiers
,
10648 /* Parse a function-definition.
10650 function-definition:
10651 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10653 decl-specifier-seq [opt] declarator function-try-block
10657 function-definition:
10658 __extension__ function-definition
10660 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10661 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10665 cp_parser_function_definition (cp_parser
* parser
, bool* friend_p
)
10667 tree decl_specifiers
;
10672 int declares_class_or_enum
;
10674 /* The saved value of the PEDANTIC flag. */
10675 int saved_pedantic
;
10677 /* Any pending qualification must be cleared by our caller. It is
10678 more robust to force the callers to clear PARSER->SCOPE than to
10679 do it here since if the qualification is in effect here, it might
10680 also end up in effect elsewhere that it is not intended. */
10681 my_friendly_assert (!parser
->scope
, 20010821);
10683 /* Handle `__extension__'. */
10684 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10686 /* Parse the function-definition. */
10687 fn
= cp_parser_function_definition (parser
, friend_p
);
10688 /* Restore the PEDANTIC flag. */
10689 pedantic
= saved_pedantic
;
10694 /* Check to see if this definition appears in a class-specifier. */
10695 member_p
= (at_class_scope_p ()
10696 && TYPE_BEING_DEFINED (current_class_type
));
10697 /* Defer access checks in the decl-specifier-seq until we know what
10698 function is being defined. There is no need to do this for the
10699 definition of member functions; we cannot be defining a member
10700 from another class. */
10701 push_deferring_access_checks (member_p
? dk_no_check
: dk_deferred
);
10703 /* Parse the decl-specifier-seq. */
10705 = cp_parser_decl_specifier_seq (parser
,
10706 CP_PARSER_FLAGS_OPTIONAL
,
10708 &declares_class_or_enum
);
10709 /* Figure out whether this declaration is a `friend'. */
10711 *friend_p
= cp_parser_friend_p (decl_specifiers
);
10713 /* Parse the declarator. */
10714 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10715 /*ctor_dtor_or_conv_p=*/NULL
);
10717 /* Gather up any access checks that occurred. */
10718 stop_deferring_access_checks ();
10720 /* If something has already gone wrong, we may as well stop now. */
10721 if (declarator
== error_mark_node
)
10723 /* Skip to the end of the function, or if this wasn't anything
10724 like a function-definition, to a `;' in the hopes of finding
10725 a sensible place from which to continue parsing. */
10726 cp_parser_skip_to_end_of_block_or_statement (parser
);
10727 pop_deferring_access_checks ();
10728 return error_mark_node
;
10731 /* The next character should be a `{' (for a simple function
10732 definition), a `:' (for a ctor-initializer), or `try' (for a
10733 function-try block). */
10734 token
= cp_lexer_peek_token (parser
->lexer
);
10735 if (!cp_parser_token_starts_function_definition_p (token
))
10737 /* Issue the error-message. */
10738 cp_parser_error (parser
, "expected function-definition");
10739 /* Skip to the next `;'. */
10740 cp_parser_skip_to_end_of_block_or_statement (parser
);
10742 pop_deferring_access_checks ();
10743 return error_mark_node
;
10746 cp_parser_check_for_definition_in_return_type (declarator
,
10747 declares_class_or_enum
);
10749 /* If we are in a class scope, then we must handle
10750 function-definitions specially. In particular, we save away the
10751 tokens that make up the function body, and parse them again
10752 later, in order to handle code like:
10755 int f () { return i; }
10759 Here, we cannot parse the body of `f' until after we have seen
10760 the declaration of `i'. */
10763 cp_token_cache
*cache
;
10765 /* Create the function-declaration. */
10766 fn
= start_method (decl_specifiers
, declarator
, attributes
);
10767 /* If something went badly wrong, bail out now. */
10768 if (fn
== error_mark_node
)
10770 /* If there's a function-body, skip it. */
10771 if (cp_parser_token_starts_function_definition_p
10772 (cp_lexer_peek_token (parser
->lexer
)))
10773 cp_parser_skip_to_end_of_block_or_statement (parser
);
10774 pop_deferring_access_checks ();
10775 return error_mark_node
;
10778 /* Remember it, if there default args to post process. */
10779 cp_parser_save_default_args (parser
, fn
);
10781 /* Create a token cache. */
10782 cache
= cp_token_cache_new ();
10783 /* Save away the tokens that make up the body of the
10785 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
10786 /* Handle function try blocks. */
10787 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
10788 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
10790 /* Save away the inline definition; we will process it when the
10791 class is complete. */
10792 DECL_PENDING_INLINE_INFO (fn
) = cache
;
10793 DECL_PENDING_INLINE_P (fn
) = 1;
10795 /* We need to know that this was defined in the class, so that
10796 friend templates are handled correctly. */
10797 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
10799 /* We're done with the inline definition. */
10800 finish_method (fn
);
10802 /* Add FN to the queue of functions to be parsed later. */
10803 TREE_VALUE (parser
->unparsed_functions_queues
)
10804 = tree_cons (NULL_TREE
, fn
,
10805 TREE_VALUE (parser
->unparsed_functions_queues
));
10807 pop_deferring_access_checks ();
10811 /* Check that the number of template-parameter-lists is OK. */
10812 if (!cp_parser_check_declarator_template_parameters (parser
,
10815 cp_parser_skip_to_end_of_block_or_statement (parser
);
10816 pop_deferring_access_checks ();
10817 return error_mark_node
;
10820 fn
= cp_parser_function_definition_from_specifiers_and_declarator
10821 (parser
, decl_specifiers
, attributes
, declarator
);
10822 pop_deferring_access_checks ();
10826 /* Parse a function-body.
10829 compound_statement */
10832 cp_parser_function_body (cp_parser
*parser
)
10834 cp_parser_compound_statement (parser
, false);
10837 /* Parse a ctor-initializer-opt followed by a function-body. Return
10838 true if a ctor-initializer was present. */
10841 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
10844 bool ctor_initializer_p
;
10846 /* Begin the function body. */
10847 body
= begin_function_body ();
10848 /* Parse the optional ctor-initializer. */
10849 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
10850 /* Parse the function-body. */
10851 cp_parser_function_body (parser
);
10852 /* Finish the function body. */
10853 finish_function_body (body
);
10855 return ctor_initializer_p
;
10858 /* Parse an initializer.
10861 = initializer-clause
10862 ( expression-list )
10864 Returns a expression representing the initializer. If no
10865 initializer is present, NULL_TREE is returned.
10867 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10868 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
10869 set to FALSE if there is no initializer present. If there is an
10870 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10871 is set to true; otherwise it is set to false. */
10874 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
10875 bool* non_constant_p
)
10880 /* Peek at the next token. */
10881 token
= cp_lexer_peek_token (parser
->lexer
);
10883 /* Let our caller know whether or not this initializer was
10885 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
10886 /* Assume that the initializer is constant. */
10887 *non_constant_p
= false;
10889 if (token
->type
== CPP_EQ
)
10891 /* Consume the `='. */
10892 cp_lexer_consume_token (parser
->lexer
);
10893 /* Parse the initializer-clause. */
10894 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
10896 else if (token
->type
== CPP_OPEN_PAREN
)
10897 init
= cp_parser_parenthesized_expression_list (parser
, false,
10901 /* Anything else is an error. */
10902 cp_parser_error (parser
, "expected initializer");
10903 init
= error_mark_node
;
10909 /* Parse an initializer-clause.
10911 initializer-clause:
10912 assignment-expression
10913 { initializer-list , [opt] }
10916 Returns an expression representing the initializer.
10918 If the `assignment-expression' production is used the value
10919 returned is simply a representation for the expression.
10921 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10922 the elements of the initializer-list (or NULL_TREE, if the last
10923 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10924 NULL_TREE. There is no way to detect whether or not the optional
10925 trailing `,' was provided. NON_CONSTANT_P is as for
10926 cp_parser_initializer. */
10929 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
10933 /* If it is not a `{', then we are looking at an
10934 assignment-expression. */
10935 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10937 = cp_parser_constant_expression (parser
,
10938 /*allow_non_constant_p=*/true,
10942 /* Consume the `{' token. */
10943 cp_lexer_consume_token (parser
->lexer
);
10944 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10945 initializer
= make_node (CONSTRUCTOR
);
10946 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10947 necessary, but check_initializer depends upon it, for
10949 TREE_HAS_CONSTRUCTOR (initializer
) = 1;
10950 /* If it's not a `}', then there is a non-trivial initializer. */
10951 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10953 /* Parse the initializer list. */
10954 CONSTRUCTOR_ELTS (initializer
)
10955 = cp_parser_initializer_list (parser
, non_constant_p
);
10956 /* A trailing `,' token is allowed. */
10957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10958 cp_lexer_consume_token (parser
->lexer
);
10960 /* Now, there should be a trailing `}'. */
10961 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10964 return initializer
;
10967 /* Parse an initializer-list.
10971 initializer-list , initializer-clause
10976 identifier : initializer-clause
10977 initializer-list, identifier : initializer-clause
10979 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10980 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
10981 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10982 as for cp_parser_initializer. */
10985 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
10987 tree initializers
= NULL_TREE
;
10989 /* Assume all of the expressions are constant. */
10990 *non_constant_p
= false;
10992 /* Parse the rest of the list. */
10998 bool clause_non_constant_p
;
11000 /* If the next token is an identifier and the following one is a
11001 colon, we are looking at the GNU designated-initializer
11003 if (cp_parser_allow_gnu_extensions_p (parser
)
11004 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11005 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11007 /* Consume the identifier. */
11008 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11009 /* Consume the `:'. */
11010 cp_lexer_consume_token (parser
->lexer
);
11013 identifier
= NULL_TREE
;
11015 /* Parse the initializer. */
11016 initializer
= cp_parser_initializer_clause (parser
,
11017 &clause_non_constant_p
);
11018 /* If any clause is non-constant, so is the entire initializer. */
11019 if (clause_non_constant_p
)
11020 *non_constant_p
= true;
11021 /* Add it to the list. */
11022 initializers
= tree_cons (identifier
, initializer
, initializers
);
11024 /* If the next token is not a comma, we have reached the end of
11026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11029 /* Peek at the next token. */
11030 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11031 /* If the next token is a `}', then we're still done. An
11032 initializer-clause can have a trailing `,' after the
11033 initializer-list and before the closing `}'. */
11034 if (token
->type
== CPP_CLOSE_BRACE
)
11037 /* Consume the `,' token. */
11038 cp_lexer_consume_token (parser
->lexer
);
11041 /* The initializers were built up in reverse order, so we need to
11042 reverse them now. */
11043 return nreverse (initializers
);
11046 /* Classes [gram.class] */
11048 /* Parse a class-name.
11054 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11055 to indicate that names looked up in dependent types should be
11056 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11057 keyword has been used to indicate that the name that appears next
11058 is a template. TYPE_P is true iff the next name should be treated
11059 as class-name, even if it is declared to be some other kind of name
11060 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11061 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11062 being defined in a class-head.
11064 Returns the TYPE_DECL representing the class. */
11067 cp_parser_class_name (cp_parser
*parser
,
11068 bool typename_keyword_p
,
11069 bool template_keyword_p
,
11071 bool check_dependency_p
,
11079 /* All class-names start with an identifier. */
11080 token
= cp_lexer_peek_token (parser
->lexer
);
11081 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11083 cp_parser_error (parser
, "expected class-name");
11084 return error_mark_node
;
11087 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11088 to a template-id, so we save it here. */
11089 scope
= parser
->scope
;
11090 if (scope
== error_mark_node
)
11091 return error_mark_node
;
11093 /* Any name names a type if we're following the `typename' keyword
11094 in a qualified name where the enclosing scope is type-dependent. */
11095 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11096 && dependent_type_p (scope
));
11097 /* Handle the common case (an identifier, but not a template-id)
11099 if (token
->type
== CPP_NAME
11100 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
11104 /* Look for the identifier. */
11105 identifier
= cp_parser_identifier (parser
);
11106 /* If the next token isn't an identifier, we are certainly not
11107 looking at a class-name. */
11108 if (identifier
== error_mark_node
)
11109 decl
= error_mark_node
;
11110 /* If we know this is a type-name, there's no need to look it
11112 else if (typename_p
)
11116 /* If the next token is a `::', then the name must be a type
11119 [basic.lookup.qual]
11121 During the lookup for a name preceding the :: scope
11122 resolution operator, object, function, and enumerator
11123 names are ignored. */
11124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11126 /* Look up the name. */
11127 decl
= cp_parser_lookup_name (parser
, identifier
,
11129 /*is_namespace=*/false,
11130 check_dependency_p
);
11135 /* Try a template-id. */
11136 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11137 check_dependency_p
);
11138 if (decl
== error_mark_node
)
11139 return error_mark_node
;
11142 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11144 /* If this is a typename, create a TYPENAME_TYPE. */
11145 if (typename_p
&& decl
!= error_mark_node
)
11146 decl
= TYPE_NAME (make_typename_type (scope
, decl
,
11149 /* Check to see that it is really the name of a class. */
11150 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11151 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11152 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11153 /* Situations like this:
11155 template <typename T> struct A {
11156 typename T::template X<int>::I i;
11159 are problematic. Is `T::template X<int>' a class-name? The
11160 standard does not seem to be definitive, but there is no other
11161 valid interpretation of the following `::'. Therefore, those
11162 names are considered class-names. */
11163 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11164 else if (decl
== error_mark_node
11165 || TREE_CODE (decl
) != TYPE_DECL
11166 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11168 cp_parser_error (parser
, "expected class-name");
11169 return error_mark_node
;
11175 /* Parse a class-specifier.
11178 class-head { member-specification [opt] }
11180 Returns the TREE_TYPE representing the class. */
11183 cp_parser_class_specifier (cp_parser
* parser
)
11187 tree attributes
= NULL_TREE
;
11188 int has_trailing_semicolon
;
11189 bool nested_name_specifier_p
;
11190 unsigned saved_num_template_parameter_lists
;
11192 push_deferring_access_checks (dk_no_deferred
);
11194 /* Parse the class-head. */
11195 type
= cp_parser_class_head (parser
,
11196 &nested_name_specifier_p
);
11197 /* If the class-head was a semantic disaster, skip the entire body
11201 cp_parser_skip_to_end_of_block_or_statement (parser
);
11202 pop_deferring_access_checks ();
11203 return error_mark_node
;
11206 /* Look for the `{'. */
11207 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11209 pop_deferring_access_checks ();
11210 return error_mark_node
;
11213 /* Issue an error message if type-definitions are forbidden here. */
11214 cp_parser_check_type_definition (parser
);
11215 /* Remember that we are defining one more class. */
11216 ++parser
->num_classes_being_defined
;
11217 /* Inside the class, surrounding template-parameter-lists do not
11219 saved_num_template_parameter_lists
11220 = parser
->num_template_parameter_lists
;
11221 parser
->num_template_parameter_lists
= 0;
11223 /* Start the class. */
11224 type
= begin_class_definition (type
);
11225 if (type
== error_mark_node
)
11226 /* If the type is erroneous, skip the entire body of the class. */
11227 cp_parser_skip_to_closing_brace (parser
);
11229 /* Parse the member-specification. */
11230 cp_parser_member_specification_opt (parser
);
11231 /* Look for the trailing `}'. */
11232 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11233 /* We get better error messages by noticing a common problem: a
11234 missing trailing `;'. */
11235 token
= cp_lexer_peek_token (parser
->lexer
);
11236 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11237 /* Look for attributes to apply to this class. */
11238 if (cp_parser_allow_gnu_extensions_p (parser
))
11239 attributes
= cp_parser_attributes_opt (parser
);
11240 /* If we got any attributes in class_head, xref_tag will stick them in
11241 TREE_TYPE of the type. Grab them now. */
11242 if (type
!= error_mark_node
)
11244 attributes
= chainon (TYPE_ATTRIBUTES (type
), attributes
);
11245 TYPE_ATTRIBUTES (type
) = NULL_TREE
;
11246 type
= finish_struct (type
, attributes
);
11248 if (nested_name_specifier_p
)
11249 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11250 /* If this class is not itself within the scope of another class,
11251 then we need to parse the bodies of all of the queued function
11252 definitions. Note that the queued functions defined in a class
11253 are not always processed immediately following the
11254 class-specifier for that class. Consider:
11257 struct B { void f() { sizeof (A); } };
11260 If `f' were processed before the processing of `A' were
11261 completed, there would be no way to compute the size of `A'.
11262 Note that the nesting we are interested in here is lexical --
11263 not the semantic nesting given by TYPE_CONTEXT. In particular,
11266 struct A { struct B; };
11267 struct A::B { void f() { } };
11269 there is no need to delay the parsing of `A::B::f'. */
11270 if (--parser
->num_classes_being_defined
== 0)
11275 /* In a first pass, parse default arguments to the functions.
11276 Then, in a second pass, parse the bodies of the functions.
11277 This two-phased approach handles cases like:
11285 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
11286 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
11287 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
11288 TREE_PURPOSE (parser
->unparsed_functions_queues
)
11289 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
11291 fn
= TREE_VALUE (queue_entry
);
11292 /* Make sure that any template parameters are in scope. */
11293 maybe_begin_member_template_processing (fn
);
11294 /* If there are default arguments that have not yet been processed,
11295 take care of them now. */
11296 cp_parser_late_parsing_default_args (parser
, fn
);
11297 /* Remove any template parameters from the symbol table. */
11298 maybe_end_member_template_processing ();
11300 /* Now parse the body of the functions. */
11301 for (TREE_VALUE (parser
->unparsed_functions_queues
)
11302 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
11303 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
11304 TREE_VALUE (parser
->unparsed_functions_queues
)
11305 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
11307 /* Figure out which function we need to process. */
11308 fn
= TREE_VALUE (queue_entry
);
11310 /* Parse the function. */
11311 cp_parser_late_parsing_for_member (parser
, fn
);
11316 /* Put back any saved access checks. */
11317 pop_deferring_access_checks ();
11319 /* Restore the count of active template-parameter-lists. */
11320 parser
->num_template_parameter_lists
11321 = saved_num_template_parameter_lists
;
11326 /* Parse a class-head.
11329 class-key identifier [opt] base-clause [opt]
11330 class-key nested-name-specifier identifier base-clause [opt]
11331 class-key nested-name-specifier [opt] template-id
11335 class-key attributes identifier [opt] base-clause [opt]
11336 class-key attributes nested-name-specifier identifier base-clause [opt]
11337 class-key attributes nested-name-specifier [opt] template-id
11340 Returns the TYPE of the indicated class. Sets
11341 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11342 involving a nested-name-specifier was used, and FALSE otherwise.
11344 Returns NULL_TREE if the class-head is syntactically valid, but
11345 semantically invalid in a way that means we should skip the entire
11346 body of the class. */
11349 cp_parser_class_head (cp_parser
* parser
,
11350 bool* nested_name_specifier_p
)
11353 tree nested_name_specifier
;
11354 enum tag_types class_key
;
11355 tree id
= NULL_TREE
;
11356 tree type
= NULL_TREE
;
11358 bool template_id_p
= false;
11359 bool qualified_p
= false;
11360 bool invalid_nested_name_p
= false;
11361 unsigned num_templates
;
11363 /* Assume no nested-name-specifier will be present. */
11364 *nested_name_specifier_p
= false;
11365 /* Assume no template parameter lists will be used in defining the
11369 /* Look for the class-key. */
11370 class_key
= cp_parser_class_key (parser
);
11371 if (class_key
== none_type
)
11372 return error_mark_node
;
11374 /* Parse the attributes. */
11375 attributes
= cp_parser_attributes_opt (parser
);
11377 /* If the next token is `::', that is invalid -- but sometimes
11378 people do try to write:
11382 Handle this gracefully by accepting the extra qualifier, and then
11383 issuing an error about it later if this really is a
11384 class-head. If it turns out just to be an elaborated type
11385 specifier, remain silent. */
11386 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
11387 qualified_p
= true;
11389 push_deferring_access_checks (dk_no_check
);
11391 /* Determine the name of the class. Begin by looking for an
11392 optional nested-name-specifier. */
11393 nested_name_specifier
11394 = cp_parser_nested_name_specifier_opt (parser
,
11395 /*typename_keyword_p=*/false,
11396 /*check_dependency_p=*/false,
11398 /* If there was a nested-name-specifier, then there *must* be an
11400 if (nested_name_specifier
)
11402 /* Although the grammar says `identifier', it really means
11403 `class-name' or `template-name'. You are only allowed to
11404 define a class that has already been declared with this
11407 The proposed resolution for Core Issue 180 says that whever
11408 you see `class T::X' you should treat `X' as a type-name.
11410 It is OK to define an inaccessible class; for example:
11412 class A { class B; };
11415 We do not know if we will see a class-name, or a
11416 template-name. We look for a class-name first, in case the
11417 class-name is a template-id; if we looked for the
11418 template-name first we would stop after the template-name. */
11419 cp_parser_parse_tentatively (parser
);
11420 type
= cp_parser_class_name (parser
,
11421 /*typename_keyword_p=*/false,
11422 /*template_keyword_p=*/false,
11424 /*check_dependency_p=*/false,
11425 /*class_head_p=*/true);
11426 /* If that didn't work, ignore the nested-name-specifier. */
11427 if (!cp_parser_parse_definitely (parser
))
11429 invalid_nested_name_p
= true;
11430 id
= cp_parser_identifier (parser
);
11431 if (id
== error_mark_node
)
11434 /* If we could not find a corresponding TYPE, treat this
11435 declaration like an unqualified declaration. */
11436 if (type
== error_mark_node
)
11437 nested_name_specifier
= NULL_TREE
;
11438 /* Otherwise, count the number of templates used in TYPE and its
11439 containing scopes. */
11444 for (scope
= TREE_TYPE (type
);
11445 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
11446 scope
= (TYPE_P (scope
)
11447 ? TYPE_CONTEXT (scope
)
11448 : DECL_CONTEXT (scope
)))
11450 && CLASS_TYPE_P (scope
)
11451 && CLASSTYPE_TEMPLATE_INFO (scope
)
11452 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
11453 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
11457 /* Otherwise, the identifier is optional. */
11460 /* We don't know whether what comes next is a template-id,
11461 an identifier, or nothing at all. */
11462 cp_parser_parse_tentatively (parser
);
11463 /* Check for a template-id. */
11464 id
= cp_parser_template_id (parser
,
11465 /*template_keyword_p=*/false,
11466 /*check_dependency_p=*/true);
11467 /* If that didn't work, it could still be an identifier. */
11468 if (!cp_parser_parse_definitely (parser
))
11470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11471 id
= cp_parser_identifier (parser
);
11477 template_id_p
= true;
11482 pop_deferring_access_checks ();
11484 /* If it's not a `:' or a `{' then we can't really be looking at a
11485 class-head, since a class-head only appears as part of a
11486 class-specifier. We have to detect this situation before calling
11487 xref_tag, since that has irreversible side-effects. */
11488 if (!cp_parser_next_token_starts_class_definition_p (parser
))
11490 cp_parser_error (parser
, "expected `{' or `:'");
11491 return error_mark_node
;
11494 /* At this point, we're going ahead with the class-specifier, even
11495 if some other problem occurs. */
11496 cp_parser_commit_to_tentative_parse (parser
);
11497 /* Issue the error about the overly-qualified name now. */
11499 cp_parser_error (parser
,
11500 "global qualification of class name is invalid");
11501 else if (invalid_nested_name_p
)
11502 cp_parser_error (parser
,
11503 "qualified name does not name a class");
11504 /* Make sure that the right number of template parameters were
11506 if (!cp_parser_check_template_parameters (parser
, num_templates
))
11507 /* If something went wrong, there is no point in even trying to
11508 process the class-definition. */
11511 /* Look up the type. */
11514 type
= TREE_TYPE (id
);
11515 maybe_process_partial_specialization (type
);
11517 else if (!nested_name_specifier
)
11519 /* If the class was unnamed, create a dummy name. */
11521 id
= make_anon_name ();
11522 type
= xref_tag (class_key
, id
, attributes
, /*globalize=*/false,
11523 parser
->num_template_parameter_lists
);
11532 template <typename T> struct S { struct T };
11533 template <typename T> struct S<T>::T { };
11535 we will get a TYPENAME_TYPE when processing the definition of
11536 `S::T'. We need to resolve it to the actual type before we
11537 try to define it. */
11538 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
11540 class_type
= resolve_typename_type (TREE_TYPE (type
),
11541 /*only_current_p=*/false);
11542 if (class_type
!= error_mark_node
)
11543 type
= TYPE_NAME (class_type
);
11546 cp_parser_error (parser
, "could not resolve typename type");
11547 type
= error_mark_node
;
11551 /* Figure out in what scope the declaration is being placed. */
11552 scope
= current_scope ();
11554 scope
= current_namespace
;
11555 /* If that scope does not contain the scope in which the
11556 class was originally declared, the program is invalid. */
11557 if (scope
&& !is_ancestor (scope
, CP_DECL_CONTEXT (type
)))
11559 error ("declaration of `%D' in `%D' which does not "
11560 "enclose `%D'", type
, scope
, nested_name_specifier
);
11565 A declarator-id shall not be qualified exception of the
11566 definition of a ... nested class outside of its class
11567 ... [or] a the definition or explicit instantiation of a
11568 class member of a namespace outside of its namespace. */
11569 if (scope
== CP_DECL_CONTEXT (type
))
11571 pedwarn ("extra qualification ignored");
11572 nested_name_specifier
= NULL_TREE
;
11575 maybe_process_partial_specialization (TREE_TYPE (type
));
11576 class_type
= current_class_type
;
11577 /* Enter the scope indicated by the nested-name-specifier. */
11578 if (nested_name_specifier
)
11579 push_scope (nested_name_specifier
);
11580 /* Get the canonical version of this type. */
11581 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
11582 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11583 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
11584 type
= push_template_decl (type
);
11585 type
= TREE_TYPE (type
);
11586 if (nested_name_specifier
)
11587 *nested_name_specifier_p
= true;
11589 /* Indicate whether this class was declared as a `class' or as a
11591 if (TREE_CODE (type
) == RECORD_TYPE
)
11592 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
11593 cp_parser_check_class_key (class_key
, type
);
11595 /* Enter the scope containing the class; the names of base classes
11596 should be looked up in that context. For example, given:
11598 struct A { struct B {}; struct C; };
11599 struct A::C : B {};
11602 if (nested_name_specifier
)
11603 push_scope (nested_name_specifier
);
11604 /* Now, look for the base-clause. */
11605 token
= cp_lexer_peek_token (parser
->lexer
);
11606 if (token
->type
== CPP_COLON
)
11610 /* Get the list of base-classes. */
11611 bases
= cp_parser_base_clause (parser
);
11612 /* Process them. */
11613 xref_basetypes (type
, bases
);
11615 /* Leave the scope given by the nested-name-specifier. We will
11616 enter the class scope itself while processing the members. */
11617 if (nested_name_specifier
)
11618 pop_scope (nested_name_specifier
);
11623 /* Parse a class-key.
11630 Returns the kind of class-key specified, or none_type to indicate
11633 static enum tag_types
11634 cp_parser_class_key (cp_parser
* parser
)
11637 enum tag_types tag_type
;
11639 /* Look for the class-key. */
11640 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
11644 /* Check to see if the TOKEN is a class-key. */
11645 tag_type
= cp_parser_token_is_class_key (token
);
11647 cp_parser_error (parser
, "expected class-key");
11651 /* Parse an (optional) member-specification.
11653 member-specification:
11654 member-declaration member-specification [opt]
11655 access-specifier : member-specification [opt] */
11658 cp_parser_member_specification_opt (cp_parser
* parser
)
11665 /* Peek at the next token. */
11666 token
= cp_lexer_peek_token (parser
->lexer
);
11667 /* If it's a `}', or EOF then we've seen all the members. */
11668 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
11671 /* See if this token is a keyword. */
11672 keyword
= token
->keyword
;
11676 case RID_PROTECTED
:
11678 /* Consume the access-specifier. */
11679 cp_lexer_consume_token (parser
->lexer
);
11680 /* Remember which access-specifier is active. */
11681 current_access_specifier
= token
->value
;
11682 /* Look for the `:'. */
11683 cp_parser_require (parser
, CPP_COLON
, "`:'");
11687 /* Otherwise, the next construction must be a
11688 member-declaration. */
11689 cp_parser_member_declaration (parser
);
11694 /* Parse a member-declaration.
11696 member-declaration:
11697 decl-specifier-seq [opt] member-declarator-list [opt] ;
11698 function-definition ; [opt]
11699 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11701 template-declaration
11703 member-declarator-list:
11705 member-declarator-list , member-declarator
11708 declarator pure-specifier [opt]
11709 declarator constant-initializer [opt]
11710 identifier [opt] : constant-expression
11714 member-declaration:
11715 __extension__ member-declaration
11718 declarator attributes [opt] pure-specifier [opt]
11719 declarator attributes [opt] constant-initializer [opt]
11720 identifier [opt] attributes [opt] : constant-expression */
11723 cp_parser_member_declaration (cp_parser
* parser
)
11725 tree decl_specifiers
;
11726 tree prefix_attributes
;
11728 int declares_class_or_enum
;
11731 int saved_pedantic
;
11733 /* Check for the `__extension__' keyword. */
11734 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11737 cp_parser_member_declaration (parser
);
11738 /* Restore the old value of the PEDANTIC flag. */
11739 pedantic
= saved_pedantic
;
11744 /* Check for a template-declaration. */
11745 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
11747 /* Parse the template-declaration. */
11748 cp_parser_template_declaration (parser
, /*member_p=*/true);
11753 /* Check for a using-declaration. */
11754 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
11756 /* Parse the using-declaration. */
11757 cp_parser_using_declaration (parser
);
11762 /* We can't tell whether we're looking at a declaration or a
11763 function-definition. */
11764 cp_parser_parse_tentatively (parser
);
11766 /* Parse the decl-specifier-seq. */
11768 = cp_parser_decl_specifier_seq (parser
,
11769 CP_PARSER_FLAGS_OPTIONAL
,
11770 &prefix_attributes
,
11771 &declares_class_or_enum
);
11772 /* Check for an invalid type-name. */
11773 if (cp_parser_diagnose_invalid_type_name (parser
))
11775 /* If there is no declarator, then the decl-specifier-seq should
11777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11779 /* If there was no decl-specifier-seq, and the next token is a
11780 `;', then we have something like:
11786 Each member-declaration shall declare at least one member
11787 name of the class. */
11788 if (!decl_specifiers
)
11791 pedwarn ("extra semicolon");
11797 /* See if this declaration is a friend. */
11798 friend_p
= cp_parser_friend_p (decl_specifiers
);
11799 /* If there were decl-specifiers, check to see if there was
11800 a class-declaration. */
11801 type
= check_tag_decl (decl_specifiers
);
11802 /* Nested classes have already been added to the class, but
11803 a `friend' needs to be explicitly registered. */
11806 /* If the `friend' keyword was present, the friend must
11807 be introduced with a class-key. */
11808 if (!declares_class_or_enum
)
11809 error ("a class-key must be used when declaring a friend");
11812 template <typename T> struct A {
11813 friend struct A<T>::B;
11816 A<T>::B will be represented by a TYPENAME_TYPE, and
11817 therefore not recognized by check_tag_decl. */
11822 for (specifier
= decl_specifiers
;
11824 specifier
= TREE_CHAIN (specifier
))
11826 tree s
= TREE_VALUE (specifier
);
11828 if (TREE_CODE (s
) == IDENTIFIER_NODE
11829 && IDENTIFIER_GLOBAL_VALUE (s
))
11830 type
= IDENTIFIER_GLOBAL_VALUE (s
);
11831 if (TREE_CODE (s
) == TYPE_DECL
)
11841 error ("friend declaration does not name a class or "
11844 make_friend_class (current_class_type
, type
,
11845 /*complain=*/true);
11847 /* If there is no TYPE, an error message will already have
11851 /* An anonymous aggregate has to be handled specially; such
11852 a declaration really declares a data member (with a
11853 particular type), as opposed to a nested class. */
11854 else if (ANON_AGGR_TYPE_P (type
))
11856 /* Remove constructors and such from TYPE, now that we
11857 know it is an anonymous aggregate. */
11858 fixup_anonymous_aggr (type
);
11859 /* And make the corresponding data member. */
11860 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
11861 /* Add it to the class. */
11862 finish_member_declaration (decl
);
11868 /* See if these declarations will be friends. */
11869 friend_p
= cp_parser_friend_p (decl_specifiers
);
11871 /* Keep going until we hit the `;' at the end of the
11873 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11875 tree attributes
= NULL_TREE
;
11876 tree first_attribute
;
11878 /* Peek at the next token. */
11879 token
= cp_lexer_peek_token (parser
->lexer
);
11881 /* Check for a bitfield declaration. */
11882 if (token
->type
== CPP_COLON
11883 || (token
->type
== CPP_NAME
11884 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11890 /* Get the name of the bitfield. Note that we cannot just
11891 check TOKEN here because it may have been invalidated by
11892 the call to cp_lexer_peek_nth_token above. */
11893 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
11894 identifier
= cp_parser_identifier (parser
);
11896 identifier
= NULL_TREE
;
11898 /* Consume the `:' token. */
11899 cp_lexer_consume_token (parser
->lexer
);
11900 /* Get the width of the bitfield. */
11902 = cp_parser_constant_expression (parser
,
11903 /*allow_non_constant=*/false,
11906 /* Look for attributes that apply to the bitfield. */
11907 attributes
= cp_parser_attributes_opt (parser
);
11908 /* Remember which attributes are prefix attributes and
11910 first_attribute
= attributes
;
11911 /* Combine the attributes. */
11912 attributes
= chainon (prefix_attributes
, attributes
);
11914 /* Create the bitfield declaration. */
11915 decl
= grokbitfield (identifier
,
11918 /* Apply the attributes. */
11919 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
11925 tree asm_specification
;
11926 int ctor_dtor_or_conv_p
;
11928 /* Parse the declarator. */
11930 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11931 &ctor_dtor_or_conv_p
);
11933 /* If something went wrong parsing the declarator, make sure
11934 that we at least consume some tokens. */
11935 if (declarator
== error_mark_node
)
11937 /* Skip to the end of the statement. */
11938 cp_parser_skip_to_end_of_statement (parser
);
11942 cp_parser_check_for_definition_in_return_type
11943 (declarator
, declares_class_or_enum
);
11945 /* Look for an asm-specification. */
11946 asm_specification
= cp_parser_asm_specification_opt (parser
);
11947 /* Look for attributes that apply to the declaration. */
11948 attributes
= cp_parser_attributes_opt (parser
);
11949 /* Remember which attributes are prefix attributes and
11951 first_attribute
= attributes
;
11952 /* Combine the attributes. */
11953 attributes
= chainon (prefix_attributes
, attributes
);
11955 /* If it's an `=', then we have a constant-initializer or a
11956 pure-specifier. It is not correct to parse the
11957 initializer before registering the member declaration
11958 since the member declaration should be in scope while
11959 its initializer is processed. However, the rest of the
11960 front end does not yet provide an interface that allows
11961 us to handle this correctly. */
11962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11966 A pure-specifier shall be used only in the declaration of
11967 a virtual function.
11969 A member-declarator can contain a constant-initializer
11970 only if it declares a static member of integral or
11973 Therefore, if the DECLARATOR is for a function, we look
11974 for a pure-specifier; otherwise, we look for a
11975 constant-initializer. When we call `grokfield', it will
11976 perform more stringent semantics checks. */
11977 if (TREE_CODE (declarator
) == CALL_EXPR
)
11978 initializer
= cp_parser_pure_specifier (parser
);
11981 /* This declaration cannot be a function
11983 cp_parser_commit_to_tentative_parse (parser
);
11984 /* Parse the initializer. */
11985 initializer
= cp_parser_constant_initializer (parser
);
11988 /* Otherwise, there is no initializer. */
11990 initializer
= NULL_TREE
;
11992 /* See if we are probably looking at a function
11993 definition. We are certainly not looking at at a
11994 member-declarator. Calling `grokfield' has
11995 side-effects, so we must not do it unless we are sure
11996 that we are looking at a member-declarator. */
11997 if (cp_parser_token_starts_function_definition_p
11998 (cp_lexer_peek_token (parser
->lexer
)))
11999 decl
= error_mark_node
;
12002 /* Create the declaration. */
12003 decl
= grokfield (declarator
, decl_specifiers
,
12004 initializer
, asm_specification
,
12006 /* Any initialization must have been from a
12007 constant-expression. */
12008 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
12009 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
12013 /* Reset PREFIX_ATTRIBUTES. */
12014 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12015 attributes
= TREE_CHAIN (attributes
);
12017 TREE_CHAIN (attributes
) = NULL_TREE
;
12019 /* If there is any qualification still in effect, clear it
12020 now; we will be starting fresh with the next declarator. */
12021 parser
->scope
= NULL_TREE
;
12022 parser
->qualifying_scope
= NULL_TREE
;
12023 parser
->object_scope
= NULL_TREE
;
12024 /* If it's a `,', then there are more declarators. */
12025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12026 cp_lexer_consume_token (parser
->lexer
);
12027 /* If the next token isn't a `;', then we have a parse error. */
12028 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12031 cp_parser_error (parser
, "expected `;'");
12032 /* Skip tokens until we find a `;' */
12033 cp_parser_skip_to_end_of_statement (parser
);
12040 /* Add DECL to the list of members. */
12042 finish_member_declaration (decl
);
12044 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12045 cp_parser_save_default_args (parser
, decl
);
12050 /* If everything went well, look for the `;'. */
12051 if (cp_parser_parse_definitely (parser
))
12053 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12057 /* Parse the function-definition. */
12058 decl
= cp_parser_function_definition (parser
, &friend_p
);
12059 /* If the member was not a friend, declare it here. */
12061 finish_member_declaration (decl
);
12062 /* Peek at the next token. */
12063 token
= cp_lexer_peek_token (parser
->lexer
);
12064 /* If the next token is a semicolon, consume it. */
12065 if (token
->type
== CPP_SEMICOLON
)
12066 cp_lexer_consume_token (parser
->lexer
);
12069 /* Parse a pure-specifier.
12074 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12075 Otherwiser, ERROR_MARK_NODE is returned. */
12078 cp_parser_pure_specifier (cp_parser
* parser
)
12082 /* Look for the `=' token. */
12083 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12084 return error_mark_node
;
12085 /* Look for the `0' token. */
12086 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12087 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12088 to get information from the lexer about how the number was
12089 spelled in order to fix this problem. */
12090 if (!token
|| !integer_zerop (token
->value
))
12091 return error_mark_node
;
12093 return integer_zero_node
;
12096 /* Parse a constant-initializer.
12098 constant-initializer:
12099 = constant-expression
12101 Returns a representation of the constant-expression. */
12104 cp_parser_constant_initializer (cp_parser
* parser
)
12106 /* Look for the `=' token. */
12107 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12108 return error_mark_node
;
12110 /* It is invalid to write:
12112 struct S { static const int i = { 7 }; };
12115 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12117 cp_parser_error (parser
,
12118 "a brace-enclosed initializer is not allowed here");
12119 /* Consume the opening brace. */
12120 cp_lexer_consume_token (parser
->lexer
);
12121 /* Skip the initializer. */
12122 cp_parser_skip_to_closing_brace (parser
);
12123 /* Look for the trailing `}'. */
12124 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12126 return error_mark_node
;
12129 return cp_parser_constant_expression (parser
,
12130 /*allow_non_constant=*/false,
12134 /* Derived classes [gram.class.derived] */
12136 /* Parse a base-clause.
12139 : base-specifier-list
12141 base-specifier-list:
12143 base-specifier-list , base-specifier
12145 Returns a TREE_LIST representing the base-classes, in the order in
12146 which they were declared. The representation of each node is as
12147 described by cp_parser_base_specifier.
12149 In the case that no bases are specified, this function will return
12150 NULL_TREE, not ERROR_MARK_NODE. */
12153 cp_parser_base_clause (cp_parser
* parser
)
12155 tree bases
= NULL_TREE
;
12157 /* Look for the `:' that begins the list. */
12158 cp_parser_require (parser
, CPP_COLON
, "`:'");
12160 /* Scan the base-specifier-list. */
12166 /* Look for the base-specifier. */
12167 base
= cp_parser_base_specifier (parser
);
12168 /* Add BASE to the front of the list. */
12169 if (base
!= error_mark_node
)
12171 TREE_CHAIN (base
) = bases
;
12174 /* Peek at the next token. */
12175 token
= cp_lexer_peek_token (parser
->lexer
);
12176 /* If it's not a comma, then the list is complete. */
12177 if (token
->type
!= CPP_COMMA
)
12179 /* Consume the `,'. */
12180 cp_lexer_consume_token (parser
->lexer
);
12183 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12184 base class had a qualified name. However, the next name that
12185 appears is certainly not qualified. */
12186 parser
->scope
= NULL_TREE
;
12187 parser
->qualifying_scope
= NULL_TREE
;
12188 parser
->object_scope
= NULL_TREE
;
12190 return nreverse (bases
);
12193 /* Parse a base-specifier.
12196 :: [opt] nested-name-specifier [opt] class-name
12197 virtual access-specifier [opt] :: [opt] nested-name-specifier
12199 access-specifier virtual [opt] :: [opt] nested-name-specifier
12202 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12203 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12204 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12205 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12208 cp_parser_base_specifier (cp_parser
* parser
)
12212 bool virtual_p
= false;
12213 bool duplicate_virtual_error_issued_p
= false;
12214 bool duplicate_access_error_issued_p
= false;
12215 bool class_scope_p
, template_p
;
12216 tree access
= access_default_node
;
12219 /* Process the optional `virtual' and `access-specifier'. */
12222 /* Peek at the next token. */
12223 token
= cp_lexer_peek_token (parser
->lexer
);
12224 /* Process `virtual'. */
12225 switch (token
->keyword
)
12228 /* If `virtual' appears more than once, issue an error. */
12229 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
12231 cp_parser_error (parser
,
12232 "`virtual' specified more than once in base-specified");
12233 duplicate_virtual_error_issued_p
= true;
12238 /* Consume the `virtual' token. */
12239 cp_lexer_consume_token (parser
->lexer
);
12244 case RID_PROTECTED
:
12246 /* If more than one access specifier appears, issue an
12248 if (access
!= access_default_node
12249 && !duplicate_access_error_issued_p
)
12251 cp_parser_error (parser
,
12252 "more than one access specifier in base-specified");
12253 duplicate_access_error_issued_p
= true;
12256 access
= ridpointers
[(int) token
->keyword
];
12258 /* Consume the access-specifier. */
12259 cp_lexer_consume_token (parser
->lexer
);
12269 /* Look for the optional `::' operator. */
12270 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
12271 /* Look for the nested-name-specifier. The simplest way to
12276 The keyword `typename' is not permitted in a base-specifier or
12277 mem-initializer; in these contexts a qualified name that
12278 depends on a template-parameter is implicitly assumed to be a
12281 is to pretend that we have seen the `typename' keyword at this
12283 cp_parser_nested_name_specifier_opt (parser
,
12284 /*typename_keyword_p=*/true,
12285 /*check_dependency_p=*/true,
12287 /* If the base class is given by a qualified name, assume that names
12288 we see are type names or templates, as appropriate. */
12289 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
12290 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
12292 /* Finally, look for the class-name. */
12293 type
= cp_parser_class_name (parser
,
12297 /*check_dependency_p=*/true,
12298 /*class_head_p=*/false);
12300 if (type
== error_mark_node
)
12301 return error_mark_node
;
12303 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
12306 /* Exception handling [gram.exception] */
12308 /* Parse an (optional) exception-specification.
12310 exception-specification:
12311 throw ( type-id-list [opt] )
12313 Returns a TREE_LIST representing the exception-specification. The
12314 TREE_VALUE of each node is a type. */
12317 cp_parser_exception_specification_opt (cp_parser
* parser
)
12322 /* Peek at the next token. */
12323 token
= cp_lexer_peek_token (parser
->lexer
);
12324 /* If it's not `throw', then there's no exception-specification. */
12325 if (!cp_parser_is_keyword (token
, RID_THROW
))
12328 /* Consume the `throw'. */
12329 cp_lexer_consume_token (parser
->lexer
);
12331 /* Look for the `('. */
12332 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12334 /* Peek at the next token. */
12335 token
= cp_lexer_peek_token (parser
->lexer
);
12336 /* If it's not a `)', then there is a type-id-list. */
12337 if (token
->type
!= CPP_CLOSE_PAREN
)
12339 const char *saved_message
;
12341 /* Types may not be defined in an exception-specification. */
12342 saved_message
= parser
->type_definition_forbidden_message
;
12343 parser
->type_definition_forbidden_message
12344 = "types may not be defined in an exception-specification";
12345 /* Parse the type-id-list. */
12346 type_id_list
= cp_parser_type_id_list (parser
);
12347 /* Restore the saved message. */
12348 parser
->type_definition_forbidden_message
= saved_message
;
12351 type_id_list
= empty_except_spec
;
12353 /* Look for the `)'. */
12354 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12356 return type_id_list
;
12359 /* Parse an (optional) type-id-list.
12363 type-id-list , type-id
12365 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12366 in the order that the types were presented. */
12369 cp_parser_type_id_list (cp_parser
* parser
)
12371 tree types
= NULL_TREE
;
12378 /* Get the next type-id. */
12379 type
= cp_parser_type_id (parser
);
12380 /* Add it to the list. */
12381 types
= add_exception_specifier (types
, type
, /*complain=*/1);
12382 /* Peek at the next token. */
12383 token
= cp_lexer_peek_token (parser
->lexer
);
12384 /* If it is not a `,', we are done. */
12385 if (token
->type
!= CPP_COMMA
)
12387 /* Consume the `,'. */
12388 cp_lexer_consume_token (parser
->lexer
);
12391 return nreverse (types
);
12394 /* Parse a try-block.
12397 try compound-statement handler-seq */
12400 cp_parser_try_block (cp_parser
* parser
)
12404 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
12405 try_block
= begin_try_block ();
12406 cp_parser_compound_statement (parser
, false);
12407 finish_try_block (try_block
);
12408 cp_parser_handler_seq (parser
);
12409 finish_handler_sequence (try_block
);
12414 /* Parse a function-try-block.
12416 function-try-block:
12417 try ctor-initializer [opt] function-body handler-seq */
12420 cp_parser_function_try_block (cp_parser
* parser
)
12423 bool ctor_initializer_p
;
12425 /* Look for the `try' keyword. */
12426 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
12428 /* Let the rest of the front-end know where we are. */
12429 try_block
= begin_function_try_block ();
12430 /* Parse the function-body. */
12432 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
12433 /* We're done with the `try' part. */
12434 finish_function_try_block (try_block
);
12435 /* Parse the handlers. */
12436 cp_parser_handler_seq (parser
);
12437 /* We're done with the handlers. */
12438 finish_function_handler_sequence (try_block
);
12440 return ctor_initializer_p
;
12443 /* Parse a handler-seq.
12446 handler handler-seq [opt] */
12449 cp_parser_handler_seq (cp_parser
* parser
)
12455 /* Parse the handler. */
12456 cp_parser_handler (parser
);
12457 /* Peek at the next token. */
12458 token
= cp_lexer_peek_token (parser
->lexer
);
12459 /* If it's not `catch' then there are no more handlers. */
12460 if (!cp_parser_is_keyword (token
, RID_CATCH
))
12465 /* Parse a handler.
12468 catch ( exception-declaration ) compound-statement */
12471 cp_parser_handler (cp_parser
* parser
)
12476 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
12477 handler
= begin_handler ();
12478 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12479 declaration
= cp_parser_exception_declaration (parser
);
12480 finish_handler_parms (declaration
, handler
);
12481 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12482 cp_parser_compound_statement (parser
, false);
12483 finish_handler (handler
);
12486 /* Parse an exception-declaration.
12488 exception-declaration:
12489 type-specifier-seq declarator
12490 type-specifier-seq abstract-declarator
12494 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12495 ellipsis variant is used. */
12498 cp_parser_exception_declaration (cp_parser
* parser
)
12500 tree type_specifiers
;
12502 const char *saved_message
;
12504 /* If it's an ellipsis, it's easy to handle. */
12505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12507 /* Consume the `...' token. */
12508 cp_lexer_consume_token (parser
->lexer
);
12512 /* Types may not be defined in exception-declarations. */
12513 saved_message
= parser
->type_definition_forbidden_message
;
12514 parser
->type_definition_forbidden_message
12515 = "types may not be defined in exception-declarations";
12517 /* Parse the type-specifier-seq. */
12518 type_specifiers
= cp_parser_type_specifier_seq (parser
);
12519 /* If it's a `)', then there is no declarator. */
12520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
12521 declarator
= NULL_TREE
;
12523 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
12524 /*ctor_dtor_or_conv_p=*/NULL
);
12526 /* Restore the saved message. */
12527 parser
->type_definition_forbidden_message
= saved_message
;
12529 return start_handler_parms (type_specifiers
, declarator
);
12532 /* Parse a throw-expression.
12535 throw assignment-expression [opt]
12537 Returns a THROW_EXPR representing the throw-expression. */
12540 cp_parser_throw_expression (cp_parser
* parser
)
12544 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
12545 /* We can't be sure if there is an assignment-expression or not. */
12546 cp_parser_parse_tentatively (parser
);
12548 expression
= cp_parser_assignment_expression (parser
);
12549 /* If it didn't work, this is just a rethrow. */
12550 if (!cp_parser_parse_definitely (parser
))
12551 expression
= NULL_TREE
;
12553 return build_throw (expression
);
12556 /* GNU Extensions */
12558 /* Parse an (optional) asm-specification.
12561 asm ( string-literal )
12563 If the asm-specification is present, returns a STRING_CST
12564 corresponding to the string-literal. Otherwise, returns
12568 cp_parser_asm_specification_opt (cp_parser
* parser
)
12571 tree asm_specification
;
12573 /* Peek at the next token. */
12574 token
= cp_lexer_peek_token (parser
->lexer
);
12575 /* If the next token isn't the `asm' keyword, then there's no
12576 asm-specification. */
12577 if (!cp_parser_is_keyword (token
, RID_ASM
))
12580 /* Consume the `asm' token. */
12581 cp_lexer_consume_token (parser
->lexer
);
12582 /* Look for the `('. */
12583 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12585 /* Look for the string-literal. */
12586 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12588 asm_specification
= token
->value
;
12590 asm_specification
= NULL_TREE
;
12592 /* Look for the `)'. */
12593 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
12595 return asm_specification
;
12598 /* Parse an asm-operand-list.
12602 asm-operand-list , asm-operand
12605 string-literal ( expression )
12606 [ string-literal ] string-literal ( expression )
12608 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12609 each node is the expression. The TREE_PURPOSE is itself a
12610 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12611 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12612 is a STRING_CST for the string literal before the parenthesis. */
12615 cp_parser_asm_operand_list (cp_parser
* parser
)
12617 tree asm_operands
= NULL_TREE
;
12621 tree string_literal
;
12626 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
12628 /* Consume the `[' token. */
12629 cp_lexer_consume_token (parser
->lexer
);
12630 /* Read the operand name. */
12631 name
= cp_parser_identifier (parser
);
12632 if (name
!= error_mark_node
)
12633 name
= build_string (IDENTIFIER_LENGTH (name
),
12634 IDENTIFIER_POINTER (name
));
12635 /* Look for the closing `]'. */
12636 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
12640 /* Look for the string-literal. */
12641 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12642 string_literal
= token
? token
->value
: error_mark_node
;
12643 /* Look for the `('. */
12644 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12645 /* Parse the expression. */
12646 expression
= cp_parser_expression (parser
);
12647 /* Look for the `)'. */
12648 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12649 /* Add this operand to the list. */
12650 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
12653 /* If the next token is not a `,', there are no more
12655 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12657 /* Consume the `,'. */
12658 cp_lexer_consume_token (parser
->lexer
);
12661 return nreverse (asm_operands
);
12664 /* Parse an asm-clobber-list.
12668 asm-clobber-list , string-literal
12670 Returns a TREE_LIST, indicating the clobbers in the order that they
12671 appeared. The TREE_VALUE of each node is a STRING_CST. */
12674 cp_parser_asm_clobber_list (cp_parser
* parser
)
12676 tree clobbers
= NULL_TREE
;
12681 tree string_literal
;
12683 /* Look for the string literal. */
12684 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12685 string_literal
= token
? token
->value
: error_mark_node
;
12686 /* Add it to the list. */
12687 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
12688 /* If the next token is not a `,', then the list is
12690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12692 /* Consume the `,' token. */
12693 cp_lexer_consume_token (parser
->lexer
);
12699 /* Parse an (optional) series of attributes.
12702 attributes attribute
12705 __attribute__ (( attribute-list [opt] ))
12707 The return value is as for cp_parser_attribute_list. */
12710 cp_parser_attributes_opt (cp_parser
* parser
)
12712 tree attributes
= NULL_TREE
;
12717 tree attribute_list
;
12719 /* Peek at the next token. */
12720 token
= cp_lexer_peek_token (parser
->lexer
);
12721 /* If it's not `__attribute__', then we're done. */
12722 if (token
->keyword
!= RID_ATTRIBUTE
)
12725 /* Consume the `__attribute__' keyword. */
12726 cp_lexer_consume_token (parser
->lexer
);
12727 /* Look for the two `(' tokens. */
12728 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12729 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12731 /* Peek at the next token. */
12732 token
= cp_lexer_peek_token (parser
->lexer
);
12733 if (token
->type
!= CPP_CLOSE_PAREN
)
12734 /* Parse the attribute-list. */
12735 attribute_list
= cp_parser_attribute_list (parser
);
12737 /* If the next token is a `)', then there is no attribute
12739 attribute_list
= NULL
;
12741 /* Look for the two `)' tokens. */
12742 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12743 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12745 /* Add these new attributes to the list. */
12746 attributes
= chainon (attributes
, attribute_list
);
12752 /* Parse an attribute-list.
12756 attribute-list , attribute
12760 identifier ( identifier )
12761 identifier ( identifier , expression-list )
12762 identifier ( expression-list )
12764 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12765 TREE_PURPOSE of each node is the identifier indicating which
12766 attribute is in use. The TREE_VALUE represents the arguments, if
12770 cp_parser_attribute_list (cp_parser
* parser
)
12772 tree attribute_list
= NULL_TREE
;
12780 /* Look for the identifier. We also allow keywords here; for
12781 example `__attribute__ ((const))' is legal. */
12782 token
= cp_lexer_peek_token (parser
->lexer
);
12783 if (token
->type
!= CPP_NAME
12784 && token
->type
!= CPP_KEYWORD
)
12785 return error_mark_node
;
12786 /* Consume the token. */
12787 token
= cp_lexer_consume_token (parser
->lexer
);
12789 /* Save away the identifier that indicates which attribute this is. */
12790 identifier
= token
->value
;
12791 attribute
= build_tree_list (identifier
, NULL_TREE
);
12793 /* Peek at the next token. */
12794 token
= cp_lexer_peek_token (parser
->lexer
);
12795 /* If it's an `(', then parse the attribute arguments. */
12796 if (token
->type
== CPP_OPEN_PAREN
)
12800 arguments
= (cp_parser_parenthesized_expression_list
12801 (parser
, true, /*non_constant_p=*/NULL
));
12802 /* Save the identifier and arguments away. */
12803 TREE_VALUE (attribute
) = arguments
;
12806 /* Add this attribute to the list. */
12807 TREE_CHAIN (attribute
) = attribute_list
;
12808 attribute_list
= attribute
;
12810 /* Now, look for more attributes. */
12811 token
= cp_lexer_peek_token (parser
->lexer
);
12812 /* If the next token isn't a `,', we're done. */
12813 if (token
->type
!= CPP_COMMA
)
12816 /* Consume the commma and keep going. */
12817 cp_lexer_consume_token (parser
->lexer
);
12820 /* We built up the list in reverse order. */
12821 return nreverse (attribute_list
);
12824 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
12825 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12826 current value of the PEDANTIC flag, regardless of whether or not
12827 the `__extension__' keyword is present. The caller is responsible
12828 for restoring the value of the PEDANTIC flag. */
12831 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
12833 /* Save the old value of the PEDANTIC flag. */
12834 *saved_pedantic
= pedantic
;
12836 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
12838 /* Consume the `__extension__' token. */
12839 cp_lexer_consume_token (parser
->lexer
);
12840 /* We're not being pedantic while the `__extension__' keyword is
12850 /* Parse a label declaration.
12853 __label__ label-declarator-seq ;
12855 label-declarator-seq:
12856 identifier , label-declarator-seq
12860 cp_parser_label_declaration (cp_parser
* parser
)
12862 /* Look for the `__label__' keyword. */
12863 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
12869 /* Look for an identifier. */
12870 identifier
= cp_parser_identifier (parser
);
12871 /* Declare it as a lobel. */
12872 finish_label_decl (identifier
);
12873 /* If the next token is a `;', stop. */
12874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12876 /* Look for the `,' separating the label declarations. */
12877 cp_parser_require (parser
, CPP_COMMA
, "`,'");
12880 /* Look for the final `;'. */
12881 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12884 /* Support Functions */
12886 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12887 NAME should have one of the representations used for an
12888 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12889 is returned. If PARSER->SCOPE is a dependent type, then a
12890 SCOPE_REF is returned.
12892 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12893 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12894 was formed. Abstractly, such entities should not be passed to this
12895 function, because they do not need to be looked up, but it is
12896 simpler to check for this special case here, rather than at the
12899 In cases not explicitly covered above, this function returns a
12900 DECL, OVERLOAD, or baselink representing the result of the lookup.
12901 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12904 If IS_TYPE is TRUE, bindings that do not refer to types are
12907 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12910 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12914 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
12915 bool is_type
, bool is_namespace
, bool check_dependency
)
12918 tree object_type
= parser
->context
->object_type
;
12920 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12921 no longer valid. Note that if we are parsing tentatively, and
12922 the parse fails, OBJECT_TYPE will be automatically restored. */
12923 parser
->context
->object_type
= NULL_TREE
;
12925 if (name
== error_mark_node
)
12926 return error_mark_node
;
12928 /* A template-id has already been resolved; there is no lookup to
12930 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12932 if (BASELINK_P (name
))
12934 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
12935 == TEMPLATE_ID_EXPR
),
12940 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12941 it should already have been checked to make sure that the name
12942 used matches the type being destroyed. */
12943 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12947 /* Figure out to which type this destructor applies. */
12949 type
= parser
->scope
;
12950 else if (object_type
)
12951 type
= object_type
;
12953 type
= current_class_type
;
12954 /* If that's not a class type, there is no destructor. */
12955 if (!type
|| !CLASS_TYPE_P (type
))
12956 return error_mark_node
;
12957 /* If it was a class type, return the destructor. */
12958 return CLASSTYPE_DESTRUCTORS (type
);
12961 /* By this point, the NAME should be an ordinary identifier. If
12962 the id-expression was a qualified name, the qualifying scope is
12963 stored in PARSER->SCOPE at this point. */
12964 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
12967 /* Perform the lookup. */
12972 if (parser
->scope
== error_mark_node
)
12973 return error_mark_node
;
12975 /* If the SCOPE is dependent, the lookup must be deferred until
12976 the template is instantiated -- unless we are explicitly
12977 looking up names in uninstantiated templates. Even then, we
12978 cannot look up the name if the scope is not a class type; it
12979 might, for example, be a template type parameter. */
12980 dependent_p
= (TYPE_P (parser
->scope
)
12981 && !(parser
->in_declarator_p
12982 && currently_open_class (parser
->scope
))
12983 && dependent_type_p (parser
->scope
));
12984 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
12988 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
12990 /* The resolution to Core Issue 180 says that `struct A::B'
12991 should be considered a type-name, even if `A' is
12993 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
12999 /* If PARSER->SCOPE is a dependent type, then it must be a
13000 class type, and we must not be checking dependencies;
13001 otherwise, we would have processed this lookup above. So
13002 that PARSER->SCOPE is not considered a dependent base by
13003 lookup_member, we must enter the scope here. */
13005 push_scope (parser
->scope
);
13006 /* If the PARSER->SCOPE is a a template specialization, it
13007 may be instantiated during name lookup. In that case,
13008 errors may be issued. Even if we rollback the current
13009 tentative parse, those errors are valid. */
13010 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
13011 /*complain=*/true);
13013 pop_scope (parser
->scope
);
13015 parser
->qualifying_scope
= parser
->scope
;
13016 parser
->object_scope
= NULL_TREE
;
13018 else if (object_type
)
13020 tree object_decl
= NULL_TREE
;
13021 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13022 OBJECT_TYPE is not a class. */
13023 if (CLASS_TYPE_P (object_type
))
13024 /* If the OBJECT_TYPE is a template specialization, it may
13025 be instantiated during name lookup. In that case, errors
13026 may be issued. Even if we rollback the current tentative
13027 parse, those errors are valid. */
13028 object_decl
= lookup_member (object_type
,
13030 /*protect=*/0, is_type
);
13031 /* Look it up in the enclosing context, too. */
13032 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13035 parser
->object_scope
= object_type
;
13036 parser
->qualifying_scope
= NULL_TREE
;
13038 decl
= object_decl
;
13042 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13045 parser
->qualifying_scope
= NULL_TREE
;
13046 parser
->object_scope
= NULL_TREE
;
13049 /* If the lookup failed, let our caller know. */
13051 || decl
== error_mark_node
13052 || (TREE_CODE (decl
) == FUNCTION_DECL
13053 && DECL_ANTICIPATED (decl
)))
13054 return error_mark_node
;
13056 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13057 if (TREE_CODE (decl
) == TREE_LIST
)
13059 /* The error message we have to print is too complicated for
13060 cp_parser_error, so we incorporate its actions directly. */
13061 if (!cp_parser_simulate_error (parser
))
13063 error ("reference to `%D' is ambiguous", name
);
13064 print_candidates (decl
);
13066 return error_mark_node
;
13069 my_friendly_assert (DECL_P (decl
)
13070 || TREE_CODE (decl
) == OVERLOAD
13071 || TREE_CODE (decl
) == SCOPE_REF
13072 || BASELINK_P (decl
),
13075 /* If we have resolved the name of a member declaration, check to
13076 see if the declaration is accessible. When the name resolves to
13077 set of overloaded functions, accessibility is checked when
13078 overload resolution is done.
13080 During an explicit instantiation, access is not checked at all,
13081 as per [temp.explicit]. */
13083 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
13088 /* Like cp_parser_lookup_name, but for use in the typical case where
13089 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13093 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13095 return cp_parser_lookup_name (parser
, name
,
13097 /*is_namespace=*/false,
13098 /*check_dependency=*/true);
13101 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13102 the current context, return the TYPE_DECL. If TAG_NAME_P is
13103 true, the DECL indicates the class being defined in a class-head,
13104 or declared in an elaborated-type-specifier.
13106 Otherwise, return DECL. */
13109 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13111 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13112 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13115 template <typename T> struct B;
13118 template <typename T> struct A::B {};
13120 Similarly, in a elaborated-type-specifier:
13122 namespace N { struct X{}; }
13125 template <typename T> friend struct N::X;
13128 However, if the DECL refers to a class type, and we are in
13129 the scope of the class, then the name lookup automatically
13130 finds the TYPE_DECL created by build_self_reference rather
13131 than a TEMPLATE_DECL. For example, in:
13133 template <class T> struct S {
13137 there is no need to handle such case. */
13139 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13140 return DECL_TEMPLATE_RESULT (decl
);
13145 /* If too many, or too few, template-parameter lists apply to the
13146 declarator, issue an error message. Returns TRUE if all went well,
13147 and FALSE otherwise. */
13150 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13153 unsigned num_templates
;
13155 /* We haven't seen any classes that involve template parameters yet. */
13158 switch (TREE_CODE (declarator
))
13165 tree main_declarator
= TREE_OPERAND (declarator
, 0);
13167 cp_parser_check_declarator_template_parameters (parser
,
13176 scope
= TREE_OPERAND (declarator
, 0);
13177 member
= TREE_OPERAND (declarator
, 1);
13179 /* If this is a pointer-to-member, then we are not interested
13180 in the SCOPE, because it does not qualify the thing that is
13182 if (TREE_CODE (member
) == INDIRECT_REF
)
13183 return (cp_parser_check_declarator_template_parameters
13186 while (scope
&& CLASS_TYPE_P (scope
))
13188 /* You're supposed to have one `template <...>'
13189 for every template class, but you don't need one
13190 for a full specialization. For example:
13192 template <class T> struct S{};
13193 template <> struct S<int> { void f(); };
13194 void S<int>::f () {}
13196 is correct; there shouldn't be a `template <>' for
13197 the definition of `S<int>::f'. */
13198 if (CLASSTYPE_TEMPLATE_INFO (scope
)
13199 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
13200 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
13201 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
13204 scope
= TYPE_CONTEXT (scope
);
13208 /* Fall through. */
13211 /* If the DECLARATOR has the form `X<y>' then it uses one
13212 additional level of template parameters. */
13213 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
13216 return cp_parser_check_template_parameters (parser
,
13221 /* NUM_TEMPLATES were used in the current declaration. If that is
13222 invalid, return FALSE and issue an error messages. Otherwise,
13226 cp_parser_check_template_parameters (cp_parser
* parser
,
13227 unsigned num_templates
)
13229 /* If there are more template classes than parameter lists, we have
13232 template <class T> void S<T>::R<T>::f (); */
13233 if (parser
->num_template_parameter_lists
< num_templates
)
13235 error ("too few template-parameter-lists");
13238 /* If there are the same number of template classes and parameter
13239 lists, that's OK. */
13240 if (parser
->num_template_parameter_lists
== num_templates
)
13242 /* If there are more, but only one more, then we are referring to a
13243 member template. That's OK too. */
13244 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
13246 /* Otherwise, there are too many template parameter lists. We have
13249 template <class T> template <class U> void S::f(); */
13250 error ("too many template-parameter-lists");
13254 /* Parse a binary-expression of the general form:
13258 binary-expression <token> <expr>
13260 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13261 to parser the <expr>s. If the first production is used, then the
13262 value returned by FN is returned directly. Otherwise, a node with
13263 the indicated EXPR_TYPE is returned, with operands corresponding to
13264 the two sub-expressions. */
13267 cp_parser_binary_expression (cp_parser
* parser
,
13268 const cp_parser_token_tree_map token_tree_map
,
13269 cp_parser_expression_fn fn
)
13273 /* Parse the first expression. */
13274 lhs
= (*fn
) (parser
);
13275 /* Now, look for more expressions. */
13279 const cp_parser_token_tree_map_node
*map_node
;
13282 /* Peek at the next token. */
13283 token
= cp_lexer_peek_token (parser
->lexer
);
13284 /* If the token is `>', and that's not an operator at the
13285 moment, then we're done. */
13286 if (token
->type
== CPP_GREATER
13287 && !parser
->greater_than_is_operator_p
)
13289 /* If we find one of the tokens we want, build the corresponding
13290 tree representation. */
13291 for (map_node
= token_tree_map
;
13292 map_node
->token_type
!= CPP_EOF
;
13294 if (map_node
->token_type
== token
->type
)
13296 /* Consume the operator token. */
13297 cp_lexer_consume_token (parser
->lexer
);
13298 /* Parse the right-hand side of the expression. */
13299 rhs
= (*fn
) (parser
);
13300 /* Build the binary tree node. */
13301 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
);
13305 /* If the token wasn't one of the ones we want, we're done. */
13306 if (map_node
->token_type
== CPP_EOF
)
13313 /* Parse an optional `::' token indicating that the following name is
13314 from the global namespace. If so, PARSER->SCOPE is set to the
13315 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13316 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13317 Returns the new value of PARSER->SCOPE, if the `::' token is
13318 present, and NULL_TREE otherwise. */
13321 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
13325 /* Peek at the next token. */
13326 token
= cp_lexer_peek_token (parser
->lexer
);
13327 /* If we're looking at a `::' token then we're starting from the
13328 global namespace, not our current location. */
13329 if (token
->type
== CPP_SCOPE
)
13331 /* Consume the `::' token. */
13332 cp_lexer_consume_token (parser
->lexer
);
13333 /* Set the SCOPE so that we know where to start the lookup. */
13334 parser
->scope
= global_namespace
;
13335 parser
->qualifying_scope
= global_namespace
;
13336 parser
->object_scope
= NULL_TREE
;
13338 return parser
->scope
;
13340 else if (!current_scope_valid_p
)
13342 parser
->scope
= NULL_TREE
;
13343 parser
->qualifying_scope
= NULL_TREE
;
13344 parser
->object_scope
= NULL_TREE
;
13350 /* Returns TRUE if the upcoming token sequence is the start of a
13351 constructor declarator. If FRIEND_P is true, the declarator is
13352 preceded by the `friend' specifier. */
13355 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
13357 bool constructor_p
;
13358 tree type_decl
= NULL_TREE
;
13359 bool nested_name_p
;
13360 cp_token
*next_token
;
13362 /* The common case is that this is not a constructor declarator, so
13363 try to avoid doing lots of work if at all possible. It's not
13364 valid declare a constructor at function scope. */
13365 if (at_function_scope_p ())
13367 /* And only certain tokens can begin a constructor declarator. */
13368 next_token
= cp_lexer_peek_token (parser
->lexer
);
13369 if (next_token
->type
!= CPP_NAME
13370 && next_token
->type
!= CPP_SCOPE
13371 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
13372 && next_token
->type
!= CPP_TEMPLATE_ID
)
13375 /* Parse tentatively; we are going to roll back all of the tokens
13377 cp_parser_parse_tentatively (parser
);
13378 /* Assume that we are looking at a constructor declarator. */
13379 constructor_p
= true;
13381 /* Look for the optional `::' operator. */
13382 cp_parser_global_scope_opt (parser
,
13383 /*current_scope_valid_p=*/false);
13384 /* Look for the nested-name-specifier. */
13386 = (cp_parser_nested_name_specifier_opt (parser
,
13387 /*typename_keyword_p=*/false,
13388 /*check_dependency_p=*/false,
13391 /* Outside of a class-specifier, there must be a
13392 nested-name-specifier. */
13393 if (!nested_name_p
&&
13394 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
13396 constructor_p
= false;
13397 /* If we still think that this might be a constructor-declarator,
13398 look for a class-name. */
13403 template <typename T> struct S { S(); };
13404 template <typename T> S<T>::S ();
13406 we must recognize that the nested `S' names a class.
13409 template <typename T> S<T>::S<T> ();
13411 we must recognize that the nested `S' names a template. */
13412 type_decl
= cp_parser_class_name (parser
,
13413 /*typename_keyword_p=*/false,
13414 /*template_keyword_p=*/false,
13416 /*check_dependency_p=*/false,
13417 /*class_head_p=*/false);
13418 /* If there was no class-name, then this is not a constructor. */
13419 constructor_p
= !cp_parser_error_occurred (parser
);
13422 /* If we're still considering a constructor, we have to see a `(',
13423 to begin the parameter-declaration-clause, followed by either a
13424 `)', an `...', or a decl-specifier. We need to check for a
13425 type-specifier to avoid being fooled into thinking that:
13429 is a constructor. (It is actually a function named `f' that
13430 takes one parameter (of type `int') and returns a value of type
13433 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
13435 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
13436 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
13437 && !cp_parser_storage_class_specifier_opt (parser
))
13441 /* Names appearing in the type-specifier should be looked up
13442 in the scope of the class. */
13443 if (current_class_type
)
13447 type
= TREE_TYPE (type_decl
);
13448 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13450 type
= resolve_typename_type (type
,
13451 /*only_current_p=*/false);
13452 if (type
== error_mark_node
)
13454 cp_parser_abort_tentative_parse (parser
);
13460 /* Look for the type-specifier. */
13461 cp_parser_type_specifier (parser
,
13462 CP_PARSER_FLAGS_NONE
,
13463 /*is_friend=*/false,
13464 /*is_declarator=*/true,
13465 /*declares_class_or_enum=*/NULL
,
13466 /*is_cv_qualifier=*/NULL
);
13467 /* Leave the scope of the class. */
13471 constructor_p
= !cp_parser_error_occurred (parser
);
13475 constructor_p
= false;
13476 /* We did not really want to consume any tokens. */
13477 cp_parser_abort_tentative_parse (parser
);
13479 return constructor_p
;
13482 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13483 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13484 they must be performed once we are in the scope of the function.
13486 Returns the function defined. */
13489 cp_parser_function_definition_from_specifiers_and_declarator
13490 (cp_parser
* parser
,
13491 tree decl_specifiers
,
13498 /* Begin the function-definition. */
13499 success_p
= begin_function_definition (decl_specifiers
,
13503 /* If there were names looked up in the decl-specifier-seq that we
13504 did not check, check them now. We must wait until we are in the
13505 scope of the function to perform the checks, since the function
13506 might be a friend. */
13507 perform_deferred_access_checks ();
13511 /* If begin_function_definition didn't like the definition, skip
13512 the entire function. */
13513 error ("invalid function declaration");
13514 cp_parser_skip_to_end_of_block_or_statement (parser
);
13515 fn
= error_mark_node
;
13518 fn
= cp_parser_function_definition_after_declarator (parser
,
13519 /*inline_p=*/false);
13524 /* Parse the part of a function-definition that follows the
13525 declarator. INLINE_P is TRUE iff this function is an inline
13526 function defined with a class-specifier.
13528 Returns the function defined. */
13531 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
13535 bool ctor_initializer_p
= false;
13536 bool saved_in_unbraced_linkage_specification_p
;
13537 unsigned saved_num_template_parameter_lists
;
13539 /* If the next token is `return', then the code may be trying to
13540 make use of the "named return value" extension that G++ used to
13542 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
13544 /* Consume the `return' keyword. */
13545 cp_lexer_consume_token (parser
->lexer
);
13546 /* Look for the identifier that indicates what value is to be
13548 cp_parser_identifier (parser
);
13549 /* Issue an error message. */
13550 error ("named return values are no longer supported");
13551 /* Skip tokens until we reach the start of the function body. */
13552 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13553 cp_lexer_consume_token (parser
->lexer
);
13555 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13556 anything declared inside `f'. */
13557 saved_in_unbraced_linkage_specification_p
13558 = parser
->in_unbraced_linkage_specification_p
;
13559 parser
->in_unbraced_linkage_specification_p
= false;
13560 /* Inside the function, surrounding template-parameter-lists do not
13562 saved_num_template_parameter_lists
13563 = parser
->num_template_parameter_lists
;
13564 parser
->num_template_parameter_lists
= 0;
13565 /* If the next token is `try', then we are looking at a
13566 function-try-block. */
13567 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
13568 ctor_initializer_p
= cp_parser_function_try_block (parser
);
13569 /* A function-try-block includes the function-body, so we only do
13570 this next part if we're not processing a function-try-block. */
13573 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13575 /* Finish the function. */
13576 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
13577 (inline_p
? 2 : 0));
13578 /* Generate code for it, if necessary. */
13579 expand_or_defer_fn (fn
);
13580 /* Restore the saved values. */
13581 parser
->in_unbraced_linkage_specification_p
13582 = saved_in_unbraced_linkage_specification_p
;
13583 parser
->num_template_parameter_lists
13584 = saved_num_template_parameter_lists
;
13589 /* Parse a template-declaration, assuming that the `export' (and
13590 `extern') keywords, if present, has already been scanned. MEMBER_P
13591 is as for cp_parser_template_declaration. */
13594 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
13596 tree decl
= NULL_TREE
;
13597 tree parameter_list
;
13598 bool friend_p
= false;
13600 /* Look for the `template' keyword. */
13601 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
13605 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
13608 /* Parse the template parameters. */
13609 begin_template_parm_list ();
13610 /* If the next token is `>', then we have an invalid
13611 specialization. Rather than complain about an invalid template
13612 parameter, issue an error message here. */
13613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
13615 cp_parser_error (parser
, "invalid explicit specialization");
13616 parameter_list
= NULL_TREE
;
13619 parameter_list
= cp_parser_template_parameter_list (parser
);
13620 parameter_list
= end_template_parm_list (parameter_list
);
13621 /* Look for the `>'. */
13622 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
13623 /* We just processed one more parameter list. */
13624 ++parser
->num_template_parameter_lists
;
13625 /* If the next token is `template', there are more template
13627 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13629 cp_parser_template_declaration_after_export (parser
, member_p
);
13632 decl
= cp_parser_single_declaration (parser
,
13636 /* If this is a member template declaration, let the front
13638 if (member_p
&& !friend_p
&& decl
)
13639 decl
= finish_member_template_decl (decl
);
13640 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
13641 make_friend_class (current_class_type
, TREE_TYPE (decl
),
13642 /*complain=*/true);
13644 /* We are done with the current parameter list. */
13645 --parser
->num_template_parameter_lists
;
13648 finish_template_decl (parameter_list
);
13650 /* Register member declarations. */
13651 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
13652 finish_member_declaration (decl
);
13654 /* If DECL is a function template, we must return to parse it later.
13655 (Even though there is no definition, there might be default
13656 arguments that need handling.) */
13657 if (member_p
&& decl
13658 && (TREE_CODE (decl
) == FUNCTION_DECL
13659 || DECL_FUNCTION_TEMPLATE_P (decl
)))
13660 TREE_VALUE (parser
->unparsed_functions_queues
)
13661 = tree_cons (NULL_TREE
, decl
,
13662 TREE_VALUE (parser
->unparsed_functions_queues
));
13665 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13666 `function-definition' sequence. MEMBER_P is true, this declaration
13667 appears in a class scope.
13669 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13670 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13673 cp_parser_single_declaration (cp_parser
* parser
,
13677 int declares_class_or_enum
;
13678 tree decl
= NULL_TREE
;
13679 tree decl_specifiers
;
13682 /* Parse the dependent declaration. We don't know yet
13683 whether it will be a function-definition. */
13684 cp_parser_parse_tentatively (parser
);
13685 /* Defer access checks until we know what is being declared. */
13686 push_deferring_access_checks (dk_deferred
);
13688 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13691 = cp_parser_decl_specifier_seq (parser
,
13692 CP_PARSER_FLAGS_OPTIONAL
,
13694 &declares_class_or_enum
);
13695 /* Gather up the access checks that occurred the
13696 decl-specifier-seq. */
13697 stop_deferring_access_checks ();
13699 /* Check for the declaration of a template class. */
13700 if (declares_class_or_enum
)
13702 if (cp_parser_declares_only_class_p (parser
))
13704 decl
= shadow_tag (decl_specifiers
);
13706 decl
= TYPE_NAME (decl
);
13708 decl
= error_mark_node
;
13713 /* If it's not a template class, try for a template function. If
13714 the next token is a `;', then this declaration does not declare
13715 anything. But, if there were errors in the decl-specifiers, then
13716 the error might well have come from an attempted class-specifier.
13717 In that case, there's no need to warn about a missing declarator. */
13719 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
13720 || !value_member (error_mark_node
, decl_specifiers
)))
13721 decl
= cp_parser_init_declarator (parser
,
13724 /*function_definition_allowed_p=*/false,
13726 declares_class_or_enum
,
13727 /*function_definition_p=*/NULL
);
13729 pop_deferring_access_checks ();
13731 /* Clear any current qualification; whatever comes next is the start
13732 of something new. */
13733 parser
->scope
= NULL_TREE
;
13734 parser
->qualifying_scope
= NULL_TREE
;
13735 parser
->object_scope
= NULL_TREE
;
13736 /* Look for a trailing `;' after the declaration. */
13737 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")
13738 && cp_parser_committed_to_tentative_parse (parser
))
13739 cp_parser_skip_to_end_of_block_or_statement (parser
);
13740 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13741 if (cp_parser_parse_definitely (parser
))
13744 *friend_p
= cp_parser_friend_p (decl_specifiers
);
13746 /* Otherwise, try a function-definition. */
13748 decl
= cp_parser_function_definition (parser
, friend_p
);
13753 /* Parse a cast-expression that is not the operand of a unary "&". */
13756 cp_parser_simple_cast_expression (cp_parser
*parser
)
13758 return cp_parser_cast_expression (parser
, /*address_p=*/false);
13761 /* Parse a functional cast to TYPE. Returns an expression
13762 representing the cast. */
13765 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
13767 tree expression_list
;
13770 = cp_parser_parenthesized_expression_list (parser
, false,
13771 /*non_constant_p=*/NULL
);
13773 return build_functional_cast (type
, expression_list
);
13776 /* MEMBER_FUNCTION is a member function, or a friend. If default
13777 arguments, or the body of the function have not yet been parsed,
13781 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
13783 cp_lexer
*saved_lexer
;
13785 /* If this member is a template, get the underlying
13787 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
13788 member_function
= DECL_TEMPLATE_RESULT (member_function
);
13790 /* There should not be any class definitions in progress at this
13791 point; the bodies of members are only parsed outside of all class
13793 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
13794 /* While we're parsing the member functions we might encounter more
13795 classes. We want to handle them right away, but we don't want
13796 them getting mixed up with functions that are currently in the
13798 parser
->unparsed_functions_queues
13799 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
13801 /* Make sure that any template parameters are in scope. */
13802 maybe_begin_member_template_processing (member_function
);
13804 /* If the body of the function has not yet been parsed, parse it
13806 if (DECL_PENDING_INLINE_P (member_function
))
13808 tree function_scope
;
13809 cp_token_cache
*tokens
;
13811 /* The function is no longer pending; we are processing it. */
13812 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
13813 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
13814 DECL_PENDING_INLINE_P (member_function
) = 0;
13815 /* If this was an inline function in a local class, enter the scope
13816 of the containing function. */
13817 function_scope
= decl_function_context (member_function
);
13818 if (function_scope
)
13819 push_function_context_to (function_scope
);
13821 /* Save away the current lexer. */
13822 saved_lexer
= parser
->lexer
;
13823 /* Make a new lexer to feed us the tokens saved for this function. */
13824 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
13825 parser
->lexer
->next
= saved_lexer
;
13827 /* Set the current source position to be the location of the first
13828 token in the saved inline body. */
13829 cp_lexer_peek_token (parser
->lexer
);
13831 /* Let the front end know that we going to be defining this
13833 start_function (NULL_TREE
, member_function
, NULL_TREE
,
13834 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
13836 /* Now, parse the body of the function. */
13837 cp_parser_function_definition_after_declarator (parser
,
13838 /*inline_p=*/true);
13840 /* Leave the scope of the containing function. */
13841 if (function_scope
)
13842 pop_function_context_from (function_scope
);
13843 /* Restore the lexer. */
13844 parser
->lexer
= saved_lexer
;
13847 /* Remove any template parameters from the symbol table. */
13848 maybe_end_member_template_processing ();
13850 /* Restore the queue. */
13851 parser
->unparsed_functions_queues
13852 = TREE_CHAIN (parser
->unparsed_functions_queues
);
13855 /* If DECL contains any default args, remeber it on the unparsed
13856 functions queue. */
13859 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
13863 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
13865 probe
= TREE_CHAIN (probe
))
13866 if (TREE_PURPOSE (probe
))
13868 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13869 = tree_cons (NULL_TREE
, decl
,
13870 TREE_PURPOSE (parser
->unparsed_functions_queues
));
13876 /* FN is a FUNCTION_DECL which may contains a parameter with an
13877 unparsed DEFAULT_ARG. Parse the default args now. */
13880 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
13882 cp_lexer
*saved_lexer
;
13883 cp_token_cache
*tokens
;
13884 bool saved_local_variables_forbidden_p
;
13887 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13889 parameters
= TREE_CHAIN (parameters
))
13891 if (!TREE_PURPOSE (parameters
)
13892 || TREE_CODE (TREE_PURPOSE (parameters
)) != DEFAULT_ARG
)
13895 /* Save away the current lexer. */
13896 saved_lexer
= parser
->lexer
;
13897 /* Create a new one, using the tokens we have saved. */
13898 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parameters
));
13899 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
13901 /* Set the current source position to be the location of the
13902 first token in the default argument. */
13903 cp_lexer_peek_token (parser
->lexer
);
13905 /* Local variable names (and the `this' keyword) may not appear
13906 in a default argument. */
13907 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
13908 parser
->local_variables_forbidden_p
= true;
13909 /* Parse the assignment-expression. */
13910 if (DECL_CONTEXT (fn
))
13911 push_nested_class (DECL_CONTEXT (fn
));
13912 TREE_PURPOSE (parameters
) = cp_parser_assignment_expression (parser
);
13913 if (DECL_CONTEXT (fn
))
13914 pop_nested_class ();
13916 /* Restore saved state. */
13917 parser
->lexer
= saved_lexer
;
13918 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
13922 /* Parse the operand of `sizeof' (or a similar operator). Returns
13923 either a TYPE or an expression, depending on the form of the
13924 input. The KEYWORD indicates which kind of expression we have
13928 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
13930 static const char *format
;
13931 tree expr
= NULL_TREE
;
13932 const char *saved_message
;
13933 bool saved_constant_expression_p
;
13935 /* Initialize FORMAT the first time we get here. */
13937 format
= "types may not be defined in `%s' expressions";
13939 /* Types cannot be defined in a `sizeof' expression. Save away the
13941 saved_message
= parser
->type_definition_forbidden_message
;
13942 /* And create the new one. */
13943 parser
->type_definition_forbidden_message
13944 = xmalloc (strlen (format
)
13945 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
13947 sprintf ((char *) parser
->type_definition_forbidden_message
,
13948 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
13950 /* The restrictions on constant-expressions do not apply inside
13951 sizeof expressions. */
13952 saved_constant_expression_p
= parser
->constant_expression_p
;
13953 parser
->constant_expression_p
= false;
13955 /* Do not actually evaluate the expression. */
13957 /* If it's a `(', then we might be looking at the type-id
13959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
13963 /* We can't be sure yet whether we're looking at a type-id or an
13965 cp_parser_parse_tentatively (parser
);
13966 /* Consume the `('. */
13967 cp_lexer_consume_token (parser
->lexer
);
13968 /* Parse the type-id. */
13969 type
= cp_parser_type_id (parser
);
13970 /* Now, look for the trailing `)'. */
13971 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13972 /* If all went well, then we're done. */
13973 if (cp_parser_parse_definitely (parser
))
13975 /* Build a list of decl-specifiers; right now, we have only
13976 a single type-specifier. */
13977 type
= build_tree_list (NULL_TREE
,
13980 /* Call grokdeclarator to figure out what type this is. */
13981 expr
= grokdeclarator (NULL_TREE
,
13985 /*attrlist=*/NULL
);
13989 /* If the type-id production did not work out, then we must be
13990 looking at the unary-expression production. */
13992 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
13993 /* Go back to evaluating expressions. */
13996 /* Free the message we created. */
13997 free ((char *) parser
->type_definition_forbidden_message
);
13998 /* And restore the old one. */
13999 parser
->type_definition_forbidden_message
= saved_message
;
14000 parser
->constant_expression_p
= saved_constant_expression_p
;
14005 /* If the current declaration has no declarator, return true. */
14008 cp_parser_declares_only_class_p (cp_parser
*parser
)
14010 /* If the next token is a `;' or a `,' then there is no
14012 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14013 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14016 /* Simplify EXPR if it is a non-dependent expression. Returns the
14017 (possibly simplified) expression. */
14020 cp_parser_fold_non_dependent_expr (tree expr
)
14022 /* If we're in a template, but EXPR isn't value dependent, simplify
14023 it. We're supposed to treat:
14025 template <typename T> void f(T[1 + 1]);
14026 template <typename T> void f(T[2]);
14028 as two declarations of the same function, for example. */
14029 if (processing_template_decl
14030 && !type_dependent_expression_p (expr
)
14031 && !value_dependent_expression_p (expr
))
14033 HOST_WIDE_INT saved_processing_template_decl
;
14035 saved_processing_template_decl
= processing_template_decl
;
14036 processing_template_decl
= 0;
14037 expr
= tsubst_copy_and_build (expr
,
14038 /*args=*/NULL_TREE
,
14040 /*in_decl=*/NULL_TREE
,
14041 /*function_p=*/false);
14042 processing_template_decl
= saved_processing_template_decl
;
14047 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14048 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14051 cp_parser_friend_p (tree decl_specifiers
)
14053 while (decl_specifiers
)
14055 /* See if this decl-specifier is `friend'. */
14056 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
14057 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
14060 /* Go on to the next decl-specifier. */
14061 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
14067 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14068 issue an error message indicating that TOKEN_DESC was expected.
14070 Returns the token consumed, if the token had the appropriate type.
14071 Otherwise, returns NULL. */
14074 cp_parser_require (cp_parser
* parser
,
14075 enum cpp_ttype type
,
14076 const char* token_desc
)
14078 if (cp_lexer_next_token_is (parser
->lexer
, type
))
14079 return cp_lexer_consume_token (parser
->lexer
);
14082 /* Output the MESSAGE -- unless we're parsing tentatively. */
14083 if (!cp_parser_simulate_error (parser
))
14084 error ("expected %s", token_desc
);
14089 /* Like cp_parser_require, except that tokens will be skipped until
14090 the desired token is found. An error message is still produced if
14091 the next token is not as expected. */
14094 cp_parser_skip_until_found (cp_parser
* parser
,
14095 enum cpp_ttype type
,
14096 const char* token_desc
)
14099 unsigned nesting_depth
= 0;
14101 if (cp_parser_require (parser
, type
, token_desc
))
14104 /* Skip tokens until the desired token is found. */
14107 /* Peek at the next token. */
14108 token
= cp_lexer_peek_token (parser
->lexer
);
14109 /* If we've reached the token we want, consume it and
14111 if (token
->type
== type
&& !nesting_depth
)
14113 cp_lexer_consume_token (parser
->lexer
);
14116 /* If we've run out of tokens, stop. */
14117 if (token
->type
== CPP_EOF
)
14119 if (token
->type
== CPP_OPEN_BRACE
14120 || token
->type
== CPP_OPEN_PAREN
14121 || token
->type
== CPP_OPEN_SQUARE
)
14123 else if (token
->type
== CPP_CLOSE_BRACE
14124 || token
->type
== CPP_CLOSE_PAREN
14125 || token
->type
== CPP_CLOSE_SQUARE
)
14127 if (nesting_depth
-- == 0)
14130 /* Consume this token. */
14131 cp_lexer_consume_token (parser
->lexer
);
14135 /* If the next token is the indicated keyword, consume it. Otherwise,
14136 issue an error message indicating that TOKEN_DESC was expected.
14138 Returns the token consumed, if the token had the appropriate type.
14139 Otherwise, returns NULL. */
14142 cp_parser_require_keyword (cp_parser
* parser
,
14144 const char* token_desc
)
14146 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
14148 if (token
&& token
->keyword
!= keyword
)
14150 dyn_string_t error_msg
;
14152 /* Format the error message. */
14153 error_msg
= dyn_string_new (0);
14154 dyn_string_append_cstr (error_msg
, "expected ");
14155 dyn_string_append_cstr (error_msg
, token_desc
);
14156 cp_parser_error (parser
, error_msg
->s
);
14157 dyn_string_delete (error_msg
);
14164 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14165 function-definition. */
14168 cp_parser_token_starts_function_definition_p (cp_token
* token
)
14170 return (/* An ordinary function-body begins with an `{'. */
14171 token
->type
== CPP_OPEN_BRACE
14172 /* A ctor-initializer begins with a `:'. */
14173 || token
->type
== CPP_COLON
14174 /* A function-try-block begins with `try'. */
14175 || token
->keyword
== RID_TRY
14176 /* The named return value extension begins with `return'. */
14177 || token
->keyword
== RID_RETURN
);
14180 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14184 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
14188 token
= cp_lexer_peek_token (parser
->lexer
);
14189 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
14192 /* Returns TRUE iff the next token is the "," or ">" ending a
14193 template-argument. */
14196 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
14200 token
= cp_lexer_peek_token (parser
->lexer
);
14201 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
14204 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14205 or none_type otherwise. */
14207 static enum tag_types
14208 cp_parser_token_is_class_key (cp_token
* token
)
14210 switch (token
->keyword
)
14215 return record_type
;
14224 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14227 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
14229 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
14230 pedwarn ("`%s' tag used in naming `%#T'",
14231 class_key
== union_type
? "union"
14232 : class_key
== record_type
? "struct" : "class",
14236 /* Look for the `template' keyword, as a syntactic disambiguator.
14237 Return TRUE iff it is present, in which case it will be
14241 cp_parser_optional_template_keyword (cp_parser
*parser
)
14243 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14245 /* The `template' keyword can only be used within templates;
14246 outside templates the parser can always figure out what is a
14247 template and what is not. */
14248 if (!processing_template_decl
)
14250 error ("`template' (as a disambiguator) is only allowed "
14251 "within templates");
14252 /* If this part of the token stream is rescanned, the same
14253 error message would be generated. So, we purge the token
14254 from the stream. */
14255 cp_lexer_purge_token (parser
->lexer
);
14260 /* Consume the `template' keyword. */
14261 cp_lexer_consume_token (parser
->lexer
);
14269 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14270 set PARSER->SCOPE, and perform other related actions. */
14273 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
14278 /* Get the stored value. */
14279 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
14280 /* Perform any access checks that were deferred. */
14281 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
14282 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
14283 /* Set the scope from the stored value. */
14284 parser
->scope
= TREE_VALUE (value
);
14285 parser
->qualifying_scope
= TREE_TYPE (value
);
14286 parser
->object_scope
= NULL_TREE
;
14289 /* Add tokens to CACHE until an non-nested END token appears. */
14292 cp_parser_cache_group (cp_parser
*parser
,
14293 cp_token_cache
*cache
,
14294 enum cpp_ttype end
,
14301 /* Abort a parenthesized expression if we encounter a brace. */
14302 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
14303 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14305 /* Consume the next token. */
14306 token
= cp_lexer_consume_token (parser
->lexer
);
14307 /* If we've reached the end of the file, stop. */
14308 if (token
->type
== CPP_EOF
)
14310 /* Add this token to the tokens we are saving. */
14311 cp_token_cache_push_token (cache
, token
);
14312 /* See if it starts a new group. */
14313 if (token
->type
== CPP_OPEN_BRACE
)
14315 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
14319 else if (token
->type
== CPP_OPEN_PAREN
)
14320 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
14321 else if (token
->type
== end
)
14326 /* Begin parsing tentatively. We always save tokens while parsing
14327 tentatively so that if the tentative parsing fails we can restore the
14331 cp_parser_parse_tentatively (cp_parser
* parser
)
14333 /* Enter a new parsing context. */
14334 parser
->context
= cp_parser_context_new (parser
->context
);
14335 /* Begin saving tokens. */
14336 cp_lexer_save_tokens (parser
->lexer
);
14337 /* In order to avoid repetitive access control error messages,
14338 access checks are queued up until we are no longer parsing
14340 push_deferring_access_checks (dk_deferred
);
14343 /* Commit to the currently active tentative parse. */
14346 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
14348 cp_parser_context
*context
;
14351 /* Mark all of the levels as committed. */
14352 lexer
= parser
->lexer
;
14353 for (context
= parser
->context
; context
->next
; context
= context
->next
)
14355 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
14357 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
14358 while (!cp_lexer_saving_tokens (lexer
))
14359 lexer
= lexer
->next
;
14360 cp_lexer_commit_tokens (lexer
);
14364 /* Abort the currently active tentative parse. All consumed tokens
14365 will be rolled back, and no diagnostics will be issued. */
14368 cp_parser_abort_tentative_parse (cp_parser
* parser
)
14370 cp_parser_simulate_error (parser
);
14371 /* Now, pretend that we want to see if the construct was
14372 successfully parsed. */
14373 cp_parser_parse_definitely (parser
);
14376 /* Stop parsing tentatively. If a parse error has occurred, restore the
14377 token stream. Otherwise, commit to the tokens we have consumed.
14378 Returns true if no error occurred; false otherwise. */
14381 cp_parser_parse_definitely (cp_parser
* parser
)
14383 bool error_occurred
;
14384 cp_parser_context
*context
;
14386 /* Remember whether or not an error occurred, since we are about to
14387 destroy that information. */
14388 error_occurred
= cp_parser_error_occurred (parser
);
14389 /* Remove the topmost context from the stack. */
14390 context
= parser
->context
;
14391 parser
->context
= context
->next
;
14392 /* If no parse errors occurred, commit to the tentative parse. */
14393 if (!error_occurred
)
14395 /* Commit to the tokens read tentatively, unless that was
14397 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
14398 cp_lexer_commit_tokens (parser
->lexer
);
14400 pop_to_parent_deferring_access_checks ();
14402 /* Otherwise, if errors occurred, roll back our state so that things
14403 are just as they were before we began the tentative parse. */
14406 cp_lexer_rollback_tokens (parser
->lexer
);
14407 pop_deferring_access_checks ();
14409 /* Add the context to the front of the free list. */
14410 context
->next
= cp_parser_context_free_list
;
14411 cp_parser_context_free_list
= context
;
14413 return !error_occurred
;
14416 /* Returns true if we are parsing tentatively -- but have decided that
14417 we will stick with this tentative parse, even if errors occur. */
14420 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
14422 return (cp_parser_parsing_tentatively (parser
)
14423 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
14426 /* Returns nonzero iff an error has occurred during the most recent
14427 tentative parse. */
14430 cp_parser_error_occurred (cp_parser
* parser
)
14432 return (cp_parser_parsing_tentatively (parser
)
14433 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
14436 /* Returns nonzero if GNU extensions are allowed. */
14439 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
14441 return parser
->allow_gnu_extensions_p
;
14448 static GTY (()) cp_parser
*the_parser
;
14450 /* External interface. */
14452 /* Parse one entire translation unit. */
14455 c_parse_file (void)
14457 bool error_occurred
;
14459 the_parser
= cp_parser_new ();
14460 push_deferring_access_checks (flag_access_control
14461 ? dk_no_deferred
: dk_no_check
);
14462 error_occurred
= cp_parser_translation_unit (the_parser
);
14466 /* Clean up after parsing the entire translation unit. */
14469 free_parser_stacks (void)
14471 /* Nothing to do. */
14474 /* This variable must be provided by every front end. */
14478 #include "gt-cp-parser.h"