2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token
GTY (())
70 /* The kind of token. */
72 /* The value associated with this token, if any. */
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
77 /* The location at which this token was found. */
81 /* The number of tokens in a single token block. */
83 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
85 /* A group of tokens. These groups are chained together to store
86 large numbers of tokens. (For example, a token block is created
87 when the body of an inline member function is first encountered;
88 the tokens are processed later after the class definition is
91 This somewhat ungainly data structure (as opposed to, say, a
92 variable-length array), is used due to contraints imposed by the
93 current garbage-collection methodology. If it is made more
94 flexible, we could perhaps simplify the data structures involved. */
96 typedef struct cp_token_block
GTY (())
99 cp_token tokens
[CP_TOKEN_BLOCK_NUM_TOKENS
];
100 /* The number of tokens in this block. */
102 /* The next token block in the chain. */
103 struct cp_token_block
*next
;
104 /* The previous block in the chain. */
105 struct cp_token_block
*prev
;
108 typedef struct cp_token_cache
GTY (())
110 /* The first block in the cache. NULL if there are no tokens in the
112 cp_token_block
*first
;
113 /* The last block in the cache. NULL If there are no tokens in the
115 cp_token_block
*last
;
120 static cp_token_cache
*cp_token_cache_new
122 static void cp_token_cache_push_token
123 (cp_token_cache
*, cp_token
*);
125 /* Create a new cp_token_cache. */
127 static cp_token_cache
*
128 cp_token_cache_new ()
130 return (cp_token_cache
*) ggc_alloc_cleared (sizeof (cp_token_cache
));
133 /* Add *TOKEN to *CACHE. */
136 cp_token_cache_push_token (cp_token_cache
*cache
,
139 cp_token_block
*b
= cache
->last
;
141 /* See if we need to allocate a new token block. */
142 if (!b
|| b
->num_tokens
== CP_TOKEN_BLOCK_NUM_TOKENS
)
144 b
= ((cp_token_block
*) ggc_alloc_cleared (sizeof (cp_token_block
)));
145 b
->prev
= cache
->last
;
148 cache
->last
->next
= b
;
152 cache
->first
= cache
->last
= b
;
154 /* Add this token to the current token block. */
155 b
->tokens
[b
->num_tokens
++] = *token
;
158 /* The cp_lexer structure represents the C++ lexer. It is responsible
159 for managing the token stream from the preprocessor and supplying
162 typedef struct cp_lexer
GTY (())
164 /* The memory allocated for the buffer. Never NULL. */
165 cp_token
* GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer
;
166 /* A pointer just past the end of the memory allocated for the buffer. */
167 cp_token
* GTY ((skip (""))) buffer_end
;
168 /* The first valid token in the buffer, or NULL if none. */
169 cp_token
* GTY ((skip (""))) first_token
;
170 /* The next available token. If NEXT_TOKEN is NULL, then there are
171 no more available tokens. */
172 cp_token
* GTY ((skip (""))) next_token
;
173 /* A pointer just past the last available token. If FIRST_TOKEN is
174 NULL, however, there are no available tokens, and then this
175 location is simply the place in which the next token read will be
176 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
177 When the LAST_TOKEN == BUFFER, then the last token is at the
178 highest memory address in the BUFFER. */
179 cp_token
* GTY ((skip (""))) last_token
;
181 /* A stack indicating positions at which cp_lexer_save_tokens was
182 called. The top entry is the most recent position at which we
183 began saving tokens. The entries are differences in token
184 position between FIRST_TOKEN and the first saved token.
186 If the stack is non-empty, we are saving tokens. When a token is
187 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
188 pointer will not. The token stream will be preserved so that it
189 can be reexamined later.
191 If the stack is empty, then we are not saving tokens. Whenever a
192 token is consumed, the FIRST_TOKEN pointer will be moved, and the
193 consumed token will be gone forever. */
194 varray_type saved_tokens
;
196 /* The STRING_CST tokens encountered while processing the current
198 varray_type string_tokens
;
200 /* True if we should obtain more tokens from the preprocessor; false
201 if we are processing a saved token cache. */
204 /* True if we should output debugging information. */
207 /* The next lexer in a linked list of lexers. */
208 struct cp_lexer
*next
;
213 static cp_lexer
*cp_lexer_new_main
215 static cp_lexer
*cp_lexer_new_from_tokens
216 (struct cp_token_cache
*);
217 static int cp_lexer_saving_tokens
219 static cp_token
*cp_lexer_next_token
220 (cp_lexer
*, cp_token
*);
221 static ptrdiff_t cp_lexer_token_difference
222 (cp_lexer
*, cp_token
*, cp_token
*);
223 static cp_token
*cp_lexer_read_token
225 static void cp_lexer_maybe_grow_buffer
227 static void cp_lexer_get_preprocessor_token
228 (cp_lexer
*, cp_token
*);
229 static cp_token
*cp_lexer_peek_token
231 static cp_token
*cp_lexer_peek_nth_token
232 (cp_lexer
*, size_t);
233 static inline bool cp_lexer_next_token_is
234 (cp_lexer
*, enum cpp_ttype
);
235 static bool cp_lexer_next_token_is_not
236 (cp_lexer
*, enum cpp_ttype
);
237 static bool cp_lexer_next_token_is_keyword
238 (cp_lexer
*, enum rid
);
239 static cp_token
*cp_lexer_consume_token
241 static void cp_lexer_purge_token
243 static void cp_lexer_purge_tokens_after
244 (cp_lexer
*, cp_token
*);
245 static void cp_lexer_save_tokens
247 static void cp_lexer_commit_tokens
249 static void cp_lexer_rollback_tokens
251 static inline void cp_lexer_set_source_position_from_token
252 (cp_lexer
*, const cp_token
*);
253 static void cp_lexer_print_token
254 (FILE *, cp_token
*);
255 static inline bool cp_lexer_debugging_p
257 static void cp_lexer_start_debugging
258 (cp_lexer
*) ATTRIBUTE_UNUSED
;
259 static void cp_lexer_stop_debugging
260 (cp_lexer
*) ATTRIBUTE_UNUSED
;
262 /* Manifest constants. */
264 #define CP_TOKEN_BUFFER_SIZE 5
265 #define CP_SAVED_TOKENS_SIZE 5
267 /* A token type for keywords, as opposed to ordinary identifiers. */
268 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
270 /* A token type for template-ids. If a template-id is processed while
271 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
272 the value of the CPP_TEMPLATE_ID is whatever was returned by
273 cp_parser_template_id. */
274 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
276 /* A token type for nested-name-specifiers. If a
277 nested-name-specifier is processed while parsing tentatively, it is
278 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
279 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
280 cp_parser_nested_name_specifier_opt. */
281 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
283 /* A token type for tokens that are not tokens at all; these are used
284 to mark the end of a token block. */
285 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289 /* The stream to which debugging output should be written. */
290 static FILE *cp_lexer_debug_stream
;
292 /* Create a new main C++ lexer, the lexer that gets tokens from the
296 cp_lexer_new_main (void)
299 cp_token first_token
;
301 /* It's possible that lexing the first token will load a PCH file,
302 which is a GC collection point. So we have to grab the first
303 token before allocating any memory. */
304 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
305 cpp_get_callbacks (parse_in
)->valid_pch
= NULL
;
307 /* Allocate the memory. */
308 lexer
= (cp_lexer
*) ggc_alloc_cleared (sizeof (cp_lexer
));
310 /* Create the circular buffer. */
311 lexer
->buffer
= ((cp_token
*)
312 ggc_calloc (CP_TOKEN_BUFFER_SIZE
, sizeof (cp_token
)));
313 lexer
->buffer_end
= lexer
->buffer
+ CP_TOKEN_BUFFER_SIZE
;
315 /* There is one token in the buffer. */
316 lexer
->last_token
= lexer
->buffer
+ 1;
317 lexer
->first_token
= lexer
->buffer
;
318 lexer
->next_token
= lexer
->buffer
;
319 memcpy (lexer
->buffer
, &first_token
, sizeof (cp_token
));
321 /* This lexer obtains more tokens by calling c_lex. */
322 lexer
->main_lexer_p
= true;
324 /* Create the SAVED_TOKENS stack. */
325 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
327 /* Create the STRINGS array. */
328 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
330 /* Assume we are not debugging. */
331 lexer
->debugging_p
= false;
336 /* Create a new lexer whose token stream is primed with the TOKENS.
337 When these tokens are exhausted, no new tokens will be read. */
340 cp_lexer_new_from_tokens (cp_token_cache
*tokens
)
344 cp_token_block
*block
;
345 ptrdiff_t num_tokens
;
347 /* Allocate the memory. */
348 lexer
= (cp_lexer
*) ggc_alloc_cleared (sizeof (cp_lexer
));
350 /* Create a new buffer, appropriately sized. */
352 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
353 num_tokens
+= block
->num_tokens
;
354 lexer
->buffer
= ((cp_token
*) ggc_alloc (num_tokens
* sizeof (cp_token
)));
355 lexer
->buffer_end
= lexer
->buffer
+ num_tokens
;
357 /* Install the tokens. */
358 token
= lexer
->buffer
;
359 for (block
= tokens
->first
; block
!= NULL
; block
= block
->next
)
361 memcpy (token
, block
->tokens
, block
->num_tokens
* sizeof (cp_token
));
362 token
+= block
->num_tokens
;
365 /* The FIRST_TOKEN is the beginning of the buffer. */
366 lexer
->first_token
= lexer
->buffer
;
367 /* The next available token is also at the beginning of the buffer. */
368 lexer
->next_token
= lexer
->buffer
;
369 /* The buffer is full. */
370 lexer
->last_token
= lexer
->first_token
;
372 /* This lexer doesn't obtain more tokens. */
373 lexer
->main_lexer_p
= false;
375 /* Create the SAVED_TOKENS stack. */
376 VARRAY_INT_INIT (lexer
->saved_tokens
, CP_SAVED_TOKENS_SIZE
, "saved_tokens");
378 /* Create the STRINGS array. */
379 VARRAY_TREE_INIT (lexer
->string_tokens
, 32, "strings");
381 /* Assume we are not debugging. */
382 lexer
->debugging_p
= false;
387 /* Returns nonzero if debugging information should be output. */
390 cp_lexer_debugging_p (cp_lexer
*lexer
)
392 return lexer
->debugging_p
;
395 /* Set the current source position from the information stored in
399 cp_lexer_set_source_position_from_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
400 const cp_token
*token
)
402 /* Ideally, the source position information would not be a global
403 variable, but it is. */
405 /* Update the line number. */
406 if (token
->type
!= CPP_EOF
)
407 input_location
= token
->location
;
410 /* TOKEN points into the circular token buffer. Return a pointer to
411 the next token in the buffer. */
413 static inline cp_token
*
414 cp_lexer_next_token (cp_lexer
* lexer
, cp_token
* token
)
417 if (token
== lexer
->buffer_end
)
418 token
= lexer
->buffer
;
422 /* nonzero if we are presently saving tokens. */
425 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
427 return VARRAY_ACTIVE_SIZE (lexer
->saved_tokens
) != 0;
430 /* Return a pointer to the token that is N tokens beyond TOKEN in the
434 cp_lexer_advance_token (cp_lexer
*lexer
, cp_token
*token
, ptrdiff_t n
)
437 if (token
>= lexer
->buffer_end
)
438 token
= lexer
->buffer
+ (token
- lexer
->buffer_end
);
442 /* Returns the number of times that START would have to be incremented
443 to reach FINISH. If START and FINISH are the same, returns zero. */
446 cp_lexer_token_difference (cp_lexer
* lexer
, cp_token
* start
, cp_token
* finish
)
449 return finish
- start
;
451 return ((lexer
->buffer_end
- lexer
->buffer
)
455 /* Obtain another token from the C preprocessor and add it to the
456 token buffer. Returns the newly read token. */
459 cp_lexer_read_token (cp_lexer
* lexer
)
463 /* Make sure there is room in the buffer. */
464 cp_lexer_maybe_grow_buffer (lexer
);
466 /* If there weren't any tokens, then this one will be the first. */
467 if (!lexer
->first_token
)
468 lexer
->first_token
= lexer
->last_token
;
469 /* Similarly, if there were no available tokens, there is one now. */
470 if (!lexer
->next_token
)
471 lexer
->next_token
= lexer
->last_token
;
473 /* Figure out where we're going to store the new token. */
474 token
= lexer
->last_token
;
476 /* Get a new token from the preprocessor. */
477 cp_lexer_get_preprocessor_token (lexer
, token
);
479 /* Increment LAST_TOKEN. */
480 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
482 /* The preprocessor does not yet do translation phase six, i.e., the
483 combination of adjacent string literals. Therefore, we do it
485 if (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
)
490 /* When we grow the buffer, we may invalidate TOKEN. So, save
491 the distance from the beginning of the BUFFER so that we can
493 delta
= cp_lexer_token_difference (lexer
, lexer
->buffer
, token
);
494 /* Make sure there is room in the buffer for another token. */
495 cp_lexer_maybe_grow_buffer (lexer
);
497 token
= lexer
->buffer
;
498 for (i
= 0; i
< delta
; ++i
)
499 token
= cp_lexer_next_token (lexer
, token
);
501 VARRAY_PUSH_TREE (lexer
->string_tokens
, token
->value
);
504 /* Read the token after TOKEN. */
505 cp_lexer_get_preprocessor_token (lexer
, lexer
->last_token
);
506 /* See whether it's another string constant. */
507 if (lexer
->last_token
->type
!= token
->type
)
509 /* If not, then it will be the next real token. */
510 lexer
->last_token
= cp_lexer_next_token (lexer
,
515 /* Chain the strings together. */
516 VARRAY_PUSH_TREE (lexer
->string_tokens
,
517 lexer
->last_token
->value
);
520 /* Create a single STRING_CST. Curiously we have to call
521 combine_strings even if there is only a single string in
522 order to get the type set correctly. */
523 token
->value
= combine_strings (lexer
->string_tokens
);
524 VARRAY_CLEAR (lexer
->string_tokens
);
525 token
->value
= fix_string_type (token
->value
);
526 /* Strings should have type `const char []'. Right now, we will
527 have an ARRAY_TYPE that is constant rather than an array of
528 constant elements. */
529 if (flag_const_strings
)
533 /* Get the current type. It will be an ARRAY_TYPE. */
534 type
= TREE_TYPE (token
->value
);
535 /* Use build_cplus_array_type to rebuild the array, thereby
536 getting the right type. */
537 type
= build_cplus_array_type (TREE_TYPE (type
),
539 /* Reset the type of the token. */
540 TREE_TYPE (token
->value
) = type
;
547 /* If the circular buffer is full, make it bigger. */
550 cp_lexer_maybe_grow_buffer (cp_lexer
* lexer
)
552 /* If the buffer is full, enlarge it. */
553 if (lexer
->last_token
== lexer
->first_token
)
555 cp_token
*new_buffer
;
556 cp_token
*old_buffer
;
557 cp_token
*new_first_token
;
558 ptrdiff_t buffer_length
;
559 size_t num_tokens_to_copy
;
561 /* Remember the current buffer pointer. It will become invalid,
562 but we will need to do pointer arithmetic involving this
564 old_buffer
= lexer
->buffer
;
565 /* Compute the current buffer size. */
566 buffer_length
= lexer
->buffer_end
- lexer
->buffer
;
567 /* Allocate a buffer twice as big. */
568 new_buffer
= ((cp_token
*)
569 ggc_realloc (lexer
->buffer
,
570 2 * buffer_length
* sizeof (cp_token
)));
572 /* Because the buffer is circular, logically consecutive tokens
573 are not necessarily placed consecutively in memory.
574 Therefore, we must keep move the tokens that were before
575 FIRST_TOKEN to the second half of the newly allocated
577 num_tokens_to_copy
= (lexer
->first_token
- old_buffer
);
578 memcpy (new_buffer
+ buffer_length
,
580 num_tokens_to_copy
* sizeof (cp_token
));
581 /* Clear the rest of the buffer. We never look at this storage,
582 but the garbage collector may. */
583 memset (new_buffer
+ buffer_length
+ num_tokens_to_copy
, 0,
584 (buffer_length
- num_tokens_to_copy
) * sizeof (cp_token
));
586 /* Now recompute all of the buffer pointers. */
588 = new_buffer
+ (lexer
->first_token
- old_buffer
);
589 if (lexer
->next_token
!= NULL
)
591 ptrdiff_t next_token_delta
;
593 if (lexer
->next_token
> lexer
->first_token
)
594 next_token_delta
= lexer
->next_token
- lexer
->first_token
;
597 buffer_length
- (lexer
->first_token
- lexer
->next_token
);
598 lexer
->next_token
= new_first_token
+ next_token_delta
;
600 lexer
->last_token
= new_first_token
+ buffer_length
;
601 lexer
->buffer
= new_buffer
;
602 lexer
->buffer_end
= new_buffer
+ buffer_length
* 2;
603 lexer
->first_token
= new_first_token
;
607 /* Store the next token from the preprocessor in *TOKEN. */
610 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
615 /* If this not the main lexer, return a terminating CPP_EOF token. */
616 if (lexer
!= NULL
&& !lexer
->main_lexer_p
)
618 token
->type
= CPP_EOF
;
619 token
->location
.line
= 0;
620 token
->location
.file
= NULL
;
621 token
->value
= NULL_TREE
;
622 token
->keyword
= RID_MAX
;
628 /* Keep going until we get a token we like. */
631 /* Get a new token from the preprocessor. */
632 token
->type
= c_lex (&token
->value
);
633 /* Issue messages about tokens we cannot process. */
639 error ("invalid token");
643 /* This is a good token, so we exit the loop. */
648 /* Now we've got our token. */
649 token
->location
= input_location
;
651 /* Check to see if this token is a keyword. */
652 if (token
->type
== CPP_NAME
653 && C_IS_RESERVED_WORD (token
->value
))
655 /* Mark this token as a keyword. */
656 token
->type
= CPP_KEYWORD
;
657 /* Record which keyword. */
658 token
->keyword
= C_RID_CODE (token
->value
);
659 /* Update the value. Some keywords are mapped to particular
660 entities, rather than simply having the value of the
661 corresponding IDENTIFIER_NODE. For example, `__const' is
662 mapped to `const'. */
663 token
->value
= ridpointers
[token
->keyword
];
666 token
->keyword
= RID_MAX
;
669 /* Return a pointer to the next token in the token stream, but do not
673 cp_lexer_peek_token (cp_lexer
* lexer
)
677 /* If there are no tokens, read one now. */
678 if (!lexer
->next_token
)
679 cp_lexer_read_token (lexer
);
681 /* Provide debugging output. */
682 if (cp_lexer_debugging_p (lexer
))
684 fprintf (cp_lexer_debug_stream
, "cp_lexer: peeking at token: ");
685 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
686 fprintf (cp_lexer_debug_stream
, "\n");
689 token
= lexer
->next_token
;
690 cp_lexer_set_source_position_from_token (lexer
, token
);
694 /* Return true if the next token has the indicated TYPE. */
697 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
701 /* Peek at the next token. */
702 token
= cp_lexer_peek_token (lexer
);
703 /* Check to see if it has the indicated TYPE. */
704 return token
->type
== type
;
707 /* Return true if the next token does not have the indicated TYPE. */
710 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
712 return !cp_lexer_next_token_is (lexer
, type
);
715 /* Return true if the next token is the indicated KEYWORD. */
718 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
722 /* Peek at the next token. */
723 token
= cp_lexer_peek_token (lexer
);
724 /* Check to see if it is the indicated keyword. */
725 return token
->keyword
== keyword
;
728 /* Return a pointer to the Nth token in the token stream. If N is 1,
729 then this is precisely equivalent to cp_lexer_peek_token. */
732 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
736 /* N is 1-based, not zero-based. */
737 my_friendly_assert (n
> 0, 20000224);
739 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
740 token
= lexer
->next_token
;
741 /* If there are no tokens in the buffer, get one now. */
744 cp_lexer_read_token (lexer
);
745 token
= lexer
->next_token
;
748 /* Now, read tokens until we have enough. */
751 /* Advance to the next token. */
752 token
= cp_lexer_next_token (lexer
, token
);
753 /* If that's all the tokens we have, read a new one. */
754 if (token
== lexer
->last_token
)
755 token
= cp_lexer_read_token (lexer
);
761 /* Consume the next token. The pointer returned is valid only until
762 another token is read. Callers should preserve copy the token
763 explicitly if they will need its value for a longer period of
767 cp_lexer_consume_token (cp_lexer
* lexer
)
771 /* If there are no tokens, read one now. */
772 if (!lexer
->next_token
)
773 cp_lexer_read_token (lexer
);
775 /* Remember the token we'll be returning. */
776 token
= lexer
->next_token
;
778 /* Increment NEXT_TOKEN. */
779 lexer
->next_token
= cp_lexer_next_token (lexer
,
781 /* Check to see if we're all out of tokens. */
782 if (lexer
->next_token
== lexer
->last_token
)
783 lexer
->next_token
= NULL
;
785 /* If we're not saving tokens, then move FIRST_TOKEN too. */
786 if (!cp_lexer_saving_tokens (lexer
))
788 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
789 if (!lexer
->next_token
)
790 lexer
->first_token
= NULL
;
792 lexer
->first_token
= lexer
->next_token
;
795 /* Provide debugging output. */
796 if (cp_lexer_debugging_p (lexer
))
798 fprintf (cp_lexer_debug_stream
, "cp_lexer: consuming token: ");
799 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
800 fprintf (cp_lexer_debug_stream
, "\n");
806 /* Permanently remove the next token from the token stream. There
807 must be a valid next token already; this token never reads
808 additional tokens from the preprocessor. */
811 cp_lexer_purge_token (cp_lexer
*lexer
)
814 cp_token
*next_token
;
816 token
= lexer
->next_token
;
819 next_token
= cp_lexer_next_token (lexer
, token
);
820 if (next_token
== lexer
->last_token
)
822 *token
= *next_token
;
826 lexer
->last_token
= token
;
827 /* The token purged may have been the only token remaining; if so,
829 if (lexer
->next_token
== token
)
830 lexer
->next_token
= NULL
;
833 /* Permanently remove all tokens after TOKEN, up to, but not
834 including, the token that will be returned next by
835 cp_lexer_peek_token. */
838 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*token
)
844 if (lexer
->next_token
)
846 /* Copy the tokens that have not yet been read to the location
847 immediately following TOKEN. */
848 t1
= cp_lexer_next_token (lexer
, token
);
849 t2
= peek
= cp_lexer_peek_token (lexer
);
850 /* Move tokens into the vacant area between TOKEN and PEEK. */
851 while (t2
!= lexer
->last_token
)
854 t1
= cp_lexer_next_token (lexer
, t1
);
855 t2
= cp_lexer_next_token (lexer
, t2
);
857 /* Now, the next available token is right after TOKEN. */
858 lexer
->next_token
= cp_lexer_next_token (lexer
, token
);
859 /* And the last token is wherever we ended up. */
860 lexer
->last_token
= t1
;
864 /* There are no tokens in the buffer, so there is nothing to
865 copy. The last token in the buffer is TOKEN itself. */
866 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
870 /* Begin saving tokens. All tokens consumed after this point will be
874 cp_lexer_save_tokens (cp_lexer
* lexer
)
876 /* Provide debugging output. */
877 if (cp_lexer_debugging_p (lexer
))
878 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
880 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
881 restore the tokens if required. */
882 if (!lexer
->next_token
)
883 cp_lexer_read_token (lexer
);
885 VARRAY_PUSH_INT (lexer
->saved_tokens
,
886 cp_lexer_token_difference (lexer
,
891 /* Commit to the portion of the token stream most recently saved. */
894 cp_lexer_commit_tokens (cp_lexer
* lexer
)
896 /* Provide debugging output. */
897 if (cp_lexer_debugging_p (lexer
))
898 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
900 VARRAY_POP (lexer
->saved_tokens
);
903 /* Return all tokens saved since the last call to cp_lexer_save_tokens
904 to the token stream. Stop saving tokens. */
907 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
911 /* Provide debugging output. */
912 if (cp_lexer_debugging_p (lexer
))
913 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
915 /* Find the token that was the NEXT_TOKEN when we started saving
917 delta
= VARRAY_TOP_INT(lexer
->saved_tokens
);
918 /* Make it the next token again now. */
919 lexer
->next_token
= cp_lexer_advance_token (lexer
,
922 /* It might be the case that there were no tokens when we started
923 saving tokens, but that there are some tokens now. */
924 if (!lexer
->next_token
&& lexer
->first_token
)
925 lexer
->next_token
= lexer
->first_token
;
927 /* Stop saving tokens. */
928 VARRAY_POP (lexer
->saved_tokens
);
931 /* Print a representation of the TOKEN on the STREAM. */
934 cp_lexer_print_token (FILE * stream
, cp_token
* token
)
936 const char *token_type
= NULL
;
938 /* Figure out what kind of token this is. */
946 token_type
= "COMMA";
950 token_type
= "OPEN_PAREN";
953 case CPP_CLOSE_PAREN
:
954 token_type
= "CLOSE_PAREN";
958 token_type
= "OPEN_BRACE";
961 case CPP_CLOSE_BRACE
:
962 token_type
= "CLOSE_BRACE";
966 token_type
= "SEMICOLON";
978 token_type
= "keyword";
981 /* This is not a token that we know how to handle yet. */
986 /* If we have a name for the token, print it out. Otherwise, we
987 simply give the numeric code. */
989 fprintf (stream
, "%s", token_type
);
991 fprintf (stream
, "%d", token
->type
);
992 /* And, for an identifier, print the identifier name. */
993 if (token
->type
== CPP_NAME
994 /* Some keywords have a value that is not an IDENTIFIER_NODE.
995 For example, `struct' is mapped to an INTEGER_CST. */
996 || (token
->type
== CPP_KEYWORD
997 && TREE_CODE (token
->value
) == IDENTIFIER_NODE
))
998 fprintf (stream
, " %s", IDENTIFIER_POINTER (token
->value
));
1001 /* Start emitting debugging information. */
1004 cp_lexer_start_debugging (cp_lexer
* lexer
)
1006 ++lexer
->debugging_p
;
1009 /* Stop emitting debugging information. */
1012 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1014 --lexer
->debugging_p
;
1023 A cp_parser parses the token stream as specified by the C++
1024 grammar. Its job is purely parsing, not semantic analysis. For
1025 example, the parser breaks the token stream into declarators,
1026 expressions, statements, and other similar syntactic constructs.
1027 It does not check that the types of the expressions on either side
1028 of an assignment-statement are compatible, or that a function is
1029 not declared with a parameter of type `void'.
1031 The parser invokes routines elsewhere in the compiler to perform
1032 semantic analysis and to build up the abstract syntax tree for the
1035 The parser (and the template instantiation code, which is, in a
1036 way, a close relative of parsing) are the only parts of the
1037 compiler that should be calling push_scope and pop_scope, or
1038 related functions. The parser (and template instantiation code)
1039 keeps track of what scope is presently active; everything else
1040 should simply honor that. (The code that generates static
1041 initializers may also need to set the scope, in order to check
1042 access control correctly when emitting the initializers.)
1047 The parser is of the standard recursive-descent variety. Upcoming
1048 tokens in the token stream are examined in order to determine which
1049 production to use when parsing a non-terminal. Some C++ constructs
1050 require arbitrary look ahead to disambiguate. For example, it is
1051 impossible, in the general case, to tell whether a statement is an
1052 expression or declaration without scanning the entire statement.
1053 Therefore, the parser is capable of "parsing tentatively." When the
1054 parser is not sure what construct comes next, it enters this mode.
1055 Then, while we attempt to parse the construct, the parser queues up
1056 error messages, rather than issuing them immediately, and saves the
1057 tokens it consumes. If the construct is parsed successfully, the
1058 parser "commits", i.e., it issues any queued error messages and
1059 the tokens that were being preserved are permanently discarded.
1060 If, however, the construct is not parsed successfully, the parser
1061 rolls back its state completely so that it can resume parsing using
1062 a different alternative.
1067 The performance of the parser could probably be improved
1068 substantially. Some possible improvements include:
1070 - The expression parser recurses through the various levels of
1071 precedence as specified in the grammar, rather than using an
1072 operator-precedence technique. Therefore, parsing a simple
1073 identifier requires multiple recursive calls.
1075 - We could often eliminate the need to parse tentatively by
1076 looking ahead a little bit. In some places, this approach
1077 might not entirely eliminate the need to parse tentatively, but
1078 it might still speed up the average case. */
1080 /* Flags that are passed to some parsing functions. These values can
1081 be bitwise-ored together. */
1083 typedef enum cp_parser_flags
1086 CP_PARSER_FLAGS_NONE
= 0x0,
1087 /* The construct is optional. If it is not present, then no error
1088 should be issued. */
1089 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1090 /* When parsing a type-specifier, do not allow user-defined types. */
1091 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1094 /* The different kinds of ids that we ecounter. */
1096 typedef enum cp_parser_id_kind
1098 /* Not an id at all. */
1099 CP_PARSER_ID_KIND_NONE
,
1100 /* An unqualified-id that is not a template-id. */
1101 CP_PARSER_ID_KIND_UNQUALIFIED
,
1102 /* An unqualified template-id. */
1103 CP_PARSER_ID_KIND_TEMPLATE_ID
,
1104 /* A qualified-id. */
1105 CP_PARSER_ID_KIND_QUALIFIED
1106 } cp_parser_id_kind
;
1108 /* The different kinds of declarators we want to parse. */
1110 typedef enum cp_parser_declarator_kind
1112 /* We want an abstract declartor. */
1113 CP_PARSER_DECLARATOR_ABSTRACT
,
1114 /* We want a named declarator. */
1115 CP_PARSER_DECLARATOR_NAMED
,
1116 /* We don't mind, but the name must be an unqualified-id */
1117 CP_PARSER_DECLARATOR_EITHER
1118 } cp_parser_declarator_kind
;
1120 /* A mapping from a token type to a corresponding tree node type. */
1122 typedef struct cp_parser_token_tree_map_node
1124 /* The token type. */
1125 enum cpp_ttype token_type
;
1126 /* The corresponding tree code. */
1127 enum tree_code tree_type
;
1128 } cp_parser_token_tree_map_node
;
1130 /* A complete map consists of several ordinary entries, followed by a
1131 terminator. The terminating entry has a token_type of CPP_EOF. */
1133 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map
[];
1135 /* The status of a tentative parse. */
1137 typedef enum cp_parser_status_kind
1139 /* No errors have occurred. */
1140 CP_PARSER_STATUS_KIND_NO_ERROR
,
1141 /* An error has occurred. */
1142 CP_PARSER_STATUS_KIND_ERROR
,
1143 /* We are committed to this tentative parse, whether or not an error
1145 CP_PARSER_STATUS_KIND_COMMITTED
1146 } cp_parser_status_kind
;
1148 /* Context that is saved and restored when parsing tentatively. */
1150 typedef struct cp_parser_context
GTY (())
1152 /* If this is a tentative parsing context, the status of the
1154 enum cp_parser_status_kind status
;
1155 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1156 that are looked up in this context must be looked up both in the
1157 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1158 the context of the containing expression. */
1160 /* The next parsing context in the stack. */
1161 struct cp_parser_context
*next
;
1162 } cp_parser_context
;
1166 /* Constructors and destructors. */
1168 static cp_parser_context
*cp_parser_context_new
1169 (cp_parser_context
*);
1171 /* Class variables. */
1173 static GTY((deletable (""))) cp_parser_context
* cp_parser_context_free_list
;
1175 /* Constructors and destructors. */
1177 /* Construct a new context. The context below this one on the stack
1178 is given by NEXT. */
1180 static cp_parser_context
*
1181 cp_parser_context_new (cp_parser_context
* next
)
1183 cp_parser_context
*context
;
1185 /* Allocate the storage. */
1186 if (cp_parser_context_free_list
!= NULL
)
1188 /* Pull the first entry from the free list. */
1189 context
= cp_parser_context_free_list
;
1190 cp_parser_context_free_list
= context
->next
;
1191 memset ((char *)context
, 0, sizeof (*context
));
1194 context
= ((cp_parser_context
*)
1195 ggc_alloc_cleared (sizeof (cp_parser_context
)));
1196 /* No errors have occurred yet in this context. */
1197 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1198 /* If this is not the bottomost context, copy information that we
1199 need from the previous context. */
1202 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203 expression, then we are parsing one in this context, too. */
1204 context
->object_type
= next
->object_type
;
1205 /* Thread the stack. */
1206 context
->next
= next
;
1212 /* The cp_parser structure represents the C++ parser. */
1214 typedef struct cp_parser
GTY(())
1216 /* The lexer from which we are obtaining tokens. */
1219 /* The scope in which names should be looked up. If NULL_TREE, then
1220 we look up names in the scope that is currently open in the
1221 source program. If non-NULL, this is either a TYPE or
1222 NAMESPACE_DECL for the scope in which we should look.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1239 tree qualifying_scope
;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context
*context
;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p
;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p
;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissable. */
1264 bool default_arg_ok_p
;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool constant_expression_p
;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_constant_expression_p
;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_constant_expression_p
;
1280 /* TRUE if local variable names and `this' are forbidden in the
1282 bool local_variables_forbidden_p
;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1287 bool in_unbraced_linkage_specification_p
;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p
;
1293 /* If non-NULL, then we are parsing a construct where new type
1294 definitions are not permitted. The string stored here will be
1295 issued as an error message if a type is defined. */
1296 const char *type_definition_forbidden_message
;
1298 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1299 but may not have been parsed. These functions are friends of
1300 members defined within a class-specification; they are not
1301 procssed until the class is complete. The active queue is at the
1304 Within each queue, functions appear in the reverse order that
1305 they appeared in the source. Each TREE_VALUE is a
1306 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1308 tree unparsed_functions_queues
;
1310 /* The number of classes whose definitions are currently in
1312 unsigned num_classes_being_defined
;
1314 /* The number of template parameter lists that apply directly to the
1315 current declaration. */
1316 unsigned num_template_parameter_lists
;
1319 /* The type of a function that parses some kind of expression */
1320 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1324 /* Constructors and destructors. */
1326 static cp_parser
*cp_parser_new
1329 /* Routines to parse various constructs.
1331 Those that return `tree' will return the error_mark_node (rather
1332 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1333 Sometimes, they will return an ordinary node if error-recovery was
1334 attempted, even though a parse error occurrred. So, to check
1335 whether or not a parse error occurred, you should always use
1336 cp_parser_error_occurred. If the construct is optional (indicated
1337 either by an `_opt' in the name of the function that does the
1338 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1339 the construct is not present. */
1341 /* Lexical conventions [gram.lex] */
1343 static tree cp_parser_identifier
1346 /* Basic concepts [gram.basic] */
1348 static bool cp_parser_translation_unit
1351 /* Expressions [gram.expr] */
1353 static tree cp_parser_primary_expression
1354 (cp_parser
*, cp_parser_id_kind
*, tree
*);
1355 static tree cp_parser_id_expression
1356 (cp_parser
*, bool, bool, bool *);
1357 static tree cp_parser_unqualified_id
1358 (cp_parser
*, bool, bool);
1359 static tree cp_parser_nested_name_specifier_opt
1360 (cp_parser
*, bool, bool, bool);
1361 static tree cp_parser_nested_name_specifier
1362 (cp_parser
*, bool, bool, bool);
1363 static tree cp_parser_class_or_namespace_name
1364 (cp_parser
*, bool, bool, bool, bool);
1365 static tree cp_parser_postfix_expression
1366 (cp_parser
*, bool);
1367 static tree cp_parser_expression_list
1369 static void cp_parser_pseudo_destructor_name
1370 (cp_parser
*, tree
*, tree
*);
1371 static tree cp_parser_unary_expression
1372 (cp_parser
*, bool);
1373 static enum tree_code cp_parser_unary_operator
1375 static tree cp_parser_new_expression
1377 static tree cp_parser_new_placement
1379 static tree cp_parser_new_type_id
1381 static tree cp_parser_new_declarator_opt
1383 static tree cp_parser_direct_new_declarator
1385 static tree cp_parser_new_initializer
1387 static tree cp_parser_delete_expression
1389 static tree cp_parser_cast_expression
1390 (cp_parser
*, bool);
1391 static tree cp_parser_pm_expression
1393 static tree cp_parser_multiplicative_expression
1395 static tree cp_parser_additive_expression
1397 static tree cp_parser_shift_expression
1399 static tree cp_parser_relational_expression
1401 static tree cp_parser_equality_expression
1403 static tree cp_parser_and_expression
1405 static tree cp_parser_exclusive_or_expression
1407 static tree cp_parser_inclusive_or_expression
1409 static tree cp_parser_logical_and_expression
1411 static tree cp_parser_logical_or_expression
1413 static tree cp_parser_conditional_expression
1415 static tree cp_parser_question_colon_clause
1416 (cp_parser
*, tree
);
1417 static tree cp_parser_assignment_expression
1419 static enum tree_code cp_parser_assignment_operator_opt
1421 static tree cp_parser_expression
1423 static tree cp_parser_constant_expression
1424 (cp_parser
*, bool, bool *);
1426 /* Statements [gram.stmt.stmt] */
1428 static void cp_parser_statement
1430 static tree cp_parser_labeled_statement
1432 static tree cp_parser_expression_statement
1434 static tree cp_parser_compound_statement
1436 static void cp_parser_statement_seq_opt
1438 static tree cp_parser_selection_statement
1440 static tree cp_parser_condition
1442 static tree cp_parser_iteration_statement
1444 static void cp_parser_for_init_statement
1446 static tree cp_parser_jump_statement
1448 static void cp_parser_declaration_statement
1451 static tree cp_parser_implicitly_scoped_statement
1453 static void cp_parser_already_scoped_statement
1456 /* Declarations [gram.dcl.dcl] */
1458 static void cp_parser_declaration_seq_opt
1460 static void cp_parser_declaration
1462 static void cp_parser_block_declaration
1463 (cp_parser
*, bool);
1464 static void cp_parser_simple_declaration
1465 (cp_parser
*, bool);
1466 static tree cp_parser_decl_specifier_seq
1467 (cp_parser
*, cp_parser_flags
, tree
*, bool *);
1468 static tree cp_parser_storage_class_specifier_opt
1470 static tree cp_parser_function_specifier_opt
1472 static tree cp_parser_type_specifier
1473 (cp_parser
*, cp_parser_flags
, bool, bool, bool *, bool *);
1474 static tree cp_parser_simple_type_specifier
1475 (cp_parser
*, cp_parser_flags
);
1476 static tree cp_parser_type_name
1478 static tree cp_parser_elaborated_type_specifier
1479 (cp_parser
*, bool, bool);
1480 static tree cp_parser_enum_specifier
1482 static void cp_parser_enumerator_list
1483 (cp_parser
*, tree
);
1484 static void cp_parser_enumerator_definition
1485 (cp_parser
*, tree
);
1486 static tree cp_parser_namespace_name
1488 static void cp_parser_namespace_definition
1490 static void cp_parser_namespace_body
1492 static tree cp_parser_qualified_namespace_specifier
1494 static void cp_parser_namespace_alias_definition
1496 static void cp_parser_using_declaration
1498 static void cp_parser_using_directive
1500 static void cp_parser_asm_definition
1502 static void cp_parser_linkage_specification
1505 /* Declarators [gram.dcl.decl] */
1507 static tree cp_parser_init_declarator
1508 (cp_parser
*, tree
, tree
, bool, bool, bool *);
1509 static tree cp_parser_declarator
1510 (cp_parser
*, cp_parser_declarator_kind
, bool *);
1511 static tree cp_parser_direct_declarator
1512 (cp_parser
*, cp_parser_declarator_kind
, bool *);
1513 static enum tree_code cp_parser_ptr_operator
1514 (cp_parser
*, tree
*, tree
*);
1515 static tree cp_parser_cv_qualifier_seq_opt
1517 static tree cp_parser_cv_qualifier_opt
1519 static tree cp_parser_declarator_id
1521 static tree cp_parser_type_id
1523 static tree cp_parser_type_specifier_seq
1525 static tree cp_parser_parameter_declaration_clause
1527 static tree cp_parser_parameter_declaration_list
1529 static tree cp_parser_parameter_declaration
1530 (cp_parser
*, bool);
1531 static tree cp_parser_function_definition
1532 (cp_parser
*, bool *);
1533 static void cp_parser_function_body
1535 static tree cp_parser_initializer
1536 (cp_parser
*, bool *);
1537 static tree cp_parser_initializer_clause
1539 static tree cp_parser_initializer_list
1542 static bool cp_parser_ctor_initializer_opt_and_function_body
1545 /* Classes [gram.class] */
1547 static tree cp_parser_class_name
1548 (cp_parser
*, bool, bool, bool, bool, bool);
1549 static tree cp_parser_class_specifier
1551 static tree cp_parser_class_head
1552 (cp_parser
*, bool *);
1553 static enum tag_types cp_parser_class_key
1555 static void cp_parser_member_specification_opt
1557 static void cp_parser_member_declaration
1559 static tree cp_parser_pure_specifier
1561 static tree cp_parser_constant_initializer
1564 /* Derived classes [gram.class.derived] */
1566 static tree cp_parser_base_clause
1568 static tree cp_parser_base_specifier
1571 /* Special member functions [gram.special] */
1573 static tree cp_parser_conversion_function_id
1575 static tree cp_parser_conversion_type_id
1577 static tree cp_parser_conversion_declarator_opt
1579 static bool cp_parser_ctor_initializer_opt
1581 static void cp_parser_mem_initializer_list
1583 static tree cp_parser_mem_initializer
1585 static tree cp_parser_mem_initializer_id
1588 /* Overloading [gram.over] */
1590 static tree cp_parser_operator_function_id
1592 static tree cp_parser_operator
1595 /* Templates [gram.temp] */
1597 static void cp_parser_template_declaration
1598 (cp_parser
*, bool);
1599 static tree cp_parser_template_parameter_list
1601 static tree cp_parser_template_parameter
1603 static tree cp_parser_type_parameter
1605 static tree cp_parser_template_id
1606 (cp_parser
*, bool, bool);
1607 static tree cp_parser_template_name
1608 (cp_parser
*, bool, bool);
1609 static tree cp_parser_template_argument_list
1611 static tree cp_parser_template_argument
1613 static void cp_parser_explicit_instantiation
1615 static void cp_parser_explicit_specialization
1618 /* Exception handling [gram.exception] */
1620 static tree cp_parser_try_block
1622 static bool cp_parser_function_try_block
1624 static void cp_parser_handler_seq
1626 static void cp_parser_handler
1628 static tree cp_parser_exception_declaration
1630 static tree cp_parser_throw_expression
1632 static tree cp_parser_exception_specification_opt
1634 static tree cp_parser_type_id_list
1637 /* GNU Extensions */
1639 static tree cp_parser_asm_specification_opt
1641 static tree cp_parser_asm_operand_list
1643 static tree cp_parser_asm_clobber_list
1645 static tree cp_parser_attributes_opt
1647 static tree cp_parser_attribute_list
1649 static bool cp_parser_extension_opt
1650 (cp_parser
*, int *);
1651 static void cp_parser_label_declaration
1654 /* Utility Routines */
1656 static tree cp_parser_lookup_name
1657 (cp_parser
*, tree
, bool, bool, bool);
1658 static tree cp_parser_lookup_name_simple
1659 (cp_parser
*, tree
);
1660 static tree cp_parser_maybe_treat_template_as_class
1662 static bool cp_parser_check_declarator_template_parameters
1663 (cp_parser
*, tree
);
1664 static bool cp_parser_check_template_parameters
1665 (cp_parser
*, unsigned);
1666 static tree cp_parser_binary_expression
1667 (cp_parser
*, const cp_parser_token_tree_map
, cp_parser_expression_fn
);
1668 static tree cp_parser_global_scope_opt
1669 (cp_parser
*, bool);
1670 static bool cp_parser_constructor_declarator_p
1671 (cp_parser
*, bool);
1672 static tree cp_parser_function_definition_from_specifiers_and_declarator
1673 (cp_parser
*, tree
, tree
, tree
);
1674 static tree cp_parser_function_definition_after_declarator
1675 (cp_parser
*, bool);
1676 static void cp_parser_template_declaration_after_export
1677 (cp_parser
*, bool);
1678 static tree cp_parser_single_declaration
1679 (cp_parser
*, bool, bool *);
1680 static tree cp_parser_functional_cast
1681 (cp_parser
*, tree
);
1682 static void cp_parser_late_parsing_for_member
1683 (cp_parser
*, tree
);
1684 static void cp_parser_late_parsing_default_args
1685 (cp_parser
*, tree
);
1686 static tree cp_parser_sizeof_operand
1687 (cp_parser
*, enum rid
);
1688 static bool cp_parser_declares_only_class_p
1690 static bool cp_parser_friend_p
1692 static cp_token
*cp_parser_require
1693 (cp_parser
*, enum cpp_ttype
, const char *);
1694 static cp_token
*cp_parser_require_keyword
1695 (cp_parser
*, enum rid
, const char *);
1696 static bool cp_parser_token_starts_function_definition_p
1698 static bool cp_parser_next_token_starts_class_definition_p
1700 static enum tag_types cp_parser_token_is_class_key
1702 static void cp_parser_check_class_key
1703 (enum tag_types
, tree type
);
1704 static bool cp_parser_optional_template_keyword
1706 static void cp_parser_pre_parsed_nested_name_specifier
1708 static void cp_parser_cache_group
1709 (cp_parser
*, cp_token_cache
*, enum cpp_ttype
, unsigned);
1710 static void cp_parser_parse_tentatively
1712 static void cp_parser_commit_to_tentative_parse
1714 static void cp_parser_abort_tentative_parse
1716 static bool cp_parser_parse_definitely
1718 static inline bool cp_parser_parsing_tentatively
1720 static bool cp_parser_committed_to_tentative_parse
1722 static void cp_parser_error
1723 (cp_parser
*, const char *);
1724 static bool cp_parser_simulate_error
1726 static void cp_parser_check_type_definition
1728 static tree cp_parser_non_constant_expression
1730 static tree cp_parser_non_constant_id_expression
1732 static bool cp_parser_diagnose_invalid_type_name
1734 static bool cp_parser_skip_to_closing_parenthesis
1736 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1738 static void cp_parser_skip_to_end_of_statement
1740 static void cp_parser_consume_semicolon_at_end_of_statement
1742 static void cp_parser_skip_to_end_of_block_or_statement
1744 static void cp_parser_skip_to_closing_brace
1746 static void cp_parser_skip_until_found
1747 (cp_parser
*, enum cpp_ttype
, const char *);
1748 static bool cp_parser_error_occurred
1750 static bool cp_parser_allow_gnu_extensions_p
1752 static bool cp_parser_is_string_literal
1754 static bool cp_parser_is_keyword
1755 (cp_token
*, enum rid
);
1756 static tree cp_parser_scope_through_which_access_occurs
1759 /* Returns nonzero if we are parsing tentatively. */
1762 cp_parser_parsing_tentatively (cp_parser
* parser
)
1764 return parser
->context
->next
!= NULL
;
1767 /* Returns nonzero if TOKEN is a string literal. */
1770 cp_parser_is_string_literal (cp_token
* token
)
1772 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1775 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1778 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1780 return token
->keyword
== keyword
;
1783 /* Returns the scope through which DECL is being accessed, or
1784 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
1785 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
1786 or `x', respectively. If the DECL was named as `A::B' then
1787 NESTED_NAME_SPECIFIER is `A'. */
1790 cp_parser_scope_through_which_access_occurs (tree decl
,
1792 tree nested_name_specifier
)
1795 tree qualifying_type
= NULL_TREE
;
1797 /* Determine the SCOPE of DECL. */
1798 scope
= context_for_name_lookup (decl
);
1799 /* If the SCOPE is not a type, then DECL is not a member. */
1800 if (!TYPE_P (scope
))
1802 /* Figure out the type through which DECL is being accessed. */
1804 /* OBJECT_TYPE might not be a class type; consider:
1806 class A { typedef int I; };
1810 In this case, we will have "A::I" as the DECL, but "I" as the
1812 && CLASS_TYPE_P (object_type
)
1813 && DERIVED_FROM_P (scope
, object_type
))
1814 /* If we are processing a `->' or `.' expression, use the type of the
1816 qualifying_type
= object_type
;
1817 else if (nested_name_specifier
)
1819 /* If the reference is to a non-static member of the
1820 current class, treat it as if it were referenced through
1822 if (DECL_NONSTATIC_MEMBER_P (decl
)
1823 && current_class_ptr
1824 && DERIVED_FROM_P (scope
, current_class_type
))
1825 qualifying_type
= current_class_type
;
1826 /* Otherwise, use the type indicated by the
1827 nested-name-specifier. */
1829 qualifying_type
= nested_name_specifier
;
1832 /* Otherwise, the name must be from the current class or one of
1834 qualifying_type
= currently_open_derived_class (scope
);
1836 return qualifying_type
;
1839 /* Issue the indicated error MESSAGE. */
1842 cp_parser_error (cp_parser
* parser
, const char* message
)
1844 /* Output the MESSAGE -- unless we're parsing tentatively. */
1845 if (!cp_parser_simulate_error (parser
))
1849 /* If we are parsing tentatively, remember that an error has occurred
1850 during this tentative parse. Returns true if the error was
1851 simulated; false if a messgae should be issued by the caller. */
1854 cp_parser_simulate_error (cp_parser
* parser
)
1856 if (cp_parser_parsing_tentatively (parser
)
1857 && !cp_parser_committed_to_tentative_parse (parser
))
1859 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1865 /* This function is called when a type is defined. If type
1866 definitions are forbidden at this point, an error message is
1870 cp_parser_check_type_definition (cp_parser
* parser
)
1872 /* If types are forbidden here, issue a message. */
1873 if (parser
->type_definition_forbidden_message
)
1874 /* Use `%s' to print the string in case there are any escape
1875 characters in the message. */
1876 error ("%s", parser
->type_definition_forbidden_message
);
1879 /* Issue an eror message about the fact that THING appeared in a
1880 constant-expression. Returns ERROR_MARK_NODE. */
1883 cp_parser_non_constant_expression (const char *thing
)
1885 error ("%s cannot appear in a constant-expression", thing
);
1886 return error_mark_node
;
1889 /* Issue an eror message about the fact that DECL appeared in a
1890 constant-expression. Returns ERROR_MARK_NODE. */
1893 cp_parser_non_constant_id_expression (tree decl
)
1895 error ("`%D' cannot appear in a constant-expression", decl
);
1896 return error_mark_node
;
1899 /* Check for a common situation where a type-name should be present,
1900 but is not, and issue a sensible error message. Returns true if an
1901 invalid type-name was detected. */
1904 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
)
1906 /* If the next two tokens are both identifiers, the code is
1907 erroneous. The usual cause of this situation is code like:
1911 where "T" should name a type -- but does not. */
1912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
1913 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
1917 /* If parsing tentatively, we should commit; we really are
1918 looking at a declaration. */
1919 /* Consume the first identifier. */
1920 name
= cp_lexer_consume_token (parser
->lexer
)->value
;
1921 /* Issue an error message. */
1922 error ("`%s' does not name a type", IDENTIFIER_POINTER (name
));
1923 /* If we're in a template class, it's possible that the user was
1924 referring to a type from a base class. For example:
1926 template <typename T> struct A { typedef T X; };
1927 template <typename T> struct B : public A<T> { X x; };
1929 The user should have said "typename A<T>::X". */
1930 if (processing_template_decl
&& current_class_type
)
1934 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1938 tree base_type
= BINFO_TYPE (b
);
1939 if (CLASS_TYPE_P (base_type
)
1940 && dependent_type_p (base_type
))
1943 /* Go from a particular instantiation of the
1944 template (which will have an empty TYPE_FIELDs),
1945 to the main version. */
1946 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1947 for (field
= TYPE_FIELDS (base_type
);
1949 field
= TREE_CHAIN (field
))
1950 if (TREE_CODE (field
) == TYPE_DECL
1951 && DECL_NAME (field
) == name
)
1953 error ("(perhaps `typename %T::%s' was intended)",
1954 BINFO_TYPE (b
), IDENTIFIER_POINTER (name
));
1962 /* Skip to the end of the declaration; there's no point in
1963 trying to process it. */
1964 cp_parser_skip_to_end_of_statement (parser
);
1972 /* Consume tokens up to, and including, the next non-nested closing `)'.
1973 Returns TRUE iff we found a closing `)'. */
1976 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
)
1978 unsigned nesting_depth
= 0;
1984 /* If we've run out of tokens, then there is no closing `)'. */
1985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
1987 /* Consume the token. */
1988 token
= cp_lexer_consume_token (parser
->lexer
);
1989 /* If it is an `(', we have entered another level of nesting. */
1990 if (token
->type
== CPP_OPEN_PAREN
)
1992 /* If it is a `)', then we might be done. */
1993 else if (token
->type
== CPP_CLOSE_PAREN
&& nesting_depth
-- == 0)
1998 /* Consume tokens until the next token is a `)', or a `,'. Returns
1999 TRUE if the next token is a `,'. */
2002 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser
*parser
)
2004 unsigned nesting_depth
= 0;
2008 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2010 /* If we've run out of tokens, then there is no closing `)'. */
2011 if (token
->type
== CPP_EOF
)
2013 /* If it is a `,' stop. */
2014 else if (token
->type
== CPP_COMMA
&& nesting_depth
-- == 0)
2016 /* If it is a `)', stop. */
2017 else if (token
->type
== CPP_CLOSE_PAREN
&& nesting_depth
-- == 0)
2019 /* If it is an `(', we have entered another level of nesting. */
2020 else if (token
->type
== CPP_OPEN_PAREN
)
2022 /* Consume the token. */
2023 token
= cp_lexer_consume_token (parser
->lexer
);
2027 /* Consume tokens until we reach the end of the current statement.
2028 Normally, that will be just before consuming a `;'. However, if a
2029 non-nested `}' comes first, then we stop before consuming that. */
2032 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2034 unsigned nesting_depth
= 0;
2040 /* Peek at the next token. */
2041 token
= cp_lexer_peek_token (parser
->lexer
);
2042 /* If we've run out of tokens, stop. */
2043 if (token
->type
== CPP_EOF
)
2045 /* If the next token is a `;', we have reached the end of the
2047 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2049 /* If the next token is a non-nested `}', then we have reached
2050 the end of the current block. */
2051 if (token
->type
== CPP_CLOSE_BRACE
)
2053 /* If this is a non-nested `}', stop before consuming it.
2054 That way, when confronted with something like:
2058 we stop before consuming the closing `}', even though we
2059 have not yet reached a `;'. */
2060 if (nesting_depth
== 0)
2062 /* If it is the closing `}' for a block that we have
2063 scanned, stop -- but only after consuming the token.
2069 we will stop after the body of the erroneously declared
2070 function, but before consuming the following `typedef'
2072 if (--nesting_depth
== 0)
2074 cp_lexer_consume_token (parser
->lexer
);
2078 /* If it the next token is a `{', then we are entering a new
2079 block. Consume the entire block. */
2080 else if (token
->type
== CPP_OPEN_BRACE
)
2082 /* Consume the token. */
2083 cp_lexer_consume_token (parser
->lexer
);
2087 /* This function is called at the end of a statement or declaration.
2088 If the next token is a semicolon, it is consumed; otherwise, error
2089 recovery is attempted. */
2092 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2094 /* Look for the trailing `;'. */
2095 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2097 /* If there is additional (erroneous) input, skip to the end of
2099 cp_parser_skip_to_end_of_statement (parser
);
2100 /* If the next token is now a `;', consume it. */
2101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2102 cp_lexer_consume_token (parser
->lexer
);
2106 /* Skip tokens until we have consumed an entire block, or until we
2107 have consumed a non-nested `;'. */
2110 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2112 unsigned nesting_depth
= 0;
2118 /* Peek at the next token. */
2119 token
= cp_lexer_peek_token (parser
->lexer
);
2120 /* If we've run out of tokens, stop. */
2121 if (token
->type
== CPP_EOF
)
2123 /* If the next token is a `;', we have reached the end of the
2125 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2127 /* Consume the `;'. */
2128 cp_lexer_consume_token (parser
->lexer
);
2131 /* Consume the token. */
2132 token
= cp_lexer_consume_token (parser
->lexer
);
2133 /* If the next token is a non-nested `}', then we have reached
2134 the end of the current block. */
2135 if (token
->type
== CPP_CLOSE_BRACE
2136 && (nesting_depth
== 0 || --nesting_depth
== 0))
2138 /* If it the next token is a `{', then we are entering a new
2139 block. Consume the entire block. */
2140 if (token
->type
== CPP_OPEN_BRACE
)
2145 /* Skip tokens until a non-nested closing curly brace is the next
2149 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2151 unsigned nesting_depth
= 0;
2157 /* Peek at the next token. */
2158 token
= cp_lexer_peek_token (parser
->lexer
);
2159 /* If we've run out of tokens, stop. */
2160 if (token
->type
== CPP_EOF
)
2162 /* If the next token is a non-nested `}', then we have reached
2163 the end of the current block. */
2164 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2166 /* If it the next token is a `{', then we are entering a new
2167 block. Consume the entire block. */
2168 else if (token
->type
== CPP_OPEN_BRACE
)
2170 /* Consume the token. */
2171 cp_lexer_consume_token (parser
->lexer
);
2175 /* Create a new C++ parser. */
2178 cp_parser_new (void)
2183 /* cp_lexer_new_main is called before calling ggc_alloc because
2184 cp_lexer_new_main might load a PCH file. */
2185 lexer
= cp_lexer_new_main ();
2187 parser
= (cp_parser
*) ggc_alloc_cleared (sizeof (cp_parser
));
2188 parser
->lexer
= lexer
;
2189 parser
->context
= cp_parser_context_new (NULL
);
2191 /* For now, we always accept GNU extensions. */
2192 parser
->allow_gnu_extensions_p
= 1;
2194 /* The `>' token is a greater-than operator, not the end of a
2196 parser
->greater_than_is_operator_p
= true;
2198 parser
->default_arg_ok_p
= true;
2200 /* We are not parsing a constant-expression. */
2201 parser
->constant_expression_p
= false;
2202 parser
->allow_non_constant_expression_p
= false;
2203 parser
->non_constant_expression_p
= false;
2205 /* Local variable names are not forbidden. */
2206 parser
->local_variables_forbidden_p
= false;
2208 /* We are not procesing an `extern "C"' declaration. */
2209 parser
->in_unbraced_linkage_specification_p
= false;
2211 /* We are not processing a declarator. */
2212 parser
->in_declarator_p
= false;
2214 /* The unparsed function queue is empty. */
2215 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2217 /* There are no classes being defined. */
2218 parser
->num_classes_being_defined
= 0;
2220 /* No template parameters apply. */
2221 parser
->num_template_parameter_lists
= 0;
2226 /* Lexical conventions [gram.lex] */
2228 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2232 cp_parser_identifier (cp_parser
* parser
)
2236 /* Look for the identifier. */
2237 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2238 /* Return the value. */
2239 return token
? token
->value
: error_mark_node
;
2242 /* Basic concepts [gram.basic] */
2244 /* Parse a translation-unit.
2247 declaration-seq [opt]
2249 Returns TRUE if all went well. */
2252 cp_parser_translation_unit (cp_parser
* parser
)
2256 cp_parser_declaration_seq_opt (parser
);
2258 /* If there are no tokens left then all went well. */
2259 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2262 /* Otherwise, issue an error message. */
2263 cp_parser_error (parser
, "expected declaration");
2267 /* Consume the EOF token. */
2268 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2271 finish_translation_unit ();
2273 /* All went well. */
2277 /* Expressions [gram.expr] */
2279 /* Parse a primary-expression.
2290 ( compound-statement )
2291 __builtin_va_arg ( assignment-expression , type-id )
2296 Returns a representation of the expression.
2298 *IDK indicates what kind of id-expression (if any) was present.
2300 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2301 used as the operand of a pointer-to-member. In that case,
2302 *QUALIFYING_CLASS gives the class that is used as the qualifying
2303 class in the pointer-to-member. */
2306 cp_parser_primary_expression (cp_parser
*parser
,
2307 cp_parser_id_kind
*idk
,
2308 tree
*qualifying_class
)
2312 /* Assume the primary expression is not an id-expression. */
2313 *idk
= CP_PARSER_ID_KIND_NONE
;
2314 /* And that it cannot be used as pointer-to-member. */
2315 *qualifying_class
= NULL_TREE
;
2317 /* Peek at the next token. */
2318 token
= cp_lexer_peek_token (parser
->lexer
);
2319 switch (token
->type
)
2332 token
= cp_lexer_consume_token (parser
->lexer
);
2333 return token
->value
;
2335 case CPP_OPEN_PAREN
:
2338 bool saved_greater_than_is_operator_p
;
2340 /* Consume the `('. */
2341 cp_lexer_consume_token (parser
->lexer
);
2342 /* Within a parenthesized expression, a `>' token is always
2343 the greater-than operator. */
2344 saved_greater_than_is_operator_p
2345 = parser
->greater_than_is_operator_p
;
2346 parser
->greater_than_is_operator_p
= true;
2347 /* If we see `( { ' then we are looking at the beginning of
2348 a GNU statement-expression. */
2349 if (cp_parser_allow_gnu_extensions_p (parser
)
2350 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2352 /* Statement-expressions are not allowed by the standard. */
2354 pedwarn ("ISO C++ forbids braced-groups within expressions");
2356 /* And they're not allowed outside of a function-body; you
2357 cannot, for example, write:
2359 int i = ({ int j = 3; j + 1; });
2361 at class or namespace scope. */
2362 if (!at_function_scope_p ())
2363 error ("statement-expressions are allowed only inside functions");
2364 /* Start the statement-expression. */
2365 expr
= begin_stmt_expr ();
2366 /* Parse the compound-statement. */
2367 cp_parser_compound_statement (parser
);
2369 expr
= finish_stmt_expr (expr
);
2373 /* Parse the parenthesized expression. */
2374 expr
= cp_parser_expression (parser
);
2375 /* Let the front end know that this expression was
2376 enclosed in parentheses. This matters in case, for
2377 example, the expression is of the form `A::B', since
2378 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2380 finish_parenthesized_expr (expr
);
2382 /* The `>' token might be the end of a template-id or
2383 template-parameter-list now. */
2384 parser
->greater_than_is_operator_p
2385 = saved_greater_than_is_operator_p
;
2386 /* Consume the `)'. */
2387 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2388 cp_parser_skip_to_end_of_statement (parser
);
2394 switch (token
->keyword
)
2396 /* These two are the boolean literals. */
2398 cp_lexer_consume_token (parser
->lexer
);
2399 return boolean_true_node
;
2401 cp_lexer_consume_token (parser
->lexer
);
2402 return boolean_false_node
;
2404 /* The `__null' literal. */
2406 cp_lexer_consume_token (parser
->lexer
);
2409 /* Recognize the `this' keyword. */
2411 cp_lexer_consume_token (parser
->lexer
);
2412 if (parser
->local_variables_forbidden_p
)
2414 error ("`this' may not be used in this context");
2415 return error_mark_node
;
2417 /* Pointers cannot appear in constant-expressions. */
2418 if (parser
->constant_expression_p
)
2420 if (!parser
->allow_non_constant_expression_p
)
2421 return cp_parser_non_constant_expression ("`this'");
2422 parser
->non_constant_expression_p
= true;
2424 return finish_this_expr ();
2426 /* The `operator' keyword can be the beginning of an
2431 case RID_FUNCTION_NAME
:
2432 case RID_PRETTY_FUNCTION_NAME
:
2433 case RID_C99_FUNCTION_NAME
:
2434 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2435 __func__ are the names of variables -- but they are
2436 treated specially. Therefore, they are handled here,
2437 rather than relying on the generic id-expression logic
2438 below. Gramatically, these names are id-expressions.
2440 Consume the token. */
2441 token
= cp_lexer_consume_token (parser
->lexer
);
2442 /* Look up the name. */
2443 return finish_fname (token
->value
);
2450 /* The `__builtin_va_arg' construct is used to handle
2451 `va_arg'. Consume the `__builtin_va_arg' token. */
2452 cp_lexer_consume_token (parser
->lexer
);
2453 /* Look for the opening `('. */
2454 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2455 /* Now, parse the assignment-expression. */
2456 expression
= cp_parser_assignment_expression (parser
);
2457 /* Look for the `,'. */
2458 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2459 /* Parse the type-id. */
2460 type
= cp_parser_type_id (parser
);
2461 /* Look for the closing `)'. */
2462 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2463 /* Using `va_arg' in a constant-expression is not
2465 if (parser
->constant_expression_p
)
2467 if (!parser
->allow_non_constant_expression_p
)
2468 return cp_parser_non_constant_expression ("`va_arg'");
2469 parser
->non_constant_expression_p
= true;
2471 return build_x_va_arg (expression
, type
);
2475 cp_parser_error (parser
, "expected primary-expression");
2476 return error_mark_node
;
2480 /* An id-expression can start with either an identifier, a
2481 `::' as the beginning of a qualified-id, or the "operator"
2485 case CPP_TEMPLATE_ID
:
2486 case CPP_NESTED_NAME_SPECIFIER
:
2492 /* Parse the id-expression. */
2494 = cp_parser_id_expression (parser
,
2495 /*template_keyword_p=*/false,
2496 /*check_dependency_p=*/true,
2497 /*template_p=*/NULL
);
2498 if (id_expression
== error_mark_node
)
2499 return error_mark_node
;
2500 /* If we have a template-id, then no further lookup is
2501 required. If the template-id was for a template-class, we
2502 will sometimes have a TYPE_DECL at this point. */
2503 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2504 || TREE_CODE (id_expression
) == TYPE_DECL
)
2505 decl
= id_expression
;
2506 /* Look up the name. */
2509 decl
= cp_parser_lookup_name_simple (parser
, id_expression
);
2510 /* If name lookup gives us a SCOPE_REF, then the
2511 qualifying scope was dependent. Just propagate the
2513 if (TREE_CODE (decl
) == SCOPE_REF
)
2515 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2516 *qualifying_class
= TREE_OPERAND (decl
, 0);
2517 /* Since this name was dependent, the expression isn't
2518 constant -- yet. No error is issued because it
2519 might be constant when things are instantiated. */
2520 if (parser
->constant_expression_p
)
2521 parser
->non_constant_expression_p
= true;
2524 /* Check to see if DECL is a local variable in a context
2525 where that is forbidden. */
2526 if (parser
->local_variables_forbidden_p
2527 && local_variable_p (decl
))
2529 /* It might be that we only found DECL because we are
2530 trying to be generous with pre-ISO scoping rules.
2531 For example, consider:
2535 for (int i = 0; i < 10; ++i) {}
2536 extern void f(int j = i);
2539 Here, name look up will originally find the out
2540 of scope `i'. We need to issue a warning message,
2541 but then use the global `i'. */
2542 decl
= check_for_out_of_scope_variable (decl
);
2543 if (local_variable_p (decl
))
2545 error ("local variable `%D' may not appear in this context",
2547 return error_mark_node
;
2551 if (decl
== error_mark_node
)
2553 /* Name lookup failed. */
2555 && processing_template_decl
)
2557 /* Unqualified name lookup failed while processing a
2559 *idk
= CP_PARSER_ID_KIND_UNQUALIFIED
;
2560 /* If the next token is a parenthesis, assume that
2561 Koenig lookup will succeed when instantiating the
2563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
2564 return build_min_nt (LOOKUP_EXPR
, id_expression
);
2565 /* If we're not doing Koenig lookup, issue an error. */
2566 error ("`%D' has not been declared", id_expression
);
2567 return error_mark_node
;
2569 else if (parser
->scope
2570 && (!TYPE_P (parser
->scope
)
2571 || !dependent_type_p (parser
->scope
)))
2573 /* Qualified name lookup failed, and the
2574 qualifying name was not a dependent type. That
2575 is always an error. */
2576 if (TYPE_P (parser
->scope
)
2577 && !COMPLETE_TYPE_P (parser
->scope
))
2578 error ("incomplete type `%T' used in nested name "
2581 else if (parser
->scope
!= global_namespace
)
2582 error ("`%D' is not a member of `%D'",
2583 id_expression
, parser
->scope
);
2585 error ("`::%D' has not been declared", id_expression
);
2586 return error_mark_node
;
2588 else if (!parser
->scope
&& !processing_template_decl
)
2590 /* It may be resolvable as a koenig lookup function
2592 *idk
= CP_PARSER_ID_KIND_UNQUALIFIED
;
2593 return id_expression
;
2596 /* If DECL is a variable that would be out of scope under
2597 ANSI/ISO rules, but in scope in the ARM, name lookup
2598 will succeed. Issue a diagnostic here. */
2600 decl
= check_for_out_of_scope_variable (decl
);
2602 /* Remember that the name was used in the definition of
2603 the current class so that we can check later to see if
2604 the meaning would have been different after the class
2605 was entirely defined. */
2606 if (!parser
->scope
&& decl
!= error_mark_node
)
2607 maybe_note_name_used_in_class (id_expression
, decl
);
2610 /* If we didn't find anything, or what we found was a type,
2611 then this wasn't really an id-expression. */
2612 if (TREE_CODE (decl
) == TEMPLATE_DECL
2613 && !DECL_FUNCTION_TEMPLATE_P (decl
))
2615 cp_parser_error (parser
, "missing template arguments");
2616 return error_mark_node
;
2618 else if (TREE_CODE (decl
) == TYPE_DECL
2619 || TREE_CODE (decl
) == NAMESPACE_DECL
)
2621 cp_parser_error (parser
,
2622 "expected primary-expression");
2623 return error_mark_node
;
2626 /* If the name resolved to a template parameter, there is no
2627 need to look it up again later. Similarly, we resolve
2628 enumeration constants to their underlying values. */
2629 if (TREE_CODE (decl
) == CONST_DECL
)
2631 *idk
= CP_PARSER_ID_KIND_NONE
;
2632 if (DECL_TEMPLATE_PARM_P (decl
) || !processing_template_decl
)
2633 return DECL_INITIAL (decl
);
2640 /* If the declaration was explicitly qualified indicate
2641 that. The semantics of `A::f(3)' are different than
2642 `f(3)' if `f' is virtual. */
2643 *idk
= (parser
->scope
2644 ? CP_PARSER_ID_KIND_QUALIFIED
2645 : (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
2646 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2647 : CP_PARSER_ID_KIND_UNQUALIFIED
));
2652 An id-expression is type-dependent if it contains an
2653 identifier that was declared with a dependent type.
2655 As an optimization, we could choose not to create a
2656 LOOKUP_EXPR for a name that resolved to a local
2657 variable in the template function that we are currently
2658 declaring; such a name cannot ever resolve to anything
2659 else. If we did that we would not have to look up
2660 these names at instantiation time.
2662 The standard is not very specific about an
2663 id-expression that names a set of overloaded functions.
2664 What if some of them have dependent types and some of
2665 them do not? Presumably, such a name should be treated
2666 as a dependent name. */
2667 /* Assume the name is not dependent. */
2668 dependent_p
= false;
2669 if (!processing_template_decl
)
2670 /* No names are dependent outside a template. */
2672 /* A template-id where the name of the template was not
2673 resolved is definitely dependent. */
2674 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
2675 && (TREE_CODE (TREE_OPERAND (decl
, 0))
2676 == IDENTIFIER_NODE
))
2678 /* For anything except an overloaded function, just check
2680 else if (!is_overloaded_fn (decl
))
2682 = dependent_type_p (TREE_TYPE (decl
));
2683 /* For a set of overloaded functions, check each of the
2689 if (BASELINK_P (fns
))
2690 fns
= BASELINK_FUNCTIONS (fns
);
2692 /* For a template-id, check to see if the template
2693 arguments are dependent. */
2694 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
2696 tree args
= TREE_OPERAND (fns
, 1);
2698 if (args
&& TREE_CODE (args
) == TREE_LIST
)
2702 if (dependent_template_arg_p (TREE_VALUE (args
)))
2707 args
= TREE_CHAIN (args
);
2710 else if (args
&& TREE_CODE (args
) == TREE_VEC
)
2713 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
2714 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
2721 /* The functions are those referred to by the
2723 fns
= TREE_OPERAND (fns
, 0);
2726 /* If there are no dependent template arguments, go
2727 through the overlaoded functions. */
2728 while (fns
&& !dependent_p
)
2730 tree fn
= OVL_CURRENT (fns
);
2732 /* Member functions of dependent classes are
2734 if (TREE_CODE (fn
) == FUNCTION_DECL
2735 && type_dependent_expression_p (fn
))
2737 else if (TREE_CODE (fn
) == TEMPLATE_DECL
2738 && dependent_template_p (fn
))
2741 fns
= OVL_NEXT (fns
);
2745 /* If the name was dependent on a template parameter,
2746 we will resolve the name at instantiation time. */
2749 /* Create a SCOPE_REF for qualified names. */
2752 if (TYPE_P (parser
->scope
))
2753 *qualifying_class
= parser
->scope
;
2754 /* Since this name was dependent, the expression isn't
2755 constant -- yet. No error is issued because it
2756 might be constant when things are instantiated. */
2757 if (parser
->constant_expression_p
)
2758 parser
->non_constant_expression_p
= true;
2759 return build_nt (SCOPE_REF
,
2763 /* A TEMPLATE_ID already contains all the information
2765 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
)
2766 return id_expression
;
2767 /* Since this name was dependent, the expression isn't
2768 constant -- yet. No error is issued because it
2769 might be constant when things are instantiated. */
2770 if (parser
->constant_expression_p
)
2771 parser
->non_constant_expression_p
= true;
2772 /* Create a LOOKUP_EXPR for other unqualified names. */
2773 return build_min_nt (LOOKUP_EXPR
, id_expression
);
2776 /* Only certain kinds of names are allowed in constant
2777 expression. Enumerators have already been handled
2779 if (parser
->constant_expression_p
2780 /* Non-type template parameters of integral or
2781 enumeration type. */
2782 && !(TREE_CODE (decl
) == TEMPLATE_PARM_INDEX
2783 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl
)))
2784 /* Const variables or static data members of integral
2785 or enumeration types initialized with constant
2787 && !(TREE_CODE (decl
) == VAR_DECL
2788 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl
))
2789 && DECL_INITIAL (decl
)
2790 && TREE_CONSTANT (DECL_INITIAL (decl
))))
2792 if (!parser
->allow_non_constant_expression_p
)
2793 return cp_parser_non_constant_id_expression (decl
);
2794 parser
->non_constant_expression_p
= true;
2799 decl
= (adjust_result_of_qualified_name_lookup
2800 (decl
, parser
->scope
, current_class_type
));
2801 if (TREE_CODE (decl
) == FIELD_DECL
|| BASELINK_P (decl
))
2802 *qualifying_class
= parser
->scope
;
2803 else if (!processing_template_decl
)
2804 decl
= convert_from_reference (decl
);
2807 /* Transform references to non-static data members into
2809 decl
= hack_identifier (decl
, id_expression
);
2811 /* Resolve references to variables of anonymous unions
2812 into COMPONENT_REFs. */
2813 if (TREE_CODE (decl
) == ALIAS_DECL
)
2814 decl
= DECL_INITIAL (decl
);
2817 if (TREE_DEPRECATED (decl
))
2818 warn_deprecated_use (decl
);
2823 /* Anything else is an error. */
2825 cp_parser_error (parser
, "expected primary-expression");
2826 return error_mark_node
;
2830 /* Parse an id-expression.
2837 :: [opt] nested-name-specifier template [opt] unqualified-id
2839 :: operator-function-id
2842 Return a representation of the unqualified portion of the
2843 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2844 a `::' or nested-name-specifier.
2846 Often, if the id-expression was a qualified-id, the caller will
2847 want to make a SCOPE_REF to represent the qualified-id. This
2848 function does not do this in order to avoid wastefully creating
2849 SCOPE_REFs when they are not required.
2851 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2854 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2855 uninstantiated templates.
2857 If *TEMPLATE_P is non-NULL, it is set to true iff the
2858 `template' keyword is used to explicitly indicate that the entity
2859 named is a template. */
2862 cp_parser_id_expression (cp_parser
*parser
,
2863 bool template_keyword_p
,
2864 bool check_dependency_p
,
2867 bool global_scope_p
;
2868 bool nested_name_specifier_p
;
2870 /* Assume the `template' keyword was not used. */
2872 *template_p
= false;
2874 /* Look for the optional `::' operator. */
2876 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2878 /* Look for the optional nested-name-specifier. */
2879 nested_name_specifier_p
2880 = (cp_parser_nested_name_specifier_opt (parser
,
2881 /*typename_keyword_p=*/false,
2885 /* If there is a nested-name-specifier, then we are looking at
2886 the first qualified-id production. */
2887 if (nested_name_specifier_p
)
2890 tree saved_object_scope
;
2891 tree saved_qualifying_scope
;
2892 tree unqualified_id
;
2895 /* See if the next token is the `template' keyword. */
2897 template_p
= &is_template
;
2898 *template_p
= cp_parser_optional_template_keyword (parser
);
2899 /* Name lookup we do during the processing of the
2900 unqualified-id might obliterate SCOPE. */
2901 saved_scope
= parser
->scope
;
2902 saved_object_scope
= parser
->object_scope
;
2903 saved_qualifying_scope
= parser
->qualifying_scope
;
2904 /* Process the final unqualified-id. */
2905 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2906 check_dependency_p
);
2907 /* Restore the SAVED_SCOPE for our caller. */
2908 parser
->scope
= saved_scope
;
2909 parser
->object_scope
= saved_object_scope
;
2910 parser
->qualifying_scope
= saved_qualifying_scope
;
2912 return unqualified_id
;
2914 /* Otherwise, if we are in global scope, then we are looking at one
2915 of the other qualified-id productions. */
2916 else if (global_scope_p
)
2921 /* Peek at the next token. */
2922 token
= cp_lexer_peek_token (parser
->lexer
);
2924 /* If it's an identifier, and the next token is not a "<", then
2925 we can avoid the template-id case. This is an optimization
2926 for this common case. */
2927 if (token
->type
== CPP_NAME
2928 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
2929 return cp_parser_identifier (parser
);
2931 cp_parser_parse_tentatively (parser
);
2932 /* Try a template-id. */
2933 id
= cp_parser_template_id (parser
,
2934 /*template_keyword_p=*/false,
2935 /*check_dependency_p=*/true);
2936 /* If that worked, we're done. */
2937 if (cp_parser_parse_definitely (parser
))
2940 /* Peek at the next token. (Changes in the token buffer may
2941 have invalidated the pointer obtained above.) */
2942 token
= cp_lexer_peek_token (parser
->lexer
);
2944 switch (token
->type
)
2947 return cp_parser_identifier (parser
);
2950 if (token
->keyword
== RID_OPERATOR
)
2951 return cp_parser_operator_function_id (parser
);
2955 cp_parser_error (parser
, "expected id-expression");
2956 return error_mark_node
;
2960 return cp_parser_unqualified_id (parser
, template_keyword_p
,
2961 /*check_dependency_p=*/true);
2964 /* Parse an unqualified-id.
2968 operator-function-id
2969 conversion-function-id
2973 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2974 keyword, in a construct like `A::template ...'.
2976 Returns a representation of unqualified-id. For the `identifier'
2977 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2978 production a BIT_NOT_EXPR is returned; the operand of the
2979 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2980 other productions, see the documentation accompanying the
2981 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2982 names are looked up in uninstantiated templates. */
2985 cp_parser_unqualified_id (cp_parser
* parser
,
2986 bool template_keyword_p
,
2987 bool check_dependency_p
)
2991 /* Peek at the next token. */
2992 token
= cp_lexer_peek_token (parser
->lexer
);
2994 switch (token
->type
)
3000 /* We don't know yet whether or not this will be a
3002 cp_parser_parse_tentatively (parser
);
3003 /* Try a template-id. */
3004 id
= cp_parser_template_id (parser
, template_keyword_p
,
3005 check_dependency_p
);
3006 /* If it worked, we're done. */
3007 if (cp_parser_parse_definitely (parser
))
3009 /* Otherwise, it's an ordinary identifier. */
3010 return cp_parser_identifier (parser
);
3013 case CPP_TEMPLATE_ID
:
3014 return cp_parser_template_id (parser
, template_keyword_p
,
3015 check_dependency_p
);
3020 tree qualifying_scope
;
3024 /* Consume the `~' token. */
3025 cp_lexer_consume_token (parser
->lexer
);
3026 /* Parse the class-name. The standard, as written, seems to
3029 template <typename T> struct S { ~S (); };
3030 template <typename T> S<T>::~S() {}
3032 is invalid, since `~' must be followed by a class-name, but
3033 `S<T>' is dependent, and so not known to be a class.
3034 That's not right; we need to look in uninstantiated
3035 templates. A further complication arises from:
3037 template <typename T> void f(T t) {
3041 Here, it is not possible to look up `T' in the scope of `T'
3042 itself. We must look in both the current scope, and the
3043 scope of the containing complete expression.
3045 Yet another issue is:
3054 The standard does not seem to say that the `S' in `~S'
3055 should refer to the type `S' and not the data member
3058 /* DR 244 says that we look up the name after the "~" in the
3059 same scope as we looked up the qualifying name. That idea
3060 isn't fully worked out; it's more complicated than that. */
3061 scope
= parser
->scope
;
3062 object_scope
= parser
->object_scope
;
3063 qualifying_scope
= parser
->qualifying_scope
;
3065 /* If the name is of the form "X::~X" it's OK. */
3066 if (scope
&& TYPE_P (scope
)
3067 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3068 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3070 && (cp_lexer_peek_token (parser
->lexer
)->value
3071 == TYPE_IDENTIFIER (scope
)))
3073 cp_lexer_consume_token (parser
->lexer
);
3074 return build_nt (BIT_NOT_EXPR
, scope
);
3077 /* If there was an explicit qualification (S::~T), first look
3078 in the scope given by the qualification (i.e., S). */
3081 cp_parser_parse_tentatively (parser
);
3082 type_decl
= cp_parser_class_name (parser
,
3083 /*typename_keyword_p=*/false,
3084 /*template_keyword_p=*/false,
3086 /*check_dependency=*/false,
3087 /*class_head_p=*/false);
3088 if (cp_parser_parse_definitely (parser
))
3089 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3091 /* In "N::S::~S", look in "N" as well. */
3092 if (scope
&& qualifying_scope
)
3094 cp_parser_parse_tentatively (parser
);
3095 parser
->scope
= qualifying_scope
;
3096 parser
->object_scope
= NULL_TREE
;
3097 parser
->qualifying_scope
= NULL_TREE
;
3099 = cp_parser_class_name (parser
,
3100 /*typename_keyword_p=*/false,
3101 /*template_keyword_p=*/false,
3103 /*check_dependency=*/false,
3104 /*class_head_p=*/false);
3105 if (cp_parser_parse_definitely (parser
))
3106 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3108 /* In "p->S::~T", look in the scope given by "*p" as well. */
3109 else if (object_scope
)
3111 cp_parser_parse_tentatively (parser
);
3112 parser
->scope
= object_scope
;
3113 parser
->object_scope
= NULL_TREE
;
3114 parser
->qualifying_scope
= NULL_TREE
;
3116 = cp_parser_class_name (parser
,
3117 /*typename_keyword_p=*/false,
3118 /*template_keyword_p=*/false,
3120 /*check_dependency=*/false,
3121 /*class_head_p=*/false);
3122 if (cp_parser_parse_definitely (parser
))
3123 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3125 /* Look in the surrounding context. */
3126 parser
->scope
= NULL_TREE
;
3127 parser
->object_scope
= NULL_TREE
;
3128 parser
->qualifying_scope
= NULL_TREE
;
3130 = cp_parser_class_name (parser
,
3131 /*typename_keyword_p=*/false,
3132 /*template_keyword_p=*/false,
3134 /*check_dependency=*/false,
3135 /*class_head_p=*/false);
3136 /* If an error occurred, assume that the name of the
3137 destructor is the same as the name of the qualifying
3138 class. That allows us to keep parsing after running
3139 into ill-formed destructor names. */
3140 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3141 return build_nt (BIT_NOT_EXPR
, scope
);
3142 else if (type_decl
== error_mark_node
)
3143 return error_mark_node
;
3145 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3149 if (token
->keyword
== RID_OPERATOR
)
3153 /* This could be a template-id, so we try that first. */
3154 cp_parser_parse_tentatively (parser
);
3155 /* Try a template-id. */
3156 id
= cp_parser_template_id (parser
, template_keyword_p
,
3157 /*check_dependency_p=*/true);
3158 /* If that worked, we're done. */
3159 if (cp_parser_parse_definitely (parser
))
3161 /* We still don't know whether we're looking at an
3162 operator-function-id or a conversion-function-id. */
3163 cp_parser_parse_tentatively (parser
);
3164 /* Try an operator-function-id. */
3165 id
= cp_parser_operator_function_id (parser
);
3166 /* If that didn't work, try a conversion-function-id. */
3167 if (!cp_parser_parse_definitely (parser
))
3168 id
= cp_parser_conversion_function_id (parser
);
3175 cp_parser_error (parser
, "expected unqualified-id");
3176 return error_mark_node
;
3180 /* Parse an (optional) nested-name-specifier.
3182 nested-name-specifier:
3183 class-or-namespace-name :: nested-name-specifier [opt]
3184 class-or-namespace-name :: template nested-name-specifier [opt]
3186 PARSER->SCOPE should be set appropriately before this function is
3187 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3188 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3191 Sets PARSER->SCOPE to the class (TYPE) or namespace
3192 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3193 it unchanged if there is no nested-name-specifier. Returns the new
3194 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3197 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3198 bool typename_keyword_p
,
3199 bool check_dependency_p
,
3202 bool success
= false;
3203 tree access_check
= NULL_TREE
;
3207 /* If the next token corresponds to a nested name specifier, there
3208 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3209 false, it may have been true before, in which case something
3210 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3211 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3212 CHECK_DEPENDENCY_P is false, we have to fall through into the
3214 if (check_dependency_p
3215 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3217 cp_parser_pre_parsed_nested_name_specifier (parser
);
3218 return parser
->scope
;
3221 /* Remember where the nested-name-specifier starts. */
3222 if (cp_parser_parsing_tentatively (parser
)
3223 && !cp_parser_committed_to_tentative_parse (parser
))
3225 token
= cp_lexer_peek_token (parser
->lexer
);
3226 start
= cp_lexer_token_difference (parser
->lexer
,
3227 parser
->lexer
->first_token
,
3233 push_deferring_access_checks (dk_deferred
);
3239 tree saved_qualifying_scope
;
3240 bool template_keyword_p
;
3242 /* Spot cases that cannot be the beginning of a
3243 nested-name-specifier. */
3244 token
= cp_lexer_peek_token (parser
->lexer
);
3246 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3247 the already parsed nested-name-specifier. */
3248 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3250 /* Grab the nested-name-specifier and continue the loop. */
3251 cp_parser_pre_parsed_nested_name_specifier (parser
);
3256 /* Spot cases that cannot be the beginning of a
3257 nested-name-specifier. On the second and subsequent times
3258 through the loop, we look for the `template' keyword. */
3259 if (success
&& token
->keyword
== RID_TEMPLATE
)
3261 /* A template-id can start a nested-name-specifier. */
3262 else if (token
->type
== CPP_TEMPLATE_ID
)
3266 /* If the next token is not an identifier, then it is
3267 definitely not a class-or-namespace-name. */
3268 if (token
->type
!= CPP_NAME
)
3270 /* If the following token is neither a `<' (to begin a
3271 template-id), nor a `::', then we are not looking at a
3272 nested-name-specifier. */
3273 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3274 if (token
->type
!= CPP_LESS
&& token
->type
!= CPP_SCOPE
)
3278 /* The nested-name-specifier is optional, so we parse
3280 cp_parser_parse_tentatively (parser
);
3282 /* Look for the optional `template' keyword, if this isn't the
3283 first time through the loop. */
3285 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3287 template_keyword_p
= false;
3289 /* Save the old scope since the name lookup we are about to do
3290 might destroy it. */
3291 old_scope
= parser
->scope
;
3292 saved_qualifying_scope
= parser
->qualifying_scope
;
3293 /* Parse the qualifying entity. */
3295 = cp_parser_class_or_namespace_name (parser
,
3300 /* Look for the `::' token. */
3301 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3303 /* If we found what we wanted, we keep going; otherwise, we're
3305 if (!cp_parser_parse_definitely (parser
))
3307 bool error_p
= false;
3309 /* Restore the OLD_SCOPE since it was valid before the
3310 failed attempt at finding the last
3311 class-or-namespace-name. */
3312 parser
->scope
= old_scope
;
3313 parser
->qualifying_scope
= saved_qualifying_scope
;
3314 /* If the next token is an identifier, and the one after
3315 that is a `::', then any valid interpretation would have
3316 found a class-or-namespace-name. */
3317 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3318 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3320 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3323 token
= cp_lexer_consume_token (parser
->lexer
);
3328 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3329 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3330 error ("`%D' used without template parameters",
3332 else if (parser
->scope
)
3334 if (TYPE_P (parser
->scope
))
3335 error ("`%T::%D' is not a class-name or "
3337 parser
->scope
, token
->value
);
3339 error ("`%D::%D' is not a class-name or "
3341 parser
->scope
, token
->value
);
3344 error ("`%D' is not a class-name or namespace-name",
3346 parser
->scope
= NULL_TREE
;
3348 /* Treat this as a successful nested-name-specifier
3353 If the name found is not a class-name (clause
3354 _class_) or namespace-name (_namespace.def_), the
3355 program is ill-formed. */
3358 cp_lexer_consume_token (parser
->lexer
);
3363 /* We've found one valid nested-name-specifier. */
3365 /* Make sure we look in the right scope the next time through
3367 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3368 ? TREE_TYPE (new_scope
)
3370 /* If it is a class scope, try to complete it; we are about to
3371 be looking up names inside the class. */
3372 if (TYPE_P (parser
->scope
)
3373 /* Since checking types for dependency can be expensive,
3374 avoid doing it if the type is already complete. */
3375 && !COMPLETE_TYPE_P (parser
->scope
)
3376 /* Do not try to complete dependent types. */
3377 && !dependent_type_p (parser
->scope
))
3378 complete_type (parser
->scope
);
3381 /* Retrieve any deferred checks. Do not pop this access checks yet
3382 so the memory will not be reclaimed during token replacing below. */
3383 access_check
= get_deferred_access_checks ();
3385 /* If parsing tentatively, replace the sequence of tokens that makes
3386 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3387 token. That way, should we re-parse the token stream, we will
3388 not have to repeat the effort required to do the parse, nor will
3389 we issue duplicate error messages. */
3390 if (success
&& start
>= 0)
3392 /* Find the token that corresponds to the start of the
3394 token
= cp_lexer_advance_token (parser
->lexer
,
3395 parser
->lexer
->first_token
,
3398 /* Reset the contents of the START token. */
3399 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3400 token
->value
= build_tree_list (access_check
, parser
->scope
);
3401 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3402 token
->keyword
= RID_MAX
;
3403 /* Purge all subsequent tokens. */
3404 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3407 pop_deferring_access_checks ();
3408 return success
? parser
->scope
: NULL_TREE
;
3411 /* Parse a nested-name-specifier. See
3412 cp_parser_nested_name_specifier_opt for details. This function
3413 behaves identically, except that it will an issue an error if no
3414 nested-name-specifier is present, and it will return
3415 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3419 cp_parser_nested_name_specifier (cp_parser
*parser
,
3420 bool typename_keyword_p
,
3421 bool check_dependency_p
,
3426 /* Look for the nested-name-specifier. */
3427 scope
= cp_parser_nested_name_specifier_opt (parser
,
3431 /* If it was not present, issue an error message. */
3434 cp_parser_error (parser
, "expected nested-name-specifier");
3435 return error_mark_node
;
3441 /* Parse a class-or-namespace-name.
3443 class-or-namespace-name:
3447 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3448 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3449 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3450 TYPE_P is TRUE iff the next name should be taken as a class-name,
3451 even the same name is declared to be another entity in the same
3454 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3455 specified by the class-or-namespace-name. If neither is found the
3456 ERROR_MARK_NODE is returned. */
3459 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3460 bool typename_keyword_p
,
3461 bool template_keyword_p
,
3462 bool check_dependency_p
,
3466 tree saved_qualifying_scope
;
3467 tree saved_object_scope
;
3471 /* Before we try to parse the class-name, we must save away the
3472 current PARSER->SCOPE since cp_parser_class_name will destroy
3474 saved_scope
= parser
->scope
;
3475 saved_qualifying_scope
= parser
->qualifying_scope
;
3476 saved_object_scope
= parser
->object_scope
;
3477 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3478 there is no need to look for a namespace-name. */
3479 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3481 cp_parser_parse_tentatively (parser
);
3482 scope
= cp_parser_class_name (parser
,
3487 /*class_head_p=*/false);
3488 /* If that didn't work, try for a namespace-name. */
3489 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3491 /* Restore the saved scope. */
3492 parser
->scope
= saved_scope
;
3493 parser
->qualifying_scope
= saved_qualifying_scope
;
3494 parser
->object_scope
= saved_object_scope
;
3495 /* If we are not looking at an identifier followed by the scope
3496 resolution operator, then this is not part of a
3497 nested-name-specifier. (Note that this function is only used
3498 to parse the components of a nested-name-specifier.) */
3499 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3500 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3501 return error_mark_node
;
3502 scope
= cp_parser_namespace_name (parser
);
3508 /* Parse a postfix-expression.
3512 postfix-expression [ expression ]
3513 postfix-expression ( expression-list [opt] )
3514 simple-type-specifier ( expression-list [opt] )
3515 typename :: [opt] nested-name-specifier identifier
3516 ( expression-list [opt] )
3517 typename :: [opt] nested-name-specifier template [opt] template-id
3518 ( expression-list [opt] )
3519 postfix-expression . template [opt] id-expression
3520 postfix-expression -> template [opt] id-expression
3521 postfix-expression . pseudo-destructor-name
3522 postfix-expression -> pseudo-destructor-name
3523 postfix-expression ++
3524 postfix-expression --
3525 dynamic_cast < type-id > ( expression )
3526 static_cast < type-id > ( expression )
3527 reinterpret_cast < type-id > ( expression )
3528 const_cast < type-id > ( expression )
3529 typeid ( expression )
3535 ( type-id ) { initializer-list , [opt] }
3537 This extension is a GNU version of the C99 compound-literal
3538 construct. (The C99 grammar uses `type-name' instead of `type-id',
3539 but they are essentially the same concept.)
3541 If ADDRESS_P is true, the postfix expression is the operand of the
3544 Returns a representation of the expression. */
3547 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3551 cp_parser_id_kind idk
= CP_PARSER_ID_KIND_NONE
;
3552 tree postfix_expression
= NULL_TREE
;
3553 /* Non-NULL only if the current postfix-expression can be used to
3554 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3555 class used to qualify the member. */
3556 tree qualifying_class
= NULL_TREE
;
3559 /* Peek at the next token. */
3560 token
= cp_lexer_peek_token (parser
->lexer
);
3561 /* Some of the productions are determined by keywords. */
3562 keyword
= token
->keyword
;
3572 const char *saved_message
;
3574 /* All of these can be handled in the same way from the point
3575 of view of parsing. Begin by consuming the token
3576 identifying the cast. */
3577 cp_lexer_consume_token (parser
->lexer
);
3579 /* New types cannot be defined in the cast. */
3580 saved_message
= parser
->type_definition_forbidden_message
;
3581 parser
->type_definition_forbidden_message
3582 = "types may not be defined in casts";
3584 /* Look for the opening `<'. */
3585 cp_parser_require (parser
, CPP_LESS
, "`<'");
3586 /* Parse the type to which we are casting. */
3587 type
= cp_parser_type_id (parser
);
3588 /* Look for the closing `>'. */
3589 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3590 /* Restore the old message. */
3591 parser
->type_definition_forbidden_message
= saved_message
;
3593 /* And the expression which is being cast. */
3594 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3595 expression
= cp_parser_expression (parser
);
3596 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3598 /* Only type conversions to integral or enumeration types
3599 can be used in constant-expressions. */
3600 if (parser
->constant_expression_p
3601 && !dependent_type_p (type
)
3602 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3604 if (!parser
->allow_non_constant_expression_p
)
3605 return (cp_parser_non_constant_expression
3606 ("a cast to a type other than an integral or "
3607 "enumeration type"));
3608 parser
->non_constant_expression_p
= true;
3615 = build_dynamic_cast (type
, expression
);
3619 = build_static_cast (type
, expression
);
3623 = build_reinterpret_cast (type
, expression
);
3627 = build_const_cast (type
, expression
);
3638 const char *saved_message
;
3640 /* Consume the `typeid' token. */
3641 cp_lexer_consume_token (parser
->lexer
);
3642 /* Look for the `(' token. */
3643 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3644 /* Types cannot be defined in a `typeid' expression. */
3645 saved_message
= parser
->type_definition_forbidden_message
;
3646 parser
->type_definition_forbidden_message
3647 = "types may not be defined in a `typeid\' expression";
3648 /* We can't be sure yet whether we're looking at a type-id or an
3650 cp_parser_parse_tentatively (parser
);
3651 /* Try a type-id first. */
3652 type
= cp_parser_type_id (parser
);
3653 /* Look for the `)' token. Otherwise, we can't be sure that
3654 we're not looking at an expression: consider `typeid (int
3655 (3))', for example. */
3656 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3657 /* If all went well, simply lookup the type-id. */
3658 if (cp_parser_parse_definitely (parser
))
3659 postfix_expression
= get_typeid (type
);
3660 /* Otherwise, fall back to the expression variant. */
3665 /* Look for an expression. */
3666 expression
= cp_parser_expression (parser
);
3667 /* Compute its typeid. */
3668 postfix_expression
= build_typeid (expression
);
3669 /* Look for the `)' token. */
3670 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3673 /* Restore the saved message. */
3674 parser
->type_definition_forbidden_message
= saved_message
;
3680 bool template_p
= false;
3684 /* Consume the `typename' token. */
3685 cp_lexer_consume_token (parser
->lexer
);
3686 /* Look for the optional `::' operator. */
3687 cp_parser_global_scope_opt (parser
,
3688 /*current_scope_valid_p=*/false);
3689 /* Look for the nested-name-specifier. */
3690 cp_parser_nested_name_specifier (parser
,
3691 /*typename_keyword_p=*/true,
3692 /*check_dependency_p=*/true,
3694 /* Look for the optional `template' keyword. */
3695 template_p
= cp_parser_optional_template_keyword (parser
);
3696 /* We don't know whether we're looking at a template-id or an
3698 cp_parser_parse_tentatively (parser
);
3699 /* Try a template-id. */
3700 id
= cp_parser_template_id (parser
, template_p
,
3701 /*check_dependency_p=*/true);
3702 /* If that didn't work, try an identifier. */
3703 if (!cp_parser_parse_definitely (parser
))
3704 id
= cp_parser_identifier (parser
);
3705 /* Create a TYPENAME_TYPE to represent the type to which the
3706 functional cast is being performed. */
3707 type
= make_typename_type (parser
->scope
, id
,
3710 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3718 /* If the next thing is a simple-type-specifier, we may be
3719 looking at a functional cast. We could also be looking at
3720 an id-expression. So, we try the functional cast, and if
3721 that doesn't work we fall back to the primary-expression. */
3722 cp_parser_parse_tentatively (parser
);
3723 /* Look for the simple-type-specifier. */
3724 type
= cp_parser_simple_type_specifier (parser
,
3725 CP_PARSER_FLAGS_NONE
);
3726 /* Parse the cast itself. */
3727 if (!cp_parser_error_occurred (parser
))
3729 = cp_parser_functional_cast (parser
, type
);
3730 /* If that worked, we're done. */
3731 if (cp_parser_parse_definitely (parser
))
3734 /* If the functional-cast didn't work out, try a
3735 compound-literal. */
3736 if (cp_parser_allow_gnu_extensions_p (parser
)
3737 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3739 tree initializer_list
= NULL_TREE
;
3741 cp_parser_parse_tentatively (parser
);
3742 /* Consume the `('. */
3743 cp_lexer_consume_token (parser
->lexer
);
3744 /* Parse the type. */
3745 type
= cp_parser_type_id (parser
);
3746 /* Look for the `)'. */
3747 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3748 /* Look for the `{'. */
3749 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3750 /* If things aren't going well, there's no need to
3752 if (!cp_parser_error_occurred (parser
))
3754 /* Parse the initializer-list. */
3756 = cp_parser_initializer_list (parser
);
3757 /* Allow a trailing `,'. */
3758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3759 cp_lexer_consume_token (parser
->lexer
);
3760 /* Look for the final `}'. */
3761 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3763 /* If that worked, we're definitely looking at a
3764 compound-literal expression. */
3765 if (cp_parser_parse_definitely (parser
))
3767 /* Warn the user that a compound literal is not
3768 allowed in standard C++. */
3770 pedwarn ("ISO C++ forbids compound-literals");
3771 /* Form the representation of the compound-literal. */
3773 = finish_compound_literal (type
, initializer_list
);
3778 /* It must be a primary-expression. */
3779 postfix_expression
= cp_parser_primary_expression (parser
,
3786 /* Peek at the next token. */
3787 token
= cp_lexer_peek_token (parser
->lexer
);
3788 done
= (token
->type
!= CPP_OPEN_SQUARE
3789 && token
->type
!= CPP_OPEN_PAREN
3790 && token
->type
!= CPP_DOT
3791 && token
->type
!= CPP_DEREF
3792 && token
->type
!= CPP_PLUS_PLUS
3793 && token
->type
!= CPP_MINUS_MINUS
);
3795 /* If the postfix expression is complete, finish up. */
3796 if (address_p
&& qualifying_class
&& done
)
3798 if (TREE_CODE (postfix_expression
) == SCOPE_REF
)
3799 postfix_expression
= TREE_OPERAND (postfix_expression
, 1);
3801 = build_offset_ref (qualifying_class
, postfix_expression
);
3802 return postfix_expression
;
3805 /* Otherwise, if we were avoiding committing until we knew
3806 whether or not we had a pointer-to-member, we now know that
3807 the expression is an ordinary reference to a qualified name. */
3808 if (qualifying_class
)
3810 if (TREE_CODE (postfix_expression
) == FIELD_DECL
)
3812 = finish_non_static_data_member (postfix_expression
,
3814 else if (BASELINK_P (postfix_expression
)
3815 && !processing_template_decl
)
3820 /* See if any of the functions are non-static members. */
3821 fns
= BASELINK_FUNCTIONS (postfix_expression
);
3822 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
3823 fns
= TREE_OPERAND (fns
, 0);
3824 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
3825 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
3827 /* If so, the expression may be relative to the current
3829 if (fn
&& current_class_type
3830 && DERIVED_FROM_P (qualifying_class
, current_class_type
))
3832 = (build_class_member_access_expr
3833 (maybe_dummy_object (qualifying_class
, NULL
),
3835 BASELINK_ACCESS_BINFO (postfix_expression
),
3836 /*preserve_reference=*/false));
3838 return build_offset_ref (qualifying_class
,
3839 postfix_expression
);
3843 /* Remember that there was a reference to this entity. */
3844 if (DECL_P (postfix_expression
))
3845 mark_used (postfix_expression
);
3847 /* Keep looping until the postfix-expression is complete. */
3850 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3851 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3853 /* It is not a Koenig lookup function call. */
3854 unqualified_name_lookup_error (postfix_expression
);
3855 postfix_expression
= error_mark_node
;
3858 /* Peek at the next token. */
3859 token
= cp_lexer_peek_token (parser
->lexer
);
3861 switch (token
->type
)
3863 case CPP_OPEN_SQUARE
:
3864 /* postfix-expression [ expression ] */
3868 /* Consume the `[' token. */
3869 cp_lexer_consume_token (parser
->lexer
);
3870 /* Parse the index expression. */
3871 index
= cp_parser_expression (parser
);
3872 /* Look for the closing `]'. */
3873 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
3875 /* Build the ARRAY_REF. */
3877 = grok_array_decl (postfix_expression
, index
);
3878 idk
= CP_PARSER_ID_KIND_NONE
;
3882 case CPP_OPEN_PAREN
:
3883 /* postfix-expression ( expression-list [opt] ) */
3887 /* Consume the `(' token. */
3888 cp_lexer_consume_token (parser
->lexer
);
3889 /* If the next token is not a `)', then there are some
3891 if (cp_lexer_next_token_is_not (parser
->lexer
,
3893 args
= cp_parser_expression_list (parser
);
3896 /* Look for the closing `)'. */
3897 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3898 /* Function calls are not permitted in
3899 constant-expressions. */
3900 if (parser
->constant_expression_p
)
3902 if (!parser
->allow_non_constant_expression_p
)
3903 return cp_parser_non_constant_expression ("a function call");
3904 parser
->non_constant_expression_p
= true;
3907 if (idk
== CP_PARSER_ID_KIND_UNQUALIFIED
3908 && (is_overloaded_fn (postfix_expression
)
3909 || DECL_P (postfix_expression
)
3910 || TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3914 tree identifier
= NULL_TREE
;
3915 tree functions
= NULL_TREE
;
3917 /* Find the name of the overloaded function. */
3918 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3919 identifier
= postfix_expression
;
3920 else if (is_overloaded_fn (postfix_expression
))
3922 functions
= postfix_expression
;
3923 identifier
= DECL_NAME (get_first_fn (functions
));
3925 else if (DECL_P (postfix_expression
))
3927 functions
= postfix_expression
;
3928 identifier
= DECL_NAME (postfix_expression
);
3931 /* A call to a namespace-scope function using an
3934 Do Koenig lookup -- unless any of the arguments are
3936 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
3937 if (type_dependent_expression_p (TREE_VALUE (arg
)))
3942 = lookup_arg_dependent(identifier
, functions
, args
);
3943 if (!postfix_expression
)
3945 /* The unqualified name could not be resolved. */
3946 unqualified_name_lookup_error (identifier
);
3947 postfix_expression
= error_mark_node
;
3950 = build_call_from_tree (postfix_expression
, args
,
3951 /*diallow_virtual=*/false);
3954 postfix_expression
= build_min_nt (LOOKUP_EXPR
,
3957 else if (idk
== CP_PARSER_ID_KIND_UNQUALIFIED
3958 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3960 /* The unqualified name could not be resolved. */
3961 unqualified_name_lookup_error (postfix_expression
);
3962 postfix_expression
= error_mark_node
;
3966 /* In the body of a template, no further processing is
3968 if (processing_template_decl
)
3970 postfix_expression
= build_nt (CALL_EXPR
,
3976 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
3978 = (build_new_method_call
3979 (TREE_OPERAND (postfix_expression
, 0),
3980 TREE_OPERAND (postfix_expression
, 1),
3982 (idk
== CP_PARSER_ID_KIND_QUALIFIED
3983 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3984 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
)
3985 postfix_expression
= (build_offset_ref_call_from_tree
3986 (postfix_expression
, args
));
3987 else if (idk
== CP_PARSER_ID_KIND_QUALIFIED
)
3988 /* A call to a static class member, or a namespace-scope
3991 = finish_call_expr (postfix_expression
, args
,
3992 /*disallow_virtual=*/true);
3994 /* All other function calls. */
3996 = finish_call_expr (postfix_expression
, args
,
3997 /*disallow_virtual=*/false);
3999 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4000 idk
= CP_PARSER_ID_KIND_NONE
;
4006 /* postfix-expression . template [opt] id-expression
4007 postfix-expression . pseudo-destructor-name
4008 postfix-expression -> template [opt] id-expression
4009 postfix-expression -> pseudo-destructor-name */
4014 tree scope
= NULL_TREE
;
4016 /* If this is a `->' operator, dereference the pointer. */
4017 if (token
->type
== CPP_DEREF
)
4018 postfix_expression
= build_x_arrow (postfix_expression
);
4019 /* Check to see whether or not the expression is
4021 dependent_p
= type_dependent_expression_p (postfix_expression
);
4022 /* The identifier following the `->' or `.' is not
4024 parser
->scope
= NULL_TREE
;
4025 parser
->qualifying_scope
= NULL_TREE
;
4026 parser
->object_scope
= NULL_TREE
;
4027 idk
= CP_PARSER_ID_KIND_NONE
;
4028 /* Enter the scope corresponding to the type of the object
4029 given by the POSTFIX_EXPRESSION. */
4031 && TREE_TYPE (postfix_expression
) != NULL_TREE
)
4033 scope
= TREE_TYPE (postfix_expression
);
4034 /* According to the standard, no expression should
4035 ever have reference type. Unfortunately, we do not
4036 currently match the standard in this respect in
4037 that our internal representation of an expression
4038 may have reference type even when the standard says
4039 it does not. Therefore, we have to manually obtain
4040 the underlying type here. */
4041 if (TREE_CODE (scope
) == REFERENCE_TYPE
)
4042 scope
= TREE_TYPE (scope
);
4043 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4044 type of the field. We get an OFFSET_TYPE for
4049 Probably, we should not get an OFFSET_TYPE here;
4050 that transformation should be made only if `&S::T'
4052 if (TREE_CODE (scope
) == OFFSET_TYPE
)
4053 scope
= TREE_TYPE (scope
);
4054 /* The type of the POSTFIX_EXPRESSION must be
4056 scope
= complete_type_or_else (scope
, NULL_TREE
);
4057 /* Let the name lookup machinery know that we are
4058 processing a class member access expression. */
4059 parser
->context
->object_type
= scope
;
4060 /* If something went wrong, we want to be able to
4061 discern that case, as opposed to the case where
4062 there was no SCOPE due to the type of expression
4065 scope
= error_mark_node
;
4068 /* Consume the `.' or `->' operator. */
4069 cp_lexer_consume_token (parser
->lexer
);
4070 /* If the SCOPE is not a scalar type, we are looking at an
4071 ordinary class member access expression, rather than a
4072 pseudo-destructor-name. */
4073 if (!scope
|| !SCALAR_TYPE_P (scope
))
4075 template_p
= cp_parser_optional_template_keyword (parser
);
4076 /* Parse the id-expression. */
4077 name
= cp_parser_id_expression (parser
,
4079 /*check_dependency_p=*/true,
4080 /*template_p=*/NULL
);
4081 /* In general, build a SCOPE_REF if the member name is
4082 qualified. However, if the name was not dependent
4083 and has already been resolved; there is no need to
4084 build the SCOPE_REF. For example;
4086 struct X { void f(); };
4087 template <typename T> void f(T* t) { t->X::f(); }
4089 Even though "t" is dependent, "X::f" is not and has
4090 except that for a BASELINK there is no need to
4091 include scope information. */
4093 /* But we do need to remember that there was an explicit
4094 scope for virtual function calls. */
4096 idk
= CP_PARSER_ID_KIND_QUALIFIED
;
4098 if (name
!= error_mark_node
4099 && !BASELINK_P (name
)
4102 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
4103 parser
->scope
= NULL_TREE
;
4104 parser
->qualifying_scope
= NULL_TREE
;
4105 parser
->object_scope
= NULL_TREE
;
4108 = finish_class_member_access_expr (postfix_expression
, name
);
4110 /* Otherwise, try the pseudo-destructor-name production. */
4116 /* Parse the pseudo-destructor-name. */
4117 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4118 /* Form the call. */
4120 = finish_pseudo_destructor_expr (postfix_expression
,
4121 s
, TREE_TYPE (type
));
4124 /* We no longer need to look up names in the scope of the
4125 object on the left-hand side of the `.' or `->'
4127 parser
->context
->object_type
= NULL_TREE
;
4132 /* postfix-expression ++ */
4133 /* Consume the `++' token. */
4134 cp_lexer_consume_token (parser
->lexer
);
4135 /* Increments may not appear in constant-expressions. */
4136 if (parser
->constant_expression_p
)
4138 if (!parser
->allow_non_constant_expression_p
)
4139 return cp_parser_non_constant_expression ("an increment");
4140 parser
->non_constant_expression_p
= true;
4142 /* Generate a reprsentation for the complete expression. */
4144 = finish_increment_expr (postfix_expression
,
4145 POSTINCREMENT_EXPR
);
4146 idk
= CP_PARSER_ID_KIND_NONE
;
4149 case CPP_MINUS_MINUS
:
4150 /* postfix-expression -- */
4151 /* Consume the `--' token. */
4152 cp_lexer_consume_token (parser
->lexer
);
4153 /* Decrements may not appear in constant-expressions. */
4154 if (parser
->constant_expression_p
)
4156 if (!parser
->allow_non_constant_expression_p
)
4157 return cp_parser_non_constant_expression ("a decrement");
4158 parser
->non_constant_expression_p
= true;
4160 /* Generate a reprsentation for the complete expression. */
4162 = finish_increment_expr (postfix_expression
,
4163 POSTDECREMENT_EXPR
);
4164 idk
= CP_PARSER_ID_KIND_NONE
;
4168 return postfix_expression
;
4172 /* We should never get here. */
4174 return error_mark_node
;
4177 /* Parse an expression-list.
4180 assignment-expression
4181 expression-list, assignment-expression
4183 Returns a TREE_LIST. The TREE_VALUE of each node is a
4184 representation of an assignment-expression. Note that a TREE_LIST
4185 is returned even if there is only a single expression in the list. */
4188 cp_parser_expression_list (cp_parser
* parser
)
4190 tree expression_list
= NULL_TREE
;
4192 /* Consume expressions until there are no more. */
4197 /* Parse the next assignment-expression. */
4198 expr
= cp_parser_assignment_expression (parser
);
4199 /* Add it to the list. */
4200 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4202 /* If the next token isn't a `,', then we are done. */
4203 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4205 /* All uses of expression-list in the grammar are followed
4206 by a `)'. Therefore, if the next token is not a `)' an
4207 error will be issued, unless we are parsing tentatively.
4208 Skip ahead to see if there is another `,' before the `)';
4209 if so, we can go there and recover. */
4210 if (cp_parser_parsing_tentatively (parser
)
4211 || cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
4212 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser
))
4216 /* Otherwise, consume the `,' and keep going. */
4217 cp_lexer_consume_token (parser
->lexer
);
4220 /* We built up the list in reverse order so we must reverse it now. */
4221 return nreverse (expression_list
);
4224 /* Parse a pseudo-destructor-name.
4226 pseudo-destructor-name:
4227 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4228 :: [opt] nested-name-specifier template template-id :: ~ type-name
4229 :: [opt] nested-name-specifier [opt] ~ type-name
4231 If either of the first two productions is used, sets *SCOPE to the
4232 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4233 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4234 or ERROR_MARK_NODE if no type-name is present. */
4237 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4241 bool nested_name_specifier_p
;
4243 /* Look for the optional `::' operator. */
4244 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4245 /* Look for the optional nested-name-specifier. */
4246 nested_name_specifier_p
4247 = (cp_parser_nested_name_specifier_opt (parser
,
4248 /*typename_keyword_p=*/false,
4249 /*check_dependency_p=*/true,
4252 /* Now, if we saw a nested-name-specifier, we might be doing the
4253 second production. */
4254 if (nested_name_specifier_p
4255 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4257 /* Consume the `template' keyword. */
4258 cp_lexer_consume_token (parser
->lexer
);
4259 /* Parse the template-id. */
4260 cp_parser_template_id (parser
,
4261 /*template_keyword_p=*/true,
4262 /*check_dependency_p=*/false);
4263 /* Look for the `::' token. */
4264 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4266 /* If the next token is not a `~', then there might be some
4267 additional qualification. */
4268 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4270 /* Look for the type-name. */
4271 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4272 /* Look for the `::' token. */
4273 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4278 /* Look for the `~'. */
4279 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4280 /* Look for the type-name again. We are not responsible for
4281 checking that it matches the first type-name. */
4282 *type
= cp_parser_type_name (parser
);
4285 /* Parse a unary-expression.
4291 unary-operator cast-expression
4292 sizeof unary-expression
4300 __extension__ cast-expression
4301 __alignof__ unary-expression
4302 __alignof__ ( type-id )
4303 __real__ cast-expression
4304 __imag__ cast-expression
4307 ADDRESS_P is true iff the unary-expression is appearing as the
4308 operand of the `&' operator.
4310 Returns a representation of the expresion. */
4313 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4316 enum tree_code unary_operator
;
4318 /* Peek at the next token. */
4319 token
= cp_lexer_peek_token (parser
->lexer
);
4320 /* Some keywords give away the kind of expression. */
4321 if (token
->type
== CPP_KEYWORD
)
4323 enum rid keyword
= token
->keyword
;
4329 /* Consume the `alignof' token. */
4330 cp_lexer_consume_token (parser
->lexer
);
4331 /* Parse the operand. */
4332 return finish_alignof (cp_parser_sizeof_operand
4340 /* Consume the `sizeof' token. */
4341 cp_lexer_consume_token (parser
->lexer
);
4342 /* Parse the operand. */
4343 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4345 /* If the type of the operand cannot be determined build a
4347 if (TYPE_P (operand
)
4348 ? dependent_type_p (operand
)
4349 : type_dependent_expression_p (operand
))
4350 return build_min (SIZEOF_EXPR
, size_type_node
, operand
);
4351 /* Otherwise, compute the constant value. */
4353 return finish_sizeof (operand
);
4357 return cp_parser_new_expression (parser
);
4360 return cp_parser_delete_expression (parser
);
4364 /* The saved value of the PEDANTIC flag. */
4368 /* Save away the PEDANTIC flag. */
4369 cp_parser_extension_opt (parser
, &saved_pedantic
);
4370 /* Parse the cast-expression. */
4371 expr
= cp_parser_cast_expression (parser
, /*address_p=*/false);
4372 /* Restore the PEDANTIC flag. */
4373 pedantic
= saved_pedantic
;
4383 /* Consume the `__real__' or `__imag__' token. */
4384 cp_lexer_consume_token (parser
->lexer
);
4385 /* Parse the cast-expression. */
4386 expression
= cp_parser_cast_expression (parser
,
4387 /*address_p=*/false);
4388 /* Create the complete representation. */
4389 return build_x_unary_op ((keyword
== RID_REALPART
4390 ? REALPART_EXPR
: IMAGPART_EXPR
),
4400 /* Look for the `:: new' and `:: delete', which also signal the
4401 beginning of a new-expression, or delete-expression,
4402 respectively. If the next token is `::', then it might be one of
4404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4408 /* See if the token after the `::' is one of the keywords in
4409 which we're interested. */
4410 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4411 /* If it's `new', we have a new-expression. */
4412 if (keyword
== RID_NEW
)
4413 return cp_parser_new_expression (parser
);
4414 /* Similarly, for `delete'. */
4415 else if (keyword
== RID_DELETE
)
4416 return cp_parser_delete_expression (parser
);
4419 /* Look for a unary operator. */
4420 unary_operator
= cp_parser_unary_operator (token
);
4421 /* The `++' and `--' operators can be handled similarly, even though
4422 they are not technically unary-operators in the grammar. */
4423 if (unary_operator
== ERROR_MARK
)
4425 if (token
->type
== CPP_PLUS_PLUS
)
4426 unary_operator
= PREINCREMENT_EXPR
;
4427 else if (token
->type
== CPP_MINUS_MINUS
)
4428 unary_operator
= PREDECREMENT_EXPR
;
4429 /* Handle the GNU address-of-label extension. */
4430 else if (cp_parser_allow_gnu_extensions_p (parser
)
4431 && token
->type
== CPP_AND_AND
)
4435 /* Consume the '&&' token. */
4436 cp_lexer_consume_token (parser
->lexer
);
4437 /* Look for the identifier. */
4438 identifier
= cp_parser_identifier (parser
);
4439 /* Create an expression representing the address. */
4440 return finish_label_address_expr (identifier
);
4443 if (unary_operator
!= ERROR_MARK
)
4445 tree cast_expression
;
4447 /* Consume the operator token. */
4448 token
= cp_lexer_consume_token (parser
->lexer
);
4449 /* Parse the cast-expression. */
4451 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4452 /* Now, build an appropriate representation. */
4453 switch (unary_operator
)
4456 return build_x_indirect_ref (cast_expression
, "unary *");
4459 return build_x_unary_op (ADDR_EXPR
, cast_expression
);
4461 case PREINCREMENT_EXPR
:
4462 case PREDECREMENT_EXPR
:
4463 if (parser
->constant_expression_p
)
4465 if (!parser
->allow_non_constant_expression_p
)
4466 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4469 parser
->non_constant_expression_p
= true;
4474 case TRUTH_NOT_EXPR
:
4475 return finish_unary_op_expr (unary_operator
, cast_expression
);
4478 return build_x_unary_op (BIT_NOT_EXPR
, cast_expression
);
4482 return error_mark_node
;
4486 return cp_parser_postfix_expression (parser
, address_p
);
4489 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4490 unary-operator, the corresponding tree code is returned. */
4492 static enum tree_code
4493 cp_parser_unary_operator (cp_token
* token
)
4495 switch (token
->type
)
4498 return INDIRECT_REF
;
4504 return CONVERT_EXPR
;
4510 return TRUTH_NOT_EXPR
;
4513 return BIT_NOT_EXPR
;
4520 /* Parse a new-expression.
4523 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4524 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4526 Returns a representation of the expression. */
4529 cp_parser_new_expression (cp_parser
* parser
)
4531 bool global_scope_p
;
4536 /* Look for the optional `::' operator. */
4538 = (cp_parser_global_scope_opt (parser
,
4539 /*current_scope_valid_p=*/false)
4541 /* Look for the `new' operator. */
4542 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4543 /* There's no easy way to tell a new-placement from the
4544 `( type-id )' construct. */
4545 cp_parser_parse_tentatively (parser
);
4546 /* Look for a new-placement. */
4547 placement
= cp_parser_new_placement (parser
);
4548 /* If that didn't work out, there's no new-placement. */
4549 if (!cp_parser_parse_definitely (parser
))
4550 placement
= NULL_TREE
;
4552 /* If the next token is a `(', then we have a parenthesized
4554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4556 /* Consume the `('. */
4557 cp_lexer_consume_token (parser
->lexer
);
4558 /* Parse the type-id. */
4559 type
= cp_parser_type_id (parser
);
4560 /* Look for the closing `)'. */
4561 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4563 /* Otherwise, there must be a new-type-id. */
4565 type
= cp_parser_new_type_id (parser
);
4567 /* If the next token is a `(', then we have a new-initializer. */
4568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4569 initializer
= cp_parser_new_initializer (parser
);
4571 initializer
= NULL_TREE
;
4573 /* Create a representation of the new-expression. */
4574 return build_new (placement
, type
, initializer
, global_scope_p
);
4577 /* Parse a new-placement.
4582 Returns the same representation as for an expression-list. */
4585 cp_parser_new_placement (cp_parser
* parser
)
4587 tree expression_list
;
4589 /* Look for the opening `('. */
4590 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4591 return error_mark_node
;
4592 /* Parse the expression-list. */
4593 expression_list
= cp_parser_expression_list (parser
);
4594 /* Look for the closing `)'. */
4595 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4597 return expression_list
;
4600 /* Parse a new-type-id.
4603 type-specifier-seq new-declarator [opt]
4605 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4606 and whose TREE_VALUE is the new-declarator. */
4609 cp_parser_new_type_id (cp_parser
* parser
)
4611 tree type_specifier_seq
;
4613 const char *saved_message
;
4615 /* The type-specifier sequence must not contain type definitions.
4616 (It cannot contain declarations of new types either, but if they
4617 are not definitions we will catch that because they are not
4619 saved_message
= parser
->type_definition_forbidden_message
;
4620 parser
->type_definition_forbidden_message
4621 = "types may not be defined in a new-type-id";
4622 /* Parse the type-specifier-seq. */
4623 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4624 /* Restore the old message. */
4625 parser
->type_definition_forbidden_message
= saved_message
;
4626 /* Parse the new-declarator. */
4627 declarator
= cp_parser_new_declarator_opt (parser
);
4629 return build_tree_list (type_specifier_seq
, declarator
);
4632 /* Parse an (optional) new-declarator.
4635 ptr-operator new-declarator [opt]
4636 direct-new-declarator
4638 Returns a representation of the declarator. See
4639 cp_parser_declarator for the representations used. */
4642 cp_parser_new_declarator_opt (cp_parser
* parser
)
4644 enum tree_code code
;
4646 tree cv_qualifier_seq
;
4648 /* We don't know if there's a ptr-operator next, or not. */
4649 cp_parser_parse_tentatively (parser
);
4650 /* Look for a ptr-operator. */
4651 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4652 /* If that worked, look for more new-declarators. */
4653 if (cp_parser_parse_definitely (parser
))
4657 /* Parse another optional declarator. */
4658 declarator
= cp_parser_new_declarator_opt (parser
);
4660 /* Create the representation of the declarator. */
4661 if (code
== INDIRECT_REF
)
4662 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4665 declarator
= make_reference_declarator (cv_qualifier_seq
,
4668 /* Handle the pointer-to-member case. */
4670 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4675 /* If the next token is a `[', there is a direct-new-declarator. */
4676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4677 return cp_parser_direct_new_declarator (parser
);
4682 /* Parse a direct-new-declarator.
4684 direct-new-declarator:
4686 direct-new-declarator [constant-expression]
4688 Returns an ARRAY_REF, following the same conventions as are
4689 documented for cp_parser_direct_declarator. */
4692 cp_parser_direct_new_declarator (cp_parser
* parser
)
4694 tree declarator
= NULL_TREE
;
4700 /* Look for the opening `['. */
4701 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4702 /* The first expression is not required to be constant. */
4705 expression
= cp_parser_expression (parser
);
4706 /* The standard requires that the expression have integral
4707 type. DR 74 adds enumeration types. We believe that the
4708 real intent is that these expressions be handled like the
4709 expression in a `switch' condition, which also allows
4710 classes with a single conversion to integral or
4711 enumeration type. */
4712 if (!processing_template_decl
)
4715 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4720 error ("expression in new-declarator must have integral or enumeration type");
4721 expression
= error_mark_node
;
4725 /* But all the other expressions must be. */
4728 = cp_parser_constant_expression (parser
,
4729 /*allow_non_constant=*/false,
4731 /* Look for the closing `]'. */
4732 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4734 /* Add this bound to the declarator. */
4735 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4737 /* If the next token is not a `[', then there are no more
4739 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4746 /* Parse a new-initializer.
4749 ( expression-list [opt] )
4751 Returns a reprsentation of the expression-list. If there is no
4752 expression-list, VOID_ZERO_NODE is returned. */
4755 cp_parser_new_initializer (cp_parser
* parser
)
4757 tree expression_list
;
4759 /* Look for the opening parenthesis. */
4760 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4761 /* If the next token is not a `)', then there is an
4763 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4764 expression_list
= cp_parser_expression_list (parser
);
4766 expression_list
= void_zero_node
;
4767 /* Look for the closing parenthesis. */
4768 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4770 return expression_list
;
4773 /* Parse a delete-expression.
4776 :: [opt] delete cast-expression
4777 :: [opt] delete [ ] cast-expression
4779 Returns a representation of the expression. */
4782 cp_parser_delete_expression (cp_parser
* parser
)
4784 bool global_scope_p
;
4788 /* Look for the optional `::' operator. */
4790 = (cp_parser_global_scope_opt (parser
,
4791 /*current_scope_valid_p=*/false)
4793 /* Look for the `delete' keyword. */
4794 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4795 /* See if the array syntax is in use. */
4796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4798 /* Consume the `[' token. */
4799 cp_lexer_consume_token (parser
->lexer
);
4800 /* Look for the `]' token. */
4801 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4802 /* Remember that this is the `[]' construct. */
4808 /* Parse the cast-expression. */
4809 expression
= cp_parser_cast_expression (parser
, /*address_p=*/false);
4811 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4814 /* Parse a cast-expression.
4818 ( type-id ) cast-expression
4820 Returns a representation of the expression. */
4823 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4825 /* If it's a `(', then we might be looking at a cast. */
4826 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4828 tree type
= NULL_TREE
;
4829 tree expr
= NULL_TREE
;
4830 bool compound_literal_p
;
4831 const char *saved_message
;
4833 /* There's no way to know yet whether or not this is a cast.
4834 For example, `(int (3))' is a unary-expression, while `(int)
4835 3' is a cast. So, we resort to parsing tentatively. */
4836 cp_parser_parse_tentatively (parser
);
4837 /* Types may not be defined in a cast. */
4838 saved_message
= parser
->type_definition_forbidden_message
;
4839 parser
->type_definition_forbidden_message
4840 = "types may not be defined in casts";
4841 /* Consume the `('. */
4842 cp_lexer_consume_token (parser
->lexer
);
4843 /* A very tricky bit is that `(struct S) { 3 }' is a
4844 compound-literal (which we permit in C++ as an extension).
4845 But, that construct is not a cast-expression -- it is a
4846 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4847 is legal; if the compound-literal were a cast-expression,
4848 you'd need an extra set of parentheses.) But, if we parse
4849 the type-id, and it happens to be a class-specifier, then we
4850 will commit to the parse at that point, because we cannot
4851 undo the action that is done when creating a new class. So,
4852 then we cannot back up and do a postfix-expression.
4854 Therefore, we scan ahead to the closing `)', and check to see
4855 if the token after the `)' is a `{'. If so, we are not
4856 looking at a cast-expression.
4858 Save tokens so that we can put them back. */
4859 cp_lexer_save_tokens (parser
->lexer
);
4860 /* Skip tokens until the next token is a closing parenthesis.
4861 If we find the closing `)', and the next token is a `{', then
4862 we are looking at a compound-literal. */
4864 = (cp_parser_skip_to_closing_parenthesis (parser
)
4865 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4866 /* Roll back the tokens we skipped. */
4867 cp_lexer_rollback_tokens (parser
->lexer
);
4868 /* If we were looking at a compound-literal, simulate an error
4869 so that the call to cp_parser_parse_definitely below will
4871 if (compound_literal_p
)
4872 cp_parser_simulate_error (parser
);
4875 /* Look for the type-id. */
4876 type
= cp_parser_type_id (parser
);
4877 /* Look for the closing `)'. */
4878 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4881 /* Restore the saved message. */
4882 parser
->type_definition_forbidden_message
= saved_message
;
4884 /* If ok so far, parse the dependent expression. We cannot be
4885 sure it is a cast. Consider `(T ())'. It is a parenthesized
4886 ctor of T, but looks like a cast to function returning T
4887 without a dependent expression. */
4888 if (!cp_parser_error_occurred (parser
))
4889 expr
= cp_parser_cast_expression (parser
, /*address_p=*/false);
4891 if (cp_parser_parse_definitely (parser
))
4893 /* Warn about old-style casts, if so requested. */
4894 if (warn_old_style_cast
4895 && !in_system_header
4896 && !VOID_TYPE_P (type
)
4897 && current_lang_name
!= lang_name_c
)
4898 warning ("use of old-style cast");
4900 /* Only type conversions to integral or enumeration types
4901 can be used in constant-expressions. */
4902 if (parser
->constant_expression_p
4903 && !dependent_type_p (type
)
4904 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4906 if (!parser
->allow_non_constant_expression_p
)
4907 return (cp_parser_non_constant_expression
4908 ("a casts to a type other than an integral or "
4909 "enumeration type"));
4910 parser
->non_constant_expression_p
= true;
4912 /* Perform the cast. */
4913 expr
= build_c_cast (type
, expr
);
4918 /* If we get here, then it's not a cast, so it must be a
4919 unary-expression. */
4920 return cp_parser_unary_expression (parser
, address_p
);
4923 /* Parse a pm-expression.
4927 pm-expression .* cast-expression
4928 pm-expression ->* cast-expression
4930 Returns a representation of the expression. */
4933 cp_parser_pm_expression (cp_parser
* parser
)
4938 /* Parse the cast-expresion. */
4939 cast_expr
= cp_parser_cast_expression (parser
, /*address_p=*/false);
4940 pm_expr
= cast_expr
;
4941 /* Now look for pointer-to-member operators. */
4945 enum cpp_ttype token_type
;
4947 /* Peek at the next token. */
4948 token
= cp_lexer_peek_token (parser
->lexer
);
4949 token_type
= token
->type
;
4950 /* If it's not `.*' or `->*' there's no pointer-to-member
4952 if (token_type
!= CPP_DOT_STAR
4953 && token_type
!= CPP_DEREF_STAR
)
4956 /* Consume the token. */
4957 cp_lexer_consume_token (parser
->lexer
);
4959 /* Parse another cast-expression. */
4960 cast_expr
= cp_parser_cast_expression (parser
, /*address_p=*/false);
4962 /* Build the representation of the pointer-to-member
4964 if (token_type
== CPP_DEREF_STAR
)
4965 pm_expr
= build_x_binary_op (MEMBER_REF
, pm_expr
, cast_expr
);
4967 pm_expr
= build_m_component_ref (pm_expr
, cast_expr
);
4973 /* Parse a multiplicative-expression.
4975 mulitplicative-expression:
4977 multiplicative-expression * pm-expression
4978 multiplicative-expression / pm-expression
4979 multiplicative-expression % pm-expression
4981 Returns a representation of the expression. */
4984 cp_parser_multiplicative_expression (cp_parser
* parser
)
4986 static const cp_parser_token_tree_map map
= {
4987 { CPP_MULT
, MULT_EXPR
},
4988 { CPP_DIV
, TRUNC_DIV_EXPR
},
4989 { CPP_MOD
, TRUNC_MOD_EXPR
},
4990 { CPP_EOF
, ERROR_MARK
}
4993 return cp_parser_binary_expression (parser
,
4995 cp_parser_pm_expression
);
4998 /* Parse an additive-expression.
5000 additive-expression:
5001 multiplicative-expression
5002 additive-expression + multiplicative-expression
5003 additive-expression - multiplicative-expression
5005 Returns a representation of the expression. */
5008 cp_parser_additive_expression (cp_parser
* parser
)
5010 static const cp_parser_token_tree_map map
= {
5011 { CPP_PLUS
, PLUS_EXPR
},
5012 { CPP_MINUS
, MINUS_EXPR
},
5013 { CPP_EOF
, ERROR_MARK
}
5016 return cp_parser_binary_expression (parser
,
5018 cp_parser_multiplicative_expression
);
5021 /* Parse a shift-expression.
5025 shift-expression << additive-expression
5026 shift-expression >> additive-expression
5028 Returns a representation of the expression. */
5031 cp_parser_shift_expression (cp_parser
* parser
)
5033 static const cp_parser_token_tree_map map
= {
5034 { CPP_LSHIFT
, LSHIFT_EXPR
},
5035 { CPP_RSHIFT
, RSHIFT_EXPR
},
5036 { CPP_EOF
, ERROR_MARK
}
5039 return cp_parser_binary_expression (parser
,
5041 cp_parser_additive_expression
);
5044 /* Parse a relational-expression.
5046 relational-expression:
5048 relational-expression < shift-expression
5049 relational-expression > shift-expression
5050 relational-expression <= shift-expression
5051 relational-expression >= shift-expression
5055 relational-expression:
5056 relational-expression <? shift-expression
5057 relational-expression >? shift-expression
5059 Returns a representation of the expression. */
5062 cp_parser_relational_expression (cp_parser
* parser
)
5064 static const cp_parser_token_tree_map map
= {
5065 { CPP_LESS
, LT_EXPR
},
5066 { CPP_GREATER
, GT_EXPR
},
5067 { CPP_LESS_EQ
, LE_EXPR
},
5068 { CPP_GREATER_EQ
, GE_EXPR
},
5069 { CPP_MIN
, MIN_EXPR
},
5070 { CPP_MAX
, MAX_EXPR
},
5071 { CPP_EOF
, ERROR_MARK
}
5074 return cp_parser_binary_expression (parser
,
5076 cp_parser_shift_expression
);
5079 /* Parse an equality-expression.
5081 equality-expression:
5082 relational-expression
5083 equality-expression == relational-expression
5084 equality-expression != relational-expression
5086 Returns a representation of the expression. */
5089 cp_parser_equality_expression (cp_parser
* parser
)
5091 static const cp_parser_token_tree_map map
= {
5092 { CPP_EQ_EQ
, EQ_EXPR
},
5093 { CPP_NOT_EQ
, NE_EXPR
},
5094 { CPP_EOF
, ERROR_MARK
}
5097 return cp_parser_binary_expression (parser
,
5099 cp_parser_relational_expression
);
5102 /* Parse an and-expression.
5106 and-expression & equality-expression
5108 Returns a representation of the expression. */
5111 cp_parser_and_expression (cp_parser
* parser
)
5113 static const cp_parser_token_tree_map map
= {
5114 { CPP_AND
, BIT_AND_EXPR
},
5115 { CPP_EOF
, ERROR_MARK
}
5118 return cp_parser_binary_expression (parser
,
5120 cp_parser_equality_expression
);
5123 /* Parse an exclusive-or-expression.
5125 exclusive-or-expression:
5127 exclusive-or-expression ^ and-expression
5129 Returns a representation of the expression. */
5132 cp_parser_exclusive_or_expression (cp_parser
* parser
)
5134 static const cp_parser_token_tree_map map
= {
5135 { CPP_XOR
, BIT_XOR_EXPR
},
5136 { CPP_EOF
, ERROR_MARK
}
5139 return cp_parser_binary_expression (parser
,
5141 cp_parser_and_expression
);
5145 /* Parse an inclusive-or-expression.
5147 inclusive-or-expression:
5148 exclusive-or-expression
5149 inclusive-or-expression | exclusive-or-expression
5151 Returns a representation of the expression. */
5154 cp_parser_inclusive_or_expression (cp_parser
* parser
)
5156 static const cp_parser_token_tree_map map
= {
5157 { CPP_OR
, BIT_IOR_EXPR
},
5158 { CPP_EOF
, ERROR_MARK
}
5161 return cp_parser_binary_expression (parser
,
5163 cp_parser_exclusive_or_expression
);
5166 /* Parse a logical-and-expression.
5168 logical-and-expression:
5169 inclusive-or-expression
5170 logical-and-expression && inclusive-or-expression
5172 Returns a representation of the expression. */
5175 cp_parser_logical_and_expression (cp_parser
* parser
)
5177 static const cp_parser_token_tree_map map
= {
5178 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
5179 { CPP_EOF
, ERROR_MARK
}
5182 return cp_parser_binary_expression (parser
,
5184 cp_parser_inclusive_or_expression
);
5187 /* Parse a logical-or-expression.
5189 logical-or-expression:
5190 logical-and-expresion
5191 logical-or-expression || logical-and-expression
5193 Returns a representation of the expression. */
5196 cp_parser_logical_or_expression (cp_parser
* parser
)
5198 static const cp_parser_token_tree_map map
= {
5199 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
5200 { CPP_EOF
, ERROR_MARK
}
5203 return cp_parser_binary_expression (parser
,
5205 cp_parser_logical_and_expression
);
5208 /* Parse a conditional-expression.
5210 conditional-expression:
5211 logical-or-expression
5212 logical-or-expression ? expression : assignment-expression
5216 conditional-expression:
5217 logical-or-expression ? : assignment-expression
5219 Returns a representation of the expression. */
5222 cp_parser_conditional_expression (cp_parser
* parser
)
5224 tree logical_or_expr
;
5226 /* Parse the logical-or-expression. */
5227 logical_or_expr
= cp_parser_logical_or_expression (parser
);
5228 /* If the next token is a `?', then we have a real conditional
5230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5231 return cp_parser_question_colon_clause (parser
, logical_or_expr
);
5232 /* Otherwise, the value is simply the logical-or-expression. */
5234 return logical_or_expr
;
5237 /* Parse the `? expression : assignment-expression' part of a
5238 conditional-expression. The LOGICAL_OR_EXPR is the
5239 logical-or-expression that started the conditional-expression.
5240 Returns a representation of the entire conditional-expression.
5242 This routine exists only so that it can be shared between
5243 cp_parser_conditional_expression and
5244 cp_parser_assignment_expression.
5246 ? expression : assignment-expression
5250 ? : assignment-expression */
5253 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5256 tree assignment_expr
;
5258 /* Consume the `?' token. */
5259 cp_lexer_consume_token (parser
->lexer
);
5260 if (cp_parser_allow_gnu_extensions_p (parser
)
5261 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5262 /* Implicit true clause. */
5265 /* Parse the expression. */
5266 expr
= cp_parser_expression (parser
);
5268 /* The next token should be a `:'. */
5269 cp_parser_require (parser
, CPP_COLON
, "`:'");
5270 /* Parse the assignment-expression. */
5271 assignment_expr
= cp_parser_assignment_expression (parser
);
5273 /* Build the conditional-expression. */
5274 return build_x_conditional_expr (logical_or_expr
,
5279 /* Parse an assignment-expression.
5281 assignment-expression:
5282 conditional-expression
5283 logical-or-expression assignment-operator assignment_expression
5286 Returns a representation for the expression. */
5289 cp_parser_assignment_expression (cp_parser
* parser
)
5293 /* If the next token is the `throw' keyword, then we're looking at
5294 a throw-expression. */
5295 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5296 expr
= cp_parser_throw_expression (parser
);
5297 /* Otherwise, it must be that we are looking at a
5298 logical-or-expression. */
5301 /* Parse the logical-or-expression. */
5302 expr
= cp_parser_logical_or_expression (parser
);
5303 /* If the next token is a `?' then we're actually looking at a
5304 conditional-expression. */
5305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5306 return cp_parser_question_colon_clause (parser
, expr
);
5309 enum tree_code assignment_operator
;
5311 /* If it's an assignment-operator, we're using the second
5314 = cp_parser_assignment_operator_opt (parser
);
5315 if (assignment_operator
!= ERROR_MARK
)
5319 /* Parse the right-hand side of the assignment. */
5320 rhs
= cp_parser_assignment_expression (parser
);
5321 /* An assignment may not appear in a
5322 constant-expression. */
5323 if (parser
->constant_expression_p
)
5325 if (!parser
->allow_non_constant_expression_p
)
5326 return cp_parser_non_constant_expression ("an assignment");
5327 parser
->non_constant_expression_p
= true;
5329 /* Build the asignment expression. */
5330 expr
= build_x_modify_expr (expr
,
5331 assignment_operator
,
5340 /* Parse an (optional) assignment-operator.
5342 assignment-operator: one of
5343 = *= /= %= += -= >>= <<= &= ^= |=
5347 assignment-operator: one of
5350 If the next token is an assignment operator, the corresponding tree
5351 code is returned, and the token is consumed. For example, for
5352 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5353 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5354 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5355 operator, ERROR_MARK is returned. */
5357 static enum tree_code
5358 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5363 /* Peek at the next toen. */
5364 token
= cp_lexer_peek_token (parser
->lexer
);
5366 switch (token
->type
)
5377 op
= TRUNC_DIV_EXPR
;
5381 op
= TRUNC_MOD_EXPR
;
5421 /* Nothing else is an assignment operator. */
5425 /* If it was an assignment operator, consume it. */
5426 if (op
!= ERROR_MARK
)
5427 cp_lexer_consume_token (parser
->lexer
);
5432 /* Parse an expression.
5435 assignment-expression
5436 expression , assignment-expression
5438 Returns a representation of the expression. */
5441 cp_parser_expression (cp_parser
* parser
)
5443 tree expression
= NULL_TREE
;
5444 bool saw_comma_p
= false;
5448 tree assignment_expression
;
5450 /* Parse the next assignment-expression. */
5451 assignment_expression
5452 = cp_parser_assignment_expression (parser
);
5453 /* If this is the first assignment-expression, we can just
5456 expression
= assignment_expression
;
5457 /* Otherwise, chain the expressions together. It is unclear why
5458 we do not simply build COMPOUND_EXPRs as we go. */
5460 expression
= tree_cons (NULL_TREE
,
5461 assignment_expression
,
5463 /* If the next token is not a comma, then we are done with the
5465 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5467 /* Consume the `,'. */
5468 cp_lexer_consume_token (parser
->lexer
);
5469 /* The first time we see a `,', we must take special action
5470 because the representation used for a single expression is
5471 different from that used for a list containing the single
5475 /* Remember that this expression has a `,' in it. */
5477 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5478 additional expressions to it. */
5479 expression
= build_tree_list (NULL_TREE
, expression
);
5483 /* Build a COMPOUND_EXPR to represent the entire expression, if
5484 necessary. We built up the list in reverse order, so we must
5485 straighten it out here. */
5488 /* A comma operator cannot appear in a constant-expression. */
5489 if (parser
->constant_expression_p
)
5491 if (!parser
->allow_non_constant_expression_p
)
5492 return cp_parser_non_constant_expression ("a comma operator");
5493 parser
->non_constant_expression_p
= true;
5495 expression
= build_x_compound_expr (nreverse (expression
));
5501 /* Parse a constant-expression.
5503 constant-expression:
5504 conditional-expression
5506 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5507 accepted. In that case *NON_CONSTANT_P is set to TRUE. If
5508 ALLOW_NON_CONSTANT_P is false, NON_CONSTANT_P should be NULL. */
5511 cp_parser_constant_expression (cp_parser
* parser
,
5512 bool allow_non_constant_p
,
5513 bool *non_constant_p
)
5515 bool saved_constant_expression_p
;
5516 bool saved_allow_non_constant_expression_p
;
5517 bool saved_non_constant_expression_p
;
5520 /* It might seem that we could simply parse the
5521 conditional-expression, and then check to see if it were
5522 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5523 one that the compiler can figure out is constant, possibly after
5524 doing some simplifications or optimizations. The standard has a
5525 precise definition of constant-expression, and we must honor
5526 that, even though it is somewhat more restrictive.
5532 is not a legal declaration, because `(2, 3)' is not a
5533 constant-expression. The `,' operator is forbidden in a
5534 constant-expression. However, GCC's constant-folding machinery
5535 will fold this operation to an INTEGER_CST for `3'. */
5537 /* Save the old settings. */
5538 saved_constant_expression_p
= parser
->constant_expression_p
;
5539 saved_allow_non_constant_expression_p
5540 = parser
->allow_non_constant_expression_p
;
5541 saved_non_constant_expression_p
= parser
->non_constant_expression_p
;
5542 /* We are now parsing a constant-expression. */
5543 parser
->constant_expression_p
= true;
5544 parser
->allow_non_constant_expression_p
= allow_non_constant_p
;
5545 parser
->non_constant_expression_p
= false;
5546 /* Parse the conditional-expression. */
5547 expression
= cp_parser_conditional_expression (parser
);
5548 /* Restore the old settings. */
5549 parser
->constant_expression_p
= saved_constant_expression_p
;
5550 parser
->allow_non_constant_expression_p
5551 = saved_allow_non_constant_expression_p
;
5552 if (allow_non_constant_p
)
5553 *non_constant_p
= parser
->non_constant_expression_p
;
5554 parser
->non_constant_expression_p
= saved_non_constant_expression_p
;
5559 /* Statements [gram.stmt.stmt] */
5561 /* Parse a statement.
5565 expression-statement
5570 declaration-statement
5574 cp_parser_statement (cp_parser
* parser
)
5578 int statement_line_number
;
5580 /* There is no statement yet. */
5581 statement
= NULL_TREE
;
5582 /* Peek at the next token. */
5583 token
= cp_lexer_peek_token (parser
->lexer
);
5584 /* Remember the line number of the first token in the statement. */
5585 statement_line_number
= token
->location
.line
;
5586 /* If this is a keyword, then that will often determine what kind of
5587 statement we have. */
5588 if (token
->type
== CPP_KEYWORD
)
5590 enum rid keyword
= token
->keyword
;
5596 statement
= cp_parser_labeled_statement (parser
);
5601 statement
= cp_parser_selection_statement (parser
);
5607 statement
= cp_parser_iteration_statement (parser
);
5614 statement
= cp_parser_jump_statement (parser
);
5618 statement
= cp_parser_try_block (parser
);
5622 /* It might be a keyword like `int' that can start a
5623 declaration-statement. */
5627 else if (token
->type
== CPP_NAME
)
5629 /* If the next token is a `:', then we are looking at a
5630 labeled-statement. */
5631 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5632 if (token
->type
== CPP_COLON
)
5633 statement
= cp_parser_labeled_statement (parser
);
5635 /* Anything that starts with a `{' must be a compound-statement. */
5636 else if (token
->type
== CPP_OPEN_BRACE
)
5637 statement
= cp_parser_compound_statement (parser
);
5639 /* Everything else must be a declaration-statement or an
5640 expression-statement. Try for the declaration-statement
5641 first, unless we are looking at a `;', in which case we know that
5642 we have an expression-statement. */
5645 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5647 cp_parser_parse_tentatively (parser
);
5648 /* Try to parse the declaration-statement. */
5649 cp_parser_declaration_statement (parser
);
5650 /* If that worked, we're done. */
5651 if (cp_parser_parse_definitely (parser
))
5654 /* Look for an expression-statement instead. */
5655 statement
= cp_parser_expression_statement (parser
);
5658 /* Set the line number for the statement. */
5659 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5660 STMT_LINENO (statement
) = statement_line_number
;
5663 /* Parse a labeled-statement.
5666 identifier : statement
5667 case constant-expression : statement
5670 Returns the new CASE_LABEL, for a `case' or `default' label. For
5671 an ordinary label, returns a LABEL_STMT. */
5674 cp_parser_labeled_statement (cp_parser
* parser
)
5677 tree statement
= NULL_TREE
;
5679 /* The next token should be an identifier. */
5680 token
= cp_lexer_peek_token (parser
->lexer
);
5681 if (token
->type
!= CPP_NAME
5682 && token
->type
!= CPP_KEYWORD
)
5684 cp_parser_error (parser
, "expected labeled-statement");
5685 return error_mark_node
;
5688 switch (token
->keyword
)
5694 /* Consume the `case' token. */
5695 cp_lexer_consume_token (parser
->lexer
);
5696 /* Parse the constant-expression. */
5697 expr
= cp_parser_constant_expression (parser
,
5698 /*allow_non_constant=*/false,
5700 /* Create the label. */
5701 statement
= finish_case_label (expr
, NULL_TREE
);
5706 /* Consume the `default' token. */
5707 cp_lexer_consume_token (parser
->lexer
);
5708 /* Create the label. */
5709 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5713 /* Anything else must be an ordinary label. */
5714 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5718 /* Require the `:' token. */
5719 cp_parser_require (parser
, CPP_COLON
, "`:'");
5720 /* Parse the labeled statement. */
5721 cp_parser_statement (parser
);
5723 /* Return the label, in the case of a `case' or `default' label. */
5727 /* Parse an expression-statement.
5729 expression-statement:
5732 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5733 statement consists of nothing more than an `;'. */
5736 cp_parser_expression_statement (cp_parser
* parser
)
5740 /* If the next token is not a `;', then there is an expression to parse. */
5741 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5742 statement
= finish_expr_stmt (cp_parser_expression (parser
));
5743 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5747 statement
= NULL_TREE
;
5749 /* Consume the final `;'. */
5750 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5755 /* Parse a compound-statement.
5758 { statement-seq [opt] }
5760 Returns a COMPOUND_STMT representing the statement. */
5763 cp_parser_compound_statement (cp_parser
*parser
)
5767 /* Consume the `{'. */
5768 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5769 return error_mark_node
;
5770 /* Begin the compound-statement. */
5771 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/0);
5772 /* Parse an (optional) statement-seq. */
5773 cp_parser_statement_seq_opt (parser
);
5774 /* Finish the compound-statement. */
5775 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt
);
5776 /* Consume the `}'. */
5777 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5779 return compound_stmt
;
5782 /* Parse an (optional) statement-seq.
5786 statement-seq [opt] statement */
5789 cp_parser_statement_seq_opt (cp_parser
* parser
)
5791 /* Scan statements until there aren't any more. */
5794 /* If we're looking at a `}', then we've run out of statements. */
5795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5796 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5799 /* Parse the statement. */
5800 cp_parser_statement (parser
);
5804 /* Parse a selection-statement.
5806 selection-statement:
5807 if ( condition ) statement
5808 if ( condition ) statement else statement
5809 switch ( condition ) statement
5811 Returns the new IF_STMT or SWITCH_STMT. */
5814 cp_parser_selection_statement (cp_parser
* parser
)
5819 /* Peek at the next token. */
5820 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5822 /* See what kind of keyword it is. */
5823 keyword
= token
->keyword
;
5832 /* Look for the `('. */
5833 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5835 cp_parser_skip_to_end_of_statement (parser
);
5836 return error_mark_node
;
5839 /* Begin the selection-statement. */
5840 if (keyword
== RID_IF
)
5841 statement
= begin_if_stmt ();
5843 statement
= begin_switch_stmt ();
5845 /* Parse the condition. */
5846 condition
= cp_parser_condition (parser
);
5847 /* Look for the `)'. */
5848 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5849 cp_parser_skip_to_closing_parenthesis (parser
);
5851 if (keyword
== RID_IF
)
5855 /* Add the condition. */
5856 finish_if_stmt_cond (condition
, statement
);
5858 /* Parse the then-clause. */
5859 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5860 finish_then_clause (statement
);
5862 /* If the next token is `else', parse the else-clause. */
5863 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5868 /* Consume the `else' keyword. */
5869 cp_lexer_consume_token (parser
->lexer
);
5870 /* Parse the else-clause. */
5872 = cp_parser_implicitly_scoped_statement (parser
);
5873 finish_else_clause (statement
);
5876 /* Now we're all done with the if-statement. */
5883 /* Add the condition. */
5884 finish_switch_cond (condition
, statement
);
5886 /* Parse the body of the switch-statement. */
5887 body
= cp_parser_implicitly_scoped_statement (parser
);
5889 /* Now we're all done with the switch-statement. */
5890 finish_switch_stmt (statement
);
5898 cp_parser_error (parser
, "expected selection-statement");
5899 return error_mark_node
;
5903 /* Parse a condition.
5907 type-specifier-seq declarator = assignment-expression
5912 type-specifier-seq declarator asm-specification [opt]
5913 attributes [opt] = assignment-expression
5915 Returns the expression that should be tested. */
5918 cp_parser_condition (cp_parser
* parser
)
5920 tree type_specifiers
;
5921 const char *saved_message
;
5923 /* Try the declaration first. */
5924 cp_parser_parse_tentatively (parser
);
5925 /* New types are not allowed in the type-specifier-seq for a
5927 saved_message
= parser
->type_definition_forbidden_message
;
5928 parser
->type_definition_forbidden_message
5929 = "types may not be defined in conditions";
5930 /* Parse the type-specifier-seq. */
5931 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5932 /* Restore the saved message. */
5933 parser
->type_definition_forbidden_message
= saved_message
;
5934 /* If all is well, we might be looking at a declaration. */
5935 if (!cp_parser_error_occurred (parser
))
5938 tree asm_specification
;
5941 tree initializer
= NULL_TREE
;
5943 /* Parse the declarator. */
5944 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5945 /*ctor_dtor_or_conv_p=*/NULL
);
5946 /* Parse the attributes. */
5947 attributes
= cp_parser_attributes_opt (parser
);
5948 /* Parse the asm-specification. */
5949 asm_specification
= cp_parser_asm_specification_opt (parser
);
5950 /* If the next token is not an `=', then we might still be
5951 looking at an expression. For example:
5955 looks like a decl-specifier-seq and a declarator -- but then
5956 there is no `=', so this is an expression. */
5957 cp_parser_require (parser
, CPP_EQ
, "`='");
5958 /* If we did see an `=', then we are looking at a declaration
5960 if (cp_parser_parse_definitely (parser
))
5962 /* Create the declaration. */
5963 decl
= start_decl (declarator
, type_specifiers
,
5964 /*initialized_p=*/true,
5965 attributes
, /*prefix_attributes=*/NULL_TREE
);
5966 /* Parse the assignment-expression. */
5967 initializer
= cp_parser_assignment_expression (parser
);
5969 /* Process the initializer. */
5970 cp_finish_decl (decl
,
5973 LOOKUP_ONLYCONVERTING
);
5975 return convert_from_reference (decl
);
5978 /* If we didn't even get past the declarator successfully, we are
5979 definitely not looking at a declaration. */
5981 cp_parser_abort_tentative_parse (parser
);
5983 /* Otherwise, we are looking at an expression. */
5984 return cp_parser_expression (parser
);
5987 /* Parse an iteration-statement.
5989 iteration-statement:
5990 while ( condition ) statement
5991 do statement while ( expression ) ;
5992 for ( for-init-statement condition [opt] ; expression [opt] )
5995 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5998 cp_parser_iteration_statement (cp_parser
* parser
)
6004 /* Peek at the next token. */
6005 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6007 return error_mark_node
;
6009 /* See what kind of keyword it is. */
6010 keyword
= token
->keyword
;
6017 /* Begin the while-statement. */
6018 statement
= begin_while_stmt ();
6019 /* Look for the `('. */
6020 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6021 /* Parse the condition. */
6022 condition
= cp_parser_condition (parser
);
6023 finish_while_stmt_cond (condition
, statement
);
6024 /* Look for the `)'. */
6025 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6026 /* Parse the dependent statement. */
6027 cp_parser_already_scoped_statement (parser
);
6028 /* We're done with the while-statement. */
6029 finish_while_stmt (statement
);
6037 /* Begin the do-statement. */
6038 statement
= begin_do_stmt ();
6039 /* Parse the body of the do-statement. */
6040 cp_parser_implicitly_scoped_statement (parser
);
6041 finish_do_body (statement
);
6042 /* Look for the `while' keyword. */
6043 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6044 /* Look for the `('. */
6045 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6046 /* Parse the expression. */
6047 expression
= cp_parser_expression (parser
);
6048 /* We're done with the do-statement. */
6049 finish_do_stmt (expression
, statement
);
6050 /* Look for the `)'. */
6051 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6052 /* Look for the `;'. */
6053 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6059 tree condition
= NULL_TREE
;
6060 tree expression
= NULL_TREE
;
6062 /* Begin the for-statement. */
6063 statement
= begin_for_stmt ();
6064 /* Look for the `('. */
6065 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6066 /* Parse the initialization. */
6067 cp_parser_for_init_statement (parser
);
6068 finish_for_init_stmt (statement
);
6070 /* If there's a condition, process it. */
6071 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6072 condition
= cp_parser_condition (parser
);
6073 finish_for_cond (condition
, statement
);
6074 /* Look for the `;'. */
6075 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6077 /* If there's an expression, process it. */
6078 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6079 expression
= cp_parser_expression (parser
);
6080 finish_for_expr (expression
, statement
);
6081 /* Look for the `)'. */
6082 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`;'");
6084 /* Parse the body of the for-statement. */
6085 cp_parser_already_scoped_statement (parser
);
6087 /* We're done with the for-statement. */
6088 finish_for_stmt (statement
);
6093 cp_parser_error (parser
, "expected iteration-statement");
6094 statement
= error_mark_node
;
6101 /* Parse a for-init-statement.
6104 expression-statement
6105 simple-declaration */
6108 cp_parser_for_init_statement (cp_parser
* parser
)
6110 /* If the next token is a `;', then we have an empty
6111 expression-statement. Gramatically, this is also a
6112 simple-declaration, but an invalid one, because it does not
6113 declare anything. Therefore, if we did not handle this case
6114 specially, we would issue an error message about an invalid
6116 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6118 /* We're going to speculatively look for a declaration, falling back
6119 to an expression, if necessary. */
6120 cp_parser_parse_tentatively (parser
);
6121 /* Parse the declaration. */
6122 cp_parser_simple_declaration (parser
,
6123 /*function_definition_allowed_p=*/false);
6124 /* If the tentative parse failed, then we shall need to look for an
6125 expression-statement. */
6126 if (cp_parser_parse_definitely (parser
))
6130 cp_parser_expression_statement (parser
);
6133 /* Parse a jump-statement.
6138 return expression [opt] ;
6146 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6150 cp_parser_jump_statement (cp_parser
* parser
)
6152 tree statement
= error_mark_node
;
6156 /* Peek at the next token. */
6157 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6159 return error_mark_node
;
6161 /* See what kind of keyword it is. */
6162 keyword
= token
->keyword
;
6166 statement
= finish_break_stmt ();
6167 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6171 statement
= finish_continue_stmt ();
6172 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6179 /* If the next token is a `;', then there is no
6181 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6182 expr
= cp_parser_expression (parser
);
6185 /* Build the return-statement. */
6186 statement
= finish_return_stmt (expr
);
6187 /* Look for the final `;'. */
6188 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6193 /* Create the goto-statement. */
6194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6196 /* Issue a warning about this use of a GNU extension. */
6198 pedwarn ("ISO C++ forbids computed gotos");
6199 /* Consume the '*' token. */
6200 cp_lexer_consume_token (parser
->lexer
);
6201 /* Parse the dependent expression. */
6202 finish_goto_stmt (cp_parser_expression (parser
));
6205 finish_goto_stmt (cp_parser_identifier (parser
));
6206 /* Look for the final `;'. */
6207 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6211 cp_parser_error (parser
, "expected jump-statement");
6218 /* Parse a declaration-statement.
6220 declaration-statement:
6221 block-declaration */
6224 cp_parser_declaration_statement (cp_parser
* parser
)
6226 /* Parse the block-declaration. */
6227 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6229 /* Finish off the statement. */
6233 /* Some dependent statements (like `if (cond) statement'), are
6234 implicitly in their own scope. In other words, if the statement is
6235 a single statement (as opposed to a compound-statement), it is
6236 none-the-less treated as if it were enclosed in braces. Any
6237 declarations appearing in the dependent statement are out of scope
6238 after control passes that point. This function parses a statement,
6239 but ensures that is in its own scope, even if it is not a
6242 Returns the new statement. */
6245 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6249 /* If the token is not a `{', then we must take special action. */
6250 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6252 /* Create a compound-statement. */
6253 statement
= begin_compound_stmt (/*has_no_scope=*/0);
6254 /* Parse the dependent-statement. */
6255 cp_parser_statement (parser
);
6256 /* Finish the dummy compound-statement. */
6257 finish_compound_stmt (/*has_no_scope=*/0, statement
);
6259 /* Otherwise, we simply parse the statement directly. */
6261 statement
= cp_parser_compound_statement (parser
);
6263 /* Return the statement. */
6267 /* For some dependent statements (like `while (cond) statement'), we
6268 have already created a scope. Therefore, even if the dependent
6269 statement is a compound-statement, we do not want to create another
6273 cp_parser_already_scoped_statement (cp_parser
* parser
)
6275 /* If the token is not a `{', then we must take special action. */
6276 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
6280 /* Create a compound-statement. */
6281 statement
= begin_compound_stmt (/*has_no_scope=*/1);
6282 /* Parse the dependent-statement. */
6283 cp_parser_statement (parser
);
6284 /* Finish the dummy compound-statement. */
6285 finish_compound_stmt (/*has_no_scope=*/1, statement
);
6287 /* Otherwise, we simply parse the statement directly. */
6289 cp_parser_statement (parser
);
6292 /* Declarations [gram.dcl.dcl] */
6294 /* Parse an optional declaration-sequence.
6298 declaration-seq declaration */
6301 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6307 token
= cp_lexer_peek_token (parser
->lexer
);
6309 if (token
->type
== CPP_CLOSE_BRACE
6310 || token
->type
== CPP_EOF
)
6313 if (token
->type
== CPP_SEMICOLON
)
6315 /* A declaration consisting of a single semicolon is
6316 invalid. Allow it unless we're being pedantic. */
6318 pedwarn ("extra `;'");
6319 cp_lexer_consume_token (parser
->lexer
);
6323 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6324 parser to enter or exit implict `extern "C"' blocks. */
6325 while (pending_lang_change
> 0)
6327 push_lang_context (lang_name_c
);
6328 --pending_lang_change
;
6330 while (pending_lang_change
< 0)
6332 pop_lang_context ();
6333 ++pending_lang_change
;
6336 /* Parse the declaration itself. */
6337 cp_parser_declaration (parser
);
6341 /* Parse a declaration.
6346 template-declaration
6347 explicit-instantiation
6348 explicit-specialization
6349 linkage-specification
6350 namespace-definition
6355 __extension__ declaration */
6358 cp_parser_declaration (cp_parser
* parser
)
6364 /* Check for the `__extension__' keyword. */
6365 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6367 /* Parse the qualified declaration. */
6368 cp_parser_declaration (parser
);
6369 /* Restore the PEDANTIC flag. */
6370 pedantic
= saved_pedantic
;
6375 /* Try to figure out what kind of declaration is present. */
6376 token1
= *cp_lexer_peek_token (parser
->lexer
);
6377 if (token1
.type
!= CPP_EOF
)
6378 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6380 /* If the next token is `extern' and the following token is a string
6381 literal, then we have a linkage specification. */
6382 if (token1
.keyword
== RID_EXTERN
6383 && cp_parser_is_string_literal (&token2
))
6384 cp_parser_linkage_specification (parser
);
6385 /* If the next token is `template', then we have either a template
6386 declaration, an explicit instantiation, or an explicit
6388 else if (token1
.keyword
== RID_TEMPLATE
)
6390 /* `template <>' indicates a template specialization. */
6391 if (token2
.type
== CPP_LESS
6392 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6393 cp_parser_explicit_specialization (parser
);
6394 /* `template <' indicates a template declaration. */
6395 else if (token2
.type
== CPP_LESS
)
6396 cp_parser_template_declaration (parser
, /*member_p=*/false);
6397 /* Anything else must be an explicit instantiation. */
6399 cp_parser_explicit_instantiation (parser
);
6401 /* If the next token is `export', then we have a template
6403 else if (token1
.keyword
== RID_EXPORT
)
6404 cp_parser_template_declaration (parser
, /*member_p=*/false);
6405 /* If the next token is `extern', 'static' or 'inline' and the one
6406 after that is `template', we have a GNU extended explicit
6407 instantiation directive. */
6408 else if (cp_parser_allow_gnu_extensions_p (parser
)
6409 && (token1
.keyword
== RID_EXTERN
6410 || token1
.keyword
== RID_STATIC
6411 || token1
.keyword
== RID_INLINE
)
6412 && token2
.keyword
== RID_TEMPLATE
)
6413 cp_parser_explicit_instantiation (parser
);
6414 /* If the next token is `namespace', check for a named or unnamed
6415 namespace definition. */
6416 else if (token1
.keyword
== RID_NAMESPACE
6417 && (/* A named namespace definition. */
6418 (token2
.type
== CPP_NAME
6419 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6421 /* An unnamed namespace definition. */
6422 || token2
.type
== CPP_OPEN_BRACE
))
6423 cp_parser_namespace_definition (parser
);
6424 /* We must have either a block declaration or a function
6427 /* Try to parse a block-declaration, or a function-definition. */
6428 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6431 /* Parse a block-declaration.
6436 namespace-alias-definition
6443 __extension__ block-declaration
6446 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6447 part of a declaration-statement. */
6450 cp_parser_block_declaration (cp_parser
*parser
,
6456 /* Check for the `__extension__' keyword. */
6457 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6459 /* Parse the qualified declaration. */
6460 cp_parser_block_declaration (parser
, statement_p
);
6461 /* Restore the PEDANTIC flag. */
6462 pedantic
= saved_pedantic
;
6467 /* Peek at the next token to figure out which kind of declaration is
6469 token1
= cp_lexer_peek_token (parser
->lexer
);
6471 /* If the next keyword is `asm', we have an asm-definition. */
6472 if (token1
->keyword
== RID_ASM
)
6475 cp_parser_commit_to_tentative_parse (parser
);
6476 cp_parser_asm_definition (parser
);
6478 /* If the next keyword is `namespace', we have a
6479 namespace-alias-definition. */
6480 else if (token1
->keyword
== RID_NAMESPACE
)
6481 cp_parser_namespace_alias_definition (parser
);
6482 /* If the next keyword is `using', we have either a
6483 using-declaration or a using-directive. */
6484 else if (token1
->keyword
== RID_USING
)
6489 cp_parser_commit_to_tentative_parse (parser
);
6490 /* If the token after `using' is `namespace', then we have a
6492 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6493 if (token2
->keyword
== RID_NAMESPACE
)
6494 cp_parser_using_directive (parser
);
6495 /* Otherwise, it's a using-declaration. */
6497 cp_parser_using_declaration (parser
);
6499 /* If the next keyword is `__label__' we have a label declaration. */
6500 else if (token1
->keyword
== RID_LABEL
)
6503 cp_parser_commit_to_tentative_parse (parser
);
6504 cp_parser_label_declaration (parser
);
6506 /* Anything else must be a simple-declaration. */
6508 cp_parser_simple_declaration (parser
, !statement_p
);
6511 /* Parse a simple-declaration.
6514 decl-specifier-seq [opt] init-declarator-list [opt] ;
6516 init-declarator-list:
6518 init-declarator-list , init-declarator
6520 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6521 function-definition as a simple-declaration. */
6524 cp_parser_simple_declaration (cp_parser
* parser
,
6525 bool function_definition_allowed_p
)
6527 tree decl_specifiers
;
6529 bool declares_class_or_enum
;
6530 bool saw_declarator
;
6532 /* Defer access checks until we know what is being declared; the
6533 checks for names appearing in the decl-specifier-seq should be
6534 done as if we were in the scope of the thing being declared. */
6535 push_deferring_access_checks (dk_deferred
);
6537 /* Parse the decl-specifier-seq. We have to keep track of whether
6538 or not the decl-specifier-seq declares a named class or
6539 enumeration type, since that is the only case in which the
6540 init-declarator-list is allowed to be empty.
6544 In a simple-declaration, the optional init-declarator-list can be
6545 omitted only when declaring a class or enumeration, that is when
6546 the decl-specifier-seq contains either a class-specifier, an
6547 elaborated-type-specifier, or an enum-specifier. */
6549 = cp_parser_decl_specifier_seq (parser
,
6550 CP_PARSER_FLAGS_OPTIONAL
,
6552 &declares_class_or_enum
);
6553 /* We no longer need to defer access checks. */
6554 stop_deferring_access_checks ();
6556 /* If the next two tokens are both identifiers, the code is
6557 erroneous. The usual cause of this situation is code like:
6561 where "T" should name a type -- but does not. */
6562 if (cp_parser_diagnose_invalid_type_name (parser
))
6564 /* If parsing tentatively, we should commit; we really are
6565 looking at a declaration. */
6566 cp_parser_commit_to_tentative_parse (parser
);
6571 /* Keep going until we hit the `;' at the end of the simple
6573 saw_declarator
= false;
6574 while (cp_lexer_next_token_is_not (parser
->lexer
,
6578 bool function_definition_p
;
6580 saw_declarator
= true;
6581 /* Parse the init-declarator. */
6582 cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6583 function_definition_allowed_p
,
6585 &function_definition_p
);
6586 /* If an error occurred while parsing tentatively, exit quickly.
6587 (That usually happens when in the body of a function; each
6588 statement is treated as a declaration-statement until proven
6590 if (cp_parser_error_occurred (parser
))
6592 pop_deferring_access_checks ();
6595 /* Handle function definitions specially. */
6596 if (function_definition_p
)
6598 /* If the next token is a `,', then we are probably
6599 processing something like:
6603 which is erroneous. */
6604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6605 error ("mixing declarations and function-definitions is forbidden");
6606 /* Otherwise, we're done with the list of declarators. */
6609 pop_deferring_access_checks ();
6613 /* The next token should be either a `,' or a `;'. */
6614 token
= cp_lexer_peek_token (parser
->lexer
);
6615 /* If it's a `,', there are more declarators to come. */
6616 if (token
->type
== CPP_COMMA
)
6617 cp_lexer_consume_token (parser
->lexer
);
6618 /* If it's a `;', we are done. */
6619 else if (token
->type
== CPP_SEMICOLON
)
6621 /* Anything else is an error. */
6624 cp_parser_error (parser
, "expected `,' or `;'");
6625 /* Skip tokens until we reach the end of the statement. */
6626 cp_parser_skip_to_end_of_statement (parser
);
6627 pop_deferring_access_checks ();
6630 /* After the first time around, a function-definition is not
6631 allowed -- even if it was OK at first. For example:
6636 function_definition_allowed_p
= false;
6639 /* Issue an error message if no declarators are present, and the
6640 decl-specifier-seq does not itself declare a class or
6642 if (!saw_declarator
)
6644 if (cp_parser_declares_only_class_p (parser
))
6645 shadow_tag (decl_specifiers
);
6646 /* Perform any deferred access checks. */
6647 perform_deferred_access_checks ();
6650 pop_deferring_access_checks ();
6652 /* Consume the `;'. */
6653 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6655 /* Mark all the classes that appeared in the decl-specifier-seq as
6656 having received a `;'. */
6657 note_list_got_semicolon (decl_specifiers
);
6660 /* Parse a decl-specifier-seq.
6663 decl-specifier-seq [opt] decl-specifier
6666 storage-class-specifier
6675 decl-specifier-seq [opt] attributes
6677 Returns a TREE_LIST, giving the decl-specifiers in the order they
6678 appear in the source code. The TREE_VALUE of each node is the
6679 decl-specifier. For a keyword (such as `auto' or `friend'), the
6680 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6681 representation of a type-specifier, see cp_parser_type_specifier.
6683 If there are attributes, they will be stored in *ATTRIBUTES,
6684 represented as described above cp_parser_attributes.
6686 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6687 appears, and the entity that will be a friend is not going to be a
6688 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6689 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6690 friendship is granted might not be a class. */
6693 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6694 cp_parser_flags flags
,
6696 bool* declares_class_or_enum
)
6698 tree decl_specs
= NULL_TREE
;
6699 bool friend_p
= false;
6700 bool constructor_possible_p
= !parser
->in_declarator_p
;
6702 /* Assume no class or enumeration type is declared. */
6703 *declares_class_or_enum
= false;
6705 /* Assume there are no attributes. */
6706 *attributes
= NULL_TREE
;
6708 /* Keep reading specifiers until there are no more to read. */
6711 tree decl_spec
= NULL_TREE
;
6715 /* Peek at the next token. */
6716 token
= cp_lexer_peek_token (parser
->lexer
);
6717 /* Handle attributes. */
6718 if (token
->keyword
== RID_ATTRIBUTE
)
6720 /* Parse the attributes. */
6721 decl_spec
= cp_parser_attributes_opt (parser
);
6722 /* Add them to the list. */
6723 *attributes
= chainon (*attributes
, decl_spec
);
6726 /* If the next token is an appropriate keyword, we can simply
6727 add it to the list. */
6728 switch (token
->keyword
)
6734 /* The representation of the specifier is simply the
6735 appropriate TREE_IDENTIFIER node. */
6736 decl_spec
= token
->value
;
6737 /* Consume the token. */
6738 cp_lexer_consume_token (parser
->lexer
);
6741 /* function-specifier:
6748 decl_spec
= cp_parser_function_specifier_opt (parser
);
6754 /* The representation of the specifier is simply the
6755 appropriate TREE_IDENTIFIER node. */
6756 decl_spec
= token
->value
;
6757 /* Consume the token. */
6758 cp_lexer_consume_token (parser
->lexer
);
6759 /* A constructor declarator cannot appear in a typedef. */
6760 constructor_possible_p
= false;
6761 /* The "typedef" keyword can only occur in a declaration; we
6762 may as well commit at this point. */
6763 cp_parser_commit_to_tentative_parse (parser
);
6766 /* storage-class-specifier:
6781 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6788 /* Constructors are a special case. The `S' in `S()' is not a
6789 decl-specifier; it is the beginning of the declarator. */
6790 constructor_p
= (!decl_spec
6791 && constructor_possible_p
6792 && cp_parser_constructor_declarator_p (parser
,
6795 /* If we don't have a DECL_SPEC yet, then we must be looking at
6796 a type-specifier. */
6797 if (!decl_spec
&& !constructor_p
)
6799 bool decl_spec_declares_class_or_enum
;
6800 bool is_cv_qualifier
;
6803 = cp_parser_type_specifier (parser
, flags
,
6805 /*is_declaration=*/true,
6806 &decl_spec_declares_class_or_enum
,
6809 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6811 /* If this type-specifier referenced a user-defined type
6812 (a typedef, class-name, etc.), then we can't allow any
6813 more such type-specifiers henceforth.
6817 The longest sequence of decl-specifiers that could
6818 possibly be a type name is taken as the
6819 decl-specifier-seq of a declaration. The sequence shall
6820 be self-consistent as described below.
6824 As a general rule, at most one type-specifier is allowed
6825 in the complete decl-specifier-seq of a declaration. The
6826 only exceptions are the following:
6828 -- const or volatile can be combined with any other
6831 -- signed or unsigned can be combined with char, long,
6839 void g (const int Pc);
6841 Here, Pc is *not* part of the decl-specifier seq; it's
6842 the declarator. Therefore, once we see a type-specifier
6843 (other than a cv-qualifier), we forbid any additional
6844 user-defined types. We *do* still allow things like `int
6845 int' to be considered a decl-specifier-seq, and issue the
6846 error message later. */
6847 if (decl_spec
&& !is_cv_qualifier
)
6848 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6849 /* A constructor declarator cannot follow a type-specifier. */
6851 constructor_possible_p
= false;
6854 /* If we still do not have a DECL_SPEC, then there are no more
6858 /* Issue an error message, unless the entire construct was
6860 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6862 cp_parser_error (parser
, "expected decl specifier");
6863 return error_mark_node
;
6869 /* Add the DECL_SPEC to the list of specifiers. */
6870 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6872 /* After we see one decl-specifier, further decl-specifiers are
6874 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6877 /* We have built up the DECL_SPECS in reverse order. Return them in
6878 the correct order. */
6879 return nreverse (decl_specs
);
6882 /* Parse an (optional) storage-class-specifier.
6884 storage-class-specifier:
6893 storage-class-specifier:
6896 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6899 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
6901 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6909 /* Consume the token. */
6910 return cp_lexer_consume_token (parser
->lexer
)->value
;
6917 /* Parse an (optional) function-specifier.
6924 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6927 cp_parser_function_specifier_opt (cp_parser
* parser
)
6929 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6934 /* Consume the token. */
6935 return cp_lexer_consume_token (parser
->lexer
)->value
;
6942 /* Parse a linkage-specification.
6944 linkage-specification:
6945 extern string-literal { declaration-seq [opt] }
6946 extern string-literal declaration */
6949 cp_parser_linkage_specification (cp_parser
* parser
)
6954 /* Look for the `extern' keyword. */
6955 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
6957 /* Peek at the next token. */
6958 token
= cp_lexer_peek_token (parser
->lexer
);
6959 /* If it's not a string-literal, then there's a problem. */
6960 if (!cp_parser_is_string_literal (token
))
6962 cp_parser_error (parser
, "expected language-name");
6965 /* Consume the token. */
6966 cp_lexer_consume_token (parser
->lexer
);
6968 /* Transform the literal into an identifier. If the literal is a
6969 wide-character string, or contains embedded NULs, then we can't
6970 handle it as the user wants. */
6971 if (token
->type
== CPP_WSTRING
6972 || (strlen (TREE_STRING_POINTER (token
->value
))
6973 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
6975 cp_parser_error (parser
, "invalid linkage-specification");
6976 /* Assume C++ linkage. */
6977 linkage
= get_identifier ("c++");
6979 /* If it's a simple string constant, things are easier. */
6981 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
6983 /* We're now using the new linkage. */
6984 push_lang_context (linkage
);
6986 /* If the next token is a `{', then we're using the first
6988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6990 /* Consume the `{' token. */
6991 cp_lexer_consume_token (parser
->lexer
);
6992 /* Parse the declarations. */
6993 cp_parser_declaration_seq_opt (parser
);
6994 /* Look for the closing `}'. */
6995 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6997 /* Otherwise, there's just one declaration. */
7000 bool saved_in_unbraced_linkage_specification_p
;
7002 saved_in_unbraced_linkage_specification_p
7003 = parser
->in_unbraced_linkage_specification_p
;
7004 parser
->in_unbraced_linkage_specification_p
= true;
7005 have_extern_spec
= true;
7006 cp_parser_declaration (parser
);
7007 have_extern_spec
= false;
7008 parser
->in_unbraced_linkage_specification_p
7009 = saved_in_unbraced_linkage_specification_p
;
7012 /* We're done with the linkage-specification. */
7013 pop_lang_context ();
7016 /* Special member functions [gram.special] */
7018 /* Parse a conversion-function-id.
7020 conversion-function-id:
7021 operator conversion-type-id
7023 Returns an IDENTIFIER_NODE representing the operator. */
7026 cp_parser_conversion_function_id (cp_parser
* parser
)
7030 tree saved_qualifying_scope
;
7031 tree saved_object_scope
;
7033 /* Look for the `operator' token. */
7034 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7035 return error_mark_node
;
7036 /* When we parse the conversion-type-id, the current scope will be
7037 reset. However, we need that information in able to look up the
7038 conversion function later, so we save it here. */
7039 saved_scope
= parser
->scope
;
7040 saved_qualifying_scope
= parser
->qualifying_scope
;
7041 saved_object_scope
= parser
->object_scope
;
7042 /* We must enter the scope of the class so that the names of
7043 entities declared within the class are available in the
7044 conversion-type-id. For example, consider:
7051 S::operator I() { ... }
7053 In order to see that `I' is a type-name in the definition, we
7054 must be in the scope of `S'. */
7056 push_scope (saved_scope
);
7057 /* Parse the conversion-type-id. */
7058 type
= cp_parser_conversion_type_id (parser
);
7059 /* Leave the scope of the class, if any. */
7061 pop_scope (saved_scope
);
7062 /* Restore the saved scope. */
7063 parser
->scope
= saved_scope
;
7064 parser
->qualifying_scope
= saved_qualifying_scope
;
7065 parser
->object_scope
= saved_object_scope
;
7066 /* If the TYPE is invalid, indicate failure. */
7067 if (type
== error_mark_node
)
7068 return error_mark_node
;
7069 return mangle_conv_op_name_for_type (type
);
7072 /* Parse a conversion-type-id:
7075 type-specifier-seq conversion-declarator [opt]
7077 Returns the TYPE specified. */
7080 cp_parser_conversion_type_id (cp_parser
* parser
)
7083 tree type_specifiers
;
7086 /* Parse the attributes. */
7087 attributes
= cp_parser_attributes_opt (parser
);
7088 /* Parse the type-specifiers. */
7089 type_specifiers
= cp_parser_type_specifier_seq (parser
);
7090 /* If that didn't work, stop. */
7091 if (type_specifiers
== error_mark_node
)
7092 return error_mark_node
;
7093 /* Parse the conversion-declarator. */
7094 declarator
= cp_parser_conversion_declarator_opt (parser
);
7096 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
7097 /*initialized=*/0, &attributes
);
7100 /* Parse an (optional) conversion-declarator.
7102 conversion-declarator:
7103 ptr-operator conversion-declarator [opt]
7105 Returns a representation of the declarator. See
7106 cp_parser_declarator for details. */
7109 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7111 enum tree_code code
;
7113 tree cv_qualifier_seq
;
7115 /* We don't know if there's a ptr-operator next, or not. */
7116 cp_parser_parse_tentatively (parser
);
7117 /* Try the ptr-operator. */
7118 code
= cp_parser_ptr_operator (parser
, &class_type
,
7120 /* If it worked, look for more conversion-declarators. */
7121 if (cp_parser_parse_definitely (parser
))
7125 /* Parse another optional declarator. */
7126 declarator
= cp_parser_conversion_declarator_opt (parser
);
7128 /* Create the representation of the declarator. */
7129 if (code
== INDIRECT_REF
)
7130 declarator
= make_pointer_declarator (cv_qualifier_seq
,
7133 declarator
= make_reference_declarator (cv_qualifier_seq
,
7136 /* Handle the pointer-to-member case. */
7138 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
7146 /* Parse an (optional) ctor-initializer.
7149 : mem-initializer-list
7151 Returns TRUE iff the ctor-initializer was actually present. */
7154 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7156 /* If the next token is not a `:', then there is no
7157 ctor-initializer. */
7158 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7160 /* Do default initialization of any bases and members. */
7161 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7162 finish_mem_initializers (NULL_TREE
);
7167 /* Consume the `:' token. */
7168 cp_lexer_consume_token (parser
->lexer
);
7169 /* And the mem-initializer-list. */
7170 cp_parser_mem_initializer_list (parser
);
7175 /* Parse a mem-initializer-list.
7177 mem-initializer-list:
7179 mem-initializer , mem-initializer-list */
7182 cp_parser_mem_initializer_list (cp_parser
* parser
)
7184 tree mem_initializer_list
= NULL_TREE
;
7186 /* Let the semantic analysis code know that we are starting the
7187 mem-initializer-list. */
7188 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7189 error ("only constructors take base initializers");
7191 /* Loop through the list. */
7194 tree mem_initializer
;
7196 /* Parse the mem-initializer. */
7197 mem_initializer
= cp_parser_mem_initializer (parser
);
7198 /* Add it to the list, unless it was erroneous. */
7199 if (mem_initializer
)
7201 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7202 mem_initializer_list
= mem_initializer
;
7204 /* If the next token is not a `,', we're done. */
7205 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7207 /* Consume the `,' token. */
7208 cp_lexer_consume_token (parser
->lexer
);
7211 /* Perform semantic analysis. */
7212 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7213 finish_mem_initializers (mem_initializer_list
);
7216 /* Parse a mem-initializer.
7219 mem-initializer-id ( expression-list [opt] )
7224 ( expresion-list [opt] )
7226 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7227 class) or FIELD_DECL (for a non-static data member) to initialize;
7228 the TREE_VALUE is the expression-list. */
7231 cp_parser_mem_initializer (cp_parser
* parser
)
7233 tree mem_initializer_id
;
7234 tree expression_list
;
7237 /* Find out what is being initialized. */
7238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7240 pedwarn ("anachronistic old-style base class initializer");
7241 mem_initializer_id
= NULL_TREE
;
7244 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7245 member
= expand_member_init (mem_initializer_id
);
7246 if (member
&& !DECL_P (member
))
7247 in_base_initializer
= 1;
7249 /* Look for the opening `('. */
7250 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7251 /* Parse the expression-list. */
7252 if (cp_lexer_next_token_is_not (parser
->lexer
,
7254 expression_list
= cp_parser_expression_list (parser
);
7256 expression_list
= void_type_node
;
7257 /* Look for the closing `)'. */
7258 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7260 in_base_initializer
= 0;
7262 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7265 /* Parse a mem-initializer-id.
7268 :: [opt] nested-name-specifier [opt] class-name
7271 Returns a TYPE indicating the class to be initializer for the first
7272 production. Returns an IDENTIFIER_NODE indicating the data member
7273 to be initialized for the second production. */
7276 cp_parser_mem_initializer_id (cp_parser
* parser
)
7278 bool global_scope_p
;
7279 bool nested_name_specifier_p
;
7282 /* Look for the optional `::' operator. */
7284 = (cp_parser_global_scope_opt (parser
,
7285 /*current_scope_valid_p=*/false)
7287 /* Look for the optional nested-name-specifier. The simplest way to
7292 The keyword `typename' is not permitted in a base-specifier or
7293 mem-initializer; in these contexts a qualified name that
7294 depends on a template-parameter is implicitly assumed to be a
7297 is to assume that we have seen the `typename' keyword at this
7299 nested_name_specifier_p
7300 = (cp_parser_nested_name_specifier_opt (parser
,
7301 /*typename_keyword_p=*/true,
7302 /*check_dependency_p=*/true,
7305 /* If there is a `::' operator or a nested-name-specifier, then we
7306 are definitely looking for a class-name. */
7307 if (global_scope_p
|| nested_name_specifier_p
)
7308 return cp_parser_class_name (parser
,
7309 /*typename_keyword_p=*/true,
7310 /*template_keyword_p=*/false,
7312 /*check_dependency_p=*/true,
7313 /*class_head_p=*/false);
7314 /* Otherwise, we could also be looking for an ordinary identifier. */
7315 cp_parser_parse_tentatively (parser
);
7316 /* Try a class-name. */
7317 id
= cp_parser_class_name (parser
,
7318 /*typename_keyword_p=*/true,
7319 /*template_keyword_p=*/false,
7321 /*check_dependency_p=*/true,
7322 /*class_head_p=*/false);
7323 /* If we found one, we're done. */
7324 if (cp_parser_parse_definitely (parser
))
7326 /* Otherwise, look for an ordinary identifier. */
7327 return cp_parser_identifier (parser
);
7330 /* Overloading [gram.over] */
7332 /* Parse an operator-function-id.
7334 operator-function-id:
7337 Returns an IDENTIFIER_NODE for the operator which is a
7338 human-readable spelling of the identifier, e.g., `operator +'. */
7341 cp_parser_operator_function_id (cp_parser
* parser
)
7343 /* Look for the `operator' keyword. */
7344 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7345 return error_mark_node
;
7346 /* And then the name of the operator itself. */
7347 return cp_parser_operator (parser
);
7350 /* Parse an operator.
7353 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7354 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7355 || ++ -- , ->* -> () []
7362 Returns an IDENTIFIER_NODE for the operator which is a
7363 human-readable spelling of the identifier, e.g., `operator +'. */
7366 cp_parser_operator (cp_parser
* parser
)
7368 tree id
= NULL_TREE
;
7371 /* Peek at the next token. */
7372 token
= cp_lexer_peek_token (parser
->lexer
);
7373 /* Figure out which operator we have. */
7374 switch (token
->type
)
7380 /* The keyword should be either `new' or `delete'. */
7381 if (token
->keyword
== RID_NEW
)
7383 else if (token
->keyword
== RID_DELETE
)
7388 /* Consume the `new' or `delete' token. */
7389 cp_lexer_consume_token (parser
->lexer
);
7391 /* Peek at the next token. */
7392 token
= cp_lexer_peek_token (parser
->lexer
);
7393 /* If it's a `[' token then this is the array variant of the
7395 if (token
->type
== CPP_OPEN_SQUARE
)
7397 /* Consume the `[' token. */
7398 cp_lexer_consume_token (parser
->lexer
);
7399 /* Look for the `]' token. */
7400 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7401 id
= ansi_opname (op
== NEW_EXPR
7402 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7404 /* Otherwise, we have the non-array variant. */
7406 id
= ansi_opname (op
);
7412 id
= ansi_opname (PLUS_EXPR
);
7416 id
= ansi_opname (MINUS_EXPR
);
7420 id
= ansi_opname (MULT_EXPR
);
7424 id
= ansi_opname (TRUNC_DIV_EXPR
);
7428 id
= ansi_opname (TRUNC_MOD_EXPR
);
7432 id
= ansi_opname (BIT_XOR_EXPR
);
7436 id
= ansi_opname (BIT_AND_EXPR
);
7440 id
= ansi_opname (BIT_IOR_EXPR
);
7444 id
= ansi_opname (BIT_NOT_EXPR
);
7448 id
= ansi_opname (TRUTH_NOT_EXPR
);
7452 id
= ansi_assopname (NOP_EXPR
);
7456 id
= ansi_opname (LT_EXPR
);
7460 id
= ansi_opname (GT_EXPR
);
7464 id
= ansi_assopname (PLUS_EXPR
);
7468 id
= ansi_assopname (MINUS_EXPR
);
7472 id
= ansi_assopname (MULT_EXPR
);
7476 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7480 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7484 id
= ansi_assopname (BIT_XOR_EXPR
);
7488 id
= ansi_assopname (BIT_AND_EXPR
);
7492 id
= ansi_assopname (BIT_IOR_EXPR
);
7496 id
= ansi_opname (LSHIFT_EXPR
);
7500 id
= ansi_opname (RSHIFT_EXPR
);
7504 id
= ansi_assopname (LSHIFT_EXPR
);
7508 id
= ansi_assopname (RSHIFT_EXPR
);
7512 id
= ansi_opname (EQ_EXPR
);
7516 id
= ansi_opname (NE_EXPR
);
7520 id
= ansi_opname (LE_EXPR
);
7523 case CPP_GREATER_EQ
:
7524 id
= ansi_opname (GE_EXPR
);
7528 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7532 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7536 id
= ansi_opname (POSTINCREMENT_EXPR
);
7539 case CPP_MINUS_MINUS
:
7540 id
= ansi_opname (PREDECREMENT_EXPR
);
7544 id
= ansi_opname (COMPOUND_EXPR
);
7547 case CPP_DEREF_STAR
:
7548 id
= ansi_opname (MEMBER_REF
);
7552 id
= ansi_opname (COMPONENT_REF
);
7555 case CPP_OPEN_PAREN
:
7556 /* Consume the `('. */
7557 cp_lexer_consume_token (parser
->lexer
);
7558 /* Look for the matching `)'. */
7559 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7560 return ansi_opname (CALL_EXPR
);
7562 case CPP_OPEN_SQUARE
:
7563 /* Consume the `['. */
7564 cp_lexer_consume_token (parser
->lexer
);
7565 /* Look for the matching `]'. */
7566 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7567 return ansi_opname (ARRAY_REF
);
7571 id
= ansi_opname (MIN_EXPR
);
7575 id
= ansi_opname (MAX_EXPR
);
7579 id
= ansi_assopname (MIN_EXPR
);
7583 id
= ansi_assopname (MAX_EXPR
);
7587 /* Anything else is an error. */
7591 /* If we have selected an identifier, we need to consume the
7594 cp_lexer_consume_token (parser
->lexer
);
7595 /* Otherwise, no valid operator name was present. */
7598 cp_parser_error (parser
, "expected operator");
7599 id
= error_mark_node
;
7605 /* Parse a template-declaration.
7607 template-declaration:
7608 export [opt] template < template-parameter-list > declaration
7610 If MEMBER_P is TRUE, this template-declaration occurs within a
7613 The grammar rule given by the standard isn't correct. What
7616 template-declaration:
7617 export [opt] template-parameter-list-seq
7618 decl-specifier-seq [opt] init-declarator [opt] ;
7619 export [opt] template-parameter-list-seq
7622 template-parameter-list-seq:
7623 template-parameter-list-seq [opt]
7624 template < template-parameter-list > */
7627 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7629 /* Check for `export'. */
7630 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7632 /* Consume the `export' token. */
7633 cp_lexer_consume_token (parser
->lexer
);
7634 /* Warn that we do not support `export'. */
7635 warning ("keyword `export' not implemented, and will be ignored");
7638 cp_parser_template_declaration_after_export (parser
, member_p
);
7641 /* Parse a template-parameter-list.
7643 template-parameter-list:
7645 template-parameter-list , template-parameter
7647 Returns a TREE_LIST. Each node represents a template parameter.
7648 The nodes are connected via their TREE_CHAINs. */
7651 cp_parser_template_parameter_list (cp_parser
* parser
)
7653 tree parameter_list
= NULL_TREE
;
7660 /* Parse the template-parameter. */
7661 parameter
= cp_parser_template_parameter (parser
);
7662 /* Add it to the list. */
7663 parameter_list
= process_template_parm (parameter_list
,
7666 /* Peek at the next token. */
7667 token
= cp_lexer_peek_token (parser
->lexer
);
7668 /* If it's not a `,', we're done. */
7669 if (token
->type
!= CPP_COMMA
)
7671 /* Otherwise, consume the `,' token. */
7672 cp_lexer_consume_token (parser
->lexer
);
7675 return parameter_list
;
7678 /* Parse a template-parameter.
7682 parameter-declaration
7684 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7685 TREE_PURPOSE is the default value, if any. */
7688 cp_parser_template_parameter (cp_parser
* parser
)
7692 /* Peek at the next token. */
7693 token
= cp_lexer_peek_token (parser
->lexer
);
7694 /* If it is `class' or `template', we have a type-parameter. */
7695 if (token
->keyword
== RID_TEMPLATE
)
7696 return cp_parser_type_parameter (parser
);
7697 /* If it is `class' or `typename' we do not know yet whether it is a
7698 type parameter or a non-type parameter. Consider:
7700 template <typename T, typename T::X X> ...
7704 template <class C, class D*> ...
7706 Here, the first parameter is a type parameter, and the second is
7707 a non-type parameter. We can tell by looking at the token after
7708 the identifier -- if it is a `,', `=', or `>' then we have a type
7710 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7712 /* Peek at the token after `class' or `typename'. */
7713 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7714 /* If it's an identifier, skip it. */
7715 if (token
->type
== CPP_NAME
)
7716 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7717 /* Now, see if the token looks like the end of a template
7719 if (token
->type
== CPP_COMMA
7720 || token
->type
== CPP_EQ
7721 || token
->type
== CPP_GREATER
)
7722 return cp_parser_type_parameter (parser
);
7725 /* Otherwise, it is a non-type parameter.
7729 When parsing a default template-argument for a non-type
7730 template-parameter, the first non-nested `>' is taken as the end
7731 of the template parameter-list rather than a greater-than
7734 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true);
7737 /* Parse a type-parameter.
7740 class identifier [opt]
7741 class identifier [opt] = type-id
7742 typename identifier [opt]
7743 typename identifier [opt] = type-id
7744 template < template-parameter-list > class identifier [opt]
7745 template < template-parameter-list > class identifier [opt]
7748 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7749 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7750 the declaration of the parameter. */
7753 cp_parser_type_parameter (cp_parser
* parser
)
7758 /* Look for a keyword to tell us what kind of parameter this is. */
7759 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7760 "`class', `typename', or `template'");
7762 return error_mark_node
;
7764 switch (token
->keyword
)
7770 tree default_argument
;
7772 /* If the next token is an identifier, then it names the
7774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7775 identifier
= cp_parser_identifier (parser
);
7777 identifier
= NULL_TREE
;
7779 /* Create the parameter. */
7780 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7782 /* If the next token is an `=', we have a default argument. */
7783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7785 /* Consume the `=' token. */
7786 cp_lexer_consume_token (parser
->lexer
);
7787 /* Parse the default-argumen. */
7788 default_argument
= cp_parser_type_id (parser
);
7791 default_argument
= NULL_TREE
;
7793 /* Create the combined representation of the parameter and the
7794 default argument. */
7795 parameter
= build_tree_list (default_argument
,
7802 tree parameter_list
;
7804 tree default_argument
;
7806 /* Look for the `<'. */
7807 cp_parser_require (parser
, CPP_LESS
, "`<'");
7808 /* Parse the template-parameter-list. */
7809 begin_template_parm_list ();
7811 = cp_parser_template_parameter_list (parser
);
7812 parameter_list
= end_template_parm_list (parameter_list
);
7813 /* Look for the `>'. */
7814 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7815 /* Look for the `class' keyword. */
7816 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7817 /* If the next token is an `=', then there is a
7818 default-argument. If the next token is a `>', we are at
7819 the end of the parameter-list. If the next token is a `,',
7820 then we are at the end of this parameter. */
7821 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7822 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7823 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7824 identifier
= cp_parser_identifier (parser
);
7826 identifier
= NULL_TREE
;
7827 /* Create the template parameter. */
7828 parameter
= finish_template_template_parm (class_type_node
,
7831 /* If the next token is an `=', then there is a
7832 default-argument. */
7833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7835 /* Consume the `='. */
7836 cp_lexer_consume_token (parser
->lexer
);
7837 /* Parse the id-expression. */
7839 = cp_parser_id_expression (parser
,
7840 /*template_keyword_p=*/false,
7841 /*check_dependency_p=*/true,
7842 /*template_p=*/NULL
);
7843 /* Look up the name. */
7845 = cp_parser_lookup_name_simple (parser
, default_argument
);
7846 /* See if the default argument is valid. */
7848 = check_template_template_default_arg (default_argument
);
7851 default_argument
= NULL_TREE
;
7853 /* Create the combined representation of the parameter and the
7854 default argument. */
7855 parameter
= build_tree_list (default_argument
,
7861 /* Anything else is an error. */
7862 cp_parser_error (parser
,
7863 "expected `class', `typename', or `template'");
7864 parameter
= error_mark_node
;
7870 /* Parse a template-id.
7873 template-name < template-argument-list [opt] >
7875 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7876 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7877 returned. Otherwise, if the template-name names a function, or set
7878 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7879 names a class, returns a TYPE_DECL for the specialization.
7881 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7882 uninstantiated templates. */
7885 cp_parser_template_id (cp_parser
*parser
,
7886 bool template_keyword_p
,
7887 bool check_dependency_p
)
7892 tree saved_qualifying_scope
;
7893 tree saved_object_scope
;
7895 bool saved_greater_than_is_operator_p
;
7896 ptrdiff_t start_of_id
;
7897 tree access_check
= NULL_TREE
;
7898 cp_token
*next_token
;
7900 /* If the next token corresponds to a template-id, there is no need
7902 next_token
= cp_lexer_peek_token (parser
->lexer
);
7903 if (next_token
->type
== CPP_TEMPLATE_ID
)
7908 /* Get the stored value. */
7909 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
7910 /* Perform any access checks that were deferred. */
7911 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
7912 perform_or_defer_access_check (TREE_PURPOSE (check
),
7913 TREE_VALUE (check
));
7914 /* Return the stored value. */
7915 return TREE_VALUE (value
);
7918 /* Avoid performing name lookup if there is no possibility of
7919 finding a template-id. */
7920 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
7921 || (next_token
->type
== CPP_NAME
7922 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
))
7924 cp_parser_error (parser
, "expected template-id");
7925 return error_mark_node
;
7928 /* Remember where the template-id starts. */
7929 if (cp_parser_parsing_tentatively (parser
)
7930 && !cp_parser_committed_to_tentative_parse (parser
))
7932 next_token
= cp_lexer_peek_token (parser
->lexer
);
7933 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
7934 parser
->lexer
->first_token
,
7940 push_deferring_access_checks (dk_deferred
);
7942 /* Parse the template-name. */
7943 template = cp_parser_template_name (parser
, template_keyword_p
,
7944 check_dependency_p
);
7945 if (template == error_mark_node
)
7947 pop_deferring_access_checks ();
7948 return error_mark_node
;
7951 /* Look for the `<' that starts the template-argument-list. */
7952 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
7954 pop_deferring_access_checks ();
7955 return error_mark_node
;
7960 When parsing a template-id, the first non-nested `>' is taken as
7961 the end of the template-argument-list rather than a greater-than
7963 saved_greater_than_is_operator_p
7964 = parser
->greater_than_is_operator_p
;
7965 parser
->greater_than_is_operator_p
= false;
7966 /* Parsing the argument list may modify SCOPE, so we save it
7968 saved_scope
= parser
->scope
;
7969 saved_qualifying_scope
= parser
->qualifying_scope
;
7970 saved_object_scope
= parser
->object_scope
;
7971 /* Parse the template-argument-list itself. */
7972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
7973 arguments
= NULL_TREE
;
7975 arguments
= cp_parser_template_argument_list (parser
);
7976 /* Look for the `>' that ends the template-argument-list. */
7977 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7978 /* The `>' token might be a greater-than operator again now. */
7979 parser
->greater_than_is_operator_p
7980 = saved_greater_than_is_operator_p
;
7981 /* Restore the SAVED_SCOPE. */
7982 parser
->scope
= saved_scope
;
7983 parser
->qualifying_scope
= saved_qualifying_scope
;
7984 parser
->object_scope
= saved_object_scope
;
7986 /* Build a representation of the specialization. */
7987 if (TREE_CODE (template) == IDENTIFIER_NODE
)
7988 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
7989 else if (DECL_CLASS_TEMPLATE_P (template)
7990 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7992 = finish_template_type (template, arguments
,
7993 cp_lexer_next_token_is (parser
->lexer
,
7997 /* If it's not a class-template or a template-template, it should be
7998 a function-template. */
7999 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8000 || TREE_CODE (template) == OVERLOAD
8001 || BASELINK_P (template)),
8004 template_id
= lookup_template_function (template, arguments
);
8007 /* Retrieve any deferred checks. Do not pop this access checks yet
8008 so the memory will not be reclaimed during token replacing below. */
8009 access_check
= get_deferred_access_checks ();
8011 /* If parsing tentatively, replace the sequence of tokens that makes
8012 up the template-id with a CPP_TEMPLATE_ID token. That way,
8013 should we re-parse the token stream, we will not have to repeat
8014 the effort required to do the parse, nor will we issue duplicate
8015 error messages about problems during instantiation of the
8017 if (start_of_id
>= 0)
8021 /* Find the token that corresponds to the start of the
8023 token
= cp_lexer_advance_token (parser
->lexer
,
8024 parser
->lexer
->first_token
,
8027 /* Reset the contents of the START_OF_ID token. */
8028 token
->type
= CPP_TEMPLATE_ID
;
8029 token
->value
= build_tree_list (access_check
, template_id
);
8030 token
->keyword
= RID_MAX
;
8031 /* Purge all subsequent tokens. */
8032 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
8035 pop_deferring_access_checks ();
8039 /* Parse a template-name.
8044 The standard should actually say:
8048 operator-function-id
8049 conversion-function-id
8051 A defect report has been filed about this issue.
8053 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8054 `template' keyword, in a construction like:
8058 In that case `f' is taken to be a template-name, even though there
8059 is no way of knowing for sure.
8061 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8062 name refers to a set of overloaded functions, at least one of which
8063 is a template, or an IDENTIFIER_NODE with the name of the template,
8064 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8065 names are looked up inside uninstantiated templates. */
8068 cp_parser_template_name (cp_parser
* parser
,
8069 bool template_keyword_p
,
8070 bool check_dependency_p
)
8076 /* If the next token is `operator', then we have either an
8077 operator-function-id or a conversion-function-id. */
8078 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8080 /* We don't know whether we're looking at an
8081 operator-function-id or a conversion-function-id. */
8082 cp_parser_parse_tentatively (parser
);
8083 /* Try an operator-function-id. */
8084 identifier
= cp_parser_operator_function_id (parser
);
8085 /* If that didn't work, try a conversion-function-id. */
8086 if (!cp_parser_parse_definitely (parser
))
8087 identifier
= cp_parser_conversion_function_id (parser
);
8089 /* Look for the identifier. */
8091 identifier
= cp_parser_identifier (parser
);
8093 /* If we didn't find an identifier, we don't have a template-id. */
8094 if (identifier
== error_mark_node
)
8095 return error_mark_node
;
8097 /* If the name immediately followed the `template' keyword, then it
8098 is a template-name. However, if the next token is not `<', then
8099 we do not treat it as a template-name, since it is not being used
8100 as part of a template-id. This enables us to handle constructs
8103 template <typename T> struct S { S(); };
8104 template <typename T> S<T>::S();
8106 correctly. We would treat `S' as a template -- if it were `S<T>'
8107 -- but we do not if there is no `<'. */
8108 if (template_keyword_p
&& processing_template_decl
8109 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
8112 /* Look up the name. */
8113 decl
= cp_parser_lookup_name (parser
, identifier
,
8115 /*is_namespace=*/false,
8116 check_dependency_p
);
8117 decl
= maybe_get_template_decl_from_type_decl (decl
);
8119 /* If DECL is a template, then the name was a template-name. */
8120 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8124 /* The standard does not explicitly indicate whether a name that
8125 names a set of overloaded declarations, some of which are
8126 templates, is a template-name. However, such a name should
8127 be a template-name; otherwise, there is no way to form a
8128 template-id for the overloaded templates. */
8129 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8130 if (TREE_CODE (fns
) == OVERLOAD
)
8134 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8135 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8140 /* Otherwise, the name does not name a template. */
8141 cp_parser_error (parser
, "expected template-name");
8142 return error_mark_node
;
8146 /* If DECL is dependent, and refers to a function, then just return
8147 its name; we will look it up again during template instantiation. */
8148 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8150 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8151 if (TYPE_P (scope
) && dependent_type_p (scope
))
8158 /* Parse a template-argument-list.
8160 template-argument-list:
8162 template-argument-list , template-argument
8164 Returns a TREE_LIST representing the arguments, in the order they
8165 appeared. The TREE_VALUE of each node is a representation of the
8169 cp_parser_template_argument_list (cp_parser
* parser
)
8171 tree arguments
= NULL_TREE
;
8177 /* Parse the template-argument. */
8178 argument
= cp_parser_template_argument (parser
);
8179 /* Add it to the list. */
8180 arguments
= tree_cons (NULL_TREE
, argument
, arguments
);
8181 /* If it is not a `,', then there are no more arguments. */
8182 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8184 /* Otherwise, consume the ','. */
8185 cp_lexer_consume_token (parser
->lexer
);
8188 /* We built up the arguments in reverse order. */
8189 return nreverse (arguments
);
8192 /* Parse a template-argument.
8195 assignment-expression
8199 The representation is that of an assignment-expression, type-id, or
8200 id-expression -- except that the qualified id-expression is
8201 evaluated, so that the value returned is either a DECL or an
8205 cp_parser_template_argument (cp_parser
* parser
)
8210 /* There's really no way to know what we're looking at, so we just
8211 try each alternative in order.
8215 In a template-argument, an ambiguity between a type-id and an
8216 expression is resolved to a type-id, regardless of the form of
8217 the corresponding template-parameter.
8219 Therefore, we try a type-id first. */
8220 cp_parser_parse_tentatively (parser
);
8221 argument
= cp_parser_type_id (parser
);
8222 /* If the next token isn't a `,' or a `>', then this argument wasn't
8224 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
8225 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
))
8226 cp_parser_error (parser
, "expected template-argument");
8227 /* If that worked, we're done. */
8228 if (cp_parser_parse_definitely (parser
))
8230 /* We're still not sure what the argument will be. */
8231 cp_parser_parse_tentatively (parser
);
8232 /* Try a template. */
8233 argument
= cp_parser_id_expression (parser
,
8234 /*template_keyword_p=*/false,
8235 /*check_dependency_p=*/true,
8237 /* If the next token isn't a `,' or a `>', then this argument wasn't
8239 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
8240 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
))
8241 cp_parser_error (parser
, "expected template-argument");
8242 if (!cp_parser_error_occurred (parser
))
8244 /* Figure out what is being referred to. */
8245 argument
= cp_parser_lookup_name_simple (parser
, argument
);
8247 argument
= make_unbound_class_template (TREE_OPERAND (argument
, 0),
8248 TREE_OPERAND (argument
, 1),
8250 else if (TREE_CODE (argument
) != TEMPLATE_DECL
)
8251 cp_parser_error (parser
, "expected template-name");
8253 if (cp_parser_parse_definitely (parser
))
8255 /* It must be an assignment-expression. */
8256 return cp_parser_assignment_expression (parser
);
8259 /* Parse an explicit-instantiation.
8261 explicit-instantiation:
8262 template declaration
8264 Although the standard says `declaration', what it really means is:
8266 explicit-instantiation:
8267 template decl-specifier-seq [opt] declarator [opt] ;
8269 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8270 supposed to be allowed. A defect report has been filed about this
8275 explicit-instantiation:
8276 storage-class-specifier template
8277 decl-specifier-seq [opt] declarator [opt] ;
8278 function-specifier template
8279 decl-specifier-seq [opt] declarator [opt] ; */
8282 cp_parser_explicit_instantiation (cp_parser
* parser
)
8284 bool declares_class_or_enum
;
8285 tree decl_specifiers
;
8287 tree extension_specifier
= NULL_TREE
;
8289 /* Look for an (optional) storage-class-specifier or
8290 function-specifier. */
8291 if (cp_parser_allow_gnu_extensions_p (parser
))
8294 = cp_parser_storage_class_specifier_opt (parser
);
8295 if (!extension_specifier
)
8296 extension_specifier
= cp_parser_function_specifier_opt (parser
);
8299 /* Look for the `template' keyword. */
8300 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8301 /* Let the front end know that we are processing an explicit
8303 begin_explicit_instantiation ();
8304 /* [temp.explicit] says that we are supposed to ignore access
8305 control while processing explicit instantiation directives. */
8306 push_deferring_access_checks (dk_no_check
);
8307 /* Parse a decl-specifier-seq. */
8309 = cp_parser_decl_specifier_seq (parser
,
8310 CP_PARSER_FLAGS_OPTIONAL
,
8312 &declares_class_or_enum
);
8313 /* If there was exactly one decl-specifier, and it declared a class,
8314 and there's no declarator, then we have an explicit type
8316 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8320 type
= check_tag_decl (decl_specifiers
);
8321 /* Turn access control back on for names used during
8322 template instantiation. */
8323 pop_deferring_access_checks ();
8325 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8332 /* Parse the declarator. */
8334 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8335 /*ctor_dtor_or_conv_p=*/NULL
);
8336 decl
= grokdeclarator (declarator
, decl_specifiers
,
8338 /* Turn access control back on for names used during
8339 template instantiation. */
8340 pop_deferring_access_checks ();
8341 /* Do the explicit instantiation. */
8342 do_decl_instantiation (decl
, extension_specifier
);
8344 /* We're done with the instantiation. */
8345 end_explicit_instantiation ();
8347 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8350 /* Parse an explicit-specialization.
8352 explicit-specialization:
8353 template < > declaration
8355 Although the standard says `declaration', what it really means is:
8357 explicit-specialization:
8358 template <> decl-specifier [opt] init-declarator [opt] ;
8359 template <> function-definition
8360 template <> explicit-specialization
8361 template <> template-declaration */
8364 cp_parser_explicit_specialization (cp_parser
* parser
)
8366 /* Look for the `template' keyword. */
8367 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8368 /* Look for the `<'. */
8369 cp_parser_require (parser
, CPP_LESS
, "`<'");
8370 /* Look for the `>'. */
8371 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8372 /* We have processed another parameter list. */
8373 ++parser
->num_template_parameter_lists
;
8374 /* Let the front end know that we are beginning a specialization. */
8375 begin_specialization ();
8377 /* If the next keyword is `template', we need to figure out whether
8378 or not we're looking a template-declaration. */
8379 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8381 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8382 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8383 cp_parser_template_declaration_after_export (parser
,
8384 /*member_p=*/false);
8386 cp_parser_explicit_specialization (parser
);
8389 /* Parse the dependent declaration. */
8390 cp_parser_single_declaration (parser
,
8394 /* We're done with the specialization. */
8395 end_specialization ();
8396 /* We're done with this parameter list. */
8397 --parser
->num_template_parameter_lists
;
8400 /* Parse a type-specifier.
8403 simple-type-specifier
8406 elaborated-type-specifier
8414 Returns a representation of the type-specifier. If the
8415 type-specifier is a keyword (like `int' or `const', or
8416 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8417 For a class-specifier, enum-specifier, or elaborated-type-specifier
8418 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8420 If IS_FRIEND is TRUE then this type-specifier is being declared a
8421 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8422 appearing in a decl-specifier-seq.
8424 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8425 class-specifier, enum-specifier, or elaborated-type-specifier, then
8426 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8429 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8430 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8434 cp_parser_type_specifier (cp_parser
* parser
,
8435 cp_parser_flags flags
,
8437 bool is_declaration
,
8438 bool* declares_class_or_enum
,
8439 bool* is_cv_qualifier
)
8441 tree type_spec
= NULL_TREE
;
8445 /* Assume this type-specifier does not declare a new type. */
8446 if (declares_class_or_enum
)
8447 *declares_class_or_enum
= false;
8448 /* And that it does not specify a cv-qualifier. */
8449 if (is_cv_qualifier
)
8450 *is_cv_qualifier
= false;
8451 /* Peek at the next token. */
8452 token
= cp_lexer_peek_token (parser
->lexer
);
8454 /* If we're looking at a keyword, we can use that to guide the
8455 production we choose. */
8456 keyword
= token
->keyword
;
8459 /* Any of these indicate either a class-specifier, or an
8460 elaborated-type-specifier. */
8465 /* Parse tentatively so that we can back up if we don't find a
8466 class-specifier or enum-specifier. */
8467 cp_parser_parse_tentatively (parser
);
8468 /* Look for the class-specifier or enum-specifier. */
8469 if (keyword
== RID_ENUM
)
8470 type_spec
= cp_parser_enum_specifier (parser
);
8472 type_spec
= cp_parser_class_specifier (parser
);
8474 /* If that worked, we're done. */
8475 if (cp_parser_parse_definitely (parser
))
8477 if (declares_class_or_enum
)
8478 *declares_class_or_enum
= true;
8485 /* Look for an elaborated-type-specifier. */
8486 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8489 /* We're declaring a class or enum -- unless we're using
8491 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8492 *declares_class_or_enum
= true;
8498 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8499 /* Even though we call a routine that looks for an optional
8500 qualifier, we know that there should be one. */
8501 my_friendly_assert (type_spec
!= NULL
, 20000328);
8502 /* This type-specifier was a cv-qualified. */
8503 if (is_cv_qualifier
)
8504 *is_cv_qualifier
= true;
8509 /* The `__complex__' keyword is a GNU extension. */
8510 return cp_lexer_consume_token (parser
->lexer
)->value
;
8516 /* If we do not already have a type-specifier, assume we are looking
8517 at a simple-type-specifier. */
8518 type_spec
= cp_parser_simple_type_specifier (parser
, flags
);
8520 /* If we didn't find a type-specifier, and a type-specifier was not
8521 optional in this context, issue an error message. */
8522 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8524 cp_parser_error (parser
, "expected type specifier");
8525 return error_mark_node
;
8531 /* Parse a simple-type-specifier.
8533 simple-type-specifier:
8534 :: [opt] nested-name-specifier [opt] type-name
8535 :: [opt] nested-name-specifier template template-id
8550 simple-type-specifier:
8551 __typeof__ unary-expression
8552 __typeof__ ( type-id )
8554 For the various keywords, the value returned is simply the
8555 TREE_IDENTIFIER representing the keyword. For the first two
8556 productions, the value returned is the indicated TYPE_DECL. */
8559 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
)
8561 tree type
= NULL_TREE
;
8564 /* Peek at the next token. */
8565 token
= cp_lexer_peek_token (parser
->lexer
);
8567 /* If we're looking at a keyword, things are easy. */
8568 switch (token
->keyword
)
8581 /* Consume the token. */
8582 return cp_lexer_consume_token (parser
->lexer
)->value
;
8588 /* Consume the `typeof' token. */
8589 cp_lexer_consume_token (parser
->lexer
);
8590 /* Parse the operand to `typeof' */
8591 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
8592 /* If it is not already a TYPE, take its type. */
8593 if (!TYPE_P (operand
))
8594 operand
= finish_typeof (operand
);
8603 /* The type-specifier must be a user-defined type. */
8604 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
8606 /* Don't gobble tokens or issue error messages if this is an
8607 optional type-specifier. */
8608 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
8609 cp_parser_parse_tentatively (parser
);
8611 /* Look for the optional `::' operator. */
8612 cp_parser_global_scope_opt (parser
,
8613 /*current_scope_valid_p=*/false);
8614 /* Look for the nested-name specifier. */
8615 cp_parser_nested_name_specifier_opt (parser
,
8616 /*typename_keyword_p=*/false,
8617 /*check_dependency_p=*/true,
8619 /* If we have seen a nested-name-specifier, and the next token
8620 is `template', then we are using the template-id production. */
8622 && cp_parser_optional_template_keyword (parser
))
8624 /* Look for the template-id. */
8625 type
= cp_parser_template_id (parser
,
8626 /*template_keyword_p=*/true,
8627 /*check_dependency_p=*/true);
8628 /* If the template-id did not name a type, we are out of
8630 if (TREE_CODE (type
) != TYPE_DECL
)
8632 cp_parser_error (parser
, "expected template-id for type");
8636 /* Otherwise, look for a type-name. */
8639 type
= cp_parser_type_name (parser
);
8640 if (type
== error_mark_node
)
8644 /* If it didn't work out, we don't have a TYPE. */
8645 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
8646 && !cp_parser_parse_definitely (parser
))
8650 /* If we didn't get a type-name, issue an error message. */
8651 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8653 cp_parser_error (parser
, "expected type-name");
8654 return error_mark_node
;
8660 /* Parse a type-name.
8673 Returns a TYPE_DECL for the the type. */
8676 cp_parser_type_name (cp_parser
* parser
)
8681 /* We can't know yet whether it is a class-name or not. */
8682 cp_parser_parse_tentatively (parser
);
8683 /* Try a class-name. */
8684 type_decl
= cp_parser_class_name (parser
,
8685 /*typename_keyword_p=*/false,
8686 /*template_keyword_p=*/false,
8688 /*check_dependency_p=*/true,
8689 /*class_head_p=*/false);
8690 /* If it's not a class-name, keep looking. */
8691 if (!cp_parser_parse_definitely (parser
))
8693 /* It must be a typedef-name or an enum-name. */
8694 identifier
= cp_parser_identifier (parser
);
8695 if (identifier
== error_mark_node
)
8696 return error_mark_node
;
8698 /* Look up the type-name. */
8699 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
8700 /* Issue an error if we did not find a type-name. */
8701 if (TREE_CODE (type_decl
) != TYPE_DECL
)
8703 cp_parser_error (parser
, "expected type-name");
8704 type_decl
= error_mark_node
;
8706 /* Remember that the name was used in the definition of the
8707 current class so that we can check later to see if the
8708 meaning would have been different after the class was
8709 entirely defined. */
8710 else if (type_decl
!= error_mark_node
8712 maybe_note_name_used_in_class (identifier
, type_decl
);
8719 /* Parse an elaborated-type-specifier. Note that the grammar given
8720 here incorporates the resolution to DR68.
8722 elaborated-type-specifier:
8723 class-key :: [opt] nested-name-specifier [opt] identifier
8724 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8725 enum :: [opt] nested-name-specifier [opt] identifier
8726 typename :: [opt] nested-name-specifier identifier
8727 typename :: [opt] nested-name-specifier template [opt]
8730 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8731 declared `friend'. If IS_DECLARATION is TRUE, then this
8732 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8733 something is being declared.
8735 Returns the TYPE specified. */
8738 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
8740 bool is_declaration
)
8742 enum tag_types tag_type
;
8744 tree type
= NULL_TREE
;
8746 /* See if we're looking at the `enum' keyword. */
8747 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
8749 /* Consume the `enum' token. */
8750 cp_lexer_consume_token (parser
->lexer
);
8751 /* Remember that it's an enumeration type. */
8752 tag_type
= enum_type
;
8754 /* Or, it might be `typename'. */
8755 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8758 /* Consume the `typename' token. */
8759 cp_lexer_consume_token (parser
->lexer
);
8760 /* Remember that it's a `typename' type. */
8761 tag_type
= typename_type
;
8762 /* The `typename' keyword is only allowed in templates. */
8763 if (!processing_template_decl
)
8764 pedwarn ("using `typename' outside of template");
8766 /* Otherwise it must be a class-key. */
8769 tag_type
= cp_parser_class_key (parser
);
8770 if (tag_type
== none_type
)
8771 return error_mark_node
;
8774 /* Look for the `::' operator. */
8775 cp_parser_global_scope_opt (parser
,
8776 /*current_scope_valid_p=*/false);
8777 /* Look for the nested-name-specifier. */
8778 if (tag_type
== typename_type
)
8780 if (cp_parser_nested_name_specifier (parser
,
8781 /*typename_keyword_p=*/true,
8782 /*check_dependency_p=*/true,
8785 return error_mark_node
;
8788 /* Even though `typename' is not present, the proposed resolution
8789 to Core Issue 180 says that in `class A<T>::B', `B' should be
8790 considered a type-name, even if `A<T>' is dependent. */
8791 cp_parser_nested_name_specifier_opt (parser
,
8792 /*typename_keyword_p=*/true,
8793 /*check_dependency_p=*/true,
8795 /* For everything but enumeration types, consider a template-id. */
8796 if (tag_type
!= enum_type
)
8798 bool template_p
= false;
8801 /* Allow the `template' keyword. */
8802 template_p
= cp_parser_optional_template_keyword (parser
);
8803 /* If we didn't see `template', we don't know if there's a
8804 template-id or not. */
8806 cp_parser_parse_tentatively (parser
);
8807 /* Parse the template-id. */
8808 decl
= cp_parser_template_id (parser
, template_p
,
8809 /*check_dependency_p=*/true);
8810 /* If we didn't find a template-id, look for an ordinary
8812 if (!template_p
&& !cp_parser_parse_definitely (parser
))
8814 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8815 in effect, then we must assume that, upon instantiation, the
8816 template will correspond to a class. */
8817 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
8818 && tag_type
== typename_type
)
8819 type
= make_typename_type (parser
->scope
, decl
,
8822 type
= TREE_TYPE (decl
);
8825 /* For an enumeration type, consider only a plain identifier. */
8828 identifier
= cp_parser_identifier (parser
);
8830 if (identifier
== error_mark_node
)
8831 return error_mark_node
;
8833 /* For a `typename', we needn't call xref_tag. */
8834 if (tag_type
== typename_type
)
8835 return make_typename_type (parser
->scope
, identifier
,
8837 /* Look up a qualified name in the usual way. */
8842 /* In an elaborated-type-specifier, names are assumed to name
8843 types, so we set IS_TYPE to TRUE when calling
8844 cp_parser_lookup_name. */
8845 decl
= cp_parser_lookup_name (parser
, identifier
,
8847 /*is_namespace=*/false,
8848 /*check_dependency=*/true);
8850 /* If we are parsing friend declaration, DECL may be a
8851 TEMPLATE_DECL tree node here. However, we need to check
8852 whether this TEMPLATE_DECL results in valid code. Consider
8853 the following example:
8856 template <class T> class C {};
8859 template <class T> friend class N::C; // #1, valid code
8861 template <class T> class Y {
8862 friend class N::C; // #2, invalid code
8865 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8866 name lookup of `N::C'. We see that friend declaration must
8867 be template for the code to be valid. Note that
8868 processing_template_decl does not work here since it is
8869 always 1 for the above two cases. */
8871 decl
= (cp_parser_maybe_treat_template_as_class
8872 (decl
, /*tag_name_p=*/is_friend
8873 && parser
->num_template_parameter_lists
));
8875 if (TREE_CODE (decl
) != TYPE_DECL
)
8877 error ("expected type-name");
8878 return error_mark_node
;
8880 else if (TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
8881 && tag_type
!= enum_type
)
8882 error ("`%T' referred to as `%s'", TREE_TYPE (decl
),
8883 tag_type
== record_type
? "struct" : "class");
8884 else if (TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
8885 && tag_type
== enum_type
)
8886 error ("`%T' referred to as enum", TREE_TYPE (decl
));
8888 type
= TREE_TYPE (decl
);
8892 /* An elaborated-type-specifier sometimes introduces a new type and
8893 sometimes names an existing type. Normally, the rule is that it
8894 introduces a new type only if there is not an existing type of
8895 the same name already in scope. For example, given:
8898 void f() { struct S s; }
8900 the `struct S' in the body of `f' is the same `struct S' as in
8901 the global scope; the existing definition is used. However, if
8902 there were no global declaration, this would introduce a new
8903 local class named `S'.
8905 An exception to this rule applies to the following code:
8907 namespace N { struct S; }
8909 Here, the elaborated-type-specifier names a new type
8910 unconditionally; even if there is already an `S' in the
8911 containing scope this declaration names a new type.
8912 This exception only applies if the elaborated-type-specifier
8913 forms the complete declaration:
8917 A declaration consisting solely of `class-key identifier ;' is
8918 either a redeclaration of the name in the current scope or a
8919 forward declaration of the identifier as a class name. It
8920 introduces the name into the current scope.
8922 We are in this situation precisely when the next token is a `;'.
8924 An exception to the exception is that a `friend' declaration does
8925 *not* name a new type; i.e., given:
8927 struct S { friend struct T; };
8929 `T' is not a new type in the scope of `S'.
8931 Also, `new struct S' or `sizeof (struct S)' never results in the
8932 definition of a new type; a new type can only be declared in a
8933 declaration context. */
8935 type
= xref_tag (tag_type
, identifier
,
8936 /*attributes=*/NULL_TREE
,
8939 || cp_lexer_next_token_is_not (parser
->lexer
,
8943 if (tag_type
!= enum_type
)
8944 cp_parser_check_class_key (tag_type
, type
);
8948 /* Parse an enum-specifier.
8951 enum identifier [opt] { enumerator-list [opt] }
8953 Returns an ENUM_TYPE representing the enumeration. */
8956 cp_parser_enum_specifier (cp_parser
* parser
)
8959 tree identifier
= NULL_TREE
;
8962 /* Look for the `enum' keyword. */
8963 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
8964 return error_mark_node
;
8965 /* Peek at the next token. */
8966 token
= cp_lexer_peek_token (parser
->lexer
);
8968 /* See if it is an identifier. */
8969 if (token
->type
== CPP_NAME
)
8970 identifier
= cp_parser_identifier (parser
);
8972 /* Look for the `{'. */
8973 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
8974 return error_mark_node
;
8976 /* At this point, we're going ahead with the enum-specifier, even
8977 if some other problem occurs. */
8978 cp_parser_commit_to_tentative_parse (parser
);
8980 /* Issue an error message if type-definitions are forbidden here. */
8981 cp_parser_check_type_definition (parser
);
8983 /* Create the new type. */
8984 type
= start_enum (identifier
? identifier
: make_anon_name ());
8986 /* Peek at the next token. */
8987 token
= cp_lexer_peek_token (parser
->lexer
);
8988 /* If it's not a `}', then there are some enumerators. */
8989 if (token
->type
!= CPP_CLOSE_BRACE
)
8990 cp_parser_enumerator_list (parser
, type
);
8991 /* Look for the `}'. */
8992 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8994 /* Finish up the enumeration. */
9000 /* Parse an enumerator-list. The enumerators all have the indicated
9004 enumerator-definition
9005 enumerator-list , enumerator-definition */
9008 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9014 /* Parse an enumerator-definition. */
9015 cp_parser_enumerator_definition (parser
, type
);
9016 /* Peek at the next token. */
9017 token
= cp_lexer_peek_token (parser
->lexer
);
9018 /* If it's not a `,', then we've reached the end of the
9020 if (token
->type
!= CPP_COMMA
)
9022 /* Otherwise, consume the `,' and keep going. */
9023 cp_lexer_consume_token (parser
->lexer
);
9024 /* If the next token is a `}', there is a trailing comma. */
9025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9027 if (pedantic
&& !in_system_header
)
9028 pedwarn ("comma at end of enumerator list");
9034 /* Parse an enumerator-definition. The enumerator has the indicated
9037 enumerator-definition:
9039 enumerator = constant-expression
9045 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9051 /* Look for the identifier. */
9052 identifier
= cp_parser_identifier (parser
);
9053 if (identifier
== error_mark_node
)
9056 /* Peek at the next token. */
9057 token
= cp_lexer_peek_token (parser
->lexer
);
9058 /* If it's an `=', then there's an explicit value. */
9059 if (token
->type
== CPP_EQ
)
9061 /* Consume the `=' token. */
9062 cp_lexer_consume_token (parser
->lexer
);
9063 /* Parse the value. */
9064 value
= cp_parser_constant_expression (parser
,
9065 /*allow_non_constant=*/false,
9071 /* Create the enumerator. */
9072 build_enumerator (identifier
, value
, type
);
9075 /* Parse a namespace-name.
9078 original-namespace-name
9081 Returns the NAMESPACE_DECL for the namespace. */
9084 cp_parser_namespace_name (cp_parser
* parser
)
9087 tree namespace_decl
;
9089 /* Get the name of the namespace. */
9090 identifier
= cp_parser_identifier (parser
);
9091 if (identifier
== error_mark_node
)
9092 return error_mark_node
;
9094 /* Look up the identifier in the currently active scope. Look only
9095 for namespaces, due to:
9099 When looking up a namespace-name in a using-directive or alias
9100 definition, only namespace names are considered.
9106 During the lookup of a name preceding the :: scope resolution
9107 operator, object, function, and enumerator names are ignored.
9109 (Note that cp_parser_class_or_namespace_name only calls this
9110 function if the token after the name is the scope resolution
9112 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
9114 /*is_namespace=*/true,
9115 /*check_dependency=*/true);
9116 /* If it's not a namespace, issue an error. */
9117 if (namespace_decl
== error_mark_node
9118 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
9120 cp_parser_error (parser
, "expected namespace-name");
9121 namespace_decl
= error_mark_node
;
9124 return namespace_decl
;
9127 /* Parse a namespace-definition.
9129 namespace-definition:
9130 named-namespace-definition
9131 unnamed-namespace-definition
9133 named-namespace-definition:
9134 original-namespace-definition
9135 extension-namespace-definition
9137 original-namespace-definition:
9138 namespace identifier { namespace-body }
9140 extension-namespace-definition:
9141 namespace original-namespace-name { namespace-body }
9143 unnamed-namespace-definition:
9144 namespace { namespace-body } */
9147 cp_parser_namespace_definition (cp_parser
* parser
)
9151 /* Look for the `namespace' keyword. */
9152 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9154 /* Get the name of the namespace. We do not attempt to distinguish
9155 between an original-namespace-definition and an
9156 extension-namespace-definition at this point. The semantic
9157 analysis routines are responsible for that. */
9158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9159 identifier
= cp_parser_identifier (parser
);
9161 identifier
= NULL_TREE
;
9163 /* Look for the `{' to start the namespace. */
9164 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
9165 /* Start the namespace. */
9166 push_namespace (identifier
);
9167 /* Parse the body of the namespace. */
9168 cp_parser_namespace_body (parser
);
9169 /* Finish the namespace. */
9171 /* Look for the final `}'. */
9172 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9175 /* Parse a namespace-body.
9178 declaration-seq [opt] */
9181 cp_parser_namespace_body (cp_parser
* parser
)
9183 cp_parser_declaration_seq_opt (parser
);
9186 /* Parse a namespace-alias-definition.
9188 namespace-alias-definition:
9189 namespace identifier = qualified-namespace-specifier ; */
9192 cp_parser_namespace_alias_definition (cp_parser
* parser
)
9195 tree namespace_specifier
;
9197 /* Look for the `namespace' keyword. */
9198 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9199 /* Look for the identifier. */
9200 identifier
= cp_parser_identifier (parser
);
9201 if (identifier
== error_mark_node
)
9203 /* Look for the `=' token. */
9204 cp_parser_require (parser
, CPP_EQ
, "`='");
9205 /* Look for the qualified-namespace-specifier. */
9207 = cp_parser_qualified_namespace_specifier (parser
);
9208 /* Look for the `;' token. */
9209 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9211 /* Register the alias in the symbol table. */
9212 do_namespace_alias (identifier
, namespace_specifier
);
9215 /* Parse a qualified-namespace-specifier.
9217 qualified-namespace-specifier:
9218 :: [opt] nested-name-specifier [opt] namespace-name
9220 Returns a NAMESPACE_DECL corresponding to the specified
9224 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
9226 /* Look for the optional `::'. */
9227 cp_parser_global_scope_opt (parser
,
9228 /*current_scope_valid_p=*/false);
9230 /* Look for the optional nested-name-specifier. */
9231 cp_parser_nested_name_specifier_opt (parser
,
9232 /*typename_keyword_p=*/false,
9233 /*check_dependency_p=*/true,
9236 return cp_parser_namespace_name (parser
);
9239 /* Parse a using-declaration.
9242 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9243 using :: unqualified-id ; */
9246 cp_parser_using_declaration (cp_parser
* parser
)
9249 bool typename_p
= false;
9250 bool global_scope_p
;
9255 /* Look for the `using' keyword. */
9256 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9258 /* Peek at the next token. */
9259 token
= cp_lexer_peek_token (parser
->lexer
);
9260 /* See if it's `typename'. */
9261 if (token
->keyword
== RID_TYPENAME
)
9263 /* Remember that we've seen it. */
9265 /* Consume the `typename' token. */
9266 cp_lexer_consume_token (parser
->lexer
);
9269 /* Look for the optional global scope qualification. */
9271 = (cp_parser_global_scope_opt (parser
,
9272 /*current_scope_valid_p=*/false)
9275 /* If we saw `typename', or didn't see `::', then there must be a
9276 nested-name-specifier present. */
9277 if (typename_p
|| !global_scope_p
)
9278 cp_parser_nested_name_specifier (parser
, typename_p
,
9279 /*check_dependency_p=*/true,
9281 /* Otherwise, we could be in either of the two productions. In that
9282 case, treat the nested-name-specifier as optional. */
9284 cp_parser_nested_name_specifier_opt (parser
,
9285 /*typename_keyword_p=*/false,
9286 /*check_dependency_p=*/true,
9289 /* Parse the unqualified-id. */
9290 identifier
= cp_parser_unqualified_id (parser
,
9291 /*template_keyword_p=*/false,
9292 /*check_dependency_p=*/true);
9294 /* The function we call to handle a using-declaration is different
9295 depending on what scope we are in. */
9296 scope
= current_scope ();
9297 if (scope
&& TYPE_P (scope
))
9299 /* Create the USING_DECL. */
9300 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9303 /* Add it to the list of members in this class. */
9304 finish_member_declaration (decl
);
9308 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9309 if (decl
== error_mark_node
)
9311 if (parser
->scope
&& parser
->scope
!= global_namespace
)
9312 error ("`%D::%D' has not been declared",
9313 parser
->scope
, identifier
);
9315 error ("`::%D' has not been declared", identifier
);
9318 do_local_using_decl (decl
);
9320 do_toplevel_using_decl (decl
);
9323 /* Look for the final `;'. */
9324 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9327 /* Parse a using-directive.
9330 using namespace :: [opt] nested-name-specifier [opt]
9334 cp_parser_using_directive (cp_parser
* parser
)
9336 tree namespace_decl
;
9338 /* Look for the `using' keyword. */
9339 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9340 /* And the `namespace' keyword. */
9341 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9342 /* Look for the optional `::' operator. */
9343 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9344 /* And the optional nested-name-sepcifier. */
9345 cp_parser_nested_name_specifier_opt (parser
,
9346 /*typename_keyword_p=*/false,
9347 /*check_dependency_p=*/true,
9349 /* Get the namespace being used. */
9350 namespace_decl
= cp_parser_namespace_name (parser
);
9351 /* Update the symbol table. */
9352 do_using_directive (namespace_decl
);
9353 /* Look for the final `;'. */
9354 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9357 /* Parse an asm-definition.
9360 asm ( string-literal ) ;
9365 asm volatile [opt] ( string-literal ) ;
9366 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9367 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9368 : asm-operand-list [opt] ) ;
9369 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9370 : asm-operand-list [opt]
9371 : asm-operand-list [opt] ) ; */
9374 cp_parser_asm_definition (cp_parser
* parser
)
9378 tree outputs
= NULL_TREE
;
9379 tree inputs
= NULL_TREE
;
9380 tree clobbers
= NULL_TREE
;
9382 bool volatile_p
= false;
9383 bool extended_p
= false;
9385 /* Look for the `asm' keyword. */
9386 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9387 /* See if the next token is `volatile'. */
9388 if (cp_parser_allow_gnu_extensions_p (parser
)
9389 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9391 /* Remember that we saw the `volatile' keyword. */
9393 /* Consume the token. */
9394 cp_lexer_consume_token (parser
->lexer
);
9396 /* Look for the opening `('. */
9397 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9398 /* Look for the string. */
9399 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9402 string
= token
->value
;
9403 /* If we're allowing GNU extensions, check for the extended assembly
9404 syntax. Unfortunately, the `:' tokens need not be separated by
9405 a space in C, and so, for compatibility, we tolerate that here
9406 too. Doing that means that we have to treat the `::' operator as
9408 if (cp_parser_allow_gnu_extensions_p (parser
)
9409 && at_function_scope_p ()
9410 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9411 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9413 bool inputs_p
= false;
9414 bool clobbers_p
= false;
9416 /* The extended syntax was used. */
9419 /* Look for outputs. */
9420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9422 /* Consume the `:'. */
9423 cp_lexer_consume_token (parser
->lexer
);
9424 /* Parse the output-operands. */
9425 if (cp_lexer_next_token_is_not (parser
->lexer
,
9427 && cp_lexer_next_token_is_not (parser
->lexer
,
9429 && cp_lexer_next_token_is_not (parser
->lexer
,
9431 outputs
= cp_parser_asm_operand_list (parser
);
9433 /* If the next token is `::', there are no outputs, and the
9434 next token is the beginning of the inputs. */
9435 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9437 /* Consume the `::' token. */
9438 cp_lexer_consume_token (parser
->lexer
);
9439 /* The inputs are coming next. */
9443 /* Look for inputs. */
9445 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9448 /* Consume the `:'. */
9449 cp_lexer_consume_token (parser
->lexer
);
9450 /* Parse the output-operands. */
9451 if (cp_lexer_next_token_is_not (parser
->lexer
,
9453 && cp_lexer_next_token_is_not (parser
->lexer
,
9455 && cp_lexer_next_token_is_not (parser
->lexer
,
9457 inputs
= cp_parser_asm_operand_list (parser
);
9459 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9460 /* The clobbers are coming next. */
9463 /* Look for clobbers. */
9465 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9468 /* Consume the `:'. */
9469 cp_lexer_consume_token (parser
->lexer
);
9470 /* Parse the clobbers. */
9471 if (cp_lexer_next_token_is_not (parser
->lexer
,
9473 clobbers
= cp_parser_asm_clobber_list (parser
);
9476 /* Look for the closing `)'. */
9477 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9478 cp_parser_skip_to_closing_parenthesis (parser
);
9479 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9481 /* Create the ASM_STMT. */
9482 if (at_function_scope_p ())
9485 finish_asm_stmt (volatile_p
9486 ? ridpointers
[(int) RID_VOLATILE
] : NULL_TREE
,
9487 string
, outputs
, inputs
, clobbers
);
9488 /* If the extended syntax was not used, mark the ASM_STMT. */
9490 ASM_INPUT_P (asm_stmt
) = 1;
9493 assemble_asm (string
);
9496 /* Declarators [gram.dcl.decl] */
9498 /* Parse an init-declarator.
9501 declarator initializer [opt]
9506 declarator asm-specification [opt] attributes [opt] initializer [opt]
9508 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9509 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9510 then this declarator appears in a class scope. The new DECL created
9511 by this declarator is returned.
9513 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9514 for a function-definition here as well. If the declarator is a
9515 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9516 be TRUE upon return. By that point, the function-definition will
9517 have been completely parsed.
9519 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9523 cp_parser_init_declarator (cp_parser
* parser
,
9524 tree decl_specifiers
,
9525 tree prefix_attributes
,
9526 bool function_definition_allowed_p
,
9528 bool* function_definition_p
)
9533 tree asm_specification
;
9535 tree decl
= NULL_TREE
;
9537 bool is_initialized
;
9538 bool is_parenthesized_init
;
9539 bool ctor_dtor_or_conv_p
;
9542 /* Assume that this is not the declarator for a function
9544 if (function_definition_p
)
9545 *function_definition_p
= false;
9547 /* Defer access checks while parsing the declarator; we cannot know
9548 what names are accessible until we know what is being
9550 resume_deferring_access_checks ();
9552 /* Parse the declarator. */
9554 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9555 &ctor_dtor_or_conv_p
);
9556 /* Gather up the deferred checks. */
9557 stop_deferring_access_checks ();
9559 /* If the DECLARATOR was erroneous, there's no need to go
9561 if (declarator
== error_mark_node
)
9562 return error_mark_node
;
9564 /* Figure out what scope the entity declared by the DECLARATOR is
9565 located in. `grokdeclarator' sometimes changes the scope, so
9566 we compute it now. */
9567 scope
= get_scope_of_declarator (declarator
);
9569 /* If we're allowing GNU extensions, look for an asm-specification
9571 if (cp_parser_allow_gnu_extensions_p (parser
))
9573 /* Look for an asm-specification. */
9574 asm_specification
= cp_parser_asm_specification_opt (parser
);
9575 /* And attributes. */
9576 attributes
= cp_parser_attributes_opt (parser
);
9580 asm_specification
= NULL_TREE
;
9581 attributes
= NULL_TREE
;
9584 /* Peek at the next token. */
9585 token
= cp_lexer_peek_token (parser
->lexer
);
9586 /* Check to see if the token indicates the start of a
9587 function-definition. */
9588 if (cp_parser_token_starts_function_definition_p (token
))
9590 if (!function_definition_allowed_p
)
9592 /* If a function-definition should not appear here, issue an
9594 cp_parser_error (parser
,
9595 "a function-definition is not allowed here");
9596 return error_mark_node
;
9600 /* Neither attributes nor an asm-specification are allowed
9601 on a function-definition. */
9602 if (asm_specification
)
9603 error ("an asm-specification is not allowed on a function-definition");
9605 error ("attributes are not allowed on a function-definition");
9606 /* This is a function-definition. */
9607 *function_definition_p
= true;
9609 /* Parse the function definition. */
9610 decl
= (cp_parser_function_definition_from_specifiers_and_declarator
9611 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
9619 Only in function declarations for constructors, destructors, and
9620 type conversions can the decl-specifier-seq be omitted.
9622 We explicitly postpone this check past the point where we handle
9623 function-definitions because we tolerate function-definitions
9624 that are missing their return types in some modes. */
9625 if (!decl_specifiers
&& !ctor_dtor_or_conv_p
)
9627 cp_parser_error (parser
,
9628 "expected constructor, destructor, or type conversion");
9629 return error_mark_node
;
9632 /* An `=' or an `(' indicates an initializer. */
9633 is_initialized
= (token
->type
== CPP_EQ
9634 || token
->type
== CPP_OPEN_PAREN
);
9635 /* If the init-declarator isn't initialized and isn't followed by a
9636 `,' or `;', it's not a valid init-declarator. */
9638 && token
->type
!= CPP_COMMA
9639 && token
->type
!= CPP_SEMICOLON
)
9641 cp_parser_error (parser
, "expected init-declarator");
9642 return error_mark_node
;
9645 /* Because start_decl has side-effects, we should only call it if we
9646 know we're going ahead. By this point, we know that we cannot
9647 possibly be looking at any other construct. */
9648 cp_parser_commit_to_tentative_parse (parser
);
9650 /* Check to see whether or not this declaration is a friend. */
9651 friend_p
= cp_parser_friend_p (decl_specifiers
);
9653 /* Check that the number of template-parameter-lists is OK. */
9654 if (!cp_parser_check_declarator_template_parameters (parser
,
9656 return error_mark_node
;
9658 /* Enter the newly declared entry in the symbol table. If we're
9659 processing a declaration in a class-specifier, we wait until
9660 after processing the initializer. */
9663 if (parser
->in_unbraced_linkage_specification_p
)
9665 decl_specifiers
= tree_cons (error_mark_node
,
9666 get_identifier ("extern"),
9668 have_extern_spec
= false;
9670 decl
= start_decl (declarator
,
9677 /* Enter the SCOPE. That way unqualified names appearing in the
9678 initializer will be looked up in SCOPE. */
9682 /* Perform deferred access control checks, now that we know in which
9683 SCOPE the declared entity resides. */
9684 if (!member_p
&& decl
)
9686 tree saved_current_function_decl
= NULL_TREE
;
9688 /* If the entity being declared is a function, pretend that we
9689 are in its scope. If it is a `friend', it may have access to
9690 things that would not otherwise be accessible. */
9691 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9693 saved_current_function_decl
= current_function_decl
;
9694 current_function_decl
= decl
;
9697 /* Perform the access control checks for the declarator and the
9698 the decl-specifiers. */
9699 perform_deferred_access_checks ();
9701 /* Restore the saved value. */
9702 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9703 current_function_decl
= saved_current_function_decl
;
9706 /* Parse the initializer. */
9708 initializer
= cp_parser_initializer (parser
, &is_parenthesized_init
);
9711 initializer
= NULL_TREE
;
9712 is_parenthesized_init
= false;
9715 /* The old parser allows attributes to appear after a parenthesized
9716 initializer. Mark Mitchell proposed removing this functionality
9717 on the GCC mailing lists on 2002-08-13. This parser accepts the
9718 attributes -- but ignores them. */
9719 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
9720 if (cp_parser_attributes_opt (parser
))
9721 warning ("attributes after parenthesized initializer ignored");
9723 /* Leave the SCOPE, now that we have processed the initializer. It
9724 is important to do this before calling cp_finish_decl because it
9725 makes decisions about whether to create DECL_STMTs or not based
9726 on the current scope. */
9730 /* For an in-class declaration, use `grokfield' to create the
9733 decl
= grokfield (declarator
, decl_specifiers
,
9734 initializer
, /*asmspec=*/NULL_TREE
,
9735 /*attributes=*/NULL_TREE
);
9737 /* Finish processing the declaration. But, skip friend
9739 if (!friend_p
&& decl
)
9740 cp_finish_decl (decl
,
9743 /* If the initializer is in parentheses, then this is
9744 a direct-initialization, which means that an
9745 `explicit' constructor is OK. Otherwise, an
9746 `explicit' constructor cannot be used. */
9747 ((is_parenthesized_init
|| !is_initialized
)
9748 ? 0 : LOOKUP_ONLYCONVERTING
));
9753 /* Parse a declarator.
9757 ptr-operator declarator
9759 abstract-declarator:
9760 ptr-operator abstract-declarator [opt]
9761 direct-abstract-declarator
9766 attributes [opt] direct-declarator
9767 attributes [opt] ptr-operator declarator
9769 abstract-declarator:
9770 attributes [opt] ptr-operator abstract-declarator [opt]
9771 attributes [opt] direct-abstract-declarator
9773 Returns a representation of the declarator. If the declarator has
9774 the form `* declarator', then an INDIRECT_REF is returned, whose
9775 only operand is the sub-declarator. Analagously, `& declarator' is
9776 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9777 used. The first operand is the TYPE for `X'. The second operand
9778 is an INDIRECT_REF whose operand is the sub-declarator.
9780 Otherwise, the reprsentation is as for a direct-declarator.
9782 (It would be better to define a structure type to represent
9783 declarators, rather than abusing `tree' nodes to represent
9784 declarators. That would be much clearer and save some memory.
9785 There is no reason for declarators to be garbage-collected, for
9786 example; they are created during parser and no longer needed after
9787 `grokdeclarator' has been called.)
9789 For a ptr-operator that has the optional cv-qualifier-seq,
9790 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9793 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9794 true if this declarator represents a constructor, destructor, or
9795 type conversion operator. Otherwise, it is set to false.
9797 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9798 a decl-specifier-seq unless it declares a constructor, destructor,
9799 or conversion. It might seem that we could check this condition in
9800 semantic analysis, rather than parsing, but that makes it difficult
9801 to handle something like `f()'. We want to notice that there are
9802 no decl-specifiers, and therefore realize that this is an
9803 expression, not a declaration.) */
9806 cp_parser_declarator (cp_parser
* parser
,
9807 cp_parser_declarator_kind dcl_kind
,
9808 bool* ctor_dtor_or_conv_p
)
9812 enum tree_code code
;
9813 tree cv_qualifier_seq
;
9815 tree attributes
= NULL_TREE
;
9817 /* Assume this is not a constructor, destructor, or type-conversion
9819 if (ctor_dtor_or_conv_p
)
9820 *ctor_dtor_or_conv_p
= false;
9822 if (cp_parser_allow_gnu_extensions_p (parser
))
9823 attributes
= cp_parser_attributes_opt (parser
);
9825 /* Peek at the next token. */
9826 token
= cp_lexer_peek_token (parser
->lexer
);
9828 /* Check for the ptr-operator production. */
9829 cp_parser_parse_tentatively (parser
);
9830 /* Parse the ptr-operator. */
9831 code
= cp_parser_ptr_operator (parser
,
9834 /* If that worked, then we have a ptr-operator. */
9835 if (cp_parser_parse_definitely (parser
))
9837 /* The dependent declarator is optional if we are parsing an
9838 abstract-declarator. */
9839 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9840 cp_parser_parse_tentatively (parser
);
9842 /* Parse the dependent declarator. */
9843 declarator
= cp_parser_declarator (parser
, dcl_kind
,
9844 /*ctor_dtor_or_conv_p=*/NULL
);
9846 /* If we are parsing an abstract-declarator, we must handle the
9847 case where the dependent declarator is absent. */
9848 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
9849 && !cp_parser_parse_definitely (parser
))
9850 declarator
= NULL_TREE
;
9852 /* Build the representation of the ptr-operator. */
9853 if (code
== INDIRECT_REF
)
9854 declarator
= make_pointer_declarator (cv_qualifier_seq
,
9857 declarator
= make_reference_declarator (cv_qualifier_seq
,
9859 /* Handle the pointer-to-member case. */
9861 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
9863 /* Everything else is a direct-declarator. */
9865 declarator
= cp_parser_direct_declarator (parser
,
9867 ctor_dtor_or_conv_p
);
9869 if (attributes
&& declarator
!= error_mark_node
)
9870 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
9875 /* Parse a direct-declarator or direct-abstract-declarator.
9879 direct-declarator ( parameter-declaration-clause )
9880 cv-qualifier-seq [opt]
9881 exception-specification [opt]
9882 direct-declarator [ constant-expression [opt] ]
9885 direct-abstract-declarator:
9886 direct-abstract-declarator [opt]
9887 ( parameter-declaration-clause )
9888 cv-qualifier-seq [opt]
9889 exception-specification [opt]
9890 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9891 ( abstract-declarator )
9893 Returns a representation of the declarator. DCL_KIND is
9894 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9895 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9896 we are parsing a direct-declarator. It is
9897 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9898 of ambiguity we prefer an abstract declarator, as per
9899 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9900 cp_parser_declarator.
9902 For the declarator-id production, the representation is as for an
9903 id-expression, except that a qualified name is represented as a
9904 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9905 see the documentation of the FUNCTION_DECLARATOR_* macros for
9906 information about how to find the various declarator components.
9907 An array-declarator is represented as an ARRAY_REF. The
9908 direct-declarator is the first operand; the constant-expression
9909 indicating the size of the array is the second operand. */
9912 cp_parser_direct_declarator (cp_parser
* parser
,
9913 cp_parser_declarator_kind dcl_kind
,
9914 bool* ctor_dtor_or_conv_p
)
9917 tree declarator
= NULL_TREE
;
9918 tree scope
= NULL_TREE
;
9919 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
9920 bool saved_in_declarator_p
= parser
->in_declarator_p
;
9925 /* Peek at the next token. */
9926 token
= cp_lexer_peek_token (parser
->lexer
);
9927 if (token
->type
== CPP_OPEN_PAREN
)
9929 /* This is either a parameter-declaration-clause, or a
9930 parenthesized declarator. When we know we are parsing a
9931 named declarator, it must be a paranthesized declarator
9932 if FIRST is true. For instance, `(int)' is a
9933 parameter-declaration-clause, with an omitted
9934 direct-abstract-declarator. But `((*))', is a
9935 parenthesized abstract declarator. Finally, when T is a
9936 template parameter `(T)' is a
9937 paremeter-declaration-clause, and not a parenthesized
9940 We first try and parse a parameter-declaration-clause,
9941 and then try a nested declarator (if FIRST is true).
9943 It is not an error for it not to be a
9944 parameter-declaration-clause, even when FIRST is
9950 The first is the declaration of a function while the
9951 second is a the definition of a variable, including its
9954 Having seen only the parenthesis, we cannot know which of
9955 these two alternatives should be selected. Even more
9956 complex are examples like:
9961 The former is a function-declaration; the latter is a
9962 variable initialization.
9964 Thus again, we try a parameter-declation-clause, and if
9965 that fails, we back out and return. */
9967 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9971 cp_parser_parse_tentatively (parser
);
9973 /* Consume the `('. */
9974 cp_lexer_consume_token (parser
->lexer
);
9977 /* If this is going to be an abstract declarator, we're
9978 in a declarator and we can't have default args. */
9979 parser
->default_arg_ok_p
= false;
9980 parser
->in_declarator_p
= true;
9983 /* Parse the parameter-declaration-clause. */
9984 params
= cp_parser_parameter_declaration_clause (parser
);
9986 /* If all went well, parse the cv-qualifier-seq and the
9987 exception-specfication. */
9988 if (cp_parser_parse_definitely (parser
))
9991 tree exception_specification
;
9994 /* Consume the `)'. */
9995 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
9997 /* Parse the cv-qualifier-seq. */
9998 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
9999 /* And the exception-specification. */
10000 exception_specification
10001 = cp_parser_exception_specification_opt (parser
);
10003 /* Create the function-declarator. */
10004 declarator
= make_call_declarator (declarator
,
10007 exception_specification
);
10008 /* Any subsequent parameter lists are to do with
10009 return type, so are not those of the declared
10011 parser
->default_arg_ok_p
= false;
10013 /* Repeat the main loop. */
10018 /* If this is the first, we can try a parenthesized
10022 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10023 parser
->in_declarator_p
= saved_in_declarator_p
;
10025 /* Consume the `('. */
10026 cp_lexer_consume_token (parser
->lexer
);
10027 /* Parse the nested declarator. */
10029 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
);
10031 /* Expect a `)'. */
10032 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10033 declarator
= error_mark_node
;
10034 if (declarator
== error_mark_node
)
10037 goto handle_declarator
;
10039 /* Otherwise, we must be done. */
10043 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10044 && token
->type
== CPP_OPEN_SQUARE
)
10046 /* Parse an array-declarator. */
10050 parser
->default_arg_ok_p
= false;
10051 parser
->in_declarator_p
= true;
10052 /* Consume the `['. */
10053 cp_lexer_consume_token (parser
->lexer
);
10054 /* Peek at the next token. */
10055 token
= cp_lexer_peek_token (parser
->lexer
);
10056 /* If the next token is `]', then there is no
10057 constant-expression. */
10058 if (token
->type
!= CPP_CLOSE_SQUARE
)
10060 bool non_constant_p
;
10063 = cp_parser_constant_expression (parser
,
10064 /*allow_non_constant=*/true,
10066 /* If we're in a template, but the constant-expression
10067 isn't value dependent, simplify it. We're supposed
10070 template <typename T> void f(T[1 + 1]);
10071 template <typename T> void f(T[2]);
10073 as two declarations of the same function, for
10075 if (processing_template_decl
10077 && !value_dependent_expression_p (bounds
))
10079 HOST_WIDE_INT saved_processing_template_decl
;
10081 saved_processing_template_decl
= processing_template_decl
;
10082 processing_template_decl
= 0;
10083 bounds
= build_expr_from_tree (bounds
);
10084 processing_template_decl
= saved_processing_template_decl
;
10088 bounds
= NULL_TREE
;
10089 /* Look for the closing `]'. */
10090 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
10092 declarator
= error_mark_node
;
10096 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
10098 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
10100 /* Parse a declarator_id */
10101 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10102 cp_parser_parse_tentatively (parser
);
10103 declarator
= cp_parser_declarator_id (parser
);
10104 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
10106 if (!cp_parser_parse_definitely (parser
))
10107 declarator
= error_mark_node
;
10108 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
10110 cp_parser_error (parser
, "expected unqualified-id");
10111 declarator
= error_mark_node
;
10115 if (declarator
== error_mark_node
)
10118 if (TREE_CODE (declarator
) == SCOPE_REF
)
10120 tree scope
= TREE_OPERAND (declarator
, 0);
10122 /* In the declaration of a member of a template class
10123 outside of the class itself, the SCOPE will sometimes
10124 be a TYPENAME_TYPE. For example, given:
10126 template <typename T>
10127 int S<T>::R::i = 3;
10129 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10130 this context, we must resolve S<T>::R to an ordinary
10131 type, rather than a typename type.
10133 The reason we normally avoid resolving TYPENAME_TYPEs
10134 is that a specialization of `S' might render
10135 `S<T>::R' not a type. However, if `S' is
10136 specialized, then this `i' will not be used, so there
10137 is no harm in resolving the types here. */
10138 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
10142 /* Resolve the TYPENAME_TYPE. */
10143 type
= resolve_typename_type (scope
,
10144 /*only_current_p=*/false);
10145 /* If that failed, the declarator is invalid. */
10146 if (type
!= error_mark_node
)
10148 /* Build a new DECLARATOR. */
10149 declarator
= build_nt (SCOPE_REF
,
10151 TREE_OPERAND (declarator
, 1));
10155 /* Check to see whether the declarator-id names a constructor,
10156 destructor, or conversion. */
10157 if (declarator
&& ctor_dtor_or_conv_p
10158 && ((TREE_CODE (declarator
) == SCOPE_REF
10159 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
10160 || (TREE_CODE (declarator
) != SCOPE_REF
10161 && at_class_scope_p ())))
10163 tree unqualified_name
;
10166 /* Get the unqualified part of the name. */
10167 if (TREE_CODE (declarator
) == SCOPE_REF
)
10169 class_type
= TREE_OPERAND (declarator
, 0);
10170 unqualified_name
= TREE_OPERAND (declarator
, 1);
10174 class_type
= current_class_type
;
10175 unqualified_name
= declarator
;
10178 /* See if it names ctor, dtor or conv. */
10179 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
10180 || IDENTIFIER_TYPENAME_P (unqualified_name
)
10181 || constructor_name_p (unqualified_name
, class_type
))
10182 *ctor_dtor_or_conv_p
= true;
10185 handle_declarator
:;
10186 scope
= get_scope_of_declarator (declarator
);
10188 /* Any names that appear after the declarator-id for a member
10189 are looked up in the containing scope. */
10190 push_scope (scope
);
10191 parser
->in_declarator_p
= true;
10192 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
10194 && (TREE_CODE (declarator
) == SCOPE_REF
10195 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
10196 /* Default args are only allowed on function
10198 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10200 parser
->default_arg_ok_p
= false;
10209 /* For an abstract declarator, we might wind up with nothing at this
10210 point. That's an error; the declarator is not optional. */
10212 cp_parser_error (parser
, "expected declarator");
10214 /* If we entered a scope, we must exit it now. */
10218 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10219 parser
->in_declarator_p
= saved_in_declarator_p
;
10224 /* Parse a ptr-operator.
10227 * cv-qualifier-seq [opt]
10229 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10234 & cv-qualifier-seq [opt]
10236 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10237 used. Returns ADDR_EXPR if a reference was used. In the
10238 case of a pointer-to-member, *TYPE is filled in with the
10239 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10240 with the cv-qualifier-seq, or NULL_TREE, if there are no
10241 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10243 static enum tree_code
10244 cp_parser_ptr_operator (cp_parser
* parser
,
10246 tree
* cv_qualifier_seq
)
10248 enum tree_code code
= ERROR_MARK
;
10251 /* Assume that it's not a pointer-to-member. */
10253 /* And that there are no cv-qualifiers. */
10254 *cv_qualifier_seq
= NULL_TREE
;
10256 /* Peek at the next token. */
10257 token
= cp_lexer_peek_token (parser
->lexer
);
10258 /* If it's a `*' or `&' we have a pointer or reference. */
10259 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10261 /* Remember which ptr-operator we were processing. */
10262 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10264 /* Consume the `*' or `&'. */
10265 cp_lexer_consume_token (parser
->lexer
);
10267 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10268 `&', if we are allowing GNU extensions. (The only qualifier
10269 that can legally appear after `&' is `restrict', but that is
10270 enforced during semantic analysis. */
10271 if (code
== INDIRECT_REF
10272 || cp_parser_allow_gnu_extensions_p (parser
))
10273 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10277 /* Try the pointer-to-member case. */
10278 cp_parser_parse_tentatively (parser
);
10279 /* Look for the optional `::' operator. */
10280 cp_parser_global_scope_opt (parser
,
10281 /*current_scope_valid_p=*/false);
10282 /* Look for the nested-name specifier. */
10283 cp_parser_nested_name_specifier (parser
,
10284 /*typename_keyword_p=*/false,
10285 /*check_dependency_p=*/true,
10287 /* If we found it, and the next token is a `*', then we are
10288 indeed looking at a pointer-to-member operator. */
10289 if (!cp_parser_error_occurred (parser
)
10290 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10292 /* The type of which the member is a member is given by the
10294 *type
= parser
->scope
;
10295 /* The next name will not be qualified. */
10296 parser
->scope
= NULL_TREE
;
10297 parser
->qualifying_scope
= NULL_TREE
;
10298 parser
->object_scope
= NULL_TREE
;
10299 /* Indicate that the `*' operator was used. */
10300 code
= INDIRECT_REF
;
10301 /* Look for the optional cv-qualifier-seq. */
10302 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10304 /* If that didn't work we don't have a ptr-operator. */
10305 if (!cp_parser_parse_definitely (parser
))
10306 cp_parser_error (parser
, "expected ptr-operator");
10312 /* Parse an (optional) cv-qualifier-seq.
10315 cv-qualifier cv-qualifier-seq [opt]
10317 Returns a TREE_LIST. The TREE_VALUE of each node is the
10318 representation of a cv-qualifier. */
10321 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10323 tree cv_qualifiers
= NULL_TREE
;
10329 /* Look for the next cv-qualifier. */
10330 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10331 /* If we didn't find one, we're done. */
10335 /* Add this cv-qualifier to the list. */
10337 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10340 /* We built up the list in reverse order. */
10341 return nreverse (cv_qualifiers
);
10344 /* Parse an (optional) cv-qualifier.
10356 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10359 tree cv_qualifier
= NULL_TREE
;
10361 /* Peek at the next token. */
10362 token
= cp_lexer_peek_token (parser
->lexer
);
10363 /* See if it's a cv-qualifier. */
10364 switch (token
->keyword
)
10369 /* Save the value of the token. */
10370 cv_qualifier
= token
->value
;
10371 /* Consume the token. */
10372 cp_lexer_consume_token (parser
->lexer
);
10379 return cv_qualifier
;
10382 /* Parse a declarator-id.
10386 :: [opt] nested-name-specifier [opt] type-name
10388 In the `id-expression' case, the value returned is as for
10389 cp_parser_id_expression if the id-expression was an unqualified-id.
10390 If the id-expression was a qualified-id, then a SCOPE_REF is
10391 returned. The first operand is the scope (either a NAMESPACE_DECL
10392 or TREE_TYPE), but the second is still just a representation of an
10396 cp_parser_declarator_id (cp_parser
* parser
)
10398 tree id_expression
;
10400 /* The expression must be an id-expression. Assume that qualified
10401 names are the names of types so that:
10404 int S<T>::R::i = 3;
10406 will work; we must treat `S<T>::R' as the name of a type.
10407 Similarly, assume that qualified names are templates, where
10411 int S<T>::R<T>::i = 3;
10414 id_expression
= cp_parser_id_expression (parser
,
10415 /*template_keyword_p=*/false,
10416 /*check_dependency_p=*/false,
10417 /*template_p=*/NULL
);
10418 /* If the name was qualified, create a SCOPE_REF to represent
10422 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
10423 parser
->scope
= NULL_TREE
;
10426 return id_expression
;
10429 /* Parse a type-id.
10432 type-specifier-seq abstract-declarator [opt]
10434 Returns the TYPE specified. */
10437 cp_parser_type_id (cp_parser
* parser
)
10439 tree type_specifier_seq
;
10440 tree abstract_declarator
;
10442 /* Parse the type-specifier-seq. */
10444 = cp_parser_type_specifier_seq (parser
);
10445 if (type_specifier_seq
== error_mark_node
)
10446 return error_mark_node
;
10448 /* There might or might not be an abstract declarator. */
10449 cp_parser_parse_tentatively (parser
);
10450 /* Look for the declarator. */
10451 abstract_declarator
10452 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
);
10453 /* Check to see if there really was a declarator. */
10454 if (!cp_parser_parse_definitely (parser
))
10455 abstract_declarator
= NULL_TREE
;
10457 return groktypename (build_tree_list (type_specifier_seq
,
10458 abstract_declarator
));
10461 /* Parse a type-specifier-seq.
10463 type-specifier-seq:
10464 type-specifier type-specifier-seq [opt]
10468 type-specifier-seq:
10469 attributes type-specifier-seq [opt]
10471 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10472 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10475 cp_parser_type_specifier_seq (cp_parser
* parser
)
10477 bool seen_type_specifier
= false;
10478 tree type_specifier_seq
= NULL_TREE
;
10480 /* Parse the type-specifiers and attributes. */
10483 tree type_specifier
;
10485 /* Check for attributes first. */
10486 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
10488 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
10490 type_specifier_seq
);
10494 /* After the first type-specifier, others are optional. */
10495 if (seen_type_specifier
)
10496 cp_parser_parse_tentatively (parser
);
10497 /* Look for the type-specifier. */
10498 type_specifier
= cp_parser_type_specifier (parser
,
10499 CP_PARSER_FLAGS_NONE
,
10500 /*is_friend=*/false,
10501 /*is_declaration=*/false,
10504 /* If the first type-specifier could not be found, this is not a
10505 type-specifier-seq at all. */
10506 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
10507 return error_mark_node
;
10508 /* If subsequent type-specifiers could not be found, the
10509 type-specifier-seq is complete. */
10510 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
10513 /* Add the new type-specifier to the list. */
10515 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
10516 seen_type_specifier
= true;
10519 /* We built up the list in reverse order. */
10520 return nreverse (type_specifier_seq
);
10523 /* Parse a parameter-declaration-clause.
10525 parameter-declaration-clause:
10526 parameter-declaration-list [opt] ... [opt]
10527 parameter-declaration-list , ...
10529 Returns a representation for the parameter declarations. Each node
10530 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10531 representation.) If the parameter-declaration-clause ends with an
10532 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10533 list. A return value of NULL_TREE indicates a
10534 parameter-declaration-clause consisting only of an ellipsis. */
10537 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
10543 /* Peek at the next token. */
10544 token
= cp_lexer_peek_token (parser
->lexer
);
10545 /* Check for trivial parameter-declaration-clauses. */
10546 if (token
->type
== CPP_ELLIPSIS
)
10548 /* Consume the `...' token. */
10549 cp_lexer_consume_token (parser
->lexer
);
10552 else if (token
->type
== CPP_CLOSE_PAREN
)
10553 /* There are no parameters. */
10555 #ifndef NO_IMPLICIT_EXTERN_C
10556 if (in_system_header
&& current_class_type
== NULL
10557 && current_lang_name
== lang_name_c
)
10561 return void_list_node
;
10563 /* Check for `(void)', too, which is a special case. */
10564 else if (token
->keyword
== RID_VOID
10565 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
10566 == CPP_CLOSE_PAREN
))
10568 /* Consume the `void' token. */
10569 cp_lexer_consume_token (parser
->lexer
);
10570 /* There are no parameters. */
10571 return void_list_node
;
10574 /* Parse the parameter-declaration-list. */
10575 parameters
= cp_parser_parameter_declaration_list (parser
);
10576 /* If a parse error occurred while parsing the
10577 parameter-declaration-list, then the entire
10578 parameter-declaration-clause is erroneous. */
10579 if (parameters
== error_mark_node
)
10580 return error_mark_node
;
10582 /* Peek at the next token. */
10583 token
= cp_lexer_peek_token (parser
->lexer
);
10584 /* If it's a `,', the clause should terminate with an ellipsis. */
10585 if (token
->type
== CPP_COMMA
)
10587 /* Consume the `,'. */
10588 cp_lexer_consume_token (parser
->lexer
);
10589 /* Expect an ellipsis. */
10591 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
10593 /* It might also be `...' if the optional trailing `,' was
10595 else if (token
->type
== CPP_ELLIPSIS
)
10597 /* Consume the `...' token. */
10598 cp_lexer_consume_token (parser
->lexer
);
10599 /* And remember that we saw it. */
10603 ellipsis_p
= false;
10605 /* Finish the parameter list. */
10606 return finish_parmlist (parameters
, ellipsis_p
);
10609 /* Parse a parameter-declaration-list.
10611 parameter-declaration-list:
10612 parameter-declaration
10613 parameter-declaration-list , parameter-declaration
10615 Returns a representation of the parameter-declaration-list, as for
10616 cp_parser_parameter_declaration_clause. However, the
10617 `void_list_node' is never appended to the list. */
10620 cp_parser_parameter_declaration_list (cp_parser
* parser
)
10622 tree parameters
= NULL_TREE
;
10624 /* Look for more parameters. */
10628 /* Parse the parameter. */
10630 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/false);
10632 /* If a parse error ocurred parsing the parameter declaration,
10633 then the entire parameter-declaration-list is erroneous. */
10634 if (parameter
== error_mark_node
)
10636 parameters
= error_mark_node
;
10639 /* Add the new parameter to the list. */
10640 TREE_CHAIN (parameter
) = parameters
;
10641 parameters
= parameter
;
10643 /* Peek at the next token. */
10644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
10645 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10646 /* The parameter-declaration-list is complete. */
10648 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10652 /* Peek at the next token. */
10653 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10654 /* If it's an ellipsis, then the list is complete. */
10655 if (token
->type
== CPP_ELLIPSIS
)
10657 /* Otherwise, there must be more parameters. Consume the
10659 cp_lexer_consume_token (parser
->lexer
);
10663 cp_parser_error (parser
, "expected `,' or `...'");
10668 /* We built up the list in reverse order; straighten it out now. */
10669 return nreverse (parameters
);
10672 /* Parse a parameter declaration.
10674 parameter-declaration:
10675 decl-specifier-seq declarator
10676 decl-specifier-seq declarator = assignment-expression
10677 decl-specifier-seq abstract-declarator [opt]
10678 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10680 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10681 declares a template parameter. (In that case, a non-nested `>'
10682 token encountered during the parsing of the assignment-expression
10683 is not interpreted as a greater-than operator.)
10685 Returns a TREE_LIST representing the parameter-declaration. The
10686 TREE_VALUE is a representation of the decl-specifier-seq and
10687 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10688 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10689 TREE_VALUE represents the declarator. */
10692 cp_parser_parameter_declaration (cp_parser
*parser
,
10693 bool template_parm_p
)
10695 bool declares_class_or_enum
;
10696 bool greater_than_is_operator_p
;
10697 tree decl_specifiers
;
10700 tree default_argument
;
10703 const char *saved_message
;
10705 /* In a template parameter, `>' is not an operator.
10709 When parsing a default template-argument for a non-type
10710 template-parameter, the first non-nested `>' is taken as the end
10711 of the template parameter-list rather than a greater-than
10713 greater_than_is_operator_p
= !template_parm_p
;
10715 /* Type definitions may not appear in parameter types. */
10716 saved_message
= parser
->type_definition_forbidden_message
;
10717 parser
->type_definition_forbidden_message
10718 = "types may not be defined in parameter types";
10720 /* Parse the declaration-specifiers. */
10722 = cp_parser_decl_specifier_seq (parser
,
10723 CP_PARSER_FLAGS_NONE
,
10725 &declares_class_or_enum
);
10726 /* If an error occurred, there's no reason to attempt to parse the
10727 rest of the declaration. */
10728 if (cp_parser_error_occurred (parser
))
10730 parser
->type_definition_forbidden_message
= saved_message
;
10731 return error_mark_node
;
10734 /* Peek at the next token. */
10735 token
= cp_lexer_peek_token (parser
->lexer
);
10736 /* If the next token is a `)', `,', `=', `>', or `...', then there
10737 is no declarator. */
10738 if (token
->type
== CPP_CLOSE_PAREN
10739 || token
->type
== CPP_COMMA
10740 || token
->type
== CPP_EQ
10741 || token
->type
== CPP_ELLIPSIS
10742 || token
->type
== CPP_GREATER
)
10743 declarator
= NULL_TREE
;
10744 /* Otherwise, there should be a declarator. */
10747 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10748 parser
->default_arg_ok_p
= false;
10750 declarator
= cp_parser_declarator (parser
,
10751 CP_PARSER_DECLARATOR_EITHER
,
10752 /*ctor_dtor_or_conv_p=*/NULL
);
10753 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10754 /* After the declarator, allow more attributes. */
10755 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
10758 /* The restriction on defining new types applies only to the type
10759 of the parameter, not to the default argument. */
10760 parser
->type_definition_forbidden_message
= saved_message
;
10762 /* If the next token is `=', then process a default argument. */
10763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10765 bool saved_greater_than_is_operator_p
;
10766 /* Consume the `='. */
10767 cp_lexer_consume_token (parser
->lexer
);
10769 /* If we are defining a class, then the tokens that make up the
10770 default argument must be saved and processed later. */
10771 if (!template_parm_p
&& at_class_scope_p ()
10772 && TYPE_BEING_DEFINED (current_class_type
))
10774 unsigned depth
= 0;
10776 /* Create a DEFAULT_ARG to represented the unparsed default
10778 default_argument
= make_node (DEFAULT_ARG
);
10779 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
10781 /* Add tokens until we have processed the entire default
10788 /* Peek at the next token. */
10789 token
= cp_lexer_peek_token (parser
->lexer
);
10790 /* What we do depends on what token we have. */
10791 switch (token
->type
)
10793 /* In valid code, a default argument must be
10794 immediately followed by a `,' `)', or `...'. */
10796 case CPP_CLOSE_PAREN
:
10798 /* If we run into a non-nested `;', `}', or `]',
10799 then the code is invalid -- but the default
10800 argument is certainly over. */
10801 case CPP_SEMICOLON
:
10802 case CPP_CLOSE_BRACE
:
10803 case CPP_CLOSE_SQUARE
:
10806 /* Update DEPTH, if necessary. */
10807 else if (token
->type
== CPP_CLOSE_PAREN
10808 || token
->type
== CPP_CLOSE_BRACE
10809 || token
->type
== CPP_CLOSE_SQUARE
)
10813 case CPP_OPEN_PAREN
:
10814 case CPP_OPEN_SQUARE
:
10815 case CPP_OPEN_BRACE
:
10820 /* If we see a non-nested `>', and `>' is not an
10821 operator, then it marks the end of the default
10823 if (!depth
&& !greater_than_is_operator_p
)
10827 /* If we run out of tokens, issue an error message. */
10829 error ("file ends in default argument");
10835 /* In these cases, we should look for template-ids.
10836 For example, if the default argument is
10837 `X<int, double>()', we need to do name lookup to
10838 figure out whether or not `X' is a template; if
10839 so, the `,' does not end the deault argument.
10841 That is not yet done. */
10848 /* If we've reached the end, stop. */
10852 /* Add the token to the token block. */
10853 token
= cp_lexer_consume_token (parser
->lexer
);
10854 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
10858 /* Outside of a class definition, we can just parse the
10859 assignment-expression. */
10862 bool saved_local_variables_forbidden_p
;
10864 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10866 saved_greater_than_is_operator_p
10867 = parser
->greater_than_is_operator_p
;
10868 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
10869 /* Local variable names (and the `this' keyword) may not
10870 appear in a default argument. */
10871 saved_local_variables_forbidden_p
10872 = parser
->local_variables_forbidden_p
;
10873 parser
->local_variables_forbidden_p
= true;
10874 /* Parse the assignment-expression. */
10875 default_argument
= cp_parser_assignment_expression (parser
);
10876 /* Restore saved state. */
10877 parser
->greater_than_is_operator_p
10878 = saved_greater_than_is_operator_p
;
10879 parser
->local_variables_forbidden_p
10880 = saved_local_variables_forbidden_p
;
10882 if (!parser
->default_arg_ok_p
)
10884 pedwarn ("default arguments are only permitted on functions");
10885 if (flag_pedantic_errors
)
10886 default_argument
= NULL_TREE
;
10890 default_argument
= NULL_TREE
;
10892 /* Create the representation of the parameter. */
10894 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
10895 parameter
= build_tree_list (default_argument
,
10896 build_tree_list (decl_specifiers
,
10902 /* Parse a function-definition.
10904 function-definition:
10905 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10907 decl-specifier-seq [opt] declarator function-try-block
10911 function-definition:
10912 __extension__ function-definition
10914 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10915 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10919 cp_parser_function_definition (cp_parser
* parser
, bool* friend_p
)
10921 tree decl_specifiers
;
10926 bool declares_class_or_enum
;
10928 /* The saved value of the PEDANTIC flag. */
10929 int saved_pedantic
;
10931 /* Any pending qualification must be cleared by our caller. It is
10932 more robust to force the callers to clear PARSER->SCOPE than to
10933 do it here since if the qualification is in effect here, it might
10934 also end up in effect elsewhere that it is not intended. */
10935 my_friendly_assert (!parser
->scope
, 20010821);
10937 /* Handle `__extension__'. */
10938 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10940 /* Parse the function-definition. */
10941 fn
= cp_parser_function_definition (parser
, friend_p
);
10942 /* Restore the PEDANTIC flag. */
10943 pedantic
= saved_pedantic
;
10948 /* Check to see if this definition appears in a class-specifier. */
10949 member_p
= (at_class_scope_p ()
10950 && TYPE_BEING_DEFINED (current_class_type
));
10951 /* Defer access checks in the decl-specifier-seq until we know what
10952 function is being defined. There is no need to do this for the
10953 definition of member functions; we cannot be defining a member
10954 from another class. */
10955 push_deferring_access_checks (member_p
? dk_no_check
: dk_deferred
);
10957 /* Parse the decl-specifier-seq. */
10959 = cp_parser_decl_specifier_seq (parser
,
10960 CP_PARSER_FLAGS_OPTIONAL
,
10962 &declares_class_or_enum
);
10963 /* Figure out whether this declaration is a `friend'. */
10965 *friend_p
= cp_parser_friend_p (decl_specifiers
);
10967 /* Parse the declarator. */
10968 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10969 /*ctor_dtor_or_conv_p=*/NULL
);
10971 /* Gather up any access checks that occurred. */
10972 stop_deferring_access_checks ();
10974 /* If something has already gone wrong, we may as well stop now. */
10975 if (declarator
== error_mark_node
)
10977 /* Skip to the end of the function, or if this wasn't anything
10978 like a function-definition, to a `;' in the hopes of finding
10979 a sensible place from which to continue parsing. */
10980 cp_parser_skip_to_end_of_block_or_statement (parser
);
10981 pop_deferring_access_checks ();
10982 return error_mark_node
;
10985 /* The next character should be a `{' (for a simple function
10986 definition), a `:' (for a ctor-initializer), or `try' (for a
10987 function-try block). */
10988 token
= cp_lexer_peek_token (parser
->lexer
);
10989 if (!cp_parser_token_starts_function_definition_p (token
))
10991 /* Issue the error-message. */
10992 cp_parser_error (parser
, "expected function-definition");
10993 /* Skip to the next `;'. */
10994 cp_parser_skip_to_end_of_block_or_statement (parser
);
10996 pop_deferring_access_checks ();
10997 return error_mark_node
;
11000 /* If we are in a class scope, then we must handle
11001 function-definitions specially. In particular, we save away the
11002 tokens that make up the function body, and parse them again
11003 later, in order to handle code like:
11006 int f () { return i; }
11010 Here, we cannot parse the body of `f' until after we have seen
11011 the declaration of `i'. */
11014 cp_token_cache
*cache
;
11016 /* Create the function-declaration. */
11017 fn
= start_method (decl_specifiers
, declarator
, attributes
);
11018 /* If something went badly wrong, bail out now. */
11019 if (fn
== error_mark_node
)
11021 /* If there's a function-body, skip it. */
11022 if (cp_parser_token_starts_function_definition_p
11023 (cp_lexer_peek_token (parser
->lexer
)))
11024 cp_parser_skip_to_end_of_block_or_statement (parser
);
11025 pop_deferring_access_checks ();
11026 return error_mark_node
;
11029 /* Create a token cache. */
11030 cache
= cp_token_cache_new ();
11031 /* Save away the tokens that make up the body of the
11033 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
11034 /* Handle function try blocks. */
11035 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
11036 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
11038 /* Save away the inline definition; we will process it when the
11039 class is complete. */
11040 DECL_PENDING_INLINE_INFO (fn
) = cache
;
11041 DECL_PENDING_INLINE_P (fn
) = 1;
11043 /* We need to know that this was defined in the class, so that
11044 friend templates are handled correctly. */
11045 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
11047 /* We're done with the inline definition. */
11048 finish_method (fn
);
11050 /* Add FN to the queue of functions to be parsed later. */
11051 TREE_VALUE (parser
->unparsed_functions_queues
)
11052 = tree_cons (NULL_TREE
, fn
,
11053 TREE_VALUE (parser
->unparsed_functions_queues
));
11055 pop_deferring_access_checks ();
11059 /* Check that the number of template-parameter-lists is OK. */
11060 if (!cp_parser_check_declarator_template_parameters (parser
,
11063 cp_parser_skip_to_end_of_block_or_statement (parser
);
11064 pop_deferring_access_checks ();
11065 return error_mark_node
;
11068 fn
= cp_parser_function_definition_from_specifiers_and_declarator
11069 (parser
, decl_specifiers
, attributes
, declarator
);
11070 pop_deferring_access_checks ();
11074 /* Parse a function-body.
11077 compound_statement */
11080 cp_parser_function_body (cp_parser
*parser
)
11082 cp_parser_compound_statement (parser
);
11085 /* Parse a ctor-initializer-opt followed by a function-body. Return
11086 true if a ctor-initializer was present. */
11089 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11092 bool ctor_initializer_p
;
11094 /* Begin the function body. */
11095 body
= begin_function_body ();
11096 /* Parse the optional ctor-initializer. */
11097 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11098 /* Parse the function-body. */
11099 cp_parser_function_body (parser
);
11100 /* Finish the function body. */
11101 finish_function_body (body
);
11103 return ctor_initializer_p
;
11106 /* Parse an initializer.
11109 = initializer-clause
11110 ( expression-list )
11112 Returns a expression representing the initializer. If no
11113 initializer is present, NULL_TREE is returned.
11115 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11116 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11117 set to FALSE if there is no initializer present. */
11120 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
)
11125 /* Peek at the next token. */
11126 token
= cp_lexer_peek_token (parser
->lexer
);
11128 /* Let our caller know whether or not this initializer was
11130 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
11132 if (token
->type
== CPP_EQ
)
11134 /* Consume the `='. */
11135 cp_lexer_consume_token (parser
->lexer
);
11136 /* Parse the initializer-clause. */
11137 init
= cp_parser_initializer_clause (parser
);
11139 else if (token
->type
== CPP_OPEN_PAREN
)
11141 /* Consume the `('. */
11142 cp_lexer_consume_token (parser
->lexer
);
11143 /* Parse the expression-list. */
11144 init
= cp_parser_expression_list (parser
);
11145 /* Consume the `)' token. */
11146 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11147 cp_parser_skip_to_closing_parenthesis (parser
);
11151 /* Anything else is an error. */
11152 cp_parser_error (parser
, "expected initializer");
11153 init
= error_mark_node
;
11159 /* Parse an initializer-clause.
11161 initializer-clause:
11162 assignment-expression
11163 { initializer-list , [opt] }
11166 Returns an expression representing the initializer.
11168 If the `assignment-expression' production is used the value
11169 returned is simply a reprsentation for the expression.
11171 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11172 the elements of the initializer-list (or NULL_TREE, if the last
11173 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11174 NULL_TREE. There is no way to detect whether or not the optional
11175 trailing `,' was provided. */
11178 cp_parser_initializer_clause (cp_parser
* parser
)
11182 /* If it is not a `{', then we are looking at an
11183 assignment-expression. */
11184 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11185 initializer
= cp_parser_assignment_expression (parser
);
11188 /* Consume the `{' token. */
11189 cp_lexer_consume_token (parser
->lexer
);
11190 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11191 initializer
= make_node (CONSTRUCTOR
);
11192 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11193 necessary, but check_initializer depends upon it, for
11195 TREE_HAS_CONSTRUCTOR (initializer
) = 1;
11196 /* If it's not a `}', then there is a non-trivial initializer. */
11197 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11199 /* Parse the initializer list. */
11200 CONSTRUCTOR_ELTS (initializer
)
11201 = cp_parser_initializer_list (parser
);
11202 /* A trailing `,' token is allowed. */
11203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11204 cp_lexer_consume_token (parser
->lexer
);
11207 /* Now, there should be a trailing `}'. */
11208 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11211 return initializer
;
11214 /* Parse an initializer-list.
11218 initializer-list , initializer-clause
11223 identifier : initializer-clause
11224 initializer-list, identifier : initializer-clause
11226 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11227 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11228 IDENTIFIER_NODE naming the field to initialize. */
11231 cp_parser_initializer_list (cp_parser
* parser
)
11233 tree initializers
= NULL_TREE
;
11235 /* Parse the rest of the list. */
11242 /* If the next token is an identifier and the following one is a
11243 colon, we are looking at the GNU designated-initializer
11245 if (cp_parser_allow_gnu_extensions_p (parser
)
11246 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11247 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11249 /* Consume the identifier. */
11250 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11251 /* Consume the `:'. */
11252 cp_lexer_consume_token (parser
->lexer
);
11255 identifier
= NULL_TREE
;
11257 /* Parse the initializer. */
11258 initializer
= cp_parser_initializer_clause (parser
);
11260 /* Add it to the list. */
11261 initializers
= tree_cons (identifier
, initializer
, initializers
);
11263 /* If the next token is not a comma, we have reached the end of
11265 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11268 /* Peek at the next token. */
11269 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11270 /* If the next token is a `}', then we're still done. An
11271 initializer-clause can have a trailing `,' after the
11272 initializer-list and before the closing `}'. */
11273 if (token
->type
== CPP_CLOSE_BRACE
)
11276 /* Consume the `,' token. */
11277 cp_lexer_consume_token (parser
->lexer
);
11280 /* The initializers were built up in reverse order, so we need to
11281 reverse them now. */
11282 return nreverse (initializers
);
11285 /* Classes [gram.class] */
11287 /* Parse a class-name.
11293 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11294 to indicate that names looked up in dependent types should be
11295 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11296 keyword has been used to indicate that the name that appears next
11297 is a template. TYPE_P is true iff the next name should be treated
11298 as class-name, even if it is declared to be some other kind of name
11299 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11300 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11301 being defined in a class-head.
11303 Returns the TYPE_DECL representing the class. */
11306 cp_parser_class_name (cp_parser
*parser
,
11307 bool typename_keyword_p
,
11308 bool template_keyword_p
,
11310 bool check_dependency_p
,
11318 /* All class-names start with an identifier. */
11319 token
= cp_lexer_peek_token (parser
->lexer
);
11320 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11322 cp_parser_error (parser
, "expected class-name");
11323 return error_mark_node
;
11326 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11327 to a template-id, so we save it here. */
11328 scope
= parser
->scope
;
11329 /* Any name names a type if we're following the `typename' keyword
11330 in a qualified name where the enclosing scope is type-dependent. */
11331 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11332 && dependent_type_p (scope
));
11333 /* Handle the common case (an identifier, but not a template-id)
11335 if (token
->type
== CPP_NAME
11336 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
11340 /* Look for the identifier. */
11341 identifier
= cp_parser_identifier (parser
);
11342 /* If the next token isn't an identifier, we are certainly not
11343 looking at a class-name. */
11344 if (identifier
== error_mark_node
)
11345 decl
= error_mark_node
;
11346 /* If we know this is a type-name, there's no need to look it
11348 else if (typename_p
)
11352 /* If the next token is a `::', then the name must be a type
11355 [basic.lookup.qual]
11357 During the lookup for a name preceding the :: scope
11358 resolution operator, object, function, and enumerator
11359 names are ignored. */
11360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11362 /* Look up the name. */
11363 decl
= cp_parser_lookup_name (parser
, identifier
,
11365 /*is_namespace=*/false,
11366 check_dependency_p
);
11371 /* Try a template-id. */
11372 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11373 check_dependency_p
);
11374 if (decl
== error_mark_node
)
11375 return error_mark_node
;
11378 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11380 /* If this is a typename, create a TYPENAME_TYPE. */
11381 if (typename_p
&& decl
!= error_mark_node
)
11382 decl
= TYPE_NAME (make_typename_type (scope
, decl
,
11385 /* Check to see that it is really the name of a class. */
11386 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11387 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11388 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11389 /* Situations like this:
11391 template <typename T> struct A {
11392 typename T::template X<int>::I i;
11395 are problematic. Is `T::template X<int>' a class-name? The
11396 standard does not seem to be definitive, but there is no other
11397 valid interpretation of the following `::'. Therefore, those
11398 names are considered class-names. */
11399 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11400 else if (decl
== error_mark_node
11401 || TREE_CODE (decl
) != TYPE_DECL
11402 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11404 cp_parser_error (parser
, "expected class-name");
11405 return error_mark_node
;
11411 /* Parse a class-specifier.
11414 class-head { member-specification [opt] }
11416 Returns the TREE_TYPE representing the class. */
11419 cp_parser_class_specifier (cp_parser
* parser
)
11423 tree attributes
= NULL_TREE
;
11424 int has_trailing_semicolon
;
11425 bool nested_name_specifier_p
;
11426 unsigned saved_num_template_parameter_lists
;
11428 push_deferring_access_checks (dk_no_deferred
);
11430 /* Parse the class-head. */
11431 type
= cp_parser_class_head (parser
,
11432 &nested_name_specifier_p
);
11433 /* If the class-head was a semantic disaster, skip the entire body
11437 cp_parser_skip_to_end_of_block_or_statement (parser
);
11438 pop_deferring_access_checks ();
11439 return error_mark_node
;
11442 /* Look for the `{'. */
11443 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11445 pop_deferring_access_checks ();
11446 return error_mark_node
;
11449 /* Issue an error message if type-definitions are forbidden here. */
11450 cp_parser_check_type_definition (parser
);
11451 /* Remember that we are defining one more class. */
11452 ++parser
->num_classes_being_defined
;
11453 /* Inside the class, surrounding template-parameter-lists do not
11455 saved_num_template_parameter_lists
11456 = parser
->num_template_parameter_lists
;
11457 parser
->num_template_parameter_lists
= 0;
11459 /* Start the class. */
11460 type
= begin_class_definition (type
);
11461 if (type
== error_mark_node
)
11462 /* If the type is erroneous, skip the entire body of the class. */
11463 cp_parser_skip_to_closing_brace (parser
);
11465 /* Parse the member-specification. */
11466 cp_parser_member_specification_opt (parser
);
11467 /* Look for the trailing `}'. */
11468 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11469 /* We get better error messages by noticing a common problem: a
11470 missing trailing `;'. */
11471 token
= cp_lexer_peek_token (parser
->lexer
);
11472 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11473 /* Look for attributes to apply to this class. */
11474 if (cp_parser_allow_gnu_extensions_p (parser
))
11475 attributes
= cp_parser_attributes_opt (parser
);
11476 /* Finish the class definition. */
11477 type
= finish_class_definition (type
,
11479 has_trailing_semicolon
,
11480 nested_name_specifier_p
);
11481 /* If this class is not itself within the scope of another class,
11482 then we need to parse the bodies of all of the queued function
11483 definitions. Note that the queued functions defined in a class
11484 are not always processed immediately following the
11485 class-specifier for that class. Consider:
11488 struct B { void f() { sizeof (A); } };
11491 If `f' were processed before the processing of `A' were
11492 completed, there would be no way to compute the size of `A'.
11493 Note that the nesting we are interested in here is lexical --
11494 not the semantic nesting given by TYPE_CONTEXT. In particular,
11497 struct A { struct B; };
11498 struct A::B { void f() { } };
11500 there is no need to delay the parsing of `A::B::f'. */
11501 if (--parser
->num_classes_being_defined
== 0)
11503 tree last_scope
= NULL_TREE
;
11507 /* Reverse the queue, so that we process it in the order the
11508 functions were declared. */
11509 TREE_VALUE (parser
->unparsed_functions_queues
)
11510 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
11511 /* In a first pass, parse default arguments to the functions.
11512 Then, in a second pass, parse the bodies of the functions.
11513 This two-phased approach handles cases like:
11521 for (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
);
11523 queue_entry
= TREE_CHAIN (queue_entry
))
11525 fn
= TREE_VALUE (queue_entry
);
11526 if (DECL_FUNCTION_TEMPLATE_P (fn
))
11527 fn
= DECL_TEMPLATE_RESULT (fn
);
11528 /* Make sure that any template parameters are in scope. */
11529 maybe_begin_member_template_processing (fn
);
11530 /* If there are default arguments that have not yet been processed,
11531 take care of them now. */
11532 cp_parser_late_parsing_default_args (parser
, fn
);
11533 /* Remove any template parameters from the symbol table. */
11534 maybe_end_member_template_processing ();
11536 /* Now parse the body of the functions. */
11537 while (TREE_VALUE (parser
->unparsed_functions_queues
))
11540 /* Figure out which function we need to process. */
11541 queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
);
11542 fn
= TREE_VALUE (queue_entry
);
11544 /* Parse the function. */
11545 cp_parser_late_parsing_for_member (parser
, fn
);
11547 TREE_VALUE (parser
->unparsed_functions_queues
)
11548 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
));
11551 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11552 more time than we have popped, so me must pop here. */
11554 pop_scope (last_scope
);
11557 /* Put back any saved access checks. */
11558 pop_deferring_access_checks ();
11560 /* Restore the count of active template-parameter-lists. */
11561 parser
->num_template_parameter_lists
11562 = saved_num_template_parameter_lists
;
11567 /* Parse a class-head.
11570 class-key identifier [opt] base-clause [opt]
11571 class-key nested-name-specifier identifier base-clause [opt]
11572 class-key nested-name-specifier [opt] template-id
11576 class-key attributes identifier [opt] base-clause [opt]
11577 class-key attributes nested-name-specifier identifier base-clause [opt]
11578 class-key attributes nested-name-specifier [opt] template-id
11581 Returns the TYPE of the indicated class. Sets
11582 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11583 involving a nested-name-specifier was used, and FALSE otherwise.
11585 Returns NULL_TREE if the class-head is syntactically valid, but
11586 semantically invalid in a way that means we should skip the entire
11587 body of the class. */
11590 cp_parser_class_head (cp_parser
* parser
,
11591 bool* nested_name_specifier_p
)
11594 tree nested_name_specifier
;
11595 enum tag_types class_key
;
11596 tree id
= NULL_TREE
;
11597 tree type
= NULL_TREE
;
11599 bool template_id_p
= false;
11600 bool qualified_p
= false;
11601 bool invalid_nested_name_p
= false;
11602 unsigned num_templates
;
11604 /* Assume no nested-name-specifier will be present. */
11605 *nested_name_specifier_p
= false;
11606 /* Assume no template parameter lists will be used in defining the
11610 /* Look for the class-key. */
11611 class_key
= cp_parser_class_key (parser
);
11612 if (class_key
== none_type
)
11613 return error_mark_node
;
11615 /* Parse the attributes. */
11616 attributes
= cp_parser_attributes_opt (parser
);
11618 /* If the next token is `::', that is invalid -- but sometimes
11619 people do try to write:
11623 Handle this gracefully by accepting the extra qualifier, and then
11624 issuing an error about it later if this really is a
11625 class-head. If it turns out just to be an elaborated type
11626 specifier, remain silent. */
11627 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
11628 qualified_p
= true;
11630 push_deferring_access_checks (dk_no_check
);
11632 /* Determine the name of the class. Begin by looking for an
11633 optional nested-name-specifier. */
11634 nested_name_specifier
11635 = cp_parser_nested_name_specifier_opt (parser
,
11636 /*typename_keyword_p=*/false,
11637 /*check_dependency_p=*/false,
11639 /* If there was a nested-name-specifier, then there *must* be an
11641 if (nested_name_specifier
)
11643 /* Although the grammar says `identifier', it really means
11644 `class-name' or `template-name'. You are only allowed to
11645 define a class that has already been declared with this
11648 The proposed resolution for Core Issue 180 says that whever
11649 you see `class T::X' you should treat `X' as a type-name.
11651 It is OK to define an inaccessible class; for example:
11653 class A { class B; };
11656 We do not know if we will see a class-name, or a
11657 template-name. We look for a class-name first, in case the
11658 class-name is a template-id; if we looked for the
11659 template-name first we would stop after the template-name. */
11660 cp_parser_parse_tentatively (parser
);
11661 type
= cp_parser_class_name (parser
,
11662 /*typename_keyword_p=*/false,
11663 /*template_keyword_p=*/false,
11665 /*check_dependency_p=*/false,
11666 /*class_head_p=*/true);
11667 /* If that didn't work, ignore the nested-name-specifier. */
11668 if (!cp_parser_parse_definitely (parser
))
11670 invalid_nested_name_p
= true;
11671 id
= cp_parser_identifier (parser
);
11672 if (id
== error_mark_node
)
11675 /* If we could not find a corresponding TYPE, treat this
11676 declaration like an unqualified declaration. */
11677 if (type
== error_mark_node
)
11678 nested_name_specifier
= NULL_TREE
;
11679 /* Otherwise, count the number of templates used in TYPE and its
11680 containing scopes. */
11685 for (scope
= TREE_TYPE (type
);
11686 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
11687 scope
= (TYPE_P (scope
)
11688 ? TYPE_CONTEXT (scope
)
11689 : DECL_CONTEXT (scope
)))
11691 && CLASS_TYPE_P (scope
)
11692 && CLASSTYPE_TEMPLATE_INFO (scope
)
11693 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
11694 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
11698 /* Otherwise, the identifier is optional. */
11701 /* We don't know whether what comes next is a template-id,
11702 an identifier, or nothing at all. */
11703 cp_parser_parse_tentatively (parser
);
11704 /* Check for a template-id. */
11705 id
= cp_parser_template_id (parser
,
11706 /*template_keyword_p=*/false,
11707 /*check_dependency_p=*/true);
11708 /* If that didn't work, it could still be an identifier. */
11709 if (!cp_parser_parse_definitely (parser
))
11711 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11712 id
= cp_parser_identifier (parser
);
11718 template_id_p
= true;
11723 pop_deferring_access_checks ();
11725 /* If it's not a `:' or a `{' then we can't really be looking at a
11726 class-head, since a class-head only appears as part of a
11727 class-specifier. We have to detect this situation before calling
11728 xref_tag, since that has irreversible side-effects. */
11729 if (!cp_parser_next_token_starts_class_definition_p (parser
))
11731 cp_parser_error (parser
, "expected `{' or `:'");
11732 return error_mark_node
;
11735 /* At this point, we're going ahead with the class-specifier, even
11736 if some other problem occurs. */
11737 cp_parser_commit_to_tentative_parse (parser
);
11738 /* Issue the error about the overly-qualified name now. */
11740 cp_parser_error (parser
,
11741 "global qualification of class name is invalid");
11742 else if (invalid_nested_name_p
)
11743 cp_parser_error (parser
,
11744 "qualified name does not name a class");
11745 /* Make sure that the right number of template parameters were
11747 if (!cp_parser_check_template_parameters (parser
, num_templates
))
11748 /* If something went wrong, there is no point in even trying to
11749 process the class-definition. */
11752 /* Look up the type. */
11755 type
= TREE_TYPE (id
);
11756 maybe_process_partial_specialization (type
);
11758 else if (!nested_name_specifier
)
11760 /* If the class was unnamed, create a dummy name. */
11762 id
= make_anon_name ();
11763 type
= xref_tag (class_key
, id
, attributes
, /*globalize=*/0);
11772 template <typename T> struct S { struct T };
11773 template <typename T> struct S<T>::T { };
11775 we will get a TYPENAME_TYPE when processing the definition of
11776 `S::T'. We need to resolve it to the actual type before we
11777 try to define it. */
11778 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
11780 class_type
= resolve_typename_type (TREE_TYPE (type
),
11781 /*only_current_p=*/false);
11782 if (class_type
!= error_mark_node
)
11783 type
= TYPE_NAME (class_type
);
11786 cp_parser_error (parser
, "could not resolve typename type");
11787 type
= error_mark_node
;
11791 /* Figure out in what scope the declaration is being placed. */
11792 scope
= current_scope ();
11794 scope
= current_namespace
;
11795 /* If that scope does not contain the scope in which the
11796 class was originally declared, the program is invalid. */
11797 if (scope
&& !is_ancestor (scope
, CP_DECL_CONTEXT (type
)))
11799 error ("declaration of `%D' in `%D' which does not "
11800 "enclose `%D'", type
, scope
, nested_name_specifier
);
11804 maybe_process_partial_specialization (TREE_TYPE (type
));
11805 class_type
= current_class_type
;
11806 type
= TREE_TYPE (handle_class_head (class_key
,
11807 nested_name_specifier
,
11810 if (type
!= error_mark_node
)
11812 if (!class_type
&& TYPE_CONTEXT (type
))
11813 *nested_name_specifier_p
= true;
11814 else if (class_type
&& !same_type_p (TYPE_CONTEXT (type
),
11816 *nested_name_specifier_p
= true;
11819 /* Indicate whether this class was declared as a `class' or as a
11821 if (TREE_CODE (type
) == RECORD_TYPE
)
11822 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
11823 cp_parser_check_class_key (class_key
, type
);
11825 /* Enter the scope containing the class; the names of base classes
11826 should be looked up in that context. For example, given:
11828 struct A { struct B {}; struct C; };
11829 struct A::C : B {};
11832 if (nested_name_specifier
)
11833 push_scope (nested_name_specifier
);
11834 /* Now, look for the base-clause. */
11835 token
= cp_lexer_peek_token (parser
->lexer
);
11836 if (token
->type
== CPP_COLON
)
11840 /* Get the list of base-classes. */
11841 bases
= cp_parser_base_clause (parser
);
11842 /* Process them. */
11843 xref_basetypes (type
, bases
);
11845 /* Leave the scope given by the nested-name-specifier. We will
11846 enter the class scope itself while processing the members. */
11847 if (nested_name_specifier
)
11848 pop_scope (nested_name_specifier
);
11853 /* Parse a class-key.
11860 Returns the kind of class-key specified, or none_type to indicate
11863 static enum tag_types
11864 cp_parser_class_key (cp_parser
* parser
)
11867 enum tag_types tag_type
;
11869 /* Look for the class-key. */
11870 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
11874 /* Check to see if the TOKEN is a class-key. */
11875 tag_type
= cp_parser_token_is_class_key (token
);
11877 cp_parser_error (parser
, "expected class-key");
11881 /* Parse an (optional) member-specification.
11883 member-specification:
11884 member-declaration member-specification [opt]
11885 access-specifier : member-specification [opt] */
11888 cp_parser_member_specification_opt (cp_parser
* parser
)
11895 /* Peek at the next token. */
11896 token
= cp_lexer_peek_token (parser
->lexer
);
11897 /* If it's a `}', or EOF then we've seen all the members. */
11898 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
11901 /* See if this token is a keyword. */
11902 keyword
= token
->keyword
;
11906 case RID_PROTECTED
:
11908 /* Consume the access-specifier. */
11909 cp_lexer_consume_token (parser
->lexer
);
11910 /* Remember which access-specifier is active. */
11911 current_access_specifier
= token
->value
;
11912 /* Look for the `:'. */
11913 cp_parser_require (parser
, CPP_COLON
, "`:'");
11917 /* Otherwise, the next construction must be a
11918 member-declaration. */
11919 cp_parser_member_declaration (parser
);
11924 /* Parse a member-declaration.
11926 member-declaration:
11927 decl-specifier-seq [opt] member-declarator-list [opt] ;
11928 function-definition ; [opt]
11929 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11931 template-declaration
11933 member-declarator-list:
11935 member-declarator-list , member-declarator
11938 declarator pure-specifier [opt]
11939 declarator constant-initializer [opt]
11940 identifier [opt] : constant-expression
11944 member-declaration:
11945 __extension__ member-declaration
11948 declarator attributes [opt] pure-specifier [opt]
11949 declarator attributes [opt] constant-initializer [opt]
11950 identifier [opt] attributes [opt] : constant-expression */
11953 cp_parser_member_declaration (cp_parser
* parser
)
11955 tree decl_specifiers
;
11956 tree prefix_attributes
;
11958 bool declares_class_or_enum
;
11961 int saved_pedantic
;
11963 /* Check for the `__extension__' keyword. */
11964 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11967 cp_parser_member_declaration (parser
);
11968 /* Restore the old value of the PEDANTIC flag. */
11969 pedantic
= saved_pedantic
;
11974 /* Check for a template-declaration. */
11975 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
11977 /* Parse the template-declaration. */
11978 cp_parser_template_declaration (parser
, /*member_p=*/true);
11983 /* Check for a using-declaration. */
11984 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
11986 /* Parse the using-declaration. */
11987 cp_parser_using_declaration (parser
);
11992 /* We can't tell whether we're looking at a declaration or a
11993 function-definition. */
11994 cp_parser_parse_tentatively (parser
);
11996 /* Parse the decl-specifier-seq. */
11998 = cp_parser_decl_specifier_seq (parser
,
11999 CP_PARSER_FLAGS_OPTIONAL
,
12000 &prefix_attributes
,
12001 &declares_class_or_enum
);
12002 /* Check for an invalid type-name. */
12003 if (cp_parser_diagnose_invalid_type_name (parser
))
12005 /* If there is no declarator, then the decl-specifier-seq should
12007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12009 /* If there was no decl-specifier-seq, and the next token is a
12010 `;', then we have something like:
12016 Each member-declaration shall declare at least one member
12017 name of the class. */
12018 if (!decl_specifiers
)
12021 pedwarn ("extra semicolon");
12027 /* See if this declaration is a friend. */
12028 friend_p
= cp_parser_friend_p (decl_specifiers
);
12029 /* If there were decl-specifiers, check to see if there was
12030 a class-declaration. */
12031 type
= check_tag_decl (decl_specifiers
);
12032 /* Nested classes have already been added to the class, but
12033 a `friend' needs to be explicitly registered. */
12036 /* If the `friend' keyword was present, the friend must
12037 be introduced with a class-key. */
12038 if (!declares_class_or_enum
)
12039 error ("a class-key must be used when declaring a friend");
12042 template <typename T> struct A {
12043 friend struct A<T>::B;
12046 A<T>::B will be represented by a TYPENAME_TYPE, and
12047 therefore not recognized by check_tag_decl. */
12052 for (specifier
= decl_specifiers
;
12054 specifier
= TREE_CHAIN (specifier
))
12056 tree s
= TREE_VALUE (specifier
);
12058 if (TREE_CODE (s
) == IDENTIFIER_NODE
12059 && IDENTIFIER_GLOBAL_VALUE (s
))
12060 type
= IDENTIFIER_GLOBAL_VALUE (s
);
12061 if (TREE_CODE (s
) == TYPE_DECL
)
12071 error ("friend declaration does not name a class or "
12074 make_friend_class (current_class_type
, type
);
12076 /* If there is no TYPE, an error message will already have
12080 /* An anonymous aggregate has to be handled specially; such
12081 a declaration really declares a data member (with a
12082 particular type), as opposed to a nested class. */
12083 else if (ANON_AGGR_TYPE_P (type
))
12085 /* Remove constructors and such from TYPE, now that we
12086 know it is an anoymous aggregate. */
12087 fixup_anonymous_aggr (type
);
12088 /* And make the corresponding data member. */
12089 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
12090 /* Add it to the class. */
12091 finish_member_declaration (decl
);
12097 /* See if these declarations will be friends. */
12098 friend_p
= cp_parser_friend_p (decl_specifiers
);
12100 /* Keep going until we hit the `;' at the end of the
12102 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12104 tree attributes
= NULL_TREE
;
12105 tree first_attribute
;
12107 /* Peek at the next token. */
12108 token
= cp_lexer_peek_token (parser
->lexer
);
12110 /* Check for a bitfield declaration. */
12111 if (token
->type
== CPP_COLON
12112 || (token
->type
== CPP_NAME
12113 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12119 /* Get the name of the bitfield. Note that we cannot just
12120 check TOKEN here because it may have been invalidated by
12121 the call to cp_lexer_peek_nth_token above. */
12122 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
12123 identifier
= cp_parser_identifier (parser
);
12125 identifier
= NULL_TREE
;
12127 /* Consume the `:' token. */
12128 cp_lexer_consume_token (parser
->lexer
);
12129 /* Get the width of the bitfield. */
12131 = cp_parser_constant_expression (parser
,
12132 /*allow_non_constant=*/false,
12135 /* Look for attributes that apply to the bitfield. */
12136 attributes
= cp_parser_attributes_opt (parser
);
12137 /* Remember which attributes are prefix attributes and
12139 first_attribute
= attributes
;
12140 /* Combine the attributes. */
12141 attributes
= chainon (prefix_attributes
, attributes
);
12143 /* Create the bitfield declaration. */
12144 decl
= grokbitfield (identifier
,
12147 /* Apply the attributes. */
12148 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
12154 tree asm_specification
;
12155 bool ctor_dtor_or_conv_p
;
12157 /* Parse the declarator. */
12159 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12160 &ctor_dtor_or_conv_p
);
12162 /* If something went wrong parsing the declarator, make sure
12163 that we at least consume some tokens. */
12164 if (declarator
== error_mark_node
)
12166 /* Skip to the end of the statement. */
12167 cp_parser_skip_to_end_of_statement (parser
);
12171 /* Look for an asm-specification. */
12172 asm_specification
= cp_parser_asm_specification_opt (parser
);
12173 /* Look for attributes that apply to the declaration. */
12174 attributes
= cp_parser_attributes_opt (parser
);
12175 /* Remember which attributes are prefix attributes and
12177 first_attribute
= attributes
;
12178 /* Combine the attributes. */
12179 attributes
= chainon (prefix_attributes
, attributes
);
12181 /* If it's an `=', then we have a constant-initializer or a
12182 pure-specifier. It is not correct to parse the
12183 initializer before registering the member declaration
12184 since the member declaration should be in scope while
12185 its initializer is processed. However, the rest of the
12186 front end does not yet provide an interface that allows
12187 us to handle this correctly. */
12188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12192 A pure-specifier shall be used only in the declaration of
12193 a virtual function.
12195 A member-declarator can contain a constant-initializer
12196 only if it declares a static member of integral or
12199 Therefore, if the DECLARATOR is for a function, we look
12200 for a pure-specifier; otherwise, we look for a
12201 constant-initializer. When we call `grokfield', it will
12202 perform more stringent semantics checks. */
12203 if (TREE_CODE (declarator
) == CALL_EXPR
)
12204 initializer
= cp_parser_pure_specifier (parser
);
12207 /* This declaration cannot be a function
12209 cp_parser_commit_to_tentative_parse (parser
);
12210 /* Parse the initializer. */
12211 initializer
= cp_parser_constant_initializer (parser
);
12214 /* Otherwise, there is no initializer. */
12216 initializer
= NULL_TREE
;
12218 /* See if we are probably looking at a function
12219 definition. We are certainly not looking at at a
12220 member-declarator. Calling `grokfield' has
12221 side-effects, so we must not do it unless we are sure
12222 that we are looking at a member-declarator. */
12223 if (cp_parser_token_starts_function_definition_p
12224 (cp_lexer_peek_token (parser
->lexer
)))
12225 decl
= error_mark_node
;
12227 /* Create the declaration. */
12228 decl
= grokfield (declarator
,
12235 /* Reset PREFIX_ATTRIBUTES. */
12236 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12237 attributes
= TREE_CHAIN (attributes
);
12239 TREE_CHAIN (attributes
) = NULL_TREE
;
12241 /* If there is any qualification still in effect, clear it
12242 now; we will be starting fresh with the next declarator. */
12243 parser
->scope
= NULL_TREE
;
12244 parser
->qualifying_scope
= NULL_TREE
;
12245 parser
->object_scope
= NULL_TREE
;
12246 /* If it's a `,', then there are more declarators. */
12247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12248 cp_lexer_consume_token (parser
->lexer
);
12249 /* If the next token isn't a `;', then we have a parse error. */
12250 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12253 cp_parser_error (parser
, "expected `;'");
12254 /* Skip tokens until we find a `;' */
12255 cp_parser_skip_to_end_of_statement (parser
);
12262 /* Add DECL to the list of members. */
12264 finish_member_declaration (decl
);
12266 /* If DECL is a function, we must return
12267 to parse it later. (Even though there is no definition,
12268 there might be default arguments that need handling.) */
12269 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12270 TREE_VALUE (parser
->unparsed_functions_queues
)
12271 = tree_cons (NULL_TREE
, decl
,
12272 TREE_VALUE (parser
->unparsed_functions_queues
));
12277 /* If everything went well, look for the `;'. */
12278 if (cp_parser_parse_definitely (parser
))
12280 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12284 /* Parse the function-definition. */
12285 decl
= cp_parser_function_definition (parser
, &friend_p
);
12286 /* If the member was not a friend, declare it here. */
12288 finish_member_declaration (decl
);
12289 /* Peek at the next token. */
12290 token
= cp_lexer_peek_token (parser
->lexer
);
12291 /* If the next token is a semicolon, consume it. */
12292 if (token
->type
== CPP_SEMICOLON
)
12293 cp_lexer_consume_token (parser
->lexer
);
12296 /* Parse a pure-specifier.
12301 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12302 Otherwiser, ERROR_MARK_NODE is returned. */
12305 cp_parser_pure_specifier (cp_parser
* parser
)
12309 /* Look for the `=' token. */
12310 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12311 return error_mark_node
;
12312 /* Look for the `0' token. */
12313 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12314 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12315 to get information from the lexer about how the number was
12316 spelled in order to fix this problem. */
12317 if (!token
|| !integer_zerop (token
->value
))
12318 return error_mark_node
;
12320 return integer_zero_node
;
12323 /* Parse a constant-initializer.
12325 constant-initializer:
12326 = constant-expression
12328 Returns a representation of the constant-expression. */
12331 cp_parser_constant_initializer (cp_parser
* parser
)
12333 /* Look for the `=' token. */
12334 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12335 return error_mark_node
;
12337 /* It is invalid to write:
12339 struct S { static const int i = { 7 }; };
12342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12344 cp_parser_error (parser
,
12345 "a brace-enclosed initializer is not allowed here");
12346 /* Consume the opening brace. */
12347 cp_lexer_consume_token (parser
->lexer
);
12348 /* Skip the initializer. */
12349 cp_parser_skip_to_closing_brace (parser
);
12350 /* Look for the trailing `}'. */
12351 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12353 return error_mark_node
;
12356 return cp_parser_constant_expression (parser
,
12357 /*allow_non_constant=*/false,
12361 /* Derived classes [gram.class.derived] */
12363 /* Parse a base-clause.
12366 : base-specifier-list
12368 base-specifier-list:
12370 base-specifier-list , base-specifier
12372 Returns a TREE_LIST representing the base-classes, in the order in
12373 which they were declared. The representation of each node is as
12374 described by cp_parser_base_specifier.
12376 In the case that no bases are specified, this function will return
12377 NULL_TREE, not ERROR_MARK_NODE. */
12380 cp_parser_base_clause (cp_parser
* parser
)
12382 tree bases
= NULL_TREE
;
12384 /* Look for the `:' that begins the list. */
12385 cp_parser_require (parser
, CPP_COLON
, "`:'");
12387 /* Scan the base-specifier-list. */
12393 /* Look for the base-specifier. */
12394 base
= cp_parser_base_specifier (parser
);
12395 /* Add BASE to the front of the list. */
12396 if (base
!= error_mark_node
)
12398 TREE_CHAIN (base
) = bases
;
12401 /* Peek at the next token. */
12402 token
= cp_lexer_peek_token (parser
->lexer
);
12403 /* If it's not a comma, then the list is complete. */
12404 if (token
->type
!= CPP_COMMA
)
12406 /* Consume the `,'. */
12407 cp_lexer_consume_token (parser
->lexer
);
12410 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12411 base class had a qualified name. However, the next name that
12412 appears is certainly not qualified. */
12413 parser
->scope
= NULL_TREE
;
12414 parser
->qualifying_scope
= NULL_TREE
;
12415 parser
->object_scope
= NULL_TREE
;
12417 return nreverse (bases
);
12420 /* Parse a base-specifier.
12423 :: [opt] nested-name-specifier [opt] class-name
12424 virtual access-specifier [opt] :: [opt] nested-name-specifier
12426 access-specifier virtual [opt] :: [opt] nested-name-specifier
12429 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12430 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12431 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12432 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12435 cp_parser_base_specifier (cp_parser
* parser
)
12439 bool virtual_p
= false;
12440 bool duplicate_virtual_error_issued_p
= false;
12441 bool duplicate_access_error_issued_p
= false;
12442 bool class_scope_p
, template_p
;
12443 tree access
= access_default_node
;
12446 /* Process the optional `virtual' and `access-specifier'. */
12449 /* Peek at the next token. */
12450 token
= cp_lexer_peek_token (parser
->lexer
);
12451 /* Process `virtual'. */
12452 switch (token
->keyword
)
12455 /* If `virtual' appears more than once, issue an error. */
12456 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
12458 cp_parser_error (parser
,
12459 "`virtual' specified more than once in base-specified");
12460 duplicate_virtual_error_issued_p
= true;
12465 /* Consume the `virtual' token. */
12466 cp_lexer_consume_token (parser
->lexer
);
12471 case RID_PROTECTED
:
12473 /* If more than one access specifier appears, issue an
12475 if (access
!= access_default_node
12476 && !duplicate_access_error_issued_p
)
12478 cp_parser_error (parser
,
12479 "more than one access specifier in base-specified");
12480 duplicate_access_error_issued_p
= true;
12483 access
= ridpointers
[(int) token
->keyword
];
12485 /* Consume the access-specifier. */
12486 cp_lexer_consume_token (parser
->lexer
);
12496 /* Look for the optional `::' operator. */
12497 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
12498 /* Look for the nested-name-specifier. The simplest way to
12503 The keyword `typename' is not permitted in a base-specifier or
12504 mem-initializer; in these contexts a qualified name that
12505 depends on a template-parameter is implicitly assumed to be a
12508 is to pretend that we have seen the `typename' keyword at this
12510 cp_parser_nested_name_specifier_opt (parser
,
12511 /*typename_keyword_p=*/true,
12512 /*check_dependency_p=*/true,
12514 /* If the base class is given by a qualified name, assume that names
12515 we see are type names or templates, as appropriate. */
12516 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
12517 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
12519 /* Finally, look for the class-name. */
12520 type
= cp_parser_class_name (parser
,
12524 /*check_dependency_p=*/true,
12525 /*class_head_p=*/false);
12527 if (type
== error_mark_node
)
12528 return error_mark_node
;
12530 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
12533 /* Exception handling [gram.exception] */
12535 /* Parse an (optional) exception-specification.
12537 exception-specification:
12538 throw ( type-id-list [opt] )
12540 Returns a TREE_LIST representing the exception-specification. The
12541 TREE_VALUE of each node is a type. */
12544 cp_parser_exception_specification_opt (cp_parser
* parser
)
12549 /* Peek at the next token. */
12550 token
= cp_lexer_peek_token (parser
->lexer
);
12551 /* If it's not `throw', then there's no exception-specification. */
12552 if (!cp_parser_is_keyword (token
, RID_THROW
))
12555 /* Consume the `throw'. */
12556 cp_lexer_consume_token (parser
->lexer
);
12558 /* Look for the `('. */
12559 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12561 /* Peek at the next token. */
12562 token
= cp_lexer_peek_token (parser
->lexer
);
12563 /* If it's not a `)', then there is a type-id-list. */
12564 if (token
->type
!= CPP_CLOSE_PAREN
)
12566 const char *saved_message
;
12568 /* Types may not be defined in an exception-specification. */
12569 saved_message
= parser
->type_definition_forbidden_message
;
12570 parser
->type_definition_forbidden_message
12571 = "types may not be defined in an exception-specification";
12572 /* Parse the type-id-list. */
12573 type_id_list
= cp_parser_type_id_list (parser
);
12574 /* Restore the saved message. */
12575 parser
->type_definition_forbidden_message
= saved_message
;
12578 type_id_list
= empty_except_spec
;
12580 /* Look for the `)'. */
12581 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12583 return type_id_list
;
12586 /* Parse an (optional) type-id-list.
12590 type-id-list , type-id
12592 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12593 in the order that the types were presented. */
12596 cp_parser_type_id_list (cp_parser
* parser
)
12598 tree types
= NULL_TREE
;
12605 /* Get the next type-id. */
12606 type
= cp_parser_type_id (parser
);
12607 /* Add it to the list. */
12608 types
= add_exception_specifier (types
, type
, /*complain=*/1);
12609 /* Peek at the next token. */
12610 token
= cp_lexer_peek_token (parser
->lexer
);
12611 /* If it is not a `,', we are done. */
12612 if (token
->type
!= CPP_COMMA
)
12614 /* Consume the `,'. */
12615 cp_lexer_consume_token (parser
->lexer
);
12618 return nreverse (types
);
12621 /* Parse a try-block.
12624 try compound-statement handler-seq */
12627 cp_parser_try_block (cp_parser
* parser
)
12631 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
12632 try_block
= begin_try_block ();
12633 cp_parser_compound_statement (parser
);
12634 finish_try_block (try_block
);
12635 cp_parser_handler_seq (parser
);
12636 finish_handler_sequence (try_block
);
12641 /* Parse a function-try-block.
12643 function-try-block:
12644 try ctor-initializer [opt] function-body handler-seq */
12647 cp_parser_function_try_block (cp_parser
* parser
)
12650 bool ctor_initializer_p
;
12652 /* Look for the `try' keyword. */
12653 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
12655 /* Let the rest of the front-end know where we are. */
12656 try_block
= begin_function_try_block ();
12657 /* Parse the function-body. */
12659 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
12660 /* We're done with the `try' part. */
12661 finish_function_try_block (try_block
);
12662 /* Parse the handlers. */
12663 cp_parser_handler_seq (parser
);
12664 /* We're done with the handlers. */
12665 finish_function_handler_sequence (try_block
);
12667 return ctor_initializer_p
;
12670 /* Parse a handler-seq.
12673 handler handler-seq [opt] */
12676 cp_parser_handler_seq (cp_parser
* parser
)
12682 /* Parse the handler. */
12683 cp_parser_handler (parser
);
12684 /* Peek at the next token. */
12685 token
= cp_lexer_peek_token (parser
->lexer
);
12686 /* If it's not `catch' then there are no more handlers. */
12687 if (!cp_parser_is_keyword (token
, RID_CATCH
))
12692 /* Parse a handler.
12695 catch ( exception-declaration ) compound-statement */
12698 cp_parser_handler (cp_parser
* parser
)
12703 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
12704 handler
= begin_handler ();
12705 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12706 declaration
= cp_parser_exception_declaration (parser
);
12707 finish_handler_parms (declaration
, handler
);
12708 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12709 cp_parser_compound_statement (parser
);
12710 finish_handler (handler
);
12713 /* Parse an exception-declaration.
12715 exception-declaration:
12716 type-specifier-seq declarator
12717 type-specifier-seq abstract-declarator
12721 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12722 ellipsis variant is used. */
12725 cp_parser_exception_declaration (cp_parser
* parser
)
12727 tree type_specifiers
;
12729 const char *saved_message
;
12731 /* If it's an ellipsis, it's easy to handle. */
12732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12734 /* Consume the `...' token. */
12735 cp_lexer_consume_token (parser
->lexer
);
12739 /* Types may not be defined in exception-declarations. */
12740 saved_message
= parser
->type_definition_forbidden_message
;
12741 parser
->type_definition_forbidden_message
12742 = "types may not be defined in exception-declarations";
12744 /* Parse the type-specifier-seq. */
12745 type_specifiers
= cp_parser_type_specifier_seq (parser
);
12746 /* If it's a `)', then there is no declarator. */
12747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
12748 declarator
= NULL_TREE
;
12750 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
12751 /*ctor_dtor_or_conv_p=*/NULL
);
12753 /* Restore the saved message. */
12754 parser
->type_definition_forbidden_message
= saved_message
;
12756 return start_handler_parms (type_specifiers
, declarator
);
12759 /* Parse a throw-expression.
12762 throw assignment-expresion [opt]
12764 Returns a THROW_EXPR representing the throw-expression. */
12767 cp_parser_throw_expression (cp_parser
* parser
)
12771 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
12772 /* We can't be sure if there is an assignment-expression or not. */
12773 cp_parser_parse_tentatively (parser
);
12775 expression
= cp_parser_assignment_expression (parser
);
12776 /* If it didn't work, this is just a rethrow. */
12777 if (!cp_parser_parse_definitely (parser
))
12778 expression
= NULL_TREE
;
12780 return build_throw (expression
);
12783 /* GNU Extensions */
12785 /* Parse an (optional) asm-specification.
12788 asm ( string-literal )
12790 If the asm-specification is present, returns a STRING_CST
12791 corresponding to the string-literal. Otherwise, returns
12795 cp_parser_asm_specification_opt (cp_parser
* parser
)
12798 tree asm_specification
;
12800 /* Peek at the next token. */
12801 token
= cp_lexer_peek_token (parser
->lexer
);
12802 /* If the next token isn't the `asm' keyword, then there's no
12803 asm-specification. */
12804 if (!cp_parser_is_keyword (token
, RID_ASM
))
12807 /* Consume the `asm' token. */
12808 cp_lexer_consume_token (parser
->lexer
);
12809 /* Look for the `('. */
12810 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12812 /* Look for the string-literal. */
12813 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12815 asm_specification
= token
->value
;
12817 asm_specification
= NULL_TREE
;
12819 /* Look for the `)'. */
12820 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
12822 return asm_specification
;
12825 /* Parse an asm-operand-list.
12829 asm-operand-list , asm-operand
12832 string-literal ( expression )
12833 [ string-literal ] string-literal ( expression )
12835 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12836 each node is the expression. The TREE_PURPOSE is itself a
12837 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12838 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12839 is a STRING_CST for the string literal before the parenthesis. */
12842 cp_parser_asm_operand_list (cp_parser
* parser
)
12844 tree asm_operands
= NULL_TREE
;
12848 tree string_literal
;
12853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
12855 /* Consume the `[' token. */
12856 cp_lexer_consume_token (parser
->lexer
);
12857 /* Read the operand name. */
12858 name
= cp_parser_identifier (parser
);
12859 if (name
!= error_mark_node
)
12860 name
= build_string (IDENTIFIER_LENGTH (name
),
12861 IDENTIFIER_POINTER (name
));
12862 /* Look for the closing `]'. */
12863 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
12867 /* Look for the string-literal. */
12868 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12869 string_literal
= token
? token
->value
: error_mark_node
;
12870 /* Look for the `('. */
12871 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12872 /* Parse the expression. */
12873 expression
= cp_parser_expression (parser
);
12874 /* Look for the `)'. */
12875 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12876 /* Add this operand to the list. */
12877 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
12880 /* If the next token is not a `,', there are no more
12882 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12884 /* Consume the `,'. */
12885 cp_lexer_consume_token (parser
->lexer
);
12888 return nreverse (asm_operands
);
12891 /* Parse an asm-clobber-list.
12895 asm-clobber-list , string-literal
12897 Returns a TREE_LIST, indicating the clobbers in the order that they
12898 appeared. The TREE_VALUE of each node is a STRING_CST. */
12901 cp_parser_asm_clobber_list (cp_parser
* parser
)
12903 tree clobbers
= NULL_TREE
;
12908 tree string_literal
;
12910 /* Look for the string literal. */
12911 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12912 string_literal
= token
? token
->value
: error_mark_node
;
12913 /* Add it to the list. */
12914 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
12915 /* If the next token is not a `,', then the list is
12917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12919 /* Consume the `,' token. */
12920 cp_lexer_consume_token (parser
->lexer
);
12926 /* Parse an (optional) series of attributes.
12929 attributes attribute
12932 __attribute__ (( attribute-list [opt] ))
12934 The return value is as for cp_parser_attribute_list. */
12937 cp_parser_attributes_opt (cp_parser
* parser
)
12939 tree attributes
= NULL_TREE
;
12944 tree attribute_list
;
12946 /* Peek at the next token. */
12947 token
= cp_lexer_peek_token (parser
->lexer
);
12948 /* If it's not `__attribute__', then we're done. */
12949 if (token
->keyword
!= RID_ATTRIBUTE
)
12952 /* Consume the `__attribute__' keyword. */
12953 cp_lexer_consume_token (parser
->lexer
);
12954 /* Look for the two `(' tokens. */
12955 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12956 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12958 /* Peek at the next token. */
12959 token
= cp_lexer_peek_token (parser
->lexer
);
12960 if (token
->type
!= CPP_CLOSE_PAREN
)
12961 /* Parse the attribute-list. */
12962 attribute_list
= cp_parser_attribute_list (parser
);
12964 /* If the next token is a `)', then there is no attribute
12966 attribute_list
= NULL
;
12968 /* Look for the two `)' tokens. */
12969 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12970 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12972 /* Add these new attributes to the list. */
12973 attributes
= chainon (attributes
, attribute_list
);
12979 /* Parse an attribute-list.
12983 attribute-list , attribute
12987 identifier ( identifier )
12988 identifier ( identifier , expression-list )
12989 identifier ( expression-list )
12991 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12992 TREE_PURPOSE of each node is the identifier indicating which
12993 attribute is in use. The TREE_VALUE represents the arguments, if
12997 cp_parser_attribute_list (cp_parser
* parser
)
12999 tree attribute_list
= NULL_TREE
;
13007 /* Look for the identifier. We also allow keywords here; for
13008 example `__attribute__ ((const))' is legal. */
13009 token
= cp_lexer_peek_token (parser
->lexer
);
13010 if (token
->type
!= CPP_NAME
13011 && token
->type
!= CPP_KEYWORD
)
13012 return error_mark_node
;
13013 /* Consume the token. */
13014 token
= cp_lexer_consume_token (parser
->lexer
);
13016 /* Save away the identifier that indicates which attribute this is. */
13017 identifier
= token
->value
;
13018 attribute
= build_tree_list (identifier
, NULL_TREE
);
13020 /* Peek at the next token. */
13021 token
= cp_lexer_peek_token (parser
->lexer
);
13022 /* If it's an `(', then parse the attribute arguments. */
13023 if (token
->type
== CPP_OPEN_PAREN
)
13026 int arguments_allowed_p
= 1;
13028 /* Consume the `('. */
13029 cp_lexer_consume_token (parser
->lexer
);
13030 /* Peek at the next token. */
13031 token
= cp_lexer_peek_token (parser
->lexer
);
13032 /* Check to see if the next token is an identifier. */
13033 if (token
->type
== CPP_NAME
)
13035 /* Save the identifier. */
13036 identifier
= token
->value
;
13037 /* Consume the identifier. */
13038 cp_lexer_consume_token (parser
->lexer
);
13039 /* Peek at the next token. */
13040 token
= cp_lexer_peek_token (parser
->lexer
);
13041 /* If the next token is a `,', then there are some other
13042 expressions as well. */
13043 if (token
->type
== CPP_COMMA
)
13044 /* Consume the comma. */
13045 cp_lexer_consume_token (parser
->lexer
);
13047 arguments_allowed_p
= 0;
13050 identifier
= NULL_TREE
;
13052 /* If there are arguments, parse them too. */
13053 if (arguments_allowed_p
)
13054 arguments
= cp_parser_expression_list (parser
);
13056 arguments
= NULL_TREE
;
13058 /* Combine the identifier and the arguments. */
13060 arguments
= tree_cons (NULL_TREE
, identifier
, arguments
);
13062 /* Save the identifier and arguments away. */
13063 TREE_VALUE (attribute
) = arguments
;
13065 /* Look for the closing `)'. */
13066 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13069 /* Add this attribute to the list. */
13070 TREE_CHAIN (attribute
) = attribute_list
;
13071 attribute_list
= attribute
;
13073 /* Now, look for more attributes. */
13074 token
= cp_lexer_peek_token (parser
->lexer
);
13075 /* If the next token isn't a `,', we're done. */
13076 if (token
->type
!= CPP_COMMA
)
13079 /* Consume the commma and keep going. */
13080 cp_lexer_consume_token (parser
->lexer
);
13083 /* We built up the list in reverse order. */
13084 return nreverse (attribute_list
);
13087 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13088 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13089 current value of the PEDANTIC flag, regardless of whether or not
13090 the `__extension__' keyword is present. The caller is responsible
13091 for restoring the value of the PEDANTIC flag. */
13094 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
13096 /* Save the old value of the PEDANTIC flag. */
13097 *saved_pedantic
= pedantic
;
13099 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
13101 /* Consume the `__extension__' token. */
13102 cp_lexer_consume_token (parser
->lexer
);
13103 /* We're not being pedantic while the `__extension__' keyword is
13113 /* Parse a label declaration.
13116 __label__ label-declarator-seq ;
13118 label-declarator-seq:
13119 identifier , label-declarator-seq
13123 cp_parser_label_declaration (cp_parser
* parser
)
13125 /* Look for the `__label__' keyword. */
13126 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
13132 /* Look for an identifier. */
13133 identifier
= cp_parser_identifier (parser
);
13134 /* Declare it as a lobel. */
13135 finish_label_decl (identifier
);
13136 /* If the next token is a `;', stop. */
13137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13139 /* Look for the `,' separating the label declarations. */
13140 cp_parser_require (parser
, CPP_COMMA
, "`,'");
13143 /* Look for the final `;'. */
13144 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13147 /* Support Functions */
13149 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13150 NAME should have one of the representations used for an
13151 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13152 is returned. If PARSER->SCOPE is a dependent type, then a
13153 SCOPE_REF is returned.
13155 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13156 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13157 was formed. Abstractly, such entities should not be passed to this
13158 function, because they do not need to be looked up, but it is
13159 simpler to check for this special case here, rather than at the
13162 In cases not explicitly covered above, this function returns a
13163 DECL, OVERLOAD, or baselink representing the result of the lookup.
13164 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13167 If IS_TYPE is TRUE, bindings that do not refer to types are
13170 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13173 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13177 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
13178 bool is_type
, bool is_namespace
, bool check_dependency
)
13181 tree object_type
= parser
->context
->object_type
;
13183 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13184 no longer valid. Note that if we are parsing tentatively, and
13185 the parse fails, OBJECT_TYPE will be automatically restored. */
13186 parser
->context
->object_type
= NULL_TREE
;
13188 if (name
== error_mark_node
)
13189 return error_mark_node
;
13191 /* A template-id has already been resolved; there is no lookup to
13193 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13195 if (BASELINK_P (name
))
13197 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
13198 == TEMPLATE_ID_EXPR
),
13203 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13204 it should already have been checked to make sure that the name
13205 used matches the type being destroyed. */
13206 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13210 /* Figure out to which type this destructor applies. */
13212 type
= parser
->scope
;
13213 else if (object_type
)
13214 type
= object_type
;
13216 type
= current_class_type
;
13217 /* If that's not a class type, there is no destructor. */
13218 if (!type
|| !CLASS_TYPE_P (type
))
13219 return error_mark_node
;
13220 /* If it was a class type, return the destructor. */
13221 return CLASSTYPE_DESTRUCTORS (type
);
13224 /* By this point, the NAME should be an ordinary identifier. If
13225 the id-expression was a qualified name, the qualifying scope is
13226 stored in PARSER->SCOPE at this point. */
13227 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
13230 /* Perform the lookup. */
13235 if (parser
->scope
== error_mark_node
)
13236 return error_mark_node
;
13238 /* If the SCOPE is dependent, the lookup must be deferred until
13239 the template is instantiated -- unless we are explicitly
13240 looking up names in uninstantiated templates. Even then, we
13241 cannot look up the name if the scope is not a class type; it
13242 might, for example, be a template type parameter. */
13243 dependent_p
= (TYPE_P (parser
->scope
)
13244 && !(parser
->in_declarator_p
13245 && currently_open_class (parser
->scope
))
13246 && dependent_type_p (parser
->scope
));
13247 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
13251 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
13253 /* The resolution to Core Issue 180 says that `struct A::B'
13254 should be considered a type-name, even if `A' is
13256 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
13262 /* If PARSER->SCOPE is a dependent type, then it must be a
13263 class type, and we must not be checking dependencies;
13264 otherwise, we would have processed this lookup above. So
13265 that PARSER->SCOPE is not considered a dependent base by
13266 lookup_member, we must enter the scope here. */
13268 push_scope (parser
->scope
);
13269 /* If the PARSER->SCOPE is a a template specialization, it
13270 may be instantiated during name lookup. In that case,
13271 errors may be issued. Even if we rollback the current
13272 tentative parse, those errors are valid. */
13273 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
,
13276 pop_scope (parser
->scope
);
13278 parser
->qualifying_scope
= parser
->scope
;
13279 parser
->object_scope
= NULL_TREE
;
13281 else if (object_type
)
13283 tree object_decl
= NULL_TREE
;
13284 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13285 OBJECT_TYPE is not a class. */
13286 if (CLASS_TYPE_P (object_type
))
13287 /* If the OBJECT_TYPE is a template specialization, it may
13288 be instantiated during name lookup. In that case, errors
13289 may be issued. Even if we rollback the current tentative
13290 parse, those errors are valid. */
13291 object_decl
= lookup_member (object_type
,
13293 /*protect=*/0, is_type
);
13294 /* Look it up in the enclosing context, too. */
13295 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13298 parser
->object_scope
= object_type
;
13299 parser
->qualifying_scope
= NULL_TREE
;
13301 decl
= object_decl
;
13305 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13308 parser
->qualifying_scope
= NULL_TREE
;
13309 parser
->object_scope
= NULL_TREE
;
13312 /* If the lookup failed, let our caller know. */
13314 || decl
== error_mark_node
13315 || (TREE_CODE (decl
) == FUNCTION_DECL
13316 && DECL_ANTICIPATED (decl
)))
13317 return error_mark_node
;
13319 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13320 if (TREE_CODE (decl
) == TREE_LIST
)
13322 /* The error message we have to print is too complicated for
13323 cp_parser_error, so we incorporate its actions directly. */
13324 if (!cp_parser_simulate_error (parser
))
13326 error ("reference to `%D' is ambiguous", name
);
13327 print_candidates (decl
);
13329 return error_mark_node
;
13332 my_friendly_assert (DECL_P (decl
)
13333 || TREE_CODE (decl
) == OVERLOAD
13334 || TREE_CODE (decl
) == SCOPE_REF
13335 || BASELINK_P (decl
),
13338 /* If we have resolved the name of a member declaration, check to
13339 see if the declaration is accessible. When the name resolves to
13340 set of overloaded functions, accesibility is checked when
13341 overload resolution is done.
13343 During an explicit instantiation, access is not checked at all,
13344 as per [temp.explicit]. */
13347 tree qualifying_type
;
13349 /* Figure out the type through which DECL is being
13352 = cp_parser_scope_through_which_access_occurs (decl
,
13355 if (qualifying_type
)
13356 perform_or_defer_access_check (qualifying_type
, decl
);
13362 /* Like cp_parser_lookup_name, but for use in the typical case where
13363 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13367 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13369 return cp_parser_lookup_name (parser
, name
,
13371 /*is_namespace=*/false,
13372 /*check_dependency=*/true);
13375 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13376 the current context, return the TYPE_DECL. If TAG_NAME_P is
13377 true, the DECL indicates the class being defined in a class-head,
13378 or declared in an elaborated-type-specifier.
13380 Otherwise, return DECL. */
13383 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13385 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13386 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13389 template <typename T> struct B;
13392 template <typename T> struct A::B {};
13394 Similarly, in a elaborated-type-specifier:
13396 namespace N { struct X{}; }
13399 template <typename T> friend struct N::X;
13402 However, if the DECL refers to a class type, and we are in
13403 the scope of the class, then the name lookup automatically
13404 finds the TYPE_DECL created by build_self_reference rather
13405 than a TEMPLATE_DECL. For example, in:
13407 template <class T> struct S {
13411 there is no need to handle such case. */
13413 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13414 return DECL_TEMPLATE_RESULT (decl
);
13419 /* If too many, or too few, template-parameter lists apply to the
13420 declarator, issue an error message. Returns TRUE if all went well,
13421 and FALSE otherwise. */
13424 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13427 unsigned num_templates
;
13429 /* We haven't seen any classes that involve template parameters yet. */
13432 switch (TREE_CODE (declarator
))
13439 tree main_declarator
= TREE_OPERAND (declarator
, 0);
13441 cp_parser_check_declarator_template_parameters (parser
,
13450 scope
= TREE_OPERAND (declarator
, 0);
13451 member
= TREE_OPERAND (declarator
, 1);
13453 /* If this is a pointer-to-member, then we are not interested
13454 in the SCOPE, because it does not qualify the thing that is
13456 if (TREE_CODE (member
) == INDIRECT_REF
)
13457 return (cp_parser_check_declarator_template_parameters
13460 while (scope
&& CLASS_TYPE_P (scope
))
13462 /* You're supposed to have one `template <...>'
13463 for every template class, but you don't need one
13464 for a full specialization. For example:
13466 template <class T> struct S{};
13467 template <> struct S<int> { void f(); };
13468 void S<int>::f () {}
13470 is correct; there shouldn't be a `template <>' for
13471 the definition of `S<int>::f'. */
13472 if (CLASSTYPE_TEMPLATE_INFO (scope
)
13473 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
13474 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
13475 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
13478 scope
= TYPE_CONTEXT (scope
);
13482 /* Fall through. */
13485 /* If the DECLARATOR has the form `X<y>' then it uses one
13486 additional level of template parameters. */
13487 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
13490 return cp_parser_check_template_parameters (parser
,
13495 /* NUM_TEMPLATES were used in the current declaration. If that is
13496 invalid, return FALSE and issue an error messages. Otherwise,
13500 cp_parser_check_template_parameters (cp_parser
* parser
,
13501 unsigned num_templates
)
13503 /* If there are more template classes than parameter lists, we have
13506 template <class T> void S<T>::R<T>::f (); */
13507 if (parser
->num_template_parameter_lists
< num_templates
)
13509 error ("too few template-parameter-lists");
13512 /* If there are the same number of template classes and parameter
13513 lists, that's OK. */
13514 if (parser
->num_template_parameter_lists
== num_templates
)
13516 /* If there are more, but only one more, then we are referring to a
13517 member template. That's OK too. */
13518 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
13520 /* Otherwise, there are too many template parameter lists. We have
13523 template <class T> template <class U> void S::f(); */
13524 error ("too many template-parameter-lists");
13528 /* Parse a binary-expression of the general form:
13532 binary-expression <token> <expr>
13534 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13535 to parser the <expr>s. If the first production is used, then the
13536 value returned by FN is returned directly. Otherwise, a node with
13537 the indicated EXPR_TYPE is returned, with operands corresponding to
13538 the two sub-expressions. */
13541 cp_parser_binary_expression (cp_parser
* parser
,
13542 const cp_parser_token_tree_map token_tree_map
,
13543 cp_parser_expression_fn fn
)
13547 /* Parse the first expression. */
13548 lhs
= (*fn
) (parser
);
13549 /* Now, look for more expressions. */
13553 const cp_parser_token_tree_map_node
*map_node
;
13556 /* Peek at the next token. */
13557 token
= cp_lexer_peek_token (parser
->lexer
);
13558 /* If the token is `>', and that's not an operator at the
13559 moment, then we're done. */
13560 if (token
->type
== CPP_GREATER
13561 && !parser
->greater_than_is_operator_p
)
13563 /* If we find one of the tokens we want, build the correspoding
13564 tree representation. */
13565 for (map_node
= token_tree_map
;
13566 map_node
->token_type
!= CPP_EOF
;
13568 if (map_node
->token_type
== token
->type
)
13570 /* Consume the operator token. */
13571 cp_lexer_consume_token (parser
->lexer
);
13572 /* Parse the right-hand side of the expression. */
13573 rhs
= (*fn
) (parser
);
13574 /* Build the binary tree node. */
13575 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
);
13579 /* If the token wasn't one of the ones we want, we're done. */
13580 if (map_node
->token_type
== CPP_EOF
)
13587 /* Parse an optional `::' token indicating that the following name is
13588 from the global namespace. If so, PARSER->SCOPE is set to the
13589 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13590 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13591 Returns the new value of PARSER->SCOPE, if the `::' token is
13592 present, and NULL_TREE otherwise. */
13595 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
13599 /* Peek at the next token. */
13600 token
= cp_lexer_peek_token (parser
->lexer
);
13601 /* If we're looking at a `::' token then we're starting from the
13602 global namespace, not our current location. */
13603 if (token
->type
== CPP_SCOPE
)
13605 /* Consume the `::' token. */
13606 cp_lexer_consume_token (parser
->lexer
);
13607 /* Set the SCOPE so that we know where to start the lookup. */
13608 parser
->scope
= global_namespace
;
13609 parser
->qualifying_scope
= global_namespace
;
13610 parser
->object_scope
= NULL_TREE
;
13612 return parser
->scope
;
13614 else if (!current_scope_valid_p
)
13616 parser
->scope
= NULL_TREE
;
13617 parser
->qualifying_scope
= NULL_TREE
;
13618 parser
->object_scope
= NULL_TREE
;
13624 /* Returns TRUE if the upcoming token sequence is the start of a
13625 constructor declarator. If FRIEND_P is true, the declarator is
13626 preceded by the `friend' specifier. */
13629 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
13631 bool constructor_p
;
13632 tree type_decl
= NULL_TREE
;
13633 bool nested_name_p
;
13634 cp_token
*next_token
;
13636 /* The common case is that this is not a constructor declarator, so
13637 try to avoid doing lots of work if at all possible. It's not
13638 valid declare a constructor at function scope. */
13639 if (at_function_scope_p ())
13641 /* And only certain tokens can begin a constructor declarator. */
13642 next_token
= cp_lexer_peek_token (parser
->lexer
);
13643 if (next_token
->type
!= CPP_NAME
13644 && next_token
->type
!= CPP_SCOPE
13645 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
13646 && next_token
->type
!= CPP_TEMPLATE_ID
)
13649 /* Parse tentatively; we are going to roll back all of the tokens
13651 cp_parser_parse_tentatively (parser
);
13652 /* Assume that we are looking at a constructor declarator. */
13653 constructor_p
= true;
13655 /* Look for the optional `::' operator. */
13656 cp_parser_global_scope_opt (parser
,
13657 /*current_scope_valid_p=*/false);
13658 /* Look for the nested-name-specifier. */
13660 = (cp_parser_nested_name_specifier_opt (parser
,
13661 /*typename_keyword_p=*/false,
13662 /*check_dependency_p=*/false,
13665 /* Outside of a class-specifier, there must be a
13666 nested-name-specifier. */
13667 if (!nested_name_p
&&
13668 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
13670 constructor_p
= false;
13671 /* If we still think that this might be a constructor-declarator,
13672 look for a class-name. */
13677 template <typename T> struct S { S(); };
13678 template <typename T> S<T>::S ();
13680 we must recognize that the nested `S' names a class.
13683 template <typename T> S<T>::S<T> ();
13685 we must recognize that the nested `S' names a template. */
13686 type_decl
= cp_parser_class_name (parser
,
13687 /*typename_keyword_p=*/false,
13688 /*template_keyword_p=*/false,
13690 /*check_dependency_p=*/false,
13691 /*class_head_p=*/false);
13692 /* If there was no class-name, then this is not a constructor. */
13693 constructor_p
= !cp_parser_error_occurred (parser
);
13696 /* If we're still considering a constructor, we have to see a `(',
13697 to begin the parameter-declaration-clause, followed by either a
13698 `)', an `...', or a decl-specifier. We need to check for a
13699 type-specifier to avoid being fooled into thinking that:
13703 is a constructor. (It is actually a function named `f' that
13704 takes one parameter (of type `int') and returns a value of type
13707 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
13709 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
13710 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
13711 && !cp_parser_storage_class_specifier_opt (parser
))
13715 /* Names appearing in the type-specifier should be looked up
13716 in the scope of the class. */
13717 if (current_class_type
)
13721 type
= TREE_TYPE (type_decl
);
13722 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13724 type
= resolve_typename_type (type
,
13725 /*only_current_p=*/false);
13726 if (type
== error_mark_node
)
13728 cp_parser_abort_tentative_parse (parser
);
13734 /* Look for the type-specifier. */
13735 cp_parser_type_specifier (parser
,
13736 CP_PARSER_FLAGS_NONE
,
13737 /*is_friend=*/false,
13738 /*is_declarator=*/true,
13739 /*declares_class_or_enum=*/NULL
,
13740 /*is_cv_qualifier=*/NULL
);
13741 /* Leave the scope of the class. */
13745 constructor_p
= !cp_parser_error_occurred (parser
);
13749 constructor_p
= false;
13750 /* We did not really want to consume any tokens. */
13751 cp_parser_abort_tentative_parse (parser
);
13753 return constructor_p
;
13756 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13757 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13758 they must be performed once we are in the scope of the function.
13760 Returns the function defined. */
13763 cp_parser_function_definition_from_specifiers_and_declarator
13764 (cp_parser
* parser
,
13765 tree decl_specifiers
,
13772 /* Begin the function-definition. */
13773 success_p
= begin_function_definition (decl_specifiers
,
13777 /* If there were names looked up in the decl-specifier-seq that we
13778 did not check, check them now. We must wait until we are in the
13779 scope of the function to perform the checks, since the function
13780 might be a friend. */
13781 perform_deferred_access_checks ();
13785 /* If begin_function_definition didn't like the definition, skip
13786 the entire function. */
13787 error ("invalid function declaration");
13788 cp_parser_skip_to_end_of_block_or_statement (parser
);
13789 fn
= error_mark_node
;
13792 fn
= cp_parser_function_definition_after_declarator (parser
,
13793 /*inline_p=*/false);
13798 /* Parse the part of a function-definition that follows the
13799 declarator. INLINE_P is TRUE iff this function is an inline
13800 function defined with a class-specifier.
13802 Returns the function defined. */
13805 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
13809 bool ctor_initializer_p
= false;
13810 bool saved_in_unbraced_linkage_specification_p
;
13811 unsigned saved_num_template_parameter_lists
;
13813 /* If the next token is `return', then the code may be trying to
13814 make use of the "named return value" extension that G++ used to
13816 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
13818 /* Consume the `return' keyword. */
13819 cp_lexer_consume_token (parser
->lexer
);
13820 /* Look for the identifier that indicates what value is to be
13822 cp_parser_identifier (parser
);
13823 /* Issue an error message. */
13824 error ("named return values are no longer supported");
13825 /* Skip tokens until we reach the start of the function body. */
13826 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13827 cp_lexer_consume_token (parser
->lexer
);
13829 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13830 anything declared inside `f'. */
13831 saved_in_unbraced_linkage_specification_p
13832 = parser
->in_unbraced_linkage_specification_p
;
13833 parser
->in_unbraced_linkage_specification_p
= false;
13834 /* Inside the function, surrounding template-parameter-lists do not
13836 saved_num_template_parameter_lists
13837 = parser
->num_template_parameter_lists
;
13838 parser
->num_template_parameter_lists
= 0;
13839 /* If the next token is `try', then we are looking at a
13840 function-try-block. */
13841 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
13842 ctor_initializer_p
= cp_parser_function_try_block (parser
);
13843 /* A function-try-block includes the function-body, so we only do
13844 this next part if we're not processing a function-try-block. */
13847 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13849 /* Finish the function. */
13850 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
13851 (inline_p
? 2 : 0));
13852 /* Generate code for it, if necessary. */
13854 /* Restore the saved values. */
13855 parser
->in_unbraced_linkage_specification_p
13856 = saved_in_unbraced_linkage_specification_p
;
13857 parser
->num_template_parameter_lists
13858 = saved_num_template_parameter_lists
;
13863 /* Parse a template-declaration, assuming that the `export' (and
13864 `extern') keywords, if present, has already been scanned. MEMBER_P
13865 is as for cp_parser_template_declaration. */
13868 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
13870 tree decl
= NULL_TREE
;
13871 tree parameter_list
;
13872 bool friend_p
= false;
13874 /* Look for the `template' keyword. */
13875 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
13879 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
13882 /* Parse the template parameters. */
13883 begin_template_parm_list ();
13884 /* If the next token is `>', then we have an invalid
13885 specialization. Rather than complain about an invalid template
13886 parameter, issue an error message here. */
13887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
13889 cp_parser_error (parser
, "invalid explicit specialization");
13890 parameter_list
= NULL_TREE
;
13893 parameter_list
= cp_parser_template_parameter_list (parser
);
13894 parameter_list
= end_template_parm_list (parameter_list
);
13895 /* Look for the `>'. */
13896 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
13897 /* We just processed one more parameter list. */
13898 ++parser
->num_template_parameter_lists
;
13899 /* If the next token is `template', there are more template
13901 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13903 cp_parser_template_declaration_after_export (parser
, member_p
);
13906 decl
= cp_parser_single_declaration (parser
,
13910 /* If this is a member template declaration, let the front
13912 if (member_p
&& !friend_p
&& decl
)
13913 decl
= finish_member_template_decl (decl
);
13914 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
13915 make_friend_class (current_class_type
, TREE_TYPE (decl
));
13917 /* We are done with the current parameter list. */
13918 --parser
->num_template_parameter_lists
;
13921 finish_template_decl (parameter_list
);
13923 /* Register member declarations. */
13924 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
13925 finish_member_declaration (decl
);
13927 /* If DECL is a function template, we must return to parse it later.
13928 (Even though there is no definition, there might be default
13929 arguments that need handling.) */
13930 if (member_p
&& decl
13931 && (TREE_CODE (decl
) == FUNCTION_DECL
13932 || DECL_FUNCTION_TEMPLATE_P (decl
)))
13933 TREE_VALUE (parser
->unparsed_functions_queues
)
13934 = tree_cons (NULL_TREE
, decl
,
13935 TREE_VALUE (parser
->unparsed_functions_queues
));
13938 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13939 `function-definition' sequence. MEMBER_P is true, this declaration
13940 appears in a class scope.
13942 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13943 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13946 cp_parser_single_declaration (cp_parser
* parser
,
13950 bool declares_class_or_enum
;
13951 tree decl
= NULL_TREE
;
13952 tree decl_specifiers
;
13955 /* Parse the dependent declaration. We don't know yet
13956 whether it will be a function-definition. */
13957 cp_parser_parse_tentatively (parser
);
13958 /* Defer access checks until we know what is being declared. */
13959 push_deferring_access_checks (dk_deferred
);
13961 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13964 = cp_parser_decl_specifier_seq (parser
,
13965 CP_PARSER_FLAGS_OPTIONAL
,
13967 &declares_class_or_enum
);
13968 /* Gather up the access checks that occurred the
13969 decl-specifier-seq. */
13970 stop_deferring_access_checks ();
13972 /* Check for the declaration of a template class. */
13973 if (declares_class_or_enum
)
13975 if (cp_parser_declares_only_class_p (parser
))
13977 decl
= shadow_tag (decl_specifiers
);
13979 decl
= TYPE_NAME (decl
);
13981 decl
= error_mark_node
;
13986 /* If it's not a template class, try for a template function. If
13987 the next token is a `;', then this declaration does not declare
13988 anything. But, if there were errors in the decl-specifiers, then
13989 the error might well have come from an attempted class-specifier.
13990 In that case, there's no need to warn about a missing declarator. */
13992 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
13993 || !value_member (error_mark_node
, decl_specifiers
)))
13994 decl
= cp_parser_init_declarator (parser
,
13997 /*function_definition_allowed_p=*/false,
13999 /*function_definition_p=*/NULL
);
14001 pop_deferring_access_checks ();
14003 /* Clear any current qualification; whatever comes next is the start
14004 of something new. */
14005 parser
->scope
= NULL_TREE
;
14006 parser
->qualifying_scope
= NULL_TREE
;
14007 parser
->object_scope
= NULL_TREE
;
14008 /* Look for a trailing `;' after the declaration. */
14009 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")
14010 && cp_parser_committed_to_tentative_parse (parser
))
14011 cp_parser_skip_to_end_of_block_or_statement (parser
);
14012 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14013 if (cp_parser_parse_definitely (parser
))
14016 *friend_p
= cp_parser_friend_p (decl_specifiers
);
14018 /* Otherwise, try a function-definition. */
14020 decl
= cp_parser_function_definition (parser
, friend_p
);
14025 /* Parse a functional cast to TYPE. Returns an expression
14026 representing the cast. */
14029 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14031 tree expression_list
;
14033 /* Look for the opening `('. */
14034 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14035 return error_mark_node
;
14036 /* If the next token is not an `)', there are arguments to the
14038 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
14039 expression_list
= cp_parser_expression_list (parser
);
14041 expression_list
= NULL_TREE
;
14042 /* Look for the closing `)'. */
14043 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14045 return build_functional_cast (type
, expression_list
);
14048 /* MEMBER_FUNCTION is a member function, or a friend. If default
14049 arguments, or the body of the function have not yet been parsed,
14053 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
14055 cp_lexer
*saved_lexer
;
14057 /* If this member is a template, get the underlying
14059 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
14060 member_function
= DECL_TEMPLATE_RESULT (member_function
);
14062 /* There should not be any class definitions in progress at this
14063 point; the bodies of members are only parsed outside of all class
14065 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
14066 /* While we're parsing the member functions we might encounter more
14067 classes. We want to handle them right away, but we don't want
14068 them getting mixed up with functions that are currently in the
14070 parser
->unparsed_functions_queues
14071 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
14073 /* Make sure that any template parameters are in scope. */
14074 maybe_begin_member_template_processing (member_function
);
14076 /* If the body of the function has not yet been parsed, parse it
14078 if (DECL_PENDING_INLINE_P (member_function
))
14080 tree function_scope
;
14081 cp_token_cache
*tokens
;
14083 /* The function is no longer pending; we are processing it. */
14084 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
14085 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
14086 DECL_PENDING_INLINE_P (member_function
) = 0;
14087 /* If this was an inline function in a local class, enter the scope
14088 of the containing function. */
14089 function_scope
= decl_function_context (member_function
);
14090 if (function_scope
)
14091 push_function_context_to (function_scope
);
14093 /* Save away the current lexer. */
14094 saved_lexer
= parser
->lexer
;
14095 /* Make a new lexer to feed us the tokens saved for this function. */
14096 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14097 parser
->lexer
->next
= saved_lexer
;
14099 /* Set the current source position to be the location of the first
14100 token in the saved inline body. */
14101 cp_lexer_peek_token (parser
->lexer
);
14103 /* Let the front end know that we going to be defining this
14105 start_function (NULL_TREE
, member_function
, NULL_TREE
,
14106 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
14108 /* Now, parse the body of the function. */
14109 cp_parser_function_definition_after_declarator (parser
,
14110 /*inline_p=*/true);
14112 /* Leave the scope of the containing function. */
14113 if (function_scope
)
14114 pop_function_context_from (function_scope
);
14115 /* Restore the lexer. */
14116 parser
->lexer
= saved_lexer
;
14119 /* Remove any template parameters from the symbol table. */
14120 maybe_end_member_template_processing ();
14122 /* Restore the queue. */
14123 parser
->unparsed_functions_queues
14124 = TREE_CHAIN (parser
->unparsed_functions_queues
);
14127 /* FN is a FUNCTION_DECL which may contains a parameter with an
14128 unparsed DEFAULT_ARG. Parse the default args now. */
14131 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
14133 cp_lexer
*saved_lexer
;
14134 cp_token_cache
*tokens
;
14135 bool saved_local_variables_forbidden_p
;
14138 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14140 parameters
= TREE_CHAIN (parameters
))
14142 if (!TREE_PURPOSE (parameters
)
14143 || TREE_CODE (TREE_PURPOSE (parameters
)) != DEFAULT_ARG
)
14146 /* Save away the current lexer. */
14147 saved_lexer
= parser
->lexer
;
14148 /* Create a new one, using the tokens we have saved. */
14149 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parameters
));
14150 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14152 /* Set the current source position to be the location of the
14153 first token in the default argument. */
14154 cp_lexer_peek_token (parser
->lexer
);
14156 /* Local variable names (and the `this' keyword) may not appear
14157 in a default argument. */
14158 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14159 parser
->local_variables_forbidden_p
= true;
14160 /* Parse the assignment-expression. */
14161 if (DECL_CONTEXT (fn
))
14162 push_nested_class (DECL_CONTEXT (fn
));
14163 TREE_PURPOSE (parameters
) = cp_parser_assignment_expression (parser
);
14164 if (DECL_CONTEXT (fn
))
14165 pop_nested_class ();
14167 /* Restore saved state. */
14168 parser
->lexer
= saved_lexer
;
14169 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14173 /* Parse the operand of `sizeof' (or a similar operator). Returns
14174 either a TYPE or an expression, depending on the form of the
14175 input. The KEYWORD indicates which kind of expression we have
14179 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
14181 static const char *format
;
14182 tree expr
= NULL_TREE
;
14183 const char *saved_message
;
14184 bool saved_constant_expression_p
;
14186 /* Initialize FORMAT the first time we get here. */
14188 format
= "types may not be defined in `%s' expressions";
14190 /* Types cannot be defined in a `sizeof' expression. Save away the
14192 saved_message
= parser
->type_definition_forbidden_message
;
14193 /* And create the new one. */
14194 parser
->type_definition_forbidden_message
14196 xmalloc (strlen (format
)
14197 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
14199 sprintf ((char *) parser
->type_definition_forbidden_message
,
14200 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
14202 /* The restrictions on constant-expressions do not apply inside
14203 sizeof expressions. */
14204 saved_constant_expression_p
= parser
->constant_expression_p
;
14205 parser
->constant_expression_p
= false;
14207 /* Do not actually evaluate the expression. */
14209 /* If it's a `(', then we might be looking at the type-id
14211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14215 /* We can't be sure yet whether we're looking at a type-id or an
14217 cp_parser_parse_tentatively (parser
);
14218 /* Consume the `('. */
14219 cp_lexer_consume_token (parser
->lexer
);
14220 /* Parse the type-id. */
14221 type
= cp_parser_type_id (parser
);
14222 /* Now, look for the trailing `)'. */
14223 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14224 /* If all went well, then we're done. */
14225 if (cp_parser_parse_definitely (parser
))
14227 /* Build a list of decl-specifiers; right now, we have only
14228 a single type-specifier. */
14229 type
= build_tree_list (NULL_TREE
,
14232 /* Call grokdeclarator to figure out what type this is. */
14233 expr
= grokdeclarator (NULL_TREE
,
14237 /*attrlist=*/NULL
);
14241 /* If the type-id production did not work out, then we must be
14242 looking at the unary-expression production. */
14244 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
14245 /* Go back to evaluating expressions. */
14248 /* Free the message we created. */
14249 free ((char *) parser
->type_definition_forbidden_message
);
14250 /* And restore the old one. */
14251 parser
->type_definition_forbidden_message
= saved_message
;
14252 parser
->constant_expression_p
= saved_constant_expression_p
;
14257 /* If the current declaration has no declarator, return true. */
14260 cp_parser_declares_only_class_p (cp_parser
*parser
)
14262 /* If the next token is a `;' or a `,' then there is no
14264 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14265 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14268 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14269 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14272 cp_parser_friend_p (tree decl_specifiers
)
14274 while (decl_specifiers
)
14276 /* See if this decl-specifier is `friend'. */
14277 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
14278 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
14281 /* Go on to the next decl-specifier. */
14282 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
14288 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14289 issue an error message indicating that TOKEN_DESC was expected.
14291 Returns the token consumed, if the token had the appropriate type.
14292 Otherwise, returns NULL. */
14295 cp_parser_require (cp_parser
* parser
,
14296 enum cpp_ttype type
,
14297 const char* token_desc
)
14299 if (cp_lexer_next_token_is (parser
->lexer
, type
))
14300 return cp_lexer_consume_token (parser
->lexer
);
14303 /* Output the MESSAGE -- unless we're parsing tentatively. */
14304 if (!cp_parser_simulate_error (parser
))
14305 error ("expected %s", token_desc
);
14310 /* Like cp_parser_require, except that tokens will be skipped until
14311 the desired token is found. An error message is still produced if
14312 the next token is not as expected. */
14315 cp_parser_skip_until_found (cp_parser
* parser
,
14316 enum cpp_ttype type
,
14317 const char* token_desc
)
14320 unsigned nesting_depth
= 0;
14322 if (cp_parser_require (parser
, type
, token_desc
))
14325 /* Skip tokens until the desired token is found. */
14328 /* Peek at the next token. */
14329 token
= cp_lexer_peek_token (parser
->lexer
);
14330 /* If we've reached the token we want, consume it and
14332 if (token
->type
== type
&& !nesting_depth
)
14334 cp_lexer_consume_token (parser
->lexer
);
14337 /* If we've run out of tokens, stop. */
14338 if (token
->type
== CPP_EOF
)
14340 if (token
->type
== CPP_OPEN_BRACE
14341 || token
->type
== CPP_OPEN_PAREN
14342 || token
->type
== CPP_OPEN_SQUARE
)
14344 else if (token
->type
== CPP_CLOSE_BRACE
14345 || token
->type
== CPP_CLOSE_PAREN
14346 || token
->type
== CPP_CLOSE_SQUARE
)
14348 if (nesting_depth
-- == 0)
14351 /* Consume this token. */
14352 cp_lexer_consume_token (parser
->lexer
);
14356 /* If the next token is the indicated keyword, consume it. Otherwise,
14357 issue an error message indicating that TOKEN_DESC was expected.
14359 Returns the token consumed, if the token had the appropriate type.
14360 Otherwise, returns NULL. */
14363 cp_parser_require_keyword (cp_parser
* parser
,
14365 const char* token_desc
)
14367 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
14369 if (token
&& token
->keyword
!= keyword
)
14371 dyn_string_t error_msg
;
14373 /* Format the error message. */
14374 error_msg
= dyn_string_new (0);
14375 dyn_string_append_cstr (error_msg
, "expected ");
14376 dyn_string_append_cstr (error_msg
, token_desc
);
14377 cp_parser_error (parser
, error_msg
->s
);
14378 dyn_string_delete (error_msg
);
14385 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14386 function-definition. */
14389 cp_parser_token_starts_function_definition_p (cp_token
* token
)
14391 return (/* An ordinary function-body begins with an `{'. */
14392 token
->type
== CPP_OPEN_BRACE
14393 /* A ctor-initializer begins with a `:'. */
14394 || token
->type
== CPP_COLON
14395 /* A function-try-block begins with `try'. */
14396 || token
->keyword
== RID_TRY
14397 /* The named return value extension begins with `return'. */
14398 || token
->keyword
== RID_RETURN
);
14401 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14405 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
14409 token
= cp_lexer_peek_token (parser
->lexer
);
14410 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
14413 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14414 or none_type otherwise. */
14416 static enum tag_types
14417 cp_parser_token_is_class_key (cp_token
* token
)
14419 switch (token
->keyword
)
14424 return record_type
;
14433 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14436 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
14438 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
14439 pedwarn ("`%s' tag used in naming `%#T'",
14440 class_key
== union_type
? "union"
14441 : class_key
== record_type
? "struct" : "class",
14445 /* Look for the `template' keyword, as a syntactic disambiguator.
14446 Return TRUE iff it is present, in which case it will be
14450 cp_parser_optional_template_keyword (cp_parser
*parser
)
14452 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14454 /* The `template' keyword can only be used within templates;
14455 outside templates the parser can always figure out what is a
14456 template and what is not. */
14457 if (!processing_template_decl
)
14459 error ("`template' (as a disambiguator) is only allowed "
14460 "within templates");
14461 /* If this part of the token stream is rescanned, the same
14462 error message would be generated. So, we purge the token
14463 from the stream. */
14464 cp_lexer_purge_token (parser
->lexer
);
14469 /* Consume the `template' keyword. */
14470 cp_lexer_consume_token (parser
->lexer
);
14478 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14479 set PARSER->SCOPE, and perform other related actions. */
14482 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
14487 /* Get the stored value. */
14488 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
14489 /* Perform any access checks that were deferred. */
14490 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
14491 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
14492 /* Set the scope from the stored value. */
14493 parser
->scope
= TREE_VALUE (value
);
14494 parser
->qualifying_scope
= TREE_TYPE (value
);
14495 parser
->object_scope
= NULL_TREE
;
14498 /* Add tokens to CACHE until an non-nested END token appears. */
14501 cp_parser_cache_group (cp_parser
*parser
,
14502 cp_token_cache
*cache
,
14503 enum cpp_ttype end
,
14510 /* Abort a parenthesized expression if we encounter a brace. */
14511 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
14512 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14514 /* Consume the next token. */
14515 token
= cp_lexer_consume_token (parser
->lexer
);
14516 /* If we've reached the end of the file, stop. */
14517 if (token
->type
== CPP_EOF
)
14519 /* Add this token to the tokens we are saving. */
14520 cp_token_cache_push_token (cache
, token
);
14521 /* See if it starts a new group. */
14522 if (token
->type
== CPP_OPEN_BRACE
)
14524 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
14528 else if (token
->type
== CPP_OPEN_PAREN
)
14529 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
14530 else if (token
->type
== end
)
14535 /* Begin parsing tentatively. We always save tokens while parsing
14536 tentatively so that if the tentative parsing fails we can restore the
14540 cp_parser_parse_tentatively (cp_parser
* parser
)
14542 /* Enter a new parsing context. */
14543 parser
->context
= cp_parser_context_new (parser
->context
);
14544 /* Begin saving tokens. */
14545 cp_lexer_save_tokens (parser
->lexer
);
14546 /* In order to avoid repetitive access control error messages,
14547 access checks are queued up until we are no longer parsing
14549 push_deferring_access_checks (dk_deferred
);
14552 /* Commit to the currently active tentative parse. */
14555 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
14557 cp_parser_context
*context
;
14560 /* Mark all of the levels as committed. */
14561 lexer
= parser
->lexer
;
14562 for (context
= parser
->context
; context
->next
; context
= context
->next
)
14564 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
14566 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
14567 while (!cp_lexer_saving_tokens (lexer
))
14568 lexer
= lexer
->next
;
14569 cp_lexer_commit_tokens (lexer
);
14573 /* Abort the currently active tentative parse. All consumed tokens
14574 will be rolled back, and no diagnostics will be issued. */
14577 cp_parser_abort_tentative_parse (cp_parser
* parser
)
14579 cp_parser_simulate_error (parser
);
14580 /* Now, pretend that we want to see if the construct was
14581 successfully parsed. */
14582 cp_parser_parse_definitely (parser
);
14585 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14586 token stream. Otherwise, commit to the tokens we have consumed.
14587 Returns true if no error occurred; false otherwise. */
14590 cp_parser_parse_definitely (cp_parser
* parser
)
14592 bool error_occurred
;
14593 cp_parser_context
*context
;
14595 /* Remember whether or not an error ocurred, since we are about to
14596 destroy that information. */
14597 error_occurred
= cp_parser_error_occurred (parser
);
14598 /* Remove the topmost context from the stack. */
14599 context
= parser
->context
;
14600 parser
->context
= context
->next
;
14601 /* If no parse errors occurred, commit to the tentative parse. */
14602 if (!error_occurred
)
14604 /* Commit to the tokens read tentatively, unless that was
14606 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
14607 cp_lexer_commit_tokens (parser
->lexer
);
14609 pop_to_parent_deferring_access_checks ();
14611 /* Otherwise, if errors occurred, roll back our state so that things
14612 are just as they were before we began the tentative parse. */
14615 cp_lexer_rollback_tokens (parser
->lexer
);
14616 pop_deferring_access_checks ();
14618 /* Add the context to the front of the free list. */
14619 context
->next
= cp_parser_context_free_list
;
14620 cp_parser_context_free_list
= context
;
14622 return !error_occurred
;
14625 /* Returns true if we are parsing tentatively -- but have decided that
14626 we will stick with this tentative parse, even if errors occur. */
14629 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
14631 return (cp_parser_parsing_tentatively (parser
)
14632 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
14635 /* Returns nonzero iff an error has occurred during the most recent
14636 tentative parse. */
14639 cp_parser_error_occurred (cp_parser
* parser
)
14641 return (cp_parser_parsing_tentatively (parser
)
14642 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
14645 /* Returns nonzero if GNU extensions are allowed. */
14648 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
14650 return parser
->allow_gnu_extensions_p
;
14657 static GTY (()) cp_parser
*the_parser
;
14659 /* External interface. */
14661 /* Parse the entire translation unit. */
14666 bool error_occurred
;
14668 the_parser
= cp_parser_new ();
14669 push_deferring_access_checks (flag_access_control
14670 ? dk_no_deferred
: dk_no_check
);
14671 error_occurred
= cp_parser_translation_unit (the_parser
);
14676 return error_occurred
;
14679 /* Clean up after parsing the entire translation unit. */
14682 free_parser_stacks (void)
14684 /* Nothing to do. */
14687 /* This variable must be provided by every front end. */
14691 #include "gt-cp-parser.h"