2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token
GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype
) type
: 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid
) keyword
: 8;
77 /* The value associated with this token, if any. */
79 /* The location at which this token was found. */
83 /* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to constraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
99 typedef struct cp_token_block
GTY (())
102 cp_token tokens
[CP_TOKEN_BLOCK_NUM_TOKENS
];
103 /* The number of tokens in this block. */
105 /* The next token block in the chain. */
106 struct cp_token_block
*next
;
107 /* The previous block in the chain. */
108 struct cp_token_block
*prev
;
111 typedef struct cp_token_cache
GTY (())
113 /* The first block in the cache. NULL if there are no tokens in the
115 cp_token_block
*first
;
116 /* The last block in the cache. NULL If there are no tokens in the
118 cp_token_block
*last
;
123 static cp_token_cache
*cp_token_cache_new
125 static void cp_token_cache_push_token
126 (cp_token_cache
*, cp_token
*);
128 /* Create a new cp_token_cache. */
130 static cp_token_cache
*
131 cp_token_cache_new (void)
133 return ggc_alloc_cleared (sizeof (cp_token_cache
));
136 /* Add *TOKEN to *CACHE. */
139 cp_token_cache_push_token (cp_token_cache
*cache
,
142 cp_token_block
*b
= cache
->last
;
144 /* See if we need to allocate a new token block. */
145 if (!b
|| b
->num_tokens
== CP_TOKEN_BLOCK_NUM_TOKENS
)
147 b
= ggc_alloc_cleared (sizeof (cp_token_block
));
148 b
->prev
= cache
->last
;
151 cache
->last
->next
= b
;
155 cache
->first
= cache
->last
= b
;
157 /* Add this token to the current token block. */
158 b
->tokens
[b
->num_tokens
++] = *token
;
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
165 typedef struct cp_lexer
GTY (())
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token
* GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer
;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token
* GTY ((skip
)) buffer_end
;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token
* GTY ((skip
)) first_token
;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token
* GTY ((skip
)) next_token
;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token
* GTY ((skip
)) last_token
;
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens
;
199 /* The STRING_CST tokens encountered while processing the current
201 varray_type string_tokens
;
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
207 /* True if we should output debugging information. */
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer
*next
;
216 static cp_lexer
*cp_lexer_new_main
218 static cp_lexer
*cp_lexer_new_from_tokens
219 (struct cp_token_cache
*);
220 static int cp_lexer_saving_tokens
222 static cp_token
*cp_lexer_next_token
223 (cp_lexer
*, cp_token
*);
224 static cp_token
*cp_lexer_prev_token
225 (cp_lexer
*, cp_token
*);
226 static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer
*, cp_token
*, cp_token
*);
228 static cp_token
*cp_lexer_read_token
230 static void cp_lexer_maybe_grow_buffer
232 static void cp_lexer_get_preprocessor_token
233 (cp_lexer
*, cp_token
*);
234 static cp_token
*cp_lexer_peek_token
236 static cp_token
*cp_lexer_peek_nth_token
237 (cp_lexer
*, size_t);
238 static inline bool cp_lexer_next_token_is
239 (cp_lexer
*, enum cpp_ttype
);
240 static bool cp_lexer_next_token_is_not
241 (cp_lexer
*, enum cpp_ttype
);
242 static bool cp_lexer_next_token_is_keyword
243 (cp_lexer
*, enum rid
);
244 static cp_token
*cp_lexer_consume_token
246 static void cp_lexer_purge_token
248 static void cp_lexer_purge_tokens_after
249 (cp_lexer
*, cp_token
*);
250 static void cp_lexer_save_tokens
252 static void cp_lexer_commit_tokens
254 static void cp_lexer_rollback_tokens
256 static inline void cp_lexer_set_source_position_from_token
257 (cp_lexer
*, const cp_token
*);
258 static void cp_lexer_print_token
259 (FILE *, cp_token
*);
260 static inline bool cp_lexer_debugging_p
262 static void cp_lexer_start_debugging
263 (cp_lexer
*) ATTRIBUTE_UNUSED
;
264 static void cp_lexer_stop_debugging
265 (cp_lexer
*) ATTRIBUTE_UNUSED
;
267 /* Manifest constants. */
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
272 /* A token type for keywords, as opposed to ordinary identifiers. */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275 /* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281 /* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288 /* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
294 /* The stream to which debugging output should be written. */
295 static FILE *cp_lexer_debug_stream
;
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
301 cp_lexer_new_main (void)
304 cp_token first_token
;
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
310 c_common_no_more_pch ();
312 /* Allocate the memory. */
313 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
315 /* Create the circular buffer. */
316 lexer
->buffer
= ggc_calloc (CP_TOKEN_BUFFER_SIZE
, sizeof (cp_token
));
317 lexer
->buffer_end
= lexer
->buffer
+ CP_TOKEN_BUFFER_SIZE
;
319 /* There is one token in the buffer. */
320 lexer
->last_token
= lexer
->buffer
+ 1;
321 lexer
->first_token
= lexer
->buffer
;
322 lexer
->next_token
= lexer
->buffer
;
323 memcpy (lexer
->buffer
, &first_token
, sizeof (cp_token
));
325 /* This lexer obtains more tokens by calling c_lex. */
326 lexer
->main_lexer_p
= true;
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
334 /* Assume we are not debugging. */
335 lexer
->debugging_p
= false;
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
344 cp_lexer_new_from_tokens (cp_token_cache
*tokens
)
348 cp_token_block
*block
;
349 ptrdiff_t num_tokens
;
351 /* Allocate the memory. */
352 lexer
= ggc_alloc_cleared (sizeof (cp_lexer
));
354 /* Create a new buffer, appropriately sized. */
356 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
357 num_tokens
+= block
->num_tokens
;
358 lexer
->buffer
= ggc_alloc (num_tokens
* sizeof (cp_token
));
359 lexer
->buffer_end
= lexer
->buffer
+ num_tokens
;
361 /* Install the tokens. */
362 token
= lexer
->buffer
;
363 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
365 memcpy (token
, block
->tokens
, block
->num_tokens
* sizeof (cp_token
));
366 token
+= block
->num_tokens
;
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer
->first_token
= lexer
->buffer
;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer
->next_token
= lexer
->buffer
;
373 /* The buffer is full. */
374 lexer
->last_token
= lexer
->first_token
;
376 /* This lexer doesn't obtain more tokens. */
377 lexer
->main_lexer_p
= false;
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
385 /* Assume we are not debugging. */
386 lexer
->debugging_p
= false;
391 /* Returns nonzero if debugging information should be output. */
394 cp_lexer_debugging_p (cp_lexer
*lexer
)
396 return lexer
->debugging_p
;
399 /* Set the current source position from the information stored in
403 cp_lexer_set_source_position_from_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
404 const cp_token
*token
)
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
409 /* Update the line number. */
410 if (token
->type
!= CPP_EOF
)
411 input_location
= token
->location
;
414 /* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
417 static inline cp_token
*
418 cp_lexer_next_token (cp_lexer
* lexer
, cp_token
* token
)
421 if (token
== lexer
->buffer_end
)
422 token
= lexer
->buffer
;
426 /* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
429 static inline cp_token
*
430 cp_lexer_prev_token (cp_lexer
* lexer
, cp_token
* token
)
432 if (token
== lexer
->buffer
)
433 token
= lexer
->buffer_end
;
437 /* nonzero if we are presently saving tokens. */
440 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
442 return VARRAY_ACTIVE_SIZE (lexer
->saved_tokens
) != 0;
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
449 cp_lexer_advance_token (cp_lexer
*lexer
, cp_token
*token
, ptrdiff_t n
)
452 if (token
>= lexer
->buffer_end
)
453 token
= lexer
->buffer
+ (token
- lexer
->buffer_end
);
457 /* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
461 cp_lexer_token_difference (cp_lexer
* lexer
, cp_token
* start
, cp_token
* finish
)
464 return finish
- start
;
466 return ((lexer
->buffer_end
- lexer
->buffer
)
470 /* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
474 cp_lexer_read_token (cp_lexer
* lexer
)
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer
);
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer
->first_token
)
483 lexer
->first_token
= lexer
->last_token
;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer
->next_token
)
486 lexer
->next_token
= lexer
->last_token
;
488 /* Figure out where we're going to store the new token. */
489 token
= lexer
->last_token
;
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer
, token
);
494 /* Increment LAST_TOKEN. */
495 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
)
502 && flag_const_strings
)
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type
= TREE_TYPE (token
->value
);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type
= build_cplus_array_type (TREE_TYPE (type
), TYPE_DOMAIN (type
));
511 /* Reset the type of the token. */
512 TREE_TYPE (token
->value
) = type
;
518 /* If the circular buffer is full, make it bigger. */
521 cp_lexer_maybe_grow_buffer (cp_lexer
* lexer
)
523 /* If the buffer is full, enlarge it. */
524 if (lexer
->last_token
== lexer
->first_token
)
526 cp_token
*new_buffer
;
527 cp_token
*old_buffer
;
528 cp_token
*new_first_token
;
529 ptrdiff_t buffer_length
;
530 size_t num_tokens_to_copy
;
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
535 old_buffer
= lexer
->buffer
;
536 /* Compute the current buffer size. */
537 buffer_length
= lexer
->buffer_end
- lexer
->buffer
;
538 /* Allocate a buffer twice as big. */
539 new_buffer
= ggc_realloc (lexer
->buffer
,
540 2 * buffer_length
* sizeof (cp_token
));
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
547 num_tokens_to_copy
= (lexer
->first_token
- old_buffer
);
548 memcpy (new_buffer
+ buffer_length
,
550 num_tokens_to_copy
* sizeof (cp_token
));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer
+ buffer_length
+ num_tokens_to_copy
, 0,
554 (buffer_length
- num_tokens_to_copy
) * sizeof (cp_token
));
556 /* Now recompute all of the buffer pointers. */
558 = new_buffer
+ (lexer
->first_token
- old_buffer
);
559 if (lexer
->next_token
!= NULL
)
561 ptrdiff_t next_token_delta
;
563 if (lexer
->next_token
> lexer
->first_token
)
564 next_token_delta
= lexer
->next_token
- lexer
->first_token
;
567 buffer_length
- (lexer
->first_token
- lexer
->next_token
);
568 lexer
->next_token
= new_first_token
+ next_token_delta
;
570 lexer
->last_token
= new_first_token
+ buffer_length
;
571 lexer
->buffer
= new_buffer
;
572 lexer
->buffer_end
= new_buffer
+ buffer_length
* 2;
573 lexer
->first_token
= new_first_token
;
577 /* Store the next token from the preprocessor in *TOKEN. */
580 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (lexer
!= NULL
&& !lexer
->main_lexer_p
)
588 token
->type
= CPP_EOF
;
589 token
->location
.line
= 0;
590 token
->location
.file
= NULL
;
591 token
->value
= NULL_TREE
;
592 token
->keyword
= RID_MAX
;
598 /* Keep going until we get a token we like. */
601 /* Get a new token from the preprocessor. */
602 token
->type
= c_lex_with_flags (&token
->value
, &token
->flags
);
603 /* Issue messages about tokens we cannot process. */
609 error ("invalid token");
613 /* This is a good token, so we exit the loop. */
618 /* Now we've got our token. */
619 token
->location
= input_location
;
621 /* Check to see if this token is a keyword. */
622 if (token
->type
== CPP_NAME
623 && C_IS_RESERVED_WORD (token
->value
))
625 /* Mark this token as a keyword. */
626 token
->type
= CPP_KEYWORD
;
627 /* Record which keyword. */
628 token
->keyword
= C_RID_CODE (token
->value
);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token
->value
= ridpointers
[token
->keyword
];
636 token
->keyword
= RID_MAX
;
639 /* Return a pointer to the next token in the token stream, but do not
643 cp_lexer_peek_token (cp_lexer
* lexer
)
647 /* If there are no tokens, read one now. */
648 if (!lexer
->next_token
)
649 cp_lexer_read_token (lexer
);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer
))
654 fprintf (cp_lexer_debug_stream
, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
656 fprintf (cp_lexer_debug_stream
, "\n");
659 token
= lexer
->next_token
;
660 cp_lexer_set_source_position_from_token (lexer
, token
);
664 /* Return true if the next token has the indicated TYPE. */
667 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
671 /* Peek at the next token. */
672 token
= cp_lexer_peek_token (lexer
);
673 /* Check to see if it has the indicated TYPE. */
674 return token
->type
== type
;
677 /* Return true if the next token does not have the indicated TYPE. */
680 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
682 return !cp_lexer_next_token_is (lexer
, type
);
685 /* Return true if the next token is the indicated KEYWORD. */
688 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
692 /* Peek at the next token. */
693 token
= cp_lexer_peek_token (lexer
);
694 /* Check to see if it is the indicated keyword. */
695 return token
->keyword
== keyword
;
698 /* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
702 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n
> 0, 20000224);
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token
= lexer
->next_token
;
711 /* If there are no tokens in the buffer, get one now. */
714 cp_lexer_read_token (lexer
);
715 token
= lexer
->next_token
;
718 /* Now, read tokens until we have enough. */
721 /* Advance to the next token. */
722 token
= cp_lexer_next_token (lexer
, token
);
723 /* If that's all the tokens we have, read a new one. */
724 if (token
== lexer
->last_token
)
725 token
= cp_lexer_read_token (lexer
);
731 /* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
737 cp_lexer_consume_token (cp_lexer
* lexer
)
741 /* If there are no tokens, read one now. */
742 if (!lexer
->next_token
)
743 cp_lexer_read_token (lexer
);
745 /* Remember the token we'll be returning. */
746 token
= lexer
->next_token
;
748 /* Increment NEXT_TOKEN. */
749 lexer
->next_token
= cp_lexer_next_token (lexer
,
751 /* Check to see if we're all out of tokens. */
752 if (lexer
->next_token
== lexer
->last_token
)
753 lexer
->next_token
= NULL
;
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer
))
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer
->next_token
)
760 lexer
->first_token
= NULL
;
762 lexer
->first_token
= lexer
->next_token
;
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer
))
768 fprintf (cp_lexer_debug_stream
, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
770 fprintf (cp_lexer_debug_stream
, "\n");
776 /* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
781 cp_lexer_purge_token (cp_lexer
*lexer
)
784 cp_token
*next_token
;
786 token
= lexer
->next_token
;
789 next_token
= cp_lexer_next_token (lexer
, token
);
790 if (next_token
== lexer
->last_token
)
792 *token
= *next_token
;
796 lexer
->last_token
= token
;
797 /* The token purged may have been the only token remaining; if so,
799 if (lexer
->next_token
== token
)
800 lexer
->next_token
= NULL
;
803 /* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
808 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*token
)
814 if (lexer
->next_token
)
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1
= cp_lexer_next_token (lexer
, token
);
819 t2
= peek
= cp_lexer_peek_token (lexer
);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2
!= lexer
->last_token
)
824 t1
= cp_lexer_next_token (lexer
, t1
);
825 t2
= cp_lexer_next_token (lexer
, t2
);
827 /* Now, the next available token is right after TOKEN. */
828 lexer
->next_token
= cp_lexer_next_token (lexer
, token
);
829 /* And the last token is wherever we ended up. */
830 lexer
->last_token
= t1
;
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
840 /* Begin saving tokens. All tokens consumed after this point will be
844 cp_lexer_save_tokens (cp_lexer
* lexer
)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer
))
848 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer
->next_token
)
853 cp_lexer_read_token (lexer
);
855 VARRAY_PUSH_INT (lexer
->saved_tokens
,
856 cp_lexer_token_difference (lexer
,
861 /* Commit to the portion of the token stream most recently saved. */
864 cp_lexer_commit_tokens (cp_lexer
* lexer
)
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer
))
868 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
870 VARRAY_POP (lexer
->saved_tokens
);
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
877 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer
))
883 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
885 /* Find the token that was the NEXT_TOKEN when we started saving
887 delta
= VARRAY_TOP_INT(lexer
->saved_tokens
);
888 /* Make it the next token again now. */
889 lexer
->next_token
= cp_lexer_advance_token (lexer
,
892 /* It might be the case that there were no tokens when we started
893 saving tokens, but that there are some tokens now. */
894 if (!lexer
->next_token
&& lexer
->first_token
)
895 lexer
->next_token
= lexer
->first_token
;
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer
->saved_tokens
);
901 /* Print a representation of the TOKEN on the STREAM. */
904 cp_lexer_print_token (FILE * stream
, cp_token
* token
)
906 const char *token_type
= NULL
;
908 /* Figure out what kind of token this is. */
916 token_type
= "COMMA";
920 token_type
= "OPEN_PAREN";
923 case CPP_CLOSE_PAREN
:
924 token_type
= "CLOSE_PAREN";
928 token_type
= "OPEN_BRACE";
931 case CPP_CLOSE_BRACE
:
932 token_type
= "CLOSE_BRACE";
936 token_type
= "SEMICOLON";
948 token_type
= "keyword";
951 /* This is not a token that we know how to handle yet. */
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
959 fprintf (stream
, "%s", token_type
);
961 fprintf (stream
, "%d", token
->type
);
962 /* And, for an identifier, print the identifier name. */
963 if (token
->type
== CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token
->type
== CPP_KEYWORD
967 && TREE_CODE (token
->value
) == IDENTIFIER_NODE
))
968 fprintf (stream
, " %s", IDENTIFIER_POINTER (token
->value
));
971 /* Start emitting debugging information. */
974 cp_lexer_start_debugging (cp_lexer
* lexer
)
976 ++lexer
->debugging_p
;
979 /* Stop emitting debugging information. */
982 cp_lexer_stop_debugging (cp_lexer
* lexer
)
984 --lexer
->debugging_p
;
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1050 /* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1053 typedef enum cp_parser_flags
1056 CP_PARSER_FLAGS_NONE
= 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1064 /* The different kinds of declarators we want to parse. */
1066 typedef enum cp_parser_declarator_kind
1068 /* We want an abstract declarator. */
1069 CP_PARSER_DECLARATOR_ABSTRACT
,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED
,
1072 /* We don't mind, but the name must be an unqualified-id. */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind
;
1076 /* A mapping from a token type to a corresponding tree node type. */
1078 typedef struct cp_parser_token_tree_map_node
1080 /* The token type. */
1081 ENUM_BITFIELD (cpp_ttype
) token_type
: 8;
1082 /* The corresponding tree code. */
1083 ENUM_BITFIELD (tree_code
) tree_type
: 8;
1084 } cp_parser_token_tree_map_node
;
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map
[];
1091 /* The status of a tentative parse. */
1093 typedef enum cp_parser_status_kind
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR
,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR
,
1099 /* We are committed to this tentative parse, whether or not an error
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind
;
1104 /* Context that is saved and restored when parsing tentatively. */
1106 typedef struct cp_parser_context
GTY (())
1108 /* If this is a tentative parsing context, the status of the
1110 enum cp_parser_status_kind status
;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context
*next
;
1118 } cp_parser_context
;
1122 /* Constructors and destructors. */
1124 static cp_parser_context
*cp_parser_context_new
1125 (cp_parser_context
*);
1127 /* Class variables. */
1129 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1131 /* Constructors and destructors. */
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1136 static cp_parser_context
*
1137 cp_parser_context_new (cp_parser_context
* next
)
1139 cp_parser_context
*context
;
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list
!= NULL
)
1144 /* Pull the first entry from the free list. */
1145 context
= cp_parser_context_free_list
;
1146 cp_parser_context_free_list
= context
->next
;
1147 memset (context
, 0, sizeof (*context
));
1150 context
= ggc_alloc_cleared (sizeof (cp_parser_context
));
1151 /* No errors have occurred yet in this context. */
1152 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context
->object_type
= next
->object_type
;
1160 /* Thread the stack. */
1161 context
->next
= next
;
1167 /* The cp_parser structure represents the C++ parser. */
1169 typedef struct cp_parser
GTY(())
1171 /* The lexer from which we are obtaining tokens. */
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1194 tree qualifying_scope
;
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context
*context
;
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p
;
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p
;
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
1218 them permissible. */
1219 bool default_arg_ok_p
;
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
1223 bool integral_constant_expression_p
;
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
1229 bool allow_non_integral_constant_expression_p
;
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
1233 bool non_integral_constant_expression_p
;
1235 /* TRUE if local variable names and `this' are forbidden in the
1237 bool local_variables_forbidden_p
;
1239 /* TRUE if the declaration we are parsing is part of a
1240 linkage-specification of the form `extern string-literal
1242 bool in_unbraced_linkage_specification_p
;
1244 /* TRUE if we are presently parsing a declarator, after the
1245 direct-declarator. */
1246 bool in_declarator_p
;
1248 /* TRUE if we are presently parsing a template-argument-list. */
1249 bool in_template_argument_list_p
;
1251 /* TRUE if we are presently parsing the body of an
1252 iteration-statement. */
1253 bool in_iteration_statement_p
;
1255 /* TRUE if we are presently parsing the body of a switch
1257 bool in_switch_statement_p
;
1259 /* TRUE if we are parsing a type-id in an expression context. In
1260 such a situation, both "type (expr)" and "type (type)" are valid
1262 bool in_type_id_in_expr_p
;
1264 /* If non-NULL, then we are parsing a construct where new type
1265 definitions are not permitted. The string stored here will be
1266 issued as an error message if a type is defined. */
1267 const char *type_definition_forbidden_message
;
1269 /* A list of lists. The outer list is a stack, used for member
1270 functions of local classes. At each level there are two sub-list,
1271 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1272 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1273 TREE_VALUE's. The functions are chained in reverse declaration
1276 The TREE_PURPOSE sublist contains those functions with default
1277 arguments that need post processing, and the TREE_VALUE sublist
1278 contains those functions with definitions that need post
1281 These lists can only be processed once the outermost class being
1282 defined is complete. */
1283 tree unparsed_functions_queues
;
1285 /* The number of classes whose definitions are currently in
1287 unsigned num_classes_being_defined
;
1289 /* The number of template parameter lists that apply directly to the
1290 current declaration. */
1291 unsigned num_template_parameter_lists
;
1294 /* The type of a function that parses some kind of expression. */
1295 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1299 /* Constructors and destructors. */
1301 static cp_parser
*cp_parser_new
1304 /* Routines to parse various constructs.
1306 Those that return `tree' will return the error_mark_node (rather
1307 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1308 Sometimes, they will return an ordinary node if error-recovery was
1309 attempted, even though a parse error occurred. So, to check
1310 whether or not a parse error occurred, you should always use
1311 cp_parser_error_occurred. If the construct is optional (indicated
1312 either by an `_opt' in the name of the function that does the
1313 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1314 the construct is not present. */
1316 /* Lexical conventions [gram.lex] */
1318 static tree cp_parser_identifier
1321 /* Basic concepts [gram.basic] */
1323 static bool cp_parser_translation_unit
1326 /* Expressions [gram.expr] */
1328 static tree cp_parser_primary_expression
1329 (cp_parser
*, cp_id_kind
*, tree
*);
1330 static tree cp_parser_id_expression
1331 (cp_parser
*, bool, bool, bool *, bool);
1332 static tree cp_parser_unqualified_id
1333 (cp_parser
*, bool, bool, bool);
1334 static tree cp_parser_nested_name_specifier_opt
1335 (cp_parser
*, bool, bool, bool, bool);
1336 static tree cp_parser_nested_name_specifier
1337 (cp_parser
*, bool, bool, bool, bool);
1338 static tree cp_parser_class_or_namespace_name
1339 (cp_parser
*, bool, bool, bool, bool, bool);
1340 static tree cp_parser_postfix_expression
1341 (cp_parser
*, bool);
1342 static tree cp_parser_postfix_open_square_expression
1343 (cp_parser
*, tree
, bool);
1344 static tree cp_parser_postfix_dot_deref_expression
1345 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1346 static tree cp_parser_parenthesized_expression_list
1347 (cp_parser
*, bool, bool *);
1348 static void cp_parser_pseudo_destructor_name
1349 (cp_parser
*, tree
*, tree
*);
1350 static tree cp_parser_unary_expression
1351 (cp_parser
*, bool);
1352 static enum tree_code cp_parser_unary_operator
1354 static tree cp_parser_new_expression
1356 static tree cp_parser_new_placement
1358 static tree cp_parser_new_type_id
1360 static tree cp_parser_new_declarator_opt
1362 static tree cp_parser_direct_new_declarator
1364 static tree cp_parser_new_initializer
1366 static tree cp_parser_delete_expression
1368 static tree cp_parser_cast_expression
1369 (cp_parser
*, bool);
1370 static tree cp_parser_pm_expression
1372 static tree cp_parser_multiplicative_expression
1374 static tree cp_parser_additive_expression
1376 static tree cp_parser_shift_expression
1378 static tree cp_parser_relational_expression
1380 static tree cp_parser_equality_expression
1382 static tree cp_parser_and_expression
1384 static tree cp_parser_exclusive_or_expression
1386 static tree cp_parser_inclusive_or_expression
1388 static tree cp_parser_logical_and_expression
1390 static tree cp_parser_logical_or_expression
1392 static tree cp_parser_question_colon_clause
1393 (cp_parser
*, tree
);
1394 static tree cp_parser_assignment_expression
1396 static enum tree_code cp_parser_assignment_operator_opt
1398 static tree cp_parser_expression
1400 static tree cp_parser_constant_expression
1401 (cp_parser
*, bool, bool *);
1402 static tree cp_parser_builtin_offsetof
1405 /* Statements [gram.stmt.stmt] */
1407 static void cp_parser_statement
1408 (cp_parser
*, bool);
1409 static tree cp_parser_labeled_statement
1410 (cp_parser
*, bool);
1411 static tree cp_parser_expression_statement
1412 (cp_parser
*, bool);
1413 static tree cp_parser_compound_statement
1414 (cp_parser
*, bool);
1415 static void cp_parser_statement_seq_opt
1416 (cp_parser
*, bool);
1417 static tree cp_parser_selection_statement
1419 static tree cp_parser_condition
1421 static tree cp_parser_iteration_statement
1423 static void cp_parser_for_init_statement
1425 static tree cp_parser_jump_statement
1427 static void cp_parser_declaration_statement
1430 static tree cp_parser_implicitly_scoped_statement
1432 static void cp_parser_already_scoped_statement
1435 /* Declarations [gram.dcl.dcl] */
1437 static void cp_parser_declaration_seq_opt
1439 static void cp_parser_declaration
1441 static void cp_parser_block_declaration
1442 (cp_parser
*, bool);
1443 static void cp_parser_simple_declaration
1444 (cp_parser
*, bool);
1445 static tree cp_parser_decl_specifier_seq
1446 (cp_parser
*, cp_parser_flags
, tree
*, int *);
1447 static tree cp_parser_storage_class_specifier_opt
1449 static tree cp_parser_function_specifier_opt
1451 static tree cp_parser_type_specifier
1452 (cp_parser
*, cp_parser_flags
, bool, bool, int *, bool *);
1453 static tree cp_parser_simple_type_specifier
1454 (cp_parser
*, cp_parser_flags
, bool);
1455 static tree cp_parser_type_name
1457 static tree cp_parser_elaborated_type_specifier
1458 (cp_parser
*, bool, bool);
1459 static tree cp_parser_enum_specifier
1461 static void cp_parser_enumerator_list
1462 (cp_parser
*, tree
);
1463 static void cp_parser_enumerator_definition
1464 (cp_parser
*, tree
);
1465 static tree cp_parser_namespace_name
1467 static void cp_parser_namespace_definition
1469 static void cp_parser_namespace_body
1471 static tree cp_parser_qualified_namespace_specifier
1473 static void cp_parser_namespace_alias_definition
1475 static void cp_parser_using_declaration
1477 static void cp_parser_using_directive
1479 static void cp_parser_asm_definition
1481 static void cp_parser_linkage_specification
1484 /* Declarators [gram.dcl.decl] */
1486 static tree cp_parser_init_declarator
1487 (cp_parser
*, tree
, tree
, bool, bool, int, bool *);
1488 static tree cp_parser_declarator
1489 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *);
1490 static tree cp_parser_direct_declarator
1491 (cp_parser
*, cp_parser_declarator_kind
, int *);
1492 static enum tree_code cp_parser_ptr_operator
1493 (cp_parser
*, tree
*, tree
*);
1494 static tree cp_parser_cv_qualifier_seq_opt
1496 static tree cp_parser_cv_qualifier_opt
1498 static tree cp_parser_declarator_id
1500 static tree cp_parser_type_id
1502 static tree cp_parser_type_specifier_seq
1504 static tree cp_parser_parameter_declaration_clause
1506 static tree cp_parser_parameter_declaration_list
1508 static tree cp_parser_parameter_declaration
1509 (cp_parser
*, bool, bool *);
1510 static void cp_parser_function_body
1512 static tree cp_parser_initializer
1513 (cp_parser
*, bool *, bool *);
1514 static tree cp_parser_initializer_clause
1515 (cp_parser
*, bool *);
1516 static tree cp_parser_initializer_list
1517 (cp_parser
*, bool *);
1519 static bool cp_parser_ctor_initializer_opt_and_function_body
1522 /* Classes [gram.class] */
1524 static tree cp_parser_class_name
1525 (cp_parser
*, bool, bool, bool, bool, bool, bool);
1526 static tree cp_parser_class_specifier
1528 static tree cp_parser_class_head
1529 (cp_parser
*, bool *, tree
*);
1530 static enum tag_types cp_parser_class_key
1532 static void cp_parser_member_specification_opt
1534 static void cp_parser_member_declaration
1536 static tree cp_parser_pure_specifier
1538 static tree cp_parser_constant_initializer
1541 /* Derived classes [gram.class.derived] */
1543 static tree cp_parser_base_clause
1545 static tree cp_parser_base_specifier
1548 /* Special member functions [gram.special] */
1550 static tree cp_parser_conversion_function_id
1552 static tree cp_parser_conversion_type_id
1554 static tree cp_parser_conversion_declarator_opt
1556 static bool cp_parser_ctor_initializer_opt
1558 static void cp_parser_mem_initializer_list
1560 static tree cp_parser_mem_initializer
1562 static tree cp_parser_mem_initializer_id
1565 /* Overloading [gram.over] */
1567 static tree cp_parser_operator_function_id
1569 static tree cp_parser_operator
1572 /* Templates [gram.temp] */
1574 static void cp_parser_template_declaration
1575 (cp_parser
*, bool);
1576 static tree cp_parser_template_parameter_list
1578 static tree cp_parser_template_parameter
1580 static tree cp_parser_type_parameter
1582 static tree cp_parser_template_id
1583 (cp_parser
*, bool, bool, bool);
1584 static tree cp_parser_template_name
1585 (cp_parser
*, bool, bool, bool, bool *);
1586 static tree cp_parser_template_argument_list
1588 static tree cp_parser_template_argument
1590 static void cp_parser_explicit_instantiation
1592 static void cp_parser_explicit_specialization
1595 /* Exception handling [gram.exception] */
1597 static tree cp_parser_try_block
1599 static bool cp_parser_function_try_block
1601 static void cp_parser_handler_seq
1603 static void cp_parser_handler
1605 static tree cp_parser_exception_declaration
1607 static tree cp_parser_throw_expression
1609 static tree cp_parser_exception_specification_opt
1611 static tree cp_parser_type_id_list
1614 /* GNU Extensions */
1616 static tree cp_parser_asm_specification_opt
1618 static tree cp_parser_asm_operand_list
1620 static tree cp_parser_asm_clobber_list
1622 static tree cp_parser_attributes_opt
1624 static tree cp_parser_attribute_list
1626 static bool cp_parser_extension_opt
1627 (cp_parser
*, int *);
1628 static void cp_parser_label_declaration
1631 /* Utility Routines */
1633 static tree cp_parser_lookup_name
1634 (cp_parser
*, tree
, bool, bool, bool, bool);
1635 static tree cp_parser_lookup_name_simple
1636 (cp_parser
*, tree
);
1637 static tree cp_parser_maybe_treat_template_as_class
1639 static bool cp_parser_check_declarator_template_parameters
1640 (cp_parser
*, tree
);
1641 static bool cp_parser_check_template_parameters
1642 (cp_parser
*, unsigned);
1643 static tree cp_parser_simple_cast_expression
1645 static tree cp_parser_binary_expression
1646 (cp_parser
*, const cp_parser_token_tree_map
, cp_parser_expression_fn
);
1647 static tree cp_parser_global_scope_opt
1648 (cp_parser
*, bool);
1649 static bool cp_parser_constructor_declarator_p
1650 (cp_parser
*, bool);
1651 static tree cp_parser_function_definition_from_specifiers_and_declarator
1652 (cp_parser
*, tree
, tree
, tree
);
1653 static tree cp_parser_function_definition_after_declarator
1654 (cp_parser
*, bool);
1655 static void cp_parser_template_declaration_after_export
1656 (cp_parser
*, bool);
1657 static tree cp_parser_single_declaration
1658 (cp_parser
*, bool, bool *);
1659 static tree cp_parser_functional_cast
1660 (cp_parser
*, tree
);
1661 static tree cp_parser_save_member_function_body
1662 (cp_parser
*, tree
, tree
, tree
);
1663 static tree cp_parser_enclosed_template_argument_list
1665 static void cp_parser_save_default_args
1666 (cp_parser
*, tree
);
1667 static void cp_parser_late_parsing_for_member
1668 (cp_parser
*, tree
);
1669 static void cp_parser_late_parsing_default_args
1670 (cp_parser
*, tree
);
1671 static tree cp_parser_sizeof_operand
1672 (cp_parser
*, enum rid
);
1673 static bool cp_parser_declares_only_class_p
1675 static bool cp_parser_friend_p
1677 static cp_token
*cp_parser_require
1678 (cp_parser
*, enum cpp_ttype
, const char *);
1679 static cp_token
*cp_parser_require_keyword
1680 (cp_parser
*, enum rid
, const char *);
1681 static bool cp_parser_token_starts_function_definition_p
1683 static bool cp_parser_next_token_starts_class_definition_p
1685 static bool cp_parser_next_token_ends_template_argument_p
1687 static bool cp_parser_nth_token_starts_template_argument_list_p
1688 (cp_parser
*, size_t);
1689 static enum tag_types cp_parser_token_is_class_key
1691 static void cp_parser_check_class_key
1692 (enum tag_types
, tree type
);
1693 static void cp_parser_check_access_in_redeclaration
1695 static bool cp_parser_optional_template_keyword
1697 static void cp_parser_pre_parsed_nested_name_specifier
1699 static void cp_parser_cache_group
1700 (cp_parser
*, cp_token_cache
*, enum cpp_ttype
, unsigned);
1701 static void cp_parser_parse_tentatively
1703 static void cp_parser_commit_to_tentative_parse
1705 static void cp_parser_abort_tentative_parse
1707 static bool cp_parser_parse_definitely
1709 static inline bool cp_parser_parsing_tentatively
1711 static bool cp_parser_committed_to_tentative_parse
1713 static void cp_parser_error
1714 (cp_parser
*, const char *);
1715 static void cp_parser_name_lookup_error
1716 (cp_parser
*, tree
, tree
, const char *);
1717 static bool cp_parser_simulate_error
1719 static void cp_parser_check_type_definition
1721 static void cp_parser_check_for_definition_in_return_type
1723 static void cp_parser_check_for_invalid_template_id
1724 (cp_parser
*, tree
);
1725 static bool cp_parser_non_integral_constant_expression
1726 (cp_parser
*, const char *);
1727 static void cp_parser_diagnose_invalid_type_name
1728 (cp_parser
*, tree
, tree
);
1729 static bool cp_parser_parse_and_diagnose_invalid_type_name
1731 static int cp_parser_skip_to_closing_parenthesis
1732 (cp_parser
*, bool, bool, bool);
1733 static void cp_parser_skip_to_end_of_statement
1735 static void cp_parser_consume_semicolon_at_end_of_statement
1737 static void cp_parser_skip_to_end_of_block_or_statement
1739 static void cp_parser_skip_to_closing_brace
1741 static void cp_parser_skip_until_found
1742 (cp_parser
*, enum cpp_ttype
, const char *);
1743 static bool cp_parser_error_occurred
1745 static bool cp_parser_allow_gnu_extensions_p
1747 static bool cp_parser_is_string_literal
1749 static bool cp_parser_is_keyword
1750 (cp_token
*, enum rid
);
1751 static tree cp_parser_make_typename_type
1752 (cp_parser
*, tree
, tree
);
1754 /* Returns nonzero if we are parsing tentatively. */
1757 cp_parser_parsing_tentatively (cp_parser
* parser
)
1759 return parser
->context
->next
!= NULL
;
1762 /* Returns nonzero if TOKEN is a string literal. */
1765 cp_parser_is_string_literal (cp_token
* token
)
1767 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1770 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1773 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1775 return token
->keyword
== keyword
;
1778 /* Issue the indicated error MESSAGE. */
1781 cp_parser_error (cp_parser
* parser
, const char* message
)
1783 /* Output the MESSAGE -- unless we're parsing tentatively. */
1784 if (!cp_parser_simulate_error (parser
))
1787 token
= cp_lexer_peek_token (parser
->lexer
);
1788 c_parse_error (message
,
1789 /* Because c_parser_error does not understand
1790 CPP_KEYWORD, keywords are treated like
1792 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1797 /* Issue an error about name-lookup failing. NAME is the
1798 IDENTIFIER_NODE DECL is the result of
1799 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1800 the thing that we hoped to find. */
1803 cp_parser_name_lookup_error (cp_parser
* parser
,
1806 const char* desired
)
1808 /* If name lookup completely failed, tell the user that NAME was not
1810 if (decl
== error_mark_node
)
1812 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1813 error ("`%D::%D' has not been declared",
1814 parser
->scope
, name
);
1815 else if (parser
->scope
== global_namespace
)
1816 error ("`::%D' has not been declared", name
);
1818 error ("`%D' has not been declared", name
);
1820 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1821 error ("`%D::%D' %s", parser
->scope
, name
, desired
);
1822 else if (parser
->scope
== global_namespace
)
1823 error ("`::%D' %s", name
, desired
);
1825 error ("`%D' %s", name
, desired
);
1828 /* If we are parsing tentatively, remember that an error has occurred
1829 during this tentative parse. Returns true if the error was
1830 simulated; false if a message should be issued by the caller. */
1833 cp_parser_simulate_error (cp_parser
* parser
)
1835 if (cp_parser_parsing_tentatively (parser
)
1836 && !cp_parser_committed_to_tentative_parse (parser
))
1838 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1844 /* This function is called when a type is defined. If type
1845 definitions are forbidden at this point, an error message is
1849 cp_parser_check_type_definition (cp_parser
* parser
)
1851 /* If types are forbidden here, issue a message. */
1852 if (parser
->type_definition_forbidden_message
)
1853 /* Use `%s' to print the string in case there are any escape
1854 characters in the message. */
1855 error ("%s", parser
->type_definition_forbidden_message
);
1858 /* This function is called when a declaration is parsed. If
1859 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1860 indicates that a type was defined in the decl-specifiers for DECL,
1861 then an error is issued. */
1864 cp_parser_check_for_definition_in_return_type (tree declarator
,
1865 int declares_class_or_enum
)
1867 /* [dcl.fct] forbids type definitions in return types.
1868 Unfortunately, it's not easy to know whether or not we are
1869 processing a return type until after the fact. */
1871 && (TREE_CODE (declarator
) == INDIRECT_REF
1872 || TREE_CODE (declarator
) == ADDR_EXPR
))
1873 declarator
= TREE_OPERAND (declarator
, 0);
1875 && TREE_CODE (declarator
) == CALL_EXPR
1876 && declares_class_or_enum
& 2)
1877 error ("new types may not be defined in a return type");
1880 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1881 "<" in any valid C++ program. If the next token is indeed "<",
1882 issue a message warning the user about what appears to be an
1883 invalid attempt to form a template-id. */
1886 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1892 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1895 error ("`%T' is not a template", type
);
1896 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1897 error ("`%E' is not a template", type
);
1899 error ("invalid template-id");
1900 /* Remember the location of the invalid "<". */
1901 if (cp_parser_parsing_tentatively (parser
)
1902 && !cp_parser_committed_to_tentative_parse (parser
))
1904 token
= cp_lexer_peek_token (parser
->lexer
);
1905 token
= cp_lexer_prev_token (parser
->lexer
, token
);
1906 start
= cp_lexer_token_difference (parser
->lexer
,
1907 parser
->lexer
->first_token
,
1912 /* Consume the "<". */
1913 cp_lexer_consume_token (parser
->lexer
);
1914 /* Parse the template arguments. */
1915 cp_parser_enclosed_template_argument_list (parser
);
1916 /* Permanently remove the invalid template arguments so that
1917 this error message is not issued again. */
1920 token
= cp_lexer_advance_token (parser
->lexer
,
1921 parser
->lexer
->first_token
,
1923 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
1928 /* If parsing an integral constant-expression, issue an error message
1929 about the fact that THING appeared and return true. Otherwise,
1930 return false, marking the current expression as non-constant. */
1933 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
1936 if (parser
->integral_constant_expression_p
)
1938 if (!parser
->allow_non_integral_constant_expression_p
)
1940 error ("%s cannot appear in a constant-expression", thing
);
1943 parser
->non_integral_constant_expression_p
= true;
1948 /* Emit a diagnostic for an invalid type name. Consider also if it is
1949 qualified or not and the result of a lookup, to provide a better
1953 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
1955 tree decl
, old_scope
;
1956 /* Try to lookup the identifier. */
1957 old_scope
= parser
->scope
;
1958 parser
->scope
= scope
;
1959 decl
= cp_parser_lookup_name_simple (parser
, id
);
1960 parser
->scope
= old_scope
;
1961 /* If the lookup found a template-name, it means that the user forgot
1962 to specify an argument list. Emit an useful error message. */
1963 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1964 error ("invalid use of template-name `%E' without an argument list",
1966 else if (!parser
->scope
)
1968 /* Issue an error message. */
1969 error ("`%E' does not name a type", id
);
1970 /* If we're in a template class, it's possible that the user was
1971 referring to a type from a base class. For example:
1973 template <typename T> struct A { typedef T X; };
1974 template <typename T> struct B : public A<T> { X x; };
1976 The user should have said "typename A<T>::X". */
1977 if (processing_template_decl
&& current_class_type
)
1981 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1985 tree base_type
= BINFO_TYPE (b
);
1986 if (CLASS_TYPE_P (base_type
)
1987 && dependent_type_p (base_type
))
1990 /* Go from a particular instantiation of the
1991 template (which will have an empty TYPE_FIELDs),
1992 to the main version. */
1993 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1994 for (field
= TYPE_FIELDS (base_type
);
1996 field
= TREE_CHAIN (field
))
1997 if (TREE_CODE (field
) == TYPE_DECL
1998 && DECL_NAME (field
) == id
)
2000 inform ("(perhaps `typename %T::%E' was intended)",
2001 BINFO_TYPE (b
), id
);
2010 /* Here we diagnose qualified-ids where the scope is actually correct,
2011 but the identifier does not resolve to a valid type name. */
2014 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2015 error ("`%E' in namespace `%E' does not name a type",
2017 else if (TYPE_P (parser
->scope
))
2018 error ("`%E' in class `%T' does not name a type",
2025 /* Check for a common situation where a type-name should be present,
2026 but is not, and issue a sensible error message. Returns true if an
2027 invalid type-name was detected.
2029 The situation handled by this function are variable declarations of the
2030 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2031 Usually, `ID' should name a type, but if we got here it means that it
2032 does not. We try to emit the best possible error message depending on
2033 how exactly the id-expression looks like.
2037 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2041 cp_parser_parse_tentatively (parser
);
2042 id
= cp_parser_id_expression (parser
,
2043 /*template_keyword_p=*/false,
2044 /*check_dependency_p=*/true,
2045 /*template_p=*/NULL
,
2046 /*declarator_p=*/true);
2047 /* After the id-expression, there should be a plain identifier,
2048 otherwise this is not a simple variable declaration. Also, if
2049 the scope is dependent, we cannot do much. */
2050 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2051 || (parser
->scope
&& TYPE_P (parser
->scope
)
2052 && dependent_type_p (parser
->scope
)))
2054 cp_parser_abort_tentative_parse (parser
);
2057 if (!cp_parser_parse_definitely (parser
))
2060 /* If we got here, this cannot be a valid variable declaration, thus
2061 the cp_parser_id_expression must have resolved to a plain identifier
2062 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2063 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 20030203);
2064 /* Emit a diagnostic for the invalid type. */
2065 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2066 /* Skip to the end of the declaration; there's no point in
2067 trying to process it. */
2068 cp_parser_skip_to_end_of_block_or_statement (parser
);
2072 /* Consume tokens up to, and including, the next non-nested closing `)'.
2073 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2074 are doing error recovery. Returns -1 if OR_COMMA is true and we
2075 found an unnested comma. */
2078 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2083 unsigned paren_depth
= 0;
2084 unsigned brace_depth
= 0;
2086 if (recovering
&& !or_comma
&& cp_parser_parsing_tentatively (parser
)
2087 && !cp_parser_committed_to_tentative_parse (parser
))
2094 /* If we've run out of tokens, then there is no closing `)'. */
2095 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2098 token
= cp_lexer_peek_token (parser
->lexer
);
2100 /* This matches the processing in skip_to_end_of_statement. */
2101 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2103 if (token
->type
== CPP_OPEN_BRACE
)
2105 if (token
->type
== CPP_CLOSE_BRACE
)
2110 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2111 && !brace_depth
&& !paren_depth
)
2116 /* If it is an `(', we have entered another level of nesting. */
2117 if (token
->type
== CPP_OPEN_PAREN
)
2119 /* If it is a `)', then we might be done. */
2120 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2123 cp_lexer_consume_token (parser
->lexer
);
2128 /* Consume the token. */
2129 cp_lexer_consume_token (parser
->lexer
);
2133 /* Consume tokens until we reach the end of the current statement.
2134 Normally, that will be just before consuming a `;'. However, if a
2135 non-nested `}' comes first, then we stop before consuming that. */
2138 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2140 unsigned nesting_depth
= 0;
2146 /* Peek at the next token. */
2147 token
= cp_lexer_peek_token (parser
->lexer
);
2148 /* If we've run out of tokens, stop. */
2149 if (token
->type
== CPP_EOF
)
2151 /* If the next token is a `;', we have reached the end of the
2153 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2155 /* If the next token is a non-nested `}', then we have reached
2156 the end of the current block. */
2157 if (token
->type
== CPP_CLOSE_BRACE
)
2159 /* If this is a non-nested `}', stop before consuming it.
2160 That way, when confronted with something like:
2164 we stop before consuming the closing `}', even though we
2165 have not yet reached a `;'. */
2166 if (nesting_depth
== 0)
2168 /* If it is the closing `}' for a block that we have
2169 scanned, stop -- but only after consuming the token.
2175 we will stop after the body of the erroneously declared
2176 function, but before consuming the following `typedef'
2178 if (--nesting_depth
== 0)
2180 cp_lexer_consume_token (parser
->lexer
);
2184 /* If it the next token is a `{', then we are entering a new
2185 block. Consume the entire block. */
2186 else if (token
->type
== CPP_OPEN_BRACE
)
2188 /* Consume the token. */
2189 cp_lexer_consume_token (parser
->lexer
);
2193 /* This function is called at the end of a statement or declaration.
2194 If the next token is a semicolon, it is consumed; otherwise, error
2195 recovery is attempted. */
2198 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2200 /* Look for the trailing `;'. */
2201 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2203 /* If there is additional (erroneous) input, skip to the end of
2205 cp_parser_skip_to_end_of_statement (parser
);
2206 /* If the next token is now a `;', consume it. */
2207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2208 cp_lexer_consume_token (parser
->lexer
);
2212 /* Skip tokens until we have consumed an entire block, or until we
2213 have consumed a non-nested `;'. */
2216 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2218 unsigned nesting_depth
= 0;
2224 /* Peek at the next token. */
2225 token
= cp_lexer_peek_token (parser
->lexer
);
2226 /* If we've run out of tokens, stop. */
2227 if (token
->type
== CPP_EOF
)
2229 /* If the next token is a `;', we have reached the end of the
2231 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2233 /* Consume the `;'. */
2234 cp_lexer_consume_token (parser
->lexer
);
2237 /* Consume the token. */
2238 token
= cp_lexer_consume_token (parser
->lexer
);
2239 /* If the next token is a non-nested `}', then we have reached
2240 the end of the current block. */
2241 if (token
->type
== CPP_CLOSE_BRACE
2242 && (nesting_depth
== 0 || --nesting_depth
== 0))
2244 /* If it the next token is a `{', then we are entering a new
2245 block. Consume the entire block. */
2246 if (token
->type
== CPP_OPEN_BRACE
)
2251 /* Skip tokens until a non-nested closing curly brace is the next
2255 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2257 unsigned nesting_depth
= 0;
2263 /* Peek at the next token. */
2264 token
= cp_lexer_peek_token (parser
->lexer
);
2265 /* If we've run out of tokens, stop. */
2266 if (token
->type
== CPP_EOF
)
2268 /* If the next token is a non-nested `}', then we have reached
2269 the end of the current block. */
2270 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2272 /* If it the next token is a `{', then we are entering a new
2273 block. Consume the entire block. */
2274 else if (token
->type
== CPP_OPEN_BRACE
)
2276 /* Consume the token. */
2277 cp_lexer_consume_token (parser
->lexer
);
2281 /* This is a simple wrapper around make_typename_type. When the id is
2282 an unresolved identifier node, we can provide a superior diagnostic
2283 using cp_parser_diagnose_invalid_type_name. */
2286 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2289 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2291 result
= make_typename_type (scope
, id
, /*complain=*/0);
2292 if (result
== error_mark_node
)
2293 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2296 return make_typename_type (scope
, id
, tf_error
);
2300 /* Create a new C++ parser. */
2303 cp_parser_new (void)
2308 /* cp_lexer_new_main is called before calling ggc_alloc because
2309 cp_lexer_new_main might load a PCH file. */
2310 lexer
= cp_lexer_new_main ();
2312 parser
= ggc_alloc_cleared (sizeof (cp_parser
));
2313 parser
->lexer
= lexer
;
2314 parser
->context
= cp_parser_context_new (NULL
);
2316 /* For now, we always accept GNU extensions. */
2317 parser
->allow_gnu_extensions_p
= 1;
2319 /* The `>' token is a greater-than operator, not the end of a
2321 parser
->greater_than_is_operator_p
= true;
2323 parser
->default_arg_ok_p
= true;
2325 /* We are not parsing a constant-expression. */
2326 parser
->integral_constant_expression_p
= false;
2327 parser
->allow_non_integral_constant_expression_p
= false;
2328 parser
->non_integral_constant_expression_p
= false;
2330 /* Local variable names are not forbidden. */
2331 parser
->local_variables_forbidden_p
= false;
2333 /* We are not processing an `extern "C"' declaration. */
2334 parser
->in_unbraced_linkage_specification_p
= false;
2336 /* We are not processing a declarator. */
2337 parser
->in_declarator_p
= false;
2339 /* We are not processing a template-argument-list. */
2340 parser
->in_template_argument_list_p
= false;
2342 /* We are not in an iteration statement. */
2343 parser
->in_iteration_statement_p
= false;
2345 /* We are not in a switch statement. */
2346 parser
->in_switch_statement_p
= false;
2348 /* We are not parsing a type-id inside an expression. */
2349 parser
->in_type_id_in_expr_p
= false;
2351 /* The unparsed function queue is empty. */
2352 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2354 /* There are no classes being defined. */
2355 parser
->num_classes_being_defined
= 0;
2357 /* No template parameters apply. */
2358 parser
->num_template_parameter_lists
= 0;
2363 /* Lexical conventions [gram.lex] */
2365 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2369 cp_parser_identifier (cp_parser
* parser
)
2373 /* Look for the identifier. */
2374 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2375 /* Return the value. */
2376 return token
? token
->value
: error_mark_node
;
2379 /* Basic concepts [gram.basic] */
2381 /* Parse a translation-unit.
2384 declaration-seq [opt]
2386 Returns TRUE if all went well. */
2389 cp_parser_translation_unit (cp_parser
* parser
)
2393 cp_parser_declaration_seq_opt (parser
);
2395 /* If there are no tokens left then all went well. */
2396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2399 /* Otherwise, issue an error message. */
2400 cp_parser_error (parser
, "expected declaration");
2404 /* Consume the EOF token. */
2405 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2408 finish_translation_unit ();
2410 /* All went well. */
2414 /* Expressions [gram.expr] */
2416 /* Parse a primary-expression.
2427 ( compound-statement )
2428 __builtin_va_arg ( assignment-expression , type-id )
2433 Returns a representation of the expression.
2435 *IDK indicates what kind of id-expression (if any) was present.
2437 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2438 used as the operand of a pointer-to-member. In that case,
2439 *QUALIFYING_CLASS gives the class that is used as the qualifying
2440 class in the pointer-to-member. */
2443 cp_parser_primary_expression (cp_parser
*parser
,
2445 tree
*qualifying_class
)
2449 /* Assume the primary expression is not an id-expression. */
2450 *idk
= CP_ID_KIND_NONE
;
2451 /* And that it cannot be used as pointer-to-member. */
2452 *qualifying_class
= NULL_TREE
;
2454 /* Peek at the next token. */
2455 token
= cp_lexer_peek_token (parser
->lexer
);
2456 switch (token
->type
)
2469 token
= cp_lexer_consume_token (parser
->lexer
);
2470 return token
->value
;
2472 case CPP_OPEN_PAREN
:
2475 bool saved_greater_than_is_operator_p
;
2477 /* Consume the `('. */
2478 cp_lexer_consume_token (parser
->lexer
);
2479 /* Within a parenthesized expression, a `>' token is always
2480 the greater-than operator. */
2481 saved_greater_than_is_operator_p
2482 = parser
->greater_than_is_operator_p
;
2483 parser
->greater_than_is_operator_p
= true;
2484 /* If we see `( { ' then we are looking at the beginning of
2485 a GNU statement-expression. */
2486 if (cp_parser_allow_gnu_extensions_p (parser
)
2487 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2489 /* Statement-expressions are not allowed by the standard. */
2491 pedwarn ("ISO C++ forbids braced-groups within expressions");
2493 /* And they're not allowed outside of a function-body; you
2494 cannot, for example, write:
2496 int i = ({ int j = 3; j + 1; });
2498 at class or namespace scope. */
2499 if (!at_function_scope_p ())
2500 error ("statement-expressions are allowed only inside functions");
2501 /* Start the statement-expression. */
2502 expr
= begin_stmt_expr ();
2503 /* Parse the compound-statement. */
2504 cp_parser_compound_statement (parser
, true);
2506 expr
= finish_stmt_expr (expr
, false);
2510 /* Parse the parenthesized expression. */
2511 expr
= cp_parser_expression (parser
);
2512 /* Let the front end know that this expression was
2513 enclosed in parentheses. This matters in case, for
2514 example, the expression is of the form `A::B', since
2515 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2517 finish_parenthesized_expr (expr
);
2519 /* The `>' token might be the end of a template-id or
2520 template-parameter-list now. */
2521 parser
->greater_than_is_operator_p
2522 = saved_greater_than_is_operator_p
;
2523 /* Consume the `)'. */
2524 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2525 cp_parser_skip_to_end_of_statement (parser
);
2531 switch (token
->keyword
)
2533 /* These two are the boolean literals. */
2535 cp_lexer_consume_token (parser
->lexer
);
2536 return boolean_true_node
;
2538 cp_lexer_consume_token (parser
->lexer
);
2539 return boolean_false_node
;
2541 /* The `__null' literal. */
2543 cp_lexer_consume_token (parser
->lexer
);
2546 /* Recognize the `this' keyword. */
2548 cp_lexer_consume_token (parser
->lexer
);
2549 if (parser
->local_variables_forbidden_p
)
2551 error ("`this' may not be used in this context");
2552 return error_mark_node
;
2554 /* Pointers cannot appear in constant-expressions. */
2555 if (cp_parser_non_integral_constant_expression (parser
,
2557 return error_mark_node
;
2558 return finish_this_expr ();
2560 /* The `operator' keyword can be the beginning of an
2565 case RID_FUNCTION_NAME
:
2566 case RID_PRETTY_FUNCTION_NAME
:
2567 case RID_C99_FUNCTION_NAME
:
2568 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2569 __func__ are the names of variables -- but they are
2570 treated specially. Therefore, they are handled here,
2571 rather than relying on the generic id-expression logic
2572 below. Grammatically, these names are id-expressions.
2574 Consume the token. */
2575 token
= cp_lexer_consume_token (parser
->lexer
);
2576 /* Look up the name. */
2577 return finish_fname (token
->value
);
2584 /* The `__builtin_va_arg' construct is used to handle
2585 `va_arg'. Consume the `__builtin_va_arg' token. */
2586 cp_lexer_consume_token (parser
->lexer
);
2587 /* Look for the opening `('. */
2588 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2589 /* Now, parse the assignment-expression. */
2590 expression
= cp_parser_assignment_expression (parser
);
2591 /* Look for the `,'. */
2592 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2593 /* Parse the type-id. */
2594 type
= cp_parser_type_id (parser
);
2595 /* Look for the closing `)'. */
2596 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2597 /* Using `va_arg' in a constant-expression is not
2599 if (cp_parser_non_integral_constant_expression (parser
,
2601 return error_mark_node
;
2602 return build_x_va_arg (expression
, type
);
2606 return cp_parser_builtin_offsetof (parser
);
2609 cp_parser_error (parser
, "expected primary-expression");
2610 return error_mark_node
;
2613 /* An id-expression can start with either an identifier, a
2614 `::' as the beginning of a qualified-id, or the "operator"
2618 case CPP_TEMPLATE_ID
:
2619 case CPP_NESTED_NAME_SPECIFIER
:
2623 const char *error_msg
;
2626 /* Parse the id-expression. */
2628 = cp_parser_id_expression (parser
,
2629 /*template_keyword_p=*/false,
2630 /*check_dependency_p=*/true,
2631 /*template_p=*/NULL
,
2632 /*declarator_p=*/false);
2633 if (id_expression
== error_mark_node
)
2634 return error_mark_node
;
2635 /* If we have a template-id, then no further lookup is
2636 required. If the template-id was for a template-class, we
2637 will sometimes have a TYPE_DECL at this point. */
2638 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2639 || TREE_CODE (id_expression
) == TYPE_DECL
)
2640 decl
= id_expression
;
2641 /* Look up the name. */
2644 decl
= cp_parser_lookup_name_simple (parser
, id_expression
);
2645 /* If name lookup gives us a SCOPE_REF, then the
2646 qualifying scope was dependent. Just propagate the
2648 if (TREE_CODE (decl
) == SCOPE_REF
)
2650 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2651 *qualifying_class
= TREE_OPERAND (decl
, 0);
2654 /* Check to see if DECL is a local variable in a context
2655 where that is forbidden. */
2656 if (parser
->local_variables_forbidden_p
2657 && local_variable_p (decl
))
2659 /* It might be that we only found DECL because we are
2660 trying to be generous with pre-ISO scoping rules.
2661 For example, consider:
2665 for (int i = 0; i < 10; ++i) {}
2666 extern void f(int j = i);
2669 Here, name look up will originally find the out
2670 of scope `i'. We need to issue a warning message,
2671 but then use the global `i'. */
2672 decl
= check_for_out_of_scope_variable (decl
);
2673 if (local_variable_p (decl
))
2675 error ("local variable `%D' may not appear in this context",
2677 return error_mark_node
;
2682 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2683 idk
, qualifying_class
,
2684 parser
->integral_constant_expression_p
,
2685 parser
->allow_non_integral_constant_expression_p
,
2686 &parser
->non_integral_constant_expression_p
,
2689 cp_parser_error (parser
, error_msg
);
2693 /* Anything else is an error. */
2695 cp_parser_error (parser
, "expected primary-expression");
2696 return error_mark_node
;
2700 /* Parse an id-expression.
2707 :: [opt] nested-name-specifier template [opt] unqualified-id
2709 :: operator-function-id
2712 Return a representation of the unqualified portion of the
2713 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2714 a `::' or nested-name-specifier.
2716 Often, if the id-expression was a qualified-id, the caller will
2717 want to make a SCOPE_REF to represent the qualified-id. This
2718 function does not do this in order to avoid wastefully creating
2719 SCOPE_REFs when they are not required.
2721 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2724 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2725 uninstantiated templates.
2727 If *TEMPLATE_P is non-NULL, it is set to true iff the
2728 `template' keyword is used to explicitly indicate that the entity
2729 named is a template.
2731 If DECLARATOR_P is true, the id-expression is appearing as part of
2732 a declarator, rather than as part of an expression. */
2735 cp_parser_id_expression (cp_parser
*parser
,
2736 bool template_keyword_p
,
2737 bool check_dependency_p
,
2741 bool global_scope_p
;
2742 bool nested_name_specifier_p
;
2744 /* Assume the `template' keyword was not used. */
2746 *template_p
= false;
2748 /* Look for the optional `::' operator. */
2750 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2752 /* Look for the optional nested-name-specifier. */
2753 nested_name_specifier_p
2754 = (cp_parser_nested_name_specifier_opt (parser
,
2755 /*typename_keyword_p=*/false,
2758 /*is_declarator=*/false)
2760 /* If there is a nested-name-specifier, then we are looking at
2761 the first qualified-id production. */
2762 if (nested_name_specifier_p
)
2765 tree saved_object_scope
;
2766 tree saved_qualifying_scope
;
2767 tree unqualified_id
;
2770 /* See if the next token is the `template' keyword. */
2772 template_p
= &is_template
;
2773 *template_p
= cp_parser_optional_template_keyword (parser
);
2774 /* Name lookup we do during the processing of the
2775 unqualified-id might obliterate SCOPE. */
2776 saved_scope
= parser
->scope
;
2777 saved_object_scope
= parser
->object_scope
;
2778 saved_qualifying_scope
= parser
->qualifying_scope
;
2779 /* Process the final unqualified-id. */
2780 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2783 /* Restore the SAVED_SCOPE for our caller. */
2784 parser
->scope
= saved_scope
;
2785 parser
->object_scope
= saved_object_scope
;
2786 parser
->qualifying_scope
= saved_qualifying_scope
;
2788 return unqualified_id
;
2790 /* Otherwise, if we are in global scope, then we are looking at one
2791 of the other qualified-id productions. */
2792 else if (global_scope_p
)
2797 /* Peek at the next token. */
2798 token
= cp_lexer_peek_token (parser
->lexer
);
2800 /* If it's an identifier, and the next token is not a "<", then
2801 we can avoid the template-id case. This is an optimization
2802 for this common case. */
2803 if (token
->type
== CPP_NAME
2804 && !cp_parser_nth_token_starts_template_argument_list_p
2806 return cp_parser_identifier (parser
);
2808 cp_parser_parse_tentatively (parser
);
2809 /* Try a template-id. */
2810 id
= cp_parser_template_id (parser
,
2811 /*template_keyword_p=*/false,
2812 /*check_dependency_p=*/true,
2814 /* If that worked, we're done. */
2815 if (cp_parser_parse_definitely (parser
))
2818 /* Peek at the next token. (Changes in the token buffer may
2819 have invalidated the pointer obtained above.) */
2820 token
= cp_lexer_peek_token (parser
->lexer
);
2822 switch (token
->type
)
2825 return cp_parser_identifier (parser
);
2828 if (token
->keyword
== RID_OPERATOR
)
2829 return cp_parser_operator_function_id (parser
);
2833 cp_parser_error (parser
, "expected id-expression");
2834 return error_mark_node
;
2838 return cp_parser_unqualified_id (parser
, template_keyword_p
,
2839 /*check_dependency_p=*/true,
2843 /* Parse an unqualified-id.
2847 operator-function-id
2848 conversion-function-id
2852 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2853 keyword, in a construct like `A::template ...'.
2855 Returns a representation of unqualified-id. For the `identifier'
2856 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2857 production a BIT_NOT_EXPR is returned; the operand of the
2858 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2859 other productions, see the documentation accompanying the
2860 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2861 names are looked up in uninstantiated templates. If DECLARATOR_P
2862 is true, the unqualified-id is appearing as part of a declarator,
2863 rather than as part of an expression. */
2866 cp_parser_unqualified_id (cp_parser
* parser
,
2867 bool template_keyword_p
,
2868 bool check_dependency_p
,
2873 /* Peek at the next token. */
2874 token
= cp_lexer_peek_token (parser
->lexer
);
2876 switch (token
->type
)
2882 /* We don't know yet whether or not this will be a
2884 cp_parser_parse_tentatively (parser
);
2885 /* Try a template-id. */
2886 id
= cp_parser_template_id (parser
, template_keyword_p
,
2889 /* If it worked, we're done. */
2890 if (cp_parser_parse_definitely (parser
))
2892 /* Otherwise, it's an ordinary identifier. */
2893 return cp_parser_identifier (parser
);
2896 case CPP_TEMPLATE_ID
:
2897 return cp_parser_template_id (parser
, template_keyword_p
,
2904 tree qualifying_scope
;
2908 /* Consume the `~' token. */
2909 cp_lexer_consume_token (parser
->lexer
);
2910 /* Parse the class-name. The standard, as written, seems to
2913 template <typename T> struct S { ~S (); };
2914 template <typename T> S<T>::~S() {}
2916 is invalid, since `~' must be followed by a class-name, but
2917 `S<T>' is dependent, and so not known to be a class.
2918 That's not right; we need to look in uninstantiated
2919 templates. A further complication arises from:
2921 template <typename T> void f(T t) {
2925 Here, it is not possible to look up `T' in the scope of `T'
2926 itself. We must look in both the current scope, and the
2927 scope of the containing complete expression.
2929 Yet another issue is:
2938 The standard does not seem to say that the `S' in `~S'
2939 should refer to the type `S' and not the data member
2942 /* DR 244 says that we look up the name after the "~" in the
2943 same scope as we looked up the qualifying name. That idea
2944 isn't fully worked out; it's more complicated than that. */
2945 scope
= parser
->scope
;
2946 object_scope
= parser
->object_scope
;
2947 qualifying_scope
= parser
->qualifying_scope
;
2949 /* If the name is of the form "X::~X" it's OK. */
2950 if (scope
&& TYPE_P (scope
)
2951 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2952 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
2954 && (cp_lexer_peek_token (parser
->lexer
)->value
2955 == TYPE_IDENTIFIER (scope
)))
2957 cp_lexer_consume_token (parser
->lexer
);
2958 return build_nt (BIT_NOT_EXPR
, scope
);
2961 /* If there was an explicit qualification (S::~T), first look
2962 in the scope given by the qualification (i.e., S). */
2965 cp_parser_parse_tentatively (parser
);
2966 type_decl
= cp_parser_class_name (parser
,
2967 /*typename_keyword_p=*/false,
2968 /*template_keyword_p=*/false,
2970 /*check_dependency=*/false,
2971 /*class_head_p=*/false,
2973 if (cp_parser_parse_definitely (parser
))
2974 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2976 /* In "N::S::~S", look in "N" as well. */
2977 if (scope
&& qualifying_scope
)
2979 cp_parser_parse_tentatively (parser
);
2980 parser
->scope
= qualifying_scope
;
2981 parser
->object_scope
= NULL_TREE
;
2982 parser
->qualifying_scope
= NULL_TREE
;
2984 = cp_parser_class_name (parser
,
2985 /*typename_keyword_p=*/false,
2986 /*template_keyword_p=*/false,
2988 /*check_dependency=*/false,
2989 /*class_head_p=*/false,
2991 if (cp_parser_parse_definitely (parser
))
2992 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
2994 /* In "p->S::~T", look in the scope given by "*p" as well. */
2995 else if (object_scope
)
2997 cp_parser_parse_tentatively (parser
);
2998 parser
->scope
= object_scope
;
2999 parser
->object_scope
= NULL_TREE
;
3000 parser
->qualifying_scope
= NULL_TREE
;
3002 = cp_parser_class_name (parser
,
3003 /*typename_keyword_p=*/false,
3004 /*template_keyword_p=*/false,
3006 /*check_dependency=*/false,
3007 /*class_head_p=*/false,
3009 if (cp_parser_parse_definitely (parser
))
3010 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3012 /* Look in the surrounding context. */
3013 parser
->scope
= NULL_TREE
;
3014 parser
->object_scope
= NULL_TREE
;
3015 parser
->qualifying_scope
= NULL_TREE
;
3017 = cp_parser_class_name (parser
,
3018 /*typename_keyword_p=*/false,
3019 /*template_keyword_p=*/false,
3021 /*check_dependency=*/false,
3022 /*class_head_p=*/false,
3024 /* If an error occurred, assume that the name of the
3025 destructor is the same as the name of the qualifying
3026 class. That allows us to keep parsing after running
3027 into ill-formed destructor names. */
3028 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3029 return build_nt (BIT_NOT_EXPR
, scope
);
3030 else if (type_decl
== error_mark_node
)
3031 return error_mark_node
;
3035 A typedef-name that names a class shall not be used as the
3036 identifier in the declarator for a destructor declaration. */
3038 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3039 && !DECL_SELF_REFERENCE_P (type_decl
))
3040 error ("typedef-name `%D' used as destructor declarator",
3043 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3047 if (token
->keyword
== RID_OPERATOR
)
3051 /* This could be a template-id, so we try that first. */
3052 cp_parser_parse_tentatively (parser
);
3053 /* Try a template-id. */
3054 id
= cp_parser_template_id (parser
, template_keyword_p
,
3055 /*check_dependency_p=*/true,
3057 /* If that worked, we're done. */
3058 if (cp_parser_parse_definitely (parser
))
3060 /* We still don't know whether we're looking at an
3061 operator-function-id or a conversion-function-id. */
3062 cp_parser_parse_tentatively (parser
);
3063 /* Try an operator-function-id. */
3064 id
= cp_parser_operator_function_id (parser
);
3065 /* If that didn't work, try a conversion-function-id. */
3066 if (!cp_parser_parse_definitely (parser
))
3067 id
= cp_parser_conversion_function_id (parser
);
3074 cp_parser_error (parser
, "expected unqualified-id");
3075 return error_mark_node
;
3079 /* Parse an (optional) nested-name-specifier.
3081 nested-name-specifier:
3082 class-or-namespace-name :: nested-name-specifier [opt]
3083 class-or-namespace-name :: template nested-name-specifier [opt]
3085 PARSER->SCOPE should be set appropriately before this function is
3086 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3087 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3090 Sets PARSER->SCOPE to the class (TYPE) or namespace
3091 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3092 it unchanged if there is no nested-name-specifier. Returns the new
3093 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3095 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3096 part of a declaration and/or decl-specifier. */
3099 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3100 bool typename_keyword_p
,
3101 bool check_dependency_p
,
3103 bool is_declaration
)
3105 bool success
= false;
3106 tree access_check
= NULL_TREE
;
3110 /* If the next token corresponds to a nested name specifier, there
3111 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3112 false, it may have been true before, in which case something
3113 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3114 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3115 CHECK_DEPENDENCY_P is false, we have to fall through into the
3117 if (check_dependency_p
3118 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3120 cp_parser_pre_parsed_nested_name_specifier (parser
);
3121 return parser
->scope
;
3124 /* Remember where the nested-name-specifier starts. */
3125 if (cp_parser_parsing_tentatively (parser
)
3126 && !cp_parser_committed_to_tentative_parse (parser
))
3128 token
= cp_lexer_peek_token (parser
->lexer
);
3129 start
= cp_lexer_token_difference (parser
->lexer
,
3130 parser
->lexer
->first_token
,
3136 push_deferring_access_checks (dk_deferred
);
3142 tree saved_qualifying_scope
;
3143 bool template_keyword_p
;
3145 /* Spot cases that cannot be the beginning of a
3146 nested-name-specifier. */
3147 token
= cp_lexer_peek_token (parser
->lexer
);
3149 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3150 the already parsed nested-name-specifier. */
3151 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3153 /* Grab the nested-name-specifier and continue the loop. */
3154 cp_parser_pre_parsed_nested_name_specifier (parser
);
3159 /* Spot cases that cannot be the beginning of a
3160 nested-name-specifier. On the second and subsequent times
3161 through the loop, we look for the `template' keyword. */
3162 if (success
&& token
->keyword
== RID_TEMPLATE
)
3164 /* A template-id can start a nested-name-specifier. */
3165 else if (token
->type
== CPP_TEMPLATE_ID
)
3169 /* If the next token is not an identifier, then it is
3170 definitely not a class-or-namespace-name. */
3171 if (token
->type
!= CPP_NAME
)
3173 /* If the following token is neither a `<' (to begin a
3174 template-id), nor a `::', then we are not looking at a
3175 nested-name-specifier. */
3176 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3177 if (token
->type
!= CPP_SCOPE
3178 && !cp_parser_nth_token_starts_template_argument_list_p
3183 /* The nested-name-specifier is optional, so we parse
3185 cp_parser_parse_tentatively (parser
);
3187 /* Look for the optional `template' keyword, if this isn't the
3188 first time through the loop. */
3190 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3192 template_keyword_p
= false;
3194 /* Save the old scope since the name lookup we are about to do
3195 might destroy it. */
3196 old_scope
= parser
->scope
;
3197 saved_qualifying_scope
= parser
->qualifying_scope
;
3198 /* Parse the qualifying entity. */
3200 = cp_parser_class_or_namespace_name (parser
,
3206 /* Look for the `::' token. */
3207 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3209 /* If we found what we wanted, we keep going; otherwise, we're
3211 if (!cp_parser_parse_definitely (parser
))
3213 bool error_p
= false;
3215 /* Restore the OLD_SCOPE since it was valid before the
3216 failed attempt at finding the last
3217 class-or-namespace-name. */
3218 parser
->scope
= old_scope
;
3219 parser
->qualifying_scope
= saved_qualifying_scope
;
3220 /* If the next token is an identifier, and the one after
3221 that is a `::', then any valid interpretation would have
3222 found a class-or-namespace-name. */
3223 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3224 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3226 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3229 token
= cp_lexer_consume_token (parser
->lexer
);
3234 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3235 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3236 error ("`%D' used without template parameters",
3239 cp_parser_name_lookup_error
3240 (parser
, token
->value
, decl
,
3241 "is not a class or namespace");
3242 parser
->scope
= NULL_TREE
;
3244 /* Treat this as a successful nested-name-specifier
3249 If the name found is not a class-name (clause
3250 _class_) or namespace-name (_namespace.def_), the
3251 program is ill-formed. */
3254 cp_lexer_consume_token (parser
->lexer
);
3259 /* We've found one valid nested-name-specifier. */
3261 /* Make sure we look in the right scope the next time through
3263 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3264 ? TREE_TYPE (new_scope
)
3266 /* If it is a class scope, try to complete it; we are about to
3267 be looking up names inside the class. */
3268 if (TYPE_P (parser
->scope
)
3269 /* Since checking types for dependency can be expensive,
3270 avoid doing it if the type is already complete. */
3271 && !COMPLETE_TYPE_P (parser
->scope
)
3272 /* Do not try to complete dependent types. */
3273 && !dependent_type_p (parser
->scope
))
3274 complete_type (parser
->scope
);
3277 /* Retrieve any deferred checks. Do not pop this access checks yet
3278 so the memory will not be reclaimed during token replacing below. */
3279 access_check
= get_deferred_access_checks ();
3281 /* If parsing tentatively, replace the sequence of tokens that makes
3282 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3283 token. That way, should we re-parse the token stream, we will
3284 not have to repeat the effort required to do the parse, nor will
3285 we issue duplicate error messages. */
3286 if (success
&& start
>= 0)
3288 /* Find the token that corresponds to the start of the
3290 token
= cp_lexer_advance_token (parser
->lexer
,
3291 parser
->lexer
->first_token
,
3294 /* Reset the contents of the START token. */
3295 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3296 token
->value
= build_tree_list (access_check
, parser
->scope
);
3297 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3298 token
->keyword
= RID_MAX
;
3299 /* Purge all subsequent tokens. */
3300 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3303 pop_deferring_access_checks ();
3304 return success
? parser
->scope
: NULL_TREE
;
3307 /* Parse a nested-name-specifier. See
3308 cp_parser_nested_name_specifier_opt for details. This function
3309 behaves identically, except that it will an issue an error if no
3310 nested-name-specifier is present, and it will return
3311 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3315 cp_parser_nested_name_specifier (cp_parser
*parser
,
3316 bool typename_keyword_p
,
3317 bool check_dependency_p
,
3319 bool is_declaration
)
3323 /* Look for the nested-name-specifier. */
3324 scope
= cp_parser_nested_name_specifier_opt (parser
,
3329 /* If it was not present, issue an error message. */
3332 cp_parser_error (parser
, "expected nested-name-specifier");
3333 parser
->scope
= NULL_TREE
;
3334 return error_mark_node
;
3340 /* Parse a class-or-namespace-name.
3342 class-or-namespace-name:
3346 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3347 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3348 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3349 TYPE_P is TRUE iff the next name should be taken as a class-name,
3350 even the same name is declared to be another entity in the same
3353 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3354 specified by the class-or-namespace-name. If neither is found the
3355 ERROR_MARK_NODE is returned. */
3358 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3359 bool typename_keyword_p
,
3360 bool template_keyword_p
,
3361 bool check_dependency_p
,
3363 bool is_declaration
)
3366 tree saved_qualifying_scope
;
3367 tree saved_object_scope
;
3371 /* Before we try to parse the class-name, we must save away the
3372 current PARSER->SCOPE since cp_parser_class_name will destroy
3374 saved_scope
= parser
->scope
;
3375 saved_qualifying_scope
= parser
->qualifying_scope
;
3376 saved_object_scope
= parser
->object_scope
;
3377 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3378 there is no need to look for a namespace-name. */
3379 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3381 cp_parser_parse_tentatively (parser
);
3382 scope
= cp_parser_class_name (parser
,
3387 /*class_head_p=*/false,
3389 /* If that didn't work, try for a namespace-name. */
3390 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3392 /* Restore the saved scope. */
3393 parser
->scope
= saved_scope
;
3394 parser
->qualifying_scope
= saved_qualifying_scope
;
3395 parser
->object_scope
= saved_object_scope
;
3396 /* If we are not looking at an identifier followed by the scope
3397 resolution operator, then this is not part of a
3398 nested-name-specifier. (Note that this function is only used
3399 to parse the components of a nested-name-specifier.) */
3400 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3401 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3402 return error_mark_node
;
3403 scope
= cp_parser_namespace_name (parser
);
3409 /* Parse a postfix-expression.
3413 postfix-expression [ expression ]
3414 postfix-expression ( expression-list [opt] )
3415 simple-type-specifier ( expression-list [opt] )
3416 typename :: [opt] nested-name-specifier identifier
3417 ( expression-list [opt] )
3418 typename :: [opt] nested-name-specifier template [opt] template-id
3419 ( expression-list [opt] )
3420 postfix-expression . template [opt] id-expression
3421 postfix-expression -> template [opt] id-expression
3422 postfix-expression . pseudo-destructor-name
3423 postfix-expression -> pseudo-destructor-name
3424 postfix-expression ++
3425 postfix-expression --
3426 dynamic_cast < type-id > ( expression )
3427 static_cast < type-id > ( expression )
3428 reinterpret_cast < type-id > ( expression )
3429 const_cast < type-id > ( expression )
3430 typeid ( expression )
3436 ( type-id ) { initializer-list , [opt] }
3438 This extension is a GNU version of the C99 compound-literal
3439 construct. (The C99 grammar uses `type-name' instead of `type-id',
3440 but they are essentially the same concept.)
3442 If ADDRESS_P is true, the postfix expression is the operand of the
3445 Returns a representation of the expression. */
3448 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3452 cp_id_kind idk
= CP_ID_KIND_NONE
;
3453 tree postfix_expression
= NULL_TREE
;
3454 /* Non-NULL only if the current postfix-expression can be used to
3455 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3456 class used to qualify the member. */
3457 tree qualifying_class
= NULL_TREE
;
3459 /* Peek at the next token. */
3460 token
= cp_lexer_peek_token (parser
->lexer
);
3461 /* Some of the productions are determined by keywords. */
3462 keyword
= token
->keyword
;
3472 const char *saved_message
;
3474 /* All of these can be handled in the same way from the point
3475 of view of parsing. Begin by consuming the token
3476 identifying the cast. */
3477 cp_lexer_consume_token (parser
->lexer
);
3479 /* New types cannot be defined in the cast. */
3480 saved_message
= parser
->type_definition_forbidden_message
;
3481 parser
->type_definition_forbidden_message
3482 = "types may not be defined in casts";
3484 /* Look for the opening `<'. */
3485 cp_parser_require (parser
, CPP_LESS
, "`<'");
3486 /* Parse the type to which we are casting. */
3487 type
= cp_parser_type_id (parser
);
3488 /* Look for the closing `>'. */
3489 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3490 /* Restore the old message. */
3491 parser
->type_definition_forbidden_message
= saved_message
;
3493 /* And the expression which is being cast. */
3494 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3495 expression
= cp_parser_expression (parser
);
3496 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3498 /* Only type conversions to integral or enumeration types
3499 can be used in constant-expressions. */
3500 if (parser
->integral_constant_expression_p
3501 && !dependent_type_p (type
)
3502 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3503 && (cp_parser_non_integral_constant_expression
3505 "a cast to a type other than an integral or "
3506 "enumeration type")))
3507 return error_mark_node
;
3513 = build_dynamic_cast (type
, expression
);
3517 = build_static_cast (type
, expression
);
3521 = build_reinterpret_cast (type
, expression
);
3525 = build_const_cast (type
, expression
);
3536 const char *saved_message
;
3537 bool saved_in_type_id_in_expr_p
;
3539 /* Consume the `typeid' token. */
3540 cp_lexer_consume_token (parser
->lexer
);
3541 /* Look for the `(' token. */
3542 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3543 /* Types cannot be defined in a `typeid' expression. */
3544 saved_message
= parser
->type_definition_forbidden_message
;
3545 parser
->type_definition_forbidden_message
3546 = "types may not be defined in a `typeid\' expression";
3547 /* We can't be sure yet whether we're looking at a type-id or an
3549 cp_parser_parse_tentatively (parser
);
3550 /* Try a type-id first. */
3551 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3552 parser
->in_type_id_in_expr_p
= true;
3553 type
= cp_parser_type_id (parser
);
3554 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3555 /* Look for the `)' token. Otherwise, we can't be sure that
3556 we're not looking at an expression: consider `typeid (int
3557 (3))', for example. */
3558 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3559 /* If all went well, simply lookup the type-id. */
3560 if (cp_parser_parse_definitely (parser
))
3561 postfix_expression
= get_typeid (type
);
3562 /* Otherwise, fall back to the expression variant. */
3567 /* Look for an expression. */
3568 expression
= cp_parser_expression (parser
);
3569 /* Compute its typeid. */
3570 postfix_expression
= build_typeid (expression
);
3571 /* Look for the `)' token. */
3572 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3574 /* `typeid' may not appear in an integral constant expression. */
3575 if (cp_parser_non_integral_constant_expression(parser
,
3576 "`typeid' operator"))
3577 return error_mark_node
;
3578 /* Restore the saved message. */
3579 parser
->type_definition_forbidden_message
= saved_message
;
3585 bool template_p
= false;
3589 /* Consume the `typename' token. */
3590 cp_lexer_consume_token (parser
->lexer
);
3591 /* Look for the optional `::' operator. */
3592 cp_parser_global_scope_opt (parser
,
3593 /*current_scope_valid_p=*/false);
3594 /* Look for the nested-name-specifier. */
3595 cp_parser_nested_name_specifier (parser
,
3596 /*typename_keyword_p=*/true,
3597 /*check_dependency_p=*/true,
3599 /*is_declaration=*/true);
3600 /* Look for the optional `template' keyword. */
3601 template_p
= cp_parser_optional_template_keyword (parser
);
3602 /* We don't know whether we're looking at a template-id or an
3604 cp_parser_parse_tentatively (parser
);
3605 /* Try a template-id. */
3606 id
= cp_parser_template_id (parser
, template_p
,
3607 /*check_dependency_p=*/true,
3608 /*is_declaration=*/true);
3609 /* If that didn't work, try an identifier. */
3610 if (!cp_parser_parse_definitely (parser
))
3611 id
= cp_parser_identifier (parser
);
3612 /* If we look up a template-id in a non-dependent qualifying
3613 scope, there's no need to create a dependent type. */
3614 if (TREE_CODE (id
) == TYPE_DECL
3615 && !dependent_type_p (parser
->scope
))
3616 type
= TREE_TYPE (id
);
3617 /* Create a TYPENAME_TYPE to represent the type to which the
3618 functional cast is being performed. */
3620 type
= make_typename_type (parser
->scope
, id
,
3623 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3631 /* If the next thing is a simple-type-specifier, we may be
3632 looking at a functional cast. We could also be looking at
3633 an id-expression. So, we try the functional cast, and if
3634 that doesn't work we fall back to the primary-expression. */
3635 cp_parser_parse_tentatively (parser
);
3636 /* Look for the simple-type-specifier. */
3637 type
= cp_parser_simple_type_specifier (parser
,
3638 CP_PARSER_FLAGS_NONE
,
3639 /*identifier_p=*/false);
3640 /* Parse the cast itself. */
3641 if (!cp_parser_error_occurred (parser
))
3643 = cp_parser_functional_cast (parser
, type
);
3644 /* If that worked, we're done. */
3645 if (cp_parser_parse_definitely (parser
))
3648 /* If the functional-cast didn't work out, try a
3649 compound-literal. */
3650 if (cp_parser_allow_gnu_extensions_p (parser
)
3651 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3653 tree initializer_list
= NULL_TREE
;
3654 bool saved_in_type_id_in_expr_p
;
3656 cp_parser_parse_tentatively (parser
);
3657 /* Consume the `('. */
3658 cp_lexer_consume_token (parser
->lexer
);
3659 /* Parse the type. */
3660 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3661 parser
->in_type_id_in_expr_p
= true;
3662 type
= cp_parser_type_id (parser
);
3663 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3664 /* Look for the `)'. */
3665 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3666 /* Look for the `{'. */
3667 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3668 /* If things aren't going well, there's no need to
3670 if (!cp_parser_error_occurred (parser
))
3672 bool non_constant_p
;
3673 /* Parse the initializer-list. */
3675 = cp_parser_initializer_list (parser
, &non_constant_p
);
3676 /* Allow a trailing `,'. */
3677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3678 cp_lexer_consume_token (parser
->lexer
);
3679 /* Look for the final `}'. */
3680 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3682 /* If that worked, we're definitely looking at a
3683 compound-literal expression. */
3684 if (cp_parser_parse_definitely (parser
))
3686 /* Warn the user that a compound literal is not
3687 allowed in standard C++. */
3689 pedwarn ("ISO C++ forbids compound-literals");
3690 /* Form the representation of the compound-literal. */
3692 = finish_compound_literal (type
, initializer_list
);
3697 /* It must be a primary-expression. */
3698 postfix_expression
= cp_parser_primary_expression (parser
,
3705 /* If we were avoiding committing to the processing of a
3706 qualified-id until we knew whether or not we had a
3707 pointer-to-member, we now know. */
3708 if (qualifying_class
)
3712 /* Peek at the next token. */
3713 token
= cp_lexer_peek_token (parser
->lexer
);
3714 done
= (token
->type
!= CPP_OPEN_SQUARE
3715 && token
->type
!= CPP_OPEN_PAREN
3716 && token
->type
!= CPP_DOT
3717 && token
->type
!= CPP_DEREF
3718 && token
->type
!= CPP_PLUS_PLUS
3719 && token
->type
!= CPP_MINUS_MINUS
);
3721 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3726 return postfix_expression
;
3729 /* Keep looping until the postfix-expression is complete. */
3732 if (idk
== CP_ID_KIND_UNQUALIFIED
3733 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3734 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3735 /* It is not a Koenig lookup function call. */
3737 = unqualified_name_lookup_error (postfix_expression
);
3739 /* Peek at the next token. */
3740 token
= cp_lexer_peek_token (parser
->lexer
);
3742 switch (token
->type
)
3744 case CPP_OPEN_SQUARE
:
3746 = cp_parser_postfix_open_square_expression (parser
,
3749 idk
= CP_ID_KIND_NONE
;
3752 case CPP_OPEN_PAREN
:
3753 /* postfix-expression ( expression-list [opt] ) */
3756 tree args
= (cp_parser_parenthesized_expression_list
3757 (parser
, false, /*non_constant_p=*/NULL
));
3759 if (args
== error_mark_node
)
3761 postfix_expression
= error_mark_node
;
3765 /* Function calls are not permitted in
3766 constant-expressions. */
3767 if (cp_parser_non_integral_constant_expression (parser
,
3770 postfix_expression
= error_mark_node
;
3775 if (idk
== CP_ID_KIND_UNQUALIFIED
)
3777 /* We do not perform argument-dependent lookup if
3778 normal lookup finds a non-function, in accordance
3779 with the expected resolution of DR 218. */
3781 && (is_overloaded_fn (postfix_expression
)
3782 || TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
))
3786 = perform_koenig_lookup (postfix_expression
, args
);
3788 else if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3790 = unqualified_fn_lookup_error (postfix_expression
);
3793 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
3795 tree instance
= TREE_OPERAND (postfix_expression
, 0);
3796 tree fn
= TREE_OPERAND (postfix_expression
, 1);
3798 if (processing_template_decl
3799 && (type_dependent_expression_p (instance
)
3800 || (!BASELINK_P (fn
)
3801 && TREE_CODE (fn
) != FIELD_DECL
)
3802 || type_dependent_expression_p (fn
)
3803 || any_type_dependent_arguments_p (args
)))
3806 = build_min_nt (CALL_EXPR
, postfix_expression
,
3811 if (BASELINK_P (fn
))
3813 = (build_new_method_call
3814 (instance
, fn
, args
, NULL_TREE
,
3815 (idk
== CP_ID_KIND_QUALIFIED
3816 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3819 = finish_call_expr (postfix_expression
, args
,
3820 /*disallow_virtual=*/false,
3821 /*koenig_p=*/false);
3823 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
3824 || TREE_CODE (postfix_expression
) == MEMBER_REF
3825 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
3826 postfix_expression
= (build_offset_ref_call_from_tree
3827 (postfix_expression
, args
));
3828 else if (idk
== CP_ID_KIND_QUALIFIED
)
3829 /* A call to a static class member, or a namespace-scope
3832 = finish_call_expr (postfix_expression
, args
,
3833 /*disallow_virtual=*/true,
3836 /* All other function calls. */
3838 = finish_call_expr (postfix_expression
, args
,
3839 /*disallow_virtual=*/false,
3842 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3843 idk
= CP_ID_KIND_NONE
;
3849 /* postfix-expression . template [opt] id-expression
3850 postfix-expression . pseudo-destructor-name
3851 postfix-expression -> template [opt] id-expression
3852 postfix-expression -> pseudo-destructor-name */
3854 /* Consume the `.' or `->' operator. */
3855 cp_lexer_consume_token (parser
->lexer
);
3858 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
3864 /* postfix-expression ++ */
3865 /* Consume the `++' token. */
3866 cp_lexer_consume_token (parser
->lexer
);
3867 /* Generate a representation for the complete expression. */
3869 = finish_increment_expr (postfix_expression
,
3870 POSTINCREMENT_EXPR
);
3871 /* Increments may not appear in constant-expressions. */
3872 if (cp_parser_non_integral_constant_expression (parser
,
3874 postfix_expression
= error_mark_node
;
3875 idk
= CP_ID_KIND_NONE
;
3878 case CPP_MINUS_MINUS
:
3879 /* postfix-expression -- */
3880 /* Consume the `--' token. */
3881 cp_lexer_consume_token (parser
->lexer
);
3882 /* Generate a representation for the complete expression. */
3884 = finish_increment_expr (postfix_expression
,
3885 POSTDECREMENT_EXPR
);
3886 /* Decrements may not appear in constant-expressions. */
3887 if (cp_parser_non_integral_constant_expression (parser
,
3889 postfix_expression
= error_mark_node
;
3890 idk
= CP_ID_KIND_NONE
;
3894 return postfix_expression
;
3898 /* We should never get here. */
3900 return error_mark_node
;
3903 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
3904 by cp_parser_builtin_offsetof. We're looking for
3906 postfix-expression [ expression ]
3908 FOR_OFFSETOF is set if we're being called in that context, which
3909 changes how we deal with integer constant expressions. */
3912 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
3913 tree postfix_expression
,
3918 /* Consume the `[' token. */
3919 cp_lexer_consume_token (parser
->lexer
);
3921 /* Parse the index expression. */
3922 /* ??? For offsetof, there is a question of what to allow here. If
3923 offsetof is not being used in an integral constant expression context,
3924 then we *could* get the right answer by computing the value at runtime.
3925 If we are in an integral constant expression context, then we might
3926 could accept any constant expression; hard to say without analysis.
3927 Rather than open the barn door too wide right away, allow only integer
3928 constant expresions here. */
3930 index
= cp_parser_constant_expression (parser
, false, NULL
);
3932 index
= cp_parser_expression (parser
);
3934 /* Look for the closing `]'. */
3935 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
3937 /* Build the ARRAY_REF. */
3938 postfix_expression
= grok_array_decl (postfix_expression
, index
);
3940 /* When not doing offsetof, array references are not permitted in
3941 constant-expressions. */
3943 && (cp_parser_non_integral_constant_expression
3944 (parser
, "an array reference")))
3945 postfix_expression
= error_mark_node
;
3947 return postfix_expression
;
3950 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
3951 by cp_parser_builtin_offsetof. We're looking for
3953 postfix-expression . template [opt] id-expression
3954 postfix-expression . pseudo-destructor-name
3955 postfix-expression -> template [opt] id-expression
3956 postfix-expression -> pseudo-destructor-name
3958 FOR_OFFSETOF is set if we're being called in that context. That sorta
3959 limits what of the above we'll actually accept, but nevermind.
3960 TOKEN_TYPE is the "." or "->" token, which will already have been
3961 removed from the stream. */
3964 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
3965 enum cpp_ttype token_type
,
3966 tree postfix_expression
,
3967 bool for_offsetof
, cp_id_kind
*idk
)
3972 tree scope
= NULL_TREE
;
3974 /* If this is a `->' operator, dereference the pointer. */
3975 if (token_type
== CPP_DEREF
)
3976 postfix_expression
= build_x_arrow (postfix_expression
);
3977 /* Check to see whether or not the expression is type-dependent. */
3978 dependent_p
= type_dependent_expression_p (postfix_expression
);
3979 /* The identifier following the `->' or `.' is not qualified. */
3980 parser
->scope
= NULL_TREE
;
3981 parser
->qualifying_scope
= NULL_TREE
;
3982 parser
->object_scope
= NULL_TREE
;
3983 *idk
= CP_ID_KIND_NONE
;
3984 /* Enter the scope corresponding to the type of the object
3985 given by the POSTFIX_EXPRESSION. */
3986 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
3988 scope
= TREE_TYPE (postfix_expression
);
3989 /* According to the standard, no expression should ever have
3990 reference type. Unfortunately, we do not currently match
3991 the standard in this respect in that our internal representation
3992 of an expression may have reference type even when the standard
3993 says it does not. Therefore, we have to manually obtain the
3994 underlying type here. */
3995 scope
= non_reference (scope
);
3996 /* The type of the POSTFIX_EXPRESSION must be complete. */
3997 scope
= complete_type_or_else (scope
, NULL_TREE
);
3998 /* Let the name lookup machinery know that we are processing a
3999 class member access expression. */
4000 parser
->context
->object_type
= scope
;
4001 /* If something went wrong, we want to be able to discern that case,
4002 as opposed to the case where there was no SCOPE due to the type
4003 of expression being dependent. */
4005 scope
= error_mark_node
;
4006 /* If the SCOPE was erroneous, make the various semantic analysis
4007 functions exit quickly -- and without issuing additional error
4009 if (scope
== error_mark_node
)
4010 postfix_expression
= error_mark_node
;
4013 /* If the SCOPE is not a scalar type, we are looking at an
4014 ordinary class member access expression, rather than a
4015 pseudo-destructor-name. */
4016 if (!scope
|| !SCALAR_TYPE_P (scope
))
4018 template_p
= cp_parser_optional_template_keyword (parser
);
4019 /* Parse the id-expression. */
4020 name
= cp_parser_id_expression (parser
, template_p
,
4021 /*check_dependency_p=*/true,
4022 /*template_p=*/NULL
,
4023 /*declarator_p=*/false);
4024 /* In general, build a SCOPE_REF if the member name is qualified.
4025 However, if the name was not dependent and has already been
4026 resolved; there is no need to build the SCOPE_REF. For example;
4028 struct X { void f(); };
4029 template <typename T> void f(T* t) { t->X::f(); }
4031 Even though "t" is dependent, "X::f" is not and has been resolved
4032 to a BASELINK; there is no need to include scope information. */
4034 /* But we do need to remember that there was an explicit scope for
4035 virtual function calls. */
4037 *idk
= CP_ID_KIND_QUALIFIED
;
4039 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4041 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
4042 parser
->scope
= NULL_TREE
;
4043 parser
->qualifying_scope
= NULL_TREE
;
4044 parser
->object_scope
= NULL_TREE
;
4046 if (scope
&& name
&& BASELINK_P (name
))
4047 adjust_result_of_qualified_name_lookup
4048 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4050 = finish_class_member_access_expr (postfix_expression
, name
);
4052 /* Otherwise, try the pseudo-destructor-name production. */
4058 /* Parse the pseudo-destructor-name. */
4059 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4060 /* Form the call. */
4062 = finish_pseudo_destructor_expr (postfix_expression
,
4063 s
, TREE_TYPE (type
));
4066 /* We no longer need to look up names in the scope of the object on
4067 the left-hand side of the `.' or `->' operator. */
4068 parser
->context
->object_type
= NULL_TREE
;
4070 /* Outside of offsetof, these operators may not appear in
4071 constant-expressions. */
4073 && (cp_parser_non_integral_constant_expression
4074 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4075 postfix_expression
= error_mark_node
;
4077 return postfix_expression
;
4080 /* Parse a parenthesized expression-list.
4083 assignment-expression
4084 expression-list, assignment-expression
4089 identifier, expression-list
4091 Returns a TREE_LIST. The TREE_VALUE of each node is a
4092 representation of an assignment-expression. Note that a TREE_LIST
4093 is returned even if there is only a single expression in the list.
4094 error_mark_node is returned if the ( and or ) are
4095 missing. NULL_TREE is returned on no expressions. The parentheses
4096 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4097 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4098 indicates whether or not all of the expressions in the list were
4102 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4103 bool is_attribute_list
,
4104 bool *non_constant_p
)
4106 tree expression_list
= NULL_TREE
;
4107 tree identifier
= NULL_TREE
;
4109 /* Assume all the expressions will be constant. */
4111 *non_constant_p
= false;
4113 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4114 return error_mark_node
;
4116 /* Consume expressions until there are no more. */
4117 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4122 /* At the beginning of attribute lists, check to see if the
4123 next token is an identifier. */
4124 if (is_attribute_list
4125 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4129 /* Consume the identifier. */
4130 token
= cp_lexer_consume_token (parser
->lexer
);
4131 /* Save the identifier. */
4132 identifier
= token
->value
;
4136 /* Parse the next assignment-expression. */
4139 bool expr_non_constant_p
;
4140 expr
= (cp_parser_constant_expression
4141 (parser
, /*allow_non_constant_p=*/true,
4142 &expr_non_constant_p
));
4143 if (expr_non_constant_p
)
4144 *non_constant_p
= true;
4147 expr
= cp_parser_assignment_expression (parser
);
4149 /* Add it to the list. We add error_mark_node
4150 expressions to the list, so that we can still tell if
4151 the correct form for a parenthesized expression-list
4152 is found. That gives better errors. */
4153 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4155 if (expr
== error_mark_node
)
4159 /* After the first item, attribute lists look the same as
4160 expression lists. */
4161 is_attribute_list
= false;
4164 /* If the next token isn't a `,', then we are done. */
4165 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4168 /* Otherwise, consume the `,' and keep going. */
4169 cp_lexer_consume_token (parser
->lexer
);
4172 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4177 /* We try and resync to an unnested comma, as that will give the
4178 user better diagnostics. */
4179 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4180 /*recovering=*/true,
4182 /*consume_paren=*/true);
4186 return error_mark_node
;
4189 /* We built up the list in reverse order so we must reverse it now. */
4190 expression_list
= nreverse (expression_list
);
4192 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4194 return expression_list
;
4197 /* Parse a pseudo-destructor-name.
4199 pseudo-destructor-name:
4200 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4201 :: [opt] nested-name-specifier template template-id :: ~ type-name
4202 :: [opt] nested-name-specifier [opt] ~ type-name
4204 If either of the first two productions is used, sets *SCOPE to the
4205 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4206 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4207 or ERROR_MARK_NODE if the parse fails. */
4210 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4214 bool nested_name_specifier_p
;
4216 /* Look for the optional `::' operator. */
4217 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4218 /* Look for the optional nested-name-specifier. */
4219 nested_name_specifier_p
4220 = (cp_parser_nested_name_specifier_opt (parser
,
4221 /*typename_keyword_p=*/false,
4222 /*check_dependency_p=*/true,
4224 /*is_declaration=*/true)
4226 /* Now, if we saw a nested-name-specifier, we might be doing the
4227 second production. */
4228 if (nested_name_specifier_p
4229 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4231 /* Consume the `template' keyword. */
4232 cp_lexer_consume_token (parser
->lexer
);
4233 /* Parse the template-id. */
4234 cp_parser_template_id (parser
,
4235 /*template_keyword_p=*/true,
4236 /*check_dependency_p=*/false,
4237 /*is_declaration=*/true);
4238 /* Look for the `::' token. */
4239 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4241 /* If the next token is not a `~', then there might be some
4242 additional qualification. */
4243 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4245 /* Look for the type-name. */
4246 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4248 /* If we didn't get an aggregate type, or we don't have ::~,
4249 then something has gone wrong. Since the only caller of this
4250 function is looking for something after `.' or `->' after a
4251 scalar type, most likely the program is trying to get a
4252 member of a non-aggregate type. */
4253 if (*scope
== error_mark_node
4254 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4255 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4257 cp_parser_error (parser
, "request for member of non-aggregate type");
4258 *type
= error_mark_node
;
4262 /* Look for the `::' token. */
4263 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4268 /* Look for the `~'. */
4269 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4270 /* Look for the type-name again. We are not responsible for
4271 checking that it matches the first type-name. */
4272 *type
= cp_parser_type_name (parser
);
4275 /* Parse a unary-expression.
4281 unary-operator cast-expression
4282 sizeof unary-expression
4290 __extension__ cast-expression
4291 __alignof__ unary-expression
4292 __alignof__ ( type-id )
4293 __real__ cast-expression
4294 __imag__ cast-expression
4297 ADDRESS_P is true iff the unary-expression is appearing as the
4298 operand of the `&' operator.
4300 Returns a representation of the expression. */
4303 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4306 enum tree_code unary_operator
;
4308 /* Peek at the next token. */
4309 token
= cp_lexer_peek_token (parser
->lexer
);
4310 /* Some keywords give away the kind of expression. */
4311 if (token
->type
== CPP_KEYWORD
)
4313 enum rid keyword
= token
->keyword
;
4323 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4324 /* Consume the token. */
4325 cp_lexer_consume_token (parser
->lexer
);
4326 /* Parse the operand. */
4327 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4329 if (TYPE_P (operand
))
4330 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4332 return cxx_sizeof_or_alignof_expr (operand
, op
);
4336 return cp_parser_new_expression (parser
);
4339 return cp_parser_delete_expression (parser
);
4343 /* The saved value of the PEDANTIC flag. */
4347 /* Save away the PEDANTIC flag. */
4348 cp_parser_extension_opt (parser
, &saved_pedantic
);
4349 /* Parse the cast-expression. */
4350 expr
= cp_parser_simple_cast_expression (parser
);
4351 /* Restore the PEDANTIC flag. */
4352 pedantic
= saved_pedantic
;
4362 /* Consume the `__real__' or `__imag__' token. */
4363 cp_lexer_consume_token (parser
->lexer
);
4364 /* Parse the cast-expression. */
4365 expression
= cp_parser_simple_cast_expression (parser
);
4366 /* Create the complete representation. */
4367 return build_x_unary_op ((keyword
== RID_REALPART
4368 ? REALPART_EXPR
: IMAGPART_EXPR
),
4378 /* Look for the `:: new' and `:: delete', which also signal the
4379 beginning of a new-expression, or delete-expression,
4380 respectively. If the next token is `::', then it might be one of
4382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4386 /* See if the token after the `::' is one of the keywords in
4387 which we're interested. */
4388 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4389 /* If it's `new', we have a new-expression. */
4390 if (keyword
== RID_NEW
)
4391 return cp_parser_new_expression (parser
);
4392 /* Similarly, for `delete'. */
4393 else if (keyword
== RID_DELETE
)
4394 return cp_parser_delete_expression (parser
);
4397 /* Look for a unary operator. */
4398 unary_operator
= cp_parser_unary_operator (token
);
4399 /* The `++' and `--' operators can be handled similarly, even though
4400 they are not technically unary-operators in the grammar. */
4401 if (unary_operator
== ERROR_MARK
)
4403 if (token
->type
== CPP_PLUS_PLUS
)
4404 unary_operator
= PREINCREMENT_EXPR
;
4405 else if (token
->type
== CPP_MINUS_MINUS
)
4406 unary_operator
= PREDECREMENT_EXPR
;
4407 /* Handle the GNU address-of-label extension. */
4408 else if (cp_parser_allow_gnu_extensions_p (parser
)
4409 && token
->type
== CPP_AND_AND
)
4413 /* Consume the '&&' token. */
4414 cp_lexer_consume_token (parser
->lexer
);
4415 /* Look for the identifier. */
4416 identifier
= cp_parser_identifier (parser
);
4417 /* Create an expression representing the address. */
4418 return finish_label_address_expr (identifier
);
4421 if (unary_operator
!= ERROR_MARK
)
4423 tree cast_expression
;
4424 tree expression
= error_mark_node
;
4425 const char *non_constant_p
= NULL
;
4427 /* Consume the operator token. */
4428 token
= cp_lexer_consume_token (parser
->lexer
);
4429 /* Parse the cast-expression. */
4431 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4432 /* Now, build an appropriate representation. */
4433 switch (unary_operator
)
4436 non_constant_p
= "`*'";
4437 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4441 non_constant_p
= "`&'";
4444 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4447 case PREINCREMENT_EXPR
:
4448 case PREDECREMENT_EXPR
:
4449 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4454 case TRUTH_NOT_EXPR
:
4455 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4463 && cp_parser_non_integral_constant_expression (parser
,
4465 expression
= error_mark_node
;
4470 return cp_parser_postfix_expression (parser
, address_p
);
4473 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4474 unary-operator, the corresponding tree code is returned. */
4476 static enum tree_code
4477 cp_parser_unary_operator (cp_token
* token
)
4479 switch (token
->type
)
4482 return INDIRECT_REF
;
4488 return CONVERT_EXPR
;
4494 return TRUTH_NOT_EXPR
;
4497 return BIT_NOT_EXPR
;
4504 /* Parse a new-expression.
4507 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4508 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4510 Returns a representation of the expression. */
4513 cp_parser_new_expression (cp_parser
* parser
)
4515 bool global_scope_p
;
4520 /* Look for the optional `::' operator. */
4522 = (cp_parser_global_scope_opt (parser
,
4523 /*current_scope_valid_p=*/false)
4525 /* Look for the `new' operator. */
4526 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4527 /* There's no easy way to tell a new-placement from the
4528 `( type-id )' construct. */
4529 cp_parser_parse_tentatively (parser
);
4530 /* Look for a new-placement. */
4531 placement
= cp_parser_new_placement (parser
);
4532 /* If that didn't work out, there's no new-placement. */
4533 if (!cp_parser_parse_definitely (parser
))
4534 placement
= NULL_TREE
;
4536 /* If the next token is a `(', then we have a parenthesized
4538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4540 /* Consume the `('. */
4541 cp_lexer_consume_token (parser
->lexer
);
4542 /* Parse the type-id. */
4543 type
= cp_parser_type_id (parser
);
4544 /* Look for the closing `)'. */
4545 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4546 /* There should not be a direct-new-declarator in this production,
4547 but GCC used to allowed this, so we check and emit a sensible error
4548 message for this case. */
4549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4551 error ("array bound forbidden after parenthesized type-id");
4552 inform ("try removing the parentheses around the type-id");
4553 cp_parser_direct_new_declarator (parser
);
4556 /* Otherwise, there must be a new-type-id. */
4558 type
= cp_parser_new_type_id (parser
);
4560 /* If the next token is a `(', then we have a new-initializer. */
4561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4562 initializer
= cp_parser_new_initializer (parser
);
4564 initializer
= NULL_TREE
;
4566 /* A new-expression may not appear in an integral constant
4568 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
4569 return error_mark_node
;
4571 /* Create a representation of the new-expression. */
4572 return build_new (placement
, type
, initializer
, global_scope_p
);
4575 /* Parse a new-placement.
4580 Returns the same representation as for an expression-list. */
4583 cp_parser_new_placement (cp_parser
* parser
)
4585 tree expression_list
;
4587 /* Parse the expression-list. */
4588 expression_list
= (cp_parser_parenthesized_expression_list
4589 (parser
, false, /*non_constant_p=*/NULL
));
4591 return expression_list
;
4594 /* Parse a new-type-id.
4597 type-specifier-seq new-declarator [opt]
4599 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4600 and whose TREE_VALUE is the new-declarator. */
4603 cp_parser_new_type_id (cp_parser
* parser
)
4605 tree type_specifier_seq
;
4607 const char *saved_message
;
4609 /* The type-specifier sequence must not contain type definitions.
4610 (It cannot contain declarations of new types either, but if they
4611 are not definitions we will catch that because they are not
4613 saved_message
= parser
->type_definition_forbidden_message
;
4614 parser
->type_definition_forbidden_message
4615 = "types may not be defined in a new-type-id";
4616 /* Parse the type-specifier-seq. */
4617 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4618 /* Restore the old message. */
4619 parser
->type_definition_forbidden_message
= saved_message
;
4620 /* Parse the new-declarator. */
4621 declarator
= cp_parser_new_declarator_opt (parser
);
4623 return build_tree_list (type_specifier_seq
, declarator
);
4626 /* Parse an (optional) new-declarator.
4629 ptr-operator new-declarator [opt]
4630 direct-new-declarator
4632 Returns a representation of the declarator. See
4633 cp_parser_declarator for the representations used. */
4636 cp_parser_new_declarator_opt (cp_parser
* parser
)
4638 enum tree_code code
;
4640 tree cv_qualifier_seq
;
4642 /* We don't know if there's a ptr-operator next, or not. */
4643 cp_parser_parse_tentatively (parser
);
4644 /* Look for a ptr-operator. */
4645 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4646 /* If that worked, look for more new-declarators. */
4647 if (cp_parser_parse_definitely (parser
))
4651 /* Parse another optional declarator. */
4652 declarator
= cp_parser_new_declarator_opt (parser
);
4654 /* Create the representation of the declarator. */
4655 if (code
== INDIRECT_REF
)
4656 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4659 declarator
= make_reference_declarator (cv_qualifier_seq
,
4662 /* Handle the pointer-to-member case. */
4664 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4669 /* If the next token is a `[', there is a direct-new-declarator. */
4670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4671 return cp_parser_direct_new_declarator (parser
);
4676 /* Parse a direct-new-declarator.
4678 direct-new-declarator:
4680 direct-new-declarator [constant-expression]
4682 Returns an ARRAY_REF, following the same conventions as are
4683 documented for cp_parser_direct_declarator. */
4686 cp_parser_direct_new_declarator (cp_parser
* parser
)
4688 tree declarator
= NULL_TREE
;
4694 /* Look for the opening `['. */
4695 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4696 /* The first expression is not required to be constant. */
4699 expression
= cp_parser_expression (parser
);
4700 /* The standard requires that the expression have integral
4701 type. DR 74 adds enumeration types. We believe that the
4702 real intent is that these expressions be handled like the
4703 expression in a `switch' condition, which also allows
4704 classes with a single conversion to integral or
4705 enumeration type. */
4706 if (!processing_template_decl
)
4709 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4714 error ("expression in new-declarator must have integral or enumeration type");
4715 expression
= error_mark_node
;
4719 /* But all the other expressions must be. */
4722 = cp_parser_constant_expression (parser
,
4723 /*allow_non_constant=*/false,
4725 /* Look for the closing `]'. */
4726 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4728 /* Add this bound to the declarator. */
4729 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4731 /* If the next token is not a `[', then there are no more
4733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4740 /* Parse a new-initializer.
4743 ( expression-list [opt] )
4745 Returns a representation of the expression-list. If there is no
4746 expression-list, VOID_ZERO_NODE is returned. */
4749 cp_parser_new_initializer (cp_parser
* parser
)
4751 tree expression_list
;
4753 expression_list
= (cp_parser_parenthesized_expression_list
4754 (parser
, false, /*non_constant_p=*/NULL
));
4755 if (!expression_list
)
4756 expression_list
= void_zero_node
;
4758 return expression_list
;
4761 /* Parse a delete-expression.
4764 :: [opt] delete cast-expression
4765 :: [opt] delete [ ] cast-expression
4767 Returns a representation of the expression. */
4770 cp_parser_delete_expression (cp_parser
* parser
)
4772 bool global_scope_p
;
4776 /* Look for the optional `::' operator. */
4778 = (cp_parser_global_scope_opt (parser
,
4779 /*current_scope_valid_p=*/false)
4781 /* Look for the `delete' keyword. */
4782 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4783 /* See if the array syntax is in use. */
4784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4786 /* Consume the `[' token. */
4787 cp_lexer_consume_token (parser
->lexer
);
4788 /* Look for the `]' token. */
4789 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4790 /* Remember that this is the `[]' construct. */
4796 /* Parse the cast-expression. */
4797 expression
= cp_parser_simple_cast_expression (parser
);
4799 /* A delete-expression may not appear in an integral constant
4801 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
4802 return error_mark_node
;
4804 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4807 /* Parse a cast-expression.
4811 ( type-id ) cast-expression
4813 Returns a representation of the expression. */
4816 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4818 /* If it's a `(', then we might be looking at a cast. */
4819 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4821 tree type
= NULL_TREE
;
4822 tree expr
= NULL_TREE
;
4823 bool compound_literal_p
;
4824 const char *saved_message
;
4826 /* There's no way to know yet whether or not this is a cast.
4827 For example, `(int (3))' is a unary-expression, while `(int)
4828 3' is a cast. So, we resort to parsing tentatively. */
4829 cp_parser_parse_tentatively (parser
);
4830 /* Types may not be defined in a cast. */
4831 saved_message
= parser
->type_definition_forbidden_message
;
4832 parser
->type_definition_forbidden_message
4833 = "types may not be defined in casts";
4834 /* Consume the `('. */
4835 cp_lexer_consume_token (parser
->lexer
);
4836 /* A very tricky bit is that `(struct S) { 3 }' is a
4837 compound-literal (which we permit in C++ as an extension).
4838 But, that construct is not a cast-expression -- it is a
4839 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4840 is legal; if the compound-literal were a cast-expression,
4841 you'd need an extra set of parentheses.) But, if we parse
4842 the type-id, and it happens to be a class-specifier, then we
4843 will commit to the parse at that point, because we cannot
4844 undo the action that is done when creating a new class. So,
4845 then we cannot back up and do a postfix-expression.
4847 Therefore, we scan ahead to the closing `)', and check to see
4848 if the token after the `)' is a `{'. If so, we are not
4849 looking at a cast-expression.
4851 Save tokens so that we can put them back. */
4852 cp_lexer_save_tokens (parser
->lexer
);
4853 /* Skip tokens until the next token is a closing parenthesis.
4854 If we find the closing `)', and the next token is a `{', then
4855 we are looking at a compound-literal. */
4857 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
4858 /*consume_paren=*/true)
4859 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4860 /* Roll back the tokens we skipped. */
4861 cp_lexer_rollback_tokens (parser
->lexer
);
4862 /* If we were looking at a compound-literal, simulate an error
4863 so that the call to cp_parser_parse_definitely below will
4865 if (compound_literal_p
)
4866 cp_parser_simulate_error (parser
);
4869 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4870 parser
->in_type_id_in_expr_p
= true;
4871 /* Look for the type-id. */
4872 type
= cp_parser_type_id (parser
);
4873 /* Look for the closing `)'. */
4874 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4875 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4878 /* Restore the saved message. */
4879 parser
->type_definition_forbidden_message
= saved_message
;
4881 /* If ok so far, parse the dependent expression. We cannot be
4882 sure it is a cast. Consider `(T ())'. It is a parenthesized
4883 ctor of T, but looks like a cast to function returning T
4884 without a dependent expression. */
4885 if (!cp_parser_error_occurred (parser
))
4886 expr
= cp_parser_simple_cast_expression (parser
);
4888 if (cp_parser_parse_definitely (parser
))
4890 /* Warn about old-style casts, if so requested. */
4891 if (warn_old_style_cast
4892 && !in_system_header
4893 && !VOID_TYPE_P (type
)
4894 && current_lang_name
!= lang_name_c
)
4895 warning ("use of old-style cast");
4897 /* Only type conversions to integral or enumeration types
4898 can be used in constant-expressions. */
4899 if (parser
->integral_constant_expression_p
4900 && !dependent_type_p (type
)
4901 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
4902 && (cp_parser_non_integral_constant_expression
4904 "a cast to a type other than an integral or "
4905 "enumeration type")))
4906 return error_mark_node
;
4908 /* Perform the cast. */
4909 expr
= build_c_cast (type
, expr
);
4914 /* If we get here, then it's not a cast, so it must be a
4915 unary-expression. */
4916 return cp_parser_unary_expression (parser
, address_p
);
4919 /* Parse a pm-expression.
4923 pm-expression .* cast-expression
4924 pm-expression ->* cast-expression
4926 Returns a representation of the expression. */
4929 cp_parser_pm_expression (cp_parser
* parser
)
4931 static const cp_parser_token_tree_map map
= {
4932 { CPP_DEREF_STAR
, MEMBER_REF
},
4933 { CPP_DOT_STAR
, DOTSTAR_EXPR
},
4934 { CPP_EOF
, ERROR_MARK
}
4937 return cp_parser_binary_expression (parser
, map
,
4938 cp_parser_simple_cast_expression
);
4941 /* Parse a multiplicative-expression.
4943 multiplicative-expression:
4945 multiplicative-expression * pm-expression
4946 multiplicative-expression / pm-expression
4947 multiplicative-expression % pm-expression
4949 Returns a representation of the expression. */
4952 cp_parser_multiplicative_expression (cp_parser
* parser
)
4954 static const cp_parser_token_tree_map map
= {
4955 { CPP_MULT
, MULT_EXPR
},
4956 { CPP_DIV
, TRUNC_DIV_EXPR
},
4957 { CPP_MOD
, TRUNC_MOD_EXPR
},
4958 { CPP_EOF
, ERROR_MARK
}
4961 return cp_parser_binary_expression (parser
,
4963 cp_parser_pm_expression
);
4966 /* Parse an additive-expression.
4968 additive-expression:
4969 multiplicative-expression
4970 additive-expression + multiplicative-expression
4971 additive-expression - multiplicative-expression
4973 Returns a representation of the expression. */
4976 cp_parser_additive_expression (cp_parser
* parser
)
4978 static const cp_parser_token_tree_map map
= {
4979 { CPP_PLUS
, PLUS_EXPR
},
4980 { CPP_MINUS
, MINUS_EXPR
},
4981 { CPP_EOF
, ERROR_MARK
}
4984 return cp_parser_binary_expression (parser
,
4986 cp_parser_multiplicative_expression
);
4989 /* Parse a shift-expression.
4993 shift-expression << additive-expression
4994 shift-expression >> additive-expression
4996 Returns a representation of the expression. */
4999 cp_parser_shift_expression (cp_parser
* parser
)
5001 static const cp_parser_token_tree_map map
= {
5002 { CPP_LSHIFT
, LSHIFT_EXPR
},
5003 { CPP_RSHIFT
, RSHIFT_EXPR
},
5004 { CPP_EOF
, ERROR_MARK
}
5007 return cp_parser_binary_expression (parser
,
5009 cp_parser_additive_expression
);
5012 /* Parse a relational-expression.
5014 relational-expression:
5016 relational-expression < shift-expression
5017 relational-expression > shift-expression
5018 relational-expression <= shift-expression
5019 relational-expression >= shift-expression
5023 relational-expression:
5024 relational-expression <? shift-expression
5025 relational-expression >? shift-expression
5027 Returns a representation of the expression. */
5030 cp_parser_relational_expression (cp_parser
* parser
)
5032 static const cp_parser_token_tree_map map
= {
5033 { CPP_LESS
, LT_EXPR
},
5034 { CPP_GREATER
, GT_EXPR
},
5035 { CPP_LESS_EQ
, LE_EXPR
},
5036 { CPP_GREATER_EQ
, GE_EXPR
},
5037 { CPP_MIN
, MIN_EXPR
},
5038 { CPP_MAX
, MAX_EXPR
},
5039 { CPP_EOF
, ERROR_MARK
}
5042 return cp_parser_binary_expression (parser
,
5044 cp_parser_shift_expression
);
5047 /* Parse an equality-expression.
5049 equality-expression:
5050 relational-expression
5051 equality-expression == relational-expression
5052 equality-expression != relational-expression
5054 Returns a representation of the expression. */
5057 cp_parser_equality_expression (cp_parser
* parser
)
5059 static const cp_parser_token_tree_map map
= {
5060 { CPP_EQ_EQ
, EQ_EXPR
},
5061 { CPP_NOT_EQ
, NE_EXPR
},
5062 { CPP_EOF
, ERROR_MARK
}
5065 return cp_parser_binary_expression (parser
,
5067 cp_parser_relational_expression
);
5070 /* Parse an and-expression.
5074 and-expression & equality-expression
5076 Returns a representation of the expression. */
5079 cp_parser_and_expression (cp_parser
* parser
)
5081 static const cp_parser_token_tree_map map
= {
5082 { CPP_AND
, BIT_AND_EXPR
},
5083 { CPP_EOF
, ERROR_MARK
}
5086 return cp_parser_binary_expression (parser
,
5088 cp_parser_equality_expression
);
5091 /* Parse an exclusive-or-expression.
5093 exclusive-or-expression:
5095 exclusive-or-expression ^ and-expression
5097 Returns a representation of the expression. */
5100 cp_parser_exclusive_or_expression (cp_parser
* parser
)
5102 static const cp_parser_token_tree_map map
= {
5103 { CPP_XOR
, BIT_XOR_EXPR
},
5104 { CPP_EOF
, ERROR_MARK
}
5107 return cp_parser_binary_expression (parser
,
5109 cp_parser_and_expression
);
5113 /* Parse an inclusive-or-expression.
5115 inclusive-or-expression:
5116 exclusive-or-expression
5117 inclusive-or-expression | exclusive-or-expression
5119 Returns a representation of the expression. */
5122 cp_parser_inclusive_or_expression (cp_parser
* parser
)
5124 static const cp_parser_token_tree_map map
= {
5125 { CPP_OR
, BIT_IOR_EXPR
},
5126 { CPP_EOF
, ERROR_MARK
}
5129 return cp_parser_binary_expression (parser
,
5131 cp_parser_exclusive_or_expression
);
5134 /* Parse a logical-and-expression.
5136 logical-and-expression:
5137 inclusive-or-expression
5138 logical-and-expression && inclusive-or-expression
5140 Returns a representation of the expression. */
5143 cp_parser_logical_and_expression (cp_parser
* parser
)
5145 static const cp_parser_token_tree_map map
= {
5146 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
5147 { CPP_EOF
, ERROR_MARK
}
5150 return cp_parser_binary_expression (parser
,
5152 cp_parser_inclusive_or_expression
);
5155 /* Parse a logical-or-expression.
5157 logical-or-expression:
5158 logical-and-expression
5159 logical-or-expression || logical-and-expression
5161 Returns a representation of the expression. */
5164 cp_parser_logical_or_expression (cp_parser
* parser
)
5166 static const cp_parser_token_tree_map map
= {
5167 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
5168 { CPP_EOF
, ERROR_MARK
}
5171 return cp_parser_binary_expression (parser
,
5173 cp_parser_logical_and_expression
);
5176 /* Parse the `? expression : assignment-expression' part of a
5177 conditional-expression. The LOGICAL_OR_EXPR is the
5178 logical-or-expression that started the conditional-expression.
5179 Returns a representation of the entire conditional-expression.
5181 This routine is used by cp_parser_assignment_expression.
5183 ? expression : assignment-expression
5187 ? : assignment-expression */
5190 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5193 tree assignment_expr
;
5195 /* Consume the `?' token. */
5196 cp_lexer_consume_token (parser
->lexer
);
5197 if (cp_parser_allow_gnu_extensions_p (parser
)
5198 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5199 /* Implicit true clause. */
5202 /* Parse the expression. */
5203 expr
= cp_parser_expression (parser
);
5205 /* The next token should be a `:'. */
5206 cp_parser_require (parser
, CPP_COLON
, "`:'");
5207 /* Parse the assignment-expression. */
5208 assignment_expr
= cp_parser_assignment_expression (parser
);
5210 /* Build the conditional-expression. */
5211 return build_x_conditional_expr (logical_or_expr
,
5216 /* Parse an assignment-expression.
5218 assignment-expression:
5219 conditional-expression
5220 logical-or-expression assignment-operator assignment_expression
5223 Returns a representation for the expression. */
5226 cp_parser_assignment_expression (cp_parser
* parser
)
5230 /* If the next token is the `throw' keyword, then we're looking at
5231 a throw-expression. */
5232 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5233 expr
= cp_parser_throw_expression (parser
);
5234 /* Otherwise, it must be that we are looking at a
5235 logical-or-expression. */
5238 /* Parse the logical-or-expression. */
5239 expr
= cp_parser_logical_or_expression (parser
);
5240 /* If the next token is a `?' then we're actually looking at a
5241 conditional-expression. */
5242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5243 return cp_parser_question_colon_clause (parser
, expr
);
5246 enum tree_code assignment_operator
;
5248 /* If it's an assignment-operator, we're using the second
5251 = cp_parser_assignment_operator_opt (parser
);
5252 if (assignment_operator
!= ERROR_MARK
)
5256 /* Parse the right-hand side of the assignment. */
5257 rhs
= cp_parser_assignment_expression (parser
);
5258 /* An assignment may not appear in a
5259 constant-expression. */
5260 if (cp_parser_non_integral_constant_expression (parser
,
5262 return error_mark_node
;
5263 /* Build the assignment expression. */
5264 expr
= build_x_modify_expr (expr
,
5265 assignment_operator
,
5274 /* Parse an (optional) assignment-operator.
5276 assignment-operator: one of
5277 = *= /= %= += -= >>= <<= &= ^= |=
5281 assignment-operator: one of
5284 If the next token is an assignment operator, the corresponding tree
5285 code is returned, and the token is consumed. For example, for
5286 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5287 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5288 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5289 operator, ERROR_MARK is returned. */
5291 static enum tree_code
5292 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5297 /* Peek at the next toen. */
5298 token
= cp_lexer_peek_token (parser
->lexer
);
5300 switch (token
->type
)
5311 op
= TRUNC_DIV_EXPR
;
5315 op
= TRUNC_MOD_EXPR
;
5355 /* Nothing else is an assignment operator. */
5359 /* If it was an assignment operator, consume it. */
5360 if (op
!= ERROR_MARK
)
5361 cp_lexer_consume_token (parser
->lexer
);
5366 /* Parse an expression.
5369 assignment-expression
5370 expression , assignment-expression
5372 Returns a representation of the expression. */
5375 cp_parser_expression (cp_parser
* parser
)
5377 tree expression
= NULL_TREE
;
5381 tree assignment_expression
;
5383 /* Parse the next assignment-expression. */
5384 assignment_expression
5385 = cp_parser_assignment_expression (parser
);
5386 /* If this is the first assignment-expression, we can just
5389 expression
= assignment_expression
;
5391 expression
= build_x_compound_expr (expression
,
5392 assignment_expression
);
5393 /* If the next token is not a comma, then we are done with the
5395 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5397 /* Consume the `,'. */
5398 cp_lexer_consume_token (parser
->lexer
);
5399 /* A comma operator cannot appear in a constant-expression. */
5400 if (cp_parser_non_integral_constant_expression (parser
,
5401 "a comma operator"))
5402 expression
= error_mark_node
;
5408 /* Parse a constant-expression.
5410 constant-expression:
5411 conditional-expression
5413 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5414 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5415 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5416 is false, NON_CONSTANT_P should be NULL. */
5419 cp_parser_constant_expression (cp_parser
* parser
,
5420 bool allow_non_constant_p
,
5421 bool *non_constant_p
)
5423 bool saved_integral_constant_expression_p
;
5424 bool saved_allow_non_integral_constant_expression_p
;
5425 bool saved_non_integral_constant_expression_p
;
5428 /* It might seem that we could simply parse the
5429 conditional-expression, and then check to see if it were
5430 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5431 one that the compiler can figure out is constant, possibly after
5432 doing some simplifications or optimizations. The standard has a
5433 precise definition of constant-expression, and we must honor
5434 that, even though it is somewhat more restrictive.
5440 is not a legal declaration, because `(2, 3)' is not a
5441 constant-expression. The `,' operator is forbidden in a
5442 constant-expression. However, GCC's constant-folding machinery
5443 will fold this operation to an INTEGER_CST for `3'. */
5445 /* Save the old settings. */
5446 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5447 saved_allow_non_integral_constant_expression_p
5448 = parser
->allow_non_integral_constant_expression_p
;
5449 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5450 /* We are now parsing a constant-expression. */
5451 parser
->integral_constant_expression_p
= true;
5452 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5453 parser
->non_integral_constant_expression_p
= false;
5454 /* Although the grammar says "conditional-expression", we parse an
5455 "assignment-expression", which also permits "throw-expression"
5456 and the use of assignment operators. In the case that
5457 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5458 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5459 actually essential that we look for an assignment-expression.
5460 For example, cp_parser_initializer_clauses uses this function to
5461 determine whether a particular assignment-expression is in fact
5463 expression
= cp_parser_assignment_expression (parser
);
5464 /* Restore the old settings. */
5465 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
5466 parser
->allow_non_integral_constant_expression_p
5467 = saved_allow_non_integral_constant_expression_p
;
5468 if (allow_non_constant_p
)
5469 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5470 parser
->non_integral_constant_expression_p
= saved_non_integral_constant_expression_p
;
5475 /* Parse __builtin_offsetof.
5477 offsetof-expression:
5478 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5480 offsetof-member-designator:
5482 | offsetof-member-designator "." id-expression
5483 | offsetof-member-designator "[" expression "]"
5487 cp_parser_builtin_offsetof (cp_parser
*parser
)
5489 int save_ice_p
, save_non_ice_p
;
5493 /* We're about to accept non-integral-constant things, but will
5494 definitely yield an integral constant expression. Save and
5495 restore these values around our local parsing. */
5496 save_ice_p
= parser
->integral_constant_expression_p
;
5497 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
5499 /* Consume the "__builtin_offsetof" token. */
5500 cp_lexer_consume_token (parser
->lexer
);
5501 /* Consume the opening `('. */
5502 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5503 /* Parse the type-id. */
5504 type
= cp_parser_type_id (parser
);
5505 /* Look for the `,'. */
5506 cp_parser_require (parser
, CPP_COMMA
, "`,'");
5508 /* Build the (type *)null that begins the traditional offsetof macro. */
5509 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
5511 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5512 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
5516 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5517 switch (token
->type
)
5519 case CPP_OPEN_SQUARE
:
5520 /* offsetof-member-designator "[" expression "]" */
5521 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
5525 /* offsetof-member-designator "." identifier */
5526 cp_lexer_consume_token (parser
->lexer
);
5527 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
5531 case CPP_CLOSE_PAREN
:
5532 /* Consume the ")" token. */
5533 cp_lexer_consume_token (parser
->lexer
);
5537 /* Error. We know the following require will fail, but
5538 that gives the proper error message. */
5539 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5540 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
5541 expr
= error_mark_node
;
5547 /* We've finished the parsing, now finish with the semantics. At present
5548 we're just mirroring the traditional macro implementation. Better
5549 would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5550 here rather than in build_x_unary_op. */
5551 expr
= build_reinterpret_cast (build_reference_type (char_type_node
), expr
);
5552 expr
= build_x_unary_op (ADDR_EXPR
, expr
);
5553 expr
= build_reinterpret_cast (size_type_node
, expr
);
5556 parser
->integral_constant_expression_p
= save_ice_p
;
5557 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
5562 /* Statements [gram.stmt.stmt] */
5564 /* Parse a statement.
5568 expression-statement
5573 declaration-statement
5577 cp_parser_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5581 location_t statement_locus
;
5583 /* There is no statement yet. */
5584 statement
= NULL_TREE
;
5585 /* Peek at the next token. */
5586 token
= cp_lexer_peek_token (parser
->lexer
);
5587 /* Remember the location of the first token in the statement. */
5588 statement_locus
= token
->location
;
5589 /* If this is a keyword, then that will often determine what kind of
5590 statement we have. */
5591 if (token
->type
== CPP_KEYWORD
)
5593 enum rid keyword
= token
->keyword
;
5599 statement
= cp_parser_labeled_statement (parser
,
5600 in_statement_expr_p
);
5605 statement
= cp_parser_selection_statement (parser
);
5611 statement
= cp_parser_iteration_statement (parser
);
5618 statement
= cp_parser_jump_statement (parser
);
5622 statement
= cp_parser_try_block (parser
);
5626 /* It might be a keyword like `int' that can start a
5627 declaration-statement. */
5631 else if (token
->type
== CPP_NAME
)
5633 /* If the next token is a `:', then we are looking at a
5634 labeled-statement. */
5635 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5636 if (token
->type
== CPP_COLON
)
5637 statement
= cp_parser_labeled_statement (parser
, in_statement_expr_p
);
5639 /* Anything that starts with a `{' must be a compound-statement. */
5640 else if (token
->type
== CPP_OPEN_BRACE
)
5641 statement
= cp_parser_compound_statement (parser
, false);
5643 /* Everything else must be a declaration-statement or an
5644 expression-statement. Try for the declaration-statement
5645 first, unless we are looking at a `;', in which case we know that
5646 we have an expression-statement. */
5649 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5651 cp_parser_parse_tentatively (parser
);
5652 /* Try to parse the declaration-statement. */
5653 cp_parser_declaration_statement (parser
);
5654 /* If that worked, we're done. */
5655 if (cp_parser_parse_definitely (parser
))
5658 /* Look for an expression-statement instead. */
5659 statement
= cp_parser_expression_statement (parser
, in_statement_expr_p
);
5662 /* Set the line number for the statement. */
5663 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5665 SET_EXPR_LOCUS (statement
, NULL
);
5666 annotate_with_locus (statement
, statement_locus
);
5670 /* Parse a labeled-statement.
5673 identifier : statement
5674 case constant-expression : statement
5680 case constant-expression ... constant-expression : statement
5682 Returns the new CASE_LABEL, for a `case' or `default' label. For
5683 an ordinary label, returns a LABEL_STMT. */
5686 cp_parser_labeled_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5689 tree statement
= error_mark_node
;
5691 /* The next token should be an identifier. */
5692 token
= cp_lexer_peek_token (parser
->lexer
);
5693 if (token
->type
!= CPP_NAME
5694 && token
->type
!= CPP_KEYWORD
)
5696 cp_parser_error (parser
, "expected labeled-statement");
5697 return error_mark_node
;
5700 switch (token
->keyword
)
5707 /* Consume the `case' token. */
5708 cp_lexer_consume_token (parser
->lexer
);
5709 /* Parse the constant-expression. */
5710 expr
= cp_parser_constant_expression (parser
,
5711 /*allow_non_constant_p=*/false,
5714 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
5715 if (ellipsis
->type
== CPP_ELLIPSIS
)
5717 /* Consume the `...' token. */
5718 cp_lexer_consume_token (parser
->lexer
);
5720 cp_parser_constant_expression (parser
,
5721 /*allow_non_constant_p=*/false,
5723 /* We don't need to emit warnings here, as the common code
5724 will do this for us. */
5727 expr_hi
= NULL_TREE
;
5729 if (!parser
->in_switch_statement_p
)
5730 error ("case label `%E' not within a switch statement", expr
);
5732 statement
= finish_case_label (expr
, expr_hi
);
5737 /* Consume the `default' token. */
5738 cp_lexer_consume_token (parser
->lexer
);
5739 if (!parser
->in_switch_statement_p
)
5740 error ("case label not within a switch statement");
5742 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5746 /* Anything else must be an ordinary label. */
5747 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5751 /* Require the `:' token. */
5752 cp_parser_require (parser
, CPP_COLON
, "`:'");
5753 /* Parse the labeled statement. */
5754 cp_parser_statement (parser
, in_statement_expr_p
);
5756 /* Return the label, in the case of a `case' or `default' label. */
5760 /* Parse an expression-statement.
5762 expression-statement:
5765 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5766 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5767 indicates whether this expression-statement is part of an
5768 expression statement. */
5771 cp_parser_expression_statement (cp_parser
* parser
, bool in_statement_expr_p
)
5773 tree statement
= NULL_TREE
;
5775 /* If the next token is a ';', then there is no expression
5777 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5778 statement
= cp_parser_expression (parser
);
5780 /* Consume the final `;'. */
5781 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5783 if (in_statement_expr_p
5784 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
5786 /* This is the final expression statement of a statement
5788 statement
= finish_stmt_expr_expr (statement
);
5791 statement
= finish_expr_stmt (statement
);
5798 /* Parse a compound-statement.
5801 { statement-seq [opt] }
5803 Returns a COMPOUND_STMT representing the statement. */
5806 cp_parser_compound_statement (cp_parser
*parser
, bool in_statement_expr_p
)
5810 /* Consume the `{'. */
5811 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5812 return error_mark_node
;
5813 /* Begin the compound-statement. */
5814 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/false);
5815 /* Parse an (optional) statement-seq. */
5816 cp_parser_statement_seq_opt (parser
, in_statement_expr_p
);
5817 /* Finish the compound-statement. */
5818 finish_compound_stmt (compound_stmt
);
5819 /* Consume the `}'. */
5820 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5822 return compound_stmt
;
5825 /* Parse an (optional) statement-seq.
5829 statement-seq [opt] statement */
5832 cp_parser_statement_seq_opt (cp_parser
* parser
, bool in_statement_expr_p
)
5834 /* Scan statements until there aren't any more. */
5837 /* If we're looking at a `}', then we've run out of statements. */
5838 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5839 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5842 /* Parse the statement. */
5843 cp_parser_statement (parser
, in_statement_expr_p
);
5847 /* Parse a selection-statement.
5849 selection-statement:
5850 if ( condition ) statement
5851 if ( condition ) statement else statement
5852 switch ( condition ) statement
5854 Returns the new IF_STMT or SWITCH_STMT. */
5857 cp_parser_selection_statement (cp_parser
* parser
)
5862 /* Peek at the next token. */
5863 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5865 /* See what kind of keyword it is. */
5866 keyword
= token
->keyword
;
5875 /* Look for the `('. */
5876 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5878 cp_parser_skip_to_end_of_statement (parser
);
5879 return error_mark_node
;
5882 /* Begin the selection-statement. */
5883 if (keyword
== RID_IF
)
5884 statement
= begin_if_stmt ();
5886 statement
= begin_switch_stmt ();
5888 /* Parse the condition. */
5889 condition
= cp_parser_condition (parser
);
5890 /* Look for the `)'. */
5891 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5892 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
5893 /*consume_paren=*/true);
5895 if (keyword
== RID_IF
)
5899 /* Add the condition. */
5900 finish_if_stmt_cond (condition
, statement
);
5902 /* Parse the then-clause. */
5903 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5904 finish_then_clause (statement
);
5906 /* If the next token is `else', parse the else-clause. */
5907 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5912 /* Consume the `else' keyword. */
5913 cp_lexer_consume_token (parser
->lexer
);
5914 /* Parse the else-clause. */
5916 = cp_parser_implicitly_scoped_statement (parser
);
5917 finish_else_clause (statement
);
5920 /* Now we're all done with the if-statement. */
5926 bool in_switch_statement_p
;
5928 /* Add the condition. */
5929 finish_switch_cond (condition
, statement
);
5931 /* Parse the body of the switch-statement. */
5932 in_switch_statement_p
= parser
->in_switch_statement_p
;
5933 parser
->in_switch_statement_p
= true;
5934 body
= cp_parser_implicitly_scoped_statement (parser
);
5935 parser
->in_switch_statement_p
= in_switch_statement_p
;
5937 /* Now we're all done with the switch-statement. */
5938 finish_switch_stmt (statement
);
5946 cp_parser_error (parser
, "expected selection-statement");
5947 return error_mark_node
;
5951 /* Parse a condition.
5955 type-specifier-seq declarator = assignment-expression
5960 type-specifier-seq declarator asm-specification [opt]
5961 attributes [opt] = assignment-expression
5963 Returns the expression that should be tested. */
5966 cp_parser_condition (cp_parser
* parser
)
5968 tree type_specifiers
;
5969 const char *saved_message
;
5971 /* Try the declaration first. */
5972 cp_parser_parse_tentatively (parser
);
5973 /* New types are not allowed in the type-specifier-seq for a
5975 saved_message
= parser
->type_definition_forbidden_message
;
5976 parser
->type_definition_forbidden_message
5977 = "types may not be defined in conditions";
5978 /* Parse the type-specifier-seq. */
5979 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5980 /* Restore the saved message. */
5981 parser
->type_definition_forbidden_message
= saved_message
;
5982 /* If all is well, we might be looking at a declaration. */
5983 if (!cp_parser_error_occurred (parser
))
5986 tree asm_specification
;
5989 tree initializer
= NULL_TREE
;
5991 /* Parse the declarator. */
5992 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5993 /*ctor_dtor_or_conv_p=*/NULL
,
5994 /*parenthesized_p=*/NULL
);
5995 /* Parse the attributes. */
5996 attributes
= cp_parser_attributes_opt (parser
);
5997 /* Parse the asm-specification. */
5998 asm_specification
= cp_parser_asm_specification_opt (parser
);
5999 /* If the next token is not an `=', then we might still be
6000 looking at an expression. For example:
6004 looks like a decl-specifier-seq and a declarator -- but then
6005 there is no `=', so this is an expression. */
6006 cp_parser_require (parser
, CPP_EQ
, "`='");
6007 /* If we did see an `=', then we are looking at a declaration
6009 if (cp_parser_parse_definitely (parser
))
6011 /* Create the declaration. */
6012 decl
= start_decl (declarator
, type_specifiers
,
6013 /*initialized_p=*/true,
6014 attributes
, /*prefix_attributes=*/NULL_TREE
);
6015 /* Parse the assignment-expression. */
6016 initializer
= cp_parser_assignment_expression (parser
);
6018 /* Process the initializer. */
6019 cp_finish_decl (decl
,
6022 LOOKUP_ONLYCONVERTING
);
6024 return convert_from_reference (decl
);
6027 /* If we didn't even get past the declarator successfully, we are
6028 definitely not looking at a declaration. */
6030 cp_parser_abort_tentative_parse (parser
);
6032 /* Otherwise, we are looking at an expression. */
6033 return cp_parser_expression (parser
);
6036 /* Parse an iteration-statement.
6038 iteration-statement:
6039 while ( condition ) statement
6040 do statement while ( expression ) ;
6041 for ( for-init-statement condition [opt] ; expression [opt] )
6044 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6047 cp_parser_iteration_statement (cp_parser
* parser
)
6052 bool in_iteration_statement_p
;
6055 /* Peek at the next token. */
6056 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6058 return error_mark_node
;
6060 /* Remember whether or not we are already within an iteration
6062 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
6064 /* See what kind of keyword it is. */
6065 keyword
= token
->keyword
;
6072 /* Begin the while-statement. */
6073 statement
= begin_while_stmt ();
6074 /* Look for the `('. */
6075 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6076 /* Parse the condition. */
6077 condition
= cp_parser_condition (parser
);
6078 finish_while_stmt_cond (condition
, statement
);
6079 /* Look for the `)'. */
6080 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6081 /* Parse the dependent statement. */
6082 parser
->in_iteration_statement_p
= true;
6083 cp_parser_already_scoped_statement (parser
);
6084 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6085 /* We're done with the while-statement. */
6086 finish_while_stmt (statement
);
6094 /* Begin the do-statement. */
6095 statement
= begin_do_stmt ();
6096 /* Parse the body of the do-statement. */
6097 parser
->in_iteration_statement_p
= true;
6098 cp_parser_implicitly_scoped_statement (parser
);
6099 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6100 finish_do_body (statement
);
6101 /* Look for the `while' keyword. */
6102 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6103 /* Look for the `('. */
6104 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6105 /* Parse the expression. */
6106 expression
= cp_parser_expression (parser
);
6107 /* We're done with the do-statement. */
6108 finish_do_stmt (expression
, statement
);
6109 /* Look for the `)'. */
6110 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6111 /* Look for the `;'. */
6112 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6118 tree condition
= NULL_TREE
;
6119 tree expression
= NULL_TREE
;
6121 /* Begin the for-statement. */
6122 statement
= begin_for_stmt ();
6123 /* Look for the `('. */
6124 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6125 /* Parse the initialization. */
6126 cp_parser_for_init_statement (parser
);
6127 finish_for_init_stmt (statement
);
6129 /* If there's a condition, process it. */
6130 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6131 condition
= cp_parser_condition (parser
);
6132 finish_for_cond (condition
, statement
);
6133 /* Look for the `;'. */
6134 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6136 /* If there's an expression, process it. */
6137 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6138 expression
= cp_parser_expression (parser
);
6139 finish_for_expr (expression
, statement
);
6140 /* Look for the `)'. */
6141 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6143 /* Parse the body of the for-statement. */
6144 parser
->in_iteration_statement_p
= true;
6145 cp_parser_already_scoped_statement (parser
);
6146 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6148 /* We're done with the for-statement. */
6149 finish_for_stmt (statement
);
6154 cp_parser_error (parser
, "expected iteration-statement");
6155 statement
= error_mark_node
;
6162 /* Parse a for-init-statement.
6165 expression-statement
6166 simple-declaration */
6169 cp_parser_for_init_statement (cp_parser
* parser
)
6171 /* If the next token is a `;', then we have an empty
6172 expression-statement. Grammatically, this is also a
6173 simple-declaration, but an invalid one, because it does not
6174 declare anything. Therefore, if we did not handle this case
6175 specially, we would issue an error message about an invalid
6177 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6179 /* We're going to speculatively look for a declaration, falling back
6180 to an expression, if necessary. */
6181 cp_parser_parse_tentatively (parser
);
6182 /* Parse the declaration. */
6183 cp_parser_simple_declaration (parser
,
6184 /*function_definition_allowed_p=*/false);
6185 /* If the tentative parse failed, then we shall need to look for an
6186 expression-statement. */
6187 if (cp_parser_parse_definitely (parser
))
6191 cp_parser_expression_statement (parser
, false);
6194 /* Parse a jump-statement.
6199 return expression [opt] ;
6207 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6211 cp_parser_jump_statement (cp_parser
* parser
)
6213 tree statement
= error_mark_node
;
6217 /* Peek at the next token. */
6218 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6220 return error_mark_node
;
6222 /* See what kind of keyword it is. */
6223 keyword
= token
->keyword
;
6227 if (!parser
->in_switch_statement_p
6228 && !parser
->in_iteration_statement_p
)
6230 error ("break statement not within loop or switch");
6231 statement
= error_mark_node
;
6234 statement
= finish_break_stmt ();
6235 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6239 if (!parser
->in_iteration_statement_p
)
6241 error ("continue statement not within a loop");
6242 statement
= error_mark_node
;
6245 statement
= finish_continue_stmt ();
6246 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6253 /* If the next token is a `;', then there is no
6255 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6256 expr
= cp_parser_expression (parser
);
6259 /* Build the return-statement. */
6260 statement
= finish_return_stmt (expr
);
6261 /* Look for the final `;'. */
6262 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6267 /* Create the goto-statement. */
6268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6270 /* Issue a warning about this use of a GNU extension. */
6272 pedwarn ("ISO C++ forbids computed gotos");
6273 /* Consume the '*' token. */
6274 cp_lexer_consume_token (parser
->lexer
);
6275 /* Parse the dependent expression. */
6276 finish_goto_stmt (cp_parser_expression (parser
));
6279 finish_goto_stmt (cp_parser_identifier (parser
));
6280 /* Look for the final `;'. */
6281 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6285 cp_parser_error (parser
, "expected jump-statement");
6292 /* Parse a declaration-statement.
6294 declaration-statement:
6295 block-declaration */
6298 cp_parser_declaration_statement (cp_parser
* parser
)
6300 /* Parse the block-declaration. */
6301 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6303 /* Finish off the statement. */
6307 /* Some dependent statements (like `if (cond) statement'), are
6308 implicitly in their own scope. In other words, if the statement is
6309 a single statement (as opposed to a compound-statement), it is
6310 none-the-less treated as if it were enclosed in braces. Any
6311 declarations appearing in the dependent statement are out of scope
6312 after control passes that point. This function parses a statement,
6313 but ensures that is in its own scope, even if it is not a
6316 Returns the new statement. */
6319 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6323 /* If the token is not a `{', then we must take special action. */
6324 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6326 /* Create a compound-statement. */
6327 statement
= begin_compound_stmt (/*has_no_scope=*/false);
6328 /* Parse the dependent-statement. */
6329 cp_parser_statement (parser
, false);
6330 /* Finish the dummy compound-statement. */
6331 finish_compound_stmt (statement
);
6333 /* Otherwise, we simply parse the statement directly. */
6335 statement
= cp_parser_compound_statement (parser
, false);
6337 /* Return the statement. */
6341 /* For some dependent statements (like `while (cond) statement'), we
6342 have already created a scope. Therefore, even if the dependent
6343 statement is a compound-statement, we do not want to create another
6347 cp_parser_already_scoped_statement (cp_parser
* parser
)
6349 /* If the token is not a `{', then we must take special action. */
6350 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
6354 /* Create a compound-statement. */
6355 statement
= begin_compound_stmt (/*has_no_scope=*/true);
6356 /* Parse the dependent-statement. */
6357 cp_parser_statement (parser
, false);
6358 /* Finish the dummy compound-statement. */
6359 finish_compound_stmt (statement
);
6361 /* Otherwise, we simply parse the statement directly. */
6363 cp_parser_statement (parser
, false);
6366 /* Declarations [gram.dcl.dcl] */
6368 /* Parse an optional declaration-sequence.
6372 declaration-seq declaration */
6375 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6381 token
= cp_lexer_peek_token (parser
->lexer
);
6383 if (token
->type
== CPP_CLOSE_BRACE
6384 || token
->type
== CPP_EOF
)
6387 if (token
->type
== CPP_SEMICOLON
)
6389 /* A declaration consisting of a single semicolon is
6390 invalid. Allow it unless we're being pedantic. */
6391 if (pedantic
&& !in_system_header
)
6392 pedwarn ("extra `;'");
6393 cp_lexer_consume_token (parser
->lexer
);
6397 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6398 parser to enter or exit implicit `extern "C"' blocks. */
6399 while (pending_lang_change
> 0)
6401 push_lang_context (lang_name_c
);
6402 --pending_lang_change
;
6404 while (pending_lang_change
< 0)
6406 pop_lang_context ();
6407 ++pending_lang_change
;
6410 /* Parse the declaration itself. */
6411 cp_parser_declaration (parser
);
6415 /* Parse a declaration.
6420 template-declaration
6421 explicit-instantiation
6422 explicit-specialization
6423 linkage-specification
6424 namespace-definition
6429 __extension__ declaration */
6432 cp_parser_declaration (cp_parser
* parser
)
6438 /* Set this here since we can be called after
6439 pushing the linkage specification. */
6440 c_lex_string_translate
= true;
6442 /* Check for the `__extension__' keyword. */
6443 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6445 /* Parse the qualified declaration. */
6446 cp_parser_declaration (parser
);
6447 /* Restore the PEDANTIC flag. */
6448 pedantic
= saved_pedantic
;
6453 /* Try to figure out what kind of declaration is present. */
6454 token1
= *cp_lexer_peek_token (parser
->lexer
);
6456 /* Don't translate the CPP_STRING in extern "C". */
6457 if (token1
.keyword
== RID_EXTERN
)
6458 c_lex_string_translate
= false;
6460 if (token1
.type
!= CPP_EOF
)
6461 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6463 c_lex_string_translate
= true;
6465 /* If the next token is `extern' and the following token is a string
6466 literal, then we have a linkage specification. */
6467 if (token1
.keyword
== RID_EXTERN
6468 && cp_parser_is_string_literal (&token2
))
6469 cp_parser_linkage_specification (parser
);
6470 /* If the next token is `template', then we have either a template
6471 declaration, an explicit instantiation, or an explicit
6473 else if (token1
.keyword
== RID_TEMPLATE
)
6475 /* `template <>' indicates a template specialization. */
6476 if (token2
.type
== CPP_LESS
6477 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6478 cp_parser_explicit_specialization (parser
);
6479 /* `template <' indicates a template declaration. */
6480 else if (token2
.type
== CPP_LESS
)
6481 cp_parser_template_declaration (parser
, /*member_p=*/false);
6482 /* Anything else must be an explicit instantiation. */
6484 cp_parser_explicit_instantiation (parser
);
6486 /* If the next token is `export', then we have a template
6488 else if (token1
.keyword
== RID_EXPORT
)
6489 cp_parser_template_declaration (parser
, /*member_p=*/false);
6490 /* If the next token is `extern', 'static' or 'inline' and the one
6491 after that is `template', we have a GNU extended explicit
6492 instantiation directive. */
6493 else if (cp_parser_allow_gnu_extensions_p (parser
)
6494 && (token1
.keyword
== RID_EXTERN
6495 || token1
.keyword
== RID_STATIC
6496 || token1
.keyword
== RID_INLINE
)
6497 && token2
.keyword
== RID_TEMPLATE
)
6498 cp_parser_explicit_instantiation (parser
);
6499 /* If the next token is `namespace', check for a named or unnamed
6500 namespace definition. */
6501 else if (token1
.keyword
== RID_NAMESPACE
6502 && (/* A named namespace definition. */
6503 (token2
.type
== CPP_NAME
6504 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6506 /* An unnamed namespace definition. */
6507 || token2
.type
== CPP_OPEN_BRACE
))
6508 cp_parser_namespace_definition (parser
);
6509 /* We must have either a block declaration or a function
6512 /* Try to parse a block-declaration, or a function-definition. */
6513 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6516 /* Parse a block-declaration.
6521 namespace-alias-definition
6528 __extension__ block-declaration
6531 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6532 part of a declaration-statement. */
6535 cp_parser_block_declaration (cp_parser
*parser
,
6541 /* Check for the `__extension__' keyword. */
6542 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6544 /* Parse the qualified declaration. */
6545 cp_parser_block_declaration (parser
, statement_p
);
6546 /* Restore the PEDANTIC flag. */
6547 pedantic
= saved_pedantic
;
6552 /* Peek at the next token to figure out which kind of declaration is
6554 token1
= cp_lexer_peek_token (parser
->lexer
);
6556 /* If the next keyword is `asm', we have an asm-definition. */
6557 if (token1
->keyword
== RID_ASM
)
6560 cp_parser_commit_to_tentative_parse (parser
);
6561 cp_parser_asm_definition (parser
);
6563 /* If the next keyword is `namespace', we have a
6564 namespace-alias-definition. */
6565 else if (token1
->keyword
== RID_NAMESPACE
)
6566 cp_parser_namespace_alias_definition (parser
);
6567 /* If the next keyword is `using', we have either a
6568 using-declaration or a using-directive. */
6569 else if (token1
->keyword
== RID_USING
)
6574 cp_parser_commit_to_tentative_parse (parser
);
6575 /* If the token after `using' is `namespace', then we have a
6577 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6578 if (token2
->keyword
== RID_NAMESPACE
)
6579 cp_parser_using_directive (parser
);
6580 /* Otherwise, it's a using-declaration. */
6582 cp_parser_using_declaration (parser
);
6584 /* If the next keyword is `__label__' we have a label declaration. */
6585 else if (token1
->keyword
== RID_LABEL
)
6588 cp_parser_commit_to_tentative_parse (parser
);
6589 cp_parser_label_declaration (parser
);
6591 /* Anything else must be a simple-declaration. */
6593 cp_parser_simple_declaration (parser
, !statement_p
);
6596 /* Parse a simple-declaration.
6599 decl-specifier-seq [opt] init-declarator-list [opt] ;
6601 init-declarator-list:
6603 init-declarator-list , init-declarator
6605 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6606 function-definition as a simple-declaration. */
6609 cp_parser_simple_declaration (cp_parser
* parser
,
6610 bool function_definition_allowed_p
)
6612 tree decl_specifiers
;
6614 int declares_class_or_enum
;
6615 bool saw_declarator
;
6617 /* Defer access checks until we know what is being declared; the
6618 checks for names appearing in the decl-specifier-seq should be
6619 done as if we were in the scope of the thing being declared. */
6620 push_deferring_access_checks (dk_deferred
);
6622 /* Parse the decl-specifier-seq. We have to keep track of whether
6623 or not the decl-specifier-seq declares a named class or
6624 enumeration type, since that is the only case in which the
6625 init-declarator-list is allowed to be empty.
6629 In a simple-declaration, the optional init-declarator-list can be
6630 omitted only when declaring a class or enumeration, that is when
6631 the decl-specifier-seq contains either a class-specifier, an
6632 elaborated-type-specifier, or an enum-specifier. */
6634 = cp_parser_decl_specifier_seq (parser
,
6635 CP_PARSER_FLAGS_OPTIONAL
,
6637 &declares_class_or_enum
);
6638 /* We no longer need to defer access checks. */
6639 stop_deferring_access_checks ();
6641 /* In a block scope, a valid declaration must always have a
6642 decl-specifier-seq. By not trying to parse declarators, we can
6643 resolve the declaration/expression ambiguity more quickly. */
6644 if (!function_definition_allowed_p
&& !decl_specifiers
)
6646 cp_parser_error (parser
, "expected declaration");
6650 /* If the next two tokens are both identifiers, the code is
6651 erroneous. The usual cause of this situation is code like:
6655 where "T" should name a type -- but does not. */
6656 if (cp_parser_parse_and_diagnose_invalid_type_name (parser
))
6658 /* If parsing tentatively, we should commit; we really are
6659 looking at a declaration. */
6660 cp_parser_commit_to_tentative_parse (parser
);
6665 /* Keep going until we hit the `;' at the end of the simple
6667 saw_declarator
= false;
6668 while (cp_lexer_next_token_is_not (parser
->lexer
,
6672 bool function_definition_p
;
6675 saw_declarator
= true;
6676 /* Parse the init-declarator. */
6677 decl
= cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6678 function_definition_allowed_p
,
6680 declares_class_or_enum
,
6681 &function_definition_p
);
6682 /* If an error occurred while parsing tentatively, exit quickly.
6683 (That usually happens when in the body of a function; each
6684 statement is treated as a declaration-statement until proven
6686 if (cp_parser_error_occurred (parser
))
6688 /* Handle function definitions specially. */
6689 if (function_definition_p
)
6691 /* If the next token is a `,', then we are probably
6692 processing something like:
6696 which is erroneous. */
6697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6698 error ("mixing declarations and function-definitions is forbidden");
6699 /* Otherwise, we're done with the list of declarators. */
6702 pop_deferring_access_checks ();
6706 /* The next token should be either a `,' or a `;'. */
6707 token
= cp_lexer_peek_token (parser
->lexer
);
6708 /* If it's a `,', there are more declarators to come. */
6709 if (token
->type
== CPP_COMMA
)
6710 cp_lexer_consume_token (parser
->lexer
);
6711 /* If it's a `;', we are done. */
6712 else if (token
->type
== CPP_SEMICOLON
)
6714 /* Anything else is an error. */
6717 cp_parser_error (parser
, "expected `,' or `;'");
6718 /* Skip tokens until we reach the end of the statement. */
6719 cp_parser_skip_to_end_of_statement (parser
);
6720 /* If the next token is now a `;', consume it. */
6721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6722 cp_lexer_consume_token (parser
->lexer
);
6725 /* After the first time around, a function-definition is not
6726 allowed -- even if it was OK at first. For example:
6731 function_definition_allowed_p
= false;
6734 /* Issue an error message if no declarators are present, and the
6735 decl-specifier-seq does not itself declare a class or
6737 if (!saw_declarator
)
6739 if (cp_parser_declares_only_class_p (parser
))
6740 shadow_tag (decl_specifiers
);
6741 /* Perform any deferred access checks. */
6742 perform_deferred_access_checks ();
6745 /* Consume the `;'. */
6746 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6749 pop_deferring_access_checks ();
6752 /* Parse a decl-specifier-seq.
6755 decl-specifier-seq [opt] decl-specifier
6758 storage-class-specifier
6769 Returns a TREE_LIST, giving the decl-specifiers in the order they
6770 appear in the source code. The TREE_VALUE of each node is the
6771 decl-specifier. For a keyword (such as `auto' or `friend'), the
6772 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6773 representation of a type-specifier, see cp_parser_type_specifier.
6775 If there are attributes, they will be stored in *ATTRIBUTES,
6776 represented as described above cp_parser_attributes.
6778 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6779 appears, and the entity that will be a friend is not going to be a
6780 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6781 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6782 friendship is granted might not be a class.
6784 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6787 1: one of the decl-specifiers is an elaborated-type-specifier
6788 (i.e., a type declaration)
6789 2: one of the decl-specifiers is an enum-specifier or a
6790 class-specifier (i.e., a type definition)
6795 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6796 cp_parser_flags flags
,
6798 int* declares_class_or_enum
)
6800 tree decl_specs
= NULL_TREE
;
6801 bool friend_p
= false;
6802 bool constructor_possible_p
= !parser
->in_declarator_p
;
6804 /* Assume no class or enumeration type is declared. */
6805 *declares_class_or_enum
= 0;
6807 /* Assume there are no attributes. */
6808 *attributes
= NULL_TREE
;
6810 /* Keep reading specifiers until there are no more to read. */
6813 tree decl_spec
= NULL_TREE
;
6817 /* Peek at the next token. */
6818 token
= cp_lexer_peek_token (parser
->lexer
);
6819 /* Handle attributes. */
6820 if (token
->keyword
== RID_ATTRIBUTE
)
6822 /* Parse the attributes. */
6823 decl_spec
= cp_parser_attributes_opt (parser
);
6824 /* Add them to the list. */
6825 *attributes
= chainon (*attributes
, decl_spec
);
6828 /* If the next token is an appropriate keyword, we can simply
6829 add it to the list. */
6830 switch (token
->keyword
)
6836 error ("duplicate `friend'");
6839 /* The representation of the specifier is simply the
6840 appropriate TREE_IDENTIFIER node. */
6841 decl_spec
= token
->value
;
6842 /* Consume the token. */
6843 cp_lexer_consume_token (parser
->lexer
);
6846 /* function-specifier:
6853 decl_spec
= cp_parser_function_specifier_opt (parser
);
6859 /* The representation of the specifier is simply the
6860 appropriate TREE_IDENTIFIER node. */
6861 decl_spec
= token
->value
;
6862 /* Consume the token. */
6863 cp_lexer_consume_token (parser
->lexer
);
6864 /* A constructor declarator cannot appear in a typedef. */
6865 constructor_possible_p
= false;
6866 /* The "typedef" keyword can only occur in a declaration; we
6867 may as well commit at this point. */
6868 cp_parser_commit_to_tentative_parse (parser
);
6871 /* storage-class-specifier:
6886 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6893 /* Constructors are a special case. The `S' in `S()' is not a
6894 decl-specifier; it is the beginning of the declarator. */
6895 constructor_p
= (!decl_spec
6896 && constructor_possible_p
6897 && cp_parser_constructor_declarator_p (parser
,
6900 /* If we don't have a DECL_SPEC yet, then we must be looking at
6901 a type-specifier. */
6902 if (!decl_spec
&& !constructor_p
)
6904 int decl_spec_declares_class_or_enum
;
6905 bool is_cv_qualifier
;
6908 = cp_parser_type_specifier (parser
, flags
,
6910 /*is_declaration=*/true,
6911 &decl_spec_declares_class_or_enum
,
6914 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6916 /* If this type-specifier referenced a user-defined type
6917 (a typedef, class-name, etc.), then we can't allow any
6918 more such type-specifiers henceforth.
6922 The longest sequence of decl-specifiers that could
6923 possibly be a type name is taken as the
6924 decl-specifier-seq of a declaration. The sequence shall
6925 be self-consistent as described below.
6929 As a general rule, at most one type-specifier is allowed
6930 in the complete decl-specifier-seq of a declaration. The
6931 only exceptions are the following:
6933 -- const or volatile can be combined with any other
6936 -- signed or unsigned can be combined with char, long,
6944 void g (const int Pc);
6946 Here, Pc is *not* part of the decl-specifier seq; it's
6947 the declarator. Therefore, once we see a type-specifier
6948 (other than a cv-qualifier), we forbid any additional
6949 user-defined types. We *do* still allow things like `int
6950 int' to be considered a decl-specifier-seq, and issue the
6951 error message later. */
6952 if (decl_spec
&& !is_cv_qualifier
)
6953 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6954 /* A constructor declarator cannot follow a type-specifier. */
6956 constructor_possible_p
= false;
6959 /* If we still do not have a DECL_SPEC, then there are no more
6963 /* Issue an error message, unless the entire construct was
6965 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6967 cp_parser_error (parser
, "expected decl specifier");
6968 return error_mark_node
;
6974 /* Add the DECL_SPEC to the list of specifiers. */
6975 if (decl_specs
== NULL
|| TREE_VALUE (decl_specs
) != error_mark_node
)
6976 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6978 /* After we see one decl-specifier, further decl-specifiers are
6980 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6983 /* Don't allow a friend specifier with a class definition. */
6984 if (friend_p
&& (*declares_class_or_enum
& 2))
6985 error ("class definition may not be declared a friend");
6987 /* We have built up the DECL_SPECS in reverse order. Return them in
6988 the correct order. */
6989 return nreverse (decl_specs
);
6992 /* Parse an (optional) storage-class-specifier.
6994 storage-class-specifier:
7003 storage-class-specifier:
7006 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7009 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7011 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7019 /* Consume the token. */
7020 return cp_lexer_consume_token (parser
->lexer
)->value
;
7027 /* Parse an (optional) function-specifier.
7034 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7037 cp_parser_function_specifier_opt (cp_parser
* parser
)
7039 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7044 /* Consume the token. */
7045 return cp_lexer_consume_token (parser
->lexer
)->value
;
7052 /* Parse a linkage-specification.
7054 linkage-specification:
7055 extern string-literal { declaration-seq [opt] }
7056 extern string-literal declaration */
7059 cp_parser_linkage_specification (cp_parser
* parser
)
7064 /* Look for the `extern' keyword. */
7065 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7067 /* Peek at the next token. */
7068 token
= cp_lexer_peek_token (parser
->lexer
);
7069 /* If it's not a string-literal, then there's a problem. */
7070 if (!cp_parser_is_string_literal (token
))
7072 cp_parser_error (parser
, "expected language-name");
7075 /* Consume the token. */
7076 cp_lexer_consume_token (parser
->lexer
);
7078 /* Transform the literal into an identifier. If the literal is a
7079 wide-character string, or contains embedded NULs, then we can't
7080 handle it as the user wants. */
7081 if (token
->type
== CPP_WSTRING
7082 || (strlen (TREE_STRING_POINTER (token
->value
))
7083 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
7085 cp_parser_error (parser
, "invalid linkage-specification");
7086 /* Assume C++ linkage. */
7087 linkage
= get_identifier ("c++");
7089 /* If it's a simple string constant, things are easier. */
7091 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
7093 /* We're now using the new linkage. */
7094 push_lang_context (linkage
);
7096 /* If the next token is a `{', then we're using the first
7098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7100 /* Consume the `{' token. */
7101 cp_lexer_consume_token (parser
->lexer
);
7102 /* Parse the declarations. */
7103 cp_parser_declaration_seq_opt (parser
);
7104 /* Look for the closing `}'. */
7105 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7107 /* Otherwise, there's just one declaration. */
7110 bool saved_in_unbraced_linkage_specification_p
;
7112 saved_in_unbraced_linkage_specification_p
7113 = parser
->in_unbraced_linkage_specification_p
;
7114 parser
->in_unbraced_linkage_specification_p
= true;
7115 have_extern_spec
= true;
7116 cp_parser_declaration (parser
);
7117 have_extern_spec
= false;
7118 parser
->in_unbraced_linkage_specification_p
7119 = saved_in_unbraced_linkage_specification_p
;
7122 /* We're done with the linkage-specification. */
7123 pop_lang_context ();
7126 /* Special member functions [gram.special] */
7128 /* Parse a conversion-function-id.
7130 conversion-function-id:
7131 operator conversion-type-id
7133 Returns an IDENTIFIER_NODE representing the operator. */
7136 cp_parser_conversion_function_id (cp_parser
* parser
)
7140 tree saved_qualifying_scope
;
7141 tree saved_object_scope
;
7144 /* Look for the `operator' token. */
7145 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7146 return error_mark_node
;
7147 /* When we parse the conversion-type-id, the current scope will be
7148 reset. However, we need that information in able to look up the
7149 conversion function later, so we save it here. */
7150 saved_scope
= parser
->scope
;
7151 saved_qualifying_scope
= parser
->qualifying_scope
;
7152 saved_object_scope
= parser
->object_scope
;
7153 /* We must enter the scope of the class so that the names of
7154 entities declared within the class are available in the
7155 conversion-type-id. For example, consider:
7162 S::operator I() { ... }
7164 In order to see that `I' is a type-name in the definition, we
7165 must be in the scope of `S'. */
7167 pop_p
= push_scope (saved_scope
);
7168 /* Parse the conversion-type-id. */
7169 type
= cp_parser_conversion_type_id (parser
);
7170 /* Leave the scope of the class, if any. */
7172 pop_scope (saved_scope
);
7173 /* Restore the saved scope. */
7174 parser
->scope
= saved_scope
;
7175 parser
->qualifying_scope
= saved_qualifying_scope
;
7176 parser
->object_scope
= saved_object_scope
;
7177 /* If the TYPE is invalid, indicate failure. */
7178 if (type
== error_mark_node
)
7179 return error_mark_node
;
7180 return mangle_conv_op_name_for_type (type
);
7183 /* Parse a conversion-type-id:
7186 type-specifier-seq conversion-declarator [opt]
7188 Returns the TYPE specified. */
7191 cp_parser_conversion_type_id (cp_parser
* parser
)
7194 tree type_specifiers
;
7197 /* Parse the attributes. */
7198 attributes
= cp_parser_attributes_opt (parser
);
7199 /* Parse the type-specifiers. */
7200 type_specifiers
= cp_parser_type_specifier_seq (parser
);
7201 /* If that didn't work, stop. */
7202 if (type_specifiers
== error_mark_node
)
7203 return error_mark_node
;
7204 /* Parse the conversion-declarator. */
7205 declarator
= cp_parser_conversion_declarator_opt (parser
);
7207 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
7208 /*initialized=*/0, &attributes
);
7211 /* Parse an (optional) conversion-declarator.
7213 conversion-declarator:
7214 ptr-operator conversion-declarator [opt]
7216 Returns a representation of the declarator. See
7217 cp_parser_declarator for details. */
7220 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7222 enum tree_code code
;
7224 tree cv_qualifier_seq
;
7226 /* We don't know if there's a ptr-operator next, or not. */
7227 cp_parser_parse_tentatively (parser
);
7228 /* Try the ptr-operator. */
7229 code
= cp_parser_ptr_operator (parser
, &class_type
,
7231 /* If it worked, look for more conversion-declarators. */
7232 if (cp_parser_parse_definitely (parser
))
7236 /* Parse another optional declarator. */
7237 declarator
= cp_parser_conversion_declarator_opt (parser
);
7239 /* Create the representation of the declarator. */
7240 if (code
== INDIRECT_REF
)
7241 declarator
= make_pointer_declarator (cv_qualifier_seq
,
7244 declarator
= make_reference_declarator (cv_qualifier_seq
,
7247 /* Handle the pointer-to-member case. */
7249 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
7257 /* Parse an (optional) ctor-initializer.
7260 : mem-initializer-list
7262 Returns TRUE iff the ctor-initializer was actually present. */
7265 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7267 /* If the next token is not a `:', then there is no
7268 ctor-initializer. */
7269 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7271 /* Do default initialization of any bases and members. */
7272 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7273 finish_mem_initializers (NULL_TREE
);
7278 /* Consume the `:' token. */
7279 cp_lexer_consume_token (parser
->lexer
);
7280 /* And the mem-initializer-list. */
7281 cp_parser_mem_initializer_list (parser
);
7286 /* Parse a mem-initializer-list.
7288 mem-initializer-list:
7290 mem-initializer , mem-initializer-list */
7293 cp_parser_mem_initializer_list (cp_parser
* parser
)
7295 tree mem_initializer_list
= NULL_TREE
;
7297 /* Let the semantic analysis code know that we are starting the
7298 mem-initializer-list. */
7299 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7300 error ("only constructors take base initializers");
7302 /* Loop through the list. */
7305 tree mem_initializer
;
7307 /* Parse the mem-initializer. */
7308 mem_initializer
= cp_parser_mem_initializer (parser
);
7309 /* Add it to the list, unless it was erroneous. */
7310 if (mem_initializer
)
7312 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7313 mem_initializer_list
= mem_initializer
;
7315 /* If the next token is not a `,', we're done. */
7316 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7318 /* Consume the `,' token. */
7319 cp_lexer_consume_token (parser
->lexer
);
7322 /* Perform semantic analysis. */
7323 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7324 finish_mem_initializers (mem_initializer_list
);
7327 /* Parse a mem-initializer.
7330 mem-initializer-id ( expression-list [opt] )
7335 ( expression-list [opt] )
7337 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7338 class) or FIELD_DECL (for a non-static data member) to initialize;
7339 the TREE_VALUE is the expression-list. */
7342 cp_parser_mem_initializer (cp_parser
* parser
)
7344 tree mem_initializer_id
;
7345 tree expression_list
;
7348 /* Find out what is being initialized. */
7349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7351 pedwarn ("anachronistic old-style base class initializer");
7352 mem_initializer_id
= NULL_TREE
;
7355 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7356 member
= expand_member_init (mem_initializer_id
);
7357 if (member
&& !DECL_P (member
))
7358 in_base_initializer
= 1;
7361 = cp_parser_parenthesized_expression_list (parser
, false,
7362 /*non_constant_p=*/NULL
);
7363 if (!expression_list
)
7364 expression_list
= void_type_node
;
7366 in_base_initializer
= 0;
7368 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7371 /* Parse a mem-initializer-id.
7374 :: [opt] nested-name-specifier [opt] class-name
7377 Returns a TYPE indicating the class to be initializer for the first
7378 production. Returns an IDENTIFIER_NODE indicating the data member
7379 to be initialized for the second production. */
7382 cp_parser_mem_initializer_id (cp_parser
* parser
)
7384 bool global_scope_p
;
7385 bool nested_name_specifier_p
;
7386 bool template_p
= false;
7389 /* `typename' is not allowed in this context ([temp.res]). */
7390 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
7392 error ("keyword `typename' not allowed in this context (a qualified "
7393 "member initializer is implicitly a type)");
7394 cp_lexer_consume_token (parser
->lexer
);
7396 /* Look for the optional `::' operator. */
7398 = (cp_parser_global_scope_opt (parser
,
7399 /*current_scope_valid_p=*/false)
7401 /* Look for the optional nested-name-specifier. The simplest way to
7406 The keyword `typename' is not permitted in a base-specifier or
7407 mem-initializer; in these contexts a qualified name that
7408 depends on a template-parameter is implicitly assumed to be a
7411 is to assume that we have seen the `typename' keyword at this
7413 nested_name_specifier_p
7414 = (cp_parser_nested_name_specifier_opt (parser
,
7415 /*typename_keyword_p=*/true,
7416 /*check_dependency_p=*/true,
7418 /*is_declaration=*/true)
7420 if (nested_name_specifier_p
)
7421 template_p
= cp_parser_optional_template_keyword (parser
);
7422 /* If there is a `::' operator or a nested-name-specifier, then we
7423 are definitely looking for a class-name. */
7424 if (global_scope_p
|| nested_name_specifier_p
)
7425 return cp_parser_class_name (parser
,
7426 /*typename_keyword_p=*/true,
7427 /*template_keyword_p=*/template_p
,
7429 /*check_dependency_p=*/true,
7430 /*class_head_p=*/false,
7431 /*is_declaration=*/true);
7432 /* Otherwise, we could also be looking for an ordinary identifier. */
7433 cp_parser_parse_tentatively (parser
);
7434 /* Try a class-name. */
7435 id
= cp_parser_class_name (parser
,
7436 /*typename_keyword_p=*/true,
7437 /*template_keyword_p=*/false,
7439 /*check_dependency_p=*/true,
7440 /*class_head_p=*/false,
7441 /*is_declaration=*/true);
7442 /* If we found one, we're done. */
7443 if (cp_parser_parse_definitely (parser
))
7445 /* Otherwise, look for an ordinary identifier. */
7446 return cp_parser_identifier (parser
);
7449 /* Overloading [gram.over] */
7451 /* Parse an operator-function-id.
7453 operator-function-id:
7456 Returns an IDENTIFIER_NODE for the operator which is a
7457 human-readable spelling of the identifier, e.g., `operator +'. */
7460 cp_parser_operator_function_id (cp_parser
* parser
)
7462 /* Look for the `operator' keyword. */
7463 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7464 return error_mark_node
;
7465 /* And then the name of the operator itself. */
7466 return cp_parser_operator (parser
);
7469 /* Parse an operator.
7472 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7473 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7474 || ++ -- , ->* -> () []
7481 Returns an IDENTIFIER_NODE for the operator which is a
7482 human-readable spelling of the identifier, e.g., `operator +'. */
7485 cp_parser_operator (cp_parser
* parser
)
7487 tree id
= NULL_TREE
;
7490 /* Peek at the next token. */
7491 token
= cp_lexer_peek_token (parser
->lexer
);
7492 /* Figure out which operator we have. */
7493 switch (token
->type
)
7499 /* The keyword should be either `new' or `delete'. */
7500 if (token
->keyword
== RID_NEW
)
7502 else if (token
->keyword
== RID_DELETE
)
7507 /* Consume the `new' or `delete' token. */
7508 cp_lexer_consume_token (parser
->lexer
);
7510 /* Peek at the next token. */
7511 token
= cp_lexer_peek_token (parser
->lexer
);
7512 /* If it's a `[' token then this is the array variant of the
7514 if (token
->type
== CPP_OPEN_SQUARE
)
7516 /* Consume the `[' token. */
7517 cp_lexer_consume_token (parser
->lexer
);
7518 /* Look for the `]' token. */
7519 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7520 id
= ansi_opname (op
== NEW_EXPR
7521 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7523 /* Otherwise, we have the non-array variant. */
7525 id
= ansi_opname (op
);
7531 id
= ansi_opname (PLUS_EXPR
);
7535 id
= ansi_opname (MINUS_EXPR
);
7539 id
= ansi_opname (MULT_EXPR
);
7543 id
= ansi_opname (TRUNC_DIV_EXPR
);
7547 id
= ansi_opname (TRUNC_MOD_EXPR
);
7551 id
= ansi_opname (BIT_XOR_EXPR
);
7555 id
= ansi_opname (BIT_AND_EXPR
);
7559 id
= ansi_opname (BIT_IOR_EXPR
);
7563 id
= ansi_opname (BIT_NOT_EXPR
);
7567 id
= ansi_opname (TRUTH_NOT_EXPR
);
7571 id
= ansi_assopname (NOP_EXPR
);
7575 id
= ansi_opname (LT_EXPR
);
7579 id
= ansi_opname (GT_EXPR
);
7583 id
= ansi_assopname (PLUS_EXPR
);
7587 id
= ansi_assopname (MINUS_EXPR
);
7591 id
= ansi_assopname (MULT_EXPR
);
7595 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7599 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7603 id
= ansi_assopname (BIT_XOR_EXPR
);
7607 id
= ansi_assopname (BIT_AND_EXPR
);
7611 id
= ansi_assopname (BIT_IOR_EXPR
);
7615 id
= ansi_opname (LSHIFT_EXPR
);
7619 id
= ansi_opname (RSHIFT_EXPR
);
7623 id
= ansi_assopname (LSHIFT_EXPR
);
7627 id
= ansi_assopname (RSHIFT_EXPR
);
7631 id
= ansi_opname (EQ_EXPR
);
7635 id
= ansi_opname (NE_EXPR
);
7639 id
= ansi_opname (LE_EXPR
);
7642 case CPP_GREATER_EQ
:
7643 id
= ansi_opname (GE_EXPR
);
7647 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7651 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7655 id
= ansi_opname (POSTINCREMENT_EXPR
);
7658 case CPP_MINUS_MINUS
:
7659 id
= ansi_opname (PREDECREMENT_EXPR
);
7663 id
= ansi_opname (COMPOUND_EXPR
);
7666 case CPP_DEREF_STAR
:
7667 id
= ansi_opname (MEMBER_REF
);
7671 id
= ansi_opname (COMPONENT_REF
);
7674 case CPP_OPEN_PAREN
:
7675 /* Consume the `('. */
7676 cp_lexer_consume_token (parser
->lexer
);
7677 /* Look for the matching `)'. */
7678 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7679 return ansi_opname (CALL_EXPR
);
7681 case CPP_OPEN_SQUARE
:
7682 /* Consume the `['. */
7683 cp_lexer_consume_token (parser
->lexer
);
7684 /* Look for the matching `]'. */
7685 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7686 return ansi_opname (ARRAY_REF
);
7690 id
= ansi_opname (MIN_EXPR
);
7694 id
= ansi_opname (MAX_EXPR
);
7698 id
= ansi_assopname (MIN_EXPR
);
7702 id
= ansi_assopname (MAX_EXPR
);
7706 /* Anything else is an error. */
7710 /* If we have selected an identifier, we need to consume the
7713 cp_lexer_consume_token (parser
->lexer
);
7714 /* Otherwise, no valid operator name was present. */
7717 cp_parser_error (parser
, "expected operator");
7718 id
= error_mark_node
;
7724 /* Parse a template-declaration.
7726 template-declaration:
7727 export [opt] template < template-parameter-list > declaration
7729 If MEMBER_P is TRUE, this template-declaration occurs within a
7732 The grammar rule given by the standard isn't correct. What
7735 template-declaration:
7736 export [opt] template-parameter-list-seq
7737 decl-specifier-seq [opt] init-declarator [opt] ;
7738 export [opt] template-parameter-list-seq
7741 template-parameter-list-seq:
7742 template-parameter-list-seq [opt]
7743 template < template-parameter-list > */
7746 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7748 /* Check for `export'. */
7749 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7751 /* Consume the `export' token. */
7752 cp_lexer_consume_token (parser
->lexer
);
7753 /* Warn that we do not support `export'. */
7754 warning ("keyword `export' not implemented, and will be ignored");
7757 cp_parser_template_declaration_after_export (parser
, member_p
);
7760 /* Parse a template-parameter-list.
7762 template-parameter-list:
7764 template-parameter-list , template-parameter
7766 Returns a TREE_LIST. Each node represents a template parameter.
7767 The nodes are connected via their TREE_CHAINs. */
7770 cp_parser_template_parameter_list (cp_parser
* parser
)
7772 tree parameter_list
= NULL_TREE
;
7779 /* Parse the template-parameter. */
7780 parameter
= cp_parser_template_parameter (parser
);
7781 /* Add it to the list. */
7782 parameter_list
= process_template_parm (parameter_list
,
7785 /* Peek at the next token. */
7786 token
= cp_lexer_peek_token (parser
->lexer
);
7787 /* If it's not a `,', we're done. */
7788 if (token
->type
!= CPP_COMMA
)
7790 /* Otherwise, consume the `,' token. */
7791 cp_lexer_consume_token (parser
->lexer
);
7794 return parameter_list
;
7797 /* Parse a template-parameter.
7801 parameter-declaration
7803 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7804 TREE_PURPOSE is the default value, if any. */
7807 cp_parser_template_parameter (cp_parser
* parser
)
7811 /* Peek at the next token. */
7812 token
= cp_lexer_peek_token (parser
->lexer
);
7813 /* If it is `class' or `template', we have a type-parameter. */
7814 if (token
->keyword
== RID_TEMPLATE
)
7815 return cp_parser_type_parameter (parser
);
7816 /* If it is `class' or `typename' we do not know yet whether it is a
7817 type parameter or a non-type parameter. Consider:
7819 template <typename T, typename T::X X> ...
7823 template <class C, class D*> ...
7825 Here, the first parameter is a type parameter, and the second is
7826 a non-type parameter. We can tell by looking at the token after
7827 the identifier -- if it is a `,', `=', or `>' then we have a type
7829 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7831 /* Peek at the token after `class' or `typename'. */
7832 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7833 /* If it's an identifier, skip it. */
7834 if (token
->type
== CPP_NAME
)
7835 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7836 /* Now, see if the token looks like the end of a template
7838 if (token
->type
== CPP_COMMA
7839 || token
->type
== CPP_EQ
7840 || token
->type
== CPP_GREATER
)
7841 return cp_parser_type_parameter (parser
);
7844 /* Otherwise, it is a non-type parameter.
7848 When parsing a default template-argument for a non-type
7849 template-parameter, the first non-nested `>' is taken as the end
7850 of the template parameter-list rather than a greater-than
7853 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
7854 /*parenthesized_p=*/NULL
);
7857 /* Parse a type-parameter.
7860 class identifier [opt]
7861 class identifier [opt] = type-id
7862 typename identifier [opt]
7863 typename identifier [opt] = type-id
7864 template < template-parameter-list > class identifier [opt]
7865 template < template-parameter-list > class identifier [opt]
7868 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7869 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7870 the declaration of the parameter. */
7873 cp_parser_type_parameter (cp_parser
* parser
)
7878 /* Look for a keyword to tell us what kind of parameter this is. */
7879 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7880 "`class', `typename', or `template'");
7882 return error_mark_node
;
7884 switch (token
->keyword
)
7890 tree default_argument
;
7892 /* If the next token is an identifier, then it names the
7894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7895 identifier
= cp_parser_identifier (parser
);
7897 identifier
= NULL_TREE
;
7899 /* Create the parameter. */
7900 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7902 /* If the next token is an `=', we have a default argument. */
7903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7905 /* Consume the `=' token. */
7906 cp_lexer_consume_token (parser
->lexer
);
7907 /* Parse the default-argument. */
7908 default_argument
= cp_parser_type_id (parser
);
7911 default_argument
= NULL_TREE
;
7913 /* Create the combined representation of the parameter and the
7914 default argument. */
7915 parameter
= build_tree_list (default_argument
, parameter
);
7921 tree parameter_list
;
7923 tree default_argument
;
7925 /* Look for the `<'. */
7926 cp_parser_require (parser
, CPP_LESS
, "`<'");
7927 /* Parse the template-parameter-list. */
7928 begin_template_parm_list ();
7930 = cp_parser_template_parameter_list (parser
);
7931 parameter_list
= end_template_parm_list (parameter_list
);
7932 /* Look for the `>'. */
7933 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7934 /* Look for the `class' keyword. */
7935 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7936 /* If the next token is an `=', then there is a
7937 default-argument. If the next token is a `>', we are at
7938 the end of the parameter-list. If the next token is a `,',
7939 then we are at the end of this parameter. */
7940 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7941 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7942 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7943 identifier
= cp_parser_identifier (parser
);
7945 identifier
= NULL_TREE
;
7946 /* Create the template parameter. */
7947 parameter
= finish_template_template_parm (class_type_node
,
7950 /* If the next token is an `=', then there is a
7951 default-argument. */
7952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7956 /* Consume the `='. */
7957 cp_lexer_consume_token (parser
->lexer
);
7958 /* Parse the id-expression. */
7960 = cp_parser_id_expression (parser
,
7961 /*template_keyword_p=*/false,
7962 /*check_dependency_p=*/true,
7963 /*template_p=*/&is_template
,
7964 /*declarator_p=*/false);
7965 if (TREE_CODE (default_argument
) == TYPE_DECL
)
7966 /* If the id-expression was a template-id that refers to
7967 a template-class, we already have the declaration here,
7968 so no further lookup is needed. */
7971 /* Look up the name. */
7973 = cp_parser_lookup_name (parser
, default_argument
,
7975 /*is_template=*/is_template
,
7976 /*is_namespace=*/false,
7977 /*check_dependency=*/true);
7978 /* See if the default argument is valid. */
7980 = check_template_template_default_arg (default_argument
);
7983 default_argument
= NULL_TREE
;
7985 /* Create the combined representation of the parameter and the
7986 default argument. */
7987 parameter
= build_tree_list (default_argument
, parameter
);
7992 /* Anything else is an error. */
7993 cp_parser_error (parser
,
7994 "expected `class', `typename', or `template'");
7995 parameter
= error_mark_node
;
8001 /* Parse a template-id.
8004 template-name < template-argument-list [opt] >
8006 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8007 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8008 returned. Otherwise, if the template-name names a function, or set
8009 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8010 names a class, returns a TYPE_DECL for the specialization.
8012 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8013 uninstantiated templates. */
8016 cp_parser_template_id (cp_parser
*parser
,
8017 bool template_keyword_p
,
8018 bool check_dependency_p
,
8019 bool is_declaration
)
8024 ptrdiff_t start_of_id
;
8025 tree access_check
= NULL_TREE
;
8026 cp_token
*next_token
, *next_token_2
;
8029 /* If the next token corresponds to a template-id, there is no need
8031 next_token
= cp_lexer_peek_token (parser
->lexer
);
8032 if (next_token
->type
== CPP_TEMPLATE_ID
)
8037 /* Get the stored value. */
8038 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8039 /* Perform any access checks that were deferred. */
8040 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8041 perform_or_defer_access_check (TREE_PURPOSE (check
),
8042 TREE_VALUE (check
));
8043 /* Return the stored value. */
8044 return TREE_VALUE (value
);
8047 /* Avoid performing name lookup if there is no possibility of
8048 finding a template-id. */
8049 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8050 || (next_token
->type
== CPP_NAME
8051 && !cp_parser_nth_token_starts_template_argument_list_p
8054 cp_parser_error (parser
, "expected template-id");
8055 return error_mark_node
;
8058 /* Remember where the template-id starts. */
8059 if (cp_parser_parsing_tentatively (parser
)
8060 && !cp_parser_committed_to_tentative_parse (parser
))
8062 next_token
= cp_lexer_peek_token (parser
->lexer
);
8063 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
8064 parser
->lexer
->first_token
,
8070 push_deferring_access_checks (dk_deferred
);
8072 /* Parse the template-name. */
8073 is_identifier
= false;
8074 template = cp_parser_template_name (parser
, template_keyword_p
,
8078 if (template == error_mark_node
|| is_identifier
)
8080 pop_deferring_access_checks ();
8084 /* If we find the sequence `[:' after a template-name, it's probably
8085 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8086 parse correctly the argument list. */
8087 next_token
= cp_lexer_peek_nth_token (parser
->lexer
, 1);
8088 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8089 if (next_token
->type
== CPP_OPEN_SQUARE
8090 && next_token
->flags
& DIGRAPH
8091 && next_token_2
->type
== CPP_COLON
8092 && !(next_token_2
->flags
& PREV_WHITE
))
8094 cp_parser_parse_tentatively (parser
);
8095 /* Change `:' into `::'. */
8096 next_token_2
->type
= CPP_SCOPE
;
8097 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8099 cp_lexer_consume_token (parser
->lexer
);
8100 /* Parse the arguments. */
8101 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8102 if (!cp_parser_parse_definitely (parser
))
8104 /* If we couldn't parse an argument list, then we revert our changes
8105 and return simply an error. Maybe this is not a template-id
8107 next_token_2
->type
= CPP_COLON
;
8108 cp_parser_error (parser
, "expected `<'");
8109 pop_deferring_access_checks ();
8110 return error_mark_node
;
8112 /* Otherwise, emit an error about the invalid digraph, but continue
8113 parsing because we got our argument list. */
8114 pedwarn ("`<::' cannot begin a template-argument list");
8115 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8116 "between `<' and `::'");
8117 if (!flag_permissive
)
8122 inform ("(if you use `-fpermissive' G++ will accept your code)");
8129 /* Look for the `<' that starts the template-argument-list. */
8130 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8132 pop_deferring_access_checks ();
8133 return error_mark_node
;
8135 /* Parse the arguments. */
8136 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8139 /* Build a representation of the specialization. */
8140 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8141 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8142 else if (DECL_CLASS_TEMPLATE_P (template)
8143 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8145 = finish_template_type (template, arguments
,
8146 cp_lexer_next_token_is (parser
->lexer
,
8150 /* If it's not a class-template or a template-template, it should be
8151 a function-template. */
8152 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8153 || TREE_CODE (template) == OVERLOAD
8154 || BASELINK_P (template)),
8157 template_id
= lookup_template_function (template, arguments
);
8160 /* Retrieve any deferred checks. Do not pop this access checks yet
8161 so the memory will not be reclaimed during token replacing below. */
8162 access_check
= get_deferred_access_checks ();
8164 /* If parsing tentatively, replace the sequence of tokens that makes
8165 up the template-id with a CPP_TEMPLATE_ID token. That way,
8166 should we re-parse the token stream, we will not have to repeat
8167 the effort required to do the parse, nor will we issue duplicate
8168 error messages about problems during instantiation of the
8170 if (start_of_id
>= 0)
8174 /* Find the token that corresponds to the start of the
8176 token
= cp_lexer_advance_token (parser
->lexer
,
8177 parser
->lexer
->first_token
,
8180 /* Reset the contents of the START_OF_ID token. */
8181 token
->type
= CPP_TEMPLATE_ID
;
8182 token
->value
= build_tree_list (access_check
, template_id
);
8183 token
->keyword
= RID_MAX
;
8184 /* Purge all subsequent tokens. */
8185 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8188 pop_deferring_access_checks ();
8192 /* Parse a template-name.
8197 The standard should actually say:
8201 operator-function-id
8203 A defect report has been filed about this issue.
8205 A conversion-function-id cannot be a template name because they cannot
8206 be part of a template-id. In fact, looking at this code:
8210 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8211 It is impossible to call a templated conversion-function-id with an
8212 explicit argument list, since the only allowed template parameter is
8213 the type to which it is converting.
8215 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8216 `template' keyword, in a construction like:
8220 In that case `f' is taken to be a template-name, even though there
8221 is no way of knowing for sure.
8223 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8224 name refers to a set of overloaded functions, at least one of which
8225 is a template, or an IDENTIFIER_NODE with the name of the template,
8226 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8227 names are looked up inside uninstantiated templates. */
8230 cp_parser_template_name (cp_parser
* parser
,
8231 bool template_keyword_p
,
8232 bool check_dependency_p
,
8233 bool is_declaration
,
8234 bool *is_identifier
)
8240 /* If the next token is `operator', then we have either an
8241 operator-function-id or a conversion-function-id. */
8242 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8244 /* We don't know whether we're looking at an
8245 operator-function-id or a conversion-function-id. */
8246 cp_parser_parse_tentatively (parser
);
8247 /* Try an operator-function-id. */
8248 identifier
= cp_parser_operator_function_id (parser
);
8249 /* If that didn't work, try a conversion-function-id. */
8250 if (!cp_parser_parse_definitely (parser
))
8252 cp_parser_error (parser
, "expected template-name");
8253 return error_mark_node
;
8256 /* Look for the identifier. */
8258 identifier
= cp_parser_identifier (parser
);
8260 /* If we didn't find an identifier, we don't have a template-id. */
8261 if (identifier
== error_mark_node
)
8262 return error_mark_node
;
8264 /* If the name immediately followed the `template' keyword, then it
8265 is a template-name. However, if the next token is not `<', then
8266 we do not treat it as a template-name, since it is not being used
8267 as part of a template-id. This enables us to handle constructs
8270 template <typename T> struct S { S(); };
8271 template <typename T> S<T>::S();
8273 correctly. We would treat `S' as a template -- if it were `S<T>'
8274 -- but we do not if there is no `<'. */
8276 if (processing_template_decl
8277 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8279 /* In a declaration, in a dependent context, we pretend that the
8280 "template" keyword was present in order to improve error
8281 recovery. For example, given:
8283 template <typename T> void f(T::X<int>);
8285 we want to treat "X<int>" as a template-id. */
8287 && !template_keyword_p
8288 && parser
->scope
&& TYPE_P (parser
->scope
)
8289 && dependent_type_p (parser
->scope
))
8293 /* Explain what went wrong. */
8294 error ("non-template `%D' used as template", identifier
);
8295 error ("(use `%T::template %D' to indicate that it is a template)",
8296 parser
->scope
, identifier
);
8297 /* If parsing tentatively, find the location of the "<"
8299 if (cp_parser_parsing_tentatively (parser
)
8300 && !cp_parser_committed_to_tentative_parse (parser
))
8302 cp_parser_simulate_error (parser
);
8303 token
= cp_lexer_peek_token (parser
->lexer
);
8304 token
= cp_lexer_prev_token (parser
->lexer
, token
);
8305 start
= cp_lexer_token_difference (parser
->lexer
,
8306 parser
->lexer
->first_token
,
8311 /* Parse the template arguments so that we can issue error
8312 messages about them. */
8313 cp_lexer_consume_token (parser
->lexer
);
8314 cp_parser_enclosed_template_argument_list (parser
);
8315 /* Skip tokens until we find a good place from which to
8316 continue parsing. */
8317 cp_parser_skip_to_closing_parenthesis (parser
,
8318 /*recovering=*/true,
8320 /*consume_paren=*/false);
8321 /* If parsing tentatively, permanently remove the
8322 template argument list. That will prevent duplicate
8323 error messages from being issued about the missing
8324 "template" keyword. */
8327 token
= cp_lexer_advance_token (parser
->lexer
,
8328 parser
->lexer
->first_token
,
8330 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8333 *is_identifier
= true;
8337 /* If the "template" keyword is present, then there is generally
8338 no point in doing name-lookup, so we just return IDENTIFIER.
8339 But, if the qualifying scope is non-dependent then we can
8340 (and must) do name-lookup normally. */
8341 if (template_keyword_p
8343 || (TYPE_P (parser
->scope
)
8344 && dependent_type_p (parser
->scope
))))
8348 /* Look up the name. */
8349 decl
= cp_parser_lookup_name (parser
, identifier
,
8351 /*is_template=*/false,
8352 /*is_namespace=*/false,
8353 check_dependency_p
);
8354 decl
= maybe_get_template_decl_from_type_decl (decl
);
8356 /* If DECL is a template, then the name was a template-name. */
8357 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8361 /* The standard does not explicitly indicate whether a name that
8362 names a set of overloaded declarations, some of which are
8363 templates, is a template-name. However, such a name should
8364 be a template-name; otherwise, there is no way to form a
8365 template-id for the overloaded templates. */
8366 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8367 if (TREE_CODE (fns
) == OVERLOAD
)
8371 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8372 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8377 /* Otherwise, the name does not name a template. */
8378 cp_parser_error (parser
, "expected template-name");
8379 return error_mark_node
;
8383 /* If DECL is dependent, and refers to a function, then just return
8384 its name; we will look it up again during template instantiation. */
8385 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8387 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8388 if (TYPE_P (scope
) && dependent_type_p (scope
))
8395 /* Parse a template-argument-list.
8397 template-argument-list:
8399 template-argument-list , template-argument
8401 Returns a TREE_VEC containing the arguments. */
8404 cp_parser_template_argument_list (cp_parser
* parser
)
8406 tree fixed_args
[10];
8407 unsigned n_args
= 0;
8408 unsigned alloced
= 10;
8409 tree
*arg_ary
= fixed_args
;
8411 bool saved_in_template_argument_list_p
;
8413 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8414 parser
->in_template_argument_list_p
= true;
8420 /* Consume the comma. */
8421 cp_lexer_consume_token (parser
->lexer
);
8423 /* Parse the template-argument. */
8424 argument
= cp_parser_template_argument (parser
);
8425 if (n_args
== alloced
)
8429 if (arg_ary
== fixed_args
)
8431 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8432 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8435 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8437 arg_ary
[n_args
++] = argument
;
8439 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8441 vec
= make_tree_vec (n_args
);
8444 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8446 if (arg_ary
!= fixed_args
)
8448 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8452 /* Parse a template-argument.
8455 assignment-expression
8459 The representation is that of an assignment-expression, type-id, or
8460 id-expression -- except that the qualified id-expression is
8461 evaluated, so that the value returned is either a DECL or an
8464 Although the standard says "assignment-expression", it forbids
8465 throw-expressions or assignments in the template argument.
8466 Therefore, we use "conditional-expression" instead. */
8469 cp_parser_template_argument (cp_parser
* parser
)
8474 bool maybe_type_id
= false;
8477 tree qualifying_class
;
8479 /* There's really no way to know what we're looking at, so we just
8480 try each alternative in order.
8484 In a template-argument, an ambiguity between a type-id and an
8485 expression is resolved to a type-id, regardless of the form of
8486 the corresponding template-parameter.
8488 Therefore, we try a type-id first. */
8489 cp_parser_parse_tentatively (parser
);
8490 argument
= cp_parser_type_id (parser
);
8491 /* If there was no error parsing the type-id but the next token is a '>>',
8492 we probably found a typo for '> >'. But there are type-id which are
8493 also valid expressions. For instance:
8495 struct X { int operator >> (int); };
8496 template <int V> struct Foo {};
8499 Here 'X()' is a valid type-id of a function type, but the user just
8500 wanted to write the expression "X() >> 5". Thus, we remember that we
8501 found a valid type-id, but we still try to parse the argument as an
8502 expression to see what happens. */
8503 if (!cp_parser_error_occurred (parser
)
8504 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8506 maybe_type_id
= true;
8507 cp_parser_abort_tentative_parse (parser
);
8511 /* If the next token isn't a `,' or a `>', then this argument wasn't
8512 really finished. This means that the argument is not a valid
8514 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8515 cp_parser_error (parser
, "expected template-argument");
8516 /* If that worked, we're done. */
8517 if (cp_parser_parse_definitely (parser
))
8520 /* We're still not sure what the argument will be. */
8521 cp_parser_parse_tentatively (parser
);
8522 /* Try a template. */
8523 argument
= cp_parser_id_expression (parser
,
8524 /*template_keyword_p=*/false,
8525 /*check_dependency_p=*/true,
8527 /*declarator_p=*/false);
8528 /* If the next token isn't a `,' or a `>', then this argument wasn't
8530 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8531 cp_parser_error (parser
, "expected template-argument");
8532 if (!cp_parser_error_occurred (parser
))
8534 /* Figure out what is being referred to. If the id-expression
8535 was for a class template specialization, then we will have a
8536 TYPE_DECL at this point. There is no need to do name lookup
8537 at this point in that case. */
8538 if (TREE_CODE (argument
) != TYPE_DECL
)
8539 argument
= cp_parser_lookup_name (parser
, argument
,
8541 /*is_template=*/template_p
,
8542 /*is_namespace=*/false,
8543 /*check_dependency=*/true);
8544 if (TREE_CODE (argument
) != TEMPLATE_DECL
8545 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8546 cp_parser_error (parser
, "expected template-name");
8548 if (cp_parser_parse_definitely (parser
))
8550 /* It must be a non-type argument. There permitted cases are given
8551 in [temp.arg.nontype]:
8553 -- an integral constant-expression of integral or enumeration
8556 -- the name of a non-type template-parameter; or
8558 -- the name of an object or function with external linkage...
8560 -- the address of an object or function with external linkage...
8562 -- a pointer to member... */
8563 /* Look for a non-type template parameter. */
8564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8566 cp_parser_parse_tentatively (parser
);
8567 argument
= cp_parser_primary_expression (parser
,
8570 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8571 || !cp_parser_next_token_ends_template_argument_p (parser
))
8572 cp_parser_simulate_error (parser
);
8573 if (cp_parser_parse_definitely (parser
))
8576 /* If the next token is "&", the argument must be the address of an
8577 object or function with external linkage. */
8578 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8580 cp_lexer_consume_token (parser
->lexer
);
8581 /* See if we might have an id-expression. */
8582 token
= cp_lexer_peek_token (parser
->lexer
);
8583 if (token
->type
== CPP_NAME
8584 || token
->keyword
== RID_OPERATOR
8585 || token
->type
== CPP_SCOPE
8586 || token
->type
== CPP_TEMPLATE_ID
8587 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8589 cp_parser_parse_tentatively (parser
);
8590 argument
= cp_parser_primary_expression (parser
,
8593 if (cp_parser_error_occurred (parser
)
8594 || !cp_parser_next_token_ends_template_argument_p (parser
))
8595 cp_parser_abort_tentative_parse (parser
);
8598 if (qualifying_class
)
8599 argument
= finish_qualified_id_expr (qualifying_class
,
8603 if (TREE_CODE (argument
) == VAR_DECL
)
8605 /* A variable without external linkage might still be a
8606 valid constant-expression, so no error is issued here
8607 if the external-linkage check fails. */
8608 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8609 cp_parser_simulate_error (parser
);
8611 else if (is_overloaded_fn (argument
))
8612 /* All overloaded functions are allowed; if the external
8613 linkage test does not pass, an error will be issued
8617 && (TREE_CODE (argument
) == OFFSET_REF
8618 || TREE_CODE (argument
) == SCOPE_REF
))
8619 /* A pointer-to-member. */
8622 cp_parser_simulate_error (parser
);
8624 if (cp_parser_parse_definitely (parser
))
8627 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8632 /* If the argument started with "&", there are no other valid
8633 alternatives at this point. */
8636 cp_parser_error (parser
, "invalid non-type template argument");
8637 return error_mark_node
;
8639 /* If the argument wasn't successfully parsed as a type-id followed
8640 by '>>', the argument can only be a constant expression now.
8641 Otherwise, we try parsing the constant-expression tentatively,
8642 because the argument could really be a type-id. */
8644 cp_parser_parse_tentatively (parser
);
8645 argument
= cp_parser_constant_expression (parser
,
8646 /*allow_non_constant_p=*/false,
8647 /*non_constant_p=*/NULL
);
8648 argument
= fold_non_dependent_expr (argument
);
8651 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8652 cp_parser_error (parser
, "expected template-argument");
8653 if (cp_parser_parse_definitely (parser
))
8655 /* We did our best to parse the argument as a non type-id, but that
8656 was the only alternative that matched (albeit with a '>' after
8657 it). We can assume it's just a typo from the user, and a
8658 diagnostic will then be issued. */
8659 return cp_parser_type_id (parser
);
8662 /* Parse an explicit-instantiation.
8664 explicit-instantiation:
8665 template declaration
8667 Although the standard says `declaration', what it really means is:
8669 explicit-instantiation:
8670 template decl-specifier-seq [opt] declarator [opt] ;
8672 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8673 supposed to be allowed. A defect report has been filed about this
8678 explicit-instantiation:
8679 storage-class-specifier template
8680 decl-specifier-seq [opt] declarator [opt] ;
8681 function-specifier template
8682 decl-specifier-seq [opt] declarator [opt] ; */
8685 cp_parser_explicit_instantiation (cp_parser
* parser
)
8687 int declares_class_or_enum
;
8688 tree decl_specifiers
;
8690 tree extension_specifier
= NULL_TREE
;
8692 /* Look for an (optional) storage-class-specifier or
8693 function-specifier. */
8694 if (cp_parser_allow_gnu_extensions_p (parser
))
8697 = cp_parser_storage_class_specifier_opt (parser
);
8698 if (!extension_specifier
)
8699 extension_specifier
= cp_parser_function_specifier_opt (parser
);
8702 /* Look for the `template' keyword. */
8703 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8704 /* Let the front end know that we are processing an explicit
8706 begin_explicit_instantiation ();
8707 /* [temp.explicit] says that we are supposed to ignore access
8708 control while processing explicit instantiation directives. */
8709 push_deferring_access_checks (dk_no_check
);
8710 /* Parse a decl-specifier-seq. */
8712 = cp_parser_decl_specifier_seq (parser
,
8713 CP_PARSER_FLAGS_OPTIONAL
,
8715 &declares_class_or_enum
);
8716 /* If there was exactly one decl-specifier, and it declared a class,
8717 and there's no declarator, then we have an explicit type
8719 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8723 type
= check_tag_decl (decl_specifiers
);
8724 /* Turn access control back on for names used during
8725 template instantiation. */
8726 pop_deferring_access_checks ();
8728 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8735 /* Parse the declarator. */
8737 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8738 /*ctor_dtor_or_conv_p=*/NULL
,
8739 /*parenthesized_p=*/NULL
);
8740 cp_parser_check_for_definition_in_return_type (declarator
,
8741 declares_class_or_enum
);
8742 if (declarator
!= error_mark_node
)
8744 decl
= grokdeclarator (declarator
, decl_specifiers
,
8746 /* Turn access control back on for names used during
8747 template instantiation. */
8748 pop_deferring_access_checks ();
8749 /* Do the explicit instantiation. */
8750 do_decl_instantiation (decl
, extension_specifier
);
8754 pop_deferring_access_checks ();
8755 /* Skip the body of the explicit instantiation. */
8756 cp_parser_skip_to_end_of_statement (parser
);
8759 /* We're done with the instantiation. */
8760 end_explicit_instantiation ();
8762 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8765 /* Parse an explicit-specialization.
8767 explicit-specialization:
8768 template < > declaration
8770 Although the standard says `declaration', what it really means is:
8772 explicit-specialization:
8773 template <> decl-specifier [opt] init-declarator [opt] ;
8774 template <> function-definition
8775 template <> explicit-specialization
8776 template <> template-declaration */
8779 cp_parser_explicit_specialization (cp_parser
* parser
)
8781 /* Look for the `template' keyword. */
8782 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8783 /* Look for the `<'. */
8784 cp_parser_require (parser
, CPP_LESS
, "`<'");
8785 /* Look for the `>'. */
8786 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8787 /* We have processed another parameter list. */
8788 ++parser
->num_template_parameter_lists
;
8789 /* Let the front end know that we are beginning a specialization. */
8790 begin_specialization ();
8792 /* If the next keyword is `template', we need to figure out whether
8793 or not we're looking a template-declaration. */
8794 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8796 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8797 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8798 cp_parser_template_declaration_after_export (parser
,
8799 /*member_p=*/false);
8801 cp_parser_explicit_specialization (parser
);
8804 /* Parse the dependent declaration. */
8805 cp_parser_single_declaration (parser
,
8809 /* We're done with the specialization. */
8810 end_specialization ();
8811 /* We're done with this parameter list. */
8812 --parser
->num_template_parameter_lists
;
8815 /* Parse a type-specifier.
8818 simple-type-specifier
8821 elaborated-type-specifier
8829 Returns a representation of the type-specifier. If the
8830 type-specifier is a keyword (like `int' or `const', or
8831 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8832 For a class-specifier, enum-specifier, or elaborated-type-specifier
8833 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8835 If IS_FRIEND is TRUE then this type-specifier is being declared a
8836 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8837 appearing in a decl-specifier-seq.
8839 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8840 class-specifier, enum-specifier, or elaborated-type-specifier, then
8841 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8842 if a type is declared; 2 if it is defined. Otherwise, it is set to
8845 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8846 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8850 cp_parser_type_specifier (cp_parser
* parser
,
8851 cp_parser_flags flags
,
8853 bool is_declaration
,
8854 int* declares_class_or_enum
,
8855 bool* is_cv_qualifier
)
8857 tree type_spec
= NULL_TREE
;
8861 /* Assume this type-specifier does not declare a new type. */
8862 if (declares_class_or_enum
)
8863 *declares_class_or_enum
= 0;
8864 /* And that it does not specify a cv-qualifier. */
8865 if (is_cv_qualifier
)
8866 *is_cv_qualifier
= false;
8867 /* Peek at the next token. */
8868 token
= cp_lexer_peek_token (parser
->lexer
);
8870 /* If we're looking at a keyword, we can use that to guide the
8871 production we choose. */
8872 keyword
= token
->keyword
;
8875 /* Any of these indicate either a class-specifier, or an
8876 elaborated-type-specifier. */
8881 /* Parse tentatively so that we can back up if we don't find a
8882 class-specifier or enum-specifier. */
8883 cp_parser_parse_tentatively (parser
);
8884 /* Look for the class-specifier or enum-specifier. */
8885 if (keyword
== RID_ENUM
)
8886 type_spec
= cp_parser_enum_specifier (parser
);
8888 type_spec
= cp_parser_class_specifier (parser
);
8890 /* If that worked, we're done. */
8891 if (cp_parser_parse_definitely (parser
))
8893 if (declares_class_or_enum
)
8894 *declares_class_or_enum
= 2;
8901 /* Look for an elaborated-type-specifier. */
8902 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8905 /* We're declaring a class or enum -- unless we're using
8907 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8908 *declares_class_or_enum
= 1;
8914 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8915 /* Even though we call a routine that looks for an optional
8916 qualifier, we know that there should be one. */
8917 my_friendly_assert (type_spec
!= NULL
, 20000328);
8918 /* This type-specifier was a cv-qualified. */
8919 if (is_cv_qualifier
)
8920 *is_cv_qualifier
= true;
8925 /* The `__complex__' keyword is a GNU extension. */
8926 return cp_lexer_consume_token (parser
->lexer
)->value
;
8932 /* If we do not already have a type-specifier, assume we are looking
8933 at a simple-type-specifier. */
8934 type_spec
= cp_parser_simple_type_specifier (parser
, flags
,
8935 /*identifier_p=*/true);
8937 /* If we didn't find a type-specifier, and a type-specifier was not
8938 optional in this context, issue an error message. */
8939 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8941 cp_parser_error (parser
, "expected type specifier");
8942 return error_mark_node
;
8948 /* Parse a simple-type-specifier.
8950 simple-type-specifier:
8951 :: [opt] nested-name-specifier [opt] type-name
8952 :: [opt] nested-name-specifier template template-id
8967 simple-type-specifier:
8968 __typeof__ unary-expression
8969 __typeof__ ( type-id )
8971 For the various keywords, the value returned is simply the
8972 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8973 For the first two productions, and if IDENTIFIER_P is false, the
8974 value returned is the indicated TYPE_DECL. */
8977 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
,
8980 tree type
= NULL_TREE
;
8983 /* Peek at the next token. */
8984 token
= cp_lexer_peek_token (parser
->lexer
);
8986 /* If we're looking at a keyword, things are easy. */
8987 switch (token
->keyword
)
8990 type
= char_type_node
;
8993 type
= wchar_type_node
;
8996 type
= boolean_type_node
;
8999 type
= short_integer_type_node
;
9002 type
= integer_type_node
;
9005 type
= long_integer_type_node
;
9008 type
= integer_type_node
;
9011 type
= unsigned_type_node
;
9014 type
= float_type_node
;
9017 type
= double_type_node
;
9020 type
= void_type_node
;
9027 /* Consume the `typeof' token. */
9028 cp_lexer_consume_token (parser
->lexer
);
9029 /* Parse the operand to `typeof'. */
9030 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9031 /* If it is not already a TYPE, take its type. */
9032 if (!TYPE_P (operand
))
9033 operand
= finish_typeof (operand
);
9042 /* If the type-specifier was for a built-in type, we're done. */
9047 /* Consume the token. */
9048 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9050 /* There is no valid C++ program where a non-template type is
9051 followed by a "<". That usually indicates that the user thought
9052 that the type was a template. */
9053 cp_parser_check_for_invalid_template_id (parser
, type
);
9055 return identifier_p
? id
: TYPE_NAME (type
);
9058 /* The type-specifier must be a user-defined type. */
9059 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9063 /* Don't gobble tokens or issue error messages if this is an
9064 optional type-specifier. */
9065 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9066 cp_parser_parse_tentatively (parser
);
9068 /* Look for the optional `::' operator. */
9069 cp_parser_global_scope_opt (parser
,
9070 /*current_scope_valid_p=*/false);
9071 /* Look for the nested-name specifier. */
9073 = (cp_parser_nested_name_specifier_opt (parser
,
9074 /*typename_keyword_p=*/false,
9075 /*check_dependency_p=*/true,
9077 /*is_declaration=*/false)
9079 /* If we have seen a nested-name-specifier, and the next token
9080 is `template', then we are using the template-id production. */
9082 && cp_parser_optional_template_keyword (parser
))
9084 /* Look for the template-id. */
9085 type
= cp_parser_template_id (parser
,
9086 /*template_keyword_p=*/true,
9087 /*check_dependency_p=*/true,
9088 /*is_declaration=*/false);
9089 /* If the template-id did not name a type, we are out of
9091 if (TREE_CODE (type
) != TYPE_DECL
)
9093 cp_parser_error (parser
, "expected template-id for type");
9097 /* Otherwise, look for a type-name. */
9099 type
= cp_parser_type_name (parser
);
9100 /* Keep track of all name-lookups performed in class scopes. */
9103 && TREE_CODE (type
) == TYPE_DECL
9104 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9105 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9106 /* If it didn't work out, we don't have a TYPE. */
9107 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9108 && !cp_parser_parse_definitely (parser
))
9112 /* If we didn't get a type-name, issue an error message. */
9113 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9115 cp_parser_error (parser
, "expected type-name");
9116 return error_mark_node
;
9119 /* There is no valid C++ program where a non-template type is
9120 followed by a "<". That usually indicates that the user thought
9121 that the type was a template. */
9122 if (type
&& type
!= error_mark_node
)
9123 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9128 /* Parse a type-name.
9141 Returns a TYPE_DECL for the the type. */
9144 cp_parser_type_name (cp_parser
* parser
)
9149 /* We can't know yet whether it is a class-name or not. */
9150 cp_parser_parse_tentatively (parser
);
9151 /* Try a class-name. */
9152 type_decl
= cp_parser_class_name (parser
,
9153 /*typename_keyword_p=*/false,
9154 /*template_keyword_p=*/false,
9156 /*check_dependency_p=*/true,
9157 /*class_head_p=*/false,
9158 /*is_declaration=*/false);
9159 /* If it's not a class-name, keep looking. */
9160 if (!cp_parser_parse_definitely (parser
))
9162 /* It must be a typedef-name or an enum-name. */
9163 identifier
= cp_parser_identifier (parser
);
9164 if (identifier
== error_mark_node
)
9165 return error_mark_node
;
9167 /* Look up the type-name. */
9168 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9169 /* Issue an error if we did not find a type-name. */
9170 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9172 if (!cp_parser_simulate_error (parser
))
9173 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9175 type_decl
= error_mark_node
;
9177 /* Remember that the name was used in the definition of the
9178 current class so that we can check later to see if the
9179 meaning would have been different after the class was
9180 entirely defined. */
9181 else if (type_decl
!= error_mark_node
9183 maybe_note_name_used_in_class (identifier
, type_decl
);
9190 /* Parse an elaborated-type-specifier. Note that the grammar given
9191 here incorporates the resolution to DR68.
9193 elaborated-type-specifier:
9194 class-key :: [opt] nested-name-specifier [opt] identifier
9195 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9196 enum :: [opt] nested-name-specifier [opt] identifier
9197 typename :: [opt] nested-name-specifier identifier
9198 typename :: [opt] nested-name-specifier template [opt]
9203 elaborated-type-specifier:
9204 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9205 class-key attributes :: [opt] nested-name-specifier [opt]
9206 template [opt] template-id
9207 enum attributes :: [opt] nested-name-specifier [opt] identifier
9209 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9210 declared `friend'. If IS_DECLARATION is TRUE, then this
9211 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9212 something is being declared.
9214 Returns the TYPE specified. */
9217 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
9219 bool is_declaration
)
9221 enum tag_types tag_type
;
9223 tree type
= NULL_TREE
;
9224 tree attributes
= NULL_TREE
;
9226 /* See if we're looking at the `enum' keyword. */
9227 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
9229 /* Consume the `enum' token. */
9230 cp_lexer_consume_token (parser
->lexer
);
9231 /* Remember that it's an enumeration type. */
9232 tag_type
= enum_type
;
9233 /* Parse the attributes. */
9234 attributes
= cp_parser_attributes_opt (parser
);
9236 /* Or, it might be `typename'. */
9237 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9240 /* Consume the `typename' token. */
9241 cp_lexer_consume_token (parser
->lexer
);
9242 /* Remember that it's a `typename' type. */
9243 tag_type
= typename_type
;
9244 /* The `typename' keyword is only allowed in templates. */
9245 if (!processing_template_decl
)
9246 pedwarn ("using `typename' outside of template");
9248 /* Otherwise it must be a class-key. */
9251 tag_type
= cp_parser_class_key (parser
);
9252 if (tag_type
== none_type
)
9253 return error_mark_node
;
9254 /* Parse the attributes. */
9255 attributes
= cp_parser_attributes_opt (parser
);
9258 /* Look for the `::' operator. */
9259 cp_parser_global_scope_opt (parser
,
9260 /*current_scope_valid_p=*/false);
9261 /* Look for the nested-name-specifier. */
9262 if (tag_type
== typename_type
)
9264 if (cp_parser_nested_name_specifier (parser
,
9265 /*typename_keyword_p=*/true,
9266 /*check_dependency_p=*/true,
9270 return error_mark_node
;
9273 /* Even though `typename' is not present, the proposed resolution
9274 to Core Issue 180 says that in `class A<T>::B', `B' should be
9275 considered a type-name, even if `A<T>' is dependent. */
9276 cp_parser_nested_name_specifier_opt (parser
,
9277 /*typename_keyword_p=*/true,
9278 /*check_dependency_p=*/true,
9281 /* For everything but enumeration types, consider a template-id. */
9282 if (tag_type
!= enum_type
)
9284 bool template_p
= false;
9287 /* Allow the `template' keyword. */
9288 template_p
= cp_parser_optional_template_keyword (parser
);
9289 /* If we didn't see `template', we don't know if there's a
9290 template-id or not. */
9292 cp_parser_parse_tentatively (parser
);
9293 /* Parse the template-id. */
9294 decl
= cp_parser_template_id (parser
, template_p
,
9295 /*check_dependency_p=*/true,
9297 /* If we didn't find a template-id, look for an ordinary
9299 if (!template_p
&& !cp_parser_parse_definitely (parser
))
9301 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9302 in effect, then we must assume that, upon instantiation, the
9303 template will correspond to a class. */
9304 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
9305 && tag_type
== typename_type
)
9306 type
= make_typename_type (parser
->scope
, decl
,
9309 type
= TREE_TYPE (decl
);
9312 /* For an enumeration type, consider only a plain identifier. */
9315 identifier
= cp_parser_identifier (parser
);
9317 if (identifier
== error_mark_node
)
9319 parser
->scope
= NULL_TREE
;
9320 return error_mark_node
;
9323 /* For a `typename', we needn't call xref_tag. */
9324 if (tag_type
== typename_type
)
9325 return cp_parser_make_typename_type (parser
, parser
->scope
,
9327 /* Look up a qualified name in the usual way. */
9332 /* In an elaborated-type-specifier, names are assumed to name
9333 types, so we set IS_TYPE to TRUE when calling
9334 cp_parser_lookup_name. */
9335 decl
= cp_parser_lookup_name (parser
, identifier
,
9337 /*is_template=*/false,
9338 /*is_namespace=*/false,
9339 /*check_dependency=*/true);
9341 /* If we are parsing friend declaration, DECL may be a
9342 TEMPLATE_DECL tree node here. However, we need to check
9343 whether this TEMPLATE_DECL results in valid code. Consider
9344 the following example:
9347 template <class T> class C {};
9350 template <class T> friend class N::C; // #1, valid code
9352 template <class T> class Y {
9353 friend class N::C; // #2, invalid code
9356 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9357 name lookup of `N::C'. We see that friend declaration must
9358 be template for the code to be valid. Note that
9359 processing_template_decl does not work here since it is
9360 always 1 for the above two cases. */
9362 decl
= (cp_parser_maybe_treat_template_as_class
9363 (decl
, /*tag_name_p=*/is_friend
9364 && parser
->num_template_parameter_lists
));
9366 if (TREE_CODE (decl
) != TYPE_DECL
)
9368 error ("expected type-name");
9369 return error_mark_node
;
9372 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9373 check_elaborated_type_specifier
9375 (parser
->num_template_parameter_lists
9376 || DECL_SELF_REFERENCE_P (decl
)));
9378 type
= TREE_TYPE (decl
);
9382 /* An elaborated-type-specifier sometimes introduces a new type and
9383 sometimes names an existing type. Normally, the rule is that it
9384 introduces a new type only if there is not an existing type of
9385 the same name already in scope. For example, given:
9388 void f() { struct S s; }
9390 the `struct S' in the body of `f' is the same `struct S' as in
9391 the global scope; the existing definition is used. However, if
9392 there were no global declaration, this would introduce a new
9393 local class named `S'.
9395 An exception to this rule applies to the following code:
9397 namespace N { struct S; }
9399 Here, the elaborated-type-specifier names a new type
9400 unconditionally; even if there is already an `S' in the
9401 containing scope this declaration names a new type.
9402 This exception only applies if the elaborated-type-specifier
9403 forms the complete declaration:
9407 A declaration consisting solely of `class-key identifier ;' is
9408 either a redeclaration of the name in the current scope or a
9409 forward declaration of the identifier as a class name. It
9410 introduces the name into the current scope.
9412 We are in this situation precisely when the next token is a `;'.
9414 An exception to the exception is that a `friend' declaration does
9415 *not* name a new type; i.e., given:
9417 struct S { friend struct T; };
9419 `T' is not a new type in the scope of `S'.
9421 Also, `new struct S' or `sizeof (struct S)' never results in the
9422 definition of a new type; a new type can only be declared in a
9423 declaration context. */
9425 /* Warn about attributes. They are ignored. */
9427 warning ("type attributes are honored only at type definition");
9429 type
= xref_tag (tag_type
, identifier
,
9432 || cp_lexer_next_token_is_not (parser
->lexer
,
9434 parser
->num_template_parameter_lists
);
9437 if (tag_type
!= enum_type
)
9438 cp_parser_check_class_key (tag_type
, type
);
9440 /* A "<" cannot follow an elaborated type specifier. If that
9441 happens, the user was probably trying to form a template-id. */
9442 cp_parser_check_for_invalid_template_id (parser
, type
);
9447 /* Parse an enum-specifier.
9450 enum identifier [opt] { enumerator-list [opt] }
9452 Returns an ENUM_TYPE representing the enumeration. */
9455 cp_parser_enum_specifier (cp_parser
* parser
)
9458 tree identifier
= NULL_TREE
;
9461 /* Look for the `enum' keyword. */
9462 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
9463 return error_mark_node
;
9464 /* Peek at the next token. */
9465 token
= cp_lexer_peek_token (parser
->lexer
);
9467 /* See if it is an identifier. */
9468 if (token
->type
== CPP_NAME
)
9469 identifier
= cp_parser_identifier (parser
);
9471 /* Look for the `{'. */
9472 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
9473 return error_mark_node
;
9475 /* At this point, we're going ahead with the enum-specifier, even
9476 if some other problem occurs. */
9477 cp_parser_commit_to_tentative_parse (parser
);
9479 /* Issue an error message if type-definitions are forbidden here. */
9480 cp_parser_check_type_definition (parser
);
9482 /* Create the new type. */
9483 type
= start_enum (identifier
? identifier
: make_anon_name ());
9485 /* Peek at the next token. */
9486 token
= cp_lexer_peek_token (parser
->lexer
);
9487 /* If it's not a `}', then there are some enumerators. */
9488 if (token
->type
!= CPP_CLOSE_BRACE
)
9489 cp_parser_enumerator_list (parser
, type
);
9490 /* Look for the `}'. */
9491 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9493 /* Finish up the enumeration. */
9499 /* Parse an enumerator-list. The enumerators all have the indicated
9503 enumerator-definition
9504 enumerator-list , enumerator-definition */
9507 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9513 /* Parse an enumerator-definition. */
9514 cp_parser_enumerator_definition (parser
, type
);
9515 /* Peek at the next token. */
9516 token
= cp_lexer_peek_token (parser
->lexer
);
9517 /* If it's not a `,', then we've reached the end of the
9519 if (token
->type
!= CPP_COMMA
)
9521 /* Otherwise, consume the `,' and keep going. */
9522 cp_lexer_consume_token (parser
->lexer
);
9523 /* If the next token is a `}', there is a trailing comma. */
9524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9526 if (pedantic
&& !in_system_header
)
9527 pedwarn ("comma at end of enumerator list");
9533 /* Parse an enumerator-definition. The enumerator has the indicated
9536 enumerator-definition:
9538 enumerator = constant-expression
9544 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9550 /* Look for the identifier. */
9551 identifier
= cp_parser_identifier (parser
);
9552 if (identifier
== error_mark_node
)
9555 /* Peek at the next token. */
9556 token
= cp_lexer_peek_token (parser
->lexer
);
9557 /* If it's an `=', then there's an explicit value. */
9558 if (token
->type
== CPP_EQ
)
9560 /* Consume the `=' token. */
9561 cp_lexer_consume_token (parser
->lexer
);
9562 /* Parse the value. */
9563 value
= cp_parser_constant_expression (parser
,
9564 /*allow_non_constant_p=*/false,
9570 /* Create the enumerator. */
9571 build_enumerator (identifier
, value
, type
);
9574 /* Parse a namespace-name.
9577 original-namespace-name
9580 Returns the NAMESPACE_DECL for the namespace. */
9583 cp_parser_namespace_name (cp_parser
* parser
)
9586 tree namespace_decl
;
9588 /* Get the name of the namespace. */
9589 identifier
= cp_parser_identifier (parser
);
9590 if (identifier
== error_mark_node
)
9591 return error_mark_node
;
9593 /* Look up the identifier in the currently active scope. Look only
9594 for namespaces, due to:
9598 When looking up a namespace-name in a using-directive or alias
9599 definition, only namespace names are considered.
9605 During the lookup of a name preceding the :: scope resolution
9606 operator, object, function, and enumerator names are ignored.
9608 (Note that cp_parser_class_or_namespace_name only calls this
9609 function if the token after the name is the scope resolution
9611 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
9613 /*is_template=*/false,
9614 /*is_namespace=*/true,
9615 /*check_dependency=*/true);
9616 /* If it's not a namespace, issue an error. */
9617 if (namespace_decl
== error_mark_node
9618 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
9620 cp_parser_error (parser
, "expected namespace-name");
9621 namespace_decl
= error_mark_node
;
9624 return namespace_decl
;
9627 /* Parse a namespace-definition.
9629 namespace-definition:
9630 named-namespace-definition
9631 unnamed-namespace-definition
9633 named-namespace-definition:
9634 original-namespace-definition
9635 extension-namespace-definition
9637 original-namespace-definition:
9638 namespace identifier { namespace-body }
9640 extension-namespace-definition:
9641 namespace original-namespace-name { namespace-body }
9643 unnamed-namespace-definition:
9644 namespace { namespace-body } */
9647 cp_parser_namespace_definition (cp_parser
* parser
)
9651 /* Look for the `namespace' keyword. */
9652 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9654 /* Get the name of the namespace. We do not attempt to distinguish
9655 between an original-namespace-definition and an
9656 extension-namespace-definition at this point. The semantic
9657 analysis routines are responsible for that. */
9658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9659 identifier
= cp_parser_identifier (parser
);
9661 identifier
= NULL_TREE
;
9663 /* Look for the `{' to start the namespace. */
9664 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
9665 /* Start the namespace. */
9666 push_namespace (identifier
);
9667 /* Parse the body of the namespace. */
9668 cp_parser_namespace_body (parser
);
9669 /* Finish the namespace. */
9671 /* Look for the final `}'. */
9672 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9675 /* Parse a namespace-body.
9678 declaration-seq [opt] */
9681 cp_parser_namespace_body (cp_parser
* parser
)
9683 cp_parser_declaration_seq_opt (parser
);
9686 /* Parse a namespace-alias-definition.
9688 namespace-alias-definition:
9689 namespace identifier = qualified-namespace-specifier ; */
9692 cp_parser_namespace_alias_definition (cp_parser
* parser
)
9695 tree namespace_specifier
;
9697 /* Look for the `namespace' keyword. */
9698 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9699 /* Look for the identifier. */
9700 identifier
= cp_parser_identifier (parser
);
9701 if (identifier
== error_mark_node
)
9703 /* Look for the `=' token. */
9704 cp_parser_require (parser
, CPP_EQ
, "`='");
9705 /* Look for the qualified-namespace-specifier. */
9707 = cp_parser_qualified_namespace_specifier (parser
);
9708 /* Look for the `;' token. */
9709 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9711 /* Register the alias in the symbol table. */
9712 do_namespace_alias (identifier
, namespace_specifier
);
9715 /* Parse a qualified-namespace-specifier.
9717 qualified-namespace-specifier:
9718 :: [opt] nested-name-specifier [opt] namespace-name
9720 Returns a NAMESPACE_DECL corresponding to the specified
9724 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
9726 /* Look for the optional `::'. */
9727 cp_parser_global_scope_opt (parser
,
9728 /*current_scope_valid_p=*/false);
9730 /* Look for the optional nested-name-specifier. */
9731 cp_parser_nested_name_specifier_opt (parser
,
9732 /*typename_keyword_p=*/false,
9733 /*check_dependency_p=*/true,
9735 /*is_declaration=*/true);
9737 return cp_parser_namespace_name (parser
);
9740 /* Parse a using-declaration.
9743 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9744 using :: unqualified-id ; */
9747 cp_parser_using_declaration (cp_parser
* parser
)
9750 bool typename_p
= false;
9751 bool global_scope_p
;
9757 /* Look for the `using' keyword. */
9758 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9760 /* Peek at the next token. */
9761 token
= cp_lexer_peek_token (parser
->lexer
);
9762 /* See if it's `typename'. */
9763 if (token
->keyword
== RID_TYPENAME
)
9765 /* Remember that we've seen it. */
9767 /* Consume the `typename' token. */
9768 cp_lexer_consume_token (parser
->lexer
);
9771 /* Look for the optional global scope qualification. */
9773 = (cp_parser_global_scope_opt (parser
,
9774 /*current_scope_valid_p=*/false)
9777 /* If we saw `typename', or didn't see `::', then there must be a
9778 nested-name-specifier present. */
9779 if (typename_p
|| !global_scope_p
)
9780 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
9781 /*check_dependency_p=*/true,
9783 /*is_declaration=*/true);
9784 /* Otherwise, we could be in either of the two productions. In that
9785 case, treat the nested-name-specifier as optional. */
9787 qscope
= cp_parser_nested_name_specifier_opt (parser
,
9788 /*typename_keyword_p=*/false,
9789 /*check_dependency_p=*/true,
9791 /*is_declaration=*/true);
9793 qscope
= global_namespace
;
9795 /* Parse the unqualified-id. */
9796 identifier
= cp_parser_unqualified_id (parser
,
9797 /*template_keyword_p=*/false,
9798 /*check_dependency_p=*/true,
9799 /*declarator_p=*/true);
9801 /* The function we call to handle a using-declaration is different
9802 depending on what scope we are in. */
9803 if (identifier
== error_mark_node
)
9805 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
9806 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
9807 /* [namespace.udecl]
9809 A using declaration shall not name a template-id. */
9810 error ("a template-id may not appear in a using-declaration");
9813 scope
= current_scope ();
9814 if (scope
&& TYPE_P (scope
))
9816 /* Create the USING_DECL. */
9817 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9820 /* Add it to the list of members in this class. */
9821 finish_member_declaration (decl
);
9825 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9826 if (decl
== error_mark_node
)
9827 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
9829 do_local_using_decl (decl
, qscope
, identifier
);
9831 do_toplevel_using_decl (decl
, qscope
, identifier
);
9835 /* Look for the final `;'. */
9836 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9839 /* Parse a using-directive.
9842 using namespace :: [opt] nested-name-specifier [opt]
9846 cp_parser_using_directive (cp_parser
* parser
)
9848 tree namespace_decl
;
9851 /* Look for the `using' keyword. */
9852 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9853 /* And the `namespace' keyword. */
9854 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9855 /* Look for the optional `::' operator. */
9856 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9857 /* And the optional nested-name-specifier. */
9858 cp_parser_nested_name_specifier_opt (parser
,
9859 /*typename_keyword_p=*/false,
9860 /*check_dependency_p=*/true,
9862 /*is_declaration=*/true);
9863 /* Get the namespace being used. */
9864 namespace_decl
= cp_parser_namespace_name (parser
);
9865 /* And any specified attributes. */
9866 attribs
= cp_parser_attributes_opt (parser
);
9867 /* Update the symbol table. */
9868 parse_using_directive (namespace_decl
, attribs
);
9869 /* Look for the final `;'. */
9870 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9873 /* Parse an asm-definition.
9876 asm ( string-literal ) ;
9881 asm volatile [opt] ( string-literal ) ;
9882 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9883 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9884 : asm-operand-list [opt] ) ;
9885 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9886 : asm-operand-list [opt]
9887 : asm-operand-list [opt] ) ; */
9890 cp_parser_asm_definition (cp_parser
* parser
)
9894 tree outputs
= NULL_TREE
;
9895 tree inputs
= NULL_TREE
;
9896 tree clobbers
= NULL_TREE
;
9898 bool volatile_p
= false;
9899 bool extended_p
= false;
9901 /* Look for the `asm' keyword. */
9902 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9903 /* See if the next token is `volatile'. */
9904 if (cp_parser_allow_gnu_extensions_p (parser
)
9905 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9907 /* Remember that we saw the `volatile' keyword. */
9909 /* Consume the token. */
9910 cp_lexer_consume_token (parser
->lexer
);
9912 /* Look for the opening `('. */
9913 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9914 /* Look for the string. */
9915 c_lex_string_translate
= false;
9916 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9919 string
= token
->value
;
9920 /* If we're allowing GNU extensions, check for the extended assembly
9921 syntax. Unfortunately, the `:' tokens need not be separated by
9922 a space in C, and so, for compatibility, we tolerate that here
9923 too. Doing that means that we have to treat the `::' operator as
9925 if (cp_parser_allow_gnu_extensions_p (parser
)
9926 && at_function_scope_p ()
9927 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9928 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9930 bool inputs_p
= false;
9931 bool clobbers_p
= false;
9933 /* The extended syntax was used. */
9936 /* Look for outputs. */
9937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9939 /* Consume the `:'. */
9940 cp_lexer_consume_token (parser
->lexer
);
9941 /* Parse the output-operands. */
9942 if (cp_lexer_next_token_is_not (parser
->lexer
,
9944 && cp_lexer_next_token_is_not (parser
->lexer
,
9946 && cp_lexer_next_token_is_not (parser
->lexer
,
9948 outputs
= cp_parser_asm_operand_list (parser
);
9950 /* If the next token is `::', there are no outputs, and the
9951 next token is the beginning of the inputs. */
9952 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9954 /* Consume the `::' token. */
9955 cp_lexer_consume_token (parser
->lexer
);
9956 /* The inputs are coming next. */
9960 /* Look for inputs. */
9962 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9965 /* Consume the `:'. */
9966 cp_lexer_consume_token (parser
->lexer
);
9967 /* Parse the output-operands. */
9968 if (cp_lexer_next_token_is_not (parser
->lexer
,
9970 && cp_lexer_next_token_is_not (parser
->lexer
,
9972 && cp_lexer_next_token_is_not (parser
->lexer
,
9974 inputs
= cp_parser_asm_operand_list (parser
);
9976 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9977 /* The clobbers are coming next. */
9980 /* Look for clobbers. */
9982 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9985 /* Consume the `:'. */
9986 cp_lexer_consume_token (parser
->lexer
);
9987 /* Parse the clobbers. */
9988 if (cp_lexer_next_token_is_not (parser
->lexer
,
9990 clobbers
= cp_parser_asm_clobber_list (parser
);
9993 /* Look for the closing `)'. */
9994 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9995 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9996 /*consume_paren=*/true);
9997 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9999 /* Create the ASM_STMT. */
10000 if (at_function_scope_p ())
10002 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10004 /* If the extended syntax was not used, mark the ASM_STMT. */
10006 ASM_INPUT_P (asm_stmt
) = 1;
10009 assemble_asm (string
);
10012 c_lex_string_translate
= true;
10015 /* Declarators [gram.dcl.decl] */
10017 /* Parse an init-declarator.
10020 declarator initializer [opt]
10025 declarator asm-specification [opt] attributes [opt] initializer [opt]
10027 function-definition:
10028 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10030 decl-specifier-seq [opt] declarator function-try-block
10034 function-definition:
10035 __extension__ function-definition
10037 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10038 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10039 then this declarator appears in a class scope. The new DECL created
10040 by this declarator is returned.
10042 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10043 for a function-definition here as well. If the declarator is a
10044 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10045 be TRUE upon return. By that point, the function-definition will
10046 have been completely parsed.
10048 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10052 cp_parser_init_declarator (cp_parser
* parser
,
10053 tree decl_specifiers
,
10054 tree prefix_attributes
,
10055 bool function_definition_allowed_p
,
10057 int declares_class_or_enum
,
10058 bool* function_definition_p
)
10063 tree asm_specification
;
10065 tree decl
= NULL_TREE
;
10067 bool is_initialized
;
10068 bool is_parenthesized_init
;
10069 bool is_non_constant_init
;
10070 int ctor_dtor_or_conv_p
;
10072 bool pop_p
= false;
10074 /* Assume that this is not the declarator for a function
10076 if (function_definition_p
)
10077 *function_definition_p
= false;
10079 /* Defer access checks while parsing the declarator; we cannot know
10080 what names are accessible until we know what is being
10082 resume_deferring_access_checks ();
10084 /* Parse the declarator. */
10086 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10087 &ctor_dtor_or_conv_p
,
10088 /*parenthesized_p=*/NULL
);
10089 /* Gather up the deferred checks. */
10090 stop_deferring_access_checks ();
10092 /* If the DECLARATOR was erroneous, there's no need to go
10094 if (declarator
== error_mark_node
)
10095 return error_mark_node
;
10097 cp_parser_check_for_definition_in_return_type (declarator
,
10098 declares_class_or_enum
);
10100 /* Figure out what scope the entity declared by the DECLARATOR is
10101 located in. `grokdeclarator' sometimes changes the scope, so
10102 we compute it now. */
10103 scope
= get_scope_of_declarator (declarator
);
10105 /* If we're allowing GNU extensions, look for an asm-specification
10107 if (cp_parser_allow_gnu_extensions_p (parser
))
10109 /* Look for an asm-specification. */
10110 asm_specification
= cp_parser_asm_specification_opt (parser
);
10111 /* And attributes. */
10112 attributes
= cp_parser_attributes_opt (parser
);
10116 asm_specification
= NULL_TREE
;
10117 attributes
= NULL_TREE
;
10120 /* Peek at the next token. */
10121 token
= cp_lexer_peek_token (parser
->lexer
);
10122 /* Check to see if the token indicates the start of a
10123 function-definition. */
10124 if (cp_parser_token_starts_function_definition_p (token
))
10126 if (!function_definition_allowed_p
)
10128 /* If a function-definition should not appear here, issue an
10130 cp_parser_error (parser
,
10131 "a function-definition is not allowed here");
10132 return error_mark_node
;
10136 /* Neither attributes nor an asm-specification are allowed
10137 on a function-definition. */
10138 if (asm_specification
)
10139 error ("an asm-specification is not allowed on a function-definition");
10141 error ("attributes are not allowed on a function-definition");
10142 /* This is a function-definition. */
10143 *function_definition_p
= true;
10145 /* Parse the function definition. */
10147 decl
= cp_parser_save_member_function_body (parser
,
10150 prefix_attributes
);
10153 = (cp_parser_function_definition_from_specifiers_and_declarator
10154 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
10162 Only in function declarations for constructors, destructors, and
10163 type conversions can the decl-specifier-seq be omitted.
10165 We explicitly postpone this check past the point where we handle
10166 function-definitions because we tolerate function-definitions
10167 that are missing their return types in some modes. */
10168 if (!decl_specifiers
&& ctor_dtor_or_conv_p
<= 0)
10170 cp_parser_error (parser
,
10171 "expected constructor, destructor, or type conversion");
10172 return error_mark_node
;
10175 /* An `=' or an `(' indicates an initializer. */
10176 is_initialized
= (token
->type
== CPP_EQ
10177 || token
->type
== CPP_OPEN_PAREN
);
10178 /* If the init-declarator isn't initialized and isn't followed by a
10179 `,' or `;', it's not a valid init-declarator. */
10180 if (!is_initialized
10181 && token
->type
!= CPP_COMMA
10182 && token
->type
!= CPP_SEMICOLON
)
10184 cp_parser_error (parser
, "expected init-declarator");
10185 return error_mark_node
;
10188 /* Because start_decl has side-effects, we should only call it if we
10189 know we're going ahead. By this point, we know that we cannot
10190 possibly be looking at any other construct. */
10191 cp_parser_commit_to_tentative_parse (parser
);
10193 /* If the decl specifiers were bad, issue an error now that we're
10194 sure this was intended to be a declarator. Then continue
10195 declaring the variable(s), as int, to try to cut down on further
10197 if (decl_specifiers
!= NULL
10198 && TREE_VALUE (decl_specifiers
) == error_mark_node
)
10200 cp_parser_error (parser
, "invalid type in declaration");
10201 TREE_VALUE (decl_specifiers
) = integer_type_node
;
10204 /* Check to see whether or not this declaration is a friend. */
10205 friend_p
= cp_parser_friend_p (decl_specifiers
);
10207 /* Check that the number of template-parameter-lists is OK. */
10208 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
10209 return error_mark_node
;
10211 /* Enter the newly declared entry in the symbol table. If we're
10212 processing a declaration in a class-specifier, we wait until
10213 after processing the initializer. */
10216 if (parser
->in_unbraced_linkage_specification_p
)
10218 decl_specifiers
= tree_cons (error_mark_node
,
10219 get_identifier ("extern"),
10221 have_extern_spec
= false;
10223 decl
= start_decl (declarator
, decl_specifiers
,
10224 is_initialized
, attributes
, prefix_attributes
);
10227 /* Enter the SCOPE. That way unqualified names appearing in the
10228 initializer will be looked up in SCOPE. */
10230 pop_p
= push_scope (scope
);
10232 /* Perform deferred access control checks, now that we know in which
10233 SCOPE the declared entity resides. */
10234 if (!member_p
&& decl
)
10236 tree saved_current_function_decl
= NULL_TREE
;
10238 /* If the entity being declared is a function, pretend that we
10239 are in its scope. If it is a `friend', it may have access to
10240 things that would not otherwise be accessible. */
10241 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10243 saved_current_function_decl
= current_function_decl
;
10244 current_function_decl
= decl
;
10247 /* Perform the access control checks for the declarator and the
10248 the decl-specifiers. */
10249 perform_deferred_access_checks ();
10251 /* Restore the saved value. */
10252 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10253 current_function_decl
= saved_current_function_decl
;
10256 /* Parse the initializer. */
10257 if (is_initialized
)
10258 initializer
= cp_parser_initializer (parser
,
10259 &is_parenthesized_init
,
10260 &is_non_constant_init
);
10263 initializer
= NULL_TREE
;
10264 is_parenthesized_init
= false;
10265 is_non_constant_init
= true;
10268 /* The old parser allows attributes to appear after a parenthesized
10269 initializer. Mark Mitchell proposed removing this functionality
10270 on the GCC mailing lists on 2002-08-13. This parser accepts the
10271 attributes -- but ignores them. */
10272 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
10273 if (cp_parser_attributes_opt (parser
))
10274 warning ("attributes after parenthesized initializer ignored");
10276 /* Leave the SCOPE, now that we have processed the initializer. It
10277 is important to do this before calling cp_finish_decl because it
10278 makes decisions about whether to create DECL_STMTs or not based
10279 on the current scope. */
10283 /* For an in-class declaration, use `grokfield' to create the
10287 decl
= grokfield (declarator
, decl_specifiers
,
10288 initializer
, /*asmspec=*/NULL_TREE
,
10289 /*attributes=*/NULL_TREE
);
10290 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
10291 cp_parser_save_default_args (parser
, decl
);
10294 /* Finish processing the declaration. But, skip friend
10296 if (!friend_p
&& decl
)
10297 cp_finish_decl (decl
,
10300 /* If the initializer is in parentheses, then this is
10301 a direct-initialization, which means that an
10302 `explicit' constructor is OK. Otherwise, an
10303 `explicit' constructor cannot be used. */
10304 ((is_parenthesized_init
|| !is_initialized
)
10305 ? 0 : LOOKUP_ONLYCONVERTING
));
10307 /* Remember whether or not variables were initialized by
10308 constant-expressions. */
10309 if (decl
&& TREE_CODE (decl
) == VAR_DECL
10310 && is_initialized
&& !is_non_constant_init
)
10311 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
10316 /* Parse a declarator.
10320 ptr-operator declarator
10322 abstract-declarator:
10323 ptr-operator abstract-declarator [opt]
10324 direct-abstract-declarator
10329 attributes [opt] direct-declarator
10330 attributes [opt] ptr-operator declarator
10332 abstract-declarator:
10333 attributes [opt] ptr-operator abstract-declarator [opt]
10334 attributes [opt] direct-abstract-declarator
10336 Returns a representation of the declarator. If the declarator has
10337 the form `* declarator', then an INDIRECT_REF is returned, whose
10338 only operand is the sub-declarator. Analogously, `& declarator' is
10339 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10340 used. The first operand is the TYPE for `X'. The second operand
10341 is an INDIRECT_REF whose operand is the sub-declarator.
10343 Otherwise, the representation is as for a direct-declarator.
10345 (It would be better to define a structure type to represent
10346 declarators, rather than abusing `tree' nodes to represent
10347 declarators. That would be much clearer and save some memory.
10348 There is no reason for declarators to be garbage-collected, for
10349 example; they are created during parser and no longer needed after
10350 `grokdeclarator' has been called.)
10352 For a ptr-operator that has the optional cv-qualifier-seq,
10353 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10356 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10357 detect constructor, destructor or conversion operators. It is set
10358 to -1 if the declarator is a name, and +1 if it is a
10359 function. Otherwise it is set to zero. Usually you just want to
10360 test for >0, but internally the negative value is used.
10362 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10363 a decl-specifier-seq unless it declares a constructor, destructor,
10364 or conversion. It might seem that we could check this condition in
10365 semantic analysis, rather than parsing, but that makes it difficult
10366 to handle something like `f()'. We want to notice that there are
10367 no decl-specifiers, and therefore realize that this is an
10368 expression, not a declaration.)
10370 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10371 the declarator is a direct-declarator of the form "(...)". */
10374 cp_parser_declarator (cp_parser
* parser
,
10375 cp_parser_declarator_kind dcl_kind
,
10376 int* ctor_dtor_or_conv_p
,
10377 bool* parenthesized_p
)
10381 enum tree_code code
;
10382 tree cv_qualifier_seq
;
10384 tree attributes
= NULL_TREE
;
10386 /* Assume this is not a constructor, destructor, or type-conversion
10388 if (ctor_dtor_or_conv_p
)
10389 *ctor_dtor_or_conv_p
= 0;
10391 if (cp_parser_allow_gnu_extensions_p (parser
))
10392 attributes
= cp_parser_attributes_opt (parser
);
10394 /* Peek at the next token. */
10395 token
= cp_lexer_peek_token (parser
->lexer
);
10397 /* Check for the ptr-operator production. */
10398 cp_parser_parse_tentatively (parser
);
10399 /* Parse the ptr-operator. */
10400 code
= cp_parser_ptr_operator (parser
,
10402 &cv_qualifier_seq
);
10403 /* If that worked, then we have a ptr-operator. */
10404 if (cp_parser_parse_definitely (parser
))
10406 /* If a ptr-operator was found, then this declarator was not
10408 if (parenthesized_p
)
10409 *parenthesized_p
= true;
10410 /* The dependent declarator is optional if we are parsing an
10411 abstract-declarator. */
10412 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10413 cp_parser_parse_tentatively (parser
);
10415 /* Parse the dependent declarator. */
10416 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10417 /*ctor_dtor_or_conv_p=*/NULL
,
10418 /*parenthesized_p=*/NULL
);
10420 /* If we are parsing an abstract-declarator, we must handle the
10421 case where the dependent declarator is absent. */
10422 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10423 && !cp_parser_parse_definitely (parser
))
10424 declarator
= NULL_TREE
;
10426 /* Build the representation of the ptr-operator. */
10427 if (code
== INDIRECT_REF
)
10428 declarator
= make_pointer_declarator (cv_qualifier_seq
,
10431 declarator
= make_reference_declarator (cv_qualifier_seq
,
10433 /* Handle the pointer-to-member case. */
10435 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
10437 /* Everything else is a direct-declarator. */
10440 if (parenthesized_p
)
10441 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10443 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10444 ctor_dtor_or_conv_p
);
10447 if (attributes
&& declarator
!= error_mark_node
)
10448 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
10453 /* Parse a direct-declarator or direct-abstract-declarator.
10457 direct-declarator ( parameter-declaration-clause )
10458 cv-qualifier-seq [opt]
10459 exception-specification [opt]
10460 direct-declarator [ constant-expression [opt] ]
10463 direct-abstract-declarator:
10464 direct-abstract-declarator [opt]
10465 ( parameter-declaration-clause )
10466 cv-qualifier-seq [opt]
10467 exception-specification [opt]
10468 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10469 ( abstract-declarator )
10471 Returns a representation of the declarator. DCL_KIND is
10472 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10473 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10474 we are parsing a direct-declarator. It is
10475 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10476 of ambiguity we prefer an abstract declarator, as per
10477 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10478 cp_parser_declarator.
10480 For the declarator-id production, the representation is as for an
10481 id-expression, except that a qualified name is represented as a
10482 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10483 see the documentation of the FUNCTION_DECLARATOR_* macros for
10484 information about how to find the various declarator components.
10485 An array-declarator is represented as an ARRAY_REF. The
10486 direct-declarator is the first operand; the constant-expression
10487 indicating the size of the array is the second operand. */
10490 cp_parser_direct_declarator (cp_parser
* parser
,
10491 cp_parser_declarator_kind dcl_kind
,
10492 int* ctor_dtor_or_conv_p
)
10495 tree declarator
= NULL_TREE
;
10496 tree scope
= NULL_TREE
;
10497 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10498 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10500 bool pop_p
= false;
10504 /* Peek at the next token. */
10505 token
= cp_lexer_peek_token (parser
->lexer
);
10506 if (token
->type
== CPP_OPEN_PAREN
)
10508 /* This is either a parameter-declaration-clause, or a
10509 parenthesized declarator. When we know we are parsing a
10510 named declarator, it must be a parenthesized declarator
10511 if FIRST is true. For instance, `(int)' is a
10512 parameter-declaration-clause, with an omitted
10513 direct-abstract-declarator. But `((*))', is a
10514 parenthesized abstract declarator. Finally, when T is a
10515 template parameter `(T)' is a
10516 parameter-declaration-clause, and not a parenthesized
10519 We first try and parse a parameter-declaration-clause,
10520 and then try a nested declarator (if FIRST is true).
10522 It is not an error for it not to be a
10523 parameter-declaration-clause, even when FIRST is
10529 The first is the declaration of a function while the
10530 second is a the definition of a variable, including its
10533 Having seen only the parenthesis, we cannot know which of
10534 these two alternatives should be selected. Even more
10535 complex are examples like:
10540 The former is a function-declaration; the latter is a
10541 variable initialization.
10543 Thus again, we try a parameter-declaration-clause, and if
10544 that fails, we back out and return. */
10546 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10549 unsigned saved_num_template_parameter_lists
;
10551 cp_parser_parse_tentatively (parser
);
10553 /* Consume the `('. */
10554 cp_lexer_consume_token (parser
->lexer
);
10557 /* If this is going to be an abstract declarator, we're
10558 in a declarator and we can't have default args. */
10559 parser
->default_arg_ok_p
= false;
10560 parser
->in_declarator_p
= true;
10563 /* Inside the function parameter list, surrounding
10564 template-parameter-lists do not apply. */
10565 saved_num_template_parameter_lists
10566 = parser
->num_template_parameter_lists
;
10567 parser
->num_template_parameter_lists
= 0;
10569 /* Parse the parameter-declaration-clause. */
10570 params
= cp_parser_parameter_declaration_clause (parser
);
10572 parser
->num_template_parameter_lists
10573 = saved_num_template_parameter_lists
;
10575 /* If all went well, parse the cv-qualifier-seq and the
10576 exception-specification. */
10577 if (cp_parser_parse_definitely (parser
))
10579 tree cv_qualifiers
;
10580 tree exception_specification
;
10582 if (ctor_dtor_or_conv_p
)
10583 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
10585 /* Consume the `)'. */
10586 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
10588 /* Parse the cv-qualifier-seq. */
10589 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
10590 /* And the exception-specification. */
10591 exception_specification
10592 = cp_parser_exception_specification_opt (parser
);
10594 /* Create the function-declarator. */
10595 declarator
= make_call_declarator (declarator
,
10598 exception_specification
);
10599 /* Any subsequent parameter lists are to do with
10600 return type, so are not those of the declared
10602 parser
->default_arg_ok_p
= false;
10604 /* Repeat the main loop. */
10609 /* If this is the first, we can try a parenthesized
10613 bool saved_in_type_id_in_expr_p
;
10615 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10616 parser
->in_declarator_p
= saved_in_declarator_p
;
10618 /* Consume the `('. */
10619 cp_lexer_consume_token (parser
->lexer
);
10620 /* Parse the nested declarator. */
10621 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
10622 parser
->in_type_id_in_expr_p
= true;
10624 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
10625 /*parenthesized_p=*/NULL
);
10626 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
10628 /* Expect a `)'. */
10629 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10630 declarator
= error_mark_node
;
10631 if (declarator
== error_mark_node
)
10634 goto handle_declarator
;
10636 /* Otherwise, we must be done. */
10640 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10641 && token
->type
== CPP_OPEN_SQUARE
)
10643 /* Parse an array-declarator. */
10646 if (ctor_dtor_or_conv_p
)
10647 *ctor_dtor_or_conv_p
= 0;
10650 parser
->default_arg_ok_p
= false;
10651 parser
->in_declarator_p
= true;
10652 /* Consume the `['. */
10653 cp_lexer_consume_token (parser
->lexer
);
10654 /* Peek at the next token. */
10655 token
= cp_lexer_peek_token (parser
->lexer
);
10656 /* If the next token is `]', then there is no
10657 constant-expression. */
10658 if (token
->type
!= CPP_CLOSE_SQUARE
)
10660 bool non_constant_p
;
10663 = cp_parser_constant_expression (parser
,
10664 /*allow_non_constant=*/true,
10666 if (!non_constant_p
)
10667 bounds
= fold_non_dependent_expr (bounds
);
10670 bounds
= NULL_TREE
;
10671 /* Look for the closing `]'. */
10672 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
10674 declarator
= error_mark_node
;
10678 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
10680 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
10682 /* Parse a declarator-id */
10683 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10684 cp_parser_parse_tentatively (parser
);
10685 declarator
= cp_parser_declarator_id (parser
);
10686 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10688 if (!cp_parser_parse_definitely (parser
))
10689 declarator
= error_mark_node
;
10690 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
10692 cp_parser_error (parser
, "expected unqualified-id");
10693 declarator
= error_mark_node
;
10697 if (declarator
== error_mark_node
)
10700 if (TREE_CODE (declarator
) == SCOPE_REF
10701 && !current_scope ())
10703 tree scope
= TREE_OPERAND (declarator
, 0);
10705 /* In the declaration of a member of a template class
10706 outside of the class itself, the SCOPE will sometimes
10707 be a TYPENAME_TYPE. For example, given:
10709 template <typename T>
10710 int S<T>::R::i = 3;
10712 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10713 this context, we must resolve S<T>::R to an ordinary
10714 type, rather than a typename type.
10716 The reason we normally avoid resolving TYPENAME_TYPEs
10717 is that a specialization of `S' might render
10718 `S<T>::R' not a type. However, if `S' is
10719 specialized, then this `i' will not be used, so there
10720 is no harm in resolving the types here. */
10721 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
10725 /* Resolve the TYPENAME_TYPE. */
10726 type
= resolve_typename_type (scope
,
10727 /*only_current_p=*/false);
10728 /* If that failed, the declarator is invalid. */
10729 if (type
== error_mark_node
)
10730 error ("`%T::%D' is not a type",
10731 TYPE_CONTEXT (scope
),
10732 TYPE_IDENTIFIER (scope
));
10733 /* Build a new DECLARATOR. */
10734 declarator
= build_nt (SCOPE_REF
,
10736 TREE_OPERAND (declarator
, 1));
10740 /* Check to see whether the declarator-id names a constructor,
10741 destructor, or conversion. */
10742 if (declarator
&& ctor_dtor_or_conv_p
10743 && ((TREE_CODE (declarator
) == SCOPE_REF
10744 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
10745 || (TREE_CODE (declarator
) != SCOPE_REF
10746 && at_class_scope_p ())))
10748 tree unqualified_name
;
10751 /* Get the unqualified part of the name. */
10752 if (TREE_CODE (declarator
) == SCOPE_REF
)
10754 class_type
= TREE_OPERAND (declarator
, 0);
10755 unqualified_name
= TREE_OPERAND (declarator
, 1);
10759 class_type
= current_class_type
;
10760 unqualified_name
= declarator
;
10763 /* See if it names ctor, dtor or conv. */
10764 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
10765 || IDENTIFIER_TYPENAME_P (unqualified_name
)
10766 || constructor_name_p (unqualified_name
, class_type
)
10767 || (TREE_CODE (unqualified_name
) == TYPE_DECL
10768 && same_type_p (TREE_TYPE (unqualified_name
),
10770 *ctor_dtor_or_conv_p
= -1;
10771 if (TREE_CODE (declarator
) == SCOPE_REF
10772 && TREE_CODE (unqualified_name
) == TYPE_DECL
10773 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name
)))
10775 error ("invalid use of constructor as a template");
10776 inform ("use `%T::%D' instead of `%T::%T' to name the "
10777 "constructor in a qualified name", class_type
,
10778 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
10779 class_type
, class_type
);
10783 handle_declarator
:;
10784 scope
= get_scope_of_declarator (declarator
);
10786 /* Any names that appear after the declarator-id for a
10787 member are looked up in the containing scope. */
10788 pop_p
= push_scope (scope
);
10789 parser
->in_declarator_p
= true;
10790 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
10792 && (TREE_CODE (declarator
) == SCOPE_REF
10793 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
10794 /* Default args are only allowed on function
10796 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10798 parser
->default_arg_ok_p
= false;
10807 /* For an abstract declarator, we might wind up with nothing at this
10808 point. That's an error; the declarator is not optional. */
10810 cp_parser_error (parser
, "expected declarator");
10812 /* If we entered a scope, we must exit it now. */
10816 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10817 parser
->in_declarator_p
= saved_in_declarator_p
;
10822 /* Parse a ptr-operator.
10825 * cv-qualifier-seq [opt]
10827 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10832 & cv-qualifier-seq [opt]
10834 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10835 used. Returns ADDR_EXPR if a reference was used. In the
10836 case of a pointer-to-member, *TYPE is filled in with the
10837 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10838 with the cv-qualifier-seq, or NULL_TREE, if there are no
10839 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10841 static enum tree_code
10842 cp_parser_ptr_operator (cp_parser
* parser
,
10844 tree
* cv_qualifier_seq
)
10846 enum tree_code code
= ERROR_MARK
;
10849 /* Assume that it's not a pointer-to-member. */
10851 /* And that there are no cv-qualifiers. */
10852 *cv_qualifier_seq
= NULL_TREE
;
10854 /* Peek at the next token. */
10855 token
= cp_lexer_peek_token (parser
->lexer
);
10856 /* If it's a `*' or `&' we have a pointer or reference. */
10857 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10859 /* Remember which ptr-operator we were processing. */
10860 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10862 /* Consume the `*' or `&'. */
10863 cp_lexer_consume_token (parser
->lexer
);
10865 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10866 `&', if we are allowing GNU extensions. (The only qualifier
10867 that can legally appear after `&' is `restrict', but that is
10868 enforced during semantic analysis. */
10869 if (code
== INDIRECT_REF
10870 || cp_parser_allow_gnu_extensions_p (parser
))
10871 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10875 /* Try the pointer-to-member case. */
10876 cp_parser_parse_tentatively (parser
);
10877 /* Look for the optional `::' operator. */
10878 cp_parser_global_scope_opt (parser
,
10879 /*current_scope_valid_p=*/false);
10880 /* Look for the nested-name specifier. */
10881 cp_parser_nested_name_specifier (parser
,
10882 /*typename_keyword_p=*/false,
10883 /*check_dependency_p=*/true,
10885 /*is_declaration=*/false);
10886 /* If we found it, and the next token is a `*', then we are
10887 indeed looking at a pointer-to-member operator. */
10888 if (!cp_parser_error_occurred (parser
)
10889 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10891 /* The type of which the member is a member is given by the
10893 *type
= parser
->scope
;
10894 /* The next name will not be qualified. */
10895 parser
->scope
= NULL_TREE
;
10896 parser
->qualifying_scope
= NULL_TREE
;
10897 parser
->object_scope
= NULL_TREE
;
10898 /* Indicate that the `*' operator was used. */
10899 code
= INDIRECT_REF
;
10900 /* Look for the optional cv-qualifier-seq. */
10901 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10903 /* If that didn't work we don't have a ptr-operator. */
10904 if (!cp_parser_parse_definitely (parser
))
10905 cp_parser_error (parser
, "expected ptr-operator");
10911 /* Parse an (optional) cv-qualifier-seq.
10914 cv-qualifier cv-qualifier-seq [opt]
10916 Returns a TREE_LIST. The TREE_VALUE of each node is the
10917 representation of a cv-qualifier. */
10920 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10922 tree cv_qualifiers
= NULL_TREE
;
10928 /* Look for the next cv-qualifier. */
10929 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10930 /* If we didn't find one, we're done. */
10934 /* Add this cv-qualifier to the list. */
10936 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10939 /* We built up the list in reverse order. */
10940 return nreverse (cv_qualifiers
);
10943 /* Parse an (optional) cv-qualifier.
10955 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10958 tree cv_qualifier
= NULL_TREE
;
10960 /* Peek at the next token. */
10961 token
= cp_lexer_peek_token (parser
->lexer
);
10962 /* See if it's a cv-qualifier. */
10963 switch (token
->keyword
)
10968 /* Save the value of the token. */
10969 cv_qualifier
= token
->value
;
10970 /* Consume the token. */
10971 cp_lexer_consume_token (parser
->lexer
);
10978 return cv_qualifier
;
10981 /* Parse a declarator-id.
10985 :: [opt] nested-name-specifier [opt] type-name
10987 In the `id-expression' case, the value returned is as for
10988 cp_parser_id_expression if the id-expression was an unqualified-id.
10989 If the id-expression was a qualified-id, then a SCOPE_REF is
10990 returned. The first operand is the scope (either a NAMESPACE_DECL
10991 or TREE_TYPE), but the second is still just a representation of an
10995 cp_parser_declarator_id (cp_parser
* parser
)
10997 tree id_expression
;
10999 /* The expression must be an id-expression. Assume that qualified
11000 names are the names of types so that:
11003 int S<T>::R::i = 3;
11005 will work; we must treat `S<T>::R' as the name of a type.
11006 Similarly, assume that qualified names are templates, where
11010 int S<T>::R<T>::i = 3;
11013 id_expression
= cp_parser_id_expression (parser
,
11014 /*template_keyword_p=*/false,
11015 /*check_dependency_p=*/false,
11016 /*template_p=*/NULL
,
11017 /*declarator_p=*/true);
11018 /* If the name was qualified, create a SCOPE_REF to represent
11022 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
11023 parser
->scope
= NULL_TREE
;
11026 return id_expression
;
11029 /* Parse a type-id.
11032 type-specifier-seq abstract-declarator [opt]
11034 Returns the TYPE specified. */
11037 cp_parser_type_id (cp_parser
* parser
)
11039 tree type_specifier_seq
;
11040 tree abstract_declarator
;
11042 /* Parse the type-specifier-seq. */
11044 = cp_parser_type_specifier_seq (parser
);
11045 if (type_specifier_seq
== error_mark_node
)
11046 return error_mark_node
;
11048 /* There might or might not be an abstract declarator. */
11049 cp_parser_parse_tentatively (parser
);
11050 /* Look for the declarator. */
11051 abstract_declarator
11052 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11053 /*parenthesized_p=*/NULL
);
11054 /* Check to see if there really was a declarator. */
11055 if (!cp_parser_parse_definitely (parser
))
11056 abstract_declarator
= NULL_TREE
;
11058 return groktypename (build_tree_list (type_specifier_seq
,
11059 abstract_declarator
));
11062 /* Parse a type-specifier-seq.
11064 type-specifier-seq:
11065 type-specifier type-specifier-seq [opt]
11069 type-specifier-seq:
11070 attributes type-specifier-seq [opt]
11072 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
11073 type-specifier, or the TREE_PURPOSE is a list of attributes. */
11076 cp_parser_type_specifier_seq (cp_parser
* parser
)
11078 bool seen_type_specifier
= false;
11079 tree type_specifier_seq
= NULL_TREE
;
11081 /* Parse the type-specifiers and attributes. */
11084 tree type_specifier
;
11086 /* Check for attributes first. */
11087 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
11089 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
11091 type_specifier_seq
);
11095 /* After the first type-specifier, others are optional. */
11096 if (seen_type_specifier
)
11097 cp_parser_parse_tentatively (parser
);
11098 /* Look for the type-specifier. */
11099 type_specifier
= cp_parser_type_specifier (parser
,
11100 CP_PARSER_FLAGS_NONE
,
11101 /*is_friend=*/false,
11102 /*is_declaration=*/false,
11105 /* If the first type-specifier could not be found, this is not a
11106 type-specifier-seq at all. */
11107 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
11108 return error_mark_node
;
11109 /* If subsequent type-specifiers could not be found, the
11110 type-specifier-seq is complete. */
11111 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
11114 /* Add the new type-specifier to the list. */
11116 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
11117 seen_type_specifier
= true;
11120 /* We built up the list in reverse order. */
11121 return nreverse (type_specifier_seq
);
11124 /* Parse a parameter-declaration-clause.
11126 parameter-declaration-clause:
11127 parameter-declaration-list [opt] ... [opt]
11128 parameter-declaration-list , ...
11130 Returns a representation for the parameter declarations. Each node
11131 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
11132 representation.) If the parameter-declaration-clause ends with an
11133 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11134 list. A return value of NULL_TREE indicates a
11135 parameter-declaration-clause consisting only of an ellipsis. */
11138 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
11144 /* Peek at the next token. */
11145 token
= cp_lexer_peek_token (parser
->lexer
);
11146 /* Check for trivial parameter-declaration-clauses. */
11147 if (token
->type
== CPP_ELLIPSIS
)
11149 /* Consume the `...' token. */
11150 cp_lexer_consume_token (parser
->lexer
);
11153 else if (token
->type
== CPP_CLOSE_PAREN
)
11154 /* There are no parameters. */
11156 #ifndef NO_IMPLICIT_EXTERN_C
11157 if (in_system_header
&& current_class_type
== NULL
11158 && current_lang_name
== lang_name_c
)
11162 return void_list_node
;
11164 /* Check for `(void)', too, which is a special case. */
11165 else if (token
->keyword
== RID_VOID
11166 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11167 == CPP_CLOSE_PAREN
))
11169 /* Consume the `void' token. */
11170 cp_lexer_consume_token (parser
->lexer
);
11171 /* There are no parameters. */
11172 return void_list_node
;
11175 /* Parse the parameter-declaration-list. */
11176 parameters
= cp_parser_parameter_declaration_list (parser
);
11177 /* If a parse error occurred while parsing the
11178 parameter-declaration-list, then the entire
11179 parameter-declaration-clause is erroneous. */
11180 if (parameters
== error_mark_node
)
11181 return error_mark_node
;
11183 /* Peek at the next token. */
11184 token
= cp_lexer_peek_token (parser
->lexer
);
11185 /* If it's a `,', the clause should terminate with an ellipsis. */
11186 if (token
->type
== CPP_COMMA
)
11188 /* Consume the `,'. */
11189 cp_lexer_consume_token (parser
->lexer
);
11190 /* Expect an ellipsis. */
11192 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
11194 /* It might also be `...' if the optional trailing `,' was
11196 else if (token
->type
== CPP_ELLIPSIS
)
11198 /* Consume the `...' token. */
11199 cp_lexer_consume_token (parser
->lexer
);
11200 /* And remember that we saw it. */
11204 ellipsis_p
= false;
11206 /* Finish the parameter list. */
11207 return finish_parmlist (parameters
, ellipsis_p
);
11210 /* Parse a parameter-declaration-list.
11212 parameter-declaration-list:
11213 parameter-declaration
11214 parameter-declaration-list , parameter-declaration
11216 Returns a representation of the parameter-declaration-list, as for
11217 cp_parser_parameter_declaration_clause. However, the
11218 `void_list_node' is never appended to the list. */
11221 cp_parser_parameter_declaration_list (cp_parser
* parser
)
11223 tree parameters
= NULL_TREE
;
11225 /* Look for more parameters. */
11229 bool parenthesized_p
;
11230 /* Parse the parameter. */
11232 = cp_parser_parameter_declaration (parser
,
11233 /*template_parm_p=*/false,
11236 /* If a parse error occurred parsing the parameter declaration,
11237 then the entire parameter-declaration-list is erroneous. */
11238 if (parameter
== error_mark_node
)
11240 parameters
= error_mark_node
;
11243 /* Add the new parameter to the list. */
11244 TREE_CHAIN (parameter
) = parameters
;
11245 parameters
= parameter
;
11247 /* Peek at the next token. */
11248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
11249 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11250 /* The parameter-declaration-list is complete. */
11252 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11256 /* Peek at the next token. */
11257 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11258 /* If it's an ellipsis, then the list is complete. */
11259 if (token
->type
== CPP_ELLIPSIS
)
11261 /* Otherwise, there must be more parameters. Consume the
11263 cp_lexer_consume_token (parser
->lexer
);
11264 /* When parsing something like:
11266 int i(float f, double d)
11268 we can tell after seeing the declaration for "f" that we
11269 are not looking at an initialization of a variable "i",
11270 but rather at the declaration of a function "i".
11272 Due to the fact that the parsing of template arguments
11273 (as specified to a template-id) requires backtracking we
11274 cannot use this technique when inside a template argument
11276 if (!parser
->in_template_argument_list_p
11277 && !parser
->in_type_id_in_expr_p
11278 && cp_parser_parsing_tentatively (parser
)
11279 && !cp_parser_committed_to_tentative_parse (parser
)
11280 /* However, a parameter-declaration of the form
11281 "foat(f)" (which is a valid declaration of a
11282 parameter "f") can also be interpreted as an
11283 expression (the conversion of "f" to "float"). */
11284 && !parenthesized_p
)
11285 cp_parser_commit_to_tentative_parse (parser
);
11289 cp_parser_error (parser
, "expected `,' or `...'");
11290 if (!cp_parser_parsing_tentatively (parser
)
11291 || cp_parser_committed_to_tentative_parse (parser
))
11292 cp_parser_skip_to_closing_parenthesis (parser
,
11293 /*recovering=*/true,
11294 /*or_comma=*/false,
11295 /*consume_paren=*/false);
11300 /* We built up the list in reverse order; straighten it out now. */
11301 return nreverse (parameters
);
11304 /* Parse a parameter declaration.
11306 parameter-declaration:
11307 decl-specifier-seq declarator
11308 decl-specifier-seq declarator = assignment-expression
11309 decl-specifier-seq abstract-declarator [opt]
11310 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11312 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11313 declares a template parameter. (In that case, a non-nested `>'
11314 token encountered during the parsing of the assignment-expression
11315 is not interpreted as a greater-than operator.)
11317 Returns a TREE_LIST representing the parameter-declaration. The
11318 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11319 there is no default argument. The TREE_VALUE is a representation
11320 of the decl-specifier-seq and declarator. In particular, the
11321 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11322 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11323 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11324 the declarator is of the form "(p)". */
11327 cp_parser_parameter_declaration (cp_parser
*parser
,
11328 bool template_parm_p
,
11329 bool *parenthesized_p
)
11331 int declares_class_or_enum
;
11332 bool greater_than_is_operator_p
;
11333 tree decl_specifiers
;
11336 tree default_argument
;
11339 const char *saved_message
;
11341 /* In a template parameter, `>' is not an operator.
11345 When parsing a default template-argument for a non-type
11346 template-parameter, the first non-nested `>' is taken as the end
11347 of the template parameter-list rather than a greater-than
11349 greater_than_is_operator_p
= !template_parm_p
;
11351 /* Type definitions may not appear in parameter types. */
11352 saved_message
= parser
->type_definition_forbidden_message
;
11353 parser
->type_definition_forbidden_message
11354 = "types may not be defined in parameter types";
11356 /* Parse the declaration-specifiers. */
11358 = cp_parser_decl_specifier_seq (parser
,
11359 CP_PARSER_FLAGS_NONE
,
11361 &declares_class_or_enum
);
11362 /* If an error occurred, there's no reason to attempt to parse the
11363 rest of the declaration. */
11364 if (cp_parser_error_occurred (parser
))
11366 parser
->type_definition_forbidden_message
= saved_message
;
11367 return error_mark_node
;
11370 /* Peek at the next token. */
11371 token
= cp_lexer_peek_token (parser
->lexer
);
11372 /* If the next token is a `)', `,', `=', `>', or `...', then there
11373 is no declarator. */
11374 if (token
->type
== CPP_CLOSE_PAREN
11375 || token
->type
== CPP_COMMA
11376 || token
->type
== CPP_EQ
11377 || token
->type
== CPP_ELLIPSIS
11378 || token
->type
== CPP_GREATER
)
11380 declarator
= NULL_TREE
;
11381 if (parenthesized_p
)
11382 *parenthesized_p
= false;
11384 /* Otherwise, there should be a declarator. */
11387 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11388 parser
->default_arg_ok_p
= false;
11390 /* After seeing a decl-specifier-seq, if the next token is not a
11391 "(", there is no possibility that the code is a valid
11392 expression. Therefore, if parsing tentatively, we commit at
11394 if (!parser
->in_template_argument_list_p
11395 /* In an expression context, having seen:
11399 we cannot be sure whether we are looking at a
11400 function-type (taking a "char" as a parameter) or a cast
11401 of some object of type "char" to "int". */
11402 && !parser
->in_type_id_in_expr_p
11403 && cp_parser_parsing_tentatively (parser
)
11404 && !cp_parser_committed_to_tentative_parse (parser
)
11405 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11406 cp_parser_commit_to_tentative_parse (parser
);
11407 /* Parse the declarator. */
11408 declarator
= cp_parser_declarator (parser
,
11409 CP_PARSER_DECLARATOR_EITHER
,
11410 /*ctor_dtor_or_conv_p=*/NULL
,
11412 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11413 /* After the declarator, allow more attributes. */
11414 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
11417 /* The restriction on defining new types applies only to the type
11418 of the parameter, not to the default argument. */
11419 parser
->type_definition_forbidden_message
= saved_message
;
11421 /* If the next token is `=', then process a default argument. */
11422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11424 bool saved_greater_than_is_operator_p
;
11425 /* Consume the `='. */
11426 cp_lexer_consume_token (parser
->lexer
);
11428 /* If we are defining a class, then the tokens that make up the
11429 default argument must be saved and processed later. */
11430 if (!template_parm_p
&& at_class_scope_p ()
11431 && TYPE_BEING_DEFINED (current_class_type
))
11433 unsigned depth
= 0;
11435 /* Create a DEFAULT_ARG to represented the unparsed default
11437 default_argument
= make_node (DEFAULT_ARG
);
11438 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
11440 /* Add tokens until we have processed the entire default
11447 /* Peek at the next token. */
11448 token
= cp_lexer_peek_token (parser
->lexer
);
11449 /* What we do depends on what token we have. */
11450 switch (token
->type
)
11452 /* In valid code, a default argument must be
11453 immediately followed by a `,' `)', or `...'. */
11455 case CPP_CLOSE_PAREN
:
11457 /* If we run into a non-nested `;', `}', or `]',
11458 then the code is invalid -- but the default
11459 argument is certainly over. */
11460 case CPP_SEMICOLON
:
11461 case CPP_CLOSE_BRACE
:
11462 case CPP_CLOSE_SQUARE
:
11465 /* Update DEPTH, if necessary. */
11466 else if (token
->type
== CPP_CLOSE_PAREN
11467 || token
->type
== CPP_CLOSE_BRACE
11468 || token
->type
== CPP_CLOSE_SQUARE
)
11472 case CPP_OPEN_PAREN
:
11473 case CPP_OPEN_SQUARE
:
11474 case CPP_OPEN_BRACE
:
11479 /* If we see a non-nested `>', and `>' is not an
11480 operator, then it marks the end of the default
11482 if (!depth
&& !greater_than_is_operator_p
)
11486 /* If we run out of tokens, issue an error message. */
11488 error ("file ends in default argument");
11494 /* In these cases, we should look for template-ids.
11495 For example, if the default argument is
11496 `X<int, double>()', we need to do name lookup to
11497 figure out whether or not `X' is a template; if
11498 so, the `,' does not end the default argument.
11500 That is not yet done. */
11507 /* If we've reached the end, stop. */
11511 /* Add the token to the token block. */
11512 token
= cp_lexer_consume_token (parser
->lexer
);
11513 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
11517 /* Outside of a class definition, we can just parse the
11518 assignment-expression. */
11521 bool saved_local_variables_forbidden_p
;
11523 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11525 saved_greater_than_is_operator_p
11526 = parser
->greater_than_is_operator_p
;
11527 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11528 /* Local variable names (and the `this' keyword) may not
11529 appear in a default argument. */
11530 saved_local_variables_forbidden_p
11531 = parser
->local_variables_forbidden_p
;
11532 parser
->local_variables_forbidden_p
= true;
11533 /* Parse the assignment-expression. */
11534 default_argument
= cp_parser_assignment_expression (parser
);
11535 /* Restore saved state. */
11536 parser
->greater_than_is_operator_p
11537 = saved_greater_than_is_operator_p
;
11538 parser
->local_variables_forbidden_p
11539 = saved_local_variables_forbidden_p
;
11541 if (!parser
->default_arg_ok_p
)
11543 if (!flag_pedantic_errors
)
11544 warning ("deprecated use of default argument for parameter of non-function");
11547 error ("default arguments are only permitted for function parameters");
11548 default_argument
= NULL_TREE
;
11553 default_argument
= NULL_TREE
;
11555 /* Create the representation of the parameter. */
11557 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
11558 parameter
= build_tree_list (default_argument
,
11559 build_tree_list (decl_specifiers
,
11565 /* Parse a function-body.
11568 compound_statement */
11571 cp_parser_function_body (cp_parser
*parser
)
11573 cp_parser_compound_statement (parser
, false);
11576 /* Parse a ctor-initializer-opt followed by a function-body. Return
11577 true if a ctor-initializer was present. */
11580 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11583 bool ctor_initializer_p
;
11585 /* Begin the function body. */
11586 body
= begin_function_body ();
11587 /* Parse the optional ctor-initializer. */
11588 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11589 /* Parse the function-body. */
11590 cp_parser_function_body (parser
);
11591 /* Finish the function body. */
11592 finish_function_body (body
);
11594 return ctor_initializer_p
;
11597 /* Parse an initializer.
11600 = initializer-clause
11601 ( expression-list )
11603 Returns a expression representing the initializer. If no
11604 initializer is present, NULL_TREE is returned.
11606 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11607 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11608 set to FALSE if there is no initializer present. If there is an
11609 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11610 is set to true; otherwise it is set to false. */
11613 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
11614 bool* non_constant_p
)
11619 /* Peek at the next token. */
11620 token
= cp_lexer_peek_token (parser
->lexer
);
11622 /* Let our caller know whether or not this initializer was
11624 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
11625 /* Assume that the initializer is constant. */
11626 *non_constant_p
= false;
11628 if (token
->type
== CPP_EQ
)
11630 /* Consume the `='. */
11631 cp_lexer_consume_token (parser
->lexer
);
11632 /* Parse the initializer-clause. */
11633 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
11635 else if (token
->type
== CPP_OPEN_PAREN
)
11636 init
= cp_parser_parenthesized_expression_list (parser
, false,
11640 /* Anything else is an error. */
11641 cp_parser_error (parser
, "expected initializer");
11642 init
= error_mark_node
;
11648 /* Parse an initializer-clause.
11650 initializer-clause:
11651 assignment-expression
11652 { initializer-list , [opt] }
11655 Returns an expression representing the initializer.
11657 If the `assignment-expression' production is used the value
11658 returned is simply a representation for the expression.
11660 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11661 the elements of the initializer-list (or NULL_TREE, if the last
11662 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11663 NULL_TREE. There is no way to detect whether or not the optional
11664 trailing `,' was provided. NON_CONSTANT_P is as for
11665 cp_parser_initializer. */
11668 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
11672 /* If it is not a `{', then we are looking at an
11673 assignment-expression. */
11674 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11677 = cp_parser_constant_expression (parser
,
11678 /*allow_non_constant_p=*/true,
11680 if (!*non_constant_p
)
11681 initializer
= fold_non_dependent_expr (initializer
);
11685 /* Consume the `{' token. */
11686 cp_lexer_consume_token (parser
->lexer
);
11687 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11688 initializer
= make_node (CONSTRUCTOR
);
11689 /* If it's not a `}', then there is a non-trivial initializer. */
11690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11692 /* Parse the initializer list. */
11693 CONSTRUCTOR_ELTS (initializer
)
11694 = cp_parser_initializer_list (parser
, non_constant_p
);
11695 /* A trailing `,' token is allowed. */
11696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11697 cp_lexer_consume_token (parser
->lexer
);
11699 /* Now, there should be a trailing `}'. */
11700 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11703 return initializer
;
11706 /* Parse an initializer-list.
11710 initializer-list , initializer-clause
11715 identifier : initializer-clause
11716 initializer-list, identifier : initializer-clause
11718 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11719 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11720 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11721 as for cp_parser_initializer. */
11724 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
11726 tree initializers
= NULL_TREE
;
11728 /* Assume all of the expressions are constant. */
11729 *non_constant_p
= false;
11731 /* Parse the rest of the list. */
11737 bool clause_non_constant_p
;
11739 /* If the next token is an identifier and the following one is a
11740 colon, we are looking at the GNU designated-initializer
11742 if (cp_parser_allow_gnu_extensions_p (parser
)
11743 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11744 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11746 /* Consume the identifier. */
11747 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11748 /* Consume the `:'. */
11749 cp_lexer_consume_token (parser
->lexer
);
11752 identifier
= NULL_TREE
;
11754 /* Parse the initializer. */
11755 initializer
= cp_parser_initializer_clause (parser
,
11756 &clause_non_constant_p
);
11757 /* If any clause is non-constant, so is the entire initializer. */
11758 if (clause_non_constant_p
)
11759 *non_constant_p
= true;
11760 /* Add it to the list. */
11761 initializers
= tree_cons (identifier
, initializer
, initializers
);
11763 /* If the next token is not a comma, we have reached the end of
11765 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11768 /* Peek at the next token. */
11769 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11770 /* If the next token is a `}', then we're still done. An
11771 initializer-clause can have a trailing `,' after the
11772 initializer-list and before the closing `}'. */
11773 if (token
->type
== CPP_CLOSE_BRACE
)
11776 /* Consume the `,' token. */
11777 cp_lexer_consume_token (parser
->lexer
);
11780 /* The initializers were built up in reverse order, so we need to
11781 reverse them now. */
11782 return nreverse (initializers
);
11785 /* Classes [gram.class] */
11787 /* Parse a class-name.
11793 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11794 to indicate that names looked up in dependent types should be
11795 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11796 keyword has been used to indicate that the name that appears next
11797 is a template. TYPE_P is true iff the next name should be treated
11798 as class-name, even if it is declared to be some other kind of name
11799 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11800 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11801 being defined in a class-head.
11803 Returns the TYPE_DECL representing the class. */
11806 cp_parser_class_name (cp_parser
*parser
,
11807 bool typename_keyword_p
,
11808 bool template_keyword_p
,
11810 bool check_dependency_p
,
11812 bool is_declaration
)
11819 /* All class-names start with an identifier. */
11820 token
= cp_lexer_peek_token (parser
->lexer
);
11821 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11823 cp_parser_error (parser
, "expected class-name");
11824 return error_mark_node
;
11827 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11828 to a template-id, so we save it here. */
11829 scope
= parser
->scope
;
11830 if (scope
== error_mark_node
)
11831 return error_mark_node
;
11833 /* Any name names a type if we're following the `typename' keyword
11834 in a qualified name where the enclosing scope is type-dependent. */
11835 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11836 && dependent_type_p (scope
));
11837 /* Handle the common case (an identifier, but not a template-id)
11839 if (token
->type
== CPP_NAME
11840 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
11844 /* Look for the identifier. */
11845 identifier
= cp_parser_identifier (parser
);
11846 /* If the next token isn't an identifier, we are certainly not
11847 looking at a class-name. */
11848 if (identifier
== error_mark_node
)
11849 decl
= error_mark_node
;
11850 /* If we know this is a type-name, there's no need to look it
11852 else if (typename_p
)
11856 /* If the next token is a `::', then the name must be a type
11859 [basic.lookup.qual]
11861 During the lookup for a name preceding the :: scope
11862 resolution operator, object, function, and enumerator
11863 names are ignored. */
11864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11866 /* Look up the name. */
11867 decl
= cp_parser_lookup_name (parser
, identifier
,
11869 /*is_template=*/false,
11870 /*is_namespace=*/false,
11871 check_dependency_p
);
11876 /* Try a template-id. */
11877 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11878 check_dependency_p
,
11880 if (decl
== error_mark_node
)
11881 return error_mark_node
;
11884 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11886 /* If this is a typename, create a TYPENAME_TYPE. */
11887 if (typename_p
&& decl
!= error_mark_node
)
11889 decl
= make_typename_type (scope
, decl
, /*complain=*/1);
11890 if (decl
!= error_mark_node
)
11891 decl
= TYPE_NAME (decl
);
11894 /* Check to see that it is really the name of a class. */
11895 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11896 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11897 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11898 /* Situations like this:
11900 template <typename T> struct A {
11901 typename T::template X<int>::I i;
11904 are problematic. Is `T::template X<int>' a class-name? The
11905 standard does not seem to be definitive, but there is no other
11906 valid interpretation of the following `::'. Therefore, those
11907 names are considered class-names. */
11908 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11909 else if (decl
== error_mark_node
11910 || TREE_CODE (decl
) != TYPE_DECL
11911 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11913 cp_parser_error (parser
, "expected class-name");
11914 return error_mark_node
;
11920 /* Parse a class-specifier.
11923 class-head { member-specification [opt] }
11925 Returns the TREE_TYPE representing the class. */
11928 cp_parser_class_specifier (cp_parser
* parser
)
11932 tree attributes
= NULL_TREE
;
11933 int has_trailing_semicolon
;
11934 bool nested_name_specifier_p
;
11935 unsigned saved_num_template_parameter_lists
;
11936 bool pop_p
= false;
11938 push_deferring_access_checks (dk_no_deferred
);
11940 /* Parse the class-head. */
11941 type
= cp_parser_class_head (parser
,
11942 &nested_name_specifier_p
,
11944 /* If the class-head was a semantic disaster, skip the entire body
11948 cp_parser_skip_to_end_of_block_or_statement (parser
);
11949 pop_deferring_access_checks ();
11950 return error_mark_node
;
11953 /* Look for the `{'. */
11954 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11956 pop_deferring_access_checks ();
11957 return error_mark_node
;
11960 /* Issue an error message if type-definitions are forbidden here. */
11961 cp_parser_check_type_definition (parser
);
11962 /* Remember that we are defining one more class. */
11963 ++parser
->num_classes_being_defined
;
11964 /* Inside the class, surrounding template-parameter-lists do not
11966 saved_num_template_parameter_lists
11967 = parser
->num_template_parameter_lists
;
11968 parser
->num_template_parameter_lists
= 0;
11970 /* Start the class. */
11971 if (nested_name_specifier_p
)
11972 pop_p
= push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11973 type
= begin_class_definition (type
);
11974 if (type
== error_mark_node
)
11975 /* If the type is erroneous, skip the entire body of the class. */
11976 cp_parser_skip_to_closing_brace (parser
);
11978 /* Parse the member-specification. */
11979 cp_parser_member_specification_opt (parser
);
11980 /* Look for the trailing `}'. */
11981 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11982 /* We get better error messages by noticing a common problem: a
11983 missing trailing `;'. */
11984 token
= cp_lexer_peek_token (parser
->lexer
);
11985 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11986 /* Look for trailing attributes to apply to this class. */
11987 if (cp_parser_allow_gnu_extensions_p (parser
))
11989 tree sub_attr
= cp_parser_attributes_opt (parser
);
11990 attributes
= chainon (attributes
, sub_attr
);
11992 if (type
!= error_mark_node
)
11993 type
= finish_struct (type
, attributes
);
11995 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
)));
11996 /* If this class is not itself within the scope of another class,
11997 then we need to parse the bodies of all of the queued function
11998 definitions. Note that the queued functions defined in a class
11999 are not always processed immediately following the
12000 class-specifier for that class. Consider:
12003 struct B { void f() { sizeof (A); } };
12006 If `f' were processed before the processing of `A' were
12007 completed, there would be no way to compute the size of `A'.
12008 Note that the nesting we are interested in here is lexical --
12009 not the semantic nesting given by TYPE_CONTEXT. In particular,
12012 struct A { struct B; };
12013 struct A::B { void f() { } };
12015 there is no need to delay the parsing of `A::B::f'. */
12016 if (--parser
->num_classes_being_defined
== 0)
12021 /* In a first pass, parse default arguments to the functions.
12022 Then, in a second pass, parse the bodies of the functions.
12023 This two-phased approach handles cases like:
12031 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
12032 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
12033 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
12034 TREE_PURPOSE (parser
->unparsed_functions_queues
)
12035 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
12037 fn
= TREE_VALUE (queue_entry
);
12038 /* Make sure that any template parameters are in scope. */
12039 maybe_begin_member_template_processing (fn
);
12040 /* If there are default arguments that have not yet been processed,
12041 take care of them now. */
12042 cp_parser_late_parsing_default_args (parser
, fn
);
12043 /* Remove any template parameters from the symbol table. */
12044 maybe_end_member_template_processing ();
12046 /* Now parse the body of the functions. */
12047 for (TREE_VALUE (parser
->unparsed_functions_queues
)
12048 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
12049 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
12050 TREE_VALUE (parser
->unparsed_functions_queues
)
12051 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
12053 /* Figure out which function we need to process. */
12054 fn
= TREE_VALUE (queue_entry
);
12056 /* A hack to prevent garbage collection. */
12059 /* Parse the function. */
12060 cp_parser_late_parsing_for_member (parser
, fn
);
12066 /* Put back any saved access checks. */
12067 pop_deferring_access_checks ();
12069 /* Restore the count of active template-parameter-lists. */
12070 parser
->num_template_parameter_lists
12071 = saved_num_template_parameter_lists
;
12076 /* Parse a class-head.
12079 class-key identifier [opt] base-clause [opt]
12080 class-key nested-name-specifier identifier base-clause [opt]
12081 class-key nested-name-specifier [opt] template-id
12085 class-key attributes identifier [opt] base-clause [opt]
12086 class-key attributes nested-name-specifier identifier base-clause [opt]
12087 class-key attributes nested-name-specifier [opt] template-id
12090 Returns the TYPE of the indicated class. Sets
12091 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12092 involving a nested-name-specifier was used, and FALSE otherwise.
12094 Returns NULL_TREE if the class-head is syntactically valid, but
12095 semantically invalid in a way that means we should skip the entire
12096 body of the class. */
12099 cp_parser_class_head (cp_parser
* parser
,
12100 bool* nested_name_specifier_p
,
12101 tree
*attributes_p
)
12104 tree nested_name_specifier
;
12105 enum tag_types class_key
;
12106 tree id
= NULL_TREE
;
12107 tree type
= NULL_TREE
;
12109 bool template_id_p
= false;
12110 bool qualified_p
= false;
12111 bool invalid_nested_name_p
= false;
12112 bool invalid_explicit_specialization_p
= false;
12113 bool pop_p
= false;
12114 unsigned num_templates
;
12116 /* Assume no nested-name-specifier will be present. */
12117 *nested_name_specifier_p
= false;
12118 /* Assume no template parameter lists will be used in defining the
12122 /* Look for the class-key. */
12123 class_key
= cp_parser_class_key (parser
);
12124 if (class_key
== none_type
)
12125 return error_mark_node
;
12127 /* Parse the attributes. */
12128 attributes
= cp_parser_attributes_opt (parser
);
12130 /* If the next token is `::', that is invalid -- but sometimes
12131 people do try to write:
12135 Handle this gracefully by accepting the extra qualifier, and then
12136 issuing an error about it later if this really is a
12137 class-head. If it turns out just to be an elaborated type
12138 specifier, remain silent. */
12139 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
12140 qualified_p
= true;
12142 push_deferring_access_checks (dk_no_check
);
12144 /* Determine the name of the class. Begin by looking for an
12145 optional nested-name-specifier. */
12146 nested_name_specifier
12147 = cp_parser_nested_name_specifier_opt (parser
,
12148 /*typename_keyword_p=*/false,
12149 /*check_dependency_p=*/false,
12151 /*is_declaration=*/false);
12152 /* If there was a nested-name-specifier, then there *must* be an
12154 if (nested_name_specifier
)
12156 /* Although the grammar says `identifier', it really means
12157 `class-name' or `template-name'. You are only allowed to
12158 define a class that has already been declared with this
12161 The proposed resolution for Core Issue 180 says that whever
12162 you see `class T::X' you should treat `X' as a type-name.
12164 It is OK to define an inaccessible class; for example:
12166 class A { class B; };
12169 We do not know if we will see a class-name, or a
12170 template-name. We look for a class-name first, in case the
12171 class-name is a template-id; if we looked for the
12172 template-name first we would stop after the template-name. */
12173 cp_parser_parse_tentatively (parser
);
12174 type
= cp_parser_class_name (parser
,
12175 /*typename_keyword_p=*/false,
12176 /*template_keyword_p=*/false,
12178 /*check_dependency_p=*/false,
12179 /*class_head_p=*/true,
12180 /*is_declaration=*/false);
12181 /* If that didn't work, ignore the nested-name-specifier. */
12182 if (!cp_parser_parse_definitely (parser
))
12184 invalid_nested_name_p
= true;
12185 id
= cp_parser_identifier (parser
);
12186 if (id
== error_mark_node
)
12189 /* If we could not find a corresponding TYPE, treat this
12190 declaration like an unqualified declaration. */
12191 if (type
== error_mark_node
)
12192 nested_name_specifier
= NULL_TREE
;
12193 /* Otherwise, count the number of templates used in TYPE and its
12194 containing scopes. */
12199 for (scope
= TREE_TYPE (type
);
12200 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
12201 scope
= (TYPE_P (scope
)
12202 ? TYPE_CONTEXT (scope
)
12203 : DECL_CONTEXT (scope
)))
12205 && CLASS_TYPE_P (scope
)
12206 && CLASSTYPE_TEMPLATE_INFO (scope
)
12207 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
12208 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
12212 /* Otherwise, the identifier is optional. */
12215 /* We don't know whether what comes next is a template-id,
12216 an identifier, or nothing at all. */
12217 cp_parser_parse_tentatively (parser
);
12218 /* Check for a template-id. */
12219 id
= cp_parser_template_id (parser
,
12220 /*template_keyword_p=*/false,
12221 /*check_dependency_p=*/true,
12222 /*is_declaration=*/true);
12223 /* If that didn't work, it could still be an identifier. */
12224 if (!cp_parser_parse_definitely (parser
))
12226 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12227 id
= cp_parser_identifier (parser
);
12233 template_id_p
= true;
12238 pop_deferring_access_checks ();
12241 cp_parser_check_for_invalid_template_id (parser
, id
);
12243 /* If it's not a `:' or a `{' then we can't really be looking at a
12244 class-head, since a class-head only appears as part of a
12245 class-specifier. We have to detect this situation before calling
12246 xref_tag, since that has irreversible side-effects. */
12247 if (!cp_parser_next_token_starts_class_definition_p (parser
))
12249 cp_parser_error (parser
, "expected `{' or `:'");
12250 return error_mark_node
;
12253 /* At this point, we're going ahead with the class-specifier, even
12254 if some other problem occurs. */
12255 cp_parser_commit_to_tentative_parse (parser
);
12256 /* Issue the error about the overly-qualified name now. */
12258 cp_parser_error (parser
,
12259 "global qualification of class name is invalid");
12260 else if (invalid_nested_name_p
)
12261 cp_parser_error (parser
,
12262 "qualified name does not name a class");
12263 else if (nested_name_specifier
)
12266 /* Figure out in what scope the declaration is being placed. */
12267 scope
= current_scope ();
12269 scope
= current_namespace
;
12270 /* If that scope does not contain the scope in which the
12271 class was originally declared, the program is invalid. */
12272 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
12274 error ("declaration of `%D' in `%D' which does not "
12275 "enclose `%D'", type
, scope
, nested_name_specifier
);
12281 A declarator-id shall not be qualified exception of the
12282 definition of a ... nested class outside of its class
12283 ... [or] a the definition or explicit instantiation of a
12284 class member of a namespace outside of its namespace. */
12285 if (scope
== nested_name_specifier
)
12287 pedwarn ("extra qualification ignored");
12288 nested_name_specifier
= NULL_TREE
;
12292 /* An explicit-specialization must be preceded by "template <>". If
12293 it is not, try to recover gracefully. */
12294 if (at_namespace_scope_p ()
12295 && parser
->num_template_parameter_lists
== 0
12298 error ("an explicit specialization must be preceded by 'template <>'");
12299 invalid_explicit_specialization_p
= true;
12300 /* Take the same action that would have been taken by
12301 cp_parser_explicit_specialization. */
12302 ++parser
->num_template_parameter_lists
;
12303 begin_specialization ();
12305 /* There must be no "return" statements between this point and the
12306 end of this function; set "type "to the correct return value and
12307 use "goto done;" to return. */
12308 /* Make sure that the right number of template parameters were
12310 if (!cp_parser_check_template_parameters (parser
, num_templates
))
12312 /* If something went wrong, there is no point in even trying to
12313 process the class-definition. */
12318 /* Look up the type. */
12321 type
= TREE_TYPE (id
);
12322 maybe_process_partial_specialization (type
);
12324 else if (!nested_name_specifier
)
12326 /* If the class was unnamed, create a dummy name. */
12328 id
= make_anon_name ();
12329 type
= xref_tag (class_key
, id
, /*globalize=*/false,
12330 parser
->num_template_parameter_lists
);
12335 bool pop_p
= false;
12339 template <typename T> struct S { struct T };
12340 template <typename T> struct S<T>::T { };
12342 we will get a TYPENAME_TYPE when processing the definition of
12343 `S::T'. We need to resolve it to the actual type before we
12344 try to define it. */
12345 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
12347 class_type
= resolve_typename_type (TREE_TYPE (type
),
12348 /*only_current_p=*/false);
12349 if (class_type
!= error_mark_node
)
12350 type
= TYPE_NAME (class_type
);
12353 cp_parser_error (parser
, "could not resolve typename type");
12354 type
= error_mark_node
;
12358 maybe_process_partial_specialization (TREE_TYPE (type
));
12359 class_type
= current_class_type
;
12360 /* Enter the scope indicated by the nested-name-specifier. */
12361 if (nested_name_specifier
)
12362 pop_p
= push_scope (nested_name_specifier
);
12363 /* Get the canonical version of this type. */
12364 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
12365 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12366 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
12367 type
= push_template_decl (type
);
12368 type
= TREE_TYPE (type
);
12369 if (nested_name_specifier
)
12371 *nested_name_specifier_p
= true;
12373 pop_scope (nested_name_specifier
);
12376 /* Indicate whether this class was declared as a `class' or as a
12378 if (TREE_CODE (type
) == RECORD_TYPE
)
12379 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
12380 cp_parser_check_class_key (class_key
, type
);
12382 /* Enter the scope containing the class; the names of base classes
12383 should be looked up in that context. For example, given:
12385 struct A { struct B {}; struct C; };
12386 struct A::C : B {};
12389 if (nested_name_specifier
)
12390 pop_p
= push_scope (nested_name_specifier
);
12391 /* Now, look for the base-clause. */
12392 token
= cp_lexer_peek_token (parser
->lexer
);
12393 if (token
->type
== CPP_COLON
)
12397 /* Get the list of base-classes. */
12398 bases
= cp_parser_base_clause (parser
);
12399 /* Process them. */
12400 xref_basetypes (type
, bases
);
12402 /* Leave the scope given by the nested-name-specifier. We will
12403 enter the class scope itself while processing the members. */
12405 pop_scope (nested_name_specifier
);
12408 if (invalid_explicit_specialization_p
)
12410 end_specialization ();
12411 --parser
->num_template_parameter_lists
;
12413 *attributes_p
= attributes
;
12417 /* Parse a class-key.
12424 Returns the kind of class-key specified, or none_type to indicate
12427 static enum tag_types
12428 cp_parser_class_key (cp_parser
* parser
)
12431 enum tag_types tag_type
;
12433 /* Look for the class-key. */
12434 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12438 /* Check to see if the TOKEN is a class-key. */
12439 tag_type
= cp_parser_token_is_class_key (token
);
12441 cp_parser_error (parser
, "expected class-key");
12445 /* Parse an (optional) member-specification.
12447 member-specification:
12448 member-declaration member-specification [opt]
12449 access-specifier : member-specification [opt] */
12452 cp_parser_member_specification_opt (cp_parser
* parser
)
12459 /* Peek at the next token. */
12460 token
= cp_lexer_peek_token (parser
->lexer
);
12461 /* If it's a `}', or EOF then we've seen all the members. */
12462 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12465 /* See if this token is a keyword. */
12466 keyword
= token
->keyword
;
12470 case RID_PROTECTED
:
12472 /* Consume the access-specifier. */
12473 cp_lexer_consume_token (parser
->lexer
);
12474 /* Remember which access-specifier is active. */
12475 current_access_specifier
= token
->value
;
12476 /* Look for the `:'. */
12477 cp_parser_require (parser
, CPP_COLON
, "`:'");
12481 /* Otherwise, the next construction must be a
12482 member-declaration. */
12483 cp_parser_member_declaration (parser
);
12488 /* Parse a member-declaration.
12490 member-declaration:
12491 decl-specifier-seq [opt] member-declarator-list [opt] ;
12492 function-definition ; [opt]
12493 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12495 template-declaration
12497 member-declarator-list:
12499 member-declarator-list , member-declarator
12502 declarator pure-specifier [opt]
12503 declarator constant-initializer [opt]
12504 identifier [opt] : constant-expression
12508 member-declaration:
12509 __extension__ member-declaration
12512 declarator attributes [opt] pure-specifier [opt]
12513 declarator attributes [opt] constant-initializer [opt]
12514 identifier [opt] attributes [opt] : constant-expression */
12517 cp_parser_member_declaration (cp_parser
* parser
)
12519 tree decl_specifiers
;
12520 tree prefix_attributes
;
12522 int declares_class_or_enum
;
12525 int saved_pedantic
;
12527 /* Check for the `__extension__' keyword. */
12528 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12531 cp_parser_member_declaration (parser
);
12532 /* Restore the old value of the PEDANTIC flag. */
12533 pedantic
= saved_pedantic
;
12538 /* Check for a template-declaration. */
12539 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12541 /* Parse the template-declaration. */
12542 cp_parser_template_declaration (parser
, /*member_p=*/true);
12547 /* Check for a using-declaration. */
12548 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
12550 /* Parse the using-declaration. */
12551 cp_parser_using_declaration (parser
);
12556 /* Parse the decl-specifier-seq. */
12558 = cp_parser_decl_specifier_seq (parser
,
12559 CP_PARSER_FLAGS_OPTIONAL
,
12560 &prefix_attributes
,
12561 &declares_class_or_enum
);
12562 /* Check for an invalid type-name. */
12563 if (cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12565 /* If there is no declarator, then the decl-specifier-seq should
12567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12569 /* If there was no decl-specifier-seq, and the next token is a
12570 `;', then we have something like:
12576 Each member-declaration shall declare at least one member
12577 name of the class. */
12578 if (!decl_specifiers
)
12581 pedwarn ("extra semicolon");
12587 /* See if this declaration is a friend. */
12588 friend_p
= cp_parser_friend_p (decl_specifiers
);
12589 /* If there were decl-specifiers, check to see if there was
12590 a class-declaration. */
12591 type
= check_tag_decl (decl_specifiers
);
12592 /* Nested classes have already been added to the class, but
12593 a `friend' needs to be explicitly registered. */
12596 /* If the `friend' keyword was present, the friend must
12597 be introduced with a class-key. */
12598 if (!declares_class_or_enum
)
12599 error ("a class-key must be used when declaring a friend");
12602 template <typename T> struct A {
12603 friend struct A<T>::B;
12606 A<T>::B will be represented by a TYPENAME_TYPE, and
12607 therefore not recognized by check_tag_decl. */
12612 for (specifier
= decl_specifiers
;
12614 specifier
= TREE_CHAIN (specifier
))
12616 tree s
= TREE_VALUE (specifier
);
12618 if (TREE_CODE (s
) == IDENTIFIER_NODE
)
12619 get_global_value_if_present (s
, &type
);
12620 if (TREE_CODE (s
) == TYPE_DECL
)
12629 if (!type
|| !TYPE_P (type
))
12630 error ("friend declaration does not name a class or "
12633 make_friend_class (current_class_type
, type
,
12634 /*complain=*/true);
12636 /* If there is no TYPE, an error message will already have
12640 /* An anonymous aggregate has to be handled specially; such
12641 a declaration really declares a data member (with a
12642 particular type), as opposed to a nested class. */
12643 else if (ANON_AGGR_TYPE_P (type
))
12645 /* Remove constructors and such from TYPE, now that we
12646 know it is an anonymous aggregate. */
12647 fixup_anonymous_aggr (type
);
12648 /* And make the corresponding data member. */
12649 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
12650 /* Add it to the class. */
12651 finish_member_declaration (decl
);
12654 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
12659 /* See if these declarations will be friends. */
12660 friend_p
= cp_parser_friend_p (decl_specifiers
);
12662 /* Keep going until we hit the `;' at the end of the
12664 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12666 tree attributes
= NULL_TREE
;
12667 tree first_attribute
;
12669 /* Peek at the next token. */
12670 token
= cp_lexer_peek_token (parser
->lexer
);
12672 /* Check for a bitfield declaration. */
12673 if (token
->type
== CPP_COLON
12674 || (token
->type
== CPP_NAME
12675 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12681 /* Get the name of the bitfield. Note that we cannot just
12682 check TOKEN here because it may have been invalidated by
12683 the call to cp_lexer_peek_nth_token above. */
12684 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
12685 identifier
= cp_parser_identifier (parser
);
12687 identifier
= NULL_TREE
;
12689 /* Consume the `:' token. */
12690 cp_lexer_consume_token (parser
->lexer
);
12691 /* Get the width of the bitfield. */
12693 = cp_parser_constant_expression (parser
,
12694 /*allow_non_constant=*/false,
12697 /* Look for attributes that apply to the bitfield. */
12698 attributes
= cp_parser_attributes_opt (parser
);
12699 /* Remember which attributes are prefix attributes and
12701 first_attribute
= attributes
;
12702 /* Combine the attributes. */
12703 attributes
= chainon (prefix_attributes
, attributes
);
12705 /* Create the bitfield declaration. */
12706 decl
= grokbitfield (identifier
,
12709 /* Apply the attributes. */
12710 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
12716 tree asm_specification
;
12717 int ctor_dtor_or_conv_p
;
12719 /* Parse the declarator. */
12721 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12722 &ctor_dtor_or_conv_p
,
12723 /*parenthesized_p=*/NULL
);
12725 /* If something went wrong parsing the declarator, make sure
12726 that we at least consume some tokens. */
12727 if (declarator
== error_mark_node
)
12729 /* Skip to the end of the statement. */
12730 cp_parser_skip_to_end_of_statement (parser
);
12731 /* If the next token is not a semicolon, that is
12732 probably because we just skipped over the body of
12733 a function. So, we consume a semicolon if
12734 present, but do not issue an error message if it
12736 if (cp_lexer_next_token_is (parser
->lexer
,
12738 cp_lexer_consume_token (parser
->lexer
);
12742 cp_parser_check_for_definition_in_return_type
12743 (declarator
, declares_class_or_enum
);
12745 /* Look for an asm-specification. */
12746 asm_specification
= cp_parser_asm_specification_opt (parser
);
12747 /* Look for attributes that apply to the declaration. */
12748 attributes
= cp_parser_attributes_opt (parser
);
12749 /* Remember which attributes are prefix attributes and
12751 first_attribute
= attributes
;
12752 /* Combine the attributes. */
12753 attributes
= chainon (prefix_attributes
, attributes
);
12755 /* If it's an `=', then we have a constant-initializer or a
12756 pure-specifier. It is not correct to parse the
12757 initializer before registering the member declaration
12758 since the member declaration should be in scope while
12759 its initializer is processed. However, the rest of the
12760 front end does not yet provide an interface that allows
12761 us to handle this correctly. */
12762 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12766 A pure-specifier shall be used only in the declaration of
12767 a virtual function.
12769 A member-declarator can contain a constant-initializer
12770 only if it declares a static member of integral or
12773 Therefore, if the DECLARATOR is for a function, we look
12774 for a pure-specifier; otherwise, we look for a
12775 constant-initializer. When we call `grokfield', it will
12776 perform more stringent semantics checks. */
12777 if (TREE_CODE (declarator
) == CALL_EXPR
)
12778 initializer
= cp_parser_pure_specifier (parser
);
12780 /* Parse the initializer. */
12781 initializer
= cp_parser_constant_initializer (parser
);
12783 /* Otherwise, there is no initializer. */
12785 initializer
= NULL_TREE
;
12787 /* See if we are probably looking at a function
12788 definition. We are certainly not looking at at a
12789 member-declarator. Calling `grokfield' has
12790 side-effects, so we must not do it unless we are sure
12791 that we are looking at a member-declarator. */
12792 if (cp_parser_token_starts_function_definition_p
12793 (cp_lexer_peek_token (parser
->lexer
)))
12795 /* The grammar does not allow a pure-specifier to be
12796 used when a member function is defined. (It is
12797 possible that this fact is an oversight in the
12798 standard, since a pure function may be defined
12799 outside of the class-specifier. */
12801 error ("pure-specifier on function-definition");
12802 decl
= cp_parser_save_member_function_body (parser
,
12806 /* If the member was not a friend, declare it here. */
12808 finish_member_declaration (decl
);
12809 /* Peek at the next token. */
12810 token
= cp_lexer_peek_token (parser
->lexer
);
12811 /* If the next token is a semicolon, consume it. */
12812 if (token
->type
== CPP_SEMICOLON
)
12813 cp_lexer_consume_token (parser
->lexer
);
12818 /* Create the declaration. */
12819 decl
= grokfield (declarator
, decl_specifiers
,
12820 initializer
, asm_specification
,
12822 /* Any initialization must have been from a
12823 constant-expression. */
12824 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
12825 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
12829 /* Reset PREFIX_ATTRIBUTES. */
12830 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12831 attributes
= TREE_CHAIN (attributes
);
12833 TREE_CHAIN (attributes
) = NULL_TREE
;
12835 /* If there is any qualification still in effect, clear it
12836 now; we will be starting fresh with the next declarator. */
12837 parser
->scope
= NULL_TREE
;
12838 parser
->qualifying_scope
= NULL_TREE
;
12839 parser
->object_scope
= NULL_TREE
;
12840 /* If it's a `,', then there are more declarators. */
12841 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12842 cp_lexer_consume_token (parser
->lexer
);
12843 /* If the next token isn't a `;', then we have a parse error. */
12844 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12847 cp_parser_error (parser
, "expected `;'");
12848 /* Skip tokens until we find a `;'. */
12849 cp_parser_skip_to_end_of_statement (parser
);
12856 /* Add DECL to the list of members. */
12858 finish_member_declaration (decl
);
12860 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12861 cp_parser_save_default_args (parser
, decl
);
12866 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12869 /* Parse a pure-specifier.
12874 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12875 Otherwise, ERROR_MARK_NODE is returned. */
12878 cp_parser_pure_specifier (cp_parser
* parser
)
12882 /* Look for the `=' token. */
12883 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12884 return error_mark_node
;
12885 /* Look for the `0' token. */
12886 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12887 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12888 to get information from the lexer about how the number was
12889 spelled in order to fix this problem. */
12890 if (!token
|| !integer_zerop (token
->value
))
12891 return error_mark_node
;
12893 return integer_zero_node
;
12896 /* Parse a constant-initializer.
12898 constant-initializer:
12899 = constant-expression
12901 Returns a representation of the constant-expression. */
12904 cp_parser_constant_initializer (cp_parser
* parser
)
12906 /* Look for the `=' token. */
12907 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12908 return error_mark_node
;
12910 /* It is invalid to write:
12912 struct S { static const int i = { 7 }; };
12915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12917 cp_parser_error (parser
,
12918 "a brace-enclosed initializer is not allowed here");
12919 /* Consume the opening brace. */
12920 cp_lexer_consume_token (parser
->lexer
);
12921 /* Skip the initializer. */
12922 cp_parser_skip_to_closing_brace (parser
);
12923 /* Look for the trailing `}'. */
12924 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12926 return error_mark_node
;
12929 return cp_parser_constant_expression (parser
,
12930 /*allow_non_constant=*/false,
12934 /* Derived classes [gram.class.derived] */
12936 /* Parse a base-clause.
12939 : base-specifier-list
12941 base-specifier-list:
12943 base-specifier-list , base-specifier
12945 Returns a TREE_LIST representing the base-classes, in the order in
12946 which they were declared. The representation of each node is as
12947 described by cp_parser_base_specifier.
12949 In the case that no bases are specified, this function will return
12950 NULL_TREE, not ERROR_MARK_NODE. */
12953 cp_parser_base_clause (cp_parser
* parser
)
12955 tree bases
= NULL_TREE
;
12957 /* Look for the `:' that begins the list. */
12958 cp_parser_require (parser
, CPP_COLON
, "`:'");
12960 /* Scan the base-specifier-list. */
12966 /* Look for the base-specifier. */
12967 base
= cp_parser_base_specifier (parser
);
12968 /* Add BASE to the front of the list. */
12969 if (base
!= error_mark_node
)
12971 TREE_CHAIN (base
) = bases
;
12974 /* Peek at the next token. */
12975 token
= cp_lexer_peek_token (parser
->lexer
);
12976 /* If it's not a comma, then the list is complete. */
12977 if (token
->type
!= CPP_COMMA
)
12979 /* Consume the `,'. */
12980 cp_lexer_consume_token (parser
->lexer
);
12983 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12984 base class had a qualified name. However, the next name that
12985 appears is certainly not qualified. */
12986 parser
->scope
= NULL_TREE
;
12987 parser
->qualifying_scope
= NULL_TREE
;
12988 parser
->object_scope
= NULL_TREE
;
12990 return nreverse (bases
);
12993 /* Parse a base-specifier.
12996 :: [opt] nested-name-specifier [opt] class-name
12997 virtual access-specifier [opt] :: [opt] nested-name-specifier
12999 access-specifier virtual [opt] :: [opt] nested-name-specifier
13002 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13003 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13004 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13005 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13008 cp_parser_base_specifier (cp_parser
* parser
)
13012 bool virtual_p
= false;
13013 bool duplicate_virtual_error_issued_p
= false;
13014 bool duplicate_access_error_issued_p
= false;
13015 bool class_scope_p
, template_p
;
13016 tree access
= access_default_node
;
13019 /* Process the optional `virtual' and `access-specifier'. */
13022 /* Peek at the next token. */
13023 token
= cp_lexer_peek_token (parser
->lexer
);
13024 /* Process `virtual'. */
13025 switch (token
->keyword
)
13028 /* If `virtual' appears more than once, issue an error. */
13029 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
13031 cp_parser_error (parser
,
13032 "`virtual' specified more than once in base-specified");
13033 duplicate_virtual_error_issued_p
= true;
13038 /* Consume the `virtual' token. */
13039 cp_lexer_consume_token (parser
->lexer
);
13044 case RID_PROTECTED
:
13046 /* If more than one access specifier appears, issue an
13048 if (access
!= access_default_node
13049 && !duplicate_access_error_issued_p
)
13051 cp_parser_error (parser
,
13052 "more than one access specifier in base-specified");
13053 duplicate_access_error_issued_p
= true;
13056 access
= ridpointers
[(int) token
->keyword
];
13058 /* Consume the access-specifier. */
13059 cp_lexer_consume_token (parser
->lexer
);
13068 /* It is not uncommon to see programs mechanically, erroneously, use
13069 the 'typename' keyword to denote (dependent) qualified types
13070 as base classes. */
13071 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13073 if (!processing_template_decl
)
13074 error ("keyword `typename' not allowed outside of templates");
13076 error ("keyword `typename' not allowed in this context "
13077 "(the base class is implicitly a type)");
13078 cp_lexer_consume_token (parser
->lexer
);
13081 /* Look for the optional `::' operator. */
13082 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13083 /* Look for the nested-name-specifier. The simplest way to
13088 The keyword `typename' is not permitted in a base-specifier or
13089 mem-initializer; in these contexts a qualified name that
13090 depends on a template-parameter is implicitly assumed to be a
13093 is to pretend that we have seen the `typename' keyword at this
13095 cp_parser_nested_name_specifier_opt (parser
,
13096 /*typename_keyword_p=*/true,
13097 /*check_dependency_p=*/true,
13099 /*is_declaration=*/true);
13100 /* If the base class is given by a qualified name, assume that names
13101 we see are type names or templates, as appropriate. */
13102 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
13103 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
13105 /* Finally, look for the class-name. */
13106 type
= cp_parser_class_name (parser
,
13110 /*check_dependency_p=*/true,
13111 /*class_head_p=*/false,
13112 /*is_declaration=*/true);
13114 if (type
== error_mark_node
)
13115 return error_mark_node
;
13117 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
13120 /* Exception handling [gram.exception] */
13122 /* Parse an (optional) exception-specification.
13124 exception-specification:
13125 throw ( type-id-list [opt] )
13127 Returns a TREE_LIST representing the exception-specification. The
13128 TREE_VALUE of each node is a type. */
13131 cp_parser_exception_specification_opt (cp_parser
* parser
)
13136 /* Peek at the next token. */
13137 token
= cp_lexer_peek_token (parser
->lexer
);
13138 /* If it's not `throw', then there's no exception-specification. */
13139 if (!cp_parser_is_keyword (token
, RID_THROW
))
13142 /* Consume the `throw'. */
13143 cp_lexer_consume_token (parser
->lexer
);
13145 /* Look for the `('. */
13146 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13148 /* Peek at the next token. */
13149 token
= cp_lexer_peek_token (parser
->lexer
);
13150 /* If it's not a `)', then there is a type-id-list. */
13151 if (token
->type
!= CPP_CLOSE_PAREN
)
13153 const char *saved_message
;
13155 /* Types may not be defined in an exception-specification. */
13156 saved_message
= parser
->type_definition_forbidden_message
;
13157 parser
->type_definition_forbidden_message
13158 = "types may not be defined in an exception-specification";
13159 /* Parse the type-id-list. */
13160 type_id_list
= cp_parser_type_id_list (parser
);
13161 /* Restore the saved message. */
13162 parser
->type_definition_forbidden_message
= saved_message
;
13165 type_id_list
= empty_except_spec
;
13167 /* Look for the `)'. */
13168 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13170 return type_id_list
;
13173 /* Parse an (optional) type-id-list.
13177 type-id-list , type-id
13179 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13180 in the order that the types were presented. */
13183 cp_parser_type_id_list (cp_parser
* parser
)
13185 tree types
= NULL_TREE
;
13192 /* Get the next type-id. */
13193 type
= cp_parser_type_id (parser
);
13194 /* Add it to the list. */
13195 types
= add_exception_specifier (types
, type
, /*complain=*/1);
13196 /* Peek at the next token. */
13197 token
= cp_lexer_peek_token (parser
->lexer
);
13198 /* If it is not a `,', we are done. */
13199 if (token
->type
!= CPP_COMMA
)
13201 /* Consume the `,'. */
13202 cp_lexer_consume_token (parser
->lexer
);
13205 return nreverse (types
);
13208 /* Parse a try-block.
13211 try compound-statement handler-seq */
13214 cp_parser_try_block (cp_parser
* parser
)
13218 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
13219 try_block
= begin_try_block ();
13220 cp_parser_compound_statement (parser
, false);
13221 finish_try_block (try_block
);
13222 cp_parser_handler_seq (parser
);
13223 finish_handler_sequence (try_block
);
13228 /* Parse a function-try-block.
13230 function-try-block:
13231 try ctor-initializer [opt] function-body handler-seq */
13234 cp_parser_function_try_block (cp_parser
* parser
)
13237 bool ctor_initializer_p
;
13239 /* Look for the `try' keyword. */
13240 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
13242 /* Let the rest of the front-end know where we are. */
13243 try_block
= begin_function_try_block ();
13244 /* Parse the function-body. */
13246 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13247 /* We're done with the `try' part. */
13248 finish_function_try_block (try_block
);
13249 /* Parse the handlers. */
13250 cp_parser_handler_seq (parser
);
13251 /* We're done with the handlers. */
13252 finish_function_handler_sequence (try_block
);
13254 return ctor_initializer_p
;
13257 /* Parse a handler-seq.
13260 handler handler-seq [opt] */
13263 cp_parser_handler_seq (cp_parser
* parser
)
13269 /* Parse the handler. */
13270 cp_parser_handler (parser
);
13271 /* Peek at the next token. */
13272 token
= cp_lexer_peek_token (parser
->lexer
);
13273 /* If it's not `catch' then there are no more handlers. */
13274 if (!cp_parser_is_keyword (token
, RID_CATCH
))
13279 /* Parse a handler.
13282 catch ( exception-declaration ) compound-statement */
13285 cp_parser_handler (cp_parser
* parser
)
13290 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
13291 handler
= begin_handler ();
13292 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13293 declaration
= cp_parser_exception_declaration (parser
);
13294 finish_handler_parms (declaration
, handler
);
13295 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13296 cp_parser_compound_statement (parser
, false);
13297 finish_handler (handler
);
13300 /* Parse an exception-declaration.
13302 exception-declaration:
13303 type-specifier-seq declarator
13304 type-specifier-seq abstract-declarator
13308 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13309 ellipsis variant is used. */
13312 cp_parser_exception_declaration (cp_parser
* parser
)
13314 tree type_specifiers
;
13316 const char *saved_message
;
13318 /* If it's an ellipsis, it's easy to handle. */
13319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13321 /* Consume the `...' token. */
13322 cp_lexer_consume_token (parser
->lexer
);
13326 /* Types may not be defined in exception-declarations. */
13327 saved_message
= parser
->type_definition_forbidden_message
;
13328 parser
->type_definition_forbidden_message
13329 = "types may not be defined in exception-declarations";
13331 /* Parse the type-specifier-seq. */
13332 type_specifiers
= cp_parser_type_specifier_seq (parser
);
13333 /* If it's a `)', then there is no declarator. */
13334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
13335 declarator
= NULL_TREE
;
13337 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
13338 /*ctor_dtor_or_conv_p=*/NULL
,
13339 /*parenthesized_p=*/NULL
);
13341 /* Restore the saved message. */
13342 parser
->type_definition_forbidden_message
= saved_message
;
13344 return start_handler_parms (type_specifiers
, declarator
);
13347 /* Parse a throw-expression.
13350 throw assignment-expression [opt]
13352 Returns a THROW_EXPR representing the throw-expression. */
13355 cp_parser_throw_expression (cp_parser
* parser
)
13360 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
13361 token
= cp_lexer_peek_token (parser
->lexer
);
13362 /* Figure out whether or not there is an assignment-expression
13363 following the "throw" keyword. */
13364 if (token
->type
== CPP_COMMA
13365 || token
->type
== CPP_SEMICOLON
13366 || token
->type
== CPP_CLOSE_PAREN
13367 || token
->type
== CPP_CLOSE_SQUARE
13368 || token
->type
== CPP_CLOSE_BRACE
13369 || token
->type
== CPP_COLON
)
13370 expression
= NULL_TREE
;
13372 expression
= cp_parser_assignment_expression (parser
);
13374 return build_throw (expression
);
13377 /* GNU Extensions */
13379 /* Parse an (optional) asm-specification.
13382 asm ( string-literal )
13384 If the asm-specification is present, returns a STRING_CST
13385 corresponding to the string-literal. Otherwise, returns
13389 cp_parser_asm_specification_opt (cp_parser
* parser
)
13392 tree asm_specification
;
13394 /* Peek at the next token. */
13395 token
= cp_lexer_peek_token (parser
->lexer
);
13396 /* If the next token isn't the `asm' keyword, then there's no
13397 asm-specification. */
13398 if (!cp_parser_is_keyword (token
, RID_ASM
))
13401 /* Consume the `asm' token. */
13402 cp_lexer_consume_token (parser
->lexer
);
13403 /* Look for the `('. */
13404 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13406 /* Look for the string-literal. */
13407 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13409 asm_specification
= token
->value
;
13411 asm_specification
= NULL_TREE
;
13413 /* Look for the `)'. */
13414 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13416 return asm_specification
;
13419 /* Parse an asm-operand-list.
13423 asm-operand-list , asm-operand
13426 string-literal ( expression )
13427 [ string-literal ] string-literal ( expression )
13429 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13430 each node is the expression. The TREE_PURPOSE is itself a
13431 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13432 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13433 is a STRING_CST for the string literal before the parenthesis. */
13436 cp_parser_asm_operand_list (cp_parser
* parser
)
13438 tree asm_operands
= NULL_TREE
;
13442 tree string_literal
;
13447 c_lex_string_translate
= false;
13449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13451 /* Consume the `[' token. */
13452 cp_lexer_consume_token (parser
->lexer
);
13453 /* Read the operand name. */
13454 name
= cp_parser_identifier (parser
);
13455 if (name
!= error_mark_node
)
13456 name
= build_string (IDENTIFIER_LENGTH (name
),
13457 IDENTIFIER_POINTER (name
));
13458 /* Look for the closing `]'. */
13459 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13463 /* Look for the string-literal. */
13464 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13465 string_literal
= token
? token
->value
: error_mark_node
;
13466 c_lex_string_translate
= true;
13467 /* Look for the `('. */
13468 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13469 /* Parse the expression. */
13470 expression
= cp_parser_expression (parser
);
13471 /* Look for the `)'. */
13472 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13473 c_lex_string_translate
= false;
13474 /* Add this operand to the list. */
13475 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
13478 /* If the next token is not a `,', there are no more
13480 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13482 /* Consume the `,'. */
13483 cp_lexer_consume_token (parser
->lexer
);
13486 return nreverse (asm_operands
);
13489 /* Parse an asm-clobber-list.
13493 asm-clobber-list , string-literal
13495 Returns a TREE_LIST, indicating the clobbers in the order that they
13496 appeared. The TREE_VALUE of each node is a STRING_CST. */
13499 cp_parser_asm_clobber_list (cp_parser
* parser
)
13501 tree clobbers
= NULL_TREE
;
13506 tree string_literal
;
13508 /* Look for the string literal. */
13509 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
13510 string_literal
= token
? token
->value
: error_mark_node
;
13511 /* Add it to the list. */
13512 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13513 /* If the next token is not a `,', then the list is
13515 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13517 /* Consume the `,' token. */
13518 cp_lexer_consume_token (parser
->lexer
);
13524 /* Parse an (optional) series of attributes.
13527 attributes attribute
13530 __attribute__ (( attribute-list [opt] ))
13532 The return value is as for cp_parser_attribute_list. */
13535 cp_parser_attributes_opt (cp_parser
* parser
)
13537 tree attributes
= NULL_TREE
;
13542 tree attribute_list
;
13544 /* Peek at the next token. */
13545 token
= cp_lexer_peek_token (parser
->lexer
);
13546 /* If it's not `__attribute__', then we're done. */
13547 if (token
->keyword
!= RID_ATTRIBUTE
)
13550 /* Consume the `__attribute__' keyword. */
13551 cp_lexer_consume_token (parser
->lexer
);
13552 /* Look for the two `(' tokens. */
13553 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13554 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13556 /* Peek at the next token. */
13557 token
= cp_lexer_peek_token (parser
->lexer
);
13558 if (token
->type
!= CPP_CLOSE_PAREN
)
13559 /* Parse the attribute-list. */
13560 attribute_list
= cp_parser_attribute_list (parser
);
13562 /* If the next token is a `)', then there is no attribute
13564 attribute_list
= NULL
;
13566 /* Look for the two `)' tokens. */
13567 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13568 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13570 /* Add these new attributes to the list. */
13571 attributes
= chainon (attributes
, attribute_list
);
13577 /* Parse an attribute-list.
13581 attribute-list , attribute
13585 identifier ( identifier )
13586 identifier ( identifier , expression-list )
13587 identifier ( expression-list )
13589 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13590 TREE_PURPOSE of each node is the identifier indicating which
13591 attribute is in use. The TREE_VALUE represents the arguments, if
13595 cp_parser_attribute_list (cp_parser
* parser
)
13597 tree attribute_list
= NULL_TREE
;
13599 c_lex_string_translate
= false;
13606 /* Look for the identifier. We also allow keywords here; for
13607 example `__attribute__ ((const))' is legal. */
13608 token
= cp_lexer_peek_token (parser
->lexer
);
13609 if (token
->type
!= CPP_NAME
13610 && token
->type
!= CPP_KEYWORD
)
13611 return error_mark_node
;
13612 /* Consume the token. */
13613 token
= cp_lexer_consume_token (parser
->lexer
);
13615 /* Save away the identifier that indicates which attribute this is. */
13616 identifier
= token
->value
;
13617 attribute
= build_tree_list (identifier
, NULL_TREE
);
13619 /* Peek at the next token. */
13620 token
= cp_lexer_peek_token (parser
->lexer
);
13621 /* If it's an `(', then parse the attribute arguments. */
13622 if (token
->type
== CPP_OPEN_PAREN
)
13626 arguments
= (cp_parser_parenthesized_expression_list
13627 (parser
, true, /*non_constant_p=*/NULL
));
13628 /* Save the identifier and arguments away. */
13629 TREE_VALUE (attribute
) = arguments
;
13632 /* Add this attribute to the list. */
13633 TREE_CHAIN (attribute
) = attribute_list
;
13634 attribute_list
= attribute
;
13636 /* Now, look for more attributes. */
13637 token
= cp_lexer_peek_token (parser
->lexer
);
13638 /* If the next token isn't a `,', we're done. */
13639 if (token
->type
!= CPP_COMMA
)
13642 /* Consume the comma and keep going. */
13643 cp_lexer_consume_token (parser
->lexer
);
13645 c_lex_string_translate
= true;
13647 /* We built up the list in reverse order. */
13648 return nreverse (attribute_list
);
13651 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13652 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13653 current value of the PEDANTIC flag, regardless of whether or not
13654 the `__extension__' keyword is present. The caller is responsible
13655 for restoring the value of the PEDANTIC flag. */
13658 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
13660 /* Save the old value of the PEDANTIC flag. */
13661 *saved_pedantic
= pedantic
;
13663 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
13665 /* Consume the `__extension__' token. */
13666 cp_lexer_consume_token (parser
->lexer
);
13667 /* We're not being pedantic while the `__extension__' keyword is
13677 /* Parse a label declaration.
13680 __label__ label-declarator-seq ;
13682 label-declarator-seq:
13683 identifier , label-declarator-seq
13687 cp_parser_label_declaration (cp_parser
* parser
)
13689 /* Look for the `__label__' keyword. */
13690 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
13696 /* Look for an identifier. */
13697 identifier
= cp_parser_identifier (parser
);
13698 /* Declare it as a lobel. */
13699 finish_label_decl (identifier
);
13700 /* If the next token is a `;', stop. */
13701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13703 /* Look for the `,' separating the label declarations. */
13704 cp_parser_require (parser
, CPP_COMMA
, "`,'");
13707 /* Look for the final `;'. */
13708 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13711 /* Support Functions */
13713 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13714 NAME should have one of the representations used for an
13715 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13716 is returned. If PARSER->SCOPE is a dependent type, then a
13717 SCOPE_REF is returned.
13719 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13720 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13721 was formed. Abstractly, such entities should not be passed to this
13722 function, because they do not need to be looked up, but it is
13723 simpler to check for this special case here, rather than at the
13726 In cases not explicitly covered above, this function returns a
13727 DECL, OVERLOAD, or baselink representing the result of the lookup.
13728 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13731 If IS_TYPE is TRUE, bindings that do not refer to types are
13734 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13737 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13740 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13744 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
13745 bool is_type
, bool is_template
, bool is_namespace
,
13746 bool check_dependency
)
13749 tree object_type
= parser
->context
->object_type
;
13751 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13752 no longer valid. Note that if we are parsing tentatively, and
13753 the parse fails, OBJECT_TYPE will be automatically restored. */
13754 parser
->context
->object_type
= NULL_TREE
;
13756 if (name
== error_mark_node
)
13757 return error_mark_node
;
13759 /* A template-id has already been resolved; there is no lookup to
13761 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13763 if (BASELINK_P (name
))
13765 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
13766 == TEMPLATE_ID_EXPR
),
13771 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13772 it should already have been checked to make sure that the name
13773 used matches the type being destroyed. */
13774 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13778 /* Figure out to which type this destructor applies. */
13780 type
= parser
->scope
;
13781 else if (object_type
)
13782 type
= object_type
;
13784 type
= current_class_type
;
13785 /* If that's not a class type, there is no destructor. */
13786 if (!type
|| !CLASS_TYPE_P (type
))
13787 return error_mark_node
;
13788 if (!CLASSTYPE_DESTRUCTORS (type
))
13789 return error_mark_node
;
13790 /* If it was a class type, return the destructor. */
13791 return CLASSTYPE_DESTRUCTORS (type
);
13794 /* By this point, the NAME should be an ordinary identifier. If
13795 the id-expression was a qualified name, the qualifying scope is
13796 stored in PARSER->SCOPE at this point. */
13797 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
13800 /* Perform the lookup. */
13805 if (parser
->scope
== error_mark_node
)
13806 return error_mark_node
;
13808 /* If the SCOPE is dependent, the lookup must be deferred until
13809 the template is instantiated -- unless we are explicitly
13810 looking up names in uninstantiated templates. Even then, we
13811 cannot look up the name if the scope is not a class type; it
13812 might, for example, be a template type parameter. */
13813 dependent_p
= (TYPE_P (parser
->scope
)
13814 && !(parser
->in_declarator_p
13815 && currently_open_class (parser
->scope
))
13816 && dependent_type_p (parser
->scope
));
13817 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
13821 /* The resolution to Core Issue 180 says that `struct A::B'
13822 should be considered a type-name, even if `A' is
13824 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
13827 else if (is_template
)
13828 decl
= make_unbound_class_template (parser
->scope
,
13832 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
13836 bool pop_p
= false;
13838 /* If PARSER->SCOPE is a dependent type, then it must be a
13839 class type, and we must not be checking dependencies;
13840 otherwise, we would have processed this lookup above. So
13841 that PARSER->SCOPE is not considered a dependent base by
13842 lookup_member, we must enter the scope here. */
13844 pop_p
= push_scope (parser
->scope
);
13845 /* If the PARSER->SCOPE is a a template specialization, it
13846 may be instantiated during name lookup. In that case,
13847 errors may be issued. Even if we rollback the current
13848 tentative parse, those errors are valid. */
13849 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
13850 /*complain=*/true);
13852 pop_scope (parser
->scope
);
13854 parser
->qualifying_scope
= parser
->scope
;
13855 parser
->object_scope
= NULL_TREE
;
13857 else if (object_type
)
13859 tree object_decl
= NULL_TREE
;
13860 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13861 OBJECT_TYPE is not a class. */
13862 if (CLASS_TYPE_P (object_type
))
13863 /* If the OBJECT_TYPE is a template specialization, it may
13864 be instantiated during name lookup. In that case, errors
13865 may be issued. Even if we rollback the current tentative
13866 parse, those errors are valid. */
13867 object_decl
= lookup_member (object_type
,
13869 /*protect=*/0, is_type
);
13870 /* Look it up in the enclosing context, too. */
13871 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13874 parser
->object_scope
= object_type
;
13875 parser
->qualifying_scope
= NULL_TREE
;
13877 decl
= object_decl
;
13881 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13884 parser
->qualifying_scope
= NULL_TREE
;
13885 parser
->object_scope
= NULL_TREE
;
13888 /* If the lookup failed, let our caller know. */
13890 || decl
== error_mark_node
13891 || (TREE_CODE (decl
) == FUNCTION_DECL
13892 && DECL_ANTICIPATED (decl
)))
13893 return error_mark_node
;
13895 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13896 if (TREE_CODE (decl
) == TREE_LIST
)
13898 /* The error message we have to print is too complicated for
13899 cp_parser_error, so we incorporate its actions directly. */
13900 if (!cp_parser_simulate_error (parser
))
13902 error ("reference to `%D' is ambiguous", name
);
13903 print_candidates (decl
);
13905 return error_mark_node
;
13908 my_friendly_assert (DECL_P (decl
)
13909 || TREE_CODE (decl
) == OVERLOAD
13910 || TREE_CODE (decl
) == SCOPE_REF
13911 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
13912 || BASELINK_P (decl
),
13915 /* If we have resolved the name of a member declaration, check to
13916 see if the declaration is accessible. When the name resolves to
13917 set of overloaded functions, accessibility is checked when
13918 overload resolution is done.
13920 During an explicit instantiation, access is not checked at all,
13921 as per [temp.explicit]. */
13923 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
13928 /* Like cp_parser_lookup_name, but for use in the typical case where
13929 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13930 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13933 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13935 return cp_parser_lookup_name (parser
, name
,
13937 /*is_template=*/false,
13938 /*is_namespace=*/false,
13939 /*check_dependency=*/true);
13942 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13943 the current context, return the TYPE_DECL. If TAG_NAME_P is
13944 true, the DECL indicates the class being defined in a class-head,
13945 or declared in an elaborated-type-specifier.
13947 Otherwise, return DECL. */
13950 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13952 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13953 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13956 template <typename T> struct B;
13959 template <typename T> struct A::B {};
13961 Similarly, in a elaborated-type-specifier:
13963 namespace N { struct X{}; }
13966 template <typename T> friend struct N::X;
13969 However, if the DECL refers to a class type, and we are in
13970 the scope of the class, then the name lookup automatically
13971 finds the TYPE_DECL created by build_self_reference rather
13972 than a TEMPLATE_DECL. For example, in:
13974 template <class T> struct S {
13978 there is no need to handle such case. */
13980 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13981 return DECL_TEMPLATE_RESULT (decl
);
13986 /* If too many, or too few, template-parameter lists apply to the
13987 declarator, issue an error message. Returns TRUE if all went well,
13988 and FALSE otherwise. */
13991 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13994 unsigned num_templates
;
13996 /* We haven't seen any classes that involve template parameters yet. */
13999 switch (TREE_CODE (declarator
))
14006 tree main_declarator
= TREE_OPERAND (declarator
, 0);
14008 cp_parser_check_declarator_template_parameters (parser
,
14017 scope
= TREE_OPERAND (declarator
, 0);
14018 member
= TREE_OPERAND (declarator
, 1);
14020 /* If this is a pointer-to-member, then we are not interested
14021 in the SCOPE, because it does not qualify the thing that is
14023 if (TREE_CODE (member
) == INDIRECT_REF
)
14024 return (cp_parser_check_declarator_template_parameters
14027 while (scope
&& CLASS_TYPE_P (scope
))
14029 /* You're supposed to have one `template <...>'
14030 for every template class, but you don't need one
14031 for a full specialization. For example:
14033 template <class T> struct S{};
14034 template <> struct S<int> { void f(); };
14035 void S<int>::f () {}
14037 is correct; there shouldn't be a `template <>' for
14038 the definition of `S<int>::f'. */
14039 if (CLASSTYPE_TEMPLATE_INFO (scope
)
14040 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
14041 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
14042 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
14045 scope
= TYPE_CONTEXT (scope
);
14049 /* Fall through. */
14052 /* If the DECLARATOR has the form `X<y>' then it uses one
14053 additional level of template parameters. */
14054 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
14057 return cp_parser_check_template_parameters (parser
,
14062 /* NUM_TEMPLATES were used in the current declaration. If that is
14063 invalid, return FALSE and issue an error messages. Otherwise,
14067 cp_parser_check_template_parameters (cp_parser
* parser
,
14068 unsigned num_templates
)
14070 /* If there are more template classes than parameter lists, we have
14073 template <class T> void S<T>::R<T>::f (); */
14074 if (parser
->num_template_parameter_lists
< num_templates
)
14076 error ("too few template-parameter-lists");
14079 /* If there are the same number of template classes and parameter
14080 lists, that's OK. */
14081 if (parser
->num_template_parameter_lists
== num_templates
)
14083 /* If there are more, but only one more, then we are referring to a
14084 member template. That's OK too. */
14085 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
14087 /* Otherwise, there are too many template parameter lists. We have
14090 template <class T> template <class U> void S::f(); */
14091 error ("too many template-parameter-lists");
14095 /* Parse a binary-expression of the general form:
14099 binary-expression <token> <expr>
14101 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14102 to parser the <expr>s. If the first production is used, then the
14103 value returned by FN is returned directly. Otherwise, a node with
14104 the indicated EXPR_TYPE is returned, with operands corresponding to
14105 the two sub-expressions. */
14108 cp_parser_binary_expression (cp_parser
* parser
,
14109 const cp_parser_token_tree_map token_tree_map
,
14110 cp_parser_expression_fn fn
)
14114 /* Parse the first expression. */
14115 lhs
= (*fn
) (parser
);
14116 /* Now, look for more expressions. */
14120 const cp_parser_token_tree_map_node
*map_node
;
14123 /* Peek at the next token. */
14124 token
= cp_lexer_peek_token (parser
->lexer
);
14125 /* If the token is `>', and that's not an operator at the
14126 moment, then we're done. */
14127 if (token
->type
== CPP_GREATER
14128 && !parser
->greater_than_is_operator_p
)
14130 /* If we find one of the tokens we want, build the corresponding
14131 tree representation. */
14132 for (map_node
= token_tree_map
;
14133 map_node
->token_type
!= CPP_EOF
;
14135 if (map_node
->token_type
== token
->type
)
14137 /* Assume that an overloaded operator will not be used. */
14138 bool overloaded_p
= false;
14140 /* Consume the operator token. */
14141 cp_lexer_consume_token (parser
->lexer
);
14142 /* Parse the right-hand side of the expression. */
14143 rhs
= (*fn
) (parser
);
14144 /* Build the binary tree node. */
14145 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
,
14147 /* If the binary operator required the use of an
14148 overloaded operator, then this expression cannot be an
14149 integral constant-expression. An overloaded operator
14150 can be used even if both operands are otherwise
14151 permissible in an integral constant-expression if at
14152 least one of the operands is of enumeration type. */
14154 && (cp_parser_non_integral_constant_expression
14155 (parser
, "calls to overloaded operators")))
14156 lhs
= error_mark_node
;
14160 /* If the token wasn't one of the ones we want, we're done. */
14161 if (map_node
->token_type
== CPP_EOF
)
14168 /* Parse an optional `::' token indicating that the following name is
14169 from the global namespace. If so, PARSER->SCOPE is set to the
14170 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14171 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14172 Returns the new value of PARSER->SCOPE, if the `::' token is
14173 present, and NULL_TREE otherwise. */
14176 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
14180 /* Peek at the next token. */
14181 token
= cp_lexer_peek_token (parser
->lexer
);
14182 /* If we're looking at a `::' token then we're starting from the
14183 global namespace, not our current location. */
14184 if (token
->type
== CPP_SCOPE
)
14186 /* Consume the `::' token. */
14187 cp_lexer_consume_token (parser
->lexer
);
14188 /* Set the SCOPE so that we know where to start the lookup. */
14189 parser
->scope
= global_namespace
;
14190 parser
->qualifying_scope
= global_namespace
;
14191 parser
->object_scope
= NULL_TREE
;
14193 return parser
->scope
;
14195 else if (!current_scope_valid_p
)
14197 parser
->scope
= NULL_TREE
;
14198 parser
->qualifying_scope
= NULL_TREE
;
14199 parser
->object_scope
= NULL_TREE
;
14205 /* Returns TRUE if the upcoming token sequence is the start of a
14206 constructor declarator. If FRIEND_P is true, the declarator is
14207 preceded by the `friend' specifier. */
14210 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
14212 bool constructor_p
;
14213 tree type_decl
= NULL_TREE
;
14214 bool nested_name_p
;
14215 cp_token
*next_token
;
14217 /* The common case is that this is not a constructor declarator, so
14218 try to avoid doing lots of work if at all possible. It's not
14219 valid declare a constructor at function scope. */
14220 if (at_function_scope_p ())
14222 /* And only certain tokens can begin a constructor declarator. */
14223 next_token
= cp_lexer_peek_token (parser
->lexer
);
14224 if (next_token
->type
!= CPP_NAME
14225 && next_token
->type
!= CPP_SCOPE
14226 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
14227 && next_token
->type
!= CPP_TEMPLATE_ID
)
14230 /* Parse tentatively; we are going to roll back all of the tokens
14232 cp_parser_parse_tentatively (parser
);
14233 /* Assume that we are looking at a constructor declarator. */
14234 constructor_p
= true;
14236 /* Look for the optional `::' operator. */
14237 cp_parser_global_scope_opt (parser
,
14238 /*current_scope_valid_p=*/false);
14239 /* Look for the nested-name-specifier. */
14241 = (cp_parser_nested_name_specifier_opt (parser
,
14242 /*typename_keyword_p=*/false,
14243 /*check_dependency_p=*/false,
14245 /*is_declaration=*/false)
14247 /* Outside of a class-specifier, there must be a
14248 nested-name-specifier. */
14249 if (!nested_name_p
&&
14250 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
14252 constructor_p
= false;
14253 /* If we still think that this might be a constructor-declarator,
14254 look for a class-name. */
14259 template <typename T> struct S { S(); };
14260 template <typename T> S<T>::S ();
14262 we must recognize that the nested `S' names a class.
14265 template <typename T> S<T>::S<T> ();
14267 we must recognize that the nested `S' names a template. */
14268 type_decl
= cp_parser_class_name (parser
,
14269 /*typename_keyword_p=*/false,
14270 /*template_keyword_p=*/false,
14272 /*check_dependency_p=*/false,
14273 /*class_head_p=*/false,
14274 /*is_declaration=*/false);
14275 /* If there was no class-name, then this is not a constructor. */
14276 constructor_p
= !cp_parser_error_occurred (parser
);
14279 /* If we're still considering a constructor, we have to see a `(',
14280 to begin the parameter-declaration-clause, followed by either a
14281 `)', an `...', or a decl-specifier. We need to check for a
14282 type-specifier to avoid being fooled into thinking that:
14286 is a constructor. (It is actually a function named `f' that
14287 takes one parameter (of type `int') and returns a value of type
14290 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14292 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
14293 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
14294 /* A parameter declaration begins with a decl-specifier,
14295 which is either the "attribute" keyword, a storage class
14296 specifier, or (usually) a type-specifier. */
14297 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
14298 && !cp_parser_storage_class_specifier_opt (parser
))
14301 bool pop_p
= false;
14302 unsigned saved_num_template_parameter_lists
;
14304 /* Names appearing in the type-specifier should be looked up
14305 in the scope of the class. */
14306 if (current_class_type
)
14310 type
= TREE_TYPE (type_decl
);
14311 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14313 type
= resolve_typename_type (type
,
14314 /*only_current_p=*/false);
14315 if (type
== error_mark_node
)
14317 cp_parser_abort_tentative_parse (parser
);
14321 pop_p
= push_scope (type
);
14324 /* Inside the constructor parameter list, surrounding
14325 template-parameter-lists do not apply. */
14326 saved_num_template_parameter_lists
14327 = parser
->num_template_parameter_lists
;
14328 parser
->num_template_parameter_lists
= 0;
14330 /* Look for the type-specifier. */
14331 cp_parser_type_specifier (parser
,
14332 CP_PARSER_FLAGS_NONE
,
14333 /*is_friend=*/false,
14334 /*is_declarator=*/true,
14335 /*declares_class_or_enum=*/NULL
,
14336 /*is_cv_qualifier=*/NULL
);
14338 parser
->num_template_parameter_lists
14339 = saved_num_template_parameter_lists
;
14341 /* Leave the scope of the class. */
14345 constructor_p
= !cp_parser_error_occurred (parser
);
14349 constructor_p
= false;
14350 /* We did not really want to consume any tokens. */
14351 cp_parser_abort_tentative_parse (parser
);
14353 return constructor_p
;
14356 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14357 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14358 they must be performed once we are in the scope of the function.
14360 Returns the function defined. */
14363 cp_parser_function_definition_from_specifiers_and_declarator
14364 (cp_parser
* parser
,
14365 tree decl_specifiers
,
14372 /* Begin the function-definition. */
14373 success_p
= begin_function_definition (decl_specifiers
,
14377 /* If there were names looked up in the decl-specifier-seq that we
14378 did not check, check them now. We must wait until we are in the
14379 scope of the function to perform the checks, since the function
14380 might be a friend. */
14381 perform_deferred_access_checks ();
14385 /* If begin_function_definition didn't like the definition, skip
14386 the entire function. */
14387 error ("invalid function declaration");
14388 cp_parser_skip_to_end_of_block_or_statement (parser
);
14389 fn
= error_mark_node
;
14392 fn
= cp_parser_function_definition_after_declarator (parser
,
14393 /*inline_p=*/false);
14398 /* Parse the part of a function-definition that follows the
14399 declarator. INLINE_P is TRUE iff this function is an inline
14400 function defined with a class-specifier.
14402 Returns the function defined. */
14405 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
14409 bool ctor_initializer_p
= false;
14410 bool saved_in_unbraced_linkage_specification_p
;
14411 unsigned saved_num_template_parameter_lists
;
14413 /* If the next token is `return', then the code may be trying to
14414 make use of the "named return value" extension that G++ used to
14416 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
14418 /* Consume the `return' keyword. */
14419 cp_lexer_consume_token (parser
->lexer
);
14420 /* Look for the identifier that indicates what value is to be
14422 cp_parser_identifier (parser
);
14423 /* Issue an error message. */
14424 error ("named return values are no longer supported");
14425 /* Skip tokens until we reach the start of the function body. */
14426 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
14427 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
14428 cp_lexer_consume_token (parser
->lexer
);
14430 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14431 anything declared inside `f'. */
14432 saved_in_unbraced_linkage_specification_p
14433 = parser
->in_unbraced_linkage_specification_p
;
14434 parser
->in_unbraced_linkage_specification_p
= false;
14435 /* Inside the function, surrounding template-parameter-lists do not
14437 saved_num_template_parameter_lists
14438 = parser
->num_template_parameter_lists
;
14439 parser
->num_template_parameter_lists
= 0;
14440 /* If the next token is `try', then we are looking at a
14441 function-try-block. */
14442 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14443 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14444 /* A function-try-block includes the function-body, so we only do
14445 this next part if we're not processing a function-try-block. */
14448 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14450 /* Finish the function. */
14451 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14452 (inline_p
? 2 : 0));
14453 /* Generate code for it, if necessary. */
14454 expand_or_defer_fn (fn
);
14455 /* Restore the saved values. */
14456 parser
->in_unbraced_linkage_specification_p
14457 = saved_in_unbraced_linkage_specification_p
;
14458 parser
->num_template_parameter_lists
14459 = saved_num_template_parameter_lists
;
14464 /* Parse a template-declaration, assuming that the `export' (and
14465 `extern') keywords, if present, has already been scanned. MEMBER_P
14466 is as for cp_parser_template_declaration. */
14469 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14471 tree decl
= NULL_TREE
;
14472 tree parameter_list
;
14473 bool friend_p
= false;
14475 /* Look for the `template' keyword. */
14476 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14480 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14483 /* If the next token is `>', then we have an invalid
14484 specialization. Rather than complain about an invalid template
14485 parameter, issue an error message here. */
14486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14488 cp_parser_error (parser
, "invalid explicit specialization");
14489 begin_specialization ();
14490 parameter_list
= NULL_TREE
;
14494 /* Parse the template parameters. */
14495 begin_template_parm_list ();
14496 parameter_list
= cp_parser_template_parameter_list (parser
);
14497 parameter_list
= end_template_parm_list (parameter_list
);
14500 /* Look for the `>'. */
14501 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14502 /* We just processed one more parameter list. */
14503 ++parser
->num_template_parameter_lists
;
14504 /* If the next token is `template', there are more template
14506 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14508 cp_parser_template_declaration_after_export (parser
, member_p
);
14511 decl
= cp_parser_single_declaration (parser
,
14515 /* If this is a member template declaration, let the front
14517 if (member_p
&& !friend_p
&& decl
)
14519 if (TREE_CODE (decl
) == TYPE_DECL
)
14520 cp_parser_check_access_in_redeclaration (decl
);
14522 decl
= finish_member_template_decl (decl
);
14524 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14525 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14526 /*complain=*/true);
14528 /* We are done with the current parameter list. */
14529 --parser
->num_template_parameter_lists
;
14532 finish_template_decl (parameter_list
);
14534 /* Register member declarations. */
14535 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14536 finish_member_declaration (decl
);
14538 /* If DECL is a function template, we must return to parse it later.
14539 (Even though there is no definition, there might be default
14540 arguments that need handling.) */
14541 if (member_p
&& decl
14542 && (TREE_CODE (decl
) == FUNCTION_DECL
14543 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14544 TREE_VALUE (parser
->unparsed_functions_queues
)
14545 = tree_cons (NULL_TREE
, decl
,
14546 TREE_VALUE (parser
->unparsed_functions_queues
));
14549 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14550 `function-definition' sequence. MEMBER_P is true, this declaration
14551 appears in a class scope.
14553 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14554 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14557 cp_parser_single_declaration (cp_parser
* parser
,
14561 int declares_class_or_enum
;
14562 tree decl
= NULL_TREE
;
14563 tree decl_specifiers
;
14565 bool function_definition_p
= false;
14567 /* Defer access checks until we know what is being declared. */
14568 push_deferring_access_checks (dk_deferred
);
14570 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14573 = cp_parser_decl_specifier_seq (parser
,
14574 CP_PARSER_FLAGS_OPTIONAL
,
14576 &declares_class_or_enum
);
14578 *friend_p
= cp_parser_friend_p (decl_specifiers
);
14579 /* Gather up the access checks that occurred the
14580 decl-specifier-seq. */
14581 stop_deferring_access_checks ();
14583 /* Check for the declaration of a template class. */
14584 if (declares_class_or_enum
)
14586 if (cp_parser_declares_only_class_p (parser
))
14588 decl
= shadow_tag (decl_specifiers
);
14590 decl
= TYPE_NAME (decl
);
14592 decl
= error_mark_node
;
14597 /* If it's not a template class, try for a template function. If
14598 the next token is a `;', then this declaration does not declare
14599 anything. But, if there were errors in the decl-specifiers, then
14600 the error might well have come from an attempted class-specifier.
14601 In that case, there's no need to warn about a missing declarator. */
14603 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
14604 || !value_member (error_mark_node
, decl_specifiers
)))
14605 decl
= cp_parser_init_declarator (parser
,
14608 /*function_definition_allowed_p=*/true,
14610 declares_class_or_enum
,
14611 &function_definition_p
);
14613 pop_deferring_access_checks ();
14615 /* Clear any current qualification; whatever comes next is the start
14616 of something new. */
14617 parser
->scope
= NULL_TREE
;
14618 parser
->qualifying_scope
= NULL_TREE
;
14619 parser
->object_scope
= NULL_TREE
;
14620 /* Look for a trailing `;' after the declaration. */
14621 if (!function_definition_p
14622 && !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
14623 cp_parser_skip_to_end_of_block_or_statement (parser
);
14628 /* Parse a cast-expression that is not the operand of a unary "&". */
14631 cp_parser_simple_cast_expression (cp_parser
*parser
)
14633 return cp_parser_cast_expression (parser
, /*address_p=*/false);
14636 /* Parse a functional cast to TYPE. Returns an expression
14637 representing the cast. */
14640 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14642 tree expression_list
;
14646 = cp_parser_parenthesized_expression_list (parser
, false,
14647 /*non_constant_p=*/NULL
);
14649 cast
= build_functional_cast (type
, expression_list
);
14650 /* [expr.const]/1: In an integral constant expression "only type
14651 conversions to integral or enumeration type can be used". */
14652 if (cast
!= error_mark_node
&& !type_dependent_expression_p (type
)
14653 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type
)))
14655 if (cp_parser_non_integral_constant_expression
14656 (parser
, "a call to a constructor"))
14657 return error_mark_node
;
14662 /* Save the tokens that make up the body of a member function defined
14663 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14664 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14665 specifiers applied to the declaration. Returns the FUNCTION_DECL
14666 for the member function. */
14669 cp_parser_save_member_function_body (cp_parser
* parser
,
14670 tree decl_specifiers
,
14674 cp_token_cache
*cache
;
14677 /* Create the function-declaration. */
14678 fn
= start_method (decl_specifiers
, declarator
, attributes
);
14679 /* If something went badly wrong, bail out now. */
14680 if (fn
== error_mark_node
)
14682 /* If there's a function-body, skip it. */
14683 if (cp_parser_token_starts_function_definition_p
14684 (cp_lexer_peek_token (parser
->lexer
)))
14685 cp_parser_skip_to_end_of_block_or_statement (parser
);
14686 return error_mark_node
;
14689 /* Remember it, if there default args to post process. */
14690 cp_parser_save_default_args (parser
, fn
);
14692 /* Create a token cache. */
14693 cache
= cp_token_cache_new ();
14694 /* Save away the tokens that make up the body of the
14696 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14697 /* Handle function try blocks. */
14698 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
14699 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
14701 /* Save away the inline definition; we will process it when the
14702 class is complete. */
14703 DECL_PENDING_INLINE_INFO (fn
) = cache
;
14704 DECL_PENDING_INLINE_P (fn
) = 1;
14706 /* We need to know that this was defined in the class, so that
14707 friend templates are handled correctly. */
14708 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
14710 /* We're done with the inline definition. */
14711 finish_method (fn
);
14713 /* Add FN to the queue of functions to be parsed later. */
14714 TREE_VALUE (parser
->unparsed_functions_queues
)
14715 = tree_cons (NULL_TREE
, fn
,
14716 TREE_VALUE (parser
->unparsed_functions_queues
));
14721 /* Parse a template-argument-list, as well as the trailing ">" (but
14722 not the opening ">"). See cp_parser_template_argument_list for the
14726 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
14730 tree saved_qualifying_scope
;
14731 tree saved_object_scope
;
14732 bool saved_greater_than_is_operator_p
;
14736 When parsing a template-id, the first non-nested `>' is taken as
14737 the end of the template-argument-list rather than a greater-than
14739 saved_greater_than_is_operator_p
14740 = parser
->greater_than_is_operator_p
;
14741 parser
->greater_than_is_operator_p
= false;
14742 /* Parsing the argument list may modify SCOPE, so we save it
14744 saved_scope
= parser
->scope
;
14745 saved_qualifying_scope
= parser
->qualifying_scope
;
14746 saved_object_scope
= parser
->object_scope
;
14747 /* Parse the template-argument-list itself. */
14748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14749 arguments
= NULL_TREE
;
14751 arguments
= cp_parser_template_argument_list (parser
);
14752 /* Look for the `>' that ends the template-argument-list. If we find
14753 a '>>' instead, it's probably just a typo. */
14754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14756 if (!saved_greater_than_is_operator_p
)
14758 /* If we're in a nested template argument list, the '>>' has to be
14759 a typo for '> >'. We emit the error message, but we continue
14760 parsing and we push a '>' as next token, so that the argument
14761 list will be parsed correctly.. */
14763 error ("`>>' should be `> >' within a nested template argument list");
14764 token
= cp_lexer_peek_token (parser
->lexer
);
14765 token
->type
= CPP_GREATER
;
14769 /* If this is not a nested template argument list, the '>>' is
14770 a typo for '>'. Emit an error message and continue. */
14771 error ("spurious `>>', use `>' to terminate a template argument list");
14772 cp_lexer_consume_token (parser
->lexer
);
14775 else if (!cp_parser_require (parser
, CPP_GREATER
, "`>'"))
14776 error ("missing `>' to terminate the template argument list");
14777 /* The `>' token might be a greater-than operator again now. */
14778 parser
->greater_than_is_operator_p
14779 = saved_greater_than_is_operator_p
;
14780 /* Restore the SAVED_SCOPE. */
14781 parser
->scope
= saved_scope
;
14782 parser
->qualifying_scope
= saved_qualifying_scope
;
14783 parser
->object_scope
= saved_object_scope
;
14788 /* MEMBER_FUNCTION is a member function, or a friend. If default
14789 arguments, or the body of the function have not yet been parsed,
14793 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
14795 cp_lexer
*saved_lexer
;
14797 /* If this member is a template, get the underlying
14799 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
14800 member_function
= DECL_TEMPLATE_RESULT (member_function
);
14802 /* There should not be any class definitions in progress at this
14803 point; the bodies of members are only parsed outside of all class
14805 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
14806 /* While we're parsing the member functions we might encounter more
14807 classes. We want to handle them right away, but we don't want
14808 them getting mixed up with functions that are currently in the
14810 parser
->unparsed_functions_queues
14811 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14813 /* Make sure that any template parameters are in scope. */
14814 maybe_begin_member_template_processing (member_function
);
14816 /* If the body of the function has not yet been parsed, parse it
14818 if (DECL_PENDING_INLINE_P (member_function
))
14820 tree function_scope
;
14821 cp_token_cache
*tokens
;
14823 /* The function is no longer pending; we are processing it. */
14824 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
14825 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
14826 DECL_PENDING_INLINE_P (member_function
) = 0;
14827 /* If this was an inline function in a local class, enter the scope
14828 of the containing function. */
14829 function_scope
= decl_function_context (member_function
);
14830 if (function_scope
)
14831 push_function_context_to (function_scope
);
14833 /* Save away the current lexer. */
14834 saved_lexer
= parser
->lexer
;
14835 /* Make a new lexer to feed us the tokens saved for this function. */
14836 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14837 parser
->lexer
->next
= saved_lexer
;
14839 /* Set the current source position to be the location of the first
14840 token in the saved inline body. */
14841 cp_lexer_peek_token (parser
->lexer
);
14843 /* Let the front end know that we going to be defining this
14845 start_function (NULL_TREE
, member_function
, NULL_TREE
,
14846 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
14848 /* Now, parse the body of the function. */
14849 cp_parser_function_definition_after_declarator (parser
,
14850 /*inline_p=*/true);
14852 /* Leave the scope of the containing function. */
14853 if (function_scope
)
14854 pop_function_context_from (function_scope
);
14855 /* Restore the lexer. */
14856 parser
->lexer
= saved_lexer
;
14859 /* Remove any template parameters from the symbol table. */
14860 maybe_end_member_template_processing ();
14862 /* Restore the queue. */
14863 parser
->unparsed_functions_queues
14864 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14867 /* If DECL contains any default args, remember it on the unparsed
14868 functions queue. */
14871 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
14875 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
14877 probe
= TREE_CHAIN (probe
))
14878 if (TREE_PURPOSE (probe
))
14880 TREE_PURPOSE (parser
->unparsed_functions_queues
)
14881 = tree_cons (NULL_TREE
, decl
,
14882 TREE_PURPOSE (parser
->unparsed_functions_queues
));
14888 /* FN is a FUNCTION_DECL which may contains a parameter with an
14889 unparsed DEFAULT_ARG. Parse the default args now. */
14892 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
14894 cp_lexer
*saved_lexer
;
14895 cp_token_cache
*tokens
;
14896 bool saved_local_variables_forbidden_p
;
14899 /* While we're parsing the default args, we might (due to the
14900 statement expression extension) encounter more classes. We want
14901 to handle them right away, but we don't want them getting mixed
14902 up with default args that are currently in the queue. */
14903 parser
->unparsed_functions_queues
14904 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14906 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14908 parameters
= TREE_CHAIN (parameters
))
14910 if (!TREE_PURPOSE (parameters
)
14911 || TREE_CODE (TREE_PURPOSE (parameters
)) != DEFAULT_ARG
)
14914 /* Save away the current lexer. */
14915 saved_lexer
= parser
->lexer
;
14916 /* Create a new one, using the tokens we have saved. */
14917 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parameters
));
14918 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14920 /* Set the current source position to be the location of the
14921 first token in the default argument. */
14922 cp_lexer_peek_token (parser
->lexer
);
14924 /* Local variable names (and the `this' keyword) may not appear
14925 in a default argument. */
14926 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14927 parser
->local_variables_forbidden_p
= true;
14928 /* Parse the assignment-expression. */
14929 if (DECL_CLASS_SCOPE_P (fn
))
14930 push_nested_class (DECL_CONTEXT (fn
));
14931 TREE_PURPOSE (parameters
) = cp_parser_assignment_expression (parser
);
14932 if (DECL_CLASS_SCOPE_P (fn
))
14933 pop_nested_class ();
14935 /* If the token stream has not been completely used up, then
14936 there was extra junk after the end of the default
14938 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
14939 cp_parser_error (parser
, "expected `,'");
14941 /* Restore saved state. */
14942 parser
->lexer
= saved_lexer
;
14943 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14946 /* Restore the queue. */
14947 parser
->unparsed_functions_queues
14948 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14951 /* Parse the operand of `sizeof' (or a similar operator). Returns
14952 either a TYPE or an expression, depending on the form of the
14953 input. The KEYWORD indicates which kind of expression we have
14957 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
14959 static const char *format
;
14960 tree expr
= NULL_TREE
;
14961 const char *saved_message
;
14962 bool saved_integral_constant_expression_p
;
14964 /* Initialize FORMAT the first time we get here. */
14966 format
= "types may not be defined in `%s' expressions";
14968 /* Types cannot be defined in a `sizeof' expression. Save away the
14970 saved_message
= parser
->type_definition_forbidden_message
;
14971 /* And create the new one. */
14972 parser
->type_definition_forbidden_message
14973 = xmalloc (strlen (format
)
14974 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
14976 sprintf ((char *) parser
->type_definition_forbidden_message
,
14977 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
14979 /* The restrictions on constant-expressions do not apply inside
14980 sizeof expressions. */
14981 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
14982 parser
->integral_constant_expression_p
= false;
14984 /* Do not actually evaluate the expression. */
14986 /* If it's a `(', then we might be looking at the type-id
14988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14991 bool saved_in_type_id_in_expr_p
;
14993 /* We can't be sure yet whether we're looking at a type-id or an
14995 cp_parser_parse_tentatively (parser
);
14996 /* Consume the `('. */
14997 cp_lexer_consume_token (parser
->lexer
);
14998 /* Parse the type-id. */
14999 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
15000 parser
->in_type_id_in_expr_p
= true;
15001 type
= cp_parser_type_id (parser
);
15002 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
15003 /* Now, look for the trailing `)'. */
15004 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15005 /* If all went well, then we're done. */
15006 if (cp_parser_parse_definitely (parser
))
15008 /* Build a list of decl-specifiers; right now, we have only
15009 a single type-specifier. */
15010 type
= build_tree_list (NULL_TREE
,
15013 /* Call grokdeclarator to figure out what type this is. */
15014 expr
= grokdeclarator (NULL_TREE
,
15018 /*attrlist=*/NULL
);
15022 /* If the type-id production did not work out, then we must be
15023 looking at the unary-expression production. */
15025 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
15026 /* Go back to evaluating expressions. */
15029 /* Free the message we created. */
15030 free ((char *) parser
->type_definition_forbidden_message
);
15031 /* And restore the old one. */
15032 parser
->type_definition_forbidden_message
= saved_message
;
15033 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
15038 /* If the current declaration has no declarator, return true. */
15041 cp_parser_declares_only_class_p (cp_parser
*parser
)
15043 /* If the next token is a `;' or a `,' then there is no
15045 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15046 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15049 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15050 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15053 cp_parser_friend_p (tree decl_specifiers
)
15055 while (decl_specifiers
)
15057 /* See if this decl-specifier is `friend'. */
15058 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
15059 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
15062 /* Go on to the next decl-specifier. */
15063 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
15069 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15070 issue an error message indicating that TOKEN_DESC was expected.
15072 Returns the token consumed, if the token had the appropriate type.
15073 Otherwise, returns NULL. */
15076 cp_parser_require (cp_parser
* parser
,
15077 enum cpp_ttype type
,
15078 const char* token_desc
)
15080 if (cp_lexer_next_token_is (parser
->lexer
, type
))
15081 return cp_lexer_consume_token (parser
->lexer
);
15084 /* Output the MESSAGE -- unless we're parsing tentatively. */
15085 if (!cp_parser_simulate_error (parser
))
15087 char *message
= concat ("expected ", token_desc
, NULL
);
15088 cp_parser_error (parser
, message
);
15095 /* Like cp_parser_require, except that tokens will be skipped until
15096 the desired token is found. An error message is still produced if
15097 the next token is not as expected. */
15100 cp_parser_skip_until_found (cp_parser
* parser
,
15101 enum cpp_ttype type
,
15102 const char* token_desc
)
15105 unsigned nesting_depth
= 0;
15107 if (cp_parser_require (parser
, type
, token_desc
))
15110 /* Skip tokens until the desired token is found. */
15113 /* Peek at the next token. */
15114 token
= cp_lexer_peek_token (parser
->lexer
);
15115 /* If we've reached the token we want, consume it and
15117 if (token
->type
== type
&& !nesting_depth
)
15119 cp_lexer_consume_token (parser
->lexer
);
15122 /* If we've run out of tokens, stop. */
15123 if (token
->type
== CPP_EOF
)
15125 if (token
->type
== CPP_OPEN_BRACE
15126 || token
->type
== CPP_OPEN_PAREN
15127 || token
->type
== CPP_OPEN_SQUARE
)
15129 else if (token
->type
== CPP_CLOSE_BRACE
15130 || token
->type
== CPP_CLOSE_PAREN
15131 || token
->type
== CPP_CLOSE_SQUARE
)
15133 if (nesting_depth
-- == 0)
15136 /* Consume this token. */
15137 cp_lexer_consume_token (parser
->lexer
);
15141 /* If the next token is the indicated keyword, consume it. Otherwise,
15142 issue an error message indicating that TOKEN_DESC was expected.
15144 Returns the token consumed, if the token had the appropriate type.
15145 Otherwise, returns NULL. */
15148 cp_parser_require_keyword (cp_parser
* parser
,
15150 const char* token_desc
)
15152 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
15154 if (token
&& token
->keyword
!= keyword
)
15156 dyn_string_t error_msg
;
15158 /* Format the error message. */
15159 error_msg
= dyn_string_new (0);
15160 dyn_string_append_cstr (error_msg
, "expected ");
15161 dyn_string_append_cstr (error_msg
, token_desc
);
15162 cp_parser_error (parser
, error_msg
->s
);
15163 dyn_string_delete (error_msg
);
15170 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15171 function-definition. */
15174 cp_parser_token_starts_function_definition_p (cp_token
* token
)
15176 return (/* An ordinary function-body begins with an `{'. */
15177 token
->type
== CPP_OPEN_BRACE
15178 /* A ctor-initializer begins with a `:'. */
15179 || token
->type
== CPP_COLON
15180 /* A function-try-block begins with `try'. */
15181 || token
->keyword
== RID_TRY
15182 /* The named return value extension begins with `return'. */
15183 || token
->keyword
== RID_RETURN
);
15186 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15190 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
15194 token
= cp_lexer_peek_token (parser
->lexer
);
15195 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
15198 /* Returns TRUE iff the next token is the "," or ">" ending a
15199 template-argument. ">>" is also accepted (after the full
15200 argument was parsed) because it's probably a typo for "> >",
15201 and there is a specific diagnostic for this. */
15204 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
15208 token
= cp_lexer_peek_token (parser
->lexer
);
15209 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
15210 || token
->type
== CPP_RSHIFT
);
15213 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15214 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15217 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
15222 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
15223 if (token
->type
== CPP_LESS
)
15225 /* Check for the sequence `<::' in the original code. It would be lexed as
15226 `[:', where `[' is a digraph, and there is no whitespace before
15228 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
15231 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
15232 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
15238 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15239 or none_type otherwise. */
15241 static enum tag_types
15242 cp_parser_token_is_class_key (cp_token
* token
)
15244 switch (token
->keyword
)
15249 return record_type
;
15258 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15261 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
15263 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
15264 pedwarn ("`%s' tag used in naming `%#T'",
15265 class_key
== union_type
? "union"
15266 : class_key
== record_type
? "struct" : "class",
15270 /* Issue an error message if DECL is redeclared with different
15271 access than its original declaration [class.access.spec/3].
15272 This applies to nested classes and nested class templates.
15275 static void cp_parser_check_access_in_redeclaration (tree decl
)
15277 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15280 if ((TREE_PRIVATE (decl
)
15281 != (current_access_specifier
== access_private_node
))
15282 || (TREE_PROTECTED (decl
)
15283 != (current_access_specifier
== access_protected_node
)))
15284 error ("%D redeclared with different access", decl
);
15287 /* Look for the `template' keyword, as a syntactic disambiguator.
15288 Return TRUE iff it is present, in which case it will be
15292 cp_parser_optional_template_keyword (cp_parser
*parser
)
15294 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15296 /* The `template' keyword can only be used within templates;
15297 outside templates the parser can always figure out what is a
15298 template and what is not. */
15299 if (!processing_template_decl
)
15301 error ("`template' (as a disambiguator) is only allowed "
15302 "within templates");
15303 /* If this part of the token stream is rescanned, the same
15304 error message would be generated. So, we purge the token
15305 from the stream. */
15306 cp_lexer_purge_token (parser
->lexer
);
15311 /* Consume the `template' keyword. */
15312 cp_lexer_consume_token (parser
->lexer
);
15320 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15321 set PARSER->SCOPE, and perform other related actions. */
15324 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
15329 /* Get the stored value. */
15330 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
15331 /* Perform any access checks that were deferred. */
15332 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
15333 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
15334 /* Set the scope from the stored value. */
15335 parser
->scope
= TREE_VALUE (value
);
15336 parser
->qualifying_scope
= TREE_TYPE (value
);
15337 parser
->object_scope
= NULL_TREE
;
15340 /* Add tokens to CACHE until a non-nested END token appears. */
15343 cp_parser_cache_group (cp_parser
*parser
,
15344 cp_token_cache
*cache
,
15345 enum cpp_ttype end
,
15352 /* Abort a parenthesized expression if we encounter a brace. */
15353 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
15354 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15356 /* If we've reached the end of the file, stop. */
15357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15359 /* Consume the next token. */
15360 token
= cp_lexer_consume_token (parser
->lexer
);
15361 /* Add this token to the tokens we are saving. */
15362 cp_token_cache_push_token (cache
, token
);
15363 /* See if it starts a new group. */
15364 if (token
->type
== CPP_OPEN_BRACE
)
15366 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
15370 else if (token
->type
== CPP_OPEN_PAREN
)
15371 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
15372 else if (token
->type
== end
)
15377 /* Begin parsing tentatively. We always save tokens while parsing
15378 tentatively so that if the tentative parsing fails we can restore the
15382 cp_parser_parse_tentatively (cp_parser
* parser
)
15384 /* Enter a new parsing context. */
15385 parser
->context
= cp_parser_context_new (parser
->context
);
15386 /* Begin saving tokens. */
15387 cp_lexer_save_tokens (parser
->lexer
);
15388 /* In order to avoid repetitive access control error messages,
15389 access checks are queued up until we are no longer parsing
15391 push_deferring_access_checks (dk_deferred
);
15394 /* Commit to the currently active tentative parse. */
15397 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
15399 cp_parser_context
*context
;
15402 /* Mark all of the levels as committed. */
15403 lexer
= parser
->lexer
;
15404 for (context
= parser
->context
; context
->next
; context
= context
->next
)
15406 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
15408 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
15409 while (!cp_lexer_saving_tokens (lexer
))
15410 lexer
= lexer
->next
;
15411 cp_lexer_commit_tokens (lexer
);
15415 /* Abort the currently active tentative parse. All consumed tokens
15416 will be rolled back, and no diagnostics will be issued. */
15419 cp_parser_abort_tentative_parse (cp_parser
* parser
)
15421 cp_parser_simulate_error (parser
);
15422 /* Now, pretend that we want to see if the construct was
15423 successfully parsed. */
15424 cp_parser_parse_definitely (parser
);
15427 /* Stop parsing tentatively. If a parse error has occurred, restore the
15428 token stream. Otherwise, commit to the tokens we have consumed.
15429 Returns true if no error occurred; false otherwise. */
15432 cp_parser_parse_definitely (cp_parser
* parser
)
15434 bool error_occurred
;
15435 cp_parser_context
*context
;
15437 /* Remember whether or not an error occurred, since we are about to
15438 destroy that information. */
15439 error_occurred
= cp_parser_error_occurred (parser
);
15440 /* Remove the topmost context from the stack. */
15441 context
= parser
->context
;
15442 parser
->context
= context
->next
;
15443 /* If no parse errors occurred, commit to the tentative parse. */
15444 if (!error_occurred
)
15446 /* Commit to the tokens read tentatively, unless that was
15448 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
15449 cp_lexer_commit_tokens (parser
->lexer
);
15451 pop_to_parent_deferring_access_checks ();
15453 /* Otherwise, if errors occurred, roll back our state so that things
15454 are just as they were before we began the tentative parse. */
15457 cp_lexer_rollback_tokens (parser
->lexer
);
15458 pop_deferring_access_checks ();
15460 /* Add the context to the front of the free list. */
15461 context
->next
= cp_parser_context_free_list
;
15462 cp_parser_context_free_list
= context
;
15464 return !error_occurred
;
15467 /* Returns true if we are parsing tentatively -- but have decided that
15468 we will stick with this tentative parse, even if errors occur. */
15471 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
15473 return (cp_parser_parsing_tentatively (parser
)
15474 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
15477 /* Returns nonzero iff an error has occurred during the most recent
15478 tentative parse. */
15481 cp_parser_error_occurred (cp_parser
* parser
)
15483 return (cp_parser_parsing_tentatively (parser
)
15484 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
15487 /* Returns nonzero if GNU extensions are allowed. */
15490 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
15492 return parser
->allow_gnu_extensions_p
;
15498 static GTY (()) cp_parser
*the_parser
;
15500 /* External interface. */
15502 /* Parse one entire translation unit. */
15505 c_parse_file (void)
15507 bool error_occurred
;
15508 static bool already_called
= false;
15510 if (already_called
)
15512 sorry ("inter-module optimizations not implemented for C++");
15515 already_called
= true;
15517 the_parser
= cp_parser_new ();
15518 push_deferring_access_checks (flag_access_control
15519 ? dk_no_deferred
: dk_no_check
);
15520 error_occurred
= cp_parser_translation_unit (the_parser
);
15524 /* This variable must be provided by every front end. */
15528 #include "gt-cp-parser.h"