]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
re PR c++/13810 (ICE on invalid default templates)
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
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)
10 any later version.
11
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.
16
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
20 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37
38 \f
39 /* The lexer. */
40
41 /* Overview
42 --------
43
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
46
47 Methodology
48 -----------
49
50 We use a circular buffer to store incoming tokens.
51
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.
58
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.
61
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
64 circular buffer. */
65
66 /* A C++ token. */
67
68 typedef struct cp_token GTY (())
69 {
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
75 /* The value associated with this token, if any. */
76 tree value;
77 /* The location at which this token was found. */
78 location_t location;
79 } cp_token;
80
81 /* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
83
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
85
86 /* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
90 complete.)
91
92 This somewhat ungainly data structure (as opposed to, say, a
93 variable-length array), is used due to constraints imposed by the
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
96
97 typedef struct cp_token_block GTY (())
98 {
99 /* The tokens. */
100 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101 /* The number of tokens in this block. */
102 size_t num_tokens;
103 /* The next token block in the chain. */
104 struct cp_token_block *next;
105 /* The previous block in the chain. */
106 struct cp_token_block *prev;
107 } cp_token_block;
108
109 typedef struct cp_token_cache GTY (())
110 {
111 /* The first block in the cache. NULL if there are no tokens in the
112 cache. */
113 cp_token_block *first;
114 /* The last block in the cache. NULL If there are no tokens in the
115 cache. */
116 cp_token_block *last;
117 } cp_token_cache;
118
119 /* Prototypes. */
120
121 static cp_token_cache *cp_token_cache_new
122 (void);
123 static void cp_token_cache_push_token
124 (cp_token_cache *, cp_token *);
125
126 /* Create a new cp_token_cache. */
127
128 static cp_token_cache *
129 cp_token_cache_new (void)
130 {
131 return ggc_alloc_cleared (sizeof (cp_token_cache));
132 }
133
134 /* Add *TOKEN to *CACHE. */
135
136 static void
137 cp_token_cache_push_token (cp_token_cache *cache,
138 cp_token *token)
139 {
140 cp_token_block *b = cache->last;
141
142 /* See if we need to allocate a new token block. */
143 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
144 {
145 b = ggc_alloc_cleared (sizeof (cp_token_block));
146 b->prev = cache->last;
147 if (cache->last)
148 {
149 cache->last->next = b;
150 cache->last = b;
151 }
152 else
153 cache->first = cache->last = b;
154 }
155 /* Add this token to the current token block. */
156 b->tokens[b->num_tokens++] = *token;
157 }
158
159 /* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
161 it to the parser. */
162
163 typedef struct cp_lexer GTY (())
164 {
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token * GTY ((skip (""))) buffer_end;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token * GTY ((skip (""))) first_token;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token * GTY ((skip (""))) next_token;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token * GTY ((skip (""))) last_token;
181
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
186
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
191
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens;
196
197 /* The STRING_CST tokens encountered while processing the current
198 string literal. */
199 varray_type string_tokens;
200
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
203 bool main_lexer_p;
204
205 /* True if we should output debugging information. */
206 bool debugging_p;
207
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer *next;
210 } cp_lexer;
211
212 /* Prototypes. */
213
214 static cp_lexer *cp_lexer_new_main
215 (void);
216 static cp_lexer *cp_lexer_new_from_tokens
217 (struct cp_token_cache *);
218 static int cp_lexer_saving_tokens
219 (const cp_lexer *);
220 static cp_token *cp_lexer_next_token
221 (cp_lexer *, cp_token *);
222 static cp_token *cp_lexer_prev_token
223 (cp_lexer *, cp_token *);
224 static ptrdiff_t cp_lexer_token_difference
225 (cp_lexer *, cp_token *, cp_token *);
226 static cp_token *cp_lexer_read_token
227 (cp_lexer *);
228 static void cp_lexer_maybe_grow_buffer
229 (cp_lexer *);
230 static void cp_lexer_get_preprocessor_token
231 (cp_lexer *, cp_token *);
232 static cp_token *cp_lexer_peek_token
233 (cp_lexer *);
234 static cp_token *cp_lexer_peek_nth_token
235 (cp_lexer *, size_t);
236 static inline bool cp_lexer_next_token_is
237 (cp_lexer *, enum cpp_ttype);
238 static bool cp_lexer_next_token_is_not
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_keyword
241 (cp_lexer *, enum rid);
242 static cp_token *cp_lexer_consume_token
243 (cp_lexer *);
244 static void cp_lexer_purge_token
245 (cp_lexer *);
246 static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249 (cp_lexer *);
250 static void cp_lexer_commit_tokens
251 (cp_lexer *);
252 static void cp_lexer_rollback_tokens
253 (cp_lexer *);
254 static inline void cp_lexer_set_source_position_from_token
255 (cp_lexer *, const cp_token *);
256 static void cp_lexer_print_token
257 (FILE *, cp_token *);
258 static inline bool cp_lexer_debugging_p
259 (cp_lexer *);
260 static void cp_lexer_start_debugging
261 (cp_lexer *) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
264
265 /* Manifest constants. */
266
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
269
270 /* A token type for keywords, as opposed to ordinary identifiers. */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273 /* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279 /* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286 /* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290 /* Variables. */
291
292 /* The stream to which debugging output should be written. */
293 static FILE *cp_lexer_debug_stream;
294
295 /* Create a new main C++ lexer, the lexer that gets tokens from the
296 preprocessor. */
297
298 static cp_lexer *
299 cp_lexer_new_main (void)
300 {
301 cp_lexer *lexer;
302 cp_token first_token;
303
304 /* It's possible that lexing the first token will load a PCH file,
305 which is a GC collection point. So we have to grab the first
306 token before allocating any memory. */
307 cp_lexer_get_preprocessor_token (NULL, &first_token);
308 c_common_no_more_pch ();
309
310 /* Allocate the memory. */
311 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
312
313 /* Create the circular buffer. */
314 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
316
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
322
323 /* This lexer obtains more tokens by calling c_lex. */
324 lexer->main_lexer_p = true;
325
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
328
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
331
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
334
335 return lexer;
336 }
337
338 /* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
340
341 static cp_lexer *
342 cp_lexer_new_from_tokens (cp_token_cache *tokens)
343 {
344 cp_lexer *lexer;
345 cp_token *token;
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
348
349 /* Allocate the memory. */
350 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
351
352 /* Create a new buffer, appropriately sized. */
353 num_tokens = 0;
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
356 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
357 lexer->buffer_end = lexer->buffer + num_tokens;
358
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
362 {
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
365 }
366
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
373
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
376
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
379
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
382
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
385
386 return lexer;
387 }
388
389 /* Returns nonzero if debugging information should be output. */
390
391 static inline bool
392 cp_lexer_debugging_p (cp_lexer *lexer)
393 {
394 return lexer->debugging_p;
395 }
396
397 /* Set the current source position from the information stored in
398 TOKEN. */
399
400 static inline void
401 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
402 const cp_token *token)
403 {
404 /* Ideally, the source position information would not be a global
405 variable, but it is. */
406
407 /* Update the line number. */
408 if (token->type != CPP_EOF)
409 input_location = token->location;
410 }
411
412 /* TOKEN points into the circular token buffer. Return a pointer to
413 the next token in the buffer. */
414
415 static inline cp_token *
416 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
417 {
418 token++;
419 if (token == lexer->buffer_end)
420 token = lexer->buffer;
421 return token;
422 }
423
424 /* TOKEN points into the circular token buffer. Return a pointer to
425 the previous token in the buffer. */
426
427 static inline cp_token *
428 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
429 {
430 if (token == lexer->buffer)
431 token = lexer->buffer_end;
432 return token - 1;
433 }
434
435 /* nonzero if we are presently saving tokens. */
436
437 static int
438 cp_lexer_saving_tokens (const cp_lexer* lexer)
439 {
440 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
441 }
442
443 /* Return a pointer to the token that is N tokens beyond TOKEN in the
444 buffer. */
445
446 static cp_token *
447 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
448 {
449 token += n;
450 if (token >= lexer->buffer_end)
451 token = lexer->buffer + (token - lexer->buffer_end);
452 return token;
453 }
454
455 /* Returns the number of times that START would have to be incremented
456 to reach FINISH. If START and FINISH are the same, returns zero. */
457
458 static ptrdiff_t
459 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
460 {
461 if (finish >= start)
462 return finish - start;
463 else
464 return ((lexer->buffer_end - lexer->buffer)
465 - (start - finish));
466 }
467
468 /* Obtain another token from the C preprocessor and add it to the
469 token buffer. Returns the newly read token. */
470
471 static cp_token *
472 cp_lexer_read_token (cp_lexer* lexer)
473 {
474 cp_token *token;
475
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
478
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
485
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
488
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
491
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
494
495 /* Strings should have type `const char []'. Right now, we will
496 have an ARRAY_TYPE that is constant rather than an array of
497 constant elements.
498 FIXME: Make fix_string_type get this right in the first place. */
499 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
500 && flag_const_strings)
501 {
502 tree type;
503
504 /* Get the current type. It will be an ARRAY_TYPE. */
505 type = TREE_TYPE (token->value);
506 /* Use build_cplus_array_type to rebuild the array, thereby
507 getting the right type. */
508 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
509 /* Reset the type of the token. */
510 TREE_TYPE (token->value) = type;
511 }
512
513 return token;
514 }
515
516 /* If the circular buffer is full, make it bigger. */
517
518 static void
519 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
520 {
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
523 {
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
529
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
532 value. */
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
537 new_buffer = ggc_realloc (lexer->buffer,
538 2 * buffer_length * sizeof (cp_token));
539
540 /* Because the buffer is circular, logically consecutive tokens
541 are not necessarily placed consecutively in memory.
542 Therefore, we must keep move the tokens that were before
543 FIRST_TOKEN to the second half of the newly allocated
544 buffer. */
545 num_tokens_to_copy = (lexer->first_token - old_buffer);
546 memcpy (new_buffer + buffer_length,
547 new_buffer,
548 num_tokens_to_copy * sizeof (cp_token));
549 /* Clear the rest of the buffer. We never look at this storage,
550 but the garbage collector may. */
551 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
552 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
553
554 /* Now recompute all of the buffer pointers. */
555 new_first_token
556 = new_buffer + (lexer->first_token - old_buffer);
557 if (lexer->next_token != NULL)
558 {
559 ptrdiff_t next_token_delta;
560
561 if (lexer->next_token > lexer->first_token)
562 next_token_delta = lexer->next_token - lexer->first_token;
563 else
564 next_token_delta =
565 buffer_length - (lexer->first_token - lexer->next_token);
566 lexer->next_token = new_first_token + next_token_delta;
567 }
568 lexer->last_token = new_first_token + buffer_length;
569 lexer->buffer = new_buffer;
570 lexer->buffer_end = new_buffer + buffer_length * 2;
571 lexer->first_token = new_first_token;
572 }
573 }
574
575 /* Store the next token from the preprocessor in *TOKEN. */
576
577 static void
578 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
579 cp_token *token)
580 {
581 bool done;
582
583 /* If this not the main lexer, return a terminating CPP_EOF token. */
584 if (lexer != NULL && !lexer->main_lexer_p)
585 {
586 token->type = CPP_EOF;
587 token->location.line = 0;
588 token->location.file = NULL;
589 token->value = NULL_TREE;
590 token->keyword = RID_MAX;
591
592 return;
593 }
594
595 done = false;
596 /* Keep going until we get a token we like. */
597 while (!done)
598 {
599 /* Get a new token from the preprocessor. */
600 token->type = c_lex (&token->value);
601 /* Issue messages about tokens we cannot process. */
602 switch (token->type)
603 {
604 case CPP_ATSIGN:
605 case CPP_HASH:
606 case CPP_PASTE:
607 error ("invalid token");
608 break;
609
610 default:
611 /* This is a good token, so we exit the loop. */
612 done = true;
613 break;
614 }
615 }
616 /* Now we've got our token. */
617 token->location = input_location;
618
619 /* Check to see if this token is a keyword. */
620 if (token->type == CPP_NAME
621 && C_IS_RESERVED_WORD (token->value))
622 {
623 /* Mark this token as a keyword. */
624 token->type = CPP_KEYWORD;
625 /* Record which keyword. */
626 token->keyword = C_RID_CODE (token->value);
627 /* Update the value. Some keywords are mapped to particular
628 entities, rather than simply having the value of the
629 corresponding IDENTIFIER_NODE. For example, `__const' is
630 mapped to `const'. */
631 token->value = ridpointers[token->keyword];
632 }
633 else
634 token->keyword = RID_MAX;
635 }
636
637 /* Return a pointer to the next token in the token stream, but do not
638 consume it. */
639
640 static cp_token *
641 cp_lexer_peek_token (cp_lexer* lexer)
642 {
643 cp_token *token;
644
645 /* If there are no tokens, read one now. */
646 if (!lexer->next_token)
647 cp_lexer_read_token (lexer);
648
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
651 {
652 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
653 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
654 fprintf (cp_lexer_debug_stream, "\n");
655 }
656
657 token = lexer->next_token;
658 cp_lexer_set_source_position_from_token (lexer, token);
659 return token;
660 }
661
662 /* Return true if the next token has the indicated TYPE. */
663
664 static bool
665 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
666 {
667 cp_token *token;
668
669 /* Peek at the next token. */
670 token = cp_lexer_peek_token (lexer);
671 /* Check to see if it has the indicated TYPE. */
672 return token->type == type;
673 }
674
675 /* Return true if the next token does not have the indicated TYPE. */
676
677 static bool
678 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
679 {
680 return !cp_lexer_next_token_is (lexer, type);
681 }
682
683 /* Return true if the next token is the indicated KEYWORD. */
684
685 static bool
686 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
687 {
688 cp_token *token;
689
690 /* Peek at the next token. */
691 token = cp_lexer_peek_token (lexer);
692 /* Check to see if it is the indicated keyword. */
693 return token->keyword == keyword;
694 }
695
696 /* Return a pointer to the Nth token in the token stream. If N is 1,
697 then this is precisely equivalent to cp_lexer_peek_token. */
698
699 static cp_token *
700 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
701 {
702 cp_token *token;
703
704 /* N is 1-based, not zero-based. */
705 my_friendly_assert (n > 0, 20000224);
706
707 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
708 token = lexer->next_token;
709 /* If there are no tokens in the buffer, get one now. */
710 if (!token)
711 {
712 cp_lexer_read_token (lexer);
713 token = lexer->next_token;
714 }
715
716 /* Now, read tokens until we have enough. */
717 while (--n > 0)
718 {
719 /* Advance to the next token. */
720 token = cp_lexer_next_token (lexer, token);
721 /* If that's all the tokens we have, read a new one. */
722 if (token == lexer->last_token)
723 token = cp_lexer_read_token (lexer);
724 }
725
726 return token;
727 }
728
729 /* Consume the next token. The pointer returned is valid only until
730 another token is read. Callers should preserve copy the token
731 explicitly if they will need its value for a longer period of
732 time. */
733
734 static cp_token *
735 cp_lexer_consume_token (cp_lexer* lexer)
736 {
737 cp_token *token;
738
739 /* If there are no tokens, read one now. */
740 if (!lexer->next_token)
741 cp_lexer_read_token (lexer);
742
743 /* Remember the token we'll be returning. */
744 token = lexer->next_token;
745
746 /* Increment NEXT_TOKEN. */
747 lexer->next_token = cp_lexer_next_token (lexer,
748 lexer->next_token);
749 /* Check to see if we're all out of tokens. */
750 if (lexer->next_token == lexer->last_token)
751 lexer->next_token = NULL;
752
753 /* If we're not saving tokens, then move FIRST_TOKEN too. */
754 if (!cp_lexer_saving_tokens (lexer))
755 {
756 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
757 if (!lexer->next_token)
758 lexer->first_token = NULL;
759 else
760 lexer->first_token = lexer->next_token;
761 }
762
763 /* Provide debugging output. */
764 if (cp_lexer_debugging_p (lexer))
765 {
766 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
767 cp_lexer_print_token (cp_lexer_debug_stream, token);
768 fprintf (cp_lexer_debug_stream, "\n");
769 }
770
771 return token;
772 }
773
774 /* Permanently remove the next token from the token stream. There
775 must be a valid next token already; this token never reads
776 additional tokens from the preprocessor. */
777
778 static void
779 cp_lexer_purge_token (cp_lexer *lexer)
780 {
781 cp_token *token;
782 cp_token *next_token;
783
784 token = lexer->next_token;
785 while (true)
786 {
787 next_token = cp_lexer_next_token (lexer, token);
788 if (next_token == lexer->last_token)
789 break;
790 *token = *next_token;
791 token = next_token;
792 }
793
794 lexer->last_token = token;
795 /* The token purged may have been the only token remaining; if so,
796 clear NEXT_TOKEN. */
797 if (lexer->next_token == token)
798 lexer->next_token = NULL;
799 }
800
801 /* Permanently remove all tokens after TOKEN, up to, but not
802 including, the token that will be returned next by
803 cp_lexer_peek_token. */
804
805 static void
806 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
807 {
808 cp_token *peek;
809 cp_token *t1;
810 cp_token *t2;
811
812 if (lexer->next_token)
813 {
814 /* Copy the tokens that have not yet been read to the location
815 immediately following TOKEN. */
816 t1 = cp_lexer_next_token (lexer, token);
817 t2 = peek = cp_lexer_peek_token (lexer);
818 /* Move tokens into the vacant area between TOKEN and PEEK. */
819 while (t2 != lexer->last_token)
820 {
821 *t1 = *t2;
822 t1 = cp_lexer_next_token (lexer, t1);
823 t2 = cp_lexer_next_token (lexer, t2);
824 }
825 /* Now, the next available token is right after TOKEN. */
826 lexer->next_token = cp_lexer_next_token (lexer, token);
827 /* And the last token is wherever we ended up. */
828 lexer->last_token = t1;
829 }
830 else
831 {
832 /* There are no tokens in the buffer, so there is nothing to
833 copy. The last token in the buffer is TOKEN itself. */
834 lexer->last_token = cp_lexer_next_token (lexer, token);
835 }
836 }
837
838 /* Begin saving tokens. All tokens consumed after this point will be
839 preserved. */
840
841 static void
842 cp_lexer_save_tokens (cp_lexer* lexer)
843 {
844 /* Provide debugging output. */
845 if (cp_lexer_debugging_p (lexer))
846 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
847
848 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
849 restore the tokens if required. */
850 if (!lexer->next_token)
851 cp_lexer_read_token (lexer);
852
853 VARRAY_PUSH_INT (lexer->saved_tokens,
854 cp_lexer_token_difference (lexer,
855 lexer->first_token,
856 lexer->next_token));
857 }
858
859 /* Commit to the portion of the token stream most recently saved. */
860
861 static void
862 cp_lexer_commit_tokens (cp_lexer* lexer)
863 {
864 /* Provide debugging output. */
865 if (cp_lexer_debugging_p (lexer))
866 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
867
868 VARRAY_POP (lexer->saved_tokens);
869 }
870
871 /* Return all tokens saved since the last call to cp_lexer_save_tokens
872 to the token stream. Stop saving tokens. */
873
874 static void
875 cp_lexer_rollback_tokens (cp_lexer* lexer)
876 {
877 size_t delta;
878
879 /* Provide debugging output. */
880 if (cp_lexer_debugging_p (lexer))
881 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
882
883 /* Find the token that was the NEXT_TOKEN when we started saving
884 tokens. */
885 delta = VARRAY_TOP_INT(lexer->saved_tokens);
886 /* Make it the next token again now. */
887 lexer->next_token = cp_lexer_advance_token (lexer,
888 lexer->first_token,
889 delta);
890 /* It might be the case that there were no tokens when we started
891 saving tokens, but that there are some tokens now. */
892 if (!lexer->next_token && lexer->first_token)
893 lexer->next_token = lexer->first_token;
894
895 /* Stop saving tokens. */
896 VARRAY_POP (lexer->saved_tokens);
897 }
898
899 /* Print a representation of the TOKEN on the STREAM. */
900
901 static void
902 cp_lexer_print_token (FILE * stream, cp_token* token)
903 {
904 const char *token_type = NULL;
905
906 /* Figure out what kind of token this is. */
907 switch (token->type)
908 {
909 case CPP_EQ:
910 token_type = "EQ";
911 break;
912
913 case CPP_COMMA:
914 token_type = "COMMA";
915 break;
916
917 case CPP_OPEN_PAREN:
918 token_type = "OPEN_PAREN";
919 break;
920
921 case CPP_CLOSE_PAREN:
922 token_type = "CLOSE_PAREN";
923 break;
924
925 case CPP_OPEN_BRACE:
926 token_type = "OPEN_BRACE";
927 break;
928
929 case CPP_CLOSE_BRACE:
930 token_type = "CLOSE_BRACE";
931 break;
932
933 case CPP_SEMICOLON:
934 token_type = "SEMICOLON";
935 break;
936
937 case CPP_NAME:
938 token_type = "NAME";
939 break;
940
941 case CPP_EOF:
942 token_type = "EOF";
943 break;
944
945 case CPP_KEYWORD:
946 token_type = "keyword";
947 break;
948
949 /* This is not a token that we know how to handle yet. */
950 default:
951 break;
952 }
953
954 /* If we have a name for the token, print it out. Otherwise, we
955 simply give the numeric code. */
956 if (token_type)
957 fprintf (stream, "%s", token_type);
958 else
959 fprintf (stream, "%d", token->type);
960 /* And, for an identifier, print the identifier name. */
961 if (token->type == CPP_NAME
962 /* Some keywords have a value that is not an IDENTIFIER_NODE.
963 For example, `struct' is mapped to an INTEGER_CST. */
964 || (token->type == CPP_KEYWORD
965 && TREE_CODE (token->value) == IDENTIFIER_NODE))
966 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
967 }
968
969 /* Start emitting debugging information. */
970
971 static void
972 cp_lexer_start_debugging (cp_lexer* lexer)
973 {
974 ++lexer->debugging_p;
975 }
976
977 /* Stop emitting debugging information. */
978
979 static void
980 cp_lexer_stop_debugging (cp_lexer* lexer)
981 {
982 --lexer->debugging_p;
983 }
984
985 \f
986 /* The parser. */
987
988 /* Overview
989 --------
990
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
998
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1001 code processed.
1002
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1011
1012 Methodology
1013 -----------
1014
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1031
1032 Future Improvements
1033 -------------------
1034
1035 The performance of the parser could probably be improved
1036 substantially. Some possible improvements include:
1037
1038 - The expression parser recurses through the various levels of
1039 precedence as specified in the grammar, rather than using an
1040 operator-precedence technique. Therefore, parsing a simple
1041 identifier requires multiple recursive calls.
1042
1043 - We could often eliminate the need to parse tentatively by
1044 looking ahead a little bit. In some places, this approach
1045 might not entirely eliminate the need to parse tentatively, but
1046 it might still speed up the average case. */
1047
1048 /* Flags that are passed to some parsing functions. These values can
1049 be bitwise-ored together. */
1050
1051 typedef enum cp_parser_flags
1052 {
1053 /* No flags. */
1054 CP_PARSER_FLAGS_NONE = 0x0,
1055 /* The construct is optional. If it is not present, then no error
1056 should be issued. */
1057 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1058 /* When parsing a type-specifier, do not allow user-defined types. */
1059 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1060 } cp_parser_flags;
1061
1062 /* The different kinds of declarators we want to parse. */
1063
1064 typedef enum cp_parser_declarator_kind
1065 {
1066 /* We want an abstract declartor. */
1067 CP_PARSER_DECLARATOR_ABSTRACT,
1068 /* We want a named declarator. */
1069 CP_PARSER_DECLARATOR_NAMED,
1070 /* We don't mind, but the name must be an unqualified-id. */
1071 CP_PARSER_DECLARATOR_EITHER
1072 } cp_parser_declarator_kind;
1073
1074 /* A mapping from a token type to a corresponding tree node type. */
1075
1076 typedef struct cp_parser_token_tree_map_node
1077 {
1078 /* The token type. */
1079 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1080 /* The corresponding tree code. */
1081 ENUM_BITFIELD (tree_code) tree_type : 8;
1082 } cp_parser_token_tree_map_node;
1083
1084 /* A complete map consists of several ordinary entries, followed by a
1085 terminator. The terminating entry has a token_type of CPP_EOF. */
1086
1087 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1088
1089 /* The status of a tentative parse. */
1090
1091 typedef enum cp_parser_status_kind
1092 {
1093 /* No errors have occurred. */
1094 CP_PARSER_STATUS_KIND_NO_ERROR,
1095 /* An error has occurred. */
1096 CP_PARSER_STATUS_KIND_ERROR,
1097 /* We are committed to this tentative parse, whether or not an error
1098 has occurred. */
1099 CP_PARSER_STATUS_KIND_COMMITTED
1100 } cp_parser_status_kind;
1101
1102 /* Context that is saved and restored when parsing tentatively. */
1103
1104 typedef struct cp_parser_context GTY (())
1105 {
1106 /* If this is a tentative parsing context, the status of the
1107 tentative parse. */
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1113 tree object_type;
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context *next;
1116 } cp_parser_context;
1117
1118 /* Prototypes. */
1119
1120 /* Constructors and destructors. */
1121
1122 static cp_parser_context *cp_parser_context_new
1123 (cp_parser_context *);
1124
1125 /* Class variables. */
1126
1127 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1128
1129 /* Constructors and destructors. */
1130
1131 /* Construct a new context. The context below this one on the stack
1132 is given by NEXT. */
1133
1134 static cp_parser_context *
1135 cp_parser_context_new (cp_parser_context* next)
1136 {
1137 cp_parser_context *context;
1138
1139 /* Allocate the storage. */
1140 if (cp_parser_context_free_list != NULL)
1141 {
1142 /* Pull the first entry from the free list. */
1143 context = cp_parser_context_free_list;
1144 cp_parser_context_free_list = context->next;
1145 memset (context, 0, sizeof (*context));
1146 }
1147 else
1148 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1149 /* No errors have occurred yet in this context. */
1150 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1151 /* If this is not the bottomost context, copy information that we
1152 need from the previous context. */
1153 if (next)
1154 {
1155 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1156 expression, then we are parsing one in this context, too. */
1157 context->object_type = next->object_type;
1158 /* Thread the stack. */
1159 context->next = next;
1160 }
1161
1162 return context;
1163 }
1164
1165 /* The cp_parser structure represents the C++ parser. */
1166
1167 typedef struct cp_parser GTY(())
1168 {
1169 /* The lexer from which we are obtaining tokens. */
1170 cp_lexer *lexer;
1171
1172 /* The scope in which names should be looked up. If NULL_TREE, then
1173 we look up names in the scope that is currently open in the
1174 source program. If non-NULL, this is either a TYPE or
1175 NAMESPACE_DECL for the scope in which we should look.
1176
1177 This value is not cleared automatically after a name is looked
1178 up, so we must be careful to clear it before starting a new look
1179 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1180 will look up `Z' in the scope of `X', rather than the current
1181 scope.) Unfortunately, it is difficult to tell when name lookup
1182 is complete, because we sometimes peek at a token, look it up,
1183 and then decide not to consume it. */
1184 tree scope;
1185
1186 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1187 last lookup took place. OBJECT_SCOPE is used if an expression
1188 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1189 respectively. QUALIFYING_SCOPE is used for an expression of the
1190 form "X::Y"; it refers to X. */
1191 tree object_scope;
1192 tree qualifying_scope;
1193
1194 /* A stack of parsing contexts. All but the bottom entry on the
1195 stack will be tentative contexts.
1196
1197 We parse tentatively in order to determine which construct is in
1198 use in some situations. For example, in order to determine
1199 whether a statement is an expression-statement or a
1200 declaration-statement we parse it tentatively as a
1201 declaration-statement. If that fails, we then reparse the same
1202 token stream as an expression-statement. */
1203 cp_parser_context *context;
1204
1205 /* True if we are parsing GNU C++. If this flag is not set, then
1206 GNU extensions are not recognized. */
1207 bool allow_gnu_extensions_p;
1208
1209 /* TRUE if the `>' token should be interpreted as the greater-than
1210 operator. FALSE if it is the end of a template-id or
1211 template-parameter-list. */
1212 bool greater_than_is_operator_p;
1213
1214 /* TRUE if default arguments are allowed within a parameter list
1215 that starts at this point. FALSE if only a gnu extension makes
1216 them permissible. */
1217 bool default_arg_ok_p;
1218
1219 /* TRUE if we are parsing an integral constant-expression. See
1220 [expr.const] for a precise definition. */
1221 bool integral_constant_expression_p;
1222
1223 /* TRUE if we are parsing an integral constant-expression -- but a
1224 non-constant expression should be permitted as well. This flag
1225 is used when parsing an array bound so that GNU variable-length
1226 arrays are tolerated. */
1227 bool allow_non_integral_constant_expression_p;
1228
1229 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1230 been seen that makes the expression non-constant. */
1231 bool non_integral_constant_expression_p;
1232
1233 /* TRUE if we are parsing the argument to "__offsetof__". */
1234 bool in_offsetof_p;
1235
1236 /* TRUE if local variable names and `this' are forbidden in the
1237 current context. */
1238 bool local_variables_forbidden_p;
1239
1240 /* TRUE if the declaration we are parsing is part of a
1241 linkage-specification of the form `extern string-literal
1242 declaration'. */
1243 bool in_unbraced_linkage_specification_p;
1244
1245 /* TRUE if we are presently parsing a declarator, after the
1246 direct-declarator. */
1247 bool in_declarator_p;
1248
1249 /* TRUE if we are presently parsing a template-argument-list. */
1250 bool in_template_argument_list_p;
1251
1252 /* TRUE if we are presently parsing the body of an
1253 iteration-statement. */
1254 bool in_iteration_statement_p;
1255
1256 /* TRUE if we are presently parsing the body of a switch
1257 statement. */
1258 bool in_switch_statement_p;
1259
1260 /* TRUE if we are parsing a type-id in an expression context. In
1261 such a situation, both "type (expr)" and "type (type)" are valid
1262 alternatives. */
1263 bool in_type_id_in_expr_p;
1264
1265 /* If non-NULL, then we are parsing a construct where new type
1266 definitions are not permitted. The string stored here will be
1267 issued as an error message if a type is defined. */
1268 const char *type_definition_forbidden_message;
1269
1270 /* A list of lists. The outer list is a stack, used for member
1271 functions of local classes. At each level there are two sub-list,
1272 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1273 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1274 TREE_VALUE's. The functions are chained in reverse declaration
1275 order.
1276
1277 The TREE_PURPOSE sublist contains those functions with default
1278 arguments that need post processing, and the TREE_VALUE sublist
1279 contains those functions with definitions that need post
1280 processing.
1281
1282 These lists can only be processed once the outermost class being
1283 defined is complete. */
1284 tree unparsed_functions_queues;
1285
1286 /* The number of classes whose definitions are currently in
1287 progress. */
1288 unsigned num_classes_being_defined;
1289
1290 /* The number of template parameter lists that apply directly to the
1291 current declaration. */
1292 unsigned num_template_parameter_lists;
1293 } cp_parser;
1294
1295 /* The type of a function that parses some kind of expression. */
1296 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1297
1298 /* Prototypes. */
1299
1300 /* Constructors and destructors. */
1301
1302 static cp_parser *cp_parser_new
1303 (void);
1304
1305 /* Routines to parse various constructs.
1306
1307 Those that return `tree' will return the error_mark_node (rather
1308 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1309 Sometimes, they will return an ordinary node if error-recovery was
1310 attempted, even though a parse error occurred. So, to check
1311 whether or not a parse error occurred, you should always use
1312 cp_parser_error_occurred. If the construct is optional (indicated
1313 either by an `_opt' in the name of the function that does the
1314 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1315 the construct is not present. */
1316
1317 /* Lexical conventions [gram.lex] */
1318
1319 static tree cp_parser_identifier
1320 (cp_parser *);
1321
1322 /* Basic concepts [gram.basic] */
1323
1324 static bool cp_parser_translation_unit
1325 (cp_parser *);
1326
1327 /* Expressions [gram.expr] */
1328
1329 static tree cp_parser_primary_expression
1330 (cp_parser *, cp_id_kind *, tree *);
1331 static tree cp_parser_id_expression
1332 (cp_parser *, bool, bool, bool *, bool);
1333 static tree cp_parser_unqualified_id
1334 (cp_parser *, bool, bool, bool);
1335 static tree cp_parser_nested_name_specifier_opt
1336 (cp_parser *, bool, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier
1338 (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_class_or_namespace_name
1340 (cp_parser *, bool, bool, bool, bool, bool);
1341 static tree cp_parser_postfix_expression
1342 (cp_parser *, bool);
1343 static tree cp_parser_parenthesized_expression_list
1344 (cp_parser *, bool, bool *);
1345 static void cp_parser_pseudo_destructor_name
1346 (cp_parser *, tree *, tree *);
1347 static tree cp_parser_unary_expression
1348 (cp_parser *, bool);
1349 static enum tree_code cp_parser_unary_operator
1350 (cp_token *);
1351 static tree cp_parser_new_expression
1352 (cp_parser *);
1353 static tree cp_parser_new_placement
1354 (cp_parser *);
1355 static tree cp_parser_new_type_id
1356 (cp_parser *);
1357 static tree cp_parser_new_declarator_opt
1358 (cp_parser *);
1359 static tree cp_parser_direct_new_declarator
1360 (cp_parser *);
1361 static tree cp_parser_new_initializer
1362 (cp_parser *);
1363 static tree cp_parser_delete_expression
1364 (cp_parser *);
1365 static tree cp_parser_cast_expression
1366 (cp_parser *, bool);
1367 static tree cp_parser_pm_expression
1368 (cp_parser *);
1369 static tree cp_parser_multiplicative_expression
1370 (cp_parser *);
1371 static tree cp_parser_additive_expression
1372 (cp_parser *);
1373 static tree cp_parser_shift_expression
1374 (cp_parser *);
1375 static tree cp_parser_relational_expression
1376 (cp_parser *);
1377 static tree cp_parser_equality_expression
1378 (cp_parser *);
1379 static tree cp_parser_and_expression
1380 (cp_parser *);
1381 static tree cp_parser_exclusive_or_expression
1382 (cp_parser *);
1383 static tree cp_parser_inclusive_or_expression
1384 (cp_parser *);
1385 static tree cp_parser_logical_and_expression
1386 (cp_parser *);
1387 static tree cp_parser_logical_or_expression
1388 (cp_parser *);
1389 static tree cp_parser_question_colon_clause
1390 (cp_parser *, tree);
1391 static tree cp_parser_assignment_expression
1392 (cp_parser *);
1393 static enum tree_code cp_parser_assignment_operator_opt
1394 (cp_parser *);
1395 static tree cp_parser_expression
1396 (cp_parser *);
1397 static tree cp_parser_constant_expression
1398 (cp_parser *, bool, bool *);
1399
1400 /* Statements [gram.stmt.stmt] */
1401
1402 static void cp_parser_statement
1403 (cp_parser *, bool);
1404 static tree cp_parser_labeled_statement
1405 (cp_parser *, bool);
1406 static tree cp_parser_expression_statement
1407 (cp_parser *, bool);
1408 static tree cp_parser_compound_statement
1409 (cp_parser *, bool);
1410 static void cp_parser_statement_seq_opt
1411 (cp_parser *, bool);
1412 static tree cp_parser_selection_statement
1413 (cp_parser *);
1414 static tree cp_parser_condition
1415 (cp_parser *);
1416 static tree cp_parser_iteration_statement
1417 (cp_parser *);
1418 static void cp_parser_for_init_statement
1419 (cp_parser *);
1420 static tree cp_parser_jump_statement
1421 (cp_parser *);
1422 static void cp_parser_declaration_statement
1423 (cp_parser *);
1424
1425 static tree cp_parser_implicitly_scoped_statement
1426 (cp_parser *);
1427 static void cp_parser_already_scoped_statement
1428 (cp_parser *);
1429
1430 /* Declarations [gram.dcl.dcl] */
1431
1432 static void cp_parser_declaration_seq_opt
1433 (cp_parser *);
1434 static void cp_parser_declaration
1435 (cp_parser *);
1436 static void cp_parser_block_declaration
1437 (cp_parser *, bool);
1438 static void cp_parser_simple_declaration
1439 (cp_parser *, bool);
1440 static tree cp_parser_decl_specifier_seq
1441 (cp_parser *, cp_parser_flags, tree *, int *);
1442 static tree cp_parser_storage_class_specifier_opt
1443 (cp_parser *);
1444 static tree cp_parser_function_specifier_opt
1445 (cp_parser *);
1446 static tree cp_parser_type_specifier
1447 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1448 static tree cp_parser_simple_type_specifier
1449 (cp_parser *, cp_parser_flags, bool);
1450 static tree cp_parser_type_name
1451 (cp_parser *);
1452 static tree cp_parser_elaborated_type_specifier
1453 (cp_parser *, bool, bool);
1454 static tree cp_parser_enum_specifier
1455 (cp_parser *);
1456 static void cp_parser_enumerator_list
1457 (cp_parser *, tree);
1458 static void cp_parser_enumerator_definition
1459 (cp_parser *, tree);
1460 static tree cp_parser_namespace_name
1461 (cp_parser *);
1462 static void cp_parser_namespace_definition
1463 (cp_parser *);
1464 static void cp_parser_namespace_body
1465 (cp_parser *);
1466 static tree cp_parser_qualified_namespace_specifier
1467 (cp_parser *);
1468 static void cp_parser_namespace_alias_definition
1469 (cp_parser *);
1470 static void cp_parser_using_declaration
1471 (cp_parser *);
1472 static void cp_parser_using_directive
1473 (cp_parser *);
1474 static void cp_parser_asm_definition
1475 (cp_parser *);
1476 static void cp_parser_linkage_specification
1477 (cp_parser *);
1478
1479 /* Declarators [gram.dcl.decl] */
1480
1481 static tree cp_parser_init_declarator
1482 (cp_parser *, tree, tree, bool, bool, int, bool *);
1483 static tree cp_parser_declarator
1484 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1485 static tree cp_parser_direct_declarator
1486 (cp_parser *, cp_parser_declarator_kind, int *);
1487 static enum tree_code cp_parser_ptr_operator
1488 (cp_parser *, tree *, tree *);
1489 static tree cp_parser_cv_qualifier_seq_opt
1490 (cp_parser *);
1491 static tree cp_parser_cv_qualifier_opt
1492 (cp_parser *);
1493 static tree cp_parser_declarator_id
1494 (cp_parser *);
1495 static tree cp_parser_type_id
1496 (cp_parser *);
1497 static tree cp_parser_type_specifier_seq
1498 (cp_parser *);
1499 static tree cp_parser_parameter_declaration_clause
1500 (cp_parser *);
1501 static tree cp_parser_parameter_declaration_list
1502 (cp_parser *);
1503 static tree cp_parser_parameter_declaration
1504 (cp_parser *, bool, bool *);
1505 static void cp_parser_function_body
1506 (cp_parser *);
1507 static tree cp_parser_initializer
1508 (cp_parser *, bool *, bool *);
1509 static tree cp_parser_initializer_clause
1510 (cp_parser *, bool *);
1511 static tree cp_parser_initializer_list
1512 (cp_parser *, bool *);
1513
1514 static bool cp_parser_ctor_initializer_opt_and_function_body
1515 (cp_parser *);
1516
1517 /* Classes [gram.class] */
1518
1519 static tree cp_parser_class_name
1520 (cp_parser *, bool, bool, bool, bool, bool, bool);
1521 static tree cp_parser_class_specifier
1522 (cp_parser *);
1523 static tree cp_parser_class_head
1524 (cp_parser *, bool *);
1525 static enum tag_types cp_parser_class_key
1526 (cp_parser *);
1527 static void cp_parser_member_specification_opt
1528 (cp_parser *);
1529 static void cp_parser_member_declaration
1530 (cp_parser *);
1531 static tree cp_parser_pure_specifier
1532 (cp_parser *);
1533 static tree cp_parser_constant_initializer
1534 (cp_parser *);
1535
1536 /* Derived classes [gram.class.derived] */
1537
1538 static tree cp_parser_base_clause
1539 (cp_parser *);
1540 static tree cp_parser_base_specifier
1541 (cp_parser *);
1542
1543 /* Special member functions [gram.special] */
1544
1545 static tree cp_parser_conversion_function_id
1546 (cp_parser *);
1547 static tree cp_parser_conversion_type_id
1548 (cp_parser *);
1549 static tree cp_parser_conversion_declarator_opt
1550 (cp_parser *);
1551 static bool cp_parser_ctor_initializer_opt
1552 (cp_parser *);
1553 static void cp_parser_mem_initializer_list
1554 (cp_parser *);
1555 static tree cp_parser_mem_initializer
1556 (cp_parser *);
1557 static tree cp_parser_mem_initializer_id
1558 (cp_parser *);
1559
1560 /* Overloading [gram.over] */
1561
1562 static tree cp_parser_operator_function_id
1563 (cp_parser *);
1564 static tree cp_parser_operator
1565 (cp_parser *);
1566
1567 /* Templates [gram.temp] */
1568
1569 static void cp_parser_template_declaration
1570 (cp_parser *, bool);
1571 static tree cp_parser_template_parameter_list
1572 (cp_parser *);
1573 static tree cp_parser_template_parameter
1574 (cp_parser *);
1575 static tree cp_parser_type_parameter
1576 (cp_parser *);
1577 static tree cp_parser_template_id
1578 (cp_parser *, bool, bool, bool);
1579 static tree cp_parser_template_name
1580 (cp_parser *, bool, bool, bool, bool *);
1581 static tree cp_parser_template_argument_list
1582 (cp_parser *);
1583 static tree cp_parser_template_argument
1584 (cp_parser *);
1585 static void cp_parser_explicit_instantiation
1586 (cp_parser *);
1587 static void cp_parser_explicit_specialization
1588 (cp_parser *);
1589
1590 /* Exception handling [gram.exception] */
1591
1592 static tree cp_parser_try_block
1593 (cp_parser *);
1594 static bool cp_parser_function_try_block
1595 (cp_parser *);
1596 static void cp_parser_handler_seq
1597 (cp_parser *);
1598 static void cp_parser_handler
1599 (cp_parser *);
1600 static tree cp_parser_exception_declaration
1601 (cp_parser *);
1602 static tree cp_parser_throw_expression
1603 (cp_parser *);
1604 static tree cp_parser_exception_specification_opt
1605 (cp_parser *);
1606 static tree cp_parser_type_id_list
1607 (cp_parser *);
1608
1609 /* GNU Extensions */
1610
1611 static tree cp_parser_asm_specification_opt
1612 (cp_parser *);
1613 static tree cp_parser_asm_operand_list
1614 (cp_parser *);
1615 static tree cp_parser_asm_clobber_list
1616 (cp_parser *);
1617 static tree cp_parser_attributes_opt
1618 (cp_parser *);
1619 static tree cp_parser_attribute_list
1620 (cp_parser *);
1621 static bool cp_parser_extension_opt
1622 (cp_parser *, int *);
1623 static void cp_parser_label_declaration
1624 (cp_parser *);
1625
1626 /* Utility Routines */
1627
1628 static tree cp_parser_lookup_name
1629 (cp_parser *, tree, bool, bool, bool, bool);
1630 static tree cp_parser_lookup_name_simple
1631 (cp_parser *, tree);
1632 static tree cp_parser_maybe_treat_template_as_class
1633 (tree, bool);
1634 static bool cp_parser_check_declarator_template_parameters
1635 (cp_parser *, tree);
1636 static bool cp_parser_check_template_parameters
1637 (cp_parser *, unsigned);
1638 static tree cp_parser_simple_cast_expression
1639 (cp_parser *);
1640 static tree cp_parser_binary_expression
1641 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1642 static tree cp_parser_global_scope_opt
1643 (cp_parser *, bool);
1644 static bool cp_parser_constructor_declarator_p
1645 (cp_parser *, bool);
1646 static tree cp_parser_function_definition_from_specifiers_and_declarator
1647 (cp_parser *, tree, tree, tree);
1648 static tree cp_parser_function_definition_after_declarator
1649 (cp_parser *, bool);
1650 static void cp_parser_template_declaration_after_export
1651 (cp_parser *, bool);
1652 static tree cp_parser_single_declaration
1653 (cp_parser *, bool, bool *);
1654 static tree cp_parser_functional_cast
1655 (cp_parser *, tree);
1656 static tree cp_parser_save_member_function_body
1657 (cp_parser *, tree, tree, tree);
1658 static tree cp_parser_enclosed_template_argument_list
1659 (cp_parser *);
1660 static void cp_parser_save_default_args
1661 (cp_parser *, tree);
1662 static void cp_parser_late_parsing_for_member
1663 (cp_parser *, tree);
1664 static void cp_parser_late_parsing_default_args
1665 (cp_parser *, tree);
1666 static tree cp_parser_sizeof_operand
1667 (cp_parser *, enum rid);
1668 static bool cp_parser_declares_only_class_p
1669 (cp_parser *);
1670 static tree cp_parser_fold_non_dependent_expr
1671 (tree);
1672 static bool cp_parser_friend_p
1673 (tree);
1674 static cp_token *cp_parser_require
1675 (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677 (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1679 (cp_token *);
1680 static bool cp_parser_next_token_starts_class_definition_p
1681 (cp_parser *);
1682 static bool cp_parser_next_token_ends_template_argument_p
1683 (cp_parser *);
1684 static enum tag_types cp_parser_token_is_class_key
1685 (cp_token *);
1686 static void cp_parser_check_class_key
1687 (enum tag_types, tree type);
1688 static void cp_parser_check_access_in_redeclaration
1689 (tree type);
1690 static bool cp_parser_optional_template_keyword
1691 (cp_parser *);
1692 static void cp_parser_pre_parsed_nested_name_specifier
1693 (cp_parser *);
1694 static void cp_parser_cache_group
1695 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1696 static void cp_parser_parse_tentatively
1697 (cp_parser *);
1698 static void cp_parser_commit_to_tentative_parse
1699 (cp_parser *);
1700 static void cp_parser_abort_tentative_parse
1701 (cp_parser *);
1702 static bool cp_parser_parse_definitely
1703 (cp_parser *);
1704 static inline bool cp_parser_parsing_tentatively
1705 (cp_parser *);
1706 static bool cp_parser_committed_to_tentative_parse
1707 (cp_parser *);
1708 static void cp_parser_error
1709 (cp_parser *, const char *);
1710 static void cp_parser_name_lookup_error
1711 (cp_parser *, tree, tree, const char *);
1712 static bool cp_parser_simulate_error
1713 (cp_parser *);
1714 static void cp_parser_check_type_definition
1715 (cp_parser *);
1716 static void cp_parser_check_for_definition_in_return_type
1717 (tree, int);
1718 static void cp_parser_check_for_invalid_template_id
1719 (cp_parser *, tree);
1720 static tree cp_parser_non_integral_constant_expression
1721 (const char *);
1722 static bool cp_parser_diagnose_invalid_type_name
1723 (cp_parser *);
1724 static int cp_parser_skip_to_closing_parenthesis
1725 (cp_parser *, bool, bool, bool);
1726 static void cp_parser_skip_to_end_of_statement
1727 (cp_parser *);
1728 static void cp_parser_consume_semicolon_at_end_of_statement
1729 (cp_parser *);
1730 static void cp_parser_skip_to_end_of_block_or_statement
1731 (cp_parser *);
1732 static void cp_parser_skip_to_closing_brace
1733 (cp_parser *);
1734 static void cp_parser_skip_until_found
1735 (cp_parser *, enum cpp_ttype, const char *);
1736 static bool cp_parser_error_occurred
1737 (cp_parser *);
1738 static bool cp_parser_allow_gnu_extensions_p
1739 (cp_parser *);
1740 static bool cp_parser_is_string_literal
1741 (cp_token *);
1742 static bool cp_parser_is_keyword
1743 (cp_token *, enum rid);
1744
1745 /* Returns nonzero if we are parsing tentatively. */
1746
1747 static inline bool
1748 cp_parser_parsing_tentatively (cp_parser* parser)
1749 {
1750 return parser->context->next != NULL;
1751 }
1752
1753 /* Returns nonzero if TOKEN is a string literal. */
1754
1755 static bool
1756 cp_parser_is_string_literal (cp_token* token)
1757 {
1758 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1759 }
1760
1761 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1762
1763 static bool
1764 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1765 {
1766 return token->keyword == keyword;
1767 }
1768
1769 /* Issue the indicated error MESSAGE. */
1770
1771 static void
1772 cp_parser_error (cp_parser* parser, const char* message)
1773 {
1774 /* Output the MESSAGE -- unless we're parsing tentatively. */
1775 if (!cp_parser_simulate_error (parser))
1776 {
1777 cp_token *token;
1778 token = cp_lexer_peek_token (parser->lexer);
1779 c_parse_error (message,
1780 /* Because c_parser_error does not understand
1781 CPP_KEYWORD, keywords are treated like
1782 identifiers. */
1783 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1784 token->value);
1785 }
1786 }
1787
1788 /* Issue an error about name-lookup failing. NAME is the
1789 IDENTIFIER_NODE DECL is the result of
1790 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1791 the thing that we hoped to find. */
1792
1793 static void
1794 cp_parser_name_lookup_error (cp_parser* parser,
1795 tree name,
1796 tree decl,
1797 const char* desired)
1798 {
1799 /* If name lookup completely failed, tell the user that NAME was not
1800 declared. */
1801 if (decl == error_mark_node)
1802 {
1803 if (parser->scope && parser->scope != global_namespace)
1804 error ("`%D::%D' has not been declared",
1805 parser->scope, name);
1806 else if (parser->scope == global_namespace)
1807 error ("`::%D' has not been declared", name);
1808 else
1809 error ("`%D' has not been declared", name);
1810 }
1811 else if (parser->scope && parser->scope != global_namespace)
1812 error ("`%D::%D' %s", parser->scope, name, desired);
1813 else if (parser->scope == global_namespace)
1814 error ("`::%D' %s", name, desired);
1815 else
1816 error ("`%D' %s", name, desired);
1817 }
1818
1819 /* If we are parsing tentatively, remember that an error has occurred
1820 during this tentative parse. Returns true if the error was
1821 simulated; false if a messgae should be issued by the caller. */
1822
1823 static bool
1824 cp_parser_simulate_error (cp_parser* parser)
1825 {
1826 if (cp_parser_parsing_tentatively (parser)
1827 && !cp_parser_committed_to_tentative_parse (parser))
1828 {
1829 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1830 return true;
1831 }
1832 return false;
1833 }
1834
1835 /* This function is called when a type is defined. If type
1836 definitions are forbidden at this point, an error message is
1837 issued. */
1838
1839 static void
1840 cp_parser_check_type_definition (cp_parser* parser)
1841 {
1842 /* If types are forbidden here, issue a message. */
1843 if (parser->type_definition_forbidden_message)
1844 /* Use `%s' to print the string in case there are any escape
1845 characters in the message. */
1846 error ("%s", parser->type_definition_forbidden_message);
1847 }
1848
1849 /* This function is called when a declaration is parsed. If
1850 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1851 indicates that a type was defined in the decl-specifiers for DECL,
1852 then an error is issued. */
1853
1854 static void
1855 cp_parser_check_for_definition_in_return_type (tree declarator,
1856 int declares_class_or_enum)
1857 {
1858 /* [dcl.fct] forbids type definitions in return types.
1859 Unfortunately, it's not easy to know whether or not we are
1860 processing a return type until after the fact. */
1861 while (declarator
1862 && (TREE_CODE (declarator) == INDIRECT_REF
1863 || TREE_CODE (declarator) == ADDR_EXPR))
1864 declarator = TREE_OPERAND (declarator, 0);
1865 if (declarator
1866 && TREE_CODE (declarator) == CALL_EXPR
1867 && declares_class_or_enum & 2)
1868 error ("new types may not be defined in a return type");
1869 }
1870
1871 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1872 "<" in any valid C++ program. If the next token is indeed "<",
1873 issue a message warning the user about what appears to be an
1874 invalid attempt to form a template-id. */
1875
1876 static void
1877 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1878 tree type)
1879 {
1880 ptrdiff_t start;
1881 cp_token *token;
1882
1883 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1884 {
1885 if (TYPE_P (type))
1886 error ("`%T' is not a template", type);
1887 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1888 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1889 else
1890 error ("invalid template-id");
1891 /* Remember the location of the invalid "<". */
1892 if (cp_parser_parsing_tentatively (parser)
1893 && !cp_parser_committed_to_tentative_parse (parser))
1894 {
1895 token = cp_lexer_peek_token (parser->lexer);
1896 token = cp_lexer_prev_token (parser->lexer, token);
1897 start = cp_lexer_token_difference (parser->lexer,
1898 parser->lexer->first_token,
1899 token);
1900 }
1901 else
1902 start = -1;
1903 /* Consume the "<". */
1904 cp_lexer_consume_token (parser->lexer);
1905 /* Parse the template arguments. */
1906 cp_parser_enclosed_template_argument_list (parser);
1907 /* Permanently remove the invalid template arguments so that
1908 this error message is not issued again. */
1909 if (start >= 0)
1910 {
1911 token = cp_lexer_advance_token (parser->lexer,
1912 parser->lexer->first_token,
1913 start);
1914 cp_lexer_purge_tokens_after (parser->lexer, token);
1915 }
1916 }
1917 }
1918
1919 /* Issue an error message about the fact that THING appeared in a
1920 constant-expression. Returns ERROR_MARK_NODE. */
1921
1922 static tree
1923 cp_parser_non_integral_constant_expression (const char *thing)
1924 {
1925 error ("%s cannot appear in a constant-expression", thing);
1926 return error_mark_node;
1927 }
1928
1929 /* Check for a common situation where a type-name should be present,
1930 but is not, and issue a sensible error message. Returns true if an
1931 invalid type-name was detected. */
1932
1933 static bool
1934 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1935 {
1936 /* If the next two tokens are both identifiers, the code is
1937 erroneous. The usual cause of this situation is code like:
1938
1939 T t;
1940
1941 where "T" should name a type -- but does not. */
1942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1943 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1944 {
1945 tree name;
1946
1947 /* If parsing tentatively, we should commit; we really are
1948 looking at a declaration. */
1949 /* Consume the first identifier. */
1950 name = cp_lexer_consume_token (parser->lexer)->value;
1951 /* Issue an error message. */
1952 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1953 /* If we're in a template class, it's possible that the user was
1954 referring to a type from a base class. For example:
1955
1956 template <typename T> struct A { typedef T X; };
1957 template <typename T> struct B : public A<T> { X x; };
1958
1959 The user should have said "typename A<T>::X". */
1960 if (processing_template_decl && current_class_type)
1961 {
1962 tree b;
1963
1964 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1965 b;
1966 b = TREE_CHAIN (b))
1967 {
1968 tree base_type = BINFO_TYPE (b);
1969 if (CLASS_TYPE_P (base_type)
1970 && dependent_type_p (base_type))
1971 {
1972 tree field;
1973 /* Go from a particular instantiation of the
1974 template (which will have an empty TYPE_FIELDs),
1975 to the main version. */
1976 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1977 for (field = TYPE_FIELDS (base_type);
1978 field;
1979 field = TREE_CHAIN (field))
1980 if (TREE_CODE (field) == TYPE_DECL
1981 && DECL_NAME (field) == name)
1982 {
1983 error ("(perhaps `typename %T::%s' was intended)",
1984 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1985 break;
1986 }
1987 if (field)
1988 break;
1989 }
1990 }
1991 }
1992 /* Skip to the end of the declaration; there's no point in
1993 trying to process it. */
1994 cp_parser_skip_to_end_of_statement (parser);
1995
1996 return true;
1997 }
1998
1999 return false;
2000 }
2001
2002 /* Consume tokens up to, and including, the next non-nested closing `)'.
2003 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2004 are doing error recovery. Returns -1 if OR_COMMA is true and we
2005 found an unnested comma. */
2006
2007 static int
2008 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2009 bool recovering,
2010 bool or_comma,
2011 bool consume_paren)
2012 {
2013 unsigned paren_depth = 0;
2014 unsigned brace_depth = 0;
2015
2016 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2017 && !cp_parser_committed_to_tentative_parse (parser))
2018 return 0;
2019
2020 while (true)
2021 {
2022 cp_token *token;
2023
2024 /* If we've run out of tokens, then there is no closing `)'. */
2025 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2026 return 0;
2027
2028 token = cp_lexer_peek_token (parser->lexer);
2029
2030 /* This matches the processing in skip_to_end_of_statement. */
2031 if (token->type == CPP_SEMICOLON && !brace_depth)
2032 return 0;
2033 if (token->type == CPP_OPEN_BRACE)
2034 ++brace_depth;
2035 if (token->type == CPP_CLOSE_BRACE)
2036 {
2037 if (!brace_depth--)
2038 return 0;
2039 }
2040 if (recovering && or_comma && token->type == CPP_COMMA
2041 && !brace_depth && !paren_depth)
2042 return -1;
2043
2044 if (!brace_depth)
2045 {
2046 /* If it is an `(', we have entered another level of nesting. */
2047 if (token->type == CPP_OPEN_PAREN)
2048 ++paren_depth;
2049 /* If it is a `)', then we might be done. */
2050 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2051 {
2052 if (consume_paren)
2053 cp_lexer_consume_token (parser->lexer);
2054 return 1;
2055 }
2056 }
2057
2058 /* Consume the token. */
2059 cp_lexer_consume_token (parser->lexer);
2060 }
2061 }
2062
2063 /* Consume tokens until we reach the end of the current statement.
2064 Normally, that will be just before consuming a `;'. However, if a
2065 non-nested `}' comes first, then we stop before consuming that. */
2066
2067 static void
2068 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2069 {
2070 unsigned nesting_depth = 0;
2071
2072 while (true)
2073 {
2074 cp_token *token;
2075
2076 /* Peek at the next token. */
2077 token = cp_lexer_peek_token (parser->lexer);
2078 /* If we've run out of tokens, stop. */
2079 if (token->type == CPP_EOF)
2080 break;
2081 /* If the next token is a `;', we have reached the end of the
2082 statement. */
2083 if (token->type == CPP_SEMICOLON && !nesting_depth)
2084 break;
2085 /* If the next token is a non-nested `}', then we have reached
2086 the end of the current block. */
2087 if (token->type == CPP_CLOSE_BRACE)
2088 {
2089 /* If this is a non-nested `}', stop before consuming it.
2090 That way, when confronted with something like:
2091
2092 { 3 + }
2093
2094 we stop before consuming the closing `}', even though we
2095 have not yet reached a `;'. */
2096 if (nesting_depth == 0)
2097 break;
2098 /* If it is the closing `}' for a block that we have
2099 scanned, stop -- but only after consuming the token.
2100 That way given:
2101
2102 void f g () { ... }
2103 typedef int I;
2104
2105 we will stop after the body of the erroneously declared
2106 function, but before consuming the following `typedef'
2107 declaration. */
2108 if (--nesting_depth == 0)
2109 {
2110 cp_lexer_consume_token (parser->lexer);
2111 break;
2112 }
2113 }
2114 /* If it the next token is a `{', then we are entering a new
2115 block. Consume the entire block. */
2116 else if (token->type == CPP_OPEN_BRACE)
2117 ++nesting_depth;
2118 /* Consume the token. */
2119 cp_lexer_consume_token (parser->lexer);
2120 }
2121 }
2122
2123 /* This function is called at the end of a statement or declaration.
2124 If the next token is a semicolon, it is consumed; otherwise, error
2125 recovery is attempted. */
2126
2127 static void
2128 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2129 {
2130 /* Look for the trailing `;'. */
2131 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2132 {
2133 /* If there is additional (erroneous) input, skip to the end of
2134 the statement. */
2135 cp_parser_skip_to_end_of_statement (parser);
2136 /* If the next token is now a `;', consume it. */
2137 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2138 cp_lexer_consume_token (parser->lexer);
2139 }
2140 }
2141
2142 /* Skip tokens until we have consumed an entire block, or until we
2143 have consumed a non-nested `;'. */
2144
2145 static void
2146 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2147 {
2148 unsigned nesting_depth = 0;
2149
2150 while (true)
2151 {
2152 cp_token *token;
2153
2154 /* Peek at the next token. */
2155 token = cp_lexer_peek_token (parser->lexer);
2156 /* If we've run out of tokens, stop. */
2157 if (token->type == CPP_EOF)
2158 break;
2159 /* If the next token is a `;', we have reached the end of the
2160 statement. */
2161 if (token->type == CPP_SEMICOLON && !nesting_depth)
2162 {
2163 /* Consume the `;'. */
2164 cp_lexer_consume_token (parser->lexer);
2165 break;
2166 }
2167 /* Consume the token. */
2168 token = cp_lexer_consume_token (parser->lexer);
2169 /* If the next token is a non-nested `}', then we have reached
2170 the end of the current block. */
2171 if (token->type == CPP_CLOSE_BRACE
2172 && (nesting_depth == 0 || --nesting_depth == 0))
2173 break;
2174 /* If it the next token is a `{', then we are entering a new
2175 block. Consume the entire block. */
2176 if (token->type == CPP_OPEN_BRACE)
2177 ++nesting_depth;
2178 }
2179 }
2180
2181 /* Skip tokens until a non-nested closing curly brace is the next
2182 token. */
2183
2184 static void
2185 cp_parser_skip_to_closing_brace (cp_parser *parser)
2186 {
2187 unsigned nesting_depth = 0;
2188
2189 while (true)
2190 {
2191 cp_token *token;
2192
2193 /* Peek at the next token. */
2194 token = cp_lexer_peek_token (parser->lexer);
2195 /* If we've run out of tokens, stop. */
2196 if (token->type == CPP_EOF)
2197 break;
2198 /* If the next token is a non-nested `}', then we have reached
2199 the end of the current block. */
2200 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2201 break;
2202 /* If it the next token is a `{', then we are entering a new
2203 block. Consume the entire block. */
2204 else if (token->type == CPP_OPEN_BRACE)
2205 ++nesting_depth;
2206 /* Consume the token. */
2207 cp_lexer_consume_token (parser->lexer);
2208 }
2209 }
2210
2211 /* Create a new C++ parser. */
2212
2213 static cp_parser *
2214 cp_parser_new (void)
2215 {
2216 cp_parser *parser;
2217 cp_lexer *lexer;
2218
2219 /* cp_lexer_new_main is called before calling ggc_alloc because
2220 cp_lexer_new_main might load a PCH file. */
2221 lexer = cp_lexer_new_main ();
2222
2223 parser = ggc_alloc_cleared (sizeof (cp_parser));
2224 parser->lexer = lexer;
2225 parser->context = cp_parser_context_new (NULL);
2226
2227 /* For now, we always accept GNU extensions. */
2228 parser->allow_gnu_extensions_p = 1;
2229
2230 /* The `>' token is a greater-than operator, not the end of a
2231 template-id. */
2232 parser->greater_than_is_operator_p = true;
2233
2234 parser->default_arg_ok_p = true;
2235
2236 /* We are not parsing a constant-expression. */
2237 parser->integral_constant_expression_p = false;
2238 parser->allow_non_integral_constant_expression_p = false;
2239 parser->non_integral_constant_expression_p = false;
2240
2241 /* We are not parsing offsetof. */
2242 parser->in_offsetof_p = false;
2243
2244 /* Local variable names are not forbidden. */
2245 parser->local_variables_forbidden_p = false;
2246
2247 /* We are not processing an `extern "C"' declaration. */
2248 parser->in_unbraced_linkage_specification_p = false;
2249
2250 /* We are not processing a declarator. */
2251 parser->in_declarator_p = false;
2252
2253 /* We are not processing a template-argument-list. */
2254 parser->in_template_argument_list_p = false;
2255
2256 /* We are not in an iteration statement. */
2257 parser->in_iteration_statement_p = false;
2258
2259 /* We are not in a switch statement. */
2260 parser->in_switch_statement_p = false;
2261
2262 /* We are not parsing a type-id inside an expression. */
2263 parser->in_type_id_in_expr_p = false;
2264
2265 /* The unparsed function queue is empty. */
2266 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2267
2268 /* There are no classes being defined. */
2269 parser->num_classes_being_defined = 0;
2270
2271 /* No template parameters apply. */
2272 parser->num_template_parameter_lists = 0;
2273
2274 return parser;
2275 }
2276
2277 /* Lexical conventions [gram.lex] */
2278
2279 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2280 identifier. */
2281
2282 static tree
2283 cp_parser_identifier (cp_parser* parser)
2284 {
2285 cp_token *token;
2286
2287 /* Look for the identifier. */
2288 token = cp_parser_require (parser, CPP_NAME, "identifier");
2289 /* Return the value. */
2290 return token ? token->value : error_mark_node;
2291 }
2292
2293 /* Basic concepts [gram.basic] */
2294
2295 /* Parse a translation-unit.
2296
2297 translation-unit:
2298 declaration-seq [opt]
2299
2300 Returns TRUE if all went well. */
2301
2302 static bool
2303 cp_parser_translation_unit (cp_parser* parser)
2304 {
2305 while (true)
2306 {
2307 cp_parser_declaration_seq_opt (parser);
2308
2309 /* If there are no tokens left then all went well. */
2310 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2311 break;
2312
2313 /* Otherwise, issue an error message. */
2314 cp_parser_error (parser, "expected declaration");
2315 return false;
2316 }
2317
2318 /* Consume the EOF token. */
2319 cp_parser_require (parser, CPP_EOF, "end-of-file");
2320
2321 /* Finish up. */
2322 finish_translation_unit ();
2323
2324 /* All went well. */
2325 return true;
2326 }
2327
2328 /* Expressions [gram.expr] */
2329
2330 /* Parse a primary-expression.
2331
2332 primary-expression:
2333 literal
2334 this
2335 ( expression )
2336 id-expression
2337
2338 GNU Extensions:
2339
2340 primary-expression:
2341 ( compound-statement )
2342 __builtin_va_arg ( assignment-expression , type-id )
2343
2344 literal:
2345 __null
2346
2347 Returns a representation of the expression.
2348
2349 *IDK indicates what kind of id-expression (if any) was present.
2350
2351 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2352 used as the operand of a pointer-to-member. In that case,
2353 *QUALIFYING_CLASS gives the class that is used as the qualifying
2354 class in the pointer-to-member. */
2355
2356 static tree
2357 cp_parser_primary_expression (cp_parser *parser,
2358 cp_id_kind *idk,
2359 tree *qualifying_class)
2360 {
2361 cp_token *token;
2362
2363 /* Assume the primary expression is not an id-expression. */
2364 *idk = CP_ID_KIND_NONE;
2365 /* And that it cannot be used as pointer-to-member. */
2366 *qualifying_class = NULL_TREE;
2367
2368 /* Peek at the next token. */
2369 token = cp_lexer_peek_token (parser->lexer);
2370 switch (token->type)
2371 {
2372 /* literal:
2373 integer-literal
2374 character-literal
2375 floating-literal
2376 string-literal
2377 boolean-literal */
2378 case CPP_CHAR:
2379 case CPP_WCHAR:
2380 case CPP_STRING:
2381 case CPP_WSTRING:
2382 case CPP_NUMBER:
2383 token = cp_lexer_consume_token (parser->lexer);
2384 return token->value;
2385
2386 case CPP_OPEN_PAREN:
2387 {
2388 tree expr;
2389 bool saved_greater_than_is_operator_p;
2390
2391 /* Consume the `('. */
2392 cp_lexer_consume_token (parser->lexer);
2393 /* Within a parenthesized expression, a `>' token is always
2394 the greater-than operator. */
2395 saved_greater_than_is_operator_p
2396 = parser->greater_than_is_operator_p;
2397 parser->greater_than_is_operator_p = true;
2398 /* If we see `( { ' then we are looking at the beginning of
2399 a GNU statement-expression. */
2400 if (cp_parser_allow_gnu_extensions_p (parser)
2401 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2402 {
2403 /* Statement-expressions are not allowed by the standard. */
2404 if (pedantic)
2405 pedwarn ("ISO C++ forbids braced-groups within expressions");
2406
2407 /* And they're not allowed outside of a function-body; you
2408 cannot, for example, write:
2409
2410 int i = ({ int j = 3; j + 1; });
2411
2412 at class or namespace scope. */
2413 if (!at_function_scope_p ())
2414 error ("statement-expressions are allowed only inside functions");
2415 /* Start the statement-expression. */
2416 expr = begin_stmt_expr ();
2417 /* Parse the compound-statement. */
2418 cp_parser_compound_statement (parser, true);
2419 /* Finish up. */
2420 expr = finish_stmt_expr (expr, false);
2421 }
2422 else
2423 {
2424 /* Parse the parenthesized expression. */
2425 expr = cp_parser_expression (parser);
2426 /* Let the front end know that this expression was
2427 enclosed in parentheses. This matters in case, for
2428 example, the expression is of the form `A::B', since
2429 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2430 not. */
2431 finish_parenthesized_expr (expr);
2432 }
2433 /* The `>' token might be the end of a template-id or
2434 template-parameter-list now. */
2435 parser->greater_than_is_operator_p
2436 = saved_greater_than_is_operator_p;
2437 /* Consume the `)'. */
2438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2439 cp_parser_skip_to_end_of_statement (parser);
2440
2441 return expr;
2442 }
2443
2444 case CPP_KEYWORD:
2445 switch (token->keyword)
2446 {
2447 /* These two are the boolean literals. */
2448 case RID_TRUE:
2449 cp_lexer_consume_token (parser->lexer);
2450 return boolean_true_node;
2451 case RID_FALSE:
2452 cp_lexer_consume_token (parser->lexer);
2453 return boolean_false_node;
2454
2455 /* The `__null' literal. */
2456 case RID_NULL:
2457 cp_lexer_consume_token (parser->lexer);
2458 return null_node;
2459
2460 /* Recognize the `this' keyword. */
2461 case RID_THIS:
2462 cp_lexer_consume_token (parser->lexer);
2463 if (parser->local_variables_forbidden_p)
2464 {
2465 error ("`this' may not be used in this context");
2466 return error_mark_node;
2467 }
2468 /* Pointers cannot appear in constant-expressions. */
2469 if (parser->integral_constant_expression_p)
2470 {
2471 if (!parser->allow_non_integral_constant_expression_p)
2472 return cp_parser_non_integral_constant_expression ("`this'");
2473 parser->non_integral_constant_expression_p = true;
2474 }
2475 return finish_this_expr ();
2476
2477 /* The `operator' keyword can be the beginning of an
2478 id-expression. */
2479 case RID_OPERATOR:
2480 goto id_expression;
2481
2482 case RID_FUNCTION_NAME:
2483 case RID_PRETTY_FUNCTION_NAME:
2484 case RID_C99_FUNCTION_NAME:
2485 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2486 __func__ are the names of variables -- but they are
2487 treated specially. Therefore, they are handled here,
2488 rather than relying on the generic id-expression logic
2489 below. Grammatically, these names are id-expressions.
2490
2491 Consume the token. */
2492 token = cp_lexer_consume_token (parser->lexer);
2493 /* Look up the name. */
2494 return finish_fname (token->value);
2495
2496 case RID_VA_ARG:
2497 {
2498 tree expression;
2499 tree type;
2500
2501 /* The `__builtin_va_arg' construct is used to handle
2502 `va_arg'. Consume the `__builtin_va_arg' token. */
2503 cp_lexer_consume_token (parser->lexer);
2504 /* Look for the opening `('. */
2505 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2506 /* Now, parse the assignment-expression. */
2507 expression = cp_parser_assignment_expression (parser);
2508 /* Look for the `,'. */
2509 cp_parser_require (parser, CPP_COMMA, "`,'");
2510 /* Parse the type-id. */
2511 type = cp_parser_type_id (parser);
2512 /* Look for the closing `)'. */
2513 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2514 /* Using `va_arg' in a constant-expression is not
2515 allowed. */
2516 if (parser->integral_constant_expression_p)
2517 {
2518 if (!parser->allow_non_integral_constant_expression_p)
2519 return cp_parser_non_integral_constant_expression ("`va_arg'");
2520 parser->non_integral_constant_expression_p = true;
2521 }
2522 return build_x_va_arg (expression, type);
2523 }
2524
2525 case RID_OFFSETOF:
2526 {
2527 tree expression;
2528 bool saved_in_offsetof_p;
2529
2530 /* Consume the "__offsetof__" token. */
2531 cp_lexer_consume_token (parser->lexer);
2532 /* Consume the opening `('. */
2533 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2534 /* Parse the parenthesized (almost) constant-expression. */
2535 saved_in_offsetof_p = parser->in_offsetof_p;
2536 parser->in_offsetof_p = true;
2537 expression
2538 = cp_parser_constant_expression (parser,
2539 /*allow_non_constant_p=*/false,
2540 /*non_constant_p=*/NULL);
2541 parser->in_offsetof_p = saved_in_offsetof_p;
2542 /* Consume the closing ')'. */
2543 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2544
2545 return expression;
2546 }
2547
2548 default:
2549 cp_parser_error (parser, "expected primary-expression");
2550 return error_mark_node;
2551 }
2552
2553 /* An id-expression can start with either an identifier, a
2554 `::' as the beginning of a qualified-id, or the "operator"
2555 keyword. */
2556 case CPP_NAME:
2557 case CPP_SCOPE:
2558 case CPP_TEMPLATE_ID:
2559 case CPP_NESTED_NAME_SPECIFIER:
2560 {
2561 tree id_expression;
2562 tree decl;
2563 const char *error_msg;
2564
2565 id_expression:
2566 /* Parse the id-expression. */
2567 id_expression
2568 = cp_parser_id_expression (parser,
2569 /*template_keyword_p=*/false,
2570 /*check_dependency_p=*/true,
2571 /*template_p=*/NULL,
2572 /*declarator_p=*/false);
2573 if (id_expression == error_mark_node)
2574 return error_mark_node;
2575 /* If we have a template-id, then no further lookup is
2576 required. If the template-id was for a template-class, we
2577 will sometimes have a TYPE_DECL at this point. */
2578 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2579 || TREE_CODE (id_expression) == TYPE_DECL)
2580 decl = id_expression;
2581 /* Look up the name. */
2582 else
2583 {
2584 decl = cp_parser_lookup_name_simple (parser, id_expression);
2585 /* If name lookup gives us a SCOPE_REF, then the
2586 qualifying scope was dependent. Just propagate the
2587 name. */
2588 if (TREE_CODE (decl) == SCOPE_REF)
2589 {
2590 if (TYPE_P (TREE_OPERAND (decl, 0)))
2591 *qualifying_class = TREE_OPERAND (decl, 0);
2592 return decl;
2593 }
2594 /* Check to see if DECL is a local variable in a context
2595 where that is forbidden. */
2596 if (parser->local_variables_forbidden_p
2597 && local_variable_p (decl))
2598 {
2599 /* It might be that we only found DECL because we are
2600 trying to be generous with pre-ISO scoping rules.
2601 For example, consider:
2602
2603 int i;
2604 void g() {
2605 for (int i = 0; i < 10; ++i) {}
2606 extern void f(int j = i);
2607 }
2608
2609 Here, name look up will originally find the out
2610 of scope `i'. We need to issue a warning message,
2611 but then use the global `i'. */
2612 decl = check_for_out_of_scope_variable (decl);
2613 if (local_variable_p (decl))
2614 {
2615 error ("local variable `%D' may not appear in this context",
2616 decl);
2617 return error_mark_node;
2618 }
2619 }
2620 }
2621
2622 decl = finish_id_expression (id_expression, decl, parser->scope,
2623 idk, qualifying_class,
2624 parser->integral_constant_expression_p,
2625 parser->allow_non_integral_constant_expression_p,
2626 &parser->non_integral_constant_expression_p,
2627 &error_msg);
2628 if (error_msg)
2629 cp_parser_error (parser, error_msg);
2630 return decl;
2631 }
2632
2633 /* Anything else is an error. */
2634 default:
2635 cp_parser_error (parser, "expected primary-expression");
2636 return error_mark_node;
2637 }
2638 }
2639
2640 /* Parse an id-expression.
2641
2642 id-expression:
2643 unqualified-id
2644 qualified-id
2645
2646 qualified-id:
2647 :: [opt] nested-name-specifier template [opt] unqualified-id
2648 :: identifier
2649 :: operator-function-id
2650 :: template-id
2651
2652 Return a representation of the unqualified portion of the
2653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2654 a `::' or nested-name-specifier.
2655
2656 Often, if the id-expression was a qualified-id, the caller will
2657 want to make a SCOPE_REF to represent the qualified-id. This
2658 function does not do this in order to avoid wastefully creating
2659 SCOPE_REFs when they are not required.
2660
2661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2662 `template' keyword.
2663
2664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2665 uninstantiated templates.
2666
2667 If *TEMPLATE_P is non-NULL, it is set to true iff the
2668 `template' keyword is used to explicitly indicate that the entity
2669 named is a template.
2670
2671 If DECLARATOR_P is true, the id-expression is appearing as part of
2672 a declarator, rather than as part of an expression. */
2673
2674 static tree
2675 cp_parser_id_expression (cp_parser *parser,
2676 bool template_keyword_p,
2677 bool check_dependency_p,
2678 bool *template_p,
2679 bool declarator_p)
2680 {
2681 bool global_scope_p;
2682 bool nested_name_specifier_p;
2683
2684 /* Assume the `template' keyword was not used. */
2685 if (template_p)
2686 *template_p = false;
2687
2688 /* Look for the optional `::' operator. */
2689 global_scope_p
2690 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2691 != NULL_TREE);
2692 /* Look for the optional nested-name-specifier. */
2693 nested_name_specifier_p
2694 = (cp_parser_nested_name_specifier_opt (parser,
2695 /*typename_keyword_p=*/false,
2696 check_dependency_p,
2697 /*type_p=*/false,
2698 /*is_declarator=*/false)
2699 != NULL_TREE);
2700 /* If there is a nested-name-specifier, then we are looking at
2701 the first qualified-id production. */
2702 if (nested_name_specifier_p)
2703 {
2704 tree saved_scope;
2705 tree saved_object_scope;
2706 tree saved_qualifying_scope;
2707 tree unqualified_id;
2708 bool is_template;
2709
2710 /* See if the next token is the `template' keyword. */
2711 if (!template_p)
2712 template_p = &is_template;
2713 *template_p = cp_parser_optional_template_keyword (parser);
2714 /* Name lookup we do during the processing of the
2715 unqualified-id might obliterate SCOPE. */
2716 saved_scope = parser->scope;
2717 saved_object_scope = parser->object_scope;
2718 saved_qualifying_scope = parser->qualifying_scope;
2719 /* Process the final unqualified-id. */
2720 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2721 check_dependency_p,
2722 declarator_p);
2723 /* Restore the SAVED_SCOPE for our caller. */
2724 parser->scope = saved_scope;
2725 parser->object_scope = saved_object_scope;
2726 parser->qualifying_scope = saved_qualifying_scope;
2727
2728 return unqualified_id;
2729 }
2730 /* Otherwise, if we are in global scope, then we are looking at one
2731 of the other qualified-id productions. */
2732 else if (global_scope_p)
2733 {
2734 cp_token *token;
2735 tree id;
2736
2737 /* Peek at the next token. */
2738 token = cp_lexer_peek_token (parser->lexer);
2739
2740 /* If it's an identifier, and the next token is not a "<", then
2741 we can avoid the template-id case. This is an optimization
2742 for this common case. */
2743 if (token->type == CPP_NAME
2744 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2745 return cp_parser_identifier (parser);
2746
2747 cp_parser_parse_tentatively (parser);
2748 /* Try a template-id. */
2749 id = cp_parser_template_id (parser,
2750 /*template_keyword_p=*/false,
2751 /*check_dependency_p=*/true,
2752 declarator_p);
2753 /* If that worked, we're done. */
2754 if (cp_parser_parse_definitely (parser))
2755 return id;
2756
2757 /* Peek at the next token. (Changes in the token buffer may
2758 have invalidated the pointer obtained above.) */
2759 token = cp_lexer_peek_token (parser->lexer);
2760
2761 switch (token->type)
2762 {
2763 case CPP_NAME:
2764 return cp_parser_identifier (parser);
2765
2766 case CPP_KEYWORD:
2767 if (token->keyword == RID_OPERATOR)
2768 return cp_parser_operator_function_id (parser);
2769 /* Fall through. */
2770
2771 default:
2772 cp_parser_error (parser, "expected id-expression");
2773 return error_mark_node;
2774 }
2775 }
2776 else
2777 return cp_parser_unqualified_id (parser, template_keyword_p,
2778 /*check_dependency_p=*/true,
2779 declarator_p);
2780 }
2781
2782 /* Parse an unqualified-id.
2783
2784 unqualified-id:
2785 identifier
2786 operator-function-id
2787 conversion-function-id
2788 ~ class-name
2789 template-id
2790
2791 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2792 keyword, in a construct like `A::template ...'.
2793
2794 Returns a representation of unqualified-id. For the `identifier'
2795 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2796 production a BIT_NOT_EXPR is returned; the operand of the
2797 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2798 other productions, see the documentation accompanying the
2799 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2800 names are looked up in uninstantiated templates. If DECLARATOR_P
2801 is true, the unqualified-id is appearing as part of a declarator,
2802 rather than as part of an expression. */
2803
2804 static tree
2805 cp_parser_unqualified_id (cp_parser* parser,
2806 bool template_keyword_p,
2807 bool check_dependency_p,
2808 bool declarator_p)
2809 {
2810 cp_token *token;
2811
2812 /* Peek at the next token. */
2813 token = cp_lexer_peek_token (parser->lexer);
2814
2815 switch (token->type)
2816 {
2817 case CPP_NAME:
2818 {
2819 tree id;
2820
2821 /* We don't know yet whether or not this will be a
2822 template-id. */
2823 cp_parser_parse_tentatively (parser);
2824 /* Try a template-id. */
2825 id = cp_parser_template_id (parser, template_keyword_p,
2826 check_dependency_p,
2827 declarator_p);
2828 /* If it worked, we're done. */
2829 if (cp_parser_parse_definitely (parser))
2830 return id;
2831 /* Otherwise, it's an ordinary identifier. */
2832 return cp_parser_identifier (parser);
2833 }
2834
2835 case CPP_TEMPLATE_ID:
2836 return cp_parser_template_id (parser, template_keyword_p,
2837 check_dependency_p,
2838 declarator_p);
2839
2840 case CPP_COMPL:
2841 {
2842 tree type_decl;
2843 tree qualifying_scope;
2844 tree object_scope;
2845 tree scope;
2846
2847 /* Consume the `~' token. */
2848 cp_lexer_consume_token (parser->lexer);
2849 /* Parse the class-name. The standard, as written, seems to
2850 say that:
2851
2852 template <typename T> struct S { ~S (); };
2853 template <typename T> S<T>::~S() {}
2854
2855 is invalid, since `~' must be followed by a class-name, but
2856 `S<T>' is dependent, and so not known to be a class.
2857 That's not right; we need to look in uninstantiated
2858 templates. A further complication arises from:
2859
2860 template <typename T> void f(T t) {
2861 t.T::~T();
2862 }
2863
2864 Here, it is not possible to look up `T' in the scope of `T'
2865 itself. We must look in both the current scope, and the
2866 scope of the containing complete expression.
2867
2868 Yet another issue is:
2869
2870 struct S {
2871 int S;
2872 ~S();
2873 };
2874
2875 S::~S() {}
2876
2877 The standard does not seem to say that the `S' in `~S'
2878 should refer to the type `S' and not the data member
2879 `S::S'. */
2880
2881 /* DR 244 says that we look up the name after the "~" in the
2882 same scope as we looked up the qualifying name. That idea
2883 isn't fully worked out; it's more complicated than that. */
2884 scope = parser->scope;
2885 object_scope = parser->object_scope;
2886 qualifying_scope = parser->qualifying_scope;
2887
2888 /* If the name is of the form "X::~X" it's OK. */
2889 if (scope && TYPE_P (scope)
2890 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2891 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2892 == CPP_OPEN_PAREN)
2893 && (cp_lexer_peek_token (parser->lexer)->value
2894 == TYPE_IDENTIFIER (scope)))
2895 {
2896 cp_lexer_consume_token (parser->lexer);
2897 return build_nt (BIT_NOT_EXPR, scope);
2898 }
2899
2900 /* If there was an explicit qualification (S::~T), first look
2901 in the scope given by the qualification (i.e., S). */
2902 if (scope)
2903 {
2904 cp_parser_parse_tentatively (parser);
2905 type_decl = cp_parser_class_name (parser,
2906 /*typename_keyword_p=*/false,
2907 /*template_keyword_p=*/false,
2908 /*type_p=*/false,
2909 /*check_dependency=*/false,
2910 /*class_head_p=*/false,
2911 declarator_p);
2912 if (cp_parser_parse_definitely (parser))
2913 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2914 }
2915 /* In "N::S::~S", look in "N" as well. */
2916 if (scope && qualifying_scope)
2917 {
2918 cp_parser_parse_tentatively (parser);
2919 parser->scope = qualifying_scope;
2920 parser->object_scope = NULL_TREE;
2921 parser->qualifying_scope = NULL_TREE;
2922 type_decl
2923 = cp_parser_class_name (parser,
2924 /*typename_keyword_p=*/false,
2925 /*template_keyword_p=*/false,
2926 /*type_p=*/false,
2927 /*check_dependency=*/false,
2928 /*class_head_p=*/false,
2929 declarator_p);
2930 if (cp_parser_parse_definitely (parser))
2931 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2932 }
2933 /* In "p->S::~T", look in the scope given by "*p" as well. */
2934 else if (object_scope)
2935 {
2936 cp_parser_parse_tentatively (parser);
2937 parser->scope = object_scope;
2938 parser->object_scope = NULL_TREE;
2939 parser->qualifying_scope = NULL_TREE;
2940 type_decl
2941 = cp_parser_class_name (parser,
2942 /*typename_keyword_p=*/false,
2943 /*template_keyword_p=*/false,
2944 /*type_p=*/false,
2945 /*check_dependency=*/false,
2946 /*class_head_p=*/false,
2947 declarator_p);
2948 if (cp_parser_parse_definitely (parser))
2949 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2950 }
2951 /* Look in the surrounding context. */
2952 parser->scope = NULL_TREE;
2953 parser->object_scope = NULL_TREE;
2954 parser->qualifying_scope = NULL_TREE;
2955 type_decl
2956 = cp_parser_class_name (parser,
2957 /*typename_keyword_p=*/false,
2958 /*template_keyword_p=*/false,
2959 /*type_p=*/false,
2960 /*check_dependency=*/false,
2961 /*class_head_p=*/false,
2962 declarator_p);
2963 /* If an error occurred, assume that the name of the
2964 destructor is the same as the name of the qualifying
2965 class. That allows us to keep parsing after running
2966 into ill-formed destructor names. */
2967 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2968 return build_nt (BIT_NOT_EXPR, scope);
2969 else if (type_decl == error_mark_node)
2970 return error_mark_node;
2971
2972 /* [class.dtor]
2973
2974 A typedef-name that names a class shall not be used as the
2975 identifier in the declarator for a destructor declaration. */
2976 if (declarator_p
2977 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2978 && !DECL_SELF_REFERENCE_P (type_decl))
2979 error ("typedef-name `%D' used as destructor declarator",
2980 type_decl);
2981
2982 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2983 }
2984
2985 case CPP_KEYWORD:
2986 if (token->keyword == RID_OPERATOR)
2987 {
2988 tree id;
2989
2990 /* This could be a template-id, so we try that first. */
2991 cp_parser_parse_tentatively (parser);
2992 /* Try a template-id. */
2993 id = cp_parser_template_id (parser, template_keyword_p,
2994 /*check_dependency_p=*/true,
2995 declarator_p);
2996 /* If that worked, we're done. */
2997 if (cp_parser_parse_definitely (parser))
2998 return id;
2999 /* We still don't know whether we're looking at an
3000 operator-function-id or a conversion-function-id. */
3001 cp_parser_parse_tentatively (parser);
3002 /* Try an operator-function-id. */
3003 id = cp_parser_operator_function_id (parser);
3004 /* If that didn't work, try a conversion-function-id. */
3005 if (!cp_parser_parse_definitely (parser))
3006 id = cp_parser_conversion_function_id (parser);
3007
3008 return id;
3009 }
3010 /* Fall through. */
3011
3012 default:
3013 cp_parser_error (parser, "expected unqualified-id");
3014 return error_mark_node;
3015 }
3016 }
3017
3018 /* Parse an (optional) nested-name-specifier.
3019
3020 nested-name-specifier:
3021 class-or-namespace-name :: nested-name-specifier [opt]
3022 class-or-namespace-name :: template nested-name-specifier [opt]
3023
3024 PARSER->SCOPE should be set appropriately before this function is
3025 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3026 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3027 in name lookups.
3028
3029 Sets PARSER->SCOPE to the class (TYPE) or namespace
3030 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3031 it unchanged if there is no nested-name-specifier. Returns the new
3032 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3033
3034 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3035 part of a declaration and/or decl-specifier. */
3036
3037 static tree
3038 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3039 bool typename_keyword_p,
3040 bool check_dependency_p,
3041 bool type_p,
3042 bool is_declaration)
3043 {
3044 bool success = false;
3045 tree access_check = NULL_TREE;
3046 ptrdiff_t start;
3047 cp_token* token;
3048
3049 /* If the next token corresponds to a nested name specifier, there
3050 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3051 false, it may have been true before, in which case something
3052 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3053 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3054 CHECK_DEPENDENCY_P is false, we have to fall through into the
3055 main loop. */
3056 if (check_dependency_p
3057 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3058 {
3059 cp_parser_pre_parsed_nested_name_specifier (parser);
3060 return parser->scope;
3061 }
3062
3063 /* Remember where the nested-name-specifier starts. */
3064 if (cp_parser_parsing_tentatively (parser)
3065 && !cp_parser_committed_to_tentative_parse (parser))
3066 {
3067 token = cp_lexer_peek_token (parser->lexer);
3068 start = cp_lexer_token_difference (parser->lexer,
3069 parser->lexer->first_token,
3070 token);
3071 }
3072 else
3073 start = -1;
3074
3075 push_deferring_access_checks (dk_deferred);
3076
3077 while (true)
3078 {
3079 tree new_scope;
3080 tree old_scope;
3081 tree saved_qualifying_scope;
3082 bool template_keyword_p;
3083
3084 /* Spot cases that cannot be the beginning of a
3085 nested-name-specifier. */
3086 token = cp_lexer_peek_token (parser->lexer);
3087
3088 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3089 the already parsed nested-name-specifier. */
3090 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3091 {
3092 /* Grab the nested-name-specifier and continue the loop. */
3093 cp_parser_pre_parsed_nested_name_specifier (parser);
3094 success = true;
3095 continue;
3096 }
3097
3098 /* Spot cases that cannot be the beginning of a
3099 nested-name-specifier. On the second and subsequent times
3100 through the loop, we look for the `template' keyword. */
3101 if (success && token->keyword == RID_TEMPLATE)
3102 ;
3103 /* A template-id can start a nested-name-specifier. */
3104 else if (token->type == CPP_TEMPLATE_ID)
3105 ;
3106 else
3107 {
3108 /* If the next token is not an identifier, then it is
3109 definitely not a class-or-namespace-name. */
3110 if (token->type != CPP_NAME)
3111 break;
3112 /* If the following token is neither a `<' (to begin a
3113 template-id), nor a `::', then we are not looking at a
3114 nested-name-specifier. */
3115 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3116 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3117 break;
3118 }
3119
3120 /* The nested-name-specifier is optional, so we parse
3121 tentatively. */
3122 cp_parser_parse_tentatively (parser);
3123
3124 /* Look for the optional `template' keyword, if this isn't the
3125 first time through the loop. */
3126 if (success)
3127 template_keyword_p = cp_parser_optional_template_keyword (parser);
3128 else
3129 template_keyword_p = false;
3130
3131 /* Save the old scope since the name lookup we are about to do
3132 might destroy it. */
3133 old_scope = parser->scope;
3134 saved_qualifying_scope = parser->qualifying_scope;
3135 /* Parse the qualifying entity. */
3136 new_scope
3137 = cp_parser_class_or_namespace_name (parser,
3138 typename_keyword_p,
3139 template_keyword_p,
3140 check_dependency_p,
3141 type_p,
3142 is_declaration);
3143 /* Look for the `::' token. */
3144 cp_parser_require (parser, CPP_SCOPE, "`::'");
3145
3146 /* If we found what we wanted, we keep going; otherwise, we're
3147 done. */
3148 if (!cp_parser_parse_definitely (parser))
3149 {
3150 bool error_p = false;
3151
3152 /* Restore the OLD_SCOPE since it was valid before the
3153 failed attempt at finding the last
3154 class-or-namespace-name. */
3155 parser->scope = old_scope;
3156 parser->qualifying_scope = saved_qualifying_scope;
3157 /* If the next token is an identifier, and the one after
3158 that is a `::', then any valid interpretation would have
3159 found a class-or-namespace-name. */
3160 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3161 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3162 == CPP_SCOPE)
3163 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3164 != CPP_COMPL))
3165 {
3166 token = cp_lexer_consume_token (parser->lexer);
3167 if (!error_p)
3168 {
3169 tree decl;
3170
3171 decl = cp_parser_lookup_name_simple (parser, token->value);
3172 if (TREE_CODE (decl) == TEMPLATE_DECL)
3173 error ("`%D' used without template parameters",
3174 decl);
3175 else
3176 cp_parser_name_lookup_error
3177 (parser, token->value, decl,
3178 "is not a class or namespace");
3179 parser->scope = NULL_TREE;
3180 error_p = true;
3181 /* Treat this as a successful nested-name-specifier
3182 due to:
3183
3184 [basic.lookup.qual]
3185
3186 If the name found is not a class-name (clause
3187 _class_) or namespace-name (_namespace.def_), the
3188 program is ill-formed. */
3189 success = true;
3190 }
3191 cp_lexer_consume_token (parser->lexer);
3192 }
3193 break;
3194 }
3195
3196 /* We've found one valid nested-name-specifier. */
3197 success = true;
3198 /* Make sure we look in the right scope the next time through
3199 the loop. */
3200 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3201 ? TREE_TYPE (new_scope)
3202 : new_scope);
3203 /* If it is a class scope, try to complete it; we are about to
3204 be looking up names inside the class. */
3205 if (TYPE_P (parser->scope)
3206 /* Since checking types for dependency can be expensive,
3207 avoid doing it if the type is already complete. */
3208 && !COMPLETE_TYPE_P (parser->scope)
3209 /* Do not try to complete dependent types. */
3210 && !dependent_type_p (parser->scope))
3211 complete_type (parser->scope);
3212 }
3213
3214 /* Retrieve any deferred checks. Do not pop this access checks yet
3215 so the memory will not be reclaimed during token replacing below. */
3216 access_check = get_deferred_access_checks ();
3217
3218 /* If parsing tentatively, replace the sequence of tokens that makes
3219 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3220 token. That way, should we re-parse the token stream, we will
3221 not have to repeat the effort required to do the parse, nor will
3222 we issue duplicate error messages. */
3223 if (success && start >= 0)
3224 {
3225 /* Find the token that corresponds to the start of the
3226 template-id. */
3227 token = cp_lexer_advance_token (parser->lexer,
3228 parser->lexer->first_token,
3229 start);
3230
3231 /* Reset the contents of the START token. */
3232 token->type = CPP_NESTED_NAME_SPECIFIER;
3233 token->value = build_tree_list (access_check, parser->scope);
3234 TREE_TYPE (token->value) = parser->qualifying_scope;
3235 token->keyword = RID_MAX;
3236 /* Purge all subsequent tokens. */
3237 cp_lexer_purge_tokens_after (parser->lexer, token);
3238 }
3239
3240 pop_deferring_access_checks ();
3241 return success ? parser->scope : NULL_TREE;
3242 }
3243
3244 /* Parse a nested-name-specifier. See
3245 cp_parser_nested_name_specifier_opt for details. This function
3246 behaves identically, except that it will an issue an error if no
3247 nested-name-specifier is present, and it will return
3248 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3249 is present. */
3250
3251 static tree
3252 cp_parser_nested_name_specifier (cp_parser *parser,
3253 bool typename_keyword_p,
3254 bool check_dependency_p,
3255 bool type_p,
3256 bool is_declaration)
3257 {
3258 tree scope;
3259
3260 /* Look for the nested-name-specifier. */
3261 scope = cp_parser_nested_name_specifier_opt (parser,
3262 typename_keyword_p,
3263 check_dependency_p,
3264 type_p,
3265 is_declaration);
3266 /* If it was not present, issue an error message. */
3267 if (!scope)
3268 {
3269 cp_parser_error (parser, "expected nested-name-specifier");
3270 parser->scope = NULL_TREE;
3271 return error_mark_node;
3272 }
3273
3274 return scope;
3275 }
3276
3277 /* Parse a class-or-namespace-name.
3278
3279 class-or-namespace-name:
3280 class-name
3281 namespace-name
3282
3283 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3284 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3285 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3286 TYPE_P is TRUE iff the next name should be taken as a class-name,
3287 even the same name is declared to be another entity in the same
3288 scope.
3289
3290 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3291 specified by the class-or-namespace-name. If neither is found the
3292 ERROR_MARK_NODE is returned. */
3293
3294 static tree
3295 cp_parser_class_or_namespace_name (cp_parser *parser,
3296 bool typename_keyword_p,
3297 bool template_keyword_p,
3298 bool check_dependency_p,
3299 bool type_p,
3300 bool is_declaration)
3301 {
3302 tree saved_scope;
3303 tree saved_qualifying_scope;
3304 tree saved_object_scope;
3305 tree scope;
3306 bool only_class_p;
3307
3308 /* Before we try to parse the class-name, we must save away the
3309 current PARSER->SCOPE since cp_parser_class_name will destroy
3310 it. */
3311 saved_scope = parser->scope;
3312 saved_qualifying_scope = parser->qualifying_scope;
3313 saved_object_scope = parser->object_scope;
3314 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3315 there is no need to look for a namespace-name. */
3316 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3317 if (!only_class_p)
3318 cp_parser_parse_tentatively (parser);
3319 scope = cp_parser_class_name (parser,
3320 typename_keyword_p,
3321 template_keyword_p,
3322 type_p,
3323 check_dependency_p,
3324 /*class_head_p=*/false,
3325 is_declaration);
3326 /* If that didn't work, try for a namespace-name. */
3327 if (!only_class_p && !cp_parser_parse_definitely (parser))
3328 {
3329 /* Restore the saved scope. */
3330 parser->scope = saved_scope;
3331 parser->qualifying_scope = saved_qualifying_scope;
3332 parser->object_scope = saved_object_scope;
3333 /* If we are not looking at an identifier followed by the scope
3334 resolution operator, then this is not part of a
3335 nested-name-specifier. (Note that this function is only used
3336 to parse the components of a nested-name-specifier.) */
3337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3338 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3339 return error_mark_node;
3340 scope = cp_parser_namespace_name (parser);
3341 }
3342
3343 return scope;
3344 }
3345
3346 /* Parse a postfix-expression.
3347
3348 postfix-expression:
3349 primary-expression
3350 postfix-expression [ expression ]
3351 postfix-expression ( expression-list [opt] )
3352 simple-type-specifier ( expression-list [opt] )
3353 typename :: [opt] nested-name-specifier identifier
3354 ( expression-list [opt] )
3355 typename :: [opt] nested-name-specifier template [opt] template-id
3356 ( expression-list [opt] )
3357 postfix-expression . template [opt] id-expression
3358 postfix-expression -> template [opt] id-expression
3359 postfix-expression . pseudo-destructor-name
3360 postfix-expression -> pseudo-destructor-name
3361 postfix-expression ++
3362 postfix-expression --
3363 dynamic_cast < type-id > ( expression )
3364 static_cast < type-id > ( expression )
3365 reinterpret_cast < type-id > ( expression )
3366 const_cast < type-id > ( expression )
3367 typeid ( expression )
3368 typeid ( type-id )
3369
3370 GNU Extension:
3371
3372 postfix-expression:
3373 ( type-id ) { initializer-list , [opt] }
3374
3375 This extension is a GNU version of the C99 compound-literal
3376 construct. (The C99 grammar uses `type-name' instead of `type-id',
3377 but they are essentially the same concept.)
3378
3379 If ADDRESS_P is true, the postfix expression is the operand of the
3380 `&' operator.
3381
3382 Returns a representation of the expression. */
3383
3384 static tree
3385 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3386 {
3387 cp_token *token;
3388 enum rid keyword;
3389 cp_id_kind idk = CP_ID_KIND_NONE;
3390 tree postfix_expression = NULL_TREE;
3391 /* Non-NULL only if the current postfix-expression can be used to
3392 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3393 class used to qualify the member. */
3394 tree qualifying_class = NULL_TREE;
3395
3396 /* Peek at the next token. */
3397 token = cp_lexer_peek_token (parser->lexer);
3398 /* Some of the productions are determined by keywords. */
3399 keyword = token->keyword;
3400 switch (keyword)
3401 {
3402 case RID_DYNCAST:
3403 case RID_STATCAST:
3404 case RID_REINTCAST:
3405 case RID_CONSTCAST:
3406 {
3407 tree type;
3408 tree expression;
3409 const char *saved_message;
3410
3411 /* All of these can be handled in the same way from the point
3412 of view of parsing. Begin by consuming the token
3413 identifying the cast. */
3414 cp_lexer_consume_token (parser->lexer);
3415
3416 /* New types cannot be defined in the cast. */
3417 saved_message = parser->type_definition_forbidden_message;
3418 parser->type_definition_forbidden_message
3419 = "types may not be defined in casts";
3420
3421 /* Look for the opening `<'. */
3422 cp_parser_require (parser, CPP_LESS, "`<'");
3423 /* Parse the type to which we are casting. */
3424 type = cp_parser_type_id (parser);
3425 /* Look for the closing `>'. */
3426 cp_parser_require (parser, CPP_GREATER, "`>'");
3427 /* Restore the old message. */
3428 parser->type_definition_forbidden_message = saved_message;
3429
3430 /* And the expression which is being cast. */
3431 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3432 expression = cp_parser_expression (parser);
3433 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3434
3435 /* Only type conversions to integral or enumeration types
3436 can be used in constant-expressions. */
3437 if (parser->integral_constant_expression_p
3438 && !dependent_type_p (type)
3439 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3440 /* A cast to pointer or reference type is allowed in the
3441 implementation of "offsetof". */
3442 && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
3443 {
3444 if (!parser->allow_non_integral_constant_expression_p)
3445 return (cp_parser_non_integral_constant_expression
3446 ("a cast to a type other than an integral or "
3447 "enumeration type"));
3448 parser->non_integral_constant_expression_p = true;
3449 }
3450
3451 switch (keyword)
3452 {
3453 case RID_DYNCAST:
3454 postfix_expression
3455 = build_dynamic_cast (type, expression);
3456 break;
3457 case RID_STATCAST:
3458 postfix_expression
3459 = build_static_cast (type, expression);
3460 break;
3461 case RID_REINTCAST:
3462 postfix_expression
3463 = build_reinterpret_cast (type, expression);
3464 break;
3465 case RID_CONSTCAST:
3466 postfix_expression
3467 = build_const_cast (type, expression);
3468 break;
3469 default:
3470 abort ();
3471 }
3472 }
3473 break;
3474
3475 case RID_TYPEID:
3476 {
3477 tree type;
3478 const char *saved_message;
3479 bool saved_in_type_id_in_expr_p;
3480
3481 /* Consume the `typeid' token. */
3482 cp_lexer_consume_token (parser->lexer);
3483 /* Look for the `(' token. */
3484 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3485 /* Types cannot be defined in a `typeid' expression. */
3486 saved_message = parser->type_definition_forbidden_message;
3487 parser->type_definition_forbidden_message
3488 = "types may not be defined in a `typeid\' expression";
3489 /* We can't be sure yet whether we're looking at a type-id or an
3490 expression. */
3491 cp_parser_parse_tentatively (parser);
3492 /* Try a type-id first. */
3493 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3494 parser->in_type_id_in_expr_p = true;
3495 type = cp_parser_type_id (parser);
3496 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3497 /* Look for the `)' token. Otherwise, we can't be sure that
3498 we're not looking at an expression: consider `typeid (int
3499 (3))', for example. */
3500 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3501 /* If all went well, simply lookup the type-id. */
3502 if (cp_parser_parse_definitely (parser))
3503 postfix_expression = get_typeid (type);
3504 /* Otherwise, fall back to the expression variant. */
3505 else
3506 {
3507 tree expression;
3508
3509 /* Look for an expression. */
3510 expression = cp_parser_expression (parser);
3511 /* Compute its typeid. */
3512 postfix_expression = build_typeid (expression);
3513 /* Look for the `)' token. */
3514 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3515 }
3516
3517 /* Restore the saved message. */
3518 parser->type_definition_forbidden_message = saved_message;
3519 }
3520 break;
3521
3522 case RID_TYPENAME:
3523 {
3524 bool template_p = false;
3525 tree id;
3526 tree type;
3527
3528 /* Consume the `typename' token. */
3529 cp_lexer_consume_token (parser->lexer);
3530 /* Look for the optional `::' operator. */
3531 cp_parser_global_scope_opt (parser,
3532 /*current_scope_valid_p=*/false);
3533 /* Look for the nested-name-specifier. */
3534 cp_parser_nested_name_specifier (parser,
3535 /*typename_keyword_p=*/true,
3536 /*check_dependency_p=*/true,
3537 /*type_p=*/true,
3538 /*is_declaration=*/true);
3539 /* Look for the optional `template' keyword. */
3540 template_p = cp_parser_optional_template_keyword (parser);
3541 /* We don't know whether we're looking at a template-id or an
3542 identifier. */
3543 cp_parser_parse_tentatively (parser);
3544 /* Try a template-id. */
3545 id = cp_parser_template_id (parser, template_p,
3546 /*check_dependency_p=*/true,
3547 /*is_declaration=*/true);
3548 /* If that didn't work, try an identifier. */
3549 if (!cp_parser_parse_definitely (parser))
3550 id = cp_parser_identifier (parser);
3551 /* Create a TYPENAME_TYPE to represent the type to which the
3552 functional cast is being performed. */
3553 type = make_typename_type (parser->scope, id,
3554 /*complain=*/1);
3555
3556 postfix_expression = cp_parser_functional_cast (parser, type);
3557 }
3558 break;
3559
3560 default:
3561 {
3562 tree type;
3563
3564 /* If the next thing is a simple-type-specifier, we may be
3565 looking at a functional cast. We could also be looking at
3566 an id-expression. So, we try the functional cast, and if
3567 that doesn't work we fall back to the primary-expression. */
3568 cp_parser_parse_tentatively (parser);
3569 /* Look for the simple-type-specifier. */
3570 type = cp_parser_simple_type_specifier (parser,
3571 CP_PARSER_FLAGS_NONE,
3572 /*identifier_p=*/false);
3573 /* Parse the cast itself. */
3574 if (!cp_parser_error_occurred (parser))
3575 postfix_expression
3576 = cp_parser_functional_cast (parser, type);
3577 /* If that worked, we're done. */
3578 if (cp_parser_parse_definitely (parser))
3579 break;
3580
3581 /* If the functional-cast didn't work out, try a
3582 compound-literal. */
3583 if (cp_parser_allow_gnu_extensions_p (parser)
3584 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3585 {
3586 tree initializer_list = NULL_TREE;
3587 bool saved_in_type_id_in_expr_p;
3588
3589 cp_parser_parse_tentatively (parser);
3590 /* Consume the `('. */
3591 cp_lexer_consume_token (parser->lexer);
3592 /* Parse the type. */
3593 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3594 parser->in_type_id_in_expr_p = true;
3595 type = cp_parser_type_id (parser);
3596 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3597 /* Look for the `)'. */
3598 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3599 /* Look for the `{'. */
3600 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3601 /* If things aren't going well, there's no need to
3602 keep going. */
3603 if (!cp_parser_error_occurred (parser))
3604 {
3605 bool non_constant_p;
3606 /* Parse the initializer-list. */
3607 initializer_list
3608 = cp_parser_initializer_list (parser, &non_constant_p);
3609 /* Allow a trailing `,'. */
3610 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3611 cp_lexer_consume_token (parser->lexer);
3612 /* Look for the final `}'. */
3613 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3614 }
3615 /* If that worked, we're definitely looking at a
3616 compound-literal expression. */
3617 if (cp_parser_parse_definitely (parser))
3618 {
3619 /* Warn the user that a compound literal is not
3620 allowed in standard C++. */
3621 if (pedantic)
3622 pedwarn ("ISO C++ forbids compound-literals");
3623 /* Form the representation of the compound-literal. */
3624 postfix_expression
3625 = finish_compound_literal (type, initializer_list);
3626 break;
3627 }
3628 }
3629
3630 /* It must be a primary-expression. */
3631 postfix_expression = cp_parser_primary_expression (parser,
3632 &idk,
3633 &qualifying_class);
3634 }
3635 break;
3636 }
3637
3638 /* If we were avoiding committing to the processing of a
3639 qualified-id until we knew whether or not we had a
3640 pointer-to-member, we now know. */
3641 if (qualifying_class)
3642 {
3643 bool done;
3644
3645 /* Peek at the next token. */
3646 token = cp_lexer_peek_token (parser->lexer);
3647 done = (token->type != CPP_OPEN_SQUARE
3648 && token->type != CPP_OPEN_PAREN
3649 && token->type != CPP_DOT
3650 && token->type != CPP_DEREF
3651 && token->type != CPP_PLUS_PLUS
3652 && token->type != CPP_MINUS_MINUS);
3653
3654 postfix_expression = finish_qualified_id_expr (qualifying_class,
3655 postfix_expression,
3656 done,
3657 address_p);
3658 if (done)
3659 return postfix_expression;
3660 }
3661
3662 /* Keep looping until the postfix-expression is complete. */
3663 while (true)
3664 {
3665 if (idk == CP_ID_KIND_UNQUALIFIED
3666 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3667 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3668 /* It is not a Koenig lookup function call. */
3669 postfix_expression
3670 = unqualified_name_lookup_error (postfix_expression);
3671
3672 /* Peek at the next token. */
3673 token = cp_lexer_peek_token (parser->lexer);
3674
3675 switch (token->type)
3676 {
3677 case CPP_OPEN_SQUARE:
3678 /* postfix-expression [ expression ] */
3679 {
3680 tree index;
3681
3682 /* Consume the `[' token. */
3683 cp_lexer_consume_token (parser->lexer);
3684 /* Parse the index expression. */
3685 index = cp_parser_expression (parser);
3686 /* Look for the closing `]'. */
3687 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3688
3689 /* Build the ARRAY_REF. */
3690 postfix_expression
3691 = grok_array_decl (postfix_expression, index);
3692 idk = CP_ID_KIND_NONE;
3693 /* Array references are not permitted in
3694 constant-expressions. */
3695 if (parser->integral_constant_expression_p)
3696 {
3697 if (!parser->allow_non_integral_constant_expression_p)
3698 postfix_expression
3699 = cp_parser_non_integral_constant_expression ("an array reference");
3700 parser->non_integral_constant_expression_p = true;
3701 }
3702 }
3703 break;
3704
3705 case CPP_OPEN_PAREN:
3706 /* postfix-expression ( expression-list [opt] ) */
3707 {
3708 bool koenig_p;
3709 tree args = (cp_parser_parenthesized_expression_list
3710 (parser, false, /*non_constant_p=*/NULL));
3711
3712 if (args == error_mark_node)
3713 {
3714 postfix_expression = error_mark_node;
3715 break;
3716 }
3717
3718 /* Function calls are not permitted in
3719 constant-expressions. */
3720 if (parser->integral_constant_expression_p)
3721 {
3722 if (!parser->allow_non_integral_constant_expression_p)
3723 {
3724 postfix_expression
3725 = cp_parser_non_integral_constant_expression ("a function call");
3726 break;
3727 }
3728 parser->non_integral_constant_expression_p = true;
3729 }
3730
3731 koenig_p = false;
3732 if (idk == CP_ID_KIND_UNQUALIFIED)
3733 {
3734 if (args
3735 && (is_overloaded_fn (postfix_expression)
3736 || DECL_P (postfix_expression)
3737 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3738 {
3739 koenig_p = true;
3740 postfix_expression
3741 = perform_koenig_lookup (postfix_expression, args);
3742 }
3743 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3744 postfix_expression
3745 = unqualified_fn_lookup_error (postfix_expression);
3746 }
3747
3748 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3749 {
3750 tree instance = TREE_OPERAND (postfix_expression, 0);
3751 tree fn = TREE_OPERAND (postfix_expression, 1);
3752
3753 if (processing_template_decl
3754 && (type_dependent_expression_p (instance)
3755 || (!BASELINK_P (fn)
3756 && TREE_CODE (fn) != FIELD_DECL)
3757 || type_dependent_expression_p (fn)
3758 || any_type_dependent_arguments_p (args)))
3759 {
3760 postfix_expression
3761 = build_min_nt (CALL_EXPR, postfix_expression, args);
3762 break;
3763 }
3764
3765 if (BASELINK_P (fn))
3766 postfix_expression
3767 = (build_new_method_call
3768 (instance, fn, args, NULL_TREE,
3769 (idk == CP_ID_KIND_QUALIFIED
3770 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3771 else
3772 postfix_expression
3773 = finish_call_expr (postfix_expression, args,
3774 /*disallow_virtual=*/false,
3775 /*koenig_p=*/false);
3776 }
3777 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3778 || TREE_CODE (postfix_expression) == MEMBER_REF
3779 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3780 postfix_expression = (build_offset_ref_call_from_tree
3781 (postfix_expression, args));
3782 else if (idk == CP_ID_KIND_QUALIFIED)
3783 /* A call to a static class member, or a namespace-scope
3784 function. */
3785 postfix_expression
3786 = finish_call_expr (postfix_expression, args,
3787 /*disallow_virtual=*/true,
3788 koenig_p);
3789 else
3790 /* All other function calls. */
3791 postfix_expression
3792 = finish_call_expr (postfix_expression, args,
3793 /*disallow_virtual=*/false,
3794 koenig_p);
3795
3796 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3797 idk = CP_ID_KIND_NONE;
3798 }
3799 break;
3800
3801 case CPP_DOT:
3802 case CPP_DEREF:
3803 /* postfix-expression . template [opt] id-expression
3804 postfix-expression . pseudo-destructor-name
3805 postfix-expression -> template [opt] id-expression
3806 postfix-expression -> pseudo-destructor-name */
3807 {
3808 tree name;
3809 bool dependent_p;
3810 bool template_p;
3811 tree scope = NULL_TREE;
3812 enum cpp_ttype token_type = token->type;
3813
3814 /* If this is a `->' operator, dereference the pointer. */
3815 if (token->type == CPP_DEREF)
3816 postfix_expression = build_x_arrow (postfix_expression);
3817 /* Check to see whether or not the expression is
3818 type-dependent. */
3819 dependent_p = type_dependent_expression_p (postfix_expression);
3820 /* The identifier following the `->' or `.' is not
3821 qualified. */
3822 parser->scope = NULL_TREE;
3823 parser->qualifying_scope = NULL_TREE;
3824 parser->object_scope = NULL_TREE;
3825 idk = CP_ID_KIND_NONE;
3826 /* Enter the scope corresponding to the type of the object
3827 given by the POSTFIX_EXPRESSION. */
3828 if (!dependent_p
3829 && TREE_TYPE (postfix_expression) != NULL_TREE)
3830 {
3831 scope = TREE_TYPE (postfix_expression);
3832 /* According to the standard, no expression should
3833 ever have reference type. Unfortunately, we do not
3834 currently match the standard in this respect in
3835 that our internal representation of an expression
3836 may have reference type even when the standard says
3837 it does not. Therefore, we have to manually obtain
3838 the underlying type here. */
3839 scope = non_reference (scope);
3840 /* The type of the POSTFIX_EXPRESSION must be
3841 complete. */
3842 scope = complete_type_or_else (scope, NULL_TREE);
3843 /* Let the name lookup machinery know that we are
3844 processing a class member access expression. */
3845 parser->context->object_type = scope;
3846 /* If something went wrong, we want to be able to
3847 discern that case, as opposed to the case where
3848 there was no SCOPE due to the type of expression
3849 being dependent. */
3850 if (!scope)
3851 scope = error_mark_node;
3852 /* If the SCOPE was erroneous, make the various
3853 semantic analysis functions exit quickly -- and
3854 without issuing additional error messages. */
3855 if (scope == error_mark_node)
3856 postfix_expression = error_mark_node;
3857 }
3858
3859 /* Consume the `.' or `->' operator. */
3860 cp_lexer_consume_token (parser->lexer);
3861 /* If the SCOPE is not a scalar type, we are looking at an
3862 ordinary class member access expression, rather than a
3863 pseudo-destructor-name. */
3864 if (!scope || !SCALAR_TYPE_P (scope))
3865 {
3866 template_p = cp_parser_optional_template_keyword (parser);
3867 /* Parse the id-expression. */
3868 name = cp_parser_id_expression (parser,
3869 template_p,
3870 /*check_dependency_p=*/true,
3871 /*template_p=*/NULL,
3872 /*declarator_p=*/false);
3873 /* In general, build a SCOPE_REF if the member name is
3874 qualified. However, if the name was not dependent
3875 and has already been resolved; there is no need to
3876 build the SCOPE_REF. For example;
3877
3878 struct X { void f(); };
3879 template <typename T> void f(T* t) { t->X::f(); }
3880
3881 Even though "t" is dependent, "X::f" is not and has
3882 been resolved to a BASELINK; there is no need to
3883 include scope information. */
3884
3885 /* But we do need to remember that there was an explicit
3886 scope for virtual function calls. */
3887 if (parser->scope)
3888 idk = CP_ID_KIND_QUALIFIED;
3889
3890 if (name != error_mark_node
3891 && !BASELINK_P (name)
3892 && parser->scope)
3893 {
3894 name = build_nt (SCOPE_REF, parser->scope, name);
3895 parser->scope = NULL_TREE;
3896 parser->qualifying_scope = NULL_TREE;
3897 parser->object_scope = NULL_TREE;
3898 }
3899 postfix_expression
3900 = finish_class_member_access_expr (postfix_expression, name);
3901 }
3902 /* Otherwise, try the pseudo-destructor-name production. */
3903 else
3904 {
3905 tree s = NULL_TREE;
3906 tree type;
3907
3908 /* Parse the pseudo-destructor-name. */
3909 cp_parser_pseudo_destructor_name (parser, &s, &type);
3910 /* Form the call. */
3911 postfix_expression
3912 = finish_pseudo_destructor_expr (postfix_expression,
3913 s, TREE_TYPE (type));
3914 }
3915
3916 /* We no longer need to look up names in the scope of the
3917 object on the left-hand side of the `.' or `->'
3918 operator. */
3919 parser->context->object_type = NULL_TREE;
3920 /* These operators may not appear in constant-expressions. */
3921 if (parser->integral_constant_expression_p
3922 /* The "->" operator is allowed in the implementation
3923 of "offsetof". The "." operator may appear in the
3924 name of the member. */
3925 && !parser->in_offsetof_p)
3926 {
3927 if (!parser->allow_non_integral_constant_expression_p)
3928 postfix_expression
3929 = (cp_parser_non_integral_constant_expression
3930 (token_type == CPP_DEREF ? "'->'" : "`.'"));
3931 parser->non_integral_constant_expression_p = true;
3932 }
3933 }
3934 break;
3935
3936 case CPP_PLUS_PLUS:
3937 /* postfix-expression ++ */
3938 /* Consume the `++' token. */
3939 cp_lexer_consume_token (parser->lexer);
3940 /* Generate a representation for the complete expression. */
3941 postfix_expression
3942 = finish_increment_expr (postfix_expression,
3943 POSTINCREMENT_EXPR);
3944 /* Increments may not appear in constant-expressions. */
3945 if (parser->integral_constant_expression_p)
3946 {
3947 if (!parser->allow_non_integral_constant_expression_p)
3948 postfix_expression
3949 = cp_parser_non_integral_constant_expression ("an increment");
3950 parser->non_integral_constant_expression_p = true;
3951 }
3952 idk = CP_ID_KIND_NONE;
3953 break;
3954
3955 case CPP_MINUS_MINUS:
3956 /* postfix-expression -- */
3957 /* Consume the `--' token. */
3958 cp_lexer_consume_token (parser->lexer);
3959 /* Generate a representation for the complete expression. */
3960 postfix_expression
3961 = finish_increment_expr (postfix_expression,
3962 POSTDECREMENT_EXPR);
3963 /* Decrements may not appear in constant-expressions. */
3964 if (parser->integral_constant_expression_p)
3965 {
3966 if (!parser->allow_non_integral_constant_expression_p)
3967 postfix_expression
3968 = cp_parser_non_integral_constant_expression ("a decrement");
3969 parser->non_integral_constant_expression_p = true;
3970 }
3971 idk = CP_ID_KIND_NONE;
3972 break;
3973
3974 default:
3975 return postfix_expression;
3976 }
3977 }
3978
3979 /* We should never get here. */
3980 abort ();
3981 return error_mark_node;
3982 }
3983
3984 /* Parse a parenthesized expression-list.
3985
3986 expression-list:
3987 assignment-expression
3988 expression-list, assignment-expression
3989
3990 attribute-list:
3991 expression-list
3992 identifier
3993 identifier, expression-list
3994
3995 Returns a TREE_LIST. The TREE_VALUE of each node is a
3996 representation of an assignment-expression. Note that a TREE_LIST
3997 is returned even if there is only a single expression in the list.
3998 error_mark_node is returned if the ( and or ) are
3999 missing. NULL_TREE is returned on no expressions. The parentheses
4000 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4001 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4002 indicates whether or not all of the expressions in the list were
4003 constant. */
4004
4005 static tree
4006 cp_parser_parenthesized_expression_list (cp_parser* parser,
4007 bool is_attribute_list,
4008 bool *non_constant_p)
4009 {
4010 tree expression_list = NULL_TREE;
4011 tree identifier = NULL_TREE;
4012
4013 /* Assume all the expressions will be constant. */
4014 if (non_constant_p)
4015 *non_constant_p = false;
4016
4017 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4018 return error_mark_node;
4019
4020 /* Consume expressions until there are no more. */
4021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4022 while (true)
4023 {
4024 tree expr;
4025
4026 /* At the beginning of attribute lists, check to see if the
4027 next token is an identifier. */
4028 if (is_attribute_list
4029 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4030 {
4031 cp_token *token;
4032
4033 /* Consume the identifier. */
4034 token = cp_lexer_consume_token (parser->lexer);
4035 /* Save the identifier. */
4036 identifier = token->value;
4037 }
4038 else
4039 {
4040 /* Parse the next assignment-expression. */
4041 if (non_constant_p)
4042 {
4043 bool expr_non_constant_p;
4044 expr = (cp_parser_constant_expression
4045 (parser, /*allow_non_constant_p=*/true,
4046 &expr_non_constant_p));
4047 if (expr_non_constant_p)
4048 *non_constant_p = true;
4049 }
4050 else
4051 expr = cp_parser_assignment_expression (parser);
4052
4053 /* Add it to the list. We add error_mark_node
4054 expressions to the list, so that we can still tell if
4055 the correct form for a parenthesized expression-list
4056 is found. That gives better errors. */
4057 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4058
4059 if (expr == error_mark_node)
4060 goto skip_comma;
4061 }
4062
4063 /* After the first item, attribute lists look the same as
4064 expression lists. */
4065 is_attribute_list = false;
4066
4067 get_comma:;
4068 /* If the next token isn't a `,', then we are done. */
4069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4070 break;
4071
4072 /* Otherwise, consume the `,' and keep going. */
4073 cp_lexer_consume_token (parser->lexer);
4074 }
4075
4076 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4077 {
4078 int ending;
4079
4080 skip_comma:;
4081 /* We try and resync to an unnested comma, as that will give the
4082 user better diagnostics. */
4083 ending = cp_parser_skip_to_closing_parenthesis (parser,
4084 /*recovering=*/true,
4085 /*or_comma=*/true,
4086 /*consume_paren=*/true);
4087 if (ending < 0)
4088 goto get_comma;
4089 if (!ending)
4090 return error_mark_node;
4091 }
4092
4093 /* We built up the list in reverse order so we must reverse it now. */
4094 expression_list = nreverse (expression_list);
4095 if (identifier)
4096 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4097
4098 return expression_list;
4099 }
4100
4101 /* Parse a pseudo-destructor-name.
4102
4103 pseudo-destructor-name:
4104 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4105 :: [opt] nested-name-specifier template template-id :: ~ type-name
4106 :: [opt] nested-name-specifier [opt] ~ type-name
4107
4108 If either of the first two productions is used, sets *SCOPE to the
4109 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4110 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4111 or ERROR_MARK_NODE if no type-name is present. */
4112
4113 static void
4114 cp_parser_pseudo_destructor_name (cp_parser* parser,
4115 tree* scope,
4116 tree* type)
4117 {
4118 bool nested_name_specifier_p;
4119
4120 /* Look for the optional `::' operator. */
4121 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4122 /* Look for the optional nested-name-specifier. */
4123 nested_name_specifier_p
4124 = (cp_parser_nested_name_specifier_opt (parser,
4125 /*typename_keyword_p=*/false,
4126 /*check_dependency_p=*/true,
4127 /*type_p=*/false,
4128 /*is_declaration=*/true)
4129 != NULL_TREE);
4130 /* Now, if we saw a nested-name-specifier, we might be doing the
4131 second production. */
4132 if (nested_name_specifier_p
4133 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4134 {
4135 /* Consume the `template' keyword. */
4136 cp_lexer_consume_token (parser->lexer);
4137 /* Parse the template-id. */
4138 cp_parser_template_id (parser,
4139 /*template_keyword_p=*/true,
4140 /*check_dependency_p=*/false,
4141 /*is_declaration=*/true);
4142 /* Look for the `::' token. */
4143 cp_parser_require (parser, CPP_SCOPE, "`::'");
4144 }
4145 /* If the next token is not a `~', then there might be some
4146 additional qualification. */
4147 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4148 {
4149 /* Look for the type-name. */
4150 *scope = TREE_TYPE (cp_parser_type_name (parser));
4151 /* Look for the `::' token. */
4152 cp_parser_require (parser, CPP_SCOPE, "`::'");
4153 }
4154 else
4155 *scope = NULL_TREE;
4156
4157 /* Look for the `~'. */
4158 cp_parser_require (parser, CPP_COMPL, "`~'");
4159 /* Look for the type-name again. We are not responsible for
4160 checking that it matches the first type-name. */
4161 *type = cp_parser_type_name (parser);
4162 }
4163
4164 /* Parse a unary-expression.
4165
4166 unary-expression:
4167 postfix-expression
4168 ++ cast-expression
4169 -- cast-expression
4170 unary-operator cast-expression
4171 sizeof unary-expression
4172 sizeof ( type-id )
4173 new-expression
4174 delete-expression
4175
4176 GNU Extensions:
4177
4178 unary-expression:
4179 __extension__ cast-expression
4180 __alignof__ unary-expression
4181 __alignof__ ( type-id )
4182 __real__ cast-expression
4183 __imag__ cast-expression
4184 && identifier
4185
4186 ADDRESS_P is true iff the unary-expression is appearing as the
4187 operand of the `&' operator.
4188
4189 Returns a representation of the expression. */
4190
4191 static tree
4192 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4193 {
4194 cp_token *token;
4195 enum tree_code unary_operator;
4196
4197 /* Peek at the next token. */
4198 token = cp_lexer_peek_token (parser->lexer);
4199 /* Some keywords give away the kind of expression. */
4200 if (token->type == CPP_KEYWORD)
4201 {
4202 enum rid keyword = token->keyword;
4203
4204 switch (keyword)
4205 {
4206 case RID_ALIGNOF:
4207 case RID_SIZEOF:
4208 {
4209 tree operand;
4210 enum tree_code op;
4211
4212 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4213 /* Consume the token. */
4214 cp_lexer_consume_token (parser->lexer);
4215 /* Parse the operand. */
4216 operand = cp_parser_sizeof_operand (parser, keyword);
4217
4218 if (TYPE_P (operand))
4219 return cxx_sizeof_or_alignof_type (operand, op, true);
4220 else
4221 return cxx_sizeof_or_alignof_expr (operand, op);
4222 }
4223
4224 case RID_NEW:
4225 return cp_parser_new_expression (parser);
4226
4227 case RID_DELETE:
4228 return cp_parser_delete_expression (parser);
4229
4230 case RID_EXTENSION:
4231 {
4232 /* The saved value of the PEDANTIC flag. */
4233 int saved_pedantic;
4234 tree expr;
4235
4236 /* Save away the PEDANTIC flag. */
4237 cp_parser_extension_opt (parser, &saved_pedantic);
4238 /* Parse the cast-expression. */
4239 expr = cp_parser_simple_cast_expression (parser);
4240 /* Restore the PEDANTIC flag. */
4241 pedantic = saved_pedantic;
4242
4243 return expr;
4244 }
4245
4246 case RID_REALPART:
4247 case RID_IMAGPART:
4248 {
4249 tree expression;
4250
4251 /* Consume the `__real__' or `__imag__' token. */
4252 cp_lexer_consume_token (parser->lexer);
4253 /* Parse the cast-expression. */
4254 expression = cp_parser_simple_cast_expression (parser);
4255 /* Create the complete representation. */
4256 return build_x_unary_op ((keyword == RID_REALPART
4257 ? REALPART_EXPR : IMAGPART_EXPR),
4258 expression);
4259 }
4260 break;
4261
4262 default:
4263 break;
4264 }
4265 }
4266
4267 /* Look for the `:: new' and `:: delete', which also signal the
4268 beginning of a new-expression, or delete-expression,
4269 respectively. If the next token is `::', then it might be one of
4270 these. */
4271 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4272 {
4273 enum rid keyword;
4274
4275 /* See if the token after the `::' is one of the keywords in
4276 which we're interested. */
4277 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4278 /* If it's `new', we have a new-expression. */
4279 if (keyword == RID_NEW)
4280 return cp_parser_new_expression (parser);
4281 /* Similarly, for `delete'. */
4282 else if (keyword == RID_DELETE)
4283 return cp_parser_delete_expression (parser);
4284 }
4285
4286 /* Look for a unary operator. */
4287 unary_operator = cp_parser_unary_operator (token);
4288 /* The `++' and `--' operators can be handled similarly, even though
4289 they are not technically unary-operators in the grammar. */
4290 if (unary_operator == ERROR_MARK)
4291 {
4292 if (token->type == CPP_PLUS_PLUS)
4293 unary_operator = PREINCREMENT_EXPR;
4294 else if (token->type == CPP_MINUS_MINUS)
4295 unary_operator = PREDECREMENT_EXPR;
4296 /* Handle the GNU address-of-label extension. */
4297 else if (cp_parser_allow_gnu_extensions_p (parser)
4298 && token->type == CPP_AND_AND)
4299 {
4300 tree identifier;
4301
4302 /* Consume the '&&' token. */
4303 cp_lexer_consume_token (parser->lexer);
4304 /* Look for the identifier. */
4305 identifier = cp_parser_identifier (parser);
4306 /* Create an expression representing the address. */
4307 return finish_label_address_expr (identifier);
4308 }
4309 }
4310 if (unary_operator != ERROR_MARK)
4311 {
4312 tree cast_expression;
4313 tree expression = error_mark_node;
4314 const char *non_constant_p = NULL;
4315
4316 /* Consume the operator token. */
4317 token = cp_lexer_consume_token (parser->lexer);
4318 /* Parse the cast-expression. */
4319 cast_expression
4320 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4321 /* Now, build an appropriate representation. */
4322 switch (unary_operator)
4323 {
4324 case INDIRECT_REF:
4325 non_constant_p = "`*'";
4326 expression = build_x_indirect_ref (cast_expression, "unary *");
4327 break;
4328
4329 case ADDR_EXPR:
4330 /* The "&" operator is allowed in the implementation of
4331 "offsetof". */
4332 if (!parser->in_offsetof_p)
4333 non_constant_p = "`&'";
4334 /* Fall through. */
4335 case BIT_NOT_EXPR:
4336 expression = build_x_unary_op (unary_operator, cast_expression);
4337 break;
4338
4339 case PREINCREMENT_EXPR:
4340 case PREDECREMENT_EXPR:
4341 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4342 ? "`++'" : "`--'");
4343 /* Fall through. */
4344 case CONVERT_EXPR:
4345 case NEGATE_EXPR:
4346 case TRUTH_NOT_EXPR:
4347 expression = finish_unary_op_expr (unary_operator, cast_expression);
4348 break;
4349
4350 default:
4351 abort ();
4352 }
4353
4354 if (non_constant_p && parser->integral_constant_expression_p)
4355 {
4356 if (!parser->allow_non_integral_constant_expression_p)
4357 return cp_parser_non_integral_constant_expression (non_constant_p);
4358 parser->non_integral_constant_expression_p = true;
4359 }
4360
4361 return expression;
4362 }
4363
4364 return cp_parser_postfix_expression (parser, address_p);
4365 }
4366
4367 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4368 unary-operator, the corresponding tree code is returned. */
4369
4370 static enum tree_code
4371 cp_parser_unary_operator (cp_token* token)
4372 {
4373 switch (token->type)
4374 {
4375 case CPP_MULT:
4376 return INDIRECT_REF;
4377
4378 case CPP_AND:
4379 return ADDR_EXPR;
4380
4381 case CPP_PLUS:
4382 return CONVERT_EXPR;
4383
4384 case CPP_MINUS:
4385 return NEGATE_EXPR;
4386
4387 case CPP_NOT:
4388 return TRUTH_NOT_EXPR;
4389
4390 case CPP_COMPL:
4391 return BIT_NOT_EXPR;
4392
4393 default:
4394 return ERROR_MARK;
4395 }
4396 }
4397
4398 /* Parse a new-expression.
4399
4400 new-expression:
4401 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4402 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4403
4404 Returns a representation of the expression. */
4405
4406 static tree
4407 cp_parser_new_expression (cp_parser* parser)
4408 {
4409 bool global_scope_p;
4410 tree placement;
4411 tree type;
4412 tree initializer;
4413
4414 /* Look for the optional `::' operator. */
4415 global_scope_p
4416 = (cp_parser_global_scope_opt (parser,
4417 /*current_scope_valid_p=*/false)
4418 != NULL_TREE);
4419 /* Look for the `new' operator. */
4420 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4421 /* There's no easy way to tell a new-placement from the
4422 `( type-id )' construct. */
4423 cp_parser_parse_tentatively (parser);
4424 /* Look for a new-placement. */
4425 placement = cp_parser_new_placement (parser);
4426 /* If that didn't work out, there's no new-placement. */
4427 if (!cp_parser_parse_definitely (parser))
4428 placement = NULL_TREE;
4429
4430 /* If the next token is a `(', then we have a parenthesized
4431 type-id. */
4432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4433 {
4434 /* Consume the `('. */
4435 cp_lexer_consume_token (parser->lexer);
4436 /* Parse the type-id. */
4437 type = cp_parser_type_id (parser);
4438 /* Look for the closing `)'. */
4439 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4440 }
4441 /* Otherwise, there must be a new-type-id. */
4442 else
4443 type = cp_parser_new_type_id (parser);
4444
4445 /* If the next token is a `(', then we have a new-initializer. */
4446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4447 initializer = cp_parser_new_initializer (parser);
4448 else
4449 initializer = NULL_TREE;
4450
4451 /* Create a representation of the new-expression. */
4452 return build_new (placement, type, initializer, global_scope_p);
4453 }
4454
4455 /* Parse a new-placement.
4456
4457 new-placement:
4458 ( expression-list )
4459
4460 Returns the same representation as for an expression-list. */
4461
4462 static tree
4463 cp_parser_new_placement (cp_parser* parser)
4464 {
4465 tree expression_list;
4466
4467 /* Parse the expression-list. */
4468 expression_list = (cp_parser_parenthesized_expression_list
4469 (parser, false, /*non_constant_p=*/NULL));
4470
4471 return expression_list;
4472 }
4473
4474 /* Parse a new-type-id.
4475
4476 new-type-id:
4477 type-specifier-seq new-declarator [opt]
4478
4479 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4480 and whose TREE_VALUE is the new-declarator. */
4481
4482 static tree
4483 cp_parser_new_type_id (cp_parser* parser)
4484 {
4485 tree type_specifier_seq;
4486 tree declarator;
4487 const char *saved_message;
4488
4489 /* The type-specifier sequence must not contain type definitions.
4490 (It cannot contain declarations of new types either, but if they
4491 are not definitions we will catch that because they are not
4492 complete.) */
4493 saved_message = parser->type_definition_forbidden_message;
4494 parser->type_definition_forbidden_message
4495 = "types may not be defined in a new-type-id";
4496 /* Parse the type-specifier-seq. */
4497 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4498 /* Restore the old message. */
4499 parser->type_definition_forbidden_message = saved_message;
4500 /* Parse the new-declarator. */
4501 declarator = cp_parser_new_declarator_opt (parser);
4502
4503 return build_tree_list (type_specifier_seq, declarator);
4504 }
4505
4506 /* Parse an (optional) new-declarator.
4507
4508 new-declarator:
4509 ptr-operator new-declarator [opt]
4510 direct-new-declarator
4511
4512 Returns a representation of the declarator. See
4513 cp_parser_declarator for the representations used. */
4514
4515 static tree
4516 cp_parser_new_declarator_opt (cp_parser* parser)
4517 {
4518 enum tree_code code;
4519 tree type;
4520 tree cv_qualifier_seq;
4521
4522 /* We don't know if there's a ptr-operator next, or not. */
4523 cp_parser_parse_tentatively (parser);
4524 /* Look for a ptr-operator. */
4525 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4526 /* If that worked, look for more new-declarators. */
4527 if (cp_parser_parse_definitely (parser))
4528 {
4529 tree declarator;
4530
4531 /* Parse another optional declarator. */
4532 declarator = cp_parser_new_declarator_opt (parser);
4533
4534 /* Create the representation of the declarator. */
4535 if (code == INDIRECT_REF)
4536 declarator = make_pointer_declarator (cv_qualifier_seq,
4537 declarator);
4538 else
4539 declarator = make_reference_declarator (cv_qualifier_seq,
4540 declarator);
4541
4542 /* Handle the pointer-to-member case. */
4543 if (type)
4544 declarator = build_nt (SCOPE_REF, type, declarator);
4545
4546 return declarator;
4547 }
4548
4549 /* If the next token is a `[', there is a direct-new-declarator. */
4550 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4551 return cp_parser_direct_new_declarator (parser);
4552
4553 return NULL_TREE;
4554 }
4555
4556 /* Parse a direct-new-declarator.
4557
4558 direct-new-declarator:
4559 [ expression ]
4560 direct-new-declarator [constant-expression]
4561
4562 Returns an ARRAY_REF, following the same conventions as are
4563 documented for cp_parser_direct_declarator. */
4564
4565 static tree
4566 cp_parser_direct_new_declarator (cp_parser* parser)
4567 {
4568 tree declarator = NULL_TREE;
4569
4570 while (true)
4571 {
4572 tree expression;
4573
4574 /* Look for the opening `['. */
4575 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4576 /* The first expression is not required to be constant. */
4577 if (!declarator)
4578 {
4579 expression = cp_parser_expression (parser);
4580 /* The standard requires that the expression have integral
4581 type. DR 74 adds enumeration types. We believe that the
4582 real intent is that these expressions be handled like the
4583 expression in a `switch' condition, which also allows
4584 classes with a single conversion to integral or
4585 enumeration type. */
4586 if (!processing_template_decl)
4587 {
4588 expression
4589 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4590 expression,
4591 /*complain=*/true);
4592 if (!expression)
4593 {
4594 error ("expression in new-declarator must have integral or enumeration type");
4595 expression = error_mark_node;
4596 }
4597 }
4598 }
4599 /* But all the other expressions must be. */
4600 else
4601 expression
4602 = cp_parser_constant_expression (parser,
4603 /*allow_non_constant=*/false,
4604 NULL);
4605 /* Look for the closing `]'. */
4606 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4607
4608 /* Add this bound to the declarator. */
4609 declarator = build_nt (ARRAY_REF, declarator, expression);
4610
4611 /* If the next token is not a `[', then there are no more
4612 bounds. */
4613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4614 break;
4615 }
4616
4617 return declarator;
4618 }
4619
4620 /* Parse a new-initializer.
4621
4622 new-initializer:
4623 ( expression-list [opt] )
4624
4625 Returns a representation of the expression-list. If there is no
4626 expression-list, VOID_ZERO_NODE is returned. */
4627
4628 static tree
4629 cp_parser_new_initializer (cp_parser* parser)
4630 {
4631 tree expression_list;
4632
4633 expression_list = (cp_parser_parenthesized_expression_list
4634 (parser, false, /*non_constant_p=*/NULL));
4635 if (!expression_list)
4636 expression_list = void_zero_node;
4637
4638 return expression_list;
4639 }
4640
4641 /* Parse a delete-expression.
4642
4643 delete-expression:
4644 :: [opt] delete cast-expression
4645 :: [opt] delete [ ] cast-expression
4646
4647 Returns a representation of the expression. */
4648
4649 static tree
4650 cp_parser_delete_expression (cp_parser* parser)
4651 {
4652 bool global_scope_p;
4653 bool array_p;
4654 tree expression;
4655
4656 /* Look for the optional `::' operator. */
4657 global_scope_p
4658 = (cp_parser_global_scope_opt (parser,
4659 /*current_scope_valid_p=*/false)
4660 != NULL_TREE);
4661 /* Look for the `delete' keyword. */
4662 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4663 /* See if the array syntax is in use. */
4664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4665 {
4666 /* Consume the `[' token. */
4667 cp_lexer_consume_token (parser->lexer);
4668 /* Look for the `]' token. */
4669 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4670 /* Remember that this is the `[]' construct. */
4671 array_p = true;
4672 }
4673 else
4674 array_p = false;
4675
4676 /* Parse the cast-expression. */
4677 expression = cp_parser_simple_cast_expression (parser);
4678
4679 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4680 }
4681
4682 /* Parse a cast-expression.
4683
4684 cast-expression:
4685 unary-expression
4686 ( type-id ) cast-expression
4687
4688 Returns a representation of the expression. */
4689
4690 static tree
4691 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4692 {
4693 /* If it's a `(', then we might be looking at a cast. */
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4695 {
4696 tree type = NULL_TREE;
4697 tree expr = NULL_TREE;
4698 bool compound_literal_p;
4699 const char *saved_message;
4700
4701 /* There's no way to know yet whether or not this is a cast.
4702 For example, `(int (3))' is a unary-expression, while `(int)
4703 3' is a cast. So, we resort to parsing tentatively. */
4704 cp_parser_parse_tentatively (parser);
4705 /* Types may not be defined in a cast. */
4706 saved_message = parser->type_definition_forbidden_message;
4707 parser->type_definition_forbidden_message
4708 = "types may not be defined in casts";
4709 /* Consume the `('. */
4710 cp_lexer_consume_token (parser->lexer);
4711 /* A very tricky bit is that `(struct S) { 3 }' is a
4712 compound-literal (which we permit in C++ as an extension).
4713 But, that construct is not a cast-expression -- it is a
4714 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4715 is legal; if the compound-literal were a cast-expression,
4716 you'd need an extra set of parentheses.) But, if we parse
4717 the type-id, and it happens to be a class-specifier, then we
4718 will commit to the parse at that point, because we cannot
4719 undo the action that is done when creating a new class. So,
4720 then we cannot back up and do a postfix-expression.
4721
4722 Therefore, we scan ahead to the closing `)', and check to see
4723 if the token after the `)' is a `{'. If so, we are not
4724 looking at a cast-expression.
4725
4726 Save tokens so that we can put them back. */
4727 cp_lexer_save_tokens (parser->lexer);
4728 /* Skip tokens until the next token is a closing parenthesis.
4729 If we find the closing `)', and the next token is a `{', then
4730 we are looking at a compound-literal. */
4731 compound_literal_p
4732 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4733 /*consume_paren=*/true)
4734 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4735 /* Roll back the tokens we skipped. */
4736 cp_lexer_rollback_tokens (parser->lexer);
4737 /* If we were looking at a compound-literal, simulate an error
4738 so that the call to cp_parser_parse_definitely below will
4739 fail. */
4740 if (compound_literal_p)
4741 cp_parser_simulate_error (parser);
4742 else
4743 {
4744 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4745 parser->in_type_id_in_expr_p = true;
4746 /* Look for the type-id. */
4747 type = cp_parser_type_id (parser);
4748 /* Look for the closing `)'. */
4749 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4750 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4751 }
4752
4753 /* Restore the saved message. */
4754 parser->type_definition_forbidden_message = saved_message;
4755
4756 /* If ok so far, parse the dependent expression. We cannot be
4757 sure it is a cast. Consider `(T ())'. It is a parenthesized
4758 ctor of T, but looks like a cast to function returning T
4759 without a dependent expression. */
4760 if (!cp_parser_error_occurred (parser))
4761 expr = cp_parser_simple_cast_expression (parser);
4762
4763 if (cp_parser_parse_definitely (parser))
4764 {
4765 /* Warn about old-style casts, if so requested. */
4766 if (warn_old_style_cast
4767 && !in_system_header
4768 && !VOID_TYPE_P (type)
4769 && current_lang_name != lang_name_c)
4770 warning ("use of old-style cast");
4771
4772 /* Only type conversions to integral or enumeration types
4773 can be used in constant-expressions. */
4774 if (parser->integral_constant_expression_p
4775 && !dependent_type_p (type)
4776 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4777 {
4778 if (!parser->allow_non_integral_constant_expression_p)
4779 return (cp_parser_non_integral_constant_expression
4780 ("a casts to a type other than an integral or "
4781 "enumeration type"));
4782 parser->non_integral_constant_expression_p = true;
4783 }
4784 /* Perform the cast. */
4785 expr = build_c_cast (type, expr);
4786 return expr;
4787 }
4788 }
4789
4790 /* If we get here, then it's not a cast, so it must be a
4791 unary-expression. */
4792 return cp_parser_unary_expression (parser, address_p);
4793 }
4794
4795 /* Parse a pm-expression.
4796
4797 pm-expression:
4798 cast-expression
4799 pm-expression .* cast-expression
4800 pm-expression ->* cast-expression
4801
4802 Returns a representation of the expression. */
4803
4804 static tree
4805 cp_parser_pm_expression (cp_parser* parser)
4806 {
4807 static const cp_parser_token_tree_map map = {
4808 { CPP_DEREF_STAR, MEMBER_REF },
4809 { CPP_DOT_STAR, DOTSTAR_EXPR },
4810 { CPP_EOF, ERROR_MARK }
4811 };
4812
4813 return cp_parser_binary_expression (parser, map,
4814 cp_parser_simple_cast_expression);
4815 }
4816
4817 /* Parse a multiplicative-expression.
4818
4819 mulitplicative-expression:
4820 pm-expression
4821 multiplicative-expression * pm-expression
4822 multiplicative-expression / pm-expression
4823 multiplicative-expression % pm-expression
4824
4825 Returns a representation of the expression. */
4826
4827 static tree
4828 cp_parser_multiplicative_expression (cp_parser* parser)
4829 {
4830 static const cp_parser_token_tree_map map = {
4831 { CPP_MULT, MULT_EXPR },
4832 { CPP_DIV, TRUNC_DIV_EXPR },
4833 { CPP_MOD, TRUNC_MOD_EXPR },
4834 { CPP_EOF, ERROR_MARK }
4835 };
4836
4837 return cp_parser_binary_expression (parser,
4838 map,
4839 cp_parser_pm_expression);
4840 }
4841
4842 /* Parse an additive-expression.
4843
4844 additive-expression:
4845 multiplicative-expression
4846 additive-expression + multiplicative-expression
4847 additive-expression - multiplicative-expression
4848
4849 Returns a representation of the expression. */
4850
4851 static tree
4852 cp_parser_additive_expression (cp_parser* parser)
4853 {
4854 static const cp_parser_token_tree_map map = {
4855 { CPP_PLUS, PLUS_EXPR },
4856 { CPP_MINUS, MINUS_EXPR },
4857 { CPP_EOF, ERROR_MARK }
4858 };
4859
4860 return cp_parser_binary_expression (parser,
4861 map,
4862 cp_parser_multiplicative_expression);
4863 }
4864
4865 /* Parse a shift-expression.
4866
4867 shift-expression:
4868 additive-expression
4869 shift-expression << additive-expression
4870 shift-expression >> additive-expression
4871
4872 Returns a representation of the expression. */
4873
4874 static tree
4875 cp_parser_shift_expression (cp_parser* parser)
4876 {
4877 static const cp_parser_token_tree_map map = {
4878 { CPP_LSHIFT, LSHIFT_EXPR },
4879 { CPP_RSHIFT, RSHIFT_EXPR },
4880 { CPP_EOF, ERROR_MARK }
4881 };
4882
4883 return cp_parser_binary_expression (parser,
4884 map,
4885 cp_parser_additive_expression);
4886 }
4887
4888 /* Parse a relational-expression.
4889
4890 relational-expression:
4891 shift-expression
4892 relational-expression < shift-expression
4893 relational-expression > shift-expression
4894 relational-expression <= shift-expression
4895 relational-expression >= shift-expression
4896
4897 GNU Extension:
4898
4899 relational-expression:
4900 relational-expression <? shift-expression
4901 relational-expression >? shift-expression
4902
4903 Returns a representation of the expression. */
4904
4905 static tree
4906 cp_parser_relational_expression (cp_parser* parser)
4907 {
4908 static const cp_parser_token_tree_map map = {
4909 { CPP_LESS, LT_EXPR },
4910 { CPP_GREATER, GT_EXPR },
4911 { CPP_LESS_EQ, LE_EXPR },
4912 { CPP_GREATER_EQ, GE_EXPR },
4913 { CPP_MIN, MIN_EXPR },
4914 { CPP_MAX, MAX_EXPR },
4915 { CPP_EOF, ERROR_MARK }
4916 };
4917
4918 return cp_parser_binary_expression (parser,
4919 map,
4920 cp_parser_shift_expression);
4921 }
4922
4923 /* Parse an equality-expression.
4924
4925 equality-expression:
4926 relational-expression
4927 equality-expression == relational-expression
4928 equality-expression != relational-expression
4929
4930 Returns a representation of the expression. */
4931
4932 static tree
4933 cp_parser_equality_expression (cp_parser* parser)
4934 {
4935 static const cp_parser_token_tree_map map = {
4936 { CPP_EQ_EQ, EQ_EXPR },
4937 { CPP_NOT_EQ, NE_EXPR },
4938 { CPP_EOF, ERROR_MARK }
4939 };
4940
4941 return cp_parser_binary_expression (parser,
4942 map,
4943 cp_parser_relational_expression);
4944 }
4945
4946 /* Parse an and-expression.
4947
4948 and-expression:
4949 equality-expression
4950 and-expression & equality-expression
4951
4952 Returns a representation of the expression. */
4953
4954 static tree
4955 cp_parser_and_expression (cp_parser* parser)
4956 {
4957 static const cp_parser_token_tree_map map = {
4958 { CPP_AND, BIT_AND_EXPR },
4959 { CPP_EOF, ERROR_MARK }
4960 };
4961
4962 return cp_parser_binary_expression (parser,
4963 map,
4964 cp_parser_equality_expression);
4965 }
4966
4967 /* Parse an exclusive-or-expression.
4968
4969 exclusive-or-expression:
4970 and-expression
4971 exclusive-or-expression ^ and-expression
4972
4973 Returns a representation of the expression. */
4974
4975 static tree
4976 cp_parser_exclusive_or_expression (cp_parser* parser)
4977 {
4978 static const cp_parser_token_tree_map map = {
4979 { CPP_XOR, BIT_XOR_EXPR },
4980 { CPP_EOF, ERROR_MARK }
4981 };
4982
4983 return cp_parser_binary_expression (parser,
4984 map,
4985 cp_parser_and_expression);
4986 }
4987
4988
4989 /* Parse an inclusive-or-expression.
4990
4991 inclusive-or-expression:
4992 exclusive-or-expression
4993 inclusive-or-expression | exclusive-or-expression
4994
4995 Returns a representation of the expression. */
4996
4997 static tree
4998 cp_parser_inclusive_or_expression (cp_parser* parser)
4999 {
5000 static const cp_parser_token_tree_map map = {
5001 { CPP_OR, BIT_IOR_EXPR },
5002 { CPP_EOF, ERROR_MARK }
5003 };
5004
5005 return cp_parser_binary_expression (parser,
5006 map,
5007 cp_parser_exclusive_or_expression);
5008 }
5009
5010 /* Parse a logical-and-expression.
5011
5012 logical-and-expression:
5013 inclusive-or-expression
5014 logical-and-expression && inclusive-or-expression
5015
5016 Returns a representation of the expression. */
5017
5018 static tree
5019 cp_parser_logical_and_expression (cp_parser* parser)
5020 {
5021 static const cp_parser_token_tree_map map = {
5022 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5023 { CPP_EOF, ERROR_MARK }
5024 };
5025
5026 return cp_parser_binary_expression (parser,
5027 map,
5028 cp_parser_inclusive_or_expression);
5029 }
5030
5031 /* Parse a logical-or-expression.
5032
5033 logical-or-expression:
5034 logical-and-expression
5035 logical-or-expression || logical-and-expression
5036
5037 Returns a representation of the expression. */
5038
5039 static tree
5040 cp_parser_logical_or_expression (cp_parser* parser)
5041 {
5042 static const cp_parser_token_tree_map map = {
5043 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5044 { CPP_EOF, ERROR_MARK }
5045 };
5046
5047 return cp_parser_binary_expression (parser,
5048 map,
5049 cp_parser_logical_and_expression);
5050 }
5051
5052 /* Parse the `? expression : assignment-expression' part of a
5053 conditional-expression. The LOGICAL_OR_EXPR is the
5054 logical-or-expression that started the conditional-expression.
5055 Returns a representation of the entire conditional-expression.
5056
5057 This routine is used by cp_parser_assignment_expression.
5058
5059 ? expression : assignment-expression
5060
5061 GNU Extensions:
5062
5063 ? : assignment-expression */
5064
5065 static tree
5066 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5067 {
5068 tree expr;
5069 tree assignment_expr;
5070
5071 /* Consume the `?' token. */
5072 cp_lexer_consume_token (parser->lexer);
5073 if (cp_parser_allow_gnu_extensions_p (parser)
5074 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5075 /* Implicit true clause. */
5076 expr = NULL_TREE;
5077 else
5078 /* Parse the expression. */
5079 expr = cp_parser_expression (parser);
5080
5081 /* The next token should be a `:'. */
5082 cp_parser_require (parser, CPP_COLON, "`:'");
5083 /* Parse the assignment-expression. */
5084 assignment_expr = cp_parser_assignment_expression (parser);
5085
5086 /* Build the conditional-expression. */
5087 return build_x_conditional_expr (logical_or_expr,
5088 expr,
5089 assignment_expr);
5090 }
5091
5092 /* Parse an assignment-expression.
5093
5094 assignment-expression:
5095 conditional-expression
5096 logical-or-expression assignment-operator assignment_expression
5097 throw-expression
5098
5099 Returns a representation for the expression. */
5100
5101 static tree
5102 cp_parser_assignment_expression (cp_parser* parser)
5103 {
5104 tree expr;
5105
5106 /* If the next token is the `throw' keyword, then we're looking at
5107 a throw-expression. */
5108 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5109 expr = cp_parser_throw_expression (parser);
5110 /* Otherwise, it must be that we are looking at a
5111 logical-or-expression. */
5112 else
5113 {
5114 /* Parse the logical-or-expression. */
5115 expr = cp_parser_logical_or_expression (parser);
5116 /* If the next token is a `?' then we're actually looking at a
5117 conditional-expression. */
5118 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5119 return cp_parser_question_colon_clause (parser, expr);
5120 else
5121 {
5122 enum tree_code assignment_operator;
5123
5124 /* If it's an assignment-operator, we're using the second
5125 production. */
5126 assignment_operator
5127 = cp_parser_assignment_operator_opt (parser);
5128 if (assignment_operator != ERROR_MARK)
5129 {
5130 tree rhs;
5131
5132 /* Parse the right-hand side of the assignment. */
5133 rhs = cp_parser_assignment_expression (parser);
5134 /* An assignment may not appear in a
5135 constant-expression. */
5136 if (parser->integral_constant_expression_p)
5137 {
5138 if (!parser->allow_non_integral_constant_expression_p)
5139 return cp_parser_non_integral_constant_expression ("an assignment");
5140 parser->non_integral_constant_expression_p = true;
5141 }
5142 /* Build the assignment expression. */
5143 expr = build_x_modify_expr (expr,
5144 assignment_operator,
5145 rhs);
5146 }
5147 }
5148 }
5149
5150 return expr;
5151 }
5152
5153 /* Parse an (optional) assignment-operator.
5154
5155 assignment-operator: one of
5156 = *= /= %= += -= >>= <<= &= ^= |=
5157
5158 GNU Extension:
5159
5160 assignment-operator: one of
5161 <?= >?=
5162
5163 If the next token is an assignment operator, the corresponding tree
5164 code is returned, and the token is consumed. For example, for
5165 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5166 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5167 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5168 operator, ERROR_MARK is returned. */
5169
5170 static enum tree_code
5171 cp_parser_assignment_operator_opt (cp_parser* parser)
5172 {
5173 enum tree_code op;
5174 cp_token *token;
5175
5176 /* Peek at the next toen. */
5177 token = cp_lexer_peek_token (parser->lexer);
5178
5179 switch (token->type)
5180 {
5181 case CPP_EQ:
5182 op = NOP_EXPR;
5183 break;
5184
5185 case CPP_MULT_EQ:
5186 op = MULT_EXPR;
5187 break;
5188
5189 case CPP_DIV_EQ:
5190 op = TRUNC_DIV_EXPR;
5191 break;
5192
5193 case CPP_MOD_EQ:
5194 op = TRUNC_MOD_EXPR;
5195 break;
5196
5197 case CPP_PLUS_EQ:
5198 op = PLUS_EXPR;
5199 break;
5200
5201 case CPP_MINUS_EQ:
5202 op = MINUS_EXPR;
5203 break;
5204
5205 case CPP_RSHIFT_EQ:
5206 op = RSHIFT_EXPR;
5207 break;
5208
5209 case CPP_LSHIFT_EQ:
5210 op = LSHIFT_EXPR;
5211 break;
5212
5213 case CPP_AND_EQ:
5214 op = BIT_AND_EXPR;
5215 break;
5216
5217 case CPP_XOR_EQ:
5218 op = BIT_XOR_EXPR;
5219 break;
5220
5221 case CPP_OR_EQ:
5222 op = BIT_IOR_EXPR;
5223 break;
5224
5225 case CPP_MIN_EQ:
5226 op = MIN_EXPR;
5227 break;
5228
5229 case CPP_MAX_EQ:
5230 op = MAX_EXPR;
5231 break;
5232
5233 default:
5234 /* Nothing else is an assignment operator. */
5235 op = ERROR_MARK;
5236 }
5237
5238 /* If it was an assignment operator, consume it. */
5239 if (op != ERROR_MARK)
5240 cp_lexer_consume_token (parser->lexer);
5241
5242 return op;
5243 }
5244
5245 /* Parse an expression.
5246
5247 expression:
5248 assignment-expression
5249 expression , assignment-expression
5250
5251 Returns a representation of the expression. */
5252
5253 static tree
5254 cp_parser_expression (cp_parser* parser)
5255 {
5256 tree expression = NULL_TREE;
5257
5258 while (true)
5259 {
5260 tree assignment_expression;
5261
5262 /* Parse the next assignment-expression. */
5263 assignment_expression
5264 = cp_parser_assignment_expression (parser);
5265 /* If this is the first assignment-expression, we can just
5266 save it away. */
5267 if (!expression)
5268 expression = assignment_expression;
5269 else
5270 expression = build_x_compound_expr (expression,
5271 assignment_expression);
5272 /* If the next token is not a comma, then we are done with the
5273 expression. */
5274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5275 break;
5276 /* Consume the `,'. */
5277 cp_lexer_consume_token (parser->lexer);
5278 /* A comma operator cannot appear in a constant-expression. */
5279 if (parser->integral_constant_expression_p)
5280 {
5281 if (!parser->allow_non_integral_constant_expression_p)
5282 expression
5283 = cp_parser_non_integral_constant_expression ("a comma operator");
5284 parser->non_integral_constant_expression_p = true;
5285 }
5286 }
5287
5288 return expression;
5289 }
5290
5291 /* Parse a constant-expression.
5292
5293 constant-expression:
5294 conditional-expression
5295
5296 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5297 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5298 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5299 is false, NON_CONSTANT_P should be NULL. */
5300
5301 static tree
5302 cp_parser_constant_expression (cp_parser* parser,
5303 bool allow_non_constant_p,
5304 bool *non_constant_p)
5305 {
5306 bool saved_integral_constant_expression_p;
5307 bool saved_allow_non_integral_constant_expression_p;
5308 bool saved_non_integral_constant_expression_p;
5309 tree expression;
5310
5311 /* It might seem that we could simply parse the
5312 conditional-expression, and then check to see if it were
5313 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5314 one that the compiler can figure out is constant, possibly after
5315 doing some simplifications or optimizations. The standard has a
5316 precise definition of constant-expression, and we must honor
5317 that, even though it is somewhat more restrictive.
5318
5319 For example:
5320
5321 int i[(2, 3)];
5322
5323 is not a legal declaration, because `(2, 3)' is not a
5324 constant-expression. The `,' operator is forbidden in a
5325 constant-expression. However, GCC's constant-folding machinery
5326 will fold this operation to an INTEGER_CST for `3'. */
5327
5328 /* Save the old settings. */
5329 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5330 saved_allow_non_integral_constant_expression_p
5331 = parser->allow_non_integral_constant_expression_p;
5332 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5333 /* We are now parsing a constant-expression. */
5334 parser->integral_constant_expression_p = true;
5335 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5336 parser->non_integral_constant_expression_p = false;
5337 /* Although the grammar says "conditional-expression", we parse an
5338 "assignment-expression", which also permits "throw-expression"
5339 and the use of assignment operators. In the case that
5340 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5341 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5342 actually essential that we look for an assignment-expression.
5343 For example, cp_parser_initializer_clauses uses this function to
5344 determine whether a particular assignment-expression is in fact
5345 constant. */
5346 expression = cp_parser_assignment_expression (parser);
5347 /* Restore the old settings. */
5348 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5349 parser->allow_non_integral_constant_expression_p
5350 = saved_allow_non_integral_constant_expression_p;
5351 if (allow_non_constant_p)
5352 *non_constant_p = parser->non_integral_constant_expression_p;
5353 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5354
5355 return expression;
5356 }
5357
5358 /* Statements [gram.stmt.stmt] */
5359
5360 /* Parse a statement.
5361
5362 statement:
5363 labeled-statement
5364 expression-statement
5365 compound-statement
5366 selection-statement
5367 iteration-statement
5368 jump-statement
5369 declaration-statement
5370 try-block */
5371
5372 static void
5373 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5374 {
5375 tree statement;
5376 cp_token *token;
5377 int statement_line_number;
5378
5379 /* There is no statement yet. */
5380 statement = NULL_TREE;
5381 /* Peek at the next token. */
5382 token = cp_lexer_peek_token (parser->lexer);
5383 /* Remember the line number of the first token in the statement. */
5384 statement_line_number = token->location.line;
5385 /* If this is a keyword, then that will often determine what kind of
5386 statement we have. */
5387 if (token->type == CPP_KEYWORD)
5388 {
5389 enum rid keyword = token->keyword;
5390
5391 switch (keyword)
5392 {
5393 case RID_CASE:
5394 case RID_DEFAULT:
5395 statement = cp_parser_labeled_statement (parser,
5396 in_statement_expr_p);
5397 break;
5398
5399 case RID_IF:
5400 case RID_SWITCH:
5401 statement = cp_parser_selection_statement (parser);
5402 break;
5403
5404 case RID_WHILE:
5405 case RID_DO:
5406 case RID_FOR:
5407 statement = cp_parser_iteration_statement (parser);
5408 break;
5409
5410 case RID_BREAK:
5411 case RID_CONTINUE:
5412 case RID_RETURN:
5413 case RID_GOTO:
5414 statement = cp_parser_jump_statement (parser);
5415 break;
5416
5417 case RID_TRY:
5418 statement = cp_parser_try_block (parser);
5419 break;
5420
5421 default:
5422 /* It might be a keyword like `int' that can start a
5423 declaration-statement. */
5424 break;
5425 }
5426 }
5427 else if (token->type == CPP_NAME)
5428 {
5429 /* If the next token is a `:', then we are looking at a
5430 labeled-statement. */
5431 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5432 if (token->type == CPP_COLON)
5433 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5434 }
5435 /* Anything that starts with a `{' must be a compound-statement. */
5436 else if (token->type == CPP_OPEN_BRACE)
5437 statement = cp_parser_compound_statement (parser, false);
5438
5439 /* Everything else must be a declaration-statement or an
5440 expression-statement. Try for the declaration-statement
5441 first, unless we are looking at a `;', in which case we know that
5442 we have an expression-statement. */
5443 if (!statement)
5444 {
5445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5446 {
5447 cp_parser_parse_tentatively (parser);
5448 /* Try to parse the declaration-statement. */
5449 cp_parser_declaration_statement (parser);
5450 /* If that worked, we're done. */
5451 if (cp_parser_parse_definitely (parser))
5452 return;
5453 }
5454 /* Look for an expression-statement instead. */
5455 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5456 }
5457
5458 /* Set the line number for the statement. */
5459 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5460 STMT_LINENO (statement) = statement_line_number;
5461 }
5462
5463 /* Parse a labeled-statement.
5464
5465 labeled-statement:
5466 identifier : statement
5467 case constant-expression : statement
5468 default : statement
5469
5470 Returns the new CASE_LABEL, for a `case' or `default' label. For
5471 an ordinary label, returns a LABEL_STMT. */
5472
5473 static tree
5474 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5475 {
5476 cp_token *token;
5477 tree statement = error_mark_node;
5478
5479 /* The next token should be an identifier. */
5480 token = cp_lexer_peek_token (parser->lexer);
5481 if (token->type != CPP_NAME
5482 && token->type != CPP_KEYWORD)
5483 {
5484 cp_parser_error (parser, "expected labeled-statement");
5485 return error_mark_node;
5486 }
5487
5488 switch (token->keyword)
5489 {
5490 case RID_CASE:
5491 {
5492 tree expr;
5493
5494 /* Consume the `case' token. */
5495 cp_lexer_consume_token (parser->lexer);
5496 /* Parse the constant-expression. */
5497 expr = cp_parser_constant_expression (parser,
5498 /*allow_non_constant_p=*/false,
5499 NULL);
5500 if (!parser->in_switch_statement_p)
5501 error ("case label `%E' not within a switch statement", expr);
5502 else
5503 statement = finish_case_label (expr, NULL_TREE);
5504 }
5505 break;
5506
5507 case RID_DEFAULT:
5508 /* Consume the `default' token. */
5509 cp_lexer_consume_token (parser->lexer);
5510 if (!parser->in_switch_statement_p)
5511 error ("case label not within a switch statement");
5512 else
5513 statement = finish_case_label (NULL_TREE, NULL_TREE);
5514 break;
5515
5516 default:
5517 /* Anything else must be an ordinary label. */
5518 statement = finish_label_stmt (cp_parser_identifier (parser));
5519 break;
5520 }
5521
5522 /* Require the `:' token. */
5523 cp_parser_require (parser, CPP_COLON, "`:'");
5524 /* Parse the labeled statement. */
5525 cp_parser_statement (parser, in_statement_expr_p);
5526
5527 /* Return the label, in the case of a `case' or `default' label. */
5528 return statement;
5529 }
5530
5531 /* Parse an expression-statement.
5532
5533 expression-statement:
5534 expression [opt] ;
5535
5536 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5537 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5538 indicates whether this expression-statement is part of an
5539 expression statement. */
5540
5541 static tree
5542 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5543 {
5544 tree statement = NULL_TREE;
5545
5546 /* If the next token is a ';', then there is no expression
5547 statement. */
5548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5549 statement = cp_parser_expression (parser);
5550
5551 /* Consume the final `;'. */
5552 cp_parser_consume_semicolon_at_end_of_statement (parser);
5553
5554 if (in_statement_expr_p
5555 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5556 {
5557 /* This is the final expression statement of a statement
5558 expression. */
5559 statement = finish_stmt_expr_expr (statement);
5560 }
5561 else if (statement)
5562 statement = finish_expr_stmt (statement);
5563 else
5564 finish_stmt ();
5565
5566 return statement;
5567 }
5568
5569 /* Parse a compound-statement.
5570
5571 compound-statement:
5572 { statement-seq [opt] }
5573
5574 Returns a COMPOUND_STMT representing the statement. */
5575
5576 static tree
5577 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5578 {
5579 tree compound_stmt;
5580
5581 /* Consume the `{'. */
5582 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5583 return error_mark_node;
5584 /* Begin the compound-statement. */
5585 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5586 /* Parse an (optional) statement-seq. */
5587 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5588 /* Finish the compound-statement. */
5589 finish_compound_stmt (compound_stmt);
5590 /* Consume the `}'. */
5591 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5592
5593 return compound_stmt;
5594 }
5595
5596 /* Parse an (optional) statement-seq.
5597
5598 statement-seq:
5599 statement
5600 statement-seq [opt] statement */
5601
5602 static void
5603 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5604 {
5605 /* Scan statements until there aren't any more. */
5606 while (true)
5607 {
5608 /* If we're looking at a `}', then we've run out of statements. */
5609 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5610 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5611 break;
5612
5613 /* Parse the statement. */
5614 cp_parser_statement (parser, in_statement_expr_p);
5615 }
5616 }
5617
5618 /* Parse a selection-statement.
5619
5620 selection-statement:
5621 if ( condition ) statement
5622 if ( condition ) statement else statement
5623 switch ( condition ) statement
5624
5625 Returns the new IF_STMT or SWITCH_STMT. */
5626
5627 static tree
5628 cp_parser_selection_statement (cp_parser* parser)
5629 {
5630 cp_token *token;
5631 enum rid keyword;
5632
5633 /* Peek at the next token. */
5634 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5635
5636 /* See what kind of keyword it is. */
5637 keyword = token->keyword;
5638 switch (keyword)
5639 {
5640 case RID_IF:
5641 case RID_SWITCH:
5642 {
5643 tree statement;
5644 tree condition;
5645
5646 /* Look for the `('. */
5647 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5648 {
5649 cp_parser_skip_to_end_of_statement (parser);
5650 return error_mark_node;
5651 }
5652
5653 /* Begin the selection-statement. */
5654 if (keyword == RID_IF)
5655 statement = begin_if_stmt ();
5656 else
5657 statement = begin_switch_stmt ();
5658
5659 /* Parse the condition. */
5660 condition = cp_parser_condition (parser);
5661 /* Look for the `)'. */
5662 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5663 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5664 /*consume_paren=*/true);
5665
5666 if (keyword == RID_IF)
5667 {
5668 tree then_stmt;
5669
5670 /* Add the condition. */
5671 finish_if_stmt_cond (condition, statement);
5672
5673 /* Parse the then-clause. */
5674 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5675 finish_then_clause (statement);
5676
5677 /* If the next token is `else', parse the else-clause. */
5678 if (cp_lexer_next_token_is_keyword (parser->lexer,
5679 RID_ELSE))
5680 {
5681 tree else_stmt;
5682
5683 /* Consume the `else' keyword. */
5684 cp_lexer_consume_token (parser->lexer);
5685 /* Parse the else-clause. */
5686 else_stmt
5687 = cp_parser_implicitly_scoped_statement (parser);
5688 finish_else_clause (statement);
5689 }
5690
5691 /* Now we're all done with the if-statement. */
5692 finish_if_stmt ();
5693 }
5694 else
5695 {
5696 tree body;
5697 bool in_switch_statement_p;
5698
5699 /* Add the condition. */
5700 finish_switch_cond (condition, statement);
5701
5702 /* Parse the body of the switch-statement. */
5703 in_switch_statement_p = parser->in_switch_statement_p;
5704 parser->in_switch_statement_p = true;
5705 body = cp_parser_implicitly_scoped_statement (parser);
5706 parser->in_switch_statement_p = in_switch_statement_p;
5707
5708 /* Now we're all done with the switch-statement. */
5709 finish_switch_stmt (statement);
5710 }
5711
5712 return statement;
5713 }
5714 break;
5715
5716 default:
5717 cp_parser_error (parser, "expected selection-statement");
5718 return error_mark_node;
5719 }
5720 }
5721
5722 /* Parse a condition.
5723
5724 condition:
5725 expression
5726 type-specifier-seq declarator = assignment-expression
5727
5728 GNU Extension:
5729
5730 condition:
5731 type-specifier-seq declarator asm-specification [opt]
5732 attributes [opt] = assignment-expression
5733
5734 Returns the expression that should be tested. */
5735
5736 static tree
5737 cp_parser_condition (cp_parser* parser)
5738 {
5739 tree type_specifiers;
5740 const char *saved_message;
5741
5742 /* Try the declaration first. */
5743 cp_parser_parse_tentatively (parser);
5744 /* New types are not allowed in the type-specifier-seq for a
5745 condition. */
5746 saved_message = parser->type_definition_forbidden_message;
5747 parser->type_definition_forbidden_message
5748 = "types may not be defined in conditions";
5749 /* Parse the type-specifier-seq. */
5750 type_specifiers = cp_parser_type_specifier_seq (parser);
5751 /* Restore the saved message. */
5752 parser->type_definition_forbidden_message = saved_message;
5753 /* If all is well, we might be looking at a declaration. */
5754 if (!cp_parser_error_occurred (parser))
5755 {
5756 tree decl;
5757 tree asm_specification;
5758 tree attributes;
5759 tree declarator;
5760 tree initializer = NULL_TREE;
5761
5762 /* Parse the declarator. */
5763 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5764 /*ctor_dtor_or_conv_p=*/NULL,
5765 /*parenthesized_p=*/NULL);
5766 /* Parse the attributes. */
5767 attributes = cp_parser_attributes_opt (parser);
5768 /* Parse the asm-specification. */
5769 asm_specification = cp_parser_asm_specification_opt (parser);
5770 /* If the next token is not an `=', then we might still be
5771 looking at an expression. For example:
5772
5773 if (A(a).x)
5774
5775 looks like a decl-specifier-seq and a declarator -- but then
5776 there is no `=', so this is an expression. */
5777 cp_parser_require (parser, CPP_EQ, "`='");
5778 /* If we did see an `=', then we are looking at a declaration
5779 for sure. */
5780 if (cp_parser_parse_definitely (parser))
5781 {
5782 /* Create the declaration. */
5783 decl = start_decl (declarator, type_specifiers,
5784 /*initialized_p=*/true,
5785 attributes, /*prefix_attributes=*/NULL_TREE);
5786 /* Parse the assignment-expression. */
5787 initializer = cp_parser_assignment_expression (parser);
5788
5789 /* Process the initializer. */
5790 cp_finish_decl (decl,
5791 initializer,
5792 asm_specification,
5793 LOOKUP_ONLYCONVERTING);
5794
5795 return convert_from_reference (decl);
5796 }
5797 }
5798 /* If we didn't even get past the declarator successfully, we are
5799 definitely not looking at a declaration. */
5800 else
5801 cp_parser_abort_tentative_parse (parser);
5802
5803 /* Otherwise, we are looking at an expression. */
5804 return cp_parser_expression (parser);
5805 }
5806
5807 /* Parse an iteration-statement.
5808
5809 iteration-statement:
5810 while ( condition ) statement
5811 do statement while ( expression ) ;
5812 for ( for-init-statement condition [opt] ; expression [opt] )
5813 statement
5814
5815 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5816
5817 static tree
5818 cp_parser_iteration_statement (cp_parser* parser)
5819 {
5820 cp_token *token;
5821 enum rid keyword;
5822 tree statement;
5823 bool in_iteration_statement_p;
5824
5825
5826 /* Peek at the next token. */
5827 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5828 if (!token)
5829 return error_mark_node;
5830
5831 /* Remember whether or not we are already within an iteration
5832 statement. */
5833 in_iteration_statement_p = parser->in_iteration_statement_p;
5834
5835 /* See what kind of keyword it is. */
5836 keyword = token->keyword;
5837 switch (keyword)
5838 {
5839 case RID_WHILE:
5840 {
5841 tree condition;
5842
5843 /* Begin the while-statement. */
5844 statement = begin_while_stmt ();
5845 /* Look for the `('. */
5846 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5847 /* Parse the condition. */
5848 condition = cp_parser_condition (parser);
5849 finish_while_stmt_cond (condition, statement);
5850 /* Look for the `)'. */
5851 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5852 /* Parse the dependent statement. */
5853 parser->in_iteration_statement_p = true;
5854 cp_parser_already_scoped_statement (parser);
5855 parser->in_iteration_statement_p = in_iteration_statement_p;
5856 /* We're done with the while-statement. */
5857 finish_while_stmt (statement);
5858 }
5859 break;
5860
5861 case RID_DO:
5862 {
5863 tree expression;
5864
5865 /* Begin the do-statement. */
5866 statement = begin_do_stmt ();
5867 /* Parse the body of the do-statement. */
5868 parser->in_iteration_statement_p = true;
5869 cp_parser_implicitly_scoped_statement (parser);
5870 parser->in_iteration_statement_p = in_iteration_statement_p;
5871 finish_do_body (statement);
5872 /* Look for the `while' keyword. */
5873 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5874 /* Look for the `('. */
5875 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5876 /* Parse the expression. */
5877 expression = cp_parser_expression (parser);
5878 /* We're done with the do-statement. */
5879 finish_do_stmt (expression, statement);
5880 /* Look for the `)'. */
5881 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5882 /* Look for the `;'. */
5883 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5884 }
5885 break;
5886
5887 case RID_FOR:
5888 {
5889 tree condition = NULL_TREE;
5890 tree expression = NULL_TREE;
5891
5892 /* Begin the for-statement. */
5893 statement = begin_for_stmt ();
5894 /* Look for the `('. */
5895 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5896 /* Parse the initialization. */
5897 cp_parser_for_init_statement (parser);
5898 finish_for_init_stmt (statement);
5899
5900 /* If there's a condition, process it. */
5901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5902 condition = cp_parser_condition (parser);
5903 finish_for_cond (condition, statement);
5904 /* Look for the `;'. */
5905 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5906
5907 /* If there's an expression, process it. */
5908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5909 expression = cp_parser_expression (parser);
5910 finish_for_expr (expression, statement);
5911 /* Look for the `)'. */
5912 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5913
5914 /* Parse the body of the for-statement. */
5915 parser->in_iteration_statement_p = true;
5916 cp_parser_already_scoped_statement (parser);
5917 parser->in_iteration_statement_p = in_iteration_statement_p;
5918
5919 /* We're done with the for-statement. */
5920 finish_for_stmt (statement);
5921 }
5922 break;
5923
5924 default:
5925 cp_parser_error (parser, "expected iteration-statement");
5926 statement = error_mark_node;
5927 break;
5928 }
5929
5930 return statement;
5931 }
5932
5933 /* Parse a for-init-statement.
5934
5935 for-init-statement:
5936 expression-statement
5937 simple-declaration */
5938
5939 static void
5940 cp_parser_for_init_statement (cp_parser* parser)
5941 {
5942 /* If the next token is a `;', then we have an empty
5943 expression-statement. Grammatically, this is also a
5944 simple-declaration, but an invalid one, because it does not
5945 declare anything. Therefore, if we did not handle this case
5946 specially, we would issue an error message about an invalid
5947 declaration. */
5948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5949 {
5950 /* We're going to speculatively look for a declaration, falling back
5951 to an expression, if necessary. */
5952 cp_parser_parse_tentatively (parser);
5953 /* Parse the declaration. */
5954 cp_parser_simple_declaration (parser,
5955 /*function_definition_allowed_p=*/false);
5956 /* If the tentative parse failed, then we shall need to look for an
5957 expression-statement. */
5958 if (cp_parser_parse_definitely (parser))
5959 return;
5960 }
5961
5962 cp_parser_expression_statement (parser, false);
5963 }
5964
5965 /* Parse a jump-statement.
5966
5967 jump-statement:
5968 break ;
5969 continue ;
5970 return expression [opt] ;
5971 goto identifier ;
5972
5973 GNU extension:
5974
5975 jump-statement:
5976 goto * expression ;
5977
5978 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5979 GOTO_STMT. */
5980
5981 static tree
5982 cp_parser_jump_statement (cp_parser* parser)
5983 {
5984 tree statement = error_mark_node;
5985 cp_token *token;
5986 enum rid keyword;
5987
5988 /* Peek at the next token. */
5989 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5990 if (!token)
5991 return error_mark_node;
5992
5993 /* See what kind of keyword it is. */
5994 keyword = token->keyword;
5995 switch (keyword)
5996 {
5997 case RID_BREAK:
5998 if (!parser->in_switch_statement_p
5999 && !parser->in_iteration_statement_p)
6000 {
6001 error ("break statement not within loop or switch");
6002 statement = error_mark_node;
6003 }
6004 else
6005 statement = finish_break_stmt ();
6006 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6007 break;
6008
6009 case RID_CONTINUE:
6010 if (!parser->in_iteration_statement_p)
6011 {
6012 error ("continue statement not within a loop");
6013 statement = error_mark_node;
6014 }
6015 else
6016 statement = finish_continue_stmt ();
6017 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6018 break;
6019
6020 case RID_RETURN:
6021 {
6022 tree expr;
6023
6024 /* If the next token is a `;', then there is no
6025 expression. */
6026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6027 expr = cp_parser_expression (parser);
6028 else
6029 expr = NULL_TREE;
6030 /* Build the return-statement. */
6031 statement = finish_return_stmt (expr);
6032 /* Look for the final `;'. */
6033 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6034 }
6035 break;
6036
6037 case RID_GOTO:
6038 /* Create the goto-statement. */
6039 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6040 {
6041 /* Issue a warning about this use of a GNU extension. */
6042 if (pedantic)
6043 pedwarn ("ISO C++ forbids computed gotos");
6044 /* Consume the '*' token. */
6045 cp_lexer_consume_token (parser->lexer);
6046 /* Parse the dependent expression. */
6047 finish_goto_stmt (cp_parser_expression (parser));
6048 }
6049 else
6050 finish_goto_stmt (cp_parser_identifier (parser));
6051 /* Look for the final `;'. */
6052 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6053 break;
6054
6055 default:
6056 cp_parser_error (parser, "expected jump-statement");
6057 break;
6058 }
6059
6060 return statement;
6061 }
6062
6063 /* Parse a declaration-statement.
6064
6065 declaration-statement:
6066 block-declaration */
6067
6068 static void
6069 cp_parser_declaration_statement (cp_parser* parser)
6070 {
6071 /* Parse the block-declaration. */
6072 cp_parser_block_declaration (parser, /*statement_p=*/true);
6073
6074 /* Finish off the statement. */
6075 finish_stmt ();
6076 }
6077
6078 /* Some dependent statements (like `if (cond) statement'), are
6079 implicitly in their own scope. In other words, if the statement is
6080 a single statement (as opposed to a compound-statement), it is
6081 none-the-less treated as if it were enclosed in braces. Any
6082 declarations appearing in the dependent statement are out of scope
6083 after control passes that point. This function parses a statement,
6084 but ensures that is in its own scope, even if it is not a
6085 compound-statement.
6086
6087 Returns the new statement. */
6088
6089 static tree
6090 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6091 {
6092 tree statement;
6093
6094 /* If the token is not a `{', then we must take special action. */
6095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6096 {
6097 /* Create a compound-statement. */
6098 statement = begin_compound_stmt (/*has_no_scope=*/false);
6099 /* Parse the dependent-statement. */
6100 cp_parser_statement (parser, false);
6101 /* Finish the dummy compound-statement. */
6102 finish_compound_stmt (statement);
6103 }
6104 /* Otherwise, we simply parse the statement directly. */
6105 else
6106 statement = cp_parser_compound_statement (parser, false);
6107
6108 /* Return the statement. */
6109 return statement;
6110 }
6111
6112 /* For some dependent statements (like `while (cond) statement'), we
6113 have already created a scope. Therefore, even if the dependent
6114 statement is a compound-statement, we do not want to create another
6115 scope. */
6116
6117 static void
6118 cp_parser_already_scoped_statement (cp_parser* parser)
6119 {
6120 /* If the token is not a `{', then we must take special action. */
6121 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6122 {
6123 tree statement;
6124
6125 /* Create a compound-statement. */
6126 statement = begin_compound_stmt (/*has_no_scope=*/true);
6127 /* Parse the dependent-statement. */
6128 cp_parser_statement (parser, false);
6129 /* Finish the dummy compound-statement. */
6130 finish_compound_stmt (statement);
6131 }
6132 /* Otherwise, we simply parse the statement directly. */
6133 else
6134 cp_parser_statement (parser, false);
6135 }
6136
6137 /* Declarations [gram.dcl.dcl] */
6138
6139 /* Parse an optional declaration-sequence.
6140
6141 declaration-seq:
6142 declaration
6143 declaration-seq declaration */
6144
6145 static void
6146 cp_parser_declaration_seq_opt (cp_parser* parser)
6147 {
6148 while (true)
6149 {
6150 cp_token *token;
6151
6152 token = cp_lexer_peek_token (parser->lexer);
6153
6154 if (token->type == CPP_CLOSE_BRACE
6155 || token->type == CPP_EOF)
6156 break;
6157
6158 if (token->type == CPP_SEMICOLON)
6159 {
6160 /* A declaration consisting of a single semicolon is
6161 invalid. Allow it unless we're being pedantic. */
6162 if (pedantic && !in_system_header)
6163 pedwarn ("extra `;'");
6164 cp_lexer_consume_token (parser->lexer);
6165 continue;
6166 }
6167
6168 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6169 parser to enter or exit implicit `extern "C"' blocks. */
6170 while (pending_lang_change > 0)
6171 {
6172 push_lang_context (lang_name_c);
6173 --pending_lang_change;
6174 }
6175 while (pending_lang_change < 0)
6176 {
6177 pop_lang_context ();
6178 ++pending_lang_change;
6179 }
6180
6181 /* Parse the declaration itself. */
6182 cp_parser_declaration (parser);
6183 }
6184 }
6185
6186 /* Parse a declaration.
6187
6188 declaration:
6189 block-declaration
6190 function-definition
6191 template-declaration
6192 explicit-instantiation
6193 explicit-specialization
6194 linkage-specification
6195 namespace-definition
6196
6197 GNU extension:
6198
6199 declaration:
6200 __extension__ declaration */
6201
6202 static void
6203 cp_parser_declaration (cp_parser* parser)
6204 {
6205 cp_token token1;
6206 cp_token token2;
6207 int saved_pedantic;
6208
6209 /* Check for the `__extension__' keyword. */
6210 if (cp_parser_extension_opt (parser, &saved_pedantic))
6211 {
6212 /* Parse the qualified declaration. */
6213 cp_parser_declaration (parser);
6214 /* Restore the PEDANTIC flag. */
6215 pedantic = saved_pedantic;
6216
6217 return;
6218 }
6219
6220 /* Try to figure out what kind of declaration is present. */
6221 token1 = *cp_lexer_peek_token (parser->lexer);
6222 if (token1.type != CPP_EOF)
6223 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6224
6225 /* If the next token is `extern' and the following token is a string
6226 literal, then we have a linkage specification. */
6227 if (token1.keyword == RID_EXTERN
6228 && cp_parser_is_string_literal (&token2))
6229 cp_parser_linkage_specification (parser);
6230 /* If the next token is `template', then we have either a template
6231 declaration, an explicit instantiation, or an explicit
6232 specialization. */
6233 else if (token1.keyword == RID_TEMPLATE)
6234 {
6235 /* `template <>' indicates a template specialization. */
6236 if (token2.type == CPP_LESS
6237 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6238 cp_parser_explicit_specialization (parser);
6239 /* `template <' indicates a template declaration. */
6240 else if (token2.type == CPP_LESS)
6241 cp_parser_template_declaration (parser, /*member_p=*/false);
6242 /* Anything else must be an explicit instantiation. */
6243 else
6244 cp_parser_explicit_instantiation (parser);
6245 }
6246 /* If the next token is `export', then we have a template
6247 declaration. */
6248 else if (token1.keyword == RID_EXPORT)
6249 cp_parser_template_declaration (parser, /*member_p=*/false);
6250 /* If the next token is `extern', 'static' or 'inline' and the one
6251 after that is `template', we have a GNU extended explicit
6252 instantiation directive. */
6253 else if (cp_parser_allow_gnu_extensions_p (parser)
6254 && (token1.keyword == RID_EXTERN
6255 || token1.keyword == RID_STATIC
6256 || token1.keyword == RID_INLINE)
6257 && token2.keyword == RID_TEMPLATE)
6258 cp_parser_explicit_instantiation (parser);
6259 /* If the next token is `namespace', check for a named or unnamed
6260 namespace definition. */
6261 else if (token1.keyword == RID_NAMESPACE
6262 && (/* A named namespace definition. */
6263 (token2.type == CPP_NAME
6264 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6265 == CPP_OPEN_BRACE))
6266 /* An unnamed namespace definition. */
6267 || token2.type == CPP_OPEN_BRACE))
6268 cp_parser_namespace_definition (parser);
6269 /* We must have either a block declaration or a function
6270 definition. */
6271 else
6272 /* Try to parse a block-declaration, or a function-definition. */
6273 cp_parser_block_declaration (parser, /*statement_p=*/false);
6274 }
6275
6276 /* Parse a block-declaration.
6277
6278 block-declaration:
6279 simple-declaration
6280 asm-definition
6281 namespace-alias-definition
6282 using-declaration
6283 using-directive
6284
6285 GNU Extension:
6286
6287 block-declaration:
6288 __extension__ block-declaration
6289 label-declaration
6290
6291 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6292 part of a declaration-statement. */
6293
6294 static void
6295 cp_parser_block_declaration (cp_parser *parser,
6296 bool statement_p)
6297 {
6298 cp_token *token1;
6299 int saved_pedantic;
6300
6301 /* Check for the `__extension__' keyword. */
6302 if (cp_parser_extension_opt (parser, &saved_pedantic))
6303 {
6304 /* Parse the qualified declaration. */
6305 cp_parser_block_declaration (parser, statement_p);
6306 /* Restore the PEDANTIC flag. */
6307 pedantic = saved_pedantic;
6308
6309 return;
6310 }
6311
6312 /* Peek at the next token to figure out which kind of declaration is
6313 present. */
6314 token1 = cp_lexer_peek_token (parser->lexer);
6315
6316 /* If the next keyword is `asm', we have an asm-definition. */
6317 if (token1->keyword == RID_ASM)
6318 {
6319 if (statement_p)
6320 cp_parser_commit_to_tentative_parse (parser);
6321 cp_parser_asm_definition (parser);
6322 }
6323 /* If the next keyword is `namespace', we have a
6324 namespace-alias-definition. */
6325 else if (token1->keyword == RID_NAMESPACE)
6326 cp_parser_namespace_alias_definition (parser);
6327 /* If the next keyword is `using', we have either a
6328 using-declaration or a using-directive. */
6329 else if (token1->keyword == RID_USING)
6330 {
6331 cp_token *token2;
6332
6333 if (statement_p)
6334 cp_parser_commit_to_tentative_parse (parser);
6335 /* If the token after `using' is `namespace', then we have a
6336 using-directive. */
6337 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6338 if (token2->keyword == RID_NAMESPACE)
6339 cp_parser_using_directive (parser);
6340 /* Otherwise, it's a using-declaration. */
6341 else
6342 cp_parser_using_declaration (parser);
6343 }
6344 /* If the next keyword is `__label__' we have a label declaration. */
6345 else if (token1->keyword == RID_LABEL)
6346 {
6347 if (statement_p)
6348 cp_parser_commit_to_tentative_parse (parser);
6349 cp_parser_label_declaration (parser);
6350 }
6351 /* Anything else must be a simple-declaration. */
6352 else
6353 cp_parser_simple_declaration (parser, !statement_p);
6354 }
6355
6356 /* Parse a simple-declaration.
6357
6358 simple-declaration:
6359 decl-specifier-seq [opt] init-declarator-list [opt] ;
6360
6361 init-declarator-list:
6362 init-declarator
6363 init-declarator-list , init-declarator
6364
6365 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6366 function-definition as a simple-declaration. */
6367
6368 static void
6369 cp_parser_simple_declaration (cp_parser* parser,
6370 bool function_definition_allowed_p)
6371 {
6372 tree decl_specifiers;
6373 tree attributes;
6374 int declares_class_or_enum;
6375 bool saw_declarator;
6376
6377 /* Defer access checks until we know what is being declared; the
6378 checks for names appearing in the decl-specifier-seq should be
6379 done as if we were in the scope of the thing being declared. */
6380 push_deferring_access_checks (dk_deferred);
6381
6382 /* Parse the decl-specifier-seq. We have to keep track of whether
6383 or not the decl-specifier-seq declares a named class or
6384 enumeration type, since that is the only case in which the
6385 init-declarator-list is allowed to be empty.
6386
6387 [dcl.dcl]
6388
6389 In a simple-declaration, the optional init-declarator-list can be
6390 omitted only when declaring a class or enumeration, that is when
6391 the decl-specifier-seq contains either a class-specifier, an
6392 elaborated-type-specifier, or an enum-specifier. */
6393 decl_specifiers
6394 = cp_parser_decl_specifier_seq (parser,
6395 CP_PARSER_FLAGS_OPTIONAL,
6396 &attributes,
6397 &declares_class_or_enum);
6398 /* We no longer need to defer access checks. */
6399 stop_deferring_access_checks ();
6400
6401 /* In a block scope, a valid declaration must always have a
6402 decl-specifier-seq. By not trying to parse declarators, we can
6403 resolve the declaration/expression ambiguity more quickly. */
6404 if (!function_definition_allowed_p && !decl_specifiers)
6405 {
6406 cp_parser_error (parser, "expected declaration");
6407 goto done;
6408 }
6409
6410 /* If the next two tokens are both identifiers, the code is
6411 erroneous. The usual cause of this situation is code like:
6412
6413 T t;
6414
6415 where "T" should name a type -- but does not. */
6416 if (cp_parser_diagnose_invalid_type_name (parser))
6417 {
6418 /* If parsing tentatively, we should commit; we really are
6419 looking at a declaration. */
6420 cp_parser_commit_to_tentative_parse (parser);
6421 /* Give up. */
6422 goto done;
6423 }
6424
6425 /* Keep going until we hit the `;' at the end of the simple
6426 declaration. */
6427 saw_declarator = false;
6428 while (cp_lexer_next_token_is_not (parser->lexer,
6429 CPP_SEMICOLON))
6430 {
6431 cp_token *token;
6432 bool function_definition_p;
6433 tree decl;
6434
6435 saw_declarator = true;
6436 /* Parse the init-declarator. */
6437 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6438 function_definition_allowed_p,
6439 /*member_p=*/false,
6440 declares_class_or_enum,
6441 &function_definition_p);
6442 /* If an error occurred while parsing tentatively, exit quickly.
6443 (That usually happens when in the body of a function; each
6444 statement is treated as a declaration-statement until proven
6445 otherwise.) */
6446 if (cp_parser_error_occurred (parser))
6447 goto done;
6448 /* Handle function definitions specially. */
6449 if (function_definition_p)
6450 {
6451 /* If the next token is a `,', then we are probably
6452 processing something like:
6453
6454 void f() {}, *p;
6455
6456 which is erroneous. */
6457 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6458 error ("mixing declarations and function-definitions is forbidden");
6459 /* Otherwise, we're done with the list of declarators. */
6460 else
6461 {
6462 pop_deferring_access_checks ();
6463 return;
6464 }
6465 }
6466 /* The next token should be either a `,' or a `;'. */
6467 token = cp_lexer_peek_token (parser->lexer);
6468 /* If it's a `,', there are more declarators to come. */
6469 if (token->type == CPP_COMMA)
6470 cp_lexer_consume_token (parser->lexer);
6471 /* If it's a `;', we are done. */
6472 else if (token->type == CPP_SEMICOLON)
6473 break;
6474 /* Anything else is an error. */
6475 else
6476 {
6477 cp_parser_error (parser, "expected `,' or `;'");
6478 /* Skip tokens until we reach the end of the statement. */
6479 cp_parser_skip_to_end_of_statement (parser);
6480 goto done;
6481 }
6482 /* After the first time around, a function-definition is not
6483 allowed -- even if it was OK at first. For example:
6484
6485 int i, f() {}
6486
6487 is not valid. */
6488 function_definition_allowed_p = false;
6489 }
6490
6491 /* Issue an error message if no declarators are present, and the
6492 decl-specifier-seq does not itself declare a class or
6493 enumeration. */
6494 if (!saw_declarator)
6495 {
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 ();
6500 }
6501
6502 /* Consume the `;'. */
6503 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6504
6505 done:
6506 pop_deferring_access_checks ();
6507 }
6508
6509 /* Parse a decl-specifier-seq.
6510
6511 decl-specifier-seq:
6512 decl-specifier-seq [opt] decl-specifier
6513
6514 decl-specifier:
6515 storage-class-specifier
6516 type-specifier
6517 function-specifier
6518 friend
6519 typedef
6520
6521 GNU Extension:
6522
6523 decl-specifier-seq:
6524 decl-specifier-seq [opt] attributes
6525
6526 Returns a TREE_LIST, giving the decl-specifiers in the order they
6527 appear in the source code. The TREE_VALUE of each node is the
6528 decl-specifier. For a keyword (such as `auto' or `friend'), the
6529 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6530 representation of a type-specifier, see cp_parser_type_specifier.
6531
6532 If there are attributes, they will be stored in *ATTRIBUTES,
6533 represented as described above cp_parser_attributes.
6534
6535 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6536 appears, and the entity that will be a friend is not going to be a
6537 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6538 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6539 friendship is granted might not be a class.
6540
6541 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6542 flags:
6543
6544 1: one of the decl-specifiers is an elaborated-type-specifier
6545 (i.e., a type declaration)
6546 2: one of the decl-specifiers is an enum-specifier or a
6547 class-specifier (i.e., a type definition)
6548
6549 */
6550
6551 static tree
6552 cp_parser_decl_specifier_seq (cp_parser* parser,
6553 cp_parser_flags flags,
6554 tree* attributes,
6555 int* declares_class_or_enum)
6556 {
6557 tree decl_specs = NULL_TREE;
6558 bool friend_p = false;
6559 bool constructor_possible_p = !parser->in_declarator_p;
6560
6561 /* Assume no class or enumeration type is declared. */
6562 *declares_class_or_enum = 0;
6563
6564 /* Assume there are no attributes. */
6565 *attributes = NULL_TREE;
6566
6567 /* Keep reading specifiers until there are no more to read. */
6568 while (true)
6569 {
6570 tree decl_spec = NULL_TREE;
6571 bool constructor_p;
6572 cp_token *token;
6573
6574 /* Peek at the next token. */
6575 token = cp_lexer_peek_token (parser->lexer);
6576 /* Handle attributes. */
6577 if (token->keyword == RID_ATTRIBUTE)
6578 {
6579 /* Parse the attributes. */
6580 decl_spec = cp_parser_attributes_opt (parser);
6581 /* Add them to the list. */
6582 *attributes = chainon (*attributes, decl_spec);
6583 continue;
6584 }
6585 /* If the next token is an appropriate keyword, we can simply
6586 add it to the list. */
6587 switch (token->keyword)
6588 {
6589 case RID_FRIEND:
6590 /* decl-specifier:
6591 friend */
6592 if (friend_p)
6593 error ("duplicate `friend'");
6594 else
6595 friend_p = true;
6596 /* The representation of the specifier is simply the
6597 appropriate TREE_IDENTIFIER node. */
6598 decl_spec = token->value;
6599 /* Consume the token. */
6600 cp_lexer_consume_token (parser->lexer);
6601 break;
6602
6603 /* function-specifier:
6604 inline
6605 virtual
6606 explicit */
6607 case RID_INLINE:
6608 case RID_VIRTUAL:
6609 case RID_EXPLICIT:
6610 decl_spec = cp_parser_function_specifier_opt (parser);
6611 break;
6612
6613 /* decl-specifier:
6614 typedef */
6615 case RID_TYPEDEF:
6616 /* The representation of the specifier is simply the
6617 appropriate TREE_IDENTIFIER node. */
6618 decl_spec = token->value;
6619 /* Consume the token. */
6620 cp_lexer_consume_token (parser->lexer);
6621 /* A constructor declarator cannot appear in a typedef. */
6622 constructor_possible_p = false;
6623 /* The "typedef" keyword can only occur in a declaration; we
6624 may as well commit at this point. */
6625 cp_parser_commit_to_tentative_parse (parser);
6626 break;
6627
6628 /* storage-class-specifier:
6629 auto
6630 register
6631 static
6632 extern
6633 mutable
6634
6635 GNU Extension:
6636 thread */
6637 case RID_AUTO:
6638 case RID_REGISTER:
6639 case RID_STATIC:
6640 case RID_EXTERN:
6641 case RID_MUTABLE:
6642 case RID_THREAD:
6643 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6644 break;
6645
6646 default:
6647 break;
6648 }
6649
6650 /* Constructors are a special case. The `S' in `S()' is not a
6651 decl-specifier; it is the beginning of the declarator. */
6652 constructor_p = (!decl_spec
6653 && constructor_possible_p
6654 && cp_parser_constructor_declarator_p (parser,
6655 friend_p));
6656
6657 /* If we don't have a DECL_SPEC yet, then we must be looking at
6658 a type-specifier. */
6659 if (!decl_spec && !constructor_p)
6660 {
6661 int decl_spec_declares_class_or_enum;
6662 bool is_cv_qualifier;
6663
6664 decl_spec
6665 = cp_parser_type_specifier (parser, flags,
6666 friend_p,
6667 /*is_declaration=*/true,
6668 &decl_spec_declares_class_or_enum,
6669 &is_cv_qualifier);
6670
6671 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6672
6673 /* If this type-specifier referenced a user-defined type
6674 (a typedef, class-name, etc.), then we can't allow any
6675 more such type-specifiers henceforth.
6676
6677 [dcl.spec]
6678
6679 The longest sequence of decl-specifiers that could
6680 possibly be a type name is taken as the
6681 decl-specifier-seq of a declaration. The sequence shall
6682 be self-consistent as described below.
6683
6684 [dcl.type]
6685
6686 As a general rule, at most one type-specifier is allowed
6687 in the complete decl-specifier-seq of a declaration. The
6688 only exceptions are the following:
6689
6690 -- const or volatile can be combined with any other
6691 type-specifier.
6692
6693 -- signed or unsigned can be combined with char, long,
6694 short, or int.
6695
6696 -- ..
6697
6698 Example:
6699
6700 typedef char* Pc;
6701 void g (const int Pc);
6702
6703 Here, Pc is *not* part of the decl-specifier seq; it's
6704 the declarator. Therefore, once we see a type-specifier
6705 (other than a cv-qualifier), we forbid any additional
6706 user-defined types. We *do* still allow things like `int
6707 int' to be considered a decl-specifier-seq, and issue the
6708 error message later. */
6709 if (decl_spec && !is_cv_qualifier)
6710 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6711 /* A constructor declarator cannot follow a type-specifier. */
6712 if (decl_spec)
6713 constructor_possible_p = false;
6714 }
6715
6716 /* If we still do not have a DECL_SPEC, then there are no more
6717 decl-specifiers. */
6718 if (!decl_spec)
6719 {
6720 /* Issue an error message, unless the entire construct was
6721 optional. */
6722 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6723 {
6724 cp_parser_error (parser, "expected decl specifier");
6725 return error_mark_node;
6726 }
6727
6728 break;
6729 }
6730
6731 /* Add the DECL_SPEC to the list of specifiers. */
6732 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6733 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6734
6735 /* After we see one decl-specifier, further decl-specifiers are
6736 always optional. */
6737 flags |= CP_PARSER_FLAGS_OPTIONAL;
6738 }
6739
6740 /* Don't allow a friend specifier with a class definition. */
6741 if (friend_p && (*declares_class_or_enum & 2))
6742 error ("class definition may not be declared a friend");
6743
6744 /* We have built up the DECL_SPECS in reverse order. Return them in
6745 the correct order. */
6746 return nreverse (decl_specs);
6747 }
6748
6749 /* Parse an (optional) storage-class-specifier.
6750
6751 storage-class-specifier:
6752 auto
6753 register
6754 static
6755 extern
6756 mutable
6757
6758 GNU Extension:
6759
6760 storage-class-specifier:
6761 thread
6762
6763 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6764
6765 static tree
6766 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6767 {
6768 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6769 {
6770 case RID_AUTO:
6771 case RID_REGISTER:
6772 case RID_STATIC:
6773 case RID_EXTERN:
6774 case RID_MUTABLE:
6775 case RID_THREAD:
6776 /* Consume the token. */
6777 return cp_lexer_consume_token (parser->lexer)->value;
6778
6779 default:
6780 return NULL_TREE;
6781 }
6782 }
6783
6784 /* Parse an (optional) function-specifier.
6785
6786 function-specifier:
6787 inline
6788 virtual
6789 explicit
6790
6791 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6792
6793 static tree
6794 cp_parser_function_specifier_opt (cp_parser* parser)
6795 {
6796 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6797 {
6798 case RID_INLINE:
6799 case RID_VIRTUAL:
6800 case RID_EXPLICIT:
6801 /* Consume the token. */
6802 return cp_lexer_consume_token (parser->lexer)->value;
6803
6804 default:
6805 return NULL_TREE;
6806 }
6807 }
6808
6809 /* Parse a linkage-specification.
6810
6811 linkage-specification:
6812 extern string-literal { declaration-seq [opt] }
6813 extern string-literal declaration */
6814
6815 static void
6816 cp_parser_linkage_specification (cp_parser* parser)
6817 {
6818 cp_token *token;
6819 tree linkage;
6820
6821 /* Look for the `extern' keyword. */
6822 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6823
6824 /* Peek at the next token. */
6825 token = cp_lexer_peek_token (parser->lexer);
6826 /* If it's not a string-literal, then there's a problem. */
6827 if (!cp_parser_is_string_literal (token))
6828 {
6829 cp_parser_error (parser, "expected language-name");
6830 return;
6831 }
6832 /* Consume the token. */
6833 cp_lexer_consume_token (parser->lexer);
6834
6835 /* Transform the literal into an identifier. If the literal is a
6836 wide-character string, or contains embedded NULs, then we can't
6837 handle it as the user wants. */
6838 if (token->type == CPP_WSTRING
6839 || (strlen (TREE_STRING_POINTER (token->value))
6840 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6841 {
6842 cp_parser_error (parser, "invalid linkage-specification");
6843 /* Assume C++ linkage. */
6844 linkage = get_identifier ("c++");
6845 }
6846 /* If it's a simple string constant, things are easier. */
6847 else
6848 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6849
6850 /* We're now using the new linkage. */
6851 push_lang_context (linkage);
6852
6853 /* If the next token is a `{', then we're using the first
6854 production. */
6855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6856 {
6857 /* Consume the `{' token. */
6858 cp_lexer_consume_token (parser->lexer);
6859 /* Parse the declarations. */
6860 cp_parser_declaration_seq_opt (parser);
6861 /* Look for the closing `}'. */
6862 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6863 }
6864 /* Otherwise, there's just one declaration. */
6865 else
6866 {
6867 bool saved_in_unbraced_linkage_specification_p;
6868
6869 saved_in_unbraced_linkage_specification_p
6870 = parser->in_unbraced_linkage_specification_p;
6871 parser->in_unbraced_linkage_specification_p = true;
6872 have_extern_spec = true;
6873 cp_parser_declaration (parser);
6874 have_extern_spec = false;
6875 parser->in_unbraced_linkage_specification_p
6876 = saved_in_unbraced_linkage_specification_p;
6877 }
6878
6879 /* We're done with the linkage-specification. */
6880 pop_lang_context ();
6881 }
6882
6883 /* Special member functions [gram.special] */
6884
6885 /* Parse a conversion-function-id.
6886
6887 conversion-function-id:
6888 operator conversion-type-id
6889
6890 Returns an IDENTIFIER_NODE representing the operator. */
6891
6892 static tree
6893 cp_parser_conversion_function_id (cp_parser* parser)
6894 {
6895 tree type;
6896 tree saved_scope;
6897 tree saved_qualifying_scope;
6898 tree saved_object_scope;
6899
6900 /* Look for the `operator' token. */
6901 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6902 return error_mark_node;
6903 /* When we parse the conversion-type-id, the current scope will be
6904 reset. However, we need that information in able to look up the
6905 conversion function later, so we save it here. */
6906 saved_scope = parser->scope;
6907 saved_qualifying_scope = parser->qualifying_scope;
6908 saved_object_scope = parser->object_scope;
6909 /* We must enter the scope of the class so that the names of
6910 entities declared within the class are available in the
6911 conversion-type-id. For example, consider:
6912
6913 struct S {
6914 typedef int I;
6915 operator I();
6916 };
6917
6918 S::operator I() { ... }
6919
6920 In order to see that `I' is a type-name in the definition, we
6921 must be in the scope of `S'. */
6922 if (saved_scope)
6923 push_scope (saved_scope);
6924 /* Parse the conversion-type-id. */
6925 type = cp_parser_conversion_type_id (parser);
6926 /* Leave the scope of the class, if any. */
6927 if (saved_scope)
6928 pop_scope (saved_scope);
6929 /* Restore the saved scope. */
6930 parser->scope = saved_scope;
6931 parser->qualifying_scope = saved_qualifying_scope;
6932 parser->object_scope = saved_object_scope;
6933 /* If the TYPE is invalid, indicate failure. */
6934 if (type == error_mark_node)
6935 return error_mark_node;
6936 return mangle_conv_op_name_for_type (type);
6937 }
6938
6939 /* Parse a conversion-type-id:
6940
6941 conversion-type-id:
6942 type-specifier-seq conversion-declarator [opt]
6943
6944 Returns the TYPE specified. */
6945
6946 static tree
6947 cp_parser_conversion_type_id (cp_parser* parser)
6948 {
6949 tree attributes;
6950 tree type_specifiers;
6951 tree declarator;
6952
6953 /* Parse the attributes. */
6954 attributes = cp_parser_attributes_opt (parser);
6955 /* Parse the type-specifiers. */
6956 type_specifiers = cp_parser_type_specifier_seq (parser);
6957 /* If that didn't work, stop. */
6958 if (type_specifiers == error_mark_node)
6959 return error_mark_node;
6960 /* Parse the conversion-declarator. */
6961 declarator = cp_parser_conversion_declarator_opt (parser);
6962
6963 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6964 /*initialized=*/0, &attributes);
6965 }
6966
6967 /* Parse an (optional) conversion-declarator.
6968
6969 conversion-declarator:
6970 ptr-operator conversion-declarator [opt]
6971
6972 Returns a representation of the declarator. See
6973 cp_parser_declarator for details. */
6974
6975 static tree
6976 cp_parser_conversion_declarator_opt (cp_parser* parser)
6977 {
6978 enum tree_code code;
6979 tree class_type;
6980 tree cv_qualifier_seq;
6981
6982 /* We don't know if there's a ptr-operator next, or not. */
6983 cp_parser_parse_tentatively (parser);
6984 /* Try the ptr-operator. */
6985 code = cp_parser_ptr_operator (parser, &class_type,
6986 &cv_qualifier_seq);
6987 /* If it worked, look for more conversion-declarators. */
6988 if (cp_parser_parse_definitely (parser))
6989 {
6990 tree declarator;
6991
6992 /* Parse another optional declarator. */
6993 declarator = cp_parser_conversion_declarator_opt (parser);
6994
6995 /* Create the representation of the declarator. */
6996 if (code == INDIRECT_REF)
6997 declarator = make_pointer_declarator (cv_qualifier_seq,
6998 declarator);
6999 else
7000 declarator = make_reference_declarator (cv_qualifier_seq,
7001 declarator);
7002
7003 /* Handle the pointer-to-member case. */
7004 if (class_type)
7005 declarator = build_nt (SCOPE_REF, class_type, declarator);
7006
7007 return declarator;
7008 }
7009
7010 return NULL_TREE;
7011 }
7012
7013 /* Parse an (optional) ctor-initializer.
7014
7015 ctor-initializer:
7016 : mem-initializer-list
7017
7018 Returns TRUE iff the ctor-initializer was actually present. */
7019
7020 static bool
7021 cp_parser_ctor_initializer_opt (cp_parser* parser)
7022 {
7023 /* If the next token is not a `:', then there is no
7024 ctor-initializer. */
7025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7026 {
7027 /* Do default initialization of any bases and members. */
7028 if (DECL_CONSTRUCTOR_P (current_function_decl))
7029 finish_mem_initializers (NULL_TREE);
7030
7031 return false;
7032 }
7033
7034 /* Consume the `:' token. */
7035 cp_lexer_consume_token (parser->lexer);
7036 /* And the mem-initializer-list. */
7037 cp_parser_mem_initializer_list (parser);
7038
7039 return true;
7040 }
7041
7042 /* Parse a mem-initializer-list.
7043
7044 mem-initializer-list:
7045 mem-initializer
7046 mem-initializer , mem-initializer-list */
7047
7048 static void
7049 cp_parser_mem_initializer_list (cp_parser* parser)
7050 {
7051 tree mem_initializer_list = NULL_TREE;
7052
7053 /* Let the semantic analysis code know that we are starting the
7054 mem-initializer-list. */
7055 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7056 error ("only constructors take base initializers");
7057
7058 /* Loop through the list. */
7059 while (true)
7060 {
7061 tree mem_initializer;
7062
7063 /* Parse the mem-initializer. */
7064 mem_initializer = cp_parser_mem_initializer (parser);
7065 /* Add it to the list, unless it was erroneous. */
7066 if (mem_initializer)
7067 {
7068 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7069 mem_initializer_list = mem_initializer;
7070 }
7071 /* If the next token is not a `,', we're done. */
7072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7073 break;
7074 /* Consume the `,' token. */
7075 cp_lexer_consume_token (parser->lexer);
7076 }
7077
7078 /* Perform semantic analysis. */
7079 if (DECL_CONSTRUCTOR_P (current_function_decl))
7080 finish_mem_initializers (mem_initializer_list);
7081 }
7082
7083 /* Parse a mem-initializer.
7084
7085 mem-initializer:
7086 mem-initializer-id ( expression-list [opt] )
7087
7088 GNU extension:
7089
7090 mem-initializer:
7091 ( expression-list [opt] )
7092
7093 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7094 class) or FIELD_DECL (for a non-static data member) to initialize;
7095 the TREE_VALUE is the expression-list. */
7096
7097 static tree
7098 cp_parser_mem_initializer (cp_parser* parser)
7099 {
7100 tree mem_initializer_id;
7101 tree expression_list;
7102 tree member;
7103
7104 /* Find out what is being initialized. */
7105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7106 {
7107 pedwarn ("anachronistic old-style base class initializer");
7108 mem_initializer_id = NULL_TREE;
7109 }
7110 else
7111 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7112 member = expand_member_init (mem_initializer_id);
7113 if (member && !DECL_P (member))
7114 in_base_initializer = 1;
7115
7116 expression_list
7117 = cp_parser_parenthesized_expression_list (parser, false,
7118 /*non_constant_p=*/NULL);
7119 if (!expression_list)
7120 expression_list = void_type_node;
7121
7122 in_base_initializer = 0;
7123
7124 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7125 }
7126
7127 /* Parse a mem-initializer-id.
7128
7129 mem-initializer-id:
7130 :: [opt] nested-name-specifier [opt] class-name
7131 identifier
7132
7133 Returns a TYPE indicating the class to be initializer for the first
7134 production. Returns an IDENTIFIER_NODE indicating the data member
7135 to be initialized for the second production. */
7136
7137 static tree
7138 cp_parser_mem_initializer_id (cp_parser* parser)
7139 {
7140 bool global_scope_p;
7141 bool nested_name_specifier_p;
7142 tree id;
7143
7144 /* Look for the optional `::' operator. */
7145 global_scope_p
7146 = (cp_parser_global_scope_opt (parser,
7147 /*current_scope_valid_p=*/false)
7148 != NULL_TREE);
7149 /* Look for the optional nested-name-specifier. The simplest way to
7150 implement:
7151
7152 [temp.res]
7153
7154 The keyword `typename' is not permitted in a base-specifier or
7155 mem-initializer; in these contexts a qualified name that
7156 depends on a template-parameter is implicitly assumed to be a
7157 type name.
7158
7159 is to assume that we have seen the `typename' keyword at this
7160 point. */
7161 nested_name_specifier_p
7162 = (cp_parser_nested_name_specifier_opt (parser,
7163 /*typename_keyword_p=*/true,
7164 /*check_dependency_p=*/true,
7165 /*type_p=*/true,
7166 /*is_declaration=*/true)
7167 != NULL_TREE);
7168 /* If there is a `::' operator or a nested-name-specifier, then we
7169 are definitely looking for a class-name. */
7170 if (global_scope_p || nested_name_specifier_p)
7171 return cp_parser_class_name (parser,
7172 /*typename_keyword_p=*/true,
7173 /*template_keyword_p=*/false,
7174 /*type_p=*/false,
7175 /*check_dependency_p=*/true,
7176 /*class_head_p=*/false,
7177 /*is_declaration=*/true);
7178 /* Otherwise, we could also be looking for an ordinary identifier. */
7179 cp_parser_parse_tentatively (parser);
7180 /* Try a class-name. */
7181 id = cp_parser_class_name (parser,
7182 /*typename_keyword_p=*/true,
7183 /*template_keyword_p=*/false,
7184 /*type_p=*/false,
7185 /*check_dependency_p=*/true,
7186 /*class_head_p=*/false,
7187 /*is_declaration=*/true);
7188 /* If we found one, we're done. */
7189 if (cp_parser_parse_definitely (parser))
7190 return id;
7191 /* Otherwise, look for an ordinary identifier. */
7192 return cp_parser_identifier (parser);
7193 }
7194
7195 /* Overloading [gram.over] */
7196
7197 /* Parse an operator-function-id.
7198
7199 operator-function-id:
7200 operator operator
7201
7202 Returns an IDENTIFIER_NODE for the operator which is a
7203 human-readable spelling of the identifier, e.g., `operator +'. */
7204
7205 static tree
7206 cp_parser_operator_function_id (cp_parser* parser)
7207 {
7208 /* Look for the `operator' keyword. */
7209 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7210 return error_mark_node;
7211 /* And then the name of the operator itself. */
7212 return cp_parser_operator (parser);
7213 }
7214
7215 /* Parse an operator.
7216
7217 operator:
7218 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7219 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7220 || ++ -- , ->* -> () []
7221
7222 GNU Extensions:
7223
7224 operator:
7225 <? >? <?= >?=
7226
7227 Returns an IDENTIFIER_NODE for the operator which is a
7228 human-readable spelling of the identifier, e.g., `operator +'. */
7229
7230 static tree
7231 cp_parser_operator (cp_parser* parser)
7232 {
7233 tree id = NULL_TREE;
7234 cp_token *token;
7235
7236 /* Peek at the next token. */
7237 token = cp_lexer_peek_token (parser->lexer);
7238 /* Figure out which operator we have. */
7239 switch (token->type)
7240 {
7241 case CPP_KEYWORD:
7242 {
7243 enum tree_code op;
7244
7245 /* The keyword should be either `new' or `delete'. */
7246 if (token->keyword == RID_NEW)
7247 op = NEW_EXPR;
7248 else if (token->keyword == RID_DELETE)
7249 op = DELETE_EXPR;
7250 else
7251 break;
7252
7253 /* Consume the `new' or `delete' token. */
7254 cp_lexer_consume_token (parser->lexer);
7255
7256 /* Peek at the next token. */
7257 token = cp_lexer_peek_token (parser->lexer);
7258 /* If it's a `[' token then this is the array variant of the
7259 operator. */
7260 if (token->type == CPP_OPEN_SQUARE)
7261 {
7262 /* Consume the `[' token. */
7263 cp_lexer_consume_token (parser->lexer);
7264 /* Look for the `]' token. */
7265 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7266 id = ansi_opname (op == NEW_EXPR
7267 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7268 }
7269 /* Otherwise, we have the non-array variant. */
7270 else
7271 id = ansi_opname (op);
7272
7273 return id;
7274 }
7275
7276 case CPP_PLUS:
7277 id = ansi_opname (PLUS_EXPR);
7278 break;
7279
7280 case CPP_MINUS:
7281 id = ansi_opname (MINUS_EXPR);
7282 break;
7283
7284 case CPP_MULT:
7285 id = ansi_opname (MULT_EXPR);
7286 break;
7287
7288 case CPP_DIV:
7289 id = ansi_opname (TRUNC_DIV_EXPR);
7290 break;
7291
7292 case CPP_MOD:
7293 id = ansi_opname (TRUNC_MOD_EXPR);
7294 break;
7295
7296 case CPP_XOR:
7297 id = ansi_opname (BIT_XOR_EXPR);
7298 break;
7299
7300 case CPP_AND:
7301 id = ansi_opname (BIT_AND_EXPR);
7302 break;
7303
7304 case CPP_OR:
7305 id = ansi_opname (BIT_IOR_EXPR);
7306 break;
7307
7308 case CPP_COMPL:
7309 id = ansi_opname (BIT_NOT_EXPR);
7310 break;
7311
7312 case CPP_NOT:
7313 id = ansi_opname (TRUTH_NOT_EXPR);
7314 break;
7315
7316 case CPP_EQ:
7317 id = ansi_assopname (NOP_EXPR);
7318 break;
7319
7320 case CPP_LESS:
7321 id = ansi_opname (LT_EXPR);
7322 break;
7323
7324 case CPP_GREATER:
7325 id = ansi_opname (GT_EXPR);
7326 break;
7327
7328 case CPP_PLUS_EQ:
7329 id = ansi_assopname (PLUS_EXPR);
7330 break;
7331
7332 case CPP_MINUS_EQ:
7333 id = ansi_assopname (MINUS_EXPR);
7334 break;
7335
7336 case CPP_MULT_EQ:
7337 id = ansi_assopname (MULT_EXPR);
7338 break;
7339
7340 case CPP_DIV_EQ:
7341 id = ansi_assopname (TRUNC_DIV_EXPR);
7342 break;
7343
7344 case CPP_MOD_EQ:
7345 id = ansi_assopname (TRUNC_MOD_EXPR);
7346 break;
7347
7348 case CPP_XOR_EQ:
7349 id = ansi_assopname (BIT_XOR_EXPR);
7350 break;
7351
7352 case CPP_AND_EQ:
7353 id = ansi_assopname (BIT_AND_EXPR);
7354 break;
7355
7356 case CPP_OR_EQ:
7357 id = ansi_assopname (BIT_IOR_EXPR);
7358 break;
7359
7360 case CPP_LSHIFT:
7361 id = ansi_opname (LSHIFT_EXPR);
7362 break;
7363
7364 case CPP_RSHIFT:
7365 id = ansi_opname (RSHIFT_EXPR);
7366 break;
7367
7368 case CPP_LSHIFT_EQ:
7369 id = ansi_assopname (LSHIFT_EXPR);
7370 break;
7371
7372 case CPP_RSHIFT_EQ:
7373 id = ansi_assopname (RSHIFT_EXPR);
7374 break;
7375
7376 case CPP_EQ_EQ:
7377 id = ansi_opname (EQ_EXPR);
7378 break;
7379
7380 case CPP_NOT_EQ:
7381 id = ansi_opname (NE_EXPR);
7382 break;
7383
7384 case CPP_LESS_EQ:
7385 id = ansi_opname (LE_EXPR);
7386 break;
7387
7388 case CPP_GREATER_EQ:
7389 id = ansi_opname (GE_EXPR);
7390 break;
7391
7392 case CPP_AND_AND:
7393 id = ansi_opname (TRUTH_ANDIF_EXPR);
7394 break;
7395
7396 case CPP_OR_OR:
7397 id = ansi_opname (TRUTH_ORIF_EXPR);
7398 break;
7399
7400 case CPP_PLUS_PLUS:
7401 id = ansi_opname (POSTINCREMENT_EXPR);
7402 break;
7403
7404 case CPP_MINUS_MINUS:
7405 id = ansi_opname (PREDECREMENT_EXPR);
7406 break;
7407
7408 case CPP_COMMA:
7409 id = ansi_opname (COMPOUND_EXPR);
7410 break;
7411
7412 case CPP_DEREF_STAR:
7413 id = ansi_opname (MEMBER_REF);
7414 break;
7415
7416 case CPP_DEREF:
7417 id = ansi_opname (COMPONENT_REF);
7418 break;
7419
7420 case CPP_OPEN_PAREN:
7421 /* Consume the `('. */
7422 cp_lexer_consume_token (parser->lexer);
7423 /* Look for the matching `)'. */
7424 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7425 return ansi_opname (CALL_EXPR);
7426
7427 case CPP_OPEN_SQUARE:
7428 /* Consume the `['. */
7429 cp_lexer_consume_token (parser->lexer);
7430 /* Look for the matching `]'. */
7431 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7432 return ansi_opname (ARRAY_REF);
7433
7434 /* Extensions. */
7435 case CPP_MIN:
7436 id = ansi_opname (MIN_EXPR);
7437 break;
7438
7439 case CPP_MAX:
7440 id = ansi_opname (MAX_EXPR);
7441 break;
7442
7443 case CPP_MIN_EQ:
7444 id = ansi_assopname (MIN_EXPR);
7445 break;
7446
7447 case CPP_MAX_EQ:
7448 id = ansi_assopname (MAX_EXPR);
7449 break;
7450
7451 default:
7452 /* Anything else is an error. */
7453 break;
7454 }
7455
7456 /* If we have selected an identifier, we need to consume the
7457 operator token. */
7458 if (id)
7459 cp_lexer_consume_token (parser->lexer);
7460 /* Otherwise, no valid operator name was present. */
7461 else
7462 {
7463 cp_parser_error (parser, "expected operator");
7464 id = error_mark_node;
7465 }
7466
7467 return id;
7468 }
7469
7470 /* Parse a template-declaration.
7471
7472 template-declaration:
7473 export [opt] template < template-parameter-list > declaration
7474
7475 If MEMBER_P is TRUE, this template-declaration occurs within a
7476 class-specifier.
7477
7478 The grammar rule given by the standard isn't correct. What
7479 is really meant is:
7480
7481 template-declaration:
7482 export [opt] template-parameter-list-seq
7483 decl-specifier-seq [opt] init-declarator [opt] ;
7484 export [opt] template-parameter-list-seq
7485 function-definition
7486
7487 template-parameter-list-seq:
7488 template-parameter-list-seq [opt]
7489 template < template-parameter-list > */
7490
7491 static void
7492 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7493 {
7494 /* Check for `export'. */
7495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7496 {
7497 /* Consume the `export' token. */
7498 cp_lexer_consume_token (parser->lexer);
7499 /* Warn that we do not support `export'. */
7500 warning ("keyword `export' not implemented, and will be ignored");
7501 }
7502
7503 cp_parser_template_declaration_after_export (parser, member_p);
7504 }
7505
7506 /* Parse a template-parameter-list.
7507
7508 template-parameter-list:
7509 template-parameter
7510 template-parameter-list , template-parameter
7511
7512 Returns a TREE_LIST. Each node represents a template parameter.
7513 The nodes are connected via their TREE_CHAINs. */
7514
7515 static tree
7516 cp_parser_template_parameter_list (cp_parser* parser)
7517 {
7518 tree parameter_list = NULL_TREE;
7519
7520 while (true)
7521 {
7522 tree parameter;
7523 cp_token *token;
7524
7525 /* Parse the template-parameter. */
7526 parameter = cp_parser_template_parameter (parser);
7527 /* Add it to the list. */
7528 parameter_list = process_template_parm (parameter_list,
7529 parameter);
7530
7531 /* Peek at the next token. */
7532 token = cp_lexer_peek_token (parser->lexer);
7533 /* If it's not a `,', we're done. */
7534 if (token->type != CPP_COMMA)
7535 break;
7536 /* Otherwise, consume the `,' token. */
7537 cp_lexer_consume_token (parser->lexer);
7538 }
7539
7540 return parameter_list;
7541 }
7542
7543 /* Parse a template-parameter.
7544
7545 template-parameter:
7546 type-parameter
7547 parameter-declaration
7548
7549 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7550 TREE_PURPOSE is the default value, if any. */
7551
7552 static tree
7553 cp_parser_template_parameter (cp_parser* parser)
7554 {
7555 cp_token *token;
7556
7557 /* Peek at the next token. */
7558 token = cp_lexer_peek_token (parser->lexer);
7559 /* If it is `class' or `template', we have a type-parameter. */
7560 if (token->keyword == RID_TEMPLATE)
7561 return cp_parser_type_parameter (parser);
7562 /* If it is `class' or `typename' we do not know yet whether it is a
7563 type parameter or a non-type parameter. Consider:
7564
7565 template <typename T, typename T::X X> ...
7566
7567 or:
7568
7569 template <class C, class D*> ...
7570
7571 Here, the first parameter is a type parameter, and the second is
7572 a non-type parameter. We can tell by looking at the token after
7573 the identifier -- if it is a `,', `=', or `>' then we have a type
7574 parameter. */
7575 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7576 {
7577 /* Peek at the token after `class' or `typename'. */
7578 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7579 /* If it's an identifier, skip it. */
7580 if (token->type == CPP_NAME)
7581 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7582 /* Now, see if the token looks like the end of a template
7583 parameter. */
7584 if (token->type == CPP_COMMA
7585 || token->type == CPP_EQ
7586 || token->type == CPP_GREATER)
7587 return cp_parser_type_parameter (parser);
7588 }
7589
7590 /* Otherwise, it is a non-type parameter.
7591
7592 [temp.param]
7593
7594 When parsing a default template-argument for a non-type
7595 template-parameter, the first non-nested `>' is taken as the end
7596 of the template parameter-list rather than a greater-than
7597 operator. */
7598 return
7599 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7600 /*parenthesized_p=*/NULL);
7601 }
7602
7603 /* Parse a type-parameter.
7604
7605 type-parameter:
7606 class identifier [opt]
7607 class identifier [opt] = type-id
7608 typename identifier [opt]
7609 typename identifier [opt] = type-id
7610 template < template-parameter-list > class identifier [opt]
7611 template < template-parameter-list > class identifier [opt]
7612 = id-expression
7613
7614 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7615 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7616 the declaration of the parameter. */
7617
7618 static tree
7619 cp_parser_type_parameter (cp_parser* parser)
7620 {
7621 cp_token *token;
7622 tree parameter;
7623
7624 /* Look for a keyword to tell us what kind of parameter this is. */
7625 token = cp_parser_require (parser, CPP_KEYWORD,
7626 "`class', `typename', or `template'");
7627 if (!token)
7628 return error_mark_node;
7629
7630 switch (token->keyword)
7631 {
7632 case RID_CLASS:
7633 case RID_TYPENAME:
7634 {
7635 tree identifier;
7636 tree default_argument;
7637
7638 /* If the next token is an identifier, then it names the
7639 parameter. */
7640 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7641 identifier = cp_parser_identifier (parser);
7642 else
7643 identifier = NULL_TREE;
7644
7645 /* Create the parameter. */
7646 parameter = finish_template_type_parm (class_type_node, identifier);
7647
7648 /* If the next token is an `=', we have a default argument. */
7649 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7650 {
7651 /* Consume the `=' token. */
7652 cp_lexer_consume_token (parser->lexer);
7653 /* Parse the default-argument. */
7654 default_argument = cp_parser_type_id (parser);
7655 }
7656 else
7657 default_argument = NULL_TREE;
7658
7659 /* Create the combined representation of the parameter and the
7660 default argument. */
7661 parameter = build_tree_list (default_argument, parameter);
7662 }
7663 break;
7664
7665 case RID_TEMPLATE:
7666 {
7667 tree parameter_list;
7668 tree identifier;
7669 tree default_argument;
7670
7671 /* Look for the `<'. */
7672 cp_parser_require (parser, CPP_LESS, "`<'");
7673 /* Parse the template-parameter-list. */
7674 begin_template_parm_list ();
7675 parameter_list
7676 = cp_parser_template_parameter_list (parser);
7677 parameter_list = end_template_parm_list (parameter_list);
7678 /* Look for the `>'. */
7679 cp_parser_require (parser, CPP_GREATER, "`>'");
7680 /* Look for the `class' keyword. */
7681 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7682 /* If the next token is an `=', then there is a
7683 default-argument. If the next token is a `>', we are at
7684 the end of the parameter-list. If the next token is a `,',
7685 then we are at the end of this parameter. */
7686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7687 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7688 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7689 identifier = cp_parser_identifier (parser);
7690 else
7691 identifier = NULL_TREE;
7692 /* Create the template parameter. */
7693 parameter = finish_template_template_parm (class_type_node,
7694 identifier);
7695
7696 /* If the next token is an `=', then there is a
7697 default-argument. */
7698 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7699 {
7700 bool is_template;
7701
7702 /* Consume the `='. */
7703 cp_lexer_consume_token (parser->lexer);
7704 /* Parse the id-expression. */
7705 default_argument
7706 = cp_parser_id_expression (parser,
7707 /*template_keyword_p=*/false,
7708 /*check_dependency_p=*/true,
7709 /*template_p=*/&is_template,
7710 /*declarator_p=*/false);
7711 if (TREE_CODE (default_argument) == TYPE_DECL)
7712 /* If the id-expression was a template-id that refers to
7713 a template-class, we already have the declaration here,
7714 so no further lookup is needed. */
7715 ;
7716 else
7717 /* Look up the name. */
7718 default_argument
7719 = cp_parser_lookup_name (parser, default_argument,
7720 /*is_type=*/false,
7721 /*is_template=*/is_template,
7722 /*is_namespace=*/false,
7723 /*check_dependency=*/true);
7724 /* See if the default argument is valid. */
7725 default_argument
7726 = check_template_template_default_arg (default_argument);
7727 }
7728 else
7729 default_argument = NULL_TREE;
7730
7731 /* Create the combined representation of the parameter and the
7732 default argument. */
7733 parameter = build_tree_list (default_argument, parameter);
7734 }
7735 break;
7736
7737 default:
7738 /* Anything else is an error. */
7739 cp_parser_error (parser,
7740 "expected `class', `typename', or `template'");
7741 parameter = error_mark_node;
7742 }
7743
7744 return parameter;
7745 }
7746
7747 /* Parse a template-id.
7748
7749 template-id:
7750 template-name < template-argument-list [opt] >
7751
7752 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7753 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7754 returned. Otherwise, if the template-name names a function, or set
7755 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7756 names a class, returns a TYPE_DECL for the specialization.
7757
7758 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7759 uninstantiated templates. */
7760
7761 static tree
7762 cp_parser_template_id (cp_parser *parser,
7763 bool template_keyword_p,
7764 bool check_dependency_p,
7765 bool is_declaration)
7766 {
7767 tree template;
7768 tree arguments;
7769 tree template_id;
7770 ptrdiff_t start_of_id;
7771 tree access_check = NULL_TREE;
7772 cp_token *next_token;
7773 bool is_identifier;
7774
7775 /* If the next token corresponds to a template-id, there is no need
7776 to reparse it. */
7777 next_token = cp_lexer_peek_token (parser->lexer);
7778 if (next_token->type == CPP_TEMPLATE_ID)
7779 {
7780 tree value;
7781 tree check;
7782
7783 /* Get the stored value. */
7784 value = cp_lexer_consume_token (parser->lexer)->value;
7785 /* Perform any access checks that were deferred. */
7786 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7787 perform_or_defer_access_check (TREE_PURPOSE (check),
7788 TREE_VALUE (check));
7789 /* Return the stored value. */
7790 return TREE_VALUE (value);
7791 }
7792
7793 /* Avoid performing name lookup if there is no possibility of
7794 finding a template-id. */
7795 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7796 || (next_token->type == CPP_NAME
7797 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7798 {
7799 cp_parser_error (parser, "expected template-id");
7800 return error_mark_node;
7801 }
7802
7803 /* Remember where the template-id starts. */
7804 if (cp_parser_parsing_tentatively (parser)
7805 && !cp_parser_committed_to_tentative_parse (parser))
7806 {
7807 next_token = cp_lexer_peek_token (parser->lexer);
7808 start_of_id = cp_lexer_token_difference (parser->lexer,
7809 parser->lexer->first_token,
7810 next_token);
7811 }
7812 else
7813 start_of_id = -1;
7814
7815 push_deferring_access_checks (dk_deferred);
7816
7817 /* Parse the template-name. */
7818 is_identifier = false;
7819 template = cp_parser_template_name (parser, template_keyword_p,
7820 check_dependency_p,
7821 is_declaration,
7822 &is_identifier);
7823 if (template == error_mark_node || is_identifier)
7824 {
7825 pop_deferring_access_checks ();
7826 return template;
7827 }
7828
7829 /* Look for the `<' that starts the template-argument-list. */
7830 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7831 {
7832 pop_deferring_access_checks ();
7833 return error_mark_node;
7834 }
7835
7836 /* Parse the arguments. */
7837 arguments = cp_parser_enclosed_template_argument_list (parser);
7838
7839 /* Build a representation of the specialization. */
7840 if (TREE_CODE (template) == IDENTIFIER_NODE)
7841 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7842 else if (DECL_CLASS_TEMPLATE_P (template)
7843 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7844 template_id
7845 = finish_template_type (template, arguments,
7846 cp_lexer_next_token_is (parser->lexer,
7847 CPP_SCOPE));
7848 else
7849 {
7850 /* If it's not a class-template or a template-template, it should be
7851 a function-template. */
7852 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7853 || TREE_CODE (template) == OVERLOAD
7854 || BASELINK_P (template)),
7855 20010716);
7856
7857 template_id = lookup_template_function (template, arguments);
7858 }
7859
7860 /* Retrieve any deferred checks. Do not pop this access checks yet
7861 so the memory will not be reclaimed during token replacing below. */
7862 access_check = get_deferred_access_checks ();
7863
7864 /* If parsing tentatively, replace the sequence of tokens that makes
7865 up the template-id with a CPP_TEMPLATE_ID token. That way,
7866 should we re-parse the token stream, we will not have to repeat
7867 the effort required to do the parse, nor will we issue duplicate
7868 error messages about problems during instantiation of the
7869 template. */
7870 if (start_of_id >= 0)
7871 {
7872 cp_token *token;
7873
7874 /* Find the token that corresponds to the start of the
7875 template-id. */
7876 token = cp_lexer_advance_token (parser->lexer,
7877 parser->lexer->first_token,
7878 start_of_id);
7879
7880 /* Reset the contents of the START_OF_ID token. */
7881 token->type = CPP_TEMPLATE_ID;
7882 token->value = build_tree_list (access_check, template_id);
7883 token->keyword = RID_MAX;
7884 /* Purge all subsequent tokens. */
7885 cp_lexer_purge_tokens_after (parser->lexer, token);
7886 }
7887
7888 pop_deferring_access_checks ();
7889 return template_id;
7890 }
7891
7892 /* Parse a template-name.
7893
7894 template-name:
7895 identifier
7896
7897 The standard should actually say:
7898
7899 template-name:
7900 identifier
7901 operator-function-id
7902
7903 A defect report has been filed about this issue.
7904
7905 A conversion-function-id cannot be a template name because they cannot
7906 be part of a template-id. In fact, looking at this code:
7907
7908 a.operator K<int>()
7909
7910 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
7911 It is impossible to call a templated conversion-function-id with an
7912 explicit argument list, since the only allowed template parameter is
7913 the type to which it is converting.
7914
7915 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7916 `template' keyword, in a construction like:
7917
7918 T::template f<3>()
7919
7920 In that case `f' is taken to be a template-name, even though there
7921 is no way of knowing for sure.
7922
7923 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7924 name refers to a set of overloaded functions, at least one of which
7925 is a template, or an IDENTIFIER_NODE with the name of the template,
7926 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7927 names are looked up inside uninstantiated templates. */
7928
7929 static tree
7930 cp_parser_template_name (cp_parser* parser,
7931 bool template_keyword_p,
7932 bool check_dependency_p,
7933 bool is_declaration,
7934 bool *is_identifier)
7935 {
7936 tree identifier;
7937 tree decl;
7938 tree fns;
7939
7940 /* If the next token is `operator', then we have either an
7941 operator-function-id or a conversion-function-id. */
7942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7943 {
7944 /* We don't know whether we're looking at an
7945 operator-function-id or a conversion-function-id. */
7946 cp_parser_parse_tentatively (parser);
7947 /* Try an operator-function-id. */
7948 identifier = cp_parser_operator_function_id (parser);
7949 /* If that didn't work, try a conversion-function-id. */
7950 if (!cp_parser_parse_definitely (parser))
7951 {
7952 cp_parser_error (parser, "expected template-name");
7953 return error_mark_node;
7954 }
7955 }
7956 /* Look for the identifier. */
7957 else
7958 identifier = cp_parser_identifier (parser);
7959
7960 /* If we didn't find an identifier, we don't have a template-id. */
7961 if (identifier == error_mark_node)
7962 return error_mark_node;
7963
7964 /* If the name immediately followed the `template' keyword, then it
7965 is a template-name. However, if the next token is not `<', then
7966 we do not treat it as a template-name, since it is not being used
7967 as part of a template-id. This enables us to handle constructs
7968 like:
7969
7970 template <typename T> struct S { S(); };
7971 template <typename T> S<T>::S();
7972
7973 correctly. We would treat `S' as a template -- if it were `S<T>'
7974 -- but we do not if there is no `<'. */
7975
7976 if (processing_template_decl
7977 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7978 {
7979 /* In a declaration, in a dependent context, we pretend that the
7980 "template" keyword was present in order to improve error
7981 recovery. For example, given:
7982
7983 template <typename T> void f(T::X<int>);
7984
7985 we want to treat "X<int>" as a template-id. */
7986 if (is_declaration
7987 && !template_keyword_p
7988 && parser->scope && TYPE_P (parser->scope)
7989 && dependent_type_p (parser->scope))
7990 {
7991 ptrdiff_t start;
7992 cp_token* token;
7993 /* Explain what went wrong. */
7994 error ("non-template `%D' used as template", identifier);
7995 error ("(use `%T::template %D' to indicate that it is a template)",
7996 parser->scope, identifier);
7997 /* If parsing tentatively, find the location of the "<"
7998 token. */
7999 if (cp_parser_parsing_tentatively (parser)
8000 && !cp_parser_committed_to_tentative_parse (parser))
8001 {
8002 cp_parser_simulate_error (parser);
8003 token = cp_lexer_peek_token (parser->lexer);
8004 token = cp_lexer_prev_token (parser->lexer, token);
8005 start = cp_lexer_token_difference (parser->lexer,
8006 parser->lexer->first_token,
8007 token);
8008 }
8009 else
8010 start = -1;
8011 /* Parse the template arguments so that we can issue error
8012 messages about them. */
8013 cp_lexer_consume_token (parser->lexer);
8014 cp_parser_enclosed_template_argument_list (parser);
8015 /* Skip tokens until we find a good place from which to
8016 continue parsing. */
8017 cp_parser_skip_to_closing_parenthesis (parser,
8018 /*recovering=*/true,
8019 /*or_comma=*/true,
8020 /*consume_paren=*/false);
8021 /* If parsing tentatively, permanently remove the
8022 template argument list. That will prevent duplicate
8023 error messages from being issued about the missing
8024 "template" keyword. */
8025 if (start >= 0)
8026 {
8027 token = cp_lexer_advance_token (parser->lexer,
8028 parser->lexer->first_token,
8029 start);
8030 cp_lexer_purge_tokens_after (parser->lexer, token);
8031 }
8032 if (is_identifier)
8033 *is_identifier = true;
8034 return identifier;
8035 }
8036 if (template_keyword_p)
8037 return identifier;
8038 }
8039
8040 /* Look up the name. */
8041 decl = cp_parser_lookup_name (parser, identifier,
8042 /*is_type=*/false,
8043 /*is_template=*/false,
8044 /*is_namespace=*/false,
8045 check_dependency_p);
8046 decl = maybe_get_template_decl_from_type_decl (decl);
8047
8048 /* If DECL is a template, then the name was a template-name. */
8049 if (TREE_CODE (decl) == TEMPLATE_DECL)
8050 ;
8051 else
8052 {
8053 /* The standard does not explicitly indicate whether a name that
8054 names a set of overloaded declarations, some of which are
8055 templates, is a template-name. However, such a name should
8056 be a template-name; otherwise, there is no way to form a
8057 template-id for the overloaded templates. */
8058 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8059 if (TREE_CODE (fns) == OVERLOAD)
8060 {
8061 tree fn;
8062
8063 for (fn = fns; fn; fn = OVL_NEXT (fn))
8064 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8065 break;
8066 }
8067 else
8068 {
8069 /* Otherwise, the name does not name a template. */
8070 cp_parser_error (parser, "expected template-name");
8071 return error_mark_node;
8072 }
8073 }
8074
8075 /* If DECL is dependent, and refers to a function, then just return
8076 its name; we will look it up again during template instantiation. */
8077 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8078 {
8079 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8080 if (TYPE_P (scope) && dependent_type_p (scope))
8081 return identifier;
8082 }
8083
8084 return decl;
8085 }
8086
8087 /* Parse a template-argument-list.
8088
8089 template-argument-list:
8090 template-argument
8091 template-argument-list , template-argument
8092
8093 Returns a TREE_VEC containing the arguments. */
8094
8095 static tree
8096 cp_parser_template_argument_list (cp_parser* parser)
8097 {
8098 tree fixed_args[10];
8099 unsigned n_args = 0;
8100 unsigned alloced = 10;
8101 tree *arg_ary = fixed_args;
8102 tree vec;
8103 bool saved_in_template_argument_list_p;
8104
8105 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8106 parser->in_template_argument_list_p = true;
8107 do
8108 {
8109 tree argument;
8110
8111 if (n_args)
8112 /* Consume the comma. */
8113 cp_lexer_consume_token (parser->lexer);
8114
8115 /* Parse the template-argument. */
8116 argument = cp_parser_template_argument (parser);
8117 if (n_args == alloced)
8118 {
8119 alloced *= 2;
8120
8121 if (arg_ary == fixed_args)
8122 {
8123 arg_ary = xmalloc (sizeof (tree) * alloced);
8124 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8125 }
8126 else
8127 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8128 }
8129 arg_ary[n_args++] = argument;
8130 }
8131 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8132
8133 vec = make_tree_vec (n_args);
8134
8135 while (n_args--)
8136 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8137
8138 if (arg_ary != fixed_args)
8139 free (arg_ary);
8140 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8141 return vec;
8142 }
8143
8144 /* Parse a template-argument.
8145
8146 template-argument:
8147 assignment-expression
8148 type-id
8149 id-expression
8150
8151 The representation is that of an assignment-expression, type-id, or
8152 id-expression -- except that the qualified id-expression is
8153 evaluated, so that the value returned is either a DECL or an
8154 OVERLOAD.
8155
8156 Although the standard says "assignment-expression", it forbids
8157 throw-expressions or assignments in the template argument.
8158 Therefore, we use "conditional-expression" instead. */
8159
8160 static tree
8161 cp_parser_template_argument (cp_parser* parser)
8162 {
8163 tree argument;
8164 bool template_p;
8165 bool address_p;
8166 bool maybe_type_id = false;
8167 cp_token *token;
8168 cp_id_kind idk;
8169 tree qualifying_class;
8170
8171 /* There's really no way to know what we're looking at, so we just
8172 try each alternative in order.
8173
8174 [temp.arg]
8175
8176 In a template-argument, an ambiguity between a type-id and an
8177 expression is resolved to a type-id, regardless of the form of
8178 the corresponding template-parameter.
8179
8180 Therefore, we try a type-id first. */
8181 cp_parser_parse_tentatively (parser);
8182 argument = cp_parser_type_id (parser);
8183 /* If there was no error parsing the type-id but the next token is a '>>',
8184 we probably found a typo for '> >'. But there are type-id which are
8185 also valid expressions. For instance:
8186
8187 struct X { int operator >> (int); };
8188 template <int V> struct Foo {};
8189 Foo<X () >> 5> r;
8190
8191 Here 'X()' is a valid type-id of a function type, but the user just
8192 wanted to write the expression "X() >> 5". Thus, we remember that we
8193 found a valid type-id, but we still try to parse the argument as an
8194 expression to see what happens. */
8195 if (!cp_parser_error_occurred (parser)
8196 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8197 {
8198 maybe_type_id = true;
8199 cp_parser_abort_tentative_parse (parser);
8200 }
8201 else
8202 {
8203 /* If the next token isn't a `,' or a `>', then this argument wasn't
8204 really finished. This means that the argument is not a valid
8205 type-id. */
8206 if (!cp_parser_next_token_ends_template_argument_p (parser))
8207 cp_parser_error (parser, "expected template-argument");
8208 /* If that worked, we're done. */
8209 if (cp_parser_parse_definitely (parser))
8210 return argument;
8211 }
8212 /* We're still not sure what the argument will be. */
8213 cp_parser_parse_tentatively (parser);
8214 /* Try a template. */
8215 argument = cp_parser_id_expression (parser,
8216 /*template_keyword_p=*/false,
8217 /*check_dependency_p=*/true,
8218 &template_p,
8219 /*declarator_p=*/false);
8220 /* If the next token isn't a `,' or a `>', then this argument wasn't
8221 really finished. */
8222 if (!cp_parser_next_token_ends_template_argument_p (parser))
8223 cp_parser_error (parser, "expected template-argument");
8224 if (!cp_parser_error_occurred (parser))
8225 {
8226 /* Figure out what is being referred to. */
8227 argument = cp_parser_lookup_name (parser, argument,
8228 /*is_type=*/false,
8229 /*is_template=*/template_p,
8230 /*is_namespace=*/false,
8231 /*check_dependency=*/true);
8232 if (TREE_CODE (argument) != TEMPLATE_DECL
8233 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8234 cp_parser_error (parser, "expected template-name");
8235 }
8236 if (cp_parser_parse_definitely (parser))
8237 return argument;
8238 /* It must be a non-type argument. There permitted cases are given
8239 in [temp.arg.nontype]:
8240
8241 -- an integral constant-expression of integral or enumeration
8242 type; or
8243
8244 -- the name of a non-type template-parameter; or
8245
8246 -- the name of an object or function with external linkage...
8247
8248 -- the address of an object or function with external linkage...
8249
8250 -- a pointer to member... */
8251 /* Look for a non-type template parameter. */
8252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8253 {
8254 cp_parser_parse_tentatively (parser);
8255 argument = cp_parser_primary_expression (parser,
8256 &idk,
8257 &qualifying_class);
8258 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8259 || !cp_parser_next_token_ends_template_argument_p (parser))
8260 cp_parser_simulate_error (parser);
8261 if (cp_parser_parse_definitely (parser))
8262 return argument;
8263 }
8264 /* If the next token is "&", the argument must be the address of an
8265 object or function with external linkage. */
8266 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8267 if (address_p)
8268 cp_lexer_consume_token (parser->lexer);
8269 /* See if we might have an id-expression. */
8270 token = cp_lexer_peek_token (parser->lexer);
8271 if (token->type == CPP_NAME
8272 || token->keyword == RID_OPERATOR
8273 || token->type == CPP_SCOPE
8274 || token->type == CPP_TEMPLATE_ID
8275 || token->type == CPP_NESTED_NAME_SPECIFIER)
8276 {
8277 cp_parser_parse_tentatively (parser);
8278 argument = cp_parser_primary_expression (parser,
8279 &idk,
8280 &qualifying_class);
8281 if (cp_parser_error_occurred (parser)
8282 || !cp_parser_next_token_ends_template_argument_p (parser))
8283 cp_parser_abort_tentative_parse (parser);
8284 else
8285 {
8286 if (qualifying_class)
8287 argument = finish_qualified_id_expr (qualifying_class,
8288 argument,
8289 /*done=*/true,
8290 address_p);
8291 if (TREE_CODE (argument) == VAR_DECL)
8292 {
8293 /* A variable without external linkage might still be a
8294 valid constant-expression, so no error is issued here
8295 if the external-linkage check fails. */
8296 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8297 cp_parser_simulate_error (parser);
8298 }
8299 else if (is_overloaded_fn (argument))
8300 /* All overloaded functions are allowed; if the external
8301 linkage test does not pass, an error will be issued
8302 later. */
8303 ;
8304 else if (address_p
8305 && (TREE_CODE (argument) == OFFSET_REF
8306 || TREE_CODE (argument) == SCOPE_REF))
8307 /* A pointer-to-member. */
8308 ;
8309 else
8310 cp_parser_simulate_error (parser);
8311
8312 if (cp_parser_parse_definitely (parser))
8313 {
8314 if (address_p)
8315 argument = build_x_unary_op (ADDR_EXPR, argument);
8316 return argument;
8317 }
8318 }
8319 }
8320 /* If the argument started with "&", there are no other valid
8321 alternatives at this point. */
8322 if (address_p)
8323 {
8324 cp_parser_error (parser, "invalid non-type template argument");
8325 return error_mark_node;
8326 }
8327 /* If the argument wasn't successfully parsed as a type-id followed
8328 by '>>', the argument can only be a constant expression now.
8329 Otherwise, we try parsing the constant-expression tentatively,
8330 because the argument could really be a type-id. */
8331 if (maybe_type_id)
8332 cp_parser_parse_tentatively (parser);
8333 argument = cp_parser_constant_expression (parser,
8334 /*allow_non_constant_p=*/false,
8335 /*non_constant_p=*/NULL);
8336 argument = cp_parser_fold_non_dependent_expr (argument);
8337 if (!maybe_type_id)
8338 return argument;
8339 if (!cp_parser_next_token_ends_template_argument_p (parser))
8340 cp_parser_error (parser, "expected template-argument");
8341 if (cp_parser_parse_definitely (parser))
8342 return argument;
8343 /* We did our best to parse the argument as a non type-id, but that
8344 was the only alternative that matched (albeit with a '>' after
8345 it). We can assume it's just a typo from the user, and a
8346 diagnostic will then be issued. */
8347 return cp_parser_type_id (parser);
8348 }
8349
8350 /* Parse an explicit-instantiation.
8351
8352 explicit-instantiation:
8353 template declaration
8354
8355 Although the standard says `declaration', what it really means is:
8356
8357 explicit-instantiation:
8358 template decl-specifier-seq [opt] declarator [opt] ;
8359
8360 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8361 supposed to be allowed. A defect report has been filed about this
8362 issue.
8363
8364 GNU Extension:
8365
8366 explicit-instantiation:
8367 storage-class-specifier template
8368 decl-specifier-seq [opt] declarator [opt] ;
8369 function-specifier template
8370 decl-specifier-seq [opt] declarator [opt] ; */
8371
8372 static void
8373 cp_parser_explicit_instantiation (cp_parser* parser)
8374 {
8375 int declares_class_or_enum;
8376 tree decl_specifiers;
8377 tree attributes;
8378 tree extension_specifier = NULL_TREE;
8379
8380 /* Look for an (optional) storage-class-specifier or
8381 function-specifier. */
8382 if (cp_parser_allow_gnu_extensions_p (parser))
8383 {
8384 extension_specifier
8385 = cp_parser_storage_class_specifier_opt (parser);
8386 if (!extension_specifier)
8387 extension_specifier = cp_parser_function_specifier_opt (parser);
8388 }
8389
8390 /* Look for the `template' keyword. */
8391 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8392 /* Let the front end know that we are processing an explicit
8393 instantiation. */
8394 begin_explicit_instantiation ();
8395 /* [temp.explicit] says that we are supposed to ignore access
8396 control while processing explicit instantiation directives. */
8397 push_deferring_access_checks (dk_no_check);
8398 /* Parse a decl-specifier-seq. */
8399 decl_specifiers
8400 = cp_parser_decl_specifier_seq (parser,
8401 CP_PARSER_FLAGS_OPTIONAL,
8402 &attributes,
8403 &declares_class_or_enum);
8404 /* If there was exactly one decl-specifier, and it declared a class,
8405 and there's no declarator, then we have an explicit type
8406 instantiation. */
8407 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8408 {
8409 tree type;
8410
8411 type = check_tag_decl (decl_specifiers);
8412 /* Turn access control back on for names used during
8413 template instantiation. */
8414 pop_deferring_access_checks ();
8415 if (type)
8416 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8417 }
8418 else
8419 {
8420 tree declarator;
8421 tree decl;
8422
8423 /* Parse the declarator. */
8424 declarator
8425 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8426 /*ctor_dtor_or_conv_p=*/NULL,
8427 /*parenthesized_p=*/NULL);
8428 cp_parser_check_for_definition_in_return_type (declarator,
8429 declares_class_or_enum);
8430 if (declarator != error_mark_node)
8431 {
8432 decl = grokdeclarator (declarator, decl_specifiers,
8433 NORMAL, 0, NULL);
8434 /* Turn access control back on for names used during
8435 template instantiation. */
8436 pop_deferring_access_checks ();
8437 /* Do the explicit instantiation. */
8438 do_decl_instantiation (decl, extension_specifier);
8439 }
8440 else
8441 {
8442 pop_deferring_access_checks ();
8443 /* Skip the body of the explicit instantiation. */
8444 cp_parser_skip_to_end_of_statement (parser);
8445 }
8446 }
8447 /* We're done with the instantiation. */
8448 end_explicit_instantiation ();
8449
8450 cp_parser_consume_semicolon_at_end_of_statement (parser);
8451 }
8452
8453 /* Parse an explicit-specialization.
8454
8455 explicit-specialization:
8456 template < > declaration
8457
8458 Although the standard says `declaration', what it really means is:
8459
8460 explicit-specialization:
8461 template <> decl-specifier [opt] init-declarator [opt] ;
8462 template <> function-definition
8463 template <> explicit-specialization
8464 template <> template-declaration */
8465
8466 static void
8467 cp_parser_explicit_specialization (cp_parser* parser)
8468 {
8469 /* Look for the `template' keyword. */
8470 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8471 /* Look for the `<'. */
8472 cp_parser_require (parser, CPP_LESS, "`<'");
8473 /* Look for the `>'. */
8474 cp_parser_require (parser, CPP_GREATER, "`>'");
8475 /* We have processed another parameter list. */
8476 ++parser->num_template_parameter_lists;
8477 /* Let the front end know that we are beginning a specialization. */
8478 begin_specialization ();
8479
8480 /* If the next keyword is `template', we need to figure out whether
8481 or not we're looking a template-declaration. */
8482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8483 {
8484 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8485 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8486 cp_parser_template_declaration_after_export (parser,
8487 /*member_p=*/false);
8488 else
8489 cp_parser_explicit_specialization (parser);
8490 }
8491 else
8492 /* Parse the dependent declaration. */
8493 cp_parser_single_declaration (parser,
8494 /*member_p=*/false,
8495 /*friend_p=*/NULL);
8496
8497 /* We're done with the specialization. */
8498 end_specialization ();
8499 /* We're done with this parameter list. */
8500 --parser->num_template_parameter_lists;
8501 }
8502
8503 /* Parse a type-specifier.
8504
8505 type-specifier:
8506 simple-type-specifier
8507 class-specifier
8508 enum-specifier
8509 elaborated-type-specifier
8510 cv-qualifier
8511
8512 GNU Extension:
8513
8514 type-specifier:
8515 __complex__
8516
8517 Returns a representation of the type-specifier. If the
8518 type-specifier is a keyword (like `int' or `const', or
8519 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8520 For a class-specifier, enum-specifier, or elaborated-type-specifier
8521 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8522
8523 If IS_FRIEND is TRUE then this type-specifier is being declared a
8524 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8525 appearing in a decl-specifier-seq.
8526
8527 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8528 class-specifier, enum-specifier, or elaborated-type-specifier, then
8529 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8530 if a type is declared; 2 if it is defined. Otherwise, it is set to
8531 zero.
8532
8533 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8534 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8535 is set to FALSE. */
8536
8537 static tree
8538 cp_parser_type_specifier (cp_parser* parser,
8539 cp_parser_flags flags,
8540 bool is_friend,
8541 bool is_declaration,
8542 int* declares_class_or_enum,
8543 bool* is_cv_qualifier)
8544 {
8545 tree type_spec = NULL_TREE;
8546 cp_token *token;
8547 enum rid keyword;
8548
8549 /* Assume this type-specifier does not declare a new type. */
8550 if (declares_class_or_enum)
8551 *declares_class_or_enum = 0;
8552 /* And that it does not specify a cv-qualifier. */
8553 if (is_cv_qualifier)
8554 *is_cv_qualifier = false;
8555 /* Peek at the next token. */
8556 token = cp_lexer_peek_token (parser->lexer);
8557
8558 /* If we're looking at a keyword, we can use that to guide the
8559 production we choose. */
8560 keyword = token->keyword;
8561 switch (keyword)
8562 {
8563 /* Any of these indicate either a class-specifier, or an
8564 elaborated-type-specifier. */
8565 case RID_CLASS:
8566 case RID_STRUCT:
8567 case RID_UNION:
8568 case RID_ENUM:
8569 /* Parse tentatively so that we can back up if we don't find a
8570 class-specifier or enum-specifier. */
8571 cp_parser_parse_tentatively (parser);
8572 /* Look for the class-specifier or enum-specifier. */
8573 if (keyword == RID_ENUM)
8574 type_spec = cp_parser_enum_specifier (parser);
8575 else
8576 type_spec = cp_parser_class_specifier (parser);
8577
8578 /* If that worked, we're done. */
8579 if (cp_parser_parse_definitely (parser))
8580 {
8581 if (declares_class_or_enum)
8582 *declares_class_or_enum = 2;
8583 return type_spec;
8584 }
8585
8586 /* Fall through. */
8587
8588 case RID_TYPENAME:
8589 /* Look for an elaborated-type-specifier. */
8590 type_spec = cp_parser_elaborated_type_specifier (parser,
8591 is_friend,
8592 is_declaration);
8593 /* We're declaring a class or enum -- unless we're using
8594 `typename'. */
8595 if (declares_class_or_enum && keyword != RID_TYPENAME)
8596 *declares_class_or_enum = 1;
8597 return type_spec;
8598
8599 case RID_CONST:
8600 case RID_VOLATILE:
8601 case RID_RESTRICT:
8602 type_spec = cp_parser_cv_qualifier_opt (parser);
8603 /* Even though we call a routine that looks for an optional
8604 qualifier, we know that there should be one. */
8605 my_friendly_assert (type_spec != NULL, 20000328);
8606 /* This type-specifier was a cv-qualified. */
8607 if (is_cv_qualifier)
8608 *is_cv_qualifier = true;
8609
8610 return type_spec;
8611
8612 case RID_COMPLEX:
8613 /* The `__complex__' keyword is a GNU extension. */
8614 return cp_lexer_consume_token (parser->lexer)->value;
8615
8616 default:
8617 break;
8618 }
8619
8620 /* If we do not already have a type-specifier, assume we are looking
8621 at a simple-type-specifier. */
8622 type_spec = cp_parser_simple_type_specifier (parser, flags,
8623 /*identifier_p=*/true);
8624
8625 /* If we didn't find a type-specifier, and a type-specifier was not
8626 optional in this context, issue an error message. */
8627 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8628 {
8629 cp_parser_error (parser, "expected type specifier");
8630 return error_mark_node;
8631 }
8632
8633 return type_spec;
8634 }
8635
8636 /* Parse a simple-type-specifier.
8637
8638 simple-type-specifier:
8639 :: [opt] nested-name-specifier [opt] type-name
8640 :: [opt] nested-name-specifier template template-id
8641 char
8642 wchar_t
8643 bool
8644 short
8645 int
8646 long
8647 signed
8648 unsigned
8649 float
8650 double
8651 void
8652
8653 GNU Extension:
8654
8655 simple-type-specifier:
8656 __typeof__ unary-expression
8657 __typeof__ ( type-id )
8658
8659 For the various keywords, the value returned is simply the
8660 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8661 For the first two productions, and if IDENTIFIER_P is false, the
8662 value returned is the indicated TYPE_DECL. */
8663
8664 static tree
8665 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8666 bool identifier_p)
8667 {
8668 tree type = NULL_TREE;
8669 cp_token *token;
8670
8671 /* Peek at the next token. */
8672 token = cp_lexer_peek_token (parser->lexer);
8673
8674 /* If we're looking at a keyword, things are easy. */
8675 switch (token->keyword)
8676 {
8677 case RID_CHAR:
8678 type = char_type_node;
8679 break;
8680 case RID_WCHAR:
8681 type = wchar_type_node;
8682 break;
8683 case RID_BOOL:
8684 type = boolean_type_node;
8685 break;
8686 case RID_SHORT:
8687 type = short_integer_type_node;
8688 break;
8689 case RID_INT:
8690 type = integer_type_node;
8691 break;
8692 case RID_LONG:
8693 type = long_integer_type_node;
8694 break;
8695 case RID_SIGNED:
8696 type = integer_type_node;
8697 break;
8698 case RID_UNSIGNED:
8699 type = unsigned_type_node;
8700 break;
8701 case RID_FLOAT:
8702 type = float_type_node;
8703 break;
8704 case RID_DOUBLE:
8705 type = double_type_node;
8706 break;
8707 case RID_VOID:
8708 type = void_type_node;
8709 break;
8710
8711 case RID_TYPEOF:
8712 {
8713 tree operand;
8714
8715 /* Consume the `typeof' token. */
8716 cp_lexer_consume_token (parser->lexer);
8717 /* Parse the operand to `typeof'. */
8718 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8719 /* If it is not already a TYPE, take its type. */
8720 if (!TYPE_P (operand))
8721 operand = finish_typeof (operand);
8722
8723 return operand;
8724 }
8725
8726 default:
8727 break;
8728 }
8729
8730 /* If the type-specifier was for a built-in type, we're done. */
8731 if (type)
8732 {
8733 tree id;
8734
8735 /* Consume the token. */
8736 id = cp_lexer_consume_token (parser->lexer)->value;
8737
8738 /* There is no valid C++ program where a non-template type is
8739 followed by a "<". That usually indicates that the user thought
8740 that the type was a template. */
8741 cp_parser_check_for_invalid_template_id (parser, type);
8742
8743 return identifier_p ? id : TYPE_NAME (type);
8744 }
8745
8746 /* The type-specifier must be a user-defined type. */
8747 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8748 {
8749 /* Don't gobble tokens or issue error messages if this is an
8750 optional type-specifier. */
8751 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8752 cp_parser_parse_tentatively (parser);
8753
8754 /* Look for the optional `::' operator. */
8755 cp_parser_global_scope_opt (parser,
8756 /*current_scope_valid_p=*/false);
8757 /* Look for the nested-name specifier. */
8758 cp_parser_nested_name_specifier_opt (parser,
8759 /*typename_keyword_p=*/false,
8760 /*check_dependency_p=*/true,
8761 /*type_p=*/false,
8762 /*is_declaration=*/false);
8763 /* If we have seen a nested-name-specifier, and the next token
8764 is `template', then we are using the template-id production. */
8765 if (parser->scope
8766 && cp_parser_optional_template_keyword (parser))
8767 {
8768 /* Look for the template-id. */
8769 type = cp_parser_template_id (parser,
8770 /*template_keyword_p=*/true,
8771 /*check_dependency_p=*/true,
8772 /*is_declaration=*/false);
8773 /* If the template-id did not name a type, we are out of
8774 luck. */
8775 if (TREE_CODE (type) != TYPE_DECL)
8776 {
8777 cp_parser_error (parser, "expected template-id for type");
8778 type = NULL_TREE;
8779 }
8780 }
8781 /* Otherwise, look for a type-name. */
8782 else
8783 type = cp_parser_type_name (parser);
8784 /* If it didn't work out, we don't have a TYPE. */
8785 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8786 && !cp_parser_parse_definitely (parser))
8787 type = NULL_TREE;
8788 }
8789
8790 /* If we didn't get a type-name, issue an error message. */
8791 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8792 {
8793 cp_parser_error (parser, "expected type-name");
8794 return error_mark_node;
8795 }
8796
8797 /* There is no valid C++ program where a non-template type is
8798 followed by a "<". That usually indicates that the user thought
8799 that the type was a template. */
8800 if (type && type != error_mark_node)
8801 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8802
8803 return type;
8804 }
8805
8806 /* Parse a type-name.
8807
8808 type-name:
8809 class-name
8810 enum-name
8811 typedef-name
8812
8813 enum-name:
8814 identifier
8815
8816 typedef-name:
8817 identifier
8818
8819 Returns a TYPE_DECL for the the type. */
8820
8821 static tree
8822 cp_parser_type_name (cp_parser* parser)
8823 {
8824 tree type_decl;
8825 tree identifier;
8826
8827 /* We can't know yet whether it is a class-name or not. */
8828 cp_parser_parse_tentatively (parser);
8829 /* Try a class-name. */
8830 type_decl = cp_parser_class_name (parser,
8831 /*typename_keyword_p=*/false,
8832 /*template_keyword_p=*/false,
8833 /*type_p=*/false,
8834 /*check_dependency_p=*/true,
8835 /*class_head_p=*/false,
8836 /*is_declaration=*/false);
8837 /* If it's not a class-name, keep looking. */
8838 if (!cp_parser_parse_definitely (parser))
8839 {
8840 /* It must be a typedef-name or an enum-name. */
8841 identifier = cp_parser_identifier (parser);
8842 if (identifier == error_mark_node)
8843 return error_mark_node;
8844
8845 /* Look up the type-name. */
8846 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8847 /* Issue an error if we did not find a type-name. */
8848 if (TREE_CODE (type_decl) != TYPE_DECL)
8849 {
8850 if (!cp_parser_simulate_error (parser))
8851 cp_parser_name_lookup_error (parser, identifier, type_decl,
8852 "is not a type");
8853 type_decl = error_mark_node;
8854 }
8855 /* Remember that the name was used in the definition of the
8856 current class so that we can check later to see if the
8857 meaning would have been different after the class was
8858 entirely defined. */
8859 else if (type_decl != error_mark_node
8860 && !parser->scope)
8861 maybe_note_name_used_in_class (identifier, type_decl);
8862 }
8863
8864 return type_decl;
8865 }
8866
8867
8868 /* Parse an elaborated-type-specifier. Note that the grammar given
8869 here incorporates the resolution to DR68.
8870
8871 elaborated-type-specifier:
8872 class-key :: [opt] nested-name-specifier [opt] identifier
8873 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8874 enum :: [opt] nested-name-specifier [opt] identifier
8875 typename :: [opt] nested-name-specifier identifier
8876 typename :: [opt] nested-name-specifier template [opt]
8877 template-id
8878
8879 GNU extension:
8880
8881 elaborated-type-specifier:
8882 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8883 class-key attributes :: [opt] nested-name-specifier [opt]
8884 template [opt] template-id
8885 enum attributes :: [opt] nested-name-specifier [opt] identifier
8886
8887 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8888 declared `friend'. If IS_DECLARATION is TRUE, then this
8889 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8890 something is being declared.
8891
8892 Returns the TYPE specified. */
8893
8894 static tree
8895 cp_parser_elaborated_type_specifier (cp_parser* parser,
8896 bool is_friend,
8897 bool is_declaration)
8898 {
8899 enum tag_types tag_type;
8900 tree identifier;
8901 tree type = NULL_TREE;
8902 tree attributes = NULL_TREE;
8903
8904 /* See if we're looking at the `enum' keyword. */
8905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8906 {
8907 /* Consume the `enum' token. */
8908 cp_lexer_consume_token (parser->lexer);
8909 /* Remember that it's an enumeration type. */
8910 tag_type = enum_type;
8911 /* Parse the attributes. */
8912 attributes = cp_parser_attributes_opt (parser);
8913 }
8914 /* Or, it might be `typename'. */
8915 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8916 RID_TYPENAME))
8917 {
8918 /* Consume the `typename' token. */
8919 cp_lexer_consume_token (parser->lexer);
8920 /* Remember that it's a `typename' type. */
8921 tag_type = typename_type;
8922 /* The `typename' keyword is only allowed in templates. */
8923 if (!processing_template_decl)
8924 pedwarn ("using `typename' outside of template");
8925 }
8926 /* Otherwise it must be a class-key. */
8927 else
8928 {
8929 tag_type = cp_parser_class_key (parser);
8930 if (tag_type == none_type)
8931 return error_mark_node;
8932 /* Parse the attributes. */
8933 attributes = cp_parser_attributes_opt (parser);
8934 }
8935
8936 /* Look for the `::' operator. */
8937 cp_parser_global_scope_opt (parser,
8938 /*current_scope_valid_p=*/false);
8939 /* Look for the nested-name-specifier. */
8940 if (tag_type == typename_type)
8941 {
8942 if (cp_parser_nested_name_specifier (parser,
8943 /*typename_keyword_p=*/true,
8944 /*check_dependency_p=*/true,
8945 /*type_p=*/true,
8946 is_declaration)
8947 == error_mark_node)
8948 return error_mark_node;
8949 }
8950 else
8951 /* Even though `typename' is not present, the proposed resolution
8952 to Core Issue 180 says that in `class A<T>::B', `B' should be
8953 considered a type-name, even if `A<T>' is dependent. */
8954 cp_parser_nested_name_specifier_opt (parser,
8955 /*typename_keyword_p=*/true,
8956 /*check_dependency_p=*/true,
8957 /*type_p=*/true,
8958 is_declaration);
8959 /* For everything but enumeration types, consider a template-id. */
8960 if (tag_type != enum_type)
8961 {
8962 bool template_p = false;
8963 tree decl;
8964
8965 /* Allow the `template' keyword. */
8966 template_p = cp_parser_optional_template_keyword (parser);
8967 /* If we didn't see `template', we don't know if there's a
8968 template-id or not. */
8969 if (!template_p)
8970 cp_parser_parse_tentatively (parser);
8971 /* Parse the template-id. */
8972 decl = cp_parser_template_id (parser, template_p,
8973 /*check_dependency_p=*/true,
8974 is_declaration);
8975 /* If we didn't find a template-id, look for an ordinary
8976 identifier. */
8977 if (!template_p && !cp_parser_parse_definitely (parser))
8978 ;
8979 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8980 in effect, then we must assume that, upon instantiation, the
8981 template will correspond to a class. */
8982 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8983 && tag_type == typename_type)
8984 type = make_typename_type (parser->scope, decl,
8985 /*complain=*/1);
8986 else
8987 type = TREE_TYPE (decl);
8988 }
8989
8990 /* For an enumeration type, consider only a plain identifier. */
8991 if (!type)
8992 {
8993 identifier = cp_parser_identifier (parser);
8994
8995 if (identifier == error_mark_node)
8996 {
8997 parser->scope = NULL_TREE;
8998 return error_mark_node;
8999 }
9000
9001 /* For a `typename', we needn't call xref_tag. */
9002 if (tag_type == typename_type)
9003 return make_typename_type (parser->scope, identifier,
9004 /*complain=*/1);
9005 /* Look up a qualified name in the usual way. */
9006 if (parser->scope)
9007 {
9008 tree decl;
9009
9010 /* In an elaborated-type-specifier, names are assumed to name
9011 types, so we set IS_TYPE to TRUE when calling
9012 cp_parser_lookup_name. */
9013 decl = cp_parser_lookup_name (parser, identifier,
9014 /*is_type=*/true,
9015 /*is_template=*/false,
9016 /*is_namespace=*/false,
9017 /*check_dependency=*/true);
9018
9019 /* If we are parsing friend declaration, DECL may be a
9020 TEMPLATE_DECL tree node here. However, we need to check
9021 whether this TEMPLATE_DECL results in valid code. Consider
9022 the following example:
9023
9024 namespace N {
9025 template <class T> class C {};
9026 }
9027 class X {
9028 template <class T> friend class N::C; // #1, valid code
9029 };
9030 template <class T> class Y {
9031 friend class N::C; // #2, invalid code
9032 };
9033
9034 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9035 name lookup of `N::C'. We see that friend declaration must
9036 be template for the code to be valid. Note that
9037 processing_template_decl does not work here since it is
9038 always 1 for the above two cases. */
9039
9040 decl = (cp_parser_maybe_treat_template_as_class
9041 (decl, /*tag_name_p=*/is_friend
9042 && parser->num_template_parameter_lists));
9043
9044 if (TREE_CODE (decl) != TYPE_DECL)
9045 {
9046 error ("expected type-name");
9047 return error_mark_node;
9048 }
9049
9050 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9051 check_elaborated_type_specifier
9052 (tag_type, decl,
9053 (parser->num_template_parameter_lists
9054 || DECL_SELF_REFERENCE_P (decl)));
9055
9056 type = TREE_TYPE (decl);
9057 }
9058 else
9059 {
9060 /* An elaborated-type-specifier sometimes introduces a new type and
9061 sometimes names an existing type. Normally, the rule is that it
9062 introduces a new type only if there is not an existing type of
9063 the same name already in scope. For example, given:
9064
9065 struct S {};
9066 void f() { struct S s; }
9067
9068 the `struct S' in the body of `f' is the same `struct S' as in
9069 the global scope; the existing definition is used. However, if
9070 there were no global declaration, this would introduce a new
9071 local class named `S'.
9072
9073 An exception to this rule applies to the following code:
9074
9075 namespace N { struct S; }
9076
9077 Here, the elaborated-type-specifier names a new type
9078 unconditionally; even if there is already an `S' in the
9079 containing scope this declaration names a new type.
9080 This exception only applies if the elaborated-type-specifier
9081 forms the complete declaration:
9082
9083 [class.name]
9084
9085 A declaration consisting solely of `class-key identifier ;' is
9086 either a redeclaration of the name in the current scope or a
9087 forward declaration of the identifier as a class name. It
9088 introduces the name into the current scope.
9089
9090 We are in this situation precisely when the next token is a `;'.
9091
9092 An exception to the exception is that a `friend' declaration does
9093 *not* name a new type; i.e., given:
9094
9095 struct S { friend struct T; };
9096
9097 `T' is not a new type in the scope of `S'.
9098
9099 Also, `new struct S' or `sizeof (struct S)' never results in the
9100 definition of a new type; a new type can only be declared in a
9101 declaration context. */
9102
9103 /* Warn about attributes. They are ignored. */
9104 if (attributes)
9105 warning ("type attributes are honored only at type definition");
9106
9107 type = xref_tag (tag_type, identifier,
9108 /*attributes=*/NULL_TREE,
9109 (is_friend
9110 || !is_declaration
9111 || cp_lexer_next_token_is_not (parser->lexer,
9112 CPP_SEMICOLON)),
9113 parser->num_template_parameter_lists);
9114 }
9115 }
9116 if (tag_type != enum_type)
9117 cp_parser_check_class_key (tag_type, type);
9118
9119 /* A "<" cannot follow an elaborated type specifier. If that
9120 happens, the user was probably trying to form a template-id. */
9121 cp_parser_check_for_invalid_template_id (parser, type);
9122
9123 return type;
9124 }
9125
9126 /* Parse an enum-specifier.
9127
9128 enum-specifier:
9129 enum identifier [opt] { enumerator-list [opt] }
9130
9131 Returns an ENUM_TYPE representing the enumeration. */
9132
9133 static tree
9134 cp_parser_enum_specifier (cp_parser* parser)
9135 {
9136 cp_token *token;
9137 tree identifier = NULL_TREE;
9138 tree type;
9139
9140 /* Look for the `enum' keyword. */
9141 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9142 return error_mark_node;
9143 /* Peek at the next token. */
9144 token = cp_lexer_peek_token (parser->lexer);
9145
9146 /* See if it is an identifier. */
9147 if (token->type == CPP_NAME)
9148 identifier = cp_parser_identifier (parser);
9149
9150 /* Look for the `{'. */
9151 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9152 return error_mark_node;
9153
9154 /* At this point, we're going ahead with the enum-specifier, even
9155 if some other problem occurs. */
9156 cp_parser_commit_to_tentative_parse (parser);
9157
9158 /* Issue an error message if type-definitions are forbidden here. */
9159 cp_parser_check_type_definition (parser);
9160
9161 /* Create the new type. */
9162 type = start_enum (identifier ? identifier : make_anon_name ());
9163
9164 /* Peek at the next token. */
9165 token = cp_lexer_peek_token (parser->lexer);
9166 /* If it's not a `}', then there are some enumerators. */
9167 if (token->type != CPP_CLOSE_BRACE)
9168 cp_parser_enumerator_list (parser, type);
9169 /* Look for the `}'. */
9170 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9171
9172 /* Finish up the enumeration. */
9173 finish_enum (type);
9174
9175 return type;
9176 }
9177
9178 /* Parse an enumerator-list. The enumerators all have the indicated
9179 TYPE.
9180
9181 enumerator-list:
9182 enumerator-definition
9183 enumerator-list , enumerator-definition */
9184
9185 static void
9186 cp_parser_enumerator_list (cp_parser* parser, tree type)
9187 {
9188 while (true)
9189 {
9190 cp_token *token;
9191
9192 /* Parse an enumerator-definition. */
9193 cp_parser_enumerator_definition (parser, type);
9194 /* Peek at the next token. */
9195 token = cp_lexer_peek_token (parser->lexer);
9196 /* If it's not a `,', then we've reached the end of the
9197 list. */
9198 if (token->type != CPP_COMMA)
9199 break;
9200 /* Otherwise, consume the `,' and keep going. */
9201 cp_lexer_consume_token (parser->lexer);
9202 /* If the next token is a `}', there is a trailing comma. */
9203 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9204 {
9205 if (pedantic && !in_system_header)
9206 pedwarn ("comma at end of enumerator list");
9207 break;
9208 }
9209 }
9210 }
9211
9212 /* Parse an enumerator-definition. The enumerator has the indicated
9213 TYPE.
9214
9215 enumerator-definition:
9216 enumerator
9217 enumerator = constant-expression
9218
9219 enumerator:
9220 identifier */
9221
9222 static void
9223 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9224 {
9225 cp_token *token;
9226 tree identifier;
9227 tree value;
9228
9229 /* Look for the identifier. */
9230 identifier = cp_parser_identifier (parser);
9231 if (identifier == error_mark_node)
9232 return;
9233
9234 /* Peek at the next token. */
9235 token = cp_lexer_peek_token (parser->lexer);
9236 /* If it's an `=', then there's an explicit value. */
9237 if (token->type == CPP_EQ)
9238 {
9239 /* Consume the `=' token. */
9240 cp_lexer_consume_token (parser->lexer);
9241 /* Parse the value. */
9242 value = cp_parser_constant_expression (parser,
9243 /*allow_non_constant_p=*/false,
9244 NULL);
9245 }
9246 else
9247 value = NULL_TREE;
9248
9249 /* Create the enumerator. */
9250 build_enumerator (identifier, value, type);
9251 }
9252
9253 /* Parse a namespace-name.
9254
9255 namespace-name:
9256 original-namespace-name
9257 namespace-alias
9258
9259 Returns the NAMESPACE_DECL for the namespace. */
9260
9261 static tree
9262 cp_parser_namespace_name (cp_parser* parser)
9263 {
9264 tree identifier;
9265 tree namespace_decl;
9266
9267 /* Get the name of the namespace. */
9268 identifier = cp_parser_identifier (parser);
9269 if (identifier == error_mark_node)
9270 return error_mark_node;
9271
9272 /* Look up the identifier in the currently active scope. Look only
9273 for namespaces, due to:
9274
9275 [basic.lookup.udir]
9276
9277 When looking up a namespace-name in a using-directive or alias
9278 definition, only namespace names are considered.
9279
9280 And:
9281
9282 [basic.lookup.qual]
9283
9284 During the lookup of a name preceding the :: scope resolution
9285 operator, object, function, and enumerator names are ignored.
9286
9287 (Note that cp_parser_class_or_namespace_name only calls this
9288 function if the token after the name is the scope resolution
9289 operator.) */
9290 namespace_decl = cp_parser_lookup_name (parser, identifier,
9291 /*is_type=*/false,
9292 /*is_template=*/false,
9293 /*is_namespace=*/true,
9294 /*check_dependency=*/true);
9295 /* If it's not a namespace, issue an error. */
9296 if (namespace_decl == error_mark_node
9297 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9298 {
9299 cp_parser_error (parser, "expected namespace-name");
9300 namespace_decl = error_mark_node;
9301 }
9302
9303 return namespace_decl;
9304 }
9305
9306 /* Parse a namespace-definition.
9307
9308 namespace-definition:
9309 named-namespace-definition
9310 unnamed-namespace-definition
9311
9312 named-namespace-definition:
9313 original-namespace-definition
9314 extension-namespace-definition
9315
9316 original-namespace-definition:
9317 namespace identifier { namespace-body }
9318
9319 extension-namespace-definition:
9320 namespace original-namespace-name { namespace-body }
9321
9322 unnamed-namespace-definition:
9323 namespace { namespace-body } */
9324
9325 static void
9326 cp_parser_namespace_definition (cp_parser* parser)
9327 {
9328 tree identifier;
9329
9330 /* Look for the `namespace' keyword. */
9331 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9332
9333 /* Get the name of the namespace. We do not attempt to distinguish
9334 between an original-namespace-definition and an
9335 extension-namespace-definition at this point. The semantic
9336 analysis routines are responsible for that. */
9337 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9338 identifier = cp_parser_identifier (parser);
9339 else
9340 identifier = NULL_TREE;
9341
9342 /* Look for the `{' to start the namespace. */
9343 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9344 /* Start the namespace. */
9345 push_namespace (identifier);
9346 /* Parse the body of the namespace. */
9347 cp_parser_namespace_body (parser);
9348 /* Finish the namespace. */
9349 pop_namespace ();
9350 /* Look for the final `}'. */
9351 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9352 }
9353
9354 /* Parse a namespace-body.
9355
9356 namespace-body:
9357 declaration-seq [opt] */
9358
9359 static void
9360 cp_parser_namespace_body (cp_parser* parser)
9361 {
9362 cp_parser_declaration_seq_opt (parser);
9363 }
9364
9365 /* Parse a namespace-alias-definition.
9366
9367 namespace-alias-definition:
9368 namespace identifier = qualified-namespace-specifier ; */
9369
9370 static void
9371 cp_parser_namespace_alias_definition (cp_parser* parser)
9372 {
9373 tree identifier;
9374 tree namespace_specifier;
9375
9376 /* Look for the `namespace' keyword. */
9377 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9378 /* Look for the identifier. */
9379 identifier = cp_parser_identifier (parser);
9380 if (identifier == error_mark_node)
9381 return;
9382 /* Look for the `=' token. */
9383 cp_parser_require (parser, CPP_EQ, "`='");
9384 /* Look for the qualified-namespace-specifier. */
9385 namespace_specifier
9386 = cp_parser_qualified_namespace_specifier (parser);
9387 /* Look for the `;' token. */
9388 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9389
9390 /* Register the alias in the symbol table. */
9391 do_namespace_alias (identifier, namespace_specifier);
9392 }
9393
9394 /* Parse a qualified-namespace-specifier.
9395
9396 qualified-namespace-specifier:
9397 :: [opt] nested-name-specifier [opt] namespace-name
9398
9399 Returns a NAMESPACE_DECL corresponding to the specified
9400 namespace. */
9401
9402 static tree
9403 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9404 {
9405 /* Look for the optional `::'. */
9406 cp_parser_global_scope_opt (parser,
9407 /*current_scope_valid_p=*/false);
9408
9409 /* Look for the optional nested-name-specifier. */
9410 cp_parser_nested_name_specifier_opt (parser,
9411 /*typename_keyword_p=*/false,
9412 /*check_dependency_p=*/true,
9413 /*type_p=*/false,
9414 /*is_declaration=*/true);
9415
9416 return cp_parser_namespace_name (parser);
9417 }
9418
9419 /* Parse a using-declaration.
9420
9421 using-declaration:
9422 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9423 using :: unqualified-id ; */
9424
9425 static void
9426 cp_parser_using_declaration (cp_parser* parser)
9427 {
9428 cp_token *token;
9429 bool typename_p = false;
9430 bool global_scope_p;
9431 tree decl;
9432 tree identifier;
9433 tree scope;
9434 tree qscope;
9435
9436 /* Look for the `using' keyword. */
9437 cp_parser_require_keyword (parser, RID_USING, "`using'");
9438
9439 /* Peek at the next token. */
9440 token = cp_lexer_peek_token (parser->lexer);
9441 /* See if it's `typename'. */
9442 if (token->keyword == RID_TYPENAME)
9443 {
9444 /* Remember that we've seen it. */
9445 typename_p = true;
9446 /* Consume the `typename' token. */
9447 cp_lexer_consume_token (parser->lexer);
9448 }
9449
9450 /* Look for the optional global scope qualification. */
9451 global_scope_p
9452 = (cp_parser_global_scope_opt (parser,
9453 /*current_scope_valid_p=*/false)
9454 != NULL_TREE);
9455
9456 /* If we saw `typename', or didn't see `::', then there must be a
9457 nested-name-specifier present. */
9458 if (typename_p || !global_scope_p)
9459 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9460 /*check_dependency_p=*/true,
9461 /*type_p=*/false,
9462 /*is_declaration=*/true);
9463 /* Otherwise, we could be in either of the two productions. In that
9464 case, treat the nested-name-specifier as optional. */
9465 else
9466 qscope = cp_parser_nested_name_specifier_opt (parser,
9467 /*typename_keyword_p=*/false,
9468 /*check_dependency_p=*/true,
9469 /*type_p=*/false,
9470 /*is_declaration=*/true);
9471 if (!qscope)
9472 qscope = global_namespace;
9473
9474 /* Parse the unqualified-id. */
9475 identifier = cp_parser_unqualified_id (parser,
9476 /*template_keyword_p=*/false,
9477 /*check_dependency_p=*/true,
9478 /*declarator_p=*/true);
9479
9480 /* The function we call to handle a using-declaration is different
9481 depending on what scope we are in. */
9482 if (identifier == error_mark_node)
9483 ;
9484 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9485 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9486 /* [namespace.udecl]
9487
9488 A using declaration shall not name a template-id. */
9489 error ("a template-id may not appear in a using-declaration");
9490 else
9491 {
9492 scope = current_scope ();
9493 if (scope && TYPE_P (scope))
9494 {
9495 /* Create the USING_DECL. */
9496 decl = do_class_using_decl (build_nt (SCOPE_REF,
9497 parser->scope,
9498 identifier));
9499 /* Add it to the list of members in this class. */
9500 finish_member_declaration (decl);
9501 }
9502 else
9503 {
9504 decl = cp_parser_lookup_name_simple (parser, identifier);
9505 if (decl == error_mark_node)
9506 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9507 else if (scope)
9508 do_local_using_decl (decl, qscope, identifier);
9509 else
9510 do_toplevel_using_decl (decl, qscope, identifier);
9511 }
9512 }
9513
9514 /* Look for the final `;'. */
9515 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9516 }
9517
9518 /* Parse a using-directive.
9519
9520 using-directive:
9521 using namespace :: [opt] nested-name-specifier [opt]
9522 namespace-name ; */
9523
9524 static void
9525 cp_parser_using_directive (cp_parser* parser)
9526 {
9527 tree namespace_decl;
9528 tree attribs;
9529
9530 /* Look for the `using' keyword. */
9531 cp_parser_require_keyword (parser, RID_USING, "`using'");
9532 /* And the `namespace' keyword. */
9533 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9534 /* Look for the optional `::' operator. */
9535 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9536 /* And the optional nested-name-specifier. */
9537 cp_parser_nested_name_specifier_opt (parser,
9538 /*typename_keyword_p=*/false,
9539 /*check_dependency_p=*/true,
9540 /*type_p=*/false,
9541 /*is_declaration=*/true);
9542 /* Get the namespace being used. */
9543 namespace_decl = cp_parser_namespace_name (parser);
9544 /* And any specified attributes. */
9545 attribs = cp_parser_attributes_opt (parser);
9546 /* Update the symbol table. */
9547 parse_using_directive (namespace_decl, attribs);
9548 /* Look for the final `;'. */
9549 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9550 }
9551
9552 /* Parse an asm-definition.
9553
9554 asm-definition:
9555 asm ( string-literal ) ;
9556
9557 GNU Extension:
9558
9559 asm-definition:
9560 asm volatile [opt] ( string-literal ) ;
9561 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9562 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9563 : asm-operand-list [opt] ) ;
9564 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9565 : asm-operand-list [opt]
9566 : asm-operand-list [opt] ) ; */
9567
9568 static void
9569 cp_parser_asm_definition (cp_parser* parser)
9570 {
9571 cp_token *token;
9572 tree string;
9573 tree outputs = NULL_TREE;
9574 tree inputs = NULL_TREE;
9575 tree clobbers = NULL_TREE;
9576 tree asm_stmt;
9577 bool volatile_p = false;
9578 bool extended_p = false;
9579
9580 /* Look for the `asm' keyword. */
9581 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9582 /* See if the next token is `volatile'. */
9583 if (cp_parser_allow_gnu_extensions_p (parser)
9584 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9585 {
9586 /* Remember that we saw the `volatile' keyword. */
9587 volatile_p = true;
9588 /* Consume the token. */
9589 cp_lexer_consume_token (parser->lexer);
9590 }
9591 /* Look for the opening `('. */
9592 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9593 /* Look for the string. */
9594 token = cp_parser_require (parser, CPP_STRING, "asm body");
9595 if (!token)
9596 return;
9597 string = token->value;
9598 /* If we're allowing GNU extensions, check for the extended assembly
9599 syntax. Unfortunately, the `:' tokens need not be separated by
9600 a space in C, and so, for compatibility, we tolerate that here
9601 too. Doing that means that we have to treat the `::' operator as
9602 two `:' tokens. */
9603 if (cp_parser_allow_gnu_extensions_p (parser)
9604 && at_function_scope_p ()
9605 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9606 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9607 {
9608 bool inputs_p = false;
9609 bool clobbers_p = false;
9610
9611 /* The extended syntax was used. */
9612 extended_p = true;
9613
9614 /* Look for outputs. */
9615 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9616 {
9617 /* Consume the `:'. */
9618 cp_lexer_consume_token (parser->lexer);
9619 /* Parse the output-operands. */
9620 if (cp_lexer_next_token_is_not (parser->lexer,
9621 CPP_COLON)
9622 && cp_lexer_next_token_is_not (parser->lexer,
9623 CPP_SCOPE)
9624 && cp_lexer_next_token_is_not (parser->lexer,
9625 CPP_CLOSE_PAREN))
9626 outputs = cp_parser_asm_operand_list (parser);
9627 }
9628 /* If the next token is `::', there are no outputs, and the
9629 next token is the beginning of the inputs. */
9630 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9631 {
9632 /* Consume the `::' token. */
9633 cp_lexer_consume_token (parser->lexer);
9634 /* The inputs are coming next. */
9635 inputs_p = true;
9636 }
9637
9638 /* Look for inputs. */
9639 if (inputs_p
9640 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9641 {
9642 if (!inputs_p)
9643 /* Consume the `:'. */
9644 cp_lexer_consume_token (parser->lexer);
9645 /* Parse the output-operands. */
9646 if (cp_lexer_next_token_is_not (parser->lexer,
9647 CPP_COLON)
9648 && cp_lexer_next_token_is_not (parser->lexer,
9649 CPP_SCOPE)
9650 && cp_lexer_next_token_is_not (parser->lexer,
9651 CPP_CLOSE_PAREN))
9652 inputs = cp_parser_asm_operand_list (parser);
9653 }
9654 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9655 /* The clobbers are coming next. */
9656 clobbers_p = true;
9657
9658 /* Look for clobbers. */
9659 if (clobbers_p
9660 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9661 {
9662 if (!clobbers_p)
9663 /* Consume the `:'. */
9664 cp_lexer_consume_token (parser->lexer);
9665 /* Parse the clobbers. */
9666 if (cp_lexer_next_token_is_not (parser->lexer,
9667 CPP_CLOSE_PAREN))
9668 clobbers = cp_parser_asm_clobber_list (parser);
9669 }
9670 }
9671 /* Look for the closing `)'. */
9672 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9673 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9674 /*consume_paren=*/true);
9675 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9676
9677 /* Create the ASM_STMT. */
9678 if (at_function_scope_p ())
9679 {
9680 asm_stmt =
9681 finish_asm_stmt (volatile_p
9682 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9683 string, outputs, inputs, clobbers);
9684 /* If the extended syntax was not used, mark the ASM_STMT. */
9685 if (!extended_p)
9686 ASM_INPUT_P (asm_stmt) = 1;
9687 }
9688 else
9689 assemble_asm (string);
9690 }
9691
9692 /* Declarators [gram.dcl.decl] */
9693
9694 /* Parse an init-declarator.
9695
9696 init-declarator:
9697 declarator initializer [opt]
9698
9699 GNU Extension:
9700
9701 init-declarator:
9702 declarator asm-specification [opt] attributes [opt] initializer [opt]
9703
9704 function-definition:
9705 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9706 function-body
9707 decl-specifier-seq [opt] declarator function-try-block
9708
9709 GNU Extension:
9710
9711 function-definition:
9712 __extension__ function-definition
9713
9714 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9715 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9716 then this declarator appears in a class scope. The new DECL created
9717 by this declarator is returned.
9718
9719 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9720 for a function-definition here as well. If the declarator is a
9721 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9722 be TRUE upon return. By that point, the function-definition will
9723 have been completely parsed.
9724
9725 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9726 is FALSE. */
9727
9728 static tree
9729 cp_parser_init_declarator (cp_parser* parser,
9730 tree decl_specifiers,
9731 tree prefix_attributes,
9732 bool function_definition_allowed_p,
9733 bool member_p,
9734 int declares_class_or_enum,
9735 bool* function_definition_p)
9736 {
9737 cp_token *token;
9738 tree declarator;
9739 tree attributes;
9740 tree asm_specification;
9741 tree initializer;
9742 tree decl = NULL_TREE;
9743 tree scope;
9744 bool is_initialized;
9745 bool is_parenthesized_init;
9746 bool is_non_constant_init;
9747 int ctor_dtor_or_conv_p;
9748 bool friend_p;
9749
9750 /* Assume that this is not the declarator for a function
9751 definition. */
9752 if (function_definition_p)
9753 *function_definition_p = false;
9754
9755 /* Defer access checks while parsing the declarator; we cannot know
9756 what names are accessible until we know what is being
9757 declared. */
9758 resume_deferring_access_checks ();
9759
9760 /* Parse the declarator. */
9761 declarator
9762 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9763 &ctor_dtor_or_conv_p,
9764 /*parenthesized_p=*/NULL);
9765 /* Gather up the deferred checks. */
9766 stop_deferring_access_checks ();
9767
9768 /* If the DECLARATOR was erroneous, there's no need to go
9769 further. */
9770 if (declarator == error_mark_node)
9771 return error_mark_node;
9772
9773 cp_parser_check_for_definition_in_return_type (declarator,
9774 declares_class_or_enum);
9775
9776 /* Figure out what scope the entity declared by the DECLARATOR is
9777 located in. `grokdeclarator' sometimes changes the scope, so
9778 we compute it now. */
9779 scope = get_scope_of_declarator (declarator);
9780
9781 /* If we're allowing GNU extensions, look for an asm-specification
9782 and attributes. */
9783 if (cp_parser_allow_gnu_extensions_p (parser))
9784 {
9785 /* Look for an asm-specification. */
9786 asm_specification = cp_parser_asm_specification_opt (parser);
9787 /* And attributes. */
9788 attributes = cp_parser_attributes_opt (parser);
9789 }
9790 else
9791 {
9792 asm_specification = NULL_TREE;
9793 attributes = NULL_TREE;
9794 }
9795
9796 /* Peek at the next token. */
9797 token = cp_lexer_peek_token (parser->lexer);
9798 /* Check to see if the token indicates the start of a
9799 function-definition. */
9800 if (cp_parser_token_starts_function_definition_p (token))
9801 {
9802 if (!function_definition_allowed_p)
9803 {
9804 /* If a function-definition should not appear here, issue an
9805 error message. */
9806 cp_parser_error (parser,
9807 "a function-definition is not allowed here");
9808 return error_mark_node;
9809 }
9810 else
9811 {
9812 /* Neither attributes nor an asm-specification are allowed
9813 on a function-definition. */
9814 if (asm_specification)
9815 error ("an asm-specification is not allowed on a function-definition");
9816 if (attributes)
9817 error ("attributes are not allowed on a function-definition");
9818 /* This is a function-definition. */
9819 *function_definition_p = true;
9820
9821 /* Parse the function definition. */
9822 if (member_p)
9823 decl = cp_parser_save_member_function_body (parser,
9824 decl_specifiers,
9825 declarator,
9826 prefix_attributes);
9827 else
9828 decl
9829 = (cp_parser_function_definition_from_specifiers_and_declarator
9830 (parser, decl_specifiers, prefix_attributes, declarator));
9831
9832 return decl;
9833 }
9834 }
9835
9836 /* [dcl.dcl]
9837
9838 Only in function declarations for constructors, destructors, and
9839 type conversions can the decl-specifier-seq be omitted.
9840
9841 We explicitly postpone this check past the point where we handle
9842 function-definitions because we tolerate function-definitions
9843 that are missing their return types in some modes. */
9844 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9845 {
9846 cp_parser_error (parser,
9847 "expected constructor, destructor, or type conversion");
9848 return error_mark_node;
9849 }
9850
9851 /* An `=' or an `(' indicates an initializer. */
9852 is_initialized = (token->type == CPP_EQ
9853 || token->type == CPP_OPEN_PAREN);
9854 /* If the init-declarator isn't initialized and isn't followed by a
9855 `,' or `;', it's not a valid init-declarator. */
9856 if (!is_initialized
9857 && token->type != CPP_COMMA
9858 && token->type != CPP_SEMICOLON)
9859 {
9860 cp_parser_error (parser, "expected init-declarator");
9861 return error_mark_node;
9862 }
9863
9864 /* Because start_decl has side-effects, we should only call it if we
9865 know we're going ahead. By this point, we know that we cannot
9866 possibly be looking at any other construct. */
9867 cp_parser_commit_to_tentative_parse (parser);
9868
9869 /* If the decl specifiers were bad, issue an error now that we're
9870 sure this was intended to be a declarator. Then continue
9871 declaring the variable(s), as int, to try to cut down on further
9872 errors. */
9873 if (decl_specifiers != NULL
9874 && TREE_VALUE (decl_specifiers) == error_mark_node)
9875 {
9876 cp_parser_error (parser, "invalid type in declaration");
9877 TREE_VALUE (decl_specifiers) = integer_type_node;
9878 }
9879
9880 /* Check to see whether or not this declaration is a friend. */
9881 friend_p = cp_parser_friend_p (decl_specifiers);
9882
9883 /* Check that the number of template-parameter-lists is OK. */
9884 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9885 return error_mark_node;
9886
9887 /* Enter the newly declared entry in the symbol table. If we're
9888 processing a declaration in a class-specifier, we wait until
9889 after processing the initializer. */
9890 if (!member_p)
9891 {
9892 if (parser->in_unbraced_linkage_specification_p)
9893 {
9894 decl_specifiers = tree_cons (error_mark_node,
9895 get_identifier ("extern"),
9896 decl_specifiers);
9897 have_extern_spec = false;
9898 }
9899 decl = start_decl (declarator, decl_specifiers,
9900 is_initialized, attributes, prefix_attributes);
9901 }
9902
9903 /* Enter the SCOPE. That way unqualified names appearing in the
9904 initializer will be looked up in SCOPE. */
9905 if (scope)
9906 push_scope (scope);
9907
9908 /* Perform deferred access control checks, now that we know in which
9909 SCOPE the declared entity resides. */
9910 if (!member_p && decl)
9911 {
9912 tree saved_current_function_decl = NULL_TREE;
9913
9914 /* If the entity being declared is a function, pretend that we
9915 are in its scope. If it is a `friend', it may have access to
9916 things that would not otherwise be accessible. */
9917 if (TREE_CODE (decl) == FUNCTION_DECL)
9918 {
9919 saved_current_function_decl = current_function_decl;
9920 current_function_decl = decl;
9921 }
9922
9923 /* Perform the access control checks for the declarator and the
9924 the decl-specifiers. */
9925 perform_deferred_access_checks ();
9926
9927 /* Restore the saved value. */
9928 if (TREE_CODE (decl) == FUNCTION_DECL)
9929 current_function_decl = saved_current_function_decl;
9930 }
9931
9932 /* Parse the initializer. */
9933 if (is_initialized)
9934 initializer = cp_parser_initializer (parser,
9935 &is_parenthesized_init,
9936 &is_non_constant_init);
9937 else
9938 {
9939 initializer = NULL_TREE;
9940 is_parenthesized_init = false;
9941 is_non_constant_init = true;
9942 }
9943
9944 /* The old parser allows attributes to appear after a parenthesized
9945 initializer. Mark Mitchell proposed removing this functionality
9946 on the GCC mailing lists on 2002-08-13. This parser accepts the
9947 attributes -- but ignores them. */
9948 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9949 if (cp_parser_attributes_opt (parser))
9950 warning ("attributes after parenthesized initializer ignored");
9951
9952 /* Leave the SCOPE, now that we have processed the initializer. It
9953 is important to do this before calling cp_finish_decl because it
9954 makes decisions about whether to create DECL_STMTs or not based
9955 on the current scope. */
9956 if (scope)
9957 pop_scope (scope);
9958
9959 /* For an in-class declaration, use `grokfield' to create the
9960 declaration. */
9961 if (member_p)
9962 {
9963 decl = grokfield (declarator, decl_specifiers,
9964 initializer, /*asmspec=*/NULL_TREE,
9965 /*attributes=*/NULL_TREE);
9966 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9967 cp_parser_save_default_args (parser, decl);
9968 }
9969
9970 /* Finish processing the declaration. But, skip friend
9971 declarations. */
9972 if (!friend_p && decl)
9973 cp_finish_decl (decl,
9974 initializer,
9975 asm_specification,
9976 /* If the initializer is in parentheses, then this is
9977 a direct-initialization, which means that an
9978 `explicit' constructor is OK. Otherwise, an
9979 `explicit' constructor cannot be used. */
9980 ((is_parenthesized_init || !is_initialized)
9981 ? 0 : LOOKUP_ONLYCONVERTING));
9982
9983 /* Remember whether or not variables were initialized by
9984 constant-expressions. */
9985 if (decl && TREE_CODE (decl) == VAR_DECL
9986 && is_initialized && !is_non_constant_init)
9987 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9988
9989 return decl;
9990 }
9991
9992 /* Parse a declarator.
9993
9994 declarator:
9995 direct-declarator
9996 ptr-operator declarator
9997
9998 abstract-declarator:
9999 ptr-operator abstract-declarator [opt]
10000 direct-abstract-declarator
10001
10002 GNU Extensions:
10003
10004 declarator:
10005 attributes [opt] direct-declarator
10006 attributes [opt] ptr-operator declarator
10007
10008 abstract-declarator:
10009 attributes [opt] ptr-operator abstract-declarator [opt]
10010 attributes [opt] direct-abstract-declarator
10011
10012 Returns a representation of the declarator. If the declarator has
10013 the form `* declarator', then an INDIRECT_REF is returned, whose
10014 only operand is the sub-declarator. Analogously, `& declarator' is
10015 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10016 used. The first operand is the TYPE for `X'. The second operand
10017 is an INDIRECT_REF whose operand is the sub-declarator.
10018
10019 Otherwise, the representation is as for a direct-declarator.
10020
10021 (It would be better to define a structure type to represent
10022 declarators, rather than abusing `tree' nodes to represent
10023 declarators. That would be much clearer and save some memory.
10024 There is no reason for declarators to be garbage-collected, for
10025 example; they are created during parser and no longer needed after
10026 `grokdeclarator' has been called.)
10027
10028 For a ptr-operator that has the optional cv-qualifier-seq,
10029 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10030 node.
10031
10032 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10033 detect constructor, destructor or conversion operators. It is set
10034 to -1 if the declarator is a name, and +1 if it is a
10035 function. Otherwise it is set to zero. Usually you just want to
10036 test for >0, but internally the negative value is used.
10037
10038 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10039 a decl-specifier-seq unless it declares a constructor, destructor,
10040 or conversion. It might seem that we could check this condition in
10041 semantic analysis, rather than parsing, but that makes it difficult
10042 to handle something like `f()'. We want to notice that there are
10043 no decl-specifiers, and therefore realize that this is an
10044 expression, not a declaration.)
10045
10046 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10047 the declarator is a direct-declarator of the form "(...)". */
10048
10049 static tree
10050 cp_parser_declarator (cp_parser* parser,
10051 cp_parser_declarator_kind dcl_kind,
10052 int* ctor_dtor_or_conv_p,
10053 bool* parenthesized_p)
10054 {
10055 cp_token *token;
10056 tree declarator;
10057 enum tree_code code;
10058 tree cv_qualifier_seq;
10059 tree class_type;
10060 tree attributes = NULL_TREE;
10061
10062 /* Assume this is not a constructor, destructor, or type-conversion
10063 operator. */
10064 if (ctor_dtor_or_conv_p)
10065 *ctor_dtor_or_conv_p = 0;
10066
10067 if (cp_parser_allow_gnu_extensions_p (parser))
10068 attributes = cp_parser_attributes_opt (parser);
10069
10070 /* Peek at the next token. */
10071 token = cp_lexer_peek_token (parser->lexer);
10072
10073 /* Check for the ptr-operator production. */
10074 cp_parser_parse_tentatively (parser);
10075 /* Parse the ptr-operator. */
10076 code = cp_parser_ptr_operator (parser,
10077 &class_type,
10078 &cv_qualifier_seq);
10079 /* If that worked, then we have a ptr-operator. */
10080 if (cp_parser_parse_definitely (parser))
10081 {
10082 /* If a ptr-operator was found, then this declarator was not
10083 parenthesized. */
10084 if (parenthesized_p)
10085 *parenthesized_p = true;
10086 /* The dependent declarator is optional if we are parsing an
10087 abstract-declarator. */
10088 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10089 cp_parser_parse_tentatively (parser);
10090
10091 /* Parse the dependent declarator. */
10092 declarator = cp_parser_declarator (parser, dcl_kind,
10093 /*ctor_dtor_or_conv_p=*/NULL,
10094 /*parenthesized_p=*/NULL);
10095
10096 /* If we are parsing an abstract-declarator, we must handle the
10097 case where the dependent declarator is absent. */
10098 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10099 && !cp_parser_parse_definitely (parser))
10100 declarator = NULL_TREE;
10101
10102 /* Build the representation of the ptr-operator. */
10103 if (code == INDIRECT_REF)
10104 declarator = make_pointer_declarator (cv_qualifier_seq,
10105 declarator);
10106 else
10107 declarator = make_reference_declarator (cv_qualifier_seq,
10108 declarator);
10109 /* Handle the pointer-to-member case. */
10110 if (class_type)
10111 declarator = build_nt (SCOPE_REF, class_type, declarator);
10112 }
10113 /* Everything else is a direct-declarator. */
10114 else
10115 {
10116 if (parenthesized_p)
10117 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10118 CPP_OPEN_PAREN);
10119 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10120 ctor_dtor_or_conv_p);
10121 }
10122
10123 if (attributes && declarator != error_mark_node)
10124 declarator = tree_cons (attributes, declarator, NULL_TREE);
10125
10126 return declarator;
10127 }
10128
10129 /* Parse a direct-declarator or direct-abstract-declarator.
10130
10131 direct-declarator:
10132 declarator-id
10133 direct-declarator ( parameter-declaration-clause )
10134 cv-qualifier-seq [opt]
10135 exception-specification [opt]
10136 direct-declarator [ constant-expression [opt] ]
10137 ( declarator )
10138
10139 direct-abstract-declarator:
10140 direct-abstract-declarator [opt]
10141 ( parameter-declaration-clause )
10142 cv-qualifier-seq [opt]
10143 exception-specification [opt]
10144 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10145 ( abstract-declarator )
10146
10147 Returns a representation of the declarator. DCL_KIND is
10148 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10149 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10150 we are parsing a direct-declarator. It is
10151 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10152 of ambiguity we prefer an abstract declarator, as per
10153 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10154 cp_parser_declarator.
10155
10156 For the declarator-id production, the representation is as for an
10157 id-expression, except that a qualified name is represented as a
10158 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10159 see the documentation of the FUNCTION_DECLARATOR_* macros for
10160 information about how to find the various declarator components.
10161 An array-declarator is represented as an ARRAY_REF. The
10162 direct-declarator is the first operand; the constant-expression
10163 indicating the size of the array is the second operand. */
10164
10165 static tree
10166 cp_parser_direct_declarator (cp_parser* parser,
10167 cp_parser_declarator_kind dcl_kind,
10168 int* ctor_dtor_or_conv_p)
10169 {
10170 cp_token *token;
10171 tree declarator = NULL_TREE;
10172 tree scope = NULL_TREE;
10173 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10174 bool saved_in_declarator_p = parser->in_declarator_p;
10175 bool first = true;
10176
10177 while (true)
10178 {
10179 /* Peek at the next token. */
10180 token = cp_lexer_peek_token (parser->lexer);
10181 if (token->type == CPP_OPEN_PAREN)
10182 {
10183 /* This is either a parameter-declaration-clause, or a
10184 parenthesized declarator. When we know we are parsing a
10185 named declarator, it must be a parenthesized declarator
10186 if FIRST is true. For instance, `(int)' is a
10187 parameter-declaration-clause, with an omitted
10188 direct-abstract-declarator. But `((*))', is a
10189 parenthesized abstract declarator. Finally, when T is a
10190 template parameter `(T)' is a
10191 parameter-declaration-clause, and not a parenthesized
10192 named declarator.
10193
10194 We first try and parse a parameter-declaration-clause,
10195 and then try a nested declarator (if FIRST is true).
10196
10197 It is not an error for it not to be a
10198 parameter-declaration-clause, even when FIRST is
10199 false. Consider,
10200
10201 int i (int);
10202 int i (3);
10203
10204 The first is the declaration of a function while the
10205 second is a the definition of a variable, including its
10206 initializer.
10207
10208 Having seen only the parenthesis, we cannot know which of
10209 these two alternatives should be selected. Even more
10210 complex are examples like:
10211
10212 int i (int (a));
10213 int i (int (3));
10214
10215 The former is a function-declaration; the latter is a
10216 variable initialization.
10217
10218 Thus again, we try a parameter-declaration-clause, and if
10219 that fails, we back out and return. */
10220
10221 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10222 {
10223 tree params;
10224 unsigned saved_num_template_parameter_lists;
10225
10226 cp_parser_parse_tentatively (parser);
10227
10228 /* Consume the `('. */
10229 cp_lexer_consume_token (parser->lexer);
10230 if (first)
10231 {
10232 /* If this is going to be an abstract declarator, we're
10233 in a declarator and we can't have default args. */
10234 parser->default_arg_ok_p = false;
10235 parser->in_declarator_p = true;
10236 }
10237
10238 /* Inside the function parameter list, surrounding
10239 template-parameter-lists do not apply. */
10240 saved_num_template_parameter_lists
10241 = parser->num_template_parameter_lists;
10242 parser->num_template_parameter_lists = 0;
10243
10244 /* Parse the parameter-declaration-clause. */
10245 params = cp_parser_parameter_declaration_clause (parser);
10246
10247 parser->num_template_parameter_lists
10248 = saved_num_template_parameter_lists;
10249
10250 /* If all went well, parse the cv-qualifier-seq and the
10251 exception-specification. */
10252 if (cp_parser_parse_definitely (parser))
10253 {
10254 tree cv_qualifiers;
10255 tree exception_specification;
10256
10257 if (ctor_dtor_or_conv_p)
10258 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10259 first = false;
10260 /* Consume the `)'. */
10261 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10262
10263 /* Parse the cv-qualifier-seq. */
10264 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10265 /* And the exception-specification. */
10266 exception_specification
10267 = cp_parser_exception_specification_opt (parser);
10268
10269 /* Create the function-declarator. */
10270 declarator = make_call_declarator (declarator,
10271 params,
10272 cv_qualifiers,
10273 exception_specification);
10274 /* Any subsequent parameter lists are to do with
10275 return type, so are not those of the declared
10276 function. */
10277 parser->default_arg_ok_p = false;
10278
10279 /* Repeat the main loop. */
10280 continue;
10281 }
10282 }
10283
10284 /* If this is the first, we can try a parenthesized
10285 declarator. */
10286 if (first)
10287 {
10288 parser->default_arg_ok_p = saved_default_arg_ok_p;
10289 parser->in_declarator_p = saved_in_declarator_p;
10290
10291 /* Consume the `('. */
10292 cp_lexer_consume_token (parser->lexer);
10293 /* Parse the nested declarator. */
10294 declarator
10295 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10296 /*parenthesized_p=*/NULL);
10297 first = false;
10298 /* Expect a `)'. */
10299 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10300 declarator = error_mark_node;
10301 if (declarator == error_mark_node)
10302 break;
10303
10304 goto handle_declarator;
10305 }
10306 /* Otherwise, we must be done. */
10307 else
10308 break;
10309 }
10310 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10311 && token->type == CPP_OPEN_SQUARE)
10312 {
10313 /* Parse an array-declarator. */
10314 tree bounds;
10315
10316 if (ctor_dtor_or_conv_p)
10317 *ctor_dtor_or_conv_p = 0;
10318
10319 first = false;
10320 parser->default_arg_ok_p = false;
10321 parser->in_declarator_p = true;
10322 /* Consume the `['. */
10323 cp_lexer_consume_token (parser->lexer);
10324 /* Peek at the next token. */
10325 token = cp_lexer_peek_token (parser->lexer);
10326 /* If the next token is `]', then there is no
10327 constant-expression. */
10328 if (token->type != CPP_CLOSE_SQUARE)
10329 {
10330 bool non_constant_p;
10331
10332 bounds
10333 = cp_parser_constant_expression (parser,
10334 /*allow_non_constant=*/true,
10335 &non_constant_p);
10336 if (!non_constant_p)
10337 bounds = cp_parser_fold_non_dependent_expr (bounds);
10338 }
10339 else
10340 bounds = NULL_TREE;
10341 /* Look for the closing `]'. */
10342 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10343 {
10344 declarator = error_mark_node;
10345 break;
10346 }
10347
10348 declarator = build_nt (ARRAY_REF, declarator, bounds);
10349 }
10350 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10351 {
10352 /* Parse a declarator-id */
10353 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10354 cp_parser_parse_tentatively (parser);
10355 declarator = cp_parser_declarator_id (parser);
10356 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10357 {
10358 if (!cp_parser_parse_definitely (parser))
10359 declarator = error_mark_node;
10360 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10361 {
10362 cp_parser_error (parser, "expected unqualified-id");
10363 declarator = error_mark_node;
10364 }
10365 }
10366
10367 if (declarator == error_mark_node)
10368 break;
10369
10370 if (TREE_CODE (declarator) == SCOPE_REF
10371 && !current_scope ())
10372 {
10373 tree scope = TREE_OPERAND (declarator, 0);
10374
10375 /* In the declaration of a member of a template class
10376 outside of the class itself, the SCOPE will sometimes
10377 be a TYPENAME_TYPE. For example, given:
10378
10379 template <typename T>
10380 int S<T>::R::i = 3;
10381
10382 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10383 this context, we must resolve S<T>::R to an ordinary
10384 type, rather than a typename type.
10385
10386 The reason we normally avoid resolving TYPENAME_TYPEs
10387 is that a specialization of `S' might render
10388 `S<T>::R' not a type. However, if `S' is
10389 specialized, then this `i' will not be used, so there
10390 is no harm in resolving the types here. */
10391 if (TREE_CODE (scope) == TYPENAME_TYPE)
10392 {
10393 tree type;
10394
10395 /* Resolve the TYPENAME_TYPE. */
10396 type = resolve_typename_type (scope,
10397 /*only_current_p=*/false);
10398 /* If that failed, the declarator is invalid. */
10399 if (type != error_mark_node)
10400 scope = type;
10401 /* Build a new DECLARATOR. */
10402 declarator = build_nt (SCOPE_REF,
10403 scope,
10404 TREE_OPERAND (declarator, 1));
10405 }
10406 }
10407
10408 /* Check to see whether the declarator-id names a constructor,
10409 destructor, or conversion. */
10410 if (declarator && ctor_dtor_or_conv_p
10411 && ((TREE_CODE (declarator) == SCOPE_REF
10412 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10413 || (TREE_CODE (declarator) != SCOPE_REF
10414 && at_class_scope_p ())))
10415 {
10416 tree unqualified_name;
10417 tree class_type;
10418
10419 /* Get the unqualified part of the name. */
10420 if (TREE_CODE (declarator) == SCOPE_REF)
10421 {
10422 class_type = TREE_OPERAND (declarator, 0);
10423 unqualified_name = TREE_OPERAND (declarator, 1);
10424 }
10425 else
10426 {
10427 class_type = current_class_type;
10428 unqualified_name = declarator;
10429 }
10430
10431 /* See if it names ctor, dtor or conv. */
10432 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10433 || IDENTIFIER_TYPENAME_P (unqualified_name)
10434 || constructor_name_p (unqualified_name, class_type))
10435 *ctor_dtor_or_conv_p = -1;
10436 }
10437
10438 handle_declarator:;
10439 scope = get_scope_of_declarator (declarator);
10440 if (scope)
10441 /* Any names that appear after the declarator-id for a member
10442 are looked up in the containing scope. */
10443 push_scope (scope);
10444 parser->in_declarator_p = true;
10445 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10446 || (declarator
10447 && (TREE_CODE (declarator) == SCOPE_REF
10448 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10449 /* Default args are only allowed on function
10450 declarations. */
10451 parser->default_arg_ok_p = saved_default_arg_ok_p;
10452 else
10453 parser->default_arg_ok_p = false;
10454
10455 first = false;
10456 }
10457 /* We're done. */
10458 else
10459 break;
10460 }
10461
10462 /* For an abstract declarator, we might wind up with nothing at this
10463 point. That's an error; the declarator is not optional. */
10464 if (!declarator)
10465 cp_parser_error (parser, "expected declarator");
10466
10467 /* If we entered a scope, we must exit it now. */
10468 if (scope)
10469 pop_scope (scope);
10470
10471 parser->default_arg_ok_p = saved_default_arg_ok_p;
10472 parser->in_declarator_p = saved_in_declarator_p;
10473
10474 return declarator;
10475 }
10476
10477 /* Parse a ptr-operator.
10478
10479 ptr-operator:
10480 * cv-qualifier-seq [opt]
10481 &
10482 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10483
10484 GNU Extension:
10485
10486 ptr-operator:
10487 & cv-qualifier-seq [opt]
10488
10489 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10490 used. Returns ADDR_EXPR if a reference was used. In the
10491 case of a pointer-to-member, *TYPE is filled in with the
10492 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10493 with the cv-qualifier-seq, or NULL_TREE, if there are no
10494 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10495
10496 static enum tree_code
10497 cp_parser_ptr_operator (cp_parser* parser,
10498 tree* type,
10499 tree* cv_qualifier_seq)
10500 {
10501 enum tree_code code = ERROR_MARK;
10502 cp_token *token;
10503
10504 /* Assume that it's not a pointer-to-member. */
10505 *type = NULL_TREE;
10506 /* And that there are no cv-qualifiers. */
10507 *cv_qualifier_seq = NULL_TREE;
10508
10509 /* Peek at the next token. */
10510 token = cp_lexer_peek_token (parser->lexer);
10511 /* If it's a `*' or `&' we have a pointer or reference. */
10512 if (token->type == CPP_MULT || token->type == CPP_AND)
10513 {
10514 /* Remember which ptr-operator we were processing. */
10515 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10516
10517 /* Consume the `*' or `&'. */
10518 cp_lexer_consume_token (parser->lexer);
10519
10520 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10521 `&', if we are allowing GNU extensions. (The only qualifier
10522 that can legally appear after `&' is `restrict', but that is
10523 enforced during semantic analysis. */
10524 if (code == INDIRECT_REF
10525 || cp_parser_allow_gnu_extensions_p (parser))
10526 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10527 }
10528 else
10529 {
10530 /* Try the pointer-to-member case. */
10531 cp_parser_parse_tentatively (parser);
10532 /* Look for the optional `::' operator. */
10533 cp_parser_global_scope_opt (parser,
10534 /*current_scope_valid_p=*/false);
10535 /* Look for the nested-name specifier. */
10536 cp_parser_nested_name_specifier (parser,
10537 /*typename_keyword_p=*/false,
10538 /*check_dependency_p=*/true,
10539 /*type_p=*/false,
10540 /*is_declaration=*/false);
10541 /* If we found it, and the next token is a `*', then we are
10542 indeed looking at a pointer-to-member operator. */
10543 if (!cp_parser_error_occurred (parser)
10544 && cp_parser_require (parser, CPP_MULT, "`*'"))
10545 {
10546 /* The type of which the member is a member is given by the
10547 current SCOPE. */
10548 *type = parser->scope;
10549 /* The next name will not be qualified. */
10550 parser->scope = NULL_TREE;
10551 parser->qualifying_scope = NULL_TREE;
10552 parser->object_scope = NULL_TREE;
10553 /* Indicate that the `*' operator was used. */
10554 code = INDIRECT_REF;
10555 /* Look for the optional cv-qualifier-seq. */
10556 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10557 }
10558 /* If that didn't work we don't have a ptr-operator. */
10559 if (!cp_parser_parse_definitely (parser))
10560 cp_parser_error (parser, "expected ptr-operator");
10561 }
10562
10563 return code;
10564 }
10565
10566 /* Parse an (optional) cv-qualifier-seq.
10567
10568 cv-qualifier-seq:
10569 cv-qualifier cv-qualifier-seq [opt]
10570
10571 Returns a TREE_LIST. The TREE_VALUE of each node is the
10572 representation of a cv-qualifier. */
10573
10574 static tree
10575 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10576 {
10577 tree cv_qualifiers = NULL_TREE;
10578
10579 while (true)
10580 {
10581 tree cv_qualifier;
10582
10583 /* Look for the next cv-qualifier. */
10584 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10585 /* If we didn't find one, we're done. */
10586 if (!cv_qualifier)
10587 break;
10588
10589 /* Add this cv-qualifier to the list. */
10590 cv_qualifiers
10591 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10592 }
10593
10594 /* We built up the list in reverse order. */
10595 return nreverse (cv_qualifiers);
10596 }
10597
10598 /* Parse an (optional) cv-qualifier.
10599
10600 cv-qualifier:
10601 const
10602 volatile
10603
10604 GNU Extension:
10605
10606 cv-qualifier:
10607 __restrict__ */
10608
10609 static tree
10610 cp_parser_cv_qualifier_opt (cp_parser* parser)
10611 {
10612 cp_token *token;
10613 tree cv_qualifier = NULL_TREE;
10614
10615 /* Peek at the next token. */
10616 token = cp_lexer_peek_token (parser->lexer);
10617 /* See if it's a cv-qualifier. */
10618 switch (token->keyword)
10619 {
10620 case RID_CONST:
10621 case RID_VOLATILE:
10622 case RID_RESTRICT:
10623 /* Save the value of the token. */
10624 cv_qualifier = token->value;
10625 /* Consume the token. */
10626 cp_lexer_consume_token (parser->lexer);
10627 break;
10628
10629 default:
10630 break;
10631 }
10632
10633 return cv_qualifier;
10634 }
10635
10636 /* Parse a declarator-id.
10637
10638 declarator-id:
10639 id-expression
10640 :: [opt] nested-name-specifier [opt] type-name
10641
10642 In the `id-expression' case, the value returned is as for
10643 cp_parser_id_expression if the id-expression was an unqualified-id.
10644 If the id-expression was a qualified-id, then a SCOPE_REF is
10645 returned. The first operand is the scope (either a NAMESPACE_DECL
10646 or TREE_TYPE), but the second is still just a representation of an
10647 unqualified-id. */
10648
10649 static tree
10650 cp_parser_declarator_id (cp_parser* parser)
10651 {
10652 tree id_expression;
10653
10654 /* The expression must be an id-expression. Assume that qualified
10655 names are the names of types so that:
10656
10657 template <class T>
10658 int S<T>::R::i = 3;
10659
10660 will work; we must treat `S<T>::R' as the name of a type.
10661 Similarly, assume that qualified names are templates, where
10662 required, so that:
10663
10664 template <class T>
10665 int S<T>::R<T>::i = 3;
10666
10667 will work, too. */
10668 id_expression = cp_parser_id_expression (parser,
10669 /*template_keyword_p=*/false,
10670 /*check_dependency_p=*/false,
10671 /*template_p=*/NULL,
10672 /*declarator_p=*/true);
10673 /* If the name was qualified, create a SCOPE_REF to represent
10674 that. */
10675 if (parser->scope)
10676 {
10677 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10678 parser->scope = NULL_TREE;
10679 }
10680
10681 return id_expression;
10682 }
10683
10684 /* Parse a type-id.
10685
10686 type-id:
10687 type-specifier-seq abstract-declarator [opt]
10688
10689 Returns the TYPE specified. */
10690
10691 static tree
10692 cp_parser_type_id (cp_parser* parser)
10693 {
10694 tree type_specifier_seq;
10695 tree abstract_declarator;
10696
10697 /* Parse the type-specifier-seq. */
10698 type_specifier_seq
10699 = cp_parser_type_specifier_seq (parser);
10700 if (type_specifier_seq == error_mark_node)
10701 return error_mark_node;
10702
10703 /* There might or might not be an abstract declarator. */
10704 cp_parser_parse_tentatively (parser);
10705 /* Look for the declarator. */
10706 abstract_declarator
10707 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10708 /*parenthesized_p=*/NULL);
10709 /* Check to see if there really was a declarator. */
10710 if (!cp_parser_parse_definitely (parser))
10711 abstract_declarator = NULL_TREE;
10712
10713 return groktypename (build_tree_list (type_specifier_seq,
10714 abstract_declarator));
10715 }
10716
10717 /* Parse a type-specifier-seq.
10718
10719 type-specifier-seq:
10720 type-specifier type-specifier-seq [opt]
10721
10722 GNU extension:
10723
10724 type-specifier-seq:
10725 attributes type-specifier-seq [opt]
10726
10727 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10728 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10729
10730 static tree
10731 cp_parser_type_specifier_seq (cp_parser* parser)
10732 {
10733 bool seen_type_specifier = false;
10734 tree type_specifier_seq = NULL_TREE;
10735
10736 /* Parse the type-specifiers and attributes. */
10737 while (true)
10738 {
10739 tree type_specifier;
10740
10741 /* Check for attributes first. */
10742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10743 {
10744 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10745 NULL_TREE,
10746 type_specifier_seq);
10747 continue;
10748 }
10749
10750 /* After the first type-specifier, others are optional. */
10751 if (seen_type_specifier)
10752 cp_parser_parse_tentatively (parser);
10753 /* Look for the type-specifier. */
10754 type_specifier = cp_parser_type_specifier (parser,
10755 CP_PARSER_FLAGS_NONE,
10756 /*is_friend=*/false,
10757 /*is_declaration=*/false,
10758 NULL,
10759 NULL);
10760 /* If the first type-specifier could not be found, this is not a
10761 type-specifier-seq at all. */
10762 if (!seen_type_specifier && type_specifier == error_mark_node)
10763 return error_mark_node;
10764 /* If subsequent type-specifiers could not be found, the
10765 type-specifier-seq is complete. */
10766 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10767 break;
10768
10769 /* Add the new type-specifier to the list. */
10770 type_specifier_seq
10771 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10772 seen_type_specifier = true;
10773 }
10774
10775 /* We built up the list in reverse order. */
10776 return nreverse (type_specifier_seq);
10777 }
10778
10779 /* Parse a parameter-declaration-clause.
10780
10781 parameter-declaration-clause:
10782 parameter-declaration-list [opt] ... [opt]
10783 parameter-declaration-list , ...
10784
10785 Returns a representation for the parameter declarations. Each node
10786 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10787 representation.) If the parameter-declaration-clause ends with an
10788 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10789 list. A return value of NULL_TREE indicates a
10790 parameter-declaration-clause consisting only of an ellipsis. */
10791
10792 static tree
10793 cp_parser_parameter_declaration_clause (cp_parser* parser)
10794 {
10795 tree parameters;
10796 cp_token *token;
10797 bool ellipsis_p;
10798
10799 /* Peek at the next token. */
10800 token = cp_lexer_peek_token (parser->lexer);
10801 /* Check for trivial parameter-declaration-clauses. */
10802 if (token->type == CPP_ELLIPSIS)
10803 {
10804 /* Consume the `...' token. */
10805 cp_lexer_consume_token (parser->lexer);
10806 return NULL_TREE;
10807 }
10808 else if (token->type == CPP_CLOSE_PAREN)
10809 /* There are no parameters. */
10810 {
10811 #ifndef NO_IMPLICIT_EXTERN_C
10812 if (in_system_header && current_class_type == NULL
10813 && current_lang_name == lang_name_c)
10814 return NULL_TREE;
10815 else
10816 #endif
10817 return void_list_node;
10818 }
10819 /* Check for `(void)', too, which is a special case. */
10820 else if (token->keyword == RID_VOID
10821 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10822 == CPP_CLOSE_PAREN))
10823 {
10824 /* Consume the `void' token. */
10825 cp_lexer_consume_token (parser->lexer);
10826 /* There are no parameters. */
10827 return void_list_node;
10828 }
10829
10830 /* Parse the parameter-declaration-list. */
10831 parameters = cp_parser_parameter_declaration_list (parser);
10832 /* If a parse error occurred while parsing the
10833 parameter-declaration-list, then the entire
10834 parameter-declaration-clause is erroneous. */
10835 if (parameters == error_mark_node)
10836 return error_mark_node;
10837
10838 /* Peek at the next token. */
10839 token = cp_lexer_peek_token (parser->lexer);
10840 /* If it's a `,', the clause should terminate with an ellipsis. */
10841 if (token->type == CPP_COMMA)
10842 {
10843 /* Consume the `,'. */
10844 cp_lexer_consume_token (parser->lexer);
10845 /* Expect an ellipsis. */
10846 ellipsis_p
10847 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10848 }
10849 /* It might also be `...' if the optional trailing `,' was
10850 omitted. */
10851 else if (token->type == CPP_ELLIPSIS)
10852 {
10853 /* Consume the `...' token. */
10854 cp_lexer_consume_token (parser->lexer);
10855 /* And remember that we saw it. */
10856 ellipsis_p = true;
10857 }
10858 else
10859 ellipsis_p = false;
10860
10861 /* Finish the parameter list. */
10862 return finish_parmlist (parameters, ellipsis_p);
10863 }
10864
10865 /* Parse a parameter-declaration-list.
10866
10867 parameter-declaration-list:
10868 parameter-declaration
10869 parameter-declaration-list , parameter-declaration
10870
10871 Returns a representation of the parameter-declaration-list, as for
10872 cp_parser_parameter_declaration_clause. However, the
10873 `void_list_node' is never appended to the list. */
10874
10875 static tree
10876 cp_parser_parameter_declaration_list (cp_parser* parser)
10877 {
10878 tree parameters = NULL_TREE;
10879
10880 /* Look for more parameters. */
10881 while (true)
10882 {
10883 tree parameter;
10884 bool parenthesized_p;
10885 /* Parse the parameter. */
10886 parameter
10887 = cp_parser_parameter_declaration (parser,
10888 /*template_parm_p=*/false,
10889 &parenthesized_p);
10890
10891 /* If a parse error occurred parsing the parameter declaration,
10892 then the entire parameter-declaration-list is erroneous. */
10893 if (parameter == error_mark_node)
10894 {
10895 parameters = error_mark_node;
10896 break;
10897 }
10898 /* Add the new parameter to the list. */
10899 TREE_CHAIN (parameter) = parameters;
10900 parameters = parameter;
10901
10902 /* Peek at the next token. */
10903 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10904 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10905 /* The parameter-declaration-list is complete. */
10906 break;
10907 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10908 {
10909 cp_token *token;
10910
10911 /* Peek at the next token. */
10912 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10913 /* If it's an ellipsis, then the list is complete. */
10914 if (token->type == CPP_ELLIPSIS)
10915 break;
10916 /* Otherwise, there must be more parameters. Consume the
10917 `,'. */
10918 cp_lexer_consume_token (parser->lexer);
10919 /* When parsing something like:
10920
10921 int i(float f, double d)
10922
10923 we can tell after seeing the declaration for "f" that we
10924 are not looking at an initialization of a variable "i",
10925 but rather at the declaration of a function "i".
10926
10927 Due to the fact that the parsing of template arguments
10928 (as specified to a template-id) requires backtracking we
10929 cannot use this technique when inside a template argument
10930 list. */
10931 if (!parser->in_template_argument_list_p
10932 && cp_parser_parsing_tentatively (parser)
10933 && !cp_parser_committed_to_tentative_parse (parser)
10934 /* However, a parameter-declaration of the form
10935 "foat(f)" (which is a valid declaration of a
10936 parameter "f") can also be interpreted as an
10937 expression (the conversion of "f" to "float"). */
10938 && !parenthesized_p)
10939 cp_parser_commit_to_tentative_parse (parser);
10940 }
10941 else
10942 {
10943 cp_parser_error (parser, "expected `,' or `...'");
10944 if (!cp_parser_parsing_tentatively (parser)
10945 || cp_parser_committed_to_tentative_parse (parser))
10946 cp_parser_skip_to_closing_parenthesis (parser,
10947 /*recovering=*/true,
10948 /*or_comma=*/false,
10949 /*consume_paren=*/false);
10950 break;
10951 }
10952 }
10953
10954 /* We built up the list in reverse order; straighten it out now. */
10955 return nreverse (parameters);
10956 }
10957
10958 /* Parse a parameter declaration.
10959
10960 parameter-declaration:
10961 decl-specifier-seq declarator
10962 decl-specifier-seq declarator = assignment-expression
10963 decl-specifier-seq abstract-declarator [opt]
10964 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10965
10966 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10967 declares a template parameter. (In that case, a non-nested `>'
10968 token encountered during the parsing of the assignment-expression
10969 is not interpreted as a greater-than operator.)
10970
10971 Returns a TREE_LIST representing the parameter-declaration. The
10972 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10973 there is no default argument. The TREE_VALUE is a representation
10974 of the decl-specifier-seq and declarator. In particular, the
10975 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10976 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10977 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10978 the declarator is of the form "(p)". */
10979
10980 static tree
10981 cp_parser_parameter_declaration (cp_parser *parser,
10982 bool template_parm_p,
10983 bool *parenthesized_p)
10984 {
10985 int declares_class_or_enum;
10986 bool greater_than_is_operator_p;
10987 tree decl_specifiers;
10988 tree attributes;
10989 tree declarator;
10990 tree default_argument;
10991 tree parameter;
10992 cp_token *token;
10993 const char *saved_message;
10994
10995 /* In a template parameter, `>' is not an operator.
10996
10997 [temp.param]
10998
10999 When parsing a default template-argument for a non-type
11000 template-parameter, the first non-nested `>' is taken as the end
11001 of the template parameter-list rather than a greater-than
11002 operator. */
11003 greater_than_is_operator_p = !template_parm_p;
11004
11005 /* Type definitions may not appear in parameter types. */
11006 saved_message = parser->type_definition_forbidden_message;
11007 parser->type_definition_forbidden_message
11008 = "types may not be defined in parameter types";
11009
11010 /* Parse the declaration-specifiers. */
11011 decl_specifiers
11012 = cp_parser_decl_specifier_seq (parser,
11013 CP_PARSER_FLAGS_NONE,
11014 &attributes,
11015 &declares_class_or_enum);
11016 /* If an error occurred, there's no reason to attempt to parse the
11017 rest of the declaration. */
11018 if (cp_parser_error_occurred (parser))
11019 {
11020 parser->type_definition_forbidden_message = saved_message;
11021 return error_mark_node;
11022 }
11023
11024 /* Peek at the next token. */
11025 token = cp_lexer_peek_token (parser->lexer);
11026 /* If the next token is a `)', `,', `=', `>', or `...', then there
11027 is no declarator. */
11028 if (token->type == CPP_CLOSE_PAREN
11029 || token->type == CPP_COMMA
11030 || token->type == CPP_EQ
11031 || token->type == CPP_ELLIPSIS
11032 || token->type == CPP_GREATER)
11033 {
11034 declarator = NULL_TREE;
11035 if (parenthesized_p)
11036 *parenthesized_p = false;
11037 }
11038 /* Otherwise, there should be a declarator. */
11039 else
11040 {
11041 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11042 parser->default_arg_ok_p = false;
11043
11044 /* After seeing a decl-specifier-seq, if the next token is not a
11045 "(", there is no possibility that the code is a valid
11046 expression. Therefore, if parsing tentatively, we commit at
11047 this point. */
11048 if (!parser->in_template_argument_list_p
11049 /* In an expression context, having seen:
11050
11051 (int((char *)...
11052
11053 we cannot be sure whether we are looking at a
11054 function-type (taking a "char*" as a parameter) or a cast
11055 of some object of type "char*" to "int". */
11056 && !parser->in_type_id_in_expr_p
11057 && cp_parser_parsing_tentatively (parser)
11058 && !cp_parser_committed_to_tentative_parse (parser)
11059 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11060 cp_parser_commit_to_tentative_parse (parser);
11061 /* Parse the declarator. */
11062 declarator = cp_parser_declarator (parser,
11063 CP_PARSER_DECLARATOR_EITHER,
11064 /*ctor_dtor_or_conv_p=*/NULL,
11065 parenthesized_p);
11066 parser->default_arg_ok_p = saved_default_arg_ok_p;
11067 /* After the declarator, allow more attributes. */
11068 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11069 }
11070
11071 /* The restriction on defining new types applies only to the type
11072 of the parameter, not to the default argument. */
11073 parser->type_definition_forbidden_message = saved_message;
11074
11075 /* If the next token is `=', then process a default argument. */
11076 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11077 {
11078 bool saved_greater_than_is_operator_p;
11079 /* Consume the `='. */
11080 cp_lexer_consume_token (parser->lexer);
11081
11082 /* If we are defining a class, then the tokens that make up the
11083 default argument must be saved and processed later. */
11084 if (!template_parm_p && at_class_scope_p ()
11085 && TYPE_BEING_DEFINED (current_class_type))
11086 {
11087 unsigned depth = 0;
11088
11089 /* Create a DEFAULT_ARG to represented the unparsed default
11090 argument. */
11091 default_argument = make_node (DEFAULT_ARG);
11092 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11093
11094 /* Add tokens until we have processed the entire default
11095 argument. */
11096 while (true)
11097 {
11098 bool done = false;
11099 cp_token *token;
11100
11101 /* Peek at the next token. */
11102 token = cp_lexer_peek_token (parser->lexer);
11103 /* What we do depends on what token we have. */
11104 switch (token->type)
11105 {
11106 /* In valid code, a default argument must be
11107 immediately followed by a `,' `)', or `...'. */
11108 case CPP_COMMA:
11109 case CPP_CLOSE_PAREN:
11110 case CPP_ELLIPSIS:
11111 /* If we run into a non-nested `;', `}', or `]',
11112 then the code is invalid -- but the default
11113 argument is certainly over. */
11114 case CPP_SEMICOLON:
11115 case CPP_CLOSE_BRACE:
11116 case CPP_CLOSE_SQUARE:
11117 if (depth == 0)
11118 done = true;
11119 /* Update DEPTH, if necessary. */
11120 else if (token->type == CPP_CLOSE_PAREN
11121 || token->type == CPP_CLOSE_BRACE
11122 || token->type == CPP_CLOSE_SQUARE)
11123 --depth;
11124 break;
11125
11126 case CPP_OPEN_PAREN:
11127 case CPP_OPEN_SQUARE:
11128 case CPP_OPEN_BRACE:
11129 ++depth;
11130 break;
11131
11132 case CPP_GREATER:
11133 /* If we see a non-nested `>', and `>' is not an
11134 operator, then it marks the end of the default
11135 argument. */
11136 if (!depth && !greater_than_is_operator_p)
11137 done = true;
11138 break;
11139
11140 /* If we run out of tokens, issue an error message. */
11141 case CPP_EOF:
11142 error ("file ends in default argument");
11143 done = true;
11144 break;
11145
11146 case CPP_NAME:
11147 case CPP_SCOPE:
11148 /* In these cases, we should look for template-ids.
11149 For example, if the default argument is
11150 `X<int, double>()', we need to do name lookup to
11151 figure out whether or not `X' is a template; if
11152 so, the `,' does not end the default argument.
11153
11154 That is not yet done. */
11155 break;
11156
11157 default:
11158 break;
11159 }
11160
11161 /* If we've reached the end, stop. */
11162 if (done)
11163 break;
11164
11165 /* Add the token to the token block. */
11166 token = cp_lexer_consume_token (parser->lexer);
11167 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11168 token);
11169 }
11170 }
11171 /* Outside of a class definition, we can just parse the
11172 assignment-expression. */
11173 else
11174 {
11175 bool saved_local_variables_forbidden_p;
11176
11177 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11178 set correctly. */
11179 saved_greater_than_is_operator_p
11180 = parser->greater_than_is_operator_p;
11181 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11182 /* Local variable names (and the `this' keyword) may not
11183 appear in a default argument. */
11184 saved_local_variables_forbidden_p
11185 = parser->local_variables_forbidden_p;
11186 parser->local_variables_forbidden_p = true;
11187 /* Parse the assignment-expression. */
11188 default_argument = cp_parser_assignment_expression (parser);
11189 /* Restore saved state. */
11190 parser->greater_than_is_operator_p
11191 = saved_greater_than_is_operator_p;
11192 parser->local_variables_forbidden_p
11193 = saved_local_variables_forbidden_p;
11194 }
11195 if (!parser->default_arg_ok_p)
11196 {
11197 if (!flag_pedantic_errors)
11198 warning ("deprecated use of default argument for parameter of non-function");
11199 else
11200 {
11201 error ("default arguments are only permitted for function parameters");
11202 default_argument = NULL_TREE;
11203 }
11204 }
11205 }
11206 else
11207 default_argument = NULL_TREE;
11208
11209 /* Create the representation of the parameter. */
11210 if (attributes)
11211 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11212 parameter = build_tree_list (default_argument,
11213 build_tree_list (decl_specifiers,
11214 declarator));
11215
11216 return parameter;
11217 }
11218
11219 /* Parse a function-body.
11220
11221 function-body:
11222 compound_statement */
11223
11224 static void
11225 cp_parser_function_body (cp_parser *parser)
11226 {
11227 cp_parser_compound_statement (parser, false);
11228 }
11229
11230 /* Parse a ctor-initializer-opt followed by a function-body. Return
11231 true if a ctor-initializer was present. */
11232
11233 static bool
11234 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11235 {
11236 tree body;
11237 bool ctor_initializer_p;
11238
11239 /* Begin the function body. */
11240 body = begin_function_body ();
11241 /* Parse the optional ctor-initializer. */
11242 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11243 /* Parse the function-body. */
11244 cp_parser_function_body (parser);
11245 /* Finish the function body. */
11246 finish_function_body (body);
11247
11248 return ctor_initializer_p;
11249 }
11250
11251 /* Parse an initializer.
11252
11253 initializer:
11254 = initializer-clause
11255 ( expression-list )
11256
11257 Returns a expression representing the initializer. If no
11258 initializer is present, NULL_TREE is returned.
11259
11260 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11261 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11262 set to FALSE if there is no initializer present. If there is an
11263 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11264 is set to true; otherwise it is set to false. */
11265
11266 static tree
11267 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11268 bool* non_constant_p)
11269 {
11270 cp_token *token;
11271 tree init;
11272
11273 /* Peek at the next token. */
11274 token = cp_lexer_peek_token (parser->lexer);
11275
11276 /* Let our caller know whether or not this initializer was
11277 parenthesized. */
11278 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11279 /* Assume that the initializer is constant. */
11280 *non_constant_p = false;
11281
11282 if (token->type == CPP_EQ)
11283 {
11284 /* Consume the `='. */
11285 cp_lexer_consume_token (parser->lexer);
11286 /* Parse the initializer-clause. */
11287 init = cp_parser_initializer_clause (parser, non_constant_p);
11288 }
11289 else if (token->type == CPP_OPEN_PAREN)
11290 init = cp_parser_parenthesized_expression_list (parser, false,
11291 non_constant_p);
11292 else
11293 {
11294 /* Anything else is an error. */
11295 cp_parser_error (parser, "expected initializer");
11296 init = error_mark_node;
11297 }
11298
11299 return init;
11300 }
11301
11302 /* Parse an initializer-clause.
11303
11304 initializer-clause:
11305 assignment-expression
11306 { initializer-list , [opt] }
11307 { }
11308
11309 Returns an expression representing the initializer.
11310
11311 If the `assignment-expression' production is used the value
11312 returned is simply a representation for the expression.
11313
11314 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11315 the elements of the initializer-list (or NULL_TREE, if the last
11316 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11317 NULL_TREE. There is no way to detect whether or not the optional
11318 trailing `,' was provided. NON_CONSTANT_P is as for
11319 cp_parser_initializer. */
11320
11321 static tree
11322 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11323 {
11324 tree initializer;
11325
11326 /* If it is not a `{', then we are looking at an
11327 assignment-expression. */
11328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11329 initializer
11330 = cp_parser_constant_expression (parser,
11331 /*allow_non_constant_p=*/true,
11332 non_constant_p);
11333 else
11334 {
11335 /* Consume the `{' token. */
11336 cp_lexer_consume_token (parser->lexer);
11337 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11338 initializer = make_node (CONSTRUCTOR);
11339 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11340 necessary, but check_initializer depends upon it, for
11341 now. */
11342 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11343 /* If it's not a `}', then there is a non-trivial initializer. */
11344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11345 {
11346 /* Parse the initializer list. */
11347 CONSTRUCTOR_ELTS (initializer)
11348 = cp_parser_initializer_list (parser, non_constant_p);
11349 /* A trailing `,' token is allowed. */
11350 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11351 cp_lexer_consume_token (parser->lexer);
11352 }
11353 /* Now, there should be a trailing `}'. */
11354 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11355 }
11356
11357 return initializer;
11358 }
11359
11360 /* Parse an initializer-list.
11361
11362 initializer-list:
11363 initializer-clause
11364 initializer-list , initializer-clause
11365
11366 GNU Extension:
11367
11368 initializer-list:
11369 identifier : initializer-clause
11370 initializer-list, identifier : initializer-clause
11371
11372 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11373 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11374 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11375 as for cp_parser_initializer. */
11376
11377 static tree
11378 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11379 {
11380 tree initializers = NULL_TREE;
11381
11382 /* Assume all of the expressions are constant. */
11383 *non_constant_p = false;
11384
11385 /* Parse the rest of the list. */
11386 while (true)
11387 {
11388 cp_token *token;
11389 tree identifier;
11390 tree initializer;
11391 bool clause_non_constant_p;
11392
11393 /* If the next token is an identifier and the following one is a
11394 colon, we are looking at the GNU designated-initializer
11395 syntax. */
11396 if (cp_parser_allow_gnu_extensions_p (parser)
11397 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11398 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11399 {
11400 /* Consume the identifier. */
11401 identifier = cp_lexer_consume_token (parser->lexer)->value;
11402 /* Consume the `:'. */
11403 cp_lexer_consume_token (parser->lexer);
11404 }
11405 else
11406 identifier = NULL_TREE;
11407
11408 /* Parse the initializer. */
11409 initializer = cp_parser_initializer_clause (parser,
11410 &clause_non_constant_p);
11411 /* If any clause is non-constant, so is the entire initializer. */
11412 if (clause_non_constant_p)
11413 *non_constant_p = true;
11414 /* Add it to the list. */
11415 initializers = tree_cons (identifier, initializer, initializers);
11416
11417 /* If the next token is not a comma, we have reached the end of
11418 the list. */
11419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11420 break;
11421
11422 /* Peek at the next token. */
11423 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11424 /* If the next token is a `}', then we're still done. An
11425 initializer-clause can have a trailing `,' after the
11426 initializer-list and before the closing `}'. */
11427 if (token->type == CPP_CLOSE_BRACE)
11428 break;
11429
11430 /* Consume the `,' token. */
11431 cp_lexer_consume_token (parser->lexer);
11432 }
11433
11434 /* The initializers were built up in reverse order, so we need to
11435 reverse them now. */
11436 return nreverse (initializers);
11437 }
11438
11439 /* Classes [gram.class] */
11440
11441 /* Parse a class-name.
11442
11443 class-name:
11444 identifier
11445 template-id
11446
11447 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11448 to indicate that names looked up in dependent types should be
11449 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11450 keyword has been used to indicate that the name that appears next
11451 is a template. TYPE_P is true iff the next name should be treated
11452 as class-name, even if it is declared to be some other kind of name
11453 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11454 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11455 being defined in a class-head.
11456
11457 Returns the TYPE_DECL representing the class. */
11458
11459 static tree
11460 cp_parser_class_name (cp_parser *parser,
11461 bool typename_keyword_p,
11462 bool template_keyword_p,
11463 bool type_p,
11464 bool check_dependency_p,
11465 bool class_head_p,
11466 bool is_declaration)
11467 {
11468 tree decl;
11469 tree scope;
11470 bool typename_p;
11471 cp_token *token;
11472
11473 /* All class-names start with an identifier. */
11474 token = cp_lexer_peek_token (parser->lexer);
11475 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11476 {
11477 cp_parser_error (parser, "expected class-name");
11478 return error_mark_node;
11479 }
11480
11481 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11482 to a template-id, so we save it here. */
11483 scope = parser->scope;
11484 if (scope == error_mark_node)
11485 return error_mark_node;
11486
11487 /* Any name names a type if we're following the `typename' keyword
11488 in a qualified name where the enclosing scope is type-dependent. */
11489 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11490 && dependent_type_p (scope));
11491 /* Handle the common case (an identifier, but not a template-id)
11492 efficiently. */
11493 if (token->type == CPP_NAME
11494 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11495 {
11496 tree identifier;
11497
11498 /* Look for the identifier. */
11499 identifier = cp_parser_identifier (parser);
11500 /* If the next token isn't an identifier, we are certainly not
11501 looking at a class-name. */
11502 if (identifier == error_mark_node)
11503 decl = error_mark_node;
11504 /* If we know this is a type-name, there's no need to look it
11505 up. */
11506 else if (typename_p)
11507 decl = identifier;
11508 else
11509 {
11510 /* If the next token is a `::', then the name must be a type
11511 name.
11512
11513 [basic.lookup.qual]
11514
11515 During the lookup for a name preceding the :: scope
11516 resolution operator, object, function, and enumerator
11517 names are ignored. */
11518 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11519 type_p = true;
11520 /* Look up the name. */
11521 decl = cp_parser_lookup_name (parser, identifier,
11522 type_p,
11523 /*is_template=*/false,
11524 /*is_namespace=*/false,
11525 check_dependency_p);
11526 }
11527 }
11528 else
11529 {
11530 /* Try a template-id. */
11531 decl = cp_parser_template_id (parser, template_keyword_p,
11532 check_dependency_p,
11533 is_declaration);
11534 if (decl == error_mark_node)
11535 return error_mark_node;
11536 }
11537
11538 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11539
11540 /* If this is a typename, create a TYPENAME_TYPE. */
11541 if (typename_p && decl != error_mark_node)
11542 decl = TYPE_NAME (make_typename_type (scope, decl,
11543 /*complain=*/1));
11544
11545 /* Check to see that it is really the name of a class. */
11546 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11547 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11548 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11549 /* Situations like this:
11550
11551 template <typename T> struct A {
11552 typename T::template X<int>::I i;
11553 };
11554
11555 are problematic. Is `T::template X<int>' a class-name? The
11556 standard does not seem to be definitive, but there is no other
11557 valid interpretation of the following `::'. Therefore, those
11558 names are considered class-names. */
11559 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11560 else if (decl == error_mark_node
11561 || TREE_CODE (decl) != TYPE_DECL
11562 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11563 {
11564 cp_parser_error (parser, "expected class-name");
11565 return error_mark_node;
11566 }
11567
11568 return decl;
11569 }
11570
11571 /* Parse a class-specifier.
11572
11573 class-specifier:
11574 class-head { member-specification [opt] }
11575
11576 Returns the TREE_TYPE representing the class. */
11577
11578 static tree
11579 cp_parser_class_specifier (cp_parser* parser)
11580 {
11581 cp_token *token;
11582 tree type;
11583 tree attributes = NULL_TREE;
11584 int has_trailing_semicolon;
11585 bool nested_name_specifier_p;
11586 unsigned saved_num_template_parameter_lists;
11587
11588 push_deferring_access_checks (dk_no_deferred);
11589
11590 /* Parse the class-head. */
11591 type = cp_parser_class_head (parser,
11592 &nested_name_specifier_p);
11593 /* If the class-head was a semantic disaster, skip the entire body
11594 of the class. */
11595 if (!type)
11596 {
11597 cp_parser_skip_to_end_of_block_or_statement (parser);
11598 pop_deferring_access_checks ();
11599 return error_mark_node;
11600 }
11601
11602 /* Look for the `{'. */
11603 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11604 {
11605 pop_deferring_access_checks ();
11606 return error_mark_node;
11607 }
11608
11609 /* Issue an error message if type-definitions are forbidden here. */
11610 cp_parser_check_type_definition (parser);
11611 /* Remember that we are defining one more class. */
11612 ++parser->num_classes_being_defined;
11613 /* Inside the class, surrounding template-parameter-lists do not
11614 apply. */
11615 saved_num_template_parameter_lists
11616 = parser->num_template_parameter_lists;
11617 parser->num_template_parameter_lists = 0;
11618
11619 /* Start the class. */
11620 if (nested_name_specifier_p)
11621 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11622 type = begin_class_definition (type);
11623 if (type == error_mark_node)
11624 /* If the type is erroneous, skip the entire body of the class. */
11625 cp_parser_skip_to_closing_brace (parser);
11626 else
11627 /* Parse the member-specification. */
11628 cp_parser_member_specification_opt (parser);
11629 /* Look for the trailing `}'. */
11630 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11631 /* We get better error messages by noticing a common problem: a
11632 missing trailing `;'. */
11633 token = cp_lexer_peek_token (parser->lexer);
11634 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11635 /* Look for attributes to apply to this class. */
11636 if (cp_parser_allow_gnu_extensions_p (parser))
11637 attributes = cp_parser_attributes_opt (parser);
11638 /* If we got any attributes in class_head, xref_tag will stick them in
11639 TREE_TYPE of the type. Grab them now. */
11640 if (type != error_mark_node)
11641 {
11642 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11643 TYPE_ATTRIBUTES (type) = NULL_TREE;
11644 type = finish_struct (type, attributes);
11645 }
11646 if (nested_name_specifier_p)
11647 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11648 /* If this class is not itself within the scope of another class,
11649 then we need to parse the bodies of all of the queued function
11650 definitions. Note that the queued functions defined in a class
11651 are not always processed immediately following the
11652 class-specifier for that class. Consider:
11653
11654 struct A {
11655 struct B { void f() { sizeof (A); } };
11656 };
11657
11658 If `f' were processed before the processing of `A' were
11659 completed, there would be no way to compute the size of `A'.
11660 Note that the nesting we are interested in here is lexical --
11661 not the semantic nesting given by TYPE_CONTEXT. In particular,
11662 for:
11663
11664 struct A { struct B; };
11665 struct A::B { void f() { } };
11666
11667 there is no need to delay the parsing of `A::B::f'. */
11668 if (--parser->num_classes_being_defined == 0)
11669 {
11670 tree queue_entry;
11671 tree fn;
11672
11673 /* In a first pass, parse default arguments to the functions.
11674 Then, in a second pass, parse the bodies of the functions.
11675 This two-phased approach handles cases like:
11676
11677 struct S {
11678 void f() { g(); }
11679 void g(int i = 3);
11680 };
11681
11682 */
11683 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11684 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11685 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11686 TREE_PURPOSE (parser->unparsed_functions_queues)
11687 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11688 {
11689 fn = TREE_VALUE (queue_entry);
11690 /* Make sure that any template parameters are in scope. */
11691 maybe_begin_member_template_processing (fn);
11692 /* If there are default arguments that have not yet been processed,
11693 take care of them now. */
11694 cp_parser_late_parsing_default_args (parser, fn);
11695 /* Remove any template parameters from the symbol table. */
11696 maybe_end_member_template_processing ();
11697 }
11698 /* Now parse the body of the functions. */
11699 for (TREE_VALUE (parser->unparsed_functions_queues)
11700 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11701 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11702 TREE_VALUE (parser->unparsed_functions_queues)
11703 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11704 {
11705 /* Figure out which function we need to process. */
11706 fn = TREE_VALUE (queue_entry);
11707
11708 /* A hack to prevent garbage collection. */
11709 function_depth++;
11710
11711 /* Parse the function. */
11712 cp_parser_late_parsing_for_member (parser, fn);
11713 function_depth--;
11714 }
11715
11716 }
11717
11718 /* Put back any saved access checks. */
11719 pop_deferring_access_checks ();
11720
11721 /* Restore the count of active template-parameter-lists. */
11722 parser->num_template_parameter_lists
11723 = saved_num_template_parameter_lists;
11724
11725 return type;
11726 }
11727
11728 /* Parse a class-head.
11729
11730 class-head:
11731 class-key identifier [opt] base-clause [opt]
11732 class-key nested-name-specifier identifier base-clause [opt]
11733 class-key nested-name-specifier [opt] template-id
11734 base-clause [opt]
11735
11736 GNU Extensions:
11737 class-key attributes identifier [opt] base-clause [opt]
11738 class-key attributes nested-name-specifier identifier base-clause [opt]
11739 class-key attributes nested-name-specifier [opt] template-id
11740 base-clause [opt]
11741
11742 Returns the TYPE of the indicated class. Sets
11743 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11744 involving a nested-name-specifier was used, and FALSE otherwise.
11745
11746 Returns NULL_TREE if the class-head is syntactically valid, but
11747 semantically invalid in a way that means we should skip the entire
11748 body of the class. */
11749
11750 static tree
11751 cp_parser_class_head (cp_parser* parser,
11752 bool* nested_name_specifier_p)
11753 {
11754 cp_token *token;
11755 tree nested_name_specifier;
11756 enum tag_types class_key;
11757 tree id = NULL_TREE;
11758 tree type = NULL_TREE;
11759 tree attributes;
11760 bool template_id_p = false;
11761 bool qualified_p = false;
11762 bool invalid_nested_name_p = false;
11763 bool invalid_explicit_specialization_p = false;
11764 unsigned num_templates;
11765
11766 /* Assume no nested-name-specifier will be present. */
11767 *nested_name_specifier_p = false;
11768 /* Assume no template parameter lists will be used in defining the
11769 type. */
11770 num_templates = 0;
11771
11772 /* Look for the class-key. */
11773 class_key = cp_parser_class_key (parser);
11774 if (class_key == none_type)
11775 return error_mark_node;
11776
11777 /* Parse the attributes. */
11778 attributes = cp_parser_attributes_opt (parser);
11779
11780 /* If the next token is `::', that is invalid -- but sometimes
11781 people do try to write:
11782
11783 struct ::S {};
11784
11785 Handle this gracefully by accepting the extra qualifier, and then
11786 issuing an error about it later if this really is a
11787 class-head. If it turns out just to be an elaborated type
11788 specifier, remain silent. */
11789 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11790 qualified_p = true;
11791
11792 push_deferring_access_checks (dk_no_check);
11793
11794 /* Determine the name of the class. Begin by looking for an
11795 optional nested-name-specifier. */
11796 nested_name_specifier
11797 = cp_parser_nested_name_specifier_opt (parser,
11798 /*typename_keyword_p=*/false,
11799 /*check_dependency_p=*/false,
11800 /*type_p=*/false,
11801 /*is_declaration=*/false);
11802 /* If there was a nested-name-specifier, then there *must* be an
11803 identifier. */
11804 if (nested_name_specifier)
11805 {
11806 /* Although the grammar says `identifier', it really means
11807 `class-name' or `template-name'. You are only allowed to
11808 define a class that has already been declared with this
11809 syntax.
11810
11811 The proposed resolution for Core Issue 180 says that whever
11812 you see `class T::X' you should treat `X' as a type-name.
11813
11814 It is OK to define an inaccessible class; for example:
11815
11816 class A { class B; };
11817 class A::B {};
11818
11819 We do not know if we will see a class-name, or a
11820 template-name. We look for a class-name first, in case the
11821 class-name is a template-id; if we looked for the
11822 template-name first we would stop after the template-name. */
11823 cp_parser_parse_tentatively (parser);
11824 type = cp_parser_class_name (parser,
11825 /*typename_keyword_p=*/false,
11826 /*template_keyword_p=*/false,
11827 /*type_p=*/true,
11828 /*check_dependency_p=*/false,
11829 /*class_head_p=*/true,
11830 /*is_declaration=*/false);
11831 /* If that didn't work, ignore the nested-name-specifier. */
11832 if (!cp_parser_parse_definitely (parser))
11833 {
11834 invalid_nested_name_p = true;
11835 id = cp_parser_identifier (parser);
11836 if (id == error_mark_node)
11837 id = NULL_TREE;
11838 }
11839 /* If we could not find a corresponding TYPE, treat this
11840 declaration like an unqualified declaration. */
11841 if (type == error_mark_node)
11842 nested_name_specifier = NULL_TREE;
11843 /* Otherwise, count the number of templates used in TYPE and its
11844 containing scopes. */
11845 else
11846 {
11847 tree scope;
11848
11849 for (scope = TREE_TYPE (type);
11850 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11851 scope = (TYPE_P (scope)
11852 ? TYPE_CONTEXT (scope)
11853 : DECL_CONTEXT (scope)))
11854 if (TYPE_P (scope)
11855 && CLASS_TYPE_P (scope)
11856 && CLASSTYPE_TEMPLATE_INFO (scope)
11857 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11858 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11859 ++num_templates;
11860 }
11861 }
11862 /* Otherwise, the identifier is optional. */
11863 else
11864 {
11865 /* We don't know whether what comes next is a template-id,
11866 an identifier, or nothing at all. */
11867 cp_parser_parse_tentatively (parser);
11868 /* Check for a template-id. */
11869 id = cp_parser_template_id (parser,
11870 /*template_keyword_p=*/false,
11871 /*check_dependency_p=*/true,
11872 /*is_declaration=*/true);
11873 /* If that didn't work, it could still be an identifier. */
11874 if (!cp_parser_parse_definitely (parser))
11875 {
11876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11877 id = cp_parser_identifier (parser);
11878 else
11879 id = NULL_TREE;
11880 }
11881 else
11882 {
11883 template_id_p = true;
11884 ++num_templates;
11885 }
11886 }
11887
11888 pop_deferring_access_checks ();
11889
11890 cp_parser_check_for_invalid_template_id (parser, id);
11891
11892 /* If it's not a `:' or a `{' then we can't really be looking at a
11893 class-head, since a class-head only appears as part of a
11894 class-specifier. We have to detect this situation before calling
11895 xref_tag, since that has irreversible side-effects. */
11896 if (!cp_parser_next_token_starts_class_definition_p (parser))
11897 {
11898 cp_parser_error (parser, "expected `{' or `:'");
11899 return error_mark_node;
11900 }
11901
11902 /* At this point, we're going ahead with the class-specifier, even
11903 if some other problem occurs. */
11904 cp_parser_commit_to_tentative_parse (parser);
11905 /* Issue the error about the overly-qualified name now. */
11906 if (qualified_p)
11907 cp_parser_error (parser,
11908 "global qualification of class name is invalid");
11909 else if (invalid_nested_name_p)
11910 cp_parser_error (parser,
11911 "qualified name does not name a class");
11912 else if (nested_name_specifier)
11913 {
11914 tree scope;
11915 /* Figure out in what scope the declaration is being placed. */
11916 scope = current_scope ();
11917 if (!scope)
11918 scope = current_namespace;
11919 /* If that scope does not contain the scope in which the
11920 class was originally declared, the program is invalid. */
11921 if (scope && !is_ancestor (scope, nested_name_specifier))
11922 {
11923 error ("declaration of `%D' in `%D' which does not "
11924 "enclose `%D'", type, scope, nested_name_specifier);
11925 type = NULL_TREE;
11926 goto done;
11927 }
11928 /* [dcl.meaning]
11929
11930 A declarator-id shall not be qualified exception of the
11931 definition of a ... nested class outside of its class
11932 ... [or] a the definition or explicit instantiation of a
11933 class member of a namespace outside of its namespace. */
11934 if (scope == nested_name_specifier)
11935 {
11936 pedwarn ("extra qualification ignored");
11937 nested_name_specifier = NULL_TREE;
11938 num_templates = 0;
11939 }
11940 }
11941 /* An explicit-specialization must be preceded by "template <>". If
11942 it is not, try to recover gracefully. */
11943 if (at_namespace_scope_p ()
11944 && parser->num_template_parameter_lists == 0
11945 && template_id_p)
11946 {
11947 error ("an explicit specialization must be preceded by 'template <>'");
11948 invalid_explicit_specialization_p = true;
11949 /* Take the same action that would have been taken by
11950 cp_parser_explicit_specialization. */
11951 ++parser->num_template_parameter_lists;
11952 begin_specialization ();
11953 }
11954 /* There must be no "return" statements between this point and the
11955 end of this function; set "type "to the correct return value and
11956 use "goto done;" to return. */
11957 /* Make sure that the right number of template parameters were
11958 present. */
11959 if (!cp_parser_check_template_parameters (parser, num_templates))
11960 {
11961 /* If something went wrong, there is no point in even trying to
11962 process the class-definition. */
11963 type = NULL_TREE;
11964 goto done;
11965 }
11966
11967 /* Look up the type. */
11968 if (template_id_p)
11969 {
11970 type = TREE_TYPE (id);
11971 maybe_process_partial_specialization (type);
11972 }
11973 else if (!nested_name_specifier)
11974 {
11975 /* If the class was unnamed, create a dummy name. */
11976 if (!id)
11977 id = make_anon_name ();
11978 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11979 parser->num_template_parameter_lists);
11980 }
11981 else
11982 {
11983 tree class_type;
11984
11985 /* Given:
11986
11987 template <typename T> struct S { struct T };
11988 template <typename T> struct S<T>::T { };
11989
11990 we will get a TYPENAME_TYPE when processing the definition of
11991 `S::T'. We need to resolve it to the actual type before we
11992 try to define it. */
11993 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11994 {
11995 class_type = resolve_typename_type (TREE_TYPE (type),
11996 /*only_current_p=*/false);
11997 if (class_type != error_mark_node)
11998 type = TYPE_NAME (class_type);
11999 else
12000 {
12001 cp_parser_error (parser, "could not resolve typename type");
12002 type = error_mark_node;
12003 }
12004 }
12005
12006 maybe_process_partial_specialization (TREE_TYPE (type));
12007 class_type = current_class_type;
12008 /* Enter the scope indicated by the nested-name-specifier. */
12009 if (nested_name_specifier)
12010 push_scope (nested_name_specifier);
12011 /* Get the canonical version of this type. */
12012 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12013 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12014 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12015 type = push_template_decl (type);
12016 type = TREE_TYPE (type);
12017 if (nested_name_specifier)
12018 {
12019 *nested_name_specifier_p = true;
12020 pop_scope (nested_name_specifier);
12021 }
12022 }
12023 /* Indicate whether this class was declared as a `class' or as a
12024 `struct'. */
12025 if (TREE_CODE (type) == RECORD_TYPE)
12026 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12027 cp_parser_check_class_key (class_key, type);
12028
12029 /* Enter the scope containing the class; the names of base classes
12030 should be looked up in that context. For example, given:
12031
12032 struct A { struct B {}; struct C; };
12033 struct A::C : B {};
12034
12035 is valid. */
12036 if (nested_name_specifier)
12037 push_scope (nested_name_specifier);
12038 /* Now, look for the base-clause. */
12039 token = cp_lexer_peek_token (parser->lexer);
12040 if (token->type == CPP_COLON)
12041 {
12042 tree bases;
12043
12044 /* Get the list of base-classes. */
12045 bases = cp_parser_base_clause (parser);
12046 /* Process them. */
12047 xref_basetypes (type, bases);
12048 }
12049 /* Leave the scope given by the nested-name-specifier. We will
12050 enter the class scope itself while processing the members. */
12051 if (nested_name_specifier)
12052 pop_scope (nested_name_specifier);
12053
12054 done:
12055 if (invalid_explicit_specialization_p)
12056 {
12057 end_specialization ();
12058 --parser->num_template_parameter_lists;
12059 }
12060 return type;
12061 }
12062
12063 /* Parse a class-key.
12064
12065 class-key:
12066 class
12067 struct
12068 union
12069
12070 Returns the kind of class-key specified, or none_type to indicate
12071 error. */
12072
12073 static enum tag_types
12074 cp_parser_class_key (cp_parser* parser)
12075 {
12076 cp_token *token;
12077 enum tag_types tag_type;
12078
12079 /* Look for the class-key. */
12080 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12081 if (!token)
12082 return none_type;
12083
12084 /* Check to see if the TOKEN is a class-key. */
12085 tag_type = cp_parser_token_is_class_key (token);
12086 if (!tag_type)
12087 cp_parser_error (parser, "expected class-key");
12088 return tag_type;
12089 }
12090
12091 /* Parse an (optional) member-specification.
12092
12093 member-specification:
12094 member-declaration member-specification [opt]
12095 access-specifier : member-specification [opt] */
12096
12097 static void
12098 cp_parser_member_specification_opt (cp_parser* parser)
12099 {
12100 while (true)
12101 {
12102 cp_token *token;
12103 enum rid keyword;
12104
12105 /* Peek at the next token. */
12106 token = cp_lexer_peek_token (parser->lexer);
12107 /* If it's a `}', or EOF then we've seen all the members. */
12108 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12109 break;
12110
12111 /* See if this token is a keyword. */
12112 keyword = token->keyword;
12113 switch (keyword)
12114 {
12115 case RID_PUBLIC:
12116 case RID_PROTECTED:
12117 case RID_PRIVATE:
12118 /* Consume the access-specifier. */
12119 cp_lexer_consume_token (parser->lexer);
12120 /* Remember which access-specifier is active. */
12121 current_access_specifier = token->value;
12122 /* Look for the `:'. */
12123 cp_parser_require (parser, CPP_COLON, "`:'");
12124 break;
12125
12126 default:
12127 /* Otherwise, the next construction must be a
12128 member-declaration. */
12129 cp_parser_member_declaration (parser);
12130 }
12131 }
12132 }
12133
12134 /* Parse a member-declaration.
12135
12136 member-declaration:
12137 decl-specifier-seq [opt] member-declarator-list [opt] ;
12138 function-definition ; [opt]
12139 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12140 using-declaration
12141 template-declaration
12142
12143 member-declarator-list:
12144 member-declarator
12145 member-declarator-list , member-declarator
12146
12147 member-declarator:
12148 declarator pure-specifier [opt]
12149 declarator constant-initializer [opt]
12150 identifier [opt] : constant-expression
12151
12152 GNU Extensions:
12153
12154 member-declaration:
12155 __extension__ member-declaration
12156
12157 member-declarator:
12158 declarator attributes [opt] pure-specifier [opt]
12159 declarator attributes [opt] constant-initializer [opt]
12160 identifier [opt] attributes [opt] : constant-expression */
12161
12162 static void
12163 cp_parser_member_declaration (cp_parser* parser)
12164 {
12165 tree decl_specifiers;
12166 tree prefix_attributes;
12167 tree decl;
12168 int declares_class_or_enum;
12169 bool friend_p;
12170 cp_token *token;
12171 int saved_pedantic;
12172
12173 /* Check for the `__extension__' keyword. */
12174 if (cp_parser_extension_opt (parser, &saved_pedantic))
12175 {
12176 /* Recurse. */
12177 cp_parser_member_declaration (parser);
12178 /* Restore the old value of the PEDANTIC flag. */
12179 pedantic = saved_pedantic;
12180
12181 return;
12182 }
12183
12184 /* Check for a template-declaration. */
12185 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12186 {
12187 /* Parse the template-declaration. */
12188 cp_parser_template_declaration (parser, /*member_p=*/true);
12189
12190 return;
12191 }
12192
12193 /* Check for a using-declaration. */
12194 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12195 {
12196 /* Parse the using-declaration. */
12197 cp_parser_using_declaration (parser);
12198
12199 return;
12200 }
12201
12202 /* Parse the decl-specifier-seq. */
12203 decl_specifiers
12204 = cp_parser_decl_specifier_seq (parser,
12205 CP_PARSER_FLAGS_OPTIONAL,
12206 &prefix_attributes,
12207 &declares_class_or_enum);
12208 /* Check for an invalid type-name. */
12209 if (cp_parser_diagnose_invalid_type_name (parser))
12210 return;
12211 /* If there is no declarator, then the decl-specifier-seq should
12212 specify a type. */
12213 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12214 {
12215 /* If there was no decl-specifier-seq, and the next token is a
12216 `;', then we have something like:
12217
12218 struct S { ; };
12219
12220 [class.mem]
12221
12222 Each member-declaration shall declare at least one member
12223 name of the class. */
12224 if (!decl_specifiers)
12225 {
12226 if (pedantic)
12227 pedwarn ("extra semicolon");
12228 }
12229 else
12230 {
12231 tree type;
12232
12233 /* See if this declaration is a friend. */
12234 friend_p = cp_parser_friend_p (decl_specifiers);
12235 /* If there were decl-specifiers, check to see if there was
12236 a class-declaration. */
12237 type = check_tag_decl (decl_specifiers);
12238 /* Nested classes have already been added to the class, but
12239 a `friend' needs to be explicitly registered. */
12240 if (friend_p)
12241 {
12242 /* If the `friend' keyword was present, the friend must
12243 be introduced with a class-key. */
12244 if (!declares_class_or_enum)
12245 error ("a class-key must be used when declaring a friend");
12246 /* In this case:
12247
12248 template <typename T> struct A {
12249 friend struct A<T>::B;
12250 };
12251
12252 A<T>::B will be represented by a TYPENAME_TYPE, and
12253 therefore not recognized by check_tag_decl. */
12254 if (!type)
12255 {
12256 tree specifier;
12257
12258 for (specifier = decl_specifiers;
12259 specifier;
12260 specifier = TREE_CHAIN (specifier))
12261 {
12262 tree s = TREE_VALUE (specifier);
12263
12264 if (TREE_CODE (s) == IDENTIFIER_NODE)
12265 get_global_value_if_present (s, &type);
12266 if (TREE_CODE (s) == TYPE_DECL)
12267 s = TREE_TYPE (s);
12268 if (TYPE_P (s))
12269 {
12270 type = s;
12271 break;
12272 }
12273 }
12274 }
12275 if (!type || !TYPE_P (type))
12276 error ("friend declaration does not name a class or "
12277 "function");
12278 else
12279 make_friend_class (current_class_type, type,
12280 /*complain=*/true);
12281 }
12282 /* If there is no TYPE, an error message will already have
12283 been issued. */
12284 else if (!type)
12285 ;
12286 /* An anonymous aggregate has to be handled specially; such
12287 a declaration really declares a data member (with a
12288 particular type), as opposed to a nested class. */
12289 else if (ANON_AGGR_TYPE_P (type))
12290 {
12291 /* Remove constructors and such from TYPE, now that we
12292 know it is an anonymous aggregate. */
12293 fixup_anonymous_aggr (type);
12294 /* And make the corresponding data member. */
12295 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12296 /* Add it to the class. */
12297 finish_member_declaration (decl);
12298 }
12299 else
12300 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12301 }
12302 }
12303 else
12304 {
12305 /* See if these declarations will be friends. */
12306 friend_p = cp_parser_friend_p (decl_specifiers);
12307
12308 /* Keep going until we hit the `;' at the end of the
12309 declaration. */
12310 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12311 {
12312 tree attributes = NULL_TREE;
12313 tree first_attribute;
12314
12315 /* Peek at the next token. */
12316 token = cp_lexer_peek_token (parser->lexer);
12317
12318 /* Check for a bitfield declaration. */
12319 if (token->type == CPP_COLON
12320 || (token->type == CPP_NAME
12321 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12322 == CPP_COLON))
12323 {
12324 tree identifier;
12325 tree width;
12326
12327 /* Get the name of the bitfield. Note that we cannot just
12328 check TOKEN here because it may have been invalidated by
12329 the call to cp_lexer_peek_nth_token above. */
12330 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12331 identifier = cp_parser_identifier (parser);
12332 else
12333 identifier = NULL_TREE;
12334
12335 /* Consume the `:' token. */
12336 cp_lexer_consume_token (parser->lexer);
12337 /* Get the width of the bitfield. */
12338 width
12339 = cp_parser_constant_expression (parser,
12340 /*allow_non_constant=*/false,
12341 NULL);
12342
12343 /* Look for attributes that apply to the bitfield. */
12344 attributes = cp_parser_attributes_opt (parser);
12345 /* Remember which attributes are prefix attributes and
12346 which are not. */
12347 first_attribute = attributes;
12348 /* Combine the attributes. */
12349 attributes = chainon (prefix_attributes, attributes);
12350
12351 /* Create the bitfield declaration. */
12352 decl = grokbitfield (identifier,
12353 decl_specifiers,
12354 width);
12355 /* Apply the attributes. */
12356 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12357 }
12358 else
12359 {
12360 tree declarator;
12361 tree initializer;
12362 tree asm_specification;
12363 int ctor_dtor_or_conv_p;
12364
12365 /* Parse the declarator. */
12366 declarator
12367 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12368 &ctor_dtor_or_conv_p,
12369 /*parenthesized_p=*/NULL);
12370
12371 /* If something went wrong parsing the declarator, make sure
12372 that we at least consume some tokens. */
12373 if (declarator == error_mark_node)
12374 {
12375 /* Skip to the end of the statement. */
12376 cp_parser_skip_to_end_of_statement (parser);
12377 /* If the next token is not a semicolon, that is
12378 probably because we just skipped over the body of
12379 a function. So, we consume a semicolon if
12380 present, but do not issue an error message if it
12381 is not present. */
12382 if (cp_lexer_next_token_is (parser->lexer,
12383 CPP_SEMICOLON))
12384 cp_lexer_consume_token (parser->lexer);
12385 return;
12386 }
12387
12388 cp_parser_check_for_definition_in_return_type
12389 (declarator, declares_class_or_enum);
12390
12391 /* Look for an asm-specification. */
12392 asm_specification = cp_parser_asm_specification_opt (parser);
12393 /* Look for attributes that apply to the declaration. */
12394 attributes = cp_parser_attributes_opt (parser);
12395 /* Remember which attributes are prefix attributes and
12396 which are not. */
12397 first_attribute = attributes;
12398 /* Combine the attributes. */
12399 attributes = chainon (prefix_attributes, attributes);
12400
12401 /* If it's an `=', then we have a constant-initializer or a
12402 pure-specifier. It is not correct to parse the
12403 initializer before registering the member declaration
12404 since the member declaration should be in scope while
12405 its initializer is processed. However, the rest of the
12406 front end does not yet provide an interface that allows
12407 us to handle this correctly. */
12408 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12409 {
12410 /* In [class.mem]:
12411
12412 A pure-specifier shall be used only in the declaration of
12413 a virtual function.
12414
12415 A member-declarator can contain a constant-initializer
12416 only if it declares a static member of integral or
12417 enumeration type.
12418
12419 Therefore, if the DECLARATOR is for a function, we look
12420 for a pure-specifier; otherwise, we look for a
12421 constant-initializer. When we call `grokfield', it will
12422 perform more stringent semantics checks. */
12423 if (TREE_CODE (declarator) == CALL_EXPR)
12424 initializer = cp_parser_pure_specifier (parser);
12425 else
12426 /* Parse the initializer. */
12427 initializer = cp_parser_constant_initializer (parser);
12428 }
12429 /* Otherwise, there is no initializer. */
12430 else
12431 initializer = NULL_TREE;
12432
12433 /* See if we are probably looking at a function
12434 definition. We are certainly not looking at at a
12435 member-declarator. Calling `grokfield' has
12436 side-effects, so we must not do it unless we are sure
12437 that we are looking at a member-declarator. */
12438 if (cp_parser_token_starts_function_definition_p
12439 (cp_lexer_peek_token (parser->lexer)))
12440 {
12441 /* The grammar does not allow a pure-specifier to be
12442 used when a member function is defined. (It is
12443 possible that this fact is an oversight in the
12444 standard, since a pure function may be defined
12445 outside of the class-specifier. */
12446 if (initializer)
12447 error ("pure-specifier on function-definition");
12448 decl = cp_parser_save_member_function_body (parser,
12449 decl_specifiers,
12450 declarator,
12451 attributes);
12452 /* If the member was not a friend, declare it here. */
12453 if (!friend_p)
12454 finish_member_declaration (decl);
12455 /* Peek at the next token. */
12456 token = cp_lexer_peek_token (parser->lexer);
12457 /* If the next token is a semicolon, consume it. */
12458 if (token->type == CPP_SEMICOLON)
12459 cp_lexer_consume_token (parser->lexer);
12460 return;
12461 }
12462 else
12463 {
12464 /* Create the declaration. */
12465 decl = grokfield (declarator, decl_specifiers,
12466 initializer, asm_specification,
12467 attributes);
12468 /* Any initialization must have been from a
12469 constant-expression. */
12470 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12471 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12472 }
12473 }
12474
12475 /* Reset PREFIX_ATTRIBUTES. */
12476 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12477 attributes = TREE_CHAIN (attributes);
12478 if (attributes)
12479 TREE_CHAIN (attributes) = NULL_TREE;
12480
12481 /* If there is any qualification still in effect, clear it
12482 now; we will be starting fresh with the next declarator. */
12483 parser->scope = NULL_TREE;
12484 parser->qualifying_scope = NULL_TREE;
12485 parser->object_scope = NULL_TREE;
12486 /* If it's a `,', then there are more declarators. */
12487 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12488 cp_lexer_consume_token (parser->lexer);
12489 /* If the next token isn't a `;', then we have a parse error. */
12490 else if (cp_lexer_next_token_is_not (parser->lexer,
12491 CPP_SEMICOLON))
12492 {
12493 cp_parser_error (parser, "expected `;'");
12494 /* Skip tokens until we find a `;'. */
12495 cp_parser_skip_to_end_of_statement (parser);
12496
12497 break;
12498 }
12499
12500 if (decl)
12501 {
12502 /* Add DECL to the list of members. */
12503 if (!friend_p)
12504 finish_member_declaration (decl);
12505
12506 if (TREE_CODE (decl) == FUNCTION_DECL)
12507 cp_parser_save_default_args (parser, decl);
12508 }
12509 }
12510 }
12511
12512 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12513 }
12514
12515 /* Parse a pure-specifier.
12516
12517 pure-specifier:
12518 = 0
12519
12520 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12521 Otherwise, ERROR_MARK_NODE is returned. */
12522
12523 static tree
12524 cp_parser_pure_specifier (cp_parser* parser)
12525 {
12526 cp_token *token;
12527
12528 /* Look for the `=' token. */
12529 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12530 return error_mark_node;
12531 /* Look for the `0' token. */
12532 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12533 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12534 to get information from the lexer about how the number was
12535 spelled in order to fix this problem. */
12536 if (!token || !integer_zerop (token->value))
12537 return error_mark_node;
12538
12539 return integer_zero_node;
12540 }
12541
12542 /* Parse a constant-initializer.
12543
12544 constant-initializer:
12545 = constant-expression
12546
12547 Returns a representation of the constant-expression. */
12548
12549 static tree
12550 cp_parser_constant_initializer (cp_parser* parser)
12551 {
12552 /* Look for the `=' token. */
12553 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12554 return error_mark_node;
12555
12556 /* It is invalid to write:
12557
12558 struct S { static const int i = { 7 }; };
12559
12560 */
12561 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12562 {
12563 cp_parser_error (parser,
12564 "a brace-enclosed initializer is not allowed here");
12565 /* Consume the opening brace. */
12566 cp_lexer_consume_token (parser->lexer);
12567 /* Skip the initializer. */
12568 cp_parser_skip_to_closing_brace (parser);
12569 /* Look for the trailing `}'. */
12570 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12571
12572 return error_mark_node;
12573 }
12574
12575 return cp_parser_constant_expression (parser,
12576 /*allow_non_constant=*/false,
12577 NULL);
12578 }
12579
12580 /* Derived classes [gram.class.derived] */
12581
12582 /* Parse a base-clause.
12583
12584 base-clause:
12585 : base-specifier-list
12586
12587 base-specifier-list:
12588 base-specifier
12589 base-specifier-list , base-specifier
12590
12591 Returns a TREE_LIST representing the base-classes, in the order in
12592 which they were declared. The representation of each node is as
12593 described by cp_parser_base_specifier.
12594
12595 In the case that no bases are specified, this function will return
12596 NULL_TREE, not ERROR_MARK_NODE. */
12597
12598 static tree
12599 cp_parser_base_clause (cp_parser* parser)
12600 {
12601 tree bases = NULL_TREE;
12602
12603 /* Look for the `:' that begins the list. */
12604 cp_parser_require (parser, CPP_COLON, "`:'");
12605
12606 /* Scan the base-specifier-list. */
12607 while (true)
12608 {
12609 cp_token *token;
12610 tree base;
12611
12612 /* Look for the base-specifier. */
12613 base = cp_parser_base_specifier (parser);
12614 /* Add BASE to the front of the list. */
12615 if (base != error_mark_node)
12616 {
12617 TREE_CHAIN (base) = bases;
12618 bases = base;
12619 }
12620 /* Peek at the next token. */
12621 token = cp_lexer_peek_token (parser->lexer);
12622 /* If it's not a comma, then the list is complete. */
12623 if (token->type != CPP_COMMA)
12624 break;
12625 /* Consume the `,'. */
12626 cp_lexer_consume_token (parser->lexer);
12627 }
12628
12629 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12630 base class had a qualified name. However, the next name that
12631 appears is certainly not qualified. */
12632 parser->scope = NULL_TREE;
12633 parser->qualifying_scope = NULL_TREE;
12634 parser->object_scope = NULL_TREE;
12635
12636 return nreverse (bases);
12637 }
12638
12639 /* Parse a base-specifier.
12640
12641 base-specifier:
12642 :: [opt] nested-name-specifier [opt] class-name
12643 virtual access-specifier [opt] :: [opt] nested-name-specifier
12644 [opt] class-name
12645 access-specifier virtual [opt] :: [opt] nested-name-specifier
12646 [opt] class-name
12647
12648 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12649 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12650 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12651 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12652
12653 static tree
12654 cp_parser_base_specifier (cp_parser* parser)
12655 {
12656 cp_token *token;
12657 bool done = false;
12658 bool virtual_p = false;
12659 bool duplicate_virtual_error_issued_p = false;
12660 bool duplicate_access_error_issued_p = false;
12661 bool class_scope_p, template_p;
12662 tree access = access_default_node;
12663 tree type;
12664
12665 /* Process the optional `virtual' and `access-specifier'. */
12666 while (!done)
12667 {
12668 /* Peek at the next token. */
12669 token = cp_lexer_peek_token (parser->lexer);
12670 /* Process `virtual'. */
12671 switch (token->keyword)
12672 {
12673 case RID_VIRTUAL:
12674 /* If `virtual' appears more than once, issue an error. */
12675 if (virtual_p && !duplicate_virtual_error_issued_p)
12676 {
12677 cp_parser_error (parser,
12678 "`virtual' specified more than once in base-specified");
12679 duplicate_virtual_error_issued_p = true;
12680 }
12681
12682 virtual_p = true;
12683
12684 /* Consume the `virtual' token. */
12685 cp_lexer_consume_token (parser->lexer);
12686
12687 break;
12688
12689 case RID_PUBLIC:
12690 case RID_PROTECTED:
12691 case RID_PRIVATE:
12692 /* If more than one access specifier appears, issue an
12693 error. */
12694 if (access != access_default_node
12695 && !duplicate_access_error_issued_p)
12696 {
12697 cp_parser_error (parser,
12698 "more than one access specifier in base-specified");
12699 duplicate_access_error_issued_p = true;
12700 }
12701
12702 access = ridpointers[(int) token->keyword];
12703
12704 /* Consume the access-specifier. */
12705 cp_lexer_consume_token (parser->lexer);
12706
12707 break;
12708
12709 default:
12710 done = true;
12711 break;
12712 }
12713 }
12714 /* It is not uncommon to see programs mechanically, errouneously, use
12715 the 'typename' keyword to denote (dependent) qualified types
12716 as base classes. */
12717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12718 {
12719 if (!processing_template_decl)
12720 error ("keyword `typename' not allowed outside of templates");
12721 else
12722 error ("keyword `typename' not allowed in this context "
12723 "(the base class is implicitly a type)");
12724 cp_lexer_consume_token (parser->lexer);
12725 }
12726
12727 /* Look for the optional `::' operator. */
12728 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12729 /* Look for the nested-name-specifier. The simplest way to
12730 implement:
12731
12732 [temp.res]
12733
12734 The keyword `typename' is not permitted in a base-specifier or
12735 mem-initializer; in these contexts a qualified name that
12736 depends on a template-parameter is implicitly assumed to be a
12737 type name.
12738
12739 is to pretend that we have seen the `typename' keyword at this
12740 point. */
12741 cp_parser_nested_name_specifier_opt (parser,
12742 /*typename_keyword_p=*/true,
12743 /*check_dependency_p=*/true,
12744 /*type_p=*/true,
12745 /*is_declaration=*/true);
12746 /* If the base class is given by a qualified name, assume that names
12747 we see are type names or templates, as appropriate. */
12748 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12749 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12750
12751 /* Finally, look for the class-name. */
12752 type = cp_parser_class_name (parser,
12753 class_scope_p,
12754 template_p,
12755 /*type_p=*/true,
12756 /*check_dependency_p=*/true,
12757 /*class_head_p=*/false,
12758 /*is_declaration=*/true);
12759
12760 if (type == error_mark_node)
12761 return error_mark_node;
12762
12763 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12764 }
12765
12766 /* Exception handling [gram.exception] */
12767
12768 /* Parse an (optional) exception-specification.
12769
12770 exception-specification:
12771 throw ( type-id-list [opt] )
12772
12773 Returns a TREE_LIST representing the exception-specification. The
12774 TREE_VALUE of each node is a type. */
12775
12776 static tree
12777 cp_parser_exception_specification_opt (cp_parser* parser)
12778 {
12779 cp_token *token;
12780 tree type_id_list;
12781
12782 /* Peek at the next token. */
12783 token = cp_lexer_peek_token (parser->lexer);
12784 /* If it's not `throw', then there's no exception-specification. */
12785 if (!cp_parser_is_keyword (token, RID_THROW))
12786 return NULL_TREE;
12787
12788 /* Consume the `throw'. */
12789 cp_lexer_consume_token (parser->lexer);
12790
12791 /* Look for the `('. */
12792 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12793
12794 /* Peek at the next token. */
12795 token = cp_lexer_peek_token (parser->lexer);
12796 /* If it's not a `)', then there is a type-id-list. */
12797 if (token->type != CPP_CLOSE_PAREN)
12798 {
12799 const char *saved_message;
12800
12801 /* Types may not be defined in an exception-specification. */
12802 saved_message = parser->type_definition_forbidden_message;
12803 parser->type_definition_forbidden_message
12804 = "types may not be defined in an exception-specification";
12805 /* Parse the type-id-list. */
12806 type_id_list = cp_parser_type_id_list (parser);
12807 /* Restore the saved message. */
12808 parser->type_definition_forbidden_message = saved_message;
12809 }
12810 else
12811 type_id_list = empty_except_spec;
12812
12813 /* Look for the `)'. */
12814 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12815
12816 return type_id_list;
12817 }
12818
12819 /* Parse an (optional) type-id-list.
12820
12821 type-id-list:
12822 type-id
12823 type-id-list , type-id
12824
12825 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12826 in the order that the types were presented. */
12827
12828 static tree
12829 cp_parser_type_id_list (cp_parser* parser)
12830 {
12831 tree types = NULL_TREE;
12832
12833 while (true)
12834 {
12835 cp_token *token;
12836 tree type;
12837
12838 /* Get the next type-id. */
12839 type = cp_parser_type_id (parser);
12840 /* Add it to the list. */
12841 types = add_exception_specifier (types, type, /*complain=*/1);
12842 /* Peek at the next token. */
12843 token = cp_lexer_peek_token (parser->lexer);
12844 /* If it is not a `,', we are done. */
12845 if (token->type != CPP_COMMA)
12846 break;
12847 /* Consume the `,'. */
12848 cp_lexer_consume_token (parser->lexer);
12849 }
12850
12851 return nreverse (types);
12852 }
12853
12854 /* Parse a try-block.
12855
12856 try-block:
12857 try compound-statement handler-seq */
12858
12859 static tree
12860 cp_parser_try_block (cp_parser* parser)
12861 {
12862 tree try_block;
12863
12864 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12865 try_block = begin_try_block ();
12866 cp_parser_compound_statement (parser, false);
12867 finish_try_block (try_block);
12868 cp_parser_handler_seq (parser);
12869 finish_handler_sequence (try_block);
12870
12871 return try_block;
12872 }
12873
12874 /* Parse a function-try-block.
12875
12876 function-try-block:
12877 try ctor-initializer [opt] function-body handler-seq */
12878
12879 static bool
12880 cp_parser_function_try_block (cp_parser* parser)
12881 {
12882 tree try_block;
12883 bool ctor_initializer_p;
12884
12885 /* Look for the `try' keyword. */
12886 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12887 return false;
12888 /* Let the rest of the front-end know where we are. */
12889 try_block = begin_function_try_block ();
12890 /* Parse the function-body. */
12891 ctor_initializer_p
12892 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12893 /* We're done with the `try' part. */
12894 finish_function_try_block (try_block);
12895 /* Parse the handlers. */
12896 cp_parser_handler_seq (parser);
12897 /* We're done with the handlers. */
12898 finish_function_handler_sequence (try_block);
12899
12900 return ctor_initializer_p;
12901 }
12902
12903 /* Parse a handler-seq.
12904
12905 handler-seq:
12906 handler handler-seq [opt] */
12907
12908 static void
12909 cp_parser_handler_seq (cp_parser* parser)
12910 {
12911 while (true)
12912 {
12913 cp_token *token;
12914
12915 /* Parse the handler. */
12916 cp_parser_handler (parser);
12917 /* Peek at the next token. */
12918 token = cp_lexer_peek_token (parser->lexer);
12919 /* If it's not `catch' then there are no more handlers. */
12920 if (!cp_parser_is_keyword (token, RID_CATCH))
12921 break;
12922 }
12923 }
12924
12925 /* Parse a handler.
12926
12927 handler:
12928 catch ( exception-declaration ) compound-statement */
12929
12930 static void
12931 cp_parser_handler (cp_parser* parser)
12932 {
12933 tree handler;
12934 tree declaration;
12935
12936 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12937 handler = begin_handler ();
12938 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12939 declaration = cp_parser_exception_declaration (parser);
12940 finish_handler_parms (declaration, handler);
12941 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12942 cp_parser_compound_statement (parser, false);
12943 finish_handler (handler);
12944 }
12945
12946 /* Parse an exception-declaration.
12947
12948 exception-declaration:
12949 type-specifier-seq declarator
12950 type-specifier-seq abstract-declarator
12951 type-specifier-seq
12952 ...
12953
12954 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12955 ellipsis variant is used. */
12956
12957 static tree
12958 cp_parser_exception_declaration (cp_parser* parser)
12959 {
12960 tree type_specifiers;
12961 tree declarator;
12962 const char *saved_message;
12963
12964 /* If it's an ellipsis, it's easy to handle. */
12965 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12966 {
12967 /* Consume the `...' token. */
12968 cp_lexer_consume_token (parser->lexer);
12969 return NULL_TREE;
12970 }
12971
12972 /* Types may not be defined in exception-declarations. */
12973 saved_message = parser->type_definition_forbidden_message;
12974 parser->type_definition_forbidden_message
12975 = "types may not be defined in exception-declarations";
12976
12977 /* Parse the type-specifier-seq. */
12978 type_specifiers = cp_parser_type_specifier_seq (parser);
12979 /* If it's a `)', then there is no declarator. */
12980 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12981 declarator = NULL_TREE;
12982 else
12983 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12984 /*ctor_dtor_or_conv_p=*/NULL,
12985 /*parenthesized_p=*/NULL);
12986
12987 /* Restore the saved message. */
12988 parser->type_definition_forbidden_message = saved_message;
12989
12990 return start_handler_parms (type_specifiers, declarator);
12991 }
12992
12993 /* Parse a throw-expression.
12994
12995 throw-expression:
12996 throw assignment-expression [opt]
12997
12998 Returns a THROW_EXPR representing the throw-expression. */
12999
13000 static tree
13001 cp_parser_throw_expression (cp_parser* parser)
13002 {
13003 tree expression;
13004 cp_token* token;
13005
13006 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13007 token = cp_lexer_peek_token (parser->lexer);
13008 /* Figure out whether or not there is an assignment-expression
13009 following the "throw" keyword. */
13010 if (token->type == CPP_COMMA
13011 || token->type == CPP_SEMICOLON
13012 || token->type == CPP_CLOSE_PAREN
13013 || token->type == CPP_CLOSE_SQUARE
13014 || token->type == CPP_CLOSE_BRACE
13015 || token->type == CPP_COLON)
13016 expression = NULL_TREE;
13017 else
13018 expression = cp_parser_assignment_expression (parser);
13019
13020 return build_throw (expression);
13021 }
13022
13023 /* GNU Extensions */
13024
13025 /* Parse an (optional) asm-specification.
13026
13027 asm-specification:
13028 asm ( string-literal )
13029
13030 If the asm-specification is present, returns a STRING_CST
13031 corresponding to the string-literal. Otherwise, returns
13032 NULL_TREE. */
13033
13034 static tree
13035 cp_parser_asm_specification_opt (cp_parser* parser)
13036 {
13037 cp_token *token;
13038 tree asm_specification;
13039
13040 /* Peek at the next token. */
13041 token = cp_lexer_peek_token (parser->lexer);
13042 /* If the next token isn't the `asm' keyword, then there's no
13043 asm-specification. */
13044 if (!cp_parser_is_keyword (token, RID_ASM))
13045 return NULL_TREE;
13046
13047 /* Consume the `asm' token. */
13048 cp_lexer_consume_token (parser->lexer);
13049 /* Look for the `('. */
13050 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13051
13052 /* Look for the string-literal. */
13053 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13054 if (token)
13055 asm_specification = token->value;
13056 else
13057 asm_specification = NULL_TREE;
13058
13059 /* Look for the `)'. */
13060 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13061
13062 return asm_specification;
13063 }
13064
13065 /* Parse an asm-operand-list.
13066
13067 asm-operand-list:
13068 asm-operand
13069 asm-operand-list , asm-operand
13070
13071 asm-operand:
13072 string-literal ( expression )
13073 [ string-literal ] string-literal ( expression )
13074
13075 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13076 each node is the expression. The TREE_PURPOSE is itself a
13077 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13078 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13079 is a STRING_CST for the string literal before the parenthesis. */
13080
13081 static tree
13082 cp_parser_asm_operand_list (cp_parser* parser)
13083 {
13084 tree asm_operands = NULL_TREE;
13085
13086 while (true)
13087 {
13088 tree string_literal;
13089 tree expression;
13090 tree name;
13091 cp_token *token;
13092
13093 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13094 {
13095 /* Consume the `[' token. */
13096 cp_lexer_consume_token (parser->lexer);
13097 /* Read the operand name. */
13098 name = cp_parser_identifier (parser);
13099 if (name != error_mark_node)
13100 name = build_string (IDENTIFIER_LENGTH (name),
13101 IDENTIFIER_POINTER (name));
13102 /* Look for the closing `]'. */
13103 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13104 }
13105 else
13106 name = NULL_TREE;
13107 /* Look for the string-literal. */
13108 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13109 string_literal = token ? token->value : error_mark_node;
13110 /* Look for the `('. */
13111 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13112 /* Parse the expression. */
13113 expression = cp_parser_expression (parser);
13114 /* Look for the `)'. */
13115 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13116 /* Add this operand to the list. */
13117 asm_operands = tree_cons (build_tree_list (name, string_literal),
13118 expression,
13119 asm_operands);
13120 /* If the next token is not a `,', there are no more
13121 operands. */
13122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13123 break;
13124 /* Consume the `,'. */
13125 cp_lexer_consume_token (parser->lexer);
13126 }
13127
13128 return nreverse (asm_operands);
13129 }
13130
13131 /* Parse an asm-clobber-list.
13132
13133 asm-clobber-list:
13134 string-literal
13135 asm-clobber-list , string-literal
13136
13137 Returns a TREE_LIST, indicating the clobbers in the order that they
13138 appeared. The TREE_VALUE of each node is a STRING_CST. */
13139
13140 static tree
13141 cp_parser_asm_clobber_list (cp_parser* parser)
13142 {
13143 tree clobbers = NULL_TREE;
13144
13145 while (true)
13146 {
13147 cp_token *token;
13148 tree string_literal;
13149
13150 /* Look for the string literal. */
13151 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13152 string_literal = token ? token->value : error_mark_node;
13153 /* Add it to the list. */
13154 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13155 /* If the next token is not a `,', then the list is
13156 complete. */
13157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13158 break;
13159 /* Consume the `,' token. */
13160 cp_lexer_consume_token (parser->lexer);
13161 }
13162
13163 return clobbers;
13164 }
13165
13166 /* Parse an (optional) series of attributes.
13167
13168 attributes:
13169 attributes attribute
13170
13171 attribute:
13172 __attribute__ (( attribute-list [opt] ))
13173
13174 The return value is as for cp_parser_attribute_list. */
13175
13176 static tree
13177 cp_parser_attributes_opt (cp_parser* parser)
13178 {
13179 tree attributes = NULL_TREE;
13180
13181 while (true)
13182 {
13183 cp_token *token;
13184 tree attribute_list;
13185
13186 /* Peek at the next token. */
13187 token = cp_lexer_peek_token (parser->lexer);
13188 /* If it's not `__attribute__', then we're done. */
13189 if (token->keyword != RID_ATTRIBUTE)
13190 break;
13191
13192 /* Consume the `__attribute__' keyword. */
13193 cp_lexer_consume_token (parser->lexer);
13194 /* Look for the two `(' tokens. */
13195 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13196 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13197
13198 /* Peek at the next token. */
13199 token = cp_lexer_peek_token (parser->lexer);
13200 if (token->type != CPP_CLOSE_PAREN)
13201 /* Parse the attribute-list. */
13202 attribute_list = cp_parser_attribute_list (parser);
13203 else
13204 /* If the next token is a `)', then there is no attribute
13205 list. */
13206 attribute_list = NULL;
13207
13208 /* Look for the two `)' tokens. */
13209 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13210 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13211
13212 /* Add these new attributes to the list. */
13213 attributes = chainon (attributes, attribute_list);
13214 }
13215
13216 return attributes;
13217 }
13218
13219 /* Parse an attribute-list.
13220
13221 attribute-list:
13222 attribute
13223 attribute-list , attribute
13224
13225 attribute:
13226 identifier
13227 identifier ( identifier )
13228 identifier ( identifier , expression-list )
13229 identifier ( expression-list )
13230
13231 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13232 TREE_PURPOSE of each node is the identifier indicating which
13233 attribute is in use. The TREE_VALUE represents the arguments, if
13234 any. */
13235
13236 static tree
13237 cp_parser_attribute_list (cp_parser* parser)
13238 {
13239 tree attribute_list = NULL_TREE;
13240
13241 while (true)
13242 {
13243 cp_token *token;
13244 tree identifier;
13245 tree attribute;
13246
13247 /* Look for the identifier. We also allow keywords here; for
13248 example `__attribute__ ((const))' is legal. */
13249 token = cp_lexer_peek_token (parser->lexer);
13250 if (token->type != CPP_NAME
13251 && token->type != CPP_KEYWORD)
13252 return error_mark_node;
13253 /* Consume the token. */
13254 token = cp_lexer_consume_token (parser->lexer);
13255
13256 /* Save away the identifier that indicates which attribute this is. */
13257 identifier = token->value;
13258 attribute = build_tree_list (identifier, NULL_TREE);
13259
13260 /* Peek at the next token. */
13261 token = cp_lexer_peek_token (parser->lexer);
13262 /* If it's an `(', then parse the attribute arguments. */
13263 if (token->type == CPP_OPEN_PAREN)
13264 {
13265 tree arguments;
13266
13267 arguments = (cp_parser_parenthesized_expression_list
13268 (parser, true, /*non_constant_p=*/NULL));
13269 /* Save the identifier and arguments away. */
13270 TREE_VALUE (attribute) = arguments;
13271 }
13272
13273 /* Add this attribute to the list. */
13274 TREE_CHAIN (attribute) = attribute_list;
13275 attribute_list = attribute;
13276
13277 /* Now, look for more attributes. */
13278 token = cp_lexer_peek_token (parser->lexer);
13279 /* If the next token isn't a `,', we're done. */
13280 if (token->type != CPP_COMMA)
13281 break;
13282
13283 /* Consume the comma and keep going. */
13284 cp_lexer_consume_token (parser->lexer);
13285 }
13286
13287 /* We built up the list in reverse order. */
13288 return nreverse (attribute_list);
13289 }
13290
13291 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13292 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13293 current value of the PEDANTIC flag, regardless of whether or not
13294 the `__extension__' keyword is present. The caller is responsible
13295 for restoring the value of the PEDANTIC flag. */
13296
13297 static bool
13298 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13299 {
13300 /* Save the old value of the PEDANTIC flag. */
13301 *saved_pedantic = pedantic;
13302
13303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13304 {
13305 /* Consume the `__extension__' token. */
13306 cp_lexer_consume_token (parser->lexer);
13307 /* We're not being pedantic while the `__extension__' keyword is
13308 in effect. */
13309 pedantic = 0;
13310
13311 return true;
13312 }
13313
13314 return false;
13315 }
13316
13317 /* Parse a label declaration.
13318
13319 label-declaration:
13320 __label__ label-declarator-seq ;
13321
13322 label-declarator-seq:
13323 identifier , label-declarator-seq
13324 identifier */
13325
13326 static void
13327 cp_parser_label_declaration (cp_parser* parser)
13328 {
13329 /* Look for the `__label__' keyword. */
13330 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13331
13332 while (true)
13333 {
13334 tree identifier;
13335
13336 /* Look for an identifier. */
13337 identifier = cp_parser_identifier (parser);
13338 /* Declare it as a lobel. */
13339 finish_label_decl (identifier);
13340 /* If the next token is a `;', stop. */
13341 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13342 break;
13343 /* Look for the `,' separating the label declarations. */
13344 cp_parser_require (parser, CPP_COMMA, "`,'");
13345 }
13346
13347 /* Look for the final `;'. */
13348 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13349 }
13350
13351 /* Support Functions */
13352
13353 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13354 NAME should have one of the representations used for an
13355 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13356 is returned. If PARSER->SCOPE is a dependent type, then a
13357 SCOPE_REF is returned.
13358
13359 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13360 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13361 was formed. Abstractly, such entities should not be passed to this
13362 function, because they do not need to be looked up, but it is
13363 simpler to check for this special case here, rather than at the
13364 call-sites.
13365
13366 In cases not explicitly covered above, this function returns a
13367 DECL, OVERLOAD, or baselink representing the result of the lookup.
13368 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13369 is returned.
13370
13371 If IS_TYPE is TRUE, bindings that do not refer to types are
13372 ignored.
13373
13374 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13375 ignored.
13376
13377 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13378 are ignored.
13379
13380 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13381 types. */
13382
13383 static tree
13384 cp_parser_lookup_name (cp_parser *parser, tree name,
13385 bool is_type, bool is_template, bool is_namespace,
13386 bool check_dependency)
13387 {
13388 tree decl;
13389 tree object_type = parser->context->object_type;
13390
13391 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13392 no longer valid. Note that if we are parsing tentatively, and
13393 the parse fails, OBJECT_TYPE will be automatically restored. */
13394 parser->context->object_type = NULL_TREE;
13395
13396 if (name == error_mark_node)
13397 return error_mark_node;
13398
13399 /* A template-id has already been resolved; there is no lookup to
13400 do. */
13401 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13402 return name;
13403 if (BASELINK_P (name))
13404 {
13405 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13406 == TEMPLATE_ID_EXPR),
13407 20020909);
13408 return name;
13409 }
13410
13411 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13412 it should already have been checked to make sure that the name
13413 used matches the type being destroyed. */
13414 if (TREE_CODE (name) == BIT_NOT_EXPR)
13415 {
13416 tree type;
13417
13418 /* Figure out to which type this destructor applies. */
13419 if (parser->scope)
13420 type = parser->scope;
13421 else if (object_type)
13422 type = object_type;
13423 else
13424 type = current_class_type;
13425 /* If that's not a class type, there is no destructor. */
13426 if (!type || !CLASS_TYPE_P (type))
13427 return error_mark_node;
13428 if (!CLASSTYPE_DESTRUCTORS (type))
13429 return error_mark_node;
13430 /* If it was a class type, return the destructor. */
13431 return CLASSTYPE_DESTRUCTORS (type);
13432 }
13433
13434 /* By this point, the NAME should be an ordinary identifier. If
13435 the id-expression was a qualified name, the qualifying scope is
13436 stored in PARSER->SCOPE at this point. */
13437 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13438 20000619);
13439
13440 /* Perform the lookup. */
13441 if (parser->scope)
13442 {
13443 bool dependent_p;
13444
13445 if (parser->scope == error_mark_node)
13446 return error_mark_node;
13447
13448 /* If the SCOPE is dependent, the lookup must be deferred until
13449 the template is instantiated -- unless we are explicitly
13450 looking up names in uninstantiated templates. Even then, we
13451 cannot look up the name if the scope is not a class type; it
13452 might, for example, be a template type parameter. */
13453 dependent_p = (TYPE_P (parser->scope)
13454 && !(parser->in_declarator_p
13455 && currently_open_class (parser->scope))
13456 && dependent_type_p (parser->scope));
13457 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13458 && dependent_p)
13459 {
13460 if (is_type)
13461 /* The resolution to Core Issue 180 says that `struct A::B'
13462 should be considered a type-name, even if `A' is
13463 dependent. */
13464 decl = TYPE_NAME (make_typename_type (parser->scope,
13465 name,
13466 /*complain=*/1));
13467 else if (is_template)
13468 decl = make_unbound_class_template (parser->scope,
13469 name,
13470 /*complain=*/1);
13471 else
13472 decl = build_nt (SCOPE_REF, parser->scope, name);
13473 }
13474 else
13475 {
13476 /* If PARSER->SCOPE is a dependent type, then it must be a
13477 class type, and we must not be checking dependencies;
13478 otherwise, we would have processed this lookup above. So
13479 that PARSER->SCOPE is not considered a dependent base by
13480 lookup_member, we must enter the scope here. */
13481 if (dependent_p)
13482 push_scope (parser->scope);
13483 /* If the PARSER->SCOPE is a a template specialization, it
13484 may be instantiated during name lookup. In that case,
13485 errors may be issued. Even if we rollback the current
13486 tentative parse, those errors are valid. */
13487 decl = lookup_qualified_name (parser->scope, name, is_type,
13488 /*complain=*/true);
13489 if (dependent_p)
13490 pop_scope (parser->scope);
13491 }
13492 parser->qualifying_scope = parser->scope;
13493 parser->object_scope = NULL_TREE;
13494 }
13495 else if (object_type)
13496 {
13497 tree object_decl = NULL_TREE;
13498 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13499 OBJECT_TYPE is not a class. */
13500 if (CLASS_TYPE_P (object_type))
13501 /* If the OBJECT_TYPE is a template specialization, it may
13502 be instantiated during name lookup. In that case, errors
13503 may be issued. Even if we rollback the current tentative
13504 parse, those errors are valid. */
13505 object_decl = lookup_member (object_type,
13506 name,
13507 /*protect=*/0, is_type);
13508 /* Look it up in the enclosing context, too. */
13509 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13510 is_namespace,
13511 /*flags=*/0);
13512 parser->object_scope = object_type;
13513 parser->qualifying_scope = NULL_TREE;
13514 if (object_decl)
13515 decl = object_decl;
13516 }
13517 else
13518 {
13519 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13520 is_namespace,
13521 /*flags=*/0);
13522 parser->qualifying_scope = NULL_TREE;
13523 parser->object_scope = NULL_TREE;
13524 }
13525
13526 /* If the lookup failed, let our caller know. */
13527 if (!decl
13528 || decl == error_mark_node
13529 || (TREE_CODE (decl) == FUNCTION_DECL
13530 && DECL_ANTICIPATED (decl)))
13531 return error_mark_node;
13532
13533 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13534 if (TREE_CODE (decl) == TREE_LIST)
13535 {
13536 /* The error message we have to print is too complicated for
13537 cp_parser_error, so we incorporate its actions directly. */
13538 if (!cp_parser_simulate_error (parser))
13539 {
13540 error ("reference to `%D' is ambiguous", name);
13541 print_candidates (decl);
13542 }
13543 return error_mark_node;
13544 }
13545
13546 my_friendly_assert (DECL_P (decl)
13547 || TREE_CODE (decl) == OVERLOAD
13548 || TREE_CODE (decl) == SCOPE_REF
13549 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13550 || BASELINK_P (decl),
13551 20000619);
13552
13553 /* If we have resolved the name of a member declaration, check to
13554 see if the declaration is accessible. When the name resolves to
13555 set of overloaded functions, accessibility is checked when
13556 overload resolution is done.
13557
13558 During an explicit instantiation, access is not checked at all,
13559 as per [temp.explicit]. */
13560 if (DECL_P (decl))
13561 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13562
13563 return decl;
13564 }
13565
13566 /* Like cp_parser_lookup_name, but for use in the typical case where
13567 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13568 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13569
13570 static tree
13571 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13572 {
13573 return cp_parser_lookup_name (parser, name,
13574 /*is_type=*/false,
13575 /*is_template=*/false,
13576 /*is_namespace=*/false,
13577 /*check_dependency=*/true);
13578 }
13579
13580 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13581 the current context, return the TYPE_DECL. If TAG_NAME_P is
13582 true, the DECL indicates the class being defined in a class-head,
13583 or declared in an elaborated-type-specifier.
13584
13585 Otherwise, return DECL. */
13586
13587 static tree
13588 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13589 {
13590 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13591 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13592
13593 struct A {
13594 template <typename T> struct B;
13595 };
13596
13597 template <typename T> struct A::B {};
13598
13599 Similarly, in a elaborated-type-specifier:
13600
13601 namespace N { struct X{}; }
13602
13603 struct A {
13604 template <typename T> friend struct N::X;
13605 };
13606
13607 However, if the DECL refers to a class type, and we are in
13608 the scope of the class, then the name lookup automatically
13609 finds the TYPE_DECL created by build_self_reference rather
13610 than a TEMPLATE_DECL. For example, in:
13611
13612 template <class T> struct S {
13613 S s;
13614 };
13615
13616 there is no need to handle such case. */
13617
13618 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13619 return DECL_TEMPLATE_RESULT (decl);
13620
13621 return decl;
13622 }
13623
13624 /* If too many, or too few, template-parameter lists apply to the
13625 declarator, issue an error message. Returns TRUE if all went well,
13626 and FALSE otherwise. */
13627
13628 static bool
13629 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13630 tree declarator)
13631 {
13632 unsigned num_templates;
13633
13634 /* We haven't seen any classes that involve template parameters yet. */
13635 num_templates = 0;
13636
13637 switch (TREE_CODE (declarator))
13638 {
13639 case CALL_EXPR:
13640 case ARRAY_REF:
13641 case INDIRECT_REF:
13642 case ADDR_EXPR:
13643 {
13644 tree main_declarator = TREE_OPERAND (declarator, 0);
13645 return
13646 cp_parser_check_declarator_template_parameters (parser,
13647 main_declarator);
13648 }
13649
13650 case SCOPE_REF:
13651 {
13652 tree scope;
13653 tree member;
13654
13655 scope = TREE_OPERAND (declarator, 0);
13656 member = TREE_OPERAND (declarator, 1);
13657
13658 /* If this is a pointer-to-member, then we are not interested
13659 in the SCOPE, because it does not qualify the thing that is
13660 being declared. */
13661 if (TREE_CODE (member) == INDIRECT_REF)
13662 return (cp_parser_check_declarator_template_parameters
13663 (parser, member));
13664
13665 while (scope && CLASS_TYPE_P (scope))
13666 {
13667 /* You're supposed to have one `template <...>'
13668 for every template class, but you don't need one
13669 for a full specialization. For example:
13670
13671 template <class T> struct S{};
13672 template <> struct S<int> { void f(); };
13673 void S<int>::f () {}
13674
13675 is correct; there shouldn't be a `template <>' for
13676 the definition of `S<int>::f'. */
13677 if (CLASSTYPE_TEMPLATE_INFO (scope)
13678 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13679 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13680 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13681 ++num_templates;
13682
13683 scope = TYPE_CONTEXT (scope);
13684 }
13685 }
13686
13687 /* Fall through. */
13688
13689 default:
13690 /* If the DECLARATOR has the form `X<y>' then it uses one
13691 additional level of template parameters. */
13692 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13693 ++num_templates;
13694
13695 return cp_parser_check_template_parameters (parser,
13696 num_templates);
13697 }
13698 }
13699
13700 /* NUM_TEMPLATES were used in the current declaration. If that is
13701 invalid, return FALSE and issue an error messages. Otherwise,
13702 return TRUE. */
13703
13704 static bool
13705 cp_parser_check_template_parameters (cp_parser* parser,
13706 unsigned num_templates)
13707 {
13708 /* If there are more template classes than parameter lists, we have
13709 something like:
13710
13711 template <class T> void S<T>::R<T>::f (); */
13712 if (parser->num_template_parameter_lists < num_templates)
13713 {
13714 error ("too few template-parameter-lists");
13715 return false;
13716 }
13717 /* If there are the same number of template classes and parameter
13718 lists, that's OK. */
13719 if (parser->num_template_parameter_lists == num_templates)
13720 return true;
13721 /* If there are more, but only one more, then we are referring to a
13722 member template. That's OK too. */
13723 if (parser->num_template_parameter_lists == num_templates + 1)
13724 return true;
13725 /* Otherwise, there are too many template parameter lists. We have
13726 something like:
13727
13728 template <class T> template <class U> void S::f(); */
13729 error ("too many template-parameter-lists");
13730 return false;
13731 }
13732
13733 /* Parse a binary-expression of the general form:
13734
13735 binary-expression:
13736 <expr>
13737 binary-expression <token> <expr>
13738
13739 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13740 to parser the <expr>s. If the first production is used, then the
13741 value returned by FN is returned directly. Otherwise, a node with
13742 the indicated EXPR_TYPE is returned, with operands corresponding to
13743 the two sub-expressions. */
13744
13745 static tree
13746 cp_parser_binary_expression (cp_parser* parser,
13747 const cp_parser_token_tree_map token_tree_map,
13748 cp_parser_expression_fn fn)
13749 {
13750 tree lhs;
13751
13752 /* Parse the first expression. */
13753 lhs = (*fn) (parser);
13754 /* Now, look for more expressions. */
13755 while (true)
13756 {
13757 cp_token *token;
13758 const cp_parser_token_tree_map_node *map_node;
13759 tree rhs;
13760
13761 /* Peek at the next token. */
13762 token = cp_lexer_peek_token (parser->lexer);
13763 /* If the token is `>', and that's not an operator at the
13764 moment, then we're done. */
13765 if (token->type == CPP_GREATER
13766 && !parser->greater_than_is_operator_p)
13767 break;
13768 /* If we find one of the tokens we want, build the corresponding
13769 tree representation. */
13770 for (map_node = token_tree_map;
13771 map_node->token_type != CPP_EOF;
13772 ++map_node)
13773 if (map_node->token_type == token->type)
13774 {
13775 /* Consume the operator token. */
13776 cp_lexer_consume_token (parser->lexer);
13777 /* Parse the right-hand side of the expression. */
13778 rhs = (*fn) (parser);
13779 /* Build the binary tree node. */
13780 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13781 break;
13782 }
13783
13784 /* If the token wasn't one of the ones we want, we're done. */
13785 if (map_node->token_type == CPP_EOF)
13786 break;
13787 }
13788
13789 return lhs;
13790 }
13791
13792 /* Parse an optional `::' token indicating that the following name is
13793 from the global namespace. If so, PARSER->SCOPE is set to the
13794 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13795 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13796 Returns the new value of PARSER->SCOPE, if the `::' token is
13797 present, and NULL_TREE otherwise. */
13798
13799 static tree
13800 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13801 {
13802 cp_token *token;
13803
13804 /* Peek at the next token. */
13805 token = cp_lexer_peek_token (parser->lexer);
13806 /* If we're looking at a `::' token then we're starting from the
13807 global namespace, not our current location. */
13808 if (token->type == CPP_SCOPE)
13809 {
13810 /* Consume the `::' token. */
13811 cp_lexer_consume_token (parser->lexer);
13812 /* Set the SCOPE so that we know where to start the lookup. */
13813 parser->scope = global_namespace;
13814 parser->qualifying_scope = global_namespace;
13815 parser->object_scope = NULL_TREE;
13816
13817 return parser->scope;
13818 }
13819 else if (!current_scope_valid_p)
13820 {
13821 parser->scope = NULL_TREE;
13822 parser->qualifying_scope = NULL_TREE;
13823 parser->object_scope = NULL_TREE;
13824 }
13825
13826 return NULL_TREE;
13827 }
13828
13829 /* Returns TRUE if the upcoming token sequence is the start of a
13830 constructor declarator. If FRIEND_P is true, the declarator is
13831 preceded by the `friend' specifier. */
13832
13833 static bool
13834 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13835 {
13836 bool constructor_p;
13837 tree type_decl = NULL_TREE;
13838 bool nested_name_p;
13839 cp_token *next_token;
13840
13841 /* The common case is that this is not a constructor declarator, so
13842 try to avoid doing lots of work if at all possible. It's not
13843 valid declare a constructor at function scope. */
13844 if (at_function_scope_p ())
13845 return false;
13846 /* And only certain tokens can begin a constructor declarator. */
13847 next_token = cp_lexer_peek_token (parser->lexer);
13848 if (next_token->type != CPP_NAME
13849 && next_token->type != CPP_SCOPE
13850 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13851 && next_token->type != CPP_TEMPLATE_ID)
13852 return false;
13853
13854 /* Parse tentatively; we are going to roll back all of the tokens
13855 consumed here. */
13856 cp_parser_parse_tentatively (parser);
13857 /* Assume that we are looking at a constructor declarator. */
13858 constructor_p = true;
13859
13860 /* Look for the optional `::' operator. */
13861 cp_parser_global_scope_opt (parser,
13862 /*current_scope_valid_p=*/false);
13863 /* Look for the nested-name-specifier. */
13864 nested_name_p
13865 = (cp_parser_nested_name_specifier_opt (parser,
13866 /*typename_keyword_p=*/false,
13867 /*check_dependency_p=*/false,
13868 /*type_p=*/false,
13869 /*is_declaration=*/false)
13870 != NULL_TREE);
13871 /* Outside of a class-specifier, there must be a
13872 nested-name-specifier. */
13873 if (!nested_name_p &&
13874 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13875 || friend_p))
13876 constructor_p = false;
13877 /* If we still think that this might be a constructor-declarator,
13878 look for a class-name. */
13879 if (constructor_p)
13880 {
13881 /* If we have:
13882
13883 template <typename T> struct S { S(); };
13884 template <typename T> S<T>::S ();
13885
13886 we must recognize that the nested `S' names a class.
13887 Similarly, for:
13888
13889 template <typename T> S<T>::S<T> ();
13890
13891 we must recognize that the nested `S' names a template. */
13892 type_decl = cp_parser_class_name (parser,
13893 /*typename_keyword_p=*/false,
13894 /*template_keyword_p=*/false,
13895 /*type_p=*/false,
13896 /*check_dependency_p=*/false,
13897 /*class_head_p=*/false,
13898 /*is_declaration=*/false);
13899 /* If there was no class-name, then this is not a constructor. */
13900 constructor_p = !cp_parser_error_occurred (parser);
13901 }
13902
13903 /* If we're still considering a constructor, we have to see a `(',
13904 to begin the parameter-declaration-clause, followed by either a
13905 `)', an `...', or a decl-specifier. We need to check for a
13906 type-specifier to avoid being fooled into thinking that:
13907
13908 S::S (f) (int);
13909
13910 is a constructor. (It is actually a function named `f' that
13911 takes one parameter (of type `int') and returns a value of type
13912 `S::S'. */
13913 if (constructor_p
13914 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13915 {
13916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13917 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13918 && !cp_parser_storage_class_specifier_opt (parser))
13919 {
13920 tree type;
13921 unsigned saved_num_template_parameter_lists;
13922
13923 /* Names appearing in the type-specifier should be looked up
13924 in the scope of the class. */
13925 if (current_class_type)
13926 type = NULL_TREE;
13927 else
13928 {
13929 type = TREE_TYPE (type_decl);
13930 if (TREE_CODE (type) == TYPENAME_TYPE)
13931 {
13932 type = resolve_typename_type (type,
13933 /*only_current_p=*/false);
13934 if (type == error_mark_node)
13935 {
13936 cp_parser_abort_tentative_parse (parser);
13937 return false;
13938 }
13939 }
13940 push_scope (type);
13941 }
13942
13943 /* Inside the constructor parameter list, surrounding
13944 template-parameter-lists do not apply. */
13945 saved_num_template_parameter_lists
13946 = parser->num_template_parameter_lists;
13947 parser->num_template_parameter_lists = 0;
13948
13949 /* Look for the type-specifier. */
13950 cp_parser_type_specifier (parser,
13951 CP_PARSER_FLAGS_NONE,
13952 /*is_friend=*/false,
13953 /*is_declarator=*/true,
13954 /*declares_class_or_enum=*/NULL,
13955 /*is_cv_qualifier=*/NULL);
13956
13957 parser->num_template_parameter_lists
13958 = saved_num_template_parameter_lists;
13959
13960 /* Leave the scope of the class. */
13961 if (type)
13962 pop_scope (type);
13963
13964 constructor_p = !cp_parser_error_occurred (parser);
13965 }
13966 }
13967 else
13968 constructor_p = false;
13969 /* We did not really want to consume any tokens. */
13970 cp_parser_abort_tentative_parse (parser);
13971
13972 return constructor_p;
13973 }
13974
13975 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13976 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13977 they must be performed once we are in the scope of the function.
13978
13979 Returns the function defined. */
13980
13981 static tree
13982 cp_parser_function_definition_from_specifiers_and_declarator
13983 (cp_parser* parser,
13984 tree decl_specifiers,
13985 tree attributes,
13986 tree declarator)
13987 {
13988 tree fn;
13989 bool success_p;
13990
13991 /* Begin the function-definition. */
13992 success_p = begin_function_definition (decl_specifiers,
13993 attributes,
13994 declarator);
13995
13996 /* If there were names looked up in the decl-specifier-seq that we
13997 did not check, check them now. We must wait until we are in the
13998 scope of the function to perform the checks, since the function
13999 might be a friend. */
14000 perform_deferred_access_checks ();
14001
14002 if (!success_p)
14003 {
14004 /* If begin_function_definition didn't like the definition, skip
14005 the entire function. */
14006 error ("invalid function declaration");
14007 cp_parser_skip_to_end_of_block_or_statement (parser);
14008 fn = error_mark_node;
14009 }
14010 else
14011 fn = cp_parser_function_definition_after_declarator (parser,
14012 /*inline_p=*/false);
14013
14014 return fn;
14015 }
14016
14017 /* Parse the part of a function-definition that follows the
14018 declarator. INLINE_P is TRUE iff this function is an inline
14019 function defined with a class-specifier.
14020
14021 Returns the function defined. */
14022
14023 static tree
14024 cp_parser_function_definition_after_declarator (cp_parser* parser,
14025 bool inline_p)
14026 {
14027 tree fn;
14028 bool ctor_initializer_p = false;
14029 bool saved_in_unbraced_linkage_specification_p;
14030 unsigned saved_num_template_parameter_lists;
14031
14032 /* If the next token is `return', then the code may be trying to
14033 make use of the "named return value" extension that G++ used to
14034 support. */
14035 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14036 {
14037 /* Consume the `return' keyword. */
14038 cp_lexer_consume_token (parser->lexer);
14039 /* Look for the identifier that indicates what value is to be
14040 returned. */
14041 cp_parser_identifier (parser);
14042 /* Issue an error message. */
14043 error ("named return values are no longer supported");
14044 /* Skip tokens until we reach the start of the function body. */
14045 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14046 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14047 cp_lexer_consume_token (parser->lexer);
14048 }
14049 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14050 anything declared inside `f'. */
14051 saved_in_unbraced_linkage_specification_p
14052 = parser->in_unbraced_linkage_specification_p;
14053 parser->in_unbraced_linkage_specification_p = false;
14054 /* Inside the function, surrounding template-parameter-lists do not
14055 apply. */
14056 saved_num_template_parameter_lists
14057 = parser->num_template_parameter_lists;
14058 parser->num_template_parameter_lists = 0;
14059 /* If the next token is `try', then we are looking at a
14060 function-try-block. */
14061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14062 ctor_initializer_p = cp_parser_function_try_block (parser);
14063 /* A function-try-block includes the function-body, so we only do
14064 this next part if we're not processing a function-try-block. */
14065 else
14066 ctor_initializer_p
14067 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14068
14069 /* Finish the function. */
14070 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14071 (inline_p ? 2 : 0));
14072 /* Generate code for it, if necessary. */
14073 expand_or_defer_fn (fn);
14074 /* Restore the saved values. */
14075 parser->in_unbraced_linkage_specification_p
14076 = saved_in_unbraced_linkage_specification_p;
14077 parser->num_template_parameter_lists
14078 = saved_num_template_parameter_lists;
14079
14080 return fn;
14081 }
14082
14083 /* Parse a template-declaration, assuming that the `export' (and
14084 `extern') keywords, if present, has already been scanned. MEMBER_P
14085 is as for cp_parser_template_declaration. */
14086
14087 static void
14088 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14089 {
14090 tree decl = NULL_TREE;
14091 tree parameter_list;
14092 bool friend_p = false;
14093
14094 /* Look for the `template' keyword. */
14095 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14096 return;
14097
14098 /* And the `<'. */
14099 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14100 return;
14101
14102 /* If the next token is `>', then we have an invalid
14103 specialization. Rather than complain about an invalid template
14104 parameter, issue an error message here. */
14105 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14106 {
14107 cp_parser_error (parser, "invalid explicit specialization");
14108 begin_specialization ();
14109 parameter_list = NULL_TREE;
14110 }
14111 else
14112 {
14113 /* Parse the template parameters. */
14114 begin_template_parm_list ();
14115 parameter_list = cp_parser_template_parameter_list (parser);
14116 parameter_list = end_template_parm_list (parameter_list);
14117 }
14118
14119 /* Look for the `>'. */
14120 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14121 /* We just processed one more parameter list. */
14122 ++parser->num_template_parameter_lists;
14123 /* If the next token is `template', there are more template
14124 parameters. */
14125 if (cp_lexer_next_token_is_keyword (parser->lexer,
14126 RID_TEMPLATE))
14127 cp_parser_template_declaration_after_export (parser, member_p);
14128 else
14129 {
14130 decl = cp_parser_single_declaration (parser,
14131 member_p,
14132 &friend_p);
14133
14134 /* If this is a member template declaration, let the front
14135 end know. */
14136 if (member_p && !friend_p && decl)
14137 {
14138 if (TREE_CODE (decl) == TYPE_DECL)
14139 cp_parser_check_access_in_redeclaration (decl);
14140
14141 decl = finish_member_template_decl (decl);
14142 }
14143 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14144 make_friend_class (current_class_type, TREE_TYPE (decl),
14145 /*complain=*/true);
14146 }
14147 /* We are done with the current parameter list. */
14148 --parser->num_template_parameter_lists;
14149
14150 /* Finish up. */
14151 finish_template_decl (parameter_list);
14152
14153 /* Register member declarations. */
14154 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14155 finish_member_declaration (decl);
14156
14157 /* If DECL is a function template, we must return to parse it later.
14158 (Even though there is no definition, there might be default
14159 arguments that need handling.) */
14160 if (member_p && decl
14161 && (TREE_CODE (decl) == FUNCTION_DECL
14162 || DECL_FUNCTION_TEMPLATE_P (decl)))
14163 TREE_VALUE (parser->unparsed_functions_queues)
14164 = tree_cons (NULL_TREE, decl,
14165 TREE_VALUE (parser->unparsed_functions_queues));
14166 }
14167
14168 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14169 `function-definition' sequence. MEMBER_P is true, this declaration
14170 appears in a class scope.
14171
14172 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14173 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14174
14175 static tree
14176 cp_parser_single_declaration (cp_parser* parser,
14177 bool member_p,
14178 bool* friend_p)
14179 {
14180 int declares_class_or_enum;
14181 tree decl = NULL_TREE;
14182 tree decl_specifiers;
14183 tree attributes;
14184 bool function_definition_p = false;
14185
14186 /* Defer access checks until we know what is being declared. */
14187 push_deferring_access_checks (dk_deferred);
14188
14189 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14190 alternative. */
14191 decl_specifiers
14192 = cp_parser_decl_specifier_seq (parser,
14193 CP_PARSER_FLAGS_OPTIONAL,
14194 &attributes,
14195 &declares_class_or_enum);
14196 if (friend_p)
14197 *friend_p = cp_parser_friend_p (decl_specifiers);
14198 /* Gather up the access checks that occurred the
14199 decl-specifier-seq. */
14200 stop_deferring_access_checks ();
14201
14202 /* Check for the declaration of a template class. */
14203 if (declares_class_or_enum)
14204 {
14205 if (cp_parser_declares_only_class_p (parser))
14206 {
14207 decl = shadow_tag (decl_specifiers);
14208 if (decl)
14209 decl = TYPE_NAME (decl);
14210 else
14211 decl = error_mark_node;
14212 }
14213 }
14214 else
14215 decl = NULL_TREE;
14216 /* If it's not a template class, try for a template function. If
14217 the next token is a `;', then this declaration does not declare
14218 anything. But, if there were errors in the decl-specifiers, then
14219 the error might well have come from an attempted class-specifier.
14220 In that case, there's no need to warn about a missing declarator. */
14221 if (!decl
14222 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14223 || !value_member (error_mark_node, decl_specifiers)))
14224 decl = cp_parser_init_declarator (parser,
14225 decl_specifiers,
14226 attributes,
14227 /*function_definition_allowed_p=*/true,
14228 member_p,
14229 declares_class_or_enum,
14230 &function_definition_p);
14231
14232 pop_deferring_access_checks ();
14233
14234 /* Clear any current qualification; whatever comes next is the start
14235 of something new. */
14236 parser->scope = NULL_TREE;
14237 parser->qualifying_scope = NULL_TREE;
14238 parser->object_scope = NULL_TREE;
14239 /* Look for a trailing `;' after the declaration. */
14240 if (!function_definition_p
14241 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14242 cp_parser_skip_to_end_of_block_or_statement (parser);
14243
14244 return decl;
14245 }
14246
14247 /* Parse a cast-expression that is not the operand of a unary "&". */
14248
14249 static tree
14250 cp_parser_simple_cast_expression (cp_parser *parser)
14251 {
14252 return cp_parser_cast_expression (parser, /*address_p=*/false);
14253 }
14254
14255 /* Parse a functional cast to TYPE. Returns an expression
14256 representing the cast. */
14257
14258 static tree
14259 cp_parser_functional_cast (cp_parser* parser, tree type)
14260 {
14261 tree expression_list;
14262
14263 expression_list
14264 = cp_parser_parenthesized_expression_list (parser, false,
14265 /*non_constant_p=*/NULL);
14266
14267 return build_functional_cast (type, expression_list);
14268 }
14269
14270 /* Save the tokens that make up the body of a member function defined
14271 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14272 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14273 specifiers applied to the declaration. Returns the FUNCTION_DECL
14274 for the member function. */
14275
14276 static tree
14277 cp_parser_save_member_function_body (cp_parser* parser,
14278 tree decl_specifiers,
14279 tree declarator,
14280 tree attributes)
14281 {
14282 cp_token_cache *cache;
14283 tree fn;
14284
14285 /* Create the function-declaration. */
14286 fn = start_method (decl_specifiers, declarator, attributes);
14287 /* If something went badly wrong, bail out now. */
14288 if (fn == error_mark_node)
14289 {
14290 /* If there's a function-body, skip it. */
14291 if (cp_parser_token_starts_function_definition_p
14292 (cp_lexer_peek_token (parser->lexer)))
14293 cp_parser_skip_to_end_of_block_or_statement (parser);
14294 return error_mark_node;
14295 }
14296
14297 /* Remember it, if there default args to post process. */
14298 cp_parser_save_default_args (parser, fn);
14299
14300 /* Create a token cache. */
14301 cache = cp_token_cache_new ();
14302 /* Save away the tokens that make up the body of the
14303 function. */
14304 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14305 /* Handle function try blocks. */
14306 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14307 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14308
14309 /* Save away the inline definition; we will process it when the
14310 class is complete. */
14311 DECL_PENDING_INLINE_INFO (fn) = cache;
14312 DECL_PENDING_INLINE_P (fn) = 1;
14313
14314 /* We need to know that this was defined in the class, so that
14315 friend templates are handled correctly. */
14316 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14317
14318 /* We're done with the inline definition. */
14319 finish_method (fn);
14320
14321 /* Add FN to the queue of functions to be parsed later. */
14322 TREE_VALUE (parser->unparsed_functions_queues)
14323 = tree_cons (NULL_TREE, fn,
14324 TREE_VALUE (parser->unparsed_functions_queues));
14325
14326 return fn;
14327 }
14328
14329 /* Parse a template-argument-list, as well as the trailing ">" (but
14330 not the opening ">"). See cp_parser_template_argument_list for the
14331 return value. */
14332
14333 static tree
14334 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14335 {
14336 tree arguments;
14337 tree saved_scope;
14338 tree saved_qualifying_scope;
14339 tree saved_object_scope;
14340 bool saved_greater_than_is_operator_p;
14341
14342 /* [temp.names]
14343
14344 When parsing a template-id, the first non-nested `>' is taken as
14345 the end of the template-argument-list rather than a greater-than
14346 operator. */
14347 saved_greater_than_is_operator_p
14348 = parser->greater_than_is_operator_p;
14349 parser->greater_than_is_operator_p = false;
14350 /* Parsing the argument list may modify SCOPE, so we save it
14351 here. */
14352 saved_scope = parser->scope;
14353 saved_qualifying_scope = parser->qualifying_scope;
14354 saved_object_scope = parser->object_scope;
14355 /* Parse the template-argument-list itself. */
14356 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14357 arguments = NULL_TREE;
14358 else
14359 arguments = cp_parser_template_argument_list (parser);
14360 /* Look for the `>' that ends the template-argument-list. If we find
14361 a '>>' instead, it's probably just a typo. */
14362 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14363 {
14364 if (!saved_greater_than_is_operator_p)
14365 {
14366 /* If we're in a nested template argument list, the '>>' has to be
14367 a typo for '> >'. We emit the error message, but we continue
14368 parsing and we push a '>' as next token, so that the argument
14369 list will be parsed correctly.. */
14370 cp_token* token;
14371 error ("`>>' should be `> >' within a nested template argument list");
14372 token = cp_lexer_peek_token (parser->lexer);
14373 token->type = CPP_GREATER;
14374 }
14375 else
14376 {
14377 /* If this is not a nested template argument list, the '>>' is
14378 a typo for '>'. Emit an error message and continue. */
14379 error ("spurious `>>', use `>' to terminate a template argument list");
14380 cp_lexer_consume_token (parser->lexer);
14381 }
14382 }
14383 else
14384 cp_parser_require (parser, CPP_GREATER, "`>'");
14385 /* The `>' token might be a greater-than operator again now. */
14386 parser->greater_than_is_operator_p
14387 = saved_greater_than_is_operator_p;
14388 /* Restore the SAVED_SCOPE. */
14389 parser->scope = saved_scope;
14390 parser->qualifying_scope = saved_qualifying_scope;
14391 parser->object_scope = saved_object_scope;
14392
14393 return arguments;
14394 }
14395
14396 /* MEMBER_FUNCTION is a member function, or a friend. If default
14397 arguments, or the body of the function have not yet been parsed,
14398 parse them now. */
14399
14400 static void
14401 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14402 {
14403 cp_lexer *saved_lexer;
14404
14405 /* If this member is a template, get the underlying
14406 FUNCTION_DECL. */
14407 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14408 member_function = DECL_TEMPLATE_RESULT (member_function);
14409
14410 /* There should not be any class definitions in progress at this
14411 point; the bodies of members are only parsed outside of all class
14412 definitions. */
14413 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14414 /* While we're parsing the member functions we might encounter more
14415 classes. We want to handle them right away, but we don't want
14416 them getting mixed up with functions that are currently in the
14417 queue. */
14418 parser->unparsed_functions_queues
14419 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14420
14421 /* Make sure that any template parameters are in scope. */
14422 maybe_begin_member_template_processing (member_function);
14423
14424 /* If the body of the function has not yet been parsed, parse it
14425 now. */
14426 if (DECL_PENDING_INLINE_P (member_function))
14427 {
14428 tree function_scope;
14429 cp_token_cache *tokens;
14430
14431 /* The function is no longer pending; we are processing it. */
14432 tokens = DECL_PENDING_INLINE_INFO (member_function);
14433 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14434 DECL_PENDING_INLINE_P (member_function) = 0;
14435 /* If this was an inline function in a local class, enter the scope
14436 of the containing function. */
14437 function_scope = decl_function_context (member_function);
14438 if (function_scope)
14439 push_function_context_to (function_scope);
14440
14441 /* Save away the current lexer. */
14442 saved_lexer = parser->lexer;
14443 /* Make a new lexer to feed us the tokens saved for this function. */
14444 parser->lexer = cp_lexer_new_from_tokens (tokens);
14445 parser->lexer->next = saved_lexer;
14446
14447 /* Set the current source position to be the location of the first
14448 token in the saved inline body. */
14449 cp_lexer_peek_token (parser->lexer);
14450
14451 /* Let the front end know that we going to be defining this
14452 function. */
14453 start_function (NULL_TREE, member_function, NULL_TREE,
14454 SF_PRE_PARSED | SF_INCLASS_INLINE);
14455
14456 /* Now, parse the body of the function. */
14457 cp_parser_function_definition_after_declarator (parser,
14458 /*inline_p=*/true);
14459
14460 /* Leave the scope of the containing function. */
14461 if (function_scope)
14462 pop_function_context_from (function_scope);
14463 /* Restore the lexer. */
14464 parser->lexer = saved_lexer;
14465 }
14466
14467 /* Remove any template parameters from the symbol table. */
14468 maybe_end_member_template_processing ();
14469
14470 /* Restore the queue. */
14471 parser->unparsed_functions_queues
14472 = TREE_CHAIN (parser->unparsed_functions_queues);
14473 }
14474
14475 /* If DECL contains any default args, remember it on the unparsed
14476 functions queue. */
14477
14478 static void
14479 cp_parser_save_default_args (cp_parser* parser, tree decl)
14480 {
14481 tree probe;
14482
14483 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14484 probe;
14485 probe = TREE_CHAIN (probe))
14486 if (TREE_PURPOSE (probe))
14487 {
14488 TREE_PURPOSE (parser->unparsed_functions_queues)
14489 = tree_cons (NULL_TREE, decl,
14490 TREE_PURPOSE (parser->unparsed_functions_queues));
14491 break;
14492 }
14493 return;
14494 }
14495
14496 /* FN is a FUNCTION_DECL which may contains a parameter with an
14497 unparsed DEFAULT_ARG. Parse the default args now. */
14498
14499 static void
14500 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14501 {
14502 cp_lexer *saved_lexer;
14503 cp_token_cache *tokens;
14504 bool saved_local_variables_forbidden_p;
14505 tree parameters;
14506
14507 /* While we're parsing the default args, we might (due to the
14508 statement expression extension) encounter more classes. We want
14509 to handle them right away, but we don't want them getting mixed
14510 up with default args that are currently in the queue. */
14511 parser->unparsed_functions_queues
14512 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14513
14514 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14515 parameters;
14516 parameters = TREE_CHAIN (parameters))
14517 {
14518 if (!TREE_PURPOSE (parameters)
14519 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14520 continue;
14521
14522 /* Save away the current lexer. */
14523 saved_lexer = parser->lexer;
14524 /* Create a new one, using the tokens we have saved. */
14525 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14526 parser->lexer = cp_lexer_new_from_tokens (tokens);
14527
14528 /* Set the current source position to be the location of the
14529 first token in the default argument. */
14530 cp_lexer_peek_token (parser->lexer);
14531
14532 /* Local variable names (and the `this' keyword) may not appear
14533 in a default argument. */
14534 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14535 parser->local_variables_forbidden_p = true;
14536 /* Parse the assignment-expression. */
14537 if (DECL_CLASS_SCOPE_P (fn))
14538 push_nested_class (DECL_CONTEXT (fn));
14539 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14540 if (DECL_CLASS_SCOPE_P (fn))
14541 pop_nested_class ();
14542
14543 /* Restore saved state. */
14544 parser->lexer = saved_lexer;
14545 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14546 }
14547
14548 /* Restore the queue. */
14549 parser->unparsed_functions_queues
14550 = TREE_CHAIN (parser->unparsed_functions_queues);
14551 }
14552
14553 /* Parse the operand of `sizeof' (or a similar operator). Returns
14554 either a TYPE or an expression, depending on the form of the
14555 input. The KEYWORD indicates which kind of expression we have
14556 encountered. */
14557
14558 static tree
14559 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14560 {
14561 static const char *format;
14562 tree expr = NULL_TREE;
14563 const char *saved_message;
14564 bool saved_integral_constant_expression_p;
14565
14566 /* Initialize FORMAT the first time we get here. */
14567 if (!format)
14568 format = "types may not be defined in `%s' expressions";
14569
14570 /* Types cannot be defined in a `sizeof' expression. Save away the
14571 old message. */
14572 saved_message = parser->type_definition_forbidden_message;
14573 /* And create the new one. */
14574 parser->type_definition_forbidden_message
14575 = xmalloc (strlen (format)
14576 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14577 + 1 /* `\0' */);
14578 sprintf ((char *) parser->type_definition_forbidden_message,
14579 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14580
14581 /* The restrictions on constant-expressions do not apply inside
14582 sizeof expressions. */
14583 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14584 parser->integral_constant_expression_p = false;
14585
14586 /* Do not actually evaluate the expression. */
14587 ++skip_evaluation;
14588 /* If it's a `(', then we might be looking at the type-id
14589 construction. */
14590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14591 {
14592 tree type;
14593 bool saved_in_type_id_in_expr_p;
14594
14595 /* We can't be sure yet whether we're looking at a type-id or an
14596 expression. */
14597 cp_parser_parse_tentatively (parser);
14598 /* Consume the `('. */
14599 cp_lexer_consume_token (parser->lexer);
14600 /* Parse the type-id. */
14601 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14602 parser->in_type_id_in_expr_p = true;
14603 type = cp_parser_type_id (parser);
14604 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14605 /* Now, look for the trailing `)'. */
14606 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14607 /* If all went well, then we're done. */
14608 if (cp_parser_parse_definitely (parser))
14609 {
14610 /* Build a list of decl-specifiers; right now, we have only
14611 a single type-specifier. */
14612 type = build_tree_list (NULL_TREE,
14613 type);
14614
14615 /* Call grokdeclarator to figure out what type this is. */
14616 expr = grokdeclarator (NULL_TREE,
14617 type,
14618 TYPENAME,
14619 /*initialized=*/0,
14620 /*attrlist=*/NULL);
14621 }
14622 }
14623
14624 /* If the type-id production did not work out, then we must be
14625 looking at the unary-expression production. */
14626 if (!expr)
14627 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14628 /* Go back to evaluating expressions. */
14629 --skip_evaluation;
14630
14631 /* Free the message we created. */
14632 free ((char *) parser->type_definition_forbidden_message);
14633 /* And restore the old one. */
14634 parser->type_definition_forbidden_message = saved_message;
14635 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14636
14637 return expr;
14638 }
14639
14640 /* If the current declaration has no declarator, return true. */
14641
14642 static bool
14643 cp_parser_declares_only_class_p (cp_parser *parser)
14644 {
14645 /* If the next token is a `;' or a `,' then there is no
14646 declarator. */
14647 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14648 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14649 }
14650
14651 /* Simplify EXPR if it is a non-dependent expression. Returns the
14652 (possibly simplified) expression. */
14653
14654 static tree
14655 cp_parser_fold_non_dependent_expr (tree expr)
14656 {
14657 /* If we're in a template, but EXPR isn't value dependent, simplify
14658 it. We're supposed to treat:
14659
14660 template <typename T> void f(T[1 + 1]);
14661 template <typename T> void f(T[2]);
14662
14663 as two declarations of the same function, for example. */
14664 if (processing_template_decl
14665 && !type_dependent_expression_p (expr)
14666 && !value_dependent_expression_p (expr))
14667 {
14668 HOST_WIDE_INT saved_processing_template_decl;
14669
14670 saved_processing_template_decl = processing_template_decl;
14671 processing_template_decl = 0;
14672 expr = tsubst_copy_and_build (expr,
14673 /*args=*/NULL_TREE,
14674 tf_error,
14675 /*in_decl=*/NULL_TREE,
14676 /*function_p=*/false);
14677 processing_template_decl = saved_processing_template_decl;
14678 }
14679 return expr;
14680 }
14681
14682 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14683 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14684
14685 static bool
14686 cp_parser_friend_p (tree decl_specifiers)
14687 {
14688 while (decl_specifiers)
14689 {
14690 /* See if this decl-specifier is `friend'. */
14691 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14692 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14693 return true;
14694
14695 /* Go on to the next decl-specifier. */
14696 decl_specifiers = TREE_CHAIN (decl_specifiers);
14697 }
14698
14699 return false;
14700 }
14701
14702 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14703 issue an error message indicating that TOKEN_DESC was expected.
14704
14705 Returns the token consumed, if the token had the appropriate type.
14706 Otherwise, returns NULL. */
14707
14708 static cp_token *
14709 cp_parser_require (cp_parser* parser,
14710 enum cpp_ttype type,
14711 const char* token_desc)
14712 {
14713 if (cp_lexer_next_token_is (parser->lexer, type))
14714 return cp_lexer_consume_token (parser->lexer);
14715 else
14716 {
14717 /* Output the MESSAGE -- unless we're parsing tentatively. */
14718 if (!cp_parser_simulate_error (parser))
14719 {
14720 char *message = concat ("expected ", token_desc, NULL);
14721 cp_parser_error (parser, message);
14722 free (message);
14723 }
14724 return NULL;
14725 }
14726 }
14727
14728 /* Like cp_parser_require, except that tokens will be skipped until
14729 the desired token is found. An error message is still produced if
14730 the next token is not as expected. */
14731
14732 static void
14733 cp_parser_skip_until_found (cp_parser* parser,
14734 enum cpp_ttype type,
14735 const char* token_desc)
14736 {
14737 cp_token *token;
14738 unsigned nesting_depth = 0;
14739
14740 if (cp_parser_require (parser, type, token_desc))
14741 return;
14742
14743 /* Skip tokens until the desired token is found. */
14744 while (true)
14745 {
14746 /* Peek at the next token. */
14747 token = cp_lexer_peek_token (parser->lexer);
14748 /* If we've reached the token we want, consume it and
14749 stop. */
14750 if (token->type == type && !nesting_depth)
14751 {
14752 cp_lexer_consume_token (parser->lexer);
14753 return;
14754 }
14755 /* If we've run out of tokens, stop. */
14756 if (token->type == CPP_EOF)
14757 return;
14758 if (token->type == CPP_OPEN_BRACE
14759 || token->type == CPP_OPEN_PAREN
14760 || token->type == CPP_OPEN_SQUARE)
14761 ++nesting_depth;
14762 else if (token->type == CPP_CLOSE_BRACE
14763 || token->type == CPP_CLOSE_PAREN
14764 || token->type == CPP_CLOSE_SQUARE)
14765 {
14766 if (nesting_depth-- == 0)
14767 return;
14768 }
14769 /* Consume this token. */
14770 cp_lexer_consume_token (parser->lexer);
14771 }
14772 }
14773
14774 /* If the next token is the indicated keyword, consume it. Otherwise,
14775 issue an error message indicating that TOKEN_DESC was expected.
14776
14777 Returns the token consumed, if the token had the appropriate type.
14778 Otherwise, returns NULL. */
14779
14780 static cp_token *
14781 cp_parser_require_keyword (cp_parser* parser,
14782 enum rid keyword,
14783 const char* token_desc)
14784 {
14785 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14786
14787 if (token && token->keyword != keyword)
14788 {
14789 dyn_string_t error_msg;
14790
14791 /* Format the error message. */
14792 error_msg = dyn_string_new (0);
14793 dyn_string_append_cstr (error_msg, "expected ");
14794 dyn_string_append_cstr (error_msg, token_desc);
14795 cp_parser_error (parser, error_msg->s);
14796 dyn_string_delete (error_msg);
14797 return NULL;
14798 }
14799
14800 return token;
14801 }
14802
14803 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14804 function-definition. */
14805
14806 static bool
14807 cp_parser_token_starts_function_definition_p (cp_token* token)
14808 {
14809 return (/* An ordinary function-body begins with an `{'. */
14810 token->type == CPP_OPEN_BRACE
14811 /* A ctor-initializer begins with a `:'. */
14812 || token->type == CPP_COLON
14813 /* A function-try-block begins with `try'. */
14814 || token->keyword == RID_TRY
14815 /* The named return value extension begins with `return'. */
14816 || token->keyword == RID_RETURN);
14817 }
14818
14819 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14820 definition. */
14821
14822 static bool
14823 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14824 {
14825 cp_token *token;
14826
14827 token = cp_lexer_peek_token (parser->lexer);
14828 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14829 }
14830
14831 /* Returns TRUE iff the next token is the "," or ">" ending a
14832 template-argument. ">>" is also accepted (after the full
14833 argument was parsed) because it's probably a typo for "> >",
14834 and there is a specific diagnostic for this. */
14835
14836 static bool
14837 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14838 {
14839 cp_token *token;
14840
14841 token = cp_lexer_peek_token (parser->lexer);
14842 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14843 || token->type == CPP_RSHIFT);
14844 }
14845
14846 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14847 or none_type otherwise. */
14848
14849 static enum tag_types
14850 cp_parser_token_is_class_key (cp_token* token)
14851 {
14852 switch (token->keyword)
14853 {
14854 case RID_CLASS:
14855 return class_type;
14856 case RID_STRUCT:
14857 return record_type;
14858 case RID_UNION:
14859 return union_type;
14860
14861 default:
14862 return none_type;
14863 }
14864 }
14865
14866 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14867
14868 static void
14869 cp_parser_check_class_key (enum tag_types class_key, tree type)
14870 {
14871 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14872 pedwarn ("`%s' tag used in naming `%#T'",
14873 class_key == union_type ? "union"
14874 : class_key == record_type ? "struct" : "class",
14875 type);
14876 }
14877
14878 /* Issue an error message if DECL is redeclared with different
14879 access than its original declaration [class.access.spec/3].
14880 This applies to nested classes and nested class templates.
14881 [class.mem/1]. */
14882
14883 static void cp_parser_check_access_in_redeclaration (tree decl)
14884 {
14885 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14886 return;
14887
14888 if ((TREE_PRIVATE (decl)
14889 != (current_access_specifier == access_private_node))
14890 || (TREE_PROTECTED (decl)
14891 != (current_access_specifier == access_protected_node)))
14892 error ("%D redeclared with different access", decl);
14893 }
14894
14895 /* Look for the `template' keyword, as a syntactic disambiguator.
14896 Return TRUE iff it is present, in which case it will be
14897 consumed. */
14898
14899 static bool
14900 cp_parser_optional_template_keyword (cp_parser *parser)
14901 {
14902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14903 {
14904 /* The `template' keyword can only be used within templates;
14905 outside templates the parser can always figure out what is a
14906 template and what is not. */
14907 if (!processing_template_decl)
14908 {
14909 error ("`template' (as a disambiguator) is only allowed "
14910 "within templates");
14911 /* If this part of the token stream is rescanned, the same
14912 error message would be generated. So, we purge the token
14913 from the stream. */
14914 cp_lexer_purge_token (parser->lexer);
14915 return false;
14916 }
14917 else
14918 {
14919 /* Consume the `template' keyword. */
14920 cp_lexer_consume_token (parser->lexer);
14921 return true;
14922 }
14923 }
14924
14925 return false;
14926 }
14927
14928 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14929 set PARSER->SCOPE, and perform other related actions. */
14930
14931 static void
14932 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14933 {
14934 tree value;
14935 tree check;
14936
14937 /* Get the stored value. */
14938 value = cp_lexer_consume_token (parser->lexer)->value;
14939 /* Perform any access checks that were deferred. */
14940 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14941 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14942 /* Set the scope from the stored value. */
14943 parser->scope = TREE_VALUE (value);
14944 parser->qualifying_scope = TREE_TYPE (value);
14945 parser->object_scope = NULL_TREE;
14946 }
14947
14948 /* Add tokens to CACHE until an non-nested END token appears. */
14949
14950 static void
14951 cp_parser_cache_group (cp_parser *parser,
14952 cp_token_cache *cache,
14953 enum cpp_ttype end,
14954 unsigned depth)
14955 {
14956 while (true)
14957 {
14958 cp_token *token;
14959
14960 /* Abort a parenthesized expression if we encounter a brace. */
14961 if ((end == CPP_CLOSE_PAREN || depth == 0)
14962 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14963 return;
14964 /* Consume the next token. */
14965 token = cp_lexer_consume_token (parser->lexer);
14966 /* If we've reached the end of the file, stop. */
14967 if (token->type == CPP_EOF)
14968 return;
14969 /* Add this token to the tokens we are saving. */
14970 cp_token_cache_push_token (cache, token);
14971 /* See if it starts a new group. */
14972 if (token->type == CPP_OPEN_BRACE)
14973 {
14974 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14975 if (depth == 0)
14976 return;
14977 }
14978 else if (token->type == CPP_OPEN_PAREN)
14979 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14980 else if (token->type == end)
14981 return;
14982 }
14983 }
14984
14985 /* Begin parsing tentatively. We always save tokens while parsing
14986 tentatively so that if the tentative parsing fails we can restore the
14987 tokens. */
14988
14989 static void
14990 cp_parser_parse_tentatively (cp_parser* parser)
14991 {
14992 /* Enter a new parsing context. */
14993 parser->context = cp_parser_context_new (parser->context);
14994 /* Begin saving tokens. */
14995 cp_lexer_save_tokens (parser->lexer);
14996 /* In order to avoid repetitive access control error messages,
14997 access checks are queued up until we are no longer parsing
14998 tentatively. */
14999 push_deferring_access_checks (dk_deferred);
15000 }
15001
15002 /* Commit to the currently active tentative parse. */
15003
15004 static void
15005 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15006 {
15007 cp_parser_context *context;
15008 cp_lexer *lexer;
15009
15010 /* Mark all of the levels as committed. */
15011 lexer = parser->lexer;
15012 for (context = parser->context; context->next; context = context->next)
15013 {
15014 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15015 break;
15016 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15017 while (!cp_lexer_saving_tokens (lexer))
15018 lexer = lexer->next;
15019 cp_lexer_commit_tokens (lexer);
15020 }
15021 }
15022
15023 /* Abort the currently active tentative parse. All consumed tokens
15024 will be rolled back, and no diagnostics will be issued. */
15025
15026 static void
15027 cp_parser_abort_tentative_parse (cp_parser* parser)
15028 {
15029 cp_parser_simulate_error (parser);
15030 /* Now, pretend that we want to see if the construct was
15031 successfully parsed. */
15032 cp_parser_parse_definitely (parser);
15033 }
15034
15035 /* Stop parsing tentatively. If a parse error has occurred, restore the
15036 token stream. Otherwise, commit to the tokens we have consumed.
15037 Returns true if no error occurred; false otherwise. */
15038
15039 static bool
15040 cp_parser_parse_definitely (cp_parser* parser)
15041 {
15042 bool error_occurred;
15043 cp_parser_context *context;
15044
15045 /* Remember whether or not an error occurred, since we are about to
15046 destroy that information. */
15047 error_occurred = cp_parser_error_occurred (parser);
15048 /* Remove the topmost context from the stack. */
15049 context = parser->context;
15050 parser->context = context->next;
15051 /* If no parse errors occurred, commit to the tentative parse. */
15052 if (!error_occurred)
15053 {
15054 /* Commit to the tokens read tentatively, unless that was
15055 already done. */
15056 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15057 cp_lexer_commit_tokens (parser->lexer);
15058
15059 pop_to_parent_deferring_access_checks ();
15060 }
15061 /* Otherwise, if errors occurred, roll back our state so that things
15062 are just as they were before we began the tentative parse. */
15063 else
15064 {
15065 cp_lexer_rollback_tokens (parser->lexer);
15066 pop_deferring_access_checks ();
15067 }
15068 /* Add the context to the front of the free list. */
15069 context->next = cp_parser_context_free_list;
15070 cp_parser_context_free_list = context;
15071
15072 return !error_occurred;
15073 }
15074
15075 /* Returns true if we are parsing tentatively -- but have decided that
15076 we will stick with this tentative parse, even if errors occur. */
15077
15078 static bool
15079 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15080 {
15081 return (cp_parser_parsing_tentatively (parser)
15082 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15083 }
15084
15085 /* Returns nonzero iff an error has occurred during the most recent
15086 tentative parse. */
15087
15088 static bool
15089 cp_parser_error_occurred (cp_parser* parser)
15090 {
15091 return (cp_parser_parsing_tentatively (parser)
15092 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15093 }
15094
15095 /* Returns nonzero if GNU extensions are allowed. */
15096
15097 static bool
15098 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15099 {
15100 return parser->allow_gnu_extensions_p;
15101 }
15102
15103 \f
15104
15105 /* The parser. */
15106
15107 static GTY (()) cp_parser *the_parser;
15108
15109 /* External interface. */
15110
15111 /* Parse one entire translation unit. */
15112
15113 void
15114 c_parse_file (void)
15115 {
15116 bool error_occurred;
15117
15118 the_parser = cp_parser_new ();
15119 push_deferring_access_checks (flag_access_control
15120 ? dk_no_deferred : dk_no_check);
15121 error_occurred = cp_parser_translation_unit (the_parser);
15122 the_parser = NULL;
15123 }
15124
15125 /* Clean up after parsing the entire translation unit. */
15126
15127 void
15128 free_parser_stacks (void)
15129 {
15130 /* Nothing to do. */
15131 }
15132
15133 /* This variable must be provided by every front end. */
15134
15135 int yydebug;
15136
15137 #include "gt-cp-parser.h"