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 constraints 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 /* Strings should have type `const char []'. Right now, we will
483 have an ARRAY_TYPE that is constant rather than an array of
485 FIXME: Make fix_string_type get this right in the first place. */
486 if ((token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
)
487 && flag_const_strings
)
491 /* Get the current type. It will be an ARRAY_TYPE. */
492 type
= TREE_TYPE (token
->value
);
493 /* Use build_cplus_array_type to rebuild the array, thereby
494 getting the right type. */
495 type
= build_cplus_array_type (TREE_TYPE (type
), TYPE_DOMAIN (type
));
496 /* Reset the type of the token. */
497 TREE_TYPE (token
->value
) = type
;
503 /* If the circular buffer is full, make it bigger. */
506 cp_lexer_maybe_grow_buffer (cp_lexer
* lexer
)
508 /* If the buffer is full, enlarge it. */
509 if (lexer
->last_token
== lexer
->first_token
)
511 cp_token
*new_buffer
;
512 cp_token
*old_buffer
;
513 cp_token
*new_first_token
;
514 ptrdiff_t buffer_length
;
515 size_t num_tokens_to_copy
;
517 /* Remember the current buffer pointer. It will become invalid,
518 but we will need to do pointer arithmetic involving this
520 old_buffer
= lexer
->buffer
;
521 /* Compute the current buffer size. */
522 buffer_length
= lexer
->buffer_end
- lexer
->buffer
;
523 /* Allocate a buffer twice as big. */
524 new_buffer
= ((cp_token
*)
525 ggc_realloc (lexer
->buffer
,
526 2 * buffer_length
* sizeof (cp_token
)));
528 /* Because the buffer is circular, logically consecutive tokens
529 are not necessarily placed consecutively in memory.
530 Therefore, we must keep move the tokens that were before
531 FIRST_TOKEN to the second half of the newly allocated
533 num_tokens_to_copy
= (lexer
->first_token
- old_buffer
);
534 memcpy (new_buffer
+ buffer_length
,
536 num_tokens_to_copy
* sizeof (cp_token
));
537 /* Clear the rest of the buffer. We never look at this storage,
538 but the garbage collector may. */
539 memset (new_buffer
+ buffer_length
+ num_tokens_to_copy
, 0,
540 (buffer_length
- num_tokens_to_copy
) * sizeof (cp_token
));
542 /* Now recompute all of the buffer pointers. */
544 = new_buffer
+ (lexer
->first_token
- old_buffer
);
545 if (lexer
->next_token
!= NULL
)
547 ptrdiff_t next_token_delta
;
549 if (lexer
->next_token
> lexer
->first_token
)
550 next_token_delta
= lexer
->next_token
- lexer
->first_token
;
553 buffer_length
- (lexer
->first_token
- lexer
->next_token
);
554 lexer
->next_token
= new_first_token
+ next_token_delta
;
556 lexer
->last_token
= new_first_token
+ buffer_length
;
557 lexer
->buffer
= new_buffer
;
558 lexer
->buffer_end
= new_buffer
+ buffer_length
* 2;
559 lexer
->first_token
= new_first_token
;
563 /* Store the next token from the preprocessor in *TOKEN. */
566 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
571 /* If this not the main lexer, return a terminating CPP_EOF token. */
572 if (lexer
!= NULL
&& !lexer
->main_lexer_p
)
574 token
->type
= CPP_EOF
;
575 token
->location
.line
= 0;
576 token
->location
.file
= NULL
;
577 token
->value
= NULL_TREE
;
578 token
->keyword
= RID_MAX
;
584 /* Keep going until we get a token we like. */
587 /* Get a new token from the preprocessor. */
588 token
->type
= c_lex (&token
->value
);
589 /* Issue messages about tokens we cannot process. */
595 error ("invalid token");
599 /* This is a good token, so we exit the loop. */
604 /* Now we've got our token. */
605 token
->location
= input_location
;
607 /* Check to see if this token is a keyword. */
608 if (token
->type
== CPP_NAME
609 && C_IS_RESERVED_WORD (token
->value
))
611 /* Mark this token as a keyword. */
612 token
->type
= CPP_KEYWORD
;
613 /* Record which keyword. */
614 token
->keyword
= C_RID_CODE (token
->value
);
615 /* Update the value. Some keywords are mapped to particular
616 entities, rather than simply having the value of the
617 corresponding IDENTIFIER_NODE. For example, `__const' is
618 mapped to `const'. */
619 token
->value
= ridpointers
[token
->keyword
];
622 token
->keyword
= RID_MAX
;
625 /* Return a pointer to the next token in the token stream, but do not
629 cp_lexer_peek_token (cp_lexer
* lexer
)
633 /* If there are no tokens, read one now. */
634 if (!lexer
->next_token
)
635 cp_lexer_read_token (lexer
);
637 /* Provide debugging output. */
638 if (cp_lexer_debugging_p (lexer
))
640 fprintf (cp_lexer_debug_stream
, "cp_lexer: peeking at token: ");
641 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
642 fprintf (cp_lexer_debug_stream
, "\n");
645 token
= lexer
->next_token
;
646 cp_lexer_set_source_position_from_token (lexer
, token
);
650 /* Return true if the next token has the indicated TYPE. */
653 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
657 /* Peek at the next token. */
658 token
= cp_lexer_peek_token (lexer
);
659 /* Check to see if it has the indicated TYPE. */
660 return token
->type
== type
;
663 /* Return true if the next token does not have the indicated TYPE. */
666 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
668 return !cp_lexer_next_token_is (lexer
, type
);
671 /* Return true if the next token is the indicated KEYWORD. */
674 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
678 /* Peek at the next token. */
679 token
= cp_lexer_peek_token (lexer
);
680 /* Check to see if it is the indicated keyword. */
681 return token
->keyword
== keyword
;
684 /* Return a pointer to the Nth token in the token stream. If N is 1,
685 then this is precisely equivalent to cp_lexer_peek_token. */
688 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
692 /* N is 1-based, not zero-based. */
693 my_friendly_assert (n
> 0, 20000224);
695 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
696 token
= lexer
->next_token
;
697 /* If there are no tokens in the buffer, get one now. */
700 cp_lexer_read_token (lexer
);
701 token
= lexer
->next_token
;
704 /* Now, read tokens until we have enough. */
707 /* Advance to the next token. */
708 token
= cp_lexer_next_token (lexer
, token
);
709 /* If that's all the tokens we have, read a new one. */
710 if (token
== lexer
->last_token
)
711 token
= cp_lexer_read_token (lexer
);
717 /* Consume the next token. The pointer returned is valid only until
718 another token is read. Callers should preserve copy the token
719 explicitly if they will need its value for a longer period of
723 cp_lexer_consume_token (cp_lexer
* lexer
)
727 /* If there are no tokens, read one now. */
728 if (!lexer
->next_token
)
729 cp_lexer_read_token (lexer
);
731 /* Remember the token we'll be returning. */
732 token
= lexer
->next_token
;
734 /* Increment NEXT_TOKEN. */
735 lexer
->next_token
= cp_lexer_next_token (lexer
,
737 /* Check to see if we're all out of tokens. */
738 if (lexer
->next_token
== lexer
->last_token
)
739 lexer
->next_token
= NULL
;
741 /* If we're not saving tokens, then move FIRST_TOKEN too. */
742 if (!cp_lexer_saving_tokens (lexer
))
744 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
745 if (!lexer
->next_token
)
746 lexer
->first_token
= NULL
;
748 lexer
->first_token
= lexer
->next_token
;
751 /* Provide debugging output. */
752 if (cp_lexer_debugging_p (lexer
))
754 fprintf (cp_lexer_debug_stream
, "cp_lexer: consuming token: ");
755 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
756 fprintf (cp_lexer_debug_stream
, "\n");
762 /* Permanently remove the next token from the token stream. There
763 must be a valid next token already; this token never reads
764 additional tokens from the preprocessor. */
767 cp_lexer_purge_token (cp_lexer
*lexer
)
770 cp_token
*next_token
;
772 token
= lexer
->next_token
;
775 next_token
= cp_lexer_next_token (lexer
, token
);
776 if (next_token
== lexer
->last_token
)
778 *token
= *next_token
;
782 lexer
->last_token
= token
;
783 /* The token purged may have been the only token remaining; if so,
785 if (lexer
->next_token
== token
)
786 lexer
->next_token
= NULL
;
789 /* Permanently remove all tokens after TOKEN, up to, but not
790 including, the token that will be returned next by
791 cp_lexer_peek_token. */
794 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*token
)
800 if (lexer
->next_token
)
802 /* Copy the tokens that have not yet been read to the location
803 immediately following TOKEN. */
804 t1
= cp_lexer_next_token (lexer
, token
);
805 t2
= peek
= cp_lexer_peek_token (lexer
);
806 /* Move tokens into the vacant area between TOKEN and PEEK. */
807 while (t2
!= lexer
->last_token
)
810 t1
= cp_lexer_next_token (lexer
, t1
);
811 t2
= cp_lexer_next_token (lexer
, t2
);
813 /* Now, the next available token is right after TOKEN. */
814 lexer
->next_token
= cp_lexer_next_token (lexer
, token
);
815 /* And the last token is wherever we ended up. */
816 lexer
->last_token
= t1
;
820 /* There are no tokens in the buffer, so there is nothing to
821 copy. The last token in the buffer is TOKEN itself. */
822 lexer
->last_token
= cp_lexer_next_token (lexer
, token
);
826 /* Begin saving tokens. All tokens consumed after this point will be
830 cp_lexer_save_tokens (cp_lexer
* lexer
)
832 /* Provide debugging output. */
833 if (cp_lexer_debugging_p (lexer
))
834 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
836 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
837 restore the tokens if required. */
838 if (!lexer
->next_token
)
839 cp_lexer_read_token (lexer
);
841 VARRAY_PUSH_INT (lexer
->saved_tokens
,
842 cp_lexer_token_difference (lexer
,
847 /* Commit to the portion of the token stream most recently saved. */
850 cp_lexer_commit_tokens (cp_lexer
* lexer
)
852 /* Provide debugging output. */
853 if (cp_lexer_debugging_p (lexer
))
854 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
856 VARRAY_POP (lexer
->saved_tokens
);
859 /* Return all tokens saved since the last call to cp_lexer_save_tokens
860 to the token stream. Stop saving tokens. */
863 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
867 /* Provide debugging output. */
868 if (cp_lexer_debugging_p (lexer
))
869 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
871 /* Find the token that was the NEXT_TOKEN when we started saving
873 delta
= VARRAY_TOP_INT(lexer
->saved_tokens
);
874 /* Make it the next token again now. */
875 lexer
->next_token
= cp_lexer_advance_token (lexer
,
878 /* It might be the case that there were no tokens when we started
879 saving tokens, but that there are some tokens now. */
880 if (!lexer
->next_token
&& lexer
->first_token
)
881 lexer
->next_token
= lexer
->first_token
;
883 /* Stop saving tokens. */
884 VARRAY_POP (lexer
->saved_tokens
);
887 /* Print a representation of the TOKEN on the STREAM. */
890 cp_lexer_print_token (FILE * stream
, cp_token
* token
)
892 const char *token_type
= NULL
;
894 /* Figure out what kind of token this is. */
902 token_type
= "COMMA";
906 token_type
= "OPEN_PAREN";
909 case CPP_CLOSE_PAREN
:
910 token_type
= "CLOSE_PAREN";
914 token_type
= "OPEN_BRACE";
917 case CPP_CLOSE_BRACE
:
918 token_type
= "CLOSE_BRACE";
922 token_type
= "SEMICOLON";
934 token_type
= "keyword";
937 /* This is not a token that we know how to handle yet. */
942 /* If we have a name for the token, print it out. Otherwise, we
943 simply give the numeric code. */
945 fprintf (stream
, "%s", token_type
);
947 fprintf (stream
, "%d", token
->type
);
948 /* And, for an identifier, print the identifier name. */
949 if (token
->type
== CPP_NAME
950 /* Some keywords have a value that is not an IDENTIFIER_NODE.
951 For example, `struct' is mapped to an INTEGER_CST. */
952 || (token
->type
== CPP_KEYWORD
953 && TREE_CODE (token
->value
) == IDENTIFIER_NODE
))
954 fprintf (stream
, " %s", IDENTIFIER_POINTER (token
->value
));
957 /* Start emitting debugging information. */
960 cp_lexer_start_debugging (cp_lexer
* lexer
)
962 ++lexer
->debugging_p
;
965 /* Stop emitting debugging information. */
968 cp_lexer_stop_debugging (cp_lexer
* lexer
)
970 --lexer
->debugging_p
;
979 A cp_parser parses the token stream as specified by the C++
980 grammar. Its job is purely parsing, not semantic analysis. For
981 example, the parser breaks the token stream into declarators,
982 expressions, statements, and other similar syntactic constructs.
983 It does not check that the types of the expressions on either side
984 of an assignment-statement are compatible, or that a function is
985 not declared with a parameter of type `void'.
987 The parser invokes routines elsewhere in the compiler to perform
988 semantic analysis and to build up the abstract syntax tree for the
991 The parser (and the template instantiation code, which is, in a
992 way, a close relative of parsing) are the only parts of the
993 compiler that should be calling push_scope and pop_scope, or
994 related functions. The parser (and template instantiation code)
995 keeps track of what scope is presently active; everything else
996 should simply honor that. (The code that generates static
997 initializers may also need to set the scope, in order to check
998 access control correctly when emitting the initializers.)
1003 The parser is of the standard recursive-descent variety. Upcoming
1004 tokens in the token stream are examined in order to determine which
1005 production to use when parsing a non-terminal. Some C++ constructs
1006 require arbitrary look ahead to disambiguate. For example, it is
1007 impossible, in the general case, to tell whether a statement is an
1008 expression or declaration without scanning the entire statement.
1009 Therefore, the parser is capable of "parsing tentatively." When the
1010 parser is not sure what construct comes next, it enters this mode.
1011 Then, while we attempt to parse the construct, the parser queues up
1012 error messages, rather than issuing them immediately, and saves the
1013 tokens it consumes. If the construct is parsed successfully, the
1014 parser "commits", i.e., it issues any queued error messages and
1015 the tokens that were being preserved are permanently discarded.
1016 If, however, the construct is not parsed successfully, the parser
1017 rolls back its state completely so that it can resume parsing using
1018 a different alternative.
1023 The performance of the parser could probably be improved
1024 substantially. Some possible improvements include:
1026 - The expression parser recurses through the various levels of
1027 precedence as specified in the grammar, rather than using an
1028 operator-precedence technique. Therefore, parsing a simple
1029 identifier requires multiple recursive calls.
1031 - We could often eliminate the need to parse tentatively by
1032 looking ahead a little bit. In some places, this approach
1033 might not entirely eliminate the need to parse tentatively, but
1034 it might still speed up the average case. */
1036 /* Flags that are passed to some parsing functions. These values can
1037 be bitwise-ored together. */
1039 typedef enum cp_parser_flags
1042 CP_PARSER_FLAGS_NONE
= 0x0,
1043 /* The construct is optional. If it is not present, then no error
1044 should be issued. */
1045 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1046 /* When parsing a type-specifier, do not allow user-defined types. */
1047 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1050 /* The different kinds of ids that we ecounter. */
1052 typedef enum cp_parser_id_kind
1054 /* Not an id at all. */
1055 CP_PARSER_ID_KIND_NONE
,
1056 /* An unqualified-id that is not a template-id. */
1057 CP_PARSER_ID_KIND_UNQUALIFIED
,
1058 /* An unqualified template-id. */
1059 CP_PARSER_ID_KIND_TEMPLATE_ID
,
1060 /* A qualified-id. */
1061 CP_PARSER_ID_KIND_QUALIFIED
1062 } cp_parser_id_kind
;
1064 /* The different kinds of declarators we want to parse. */
1066 typedef enum cp_parser_declarator_kind
1068 /* We want an abstract declartor. */
1069 CP_PARSER_DECLARATOR_ABSTRACT
,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED
,
1072 /* We don't mind, but the name must be an unqualified-id */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind
;
1076 /* A mapping from a token type to a corresponding tree node type. */
1078 typedef struct cp_parser_token_tree_map_node
1080 /* The token type. */
1081 enum cpp_ttype token_type
;
1082 /* The corresponding tree code. */
1083 enum tree_code tree_type
;
1084 } cp_parser_token_tree_map_node
;
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map
[];
1091 /* The status of a tentative parse. */
1093 typedef enum cp_parser_status_kind
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR
,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR
,
1099 /* We are committed to this tentative parse, whether or not an error
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind
;
1104 /* Context that is saved and restored when parsing tentatively. */
1106 typedef struct cp_parser_context
GTY (())
1108 /* If this is a tentative parsing context, the status of the
1110 enum cp_parser_status_kind status
;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context
*next
;
1118 } cp_parser_context
;
1122 /* Constructors and destructors. */
1124 static cp_parser_context
*cp_parser_context_new
1125 (cp_parser_context
*);
1127 /* Class variables. */
1129 static GTY((deletable (""))) cp_parser_context
* cp_parser_context_free_list
;
1131 /* Constructors and destructors. */
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1136 static cp_parser_context
*
1137 cp_parser_context_new (cp_parser_context
* next
)
1139 cp_parser_context
*context
;
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list
!= NULL
)
1144 /* Pull the first entry from the free list. */
1145 context
= cp_parser_context_free_list
;
1146 cp_parser_context_free_list
= context
->next
;
1147 memset ((char *)context
, 0, sizeof (*context
));
1150 context
= ((cp_parser_context
*)
1151 ggc_alloc_cleared (sizeof (cp_parser_context
)));
1152 /* No errors have occurred yet in this context. */
1153 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1154 /* If this is not the bottomost context, copy information that we
1155 need from the previous context. */
1158 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159 expression, then we are parsing one in this context, too. */
1160 context
->object_type
= next
->object_type
;
1161 /* Thread the stack. */
1162 context
->next
= next
;
1168 /* The cp_parser structure represents the C++ parser. */
1170 typedef struct cp_parser
GTY(())
1172 /* The lexer from which we are obtaining tokens. */
1175 /* The scope in which names should be looked up. If NULL_TREE, then
1176 we look up names in the scope that is currently open in the
1177 source program. If non-NULL, this is either a TYPE or
1178 NAMESPACE_DECL for the scope in which we should look.
1180 This value is not cleared automatically after a name is looked
1181 up, so we must be careful to clear it before starting a new look
1182 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1183 will look up `Z' in the scope of `X', rather than the current
1184 scope.) Unfortunately, it is difficult to tell when name lookup
1185 is complete, because we sometimes peek at a token, look it up,
1186 and then decide not to consume it. */
1189 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190 last lookup took place. OBJECT_SCOPE is used if an expression
1191 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192 respectively. QUALIFYING_SCOPE is used for an expression of the
1193 form "X::Y"; it refers to X. */
1195 tree qualifying_scope
;
1197 /* A stack of parsing contexts. All but the bottom entry on the
1198 stack will be tentative contexts.
1200 We parse tentatively in order to determine which construct is in
1201 use in some situations. For example, in order to determine
1202 whether a statement is an expression-statement or a
1203 declaration-statement we parse it tentatively as a
1204 declaration-statement. If that fails, we then reparse the same
1205 token stream as an expression-statement. */
1206 cp_parser_context
*context
;
1208 /* True if we are parsing GNU C++. If this flag is not set, then
1209 GNU extensions are not recognized. */
1210 bool allow_gnu_extensions_p
;
1212 /* TRUE if the `>' token should be interpreted as the greater-than
1213 operator. FALSE if it is the end of a template-id or
1214 template-parameter-list. */
1215 bool greater_than_is_operator_p
;
1217 /* TRUE if default arguments are allowed within a parameter list
1218 that starts at this point. FALSE if only a gnu extension makes
1219 them permissable. */
1220 bool default_arg_ok_p
;
1222 /* TRUE if we are parsing an integral constant-expression. See
1223 [expr.const] for a precise definition. */
1224 bool constant_expression_p
;
1226 /* TRUE if we are parsing an integral constant-expression -- but a
1227 non-constant expression should be permitted as well. This flag
1228 is used when parsing an array bound so that GNU variable-length
1229 arrays are tolerated. */
1230 bool allow_non_constant_expression_p
;
1232 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233 been seen that makes the expression non-constant. */
1234 bool non_constant_expression_p
;
1236 /* TRUE if local variable names and `this' are forbidden in the
1238 bool local_variables_forbidden_p
;
1240 /* TRUE if the declaration we are parsing is part of a
1241 linkage-specification of the form `extern string-literal
1243 bool in_unbraced_linkage_specification_p
;
1245 /* TRUE if we are presently parsing a declarator, after the
1246 direct-declarator. */
1247 bool in_declarator_p
;
1249 /* If non-NULL, then we are parsing a construct where new type
1250 definitions are not permitted. The string stored here will be
1251 issued as an error message if a type is defined. */
1252 const char *type_definition_forbidden_message
;
1254 /* A list of lists. The outer list is a stack, used for member
1255 functions of local classes. At each level there are two sub-list,
1256 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1257 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1258 TREE_VALUE's. The functions are chained in reverse declaration
1261 The TREE_PURPOSE sublist contains those functions with default
1262 arguments that need post processing, and the TREE_VALUE sublist
1263 contains those functions with definitions that need post
1266 These lists can only be processed once the outermost class being
1267 defined is complete. */
1268 tree unparsed_functions_queues
;
1270 /* The number of classes whose definitions are currently in
1272 unsigned num_classes_being_defined
;
1274 /* The number of template parameter lists that apply directly to the
1275 current declaration. */
1276 unsigned num_template_parameter_lists
;
1279 /* The type of a function that parses some kind of expression */
1280 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1284 /* Constructors and destructors. */
1286 static cp_parser
*cp_parser_new
1289 /* Routines to parse various constructs.
1291 Those that return `tree' will return the error_mark_node (rather
1292 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1293 Sometimes, they will return an ordinary node if error-recovery was
1294 attempted, even though a parse error occurred. So, to check
1295 whether or not a parse error occurred, you should always use
1296 cp_parser_error_occurred. If the construct is optional (indicated
1297 either by an `_opt' in the name of the function that does the
1298 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1299 the construct is not present. */
1301 /* Lexical conventions [gram.lex] */
1303 static tree cp_parser_identifier
1306 /* Basic concepts [gram.basic] */
1308 static bool cp_parser_translation_unit
1311 /* Expressions [gram.expr] */
1313 static tree cp_parser_primary_expression
1314 (cp_parser
*, cp_parser_id_kind
*, tree
*);
1315 static tree cp_parser_id_expression
1316 (cp_parser
*, bool, bool, bool *);
1317 static tree cp_parser_unqualified_id
1318 (cp_parser
*, bool, bool);
1319 static tree cp_parser_nested_name_specifier_opt
1320 (cp_parser
*, bool, bool, bool);
1321 static tree cp_parser_nested_name_specifier
1322 (cp_parser
*, bool, bool, bool);
1323 static tree cp_parser_class_or_namespace_name
1324 (cp_parser
*, bool, bool, bool, bool);
1325 static tree cp_parser_postfix_expression
1326 (cp_parser
*, bool);
1327 static tree cp_parser_expression_list
1329 static void cp_parser_pseudo_destructor_name
1330 (cp_parser
*, tree
*, tree
*);
1331 static tree cp_parser_unary_expression
1332 (cp_parser
*, bool);
1333 static enum tree_code cp_parser_unary_operator
1335 static tree cp_parser_new_expression
1337 static tree cp_parser_new_placement
1339 static tree cp_parser_new_type_id
1341 static tree cp_parser_new_declarator_opt
1343 static tree cp_parser_direct_new_declarator
1345 static tree cp_parser_new_initializer
1347 static tree cp_parser_delete_expression
1349 static tree cp_parser_cast_expression
1350 (cp_parser
*, bool);
1351 static tree cp_parser_pm_expression
1353 static tree cp_parser_multiplicative_expression
1355 static tree cp_parser_additive_expression
1357 static tree cp_parser_shift_expression
1359 static tree cp_parser_relational_expression
1361 static tree cp_parser_equality_expression
1363 static tree cp_parser_and_expression
1365 static tree cp_parser_exclusive_or_expression
1367 static tree cp_parser_inclusive_or_expression
1369 static tree cp_parser_logical_and_expression
1371 static tree cp_parser_logical_or_expression
1373 static tree cp_parser_conditional_expression
1375 static tree cp_parser_question_colon_clause
1376 (cp_parser
*, tree
);
1377 static tree cp_parser_assignment_expression
1379 static enum tree_code cp_parser_assignment_operator_opt
1381 static tree cp_parser_expression
1383 static tree cp_parser_constant_expression
1384 (cp_parser
*, bool, bool *);
1386 /* Statements [gram.stmt.stmt] */
1388 static void cp_parser_statement
1390 static tree cp_parser_labeled_statement
1392 static tree cp_parser_expression_statement
1394 static tree cp_parser_compound_statement
1396 static void cp_parser_statement_seq_opt
1398 static tree cp_parser_selection_statement
1400 static tree cp_parser_condition
1402 static tree cp_parser_iteration_statement
1404 static void cp_parser_for_init_statement
1406 static tree cp_parser_jump_statement
1408 static void cp_parser_declaration_statement
1411 static tree cp_parser_implicitly_scoped_statement
1413 static void cp_parser_already_scoped_statement
1416 /* Declarations [gram.dcl.dcl] */
1418 static void cp_parser_declaration_seq_opt
1420 static void cp_parser_declaration
1422 static void cp_parser_block_declaration
1423 (cp_parser
*, bool);
1424 static void cp_parser_simple_declaration
1425 (cp_parser
*, bool);
1426 static tree cp_parser_decl_specifier_seq
1427 (cp_parser
*, cp_parser_flags
, tree
*, bool *);
1428 static tree cp_parser_storage_class_specifier_opt
1430 static tree cp_parser_function_specifier_opt
1432 static tree cp_parser_type_specifier
1433 (cp_parser
*, cp_parser_flags
, bool, bool, bool *, bool *);
1434 static tree cp_parser_simple_type_specifier
1435 (cp_parser
*, cp_parser_flags
);
1436 static tree cp_parser_type_name
1438 static tree cp_parser_elaborated_type_specifier
1439 (cp_parser
*, bool, bool);
1440 static tree cp_parser_enum_specifier
1442 static void cp_parser_enumerator_list
1443 (cp_parser
*, tree
);
1444 static void cp_parser_enumerator_definition
1445 (cp_parser
*, tree
);
1446 static tree cp_parser_namespace_name
1448 static void cp_parser_namespace_definition
1450 static void cp_parser_namespace_body
1452 static tree cp_parser_qualified_namespace_specifier
1454 static void cp_parser_namespace_alias_definition
1456 static void cp_parser_using_declaration
1458 static void cp_parser_using_directive
1460 static void cp_parser_asm_definition
1462 static void cp_parser_linkage_specification
1465 /* Declarators [gram.dcl.decl] */
1467 static tree cp_parser_init_declarator
1468 (cp_parser
*, tree
, tree
, bool, bool, bool *);
1469 static tree cp_parser_declarator
1470 (cp_parser
*, cp_parser_declarator_kind
, bool *);
1471 static tree cp_parser_direct_declarator
1472 (cp_parser
*, cp_parser_declarator_kind
, bool *);
1473 static enum tree_code cp_parser_ptr_operator
1474 (cp_parser
*, tree
*, tree
*);
1475 static tree cp_parser_cv_qualifier_seq_opt
1477 static tree cp_parser_cv_qualifier_opt
1479 static tree cp_parser_declarator_id
1481 static tree cp_parser_type_id
1483 static tree cp_parser_type_specifier_seq
1485 static tree cp_parser_parameter_declaration_clause
1487 static tree cp_parser_parameter_declaration_list
1489 static tree cp_parser_parameter_declaration
1490 (cp_parser
*, bool);
1491 static tree cp_parser_function_definition
1492 (cp_parser
*, bool *);
1493 static void cp_parser_function_body
1495 static tree cp_parser_initializer
1496 (cp_parser
*, bool *);
1497 static tree cp_parser_initializer_clause
1499 static tree cp_parser_initializer_list
1502 static bool cp_parser_ctor_initializer_opt_and_function_body
1505 /* Classes [gram.class] */
1507 static tree cp_parser_class_name
1508 (cp_parser
*, bool, bool, bool, bool, bool);
1509 static tree cp_parser_class_specifier
1511 static tree cp_parser_class_head
1512 (cp_parser
*, bool *);
1513 static enum tag_types cp_parser_class_key
1515 static void cp_parser_member_specification_opt
1517 static void cp_parser_member_declaration
1519 static tree cp_parser_pure_specifier
1521 static tree cp_parser_constant_initializer
1524 /* Derived classes [gram.class.derived] */
1526 static tree cp_parser_base_clause
1528 static tree cp_parser_base_specifier
1531 /* Special member functions [gram.special] */
1533 static tree cp_parser_conversion_function_id
1535 static tree cp_parser_conversion_type_id
1537 static tree cp_parser_conversion_declarator_opt
1539 static bool cp_parser_ctor_initializer_opt
1541 static void cp_parser_mem_initializer_list
1543 static tree cp_parser_mem_initializer
1545 static tree cp_parser_mem_initializer_id
1548 /* Overloading [gram.over] */
1550 static tree cp_parser_operator_function_id
1552 static tree cp_parser_operator
1555 /* Templates [gram.temp] */
1557 static void cp_parser_template_declaration
1558 (cp_parser
*, bool);
1559 static tree cp_parser_template_parameter_list
1561 static tree cp_parser_template_parameter
1563 static tree cp_parser_type_parameter
1565 static tree cp_parser_template_id
1566 (cp_parser
*, bool, bool);
1567 static tree cp_parser_template_name
1568 (cp_parser
*, bool, bool);
1569 static tree cp_parser_template_argument_list
1571 static tree cp_parser_template_argument
1573 static void cp_parser_explicit_instantiation
1575 static void cp_parser_explicit_specialization
1578 /* Exception handling [gram.exception] */
1580 static tree cp_parser_try_block
1582 static bool cp_parser_function_try_block
1584 static void cp_parser_handler_seq
1586 static void cp_parser_handler
1588 static tree cp_parser_exception_declaration
1590 static tree cp_parser_throw_expression
1592 static tree cp_parser_exception_specification_opt
1594 static tree cp_parser_type_id_list
1597 /* GNU Extensions */
1599 static tree cp_parser_asm_specification_opt
1601 static tree cp_parser_asm_operand_list
1603 static tree cp_parser_asm_clobber_list
1605 static tree cp_parser_attributes_opt
1607 static tree cp_parser_attribute_list
1609 static bool cp_parser_extension_opt
1610 (cp_parser
*, int *);
1611 static void cp_parser_label_declaration
1614 /* Utility Routines */
1616 static tree cp_parser_lookup_name
1617 (cp_parser
*, tree
, bool, bool, bool);
1618 static tree cp_parser_lookup_name_simple
1619 (cp_parser
*, tree
);
1620 static tree cp_parser_maybe_treat_template_as_class
1622 static bool cp_parser_check_declarator_template_parameters
1623 (cp_parser
*, tree
);
1624 static bool cp_parser_check_template_parameters
1625 (cp_parser
*, unsigned);
1626 static tree cp_parser_simple_cast_expression
1628 static tree cp_parser_binary_expression
1629 (cp_parser
*, const cp_parser_token_tree_map
, cp_parser_expression_fn
);
1630 static tree cp_parser_global_scope_opt
1631 (cp_parser
*, bool);
1632 static bool cp_parser_constructor_declarator_p
1633 (cp_parser
*, bool);
1634 static tree cp_parser_function_definition_from_specifiers_and_declarator
1635 (cp_parser
*, tree
, tree
, tree
);
1636 static tree cp_parser_function_definition_after_declarator
1637 (cp_parser
*, bool);
1638 static void cp_parser_template_declaration_after_export
1639 (cp_parser
*, bool);
1640 static tree cp_parser_single_declaration
1641 (cp_parser
*, bool, bool *);
1642 static tree cp_parser_functional_cast
1643 (cp_parser
*, tree
);
1644 static void cp_parser_save_default_args
1645 (cp_parser
*, tree
);
1646 static void cp_parser_late_parsing_for_member
1647 (cp_parser
*, tree
);
1648 static void cp_parser_late_parsing_default_args
1649 (cp_parser
*, tree
);
1650 static tree cp_parser_sizeof_operand
1651 (cp_parser
*, enum rid
);
1652 static bool cp_parser_declares_only_class_p
1654 static bool cp_parser_friend_p
1656 static cp_token
*cp_parser_require
1657 (cp_parser
*, enum cpp_ttype
, const char *);
1658 static cp_token
*cp_parser_require_keyword
1659 (cp_parser
*, enum rid
, const char *);
1660 static bool cp_parser_token_starts_function_definition_p
1662 static bool cp_parser_next_token_starts_class_definition_p
1664 static enum tag_types cp_parser_token_is_class_key
1666 static void cp_parser_check_class_key
1667 (enum tag_types
, tree type
);
1668 static bool cp_parser_optional_template_keyword
1670 static void cp_parser_pre_parsed_nested_name_specifier
1672 static void cp_parser_cache_group
1673 (cp_parser
*, cp_token_cache
*, enum cpp_ttype
, unsigned);
1674 static void cp_parser_parse_tentatively
1676 static void cp_parser_commit_to_tentative_parse
1678 static void cp_parser_abort_tentative_parse
1680 static bool cp_parser_parse_definitely
1682 static inline bool cp_parser_parsing_tentatively
1684 static bool cp_parser_committed_to_tentative_parse
1686 static void cp_parser_error
1687 (cp_parser
*, const char *);
1688 static bool cp_parser_simulate_error
1690 static void cp_parser_check_type_definition
1692 static tree cp_parser_non_constant_expression
1694 static tree cp_parser_non_constant_id_expression
1696 static bool cp_parser_diagnose_invalid_type_name
1698 static bool cp_parser_skip_to_closing_parenthesis
1700 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1702 static void cp_parser_skip_to_end_of_statement
1704 static void cp_parser_consume_semicolon_at_end_of_statement
1706 static void cp_parser_skip_to_end_of_block_or_statement
1708 static void cp_parser_skip_to_closing_brace
1710 static void cp_parser_skip_until_found
1711 (cp_parser
*, enum cpp_ttype
, const char *);
1712 static bool cp_parser_error_occurred
1714 static bool cp_parser_allow_gnu_extensions_p
1716 static bool cp_parser_is_string_literal
1718 static bool cp_parser_is_keyword
1719 (cp_token
*, enum rid
);
1721 /* Returns nonzero if we are parsing tentatively. */
1724 cp_parser_parsing_tentatively (cp_parser
* parser
)
1726 return parser
->context
->next
!= NULL
;
1729 /* Returns nonzero if TOKEN is a string literal. */
1732 cp_parser_is_string_literal (cp_token
* token
)
1734 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1737 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1740 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1742 return token
->keyword
== keyword
;
1745 /* Issue the indicated error MESSAGE. */
1748 cp_parser_error (cp_parser
* parser
, const char* message
)
1750 /* Output the MESSAGE -- unless we're parsing tentatively. */
1751 if (!cp_parser_simulate_error (parser
))
1755 /* If we are parsing tentatively, remember that an error has occurred
1756 during this tentative parse. Returns true if the error was
1757 simulated; false if a messgae should be issued by the caller. */
1760 cp_parser_simulate_error (cp_parser
* parser
)
1762 if (cp_parser_parsing_tentatively (parser
)
1763 && !cp_parser_committed_to_tentative_parse (parser
))
1765 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1771 /* This function is called when a type is defined. If type
1772 definitions are forbidden at this point, an error message is
1776 cp_parser_check_type_definition (cp_parser
* parser
)
1778 /* If types are forbidden here, issue a message. */
1779 if (parser
->type_definition_forbidden_message
)
1780 /* Use `%s' to print the string in case there are any escape
1781 characters in the message. */
1782 error ("%s", parser
->type_definition_forbidden_message
);
1785 /* Issue an eror message about the fact that THING appeared in a
1786 constant-expression. Returns ERROR_MARK_NODE. */
1789 cp_parser_non_constant_expression (const char *thing
)
1791 error ("%s cannot appear in a constant-expression", thing
);
1792 return error_mark_node
;
1795 /* Issue an eror message about the fact that DECL appeared in a
1796 constant-expression. Returns ERROR_MARK_NODE. */
1799 cp_parser_non_constant_id_expression (tree decl
)
1801 error ("`%D' cannot appear in a constant-expression", decl
);
1802 return error_mark_node
;
1805 /* Check for a common situation where a type-name should be present,
1806 but is not, and issue a sensible error message. Returns true if an
1807 invalid type-name was detected. */
1810 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
)
1812 /* If the next two tokens are both identifiers, the code is
1813 erroneous. The usual cause of this situation is code like:
1817 where "T" should name a type -- but does not. */
1818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
1819 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
1823 /* If parsing tentatively, we should commit; we really are
1824 looking at a declaration. */
1825 /* Consume the first identifier. */
1826 name
= cp_lexer_consume_token (parser
->lexer
)->value
;
1827 /* Issue an error message. */
1828 error ("`%s' does not name a type", IDENTIFIER_POINTER (name
));
1829 /* If we're in a template class, it's possible that the user was
1830 referring to a type from a base class. For example:
1832 template <typename T> struct A { typedef T X; };
1833 template <typename T> struct B : public A<T> { X x; };
1835 The user should have said "typename A<T>::X". */
1836 if (processing_template_decl
&& current_class_type
)
1840 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1844 tree base_type
= BINFO_TYPE (b
);
1845 if (CLASS_TYPE_P (base_type
)
1846 && dependent_type_p (base_type
))
1849 /* Go from a particular instantiation of the
1850 template (which will have an empty TYPE_FIELDs),
1851 to the main version. */
1852 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1853 for (field
= TYPE_FIELDS (base_type
);
1855 field
= TREE_CHAIN (field
))
1856 if (TREE_CODE (field
) == TYPE_DECL
1857 && DECL_NAME (field
) == name
)
1859 error ("(perhaps `typename %T::%s' was intended)",
1860 BINFO_TYPE (b
), IDENTIFIER_POINTER (name
));
1868 /* Skip to the end of the declaration; there's no point in
1869 trying to process it. */
1870 cp_parser_skip_to_end_of_statement (parser
);
1878 /* Consume tokens up to, and including, the next non-nested closing `)'.
1879 Returns TRUE iff we found a closing `)'. */
1882 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
)
1884 unsigned nesting_depth
= 0;
1890 /* If we've run out of tokens, then there is no closing `)'. */
1891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
1893 /* Consume the token. */
1894 token
= cp_lexer_consume_token (parser
->lexer
);
1895 /* If it is an `(', we have entered another level of nesting. */
1896 if (token
->type
== CPP_OPEN_PAREN
)
1898 /* If it is a `)', then we might be done. */
1899 else if (token
->type
== CPP_CLOSE_PAREN
&& nesting_depth
-- == 0)
1904 /* Consume tokens until the next token is a `)', or a `,'. Returns
1905 TRUE if the next token is a `,'. */
1908 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser
*parser
)
1910 unsigned nesting_depth
= 0;
1914 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1916 /* If we've run out of tokens, then there is no closing `)'. */
1917 if (token
->type
== CPP_EOF
)
1919 /* If it is a `,' stop. */
1920 else if (token
->type
== CPP_COMMA
&& nesting_depth
-- == 0)
1922 /* If it is a `)', stop. */
1923 else if (token
->type
== CPP_CLOSE_PAREN
&& nesting_depth
-- == 0)
1925 /* If it is an `(', we have entered another level of nesting. */
1926 else if (token
->type
== CPP_OPEN_PAREN
)
1928 /* Consume the token. */
1929 token
= cp_lexer_consume_token (parser
->lexer
);
1933 /* Consume tokens until we reach the end of the current statement.
1934 Normally, that will be just before consuming a `;'. However, if a
1935 non-nested `}' comes first, then we stop before consuming that. */
1938 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
1940 unsigned nesting_depth
= 0;
1946 /* Peek at the next token. */
1947 token
= cp_lexer_peek_token (parser
->lexer
);
1948 /* If we've run out of tokens, stop. */
1949 if (token
->type
== CPP_EOF
)
1951 /* If the next token is a `;', we have reached the end of the
1953 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
1955 /* If the next token is a non-nested `}', then we have reached
1956 the end of the current block. */
1957 if (token
->type
== CPP_CLOSE_BRACE
)
1959 /* If this is a non-nested `}', stop before consuming it.
1960 That way, when confronted with something like:
1964 we stop before consuming the closing `}', even though we
1965 have not yet reached a `;'. */
1966 if (nesting_depth
== 0)
1968 /* If it is the closing `}' for a block that we have
1969 scanned, stop -- but only after consuming the token.
1975 we will stop after the body of the erroneously declared
1976 function, but before consuming the following `typedef'
1978 if (--nesting_depth
== 0)
1980 cp_lexer_consume_token (parser
->lexer
);
1984 /* If it the next token is a `{', then we are entering a new
1985 block. Consume the entire block. */
1986 else if (token
->type
== CPP_OPEN_BRACE
)
1988 /* Consume the token. */
1989 cp_lexer_consume_token (parser
->lexer
);
1993 /* This function is called at the end of a statement or declaration.
1994 If the next token is a semicolon, it is consumed; otherwise, error
1995 recovery is attempted. */
1998 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2000 /* Look for the trailing `;'. */
2001 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2003 /* If there is additional (erroneous) input, skip to the end of
2005 cp_parser_skip_to_end_of_statement (parser
);
2006 /* If the next token is now a `;', consume it. */
2007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2008 cp_lexer_consume_token (parser
->lexer
);
2012 /* Skip tokens until we have consumed an entire block, or until we
2013 have consumed a non-nested `;'. */
2016 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2018 unsigned nesting_depth
= 0;
2024 /* Peek at the next token. */
2025 token
= cp_lexer_peek_token (parser
->lexer
);
2026 /* If we've run out of tokens, stop. */
2027 if (token
->type
== CPP_EOF
)
2029 /* If the next token is a `;', we have reached the end of the
2031 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2033 /* Consume the `;'. */
2034 cp_lexer_consume_token (parser
->lexer
);
2037 /* Consume the token. */
2038 token
= cp_lexer_consume_token (parser
->lexer
);
2039 /* If the next token is a non-nested `}', then we have reached
2040 the end of the current block. */
2041 if (token
->type
== CPP_CLOSE_BRACE
2042 && (nesting_depth
== 0 || --nesting_depth
== 0))
2044 /* If it the next token is a `{', then we are entering a new
2045 block. Consume the entire block. */
2046 if (token
->type
== CPP_OPEN_BRACE
)
2051 /* Skip tokens until a non-nested closing curly brace is the next
2055 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2057 unsigned nesting_depth
= 0;
2063 /* Peek at the next token. */
2064 token
= cp_lexer_peek_token (parser
->lexer
);
2065 /* If we've run out of tokens, stop. */
2066 if (token
->type
== CPP_EOF
)
2068 /* If the next token is a non-nested `}', then we have reached
2069 the end of the current block. */
2070 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2072 /* If it the next token is a `{', then we are entering a new
2073 block. Consume the entire block. */
2074 else if (token
->type
== CPP_OPEN_BRACE
)
2076 /* Consume the token. */
2077 cp_lexer_consume_token (parser
->lexer
);
2081 /* Create a new C++ parser. */
2084 cp_parser_new (void)
2089 /* cp_lexer_new_main is called before calling ggc_alloc because
2090 cp_lexer_new_main might load a PCH file. */
2091 lexer
= cp_lexer_new_main ();
2093 parser
= (cp_parser
*) ggc_alloc_cleared (sizeof (cp_parser
));
2094 parser
->lexer
= lexer
;
2095 parser
->context
= cp_parser_context_new (NULL
);
2097 /* For now, we always accept GNU extensions. */
2098 parser
->allow_gnu_extensions_p
= 1;
2100 /* The `>' token is a greater-than operator, not the end of a
2102 parser
->greater_than_is_operator_p
= true;
2104 parser
->default_arg_ok_p
= true;
2106 /* We are not parsing a constant-expression. */
2107 parser
->constant_expression_p
= false;
2108 parser
->allow_non_constant_expression_p
= false;
2109 parser
->non_constant_expression_p
= false;
2111 /* Local variable names are not forbidden. */
2112 parser
->local_variables_forbidden_p
= false;
2114 /* We are not processing an `extern "C"' declaration. */
2115 parser
->in_unbraced_linkage_specification_p
= false;
2117 /* We are not processing a declarator. */
2118 parser
->in_declarator_p
= false;
2120 /* The unparsed function queue is empty. */
2121 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2123 /* There are no classes being defined. */
2124 parser
->num_classes_being_defined
= 0;
2126 /* No template parameters apply. */
2127 parser
->num_template_parameter_lists
= 0;
2132 /* Lexical conventions [gram.lex] */
2134 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2138 cp_parser_identifier (cp_parser
* parser
)
2142 /* Look for the identifier. */
2143 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2144 /* Return the value. */
2145 return token
? token
->value
: error_mark_node
;
2148 /* Basic concepts [gram.basic] */
2150 /* Parse a translation-unit.
2153 declaration-seq [opt]
2155 Returns TRUE if all went well. */
2158 cp_parser_translation_unit (cp_parser
* parser
)
2162 cp_parser_declaration_seq_opt (parser
);
2164 /* If there are no tokens left then all went well. */
2165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2168 /* Otherwise, issue an error message. */
2169 cp_parser_error (parser
, "expected declaration");
2173 /* Consume the EOF token. */
2174 cp_parser_require (parser
, CPP_EOF
, "end-of-file");
2177 finish_translation_unit ();
2179 /* All went well. */
2183 /* Expressions [gram.expr] */
2185 /* Parse a primary-expression.
2196 ( compound-statement )
2197 __builtin_va_arg ( assignment-expression , type-id )
2202 Returns a representation of the expression.
2204 *IDK indicates what kind of id-expression (if any) was present.
2206 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2207 used as the operand of a pointer-to-member. In that case,
2208 *QUALIFYING_CLASS gives the class that is used as the qualifying
2209 class in the pointer-to-member. */
2212 cp_parser_primary_expression (cp_parser
*parser
,
2213 cp_parser_id_kind
*idk
,
2214 tree
*qualifying_class
)
2218 /* Assume the primary expression is not an id-expression. */
2219 *idk
= CP_PARSER_ID_KIND_NONE
;
2220 /* And that it cannot be used as pointer-to-member. */
2221 *qualifying_class
= NULL_TREE
;
2223 /* Peek at the next token. */
2224 token
= cp_lexer_peek_token (parser
->lexer
);
2225 switch (token
->type
)
2238 token
= cp_lexer_consume_token (parser
->lexer
);
2239 return token
->value
;
2241 case CPP_OPEN_PAREN
:
2244 bool saved_greater_than_is_operator_p
;
2246 /* Consume the `('. */
2247 cp_lexer_consume_token (parser
->lexer
);
2248 /* Within a parenthesized expression, a `>' token is always
2249 the greater-than operator. */
2250 saved_greater_than_is_operator_p
2251 = parser
->greater_than_is_operator_p
;
2252 parser
->greater_than_is_operator_p
= true;
2253 /* If we see `( { ' then we are looking at the beginning of
2254 a GNU statement-expression. */
2255 if (cp_parser_allow_gnu_extensions_p (parser
)
2256 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2258 /* Statement-expressions are not allowed by the standard. */
2260 pedwarn ("ISO C++ forbids braced-groups within expressions");
2262 /* And they're not allowed outside of a function-body; you
2263 cannot, for example, write:
2265 int i = ({ int j = 3; j + 1; });
2267 at class or namespace scope. */
2268 if (!at_function_scope_p ())
2269 error ("statement-expressions are allowed only inside functions");
2270 /* Start the statement-expression. */
2271 expr
= begin_stmt_expr ();
2272 /* Parse the compound-statement. */
2273 cp_parser_compound_statement (parser
);
2275 expr
= finish_stmt_expr (expr
);
2279 /* Parse the parenthesized expression. */
2280 expr
= cp_parser_expression (parser
);
2281 /* Let the front end know that this expression was
2282 enclosed in parentheses. This matters in case, for
2283 example, the expression is of the form `A::B', since
2284 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2286 finish_parenthesized_expr (expr
);
2288 /* The `>' token might be the end of a template-id or
2289 template-parameter-list now. */
2290 parser
->greater_than_is_operator_p
2291 = saved_greater_than_is_operator_p
;
2292 /* Consume the `)'. */
2293 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2294 cp_parser_skip_to_end_of_statement (parser
);
2300 switch (token
->keyword
)
2302 /* These two are the boolean literals. */
2304 cp_lexer_consume_token (parser
->lexer
);
2305 return boolean_true_node
;
2307 cp_lexer_consume_token (parser
->lexer
);
2308 return boolean_false_node
;
2310 /* The `__null' literal. */
2312 cp_lexer_consume_token (parser
->lexer
);
2315 /* Recognize the `this' keyword. */
2317 cp_lexer_consume_token (parser
->lexer
);
2318 if (parser
->local_variables_forbidden_p
)
2320 error ("`this' may not be used in this context");
2321 return error_mark_node
;
2323 /* Pointers cannot appear in constant-expressions. */
2324 if (parser
->constant_expression_p
)
2326 if (!parser
->allow_non_constant_expression_p
)
2327 return cp_parser_non_constant_expression ("`this'");
2328 parser
->non_constant_expression_p
= true;
2330 return finish_this_expr ();
2332 /* The `operator' keyword can be the beginning of an
2337 case RID_FUNCTION_NAME
:
2338 case RID_PRETTY_FUNCTION_NAME
:
2339 case RID_C99_FUNCTION_NAME
:
2340 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2341 __func__ are the names of variables -- but they are
2342 treated specially. Therefore, they are handled here,
2343 rather than relying on the generic id-expression logic
2344 below. Grammatically, these names are id-expressions.
2346 Consume the token. */
2347 token
= cp_lexer_consume_token (parser
->lexer
);
2348 /* Look up the name. */
2349 return finish_fname (token
->value
);
2356 /* The `__builtin_va_arg' construct is used to handle
2357 `va_arg'. Consume the `__builtin_va_arg' token. */
2358 cp_lexer_consume_token (parser
->lexer
);
2359 /* Look for the opening `('. */
2360 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2361 /* Now, parse the assignment-expression. */
2362 expression
= cp_parser_assignment_expression (parser
);
2363 /* Look for the `,'. */
2364 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2365 /* Parse the type-id. */
2366 type
= cp_parser_type_id (parser
);
2367 /* Look for the closing `)'. */
2368 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2369 /* Using `va_arg' in a constant-expression is not
2371 if (parser
->constant_expression_p
)
2373 if (!parser
->allow_non_constant_expression_p
)
2374 return cp_parser_non_constant_expression ("`va_arg'");
2375 parser
->non_constant_expression_p
= true;
2377 return build_x_va_arg (expression
, type
);
2381 cp_parser_error (parser
, "expected primary-expression");
2382 return error_mark_node
;
2386 /* An id-expression can start with either an identifier, a
2387 `::' as the beginning of a qualified-id, or the "operator"
2391 case CPP_TEMPLATE_ID
:
2392 case CPP_NESTED_NAME_SPECIFIER
:
2398 /* Parse the id-expression. */
2400 = cp_parser_id_expression (parser
,
2401 /*template_keyword_p=*/false,
2402 /*check_dependency_p=*/true,
2403 /*template_p=*/NULL
);
2404 if (id_expression
== error_mark_node
)
2405 return error_mark_node
;
2406 /* If we have a template-id, then no further lookup is
2407 required. If the template-id was for a template-class, we
2408 will sometimes have a TYPE_DECL at this point. */
2409 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2410 || TREE_CODE (id_expression
) == TYPE_DECL
)
2411 decl
= id_expression
;
2412 /* Look up the name. */
2415 decl
= cp_parser_lookup_name_simple (parser
, id_expression
);
2416 /* If name lookup gives us a SCOPE_REF, then the
2417 qualifying scope was dependent. Just propagate the
2419 if (TREE_CODE (decl
) == SCOPE_REF
)
2421 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2422 *qualifying_class
= TREE_OPERAND (decl
, 0);
2423 /* Since this name was dependent, the expression isn't
2424 constant -- yet. No error is issued because it
2425 might be constant when things are instantiated. */
2426 if (parser
->constant_expression_p
)
2427 parser
->non_constant_expression_p
= true;
2430 /* Check to see if DECL is a local variable in a context
2431 where that is forbidden. */
2432 if (parser
->local_variables_forbidden_p
2433 && local_variable_p (decl
))
2435 /* It might be that we only found DECL because we are
2436 trying to be generous with pre-ISO scoping rules.
2437 For example, consider:
2441 for (int i = 0; i < 10; ++i) {}
2442 extern void f(int j = i);
2445 Here, name look up will originally find the out
2446 of scope `i'. We need to issue a warning message,
2447 but then use the global `i'. */
2448 decl
= check_for_out_of_scope_variable (decl
);
2449 if (local_variable_p (decl
))
2451 error ("local variable `%D' may not appear in this context",
2453 return error_mark_node
;
2457 if (decl
== error_mark_node
)
2459 /* Name lookup failed. */
2461 && processing_template_decl
)
2463 /* Unqualified name lookup failed while processing a
2465 *idk
= CP_PARSER_ID_KIND_UNQUALIFIED
;
2466 /* If the next token is a parenthesis, assume that
2467 Koenig lookup will succeed when instantiating the
2469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
2470 return build_min_nt (LOOKUP_EXPR
, id_expression
);
2471 /* If we're not doing Koenig lookup, issue an error. */
2472 error ("`%D' has not been declared", id_expression
);
2473 return error_mark_node
;
2475 else if (parser
->scope
2476 && (!TYPE_P (parser
->scope
)
2477 || !dependent_type_p (parser
->scope
)))
2479 /* Qualified name lookup failed, and the
2480 qualifying name was not a dependent type. That
2481 is always an error. */
2482 if (TYPE_P (parser
->scope
)
2483 && !COMPLETE_TYPE_P (parser
->scope
))
2484 error ("incomplete type `%T' used in nested name "
2487 else if (parser
->scope
!= global_namespace
)
2488 error ("`%D' is not a member of `%D'",
2489 id_expression
, parser
->scope
);
2491 error ("`::%D' has not been declared", id_expression
);
2492 return error_mark_node
;
2494 else if (!parser
->scope
&& !processing_template_decl
)
2496 /* It may be resolvable as a koenig lookup function
2498 *idk
= CP_PARSER_ID_KIND_UNQUALIFIED
;
2499 return id_expression
;
2502 /* If DECL is a variable that would be out of scope under
2503 ANSI/ISO rules, but in scope in the ARM, name lookup
2504 will succeed. Issue a diagnostic here. */
2506 decl
= check_for_out_of_scope_variable (decl
);
2508 /* Remember that the name was used in the definition of
2509 the current class so that we can check later to see if
2510 the meaning would have been different after the class
2511 was entirely defined. */
2512 if (!parser
->scope
&& decl
!= error_mark_node
)
2513 maybe_note_name_used_in_class (id_expression
, decl
);
2516 /* If we didn't find anything, or what we found was a type,
2517 then this wasn't really an id-expression. */
2518 if (TREE_CODE (decl
) == TEMPLATE_DECL
2519 && !DECL_FUNCTION_TEMPLATE_P (decl
))
2521 cp_parser_error (parser
, "missing template arguments");
2522 return error_mark_node
;
2524 else if (TREE_CODE (decl
) == TYPE_DECL
2525 || TREE_CODE (decl
) == NAMESPACE_DECL
)
2527 cp_parser_error (parser
,
2528 "expected primary-expression");
2529 return error_mark_node
;
2532 /* If the name resolved to a template parameter, there is no
2533 need to look it up again later. Similarly, we resolve
2534 enumeration constants to their underlying values. */
2535 if (TREE_CODE (decl
) == CONST_DECL
)
2537 *idk
= CP_PARSER_ID_KIND_NONE
;
2538 if (DECL_TEMPLATE_PARM_P (decl
) || !processing_template_decl
)
2539 return DECL_INITIAL (decl
);
2546 /* If the declaration was explicitly qualified indicate
2547 that. The semantics of `A::f(3)' are different than
2548 `f(3)' if `f' is virtual. */
2549 *idk
= (parser
->scope
2550 ? CP_PARSER_ID_KIND_QUALIFIED
2551 : (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
2552 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2553 : CP_PARSER_ID_KIND_UNQUALIFIED
));
2558 An id-expression is type-dependent if it contains an
2559 identifier that was declared with a dependent type.
2561 As an optimization, we could choose not to create a
2562 LOOKUP_EXPR for a name that resolved to a local
2563 variable in the template function that we are currently
2564 declaring; such a name cannot ever resolve to anything
2565 else. If we did that we would not have to look up
2566 these names at instantiation time.
2568 The standard is not very specific about an
2569 id-expression that names a set of overloaded functions.
2570 What if some of them have dependent types and some of
2571 them do not? Presumably, such a name should be treated
2572 as a dependent name. */
2573 /* Assume the name is not dependent. */
2574 dependent_p
= false;
2575 if (!processing_template_decl
)
2576 /* No names are dependent outside a template. */
2578 /* A template-id where the name of the template was not
2579 resolved is definitely dependent. */
2580 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
2581 && (TREE_CODE (TREE_OPERAND (decl
, 0))
2582 == IDENTIFIER_NODE
))
2584 /* For anything except an overloaded function, just check
2586 else if (!is_overloaded_fn (decl
))
2588 = dependent_type_p (TREE_TYPE (decl
));
2589 /* For a set of overloaded functions, check each of the
2595 if (BASELINK_P (fns
))
2596 fns
= BASELINK_FUNCTIONS (fns
);
2598 /* For a template-id, check to see if the template
2599 arguments are dependent. */
2600 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
2602 tree args
= TREE_OPERAND (fns
, 1);
2604 if (args
&& TREE_CODE (args
) == TREE_LIST
)
2608 if (dependent_template_arg_p (TREE_VALUE (args
)))
2613 args
= TREE_CHAIN (args
);
2616 else if (args
&& TREE_CODE (args
) == TREE_VEC
)
2619 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
2620 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
2627 /* The functions are those referred to by the
2629 fns
= TREE_OPERAND (fns
, 0);
2632 /* If there are no dependent template arguments, go
2633 through the overlaoded functions. */
2634 while (fns
&& !dependent_p
)
2636 tree fn
= OVL_CURRENT (fns
);
2638 /* Member functions of dependent classes are
2640 if (TREE_CODE (fn
) == FUNCTION_DECL
2641 && type_dependent_expression_p (fn
))
2643 else if (TREE_CODE (fn
) == TEMPLATE_DECL
2644 && dependent_template_p (fn
))
2647 fns
= OVL_NEXT (fns
);
2651 /* If the name was dependent on a template parameter,
2652 we will resolve the name at instantiation time. */
2655 /* Create a SCOPE_REF for qualified names, if the
2656 scope is dependent. */
2659 if (TYPE_P (parser
->scope
))
2660 *qualifying_class
= parser
->scope
;
2661 /* Since this name was dependent, the expression isn't
2662 constant -- yet. No error is issued because it
2663 might be constant when things are instantiated. */
2664 if (parser
->constant_expression_p
)
2665 parser
->non_constant_expression_p
= true;
2666 if (TYPE_P (parser
->scope
)
2667 && dependent_type_p (parser
->scope
))
2668 return build_nt (SCOPE_REF
,
2674 /* A TEMPLATE_ID already contains all the information
2676 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
)
2677 return id_expression
;
2678 /* Since this name was dependent, the expression isn't
2679 constant -- yet. No error is issued because it
2680 might be constant when things are instantiated. */
2681 if (parser
->constant_expression_p
)
2682 parser
->non_constant_expression_p
= true;
2683 /* Create a LOOKUP_EXPR for other unqualified names. */
2684 return build_min_nt (LOOKUP_EXPR
, id_expression
);
2687 /* Only certain kinds of names are allowed in constant
2688 expression. Enumerators have already been handled
2690 if (parser
->constant_expression_p
2691 /* Non-type template parameters of integral or
2692 enumeration type. */
2693 && !(TREE_CODE (decl
) == TEMPLATE_PARM_INDEX
2694 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl
)))
2695 /* Const variables or static data members of integral
2696 or enumeration types initialized with constant
2697 expressions (or dependent expressions - in this case
2698 the check will be done at instantiation time). */
2699 && !(TREE_CODE (decl
) == VAR_DECL
2700 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl
))
2701 && DECL_INITIAL (decl
)
2702 && (TREE_CONSTANT (DECL_INITIAL (decl
))
2703 || type_dependent_expression_p
2704 (DECL_INITIAL (decl
))
2705 || value_dependent_expression_p
2706 (DECL_INITIAL (decl
)))))
2708 if (!parser
->allow_non_constant_expression_p
)
2709 return cp_parser_non_constant_id_expression (decl
);
2710 parser
->non_constant_expression_p
= true;
2715 decl
= (adjust_result_of_qualified_name_lookup
2716 (decl
, parser
->scope
, current_class_type
));
2717 if (TREE_CODE (decl
) == FIELD_DECL
|| BASELINK_P (decl
))
2718 *qualifying_class
= parser
->scope
;
2719 else if (!processing_template_decl
)
2720 decl
= convert_from_reference (decl
);
2723 /* Transform references to non-static data members into
2725 decl
= hack_identifier (decl
, id_expression
);
2727 /* Resolve references to variables of anonymous unions
2728 into COMPONENT_REFs. */
2729 if (TREE_CODE (decl
) == ALIAS_DECL
)
2730 decl
= DECL_INITIAL (decl
);
2733 if (TREE_DEPRECATED (decl
))
2734 warn_deprecated_use (decl
);
2739 /* Anything else is an error. */
2741 cp_parser_error (parser
, "expected primary-expression");
2742 return error_mark_node
;
2746 /* Parse an id-expression.
2753 :: [opt] nested-name-specifier template [opt] unqualified-id
2755 :: operator-function-id
2758 Return a representation of the unqualified portion of the
2759 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2760 a `::' or nested-name-specifier.
2762 Often, if the id-expression was a qualified-id, the caller will
2763 want to make a SCOPE_REF to represent the qualified-id. This
2764 function does not do this in order to avoid wastefully creating
2765 SCOPE_REFs when they are not required.
2767 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2770 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2771 uninstantiated templates.
2773 If *TEMPLATE_P is non-NULL, it is set to true iff the
2774 `template' keyword is used to explicitly indicate that the entity
2775 named is a template. */
2778 cp_parser_id_expression (cp_parser
*parser
,
2779 bool template_keyword_p
,
2780 bool check_dependency_p
,
2783 bool global_scope_p
;
2784 bool nested_name_specifier_p
;
2786 /* Assume the `template' keyword was not used. */
2788 *template_p
= false;
2790 /* Look for the optional `::' operator. */
2792 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2794 /* Look for the optional nested-name-specifier. */
2795 nested_name_specifier_p
2796 = (cp_parser_nested_name_specifier_opt (parser
,
2797 /*typename_keyword_p=*/false,
2801 /* If there is a nested-name-specifier, then we are looking at
2802 the first qualified-id production. */
2803 if (nested_name_specifier_p
)
2806 tree saved_object_scope
;
2807 tree saved_qualifying_scope
;
2808 tree unqualified_id
;
2811 /* See if the next token is the `template' keyword. */
2813 template_p
= &is_template
;
2814 *template_p
= cp_parser_optional_template_keyword (parser
);
2815 /* Name lookup we do during the processing of the
2816 unqualified-id might obliterate SCOPE. */
2817 saved_scope
= parser
->scope
;
2818 saved_object_scope
= parser
->object_scope
;
2819 saved_qualifying_scope
= parser
->qualifying_scope
;
2820 /* Process the final unqualified-id. */
2821 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2822 check_dependency_p
);
2823 /* Restore the SAVED_SCOPE for our caller. */
2824 parser
->scope
= saved_scope
;
2825 parser
->object_scope
= saved_object_scope
;
2826 parser
->qualifying_scope
= saved_qualifying_scope
;
2828 return unqualified_id
;
2830 /* Otherwise, if we are in global scope, then we are looking at one
2831 of the other qualified-id productions. */
2832 else if (global_scope_p
)
2837 /* Peek at the next token. */
2838 token
= cp_lexer_peek_token (parser
->lexer
);
2840 /* If it's an identifier, and the next token is not a "<", then
2841 we can avoid the template-id case. This is an optimization
2842 for this common case. */
2843 if (token
->type
== CPP_NAME
2844 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
2845 return cp_parser_identifier (parser
);
2847 cp_parser_parse_tentatively (parser
);
2848 /* Try a template-id. */
2849 id
= cp_parser_template_id (parser
,
2850 /*template_keyword_p=*/false,
2851 /*check_dependency_p=*/true);
2852 /* If that worked, we're done. */
2853 if (cp_parser_parse_definitely (parser
))
2856 /* Peek at the next token. (Changes in the token buffer may
2857 have invalidated the pointer obtained above.) */
2858 token
= cp_lexer_peek_token (parser
->lexer
);
2860 switch (token
->type
)
2863 return cp_parser_identifier (parser
);
2866 if (token
->keyword
== RID_OPERATOR
)
2867 return cp_parser_operator_function_id (parser
);
2871 cp_parser_error (parser
, "expected id-expression");
2872 return error_mark_node
;
2876 return cp_parser_unqualified_id (parser
, template_keyword_p
,
2877 /*check_dependency_p=*/true);
2880 /* Parse an unqualified-id.
2884 operator-function-id
2885 conversion-function-id
2889 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2890 keyword, in a construct like `A::template ...'.
2892 Returns a representation of unqualified-id. For the `identifier'
2893 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2894 production a BIT_NOT_EXPR is returned; the operand of the
2895 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2896 other productions, see the documentation accompanying the
2897 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2898 names are looked up in uninstantiated templates. */
2901 cp_parser_unqualified_id (cp_parser
* parser
,
2902 bool template_keyword_p
,
2903 bool check_dependency_p
)
2907 /* Peek at the next token. */
2908 token
= cp_lexer_peek_token (parser
->lexer
);
2910 switch (token
->type
)
2916 /* We don't know yet whether or not this will be a
2918 cp_parser_parse_tentatively (parser
);
2919 /* Try a template-id. */
2920 id
= cp_parser_template_id (parser
, template_keyword_p
,
2921 check_dependency_p
);
2922 /* If it worked, we're done. */
2923 if (cp_parser_parse_definitely (parser
))
2925 /* Otherwise, it's an ordinary identifier. */
2926 return cp_parser_identifier (parser
);
2929 case CPP_TEMPLATE_ID
:
2930 return cp_parser_template_id (parser
, template_keyword_p
,
2931 check_dependency_p
);
2936 tree qualifying_scope
;
2940 /* Consume the `~' token. */
2941 cp_lexer_consume_token (parser
->lexer
);
2942 /* Parse the class-name. The standard, as written, seems to
2945 template <typename T> struct S { ~S (); };
2946 template <typename T> S<T>::~S() {}
2948 is invalid, since `~' must be followed by a class-name, but
2949 `S<T>' is dependent, and so not known to be a class.
2950 That's not right; we need to look in uninstantiated
2951 templates. A further complication arises from:
2953 template <typename T> void f(T t) {
2957 Here, it is not possible to look up `T' in the scope of `T'
2958 itself. We must look in both the current scope, and the
2959 scope of the containing complete expression.
2961 Yet another issue is:
2970 The standard does not seem to say that the `S' in `~S'
2971 should refer to the type `S' and not the data member
2974 /* DR 244 says that we look up the name after the "~" in the
2975 same scope as we looked up the qualifying name. That idea
2976 isn't fully worked out; it's more complicated than that. */
2977 scope
= parser
->scope
;
2978 object_scope
= parser
->object_scope
;
2979 qualifying_scope
= parser
->qualifying_scope
;
2981 /* If the name is of the form "X::~X" it's OK. */
2982 if (scope
&& TYPE_P (scope
)
2983 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2984 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
2986 && (cp_lexer_peek_token (parser
->lexer
)->value
2987 == TYPE_IDENTIFIER (scope
)))
2989 cp_lexer_consume_token (parser
->lexer
);
2990 return build_nt (BIT_NOT_EXPR
, scope
);
2993 /* If there was an explicit qualification (S::~T), first look
2994 in the scope given by the qualification (i.e., S). */
2997 cp_parser_parse_tentatively (parser
);
2998 type_decl
= cp_parser_class_name (parser
,
2999 /*typename_keyword_p=*/false,
3000 /*template_keyword_p=*/false,
3002 /*check_dependency=*/false,
3003 /*class_head_p=*/false);
3004 if (cp_parser_parse_definitely (parser
))
3005 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3007 /* In "N::S::~S", look in "N" as well. */
3008 if (scope
&& qualifying_scope
)
3010 cp_parser_parse_tentatively (parser
);
3011 parser
->scope
= qualifying_scope
;
3012 parser
->object_scope
= NULL_TREE
;
3013 parser
->qualifying_scope
= NULL_TREE
;
3015 = cp_parser_class_name (parser
,
3016 /*typename_keyword_p=*/false,
3017 /*template_keyword_p=*/false,
3019 /*check_dependency=*/false,
3020 /*class_head_p=*/false);
3021 if (cp_parser_parse_definitely (parser
))
3022 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3024 /* In "p->S::~T", look in the scope given by "*p" as well. */
3025 else if (object_scope
)
3027 cp_parser_parse_tentatively (parser
);
3028 parser
->scope
= object_scope
;
3029 parser
->object_scope
= NULL_TREE
;
3030 parser
->qualifying_scope
= NULL_TREE
;
3032 = cp_parser_class_name (parser
,
3033 /*typename_keyword_p=*/false,
3034 /*template_keyword_p=*/false,
3036 /*check_dependency=*/false,
3037 /*class_head_p=*/false);
3038 if (cp_parser_parse_definitely (parser
))
3039 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3041 /* Look in the surrounding context. */
3042 parser
->scope
= NULL_TREE
;
3043 parser
->object_scope
= NULL_TREE
;
3044 parser
->qualifying_scope
= NULL_TREE
;
3046 = cp_parser_class_name (parser
,
3047 /*typename_keyword_p=*/false,
3048 /*template_keyword_p=*/false,
3050 /*check_dependency=*/false,
3051 /*class_head_p=*/false);
3052 /* If an error occurred, assume that the name of the
3053 destructor is the same as the name of the qualifying
3054 class. That allows us to keep parsing after running
3055 into ill-formed destructor names. */
3056 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3057 return build_nt (BIT_NOT_EXPR
, scope
);
3058 else if (type_decl
== error_mark_node
)
3059 return error_mark_node
;
3061 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3065 if (token
->keyword
== RID_OPERATOR
)
3069 /* This could be a template-id, so we try that first. */
3070 cp_parser_parse_tentatively (parser
);
3071 /* Try a template-id. */
3072 id
= cp_parser_template_id (parser
, template_keyword_p
,
3073 /*check_dependency_p=*/true);
3074 /* If that worked, we're done. */
3075 if (cp_parser_parse_definitely (parser
))
3077 /* We still don't know whether we're looking at an
3078 operator-function-id or a conversion-function-id. */
3079 cp_parser_parse_tentatively (parser
);
3080 /* Try an operator-function-id. */
3081 id
= cp_parser_operator_function_id (parser
);
3082 /* If that didn't work, try a conversion-function-id. */
3083 if (!cp_parser_parse_definitely (parser
))
3084 id
= cp_parser_conversion_function_id (parser
);
3091 cp_parser_error (parser
, "expected unqualified-id");
3092 return error_mark_node
;
3096 /* Parse an (optional) nested-name-specifier.
3098 nested-name-specifier:
3099 class-or-namespace-name :: nested-name-specifier [opt]
3100 class-or-namespace-name :: template nested-name-specifier [opt]
3102 PARSER->SCOPE should be set appropriately before this function is
3103 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3104 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3107 Sets PARSER->SCOPE to the class (TYPE) or namespace
3108 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3109 it unchanged if there is no nested-name-specifier. Returns the new
3110 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3113 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3114 bool typename_keyword_p
,
3115 bool check_dependency_p
,
3118 bool success
= false;
3119 tree access_check
= NULL_TREE
;
3123 /* If the next token corresponds to a nested name specifier, there
3124 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3125 false, it may have been true before, in which case something
3126 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3127 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3128 CHECK_DEPENDENCY_P is false, we have to fall through into the
3130 if (check_dependency_p
3131 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3133 cp_parser_pre_parsed_nested_name_specifier (parser
);
3134 return parser
->scope
;
3137 /* Remember where the nested-name-specifier starts. */
3138 if (cp_parser_parsing_tentatively (parser
)
3139 && !cp_parser_committed_to_tentative_parse (parser
))
3141 token
= cp_lexer_peek_token (parser
->lexer
);
3142 start
= cp_lexer_token_difference (parser
->lexer
,
3143 parser
->lexer
->first_token
,
3149 push_deferring_access_checks (dk_deferred
);
3155 tree saved_qualifying_scope
;
3156 bool template_keyword_p
;
3158 /* Spot cases that cannot be the beginning of a
3159 nested-name-specifier. */
3160 token
= cp_lexer_peek_token (parser
->lexer
);
3162 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3163 the already parsed nested-name-specifier. */
3164 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3166 /* Grab the nested-name-specifier and continue the loop. */
3167 cp_parser_pre_parsed_nested_name_specifier (parser
);
3172 /* Spot cases that cannot be the beginning of a
3173 nested-name-specifier. On the second and subsequent times
3174 through the loop, we look for the `template' keyword. */
3175 if (success
&& token
->keyword
== RID_TEMPLATE
)
3177 /* A template-id can start a nested-name-specifier. */
3178 else if (token
->type
== CPP_TEMPLATE_ID
)
3182 /* If the next token is not an identifier, then it is
3183 definitely not a class-or-namespace-name. */
3184 if (token
->type
!= CPP_NAME
)
3186 /* If the following token is neither a `<' (to begin a
3187 template-id), nor a `::', then we are not looking at a
3188 nested-name-specifier. */
3189 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3190 if (token
->type
!= CPP_LESS
&& token
->type
!= CPP_SCOPE
)
3194 /* The nested-name-specifier is optional, so we parse
3196 cp_parser_parse_tentatively (parser
);
3198 /* Look for the optional `template' keyword, if this isn't the
3199 first time through the loop. */
3201 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3203 template_keyword_p
= false;
3205 /* Save the old scope since the name lookup we are about to do
3206 might destroy it. */
3207 old_scope
= parser
->scope
;
3208 saved_qualifying_scope
= parser
->qualifying_scope
;
3209 /* Parse the qualifying entity. */
3211 = cp_parser_class_or_namespace_name (parser
,
3216 /* Look for the `::' token. */
3217 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3219 /* If we found what we wanted, we keep going; otherwise, we're
3221 if (!cp_parser_parse_definitely (parser
))
3223 bool error_p
= false;
3225 /* Restore the OLD_SCOPE since it was valid before the
3226 failed attempt at finding the last
3227 class-or-namespace-name. */
3228 parser
->scope
= old_scope
;
3229 parser
->qualifying_scope
= saved_qualifying_scope
;
3230 /* If the next token is an identifier, and the one after
3231 that is a `::', then any valid interpretation would have
3232 found a class-or-namespace-name. */
3233 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3234 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3236 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3239 token
= cp_lexer_consume_token (parser
->lexer
);
3244 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3245 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3246 error ("`%D' used without template parameters",
3248 else if (parser
->scope
)
3250 if (TYPE_P (parser
->scope
))
3251 error ("`%T::%D' is not a class-name or "
3253 parser
->scope
, token
->value
);
3255 error ("`%D::%D' is not a class-name or "
3257 parser
->scope
, token
->value
);
3260 error ("`%D' is not a class-name or namespace-name",
3262 parser
->scope
= NULL_TREE
;
3264 /* Treat this as a successful nested-name-specifier
3269 If the name found is not a class-name (clause
3270 _class_) or namespace-name (_namespace.def_), the
3271 program is ill-formed. */
3274 cp_lexer_consume_token (parser
->lexer
);
3279 /* We've found one valid nested-name-specifier. */
3281 /* Make sure we look in the right scope the next time through
3283 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3284 ? TREE_TYPE (new_scope
)
3286 /* If it is a class scope, try to complete it; we are about to
3287 be looking up names inside the class. */
3288 if (TYPE_P (parser
->scope
)
3289 /* Since checking types for dependency can be expensive,
3290 avoid doing it if the type is already complete. */
3291 && !COMPLETE_TYPE_P (parser
->scope
)
3292 /* Do not try to complete dependent types. */
3293 && !dependent_type_p (parser
->scope
))
3294 complete_type (parser
->scope
);
3297 /* Retrieve any deferred checks. Do not pop this access checks yet
3298 so the memory will not be reclaimed during token replacing below. */
3299 access_check
= get_deferred_access_checks ();
3301 /* If parsing tentatively, replace the sequence of tokens that makes
3302 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3303 token. That way, should we re-parse the token stream, we will
3304 not have to repeat the effort required to do the parse, nor will
3305 we issue duplicate error messages. */
3306 if (success
&& start
>= 0)
3308 /* Find the token that corresponds to the start of the
3310 token
= cp_lexer_advance_token (parser
->lexer
,
3311 parser
->lexer
->first_token
,
3314 /* Reset the contents of the START token. */
3315 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3316 token
->value
= build_tree_list (access_check
, parser
->scope
);
3317 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3318 token
->keyword
= RID_MAX
;
3319 /* Purge all subsequent tokens. */
3320 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
3323 pop_deferring_access_checks ();
3324 return success
? parser
->scope
: NULL_TREE
;
3327 /* Parse a nested-name-specifier. See
3328 cp_parser_nested_name_specifier_opt for details. This function
3329 behaves identically, except that it will an issue an error if no
3330 nested-name-specifier is present, and it will return
3331 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3335 cp_parser_nested_name_specifier (cp_parser
*parser
,
3336 bool typename_keyword_p
,
3337 bool check_dependency_p
,
3342 /* Look for the nested-name-specifier. */
3343 scope
= cp_parser_nested_name_specifier_opt (parser
,
3347 /* If it was not present, issue an error message. */
3350 cp_parser_error (parser
, "expected nested-name-specifier");
3351 return error_mark_node
;
3357 /* Parse a class-or-namespace-name.
3359 class-or-namespace-name:
3363 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3364 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3365 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3366 TYPE_P is TRUE iff the next name should be taken as a class-name,
3367 even the same name is declared to be another entity in the same
3370 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3371 specified by the class-or-namespace-name. If neither is found the
3372 ERROR_MARK_NODE is returned. */
3375 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3376 bool typename_keyword_p
,
3377 bool template_keyword_p
,
3378 bool check_dependency_p
,
3382 tree saved_qualifying_scope
;
3383 tree saved_object_scope
;
3387 /* Before we try to parse the class-name, we must save away the
3388 current PARSER->SCOPE since cp_parser_class_name will destroy
3390 saved_scope
= parser
->scope
;
3391 saved_qualifying_scope
= parser
->qualifying_scope
;
3392 saved_object_scope
= parser
->object_scope
;
3393 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3394 there is no need to look for a namespace-name. */
3395 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3397 cp_parser_parse_tentatively (parser
);
3398 scope
= cp_parser_class_name (parser
,
3403 /*class_head_p=*/false);
3404 /* If that didn't work, try for a namespace-name. */
3405 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3407 /* Restore the saved scope. */
3408 parser
->scope
= saved_scope
;
3409 parser
->qualifying_scope
= saved_qualifying_scope
;
3410 parser
->object_scope
= saved_object_scope
;
3411 /* If we are not looking at an identifier followed by the scope
3412 resolution operator, then this is not part of a
3413 nested-name-specifier. (Note that this function is only used
3414 to parse the components of a nested-name-specifier.) */
3415 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3416 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3417 return error_mark_node
;
3418 scope
= cp_parser_namespace_name (parser
);
3424 /* Parse a postfix-expression.
3428 postfix-expression [ expression ]
3429 postfix-expression ( expression-list [opt] )
3430 simple-type-specifier ( expression-list [opt] )
3431 typename :: [opt] nested-name-specifier identifier
3432 ( expression-list [opt] )
3433 typename :: [opt] nested-name-specifier template [opt] template-id
3434 ( expression-list [opt] )
3435 postfix-expression . template [opt] id-expression
3436 postfix-expression -> template [opt] id-expression
3437 postfix-expression . pseudo-destructor-name
3438 postfix-expression -> pseudo-destructor-name
3439 postfix-expression ++
3440 postfix-expression --
3441 dynamic_cast < type-id > ( expression )
3442 static_cast < type-id > ( expression )
3443 reinterpret_cast < type-id > ( expression )
3444 const_cast < type-id > ( expression )
3445 typeid ( expression )
3451 ( type-id ) { initializer-list , [opt] }
3453 This extension is a GNU version of the C99 compound-literal
3454 construct. (The C99 grammar uses `type-name' instead of `type-id',
3455 but they are essentially the same concept.)
3457 If ADDRESS_P is true, the postfix expression is the operand of the
3460 Returns a representation of the expression. */
3463 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3467 cp_parser_id_kind idk
= CP_PARSER_ID_KIND_NONE
;
3468 tree postfix_expression
= NULL_TREE
;
3469 /* Non-NULL only if the current postfix-expression can be used to
3470 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3471 class used to qualify the member. */
3472 tree qualifying_class
= NULL_TREE
;
3474 /* Peek at the next token. */
3475 token
= cp_lexer_peek_token (parser
->lexer
);
3476 /* Some of the productions are determined by keywords. */
3477 keyword
= token
->keyword
;
3487 const char *saved_message
;
3489 /* All of these can be handled in the same way from the point
3490 of view of parsing. Begin by consuming the token
3491 identifying the cast. */
3492 cp_lexer_consume_token (parser
->lexer
);
3494 /* New types cannot be defined in the cast. */
3495 saved_message
= parser
->type_definition_forbidden_message
;
3496 parser
->type_definition_forbidden_message
3497 = "types may not be defined in casts";
3499 /* Look for the opening `<'. */
3500 cp_parser_require (parser
, CPP_LESS
, "`<'");
3501 /* Parse the type to which we are casting. */
3502 type
= cp_parser_type_id (parser
);
3503 /* Look for the closing `>'. */
3504 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3505 /* Restore the old message. */
3506 parser
->type_definition_forbidden_message
= saved_message
;
3508 /* And the expression which is being cast. */
3509 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3510 expression
= cp_parser_expression (parser
);
3511 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3513 /* Only type conversions to integral or enumeration types
3514 can be used in constant-expressions. */
3515 if (parser
->constant_expression_p
3516 && !dependent_type_p (type
)
3517 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3519 if (!parser
->allow_non_constant_expression_p
)
3520 return (cp_parser_non_constant_expression
3521 ("a cast to a type other than an integral or "
3522 "enumeration type"));
3523 parser
->non_constant_expression_p
= true;
3530 = build_dynamic_cast (type
, expression
);
3534 = build_static_cast (type
, expression
);
3538 = build_reinterpret_cast (type
, expression
);
3542 = build_const_cast (type
, expression
);
3553 const char *saved_message
;
3555 /* Consume the `typeid' token. */
3556 cp_lexer_consume_token (parser
->lexer
);
3557 /* Look for the `(' token. */
3558 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3559 /* Types cannot be defined in a `typeid' expression. */
3560 saved_message
= parser
->type_definition_forbidden_message
;
3561 parser
->type_definition_forbidden_message
3562 = "types may not be defined in a `typeid\' expression";
3563 /* We can't be sure yet whether we're looking at a type-id or an
3565 cp_parser_parse_tentatively (parser
);
3566 /* Try a type-id first. */
3567 type
= cp_parser_type_id (parser
);
3568 /* Look for the `)' token. Otherwise, we can't be sure that
3569 we're not looking at an expression: consider `typeid (int
3570 (3))', for example. */
3571 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3572 /* If all went well, simply lookup the type-id. */
3573 if (cp_parser_parse_definitely (parser
))
3574 postfix_expression
= get_typeid (type
);
3575 /* Otherwise, fall back to the expression variant. */
3580 /* Look for an expression. */
3581 expression
= cp_parser_expression (parser
);
3582 /* Compute its typeid. */
3583 postfix_expression
= build_typeid (expression
);
3584 /* Look for the `)' token. */
3585 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3588 /* Restore the saved message. */
3589 parser
->type_definition_forbidden_message
= saved_message
;
3595 bool template_p
= false;
3599 /* Consume the `typename' token. */
3600 cp_lexer_consume_token (parser
->lexer
);
3601 /* Look for the optional `::' operator. */
3602 cp_parser_global_scope_opt (parser
,
3603 /*current_scope_valid_p=*/false);
3604 /* Look for the nested-name-specifier. */
3605 cp_parser_nested_name_specifier (parser
,
3606 /*typename_keyword_p=*/true,
3607 /*check_dependency_p=*/true,
3609 /* Look for the optional `template' keyword. */
3610 template_p
= cp_parser_optional_template_keyword (parser
);
3611 /* We don't know whether we're looking at a template-id or an
3613 cp_parser_parse_tentatively (parser
);
3614 /* Try a template-id. */
3615 id
= cp_parser_template_id (parser
, template_p
,
3616 /*check_dependency_p=*/true);
3617 /* If that didn't work, try an identifier. */
3618 if (!cp_parser_parse_definitely (parser
))
3619 id
= cp_parser_identifier (parser
);
3620 /* Create a TYPENAME_TYPE to represent the type to which the
3621 functional cast is being performed. */
3622 type
= make_typename_type (parser
->scope
, id
,
3625 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3633 /* If the next thing is a simple-type-specifier, we may be
3634 looking at a functional cast. We could also be looking at
3635 an id-expression. So, we try the functional cast, and if
3636 that doesn't work we fall back to the primary-expression. */
3637 cp_parser_parse_tentatively (parser
);
3638 /* Look for the simple-type-specifier. */
3639 type
= cp_parser_simple_type_specifier (parser
,
3640 CP_PARSER_FLAGS_NONE
);
3641 /* Parse the cast itself. */
3642 if (!cp_parser_error_occurred (parser
))
3644 = cp_parser_functional_cast (parser
, type
);
3645 /* If that worked, we're done. */
3646 if (cp_parser_parse_definitely (parser
))
3649 /* If the functional-cast didn't work out, try a
3650 compound-literal. */
3651 if (cp_parser_allow_gnu_extensions_p (parser
)
3652 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3654 tree initializer_list
= NULL_TREE
;
3656 cp_parser_parse_tentatively (parser
);
3657 /* Consume the `('. */
3658 cp_lexer_consume_token (parser
->lexer
);
3659 /* Parse the type. */
3660 type
= cp_parser_type_id (parser
);
3661 /* Look for the `)'. */
3662 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3663 /* Look for the `{'. */
3664 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3665 /* If things aren't going well, there's no need to
3667 if (!cp_parser_error_occurred (parser
))
3669 /* Parse the initializer-list. */
3671 = cp_parser_initializer_list (parser
);
3672 /* Allow a trailing `,'. */
3673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3674 cp_lexer_consume_token (parser
->lexer
);
3675 /* Look for the final `}'. */
3676 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3678 /* If that worked, we're definitely looking at a
3679 compound-literal expression. */
3680 if (cp_parser_parse_definitely (parser
))
3682 /* Warn the user that a compound literal is not
3683 allowed in standard C++. */
3685 pedwarn ("ISO C++ forbids compound-literals");
3686 /* Form the representation of the compound-literal. */
3688 = finish_compound_literal (type
, initializer_list
);
3693 /* It must be a primary-expression. */
3694 postfix_expression
= cp_parser_primary_expression (parser
,
3701 /* If we were avoiding committing to the processing of a
3702 qualified-id until we knew whether or not we had a
3703 pointer-to-member, we now know. */
3704 if (qualifying_class
)
3708 /* Peek at the next token. */
3709 token
= cp_lexer_peek_token (parser
->lexer
);
3710 done
= (token
->type
!= CPP_OPEN_SQUARE
3711 && token
->type
!= CPP_OPEN_PAREN
3712 && token
->type
!= CPP_DOT
3713 && token
->type
!= CPP_DEREF
3714 && token
->type
!= CPP_PLUS_PLUS
3715 && token
->type
!= CPP_MINUS_MINUS
);
3717 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3722 return postfix_expression
;
3725 /* Remember that there was a reference to this entity. */
3726 if (DECL_P (postfix_expression
))
3727 mark_used (postfix_expression
);
3729 /* Keep looping until the postfix-expression is complete. */
3732 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3733 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3735 /* It is not a Koenig lookup function call. */
3736 unqualified_name_lookup_error (postfix_expression
);
3737 postfix_expression
= error_mark_node
;
3740 /* Peek at the next token. */
3741 token
= cp_lexer_peek_token (parser
->lexer
);
3743 switch (token
->type
)
3745 case CPP_OPEN_SQUARE
:
3746 /* postfix-expression [ expression ] */
3750 /* Consume the `[' token. */
3751 cp_lexer_consume_token (parser
->lexer
);
3752 /* Parse the index expression. */
3753 index
= cp_parser_expression (parser
);
3754 /* Look for the closing `]'. */
3755 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
3757 /* Build the ARRAY_REF. */
3759 = grok_array_decl (postfix_expression
, index
);
3760 idk
= CP_PARSER_ID_KIND_NONE
;
3764 case CPP_OPEN_PAREN
:
3765 /* postfix-expression ( expression-list [opt] ) */
3769 /* Consume the `(' token. */
3770 cp_lexer_consume_token (parser
->lexer
);
3771 /* If the next token is not a `)', then there are some
3773 if (cp_lexer_next_token_is_not (parser
->lexer
,
3775 args
= cp_parser_expression_list (parser
);
3778 /* Look for the closing `)'. */
3779 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3780 /* Function calls are not permitted in
3781 constant-expressions. */
3782 if (parser
->constant_expression_p
)
3784 if (!parser
->allow_non_constant_expression_p
)
3785 return cp_parser_non_constant_expression ("a function call");
3786 parser
->non_constant_expression_p
= true;
3789 if (idk
== CP_PARSER_ID_KIND_UNQUALIFIED
3790 && (is_overloaded_fn (postfix_expression
)
3791 || DECL_P (postfix_expression
)
3792 || TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3796 tree identifier
= NULL_TREE
;
3797 tree functions
= NULL_TREE
;
3799 /* Find the name of the overloaded function. */
3800 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3801 identifier
= postfix_expression
;
3802 else if (is_overloaded_fn (postfix_expression
))
3804 functions
= postfix_expression
;
3805 identifier
= DECL_NAME (get_first_fn (functions
));
3807 else if (DECL_P (postfix_expression
))
3809 functions
= postfix_expression
;
3810 identifier
= DECL_NAME (postfix_expression
);
3813 /* A call to a namespace-scope function using an
3816 Do Koenig lookup -- unless any of the arguments are
3818 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
3819 if (type_dependent_expression_p (TREE_VALUE (arg
)))
3824 = lookup_arg_dependent (identifier
, functions
, args
);
3825 if (!postfix_expression
)
3827 /* The unqualified name could not be resolved. */
3828 unqualified_name_lookup_error (identifier
);
3829 postfix_expression
= error_mark_node
;
3832 = build_call_from_tree (postfix_expression
, args
,
3833 /*diallow_virtual=*/false);
3836 postfix_expression
= build_min_nt (LOOKUP_EXPR
,
3839 else if (idk
== CP_PARSER_ID_KIND_UNQUALIFIED
3840 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3842 /* The unqualified name could not be resolved. */
3843 unqualified_name_lookup_error (postfix_expression
);
3844 postfix_expression
= error_mark_node
;
3848 /* In the body of a template, no further processing is
3850 if (processing_template_decl
)
3852 postfix_expression
= build_nt (CALL_EXPR
,
3858 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
3860 = (build_new_method_call
3861 (TREE_OPERAND (postfix_expression
, 0),
3862 TREE_OPERAND (postfix_expression
, 1),
3864 (idk
== CP_PARSER_ID_KIND_QUALIFIED
3865 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
3866 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
)
3867 postfix_expression
= (build_offset_ref_call_from_tree
3868 (postfix_expression
, args
));
3869 else if (idk
== CP_PARSER_ID_KIND_QUALIFIED
)
3870 /* A call to a static class member, or a namespace-scope
3873 = finish_call_expr (postfix_expression
, args
,
3874 /*disallow_virtual=*/true);
3876 /* All other function calls. */
3878 = finish_call_expr (postfix_expression
, args
,
3879 /*disallow_virtual=*/false);
3881 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3882 idk
= CP_PARSER_ID_KIND_NONE
;
3888 /* postfix-expression . template [opt] id-expression
3889 postfix-expression . pseudo-destructor-name
3890 postfix-expression -> template [opt] id-expression
3891 postfix-expression -> pseudo-destructor-name */
3896 tree scope
= NULL_TREE
;
3898 /* If this is a `->' operator, dereference the pointer. */
3899 if (token
->type
== CPP_DEREF
)
3900 postfix_expression
= build_x_arrow (postfix_expression
);
3901 /* Check to see whether or not the expression is
3903 dependent_p
= type_dependent_expression_p (postfix_expression
);
3904 /* The identifier following the `->' or `.' is not
3906 parser
->scope
= NULL_TREE
;
3907 parser
->qualifying_scope
= NULL_TREE
;
3908 parser
->object_scope
= NULL_TREE
;
3909 idk
= CP_PARSER_ID_KIND_NONE
;
3910 /* Enter the scope corresponding to the type of the object
3911 given by the POSTFIX_EXPRESSION. */
3913 && TREE_TYPE (postfix_expression
) != NULL_TREE
)
3915 scope
= TREE_TYPE (postfix_expression
);
3916 /* According to the standard, no expression should
3917 ever have reference type. Unfortunately, we do not
3918 currently match the standard in this respect in
3919 that our internal representation of an expression
3920 may have reference type even when the standard says
3921 it does not. Therefore, we have to manually obtain
3922 the underlying type here. */
3923 scope
= non_reference (scope
);
3924 /* If the SCOPE is an OFFSET_TYPE, then we grab the
3925 type of the field. We get an OFFSET_TYPE for
3930 Probably, we should not get an OFFSET_TYPE here;
3931 that transformation should be made only if `&S::T'
3933 if (TREE_CODE (scope
) == OFFSET_TYPE
)
3934 scope
= TREE_TYPE (scope
);
3935 /* The type of the POSTFIX_EXPRESSION must be
3937 scope
= complete_type_or_else (scope
, NULL_TREE
);
3938 /* Let the name lookup machinery know that we are
3939 processing a class member access expression. */
3940 parser
->context
->object_type
= scope
;
3941 /* If something went wrong, we want to be able to
3942 discern that case, as opposed to the case where
3943 there was no SCOPE due to the type of expression
3946 scope
= error_mark_node
;
3949 /* Consume the `.' or `->' operator. */
3950 cp_lexer_consume_token (parser
->lexer
);
3951 /* If the SCOPE is not a scalar type, we are looking at an
3952 ordinary class member access expression, rather than a
3953 pseudo-destructor-name. */
3954 if (!scope
|| !SCALAR_TYPE_P (scope
))
3956 template_p
= cp_parser_optional_template_keyword (parser
);
3957 /* Parse the id-expression. */
3958 name
= cp_parser_id_expression (parser
,
3960 /*check_dependency_p=*/true,
3961 /*template_p=*/NULL
);
3962 /* In general, build a SCOPE_REF if the member name is
3963 qualified. However, if the name was not dependent
3964 and has already been resolved; there is no need to
3965 build the SCOPE_REF. For example;
3967 struct X { void f(); };
3968 template <typename T> void f(T* t) { t->X::f(); }
3970 Even though "t" is dependent, "X::f" is not and has
3971 except that for a BASELINK there is no need to
3972 include scope information. */
3974 /* But we do need to remember that there was an explicit
3975 scope for virtual function calls. */
3977 idk
= CP_PARSER_ID_KIND_QUALIFIED
;
3979 if (name
!= error_mark_node
3980 && !BASELINK_P (name
)
3983 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
3984 parser
->scope
= NULL_TREE
;
3985 parser
->qualifying_scope
= NULL_TREE
;
3986 parser
->object_scope
= NULL_TREE
;
3989 = finish_class_member_access_expr (postfix_expression
, name
);
3991 /* Otherwise, try the pseudo-destructor-name production. */
3997 /* Parse the pseudo-destructor-name. */
3998 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
3999 /* Form the call. */
4001 = finish_pseudo_destructor_expr (postfix_expression
,
4002 s
, TREE_TYPE (type
));
4005 /* We no longer need to look up names in the scope of the
4006 object on the left-hand side of the `.' or `->'
4008 parser
->context
->object_type
= NULL_TREE
;
4013 /* postfix-expression ++ */
4014 /* Consume the `++' token. */
4015 cp_lexer_consume_token (parser
->lexer
);
4016 /* Increments may not appear in constant-expressions. */
4017 if (parser
->constant_expression_p
)
4019 if (!parser
->allow_non_constant_expression_p
)
4020 return cp_parser_non_constant_expression ("an increment");
4021 parser
->non_constant_expression_p
= true;
4023 /* Generate a representation for the complete expression. */
4025 = finish_increment_expr (postfix_expression
,
4026 POSTINCREMENT_EXPR
);
4027 idk
= CP_PARSER_ID_KIND_NONE
;
4030 case CPP_MINUS_MINUS
:
4031 /* postfix-expression -- */
4032 /* Consume the `--' token. */
4033 cp_lexer_consume_token (parser
->lexer
);
4034 /* Decrements may not appear in constant-expressions. */
4035 if (parser
->constant_expression_p
)
4037 if (!parser
->allow_non_constant_expression_p
)
4038 return cp_parser_non_constant_expression ("a decrement");
4039 parser
->non_constant_expression_p
= true;
4041 /* Generate a representation for the complete expression. */
4043 = finish_increment_expr (postfix_expression
,
4044 POSTDECREMENT_EXPR
);
4045 idk
= CP_PARSER_ID_KIND_NONE
;
4049 return postfix_expression
;
4053 /* We should never get here. */
4055 return error_mark_node
;
4058 /* Parse an expression-list.
4061 assignment-expression
4062 expression-list, assignment-expression
4064 Returns a TREE_LIST. The TREE_VALUE of each node is a
4065 representation of an assignment-expression. Note that a TREE_LIST
4066 is returned even if there is only a single expression in the list. */
4069 cp_parser_expression_list (cp_parser
* parser
)
4071 tree expression_list
= NULL_TREE
;
4073 /* Consume expressions until there are no more. */
4078 /* Parse the next assignment-expression. */
4079 expr
= cp_parser_assignment_expression (parser
);
4080 /* Add it to the list. */
4081 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4083 /* If the next token isn't a `,', then we are done. */
4084 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4086 /* All uses of expression-list in the grammar are followed
4087 by a `)'. Therefore, if the next token is not a `)' an
4088 error will be issued, unless we are parsing tentatively.
4089 Skip ahead to see if there is another `,' before the `)';
4090 if so, we can go there and recover. */
4091 if (cp_parser_parsing_tentatively (parser
)
4092 || cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
4093 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser
))
4097 /* Otherwise, consume the `,' and keep going. */
4098 cp_lexer_consume_token (parser
->lexer
);
4101 /* We built up the list in reverse order so we must reverse it now. */
4102 return nreverse (expression_list
);
4105 /* Parse a pseudo-destructor-name.
4107 pseudo-destructor-name:
4108 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4109 :: [opt] nested-name-specifier template template-id :: ~ type-name
4110 :: [opt] nested-name-specifier [opt] ~ type-name
4112 If either of the first two productions is used, sets *SCOPE to the
4113 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4114 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4115 or ERROR_MARK_NODE if no type-name is present. */
4118 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4122 bool nested_name_specifier_p
;
4124 /* Look for the optional `::' operator. */
4125 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4126 /* Look for the optional nested-name-specifier. */
4127 nested_name_specifier_p
4128 = (cp_parser_nested_name_specifier_opt (parser
,
4129 /*typename_keyword_p=*/false,
4130 /*check_dependency_p=*/true,
4133 /* Now, if we saw a nested-name-specifier, we might be doing the
4134 second production. */
4135 if (nested_name_specifier_p
4136 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4138 /* Consume the `template' keyword. */
4139 cp_lexer_consume_token (parser
->lexer
);
4140 /* Parse the template-id. */
4141 cp_parser_template_id (parser
,
4142 /*template_keyword_p=*/true,
4143 /*check_dependency_p=*/false);
4144 /* Look for the `::' token. */
4145 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4147 /* If the next token is not a `~', then there might be some
4148 additional qualification. */
4149 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4151 /* Look for the type-name. */
4152 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4153 /* Look for the `::' token. */
4154 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4159 /* Look for the `~'. */
4160 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4161 /* Look for the type-name again. We are not responsible for
4162 checking that it matches the first type-name. */
4163 *type
= cp_parser_type_name (parser
);
4166 /* Parse a unary-expression.
4172 unary-operator cast-expression
4173 sizeof unary-expression
4181 __extension__ cast-expression
4182 __alignof__ unary-expression
4183 __alignof__ ( type-id )
4184 __real__ cast-expression
4185 __imag__ cast-expression
4188 ADDRESS_P is true iff the unary-expression is appearing as the
4189 operand of the `&' operator.
4191 Returns a representation of the expression. */
4194 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4197 enum tree_code unary_operator
;
4199 /* Peek at the next token. */
4200 token
= cp_lexer_peek_token (parser
->lexer
);
4201 /* Some keywords give away the kind of expression. */
4202 if (token
->type
== CPP_KEYWORD
)
4204 enum rid keyword
= token
->keyword
;
4210 /* Consume the `alignof' token. */
4211 cp_lexer_consume_token (parser
->lexer
);
4212 /* Parse the operand. */
4213 return finish_alignof (cp_parser_sizeof_operand
4221 /* Consume the `sizeof' token. */
4222 cp_lexer_consume_token (parser
->lexer
);
4223 /* Parse the operand. */
4224 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4226 /* If the type of the operand cannot be determined build a
4228 if (TYPE_P (operand
)
4229 ? dependent_type_p (operand
)
4230 : type_dependent_expression_p (operand
))
4231 return build_min (SIZEOF_EXPR
, size_type_node
, operand
);
4232 /* Otherwise, compute the constant value. */
4234 return finish_sizeof (operand
);
4238 return cp_parser_new_expression (parser
);
4241 return cp_parser_delete_expression (parser
);
4245 /* The saved value of the PEDANTIC flag. */
4249 /* Save away the PEDANTIC flag. */
4250 cp_parser_extension_opt (parser
, &saved_pedantic
);
4251 /* Parse the cast-expression. */
4252 expr
= cp_parser_simple_cast_expression (parser
);
4253 /* Restore the PEDANTIC flag. */
4254 pedantic
= saved_pedantic
;
4264 /* Consume the `__real__' or `__imag__' token. */
4265 cp_lexer_consume_token (parser
->lexer
);
4266 /* Parse the cast-expression. */
4267 expression
= cp_parser_simple_cast_expression (parser
);
4268 /* Create the complete representation. */
4269 return build_x_unary_op ((keyword
== RID_REALPART
4270 ? REALPART_EXPR
: IMAGPART_EXPR
),
4280 /* Look for the `:: new' and `:: delete', which also signal the
4281 beginning of a new-expression, or delete-expression,
4282 respectively. If the next token is `::', then it might be one of
4284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4288 /* See if the token after the `::' is one of the keywords in
4289 which we're interested. */
4290 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4291 /* If it's `new', we have a new-expression. */
4292 if (keyword
== RID_NEW
)
4293 return cp_parser_new_expression (parser
);
4294 /* Similarly, for `delete'. */
4295 else if (keyword
== RID_DELETE
)
4296 return cp_parser_delete_expression (parser
);
4299 /* Look for a unary operator. */
4300 unary_operator
= cp_parser_unary_operator (token
);
4301 /* The `++' and `--' operators can be handled similarly, even though
4302 they are not technically unary-operators in the grammar. */
4303 if (unary_operator
== ERROR_MARK
)
4305 if (token
->type
== CPP_PLUS_PLUS
)
4306 unary_operator
= PREINCREMENT_EXPR
;
4307 else if (token
->type
== CPP_MINUS_MINUS
)
4308 unary_operator
= PREDECREMENT_EXPR
;
4309 /* Handle the GNU address-of-label extension. */
4310 else if (cp_parser_allow_gnu_extensions_p (parser
)
4311 && token
->type
== CPP_AND_AND
)
4315 /* Consume the '&&' token. */
4316 cp_lexer_consume_token (parser
->lexer
);
4317 /* Look for the identifier. */
4318 identifier
= cp_parser_identifier (parser
);
4319 /* Create an expression representing the address. */
4320 return finish_label_address_expr (identifier
);
4323 if (unary_operator
!= ERROR_MARK
)
4325 tree cast_expression
;
4327 /* Consume the operator token. */
4328 token
= cp_lexer_consume_token (parser
->lexer
);
4329 /* Parse the cast-expression. */
4331 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4332 /* Now, build an appropriate representation. */
4333 switch (unary_operator
)
4336 return build_x_indirect_ref (cast_expression
, "unary *");
4339 return build_x_unary_op (ADDR_EXPR
, cast_expression
);
4341 case PREINCREMENT_EXPR
:
4342 case PREDECREMENT_EXPR
:
4343 if (parser
->constant_expression_p
)
4345 if (!parser
->allow_non_constant_expression_p
)
4346 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4349 parser
->non_constant_expression_p
= true;
4354 case TRUTH_NOT_EXPR
:
4355 return finish_unary_op_expr (unary_operator
, cast_expression
);
4358 return build_x_unary_op (BIT_NOT_EXPR
, cast_expression
);
4362 return error_mark_node
;
4366 return cp_parser_postfix_expression (parser
, address_p
);
4369 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4370 unary-operator, the corresponding tree code is returned. */
4372 static enum tree_code
4373 cp_parser_unary_operator (cp_token
* token
)
4375 switch (token
->type
)
4378 return INDIRECT_REF
;
4384 return CONVERT_EXPR
;
4390 return TRUTH_NOT_EXPR
;
4393 return BIT_NOT_EXPR
;
4400 /* Parse a new-expression.
4403 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4404 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4406 Returns a representation of the expression. */
4409 cp_parser_new_expression (cp_parser
* parser
)
4411 bool global_scope_p
;
4416 /* Look for the optional `::' operator. */
4418 = (cp_parser_global_scope_opt (parser
,
4419 /*current_scope_valid_p=*/false)
4421 /* Look for the `new' operator. */
4422 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4423 /* There's no easy way to tell a new-placement from the
4424 `( type-id )' construct. */
4425 cp_parser_parse_tentatively (parser
);
4426 /* Look for a new-placement. */
4427 placement
= cp_parser_new_placement (parser
);
4428 /* If that didn't work out, there's no new-placement. */
4429 if (!cp_parser_parse_definitely (parser
))
4430 placement
= NULL_TREE
;
4432 /* If the next token is a `(', then we have a parenthesized
4434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4436 /* Consume the `('. */
4437 cp_lexer_consume_token (parser
->lexer
);
4438 /* Parse the type-id. */
4439 type
= cp_parser_type_id (parser
);
4440 /* Look for the closing `)'. */
4441 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4443 /* Otherwise, there must be a new-type-id. */
4445 type
= cp_parser_new_type_id (parser
);
4447 /* If the next token is a `(', then we have a new-initializer. */
4448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4449 initializer
= cp_parser_new_initializer (parser
);
4451 initializer
= NULL_TREE
;
4453 /* Create a representation of the new-expression. */
4454 return build_new (placement
, type
, initializer
, global_scope_p
);
4457 /* Parse a new-placement.
4462 Returns the same representation as for an expression-list. */
4465 cp_parser_new_placement (cp_parser
* parser
)
4467 tree expression_list
;
4469 /* Look for the opening `('. */
4470 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4471 return error_mark_node
;
4472 /* Parse the expression-list. */
4473 expression_list
= cp_parser_expression_list (parser
);
4474 /* Look for the closing `)'. */
4475 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4477 return expression_list
;
4480 /* Parse a new-type-id.
4483 type-specifier-seq new-declarator [opt]
4485 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4486 and whose TREE_VALUE is the new-declarator. */
4489 cp_parser_new_type_id (cp_parser
* parser
)
4491 tree type_specifier_seq
;
4493 const char *saved_message
;
4495 /* The type-specifier sequence must not contain type definitions.
4496 (It cannot contain declarations of new types either, but if they
4497 are not definitions we will catch that because they are not
4499 saved_message
= parser
->type_definition_forbidden_message
;
4500 parser
->type_definition_forbidden_message
4501 = "types may not be defined in a new-type-id";
4502 /* Parse the type-specifier-seq. */
4503 type_specifier_seq
= cp_parser_type_specifier_seq (parser
);
4504 /* Restore the old message. */
4505 parser
->type_definition_forbidden_message
= saved_message
;
4506 /* Parse the new-declarator. */
4507 declarator
= cp_parser_new_declarator_opt (parser
);
4509 return build_tree_list (type_specifier_seq
, declarator
);
4512 /* Parse an (optional) new-declarator.
4515 ptr-operator new-declarator [opt]
4516 direct-new-declarator
4518 Returns a representation of the declarator. See
4519 cp_parser_declarator for the representations used. */
4522 cp_parser_new_declarator_opt (cp_parser
* parser
)
4524 enum tree_code code
;
4526 tree cv_qualifier_seq
;
4528 /* We don't know if there's a ptr-operator next, or not. */
4529 cp_parser_parse_tentatively (parser
);
4530 /* Look for a ptr-operator. */
4531 code
= cp_parser_ptr_operator (parser
, &type
, &cv_qualifier_seq
);
4532 /* If that worked, look for more new-declarators. */
4533 if (cp_parser_parse_definitely (parser
))
4537 /* Parse another optional declarator. */
4538 declarator
= cp_parser_new_declarator_opt (parser
);
4540 /* Create the representation of the declarator. */
4541 if (code
== INDIRECT_REF
)
4542 declarator
= make_pointer_declarator (cv_qualifier_seq
,
4545 declarator
= make_reference_declarator (cv_qualifier_seq
,
4548 /* Handle the pointer-to-member case. */
4550 declarator
= build_nt (SCOPE_REF
, type
, declarator
);
4555 /* If the next token is a `[', there is a direct-new-declarator. */
4556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4557 return cp_parser_direct_new_declarator (parser
);
4562 /* Parse a direct-new-declarator.
4564 direct-new-declarator:
4566 direct-new-declarator [constant-expression]
4568 Returns an ARRAY_REF, following the same conventions as are
4569 documented for cp_parser_direct_declarator. */
4572 cp_parser_direct_new_declarator (cp_parser
* parser
)
4574 tree declarator
= NULL_TREE
;
4580 /* Look for the opening `['. */
4581 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4582 /* The first expression is not required to be constant. */
4585 expression
= cp_parser_expression (parser
);
4586 /* The standard requires that the expression have integral
4587 type. DR 74 adds enumeration types. We believe that the
4588 real intent is that these expressions be handled like the
4589 expression in a `switch' condition, which also allows
4590 classes with a single conversion to integral or
4591 enumeration type. */
4592 if (!processing_template_decl
)
4595 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4600 error ("expression in new-declarator must have integral or enumeration type");
4601 expression
= error_mark_node
;
4605 /* But all the other expressions must be. */
4608 = cp_parser_constant_expression (parser
,
4609 /*allow_non_constant=*/false,
4611 /* Look for the closing `]'. */
4612 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4614 /* Add this bound to the declarator. */
4615 declarator
= build_nt (ARRAY_REF
, declarator
, expression
);
4617 /* If the next token is not a `[', then there are no more
4619 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
4626 /* Parse a new-initializer.
4629 ( expression-list [opt] )
4631 Returns a representation of the expression-list. If there is no
4632 expression-list, VOID_ZERO_NODE is returned. */
4635 cp_parser_new_initializer (cp_parser
* parser
)
4637 tree expression_list
;
4639 /* Look for the opening parenthesis. */
4640 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4641 /* If the next token is not a `)', then there is an
4643 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4644 expression_list
= cp_parser_expression_list (parser
);
4646 expression_list
= void_zero_node
;
4647 /* Look for the closing parenthesis. */
4648 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4650 return expression_list
;
4653 /* Parse a delete-expression.
4656 :: [opt] delete cast-expression
4657 :: [opt] delete [ ] cast-expression
4659 Returns a representation of the expression. */
4662 cp_parser_delete_expression (cp_parser
* parser
)
4664 bool global_scope_p
;
4668 /* Look for the optional `::' operator. */
4670 = (cp_parser_global_scope_opt (parser
,
4671 /*current_scope_valid_p=*/false)
4673 /* Look for the `delete' keyword. */
4674 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
4675 /* See if the array syntax is in use. */
4676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4678 /* Consume the `[' token. */
4679 cp_lexer_consume_token (parser
->lexer
);
4680 /* Look for the `]' token. */
4681 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4682 /* Remember that this is the `[]' construct. */
4688 /* Parse the cast-expression. */
4689 expression
= cp_parser_simple_cast_expression (parser
);
4691 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
4694 /* Parse a cast-expression.
4698 ( type-id ) cast-expression
4700 Returns a representation of the expression. */
4703 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
4705 /* If it's a `(', then we might be looking at a cast. */
4706 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4708 tree type
= NULL_TREE
;
4709 tree expr
= NULL_TREE
;
4710 bool compound_literal_p
;
4711 const char *saved_message
;
4713 /* There's no way to know yet whether or not this is a cast.
4714 For example, `(int (3))' is a unary-expression, while `(int)
4715 3' is a cast. So, we resort to parsing tentatively. */
4716 cp_parser_parse_tentatively (parser
);
4717 /* Types may not be defined in a cast. */
4718 saved_message
= parser
->type_definition_forbidden_message
;
4719 parser
->type_definition_forbidden_message
4720 = "types may not be defined in casts";
4721 /* Consume the `('. */
4722 cp_lexer_consume_token (parser
->lexer
);
4723 /* A very tricky bit is that `(struct S) { 3 }' is a
4724 compound-literal (which we permit in C++ as an extension).
4725 But, that construct is not a cast-expression -- it is a
4726 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4727 is legal; if the compound-literal were a cast-expression,
4728 you'd need an extra set of parentheses.) But, if we parse
4729 the type-id, and it happens to be a class-specifier, then we
4730 will commit to the parse at that point, because we cannot
4731 undo the action that is done when creating a new class. So,
4732 then we cannot back up and do a postfix-expression.
4734 Therefore, we scan ahead to the closing `)', and check to see
4735 if the token after the `)' is a `{'. If so, we are not
4736 looking at a cast-expression.
4738 Save tokens so that we can put them back. */
4739 cp_lexer_save_tokens (parser
->lexer
);
4740 /* Skip tokens until the next token is a closing parenthesis.
4741 If we find the closing `)', and the next token is a `{', then
4742 we are looking at a compound-literal. */
4744 = (cp_parser_skip_to_closing_parenthesis (parser
)
4745 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
4746 /* Roll back the tokens we skipped. */
4747 cp_lexer_rollback_tokens (parser
->lexer
);
4748 /* If we were looking at a compound-literal, simulate an error
4749 so that the call to cp_parser_parse_definitely below will
4751 if (compound_literal_p
)
4752 cp_parser_simulate_error (parser
);
4755 /* Look for the type-id. */
4756 type
= cp_parser_type_id (parser
);
4757 /* Look for the closing `)'. */
4758 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4761 /* Restore the saved message. */
4762 parser
->type_definition_forbidden_message
= saved_message
;
4764 /* If ok so far, parse the dependent expression. We cannot be
4765 sure it is a cast. Consider `(T ())'. It is a parenthesized
4766 ctor of T, but looks like a cast to function returning T
4767 without a dependent expression. */
4768 if (!cp_parser_error_occurred (parser
))
4769 expr
= cp_parser_simple_cast_expression (parser
);
4771 if (cp_parser_parse_definitely (parser
))
4773 /* Warn about old-style casts, if so requested. */
4774 if (warn_old_style_cast
4775 && !in_system_header
4776 && !VOID_TYPE_P (type
)
4777 && current_lang_name
!= lang_name_c
)
4778 warning ("use of old-style cast");
4780 /* Only type conversions to integral or enumeration types
4781 can be used in constant-expressions. */
4782 if (parser
->constant_expression_p
4783 && !dependent_type_p (type
)
4784 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4786 if (!parser
->allow_non_constant_expression_p
)
4787 return (cp_parser_non_constant_expression
4788 ("a casts to a type other than an integral or "
4789 "enumeration type"));
4790 parser
->non_constant_expression_p
= true;
4792 /* Perform the cast. */
4793 expr
= build_c_cast (type
, expr
);
4798 /* If we get here, then it's not a cast, so it must be a
4799 unary-expression. */
4800 return cp_parser_unary_expression (parser
, address_p
);
4803 /* Parse a pm-expression.
4807 pm-expression .* cast-expression
4808 pm-expression ->* cast-expression
4810 Returns a representation of the expression. */
4813 cp_parser_pm_expression (cp_parser
* parser
)
4815 static const cp_parser_token_tree_map map
= {
4816 { CPP_DEREF_STAR
, MEMBER_REF
},
4817 { CPP_DOT_STAR
, DOTSTAR_EXPR
},
4818 { CPP_EOF
, ERROR_MARK
}
4821 return cp_parser_binary_expression (parser
, map
,
4822 cp_parser_simple_cast_expression
);
4825 /* Parse a multiplicative-expression.
4827 mulitplicative-expression:
4829 multiplicative-expression * pm-expression
4830 multiplicative-expression / pm-expression
4831 multiplicative-expression % pm-expression
4833 Returns a representation of the expression. */
4836 cp_parser_multiplicative_expression (cp_parser
* parser
)
4838 static const cp_parser_token_tree_map map
= {
4839 { CPP_MULT
, MULT_EXPR
},
4840 { CPP_DIV
, TRUNC_DIV_EXPR
},
4841 { CPP_MOD
, TRUNC_MOD_EXPR
},
4842 { CPP_EOF
, ERROR_MARK
}
4845 return cp_parser_binary_expression (parser
,
4847 cp_parser_pm_expression
);
4850 /* Parse an additive-expression.
4852 additive-expression:
4853 multiplicative-expression
4854 additive-expression + multiplicative-expression
4855 additive-expression - multiplicative-expression
4857 Returns a representation of the expression. */
4860 cp_parser_additive_expression (cp_parser
* parser
)
4862 static const cp_parser_token_tree_map map
= {
4863 { CPP_PLUS
, PLUS_EXPR
},
4864 { CPP_MINUS
, MINUS_EXPR
},
4865 { CPP_EOF
, ERROR_MARK
}
4868 return cp_parser_binary_expression (parser
,
4870 cp_parser_multiplicative_expression
);
4873 /* Parse a shift-expression.
4877 shift-expression << additive-expression
4878 shift-expression >> additive-expression
4880 Returns a representation of the expression. */
4883 cp_parser_shift_expression (cp_parser
* parser
)
4885 static const cp_parser_token_tree_map map
= {
4886 { CPP_LSHIFT
, LSHIFT_EXPR
},
4887 { CPP_RSHIFT
, RSHIFT_EXPR
},
4888 { CPP_EOF
, ERROR_MARK
}
4891 return cp_parser_binary_expression (parser
,
4893 cp_parser_additive_expression
);
4896 /* Parse a relational-expression.
4898 relational-expression:
4900 relational-expression < shift-expression
4901 relational-expression > shift-expression
4902 relational-expression <= shift-expression
4903 relational-expression >= shift-expression
4907 relational-expression:
4908 relational-expression <? shift-expression
4909 relational-expression >? shift-expression
4911 Returns a representation of the expression. */
4914 cp_parser_relational_expression (cp_parser
* parser
)
4916 static const cp_parser_token_tree_map map
= {
4917 { CPP_LESS
, LT_EXPR
},
4918 { CPP_GREATER
, GT_EXPR
},
4919 { CPP_LESS_EQ
, LE_EXPR
},
4920 { CPP_GREATER_EQ
, GE_EXPR
},
4921 { CPP_MIN
, MIN_EXPR
},
4922 { CPP_MAX
, MAX_EXPR
},
4923 { CPP_EOF
, ERROR_MARK
}
4926 return cp_parser_binary_expression (parser
,
4928 cp_parser_shift_expression
);
4931 /* Parse an equality-expression.
4933 equality-expression:
4934 relational-expression
4935 equality-expression == relational-expression
4936 equality-expression != relational-expression
4938 Returns a representation of the expression. */
4941 cp_parser_equality_expression (cp_parser
* parser
)
4943 static const cp_parser_token_tree_map map
= {
4944 { CPP_EQ_EQ
, EQ_EXPR
},
4945 { CPP_NOT_EQ
, NE_EXPR
},
4946 { CPP_EOF
, ERROR_MARK
}
4949 return cp_parser_binary_expression (parser
,
4951 cp_parser_relational_expression
);
4954 /* Parse an and-expression.
4958 and-expression & equality-expression
4960 Returns a representation of the expression. */
4963 cp_parser_and_expression (cp_parser
* parser
)
4965 static const cp_parser_token_tree_map map
= {
4966 { CPP_AND
, BIT_AND_EXPR
},
4967 { CPP_EOF
, ERROR_MARK
}
4970 return cp_parser_binary_expression (parser
,
4972 cp_parser_equality_expression
);
4975 /* Parse an exclusive-or-expression.
4977 exclusive-or-expression:
4979 exclusive-or-expression ^ and-expression
4981 Returns a representation of the expression. */
4984 cp_parser_exclusive_or_expression (cp_parser
* parser
)
4986 static const cp_parser_token_tree_map map
= {
4987 { CPP_XOR
, BIT_XOR_EXPR
},
4988 { CPP_EOF
, ERROR_MARK
}
4991 return cp_parser_binary_expression (parser
,
4993 cp_parser_and_expression
);
4997 /* Parse an inclusive-or-expression.
4999 inclusive-or-expression:
5000 exclusive-or-expression
5001 inclusive-or-expression | exclusive-or-expression
5003 Returns a representation of the expression. */
5006 cp_parser_inclusive_or_expression (cp_parser
* parser
)
5008 static const cp_parser_token_tree_map map
= {
5009 { CPP_OR
, BIT_IOR_EXPR
},
5010 { CPP_EOF
, ERROR_MARK
}
5013 return cp_parser_binary_expression (parser
,
5015 cp_parser_exclusive_or_expression
);
5018 /* Parse a logical-and-expression.
5020 logical-and-expression:
5021 inclusive-or-expression
5022 logical-and-expression && inclusive-or-expression
5024 Returns a representation of the expression. */
5027 cp_parser_logical_and_expression (cp_parser
* parser
)
5029 static const cp_parser_token_tree_map map
= {
5030 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
},
5031 { CPP_EOF
, ERROR_MARK
}
5034 return cp_parser_binary_expression (parser
,
5036 cp_parser_inclusive_or_expression
);
5039 /* Parse a logical-or-expression.
5041 logical-or-expression:
5042 logical-and-expression
5043 logical-or-expression || logical-and-expression
5045 Returns a representation of the expression. */
5048 cp_parser_logical_or_expression (cp_parser
* parser
)
5050 static const cp_parser_token_tree_map map
= {
5051 { CPP_OR_OR
, TRUTH_ORIF_EXPR
},
5052 { CPP_EOF
, ERROR_MARK
}
5055 return cp_parser_binary_expression (parser
,
5057 cp_parser_logical_and_expression
);
5060 /* Parse a conditional-expression.
5062 conditional-expression:
5063 logical-or-expression
5064 logical-or-expression ? expression : assignment-expression
5068 conditional-expression:
5069 logical-or-expression ? : assignment-expression
5071 Returns a representation of the expression. */
5074 cp_parser_conditional_expression (cp_parser
* parser
)
5076 tree logical_or_expr
;
5078 /* Parse the logical-or-expression. */
5079 logical_or_expr
= cp_parser_logical_or_expression (parser
);
5080 /* If the next token is a `?', then we have a real conditional
5082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5083 return cp_parser_question_colon_clause (parser
, logical_or_expr
);
5084 /* Otherwise, the value is simply the logical-or-expression. */
5086 return logical_or_expr
;
5089 /* Parse the `? expression : assignment-expression' part of a
5090 conditional-expression. The LOGICAL_OR_EXPR is the
5091 logical-or-expression that started the conditional-expression.
5092 Returns a representation of the entire conditional-expression.
5094 This routine exists only so that it can be shared between
5095 cp_parser_conditional_expression and
5096 cp_parser_assignment_expression.
5098 ? expression : assignment-expression
5102 ? : assignment-expression */
5105 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5108 tree assignment_expr
;
5110 /* Consume the `?' token. */
5111 cp_lexer_consume_token (parser
->lexer
);
5112 if (cp_parser_allow_gnu_extensions_p (parser
)
5113 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5114 /* Implicit true clause. */
5117 /* Parse the expression. */
5118 expr
= cp_parser_expression (parser
);
5120 /* The next token should be a `:'. */
5121 cp_parser_require (parser
, CPP_COLON
, "`:'");
5122 /* Parse the assignment-expression. */
5123 assignment_expr
= cp_parser_assignment_expression (parser
);
5125 /* Build the conditional-expression. */
5126 return build_x_conditional_expr (logical_or_expr
,
5131 /* Parse an assignment-expression.
5133 assignment-expression:
5134 conditional-expression
5135 logical-or-expression assignment-operator assignment_expression
5138 Returns a representation for the expression. */
5141 cp_parser_assignment_expression (cp_parser
* parser
)
5145 /* If the next token is the `throw' keyword, then we're looking at
5146 a throw-expression. */
5147 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5148 expr
= cp_parser_throw_expression (parser
);
5149 /* Otherwise, it must be that we are looking at a
5150 logical-or-expression. */
5153 /* Parse the logical-or-expression. */
5154 expr
= cp_parser_logical_or_expression (parser
);
5155 /* If the next token is a `?' then we're actually looking at a
5156 conditional-expression. */
5157 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5158 return cp_parser_question_colon_clause (parser
, expr
);
5161 enum tree_code assignment_operator
;
5163 /* If it's an assignment-operator, we're using the second
5166 = cp_parser_assignment_operator_opt (parser
);
5167 if (assignment_operator
!= ERROR_MARK
)
5171 /* Parse the right-hand side of the assignment. */
5172 rhs
= cp_parser_assignment_expression (parser
);
5173 /* An assignment may not appear in a
5174 constant-expression. */
5175 if (parser
->constant_expression_p
)
5177 if (!parser
->allow_non_constant_expression_p
)
5178 return cp_parser_non_constant_expression ("an assignment");
5179 parser
->non_constant_expression_p
= true;
5181 /* Build the assignment expression. */
5182 expr
= build_x_modify_expr (expr
,
5183 assignment_operator
,
5192 /* Parse an (optional) assignment-operator.
5194 assignment-operator: one of
5195 = *= /= %= += -= >>= <<= &= ^= |=
5199 assignment-operator: one of
5202 If the next token is an assignment operator, the corresponding tree
5203 code is returned, and the token is consumed. For example, for
5204 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5205 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5206 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5207 operator, ERROR_MARK is returned. */
5209 static enum tree_code
5210 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5215 /* Peek at the next toen. */
5216 token
= cp_lexer_peek_token (parser
->lexer
);
5218 switch (token
->type
)
5229 op
= TRUNC_DIV_EXPR
;
5233 op
= TRUNC_MOD_EXPR
;
5273 /* Nothing else is an assignment operator. */
5277 /* If it was an assignment operator, consume it. */
5278 if (op
!= ERROR_MARK
)
5279 cp_lexer_consume_token (parser
->lexer
);
5284 /* Parse an expression.
5287 assignment-expression
5288 expression , assignment-expression
5290 Returns a representation of the expression. */
5293 cp_parser_expression (cp_parser
* parser
)
5295 tree expression
= NULL_TREE
;
5296 bool saw_comma_p
= false;
5300 tree assignment_expression
;
5302 /* Parse the next assignment-expression. */
5303 assignment_expression
5304 = cp_parser_assignment_expression (parser
);
5305 /* If this is the first assignment-expression, we can just
5308 expression
= assignment_expression
;
5309 /* Otherwise, chain the expressions together. It is unclear why
5310 we do not simply build COMPOUND_EXPRs as we go. */
5312 expression
= tree_cons (NULL_TREE
,
5313 assignment_expression
,
5315 /* If the next token is not a comma, then we are done with the
5317 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5319 /* Consume the `,'. */
5320 cp_lexer_consume_token (parser
->lexer
);
5321 /* The first time we see a `,', we must take special action
5322 because the representation used for a single expression is
5323 different from that used for a list containing the single
5327 /* Remember that this expression has a `,' in it. */
5329 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5330 additional expressions to it. */
5331 expression
= build_tree_list (NULL_TREE
, expression
);
5335 /* Build a COMPOUND_EXPR to represent the entire expression, if
5336 necessary. We built up the list in reverse order, so we must
5337 straighten it out here. */
5340 /* A comma operator cannot appear in a constant-expression. */
5341 if (parser
->constant_expression_p
)
5343 if (!parser
->allow_non_constant_expression_p
)
5344 return cp_parser_non_constant_expression ("a comma operator");
5345 parser
->non_constant_expression_p
= true;
5347 expression
= build_x_compound_expr (nreverse (expression
));
5353 /* Parse a constant-expression.
5355 constant-expression:
5356 conditional-expression
5358 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5359 accepted. In that case *NON_CONSTANT_P is set to TRUE. If
5360 ALLOW_NON_CONSTANT_P is false, NON_CONSTANT_P should be NULL. */
5363 cp_parser_constant_expression (cp_parser
* parser
,
5364 bool allow_non_constant_p
,
5365 bool *non_constant_p
)
5367 bool saved_constant_expression_p
;
5368 bool saved_allow_non_constant_expression_p
;
5369 bool saved_non_constant_expression_p
;
5372 /* It might seem that we could simply parse the
5373 conditional-expression, and then check to see if it were
5374 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5375 one that the compiler can figure out is constant, possibly after
5376 doing some simplifications or optimizations. The standard has a
5377 precise definition of constant-expression, and we must honor
5378 that, even though it is somewhat more restrictive.
5384 is not a legal declaration, because `(2, 3)' is not a
5385 constant-expression. The `,' operator is forbidden in a
5386 constant-expression. However, GCC's constant-folding machinery
5387 will fold this operation to an INTEGER_CST for `3'. */
5389 /* Save the old settings. */
5390 saved_constant_expression_p
= parser
->constant_expression_p
;
5391 saved_allow_non_constant_expression_p
5392 = parser
->allow_non_constant_expression_p
;
5393 saved_non_constant_expression_p
= parser
->non_constant_expression_p
;
5394 /* We are now parsing a constant-expression. */
5395 parser
->constant_expression_p
= true;
5396 parser
->allow_non_constant_expression_p
= allow_non_constant_p
;
5397 parser
->non_constant_expression_p
= false;
5398 /* Parse the conditional-expression. */
5399 expression
= cp_parser_conditional_expression (parser
);
5400 /* Restore the old settings. */
5401 parser
->constant_expression_p
= saved_constant_expression_p
;
5402 parser
->allow_non_constant_expression_p
5403 = saved_allow_non_constant_expression_p
;
5404 if (allow_non_constant_p
)
5405 *non_constant_p
= parser
->non_constant_expression_p
;
5406 parser
->non_constant_expression_p
= saved_non_constant_expression_p
;
5411 /* Statements [gram.stmt.stmt] */
5413 /* Parse a statement.
5417 expression-statement
5422 declaration-statement
5426 cp_parser_statement (cp_parser
* parser
)
5430 int statement_line_number
;
5432 /* There is no statement yet. */
5433 statement
= NULL_TREE
;
5434 /* Peek at the next token. */
5435 token
= cp_lexer_peek_token (parser
->lexer
);
5436 /* Remember the line number of the first token in the statement. */
5437 statement_line_number
= token
->location
.line
;
5438 /* If this is a keyword, then that will often determine what kind of
5439 statement we have. */
5440 if (token
->type
== CPP_KEYWORD
)
5442 enum rid keyword
= token
->keyword
;
5448 statement
= cp_parser_labeled_statement (parser
);
5453 statement
= cp_parser_selection_statement (parser
);
5459 statement
= cp_parser_iteration_statement (parser
);
5466 statement
= cp_parser_jump_statement (parser
);
5470 statement
= cp_parser_try_block (parser
);
5474 /* It might be a keyword like `int' that can start a
5475 declaration-statement. */
5479 else if (token
->type
== CPP_NAME
)
5481 /* If the next token is a `:', then we are looking at a
5482 labeled-statement. */
5483 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5484 if (token
->type
== CPP_COLON
)
5485 statement
= cp_parser_labeled_statement (parser
);
5487 /* Anything that starts with a `{' must be a compound-statement. */
5488 else if (token
->type
== CPP_OPEN_BRACE
)
5489 statement
= cp_parser_compound_statement (parser
);
5491 /* Everything else must be a declaration-statement or an
5492 expression-statement. Try for the declaration-statement
5493 first, unless we are looking at a `;', in which case we know that
5494 we have an expression-statement. */
5497 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5499 cp_parser_parse_tentatively (parser
);
5500 /* Try to parse the declaration-statement. */
5501 cp_parser_declaration_statement (parser
);
5502 /* If that worked, we're done. */
5503 if (cp_parser_parse_definitely (parser
))
5506 /* Look for an expression-statement instead. */
5507 statement
= cp_parser_expression_statement (parser
);
5510 /* Set the line number for the statement. */
5511 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5512 STMT_LINENO (statement
) = statement_line_number
;
5515 /* Parse a labeled-statement.
5518 identifier : statement
5519 case constant-expression : statement
5522 Returns the new CASE_LABEL, for a `case' or `default' label. For
5523 an ordinary label, returns a LABEL_STMT. */
5526 cp_parser_labeled_statement (cp_parser
* parser
)
5529 tree statement
= NULL_TREE
;
5531 /* The next token should be an identifier. */
5532 token
= cp_lexer_peek_token (parser
->lexer
);
5533 if (token
->type
!= CPP_NAME
5534 && token
->type
!= CPP_KEYWORD
)
5536 cp_parser_error (parser
, "expected labeled-statement");
5537 return error_mark_node
;
5540 switch (token
->keyword
)
5546 /* Consume the `case' token. */
5547 cp_lexer_consume_token (parser
->lexer
);
5548 /* Parse the constant-expression. */
5549 expr
= cp_parser_constant_expression (parser
,
5550 /*allow_non_constant=*/false,
5552 /* Create the label. */
5553 statement
= finish_case_label (expr
, NULL_TREE
);
5558 /* Consume the `default' token. */
5559 cp_lexer_consume_token (parser
->lexer
);
5560 /* Create the label. */
5561 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5565 /* Anything else must be an ordinary label. */
5566 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5570 /* Require the `:' token. */
5571 cp_parser_require (parser
, CPP_COLON
, "`:'");
5572 /* Parse the labeled statement. */
5573 cp_parser_statement (parser
);
5575 /* Return the label, in the case of a `case' or `default' label. */
5579 /* Parse an expression-statement.
5581 expression-statement:
5584 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5585 statement consists of nothing more than an `;'. */
5588 cp_parser_expression_statement (cp_parser
* parser
)
5592 /* If the next token is not a `;', then there is an expression to parse. */
5593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5594 statement
= finish_expr_stmt (cp_parser_expression (parser
));
5595 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5599 statement
= NULL_TREE
;
5601 /* Consume the final `;'. */
5602 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5607 /* Parse a compound-statement.
5610 { statement-seq [opt] }
5612 Returns a COMPOUND_STMT representing the statement. */
5615 cp_parser_compound_statement (cp_parser
*parser
)
5619 /* Consume the `{'. */
5620 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
5621 return error_mark_node
;
5622 /* Begin the compound-statement. */
5623 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/0);
5624 /* Parse an (optional) statement-seq. */
5625 cp_parser_statement_seq_opt (parser
);
5626 /* Finish the compound-statement. */
5627 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt
);
5628 /* Consume the `}'. */
5629 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
5631 return compound_stmt
;
5634 /* Parse an (optional) statement-seq.
5638 statement-seq [opt] statement */
5641 cp_parser_statement_seq_opt (cp_parser
* parser
)
5643 /* Scan statements until there aren't any more. */
5646 /* If we're looking at a `}', then we've run out of statements. */
5647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
5648 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
5651 /* Parse the statement. */
5652 cp_parser_statement (parser
);
5656 /* Parse a selection-statement.
5658 selection-statement:
5659 if ( condition ) statement
5660 if ( condition ) statement else statement
5661 switch ( condition ) statement
5663 Returns the new IF_STMT or SWITCH_STMT. */
5666 cp_parser_selection_statement (cp_parser
* parser
)
5671 /* Peek at the next token. */
5672 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
5674 /* See what kind of keyword it is. */
5675 keyword
= token
->keyword
;
5684 /* Look for the `('. */
5685 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5687 cp_parser_skip_to_end_of_statement (parser
);
5688 return error_mark_node
;
5691 /* Begin the selection-statement. */
5692 if (keyword
== RID_IF
)
5693 statement
= begin_if_stmt ();
5695 statement
= begin_switch_stmt ();
5697 /* Parse the condition. */
5698 condition
= cp_parser_condition (parser
);
5699 /* Look for the `)'. */
5700 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5701 cp_parser_skip_to_closing_parenthesis (parser
);
5703 if (keyword
== RID_IF
)
5707 /* Add the condition. */
5708 finish_if_stmt_cond (condition
, statement
);
5710 /* Parse the then-clause. */
5711 then_stmt
= cp_parser_implicitly_scoped_statement (parser
);
5712 finish_then_clause (statement
);
5714 /* If the next token is `else', parse the else-clause. */
5715 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
5720 /* Consume the `else' keyword. */
5721 cp_lexer_consume_token (parser
->lexer
);
5722 /* Parse the else-clause. */
5724 = cp_parser_implicitly_scoped_statement (parser
);
5725 finish_else_clause (statement
);
5728 /* Now we're all done with the if-statement. */
5735 /* Add the condition. */
5736 finish_switch_cond (condition
, statement
);
5738 /* Parse the body of the switch-statement. */
5739 body
= cp_parser_implicitly_scoped_statement (parser
);
5741 /* Now we're all done with the switch-statement. */
5742 finish_switch_stmt (statement
);
5750 cp_parser_error (parser
, "expected selection-statement");
5751 return error_mark_node
;
5755 /* Parse a condition.
5759 type-specifier-seq declarator = assignment-expression
5764 type-specifier-seq declarator asm-specification [opt]
5765 attributes [opt] = assignment-expression
5767 Returns the expression that should be tested. */
5770 cp_parser_condition (cp_parser
* parser
)
5772 tree type_specifiers
;
5773 const char *saved_message
;
5775 /* Try the declaration first. */
5776 cp_parser_parse_tentatively (parser
);
5777 /* New types are not allowed in the type-specifier-seq for a
5779 saved_message
= parser
->type_definition_forbidden_message
;
5780 parser
->type_definition_forbidden_message
5781 = "types may not be defined in conditions";
5782 /* Parse the type-specifier-seq. */
5783 type_specifiers
= cp_parser_type_specifier_seq (parser
);
5784 /* Restore the saved message. */
5785 parser
->type_definition_forbidden_message
= saved_message
;
5786 /* If all is well, we might be looking at a declaration. */
5787 if (!cp_parser_error_occurred (parser
))
5790 tree asm_specification
;
5793 tree initializer
= NULL_TREE
;
5795 /* Parse the declarator. */
5796 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
5797 /*ctor_dtor_or_conv_p=*/NULL
);
5798 /* Parse the attributes. */
5799 attributes
= cp_parser_attributes_opt (parser
);
5800 /* Parse the asm-specification. */
5801 asm_specification
= cp_parser_asm_specification_opt (parser
);
5802 /* If the next token is not an `=', then we might still be
5803 looking at an expression. For example:
5807 looks like a decl-specifier-seq and a declarator -- but then
5808 there is no `=', so this is an expression. */
5809 cp_parser_require (parser
, CPP_EQ
, "`='");
5810 /* If we did see an `=', then we are looking at a declaration
5812 if (cp_parser_parse_definitely (parser
))
5814 /* Create the declaration. */
5815 decl
= start_decl (declarator
, type_specifiers
,
5816 /*initialized_p=*/true,
5817 attributes
, /*prefix_attributes=*/NULL_TREE
);
5818 /* Parse the assignment-expression. */
5819 initializer
= cp_parser_assignment_expression (parser
);
5821 /* Process the initializer. */
5822 cp_finish_decl (decl
,
5825 LOOKUP_ONLYCONVERTING
);
5827 return convert_from_reference (decl
);
5830 /* If we didn't even get past the declarator successfully, we are
5831 definitely not looking at a declaration. */
5833 cp_parser_abort_tentative_parse (parser
);
5835 /* Otherwise, we are looking at an expression. */
5836 return cp_parser_expression (parser
);
5839 /* Parse an iteration-statement.
5841 iteration-statement:
5842 while ( condition ) statement
5843 do statement while ( expression ) ;
5844 for ( for-init-statement condition [opt] ; expression [opt] )
5847 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5850 cp_parser_iteration_statement (cp_parser
* parser
)
5856 /* Peek at the next token. */
5857 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
5859 return error_mark_node
;
5861 /* See what kind of keyword it is. */
5862 keyword
= token
->keyword
;
5869 /* Begin the while-statement. */
5870 statement
= begin_while_stmt ();
5871 /* Look for the `('. */
5872 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5873 /* Parse the condition. */
5874 condition
= cp_parser_condition (parser
);
5875 finish_while_stmt_cond (condition
, statement
);
5876 /* Look for the `)'. */
5877 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5878 /* Parse the dependent statement. */
5879 cp_parser_already_scoped_statement (parser
);
5880 /* We're done with the while-statement. */
5881 finish_while_stmt (statement
);
5889 /* Begin the do-statement. */
5890 statement
= begin_do_stmt ();
5891 /* Parse the body of the do-statement. */
5892 cp_parser_implicitly_scoped_statement (parser
);
5893 finish_do_body (statement
);
5894 /* Look for the `while' keyword. */
5895 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
5896 /* Look for the `('. */
5897 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5898 /* Parse the expression. */
5899 expression
= cp_parser_expression (parser
);
5900 /* We're done with the do-statement. */
5901 finish_do_stmt (expression
, statement
);
5902 /* Look for the `)'. */
5903 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5904 /* Look for the `;'. */
5905 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5911 tree condition
= NULL_TREE
;
5912 tree expression
= NULL_TREE
;
5914 /* Begin the for-statement. */
5915 statement
= begin_for_stmt ();
5916 /* Look for the `('. */
5917 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5918 /* Parse the initialization. */
5919 cp_parser_for_init_statement (parser
);
5920 finish_for_init_stmt (statement
);
5922 /* If there's a condition, process it. */
5923 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5924 condition
= cp_parser_condition (parser
);
5925 finish_for_cond (condition
, statement
);
5926 /* Look for the `;'. */
5927 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
5929 /* If there's an expression, process it. */
5930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5931 expression
= cp_parser_expression (parser
);
5932 finish_for_expr (expression
, statement
);
5933 /* Look for the `)'. */
5934 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`;'");
5936 /* Parse the body of the for-statement. */
5937 cp_parser_already_scoped_statement (parser
);
5939 /* We're done with the for-statement. */
5940 finish_for_stmt (statement
);
5945 cp_parser_error (parser
, "expected iteration-statement");
5946 statement
= error_mark_node
;
5953 /* Parse a for-init-statement.
5956 expression-statement
5957 simple-declaration */
5960 cp_parser_for_init_statement (cp_parser
* parser
)
5962 /* If the next token is a `;', then we have an empty
5963 expression-statement. Grammatically, this is also a
5964 simple-declaration, but an invalid one, because it does not
5965 declare anything. Therefore, if we did not handle this case
5966 specially, we would issue an error message about an invalid
5968 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5970 /* We're going to speculatively look for a declaration, falling back
5971 to an expression, if necessary. */
5972 cp_parser_parse_tentatively (parser
);
5973 /* Parse the declaration. */
5974 cp_parser_simple_declaration (parser
,
5975 /*function_definition_allowed_p=*/false);
5976 /* If the tentative parse failed, then we shall need to look for an
5977 expression-statement. */
5978 if (cp_parser_parse_definitely (parser
))
5982 cp_parser_expression_statement (parser
);
5985 /* Parse a jump-statement.
5990 return expression [opt] ;
5998 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6002 cp_parser_jump_statement (cp_parser
* parser
)
6004 tree statement
= error_mark_node
;
6008 /* Peek at the next token. */
6009 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6011 return error_mark_node
;
6013 /* See what kind of keyword it is. */
6014 keyword
= token
->keyword
;
6018 statement
= finish_break_stmt ();
6019 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6023 statement
= finish_continue_stmt ();
6024 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6031 /* If the next token is a `;', then there is no
6033 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6034 expr
= cp_parser_expression (parser
);
6037 /* Build the return-statement. */
6038 statement
= finish_return_stmt (expr
);
6039 /* Look for the final `;'. */
6040 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6045 /* Create the goto-statement. */
6046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6048 /* Issue a warning about this use of a GNU extension. */
6050 pedwarn ("ISO C++ forbids computed gotos");
6051 /* Consume the '*' token. */
6052 cp_lexer_consume_token (parser
->lexer
);
6053 /* Parse the dependent expression. */
6054 finish_goto_stmt (cp_parser_expression (parser
));
6057 finish_goto_stmt (cp_parser_identifier (parser
));
6058 /* Look for the final `;'. */
6059 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6063 cp_parser_error (parser
, "expected jump-statement");
6070 /* Parse a declaration-statement.
6072 declaration-statement:
6073 block-declaration */
6076 cp_parser_declaration_statement (cp_parser
* parser
)
6078 /* Parse the block-declaration. */
6079 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6081 /* Finish off the statement. */
6085 /* Some dependent statements (like `if (cond) statement'), are
6086 implicitly in their own scope. In other words, if the statement is
6087 a single statement (as opposed to a compound-statement), it is
6088 none-the-less treated as if it were enclosed in braces. Any
6089 declarations appearing in the dependent statement are out of scope
6090 after control passes that point. This function parses a statement,
6091 but ensures that is in its own scope, even if it is not a
6094 Returns the new statement. */
6097 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6101 /* If the token is not a `{', then we must take special action. */
6102 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6104 /* Create a compound-statement. */
6105 statement
= begin_compound_stmt (/*has_no_scope=*/0);
6106 /* Parse the dependent-statement. */
6107 cp_parser_statement (parser
);
6108 /* Finish the dummy compound-statement. */
6109 finish_compound_stmt (/*has_no_scope=*/0, statement
);
6111 /* Otherwise, we simply parse the statement directly. */
6113 statement
= cp_parser_compound_statement (parser
);
6115 /* Return the statement. */
6119 /* For some dependent statements (like `while (cond) statement'), we
6120 have already created a scope. Therefore, even if the dependent
6121 statement is a compound-statement, we do not want to create another
6125 cp_parser_already_scoped_statement (cp_parser
* parser
)
6127 /* If the token is not a `{', then we must take special action. */
6128 if (cp_lexer_next_token_is_not(parser
->lexer
, CPP_OPEN_BRACE
))
6132 /* Create a compound-statement. */
6133 statement
= begin_compound_stmt (/*has_no_scope=*/1);
6134 /* Parse the dependent-statement. */
6135 cp_parser_statement (parser
);
6136 /* Finish the dummy compound-statement. */
6137 finish_compound_stmt (/*has_no_scope=*/1, statement
);
6139 /* Otherwise, we simply parse the statement directly. */
6141 cp_parser_statement (parser
);
6144 /* Declarations [gram.dcl.dcl] */
6146 /* Parse an optional declaration-sequence.
6150 declaration-seq declaration */
6153 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6159 token
= cp_lexer_peek_token (parser
->lexer
);
6161 if (token
->type
== CPP_CLOSE_BRACE
6162 || token
->type
== CPP_EOF
)
6165 if (token
->type
== CPP_SEMICOLON
)
6167 /* A declaration consisting of a single semicolon is
6168 invalid. Allow it unless we're being pedantic. */
6170 pedwarn ("extra `;'");
6171 cp_lexer_consume_token (parser
->lexer
);
6175 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6176 parser to enter or exit implicit `extern "C"' blocks. */
6177 while (pending_lang_change
> 0)
6179 push_lang_context (lang_name_c
);
6180 --pending_lang_change
;
6182 while (pending_lang_change
< 0)
6184 pop_lang_context ();
6185 ++pending_lang_change
;
6188 /* Parse the declaration itself. */
6189 cp_parser_declaration (parser
);
6193 /* Parse a declaration.
6198 template-declaration
6199 explicit-instantiation
6200 explicit-specialization
6201 linkage-specification
6202 namespace-definition
6207 __extension__ declaration */
6210 cp_parser_declaration (cp_parser
* parser
)
6216 /* Check for the `__extension__' keyword. */
6217 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6219 /* Parse the qualified declaration. */
6220 cp_parser_declaration (parser
);
6221 /* Restore the PEDANTIC flag. */
6222 pedantic
= saved_pedantic
;
6227 /* Try to figure out what kind of declaration is present. */
6228 token1
= *cp_lexer_peek_token (parser
->lexer
);
6229 if (token1
.type
!= CPP_EOF
)
6230 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6232 /* If the next token is `extern' and the following token is a string
6233 literal, then we have a linkage specification. */
6234 if (token1
.keyword
== RID_EXTERN
6235 && cp_parser_is_string_literal (&token2
))
6236 cp_parser_linkage_specification (parser
);
6237 /* If the next token is `template', then we have either a template
6238 declaration, an explicit instantiation, or an explicit
6240 else if (token1
.keyword
== RID_TEMPLATE
)
6242 /* `template <>' indicates a template specialization. */
6243 if (token2
.type
== CPP_LESS
6244 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6245 cp_parser_explicit_specialization (parser
);
6246 /* `template <' indicates a template declaration. */
6247 else if (token2
.type
== CPP_LESS
)
6248 cp_parser_template_declaration (parser
, /*member_p=*/false);
6249 /* Anything else must be an explicit instantiation. */
6251 cp_parser_explicit_instantiation (parser
);
6253 /* If the next token is `export', then we have a template
6255 else if (token1
.keyword
== RID_EXPORT
)
6256 cp_parser_template_declaration (parser
, /*member_p=*/false);
6257 /* If the next token is `extern', 'static' or 'inline' and the one
6258 after that is `template', we have a GNU extended explicit
6259 instantiation directive. */
6260 else if (cp_parser_allow_gnu_extensions_p (parser
)
6261 && (token1
.keyword
== RID_EXTERN
6262 || token1
.keyword
== RID_STATIC
6263 || token1
.keyword
== RID_INLINE
)
6264 && token2
.keyword
== RID_TEMPLATE
)
6265 cp_parser_explicit_instantiation (parser
);
6266 /* If the next token is `namespace', check for a named or unnamed
6267 namespace definition. */
6268 else if (token1
.keyword
== RID_NAMESPACE
6269 && (/* A named namespace definition. */
6270 (token2
.type
== CPP_NAME
6271 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6273 /* An unnamed namespace definition. */
6274 || token2
.type
== CPP_OPEN_BRACE
))
6275 cp_parser_namespace_definition (parser
);
6276 /* We must have either a block declaration or a function
6279 /* Try to parse a block-declaration, or a function-definition. */
6280 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6283 /* Parse a block-declaration.
6288 namespace-alias-definition
6295 __extension__ block-declaration
6298 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6299 part of a declaration-statement. */
6302 cp_parser_block_declaration (cp_parser
*parser
,
6308 /* Check for the `__extension__' keyword. */
6309 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6311 /* Parse the qualified declaration. */
6312 cp_parser_block_declaration (parser
, statement_p
);
6313 /* Restore the PEDANTIC flag. */
6314 pedantic
= saved_pedantic
;
6319 /* Peek at the next token to figure out which kind of declaration is
6321 token1
= cp_lexer_peek_token (parser
->lexer
);
6323 /* If the next keyword is `asm', we have an asm-definition. */
6324 if (token1
->keyword
== RID_ASM
)
6327 cp_parser_commit_to_tentative_parse (parser
);
6328 cp_parser_asm_definition (parser
);
6330 /* If the next keyword is `namespace', we have a
6331 namespace-alias-definition. */
6332 else if (token1
->keyword
== RID_NAMESPACE
)
6333 cp_parser_namespace_alias_definition (parser
);
6334 /* If the next keyword is `using', we have either a
6335 using-declaration or a using-directive. */
6336 else if (token1
->keyword
== RID_USING
)
6341 cp_parser_commit_to_tentative_parse (parser
);
6342 /* If the token after `using' is `namespace', then we have a
6344 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6345 if (token2
->keyword
== RID_NAMESPACE
)
6346 cp_parser_using_directive (parser
);
6347 /* Otherwise, it's a using-declaration. */
6349 cp_parser_using_declaration (parser
);
6351 /* If the next keyword is `__label__' we have a label declaration. */
6352 else if (token1
->keyword
== RID_LABEL
)
6355 cp_parser_commit_to_tentative_parse (parser
);
6356 cp_parser_label_declaration (parser
);
6358 /* Anything else must be a simple-declaration. */
6360 cp_parser_simple_declaration (parser
, !statement_p
);
6363 /* Parse a simple-declaration.
6366 decl-specifier-seq [opt] init-declarator-list [opt] ;
6368 init-declarator-list:
6370 init-declarator-list , init-declarator
6372 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6373 function-definition as a simple-declaration. */
6376 cp_parser_simple_declaration (cp_parser
* parser
,
6377 bool function_definition_allowed_p
)
6379 tree decl_specifiers
;
6381 bool declares_class_or_enum
;
6382 bool saw_declarator
;
6384 /* Defer access checks until we know what is being declared; the
6385 checks for names appearing in the decl-specifier-seq should be
6386 done as if we were in the scope of the thing being declared. */
6387 push_deferring_access_checks (dk_deferred
);
6389 /* Parse the decl-specifier-seq. We have to keep track of whether
6390 or not the decl-specifier-seq declares a named class or
6391 enumeration type, since that is the only case in which the
6392 init-declarator-list is allowed to be empty.
6396 In a simple-declaration, the optional init-declarator-list can be
6397 omitted only when declaring a class or enumeration, that is when
6398 the decl-specifier-seq contains either a class-specifier, an
6399 elaborated-type-specifier, or an enum-specifier. */
6401 = cp_parser_decl_specifier_seq (parser
,
6402 CP_PARSER_FLAGS_OPTIONAL
,
6404 &declares_class_or_enum
);
6405 /* We no longer need to defer access checks. */
6406 stop_deferring_access_checks ();
6408 /* If the next two tokens are both identifiers, the code is
6409 erroneous. The usual cause of this situation is code like:
6413 where "T" should name a type -- but does not. */
6414 if (cp_parser_diagnose_invalid_type_name (parser
))
6416 /* If parsing tentatively, we should commit; we really are
6417 looking at a declaration. */
6418 cp_parser_commit_to_tentative_parse (parser
);
6423 /* Keep going until we hit the `;' at the end of the simple
6425 saw_declarator
= false;
6426 while (cp_lexer_next_token_is_not (parser
->lexer
,
6430 bool function_definition_p
;
6432 saw_declarator
= true;
6433 /* Parse the init-declarator. */
6434 cp_parser_init_declarator (parser
, decl_specifiers
, attributes
,
6435 function_definition_allowed_p
,
6437 &function_definition_p
);
6438 /* If an error occurred while parsing tentatively, exit quickly.
6439 (That usually happens when in the body of a function; each
6440 statement is treated as a declaration-statement until proven
6442 if (cp_parser_error_occurred (parser
))
6444 pop_deferring_access_checks ();
6447 /* Handle function definitions specially. */
6448 if (function_definition_p
)
6450 /* If the next token is a `,', then we are probably
6451 processing something like:
6455 which is erroneous. */
6456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6457 error ("mixing declarations and function-definitions is forbidden");
6458 /* Otherwise, we're done with the list of declarators. */
6461 pop_deferring_access_checks ();
6465 /* The next token should be either a `,' or a `;'. */
6466 token
= cp_lexer_peek_token (parser
->lexer
);
6467 /* If it's a `,', there are more declarators to come. */
6468 if (token
->type
== CPP_COMMA
)
6469 cp_lexer_consume_token (parser
->lexer
);
6470 /* If it's a `;', we are done. */
6471 else if (token
->type
== CPP_SEMICOLON
)
6473 /* Anything else is an error. */
6476 cp_parser_error (parser
, "expected `,' or `;'");
6477 /* Skip tokens until we reach the end of the statement. */
6478 cp_parser_skip_to_end_of_statement (parser
);
6479 pop_deferring_access_checks ();
6482 /* After the first time around, a function-definition is not
6483 allowed -- even if it was OK at first. For example:
6488 function_definition_allowed_p
= false;
6491 /* Issue an error message if no declarators are present, and the
6492 decl-specifier-seq does not itself declare a class or
6494 if (!saw_declarator
)
6496 if (cp_parser_declares_only_class_p (parser
))
6497 shadow_tag (decl_specifiers
);
6498 /* Perform any deferred access checks. */
6499 perform_deferred_access_checks ();
6502 pop_deferring_access_checks ();
6504 /* Consume the `;'. */
6505 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6507 /* Mark all the classes that appeared in the decl-specifier-seq as
6508 having received a `;'. */
6509 note_list_got_semicolon (decl_specifiers
);
6512 /* Parse a decl-specifier-seq.
6515 decl-specifier-seq [opt] decl-specifier
6518 storage-class-specifier
6527 decl-specifier-seq [opt] attributes
6529 Returns a TREE_LIST, giving the decl-specifiers in the order they
6530 appear in the source code. The TREE_VALUE of each node is the
6531 decl-specifier. For a keyword (such as `auto' or `friend'), the
6532 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6533 representation of a type-specifier, see cp_parser_type_specifier.
6535 If there are attributes, they will be stored in *ATTRIBUTES,
6536 represented as described above cp_parser_attributes.
6538 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6539 appears, and the entity that will be a friend is not going to be a
6540 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6541 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6542 friendship is granted might not be a class. */
6545 cp_parser_decl_specifier_seq (cp_parser
* parser
,
6546 cp_parser_flags flags
,
6548 bool* declares_class_or_enum
)
6550 tree decl_specs
= NULL_TREE
;
6551 bool friend_p
= false;
6552 bool constructor_possible_p
= !parser
->in_declarator_p
;
6554 /* Assume no class or enumeration type is declared. */
6555 *declares_class_or_enum
= false;
6557 /* Assume there are no attributes. */
6558 *attributes
= NULL_TREE
;
6560 /* Keep reading specifiers until there are no more to read. */
6563 tree decl_spec
= NULL_TREE
;
6567 /* Peek at the next token. */
6568 token
= cp_lexer_peek_token (parser
->lexer
);
6569 /* Handle attributes. */
6570 if (token
->keyword
== RID_ATTRIBUTE
)
6572 /* Parse the attributes. */
6573 decl_spec
= cp_parser_attributes_opt (parser
);
6574 /* Add them to the list. */
6575 *attributes
= chainon (*attributes
, decl_spec
);
6578 /* If the next token is an appropriate keyword, we can simply
6579 add it to the list. */
6580 switch (token
->keyword
)
6586 /* The representation of the specifier is simply the
6587 appropriate TREE_IDENTIFIER node. */
6588 decl_spec
= token
->value
;
6589 /* Consume the token. */
6590 cp_lexer_consume_token (parser
->lexer
);
6593 /* function-specifier:
6600 decl_spec
= cp_parser_function_specifier_opt (parser
);
6606 /* The representation of the specifier is simply the
6607 appropriate TREE_IDENTIFIER node. */
6608 decl_spec
= token
->value
;
6609 /* Consume the token. */
6610 cp_lexer_consume_token (parser
->lexer
);
6611 /* A constructor declarator cannot appear in a typedef. */
6612 constructor_possible_p
= false;
6613 /* The "typedef" keyword can only occur in a declaration; we
6614 may as well commit at this point. */
6615 cp_parser_commit_to_tentative_parse (parser
);
6618 /* storage-class-specifier:
6633 decl_spec
= cp_parser_storage_class_specifier_opt (parser
);
6640 /* Constructors are a special case. The `S' in `S()' is not a
6641 decl-specifier; it is the beginning of the declarator. */
6642 constructor_p
= (!decl_spec
6643 && constructor_possible_p
6644 && cp_parser_constructor_declarator_p (parser
,
6647 /* If we don't have a DECL_SPEC yet, then we must be looking at
6648 a type-specifier. */
6649 if (!decl_spec
&& !constructor_p
)
6651 bool decl_spec_declares_class_or_enum
;
6652 bool is_cv_qualifier
;
6655 = cp_parser_type_specifier (parser
, flags
,
6657 /*is_declaration=*/true,
6658 &decl_spec_declares_class_or_enum
,
6661 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
6663 /* If this type-specifier referenced a user-defined type
6664 (a typedef, class-name, etc.), then we can't allow any
6665 more such type-specifiers henceforth.
6669 The longest sequence of decl-specifiers that could
6670 possibly be a type name is taken as the
6671 decl-specifier-seq of a declaration. The sequence shall
6672 be self-consistent as described below.
6676 As a general rule, at most one type-specifier is allowed
6677 in the complete decl-specifier-seq of a declaration. The
6678 only exceptions are the following:
6680 -- const or volatile can be combined with any other
6683 -- signed or unsigned can be combined with char, long,
6691 void g (const int Pc);
6693 Here, Pc is *not* part of the decl-specifier seq; it's
6694 the declarator. Therefore, once we see a type-specifier
6695 (other than a cv-qualifier), we forbid any additional
6696 user-defined types. We *do* still allow things like `int
6697 int' to be considered a decl-specifier-seq, and issue the
6698 error message later. */
6699 if (decl_spec
&& !is_cv_qualifier
)
6700 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
6701 /* A constructor declarator cannot follow a type-specifier. */
6703 constructor_possible_p
= false;
6706 /* If we still do not have a DECL_SPEC, then there are no more
6710 /* Issue an error message, unless the entire construct was
6712 if (!(flags
& CP_PARSER_FLAGS_OPTIONAL
))
6714 cp_parser_error (parser
, "expected decl specifier");
6715 return error_mark_node
;
6721 /* Add the DECL_SPEC to the list of specifiers. */
6722 decl_specs
= tree_cons (NULL_TREE
, decl_spec
, decl_specs
);
6724 /* After we see one decl-specifier, further decl-specifiers are
6726 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
6729 /* We have built up the DECL_SPECS in reverse order. Return them in
6730 the correct order. */
6731 return nreverse (decl_specs
);
6734 /* Parse an (optional) storage-class-specifier.
6736 storage-class-specifier:
6745 storage-class-specifier:
6748 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6751 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
6753 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6761 /* Consume the token. */
6762 return cp_lexer_consume_token (parser
->lexer
)->value
;
6769 /* Parse an (optional) function-specifier.
6776 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6779 cp_parser_function_specifier_opt (cp_parser
* parser
)
6781 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
6786 /* Consume the token. */
6787 return cp_lexer_consume_token (parser
->lexer
)->value
;
6794 /* Parse a linkage-specification.
6796 linkage-specification:
6797 extern string-literal { declaration-seq [opt] }
6798 extern string-literal declaration */
6801 cp_parser_linkage_specification (cp_parser
* parser
)
6806 /* Look for the `extern' keyword. */
6807 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
6809 /* Peek at the next token. */
6810 token
= cp_lexer_peek_token (parser
->lexer
);
6811 /* If it's not a string-literal, then there's a problem. */
6812 if (!cp_parser_is_string_literal (token
))
6814 cp_parser_error (parser
, "expected language-name");
6817 /* Consume the token. */
6818 cp_lexer_consume_token (parser
->lexer
);
6820 /* Transform the literal into an identifier. If the literal is a
6821 wide-character string, or contains embedded NULs, then we can't
6822 handle it as the user wants. */
6823 if (token
->type
== CPP_WSTRING
6824 || (strlen (TREE_STRING_POINTER (token
->value
))
6825 != (size_t) (TREE_STRING_LENGTH (token
->value
) - 1)))
6827 cp_parser_error (parser
, "invalid linkage-specification");
6828 /* Assume C++ linkage. */
6829 linkage
= get_identifier ("c++");
6831 /* If it's a simple string constant, things are easier. */
6833 linkage
= get_identifier (TREE_STRING_POINTER (token
->value
));
6835 /* We're now using the new linkage. */
6836 push_lang_context (linkage
);
6838 /* If the next token is a `{', then we're using the first
6840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6842 /* Consume the `{' token. */
6843 cp_lexer_consume_token (parser
->lexer
);
6844 /* Parse the declarations. */
6845 cp_parser_declaration_seq_opt (parser
);
6846 /* Look for the closing `}'. */
6847 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6849 /* Otherwise, there's just one declaration. */
6852 bool saved_in_unbraced_linkage_specification_p
;
6854 saved_in_unbraced_linkage_specification_p
6855 = parser
->in_unbraced_linkage_specification_p
;
6856 parser
->in_unbraced_linkage_specification_p
= true;
6857 have_extern_spec
= true;
6858 cp_parser_declaration (parser
);
6859 have_extern_spec
= false;
6860 parser
->in_unbraced_linkage_specification_p
6861 = saved_in_unbraced_linkage_specification_p
;
6864 /* We're done with the linkage-specification. */
6865 pop_lang_context ();
6868 /* Special member functions [gram.special] */
6870 /* Parse a conversion-function-id.
6872 conversion-function-id:
6873 operator conversion-type-id
6875 Returns an IDENTIFIER_NODE representing the operator. */
6878 cp_parser_conversion_function_id (cp_parser
* parser
)
6882 tree saved_qualifying_scope
;
6883 tree saved_object_scope
;
6885 /* Look for the `operator' token. */
6886 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
6887 return error_mark_node
;
6888 /* When we parse the conversion-type-id, the current scope will be
6889 reset. However, we need that information in able to look up the
6890 conversion function later, so we save it here. */
6891 saved_scope
= parser
->scope
;
6892 saved_qualifying_scope
= parser
->qualifying_scope
;
6893 saved_object_scope
= parser
->object_scope
;
6894 /* We must enter the scope of the class so that the names of
6895 entities declared within the class are available in the
6896 conversion-type-id. For example, consider:
6903 S::operator I() { ... }
6905 In order to see that `I' is a type-name in the definition, we
6906 must be in the scope of `S'. */
6908 push_scope (saved_scope
);
6909 /* Parse the conversion-type-id. */
6910 type
= cp_parser_conversion_type_id (parser
);
6911 /* Leave the scope of the class, if any. */
6913 pop_scope (saved_scope
);
6914 /* Restore the saved scope. */
6915 parser
->scope
= saved_scope
;
6916 parser
->qualifying_scope
= saved_qualifying_scope
;
6917 parser
->object_scope
= saved_object_scope
;
6918 /* If the TYPE is invalid, indicate failure. */
6919 if (type
== error_mark_node
)
6920 return error_mark_node
;
6921 return mangle_conv_op_name_for_type (type
);
6924 /* Parse a conversion-type-id:
6927 type-specifier-seq conversion-declarator [opt]
6929 Returns the TYPE specified. */
6932 cp_parser_conversion_type_id (cp_parser
* parser
)
6935 tree type_specifiers
;
6938 /* Parse the attributes. */
6939 attributes
= cp_parser_attributes_opt (parser
);
6940 /* Parse the type-specifiers. */
6941 type_specifiers
= cp_parser_type_specifier_seq (parser
);
6942 /* If that didn't work, stop. */
6943 if (type_specifiers
== error_mark_node
)
6944 return error_mark_node
;
6945 /* Parse the conversion-declarator. */
6946 declarator
= cp_parser_conversion_declarator_opt (parser
);
6948 return grokdeclarator (declarator
, type_specifiers
, TYPENAME
,
6949 /*initialized=*/0, &attributes
);
6952 /* Parse an (optional) conversion-declarator.
6954 conversion-declarator:
6955 ptr-operator conversion-declarator [opt]
6957 Returns a representation of the declarator. See
6958 cp_parser_declarator for details. */
6961 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
6963 enum tree_code code
;
6965 tree cv_qualifier_seq
;
6967 /* We don't know if there's a ptr-operator next, or not. */
6968 cp_parser_parse_tentatively (parser
);
6969 /* Try the ptr-operator. */
6970 code
= cp_parser_ptr_operator (parser
, &class_type
,
6972 /* If it worked, look for more conversion-declarators. */
6973 if (cp_parser_parse_definitely (parser
))
6977 /* Parse another optional declarator. */
6978 declarator
= cp_parser_conversion_declarator_opt (parser
);
6980 /* Create the representation of the declarator. */
6981 if (code
== INDIRECT_REF
)
6982 declarator
= make_pointer_declarator (cv_qualifier_seq
,
6985 declarator
= make_reference_declarator (cv_qualifier_seq
,
6988 /* Handle the pointer-to-member case. */
6990 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
6998 /* Parse an (optional) ctor-initializer.
7001 : mem-initializer-list
7003 Returns TRUE iff the ctor-initializer was actually present. */
7006 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7008 /* If the next token is not a `:', then there is no
7009 ctor-initializer. */
7010 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7012 /* Do default initialization of any bases and members. */
7013 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7014 finish_mem_initializers (NULL_TREE
);
7019 /* Consume the `:' token. */
7020 cp_lexer_consume_token (parser
->lexer
);
7021 /* And the mem-initializer-list. */
7022 cp_parser_mem_initializer_list (parser
);
7027 /* Parse a mem-initializer-list.
7029 mem-initializer-list:
7031 mem-initializer , mem-initializer-list */
7034 cp_parser_mem_initializer_list (cp_parser
* parser
)
7036 tree mem_initializer_list
= NULL_TREE
;
7038 /* Let the semantic analysis code know that we are starting the
7039 mem-initializer-list. */
7040 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7041 error ("only constructors take base initializers");
7043 /* Loop through the list. */
7046 tree mem_initializer
;
7048 /* Parse the mem-initializer. */
7049 mem_initializer
= cp_parser_mem_initializer (parser
);
7050 /* Add it to the list, unless it was erroneous. */
7051 if (mem_initializer
)
7053 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7054 mem_initializer_list
= mem_initializer
;
7056 /* If the next token is not a `,', we're done. */
7057 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7059 /* Consume the `,' token. */
7060 cp_lexer_consume_token (parser
->lexer
);
7063 /* Perform semantic analysis. */
7064 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7065 finish_mem_initializers (mem_initializer_list
);
7068 /* Parse a mem-initializer.
7071 mem-initializer-id ( expression-list [opt] )
7076 ( expression-list [opt] )
7078 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7079 class) or FIELD_DECL (for a non-static data member) to initialize;
7080 the TREE_VALUE is the expression-list. */
7083 cp_parser_mem_initializer (cp_parser
* parser
)
7085 tree mem_initializer_id
;
7086 tree expression_list
;
7089 /* Find out what is being initialized. */
7090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7092 pedwarn ("anachronistic old-style base class initializer");
7093 mem_initializer_id
= NULL_TREE
;
7096 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7097 member
= expand_member_init (mem_initializer_id
);
7098 if (member
&& !DECL_P (member
))
7099 in_base_initializer
= 1;
7101 /* Look for the opening `('. */
7102 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7103 /* Parse the expression-list. */
7104 if (cp_lexer_next_token_is_not (parser
->lexer
,
7106 expression_list
= cp_parser_expression_list (parser
);
7108 expression_list
= void_type_node
;
7109 /* Look for the closing `)'. */
7110 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7112 in_base_initializer
= 0;
7114 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7117 /* Parse a mem-initializer-id.
7120 :: [opt] nested-name-specifier [opt] class-name
7123 Returns a TYPE indicating the class to be initializer for the first
7124 production. Returns an IDENTIFIER_NODE indicating the data member
7125 to be initialized for the second production. */
7128 cp_parser_mem_initializer_id (cp_parser
* parser
)
7130 bool global_scope_p
;
7131 bool nested_name_specifier_p
;
7134 /* Look for the optional `::' operator. */
7136 = (cp_parser_global_scope_opt (parser
,
7137 /*current_scope_valid_p=*/false)
7139 /* Look for the optional nested-name-specifier. The simplest way to
7144 The keyword `typename' is not permitted in a base-specifier or
7145 mem-initializer; in these contexts a qualified name that
7146 depends on a template-parameter is implicitly assumed to be a
7149 is to assume that we have seen the `typename' keyword at this
7151 nested_name_specifier_p
7152 = (cp_parser_nested_name_specifier_opt (parser
,
7153 /*typename_keyword_p=*/true,
7154 /*check_dependency_p=*/true,
7157 /* If there is a `::' operator or a nested-name-specifier, then we
7158 are definitely looking for a class-name. */
7159 if (global_scope_p
|| nested_name_specifier_p
)
7160 return cp_parser_class_name (parser
,
7161 /*typename_keyword_p=*/true,
7162 /*template_keyword_p=*/false,
7164 /*check_dependency_p=*/true,
7165 /*class_head_p=*/false);
7166 /* Otherwise, we could also be looking for an ordinary identifier. */
7167 cp_parser_parse_tentatively (parser
);
7168 /* Try a class-name. */
7169 id
= cp_parser_class_name (parser
,
7170 /*typename_keyword_p=*/true,
7171 /*template_keyword_p=*/false,
7173 /*check_dependency_p=*/true,
7174 /*class_head_p=*/false);
7175 /* If we found one, we're done. */
7176 if (cp_parser_parse_definitely (parser
))
7178 /* Otherwise, look for an ordinary identifier. */
7179 return cp_parser_identifier (parser
);
7182 /* Overloading [gram.over] */
7184 /* Parse an operator-function-id.
7186 operator-function-id:
7189 Returns an IDENTIFIER_NODE for the operator which is a
7190 human-readable spelling of the identifier, e.g., `operator +'. */
7193 cp_parser_operator_function_id (cp_parser
* parser
)
7195 /* Look for the `operator' keyword. */
7196 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7197 return error_mark_node
;
7198 /* And then the name of the operator itself. */
7199 return cp_parser_operator (parser
);
7202 /* Parse an operator.
7205 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7206 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7207 || ++ -- , ->* -> () []
7214 Returns an IDENTIFIER_NODE for the operator which is a
7215 human-readable spelling of the identifier, e.g., `operator +'. */
7218 cp_parser_operator (cp_parser
* parser
)
7220 tree id
= NULL_TREE
;
7223 /* Peek at the next token. */
7224 token
= cp_lexer_peek_token (parser
->lexer
);
7225 /* Figure out which operator we have. */
7226 switch (token
->type
)
7232 /* The keyword should be either `new' or `delete'. */
7233 if (token
->keyword
== RID_NEW
)
7235 else if (token
->keyword
== RID_DELETE
)
7240 /* Consume the `new' or `delete' token. */
7241 cp_lexer_consume_token (parser
->lexer
);
7243 /* Peek at the next token. */
7244 token
= cp_lexer_peek_token (parser
->lexer
);
7245 /* If it's a `[' token then this is the array variant of the
7247 if (token
->type
== CPP_OPEN_SQUARE
)
7249 /* Consume the `[' token. */
7250 cp_lexer_consume_token (parser
->lexer
);
7251 /* Look for the `]' token. */
7252 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7253 id
= ansi_opname (op
== NEW_EXPR
7254 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7256 /* Otherwise, we have the non-array variant. */
7258 id
= ansi_opname (op
);
7264 id
= ansi_opname (PLUS_EXPR
);
7268 id
= ansi_opname (MINUS_EXPR
);
7272 id
= ansi_opname (MULT_EXPR
);
7276 id
= ansi_opname (TRUNC_DIV_EXPR
);
7280 id
= ansi_opname (TRUNC_MOD_EXPR
);
7284 id
= ansi_opname (BIT_XOR_EXPR
);
7288 id
= ansi_opname (BIT_AND_EXPR
);
7292 id
= ansi_opname (BIT_IOR_EXPR
);
7296 id
= ansi_opname (BIT_NOT_EXPR
);
7300 id
= ansi_opname (TRUTH_NOT_EXPR
);
7304 id
= ansi_assopname (NOP_EXPR
);
7308 id
= ansi_opname (LT_EXPR
);
7312 id
= ansi_opname (GT_EXPR
);
7316 id
= ansi_assopname (PLUS_EXPR
);
7320 id
= ansi_assopname (MINUS_EXPR
);
7324 id
= ansi_assopname (MULT_EXPR
);
7328 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7332 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7336 id
= ansi_assopname (BIT_XOR_EXPR
);
7340 id
= ansi_assopname (BIT_AND_EXPR
);
7344 id
= ansi_assopname (BIT_IOR_EXPR
);
7348 id
= ansi_opname (LSHIFT_EXPR
);
7352 id
= ansi_opname (RSHIFT_EXPR
);
7356 id
= ansi_assopname (LSHIFT_EXPR
);
7360 id
= ansi_assopname (RSHIFT_EXPR
);
7364 id
= ansi_opname (EQ_EXPR
);
7368 id
= ansi_opname (NE_EXPR
);
7372 id
= ansi_opname (LE_EXPR
);
7375 case CPP_GREATER_EQ
:
7376 id
= ansi_opname (GE_EXPR
);
7380 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7384 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7388 id
= ansi_opname (POSTINCREMENT_EXPR
);
7391 case CPP_MINUS_MINUS
:
7392 id
= ansi_opname (PREDECREMENT_EXPR
);
7396 id
= ansi_opname (COMPOUND_EXPR
);
7399 case CPP_DEREF_STAR
:
7400 id
= ansi_opname (MEMBER_REF
);
7404 id
= ansi_opname (COMPONENT_REF
);
7407 case CPP_OPEN_PAREN
:
7408 /* Consume the `('. */
7409 cp_lexer_consume_token (parser
->lexer
);
7410 /* Look for the matching `)'. */
7411 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7412 return ansi_opname (CALL_EXPR
);
7414 case CPP_OPEN_SQUARE
:
7415 /* Consume the `['. */
7416 cp_lexer_consume_token (parser
->lexer
);
7417 /* Look for the matching `]'. */
7418 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7419 return ansi_opname (ARRAY_REF
);
7423 id
= ansi_opname (MIN_EXPR
);
7427 id
= ansi_opname (MAX_EXPR
);
7431 id
= ansi_assopname (MIN_EXPR
);
7435 id
= ansi_assopname (MAX_EXPR
);
7439 /* Anything else is an error. */
7443 /* If we have selected an identifier, we need to consume the
7446 cp_lexer_consume_token (parser
->lexer
);
7447 /* Otherwise, no valid operator name was present. */
7450 cp_parser_error (parser
, "expected operator");
7451 id
= error_mark_node
;
7457 /* Parse a template-declaration.
7459 template-declaration:
7460 export [opt] template < template-parameter-list > declaration
7462 If MEMBER_P is TRUE, this template-declaration occurs within a
7465 The grammar rule given by the standard isn't correct. What
7468 template-declaration:
7469 export [opt] template-parameter-list-seq
7470 decl-specifier-seq [opt] init-declarator [opt] ;
7471 export [opt] template-parameter-list-seq
7474 template-parameter-list-seq:
7475 template-parameter-list-seq [opt]
7476 template < template-parameter-list > */
7479 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7481 /* Check for `export'. */
7482 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7484 /* Consume the `export' token. */
7485 cp_lexer_consume_token (parser
->lexer
);
7486 /* Warn that we do not support `export'. */
7487 warning ("keyword `export' not implemented, and will be ignored");
7490 cp_parser_template_declaration_after_export (parser
, member_p
);
7493 /* Parse a template-parameter-list.
7495 template-parameter-list:
7497 template-parameter-list , template-parameter
7499 Returns a TREE_LIST. Each node represents a template parameter.
7500 The nodes are connected via their TREE_CHAINs. */
7503 cp_parser_template_parameter_list (cp_parser
* parser
)
7505 tree parameter_list
= NULL_TREE
;
7512 /* Parse the template-parameter. */
7513 parameter
= cp_parser_template_parameter (parser
);
7514 /* Add it to the list. */
7515 parameter_list
= process_template_parm (parameter_list
,
7518 /* Peek at the next token. */
7519 token
= cp_lexer_peek_token (parser
->lexer
);
7520 /* If it's not a `,', we're done. */
7521 if (token
->type
!= CPP_COMMA
)
7523 /* Otherwise, consume the `,' token. */
7524 cp_lexer_consume_token (parser
->lexer
);
7527 return parameter_list
;
7530 /* Parse a template-parameter.
7534 parameter-declaration
7536 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7537 TREE_PURPOSE is the default value, if any. */
7540 cp_parser_template_parameter (cp_parser
* parser
)
7544 /* Peek at the next token. */
7545 token
= cp_lexer_peek_token (parser
->lexer
);
7546 /* If it is `class' or `template', we have a type-parameter. */
7547 if (token
->keyword
== RID_TEMPLATE
)
7548 return cp_parser_type_parameter (parser
);
7549 /* If it is `class' or `typename' we do not know yet whether it is a
7550 type parameter or a non-type parameter. Consider:
7552 template <typename T, typename T::X X> ...
7556 template <class C, class D*> ...
7558 Here, the first parameter is a type parameter, and the second is
7559 a non-type parameter. We can tell by looking at the token after
7560 the identifier -- if it is a `,', `=', or `>' then we have a type
7562 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
7564 /* Peek at the token after `class' or `typename'. */
7565 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7566 /* If it's an identifier, skip it. */
7567 if (token
->type
== CPP_NAME
)
7568 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
7569 /* Now, see if the token looks like the end of a template
7571 if (token
->type
== CPP_COMMA
7572 || token
->type
== CPP_EQ
7573 || token
->type
== CPP_GREATER
)
7574 return cp_parser_type_parameter (parser
);
7577 /* Otherwise, it is a non-type parameter.
7581 When parsing a default template-argument for a non-type
7582 template-parameter, the first non-nested `>' is taken as the end
7583 of the template parameter-list rather than a greater-than
7586 cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true);
7589 /* Parse a type-parameter.
7592 class identifier [opt]
7593 class identifier [opt] = type-id
7594 typename identifier [opt]
7595 typename identifier [opt] = type-id
7596 template < template-parameter-list > class identifier [opt]
7597 template < template-parameter-list > class identifier [opt]
7600 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7601 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7602 the declaration of the parameter. */
7605 cp_parser_type_parameter (cp_parser
* parser
)
7610 /* Look for a keyword to tell us what kind of parameter this is. */
7611 token
= cp_parser_require (parser
, CPP_KEYWORD
,
7612 "`class', `typename', or `template'");
7614 return error_mark_node
;
7616 switch (token
->keyword
)
7622 tree default_argument
;
7624 /* If the next token is an identifier, then it names the
7626 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
7627 identifier
= cp_parser_identifier (parser
);
7629 identifier
= NULL_TREE
;
7631 /* Create the parameter. */
7632 parameter
= finish_template_type_parm (class_type_node
, identifier
);
7634 /* If the next token is an `=', we have a default argument. */
7635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7637 /* Consume the `=' token. */
7638 cp_lexer_consume_token (parser
->lexer
);
7639 /* Parse the default-argument. */
7640 default_argument
= cp_parser_type_id (parser
);
7643 default_argument
= NULL_TREE
;
7645 /* Create the combined representation of the parameter and the
7646 default argument. */
7647 parameter
= build_tree_list (default_argument
,
7654 tree parameter_list
;
7656 tree default_argument
;
7658 /* Look for the `<'. */
7659 cp_parser_require (parser
, CPP_LESS
, "`<'");
7660 /* Parse the template-parameter-list. */
7661 begin_template_parm_list ();
7663 = cp_parser_template_parameter_list (parser
);
7664 parameter_list
= end_template_parm_list (parameter_list
);
7665 /* Look for the `>'. */
7666 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7667 /* Look for the `class' keyword. */
7668 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
7669 /* If the next token is an `=', then there is a
7670 default-argument. If the next token is a `>', we are at
7671 the end of the parameter-list. If the next token is a `,',
7672 then we are at the end of this parameter. */
7673 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
7674 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
7675 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7676 identifier
= cp_parser_identifier (parser
);
7678 identifier
= NULL_TREE
;
7679 /* Create the template parameter. */
7680 parameter
= finish_template_template_parm (class_type_node
,
7683 /* If the next token is an `=', then there is a
7684 default-argument. */
7685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7687 /* Consume the `='. */
7688 cp_lexer_consume_token (parser
->lexer
);
7689 /* Parse the id-expression. */
7691 = cp_parser_id_expression (parser
,
7692 /*template_keyword_p=*/false,
7693 /*check_dependency_p=*/true,
7694 /*template_p=*/NULL
);
7695 /* Look up the name. */
7697 = cp_parser_lookup_name_simple (parser
, default_argument
);
7698 /* See if the default argument is valid. */
7700 = check_template_template_default_arg (default_argument
);
7703 default_argument
= NULL_TREE
;
7705 /* Create the combined representation of the parameter and the
7706 default argument. */
7707 parameter
= build_tree_list (default_argument
,
7713 /* Anything else is an error. */
7714 cp_parser_error (parser
,
7715 "expected `class', `typename', or `template'");
7716 parameter
= error_mark_node
;
7722 /* Parse a template-id.
7725 template-name < template-argument-list [opt] >
7727 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7728 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7729 returned. Otherwise, if the template-name names a function, or set
7730 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7731 names a class, returns a TYPE_DECL for the specialization.
7733 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7734 uninstantiated templates. */
7737 cp_parser_template_id (cp_parser
*parser
,
7738 bool template_keyword_p
,
7739 bool check_dependency_p
)
7744 tree saved_qualifying_scope
;
7745 tree saved_object_scope
;
7747 bool saved_greater_than_is_operator_p
;
7748 ptrdiff_t start_of_id
;
7749 tree access_check
= NULL_TREE
;
7750 cp_token
*next_token
;
7752 /* If the next token corresponds to a template-id, there is no need
7754 next_token
= cp_lexer_peek_token (parser
->lexer
);
7755 if (next_token
->type
== CPP_TEMPLATE_ID
)
7760 /* Get the stored value. */
7761 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
7762 /* Perform any access checks that were deferred. */
7763 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
7764 perform_or_defer_access_check (TREE_PURPOSE (check
),
7765 TREE_VALUE (check
));
7766 /* Return the stored value. */
7767 return TREE_VALUE (value
);
7770 /* Avoid performing name lookup if there is no possibility of
7771 finding a template-id. */
7772 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
7773 || (next_token
->type
== CPP_NAME
7774 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
))
7776 cp_parser_error (parser
, "expected template-id");
7777 return error_mark_node
;
7780 /* Remember where the template-id starts. */
7781 if (cp_parser_parsing_tentatively (parser
)
7782 && !cp_parser_committed_to_tentative_parse (parser
))
7784 next_token
= cp_lexer_peek_token (parser
->lexer
);
7785 start_of_id
= cp_lexer_token_difference (parser
->lexer
,
7786 parser
->lexer
->first_token
,
7792 push_deferring_access_checks (dk_deferred
);
7794 /* Parse the template-name. */
7795 template = cp_parser_template_name (parser
, template_keyword_p
,
7796 check_dependency_p
);
7797 if (template == error_mark_node
)
7799 pop_deferring_access_checks ();
7800 return error_mark_node
;
7803 /* Look for the `<' that starts the template-argument-list. */
7804 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
7806 pop_deferring_access_checks ();
7807 return error_mark_node
;
7812 When parsing a template-id, the first non-nested `>' is taken as
7813 the end of the template-argument-list rather than a greater-than
7815 saved_greater_than_is_operator_p
7816 = parser
->greater_than_is_operator_p
;
7817 parser
->greater_than_is_operator_p
= false;
7818 /* Parsing the argument list may modify SCOPE, so we save it
7820 saved_scope
= parser
->scope
;
7821 saved_qualifying_scope
= parser
->qualifying_scope
;
7822 saved_object_scope
= parser
->object_scope
;
7823 /* Parse the template-argument-list itself. */
7824 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
7825 arguments
= NULL_TREE
;
7827 arguments
= cp_parser_template_argument_list (parser
);
7828 /* Look for the `>' that ends the template-argument-list. */
7829 cp_parser_require (parser
, CPP_GREATER
, "`>'");
7830 /* The `>' token might be a greater-than operator again now. */
7831 parser
->greater_than_is_operator_p
7832 = saved_greater_than_is_operator_p
;
7833 /* Restore the SAVED_SCOPE. */
7834 parser
->scope
= saved_scope
;
7835 parser
->qualifying_scope
= saved_qualifying_scope
;
7836 parser
->object_scope
= saved_object_scope
;
7838 /* Build a representation of the specialization. */
7839 if (TREE_CODE (template) == IDENTIFIER_NODE
)
7840 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
7841 else if (DECL_CLASS_TEMPLATE_P (template)
7842 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7844 = finish_template_type (template, arguments
,
7845 cp_lexer_next_token_is (parser
->lexer
,
7849 /* If it's not a class-template or a template-template, it should be
7850 a function-template. */
7851 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7852 || TREE_CODE (template) == OVERLOAD
7853 || BASELINK_P (template)),
7856 template_id
= lookup_template_function (template, arguments
);
7859 /* Retrieve any deferred checks. Do not pop this access checks yet
7860 so the memory will not be reclaimed during token replacing below. */
7861 access_check
= get_deferred_access_checks ();
7863 /* If parsing tentatively, replace the sequence of tokens that makes
7864 up the template-id with a CPP_TEMPLATE_ID token. That way,
7865 should we re-parse the token stream, we will not have to repeat
7866 the effort required to do the parse, nor will we issue duplicate
7867 error messages about problems during instantiation of the
7869 if (start_of_id
>= 0)
7873 /* Find the token that corresponds to the start of the
7875 token
= cp_lexer_advance_token (parser
->lexer
,
7876 parser
->lexer
->first_token
,
7879 /* Reset the contents of the START_OF_ID token. */
7880 token
->type
= CPP_TEMPLATE_ID
;
7881 token
->value
= build_tree_list (access_check
, template_id
);
7882 token
->keyword
= RID_MAX
;
7883 /* Purge all subsequent tokens. */
7884 cp_lexer_purge_tokens_after (parser
->lexer
, token
);
7887 pop_deferring_access_checks ();
7891 /* Parse a template-name.
7896 The standard should actually say:
7900 operator-function-id
7901 conversion-function-id
7903 A defect report has been filed about this issue.
7905 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7906 `template' keyword, in a construction like:
7910 In that case `f' is taken to be a template-name, even though there
7911 is no way of knowing for sure.
7913 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7914 name refers to a set of overloaded functions, at least one of which
7915 is a template, or an IDENTIFIER_NODE with the name of the template,
7916 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7917 names are looked up inside uninstantiated templates. */
7920 cp_parser_template_name (cp_parser
* parser
,
7921 bool template_keyword_p
,
7922 bool check_dependency_p
)
7928 /* If the next token is `operator', then we have either an
7929 operator-function-id or a conversion-function-id. */
7930 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
7932 /* We don't know whether we're looking at an
7933 operator-function-id or a conversion-function-id. */
7934 cp_parser_parse_tentatively (parser
);
7935 /* Try an operator-function-id. */
7936 identifier
= cp_parser_operator_function_id (parser
);
7937 /* If that didn't work, try a conversion-function-id. */
7938 if (!cp_parser_parse_definitely (parser
))
7939 identifier
= cp_parser_conversion_function_id (parser
);
7941 /* Look for the identifier. */
7943 identifier
= cp_parser_identifier (parser
);
7945 /* If we didn't find an identifier, we don't have a template-id. */
7946 if (identifier
== error_mark_node
)
7947 return error_mark_node
;
7949 /* If the name immediately followed the `template' keyword, then it
7950 is a template-name. However, if the next token is not `<', then
7951 we do not treat it as a template-name, since it is not being used
7952 as part of a template-id. This enables us to handle constructs
7955 template <typename T> struct S { S(); };
7956 template <typename T> S<T>::S();
7958 correctly. We would treat `S' as a template -- if it were `S<T>'
7959 -- but we do not if there is no `<'. */
7960 if (template_keyword_p
&& processing_template_decl
7961 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
7964 /* Look up the name. */
7965 decl
= cp_parser_lookup_name (parser
, identifier
,
7967 /*is_namespace=*/false,
7968 check_dependency_p
);
7969 decl
= maybe_get_template_decl_from_type_decl (decl
);
7971 /* If DECL is a template, then the name was a template-name. */
7972 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7976 /* The standard does not explicitly indicate whether a name that
7977 names a set of overloaded declarations, some of which are
7978 templates, is a template-name. However, such a name should
7979 be a template-name; otherwise, there is no way to form a
7980 template-id for the overloaded templates. */
7981 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
7982 if (TREE_CODE (fns
) == OVERLOAD
)
7986 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
7987 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
7992 /* Otherwise, the name does not name a template. */
7993 cp_parser_error (parser
, "expected template-name");
7994 return error_mark_node
;
7998 /* If DECL is dependent, and refers to a function, then just return
7999 its name; we will look it up again during template instantiation. */
8000 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8002 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8003 if (TYPE_P (scope
) && dependent_type_p (scope
))
8010 /* Parse a template-argument-list.
8012 template-argument-list:
8014 template-argument-list , template-argument
8016 Returns a TREE_LIST representing the arguments, in the order they
8017 appeared. The TREE_VALUE of each node is a representation of the
8021 cp_parser_template_argument_list (cp_parser
* parser
)
8023 tree arguments
= NULL_TREE
;
8029 /* Parse the template-argument. */
8030 argument
= cp_parser_template_argument (parser
);
8031 /* Add it to the list. */
8032 arguments
= tree_cons (NULL_TREE
, argument
, arguments
);
8033 /* If it is not a `,', then there are no more arguments. */
8034 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8036 /* Otherwise, consume the ','. */
8037 cp_lexer_consume_token (parser
->lexer
);
8040 /* We built up the arguments in reverse order. */
8041 return nreverse (arguments
);
8044 /* Parse a template-argument.
8047 assignment-expression
8051 The representation is that of an assignment-expression, type-id, or
8052 id-expression -- except that the qualified id-expression is
8053 evaluated, so that the value returned is either a DECL or an
8057 cp_parser_template_argument (cp_parser
* parser
)
8062 /* There's really no way to know what we're looking at, so we just
8063 try each alternative in order.
8067 In a template-argument, an ambiguity between a type-id and an
8068 expression is resolved to a type-id, regardless of the form of
8069 the corresponding template-parameter.
8071 Therefore, we try a type-id first. */
8072 cp_parser_parse_tentatively (parser
);
8073 argument
= cp_parser_type_id (parser
);
8074 /* If the next token isn't a `,' or a `>', then this argument wasn't
8076 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
8077 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
))
8078 cp_parser_error (parser
, "expected template-argument");
8079 /* If that worked, we're done. */
8080 if (cp_parser_parse_definitely (parser
))
8082 /* We're still not sure what the argument will be. */
8083 cp_parser_parse_tentatively (parser
);
8084 /* Try a template. */
8085 argument
= cp_parser_id_expression (parser
,
8086 /*template_keyword_p=*/false,
8087 /*check_dependency_p=*/true,
8089 /* If the next token isn't a `,' or a `>', then this argument wasn't
8091 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
8092 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
))
8093 cp_parser_error (parser
, "expected template-argument");
8094 if (!cp_parser_error_occurred (parser
))
8096 /* Figure out what is being referred to. */
8097 argument
= cp_parser_lookup_name_simple (parser
, argument
);
8099 argument
= make_unbound_class_template (TREE_OPERAND (argument
, 0),
8100 TREE_OPERAND (argument
, 1),
8102 else if (TREE_CODE (argument
) != TEMPLATE_DECL
)
8103 cp_parser_error (parser
, "expected template-name");
8105 if (cp_parser_parse_definitely (parser
))
8107 /* It must be an assignment-expression. */
8108 return cp_parser_assignment_expression (parser
);
8111 /* Parse an explicit-instantiation.
8113 explicit-instantiation:
8114 template declaration
8116 Although the standard says `declaration', what it really means is:
8118 explicit-instantiation:
8119 template decl-specifier-seq [opt] declarator [opt] ;
8121 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8122 supposed to be allowed. A defect report has been filed about this
8127 explicit-instantiation:
8128 storage-class-specifier template
8129 decl-specifier-seq [opt] declarator [opt] ;
8130 function-specifier template
8131 decl-specifier-seq [opt] declarator [opt] ; */
8134 cp_parser_explicit_instantiation (cp_parser
* parser
)
8136 bool declares_class_or_enum
;
8137 tree decl_specifiers
;
8139 tree extension_specifier
= NULL_TREE
;
8141 /* Look for an (optional) storage-class-specifier or
8142 function-specifier. */
8143 if (cp_parser_allow_gnu_extensions_p (parser
))
8146 = cp_parser_storage_class_specifier_opt (parser
);
8147 if (!extension_specifier
)
8148 extension_specifier
= cp_parser_function_specifier_opt (parser
);
8151 /* Look for the `template' keyword. */
8152 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8153 /* Let the front end know that we are processing an explicit
8155 begin_explicit_instantiation ();
8156 /* [temp.explicit] says that we are supposed to ignore access
8157 control while processing explicit instantiation directives. */
8158 push_deferring_access_checks (dk_no_check
);
8159 /* Parse a decl-specifier-seq. */
8161 = cp_parser_decl_specifier_seq (parser
,
8162 CP_PARSER_FLAGS_OPTIONAL
,
8164 &declares_class_or_enum
);
8165 /* If there was exactly one decl-specifier, and it declared a class,
8166 and there's no declarator, then we have an explicit type
8168 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8172 type
= check_tag_decl (decl_specifiers
);
8173 /* Turn access control back on for names used during
8174 template instantiation. */
8175 pop_deferring_access_checks ();
8177 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8184 /* Parse the declarator. */
8186 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8187 /*ctor_dtor_or_conv_p=*/NULL
);
8188 decl
= grokdeclarator (declarator
, decl_specifiers
,
8190 /* Turn access control back on for names used during
8191 template instantiation. */
8192 pop_deferring_access_checks ();
8193 /* Do the explicit instantiation. */
8194 do_decl_instantiation (decl
, extension_specifier
);
8196 /* We're done with the instantiation. */
8197 end_explicit_instantiation ();
8199 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8202 /* Parse an explicit-specialization.
8204 explicit-specialization:
8205 template < > declaration
8207 Although the standard says `declaration', what it really means is:
8209 explicit-specialization:
8210 template <> decl-specifier [opt] init-declarator [opt] ;
8211 template <> function-definition
8212 template <> explicit-specialization
8213 template <> template-declaration */
8216 cp_parser_explicit_specialization (cp_parser
* parser
)
8218 /* Look for the `template' keyword. */
8219 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8220 /* Look for the `<'. */
8221 cp_parser_require (parser
, CPP_LESS
, "`<'");
8222 /* Look for the `>'. */
8223 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8224 /* We have processed another parameter list. */
8225 ++parser
->num_template_parameter_lists
;
8226 /* Let the front end know that we are beginning a specialization. */
8227 begin_specialization ();
8229 /* If the next keyword is `template', we need to figure out whether
8230 or not we're looking a template-declaration. */
8231 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8233 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
8234 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
8235 cp_parser_template_declaration_after_export (parser
,
8236 /*member_p=*/false);
8238 cp_parser_explicit_specialization (parser
);
8241 /* Parse the dependent declaration. */
8242 cp_parser_single_declaration (parser
,
8246 /* We're done with the specialization. */
8247 end_specialization ();
8248 /* We're done with this parameter list. */
8249 --parser
->num_template_parameter_lists
;
8252 /* Parse a type-specifier.
8255 simple-type-specifier
8258 elaborated-type-specifier
8266 Returns a representation of the type-specifier. If the
8267 type-specifier is a keyword (like `int' or `const', or
8268 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8269 For a class-specifier, enum-specifier, or elaborated-type-specifier
8270 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8272 If IS_FRIEND is TRUE then this type-specifier is being declared a
8273 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8274 appearing in a decl-specifier-seq.
8276 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8277 class-specifier, enum-specifier, or elaborated-type-specifier, then
8278 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8281 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8282 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8286 cp_parser_type_specifier (cp_parser
* parser
,
8287 cp_parser_flags flags
,
8289 bool is_declaration
,
8290 bool* declares_class_or_enum
,
8291 bool* is_cv_qualifier
)
8293 tree type_spec
= NULL_TREE
;
8297 /* Assume this type-specifier does not declare a new type. */
8298 if (declares_class_or_enum
)
8299 *declares_class_or_enum
= false;
8300 /* And that it does not specify a cv-qualifier. */
8301 if (is_cv_qualifier
)
8302 *is_cv_qualifier
= false;
8303 /* Peek at the next token. */
8304 token
= cp_lexer_peek_token (parser
->lexer
);
8306 /* If we're looking at a keyword, we can use that to guide the
8307 production we choose. */
8308 keyword
= token
->keyword
;
8311 /* Any of these indicate either a class-specifier, or an
8312 elaborated-type-specifier. */
8317 /* Parse tentatively so that we can back up if we don't find a
8318 class-specifier or enum-specifier. */
8319 cp_parser_parse_tentatively (parser
);
8320 /* Look for the class-specifier or enum-specifier. */
8321 if (keyword
== RID_ENUM
)
8322 type_spec
= cp_parser_enum_specifier (parser
);
8324 type_spec
= cp_parser_class_specifier (parser
);
8326 /* If that worked, we're done. */
8327 if (cp_parser_parse_definitely (parser
))
8329 if (declares_class_or_enum
)
8330 *declares_class_or_enum
= true;
8337 /* Look for an elaborated-type-specifier. */
8338 type_spec
= cp_parser_elaborated_type_specifier (parser
,
8341 /* We're declaring a class or enum -- unless we're using
8343 if (declares_class_or_enum
&& keyword
!= RID_TYPENAME
)
8344 *declares_class_or_enum
= true;
8350 type_spec
= cp_parser_cv_qualifier_opt (parser
);
8351 /* Even though we call a routine that looks for an optional
8352 qualifier, we know that there should be one. */
8353 my_friendly_assert (type_spec
!= NULL
, 20000328);
8354 /* This type-specifier was a cv-qualified. */
8355 if (is_cv_qualifier
)
8356 *is_cv_qualifier
= true;
8361 /* The `__complex__' keyword is a GNU extension. */
8362 return cp_lexer_consume_token (parser
->lexer
)->value
;
8368 /* If we do not already have a type-specifier, assume we are looking
8369 at a simple-type-specifier. */
8370 type_spec
= cp_parser_simple_type_specifier (parser
, flags
);
8372 /* If we didn't find a type-specifier, and a type-specifier was not
8373 optional in this context, issue an error message. */
8374 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8376 cp_parser_error (parser
, "expected type specifier");
8377 return error_mark_node
;
8383 /* Parse a simple-type-specifier.
8385 simple-type-specifier:
8386 :: [opt] nested-name-specifier [opt] type-name
8387 :: [opt] nested-name-specifier template template-id
8402 simple-type-specifier:
8403 __typeof__ unary-expression
8404 __typeof__ ( type-id )
8406 For the various keywords, the value returned is simply the
8407 TREE_IDENTIFIER representing the keyword. For the first two
8408 productions, the value returned is the indicated TYPE_DECL. */
8411 cp_parser_simple_type_specifier (cp_parser
* parser
, cp_parser_flags flags
)
8413 tree type
= NULL_TREE
;
8416 /* Peek at the next token. */
8417 token
= cp_lexer_peek_token (parser
->lexer
);
8419 /* If we're looking at a keyword, things are easy. */
8420 switch (token
->keyword
)
8433 /* Consume the token. */
8434 return cp_lexer_consume_token (parser
->lexer
)->value
;
8440 /* Consume the `typeof' token. */
8441 cp_lexer_consume_token (parser
->lexer
);
8442 /* Parse the operand to `typeof' */
8443 operand
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
8444 /* If it is not already a TYPE, take its type. */
8445 if (!TYPE_P (operand
))
8446 operand
= finish_typeof (operand
);
8455 /* The type-specifier must be a user-defined type. */
8456 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
8458 /* Don't gobble tokens or issue error messages if this is an
8459 optional type-specifier. */
8460 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
8461 cp_parser_parse_tentatively (parser
);
8463 /* Look for the optional `::' operator. */
8464 cp_parser_global_scope_opt (parser
,
8465 /*current_scope_valid_p=*/false);
8466 /* Look for the nested-name specifier. */
8467 cp_parser_nested_name_specifier_opt (parser
,
8468 /*typename_keyword_p=*/false,
8469 /*check_dependency_p=*/true,
8471 /* If we have seen a nested-name-specifier, and the next token
8472 is `template', then we are using the template-id production. */
8474 && cp_parser_optional_template_keyword (parser
))
8476 /* Look for the template-id. */
8477 type
= cp_parser_template_id (parser
,
8478 /*template_keyword_p=*/true,
8479 /*check_dependency_p=*/true);
8480 /* If the template-id did not name a type, we are out of
8482 if (TREE_CODE (type
) != TYPE_DECL
)
8484 cp_parser_error (parser
, "expected template-id for type");
8488 /* Otherwise, look for a type-name. */
8491 type
= cp_parser_type_name (parser
);
8492 if (type
== error_mark_node
)
8496 /* If it didn't work out, we don't have a TYPE. */
8497 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
8498 && !cp_parser_parse_definitely (parser
))
8502 /* If we didn't get a type-name, issue an error message. */
8503 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
8505 cp_parser_error (parser
, "expected type-name");
8506 return error_mark_node
;
8512 /* Parse a type-name.
8525 Returns a TYPE_DECL for the the type. */
8528 cp_parser_type_name (cp_parser
* parser
)
8533 /* We can't know yet whether it is a class-name or not. */
8534 cp_parser_parse_tentatively (parser
);
8535 /* Try a class-name. */
8536 type_decl
= cp_parser_class_name (parser
,
8537 /*typename_keyword_p=*/false,
8538 /*template_keyword_p=*/false,
8540 /*check_dependency_p=*/true,
8541 /*class_head_p=*/false);
8542 /* If it's not a class-name, keep looking. */
8543 if (!cp_parser_parse_definitely (parser
))
8545 /* It must be a typedef-name or an enum-name. */
8546 identifier
= cp_parser_identifier (parser
);
8547 if (identifier
== error_mark_node
)
8548 return error_mark_node
;
8550 /* Look up the type-name. */
8551 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
8552 /* Issue an error if we did not find a type-name. */
8553 if (TREE_CODE (type_decl
) != TYPE_DECL
)
8555 cp_parser_error (parser
, "expected type-name");
8556 type_decl
= error_mark_node
;
8558 /* Remember that the name was used in the definition of the
8559 current class so that we can check later to see if the
8560 meaning would have been different after the class was
8561 entirely defined. */
8562 else if (type_decl
!= error_mark_node
8564 maybe_note_name_used_in_class (identifier
, type_decl
);
8571 /* Parse an elaborated-type-specifier. Note that the grammar given
8572 here incorporates the resolution to DR68.
8574 elaborated-type-specifier:
8575 class-key :: [opt] nested-name-specifier [opt] identifier
8576 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8577 enum :: [opt] nested-name-specifier [opt] identifier
8578 typename :: [opt] nested-name-specifier identifier
8579 typename :: [opt] nested-name-specifier template [opt]
8582 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8583 declared `friend'. If IS_DECLARATION is TRUE, then this
8584 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8585 something is being declared.
8587 Returns the TYPE specified. */
8590 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
8592 bool is_declaration
)
8594 enum tag_types tag_type
;
8596 tree type
= NULL_TREE
;
8598 /* See if we're looking at the `enum' keyword. */
8599 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
8601 /* Consume the `enum' token. */
8602 cp_lexer_consume_token (parser
->lexer
);
8603 /* Remember that it's an enumeration type. */
8604 tag_type
= enum_type
;
8606 /* Or, it might be `typename'. */
8607 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8610 /* Consume the `typename' token. */
8611 cp_lexer_consume_token (parser
->lexer
);
8612 /* Remember that it's a `typename' type. */
8613 tag_type
= typename_type
;
8614 /* The `typename' keyword is only allowed in templates. */
8615 if (!processing_template_decl
)
8616 pedwarn ("using `typename' outside of template");
8618 /* Otherwise it must be a class-key. */
8621 tag_type
= cp_parser_class_key (parser
);
8622 if (tag_type
== none_type
)
8623 return error_mark_node
;
8626 /* Look for the `::' operator. */
8627 cp_parser_global_scope_opt (parser
,
8628 /*current_scope_valid_p=*/false);
8629 /* Look for the nested-name-specifier. */
8630 if (tag_type
== typename_type
)
8632 if (cp_parser_nested_name_specifier (parser
,
8633 /*typename_keyword_p=*/true,
8634 /*check_dependency_p=*/true,
8637 return error_mark_node
;
8640 /* Even though `typename' is not present, the proposed resolution
8641 to Core Issue 180 says that in `class A<T>::B', `B' should be
8642 considered a type-name, even if `A<T>' is dependent. */
8643 cp_parser_nested_name_specifier_opt (parser
,
8644 /*typename_keyword_p=*/true,
8645 /*check_dependency_p=*/true,
8647 /* For everything but enumeration types, consider a template-id. */
8648 if (tag_type
!= enum_type
)
8650 bool template_p
= false;
8653 /* Allow the `template' keyword. */
8654 template_p
= cp_parser_optional_template_keyword (parser
);
8655 /* If we didn't see `template', we don't know if there's a
8656 template-id or not. */
8658 cp_parser_parse_tentatively (parser
);
8659 /* Parse the template-id. */
8660 decl
= cp_parser_template_id (parser
, template_p
,
8661 /*check_dependency_p=*/true);
8662 /* If we didn't find a template-id, look for an ordinary
8664 if (!template_p
&& !cp_parser_parse_definitely (parser
))
8666 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8667 in effect, then we must assume that, upon instantiation, the
8668 template will correspond to a class. */
8669 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
8670 && tag_type
== typename_type
)
8671 type
= make_typename_type (parser
->scope
, decl
,
8674 type
= TREE_TYPE (decl
);
8677 /* For an enumeration type, consider only a plain identifier. */
8680 identifier
= cp_parser_identifier (parser
);
8682 if (identifier
== error_mark_node
)
8683 return error_mark_node
;
8685 /* For a `typename', we needn't call xref_tag. */
8686 if (tag_type
== typename_type
)
8687 return make_typename_type (parser
->scope
, identifier
,
8689 /* Look up a qualified name in the usual way. */
8694 /* In an elaborated-type-specifier, names are assumed to name
8695 types, so we set IS_TYPE to TRUE when calling
8696 cp_parser_lookup_name. */
8697 decl
= cp_parser_lookup_name (parser
, identifier
,
8699 /*is_namespace=*/false,
8700 /*check_dependency=*/true);
8702 /* If we are parsing friend declaration, DECL may be a
8703 TEMPLATE_DECL tree node here. However, we need to check
8704 whether this TEMPLATE_DECL results in valid code. Consider
8705 the following example:
8708 template <class T> class C {};
8711 template <class T> friend class N::C; // #1, valid code
8713 template <class T> class Y {
8714 friend class N::C; // #2, invalid code
8717 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8718 name lookup of `N::C'. We see that friend declaration must
8719 be template for the code to be valid. Note that
8720 processing_template_decl does not work here since it is
8721 always 1 for the above two cases. */
8723 decl
= (cp_parser_maybe_treat_template_as_class
8724 (decl
, /*tag_name_p=*/is_friend
8725 && parser
->num_template_parameter_lists
));
8727 if (TREE_CODE (decl
) != TYPE_DECL
)
8729 error ("expected type-name");
8730 return error_mark_node
;
8732 else if (TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
8733 && tag_type
!= enum_type
)
8734 error ("`%T' referred to as `%s'", TREE_TYPE (decl
),
8735 tag_type
== record_type
? "struct" : "class");
8736 else if (TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
8737 && tag_type
== enum_type
)
8738 error ("`%T' referred to as enum", TREE_TYPE (decl
));
8740 type
= TREE_TYPE (decl
);
8744 /* An elaborated-type-specifier sometimes introduces a new type and
8745 sometimes names an existing type. Normally, the rule is that it
8746 introduces a new type only if there is not an existing type of
8747 the same name already in scope. For example, given:
8750 void f() { struct S s; }
8752 the `struct S' in the body of `f' is the same `struct S' as in
8753 the global scope; the existing definition is used. However, if
8754 there were no global declaration, this would introduce a new
8755 local class named `S'.
8757 An exception to this rule applies to the following code:
8759 namespace N { struct S; }
8761 Here, the elaborated-type-specifier names a new type
8762 unconditionally; even if there is already an `S' in the
8763 containing scope this declaration names a new type.
8764 This exception only applies if the elaborated-type-specifier
8765 forms the complete declaration:
8769 A declaration consisting solely of `class-key identifier ;' is
8770 either a redeclaration of the name in the current scope or a
8771 forward declaration of the identifier as a class name. It
8772 introduces the name into the current scope.
8774 We are in this situation precisely when the next token is a `;'.
8776 An exception to the exception is that a `friend' declaration does
8777 *not* name a new type; i.e., given:
8779 struct S { friend struct T; };
8781 `T' is not a new type in the scope of `S'.
8783 Also, `new struct S' or `sizeof (struct S)' never results in the
8784 definition of a new type; a new type can only be declared in a
8785 declaration context. */
8787 type
= xref_tag (tag_type
, identifier
,
8788 /*attributes=*/NULL_TREE
,
8791 || cp_lexer_next_token_is_not (parser
->lexer
,
8795 if (tag_type
!= enum_type
)
8796 cp_parser_check_class_key (tag_type
, type
);
8800 /* Parse an enum-specifier.
8803 enum identifier [opt] { enumerator-list [opt] }
8805 Returns an ENUM_TYPE representing the enumeration. */
8808 cp_parser_enum_specifier (cp_parser
* parser
)
8811 tree identifier
= NULL_TREE
;
8814 /* Look for the `enum' keyword. */
8815 if (!cp_parser_require_keyword (parser
, RID_ENUM
, "`enum'"))
8816 return error_mark_node
;
8817 /* Peek at the next token. */
8818 token
= cp_lexer_peek_token (parser
->lexer
);
8820 /* See if it is an identifier. */
8821 if (token
->type
== CPP_NAME
)
8822 identifier
= cp_parser_identifier (parser
);
8824 /* Look for the `{'. */
8825 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
8826 return error_mark_node
;
8828 /* At this point, we're going ahead with the enum-specifier, even
8829 if some other problem occurs. */
8830 cp_parser_commit_to_tentative_parse (parser
);
8832 /* Issue an error message if type-definitions are forbidden here. */
8833 cp_parser_check_type_definition (parser
);
8835 /* Create the new type. */
8836 type
= start_enum (identifier
? identifier
: make_anon_name ());
8838 /* Peek at the next token. */
8839 token
= cp_lexer_peek_token (parser
->lexer
);
8840 /* If it's not a `}', then there are some enumerators. */
8841 if (token
->type
!= CPP_CLOSE_BRACE
)
8842 cp_parser_enumerator_list (parser
, type
);
8843 /* Look for the `}'. */
8844 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8846 /* Finish up the enumeration. */
8852 /* Parse an enumerator-list. The enumerators all have the indicated
8856 enumerator-definition
8857 enumerator-list , enumerator-definition */
8860 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
8866 /* Parse an enumerator-definition. */
8867 cp_parser_enumerator_definition (parser
, type
);
8868 /* Peek at the next token. */
8869 token
= cp_lexer_peek_token (parser
->lexer
);
8870 /* If it's not a `,', then we've reached the end of the
8872 if (token
->type
!= CPP_COMMA
)
8874 /* Otherwise, consume the `,' and keep going. */
8875 cp_lexer_consume_token (parser
->lexer
);
8876 /* If the next token is a `}', there is a trailing comma. */
8877 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
8879 if (pedantic
&& !in_system_header
)
8880 pedwarn ("comma at end of enumerator list");
8886 /* Parse an enumerator-definition. The enumerator has the indicated
8889 enumerator-definition:
8891 enumerator = constant-expression
8897 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
8903 /* Look for the identifier. */
8904 identifier
= cp_parser_identifier (parser
);
8905 if (identifier
== error_mark_node
)
8908 /* Peek at the next token. */
8909 token
= cp_lexer_peek_token (parser
->lexer
);
8910 /* If it's an `=', then there's an explicit value. */
8911 if (token
->type
== CPP_EQ
)
8913 /* Consume the `=' token. */
8914 cp_lexer_consume_token (parser
->lexer
);
8915 /* Parse the value. */
8916 value
= cp_parser_constant_expression (parser
,
8917 /*allow_non_constant=*/false,
8923 /* Create the enumerator. */
8924 build_enumerator (identifier
, value
, type
);
8927 /* Parse a namespace-name.
8930 original-namespace-name
8933 Returns the NAMESPACE_DECL for the namespace. */
8936 cp_parser_namespace_name (cp_parser
* parser
)
8939 tree namespace_decl
;
8941 /* Get the name of the namespace. */
8942 identifier
= cp_parser_identifier (parser
);
8943 if (identifier
== error_mark_node
)
8944 return error_mark_node
;
8946 /* Look up the identifier in the currently active scope. Look only
8947 for namespaces, due to:
8951 When looking up a namespace-name in a using-directive or alias
8952 definition, only namespace names are considered.
8958 During the lookup of a name preceding the :: scope resolution
8959 operator, object, function, and enumerator names are ignored.
8961 (Note that cp_parser_class_or_namespace_name only calls this
8962 function if the token after the name is the scope resolution
8964 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
8966 /*is_namespace=*/true,
8967 /*check_dependency=*/true);
8968 /* If it's not a namespace, issue an error. */
8969 if (namespace_decl
== error_mark_node
8970 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
8972 cp_parser_error (parser
, "expected namespace-name");
8973 namespace_decl
= error_mark_node
;
8976 return namespace_decl
;
8979 /* Parse a namespace-definition.
8981 namespace-definition:
8982 named-namespace-definition
8983 unnamed-namespace-definition
8985 named-namespace-definition:
8986 original-namespace-definition
8987 extension-namespace-definition
8989 original-namespace-definition:
8990 namespace identifier { namespace-body }
8992 extension-namespace-definition:
8993 namespace original-namespace-name { namespace-body }
8995 unnamed-namespace-definition:
8996 namespace { namespace-body } */
8999 cp_parser_namespace_definition (cp_parser
* parser
)
9003 /* Look for the `namespace' keyword. */
9004 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9006 /* Get the name of the namespace. We do not attempt to distinguish
9007 between an original-namespace-definition and an
9008 extension-namespace-definition at this point. The semantic
9009 analysis routines are responsible for that. */
9010 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9011 identifier
= cp_parser_identifier (parser
);
9013 identifier
= NULL_TREE
;
9015 /* Look for the `{' to start the namespace. */
9016 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
9017 /* Start the namespace. */
9018 push_namespace (identifier
);
9019 /* Parse the body of the namespace. */
9020 cp_parser_namespace_body (parser
);
9021 /* Finish the namespace. */
9023 /* Look for the final `}'. */
9024 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9027 /* Parse a namespace-body.
9030 declaration-seq [opt] */
9033 cp_parser_namespace_body (cp_parser
* parser
)
9035 cp_parser_declaration_seq_opt (parser
);
9038 /* Parse a namespace-alias-definition.
9040 namespace-alias-definition:
9041 namespace identifier = qualified-namespace-specifier ; */
9044 cp_parser_namespace_alias_definition (cp_parser
* parser
)
9047 tree namespace_specifier
;
9049 /* Look for the `namespace' keyword. */
9050 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9051 /* Look for the identifier. */
9052 identifier
= cp_parser_identifier (parser
);
9053 if (identifier
== error_mark_node
)
9055 /* Look for the `=' token. */
9056 cp_parser_require (parser
, CPP_EQ
, "`='");
9057 /* Look for the qualified-namespace-specifier. */
9059 = cp_parser_qualified_namespace_specifier (parser
);
9060 /* Look for the `;' token. */
9061 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9063 /* Register the alias in the symbol table. */
9064 do_namespace_alias (identifier
, namespace_specifier
);
9067 /* Parse a qualified-namespace-specifier.
9069 qualified-namespace-specifier:
9070 :: [opt] nested-name-specifier [opt] namespace-name
9072 Returns a NAMESPACE_DECL corresponding to the specified
9076 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
9078 /* Look for the optional `::'. */
9079 cp_parser_global_scope_opt (parser
,
9080 /*current_scope_valid_p=*/false);
9082 /* Look for the optional nested-name-specifier. */
9083 cp_parser_nested_name_specifier_opt (parser
,
9084 /*typename_keyword_p=*/false,
9085 /*check_dependency_p=*/true,
9088 return cp_parser_namespace_name (parser
);
9091 /* Parse a using-declaration.
9094 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9095 using :: unqualified-id ; */
9098 cp_parser_using_declaration (cp_parser
* parser
)
9101 bool typename_p
= false;
9102 bool global_scope_p
;
9107 /* Look for the `using' keyword. */
9108 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9110 /* Peek at the next token. */
9111 token
= cp_lexer_peek_token (parser
->lexer
);
9112 /* See if it's `typename'. */
9113 if (token
->keyword
== RID_TYPENAME
)
9115 /* Remember that we've seen it. */
9117 /* Consume the `typename' token. */
9118 cp_lexer_consume_token (parser
->lexer
);
9121 /* Look for the optional global scope qualification. */
9123 = (cp_parser_global_scope_opt (parser
,
9124 /*current_scope_valid_p=*/false)
9127 /* If we saw `typename', or didn't see `::', then there must be a
9128 nested-name-specifier present. */
9129 if (typename_p
|| !global_scope_p
)
9130 cp_parser_nested_name_specifier (parser
, typename_p
,
9131 /*check_dependency_p=*/true,
9133 /* Otherwise, we could be in either of the two productions. In that
9134 case, treat the nested-name-specifier as optional. */
9136 cp_parser_nested_name_specifier_opt (parser
,
9137 /*typename_keyword_p=*/false,
9138 /*check_dependency_p=*/true,
9141 /* Parse the unqualified-id. */
9142 identifier
= cp_parser_unqualified_id (parser
,
9143 /*template_keyword_p=*/false,
9144 /*check_dependency_p=*/true);
9146 /* The function we call to handle a using-declaration is different
9147 depending on what scope we are in. */
9148 scope
= current_scope ();
9149 if (scope
&& TYPE_P (scope
))
9151 /* Create the USING_DECL. */
9152 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
9155 /* Add it to the list of members in this class. */
9156 finish_member_declaration (decl
);
9160 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9161 if (decl
== error_mark_node
)
9163 if (parser
->scope
&& parser
->scope
!= global_namespace
)
9164 error ("`%D::%D' has not been declared",
9165 parser
->scope
, identifier
);
9167 error ("`::%D' has not been declared", identifier
);
9170 do_local_using_decl (decl
);
9172 do_toplevel_using_decl (decl
);
9175 /* Look for the final `;'. */
9176 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9179 /* Parse a using-directive.
9182 using namespace :: [opt] nested-name-specifier [opt]
9186 cp_parser_using_directive (cp_parser
* parser
)
9188 tree namespace_decl
;
9190 /* Look for the `using' keyword. */
9191 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
9192 /* And the `namespace' keyword. */
9193 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
9194 /* Look for the optional `::' operator. */
9195 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
9196 /* And the optional nested-name-specifier. */
9197 cp_parser_nested_name_specifier_opt (parser
,
9198 /*typename_keyword_p=*/false,
9199 /*check_dependency_p=*/true,
9201 /* Get the namespace being used. */
9202 namespace_decl
= cp_parser_namespace_name (parser
);
9203 /* Update the symbol table. */
9204 do_using_directive (namespace_decl
);
9205 /* Look for the final `;'. */
9206 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9209 /* Parse an asm-definition.
9212 asm ( string-literal ) ;
9217 asm volatile [opt] ( string-literal ) ;
9218 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9219 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9220 : asm-operand-list [opt] ) ;
9221 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9222 : asm-operand-list [opt]
9223 : asm-operand-list [opt] ) ; */
9226 cp_parser_asm_definition (cp_parser
* parser
)
9230 tree outputs
= NULL_TREE
;
9231 tree inputs
= NULL_TREE
;
9232 tree clobbers
= NULL_TREE
;
9234 bool volatile_p
= false;
9235 bool extended_p
= false;
9237 /* Look for the `asm' keyword. */
9238 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
9239 /* See if the next token is `volatile'. */
9240 if (cp_parser_allow_gnu_extensions_p (parser
)
9241 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
9243 /* Remember that we saw the `volatile' keyword. */
9245 /* Consume the token. */
9246 cp_lexer_consume_token (parser
->lexer
);
9248 /* Look for the opening `('. */
9249 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
9250 /* Look for the string. */
9251 token
= cp_parser_require (parser
, CPP_STRING
, "asm body");
9254 string
= token
->value
;
9255 /* If we're allowing GNU extensions, check for the extended assembly
9256 syntax. Unfortunately, the `:' tokens need not be separated by
9257 a space in C, and so, for compatibility, we tolerate that here
9258 too. Doing that means that we have to treat the `::' operator as
9260 if (cp_parser_allow_gnu_extensions_p (parser
)
9261 && at_function_scope_p ()
9262 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
9263 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
9265 bool inputs_p
= false;
9266 bool clobbers_p
= false;
9268 /* The extended syntax was used. */
9271 /* Look for outputs. */
9272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9274 /* Consume the `:'. */
9275 cp_lexer_consume_token (parser
->lexer
);
9276 /* Parse the output-operands. */
9277 if (cp_lexer_next_token_is_not (parser
->lexer
,
9279 && cp_lexer_next_token_is_not (parser
->lexer
,
9281 && cp_lexer_next_token_is_not (parser
->lexer
,
9283 outputs
= cp_parser_asm_operand_list (parser
);
9285 /* If the next token is `::', there are no outputs, and the
9286 next token is the beginning of the inputs. */
9287 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9289 /* Consume the `::' token. */
9290 cp_lexer_consume_token (parser
->lexer
);
9291 /* The inputs are coming next. */
9295 /* Look for inputs. */
9297 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9300 /* Consume the `:'. */
9301 cp_lexer_consume_token (parser
->lexer
);
9302 /* Parse the output-operands. */
9303 if (cp_lexer_next_token_is_not (parser
->lexer
,
9305 && cp_lexer_next_token_is_not (parser
->lexer
,
9307 && cp_lexer_next_token_is_not (parser
->lexer
,
9309 inputs
= cp_parser_asm_operand_list (parser
);
9311 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
9312 /* The clobbers are coming next. */
9315 /* Look for clobbers. */
9317 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9320 /* Consume the `:'. */
9321 cp_lexer_consume_token (parser
->lexer
);
9322 /* Parse the clobbers. */
9323 if (cp_lexer_next_token_is_not (parser
->lexer
,
9325 clobbers
= cp_parser_asm_clobber_list (parser
);
9328 /* Look for the closing `)'. */
9329 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9330 cp_parser_skip_to_closing_parenthesis (parser
);
9331 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
9333 /* Create the ASM_STMT. */
9334 if (at_function_scope_p ())
9337 finish_asm_stmt (volatile_p
9338 ? ridpointers
[(int) RID_VOLATILE
] : NULL_TREE
,
9339 string
, outputs
, inputs
, clobbers
);
9340 /* If the extended syntax was not used, mark the ASM_STMT. */
9342 ASM_INPUT_P (asm_stmt
) = 1;
9345 assemble_asm (string
);
9348 /* Declarators [gram.dcl.decl] */
9350 /* Parse an init-declarator.
9353 declarator initializer [opt]
9358 declarator asm-specification [opt] attributes [opt] initializer [opt]
9360 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9361 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9362 then this declarator appears in a class scope. The new DECL created
9363 by this declarator is returned.
9365 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9366 for a function-definition here as well. If the declarator is a
9367 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9368 be TRUE upon return. By that point, the function-definition will
9369 have been completely parsed.
9371 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9375 cp_parser_init_declarator (cp_parser
* parser
,
9376 tree decl_specifiers
,
9377 tree prefix_attributes
,
9378 bool function_definition_allowed_p
,
9380 bool* function_definition_p
)
9385 tree asm_specification
;
9387 tree decl
= NULL_TREE
;
9389 bool is_initialized
;
9390 bool is_parenthesized_init
;
9391 bool ctor_dtor_or_conv_p
;
9394 /* Assume that this is not the declarator for a function
9396 if (function_definition_p
)
9397 *function_definition_p
= false;
9399 /* Defer access checks while parsing the declarator; we cannot know
9400 what names are accessible until we know what is being
9402 resume_deferring_access_checks ();
9404 /* Parse the declarator. */
9406 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9407 &ctor_dtor_or_conv_p
);
9408 /* Gather up the deferred checks. */
9409 stop_deferring_access_checks ();
9411 /* If the DECLARATOR was erroneous, there's no need to go
9413 if (declarator
== error_mark_node
)
9414 return error_mark_node
;
9416 /* Figure out what scope the entity declared by the DECLARATOR is
9417 located in. `grokdeclarator' sometimes changes the scope, so
9418 we compute it now. */
9419 scope
= get_scope_of_declarator (declarator
);
9421 /* If we're allowing GNU extensions, look for an asm-specification
9423 if (cp_parser_allow_gnu_extensions_p (parser
))
9425 /* Look for an asm-specification. */
9426 asm_specification
= cp_parser_asm_specification_opt (parser
);
9427 /* And attributes. */
9428 attributes
= cp_parser_attributes_opt (parser
);
9432 asm_specification
= NULL_TREE
;
9433 attributes
= NULL_TREE
;
9436 /* Peek at the next token. */
9437 token
= cp_lexer_peek_token (parser
->lexer
);
9438 /* Check to see if the token indicates the start of a
9439 function-definition. */
9440 if (cp_parser_token_starts_function_definition_p (token
))
9442 if (!function_definition_allowed_p
)
9444 /* If a function-definition should not appear here, issue an
9446 cp_parser_error (parser
,
9447 "a function-definition is not allowed here");
9448 return error_mark_node
;
9452 /* Neither attributes nor an asm-specification are allowed
9453 on a function-definition. */
9454 if (asm_specification
)
9455 error ("an asm-specification is not allowed on a function-definition");
9457 error ("attributes are not allowed on a function-definition");
9458 /* This is a function-definition. */
9459 *function_definition_p
= true;
9461 /* Parse the function definition. */
9462 decl
= (cp_parser_function_definition_from_specifiers_and_declarator
9463 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
9471 Only in function declarations for constructors, destructors, and
9472 type conversions can the decl-specifier-seq be omitted.
9474 We explicitly postpone this check past the point where we handle
9475 function-definitions because we tolerate function-definitions
9476 that are missing their return types in some modes. */
9477 if (!decl_specifiers
&& !ctor_dtor_or_conv_p
)
9479 cp_parser_error (parser
,
9480 "expected constructor, destructor, or type conversion");
9481 return error_mark_node
;
9484 /* An `=' or an `(' indicates an initializer. */
9485 is_initialized
= (token
->type
== CPP_EQ
9486 || token
->type
== CPP_OPEN_PAREN
);
9487 /* If the init-declarator isn't initialized and isn't followed by a
9488 `,' or `;', it's not a valid init-declarator. */
9490 && token
->type
!= CPP_COMMA
9491 && token
->type
!= CPP_SEMICOLON
)
9493 cp_parser_error (parser
, "expected init-declarator");
9494 return error_mark_node
;
9497 /* Because start_decl has side-effects, we should only call it if we
9498 know we're going ahead. By this point, we know that we cannot
9499 possibly be looking at any other construct. */
9500 cp_parser_commit_to_tentative_parse (parser
);
9502 /* Check to see whether or not this declaration is a friend. */
9503 friend_p
= cp_parser_friend_p (decl_specifiers
);
9505 /* Check that the number of template-parameter-lists is OK. */
9506 if (!cp_parser_check_declarator_template_parameters (parser
,
9508 return error_mark_node
;
9510 /* Enter the newly declared entry in the symbol table. If we're
9511 processing a declaration in a class-specifier, we wait until
9512 after processing the initializer. */
9515 if (parser
->in_unbraced_linkage_specification_p
)
9517 decl_specifiers
= tree_cons (error_mark_node
,
9518 get_identifier ("extern"),
9520 have_extern_spec
= false;
9522 decl
= start_decl (declarator
,
9529 /* Enter the SCOPE. That way unqualified names appearing in the
9530 initializer will be looked up in SCOPE. */
9534 /* Perform deferred access control checks, now that we know in which
9535 SCOPE the declared entity resides. */
9536 if (!member_p
&& decl
)
9538 tree saved_current_function_decl
= NULL_TREE
;
9540 /* If the entity being declared is a function, pretend that we
9541 are in its scope. If it is a `friend', it may have access to
9542 things that would not otherwise be accessible. */
9543 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9545 saved_current_function_decl
= current_function_decl
;
9546 current_function_decl
= decl
;
9549 /* Perform the access control checks for the declarator and the
9550 the decl-specifiers. */
9551 perform_deferred_access_checks ();
9553 /* Restore the saved value. */
9554 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9555 current_function_decl
= saved_current_function_decl
;
9558 /* Parse the initializer. */
9560 initializer
= cp_parser_initializer (parser
, &is_parenthesized_init
);
9563 initializer
= NULL_TREE
;
9564 is_parenthesized_init
= false;
9567 /* The old parser allows attributes to appear after a parenthesized
9568 initializer. Mark Mitchell proposed removing this functionality
9569 on the GCC mailing lists on 2002-08-13. This parser accepts the
9570 attributes -- but ignores them. */
9571 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
9572 if (cp_parser_attributes_opt (parser
))
9573 warning ("attributes after parenthesized initializer ignored");
9575 /* Leave the SCOPE, now that we have processed the initializer. It
9576 is important to do this before calling cp_finish_decl because it
9577 makes decisions about whether to create DECL_STMTs or not based
9578 on the current scope. */
9582 /* For an in-class declaration, use `grokfield' to create the
9586 decl
= grokfield (declarator
, decl_specifiers
,
9587 initializer
, /*asmspec=*/NULL_TREE
,
9588 /*attributes=*/NULL_TREE
);
9589 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
9590 cp_parser_save_default_args (parser
, decl
);
9593 /* Finish processing the declaration. But, skip friend
9595 if (!friend_p
&& decl
)
9596 cp_finish_decl (decl
,
9599 /* If the initializer is in parentheses, then this is
9600 a direct-initialization, which means that an
9601 `explicit' constructor is OK. Otherwise, an
9602 `explicit' constructor cannot be used. */
9603 ((is_parenthesized_init
|| !is_initialized
)
9604 ? 0 : LOOKUP_ONLYCONVERTING
));
9609 /* Parse a declarator.
9613 ptr-operator declarator
9615 abstract-declarator:
9616 ptr-operator abstract-declarator [opt]
9617 direct-abstract-declarator
9622 attributes [opt] direct-declarator
9623 attributes [opt] ptr-operator declarator
9625 abstract-declarator:
9626 attributes [opt] ptr-operator abstract-declarator [opt]
9627 attributes [opt] direct-abstract-declarator
9629 Returns a representation of the declarator. If the declarator has
9630 the form `* declarator', then an INDIRECT_REF is returned, whose
9631 only operand is the sub-declarator. Analogously, `& declarator' is
9632 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9633 used. The first operand is the TYPE for `X'. The second operand
9634 is an INDIRECT_REF whose operand is the sub-declarator.
9636 Otherwise, the representation is as for a direct-declarator.
9638 (It would be better to define a structure type to represent
9639 declarators, rather than abusing `tree' nodes to represent
9640 declarators. That would be much clearer and save some memory.
9641 There is no reason for declarators to be garbage-collected, for
9642 example; they are created during parser and no longer needed after
9643 `grokdeclarator' has been called.)
9645 For a ptr-operator that has the optional cv-qualifier-seq,
9646 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9649 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9650 true if this declarator represents a constructor, destructor, or
9651 type conversion operator. Otherwise, it is set to false.
9653 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9654 a decl-specifier-seq unless it declares a constructor, destructor,
9655 or conversion. It might seem that we could check this condition in
9656 semantic analysis, rather than parsing, but that makes it difficult
9657 to handle something like `f()'. We want to notice that there are
9658 no decl-specifiers, and therefore realize that this is an
9659 expression, not a declaration.) */
9662 cp_parser_declarator (cp_parser
* parser
,
9663 cp_parser_declarator_kind dcl_kind
,
9664 bool* ctor_dtor_or_conv_p
)
9668 enum tree_code code
;
9669 tree cv_qualifier_seq
;
9671 tree attributes
= NULL_TREE
;
9673 /* Assume this is not a constructor, destructor, or type-conversion
9675 if (ctor_dtor_or_conv_p
)
9676 *ctor_dtor_or_conv_p
= false;
9678 if (cp_parser_allow_gnu_extensions_p (parser
))
9679 attributes
= cp_parser_attributes_opt (parser
);
9681 /* Peek at the next token. */
9682 token
= cp_lexer_peek_token (parser
->lexer
);
9684 /* Check for the ptr-operator production. */
9685 cp_parser_parse_tentatively (parser
);
9686 /* Parse the ptr-operator. */
9687 code
= cp_parser_ptr_operator (parser
,
9690 /* If that worked, then we have a ptr-operator. */
9691 if (cp_parser_parse_definitely (parser
))
9693 /* The dependent declarator is optional if we are parsing an
9694 abstract-declarator. */
9695 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9696 cp_parser_parse_tentatively (parser
);
9698 /* Parse the dependent declarator. */
9699 declarator
= cp_parser_declarator (parser
, dcl_kind
,
9700 /*ctor_dtor_or_conv_p=*/NULL
);
9702 /* If we are parsing an abstract-declarator, we must handle the
9703 case where the dependent declarator is absent. */
9704 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
9705 && !cp_parser_parse_definitely (parser
))
9706 declarator
= NULL_TREE
;
9708 /* Build the representation of the ptr-operator. */
9709 if (code
== INDIRECT_REF
)
9710 declarator
= make_pointer_declarator (cv_qualifier_seq
,
9713 declarator
= make_reference_declarator (cv_qualifier_seq
,
9715 /* Handle the pointer-to-member case. */
9717 declarator
= build_nt (SCOPE_REF
, class_type
, declarator
);
9719 /* Everything else is a direct-declarator. */
9721 declarator
= cp_parser_direct_declarator (parser
,
9723 ctor_dtor_or_conv_p
);
9725 if (attributes
&& declarator
!= error_mark_node
)
9726 declarator
= tree_cons (attributes
, declarator
, NULL_TREE
);
9731 /* Parse a direct-declarator or direct-abstract-declarator.
9735 direct-declarator ( parameter-declaration-clause )
9736 cv-qualifier-seq [opt]
9737 exception-specification [opt]
9738 direct-declarator [ constant-expression [opt] ]
9741 direct-abstract-declarator:
9742 direct-abstract-declarator [opt]
9743 ( parameter-declaration-clause )
9744 cv-qualifier-seq [opt]
9745 exception-specification [opt]
9746 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9747 ( abstract-declarator )
9749 Returns a representation of the declarator. DCL_KIND is
9750 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9751 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9752 we are parsing a direct-declarator. It is
9753 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9754 of ambiguity we prefer an abstract declarator, as per
9755 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9756 cp_parser_declarator.
9758 For the declarator-id production, the representation is as for an
9759 id-expression, except that a qualified name is represented as a
9760 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9761 see the documentation of the FUNCTION_DECLARATOR_* macros for
9762 information about how to find the various declarator components.
9763 An array-declarator is represented as an ARRAY_REF. The
9764 direct-declarator is the first operand; the constant-expression
9765 indicating the size of the array is the second operand. */
9768 cp_parser_direct_declarator (cp_parser
* parser
,
9769 cp_parser_declarator_kind dcl_kind
,
9770 bool* ctor_dtor_or_conv_p
)
9773 tree declarator
= NULL_TREE
;
9774 tree scope
= NULL_TREE
;
9775 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
9776 bool saved_in_declarator_p
= parser
->in_declarator_p
;
9781 /* Peek at the next token. */
9782 token
= cp_lexer_peek_token (parser
->lexer
);
9783 if (token
->type
== CPP_OPEN_PAREN
)
9785 /* This is either a parameter-declaration-clause, or a
9786 parenthesized declarator. When we know we are parsing a
9787 named declarator, it must be a parenthesized declarator
9788 if FIRST is true. For instance, `(int)' is a
9789 parameter-declaration-clause, with an omitted
9790 direct-abstract-declarator. But `((*))', is a
9791 parenthesized abstract declarator. Finally, when T is a
9792 template parameter `(T)' is a
9793 parameter-declaration-clause, and not a parenthesized
9796 We first try and parse a parameter-declaration-clause,
9797 and then try a nested declarator (if FIRST is true).
9799 It is not an error for it not to be a
9800 parameter-declaration-clause, even when FIRST is
9806 The first is the declaration of a function while the
9807 second is a the definition of a variable, including its
9810 Having seen only the parenthesis, we cannot know which of
9811 these two alternatives should be selected. Even more
9812 complex are examples like:
9817 The former is a function-declaration; the latter is a
9818 variable initialization.
9820 Thus again, we try a parameter-declaration-clause, and if
9821 that fails, we back out and return. */
9823 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9827 cp_parser_parse_tentatively (parser
);
9829 /* Consume the `('. */
9830 cp_lexer_consume_token (parser
->lexer
);
9833 /* If this is going to be an abstract declarator, we're
9834 in a declarator and we can't have default args. */
9835 parser
->default_arg_ok_p
= false;
9836 parser
->in_declarator_p
= true;
9839 /* Parse the parameter-declaration-clause. */
9840 params
= cp_parser_parameter_declaration_clause (parser
);
9842 /* If all went well, parse the cv-qualifier-seq and the
9843 exception-specification. */
9844 if (cp_parser_parse_definitely (parser
))
9847 tree exception_specification
;
9850 /* Consume the `)'. */
9851 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
9853 /* Parse the cv-qualifier-seq. */
9854 cv_qualifiers
= cp_parser_cv_qualifier_seq_opt (parser
);
9855 /* And the exception-specification. */
9856 exception_specification
9857 = cp_parser_exception_specification_opt (parser
);
9859 /* Create the function-declarator. */
9860 declarator
= make_call_declarator (declarator
,
9863 exception_specification
);
9864 /* Any subsequent parameter lists are to do with
9865 return type, so are not those of the declared
9867 parser
->default_arg_ok_p
= false;
9869 /* Repeat the main loop. */
9874 /* If this is the first, we can try a parenthesized
9878 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
9879 parser
->in_declarator_p
= saved_in_declarator_p
;
9881 /* Consume the `('. */
9882 cp_lexer_consume_token (parser
->lexer
);
9883 /* Parse the nested declarator. */
9885 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
);
9888 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
9889 declarator
= error_mark_node
;
9890 if (declarator
== error_mark_node
)
9893 goto handle_declarator
;
9895 /* Otherwise, we must be done. */
9899 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
9900 && token
->type
== CPP_OPEN_SQUARE
)
9902 /* Parse an array-declarator. */
9906 parser
->default_arg_ok_p
= false;
9907 parser
->in_declarator_p
= true;
9908 /* Consume the `['. */
9909 cp_lexer_consume_token (parser
->lexer
);
9910 /* Peek at the next token. */
9911 token
= cp_lexer_peek_token (parser
->lexer
);
9912 /* If the next token is `]', then there is no
9913 constant-expression. */
9914 if (token
->type
!= CPP_CLOSE_SQUARE
)
9916 bool non_constant_p
;
9919 = cp_parser_constant_expression (parser
,
9920 /*allow_non_constant=*/true,
9922 /* If we're in a template, but the constant-expression
9923 isn't value dependent, simplify it. We're supposed
9926 template <typename T> void f(T[1 + 1]);
9927 template <typename T> void f(T[2]);
9929 as two declarations of the same function, for
9931 if (processing_template_decl
9933 && !value_dependent_expression_p (bounds
))
9935 HOST_WIDE_INT saved_processing_template_decl
;
9937 saved_processing_template_decl
= processing_template_decl
;
9938 processing_template_decl
= 0;
9939 bounds
= tsubst_copy_and_build (bounds
,
9942 /*in_decl=*/NULL_TREE
);
9943 processing_template_decl
= saved_processing_template_decl
;
9948 /* Look for the closing `]'. */
9949 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
9951 declarator
= error_mark_node
;
9955 declarator
= build_nt (ARRAY_REF
, declarator
, bounds
);
9957 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
9959 /* Parse a declarator_id */
9960 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
9961 cp_parser_parse_tentatively (parser
);
9962 declarator
= cp_parser_declarator_id (parser
);
9963 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
9965 if (!cp_parser_parse_definitely (parser
))
9966 declarator
= error_mark_node
;
9967 else if (TREE_CODE (declarator
) != IDENTIFIER_NODE
)
9969 cp_parser_error (parser
, "expected unqualified-id");
9970 declarator
= error_mark_node
;
9974 if (declarator
== error_mark_node
)
9977 if (TREE_CODE (declarator
) == SCOPE_REF
)
9979 tree scope
= TREE_OPERAND (declarator
, 0);
9981 /* In the declaration of a member of a template class
9982 outside of the class itself, the SCOPE will sometimes
9983 be a TYPENAME_TYPE. For example, given:
9985 template <typename T>
9988 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9989 this context, we must resolve S<T>::R to an ordinary
9990 type, rather than a typename type.
9992 The reason we normally avoid resolving TYPENAME_TYPEs
9993 is that a specialization of `S' might render
9994 `S<T>::R' not a type. However, if `S' is
9995 specialized, then this `i' will not be used, so there
9996 is no harm in resolving the types here. */
9997 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
10001 /* Resolve the TYPENAME_TYPE. */
10002 type
= resolve_typename_type (scope
,
10003 /*only_current_p=*/false);
10004 /* If that failed, the declarator is invalid. */
10005 if (type
!= error_mark_node
)
10007 /* Build a new DECLARATOR. */
10008 declarator
= build_nt (SCOPE_REF
,
10010 TREE_OPERAND (declarator
, 1));
10014 /* Check to see whether the declarator-id names a constructor,
10015 destructor, or conversion. */
10016 if (declarator
&& ctor_dtor_or_conv_p
10017 && ((TREE_CODE (declarator
) == SCOPE_REF
10018 && CLASS_TYPE_P (TREE_OPERAND (declarator
, 0)))
10019 || (TREE_CODE (declarator
) != SCOPE_REF
10020 && at_class_scope_p ())))
10022 tree unqualified_name
;
10025 /* Get the unqualified part of the name. */
10026 if (TREE_CODE (declarator
) == SCOPE_REF
)
10028 class_type
= TREE_OPERAND (declarator
, 0);
10029 unqualified_name
= TREE_OPERAND (declarator
, 1);
10033 class_type
= current_class_type
;
10034 unqualified_name
= declarator
;
10037 /* See if it names ctor, dtor or conv. */
10038 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
10039 || IDENTIFIER_TYPENAME_P (unqualified_name
)
10040 || constructor_name_p (unqualified_name
, class_type
))
10041 *ctor_dtor_or_conv_p
= true;
10044 handle_declarator
:;
10045 scope
= get_scope_of_declarator (declarator
);
10047 /* Any names that appear after the declarator-id for a member
10048 are looked up in the containing scope. */
10049 push_scope (scope
);
10050 parser
->in_declarator_p
= true;
10051 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
10053 && (TREE_CODE (declarator
) == SCOPE_REF
10054 || TREE_CODE (declarator
) == IDENTIFIER_NODE
)))
10055 /* Default args are only allowed on function
10057 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10059 parser
->default_arg_ok_p
= false;
10068 /* For an abstract declarator, we might wind up with nothing at this
10069 point. That's an error; the declarator is not optional. */
10071 cp_parser_error (parser
, "expected declarator");
10073 /* If we entered a scope, we must exit it now. */
10077 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10078 parser
->in_declarator_p
= saved_in_declarator_p
;
10083 /* Parse a ptr-operator.
10086 * cv-qualifier-seq [opt]
10088 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10093 & cv-qualifier-seq [opt]
10095 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10096 used. Returns ADDR_EXPR if a reference was used. In the
10097 case of a pointer-to-member, *TYPE is filled in with the
10098 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10099 with the cv-qualifier-seq, or NULL_TREE, if there are no
10100 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10102 static enum tree_code
10103 cp_parser_ptr_operator (cp_parser
* parser
,
10105 tree
* cv_qualifier_seq
)
10107 enum tree_code code
= ERROR_MARK
;
10110 /* Assume that it's not a pointer-to-member. */
10112 /* And that there are no cv-qualifiers. */
10113 *cv_qualifier_seq
= NULL_TREE
;
10115 /* Peek at the next token. */
10116 token
= cp_lexer_peek_token (parser
->lexer
);
10117 /* If it's a `*' or `&' we have a pointer or reference. */
10118 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
10120 /* Remember which ptr-operator we were processing. */
10121 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
10123 /* Consume the `*' or `&'. */
10124 cp_lexer_consume_token (parser
->lexer
);
10126 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10127 `&', if we are allowing GNU extensions. (The only qualifier
10128 that can legally appear after `&' is `restrict', but that is
10129 enforced during semantic analysis. */
10130 if (code
== INDIRECT_REF
10131 || cp_parser_allow_gnu_extensions_p (parser
))
10132 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10136 /* Try the pointer-to-member case. */
10137 cp_parser_parse_tentatively (parser
);
10138 /* Look for the optional `::' operator. */
10139 cp_parser_global_scope_opt (parser
,
10140 /*current_scope_valid_p=*/false);
10141 /* Look for the nested-name specifier. */
10142 cp_parser_nested_name_specifier (parser
,
10143 /*typename_keyword_p=*/false,
10144 /*check_dependency_p=*/true,
10146 /* If we found it, and the next token is a `*', then we are
10147 indeed looking at a pointer-to-member operator. */
10148 if (!cp_parser_error_occurred (parser
)
10149 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
10151 /* The type of which the member is a member is given by the
10153 *type
= parser
->scope
;
10154 /* The next name will not be qualified. */
10155 parser
->scope
= NULL_TREE
;
10156 parser
->qualifying_scope
= NULL_TREE
;
10157 parser
->object_scope
= NULL_TREE
;
10158 /* Indicate that the `*' operator was used. */
10159 code
= INDIRECT_REF
;
10160 /* Look for the optional cv-qualifier-seq. */
10161 *cv_qualifier_seq
= cp_parser_cv_qualifier_seq_opt (parser
);
10163 /* If that didn't work we don't have a ptr-operator. */
10164 if (!cp_parser_parse_definitely (parser
))
10165 cp_parser_error (parser
, "expected ptr-operator");
10171 /* Parse an (optional) cv-qualifier-seq.
10174 cv-qualifier cv-qualifier-seq [opt]
10176 Returns a TREE_LIST. The TREE_VALUE of each node is the
10177 representation of a cv-qualifier. */
10180 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
10182 tree cv_qualifiers
= NULL_TREE
;
10188 /* Look for the next cv-qualifier. */
10189 cv_qualifier
= cp_parser_cv_qualifier_opt (parser
);
10190 /* If we didn't find one, we're done. */
10194 /* Add this cv-qualifier to the list. */
10196 = tree_cons (NULL_TREE
, cv_qualifier
, cv_qualifiers
);
10199 /* We built up the list in reverse order. */
10200 return nreverse (cv_qualifiers
);
10203 /* Parse an (optional) cv-qualifier.
10215 cp_parser_cv_qualifier_opt (cp_parser
* parser
)
10218 tree cv_qualifier
= NULL_TREE
;
10220 /* Peek at the next token. */
10221 token
= cp_lexer_peek_token (parser
->lexer
);
10222 /* See if it's a cv-qualifier. */
10223 switch (token
->keyword
)
10228 /* Save the value of the token. */
10229 cv_qualifier
= token
->value
;
10230 /* Consume the token. */
10231 cp_lexer_consume_token (parser
->lexer
);
10238 return cv_qualifier
;
10241 /* Parse a declarator-id.
10245 :: [opt] nested-name-specifier [opt] type-name
10247 In the `id-expression' case, the value returned is as for
10248 cp_parser_id_expression if the id-expression was an unqualified-id.
10249 If the id-expression was a qualified-id, then a SCOPE_REF is
10250 returned. The first operand is the scope (either a NAMESPACE_DECL
10251 or TREE_TYPE), but the second is still just a representation of an
10255 cp_parser_declarator_id (cp_parser
* parser
)
10257 tree id_expression
;
10259 /* The expression must be an id-expression. Assume that qualified
10260 names are the names of types so that:
10263 int S<T>::R::i = 3;
10265 will work; we must treat `S<T>::R' as the name of a type.
10266 Similarly, assume that qualified names are templates, where
10270 int S<T>::R<T>::i = 3;
10273 id_expression
= cp_parser_id_expression (parser
,
10274 /*template_keyword_p=*/false,
10275 /*check_dependency_p=*/false,
10276 /*template_p=*/NULL
);
10277 /* If the name was qualified, create a SCOPE_REF to represent
10281 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
10282 parser
->scope
= NULL_TREE
;
10285 return id_expression
;
10288 /* Parse a type-id.
10291 type-specifier-seq abstract-declarator [opt]
10293 Returns the TYPE specified. */
10296 cp_parser_type_id (cp_parser
* parser
)
10298 tree type_specifier_seq
;
10299 tree abstract_declarator
;
10301 /* Parse the type-specifier-seq. */
10303 = cp_parser_type_specifier_seq (parser
);
10304 if (type_specifier_seq
== error_mark_node
)
10305 return error_mark_node
;
10307 /* There might or might not be an abstract declarator. */
10308 cp_parser_parse_tentatively (parser
);
10309 /* Look for the declarator. */
10310 abstract_declarator
10311 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
);
10312 /* Check to see if there really was a declarator. */
10313 if (!cp_parser_parse_definitely (parser
))
10314 abstract_declarator
= NULL_TREE
;
10316 return groktypename (build_tree_list (type_specifier_seq
,
10317 abstract_declarator
));
10320 /* Parse a type-specifier-seq.
10322 type-specifier-seq:
10323 type-specifier type-specifier-seq [opt]
10327 type-specifier-seq:
10328 attributes type-specifier-seq [opt]
10330 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10331 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10334 cp_parser_type_specifier_seq (cp_parser
* parser
)
10336 bool seen_type_specifier
= false;
10337 tree type_specifier_seq
= NULL_TREE
;
10339 /* Parse the type-specifiers and attributes. */
10342 tree type_specifier
;
10344 /* Check for attributes first. */
10345 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
10347 type_specifier_seq
= tree_cons (cp_parser_attributes_opt (parser
),
10349 type_specifier_seq
);
10353 /* After the first type-specifier, others are optional. */
10354 if (seen_type_specifier
)
10355 cp_parser_parse_tentatively (parser
);
10356 /* Look for the type-specifier. */
10357 type_specifier
= cp_parser_type_specifier (parser
,
10358 CP_PARSER_FLAGS_NONE
,
10359 /*is_friend=*/false,
10360 /*is_declaration=*/false,
10363 /* If the first type-specifier could not be found, this is not a
10364 type-specifier-seq at all. */
10365 if (!seen_type_specifier
&& type_specifier
== error_mark_node
)
10366 return error_mark_node
;
10367 /* If subsequent type-specifiers could not be found, the
10368 type-specifier-seq is complete. */
10369 else if (seen_type_specifier
&& !cp_parser_parse_definitely (parser
))
10372 /* Add the new type-specifier to the list. */
10374 = tree_cons (NULL_TREE
, type_specifier
, type_specifier_seq
);
10375 seen_type_specifier
= true;
10378 /* We built up the list in reverse order. */
10379 return nreverse (type_specifier_seq
);
10382 /* Parse a parameter-declaration-clause.
10384 parameter-declaration-clause:
10385 parameter-declaration-list [opt] ... [opt]
10386 parameter-declaration-list , ...
10388 Returns a representation for the parameter declarations. Each node
10389 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10390 representation.) If the parameter-declaration-clause ends with an
10391 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10392 list. A return value of NULL_TREE indicates a
10393 parameter-declaration-clause consisting only of an ellipsis. */
10396 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
10402 /* Peek at the next token. */
10403 token
= cp_lexer_peek_token (parser
->lexer
);
10404 /* Check for trivial parameter-declaration-clauses. */
10405 if (token
->type
== CPP_ELLIPSIS
)
10407 /* Consume the `...' token. */
10408 cp_lexer_consume_token (parser
->lexer
);
10411 else if (token
->type
== CPP_CLOSE_PAREN
)
10412 /* There are no parameters. */
10414 #ifndef NO_IMPLICIT_EXTERN_C
10415 if (in_system_header
&& current_class_type
== NULL
10416 && current_lang_name
== lang_name_c
)
10420 return void_list_node
;
10422 /* Check for `(void)', too, which is a special case. */
10423 else if (token
->keyword
== RID_VOID
10424 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
10425 == CPP_CLOSE_PAREN
))
10427 /* Consume the `void' token. */
10428 cp_lexer_consume_token (parser
->lexer
);
10429 /* There are no parameters. */
10430 return void_list_node
;
10433 /* Parse the parameter-declaration-list. */
10434 parameters
= cp_parser_parameter_declaration_list (parser
);
10435 /* If a parse error occurred while parsing the
10436 parameter-declaration-list, then the entire
10437 parameter-declaration-clause is erroneous. */
10438 if (parameters
== error_mark_node
)
10439 return error_mark_node
;
10441 /* Peek at the next token. */
10442 token
= cp_lexer_peek_token (parser
->lexer
);
10443 /* If it's a `,', the clause should terminate with an ellipsis. */
10444 if (token
->type
== CPP_COMMA
)
10446 /* Consume the `,'. */
10447 cp_lexer_consume_token (parser
->lexer
);
10448 /* Expect an ellipsis. */
10450 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
10452 /* It might also be `...' if the optional trailing `,' was
10454 else if (token
->type
== CPP_ELLIPSIS
)
10456 /* Consume the `...' token. */
10457 cp_lexer_consume_token (parser
->lexer
);
10458 /* And remember that we saw it. */
10462 ellipsis_p
= false;
10464 /* Finish the parameter list. */
10465 return finish_parmlist (parameters
, ellipsis_p
);
10468 /* Parse a parameter-declaration-list.
10470 parameter-declaration-list:
10471 parameter-declaration
10472 parameter-declaration-list , parameter-declaration
10474 Returns a representation of the parameter-declaration-list, as for
10475 cp_parser_parameter_declaration_clause. However, the
10476 `void_list_node' is never appended to the list. */
10479 cp_parser_parameter_declaration_list (cp_parser
* parser
)
10481 tree parameters
= NULL_TREE
;
10483 /* Look for more parameters. */
10487 /* Parse the parameter. */
10489 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/false);
10491 /* If a parse error occurred parsing the parameter declaration,
10492 then the entire parameter-declaration-list is erroneous. */
10493 if (parameter
== error_mark_node
)
10495 parameters
= error_mark_node
;
10498 /* Add the new parameter to the list. */
10499 TREE_CHAIN (parameter
) = parameters
;
10500 parameters
= parameter
;
10502 /* Peek at the next token. */
10503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
10504 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10505 /* The parameter-declaration-list is complete. */
10507 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10511 /* Peek at the next token. */
10512 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10513 /* If it's an ellipsis, then the list is complete. */
10514 if (token
->type
== CPP_ELLIPSIS
)
10516 /* Otherwise, there must be more parameters. Consume the
10518 cp_lexer_consume_token (parser
->lexer
);
10522 cp_parser_error (parser
, "expected `,' or `...'");
10527 /* We built up the list in reverse order; straighten it out now. */
10528 return nreverse (parameters
);
10531 /* Parse a parameter declaration.
10533 parameter-declaration:
10534 decl-specifier-seq declarator
10535 decl-specifier-seq declarator = assignment-expression
10536 decl-specifier-seq abstract-declarator [opt]
10537 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10539 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10540 declares a template parameter. (In that case, a non-nested `>'
10541 token encountered during the parsing of the assignment-expression
10542 is not interpreted as a greater-than operator.)
10544 Returns a TREE_LIST representing the parameter-declaration. The
10545 TREE_VALUE is a representation of the decl-specifier-seq and
10546 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10547 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10548 TREE_VALUE represents the declarator. */
10551 cp_parser_parameter_declaration (cp_parser
*parser
,
10552 bool template_parm_p
)
10554 bool declares_class_or_enum
;
10555 bool greater_than_is_operator_p
;
10556 tree decl_specifiers
;
10559 tree default_argument
;
10562 const char *saved_message
;
10564 /* In a template parameter, `>' is not an operator.
10568 When parsing a default template-argument for a non-type
10569 template-parameter, the first non-nested `>' is taken as the end
10570 of the template parameter-list rather than a greater-than
10572 greater_than_is_operator_p
= !template_parm_p
;
10574 /* Type definitions may not appear in parameter types. */
10575 saved_message
= parser
->type_definition_forbidden_message
;
10576 parser
->type_definition_forbidden_message
10577 = "types may not be defined in parameter types";
10579 /* Parse the declaration-specifiers. */
10581 = cp_parser_decl_specifier_seq (parser
,
10582 CP_PARSER_FLAGS_NONE
,
10584 &declares_class_or_enum
);
10585 /* If an error occurred, there's no reason to attempt to parse the
10586 rest of the declaration. */
10587 if (cp_parser_error_occurred (parser
))
10589 parser
->type_definition_forbidden_message
= saved_message
;
10590 return error_mark_node
;
10593 /* Peek at the next token. */
10594 token
= cp_lexer_peek_token (parser
->lexer
);
10595 /* If the next token is a `)', `,', `=', `>', or `...', then there
10596 is no declarator. */
10597 if (token
->type
== CPP_CLOSE_PAREN
10598 || token
->type
== CPP_COMMA
10599 || token
->type
== CPP_EQ
10600 || token
->type
== CPP_ELLIPSIS
10601 || token
->type
== CPP_GREATER
)
10602 declarator
= NULL_TREE
;
10603 /* Otherwise, there should be a declarator. */
10606 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10607 parser
->default_arg_ok_p
= false;
10609 declarator
= cp_parser_declarator (parser
,
10610 CP_PARSER_DECLARATOR_EITHER
,
10611 /*ctor_dtor_or_conv_p=*/NULL
);
10612 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10613 /* After the declarator, allow more attributes. */
10614 attributes
= chainon (attributes
, cp_parser_attributes_opt (parser
));
10617 /* The restriction on defining new types applies only to the type
10618 of the parameter, not to the default argument. */
10619 parser
->type_definition_forbidden_message
= saved_message
;
10621 /* If the next token is `=', then process a default argument. */
10622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10624 bool saved_greater_than_is_operator_p
;
10625 /* Consume the `='. */
10626 cp_lexer_consume_token (parser
->lexer
);
10628 /* If we are defining a class, then the tokens that make up the
10629 default argument must be saved and processed later. */
10630 if (!template_parm_p
&& at_class_scope_p ()
10631 && TYPE_BEING_DEFINED (current_class_type
))
10633 unsigned depth
= 0;
10635 /* Create a DEFAULT_ARG to represented the unparsed default
10637 default_argument
= make_node (DEFAULT_ARG
);
10638 DEFARG_TOKENS (default_argument
) = cp_token_cache_new ();
10640 /* Add tokens until we have processed the entire default
10647 /* Peek at the next token. */
10648 token
= cp_lexer_peek_token (parser
->lexer
);
10649 /* What we do depends on what token we have. */
10650 switch (token
->type
)
10652 /* In valid code, a default argument must be
10653 immediately followed by a `,' `)', or `...'. */
10655 case CPP_CLOSE_PAREN
:
10657 /* If we run into a non-nested `;', `}', or `]',
10658 then the code is invalid -- but the default
10659 argument is certainly over. */
10660 case CPP_SEMICOLON
:
10661 case CPP_CLOSE_BRACE
:
10662 case CPP_CLOSE_SQUARE
:
10665 /* Update DEPTH, if necessary. */
10666 else if (token
->type
== CPP_CLOSE_PAREN
10667 || token
->type
== CPP_CLOSE_BRACE
10668 || token
->type
== CPP_CLOSE_SQUARE
)
10672 case CPP_OPEN_PAREN
:
10673 case CPP_OPEN_SQUARE
:
10674 case CPP_OPEN_BRACE
:
10679 /* If we see a non-nested `>', and `>' is not an
10680 operator, then it marks the end of the default
10682 if (!depth
&& !greater_than_is_operator_p
)
10686 /* If we run out of tokens, issue an error message. */
10688 error ("file ends in default argument");
10694 /* In these cases, we should look for template-ids.
10695 For example, if the default argument is
10696 `X<int, double>()', we need to do name lookup to
10697 figure out whether or not `X' is a template; if
10698 so, the `,' does not end the default argument.
10700 That is not yet done. */
10707 /* If we've reached the end, stop. */
10711 /* Add the token to the token block. */
10712 token
= cp_lexer_consume_token (parser
->lexer
);
10713 cp_token_cache_push_token (DEFARG_TOKENS (default_argument
),
10717 /* Outside of a class definition, we can just parse the
10718 assignment-expression. */
10721 bool saved_local_variables_forbidden_p
;
10723 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10725 saved_greater_than_is_operator_p
10726 = parser
->greater_than_is_operator_p
;
10727 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
10728 /* Local variable names (and the `this' keyword) may not
10729 appear in a default argument. */
10730 saved_local_variables_forbidden_p
10731 = parser
->local_variables_forbidden_p
;
10732 parser
->local_variables_forbidden_p
= true;
10733 /* Parse the assignment-expression. */
10734 default_argument
= cp_parser_assignment_expression (parser
);
10735 /* Restore saved state. */
10736 parser
->greater_than_is_operator_p
10737 = saved_greater_than_is_operator_p
;
10738 parser
->local_variables_forbidden_p
10739 = saved_local_variables_forbidden_p
;
10741 if (!parser
->default_arg_ok_p
)
10743 pedwarn ("default arguments are only permitted on functions");
10744 if (flag_pedantic_errors
)
10745 default_argument
= NULL_TREE
;
10749 default_argument
= NULL_TREE
;
10751 /* Create the representation of the parameter. */
10753 decl_specifiers
= tree_cons (attributes
, NULL_TREE
, decl_specifiers
);
10754 parameter
= build_tree_list (default_argument
,
10755 build_tree_list (decl_specifiers
,
10761 /* Parse a function-definition.
10763 function-definition:
10764 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10766 decl-specifier-seq [opt] declarator function-try-block
10770 function-definition:
10771 __extension__ function-definition
10773 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10774 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10778 cp_parser_function_definition (cp_parser
* parser
, bool* friend_p
)
10780 tree decl_specifiers
;
10785 bool declares_class_or_enum
;
10787 /* The saved value of the PEDANTIC flag. */
10788 int saved_pedantic
;
10790 /* Any pending qualification must be cleared by our caller. It is
10791 more robust to force the callers to clear PARSER->SCOPE than to
10792 do it here since if the qualification is in effect here, it might
10793 also end up in effect elsewhere that it is not intended. */
10794 my_friendly_assert (!parser
->scope
, 20010821);
10796 /* Handle `__extension__'. */
10797 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10799 /* Parse the function-definition. */
10800 fn
= cp_parser_function_definition (parser
, friend_p
);
10801 /* Restore the PEDANTIC flag. */
10802 pedantic
= saved_pedantic
;
10807 /* Check to see if this definition appears in a class-specifier. */
10808 member_p
= (at_class_scope_p ()
10809 && TYPE_BEING_DEFINED (current_class_type
));
10810 /* Defer access checks in the decl-specifier-seq until we know what
10811 function is being defined. There is no need to do this for the
10812 definition of member functions; we cannot be defining a member
10813 from another class. */
10814 push_deferring_access_checks (member_p
? dk_no_check
: dk_deferred
);
10816 /* Parse the decl-specifier-seq. */
10818 = cp_parser_decl_specifier_seq (parser
,
10819 CP_PARSER_FLAGS_OPTIONAL
,
10821 &declares_class_or_enum
);
10822 /* Figure out whether this declaration is a `friend'. */
10824 *friend_p
= cp_parser_friend_p (decl_specifiers
);
10826 /* Parse the declarator. */
10827 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10828 /*ctor_dtor_or_conv_p=*/NULL
);
10830 /* Gather up any access checks that occurred. */
10831 stop_deferring_access_checks ();
10833 /* If something has already gone wrong, we may as well stop now. */
10834 if (declarator
== error_mark_node
)
10836 /* Skip to the end of the function, or if this wasn't anything
10837 like a function-definition, to a `;' in the hopes of finding
10838 a sensible place from which to continue parsing. */
10839 cp_parser_skip_to_end_of_block_or_statement (parser
);
10840 pop_deferring_access_checks ();
10841 return error_mark_node
;
10844 /* The next character should be a `{' (for a simple function
10845 definition), a `:' (for a ctor-initializer), or `try' (for a
10846 function-try block). */
10847 token
= cp_lexer_peek_token (parser
->lexer
);
10848 if (!cp_parser_token_starts_function_definition_p (token
))
10850 /* Issue the error-message. */
10851 cp_parser_error (parser
, "expected function-definition");
10852 /* Skip to the next `;'. */
10853 cp_parser_skip_to_end_of_block_or_statement (parser
);
10855 pop_deferring_access_checks ();
10856 return error_mark_node
;
10859 /* If we are in a class scope, then we must handle
10860 function-definitions specially. In particular, we save away the
10861 tokens that make up the function body, and parse them again
10862 later, in order to handle code like:
10865 int f () { return i; }
10869 Here, we cannot parse the body of `f' until after we have seen
10870 the declaration of `i'. */
10873 cp_token_cache
*cache
;
10875 /* Create the function-declaration. */
10876 fn
= start_method (decl_specifiers
, declarator
, attributes
);
10877 /* If something went badly wrong, bail out now. */
10878 if (fn
== error_mark_node
)
10880 /* If there's a function-body, skip it. */
10881 if (cp_parser_token_starts_function_definition_p
10882 (cp_lexer_peek_token (parser
->lexer
)))
10883 cp_parser_skip_to_end_of_block_or_statement (parser
);
10884 pop_deferring_access_checks ();
10885 return error_mark_node
;
10888 /* Remember it, if there default args to post process. */
10889 cp_parser_save_default_args (parser
, fn
);
10891 /* Create a token cache. */
10892 cache
= cp_token_cache_new ();
10893 /* Save away the tokens that make up the body of the
10895 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
10896 /* Handle function try blocks. */
10897 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
10898 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, /*depth=*/0);
10900 /* Save away the inline definition; we will process it when the
10901 class is complete. */
10902 DECL_PENDING_INLINE_INFO (fn
) = cache
;
10903 DECL_PENDING_INLINE_P (fn
) = 1;
10905 /* We need to know that this was defined in the class, so that
10906 friend templates are handled correctly. */
10907 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
10909 /* We're done with the inline definition. */
10910 finish_method (fn
);
10912 /* Add FN to the queue of functions to be parsed later. */
10913 TREE_VALUE (parser
->unparsed_functions_queues
)
10914 = tree_cons (NULL_TREE
, fn
,
10915 TREE_VALUE (parser
->unparsed_functions_queues
));
10917 pop_deferring_access_checks ();
10921 /* Check that the number of template-parameter-lists is OK. */
10922 if (!cp_parser_check_declarator_template_parameters (parser
,
10925 cp_parser_skip_to_end_of_block_or_statement (parser
);
10926 pop_deferring_access_checks ();
10927 return error_mark_node
;
10930 fn
= cp_parser_function_definition_from_specifiers_and_declarator
10931 (parser
, decl_specifiers
, attributes
, declarator
);
10932 pop_deferring_access_checks ();
10936 /* Parse a function-body.
10939 compound_statement */
10942 cp_parser_function_body (cp_parser
*parser
)
10944 cp_parser_compound_statement (parser
);
10947 /* Parse a ctor-initializer-opt followed by a function-body. Return
10948 true if a ctor-initializer was present. */
10951 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
10954 bool ctor_initializer_p
;
10956 /* Begin the function body. */
10957 body
= begin_function_body ();
10958 /* Parse the optional ctor-initializer. */
10959 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
10960 /* Parse the function-body. */
10961 cp_parser_function_body (parser
);
10962 /* Finish the function body. */
10963 finish_function_body (body
);
10965 return ctor_initializer_p
;
10968 /* Parse an initializer.
10971 = initializer-clause
10972 ( expression-list )
10974 Returns a expression representing the initializer. If no
10975 initializer is present, NULL_TREE is returned.
10977 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10978 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
10979 set to FALSE if there is no initializer present. */
10982 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
)
10987 /* Peek at the next token. */
10988 token
= cp_lexer_peek_token (parser
->lexer
);
10990 /* Let our caller know whether or not this initializer was
10992 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
10994 if (token
->type
== CPP_EQ
)
10996 /* Consume the `='. */
10997 cp_lexer_consume_token (parser
->lexer
);
10998 /* Parse the initializer-clause. */
10999 init
= cp_parser_initializer_clause (parser
);
11001 else if (token
->type
== CPP_OPEN_PAREN
)
11003 /* Consume the `('. */
11004 cp_lexer_consume_token (parser
->lexer
);
11005 /* Parse the expression-list. */
11006 init
= cp_parser_expression_list (parser
);
11007 /* Consume the `)' token. */
11008 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11009 cp_parser_skip_to_closing_parenthesis (parser
);
11013 /* Anything else is an error. */
11014 cp_parser_error (parser
, "expected initializer");
11015 init
= error_mark_node
;
11021 /* Parse an initializer-clause.
11023 initializer-clause:
11024 assignment-expression
11025 { initializer-list , [opt] }
11028 Returns an expression representing the initializer.
11030 If the `assignment-expression' production is used the value
11031 returned is simply a representation for the expression.
11033 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11034 the elements of the initializer-list (or NULL_TREE, if the last
11035 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11036 NULL_TREE. There is no way to detect whether or not the optional
11037 trailing `,' was provided. */
11040 cp_parser_initializer_clause (cp_parser
* parser
)
11044 /* If it is not a `{', then we are looking at an
11045 assignment-expression. */
11046 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11047 initializer
= cp_parser_assignment_expression (parser
);
11050 /* Consume the `{' token. */
11051 cp_lexer_consume_token (parser
->lexer
);
11052 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11053 initializer
= make_node (CONSTRUCTOR
);
11054 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11055 necessary, but check_initializer depends upon it, for
11057 TREE_HAS_CONSTRUCTOR (initializer
) = 1;
11058 /* If it's not a `}', then there is a non-trivial initializer. */
11059 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11061 /* Parse the initializer list. */
11062 CONSTRUCTOR_ELTS (initializer
)
11063 = cp_parser_initializer_list (parser
);
11064 /* A trailing `,' token is allowed. */
11065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11066 cp_lexer_consume_token (parser
->lexer
);
11069 /* Now, there should be a trailing `}'. */
11070 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11073 return initializer
;
11076 /* Parse an initializer-list.
11080 initializer-list , initializer-clause
11085 identifier : initializer-clause
11086 initializer-list, identifier : initializer-clause
11088 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11089 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11090 IDENTIFIER_NODE naming the field to initialize. */
11093 cp_parser_initializer_list (cp_parser
* parser
)
11095 tree initializers
= NULL_TREE
;
11097 /* Parse the rest of the list. */
11104 /* If the next token is an identifier and the following one is a
11105 colon, we are looking at the GNU designated-initializer
11107 if (cp_parser_allow_gnu_extensions_p (parser
)
11108 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
11109 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
11111 /* Consume the identifier. */
11112 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
11113 /* Consume the `:'. */
11114 cp_lexer_consume_token (parser
->lexer
);
11117 identifier
= NULL_TREE
;
11119 /* Parse the initializer. */
11120 initializer
= cp_parser_initializer_clause (parser
);
11122 /* Add it to the list. */
11123 initializers
= tree_cons (identifier
, initializer
, initializers
);
11125 /* If the next token is not a comma, we have reached the end of
11127 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11130 /* Peek at the next token. */
11131 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11132 /* If the next token is a `}', then we're still done. An
11133 initializer-clause can have a trailing `,' after the
11134 initializer-list and before the closing `}'. */
11135 if (token
->type
== CPP_CLOSE_BRACE
)
11138 /* Consume the `,' token. */
11139 cp_lexer_consume_token (parser
->lexer
);
11142 /* The initializers were built up in reverse order, so we need to
11143 reverse them now. */
11144 return nreverse (initializers
);
11147 /* Classes [gram.class] */
11149 /* Parse a class-name.
11155 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11156 to indicate that names looked up in dependent types should be
11157 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11158 keyword has been used to indicate that the name that appears next
11159 is a template. TYPE_P is true iff the next name should be treated
11160 as class-name, even if it is declared to be some other kind of name
11161 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11162 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11163 being defined in a class-head.
11165 Returns the TYPE_DECL representing the class. */
11168 cp_parser_class_name (cp_parser
*parser
,
11169 bool typename_keyword_p
,
11170 bool template_keyword_p
,
11172 bool check_dependency_p
,
11180 /* All class-names start with an identifier. */
11181 token
= cp_lexer_peek_token (parser
->lexer
);
11182 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
11184 cp_parser_error (parser
, "expected class-name");
11185 return error_mark_node
;
11188 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11189 to a template-id, so we save it here. */
11190 scope
= parser
->scope
;
11191 /* Any name names a type if we're following the `typename' keyword
11192 in a qualified name where the enclosing scope is type-dependent. */
11193 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
11194 && dependent_type_p (scope
));
11195 /* Handle the common case (an identifier, but not a template-id)
11197 if (token
->type
== CPP_NAME
11198 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_LESS
)
11202 /* Look for the identifier. */
11203 identifier
= cp_parser_identifier (parser
);
11204 /* If the next token isn't an identifier, we are certainly not
11205 looking at a class-name. */
11206 if (identifier
== error_mark_node
)
11207 decl
= error_mark_node
;
11208 /* If we know this is a type-name, there's no need to look it
11210 else if (typename_p
)
11214 /* If the next token is a `::', then the name must be a type
11217 [basic.lookup.qual]
11219 During the lookup for a name preceding the :: scope
11220 resolution operator, object, function, and enumerator
11221 names are ignored. */
11222 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11224 /* Look up the name. */
11225 decl
= cp_parser_lookup_name (parser
, identifier
,
11227 /*is_namespace=*/false,
11228 check_dependency_p
);
11233 /* Try a template-id. */
11234 decl
= cp_parser_template_id (parser
, template_keyword_p
,
11235 check_dependency_p
);
11236 if (decl
== error_mark_node
)
11237 return error_mark_node
;
11240 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
11242 /* If this is a typename, create a TYPENAME_TYPE. */
11243 if (typename_p
&& decl
!= error_mark_node
)
11244 decl
= TYPE_NAME (make_typename_type (scope
, decl
,
11247 /* Check to see that it is really the name of a class. */
11248 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11249 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
11250 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11251 /* Situations like this:
11253 template <typename T> struct A {
11254 typename T::template X<int>::I i;
11257 are problematic. Is `T::template X<int>' a class-name? The
11258 standard does not seem to be definitive, but there is no other
11259 valid interpretation of the following `::'. Therefore, those
11260 names are considered class-names. */
11261 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tf_error
));
11262 else if (decl
== error_mark_node
11263 || TREE_CODE (decl
) != TYPE_DECL
11264 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
11266 cp_parser_error (parser
, "expected class-name");
11267 return error_mark_node
;
11273 /* Parse a class-specifier.
11276 class-head { member-specification [opt] }
11278 Returns the TREE_TYPE representing the class. */
11281 cp_parser_class_specifier (cp_parser
* parser
)
11285 tree attributes
= NULL_TREE
;
11286 int has_trailing_semicolon
;
11287 bool nested_name_specifier_p
;
11288 unsigned saved_num_template_parameter_lists
;
11290 push_deferring_access_checks (dk_no_deferred
);
11292 /* Parse the class-head. */
11293 type
= cp_parser_class_head (parser
,
11294 &nested_name_specifier_p
);
11295 /* If the class-head was a semantic disaster, skip the entire body
11299 cp_parser_skip_to_end_of_block_or_statement (parser
);
11300 pop_deferring_access_checks ();
11301 return error_mark_node
;
11304 /* Look for the `{'. */
11305 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
11307 pop_deferring_access_checks ();
11308 return error_mark_node
;
11311 /* Issue an error message if type-definitions are forbidden here. */
11312 cp_parser_check_type_definition (parser
);
11313 /* Remember that we are defining one more class. */
11314 ++parser
->num_classes_being_defined
;
11315 /* Inside the class, surrounding template-parameter-lists do not
11317 saved_num_template_parameter_lists
11318 = parser
->num_template_parameter_lists
;
11319 parser
->num_template_parameter_lists
= 0;
11321 /* Start the class. */
11322 type
= begin_class_definition (type
);
11323 if (type
== error_mark_node
)
11324 /* If the type is erroneous, skip the entire body of the class. */
11325 cp_parser_skip_to_closing_brace (parser
);
11327 /* Parse the member-specification. */
11328 cp_parser_member_specification_opt (parser
);
11329 /* Look for the trailing `}'. */
11330 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11331 /* We get better error messages by noticing a common problem: a
11332 missing trailing `;'. */
11333 token
= cp_lexer_peek_token (parser
->lexer
);
11334 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
11335 /* Look for attributes to apply to this class. */
11336 if (cp_parser_allow_gnu_extensions_p (parser
))
11337 attributes
= cp_parser_attributes_opt (parser
);
11338 /* Finish the class definition. */
11339 type
= finish_class_definition (type
,
11341 has_trailing_semicolon
,
11342 nested_name_specifier_p
);
11343 /* If this class is not itself within the scope of another class,
11344 then we need to parse the bodies of all of the queued function
11345 definitions. Note that the queued functions defined in a class
11346 are not always processed immediately following the
11347 class-specifier for that class. Consider:
11350 struct B { void f() { sizeof (A); } };
11353 If `f' were processed before the processing of `A' were
11354 completed, there would be no way to compute the size of `A'.
11355 Note that the nesting we are interested in here is lexical --
11356 not the semantic nesting given by TYPE_CONTEXT. In particular,
11359 struct A { struct B; };
11360 struct A::B { void f() { } };
11362 there is no need to delay the parsing of `A::B::f'. */
11363 if (--parser
->num_classes_being_defined
== 0)
11368 /* In a first pass, parse default arguments to the functions.
11369 Then, in a second pass, parse the bodies of the functions.
11370 This two-phased approach handles cases like:
11378 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
11379 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
11380 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
11381 TREE_PURPOSE (parser
->unparsed_functions_queues
)
11382 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
11384 fn
= TREE_VALUE (queue_entry
);
11385 /* Make sure that any template parameters are in scope. */
11386 maybe_begin_member_template_processing (fn
);
11387 /* If there are default arguments that have not yet been processed,
11388 take care of them now. */
11389 cp_parser_late_parsing_default_args (parser
, fn
);
11390 /* Remove any template parameters from the symbol table. */
11391 maybe_end_member_template_processing ();
11393 /* Now parse the body of the functions. */
11394 for (TREE_VALUE (parser
->unparsed_functions_queues
)
11395 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
11396 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
11397 TREE_VALUE (parser
->unparsed_functions_queues
)
11398 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
11400 /* Figure out which function we need to process. */
11401 fn
= TREE_VALUE (queue_entry
);
11403 /* Parse the function. */
11404 cp_parser_late_parsing_for_member (parser
, fn
);
11409 /* Put back any saved access checks. */
11410 pop_deferring_access_checks ();
11412 /* Restore the count of active template-parameter-lists. */
11413 parser
->num_template_parameter_lists
11414 = saved_num_template_parameter_lists
;
11419 /* Parse a class-head.
11422 class-key identifier [opt] base-clause [opt]
11423 class-key nested-name-specifier identifier base-clause [opt]
11424 class-key nested-name-specifier [opt] template-id
11428 class-key attributes identifier [opt] base-clause [opt]
11429 class-key attributes nested-name-specifier identifier base-clause [opt]
11430 class-key attributes nested-name-specifier [opt] template-id
11433 Returns the TYPE of the indicated class. Sets
11434 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11435 involving a nested-name-specifier was used, and FALSE otherwise.
11437 Returns NULL_TREE if the class-head is syntactically valid, but
11438 semantically invalid in a way that means we should skip the entire
11439 body of the class. */
11442 cp_parser_class_head (cp_parser
* parser
,
11443 bool* nested_name_specifier_p
)
11446 tree nested_name_specifier
;
11447 enum tag_types class_key
;
11448 tree id
= NULL_TREE
;
11449 tree type
= NULL_TREE
;
11451 bool template_id_p
= false;
11452 bool qualified_p
= false;
11453 bool invalid_nested_name_p
= false;
11454 unsigned num_templates
;
11456 /* Assume no nested-name-specifier will be present. */
11457 *nested_name_specifier_p
= false;
11458 /* Assume no template parameter lists will be used in defining the
11462 /* Look for the class-key. */
11463 class_key
= cp_parser_class_key (parser
);
11464 if (class_key
== none_type
)
11465 return error_mark_node
;
11467 /* Parse the attributes. */
11468 attributes
= cp_parser_attributes_opt (parser
);
11470 /* If the next token is `::', that is invalid -- but sometimes
11471 people do try to write:
11475 Handle this gracefully by accepting the extra qualifier, and then
11476 issuing an error about it later if this really is a
11477 class-head. If it turns out just to be an elaborated type
11478 specifier, remain silent. */
11479 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
11480 qualified_p
= true;
11482 push_deferring_access_checks (dk_no_check
);
11484 /* Determine the name of the class. Begin by looking for an
11485 optional nested-name-specifier. */
11486 nested_name_specifier
11487 = cp_parser_nested_name_specifier_opt (parser
,
11488 /*typename_keyword_p=*/false,
11489 /*check_dependency_p=*/false,
11491 /* If there was a nested-name-specifier, then there *must* be an
11493 if (nested_name_specifier
)
11495 /* Although the grammar says `identifier', it really means
11496 `class-name' or `template-name'. You are only allowed to
11497 define a class that has already been declared with this
11500 The proposed resolution for Core Issue 180 says that whever
11501 you see `class T::X' you should treat `X' as a type-name.
11503 It is OK to define an inaccessible class; for example:
11505 class A { class B; };
11508 We do not know if we will see a class-name, or a
11509 template-name. We look for a class-name first, in case the
11510 class-name is a template-id; if we looked for the
11511 template-name first we would stop after the template-name. */
11512 cp_parser_parse_tentatively (parser
);
11513 type
= cp_parser_class_name (parser
,
11514 /*typename_keyword_p=*/false,
11515 /*template_keyword_p=*/false,
11517 /*check_dependency_p=*/false,
11518 /*class_head_p=*/true);
11519 /* If that didn't work, ignore the nested-name-specifier. */
11520 if (!cp_parser_parse_definitely (parser
))
11522 invalid_nested_name_p
= true;
11523 id
= cp_parser_identifier (parser
);
11524 if (id
== error_mark_node
)
11527 /* If we could not find a corresponding TYPE, treat this
11528 declaration like an unqualified declaration. */
11529 if (type
== error_mark_node
)
11530 nested_name_specifier
= NULL_TREE
;
11531 /* Otherwise, count the number of templates used in TYPE and its
11532 containing scopes. */
11537 for (scope
= TREE_TYPE (type
);
11538 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
11539 scope
= (TYPE_P (scope
)
11540 ? TYPE_CONTEXT (scope
)
11541 : DECL_CONTEXT (scope
)))
11543 && CLASS_TYPE_P (scope
)
11544 && CLASSTYPE_TEMPLATE_INFO (scope
)
11545 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
11546 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
11550 /* Otherwise, the identifier is optional. */
11553 /* We don't know whether what comes next is a template-id,
11554 an identifier, or nothing at all. */
11555 cp_parser_parse_tentatively (parser
);
11556 /* Check for a template-id. */
11557 id
= cp_parser_template_id (parser
,
11558 /*template_keyword_p=*/false,
11559 /*check_dependency_p=*/true);
11560 /* If that didn't work, it could still be an identifier. */
11561 if (!cp_parser_parse_definitely (parser
))
11563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11564 id
= cp_parser_identifier (parser
);
11570 template_id_p
= true;
11575 pop_deferring_access_checks ();
11577 /* If it's not a `:' or a `{' then we can't really be looking at a
11578 class-head, since a class-head only appears as part of a
11579 class-specifier. We have to detect this situation before calling
11580 xref_tag, since that has irreversible side-effects. */
11581 if (!cp_parser_next_token_starts_class_definition_p (parser
))
11583 cp_parser_error (parser
, "expected `{' or `:'");
11584 return error_mark_node
;
11587 /* At this point, we're going ahead with the class-specifier, even
11588 if some other problem occurs. */
11589 cp_parser_commit_to_tentative_parse (parser
);
11590 /* Issue the error about the overly-qualified name now. */
11592 cp_parser_error (parser
,
11593 "global qualification of class name is invalid");
11594 else if (invalid_nested_name_p
)
11595 cp_parser_error (parser
,
11596 "qualified name does not name a class");
11597 /* Make sure that the right number of template parameters were
11599 if (!cp_parser_check_template_parameters (parser
, num_templates
))
11600 /* If something went wrong, there is no point in even trying to
11601 process the class-definition. */
11604 /* Look up the type. */
11607 type
= TREE_TYPE (id
);
11608 maybe_process_partial_specialization (type
);
11610 else if (!nested_name_specifier
)
11612 /* If the class was unnamed, create a dummy name. */
11614 id
= make_anon_name ();
11615 type
= xref_tag (class_key
, id
, attributes
, /*globalize=*/0);
11624 template <typename T> struct S { struct T };
11625 template <typename T> struct S<T>::T { };
11627 we will get a TYPENAME_TYPE when processing the definition of
11628 `S::T'. We need to resolve it to the actual type before we
11629 try to define it. */
11630 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
11632 class_type
= resolve_typename_type (TREE_TYPE (type
),
11633 /*only_current_p=*/false);
11634 if (class_type
!= error_mark_node
)
11635 type
= TYPE_NAME (class_type
);
11638 cp_parser_error (parser
, "could not resolve typename type");
11639 type
= error_mark_node
;
11643 /* Figure out in what scope the declaration is being placed. */
11644 scope
= current_scope ();
11646 scope
= current_namespace
;
11647 /* If that scope does not contain the scope in which the
11648 class was originally declared, the program is invalid. */
11649 if (scope
&& !is_ancestor (scope
, CP_DECL_CONTEXT (type
)))
11651 error ("declaration of `%D' in `%D' which does not "
11652 "enclose `%D'", type
, scope
, nested_name_specifier
);
11656 maybe_process_partial_specialization (TREE_TYPE (type
));
11657 class_type
= current_class_type
;
11658 type
= TREE_TYPE (handle_class_head (class_key
,
11659 nested_name_specifier
,
11662 if (type
!= error_mark_node
)
11664 if (!class_type
&& TYPE_CONTEXT (type
))
11665 *nested_name_specifier_p
= true;
11666 else if (class_type
&& !same_type_p (TYPE_CONTEXT (type
),
11668 *nested_name_specifier_p
= true;
11671 /* Indicate whether this class was declared as a `class' or as a
11673 if (TREE_CODE (type
) == RECORD_TYPE
)
11674 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
11675 cp_parser_check_class_key (class_key
, type
);
11677 /* Enter the scope containing the class; the names of base classes
11678 should be looked up in that context. For example, given:
11680 struct A { struct B {}; struct C; };
11681 struct A::C : B {};
11684 if (nested_name_specifier
)
11685 push_scope (nested_name_specifier
);
11686 /* Now, look for the base-clause. */
11687 token
= cp_lexer_peek_token (parser
->lexer
);
11688 if (token
->type
== CPP_COLON
)
11692 /* Get the list of base-classes. */
11693 bases
= cp_parser_base_clause (parser
);
11694 /* Process them. */
11695 xref_basetypes (type
, bases
);
11697 /* Leave the scope given by the nested-name-specifier. We will
11698 enter the class scope itself while processing the members. */
11699 if (nested_name_specifier
)
11700 pop_scope (nested_name_specifier
);
11705 /* Parse a class-key.
11712 Returns the kind of class-key specified, or none_type to indicate
11715 static enum tag_types
11716 cp_parser_class_key (cp_parser
* parser
)
11719 enum tag_types tag_type
;
11721 /* Look for the class-key. */
11722 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
11726 /* Check to see if the TOKEN is a class-key. */
11727 tag_type
= cp_parser_token_is_class_key (token
);
11729 cp_parser_error (parser
, "expected class-key");
11733 /* Parse an (optional) member-specification.
11735 member-specification:
11736 member-declaration member-specification [opt]
11737 access-specifier : member-specification [opt] */
11740 cp_parser_member_specification_opt (cp_parser
* parser
)
11747 /* Peek at the next token. */
11748 token
= cp_lexer_peek_token (parser
->lexer
);
11749 /* If it's a `}', or EOF then we've seen all the members. */
11750 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
11753 /* See if this token is a keyword. */
11754 keyword
= token
->keyword
;
11758 case RID_PROTECTED
:
11760 /* Consume the access-specifier. */
11761 cp_lexer_consume_token (parser
->lexer
);
11762 /* Remember which access-specifier is active. */
11763 current_access_specifier
= token
->value
;
11764 /* Look for the `:'. */
11765 cp_parser_require (parser
, CPP_COLON
, "`:'");
11769 /* Otherwise, the next construction must be a
11770 member-declaration. */
11771 cp_parser_member_declaration (parser
);
11776 /* Parse a member-declaration.
11778 member-declaration:
11779 decl-specifier-seq [opt] member-declarator-list [opt] ;
11780 function-definition ; [opt]
11781 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11783 template-declaration
11785 member-declarator-list:
11787 member-declarator-list , member-declarator
11790 declarator pure-specifier [opt]
11791 declarator constant-initializer [opt]
11792 identifier [opt] : constant-expression
11796 member-declaration:
11797 __extension__ member-declaration
11800 declarator attributes [opt] pure-specifier [opt]
11801 declarator attributes [opt] constant-initializer [opt]
11802 identifier [opt] attributes [opt] : constant-expression */
11805 cp_parser_member_declaration (cp_parser
* parser
)
11807 tree decl_specifiers
;
11808 tree prefix_attributes
;
11810 bool declares_class_or_enum
;
11813 int saved_pedantic
;
11815 /* Check for the `__extension__' keyword. */
11816 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11819 cp_parser_member_declaration (parser
);
11820 /* Restore the old value of the PEDANTIC flag. */
11821 pedantic
= saved_pedantic
;
11826 /* Check for a template-declaration. */
11827 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
11829 /* Parse the template-declaration. */
11830 cp_parser_template_declaration (parser
, /*member_p=*/true);
11835 /* Check for a using-declaration. */
11836 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
11838 /* Parse the using-declaration. */
11839 cp_parser_using_declaration (parser
);
11844 /* We can't tell whether we're looking at a declaration or a
11845 function-definition. */
11846 cp_parser_parse_tentatively (parser
);
11848 /* Parse the decl-specifier-seq. */
11850 = cp_parser_decl_specifier_seq (parser
,
11851 CP_PARSER_FLAGS_OPTIONAL
,
11852 &prefix_attributes
,
11853 &declares_class_or_enum
);
11854 /* Check for an invalid type-name. */
11855 if (cp_parser_diagnose_invalid_type_name (parser
))
11857 /* If there is no declarator, then the decl-specifier-seq should
11859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11861 /* If there was no decl-specifier-seq, and the next token is a
11862 `;', then we have something like:
11868 Each member-declaration shall declare at least one member
11869 name of the class. */
11870 if (!decl_specifiers
)
11873 pedwarn ("extra semicolon");
11879 /* See if this declaration is a friend. */
11880 friend_p
= cp_parser_friend_p (decl_specifiers
);
11881 /* If there were decl-specifiers, check to see if there was
11882 a class-declaration. */
11883 type
= check_tag_decl (decl_specifiers
);
11884 /* Nested classes have already been added to the class, but
11885 a `friend' needs to be explicitly registered. */
11888 /* If the `friend' keyword was present, the friend must
11889 be introduced with a class-key. */
11890 if (!declares_class_or_enum
)
11891 error ("a class-key must be used when declaring a friend");
11894 template <typename T> struct A {
11895 friend struct A<T>::B;
11898 A<T>::B will be represented by a TYPENAME_TYPE, and
11899 therefore not recognized by check_tag_decl. */
11904 for (specifier
= decl_specifiers
;
11906 specifier
= TREE_CHAIN (specifier
))
11908 tree s
= TREE_VALUE (specifier
);
11910 if (TREE_CODE (s
) == IDENTIFIER_NODE
11911 && IDENTIFIER_GLOBAL_VALUE (s
))
11912 type
= IDENTIFIER_GLOBAL_VALUE (s
);
11913 if (TREE_CODE (s
) == TYPE_DECL
)
11923 error ("friend declaration does not name a class or "
11926 make_friend_class (current_class_type
, type
);
11928 /* If there is no TYPE, an error message will already have
11932 /* An anonymous aggregate has to be handled specially; such
11933 a declaration really declares a data member (with a
11934 particular type), as opposed to a nested class. */
11935 else if (ANON_AGGR_TYPE_P (type
))
11937 /* Remove constructors and such from TYPE, now that we
11938 know it is an anonymous aggregate. */
11939 fixup_anonymous_aggr (type
);
11940 /* And make the corresponding data member. */
11941 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
11942 /* Add it to the class. */
11943 finish_member_declaration (decl
);
11949 /* See if these declarations will be friends. */
11950 friend_p
= cp_parser_friend_p (decl_specifiers
);
11952 /* Keep going until we hit the `;' at the end of the
11954 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11956 tree attributes
= NULL_TREE
;
11957 tree first_attribute
;
11959 /* Peek at the next token. */
11960 token
= cp_lexer_peek_token (parser
->lexer
);
11962 /* Check for a bitfield declaration. */
11963 if (token
->type
== CPP_COLON
11964 || (token
->type
== CPP_NAME
11965 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11971 /* Get the name of the bitfield. Note that we cannot just
11972 check TOKEN here because it may have been invalidated by
11973 the call to cp_lexer_peek_nth_token above. */
11974 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
11975 identifier
= cp_parser_identifier (parser
);
11977 identifier
= NULL_TREE
;
11979 /* Consume the `:' token. */
11980 cp_lexer_consume_token (parser
->lexer
);
11981 /* Get the width of the bitfield. */
11983 = cp_parser_constant_expression (parser
,
11984 /*allow_non_constant=*/false,
11987 /* Look for attributes that apply to the bitfield. */
11988 attributes
= cp_parser_attributes_opt (parser
);
11989 /* Remember which attributes are prefix attributes and
11991 first_attribute
= attributes
;
11992 /* Combine the attributes. */
11993 attributes
= chainon (prefix_attributes
, attributes
);
11995 /* Create the bitfield declaration. */
11996 decl
= grokbitfield (identifier
,
11999 /* Apply the attributes. */
12000 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
12006 tree asm_specification
;
12007 bool ctor_dtor_or_conv_p
;
12009 /* Parse the declarator. */
12011 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12012 &ctor_dtor_or_conv_p
);
12014 /* If something went wrong parsing the declarator, make sure
12015 that we at least consume some tokens. */
12016 if (declarator
== error_mark_node
)
12018 /* Skip to the end of the statement. */
12019 cp_parser_skip_to_end_of_statement (parser
);
12023 /* Look for an asm-specification. */
12024 asm_specification
= cp_parser_asm_specification_opt (parser
);
12025 /* Look for attributes that apply to the declaration. */
12026 attributes
= cp_parser_attributes_opt (parser
);
12027 /* Remember which attributes are prefix attributes and
12029 first_attribute
= attributes
;
12030 /* Combine the attributes. */
12031 attributes
= chainon (prefix_attributes
, attributes
);
12033 /* If it's an `=', then we have a constant-initializer or a
12034 pure-specifier. It is not correct to parse the
12035 initializer before registering the member declaration
12036 since the member declaration should be in scope while
12037 its initializer is processed. However, the rest of the
12038 front end does not yet provide an interface that allows
12039 us to handle this correctly. */
12040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12044 A pure-specifier shall be used only in the declaration of
12045 a virtual function.
12047 A member-declarator can contain a constant-initializer
12048 only if it declares a static member of integral or
12051 Therefore, if the DECLARATOR is for a function, we look
12052 for a pure-specifier; otherwise, we look for a
12053 constant-initializer. When we call `grokfield', it will
12054 perform more stringent semantics checks. */
12055 if (TREE_CODE (declarator
) == CALL_EXPR
)
12056 initializer
= cp_parser_pure_specifier (parser
);
12059 /* This declaration cannot be a function
12061 cp_parser_commit_to_tentative_parse (parser
);
12062 /* Parse the initializer. */
12063 initializer
= cp_parser_constant_initializer (parser
);
12066 /* Otherwise, there is no initializer. */
12068 initializer
= NULL_TREE
;
12070 /* See if we are probably looking at a function
12071 definition. We are certainly not looking at at a
12072 member-declarator. Calling `grokfield' has
12073 side-effects, so we must not do it unless we are sure
12074 that we are looking at a member-declarator. */
12075 if (cp_parser_token_starts_function_definition_p
12076 (cp_lexer_peek_token (parser
->lexer
)))
12077 decl
= error_mark_node
;
12079 /* Create the declaration. */
12080 decl
= grokfield (declarator
,
12087 /* Reset PREFIX_ATTRIBUTES. */
12088 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
12089 attributes
= TREE_CHAIN (attributes
);
12091 TREE_CHAIN (attributes
) = NULL_TREE
;
12093 /* If there is any qualification still in effect, clear it
12094 now; we will be starting fresh with the next declarator. */
12095 parser
->scope
= NULL_TREE
;
12096 parser
->qualifying_scope
= NULL_TREE
;
12097 parser
->object_scope
= NULL_TREE
;
12098 /* If it's a `,', then there are more declarators. */
12099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12100 cp_lexer_consume_token (parser
->lexer
);
12101 /* If the next token isn't a `;', then we have a parse error. */
12102 else if (cp_lexer_next_token_is_not (parser
->lexer
,
12105 cp_parser_error (parser
, "expected `;'");
12106 /* Skip tokens until we find a `;' */
12107 cp_parser_skip_to_end_of_statement (parser
);
12114 /* Add DECL to the list of members. */
12116 finish_member_declaration (decl
);
12118 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12119 cp_parser_save_default_args (parser
, decl
);
12124 /* If everything went well, look for the `;'. */
12125 if (cp_parser_parse_definitely (parser
))
12127 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12131 /* Parse the function-definition. */
12132 decl
= cp_parser_function_definition (parser
, &friend_p
);
12133 /* If the member was not a friend, declare it here. */
12135 finish_member_declaration (decl
);
12136 /* Peek at the next token. */
12137 token
= cp_lexer_peek_token (parser
->lexer
);
12138 /* If the next token is a semicolon, consume it. */
12139 if (token
->type
== CPP_SEMICOLON
)
12140 cp_lexer_consume_token (parser
->lexer
);
12143 /* Parse a pure-specifier.
12148 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12149 Otherwiser, ERROR_MARK_NODE is returned. */
12152 cp_parser_pure_specifier (cp_parser
* parser
)
12156 /* Look for the `=' token. */
12157 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12158 return error_mark_node
;
12159 /* Look for the `0' token. */
12160 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
12161 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12162 to get information from the lexer about how the number was
12163 spelled in order to fix this problem. */
12164 if (!token
|| !integer_zerop (token
->value
))
12165 return error_mark_node
;
12167 return integer_zero_node
;
12170 /* Parse a constant-initializer.
12172 constant-initializer:
12173 = constant-expression
12175 Returns a representation of the constant-expression. */
12178 cp_parser_constant_initializer (cp_parser
* parser
)
12180 /* Look for the `=' token. */
12181 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
12182 return error_mark_node
;
12184 /* It is invalid to write:
12186 struct S { static const int i = { 7 }; };
12189 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12191 cp_parser_error (parser
,
12192 "a brace-enclosed initializer is not allowed here");
12193 /* Consume the opening brace. */
12194 cp_lexer_consume_token (parser
->lexer
);
12195 /* Skip the initializer. */
12196 cp_parser_skip_to_closing_brace (parser
);
12197 /* Look for the trailing `}'. */
12198 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12200 return error_mark_node
;
12203 return cp_parser_constant_expression (parser
,
12204 /*allow_non_constant=*/false,
12208 /* Derived classes [gram.class.derived] */
12210 /* Parse a base-clause.
12213 : base-specifier-list
12215 base-specifier-list:
12217 base-specifier-list , base-specifier
12219 Returns a TREE_LIST representing the base-classes, in the order in
12220 which they were declared. The representation of each node is as
12221 described by cp_parser_base_specifier.
12223 In the case that no bases are specified, this function will return
12224 NULL_TREE, not ERROR_MARK_NODE. */
12227 cp_parser_base_clause (cp_parser
* parser
)
12229 tree bases
= NULL_TREE
;
12231 /* Look for the `:' that begins the list. */
12232 cp_parser_require (parser
, CPP_COLON
, "`:'");
12234 /* Scan the base-specifier-list. */
12240 /* Look for the base-specifier. */
12241 base
= cp_parser_base_specifier (parser
);
12242 /* Add BASE to the front of the list. */
12243 if (base
!= error_mark_node
)
12245 TREE_CHAIN (base
) = bases
;
12248 /* Peek at the next token. */
12249 token
= cp_lexer_peek_token (parser
->lexer
);
12250 /* If it's not a comma, then the list is complete. */
12251 if (token
->type
!= CPP_COMMA
)
12253 /* Consume the `,'. */
12254 cp_lexer_consume_token (parser
->lexer
);
12257 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12258 base class had a qualified name. However, the next name that
12259 appears is certainly not qualified. */
12260 parser
->scope
= NULL_TREE
;
12261 parser
->qualifying_scope
= NULL_TREE
;
12262 parser
->object_scope
= NULL_TREE
;
12264 return nreverse (bases
);
12267 /* Parse a base-specifier.
12270 :: [opt] nested-name-specifier [opt] class-name
12271 virtual access-specifier [opt] :: [opt] nested-name-specifier
12273 access-specifier virtual [opt] :: [opt] nested-name-specifier
12276 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12277 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12278 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12279 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12282 cp_parser_base_specifier (cp_parser
* parser
)
12286 bool virtual_p
= false;
12287 bool duplicate_virtual_error_issued_p
= false;
12288 bool duplicate_access_error_issued_p
= false;
12289 bool class_scope_p
, template_p
;
12290 tree access
= access_default_node
;
12293 /* Process the optional `virtual' and `access-specifier'. */
12296 /* Peek at the next token. */
12297 token
= cp_lexer_peek_token (parser
->lexer
);
12298 /* Process `virtual'. */
12299 switch (token
->keyword
)
12302 /* If `virtual' appears more than once, issue an error. */
12303 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
12305 cp_parser_error (parser
,
12306 "`virtual' specified more than once in base-specified");
12307 duplicate_virtual_error_issued_p
= true;
12312 /* Consume the `virtual' token. */
12313 cp_lexer_consume_token (parser
->lexer
);
12318 case RID_PROTECTED
:
12320 /* If more than one access specifier appears, issue an
12322 if (access
!= access_default_node
12323 && !duplicate_access_error_issued_p
)
12325 cp_parser_error (parser
,
12326 "more than one access specifier in base-specified");
12327 duplicate_access_error_issued_p
= true;
12330 access
= ridpointers
[(int) token
->keyword
];
12332 /* Consume the access-specifier. */
12333 cp_lexer_consume_token (parser
->lexer
);
12343 /* Look for the optional `::' operator. */
12344 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
12345 /* Look for the nested-name-specifier. The simplest way to
12350 The keyword `typename' is not permitted in a base-specifier or
12351 mem-initializer; in these contexts a qualified name that
12352 depends on a template-parameter is implicitly assumed to be a
12355 is to pretend that we have seen the `typename' keyword at this
12357 cp_parser_nested_name_specifier_opt (parser
,
12358 /*typename_keyword_p=*/true,
12359 /*check_dependency_p=*/true,
12361 /* If the base class is given by a qualified name, assume that names
12362 we see are type names or templates, as appropriate. */
12363 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
12364 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
12366 /* Finally, look for the class-name. */
12367 type
= cp_parser_class_name (parser
,
12371 /*check_dependency_p=*/true,
12372 /*class_head_p=*/false);
12374 if (type
== error_mark_node
)
12375 return error_mark_node
;
12377 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
12380 /* Exception handling [gram.exception] */
12382 /* Parse an (optional) exception-specification.
12384 exception-specification:
12385 throw ( type-id-list [opt] )
12387 Returns a TREE_LIST representing the exception-specification. The
12388 TREE_VALUE of each node is a type. */
12391 cp_parser_exception_specification_opt (cp_parser
* parser
)
12396 /* Peek at the next token. */
12397 token
= cp_lexer_peek_token (parser
->lexer
);
12398 /* If it's not `throw', then there's no exception-specification. */
12399 if (!cp_parser_is_keyword (token
, RID_THROW
))
12402 /* Consume the `throw'. */
12403 cp_lexer_consume_token (parser
->lexer
);
12405 /* Look for the `('. */
12406 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12408 /* Peek at the next token. */
12409 token
= cp_lexer_peek_token (parser
->lexer
);
12410 /* If it's not a `)', then there is a type-id-list. */
12411 if (token
->type
!= CPP_CLOSE_PAREN
)
12413 const char *saved_message
;
12415 /* Types may not be defined in an exception-specification. */
12416 saved_message
= parser
->type_definition_forbidden_message
;
12417 parser
->type_definition_forbidden_message
12418 = "types may not be defined in an exception-specification";
12419 /* Parse the type-id-list. */
12420 type_id_list
= cp_parser_type_id_list (parser
);
12421 /* Restore the saved message. */
12422 parser
->type_definition_forbidden_message
= saved_message
;
12425 type_id_list
= empty_except_spec
;
12427 /* Look for the `)'. */
12428 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12430 return type_id_list
;
12433 /* Parse an (optional) type-id-list.
12437 type-id-list , type-id
12439 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12440 in the order that the types were presented. */
12443 cp_parser_type_id_list (cp_parser
* parser
)
12445 tree types
= NULL_TREE
;
12452 /* Get the next type-id. */
12453 type
= cp_parser_type_id (parser
);
12454 /* Add it to the list. */
12455 types
= add_exception_specifier (types
, type
, /*complain=*/1);
12456 /* Peek at the next token. */
12457 token
= cp_lexer_peek_token (parser
->lexer
);
12458 /* If it is not a `,', we are done. */
12459 if (token
->type
!= CPP_COMMA
)
12461 /* Consume the `,'. */
12462 cp_lexer_consume_token (parser
->lexer
);
12465 return nreverse (types
);
12468 /* Parse a try-block.
12471 try compound-statement handler-seq */
12474 cp_parser_try_block (cp_parser
* parser
)
12478 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
12479 try_block
= begin_try_block ();
12480 cp_parser_compound_statement (parser
);
12481 finish_try_block (try_block
);
12482 cp_parser_handler_seq (parser
);
12483 finish_handler_sequence (try_block
);
12488 /* Parse a function-try-block.
12490 function-try-block:
12491 try ctor-initializer [opt] function-body handler-seq */
12494 cp_parser_function_try_block (cp_parser
* parser
)
12497 bool ctor_initializer_p
;
12499 /* Look for the `try' keyword. */
12500 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
12502 /* Let the rest of the front-end know where we are. */
12503 try_block
= begin_function_try_block ();
12504 /* Parse the function-body. */
12506 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
12507 /* We're done with the `try' part. */
12508 finish_function_try_block (try_block
);
12509 /* Parse the handlers. */
12510 cp_parser_handler_seq (parser
);
12511 /* We're done with the handlers. */
12512 finish_function_handler_sequence (try_block
);
12514 return ctor_initializer_p
;
12517 /* Parse a handler-seq.
12520 handler handler-seq [opt] */
12523 cp_parser_handler_seq (cp_parser
* parser
)
12529 /* Parse the handler. */
12530 cp_parser_handler (parser
);
12531 /* Peek at the next token. */
12532 token
= cp_lexer_peek_token (parser
->lexer
);
12533 /* If it's not `catch' then there are no more handlers. */
12534 if (!cp_parser_is_keyword (token
, RID_CATCH
))
12539 /* Parse a handler.
12542 catch ( exception-declaration ) compound-statement */
12545 cp_parser_handler (cp_parser
* parser
)
12550 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
12551 handler
= begin_handler ();
12552 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12553 declaration
= cp_parser_exception_declaration (parser
);
12554 finish_handler_parms (declaration
, handler
);
12555 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12556 cp_parser_compound_statement (parser
);
12557 finish_handler (handler
);
12560 /* Parse an exception-declaration.
12562 exception-declaration:
12563 type-specifier-seq declarator
12564 type-specifier-seq abstract-declarator
12568 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12569 ellipsis variant is used. */
12572 cp_parser_exception_declaration (cp_parser
* parser
)
12574 tree type_specifiers
;
12576 const char *saved_message
;
12578 /* If it's an ellipsis, it's easy to handle. */
12579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12581 /* Consume the `...' token. */
12582 cp_lexer_consume_token (parser
->lexer
);
12586 /* Types may not be defined in exception-declarations. */
12587 saved_message
= parser
->type_definition_forbidden_message
;
12588 parser
->type_definition_forbidden_message
12589 = "types may not be defined in exception-declarations";
12591 /* Parse the type-specifier-seq. */
12592 type_specifiers
= cp_parser_type_specifier_seq (parser
);
12593 /* If it's a `)', then there is no declarator. */
12594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
12595 declarator
= NULL_TREE
;
12597 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
12598 /*ctor_dtor_or_conv_p=*/NULL
);
12600 /* Restore the saved message. */
12601 parser
->type_definition_forbidden_message
= saved_message
;
12603 return start_handler_parms (type_specifiers
, declarator
);
12606 /* Parse a throw-expression.
12609 throw assignment-expression [opt]
12611 Returns a THROW_EXPR representing the throw-expression. */
12614 cp_parser_throw_expression (cp_parser
* parser
)
12618 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
12619 /* We can't be sure if there is an assignment-expression or not. */
12620 cp_parser_parse_tentatively (parser
);
12622 expression
= cp_parser_assignment_expression (parser
);
12623 /* If it didn't work, this is just a rethrow. */
12624 if (!cp_parser_parse_definitely (parser
))
12625 expression
= NULL_TREE
;
12627 return build_throw (expression
);
12630 /* GNU Extensions */
12632 /* Parse an (optional) asm-specification.
12635 asm ( string-literal )
12637 If the asm-specification is present, returns a STRING_CST
12638 corresponding to the string-literal. Otherwise, returns
12642 cp_parser_asm_specification_opt (cp_parser
* parser
)
12645 tree asm_specification
;
12647 /* Peek at the next token. */
12648 token
= cp_lexer_peek_token (parser
->lexer
);
12649 /* If the next token isn't the `asm' keyword, then there's no
12650 asm-specification. */
12651 if (!cp_parser_is_keyword (token
, RID_ASM
))
12654 /* Consume the `asm' token. */
12655 cp_lexer_consume_token (parser
->lexer
);
12656 /* Look for the `('. */
12657 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12659 /* Look for the string-literal. */
12660 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12662 asm_specification
= token
->value
;
12664 asm_specification
= NULL_TREE
;
12666 /* Look for the `)'. */
12667 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
12669 return asm_specification
;
12672 /* Parse an asm-operand-list.
12676 asm-operand-list , asm-operand
12679 string-literal ( expression )
12680 [ string-literal ] string-literal ( expression )
12682 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12683 each node is the expression. The TREE_PURPOSE is itself a
12684 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12685 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12686 is a STRING_CST for the string literal before the parenthesis. */
12689 cp_parser_asm_operand_list (cp_parser
* parser
)
12691 tree asm_operands
= NULL_TREE
;
12695 tree string_literal
;
12700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
12702 /* Consume the `[' token. */
12703 cp_lexer_consume_token (parser
->lexer
);
12704 /* Read the operand name. */
12705 name
= cp_parser_identifier (parser
);
12706 if (name
!= error_mark_node
)
12707 name
= build_string (IDENTIFIER_LENGTH (name
),
12708 IDENTIFIER_POINTER (name
));
12709 /* Look for the closing `]'. */
12710 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
12714 /* Look for the string-literal. */
12715 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12716 string_literal
= token
? token
->value
: error_mark_node
;
12717 /* Look for the `('. */
12718 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12719 /* Parse the expression. */
12720 expression
= cp_parser_expression (parser
);
12721 /* Look for the `)'. */
12722 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12723 /* Add this operand to the list. */
12724 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
12727 /* If the next token is not a `,', there are no more
12729 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12731 /* Consume the `,'. */
12732 cp_lexer_consume_token (parser
->lexer
);
12735 return nreverse (asm_operands
);
12738 /* Parse an asm-clobber-list.
12742 asm-clobber-list , string-literal
12744 Returns a TREE_LIST, indicating the clobbers in the order that they
12745 appeared. The TREE_VALUE of each node is a STRING_CST. */
12748 cp_parser_asm_clobber_list (cp_parser
* parser
)
12750 tree clobbers
= NULL_TREE
;
12755 tree string_literal
;
12757 /* Look for the string literal. */
12758 token
= cp_parser_require (parser
, CPP_STRING
, "string-literal");
12759 string_literal
= token
? token
->value
: error_mark_node
;
12760 /* Add it to the list. */
12761 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
12762 /* If the next token is not a `,', then the list is
12764 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12766 /* Consume the `,' token. */
12767 cp_lexer_consume_token (parser
->lexer
);
12773 /* Parse an (optional) series of attributes.
12776 attributes attribute
12779 __attribute__ (( attribute-list [opt] ))
12781 The return value is as for cp_parser_attribute_list. */
12784 cp_parser_attributes_opt (cp_parser
* parser
)
12786 tree attributes
= NULL_TREE
;
12791 tree attribute_list
;
12793 /* Peek at the next token. */
12794 token
= cp_lexer_peek_token (parser
->lexer
);
12795 /* If it's not `__attribute__', then we're done. */
12796 if (token
->keyword
!= RID_ATTRIBUTE
)
12799 /* Consume the `__attribute__' keyword. */
12800 cp_lexer_consume_token (parser
->lexer
);
12801 /* Look for the two `(' tokens. */
12802 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12803 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
12805 /* Peek at the next token. */
12806 token
= cp_lexer_peek_token (parser
->lexer
);
12807 if (token
->type
!= CPP_CLOSE_PAREN
)
12808 /* Parse the attribute-list. */
12809 attribute_list
= cp_parser_attribute_list (parser
);
12811 /* If the next token is a `)', then there is no attribute
12813 attribute_list
= NULL
;
12815 /* Look for the two `)' tokens. */
12816 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12817 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12819 /* Add these new attributes to the list. */
12820 attributes
= chainon (attributes
, attribute_list
);
12826 /* Parse an attribute-list.
12830 attribute-list , attribute
12834 identifier ( identifier )
12835 identifier ( identifier , expression-list )
12836 identifier ( expression-list )
12838 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12839 TREE_PURPOSE of each node is the identifier indicating which
12840 attribute is in use. The TREE_VALUE represents the arguments, if
12844 cp_parser_attribute_list (cp_parser
* parser
)
12846 tree attribute_list
= NULL_TREE
;
12854 /* Look for the identifier. We also allow keywords here; for
12855 example `__attribute__ ((const))' is legal. */
12856 token
= cp_lexer_peek_token (parser
->lexer
);
12857 if (token
->type
!= CPP_NAME
12858 && token
->type
!= CPP_KEYWORD
)
12859 return error_mark_node
;
12860 /* Consume the token. */
12861 token
= cp_lexer_consume_token (parser
->lexer
);
12863 /* Save away the identifier that indicates which attribute this is. */
12864 identifier
= token
->value
;
12865 attribute
= build_tree_list (identifier
, NULL_TREE
);
12867 /* Peek at the next token. */
12868 token
= cp_lexer_peek_token (parser
->lexer
);
12869 /* If it's an `(', then parse the attribute arguments. */
12870 if (token
->type
== CPP_OPEN_PAREN
)
12873 int arguments_allowed_p
= 1;
12875 /* Consume the `('. */
12876 cp_lexer_consume_token (parser
->lexer
);
12877 /* Peek at the next token. */
12878 token
= cp_lexer_peek_token (parser
->lexer
);
12879 /* Check to see if the next token is an identifier. */
12880 if (token
->type
== CPP_NAME
)
12882 /* Save the identifier. */
12883 identifier
= token
->value
;
12884 /* Consume the identifier. */
12885 cp_lexer_consume_token (parser
->lexer
);
12886 /* Peek at the next token. */
12887 token
= cp_lexer_peek_token (parser
->lexer
);
12888 /* If the next token is a `,', then there are some other
12889 expressions as well. */
12890 if (token
->type
== CPP_COMMA
)
12891 /* Consume the comma. */
12892 cp_lexer_consume_token (parser
->lexer
);
12894 arguments_allowed_p
= 0;
12897 identifier
= NULL_TREE
;
12899 /* If there are arguments, parse them too. */
12900 if (arguments_allowed_p
)
12901 arguments
= cp_parser_expression_list (parser
);
12903 arguments
= NULL_TREE
;
12905 /* Combine the identifier and the arguments. */
12907 arguments
= tree_cons (NULL_TREE
, identifier
, arguments
);
12909 /* Save the identifier and arguments away. */
12910 TREE_VALUE (attribute
) = arguments
;
12912 /* Look for the closing `)'. */
12913 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12916 /* Add this attribute to the list. */
12917 TREE_CHAIN (attribute
) = attribute_list
;
12918 attribute_list
= attribute
;
12920 /* Now, look for more attributes. */
12921 token
= cp_lexer_peek_token (parser
->lexer
);
12922 /* If the next token isn't a `,', we're done. */
12923 if (token
->type
!= CPP_COMMA
)
12926 /* Consume the commma and keep going. */
12927 cp_lexer_consume_token (parser
->lexer
);
12930 /* We built up the list in reverse order. */
12931 return nreverse (attribute_list
);
12934 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
12935 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12936 current value of the PEDANTIC flag, regardless of whether or not
12937 the `__extension__' keyword is present. The caller is responsible
12938 for restoring the value of the PEDANTIC flag. */
12941 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
12943 /* Save the old value of the PEDANTIC flag. */
12944 *saved_pedantic
= pedantic
;
12946 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
12948 /* Consume the `__extension__' token. */
12949 cp_lexer_consume_token (parser
->lexer
);
12950 /* We're not being pedantic while the `__extension__' keyword is
12960 /* Parse a label declaration.
12963 __label__ label-declarator-seq ;
12965 label-declarator-seq:
12966 identifier , label-declarator-seq
12970 cp_parser_label_declaration (cp_parser
* parser
)
12972 /* Look for the `__label__' keyword. */
12973 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
12979 /* Look for an identifier. */
12980 identifier
= cp_parser_identifier (parser
);
12981 /* Declare it as a lobel. */
12982 finish_label_decl (identifier
);
12983 /* If the next token is a `;', stop. */
12984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12986 /* Look for the `,' separating the label declarations. */
12987 cp_parser_require (parser
, CPP_COMMA
, "`,'");
12990 /* Look for the final `;'. */
12991 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
12994 /* Support Functions */
12996 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12997 NAME should have one of the representations used for an
12998 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12999 is returned. If PARSER->SCOPE is a dependent type, then a
13000 SCOPE_REF is returned.
13002 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13003 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13004 was formed. Abstractly, such entities should not be passed to this
13005 function, because they do not need to be looked up, but it is
13006 simpler to check for this special case here, rather than at the
13009 In cases not explicitly covered above, this function returns a
13010 DECL, OVERLOAD, or baselink representing the result of the lookup.
13011 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13014 If IS_TYPE is TRUE, bindings that do not refer to types are
13017 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13020 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13024 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
13025 bool is_type
, bool is_namespace
, bool check_dependency
)
13028 tree object_type
= parser
->context
->object_type
;
13030 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13031 no longer valid. Note that if we are parsing tentatively, and
13032 the parse fails, OBJECT_TYPE will be automatically restored. */
13033 parser
->context
->object_type
= NULL_TREE
;
13035 if (name
== error_mark_node
)
13036 return error_mark_node
;
13038 /* A template-id has already been resolved; there is no lookup to
13040 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13042 if (BASELINK_P (name
))
13044 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
13045 == TEMPLATE_ID_EXPR
),
13050 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13051 it should already have been checked to make sure that the name
13052 used matches the type being destroyed. */
13053 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13057 /* Figure out to which type this destructor applies. */
13059 type
= parser
->scope
;
13060 else if (object_type
)
13061 type
= object_type
;
13063 type
= current_class_type
;
13064 /* If that's not a class type, there is no destructor. */
13065 if (!type
|| !CLASS_TYPE_P (type
))
13066 return error_mark_node
;
13067 /* If it was a class type, return the destructor. */
13068 return CLASSTYPE_DESTRUCTORS (type
);
13071 /* By this point, the NAME should be an ordinary identifier. If
13072 the id-expression was a qualified name, the qualifying scope is
13073 stored in PARSER->SCOPE at this point. */
13074 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
,
13077 /* Perform the lookup. */
13082 if (parser
->scope
== error_mark_node
)
13083 return error_mark_node
;
13085 /* If the SCOPE is dependent, the lookup must be deferred until
13086 the template is instantiated -- unless we are explicitly
13087 looking up names in uninstantiated templates. Even then, we
13088 cannot look up the name if the scope is not a class type; it
13089 might, for example, be a template type parameter. */
13090 dependent_p
= (TYPE_P (parser
->scope
)
13091 && !(parser
->in_declarator_p
13092 && currently_open_class (parser
->scope
))
13093 && dependent_type_p (parser
->scope
));
13094 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
13098 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
13100 /* The resolution to Core Issue 180 says that `struct A::B'
13101 should be considered a type-name, even if `A' is
13103 decl
= TYPE_NAME (make_typename_type (parser
->scope
,
13109 /* If PARSER->SCOPE is a dependent type, then it must be a
13110 class type, and we must not be checking dependencies;
13111 otherwise, we would have processed this lookup above. So
13112 that PARSER->SCOPE is not considered a dependent base by
13113 lookup_member, we must enter the scope here. */
13115 push_scope (parser
->scope
);
13116 /* If the PARSER->SCOPE is a a template specialization, it
13117 may be instantiated during name lookup. In that case,
13118 errors may be issued. Even if we rollback the current
13119 tentative parse, those errors are valid. */
13120 decl
= lookup_qualified_name (parser
->scope
, name
, is_type
);
13122 pop_scope (parser
->scope
);
13124 parser
->qualifying_scope
= parser
->scope
;
13125 parser
->object_scope
= NULL_TREE
;
13127 else if (object_type
)
13129 tree object_decl
= NULL_TREE
;
13130 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13131 OBJECT_TYPE is not a class. */
13132 if (CLASS_TYPE_P (object_type
))
13133 /* If the OBJECT_TYPE is a template specialization, it may
13134 be instantiated during name lookup. In that case, errors
13135 may be issued. Even if we rollback the current tentative
13136 parse, those errors are valid. */
13137 object_decl
= lookup_member (object_type
,
13139 /*protect=*/0, is_type
);
13140 /* Look it up in the enclosing context, too. */
13141 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13144 parser
->object_scope
= object_type
;
13145 parser
->qualifying_scope
= NULL_TREE
;
13147 decl
= object_decl
;
13151 decl
= lookup_name_real (name
, is_type
, /*nonclass=*/0,
13154 parser
->qualifying_scope
= NULL_TREE
;
13155 parser
->object_scope
= NULL_TREE
;
13158 /* If the lookup failed, let our caller know. */
13160 || decl
== error_mark_node
13161 || (TREE_CODE (decl
) == FUNCTION_DECL
13162 && DECL_ANTICIPATED (decl
)))
13163 return error_mark_node
;
13165 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13166 if (TREE_CODE (decl
) == TREE_LIST
)
13168 /* The error message we have to print is too complicated for
13169 cp_parser_error, so we incorporate its actions directly. */
13170 if (!cp_parser_simulate_error (parser
))
13172 error ("reference to `%D' is ambiguous", name
);
13173 print_candidates (decl
);
13175 return error_mark_node
;
13178 my_friendly_assert (DECL_P (decl
)
13179 || TREE_CODE (decl
) == OVERLOAD
13180 || TREE_CODE (decl
) == SCOPE_REF
13181 || BASELINK_P (decl
),
13184 /* If we have resolved the name of a member declaration, check to
13185 see if the declaration is accessible. When the name resolves to
13186 set of overloaded functions, accessibility is checked when
13187 overload resolution is done.
13189 During an explicit instantiation, access is not checked at all,
13190 as per [temp.explicit]. */
13192 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
13197 /* Like cp_parser_lookup_name, but for use in the typical case where
13198 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13202 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
13204 return cp_parser_lookup_name (parser
, name
,
13206 /*is_namespace=*/false,
13207 /*check_dependency=*/true);
13210 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13211 the current context, return the TYPE_DECL. If TAG_NAME_P is
13212 true, the DECL indicates the class being defined in a class-head,
13213 or declared in an elaborated-type-specifier.
13215 Otherwise, return DECL. */
13218 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
13220 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13221 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13224 template <typename T> struct B;
13227 template <typename T> struct A::B {};
13229 Similarly, in a elaborated-type-specifier:
13231 namespace N { struct X{}; }
13234 template <typename T> friend struct N::X;
13237 However, if the DECL refers to a class type, and we are in
13238 the scope of the class, then the name lookup automatically
13239 finds the TYPE_DECL created by build_self_reference rather
13240 than a TEMPLATE_DECL. For example, in:
13242 template <class T> struct S {
13246 there is no need to handle such case. */
13248 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
13249 return DECL_TEMPLATE_RESULT (decl
);
13254 /* If too many, or too few, template-parameter lists apply to the
13255 declarator, issue an error message. Returns TRUE if all went well,
13256 and FALSE otherwise. */
13259 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
13262 unsigned num_templates
;
13264 /* We haven't seen any classes that involve template parameters yet. */
13267 switch (TREE_CODE (declarator
))
13274 tree main_declarator
= TREE_OPERAND (declarator
, 0);
13276 cp_parser_check_declarator_template_parameters (parser
,
13285 scope
= TREE_OPERAND (declarator
, 0);
13286 member
= TREE_OPERAND (declarator
, 1);
13288 /* If this is a pointer-to-member, then we are not interested
13289 in the SCOPE, because it does not qualify the thing that is
13291 if (TREE_CODE (member
) == INDIRECT_REF
)
13292 return (cp_parser_check_declarator_template_parameters
13295 while (scope
&& CLASS_TYPE_P (scope
))
13297 /* You're supposed to have one `template <...>'
13298 for every template class, but you don't need one
13299 for a full specialization. For example:
13301 template <class T> struct S{};
13302 template <> struct S<int> { void f(); };
13303 void S<int>::f () {}
13305 is correct; there shouldn't be a `template <>' for
13306 the definition of `S<int>::f'. */
13307 if (CLASSTYPE_TEMPLATE_INFO (scope
)
13308 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
13309 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
13310 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
13313 scope
= TYPE_CONTEXT (scope
);
13317 /* Fall through. */
13320 /* If the DECLARATOR has the form `X<y>' then it uses one
13321 additional level of template parameters. */
13322 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
13325 return cp_parser_check_template_parameters (parser
,
13330 /* NUM_TEMPLATES were used in the current declaration. If that is
13331 invalid, return FALSE and issue an error messages. Otherwise,
13335 cp_parser_check_template_parameters (cp_parser
* parser
,
13336 unsigned num_templates
)
13338 /* If there are more template classes than parameter lists, we have
13341 template <class T> void S<T>::R<T>::f (); */
13342 if (parser
->num_template_parameter_lists
< num_templates
)
13344 error ("too few template-parameter-lists");
13347 /* If there are the same number of template classes and parameter
13348 lists, that's OK. */
13349 if (parser
->num_template_parameter_lists
== num_templates
)
13351 /* If there are more, but only one more, then we are referring to a
13352 member template. That's OK too. */
13353 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
13355 /* Otherwise, there are too many template parameter lists. We have
13358 template <class T> template <class U> void S::f(); */
13359 error ("too many template-parameter-lists");
13363 /* Parse a binary-expression of the general form:
13367 binary-expression <token> <expr>
13369 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13370 to parser the <expr>s. If the first production is used, then the
13371 value returned by FN is returned directly. Otherwise, a node with
13372 the indicated EXPR_TYPE is returned, with operands corresponding to
13373 the two sub-expressions. */
13376 cp_parser_binary_expression (cp_parser
* parser
,
13377 const cp_parser_token_tree_map token_tree_map
,
13378 cp_parser_expression_fn fn
)
13382 /* Parse the first expression. */
13383 lhs
= (*fn
) (parser
);
13384 /* Now, look for more expressions. */
13388 const cp_parser_token_tree_map_node
*map_node
;
13391 /* Peek at the next token. */
13392 token
= cp_lexer_peek_token (parser
->lexer
);
13393 /* If the token is `>', and that's not an operator at the
13394 moment, then we're done. */
13395 if (token
->type
== CPP_GREATER
13396 && !parser
->greater_than_is_operator_p
)
13398 /* If we find one of the tokens we want, build the corresponding
13399 tree representation. */
13400 for (map_node
= token_tree_map
;
13401 map_node
->token_type
!= CPP_EOF
;
13403 if (map_node
->token_type
== token
->type
)
13405 /* Consume the operator token. */
13406 cp_lexer_consume_token (parser
->lexer
);
13407 /* Parse the right-hand side of the expression. */
13408 rhs
= (*fn
) (parser
);
13409 /* Build the binary tree node. */
13410 lhs
= build_x_binary_op (map_node
->tree_type
, lhs
, rhs
);
13414 /* If the token wasn't one of the ones we want, we're done. */
13415 if (map_node
->token_type
== CPP_EOF
)
13422 /* Parse an optional `::' token indicating that the following name is
13423 from the global namespace. If so, PARSER->SCOPE is set to the
13424 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13425 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13426 Returns the new value of PARSER->SCOPE, if the `::' token is
13427 present, and NULL_TREE otherwise. */
13430 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
13434 /* Peek at the next token. */
13435 token
= cp_lexer_peek_token (parser
->lexer
);
13436 /* If we're looking at a `::' token then we're starting from the
13437 global namespace, not our current location. */
13438 if (token
->type
== CPP_SCOPE
)
13440 /* Consume the `::' token. */
13441 cp_lexer_consume_token (parser
->lexer
);
13442 /* Set the SCOPE so that we know where to start the lookup. */
13443 parser
->scope
= global_namespace
;
13444 parser
->qualifying_scope
= global_namespace
;
13445 parser
->object_scope
= NULL_TREE
;
13447 return parser
->scope
;
13449 else if (!current_scope_valid_p
)
13451 parser
->scope
= NULL_TREE
;
13452 parser
->qualifying_scope
= NULL_TREE
;
13453 parser
->object_scope
= NULL_TREE
;
13459 /* Returns TRUE if the upcoming token sequence is the start of a
13460 constructor declarator. If FRIEND_P is true, the declarator is
13461 preceded by the `friend' specifier. */
13464 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
13466 bool constructor_p
;
13467 tree type_decl
= NULL_TREE
;
13468 bool nested_name_p
;
13469 cp_token
*next_token
;
13471 /* The common case is that this is not a constructor declarator, so
13472 try to avoid doing lots of work if at all possible. It's not
13473 valid declare a constructor at function scope. */
13474 if (at_function_scope_p ())
13476 /* And only certain tokens can begin a constructor declarator. */
13477 next_token
= cp_lexer_peek_token (parser
->lexer
);
13478 if (next_token
->type
!= CPP_NAME
13479 && next_token
->type
!= CPP_SCOPE
13480 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
13481 && next_token
->type
!= CPP_TEMPLATE_ID
)
13484 /* Parse tentatively; we are going to roll back all of the tokens
13486 cp_parser_parse_tentatively (parser
);
13487 /* Assume that we are looking at a constructor declarator. */
13488 constructor_p
= true;
13490 /* Look for the optional `::' operator. */
13491 cp_parser_global_scope_opt (parser
,
13492 /*current_scope_valid_p=*/false);
13493 /* Look for the nested-name-specifier. */
13495 = (cp_parser_nested_name_specifier_opt (parser
,
13496 /*typename_keyword_p=*/false,
13497 /*check_dependency_p=*/false,
13500 /* Outside of a class-specifier, there must be a
13501 nested-name-specifier. */
13502 if (!nested_name_p
&&
13503 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
13505 constructor_p
= false;
13506 /* If we still think that this might be a constructor-declarator,
13507 look for a class-name. */
13512 template <typename T> struct S { S(); };
13513 template <typename T> S<T>::S ();
13515 we must recognize that the nested `S' names a class.
13518 template <typename T> S<T>::S<T> ();
13520 we must recognize that the nested `S' names a template. */
13521 type_decl
= cp_parser_class_name (parser
,
13522 /*typename_keyword_p=*/false,
13523 /*template_keyword_p=*/false,
13525 /*check_dependency_p=*/false,
13526 /*class_head_p=*/false);
13527 /* If there was no class-name, then this is not a constructor. */
13528 constructor_p
= !cp_parser_error_occurred (parser
);
13531 /* If we're still considering a constructor, we have to see a `(',
13532 to begin the parameter-declaration-clause, followed by either a
13533 `)', an `...', or a decl-specifier. We need to check for a
13534 type-specifier to avoid being fooled into thinking that:
13538 is a constructor. (It is actually a function named `f' that
13539 takes one parameter (of type `int') and returns a value of type
13542 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
13544 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
13545 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
13546 && !cp_parser_storage_class_specifier_opt (parser
))
13550 /* Names appearing in the type-specifier should be looked up
13551 in the scope of the class. */
13552 if (current_class_type
)
13556 type
= TREE_TYPE (type_decl
);
13557 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13559 type
= resolve_typename_type (type
,
13560 /*only_current_p=*/false);
13561 if (type
== error_mark_node
)
13563 cp_parser_abort_tentative_parse (parser
);
13569 /* Look for the type-specifier. */
13570 cp_parser_type_specifier (parser
,
13571 CP_PARSER_FLAGS_NONE
,
13572 /*is_friend=*/false,
13573 /*is_declarator=*/true,
13574 /*declares_class_or_enum=*/NULL
,
13575 /*is_cv_qualifier=*/NULL
);
13576 /* Leave the scope of the class. */
13580 constructor_p
= !cp_parser_error_occurred (parser
);
13584 constructor_p
= false;
13585 /* We did not really want to consume any tokens. */
13586 cp_parser_abort_tentative_parse (parser
);
13588 return constructor_p
;
13591 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13592 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13593 they must be performed once we are in the scope of the function.
13595 Returns the function defined. */
13598 cp_parser_function_definition_from_specifiers_and_declarator
13599 (cp_parser
* parser
,
13600 tree decl_specifiers
,
13607 /* Begin the function-definition. */
13608 success_p
= begin_function_definition (decl_specifiers
,
13612 /* If there were names looked up in the decl-specifier-seq that we
13613 did not check, check them now. We must wait until we are in the
13614 scope of the function to perform the checks, since the function
13615 might be a friend. */
13616 perform_deferred_access_checks ();
13620 /* If begin_function_definition didn't like the definition, skip
13621 the entire function. */
13622 error ("invalid function declaration");
13623 cp_parser_skip_to_end_of_block_or_statement (parser
);
13624 fn
= error_mark_node
;
13627 fn
= cp_parser_function_definition_after_declarator (parser
,
13628 /*inline_p=*/false);
13633 /* Parse the part of a function-definition that follows the
13634 declarator. INLINE_P is TRUE iff this function is an inline
13635 function defined with a class-specifier.
13637 Returns the function defined. */
13640 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
13644 bool ctor_initializer_p
= false;
13645 bool saved_in_unbraced_linkage_specification_p
;
13646 unsigned saved_num_template_parameter_lists
;
13648 /* If the next token is `return', then the code may be trying to
13649 make use of the "named return value" extension that G++ used to
13651 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
13653 /* Consume the `return' keyword. */
13654 cp_lexer_consume_token (parser
->lexer
);
13655 /* Look for the identifier that indicates what value is to be
13657 cp_parser_identifier (parser
);
13658 /* Issue an error message. */
13659 error ("named return values are no longer supported");
13660 /* Skip tokens until we reach the start of the function body. */
13661 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13662 cp_lexer_consume_token (parser
->lexer
);
13664 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13665 anything declared inside `f'. */
13666 saved_in_unbraced_linkage_specification_p
13667 = parser
->in_unbraced_linkage_specification_p
;
13668 parser
->in_unbraced_linkage_specification_p
= false;
13669 /* Inside the function, surrounding template-parameter-lists do not
13671 saved_num_template_parameter_lists
13672 = parser
->num_template_parameter_lists
;
13673 parser
->num_template_parameter_lists
= 0;
13674 /* If the next token is `try', then we are looking at a
13675 function-try-block. */
13676 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
13677 ctor_initializer_p
= cp_parser_function_try_block (parser
);
13678 /* A function-try-block includes the function-body, so we only do
13679 this next part if we're not processing a function-try-block. */
13682 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13684 /* Finish the function. */
13685 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
13686 (inline_p
? 2 : 0));
13687 /* Generate code for it, if necessary. */
13688 expand_or_defer_fn (fn
);
13689 /* Restore the saved values. */
13690 parser
->in_unbraced_linkage_specification_p
13691 = saved_in_unbraced_linkage_specification_p
;
13692 parser
->num_template_parameter_lists
13693 = saved_num_template_parameter_lists
;
13698 /* Parse a template-declaration, assuming that the `export' (and
13699 `extern') keywords, if present, has already been scanned. MEMBER_P
13700 is as for cp_parser_template_declaration. */
13703 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
13705 tree decl
= NULL_TREE
;
13706 tree parameter_list
;
13707 bool friend_p
= false;
13709 /* Look for the `template' keyword. */
13710 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
13714 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
13717 /* Parse the template parameters. */
13718 begin_template_parm_list ();
13719 /* If the next token is `>', then we have an invalid
13720 specialization. Rather than complain about an invalid template
13721 parameter, issue an error message here. */
13722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
13724 cp_parser_error (parser
, "invalid explicit specialization");
13725 parameter_list
= NULL_TREE
;
13728 parameter_list
= cp_parser_template_parameter_list (parser
);
13729 parameter_list
= end_template_parm_list (parameter_list
);
13730 /* Look for the `>'. */
13731 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
13732 /* We just processed one more parameter list. */
13733 ++parser
->num_template_parameter_lists
;
13734 /* If the next token is `template', there are more template
13736 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13738 cp_parser_template_declaration_after_export (parser
, member_p
);
13741 decl
= cp_parser_single_declaration (parser
,
13745 /* If this is a member template declaration, let the front
13747 if (member_p
&& !friend_p
&& decl
)
13748 decl
= finish_member_template_decl (decl
);
13749 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
13750 make_friend_class (current_class_type
, TREE_TYPE (decl
));
13752 /* We are done with the current parameter list. */
13753 --parser
->num_template_parameter_lists
;
13756 finish_template_decl (parameter_list
);
13758 /* Register member declarations. */
13759 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
13760 finish_member_declaration (decl
);
13762 /* If DECL is a function template, we must return to parse it later.
13763 (Even though there is no definition, there might be default
13764 arguments that need handling.) */
13765 if (member_p
&& decl
13766 && (TREE_CODE (decl
) == FUNCTION_DECL
13767 || DECL_FUNCTION_TEMPLATE_P (decl
)))
13768 TREE_VALUE (parser
->unparsed_functions_queues
)
13769 = tree_cons (NULL_TREE
, decl
,
13770 TREE_VALUE (parser
->unparsed_functions_queues
));
13773 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13774 `function-definition' sequence. MEMBER_P is true, this declaration
13775 appears in a class scope.
13777 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13778 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13781 cp_parser_single_declaration (cp_parser
* parser
,
13785 bool declares_class_or_enum
;
13786 tree decl
= NULL_TREE
;
13787 tree decl_specifiers
;
13790 /* Parse the dependent declaration. We don't know yet
13791 whether it will be a function-definition. */
13792 cp_parser_parse_tentatively (parser
);
13793 /* Defer access checks until we know what is being declared. */
13794 push_deferring_access_checks (dk_deferred
);
13796 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13799 = cp_parser_decl_specifier_seq (parser
,
13800 CP_PARSER_FLAGS_OPTIONAL
,
13802 &declares_class_or_enum
);
13803 /* Gather up the access checks that occurred the
13804 decl-specifier-seq. */
13805 stop_deferring_access_checks ();
13807 /* Check for the declaration of a template class. */
13808 if (declares_class_or_enum
)
13810 if (cp_parser_declares_only_class_p (parser
))
13812 decl
= shadow_tag (decl_specifiers
);
13814 decl
= TYPE_NAME (decl
);
13816 decl
= error_mark_node
;
13821 /* If it's not a template class, try for a template function. If
13822 the next token is a `;', then this declaration does not declare
13823 anything. But, if there were errors in the decl-specifiers, then
13824 the error might well have come from an attempted class-specifier.
13825 In that case, there's no need to warn about a missing declarator. */
13827 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
13828 || !value_member (error_mark_node
, decl_specifiers
)))
13829 decl
= cp_parser_init_declarator (parser
,
13832 /*function_definition_allowed_p=*/false,
13834 /*function_definition_p=*/NULL
);
13836 pop_deferring_access_checks ();
13838 /* Clear any current qualification; whatever comes next is the start
13839 of something new. */
13840 parser
->scope
= NULL_TREE
;
13841 parser
->qualifying_scope
= NULL_TREE
;
13842 parser
->object_scope
= NULL_TREE
;
13843 /* Look for a trailing `;' after the declaration. */
13844 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")
13845 && cp_parser_committed_to_tentative_parse (parser
))
13846 cp_parser_skip_to_end_of_block_or_statement (parser
);
13847 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13848 if (cp_parser_parse_definitely (parser
))
13851 *friend_p
= cp_parser_friend_p (decl_specifiers
);
13853 /* Otherwise, try a function-definition. */
13855 decl
= cp_parser_function_definition (parser
, friend_p
);
13860 /* Parse a cast-expression that is not the operand of a unary "&". */
13863 cp_parser_simple_cast_expression (cp_parser
*parser
)
13865 return cp_parser_cast_expression (parser
, /*address_p=*/false);
13868 /* Parse a functional cast to TYPE. Returns an expression
13869 representing the cast. */
13872 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
13874 tree expression_list
;
13876 /* Look for the opening `('. */
13877 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
13878 return error_mark_node
;
13879 /* If the next token is not an `)', there are arguments to the
13881 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
13882 expression_list
= cp_parser_expression_list (parser
);
13884 expression_list
= NULL_TREE
;
13885 /* Look for the closing `)'. */
13886 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13888 return build_functional_cast (type
, expression_list
);
13891 /* MEMBER_FUNCTION is a member function, or a friend. If default
13892 arguments, or the body of the function have not yet been parsed,
13896 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
13898 cp_lexer
*saved_lexer
;
13900 /* If this member is a template, get the underlying
13902 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
13903 member_function
= DECL_TEMPLATE_RESULT (member_function
);
13905 /* There should not be any class definitions in progress at this
13906 point; the bodies of members are only parsed outside of all class
13908 my_friendly_assert (parser
->num_classes_being_defined
== 0, 20010816);
13909 /* While we're parsing the member functions we might encounter more
13910 classes. We want to handle them right away, but we don't want
13911 them getting mixed up with functions that are currently in the
13913 parser
->unparsed_functions_queues
13914 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
13916 /* Make sure that any template parameters are in scope. */
13917 maybe_begin_member_template_processing (member_function
);
13919 /* If the body of the function has not yet been parsed, parse it
13921 if (DECL_PENDING_INLINE_P (member_function
))
13923 tree function_scope
;
13924 cp_token_cache
*tokens
;
13926 /* The function is no longer pending; we are processing it. */
13927 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
13928 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
13929 DECL_PENDING_INLINE_P (member_function
) = 0;
13930 /* If this was an inline function in a local class, enter the scope
13931 of the containing function. */
13932 function_scope
= decl_function_context (member_function
);
13933 if (function_scope
)
13934 push_function_context_to (function_scope
);
13936 /* Save away the current lexer. */
13937 saved_lexer
= parser
->lexer
;
13938 /* Make a new lexer to feed us the tokens saved for this function. */
13939 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
13940 parser
->lexer
->next
= saved_lexer
;
13942 /* Set the current source position to be the location of the first
13943 token in the saved inline body. */
13944 cp_lexer_peek_token (parser
->lexer
);
13946 /* Let the front end know that we going to be defining this
13948 start_function (NULL_TREE
, member_function
, NULL_TREE
,
13949 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
13951 /* Now, parse the body of the function. */
13952 cp_parser_function_definition_after_declarator (parser
,
13953 /*inline_p=*/true);
13955 /* Leave the scope of the containing function. */
13956 if (function_scope
)
13957 pop_function_context_from (function_scope
);
13958 /* Restore the lexer. */
13959 parser
->lexer
= saved_lexer
;
13962 /* Remove any template parameters from the symbol table. */
13963 maybe_end_member_template_processing ();
13965 /* Restore the queue. */
13966 parser
->unparsed_functions_queues
13967 = TREE_CHAIN (parser
->unparsed_functions_queues
);
13970 /* If DECL contains any default args, remeber it on the unparsed
13971 functions queue. */
13974 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
13978 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
13980 probe
= TREE_CHAIN (probe
))
13981 if (TREE_PURPOSE (probe
))
13983 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13984 = tree_cons (NULL_TREE
, decl
,
13985 TREE_PURPOSE (parser
->unparsed_functions_queues
));
13991 /* FN is a FUNCTION_DECL which may contains a parameter with an
13992 unparsed DEFAULT_ARG. Parse the default args now. */
13995 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
13997 cp_lexer
*saved_lexer
;
13998 cp_token_cache
*tokens
;
13999 bool saved_local_variables_forbidden_p
;
14002 for (parameters
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
14004 parameters
= TREE_CHAIN (parameters
))
14006 if (!TREE_PURPOSE (parameters
)
14007 || TREE_CODE (TREE_PURPOSE (parameters
)) != DEFAULT_ARG
)
14010 /* Save away the current lexer. */
14011 saved_lexer
= parser
->lexer
;
14012 /* Create a new one, using the tokens we have saved. */
14013 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parameters
));
14014 parser
->lexer
= cp_lexer_new_from_tokens (tokens
);
14016 /* Set the current source position to be the location of the
14017 first token in the default argument. */
14018 cp_lexer_peek_token (parser
->lexer
);
14020 /* Local variable names (and the `this' keyword) may not appear
14021 in a default argument. */
14022 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
14023 parser
->local_variables_forbidden_p
= true;
14024 /* Parse the assignment-expression. */
14025 if (DECL_CONTEXT (fn
))
14026 push_nested_class (DECL_CONTEXT (fn
));
14027 TREE_PURPOSE (parameters
) = cp_parser_assignment_expression (parser
);
14028 if (DECL_CONTEXT (fn
))
14029 pop_nested_class ();
14031 /* Restore saved state. */
14032 parser
->lexer
= saved_lexer
;
14033 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
14037 /* Parse the operand of `sizeof' (or a similar operator). Returns
14038 either a TYPE or an expression, depending on the form of the
14039 input. The KEYWORD indicates which kind of expression we have
14043 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
14045 static const char *format
;
14046 tree expr
= NULL_TREE
;
14047 const char *saved_message
;
14048 bool saved_constant_expression_p
;
14050 /* Initialize FORMAT the first time we get here. */
14052 format
= "types may not be defined in `%s' expressions";
14054 /* Types cannot be defined in a `sizeof' expression. Save away the
14056 saved_message
= parser
->type_definition_forbidden_message
;
14057 /* And create the new one. */
14058 parser
->type_definition_forbidden_message
14060 xmalloc (strlen (format
)
14061 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
14063 sprintf ((char *) parser
->type_definition_forbidden_message
,
14064 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
14066 /* The restrictions on constant-expressions do not apply inside
14067 sizeof expressions. */
14068 saved_constant_expression_p
= parser
->constant_expression_p
;
14069 parser
->constant_expression_p
= false;
14071 /* Do not actually evaluate the expression. */
14073 /* If it's a `(', then we might be looking at the type-id
14075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14079 /* We can't be sure yet whether we're looking at a type-id or an
14081 cp_parser_parse_tentatively (parser
);
14082 /* Consume the `('. */
14083 cp_lexer_consume_token (parser
->lexer
);
14084 /* Parse the type-id. */
14085 type
= cp_parser_type_id (parser
);
14086 /* Now, look for the trailing `)'. */
14087 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14088 /* If all went well, then we're done. */
14089 if (cp_parser_parse_definitely (parser
))
14091 /* Build a list of decl-specifiers; right now, we have only
14092 a single type-specifier. */
14093 type
= build_tree_list (NULL_TREE
,
14096 /* Call grokdeclarator to figure out what type this is. */
14097 expr
= grokdeclarator (NULL_TREE
,
14101 /*attrlist=*/NULL
);
14105 /* If the type-id production did not work out, then we must be
14106 looking at the unary-expression production. */
14108 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
14109 /* Go back to evaluating expressions. */
14112 /* Free the message we created. */
14113 free ((char *) parser
->type_definition_forbidden_message
);
14114 /* And restore the old one. */
14115 parser
->type_definition_forbidden_message
= saved_message
;
14116 parser
->constant_expression_p
= saved_constant_expression_p
;
14121 /* If the current declaration has no declarator, return true. */
14124 cp_parser_declares_only_class_p (cp_parser
*parser
)
14126 /* If the next token is a `;' or a `,' then there is no
14128 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14129 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14132 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14133 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14136 cp_parser_friend_p (tree decl_specifiers
)
14138 while (decl_specifiers
)
14140 /* See if this decl-specifier is `friend'. */
14141 if (TREE_CODE (TREE_VALUE (decl_specifiers
)) == IDENTIFIER_NODE
14142 && C_RID_CODE (TREE_VALUE (decl_specifiers
)) == RID_FRIEND
)
14145 /* Go on to the next decl-specifier. */
14146 decl_specifiers
= TREE_CHAIN (decl_specifiers
);
14152 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14153 issue an error message indicating that TOKEN_DESC was expected.
14155 Returns the token consumed, if the token had the appropriate type.
14156 Otherwise, returns NULL. */
14159 cp_parser_require (cp_parser
* parser
,
14160 enum cpp_ttype type
,
14161 const char* token_desc
)
14163 if (cp_lexer_next_token_is (parser
->lexer
, type
))
14164 return cp_lexer_consume_token (parser
->lexer
);
14167 /* Output the MESSAGE -- unless we're parsing tentatively. */
14168 if (!cp_parser_simulate_error (parser
))
14169 error ("expected %s", token_desc
);
14174 /* Like cp_parser_require, except that tokens will be skipped until
14175 the desired token is found. An error message is still produced if
14176 the next token is not as expected. */
14179 cp_parser_skip_until_found (cp_parser
* parser
,
14180 enum cpp_ttype type
,
14181 const char* token_desc
)
14184 unsigned nesting_depth
= 0;
14186 if (cp_parser_require (parser
, type
, token_desc
))
14189 /* Skip tokens until the desired token is found. */
14192 /* Peek at the next token. */
14193 token
= cp_lexer_peek_token (parser
->lexer
);
14194 /* If we've reached the token we want, consume it and
14196 if (token
->type
== type
&& !nesting_depth
)
14198 cp_lexer_consume_token (parser
->lexer
);
14201 /* If we've run out of tokens, stop. */
14202 if (token
->type
== CPP_EOF
)
14204 if (token
->type
== CPP_OPEN_BRACE
14205 || token
->type
== CPP_OPEN_PAREN
14206 || token
->type
== CPP_OPEN_SQUARE
)
14208 else if (token
->type
== CPP_CLOSE_BRACE
14209 || token
->type
== CPP_CLOSE_PAREN
14210 || token
->type
== CPP_CLOSE_SQUARE
)
14212 if (nesting_depth
-- == 0)
14215 /* Consume this token. */
14216 cp_lexer_consume_token (parser
->lexer
);
14220 /* If the next token is the indicated keyword, consume it. Otherwise,
14221 issue an error message indicating that TOKEN_DESC was expected.
14223 Returns the token consumed, if the token had the appropriate type.
14224 Otherwise, returns NULL. */
14227 cp_parser_require_keyword (cp_parser
* parser
,
14229 const char* token_desc
)
14231 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
14233 if (token
&& token
->keyword
!= keyword
)
14235 dyn_string_t error_msg
;
14237 /* Format the error message. */
14238 error_msg
= dyn_string_new (0);
14239 dyn_string_append_cstr (error_msg
, "expected ");
14240 dyn_string_append_cstr (error_msg
, token_desc
);
14241 cp_parser_error (parser
, error_msg
->s
);
14242 dyn_string_delete (error_msg
);
14249 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14250 function-definition. */
14253 cp_parser_token_starts_function_definition_p (cp_token
* token
)
14255 return (/* An ordinary function-body begins with an `{'. */
14256 token
->type
== CPP_OPEN_BRACE
14257 /* A ctor-initializer begins with a `:'. */
14258 || token
->type
== CPP_COLON
14259 /* A function-try-block begins with `try'. */
14260 || token
->keyword
== RID_TRY
14261 /* The named return value extension begins with `return'. */
14262 || token
->keyword
== RID_RETURN
);
14265 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14269 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
14273 token
= cp_lexer_peek_token (parser
->lexer
);
14274 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
14277 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14278 or none_type otherwise. */
14280 static enum tag_types
14281 cp_parser_token_is_class_key (cp_token
* token
)
14283 switch (token
->keyword
)
14288 return record_type
;
14297 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14300 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
14302 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
14303 pedwarn ("`%s' tag used in naming `%#T'",
14304 class_key
== union_type
? "union"
14305 : class_key
== record_type
? "struct" : "class",
14309 /* Look for the `template' keyword, as a syntactic disambiguator.
14310 Return TRUE iff it is present, in which case it will be
14314 cp_parser_optional_template_keyword (cp_parser
*parser
)
14316 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14318 /* The `template' keyword can only be used within templates;
14319 outside templates the parser can always figure out what is a
14320 template and what is not. */
14321 if (!processing_template_decl
)
14323 error ("`template' (as a disambiguator) is only allowed "
14324 "within templates");
14325 /* If this part of the token stream is rescanned, the same
14326 error message would be generated. So, we purge the token
14327 from the stream. */
14328 cp_lexer_purge_token (parser
->lexer
);
14333 /* Consume the `template' keyword. */
14334 cp_lexer_consume_token (parser
->lexer
);
14342 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14343 set PARSER->SCOPE, and perform other related actions. */
14346 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
14351 /* Get the stored value. */
14352 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
14353 /* Perform any access checks that were deferred. */
14354 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
14355 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
14356 /* Set the scope from the stored value. */
14357 parser
->scope
= TREE_VALUE (value
);
14358 parser
->qualifying_scope
= TREE_TYPE (value
);
14359 parser
->object_scope
= NULL_TREE
;
14362 /* Add tokens to CACHE until an non-nested END token appears. */
14365 cp_parser_cache_group (cp_parser
*parser
,
14366 cp_token_cache
*cache
,
14367 enum cpp_ttype end
,
14374 /* Abort a parenthesized expression if we encounter a brace. */
14375 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
14376 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14378 /* Consume the next token. */
14379 token
= cp_lexer_consume_token (parser
->lexer
);
14380 /* If we've reached the end of the file, stop. */
14381 if (token
->type
== CPP_EOF
)
14383 /* Add this token to the tokens we are saving. */
14384 cp_token_cache_push_token (cache
, token
);
14385 /* See if it starts a new group. */
14386 if (token
->type
== CPP_OPEN_BRACE
)
14388 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_BRACE
, depth
+ 1);
14392 else if (token
->type
== CPP_OPEN_PAREN
)
14393 cp_parser_cache_group (parser
, cache
, CPP_CLOSE_PAREN
, depth
+ 1);
14394 else if (token
->type
== end
)
14399 /* Begin parsing tentatively. We always save tokens while parsing
14400 tentatively so that if the tentative parsing fails we can restore the
14404 cp_parser_parse_tentatively (cp_parser
* parser
)
14406 /* Enter a new parsing context. */
14407 parser
->context
= cp_parser_context_new (parser
->context
);
14408 /* Begin saving tokens. */
14409 cp_lexer_save_tokens (parser
->lexer
);
14410 /* In order to avoid repetitive access control error messages,
14411 access checks are queued up until we are no longer parsing
14413 push_deferring_access_checks (dk_deferred
);
14416 /* Commit to the currently active tentative parse. */
14419 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
14421 cp_parser_context
*context
;
14424 /* Mark all of the levels as committed. */
14425 lexer
= parser
->lexer
;
14426 for (context
= parser
->context
; context
->next
; context
= context
->next
)
14428 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
14430 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
14431 while (!cp_lexer_saving_tokens (lexer
))
14432 lexer
= lexer
->next
;
14433 cp_lexer_commit_tokens (lexer
);
14437 /* Abort the currently active tentative parse. All consumed tokens
14438 will be rolled back, and no diagnostics will be issued. */
14441 cp_parser_abort_tentative_parse (cp_parser
* parser
)
14443 cp_parser_simulate_error (parser
);
14444 /* Now, pretend that we want to see if the construct was
14445 successfully parsed. */
14446 cp_parser_parse_definitely (parser
);
14449 /* Stop parsing tentatively. If a parse error has occurred, restore the
14450 token stream. Otherwise, commit to the tokens we have consumed.
14451 Returns true if no error occurred; false otherwise. */
14454 cp_parser_parse_definitely (cp_parser
* parser
)
14456 bool error_occurred
;
14457 cp_parser_context
*context
;
14459 /* Remember whether or not an error occurred, since we are about to
14460 destroy that information. */
14461 error_occurred
= cp_parser_error_occurred (parser
);
14462 /* Remove the topmost context from the stack. */
14463 context
= parser
->context
;
14464 parser
->context
= context
->next
;
14465 /* If no parse errors occurred, commit to the tentative parse. */
14466 if (!error_occurred
)
14468 /* Commit to the tokens read tentatively, unless that was
14470 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
14471 cp_lexer_commit_tokens (parser
->lexer
);
14473 pop_to_parent_deferring_access_checks ();
14475 /* Otherwise, if errors occurred, roll back our state so that things
14476 are just as they were before we began the tentative parse. */
14479 cp_lexer_rollback_tokens (parser
->lexer
);
14480 pop_deferring_access_checks ();
14482 /* Add the context to the front of the free list. */
14483 context
->next
= cp_parser_context_free_list
;
14484 cp_parser_context_free_list
= context
;
14486 return !error_occurred
;
14489 /* Returns true if we are parsing tentatively -- but have decided that
14490 we will stick with this tentative parse, even if errors occur. */
14493 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
14495 return (cp_parser_parsing_tentatively (parser
)
14496 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
14499 /* Returns nonzero iff an error has occurred during the most recent
14500 tentative parse. */
14503 cp_parser_error_occurred (cp_parser
* parser
)
14505 return (cp_parser_parsing_tentatively (parser
)
14506 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
14509 /* Returns nonzero if GNU extensions are allowed. */
14512 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
14514 return parser
->allow_gnu_extensions_p
;
14521 static GTY (()) cp_parser
*the_parser
;
14523 /* External interface. */
14525 /* Parse the entire translation unit. */
14530 bool error_occurred
;
14532 the_parser
= cp_parser_new ();
14533 push_deferring_access_checks (flag_access_control
14534 ? dk_no_deferred
: dk_no_check
);
14535 error_occurred
= cp_parser_translation_unit (the_parser
);
14540 return error_occurred
;
14543 /* Clean up after parsing the entire translation unit. */
14546 free_parser_stacks (void)
14548 /* Nothing to do. */
14551 /* This variable must be provided by every front end. */
14555 #include "gt-cp-parser.h"