fix typo
[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 /* Token flags. */
76 unsigned char flags;
77 /* The value associated with this token, if any. */
78 tree value;
79 /* The location at which this token was found. */
80 location_t location;
81 } cp_token;
82
83 /* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
85
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
87
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
92 complete.)
93
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to constraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
98
99 typedef struct cp_token_block GTY (())
100 {
101 /* The tokens. */
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
104 size_t num_tokens;
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
109 } cp_token_block;
110
111 typedef struct cp_token_cache GTY (())
112 {
113 /* The first block in the cache. NULL if there are no tokens in the
114 cache. */
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
117 cache. */
118 cp_token_block *last;
119 } cp_token_cache;
120
121 /* Prototypes. */
122
123 static cp_token_cache *cp_token_cache_new
124 (void);
125 static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
127
128 /* Create a new cp_token_cache. */
129
130 static cp_token_cache *
131 cp_token_cache_new (void)
132 {
133 return ggc_alloc_cleared (sizeof (cp_token_cache));
134 }
135
136 /* Add *TOKEN to *CACHE. */
137
138 static void
139 cp_token_cache_push_token (cp_token_cache *cache,
140 cp_token *token)
141 {
142 cp_token_block *b = cache->last;
143
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146 {
147 b = ggc_alloc_cleared (sizeof (cp_token_block));
148 b->prev = cache->last;
149 if (cache->last)
150 {
151 cache->last->next = b;
152 cache->last = b;
153 }
154 else
155 cache->first = cache->last = b;
156 }
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
159 }
160
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
163 it to the parser. */
164
165 typedef struct cp_lexer GTY (())
166 {
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip)) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip)) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip)) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip)) last_token;
183
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
188
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
193
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
198
199 /* The STRING_CST tokens encountered while processing the current
200 string literal. */
201 varray_type string_tokens;
202
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
205 bool main_lexer_p;
206
207 /* True if we should output debugging information. */
208 bool debugging_p;
209
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
212 } cp_lexer;
213
214 /* Prototypes. */
215
216 static cp_lexer *cp_lexer_new_main
217 (void);
218 static cp_lexer *cp_lexer_new_from_tokens
219 (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
221 (const cp_lexer *);
222 static cp_token *cp_lexer_next_token
223 (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
229 (cp_lexer *);
230 static void cp_lexer_maybe_grow_buffer
231 (cp_lexer *);
232 static void cp_lexer_get_preprocessor_token
233 (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
235 (cp_lexer *);
236 static cp_token *cp_lexer_peek_nth_token
237 (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241 (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243 (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token
245 (cp_lexer *);
246 static void cp_lexer_purge_token
247 (cp_lexer *);
248 static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
251 (cp_lexer *);
252 static void cp_lexer_commit_tokens
253 (cp_lexer *);
254 static void cp_lexer_rollback_tokens
255 (cp_lexer *);
256 static inline void cp_lexer_set_source_position_from_token
257 (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259 (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p
261 (cp_lexer *);
262 static void cp_lexer_start_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265 (cp_lexer *) ATTRIBUTE_UNUSED;
266
267 /* Manifest constants. */
268
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
271
272 /* A token type for keywords, as opposed to ordinary identifiers. */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
274
275 /* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
280
281 /* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
287
288 /* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
291
292 /* Variables. */
293
294 /* The stream to which debugging output should be written. */
295 static FILE *cp_lexer_debug_stream;
296
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
298 preprocessor. */
299
300 static cp_lexer *
301 cp_lexer_new_main (void)
302 {
303 cp_lexer *lexer;
304 cp_token first_token;
305
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL, &first_token);
310 c_common_no_more_pch ();
311
312 /* Allocate the memory. */
313 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
314
315 /* Create the circular buffer. */
316 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
318
319 /* There is one token in the buffer. */
320 lexer->last_token = lexer->buffer + 1;
321 lexer->first_token = lexer->buffer;
322 lexer->next_token = lexer->buffer;
323 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
324
325 /* This lexer obtains more tokens by calling c_lex. */
326 lexer->main_lexer_p = true;
327
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
330
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
333
334 /* Assume we are not debugging. */
335 lexer->debugging_p = false;
336
337 return lexer;
338 }
339
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
342
343 static cp_lexer *
344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
345 {
346 cp_lexer *lexer;
347 cp_token *token;
348 cp_token_block *block;
349 ptrdiff_t num_tokens;
350
351 /* Allocate the memory. */
352 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
353
354 /* Create a new buffer, appropriately sized. */
355 num_tokens = 0;
356 for (block = tokens->first; block != NULL; block = block->next)
357 num_tokens += block->num_tokens;
358 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359 lexer->buffer_end = lexer->buffer + num_tokens;
360
361 /* Install the tokens. */
362 token = lexer->buffer;
363 for (block = tokens->first; block != NULL; block = block->next)
364 {
365 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366 token += block->num_tokens;
367 }
368
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer->first_token = lexer->buffer;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer->next_token = lexer->buffer;
373 /* The buffer is full. */
374 lexer->last_token = lexer->first_token;
375
376 /* This lexer doesn't obtain more tokens. */
377 lexer->main_lexer_p = false;
378
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
381
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
384
385 /* Assume we are not debugging. */
386 lexer->debugging_p = false;
387
388 return lexer;
389 }
390
391 /* Returns nonzero if debugging information should be output. */
392
393 static inline bool
394 cp_lexer_debugging_p (cp_lexer *lexer)
395 {
396 return lexer->debugging_p;
397 }
398
399 /* Set the current source position from the information stored in
400 TOKEN. */
401
402 static inline void
403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404 const cp_token *token)
405 {
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
408
409 /* Update the line number. */
410 if (token->type != CPP_EOF)
411 input_location = token->location;
412 }
413
414 /* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
416
417 static inline cp_token *
418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
419 {
420 token++;
421 if (token == lexer->buffer_end)
422 token = lexer->buffer;
423 return token;
424 }
425
426 /* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
428
429 static inline cp_token *
430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
431 {
432 if (token == lexer->buffer)
433 token = lexer->buffer_end;
434 return token - 1;
435 }
436
437 /* nonzero if we are presently saving tokens. */
438
439 static int
440 cp_lexer_saving_tokens (const cp_lexer* lexer)
441 {
442 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443 }
444
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
446 buffer. */
447
448 static cp_token *
449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
450 {
451 token += n;
452 if (token >= lexer->buffer_end)
453 token = lexer->buffer + (token - lexer->buffer_end);
454 return token;
455 }
456
457 /* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
459
460 static ptrdiff_t
461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
462 {
463 if (finish >= start)
464 return finish - start;
465 else
466 return ((lexer->buffer_end - lexer->buffer)
467 - (start - finish));
468 }
469
470 /* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
472
473 static cp_token *
474 cp_lexer_read_token (cp_lexer* lexer)
475 {
476 cp_token *token;
477
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer);
480
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer->first_token)
483 lexer->first_token = lexer->last_token;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer->next_token)
486 lexer->next_token = lexer->last_token;
487
488 /* Figure out where we're going to store the new token. */
489 token = lexer->last_token;
490
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer, token);
493
494 /* Increment LAST_TOKEN. */
495 lexer->last_token = cp_lexer_next_token (lexer, token);
496
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
499 constant elements.
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502 && flag_const_strings)
503 {
504 tree type;
505
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
513 }
514
515 return token;
516 }
517
518 /* If the circular buffer is full, make it bigger. */
519
520 static void
521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
522 {
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
525 {
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
531
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
534 value. */
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
539 new_buffer = ggc_realloc (lexer->buffer,
540 2 * buffer_length * sizeof (cp_token));
541
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
546 buffer. */
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
549 new_buffer,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555
556 /* Now recompute all of the buffer pointers. */
557 new_first_token
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
560 {
561 ptrdiff_t next_token_delta;
562
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
565 else
566 next_token_delta =
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
569 }
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
574 }
575 }
576
577 /* Store the next token from the preprocessor in *TOKEN. */
578
579 static void
580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581 cp_token *token)
582 {
583 bool done;
584
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (lexer != NULL && !lexer->main_lexer_p)
587 {
588 token->type = CPP_EOF;
589 token->location.line = 0;
590 token->location.file = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
593
594 return;
595 }
596
597 done = false;
598 /* Keep going until we get a token we like. */
599 while (!done)
600 {
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex_with_flags (&token->value, &token->flags);
603 /* Issue messages about tokens we cannot process. */
604 switch (token->type)
605 {
606 case CPP_ATSIGN:
607 case CPP_HASH:
608 case CPP_PASTE:
609 error ("invalid token");
610 break;
611
612 default:
613 /* This is a good token, so we exit the loop. */
614 done = true;
615 break;
616 }
617 }
618 /* Now we've got our token. */
619 token->location = input_location;
620
621 /* Check to see if this token is a keyword. */
622 if (token->type == CPP_NAME
623 && C_IS_RESERVED_WORD (token->value))
624 {
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
634 }
635 else
636 token->keyword = RID_MAX;
637 }
638
639 /* Return a pointer to the next token in the token stream, but do not
640 consume it. */
641
642 static cp_token *
643 cp_lexer_peek_token (cp_lexer* lexer)
644 {
645 cp_token *token;
646
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
650
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
653 {
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
657 }
658
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
661 return token;
662 }
663
664 /* Return true if the next token has the indicated TYPE. */
665
666 static bool
667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
668 {
669 cp_token *token;
670
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
675 }
676
677 /* Return true if the next token does not have the indicated TYPE. */
678
679 static bool
680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
681 {
682 return !cp_lexer_next_token_is (lexer, type);
683 }
684
685 /* Return true if the next token is the indicated KEYWORD. */
686
687 static bool
688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
689 {
690 cp_token *token;
691
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
696 }
697
698 /* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
700
701 static cp_token *
702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
703 {
704 cp_token *token;
705
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
708
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
712 if (!token)
713 {
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
716 }
717
718 /* Now, read tokens until we have enough. */
719 while (--n > 0)
720 {
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
726 }
727
728 return token;
729 }
730
731 /* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
734 time. */
735
736 static cp_token *
737 cp_lexer_consume_token (cp_lexer* lexer)
738 {
739 cp_token *token;
740
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
744
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
747
748 /* Increment NEXT_TOKEN. */
749 lexer->next_token = cp_lexer_next_token (lexer,
750 lexer->next_token);
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
754
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
757 {
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
761 else
762 lexer->first_token = lexer->next_token;
763 }
764
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
767 {
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
771 }
772
773 return token;
774 }
775
776 /* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
779
780 static void
781 cp_lexer_purge_token (cp_lexer *lexer)
782 {
783 cp_token *token;
784 cp_token *next_token;
785
786 token = lexer->next_token;
787 while (true)
788 {
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
791 break;
792 *token = *next_token;
793 token = next_token;
794 }
795
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
798 clear NEXT_TOKEN. */
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
801 }
802
803 /* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
806
807 static void
808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
809 {
810 cp_token *peek;
811 cp_token *t1;
812 cp_token *t2;
813
814 if (lexer->next_token)
815 {
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
822 {
823 *t1 = *t2;
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
826 }
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
831 }
832 else
833 {
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
837 }
838 }
839
840 /* Begin saving tokens. All tokens consumed after this point will be
841 preserved. */
842
843 static void
844 cp_lexer_save_tokens (cp_lexer* lexer)
845 {
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
854
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
857 lexer->first_token,
858 lexer->next_token));
859 }
860
861 /* Commit to the portion of the token stream most recently saved. */
862
863 static void
864 cp_lexer_commit_tokens (cp_lexer* lexer)
865 {
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
869
870 VARRAY_POP (lexer->saved_tokens);
871 }
872
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
875
876 static void
877 cp_lexer_rollback_tokens (cp_lexer* lexer)
878 {
879 size_t delta;
880
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
884
885 /* Find the token that was the NEXT_TOKEN when we started saving
886 tokens. */
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
890 lexer->first_token,
891 delta);
892 /* It might be the case that there were no tokens when we started
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
896
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
899 }
900
901 /* Print a representation of the TOKEN on the STREAM. */
902
903 static void
904 cp_lexer_print_token (FILE * stream, cp_token* token)
905 {
906 const char *token_type = NULL;
907
908 /* Figure out what kind of token this is. */
909 switch (token->type)
910 {
911 case CPP_EQ:
912 token_type = "EQ";
913 break;
914
915 case CPP_COMMA:
916 token_type = "COMMA";
917 break;
918
919 case CPP_OPEN_PAREN:
920 token_type = "OPEN_PAREN";
921 break;
922
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
925 break;
926
927 case CPP_OPEN_BRACE:
928 token_type = "OPEN_BRACE";
929 break;
930
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
933 break;
934
935 case CPP_SEMICOLON:
936 token_type = "SEMICOLON";
937 break;
938
939 case CPP_NAME:
940 token_type = "NAME";
941 break;
942
943 case CPP_EOF:
944 token_type = "EOF";
945 break;
946
947 case CPP_KEYWORD:
948 token_type = "keyword";
949 break;
950
951 /* This is not a token that we know how to handle yet. */
952 default:
953 break;
954 }
955
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
958 if (token_type)
959 fprintf (stream, "%s", token_type);
960 else
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
963 if (token->type == CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token->type == CPP_KEYWORD
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969 }
970
971 /* Start emitting debugging information. */
972
973 static void
974 cp_lexer_start_debugging (cp_lexer* lexer)
975 {
976 ++lexer->debugging_p;
977 }
978
979 /* Stop emitting debugging information. */
980
981 static void
982 cp_lexer_stop_debugging (cp_lexer* lexer)
983 {
984 --lexer->debugging_p;
985 }
986
987 \f
988 /* The parser. */
989
990 /* Overview
991 --------
992
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1000
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1003 code processed.
1004
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1013
1014 Methodology
1015 -----------
1016
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1033
1034 Future Improvements
1035 -------------------
1036
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1039
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1044
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1049
1050 /* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1052
1053 typedef enum cp_parser_flags
1054 {
1055 /* No flags. */
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062 } cp_parser_flags;
1063
1064 /* The different kinds of declarators we want to parse. */
1065
1066 typedef enum cp_parser_declarator_kind
1067 {
1068 /* We want an abstract declarator. */
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
1072 /* We don't mind, but the name must be an unqualified-id. */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1075
1076 /* A mapping from a token type to a corresponding tree node type. */
1077
1078 typedef struct cp_parser_token_tree_map_node
1079 {
1080 /* The token type. */
1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082 /* The corresponding tree code. */
1083 ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1085
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1088
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1090
1091 /* The status of a tentative parse. */
1092
1093 typedef enum cp_parser_status_kind
1094 {
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1100 has occurred. */
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1103
1104 /* Context that is saved and restored when parsing tentatively. */
1105
1106 typedef struct cp_parser_context GTY (())
1107 {
1108 /* If this is a tentative parsing context, the status of the
1109 tentative parse. */
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1115 tree object_type;
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118 } cp_parser_context;
1119
1120 /* Prototypes. */
1121
1122 /* Constructors and destructors. */
1123
1124 static cp_parser_context *cp_parser_context_new
1125 (cp_parser_context *);
1126
1127 /* Class variables. */
1128
1129 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130
1131 /* Constructors and destructors. */
1132
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1135
1136 static cp_parser_context *
1137 cp_parser_context_new (cp_parser_context* next)
1138 {
1139 cp_parser_context *context;
1140
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list != NULL)
1143 {
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
1147 memset (context, 0, sizeof (*context));
1148 }
1149 else
1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1155 if (next)
1156 {
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
1160 /* Thread the stack. */
1161 context->next = next;
1162 }
1163
1164 return context;
1165 }
1166
1167 /* The cp_parser structure represents the C++ parser. */
1168
1169 typedef struct cp_parser GTY(())
1170 {
1171 /* The lexer from which we are obtaining tokens. */
1172 cp_lexer *lexer;
1173
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1178
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1186 tree scope;
1187
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1193 tree object_scope;
1194 tree qualifying_scope;
1195
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1198
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1206
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1210
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1215
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
1218 them permissible. */
1219 bool default_arg_ok_p;
1220
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
1223 bool integral_constant_expression_p;
1224
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
1229 bool allow_non_integral_constant_expression_p;
1230
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
1233 bool non_integral_constant_expression_p;
1234
1235 /* TRUE if local variable names and `this' are forbidden in the
1236 current context. */
1237 bool local_variables_forbidden_p;
1238
1239 /* TRUE if the declaration we are parsing is part of a
1240 linkage-specification of the form `extern string-literal
1241 declaration'. */
1242 bool in_unbraced_linkage_specification_p;
1243
1244 /* TRUE if we are presently parsing a declarator, after the
1245 direct-declarator. */
1246 bool in_declarator_p;
1247
1248 /* TRUE if we are presently parsing a template-argument-list. */
1249 bool in_template_argument_list_p;
1250
1251 /* TRUE if we are presently parsing the body of an
1252 iteration-statement. */
1253 bool in_iteration_statement_p;
1254
1255 /* TRUE if we are presently parsing the body of a switch
1256 statement. */
1257 bool in_switch_statement_p;
1258
1259 /* TRUE if we are parsing a type-id in an expression context. In
1260 such a situation, both "type (expr)" and "type (type)" are valid
1261 alternatives. */
1262 bool in_type_id_in_expr_p;
1263
1264 /* If non-NULL, then we are parsing a construct where new type
1265 definitions are not permitted. The string stored here will be
1266 issued as an error message if a type is defined. */
1267 const char *type_definition_forbidden_message;
1268
1269 /* A list of lists. The outer list is a stack, used for member
1270 functions of local classes. At each level there are two sub-list,
1271 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1272 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1273 TREE_VALUE's. The functions are chained in reverse declaration
1274 order.
1275
1276 The TREE_PURPOSE sublist contains those functions with default
1277 arguments that need post processing, and the TREE_VALUE sublist
1278 contains those functions with definitions that need post
1279 processing.
1280
1281 These lists can only be processed once the outermost class being
1282 defined is complete. */
1283 tree unparsed_functions_queues;
1284
1285 /* The number of classes whose definitions are currently in
1286 progress. */
1287 unsigned num_classes_being_defined;
1288
1289 /* The number of template parameter lists that apply directly to the
1290 current declaration. */
1291 unsigned num_template_parameter_lists;
1292 } cp_parser;
1293
1294 /* The type of a function that parses some kind of expression. */
1295 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1296
1297 /* Prototypes. */
1298
1299 /* Constructors and destructors. */
1300
1301 static cp_parser *cp_parser_new
1302 (void);
1303
1304 /* Routines to parse various constructs.
1305
1306 Those that return `tree' will return the error_mark_node (rather
1307 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1308 Sometimes, they will return an ordinary node if error-recovery was
1309 attempted, even though a parse error occurred. So, to check
1310 whether or not a parse error occurred, you should always use
1311 cp_parser_error_occurred. If the construct is optional (indicated
1312 either by an `_opt' in the name of the function that does the
1313 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1314 the construct is not present. */
1315
1316 /* Lexical conventions [gram.lex] */
1317
1318 static tree cp_parser_identifier
1319 (cp_parser *);
1320
1321 /* Basic concepts [gram.basic] */
1322
1323 static bool cp_parser_translation_unit
1324 (cp_parser *);
1325
1326 /* Expressions [gram.expr] */
1327
1328 static tree cp_parser_primary_expression
1329 (cp_parser *, cp_id_kind *, tree *);
1330 static tree cp_parser_id_expression
1331 (cp_parser *, bool, bool, bool *, bool);
1332 static tree cp_parser_unqualified_id
1333 (cp_parser *, bool, bool, bool);
1334 static tree cp_parser_nested_name_specifier_opt
1335 (cp_parser *, bool, bool, bool, bool);
1336 static tree cp_parser_nested_name_specifier
1337 (cp_parser *, bool, bool, bool, bool);
1338 static tree cp_parser_class_or_namespace_name
1339 (cp_parser *, bool, bool, bool, bool, bool);
1340 static tree cp_parser_postfix_expression
1341 (cp_parser *, bool);
1342 static tree cp_parser_postfix_open_square_expression
1343 (cp_parser *, tree, bool);
1344 static tree cp_parser_postfix_dot_deref_expression
1345 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1346 static tree cp_parser_parenthesized_expression_list
1347 (cp_parser *, bool, bool *);
1348 static void cp_parser_pseudo_destructor_name
1349 (cp_parser *, tree *, tree *);
1350 static tree cp_parser_unary_expression
1351 (cp_parser *, bool);
1352 static enum tree_code cp_parser_unary_operator
1353 (cp_token *);
1354 static tree cp_parser_new_expression
1355 (cp_parser *);
1356 static tree cp_parser_new_placement
1357 (cp_parser *);
1358 static tree cp_parser_new_type_id
1359 (cp_parser *);
1360 static tree cp_parser_new_declarator_opt
1361 (cp_parser *);
1362 static tree cp_parser_direct_new_declarator
1363 (cp_parser *);
1364 static tree cp_parser_new_initializer
1365 (cp_parser *);
1366 static tree cp_parser_delete_expression
1367 (cp_parser *);
1368 static tree cp_parser_cast_expression
1369 (cp_parser *, bool);
1370 static tree cp_parser_pm_expression
1371 (cp_parser *);
1372 static tree cp_parser_multiplicative_expression
1373 (cp_parser *);
1374 static tree cp_parser_additive_expression
1375 (cp_parser *);
1376 static tree cp_parser_shift_expression
1377 (cp_parser *);
1378 static tree cp_parser_relational_expression
1379 (cp_parser *);
1380 static tree cp_parser_equality_expression
1381 (cp_parser *);
1382 static tree cp_parser_and_expression
1383 (cp_parser *);
1384 static tree cp_parser_exclusive_or_expression
1385 (cp_parser *);
1386 static tree cp_parser_inclusive_or_expression
1387 (cp_parser *);
1388 static tree cp_parser_logical_and_expression
1389 (cp_parser *);
1390 static tree cp_parser_logical_or_expression
1391 (cp_parser *);
1392 static tree cp_parser_question_colon_clause
1393 (cp_parser *, tree);
1394 static tree cp_parser_assignment_expression
1395 (cp_parser *);
1396 static enum tree_code cp_parser_assignment_operator_opt
1397 (cp_parser *);
1398 static tree cp_parser_expression
1399 (cp_parser *);
1400 static tree cp_parser_constant_expression
1401 (cp_parser *, bool, bool *);
1402 static tree cp_parser_builtin_offsetof
1403 (cp_parser *);
1404
1405 /* Statements [gram.stmt.stmt] */
1406
1407 static void cp_parser_statement
1408 (cp_parser *, bool);
1409 static tree cp_parser_labeled_statement
1410 (cp_parser *, bool);
1411 static tree cp_parser_expression_statement
1412 (cp_parser *, bool);
1413 static tree cp_parser_compound_statement
1414 (cp_parser *, bool);
1415 static void cp_parser_statement_seq_opt
1416 (cp_parser *, bool);
1417 static tree cp_parser_selection_statement
1418 (cp_parser *);
1419 static tree cp_parser_condition
1420 (cp_parser *);
1421 static tree cp_parser_iteration_statement
1422 (cp_parser *);
1423 static void cp_parser_for_init_statement
1424 (cp_parser *);
1425 static tree cp_parser_jump_statement
1426 (cp_parser *);
1427 static void cp_parser_declaration_statement
1428 (cp_parser *);
1429
1430 static tree cp_parser_implicitly_scoped_statement
1431 (cp_parser *);
1432 static void cp_parser_already_scoped_statement
1433 (cp_parser *);
1434
1435 /* Declarations [gram.dcl.dcl] */
1436
1437 static void cp_parser_declaration_seq_opt
1438 (cp_parser *);
1439 static void cp_parser_declaration
1440 (cp_parser *);
1441 static void cp_parser_block_declaration
1442 (cp_parser *, bool);
1443 static void cp_parser_simple_declaration
1444 (cp_parser *, bool);
1445 static tree cp_parser_decl_specifier_seq
1446 (cp_parser *, cp_parser_flags, tree *, int *);
1447 static tree cp_parser_storage_class_specifier_opt
1448 (cp_parser *);
1449 static tree cp_parser_function_specifier_opt
1450 (cp_parser *);
1451 static tree cp_parser_type_specifier
1452 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1453 static tree cp_parser_simple_type_specifier
1454 (cp_parser *, cp_parser_flags, bool);
1455 static tree cp_parser_type_name
1456 (cp_parser *);
1457 static tree cp_parser_elaborated_type_specifier
1458 (cp_parser *, bool, bool);
1459 static tree cp_parser_enum_specifier
1460 (cp_parser *);
1461 static void cp_parser_enumerator_list
1462 (cp_parser *, tree);
1463 static void cp_parser_enumerator_definition
1464 (cp_parser *, tree);
1465 static tree cp_parser_namespace_name
1466 (cp_parser *);
1467 static void cp_parser_namespace_definition
1468 (cp_parser *);
1469 static void cp_parser_namespace_body
1470 (cp_parser *);
1471 static tree cp_parser_qualified_namespace_specifier
1472 (cp_parser *);
1473 static void cp_parser_namespace_alias_definition
1474 (cp_parser *);
1475 static void cp_parser_using_declaration
1476 (cp_parser *);
1477 static void cp_parser_using_directive
1478 (cp_parser *);
1479 static void cp_parser_asm_definition
1480 (cp_parser *);
1481 static void cp_parser_linkage_specification
1482 (cp_parser *);
1483
1484 /* Declarators [gram.dcl.decl] */
1485
1486 static tree cp_parser_init_declarator
1487 (cp_parser *, tree, tree, bool, bool, int, bool *);
1488 static tree cp_parser_declarator
1489 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1490 static tree cp_parser_direct_declarator
1491 (cp_parser *, cp_parser_declarator_kind, int *);
1492 static enum tree_code cp_parser_ptr_operator
1493 (cp_parser *, tree *, tree *);
1494 static tree cp_parser_cv_qualifier_seq_opt
1495 (cp_parser *);
1496 static tree cp_parser_cv_qualifier_opt
1497 (cp_parser *);
1498 static tree cp_parser_declarator_id
1499 (cp_parser *);
1500 static tree cp_parser_type_id
1501 (cp_parser *);
1502 static tree cp_parser_type_specifier_seq
1503 (cp_parser *);
1504 static tree cp_parser_parameter_declaration_clause
1505 (cp_parser *);
1506 static tree cp_parser_parameter_declaration_list
1507 (cp_parser *);
1508 static tree cp_parser_parameter_declaration
1509 (cp_parser *, bool, bool *);
1510 static void cp_parser_function_body
1511 (cp_parser *);
1512 static tree cp_parser_initializer
1513 (cp_parser *, bool *, bool *);
1514 static tree cp_parser_initializer_clause
1515 (cp_parser *, bool *);
1516 static tree cp_parser_initializer_list
1517 (cp_parser *, bool *);
1518
1519 static bool cp_parser_ctor_initializer_opt_and_function_body
1520 (cp_parser *);
1521
1522 /* Classes [gram.class] */
1523
1524 static tree cp_parser_class_name
1525 (cp_parser *, bool, bool, bool, bool, bool, bool);
1526 static tree cp_parser_class_specifier
1527 (cp_parser *);
1528 static tree cp_parser_class_head
1529 (cp_parser *, bool *, tree *);
1530 static enum tag_types cp_parser_class_key
1531 (cp_parser *);
1532 static void cp_parser_member_specification_opt
1533 (cp_parser *);
1534 static void cp_parser_member_declaration
1535 (cp_parser *);
1536 static tree cp_parser_pure_specifier
1537 (cp_parser *);
1538 static tree cp_parser_constant_initializer
1539 (cp_parser *);
1540
1541 /* Derived classes [gram.class.derived] */
1542
1543 static tree cp_parser_base_clause
1544 (cp_parser *);
1545 static tree cp_parser_base_specifier
1546 (cp_parser *);
1547
1548 /* Special member functions [gram.special] */
1549
1550 static tree cp_parser_conversion_function_id
1551 (cp_parser *);
1552 static tree cp_parser_conversion_type_id
1553 (cp_parser *);
1554 static tree cp_parser_conversion_declarator_opt
1555 (cp_parser *);
1556 static bool cp_parser_ctor_initializer_opt
1557 (cp_parser *);
1558 static void cp_parser_mem_initializer_list
1559 (cp_parser *);
1560 static tree cp_parser_mem_initializer
1561 (cp_parser *);
1562 static tree cp_parser_mem_initializer_id
1563 (cp_parser *);
1564
1565 /* Overloading [gram.over] */
1566
1567 static tree cp_parser_operator_function_id
1568 (cp_parser *);
1569 static tree cp_parser_operator
1570 (cp_parser *);
1571
1572 /* Templates [gram.temp] */
1573
1574 static void cp_parser_template_declaration
1575 (cp_parser *, bool);
1576 static tree cp_parser_template_parameter_list
1577 (cp_parser *);
1578 static tree cp_parser_template_parameter
1579 (cp_parser *);
1580 static tree cp_parser_type_parameter
1581 (cp_parser *);
1582 static tree cp_parser_template_id
1583 (cp_parser *, bool, bool, bool);
1584 static tree cp_parser_template_name
1585 (cp_parser *, bool, bool, bool, bool *);
1586 static tree cp_parser_template_argument_list
1587 (cp_parser *);
1588 static tree cp_parser_template_argument
1589 (cp_parser *);
1590 static void cp_parser_explicit_instantiation
1591 (cp_parser *);
1592 static void cp_parser_explicit_specialization
1593 (cp_parser *);
1594
1595 /* Exception handling [gram.exception] */
1596
1597 static tree cp_parser_try_block
1598 (cp_parser *);
1599 static bool cp_parser_function_try_block
1600 (cp_parser *);
1601 static void cp_parser_handler_seq
1602 (cp_parser *);
1603 static void cp_parser_handler
1604 (cp_parser *);
1605 static tree cp_parser_exception_declaration
1606 (cp_parser *);
1607 static tree cp_parser_throw_expression
1608 (cp_parser *);
1609 static tree cp_parser_exception_specification_opt
1610 (cp_parser *);
1611 static tree cp_parser_type_id_list
1612 (cp_parser *);
1613
1614 /* GNU Extensions */
1615
1616 static tree cp_parser_asm_specification_opt
1617 (cp_parser *);
1618 static tree cp_parser_asm_operand_list
1619 (cp_parser *);
1620 static tree cp_parser_asm_clobber_list
1621 (cp_parser *);
1622 static tree cp_parser_attributes_opt
1623 (cp_parser *);
1624 static tree cp_parser_attribute_list
1625 (cp_parser *);
1626 static bool cp_parser_extension_opt
1627 (cp_parser *, int *);
1628 static void cp_parser_label_declaration
1629 (cp_parser *);
1630
1631 /* Utility Routines */
1632
1633 static tree cp_parser_lookup_name
1634 (cp_parser *, tree, bool, bool, bool, bool);
1635 static tree cp_parser_lookup_name_simple
1636 (cp_parser *, tree);
1637 static tree cp_parser_maybe_treat_template_as_class
1638 (tree, bool);
1639 static bool cp_parser_check_declarator_template_parameters
1640 (cp_parser *, tree);
1641 static bool cp_parser_check_template_parameters
1642 (cp_parser *, unsigned);
1643 static tree cp_parser_simple_cast_expression
1644 (cp_parser *);
1645 static tree cp_parser_binary_expression
1646 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1647 static tree cp_parser_global_scope_opt
1648 (cp_parser *, bool);
1649 static bool cp_parser_constructor_declarator_p
1650 (cp_parser *, bool);
1651 static tree cp_parser_function_definition_from_specifiers_and_declarator
1652 (cp_parser *, tree, tree, tree);
1653 static tree cp_parser_function_definition_after_declarator
1654 (cp_parser *, bool);
1655 static void cp_parser_template_declaration_after_export
1656 (cp_parser *, bool);
1657 static tree cp_parser_single_declaration
1658 (cp_parser *, bool, bool *);
1659 static tree cp_parser_functional_cast
1660 (cp_parser *, tree);
1661 static tree cp_parser_save_member_function_body
1662 (cp_parser *, tree, tree, tree);
1663 static tree cp_parser_enclosed_template_argument_list
1664 (cp_parser *);
1665 static void cp_parser_save_default_args
1666 (cp_parser *, tree);
1667 static void cp_parser_late_parsing_for_member
1668 (cp_parser *, tree);
1669 static void cp_parser_late_parsing_default_args
1670 (cp_parser *, tree);
1671 static tree cp_parser_sizeof_operand
1672 (cp_parser *, enum rid);
1673 static bool cp_parser_declares_only_class_p
1674 (cp_parser *);
1675 static bool cp_parser_friend_p
1676 (tree);
1677 static cp_token *cp_parser_require
1678 (cp_parser *, enum cpp_ttype, const char *);
1679 static cp_token *cp_parser_require_keyword
1680 (cp_parser *, enum rid, const char *);
1681 static bool cp_parser_token_starts_function_definition_p
1682 (cp_token *);
1683 static bool cp_parser_next_token_starts_class_definition_p
1684 (cp_parser *);
1685 static bool cp_parser_next_token_ends_template_argument_p
1686 (cp_parser *);
1687 static bool cp_parser_nth_token_starts_template_argument_list_p
1688 (cp_parser *, size_t);
1689 static enum tag_types cp_parser_token_is_class_key
1690 (cp_token *);
1691 static void cp_parser_check_class_key
1692 (enum tag_types, tree type);
1693 static void cp_parser_check_access_in_redeclaration
1694 (tree type);
1695 static bool cp_parser_optional_template_keyword
1696 (cp_parser *);
1697 static void cp_parser_pre_parsed_nested_name_specifier
1698 (cp_parser *);
1699 static void cp_parser_cache_group
1700 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1701 static void cp_parser_parse_tentatively
1702 (cp_parser *);
1703 static void cp_parser_commit_to_tentative_parse
1704 (cp_parser *);
1705 static void cp_parser_abort_tentative_parse
1706 (cp_parser *);
1707 static bool cp_parser_parse_definitely
1708 (cp_parser *);
1709 static inline bool cp_parser_parsing_tentatively
1710 (cp_parser *);
1711 static bool cp_parser_committed_to_tentative_parse
1712 (cp_parser *);
1713 static void cp_parser_error
1714 (cp_parser *, const char *);
1715 static void cp_parser_name_lookup_error
1716 (cp_parser *, tree, tree, const char *);
1717 static bool cp_parser_simulate_error
1718 (cp_parser *);
1719 static void cp_parser_check_type_definition
1720 (cp_parser *);
1721 static void cp_parser_check_for_definition_in_return_type
1722 (tree, int);
1723 static void cp_parser_check_for_invalid_template_id
1724 (cp_parser *, tree);
1725 static bool cp_parser_non_integral_constant_expression
1726 (cp_parser *, const char *);
1727 static void cp_parser_diagnose_invalid_type_name
1728 (cp_parser *, tree, tree);
1729 static bool cp_parser_parse_and_diagnose_invalid_type_name
1730 (cp_parser *);
1731 static int cp_parser_skip_to_closing_parenthesis
1732 (cp_parser *, bool, bool, bool);
1733 static void cp_parser_skip_to_end_of_statement
1734 (cp_parser *);
1735 static void cp_parser_consume_semicolon_at_end_of_statement
1736 (cp_parser *);
1737 static void cp_parser_skip_to_end_of_block_or_statement
1738 (cp_parser *);
1739 static void cp_parser_skip_to_closing_brace
1740 (cp_parser *);
1741 static void cp_parser_skip_until_found
1742 (cp_parser *, enum cpp_ttype, const char *);
1743 static bool cp_parser_error_occurred
1744 (cp_parser *);
1745 static bool cp_parser_allow_gnu_extensions_p
1746 (cp_parser *);
1747 static bool cp_parser_is_string_literal
1748 (cp_token *);
1749 static bool cp_parser_is_keyword
1750 (cp_token *, enum rid);
1751 static tree cp_parser_make_typename_type
1752 (cp_parser *, tree, tree);
1753
1754 /* Returns nonzero if we are parsing tentatively. */
1755
1756 static inline bool
1757 cp_parser_parsing_tentatively (cp_parser* parser)
1758 {
1759 return parser->context->next != NULL;
1760 }
1761
1762 /* Returns nonzero if TOKEN is a string literal. */
1763
1764 static bool
1765 cp_parser_is_string_literal (cp_token* token)
1766 {
1767 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1768 }
1769
1770 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1771
1772 static bool
1773 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1774 {
1775 return token->keyword == keyword;
1776 }
1777
1778 /* Issue the indicated error MESSAGE. */
1779
1780 static void
1781 cp_parser_error (cp_parser* parser, const char* message)
1782 {
1783 /* Output the MESSAGE -- unless we're parsing tentatively. */
1784 if (!cp_parser_simulate_error (parser))
1785 {
1786 cp_token *token;
1787 token = cp_lexer_peek_token (parser->lexer);
1788 c_parse_error (message,
1789 /* Because c_parser_error does not understand
1790 CPP_KEYWORD, keywords are treated like
1791 identifiers. */
1792 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1793 token->value);
1794 }
1795 }
1796
1797 /* Issue an error about name-lookup failing. NAME is the
1798 IDENTIFIER_NODE DECL is the result of
1799 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1800 the thing that we hoped to find. */
1801
1802 static void
1803 cp_parser_name_lookup_error (cp_parser* parser,
1804 tree name,
1805 tree decl,
1806 const char* desired)
1807 {
1808 /* If name lookup completely failed, tell the user that NAME was not
1809 declared. */
1810 if (decl == error_mark_node)
1811 {
1812 if (parser->scope && parser->scope != global_namespace)
1813 error ("`%D::%D' has not been declared",
1814 parser->scope, name);
1815 else if (parser->scope == global_namespace)
1816 error ("`::%D' has not been declared", name);
1817 else
1818 error ("`%D' has not been declared", name);
1819 }
1820 else if (parser->scope && parser->scope != global_namespace)
1821 error ("`%D::%D' %s", parser->scope, name, desired);
1822 else if (parser->scope == global_namespace)
1823 error ("`::%D' %s", name, desired);
1824 else
1825 error ("`%D' %s", name, desired);
1826 }
1827
1828 /* If we are parsing tentatively, remember that an error has occurred
1829 during this tentative parse. Returns true if the error was
1830 simulated; false if a message should be issued by the caller. */
1831
1832 static bool
1833 cp_parser_simulate_error (cp_parser* parser)
1834 {
1835 if (cp_parser_parsing_tentatively (parser)
1836 && !cp_parser_committed_to_tentative_parse (parser))
1837 {
1838 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1839 return true;
1840 }
1841 return false;
1842 }
1843
1844 /* This function is called when a type is defined. If type
1845 definitions are forbidden at this point, an error message is
1846 issued. */
1847
1848 static void
1849 cp_parser_check_type_definition (cp_parser* parser)
1850 {
1851 /* If types are forbidden here, issue a message. */
1852 if (parser->type_definition_forbidden_message)
1853 /* Use `%s' to print the string in case there are any escape
1854 characters in the message. */
1855 error ("%s", parser->type_definition_forbidden_message);
1856 }
1857
1858 /* This function is called when a declaration is parsed. If
1859 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1860 indicates that a type was defined in the decl-specifiers for DECL,
1861 then an error is issued. */
1862
1863 static void
1864 cp_parser_check_for_definition_in_return_type (tree declarator,
1865 int declares_class_or_enum)
1866 {
1867 /* [dcl.fct] forbids type definitions in return types.
1868 Unfortunately, it's not easy to know whether or not we are
1869 processing a return type until after the fact. */
1870 while (declarator
1871 && (TREE_CODE (declarator) == INDIRECT_REF
1872 || TREE_CODE (declarator) == ADDR_EXPR))
1873 declarator = TREE_OPERAND (declarator, 0);
1874 if (declarator
1875 && TREE_CODE (declarator) == CALL_EXPR
1876 && declares_class_or_enum & 2)
1877 error ("new types may not be defined in a return type");
1878 }
1879
1880 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1881 "<" in any valid C++ program. If the next token is indeed "<",
1882 issue a message warning the user about what appears to be an
1883 invalid attempt to form a template-id. */
1884
1885 static void
1886 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1887 tree type)
1888 {
1889 ptrdiff_t start;
1890 cp_token *token;
1891
1892 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1893 {
1894 if (TYPE_P (type))
1895 error ("`%T' is not a template", type);
1896 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1897 error ("`%E' is not a template", type);
1898 else
1899 error ("invalid template-id");
1900 /* Remember the location of the invalid "<". */
1901 if (cp_parser_parsing_tentatively (parser)
1902 && !cp_parser_committed_to_tentative_parse (parser))
1903 {
1904 token = cp_lexer_peek_token (parser->lexer);
1905 token = cp_lexer_prev_token (parser->lexer, token);
1906 start = cp_lexer_token_difference (parser->lexer,
1907 parser->lexer->first_token,
1908 token);
1909 }
1910 else
1911 start = -1;
1912 /* Consume the "<". */
1913 cp_lexer_consume_token (parser->lexer);
1914 /* Parse the template arguments. */
1915 cp_parser_enclosed_template_argument_list (parser);
1916 /* Permanently remove the invalid template arguments so that
1917 this error message is not issued again. */
1918 if (start >= 0)
1919 {
1920 token = cp_lexer_advance_token (parser->lexer,
1921 parser->lexer->first_token,
1922 start);
1923 cp_lexer_purge_tokens_after (parser->lexer, token);
1924 }
1925 }
1926 }
1927
1928 /* If parsing an integral constant-expression, issue an error message
1929 about the fact that THING appeared and return true. Otherwise,
1930 return false, marking the current expression as non-constant. */
1931
1932 static bool
1933 cp_parser_non_integral_constant_expression (cp_parser *parser,
1934 const char *thing)
1935 {
1936 if (parser->integral_constant_expression_p)
1937 {
1938 if (!parser->allow_non_integral_constant_expression_p)
1939 {
1940 error ("%s cannot appear in a constant-expression", thing);
1941 return true;
1942 }
1943 parser->non_integral_constant_expression_p = true;
1944 }
1945 return false;
1946 }
1947
1948 /* Emit a diagnostic for an invalid type name. Consider also if it is
1949 qualified or not and the result of a lookup, to provide a better
1950 message. */
1951
1952 static void
1953 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1954 {
1955 tree decl, old_scope;
1956 /* Try to lookup the identifier. */
1957 old_scope = parser->scope;
1958 parser->scope = scope;
1959 decl = cp_parser_lookup_name_simple (parser, id);
1960 parser->scope = old_scope;
1961 /* If the lookup found a template-name, it means that the user forgot
1962 to specify an argument list. Emit an useful error message. */
1963 if (TREE_CODE (decl) == TEMPLATE_DECL)
1964 error ("invalid use of template-name `%E' without an argument list",
1965 decl);
1966 else if (!parser->scope)
1967 {
1968 /* Issue an error message. */
1969 error ("`%E' does not name a type", id);
1970 /* If we're in a template class, it's possible that the user was
1971 referring to a type from a base class. For example:
1972
1973 template <typename T> struct A { typedef T X; };
1974 template <typename T> struct B : public A<T> { X x; };
1975
1976 The user should have said "typename A<T>::X". */
1977 if (processing_template_decl && current_class_type)
1978 {
1979 tree b;
1980
1981 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1982 b;
1983 b = TREE_CHAIN (b))
1984 {
1985 tree base_type = BINFO_TYPE (b);
1986 if (CLASS_TYPE_P (base_type)
1987 && dependent_type_p (base_type))
1988 {
1989 tree field;
1990 /* Go from a particular instantiation of the
1991 template (which will have an empty TYPE_FIELDs),
1992 to the main version. */
1993 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1994 for (field = TYPE_FIELDS (base_type);
1995 field;
1996 field = TREE_CHAIN (field))
1997 if (TREE_CODE (field) == TYPE_DECL
1998 && DECL_NAME (field) == id)
1999 {
2000 inform ("(perhaps `typename %T::%E' was intended)",
2001 BINFO_TYPE (b), id);
2002 break;
2003 }
2004 if (field)
2005 break;
2006 }
2007 }
2008 }
2009 }
2010 /* Here we diagnose qualified-ids where the scope is actually correct,
2011 but the identifier does not resolve to a valid type name. */
2012 else
2013 {
2014 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2015 error ("`%E' in namespace `%E' does not name a type",
2016 id, parser->scope);
2017 else if (TYPE_P (parser->scope))
2018 error ("`%E' in class `%T' does not name a type",
2019 id, parser->scope);
2020 else
2021 abort();
2022 }
2023 }
2024
2025 /* Check for a common situation where a type-name should be present,
2026 but is not, and issue a sensible error message. Returns true if an
2027 invalid type-name was detected.
2028
2029 The situation handled by this function are variable declarations of the
2030 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2031 Usually, `ID' should name a type, but if we got here it means that it
2032 does not. We try to emit the best possible error message depending on
2033 how exactly the id-expression looks like.
2034 */
2035
2036 static bool
2037 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2038 {
2039 tree id;
2040
2041 cp_parser_parse_tentatively (parser);
2042 id = cp_parser_id_expression (parser,
2043 /*template_keyword_p=*/false,
2044 /*check_dependency_p=*/true,
2045 /*template_p=*/NULL,
2046 /*declarator_p=*/true);
2047 /* After the id-expression, there should be a plain identifier,
2048 otherwise this is not a simple variable declaration. Also, if
2049 the scope is dependent, we cannot do much. */
2050 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2051 || (parser->scope && TYPE_P (parser->scope)
2052 && dependent_type_p (parser->scope)))
2053 {
2054 cp_parser_abort_tentative_parse (parser);
2055 return false;
2056 }
2057 if (!cp_parser_parse_definitely (parser))
2058 return false;
2059
2060 /* If we got here, this cannot be a valid variable declaration, thus
2061 the cp_parser_id_expression must have resolved to a plain identifier
2062 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2063 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2064 /* Emit a diagnostic for the invalid type. */
2065 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2066 /* Skip to the end of the declaration; there's no point in
2067 trying to process it. */
2068 cp_parser_skip_to_end_of_block_or_statement (parser);
2069 return true;
2070 }
2071
2072 /* Consume tokens up to, and including, the next non-nested closing `)'.
2073 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2074 are doing error recovery. Returns -1 if OR_COMMA is true and we
2075 found an unnested comma. */
2076
2077 static int
2078 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2079 bool recovering,
2080 bool or_comma,
2081 bool consume_paren)
2082 {
2083 unsigned paren_depth = 0;
2084 unsigned brace_depth = 0;
2085
2086 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2087 && !cp_parser_committed_to_tentative_parse (parser))
2088 return 0;
2089
2090 while (true)
2091 {
2092 cp_token *token;
2093
2094 /* If we've run out of tokens, then there is no closing `)'. */
2095 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2096 return 0;
2097
2098 token = cp_lexer_peek_token (parser->lexer);
2099
2100 /* This matches the processing in skip_to_end_of_statement. */
2101 if (token->type == CPP_SEMICOLON && !brace_depth)
2102 return 0;
2103 if (token->type == CPP_OPEN_BRACE)
2104 ++brace_depth;
2105 if (token->type == CPP_CLOSE_BRACE)
2106 {
2107 if (!brace_depth--)
2108 return 0;
2109 }
2110 if (recovering && or_comma && token->type == CPP_COMMA
2111 && !brace_depth && !paren_depth)
2112 return -1;
2113
2114 if (!brace_depth)
2115 {
2116 /* If it is an `(', we have entered another level of nesting. */
2117 if (token->type == CPP_OPEN_PAREN)
2118 ++paren_depth;
2119 /* If it is a `)', then we might be done. */
2120 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2121 {
2122 if (consume_paren)
2123 cp_lexer_consume_token (parser->lexer);
2124 return 1;
2125 }
2126 }
2127
2128 /* Consume the token. */
2129 cp_lexer_consume_token (parser->lexer);
2130 }
2131 }
2132
2133 /* Consume tokens until we reach the end of the current statement.
2134 Normally, that will be just before consuming a `;'. However, if a
2135 non-nested `}' comes first, then we stop before consuming that. */
2136
2137 static void
2138 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2139 {
2140 unsigned nesting_depth = 0;
2141
2142 while (true)
2143 {
2144 cp_token *token;
2145
2146 /* Peek at the next token. */
2147 token = cp_lexer_peek_token (parser->lexer);
2148 /* If we've run out of tokens, stop. */
2149 if (token->type == CPP_EOF)
2150 break;
2151 /* If the next token is a `;', we have reached the end of the
2152 statement. */
2153 if (token->type == CPP_SEMICOLON && !nesting_depth)
2154 break;
2155 /* If the next token is a non-nested `}', then we have reached
2156 the end of the current block. */
2157 if (token->type == CPP_CLOSE_BRACE)
2158 {
2159 /* If this is a non-nested `}', stop before consuming it.
2160 That way, when confronted with something like:
2161
2162 { 3 + }
2163
2164 we stop before consuming the closing `}', even though we
2165 have not yet reached a `;'. */
2166 if (nesting_depth == 0)
2167 break;
2168 /* If it is the closing `}' for a block that we have
2169 scanned, stop -- but only after consuming the token.
2170 That way given:
2171
2172 void f g () { ... }
2173 typedef int I;
2174
2175 we will stop after the body of the erroneously declared
2176 function, but before consuming the following `typedef'
2177 declaration. */
2178 if (--nesting_depth == 0)
2179 {
2180 cp_lexer_consume_token (parser->lexer);
2181 break;
2182 }
2183 }
2184 /* If it the next token is a `{', then we are entering a new
2185 block. Consume the entire block. */
2186 else if (token->type == CPP_OPEN_BRACE)
2187 ++nesting_depth;
2188 /* Consume the token. */
2189 cp_lexer_consume_token (parser->lexer);
2190 }
2191 }
2192
2193 /* This function is called at the end of a statement or declaration.
2194 If the next token is a semicolon, it is consumed; otherwise, error
2195 recovery is attempted. */
2196
2197 static void
2198 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2199 {
2200 /* Look for the trailing `;'. */
2201 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2202 {
2203 /* If there is additional (erroneous) input, skip to the end of
2204 the statement. */
2205 cp_parser_skip_to_end_of_statement (parser);
2206 /* If the next token is now a `;', consume it. */
2207 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2208 cp_lexer_consume_token (parser->lexer);
2209 }
2210 }
2211
2212 /* Skip tokens until we have consumed an entire block, or until we
2213 have consumed a non-nested `;'. */
2214
2215 static void
2216 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2217 {
2218 unsigned nesting_depth = 0;
2219
2220 while (true)
2221 {
2222 cp_token *token;
2223
2224 /* Peek at the next token. */
2225 token = cp_lexer_peek_token (parser->lexer);
2226 /* If we've run out of tokens, stop. */
2227 if (token->type == CPP_EOF)
2228 break;
2229 /* If the next token is a `;', we have reached the end of the
2230 statement. */
2231 if (token->type == CPP_SEMICOLON && !nesting_depth)
2232 {
2233 /* Consume the `;'. */
2234 cp_lexer_consume_token (parser->lexer);
2235 break;
2236 }
2237 /* Consume the token. */
2238 token = cp_lexer_consume_token (parser->lexer);
2239 /* If the next token is a non-nested `}', then we have reached
2240 the end of the current block. */
2241 if (token->type == CPP_CLOSE_BRACE
2242 && (nesting_depth == 0 || --nesting_depth == 0))
2243 break;
2244 /* If it the next token is a `{', then we are entering a new
2245 block. Consume the entire block. */
2246 if (token->type == CPP_OPEN_BRACE)
2247 ++nesting_depth;
2248 }
2249 }
2250
2251 /* Skip tokens until a non-nested closing curly brace is the next
2252 token. */
2253
2254 static void
2255 cp_parser_skip_to_closing_brace (cp_parser *parser)
2256 {
2257 unsigned nesting_depth = 0;
2258
2259 while (true)
2260 {
2261 cp_token *token;
2262
2263 /* Peek at the next token. */
2264 token = cp_lexer_peek_token (parser->lexer);
2265 /* If we've run out of tokens, stop. */
2266 if (token->type == CPP_EOF)
2267 break;
2268 /* If the next token is a non-nested `}', then we have reached
2269 the end of the current block. */
2270 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2271 break;
2272 /* If it the next token is a `{', then we are entering a new
2273 block. Consume the entire block. */
2274 else if (token->type == CPP_OPEN_BRACE)
2275 ++nesting_depth;
2276 /* Consume the token. */
2277 cp_lexer_consume_token (parser->lexer);
2278 }
2279 }
2280
2281 /* This is a simple wrapper around make_typename_type. When the id is
2282 an unresolved identifier node, we can provide a superior diagnostic
2283 using cp_parser_diagnose_invalid_type_name. */
2284
2285 static tree
2286 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2287 {
2288 tree result;
2289 if (TREE_CODE (id) == IDENTIFIER_NODE)
2290 {
2291 result = make_typename_type (scope, id, /*complain=*/0);
2292 if (result == error_mark_node)
2293 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2294 return result;
2295 }
2296 return make_typename_type (scope, id, tf_error);
2297 }
2298
2299
2300 /* Create a new C++ parser. */
2301
2302 static cp_parser *
2303 cp_parser_new (void)
2304 {
2305 cp_parser *parser;
2306 cp_lexer *lexer;
2307
2308 /* cp_lexer_new_main is called before calling ggc_alloc because
2309 cp_lexer_new_main might load a PCH file. */
2310 lexer = cp_lexer_new_main ();
2311
2312 parser = ggc_alloc_cleared (sizeof (cp_parser));
2313 parser->lexer = lexer;
2314 parser->context = cp_parser_context_new (NULL);
2315
2316 /* For now, we always accept GNU extensions. */
2317 parser->allow_gnu_extensions_p = 1;
2318
2319 /* The `>' token is a greater-than operator, not the end of a
2320 template-id. */
2321 parser->greater_than_is_operator_p = true;
2322
2323 parser->default_arg_ok_p = true;
2324
2325 /* We are not parsing a constant-expression. */
2326 parser->integral_constant_expression_p = false;
2327 parser->allow_non_integral_constant_expression_p = false;
2328 parser->non_integral_constant_expression_p = false;
2329
2330 /* Local variable names are not forbidden. */
2331 parser->local_variables_forbidden_p = false;
2332
2333 /* We are not processing an `extern "C"' declaration. */
2334 parser->in_unbraced_linkage_specification_p = false;
2335
2336 /* We are not processing a declarator. */
2337 parser->in_declarator_p = false;
2338
2339 /* We are not processing a template-argument-list. */
2340 parser->in_template_argument_list_p = false;
2341
2342 /* We are not in an iteration statement. */
2343 parser->in_iteration_statement_p = false;
2344
2345 /* We are not in a switch statement. */
2346 parser->in_switch_statement_p = false;
2347
2348 /* We are not parsing a type-id inside an expression. */
2349 parser->in_type_id_in_expr_p = false;
2350
2351 /* The unparsed function queue is empty. */
2352 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2353
2354 /* There are no classes being defined. */
2355 parser->num_classes_being_defined = 0;
2356
2357 /* No template parameters apply. */
2358 parser->num_template_parameter_lists = 0;
2359
2360 return parser;
2361 }
2362
2363 /* Lexical conventions [gram.lex] */
2364
2365 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2366 identifier. */
2367
2368 static tree
2369 cp_parser_identifier (cp_parser* parser)
2370 {
2371 cp_token *token;
2372
2373 /* Look for the identifier. */
2374 token = cp_parser_require (parser, CPP_NAME, "identifier");
2375 /* Return the value. */
2376 return token ? token->value : error_mark_node;
2377 }
2378
2379 /* Basic concepts [gram.basic] */
2380
2381 /* Parse a translation-unit.
2382
2383 translation-unit:
2384 declaration-seq [opt]
2385
2386 Returns TRUE if all went well. */
2387
2388 static bool
2389 cp_parser_translation_unit (cp_parser* parser)
2390 {
2391 while (true)
2392 {
2393 cp_parser_declaration_seq_opt (parser);
2394
2395 /* If there are no tokens left then all went well. */
2396 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2397 break;
2398
2399 /* Otherwise, issue an error message. */
2400 cp_parser_error (parser, "expected declaration");
2401 return false;
2402 }
2403
2404 /* Consume the EOF token. */
2405 cp_parser_require (parser, CPP_EOF, "end-of-file");
2406
2407 /* Finish up. */
2408 finish_translation_unit ();
2409
2410 /* All went well. */
2411 return true;
2412 }
2413
2414 /* Expressions [gram.expr] */
2415
2416 /* Parse a primary-expression.
2417
2418 primary-expression:
2419 literal
2420 this
2421 ( expression )
2422 id-expression
2423
2424 GNU Extensions:
2425
2426 primary-expression:
2427 ( compound-statement )
2428 __builtin_va_arg ( assignment-expression , type-id )
2429
2430 literal:
2431 __null
2432
2433 Returns a representation of the expression.
2434
2435 *IDK indicates what kind of id-expression (if any) was present.
2436
2437 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2438 used as the operand of a pointer-to-member. In that case,
2439 *QUALIFYING_CLASS gives the class that is used as the qualifying
2440 class in the pointer-to-member. */
2441
2442 static tree
2443 cp_parser_primary_expression (cp_parser *parser,
2444 cp_id_kind *idk,
2445 tree *qualifying_class)
2446 {
2447 cp_token *token;
2448
2449 /* Assume the primary expression is not an id-expression. */
2450 *idk = CP_ID_KIND_NONE;
2451 /* And that it cannot be used as pointer-to-member. */
2452 *qualifying_class = NULL_TREE;
2453
2454 /* Peek at the next token. */
2455 token = cp_lexer_peek_token (parser->lexer);
2456 switch (token->type)
2457 {
2458 /* literal:
2459 integer-literal
2460 character-literal
2461 floating-literal
2462 string-literal
2463 boolean-literal */
2464 case CPP_CHAR:
2465 case CPP_WCHAR:
2466 case CPP_STRING:
2467 case CPP_WSTRING:
2468 case CPP_NUMBER:
2469 token = cp_lexer_consume_token (parser->lexer);
2470 return token->value;
2471
2472 case CPP_OPEN_PAREN:
2473 {
2474 tree expr;
2475 bool saved_greater_than_is_operator_p;
2476
2477 /* Consume the `('. */
2478 cp_lexer_consume_token (parser->lexer);
2479 /* Within a parenthesized expression, a `>' token is always
2480 the greater-than operator. */
2481 saved_greater_than_is_operator_p
2482 = parser->greater_than_is_operator_p;
2483 parser->greater_than_is_operator_p = true;
2484 /* If we see `( { ' then we are looking at the beginning of
2485 a GNU statement-expression. */
2486 if (cp_parser_allow_gnu_extensions_p (parser)
2487 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2488 {
2489 /* Statement-expressions are not allowed by the standard. */
2490 if (pedantic)
2491 pedwarn ("ISO C++ forbids braced-groups within expressions");
2492
2493 /* And they're not allowed outside of a function-body; you
2494 cannot, for example, write:
2495
2496 int i = ({ int j = 3; j + 1; });
2497
2498 at class or namespace scope. */
2499 if (!at_function_scope_p ())
2500 error ("statement-expressions are allowed only inside functions");
2501 /* Start the statement-expression. */
2502 expr = begin_stmt_expr ();
2503 /* Parse the compound-statement. */
2504 cp_parser_compound_statement (parser, true);
2505 /* Finish up. */
2506 expr = finish_stmt_expr (expr, false);
2507 }
2508 else
2509 {
2510 /* Parse the parenthesized expression. */
2511 expr = cp_parser_expression (parser);
2512 /* Let the front end know that this expression was
2513 enclosed in parentheses. This matters in case, for
2514 example, the expression is of the form `A::B', since
2515 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2516 not. */
2517 finish_parenthesized_expr (expr);
2518 }
2519 /* The `>' token might be the end of a template-id or
2520 template-parameter-list now. */
2521 parser->greater_than_is_operator_p
2522 = saved_greater_than_is_operator_p;
2523 /* Consume the `)'. */
2524 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2525 cp_parser_skip_to_end_of_statement (parser);
2526
2527 return expr;
2528 }
2529
2530 case CPP_KEYWORD:
2531 switch (token->keyword)
2532 {
2533 /* These two are the boolean literals. */
2534 case RID_TRUE:
2535 cp_lexer_consume_token (parser->lexer);
2536 return boolean_true_node;
2537 case RID_FALSE:
2538 cp_lexer_consume_token (parser->lexer);
2539 return boolean_false_node;
2540
2541 /* The `__null' literal. */
2542 case RID_NULL:
2543 cp_lexer_consume_token (parser->lexer);
2544 return null_node;
2545
2546 /* Recognize the `this' keyword. */
2547 case RID_THIS:
2548 cp_lexer_consume_token (parser->lexer);
2549 if (parser->local_variables_forbidden_p)
2550 {
2551 error ("`this' may not be used in this context");
2552 return error_mark_node;
2553 }
2554 /* Pointers cannot appear in constant-expressions. */
2555 if (cp_parser_non_integral_constant_expression (parser,
2556 "`this'"))
2557 return error_mark_node;
2558 return finish_this_expr ();
2559
2560 /* The `operator' keyword can be the beginning of an
2561 id-expression. */
2562 case RID_OPERATOR:
2563 goto id_expression;
2564
2565 case RID_FUNCTION_NAME:
2566 case RID_PRETTY_FUNCTION_NAME:
2567 case RID_C99_FUNCTION_NAME:
2568 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2569 __func__ are the names of variables -- but they are
2570 treated specially. Therefore, they are handled here,
2571 rather than relying on the generic id-expression logic
2572 below. Grammatically, these names are id-expressions.
2573
2574 Consume the token. */
2575 token = cp_lexer_consume_token (parser->lexer);
2576 /* Look up the name. */
2577 return finish_fname (token->value);
2578
2579 case RID_VA_ARG:
2580 {
2581 tree expression;
2582 tree type;
2583
2584 /* The `__builtin_va_arg' construct is used to handle
2585 `va_arg'. Consume the `__builtin_va_arg' token. */
2586 cp_lexer_consume_token (parser->lexer);
2587 /* Look for the opening `('. */
2588 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2589 /* Now, parse the assignment-expression. */
2590 expression = cp_parser_assignment_expression (parser);
2591 /* Look for the `,'. */
2592 cp_parser_require (parser, CPP_COMMA, "`,'");
2593 /* Parse the type-id. */
2594 type = cp_parser_type_id (parser);
2595 /* Look for the closing `)'. */
2596 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2597 /* Using `va_arg' in a constant-expression is not
2598 allowed. */
2599 if (cp_parser_non_integral_constant_expression (parser,
2600 "`va_arg'"))
2601 return error_mark_node;
2602 return build_x_va_arg (expression, type);
2603 }
2604
2605 case RID_OFFSETOF:
2606 return cp_parser_builtin_offsetof (parser);
2607
2608 default:
2609 cp_parser_error (parser, "expected primary-expression");
2610 return error_mark_node;
2611 }
2612
2613 /* An id-expression can start with either an identifier, a
2614 `::' as the beginning of a qualified-id, or the "operator"
2615 keyword. */
2616 case CPP_NAME:
2617 case CPP_SCOPE:
2618 case CPP_TEMPLATE_ID:
2619 case CPP_NESTED_NAME_SPECIFIER:
2620 {
2621 tree id_expression;
2622 tree decl;
2623 const char *error_msg;
2624
2625 id_expression:
2626 /* Parse the id-expression. */
2627 id_expression
2628 = cp_parser_id_expression (parser,
2629 /*template_keyword_p=*/false,
2630 /*check_dependency_p=*/true,
2631 /*template_p=*/NULL,
2632 /*declarator_p=*/false);
2633 if (id_expression == error_mark_node)
2634 return error_mark_node;
2635 /* If we have a template-id, then no further lookup is
2636 required. If the template-id was for a template-class, we
2637 will sometimes have a TYPE_DECL at this point. */
2638 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2639 || TREE_CODE (id_expression) == TYPE_DECL)
2640 decl = id_expression;
2641 /* Look up the name. */
2642 else
2643 {
2644 decl = cp_parser_lookup_name_simple (parser, id_expression);
2645 /* If name lookup gives us a SCOPE_REF, then the
2646 qualifying scope was dependent. Just propagate the
2647 name. */
2648 if (TREE_CODE (decl) == SCOPE_REF)
2649 {
2650 if (TYPE_P (TREE_OPERAND (decl, 0)))
2651 *qualifying_class = TREE_OPERAND (decl, 0);
2652 return decl;
2653 }
2654 /* Check to see if DECL is a local variable in a context
2655 where that is forbidden. */
2656 if (parser->local_variables_forbidden_p
2657 && local_variable_p (decl))
2658 {
2659 /* It might be that we only found DECL because we are
2660 trying to be generous with pre-ISO scoping rules.
2661 For example, consider:
2662
2663 int i;
2664 void g() {
2665 for (int i = 0; i < 10; ++i) {}
2666 extern void f(int j = i);
2667 }
2668
2669 Here, name look up will originally find the out
2670 of scope `i'. We need to issue a warning message,
2671 but then use the global `i'. */
2672 decl = check_for_out_of_scope_variable (decl);
2673 if (local_variable_p (decl))
2674 {
2675 error ("local variable `%D' may not appear in this context",
2676 decl);
2677 return error_mark_node;
2678 }
2679 }
2680 }
2681
2682 decl = finish_id_expression (id_expression, decl, parser->scope,
2683 idk, qualifying_class,
2684 parser->integral_constant_expression_p,
2685 parser->allow_non_integral_constant_expression_p,
2686 &parser->non_integral_constant_expression_p,
2687 &error_msg);
2688 if (error_msg)
2689 cp_parser_error (parser, error_msg);
2690 return decl;
2691 }
2692
2693 /* Anything else is an error. */
2694 default:
2695 cp_parser_error (parser, "expected primary-expression");
2696 return error_mark_node;
2697 }
2698 }
2699
2700 /* Parse an id-expression.
2701
2702 id-expression:
2703 unqualified-id
2704 qualified-id
2705
2706 qualified-id:
2707 :: [opt] nested-name-specifier template [opt] unqualified-id
2708 :: identifier
2709 :: operator-function-id
2710 :: template-id
2711
2712 Return a representation of the unqualified portion of the
2713 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2714 a `::' or nested-name-specifier.
2715
2716 Often, if the id-expression was a qualified-id, the caller will
2717 want to make a SCOPE_REF to represent the qualified-id. This
2718 function does not do this in order to avoid wastefully creating
2719 SCOPE_REFs when they are not required.
2720
2721 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2722 `template' keyword.
2723
2724 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2725 uninstantiated templates.
2726
2727 If *TEMPLATE_P is non-NULL, it is set to true iff the
2728 `template' keyword is used to explicitly indicate that the entity
2729 named is a template.
2730
2731 If DECLARATOR_P is true, the id-expression is appearing as part of
2732 a declarator, rather than as part of an expression. */
2733
2734 static tree
2735 cp_parser_id_expression (cp_parser *parser,
2736 bool template_keyword_p,
2737 bool check_dependency_p,
2738 bool *template_p,
2739 bool declarator_p)
2740 {
2741 bool global_scope_p;
2742 bool nested_name_specifier_p;
2743
2744 /* Assume the `template' keyword was not used. */
2745 if (template_p)
2746 *template_p = false;
2747
2748 /* Look for the optional `::' operator. */
2749 global_scope_p
2750 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2751 != NULL_TREE);
2752 /* Look for the optional nested-name-specifier. */
2753 nested_name_specifier_p
2754 = (cp_parser_nested_name_specifier_opt (parser,
2755 /*typename_keyword_p=*/false,
2756 check_dependency_p,
2757 /*type_p=*/false,
2758 /*is_declarator=*/false)
2759 != NULL_TREE);
2760 /* If there is a nested-name-specifier, then we are looking at
2761 the first qualified-id production. */
2762 if (nested_name_specifier_p)
2763 {
2764 tree saved_scope;
2765 tree saved_object_scope;
2766 tree saved_qualifying_scope;
2767 tree unqualified_id;
2768 bool is_template;
2769
2770 /* See if the next token is the `template' keyword. */
2771 if (!template_p)
2772 template_p = &is_template;
2773 *template_p = cp_parser_optional_template_keyword (parser);
2774 /* Name lookup we do during the processing of the
2775 unqualified-id might obliterate SCOPE. */
2776 saved_scope = parser->scope;
2777 saved_object_scope = parser->object_scope;
2778 saved_qualifying_scope = parser->qualifying_scope;
2779 /* Process the final unqualified-id. */
2780 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2781 check_dependency_p,
2782 declarator_p);
2783 /* Restore the SAVED_SCOPE for our caller. */
2784 parser->scope = saved_scope;
2785 parser->object_scope = saved_object_scope;
2786 parser->qualifying_scope = saved_qualifying_scope;
2787
2788 return unqualified_id;
2789 }
2790 /* Otherwise, if we are in global scope, then we are looking at one
2791 of the other qualified-id productions. */
2792 else if (global_scope_p)
2793 {
2794 cp_token *token;
2795 tree id;
2796
2797 /* Peek at the next token. */
2798 token = cp_lexer_peek_token (parser->lexer);
2799
2800 /* If it's an identifier, and the next token is not a "<", then
2801 we can avoid the template-id case. This is an optimization
2802 for this common case. */
2803 if (token->type == CPP_NAME
2804 && !cp_parser_nth_token_starts_template_argument_list_p
2805 (parser, 2))
2806 return cp_parser_identifier (parser);
2807
2808 cp_parser_parse_tentatively (parser);
2809 /* Try a template-id. */
2810 id = cp_parser_template_id (parser,
2811 /*template_keyword_p=*/false,
2812 /*check_dependency_p=*/true,
2813 declarator_p);
2814 /* If that worked, we're done. */
2815 if (cp_parser_parse_definitely (parser))
2816 return id;
2817
2818 /* Peek at the next token. (Changes in the token buffer may
2819 have invalidated the pointer obtained above.) */
2820 token = cp_lexer_peek_token (parser->lexer);
2821
2822 switch (token->type)
2823 {
2824 case CPP_NAME:
2825 return cp_parser_identifier (parser);
2826
2827 case CPP_KEYWORD:
2828 if (token->keyword == RID_OPERATOR)
2829 return cp_parser_operator_function_id (parser);
2830 /* Fall through. */
2831
2832 default:
2833 cp_parser_error (parser, "expected id-expression");
2834 return error_mark_node;
2835 }
2836 }
2837 else
2838 return cp_parser_unqualified_id (parser, template_keyword_p,
2839 /*check_dependency_p=*/true,
2840 declarator_p);
2841 }
2842
2843 /* Parse an unqualified-id.
2844
2845 unqualified-id:
2846 identifier
2847 operator-function-id
2848 conversion-function-id
2849 ~ class-name
2850 template-id
2851
2852 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2853 keyword, in a construct like `A::template ...'.
2854
2855 Returns a representation of unqualified-id. For the `identifier'
2856 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2857 production a BIT_NOT_EXPR is returned; the operand of the
2858 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2859 other productions, see the documentation accompanying the
2860 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2861 names are looked up in uninstantiated templates. If DECLARATOR_P
2862 is true, the unqualified-id is appearing as part of a declarator,
2863 rather than as part of an expression. */
2864
2865 static tree
2866 cp_parser_unqualified_id (cp_parser* parser,
2867 bool template_keyword_p,
2868 bool check_dependency_p,
2869 bool declarator_p)
2870 {
2871 cp_token *token;
2872
2873 /* Peek at the next token. */
2874 token = cp_lexer_peek_token (parser->lexer);
2875
2876 switch (token->type)
2877 {
2878 case CPP_NAME:
2879 {
2880 tree id;
2881
2882 /* We don't know yet whether or not this will be a
2883 template-id. */
2884 cp_parser_parse_tentatively (parser);
2885 /* Try a template-id. */
2886 id = cp_parser_template_id (parser, template_keyword_p,
2887 check_dependency_p,
2888 declarator_p);
2889 /* If it worked, we're done. */
2890 if (cp_parser_parse_definitely (parser))
2891 return id;
2892 /* Otherwise, it's an ordinary identifier. */
2893 return cp_parser_identifier (parser);
2894 }
2895
2896 case CPP_TEMPLATE_ID:
2897 return cp_parser_template_id (parser, template_keyword_p,
2898 check_dependency_p,
2899 declarator_p);
2900
2901 case CPP_COMPL:
2902 {
2903 tree type_decl;
2904 tree qualifying_scope;
2905 tree object_scope;
2906 tree scope;
2907
2908 /* Consume the `~' token. */
2909 cp_lexer_consume_token (parser->lexer);
2910 /* Parse the class-name. The standard, as written, seems to
2911 say that:
2912
2913 template <typename T> struct S { ~S (); };
2914 template <typename T> S<T>::~S() {}
2915
2916 is invalid, since `~' must be followed by a class-name, but
2917 `S<T>' is dependent, and so not known to be a class.
2918 That's not right; we need to look in uninstantiated
2919 templates. A further complication arises from:
2920
2921 template <typename T> void f(T t) {
2922 t.T::~T();
2923 }
2924
2925 Here, it is not possible to look up `T' in the scope of `T'
2926 itself. We must look in both the current scope, and the
2927 scope of the containing complete expression.
2928
2929 Yet another issue is:
2930
2931 struct S {
2932 int S;
2933 ~S();
2934 };
2935
2936 S::~S() {}
2937
2938 The standard does not seem to say that the `S' in `~S'
2939 should refer to the type `S' and not the data member
2940 `S::S'. */
2941
2942 /* DR 244 says that we look up the name after the "~" in the
2943 same scope as we looked up the qualifying name. That idea
2944 isn't fully worked out; it's more complicated than that. */
2945 scope = parser->scope;
2946 object_scope = parser->object_scope;
2947 qualifying_scope = parser->qualifying_scope;
2948
2949 /* If the name is of the form "X::~X" it's OK. */
2950 if (scope && TYPE_P (scope)
2951 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2952 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2953 == CPP_OPEN_PAREN)
2954 && (cp_lexer_peek_token (parser->lexer)->value
2955 == TYPE_IDENTIFIER (scope)))
2956 {
2957 cp_lexer_consume_token (parser->lexer);
2958 return build_nt (BIT_NOT_EXPR, scope);
2959 }
2960
2961 /* If there was an explicit qualification (S::~T), first look
2962 in the scope given by the qualification (i.e., S). */
2963 if (scope)
2964 {
2965 cp_parser_parse_tentatively (parser);
2966 type_decl = cp_parser_class_name (parser,
2967 /*typename_keyword_p=*/false,
2968 /*template_keyword_p=*/false,
2969 /*type_p=*/false,
2970 /*check_dependency=*/false,
2971 /*class_head_p=*/false,
2972 declarator_p);
2973 if (cp_parser_parse_definitely (parser))
2974 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2975 }
2976 /* In "N::S::~S", look in "N" as well. */
2977 if (scope && qualifying_scope)
2978 {
2979 cp_parser_parse_tentatively (parser);
2980 parser->scope = qualifying_scope;
2981 parser->object_scope = NULL_TREE;
2982 parser->qualifying_scope = NULL_TREE;
2983 type_decl
2984 = cp_parser_class_name (parser,
2985 /*typename_keyword_p=*/false,
2986 /*template_keyword_p=*/false,
2987 /*type_p=*/false,
2988 /*check_dependency=*/false,
2989 /*class_head_p=*/false,
2990 declarator_p);
2991 if (cp_parser_parse_definitely (parser))
2992 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2993 }
2994 /* In "p->S::~T", look in the scope given by "*p" as well. */
2995 else if (object_scope)
2996 {
2997 cp_parser_parse_tentatively (parser);
2998 parser->scope = object_scope;
2999 parser->object_scope = NULL_TREE;
3000 parser->qualifying_scope = NULL_TREE;
3001 type_decl
3002 = cp_parser_class_name (parser,
3003 /*typename_keyword_p=*/false,
3004 /*template_keyword_p=*/false,
3005 /*type_p=*/false,
3006 /*check_dependency=*/false,
3007 /*class_head_p=*/false,
3008 declarator_p);
3009 if (cp_parser_parse_definitely (parser))
3010 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3011 }
3012 /* Look in the surrounding context. */
3013 parser->scope = NULL_TREE;
3014 parser->object_scope = NULL_TREE;
3015 parser->qualifying_scope = NULL_TREE;
3016 type_decl
3017 = cp_parser_class_name (parser,
3018 /*typename_keyword_p=*/false,
3019 /*template_keyword_p=*/false,
3020 /*type_p=*/false,
3021 /*check_dependency=*/false,
3022 /*class_head_p=*/false,
3023 declarator_p);
3024 /* If an error occurred, assume that the name of the
3025 destructor is the same as the name of the qualifying
3026 class. That allows us to keep parsing after running
3027 into ill-formed destructor names. */
3028 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3029 return build_nt (BIT_NOT_EXPR, scope);
3030 else if (type_decl == error_mark_node)
3031 return error_mark_node;
3032
3033 /* [class.dtor]
3034
3035 A typedef-name that names a class shall not be used as the
3036 identifier in the declarator for a destructor declaration. */
3037 if (declarator_p
3038 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3039 && !DECL_SELF_REFERENCE_P (type_decl))
3040 error ("typedef-name `%D' used as destructor declarator",
3041 type_decl);
3042
3043 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3044 }
3045
3046 case CPP_KEYWORD:
3047 if (token->keyword == RID_OPERATOR)
3048 {
3049 tree id;
3050
3051 /* This could be a template-id, so we try that first. */
3052 cp_parser_parse_tentatively (parser);
3053 /* Try a template-id. */
3054 id = cp_parser_template_id (parser, template_keyword_p,
3055 /*check_dependency_p=*/true,
3056 declarator_p);
3057 /* If that worked, we're done. */
3058 if (cp_parser_parse_definitely (parser))
3059 return id;
3060 /* We still don't know whether we're looking at an
3061 operator-function-id or a conversion-function-id. */
3062 cp_parser_parse_tentatively (parser);
3063 /* Try an operator-function-id. */
3064 id = cp_parser_operator_function_id (parser);
3065 /* If that didn't work, try a conversion-function-id. */
3066 if (!cp_parser_parse_definitely (parser))
3067 id = cp_parser_conversion_function_id (parser);
3068
3069 return id;
3070 }
3071 /* Fall through. */
3072
3073 default:
3074 cp_parser_error (parser, "expected unqualified-id");
3075 return error_mark_node;
3076 }
3077 }
3078
3079 /* Parse an (optional) nested-name-specifier.
3080
3081 nested-name-specifier:
3082 class-or-namespace-name :: nested-name-specifier [opt]
3083 class-or-namespace-name :: template nested-name-specifier [opt]
3084
3085 PARSER->SCOPE should be set appropriately before this function is
3086 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3087 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3088 in name lookups.
3089
3090 Sets PARSER->SCOPE to the class (TYPE) or namespace
3091 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3092 it unchanged if there is no nested-name-specifier. Returns the new
3093 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3094
3095 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3096 part of a declaration and/or decl-specifier. */
3097
3098 static tree
3099 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3100 bool typename_keyword_p,
3101 bool check_dependency_p,
3102 bool type_p,
3103 bool is_declaration)
3104 {
3105 bool success = false;
3106 tree access_check = NULL_TREE;
3107 ptrdiff_t start;
3108 cp_token* token;
3109
3110 /* If the next token corresponds to a nested name specifier, there
3111 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3112 false, it may have been true before, in which case something
3113 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3114 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3115 CHECK_DEPENDENCY_P is false, we have to fall through into the
3116 main loop. */
3117 if (check_dependency_p
3118 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3119 {
3120 cp_parser_pre_parsed_nested_name_specifier (parser);
3121 return parser->scope;
3122 }
3123
3124 /* Remember where the nested-name-specifier starts. */
3125 if (cp_parser_parsing_tentatively (parser)
3126 && !cp_parser_committed_to_tentative_parse (parser))
3127 {
3128 token = cp_lexer_peek_token (parser->lexer);
3129 start = cp_lexer_token_difference (parser->lexer,
3130 parser->lexer->first_token,
3131 token);
3132 }
3133 else
3134 start = -1;
3135
3136 push_deferring_access_checks (dk_deferred);
3137
3138 while (true)
3139 {
3140 tree new_scope;
3141 tree old_scope;
3142 tree saved_qualifying_scope;
3143 bool template_keyword_p;
3144
3145 /* Spot cases that cannot be the beginning of a
3146 nested-name-specifier. */
3147 token = cp_lexer_peek_token (parser->lexer);
3148
3149 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3150 the already parsed nested-name-specifier. */
3151 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3152 {
3153 /* Grab the nested-name-specifier and continue the loop. */
3154 cp_parser_pre_parsed_nested_name_specifier (parser);
3155 success = true;
3156 continue;
3157 }
3158
3159 /* Spot cases that cannot be the beginning of a
3160 nested-name-specifier. On the second and subsequent times
3161 through the loop, we look for the `template' keyword. */
3162 if (success && token->keyword == RID_TEMPLATE)
3163 ;
3164 /* A template-id can start a nested-name-specifier. */
3165 else if (token->type == CPP_TEMPLATE_ID)
3166 ;
3167 else
3168 {
3169 /* If the next token is not an identifier, then it is
3170 definitely not a class-or-namespace-name. */
3171 if (token->type != CPP_NAME)
3172 break;
3173 /* If the following token is neither a `<' (to begin a
3174 template-id), nor a `::', then we are not looking at a
3175 nested-name-specifier. */
3176 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3177 if (token->type != CPP_SCOPE
3178 && !cp_parser_nth_token_starts_template_argument_list_p
3179 (parser, 2))
3180 break;
3181 }
3182
3183 /* The nested-name-specifier is optional, so we parse
3184 tentatively. */
3185 cp_parser_parse_tentatively (parser);
3186
3187 /* Look for the optional `template' keyword, if this isn't the
3188 first time through the loop. */
3189 if (success)
3190 template_keyword_p = cp_parser_optional_template_keyword (parser);
3191 else
3192 template_keyword_p = false;
3193
3194 /* Save the old scope since the name lookup we are about to do
3195 might destroy it. */
3196 old_scope = parser->scope;
3197 saved_qualifying_scope = parser->qualifying_scope;
3198 /* Parse the qualifying entity. */
3199 new_scope
3200 = cp_parser_class_or_namespace_name (parser,
3201 typename_keyword_p,
3202 template_keyword_p,
3203 check_dependency_p,
3204 type_p,
3205 is_declaration);
3206 /* Look for the `::' token. */
3207 cp_parser_require (parser, CPP_SCOPE, "`::'");
3208
3209 /* If we found what we wanted, we keep going; otherwise, we're
3210 done. */
3211 if (!cp_parser_parse_definitely (parser))
3212 {
3213 bool error_p = false;
3214
3215 /* Restore the OLD_SCOPE since it was valid before the
3216 failed attempt at finding the last
3217 class-or-namespace-name. */
3218 parser->scope = old_scope;
3219 parser->qualifying_scope = saved_qualifying_scope;
3220 /* If the next token is an identifier, and the one after
3221 that is a `::', then any valid interpretation would have
3222 found a class-or-namespace-name. */
3223 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3224 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3225 == CPP_SCOPE)
3226 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3227 != CPP_COMPL))
3228 {
3229 token = cp_lexer_consume_token (parser->lexer);
3230 if (!error_p)
3231 {
3232 tree decl;
3233
3234 decl = cp_parser_lookup_name_simple (parser, token->value);
3235 if (TREE_CODE (decl) == TEMPLATE_DECL)
3236 error ("`%D' used without template parameters",
3237 decl);
3238 else
3239 cp_parser_name_lookup_error
3240 (parser, token->value, decl,
3241 "is not a class or namespace");
3242 parser->scope = NULL_TREE;
3243 error_p = true;
3244 /* Treat this as a successful nested-name-specifier
3245 due to:
3246
3247 [basic.lookup.qual]
3248
3249 If the name found is not a class-name (clause
3250 _class_) or namespace-name (_namespace.def_), the
3251 program is ill-formed. */
3252 success = true;
3253 }
3254 cp_lexer_consume_token (parser->lexer);
3255 }
3256 break;
3257 }
3258
3259 /* We've found one valid nested-name-specifier. */
3260 success = true;
3261 /* Make sure we look in the right scope the next time through
3262 the loop. */
3263 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3264 ? TREE_TYPE (new_scope)
3265 : new_scope);
3266 /* If it is a class scope, try to complete it; we are about to
3267 be looking up names inside the class. */
3268 if (TYPE_P (parser->scope)
3269 /* Since checking types for dependency can be expensive,
3270 avoid doing it if the type is already complete. */
3271 && !COMPLETE_TYPE_P (parser->scope)
3272 /* Do not try to complete dependent types. */
3273 && !dependent_type_p (parser->scope))
3274 complete_type (parser->scope);
3275 }
3276
3277 /* Retrieve any deferred checks. Do not pop this access checks yet
3278 so the memory will not be reclaimed during token replacing below. */
3279 access_check = get_deferred_access_checks ();
3280
3281 /* If parsing tentatively, replace the sequence of tokens that makes
3282 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3283 token. That way, should we re-parse the token stream, we will
3284 not have to repeat the effort required to do the parse, nor will
3285 we issue duplicate error messages. */
3286 if (success && start >= 0)
3287 {
3288 /* Find the token that corresponds to the start of the
3289 template-id. */
3290 token = cp_lexer_advance_token (parser->lexer,
3291 parser->lexer->first_token,
3292 start);
3293
3294 /* Reset the contents of the START token. */
3295 token->type = CPP_NESTED_NAME_SPECIFIER;
3296 token->value = build_tree_list (access_check, parser->scope);
3297 TREE_TYPE (token->value) = parser->qualifying_scope;
3298 token->keyword = RID_MAX;
3299 /* Purge all subsequent tokens. */
3300 cp_lexer_purge_tokens_after (parser->lexer, token);
3301 }
3302
3303 pop_deferring_access_checks ();
3304 return success ? parser->scope : NULL_TREE;
3305 }
3306
3307 /* Parse a nested-name-specifier. See
3308 cp_parser_nested_name_specifier_opt for details. This function
3309 behaves identically, except that it will an issue an error if no
3310 nested-name-specifier is present, and it will return
3311 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3312 is present. */
3313
3314 static tree
3315 cp_parser_nested_name_specifier (cp_parser *parser,
3316 bool typename_keyword_p,
3317 bool check_dependency_p,
3318 bool type_p,
3319 bool is_declaration)
3320 {
3321 tree scope;
3322
3323 /* Look for the nested-name-specifier. */
3324 scope = cp_parser_nested_name_specifier_opt (parser,
3325 typename_keyword_p,
3326 check_dependency_p,
3327 type_p,
3328 is_declaration);
3329 /* If it was not present, issue an error message. */
3330 if (!scope)
3331 {
3332 cp_parser_error (parser, "expected nested-name-specifier");
3333 parser->scope = NULL_TREE;
3334 return error_mark_node;
3335 }
3336
3337 return scope;
3338 }
3339
3340 /* Parse a class-or-namespace-name.
3341
3342 class-or-namespace-name:
3343 class-name
3344 namespace-name
3345
3346 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3347 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3348 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3349 TYPE_P is TRUE iff the next name should be taken as a class-name,
3350 even the same name is declared to be another entity in the same
3351 scope.
3352
3353 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3354 specified by the class-or-namespace-name. If neither is found the
3355 ERROR_MARK_NODE is returned. */
3356
3357 static tree
3358 cp_parser_class_or_namespace_name (cp_parser *parser,
3359 bool typename_keyword_p,
3360 bool template_keyword_p,
3361 bool check_dependency_p,
3362 bool type_p,
3363 bool is_declaration)
3364 {
3365 tree saved_scope;
3366 tree saved_qualifying_scope;
3367 tree saved_object_scope;
3368 tree scope;
3369 bool only_class_p;
3370
3371 /* Before we try to parse the class-name, we must save away the
3372 current PARSER->SCOPE since cp_parser_class_name will destroy
3373 it. */
3374 saved_scope = parser->scope;
3375 saved_qualifying_scope = parser->qualifying_scope;
3376 saved_object_scope = parser->object_scope;
3377 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3378 there is no need to look for a namespace-name. */
3379 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3380 if (!only_class_p)
3381 cp_parser_parse_tentatively (parser);
3382 scope = cp_parser_class_name (parser,
3383 typename_keyword_p,
3384 template_keyword_p,
3385 type_p,
3386 check_dependency_p,
3387 /*class_head_p=*/false,
3388 is_declaration);
3389 /* If that didn't work, try for a namespace-name. */
3390 if (!only_class_p && !cp_parser_parse_definitely (parser))
3391 {
3392 /* Restore the saved scope. */
3393 parser->scope = saved_scope;
3394 parser->qualifying_scope = saved_qualifying_scope;
3395 parser->object_scope = saved_object_scope;
3396 /* If we are not looking at an identifier followed by the scope
3397 resolution operator, then this is not part of a
3398 nested-name-specifier. (Note that this function is only used
3399 to parse the components of a nested-name-specifier.) */
3400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3401 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3402 return error_mark_node;
3403 scope = cp_parser_namespace_name (parser);
3404 }
3405
3406 return scope;
3407 }
3408
3409 /* Parse a postfix-expression.
3410
3411 postfix-expression:
3412 primary-expression
3413 postfix-expression [ expression ]
3414 postfix-expression ( expression-list [opt] )
3415 simple-type-specifier ( expression-list [opt] )
3416 typename :: [opt] nested-name-specifier identifier
3417 ( expression-list [opt] )
3418 typename :: [opt] nested-name-specifier template [opt] template-id
3419 ( expression-list [opt] )
3420 postfix-expression . template [opt] id-expression
3421 postfix-expression -> template [opt] id-expression
3422 postfix-expression . pseudo-destructor-name
3423 postfix-expression -> pseudo-destructor-name
3424 postfix-expression ++
3425 postfix-expression --
3426 dynamic_cast < type-id > ( expression )
3427 static_cast < type-id > ( expression )
3428 reinterpret_cast < type-id > ( expression )
3429 const_cast < type-id > ( expression )
3430 typeid ( expression )
3431 typeid ( type-id )
3432
3433 GNU Extension:
3434
3435 postfix-expression:
3436 ( type-id ) { initializer-list , [opt] }
3437
3438 This extension is a GNU version of the C99 compound-literal
3439 construct. (The C99 grammar uses `type-name' instead of `type-id',
3440 but they are essentially the same concept.)
3441
3442 If ADDRESS_P is true, the postfix expression is the operand of the
3443 `&' operator.
3444
3445 Returns a representation of the expression. */
3446
3447 static tree
3448 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3449 {
3450 cp_token *token;
3451 enum rid keyword;
3452 cp_id_kind idk = CP_ID_KIND_NONE;
3453 tree postfix_expression = NULL_TREE;
3454 /* Non-NULL only if the current postfix-expression can be used to
3455 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3456 class used to qualify the member. */
3457 tree qualifying_class = NULL_TREE;
3458
3459 /* Peek at the next token. */
3460 token = cp_lexer_peek_token (parser->lexer);
3461 /* Some of the productions are determined by keywords. */
3462 keyword = token->keyword;
3463 switch (keyword)
3464 {
3465 case RID_DYNCAST:
3466 case RID_STATCAST:
3467 case RID_REINTCAST:
3468 case RID_CONSTCAST:
3469 {
3470 tree type;
3471 tree expression;
3472 const char *saved_message;
3473
3474 /* All of these can be handled in the same way from the point
3475 of view of parsing. Begin by consuming the token
3476 identifying the cast. */
3477 cp_lexer_consume_token (parser->lexer);
3478
3479 /* New types cannot be defined in the cast. */
3480 saved_message = parser->type_definition_forbidden_message;
3481 parser->type_definition_forbidden_message
3482 = "types may not be defined in casts";
3483
3484 /* Look for the opening `<'. */
3485 cp_parser_require (parser, CPP_LESS, "`<'");
3486 /* Parse the type to which we are casting. */
3487 type = cp_parser_type_id (parser);
3488 /* Look for the closing `>'. */
3489 cp_parser_require (parser, CPP_GREATER, "`>'");
3490 /* Restore the old message. */
3491 parser->type_definition_forbidden_message = saved_message;
3492
3493 /* And the expression which is being cast. */
3494 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3495 expression = cp_parser_expression (parser);
3496 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3497
3498 /* Only type conversions to integral or enumeration types
3499 can be used in constant-expressions. */
3500 if (parser->integral_constant_expression_p
3501 && !dependent_type_p (type)
3502 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3503 && (cp_parser_non_integral_constant_expression
3504 (parser,
3505 "a cast to a type other than an integral or "
3506 "enumeration type")))
3507 return error_mark_node;
3508
3509 switch (keyword)
3510 {
3511 case RID_DYNCAST:
3512 postfix_expression
3513 = build_dynamic_cast (type, expression);
3514 break;
3515 case RID_STATCAST:
3516 postfix_expression
3517 = build_static_cast (type, expression);
3518 break;
3519 case RID_REINTCAST:
3520 postfix_expression
3521 = build_reinterpret_cast (type, expression);
3522 break;
3523 case RID_CONSTCAST:
3524 postfix_expression
3525 = build_const_cast (type, expression);
3526 break;
3527 default:
3528 abort ();
3529 }
3530 }
3531 break;
3532
3533 case RID_TYPEID:
3534 {
3535 tree type;
3536 const char *saved_message;
3537 bool saved_in_type_id_in_expr_p;
3538
3539 /* Consume the `typeid' token. */
3540 cp_lexer_consume_token (parser->lexer);
3541 /* Look for the `(' token. */
3542 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3543 /* Types cannot be defined in a `typeid' expression. */
3544 saved_message = parser->type_definition_forbidden_message;
3545 parser->type_definition_forbidden_message
3546 = "types may not be defined in a `typeid\' expression";
3547 /* We can't be sure yet whether we're looking at a type-id or an
3548 expression. */
3549 cp_parser_parse_tentatively (parser);
3550 /* Try a type-id first. */
3551 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3552 parser->in_type_id_in_expr_p = true;
3553 type = cp_parser_type_id (parser);
3554 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3555 /* Look for the `)' token. Otherwise, we can't be sure that
3556 we're not looking at an expression: consider `typeid (int
3557 (3))', for example. */
3558 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3559 /* If all went well, simply lookup the type-id. */
3560 if (cp_parser_parse_definitely (parser))
3561 postfix_expression = get_typeid (type);
3562 /* Otherwise, fall back to the expression variant. */
3563 else
3564 {
3565 tree expression;
3566
3567 /* Look for an expression. */
3568 expression = cp_parser_expression (parser);
3569 /* Compute its typeid. */
3570 postfix_expression = build_typeid (expression);
3571 /* Look for the `)' token. */
3572 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3573 }
3574 /* `typeid' may not appear in an integral constant expression. */
3575 if (cp_parser_non_integral_constant_expression(parser,
3576 "`typeid' operator"))
3577 return error_mark_node;
3578 /* Restore the saved message. */
3579 parser->type_definition_forbidden_message = saved_message;
3580 }
3581 break;
3582
3583 case RID_TYPENAME:
3584 {
3585 bool template_p = false;
3586 tree id;
3587 tree type;
3588
3589 /* Consume the `typename' token. */
3590 cp_lexer_consume_token (parser->lexer);
3591 /* Look for the optional `::' operator. */
3592 cp_parser_global_scope_opt (parser,
3593 /*current_scope_valid_p=*/false);
3594 /* Look for the nested-name-specifier. */
3595 cp_parser_nested_name_specifier (parser,
3596 /*typename_keyword_p=*/true,
3597 /*check_dependency_p=*/true,
3598 /*type_p=*/true,
3599 /*is_declaration=*/true);
3600 /* Look for the optional `template' keyword. */
3601 template_p = cp_parser_optional_template_keyword (parser);
3602 /* We don't know whether we're looking at a template-id or an
3603 identifier. */
3604 cp_parser_parse_tentatively (parser);
3605 /* Try a template-id. */
3606 id = cp_parser_template_id (parser, template_p,
3607 /*check_dependency_p=*/true,
3608 /*is_declaration=*/true);
3609 /* If that didn't work, try an identifier. */
3610 if (!cp_parser_parse_definitely (parser))
3611 id = cp_parser_identifier (parser);
3612 /* If we look up a template-id in a non-dependent qualifying
3613 scope, there's no need to create a dependent type. */
3614 if (TREE_CODE (id) == TYPE_DECL
3615 && !dependent_type_p (parser->scope))
3616 type = TREE_TYPE (id);
3617 /* Create a TYPENAME_TYPE to represent the type to which the
3618 functional cast is being performed. */
3619 else
3620 type = make_typename_type (parser->scope, id,
3621 /*complain=*/1);
3622
3623 postfix_expression = cp_parser_functional_cast (parser, type);
3624 }
3625 break;
3626
3627 default:
3628 {
3629 tree type;
3630
3631 /* If the next thing is a simple-type-specifier, we may be
3632 looking at a functional cast. We could also be looking at
3633 an id-expression. So, we try the functional cast, and if
3634 that doesn't work we fall back to the primary-expression. */
3635 cp_parser_parse_tentatively (parser);
3636 /* Look for the simple-type-specifier. */
3637 type = cp_parser_simple_type_specifier (parser,
3638 CP_PARSER_FLAGS_NONE,
3639 /*identifier_p=*/false);
3640 /* Parse the cast itself. */
3641 if (!cp_parser_error_occurred (parser))
3642 postfix_expression
3643 = cp_parser_functional_cast (parser, type);
3644 /* If that worked, we're done. */
3645 if (cp_parser_parse_definitely (parser))
3646 break;
3647
3648 /* If the functional-cast didn't work out, try a
3649 compound-literal. */
3650 if (cp_parser_allow_gnu_extensions_p (parser)
3651 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3652 {
3653 tree initializer_list = NULL_TREE;
3654 bool saved_in_type_id_in_expr_p;
3655
3656 cp_parser_parse_tentatively (parser);
3657 /* Consume the `('. */
3658 cp_lexer_consume_token (parser->lexer);
3659 /* Parse the type. */
3660 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3661 parser->in_type_id_in_expr_p = true;
3662 type = cp_parser_type_id (parser);
3663 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3664 /* Look for the `)'. */
3665 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3666 /* Look for the `{'. */
3667 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3668 /* If things aren't going well, there's no need to
3669 keep going. */
3670 if (!cp_parser_error_occurred (parser))
3671 {
3672 bool non_constant_p;
3673 /* Parse the initializer-list. */
3674 initializer_list
3675 = cp_parser_initializer_list (parser, &non_constant_p);
3676 /* Allow a trailing `,'. */
3677 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3678 cp_lexer_consume_token (parser->lexer);
3679 /* Look for the final `}'. */
3680 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3681 }
3682 /* If that worked, we're definitely looking at a
3683 compound-literal expression. */
3684 if (cp_parser_parse_definitely (parser))
3685 {
3686 /* Warn the user that a compound literal is not
3687 allowed in standard C++. */
3688 if (pedantic)
3689 pedwarn ("ISO C++ forbids compound-literals");
3690 /* Form the representation of the compound-literal. */
3691 postfix_expression
3692 = finish_compound_literal (type, initializer_list);
3693 break;
3694 }
3695 }
3696
3697 /* It must be a primary-expression. */
3698 postfix_expression = cp_parser_primary_expression (parser,
3699 &idk,
3700 &qualifying_class);
3701 }
3702 break;
3703 }
3704
3705 /* If we were avoiding committing to the processing of a
3706 qualified-id until we knew whether or not we had a
3707 pointer-to-member, we now know. */
3708 if (qualifying_class)
3709 {
3710 bool done;
3711
3712 /* Peek at the next token. */
3713 token = cp_lexer_peek_token (parser->lexer);
3714 done = (token->type != CPP_OPEN_SQUARE
3715 && token->type != CPP_OPEN_PAREN
3716 && token->type != CPP_DOT
3717 && token->type != CPP_DEREF
3718 && token->type != CPP_PLUS_PLUS
3719 && token->type != CPP_MINUS_MINUS);
3720
3721 postfix_expression = finish_qualified_id_expr (qualifying_class,
3722 postfix_expression,
3723 done,
3724 address_p);
3725 if (done)
3726 return postfix_expression;
3727 }
3728
3729 /* Keep looping until the postfix-expression is complete. */
3730 while (true)
3731 {
3732 if (idk == CP_ID_KIND_UNQUALIFIED
3733 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3734 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3735 /* It is not a Koenig lookup function call. */
3736 postfix_expression
3737 = unqualified_name_lookup_error (postfix_expression);
3738
3739 /* Peek at the next token. */
3740 token = cp_lexer_peek_token (parser->lexer);
3741
3742 switch (token->type)
3743 {
3744 case CPP_OPEN_SQUARE:
3745 postfix_expression
3746 = cp_parser_postfix_open_square_expression (parser,
3747 postfix_expression,
3748 false);
3749 idk = CP_ID_KIND_NONE;
3750 break;
3751
3752 case CPP_OPEN_PAREN:
3753 /* postfix-expression ( expression-list [opt] ) */
3754 {
3755 bool koenig_p;
3756 tree args = (cp_parser_parenthesized_expression_list
3757 (parser, false, /*non_constant_p=*/NULL));
3758
3759 if (args == error_mark_node)
3760 {
3761 postfix_expression = error_mark_node;
3762 break;
3763 }
3764
3765 /* Function calls are not permitted in
3766 constant-expressions. */
3767 if (cp_parser_non_integral_constant_expression (parser,
3768 "a function call"))
3769 {
3770 postfix_expression = error_mark_node;
3771 break;
3772 }
3773
3774 koenig_p = false;
3775 if (idk == CP_ID_KIND_UNQUALIFIED)
3776 {
3777 /* We do not perform argument-dependent lookup if
3778 normal lookup finds a non-function, in accordance
3779 with the expected resolution of DR 218. */
3780 if (args
3781 && (is_overloaded_fn (postfix_expression)
3782 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3783 {
3784 koenig_p = true;
3785 postfix_expression
3786 = perform_koenig_lookup (postfix_expression, args);
3787 }
3788 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3789 postfix_expression
3790 = unqualified_fn_lookup_error (postfix_expression);
3791 }
3792
3793 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3794 {
3795 tree instance = TREE_OPERAND (postfix_expression, 0);
3796 tree fn = TREE_OPERAND (postfix_expression, 1);
3797
3798 if (processing_template_decl
3799 && (type_dependent_expression_p (instance)
3800 || (!BASELINK_P (fn)
3801 && TREE_CODE (fn) != FIELD_DECL)
3802 || type_dependent_expression_p (fn)
3803 || any_type_dependent_arguments_p (args)))
3804 {
3805 postfix_expression
3806 = build_min_nt (CALL_EXPR, postfix_expression,
3807 args, NULL_TREE);
3808 break;
3809 }
3810
3811 if (BASELINK_P (fn))
3812 postfix_expression
3813 = (build_new_method_call
3814 (instance, fn, args, NULL_TREE,
3815 (idk == CP_ID_KIND_QUALIFIED
3816 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3817 else
3818 postfix_expression
3819 = finish_call_expr (postfix_expression, args,
3820 /*disallow_virtual=*/false,
3821 /*koenig_p=*/false);
3822 }
3823 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3824 || TREE_CODE (postfix_expression) == MEMBER_REF
3825 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3826 postfix_expression = (build_offset_ref_call_from_tree
3827 (postfix_expression, args));
3828 else if (idk == CP_ID_KIND_QUALIFIED)
3829 /* A call to a static class member, or a namespace-scope
3830 function. */
3831 postfix_expression
3832 = finish_call_expr (postfix_expression, args,
3833 /*disallow_virtual=*/true,
3834 koenig_p);
3835 else
3836 /* All other function calls. */
3837 postfix_expression
3838 = finish_call_expr (postfix_expression, args,
3839 /*disallow_virtual=*/false,
3840 koenig_p);
3841
3842 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3843 idk = CP_ID_KIND_NONE;
3844 }
3845 break;
3846
3847 case CPP_DOT:
3848 case CPP_DEREF:
3849 /* postfix-expression . template [opt] id-expression
3850 postfix-expression . pseudo-destructor-name
3851 postfix-expression -> template [opt] id-expression
3852 postfix-expression -> pseudo-destructor-name */
3853
3854 /* Consume the `.' or `->' operator. */
3855 cp_lexer_consume_token (parser->lexer);
3856
3857 postfix_expression
3858 = cp_parser_postfix_dot_deref_expression (parser, token->type,
3859 postfix_expression,
3860 false, &idk);
3861 break;
3862
3863 case CPP_PLUS_PLUS:
3864 /* postfix-expression ++ */
3865 /* Consume the `++' token. */
3866 cp_lexer_consume_token (parser->lexer);
3867 /* Generate a representation for the complete expression. */
3868 postfix_expression
3869 = finish_increment_expr (postfix_expression,
3870 POSTINCREMENT_EXPR);
3871 /* Increments may not appear in constant-expressions. */
3872 if (cp_parser_non_integral_constant_expression (parser,
3873 "an increment"))
3874 postfix_expression = error_mark_node;
3875 idk = CP_ID_KIND_NONE;
3876 break;
3877
3878 case CPP_MINUS_MINUS:
3879 /* postfix-expression -- */
3880 /* Consume the `--' token. */
3881 cp_lexer_consume_token (parser->lexer);
3882 /* Generate a representation for the complete expression. */
3883 postfix_expression
3884 = finish_increment_expr (postfix_expression,
3885 POSTDECREMENT_EXPR);
3886 /* Decrements may not appear in constant-expressions. */
3887 if (cp_parser_non_integral_constant_expression (parser,
3888 "a decrement"))
3889 postfix_expression = error_mark_node;
3890 idk = CP_ID_KIND_NONE;
3891 break;
3892
3893 default:
3894 return postfix_expression;
3895 }
3896 }
3897
3898 /* We should never get here. */
3899 abort ();
3900 return error_mark_node;
3901 }
3902
3903 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
3904 by cp_parser_builtin_offsetof. We're looking for
3905
3906 postfix-expression [ expression ]
3907
3908 FOR_OFFSETOF is set if we're being called in that context, which
3909 changes how we deal with integer constant expressions. */
3910
3911 static tree
3912 cp_parser_postfix_open_square_expression (cp_parser *parser,
3913 tree postfix_expression,
3914 bool for_offsetof)
3915 {
3916 tree index;
3917
3918 /* Consume the `[' token. */
3919 cp_lexer_consume_token (parser->lexer);
3920
3921 /* Parse the index expression. */
3922 /* ??? For offsetof, there is a question of what to allow here. If
3923 offsetof is not being used in an integral constant expression context,
3924 then we *could* get the right answer by computing the value at runtime.
3925 If we are in an integral constant expression context, then we might
3926 could accept any constant expression; hard to say without analysis.
3927 Rather than open the barn door too wide right away, allow only integer
3928 constant expresions here. */
3929 if (for_offsetof)
3930 index = cp_parser_constant_expression (parser, false, NULL);
3931 else
3932 index = cp_parser_expression (parser);
3933
3934 /* Look for the closing `]'. */
3935 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3936
3937 /* Build the ARRAY_REF. */
3938 postfix_expression = grok_array_decl (postfix_expression, index);
3939
3940 /* When not doing offsetof, array references are not permitted in
3941 constant-expressions. */
3942 if (!for_offsetof
3943 && (cp_parser_non_integral_constant_expression
3944 (parser, "an array reference")))
3945 postfix_expression = error_mark_node;
3946
3947 return postfix_expression;
3948 }
3949
3950 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
3951 by cp_parser_builtin_offsetof. We're looking for
3952
3953 postfix-expression . template [opt] id-expression
3954 postfix-expression . pseudo-destructor-name
3955 postfix-expression -> template [opt] id-expression
3956 postfix-expression -> pseudo-destructor-name
3957
3958 FOR_OFFSETOF is set if we're being called in that context. That sorta
3959 limits what of the above we'll actually accept, but nevermind.
3960 TOKEN_TYPE is the "." or "->" token, which will already have been
3961 removed from the stream. */
3962
3963 static tree
3964 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
3965 enum cpp_ttype token_type,
3966 tree postfix_expression,
3967 bool for_offsetof, cp_id_kind *idk)
3968 {
3969 tree name;
3970 bool dependent_p;
3971 bool template_p;
3972 tree scope = NULL_TREE;
3973
3974 /* If this is a `->' operator, dereference the pointer. */
3975 if (token_type == CPP_DEREF)
3976 postfix_expression = build_x_arrow (postfix_expression);
3977 /* Check to see whether or not the expression is type-dependent. */
3978 dependent_p = type_dependent_expression_p (postfix_expression);
3979 /* The identifier following the `->' or `.' is not qualified. */
3980 parser->scope = NULL_TREE;
3981 parser->qualifying_scope = NULL_TREE;
3982 parser->object_scope = NULL_TREE;
3983 *idk = CP_ID_KIND_NONE;
3984 /* Enter the scope corresponding to the type of the object
3985 given by the POSTFIX_EXPRESSION. */
3986 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
3987 {
3988 scope = TREE_TYPE (postfix_expression);
3989 /* According to the standard, no expression should ever have
3990 reference type. Unfortunately, we do not currently match
3991 the standard in this respect in that our internal representation
3992 of an expression may have reference type even when the standard
3993 says it does not. Therefore, we have to manually obtain the
3994 underlying type here. */
3995 scope = non_reference (scope);
3996 /* The type of the POSTFIX_EXPRESSION must be complete. */
3997 scope = complete_type_or_else (scope, NULL_TREE);
3998 /* Let the name lookup machinery know that we are processing a
3999 class member access expression. */
4000 parser->context->object_type = scope;
4001 /* If something went wrong, we want to be able to discern that case,
4002 as opposed to the case where there was no SCOPE due to the type
4003 of expression being dependent. */
4004 if (!scope)
4005 scope = error_mark_node;
4006 /* If the SCOPE was erroneous, make the various semantic analysis
4007 functions exit quickly -- and without issuing additional error
4008 messages. */
4009 if (scope == error_mark_node)
4010 postfix_expression = error_mark_node;
4011 }
4012
4013 /* If the SCOPE is not a scalar type, we are looking at an
4014 ordinary class member access expression, rather than a
4015 pseudo-destructor-name. */
4016 if (!scope || !SCALAR_TYPE_P (scope))
4017 {
4018 template_p = cp_parser_optional_template_keyword (parser);
4019 /* Parse the id-expression. */
4020 name = cp_parser_id_expression (parser, template_p,
4021 /*check_dependency_p=*/true,
4022 /*template_p=*/NULL,
4023 /*declarator_p=*/false);
4024 /* In general, build a SCOPE_REF if the member name is qualified.
4025 However, if the name was not dependent and has already been
4026 resolved; there is no need to build the SCOPE_REF. For example;
4027
4028 struct X { void f(); };
4029 template <typename T> void f(T* t) { t->X::f(); }
4030
4031 Even though "t" is dependent, "X::f" is not and has been resolved
4032 to a BASELINK; there is no need to include scope information. */
4033
4034 /* But we do need to remember that there was an explicit scope for
4035 virtual function calls. */
4036 if (parser->scope)
4037 *idk = CP_ID_KIND_QUALIFIED;
4038
4039 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4040 {
4041 name = build_nt (SCOPE_REF, parser->scope, name);
4042 parser->scope = NULL_TREE;
4043 parser->qualifying_scope = NULL_TREE;
4044 parser->object_scope = NULL_TREE;
4045 }
4046 if (scope && name && BASELINK_P (name))
4047 adjust_result_of_qualified_name_lookup
4048 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4049 postfix_expression
4050 = finish_class_member_access_expr (postfix_expression, name);
4051 }
4052 /* Otherwise, try the pseudo-destructor-name production. */
4053 else
4054 {
4055 tree s = NULL_TREE;
4056 tree type;
4057
4058 /* Parse the pseudo-destructor-name. */
4059 cp_parser_pseudo_destructor_name (parser, &s, &type);
4060 /* Form the call. */
4061 postfix_expression
4062 = finish_pseudo_destructor_expr (postfix_expression,
4063 s, TREE_TYPE (type));
4064 }
4065
4066 /* We no longer need to look up names in the scope of the object on
4067 the left-hand side of the `.' or `->' operator. */
4068 parser->context->object_type = NULL_TREE;
4069
4070 /* Outside of offsetof, these operators may not appear in
4071 constant-expressions. */
4072 if (!for_offsetof
4073 && (cp_parser_non_integral_constant_expression
4074 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4075 postfix_expression = error_mark_node;
4076
4077 return postfix_expression;
4078 }
4079
4080 /* Parse a parenthesized expression-list.
4081
4082 expression-list:
4083 assignment-expression
4084 expression-list, assignment-expression
4085
4086 attribute-list:
4087 expression-list
4088 identifier
4089 identifier, expression-list
4090
4091 Returns a TREE_LIST. The TREE_VALUE of each node is a
4092 representation of an assignment-expression. Note that a TREE_LIST
4093 is returned even if there is only a single expression in the list.
4094 error_mark_node is returned if the ( and or ) are
4095 missing. NULL_TREE is returned on no expressions. The parentheses
4096 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4097 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4098 indicates whether or not all of the expressions in the list were
4099 constant. */
4100
4101 static tree
4102 cp_parser_parenthesized_expression_list (cp_parser* parser,
4103 bool is_attribute_list,
4104 bool *non_constant_p)
4105 {
4106 tree expression_list = NULL_TREE;
4107 tree identifier = NULL_TREE;
4108
4109 /* Assume all the expressions will be constant. */
4110 if (non_constant_p)
4111 *non_constant_p = false;
4112
4113 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4114 return error_mark_node;
4115
4116 /* Consume expressions until there are no more. */
4117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4118 while (true)
4119 {
4120 tree expr;
4121
4122 /* At the beginning of attribute lists, check to see if the
4123 next token is an identifier. */
4124 if (is_attribute_list
4125 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4126 {
4127 cp_token *token;
4128
4129 /* Consume the identifier. */
4130 token = cp_lexer_consume_token (parser->lexer);
4131 /* Save the identifier. */
4132 identifier = token->value;
4133 }
4134 else
4135 {
4136 /* Parse the next assignment-expression. */
4137 if (non_constant_p)
4138 {
4139 bool expr_non_constant_p;
4140 expr = (cp_parser_constant_expression
4141 (parser, /*allow_non_constant_p=*/true,
4142 &expr_non_constant_p));
4143 if (expr_non_constant_p)
4144 *non_constant_p = true;
4145 }
4146 else
4147 expr = cp_parser_assignment_expression (parser);
4148
4149 /* Add it to the list. We add error_mark_node
4150 expressions to the list, so that we can still tell if
4151 the correct form for a parenthesized expression-list
4152 is found. That gives better errors. */
4153 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4154
4155 if (expr == error_mark_node)
4156 goto skip_comma;
4157 }
4158
4159 /* After the first item, attribute lists look the same as
4160 expression lists. */
4161 is_attribute_list = false;
4162
4163 get_comma:;
4164 /* If the next token isn't a `,', then we are done. */
4165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4166 break;
4167
4168 /* Otherwise, consume the `,' and keep going. */
4169 cp_lexer_consume_token (parser->lexer);
4170 }
4171
4172 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4173 {
4174 int ending;
4175
4176 skip_comma:;
4177 /* We try and resync to an unnested comma, as that will give the
4178 user better diagnostics. */
4179 ending = cp_parser_skip_to_closing_parenthesis (parser,
4180 /*recovering=*/true,
4181 /*or_comma=*/true,
4182 /*consume_paren=*/true);
4183 if (ending < 0)
4184 goto get_comma;
4185 if (!ending)
4186 return error_mark_node;
4187 }
4188
4189 /* We built up the list in reverse order so we must reverse it now. */
4190 expression_list = nreverse (expression_list);
4191 if (identifier)
4192 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4193
4194 return expression_list;
4195 }
4196
4197 /* Parse a pseudo-destructor-name.
4198
4199 pseudo-destructor-name:
4200 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4201 :: [opt] nested-name-specifier template template-id :: ~ type-name
4202 :: [opt] nested-name-specifier [opt] ~ type-name
4203
4204 If either of the first two productions is used, sets *SCOPE to the
4205 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4206 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4207 or ERROR_MARK_NODE if the parse fails. */
4208
4209 static void
4210 cp_parser_pseudo_destructor_name (cp_parser* parser,
4211 tree* scope,
4212 tree* type)
4213 {
4214 bool nested_name_specifier_p;
4215
4216 /* Look for the optional `::' operator. */
4217 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4218 /* Look for the optional nested-name-specifier. */
4219 nested_name_specifier_p
4220 = (cp_parser_nested_name_specifier_opt (parser,
4221 /*typename_keyword_p=*/false,
4222 /*check_dependency_p=*/true,
4223 /*type_p=*/false,
4224 /*is_declaration=*/true)
4225 != NULL_TREE);
4226 /* Now, if we saw a nested-name-specifier, we might be doing the
4227 second production. */
4228 if (nested_name_specifier_p
4229 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4230 {
4231 /* Consume the `template' keyword. */
4232 cp_lexer_consume_token (parser->lexer);
4233 /* Parse the template-id. */
4234 cp_parser_template_id (parser,
4235 /*template_keyword_p=*/true,
4236 /*check_dependency_p=*/false,
4237 /*is_declaration=*/true);
4238 /* Look for the `::' token. */
4239 cp_parser_require (parser, CPP_SCOPE, "`::'");
4240 }
4241 /* If the next token is not a `~', then there might be some
4242 additional qualification. */
4243 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4244 {
4245 /* Look for the type-name. */
4246 *scope = TREE_TYPE (cp_parser_type_name (parser));
4247
4248 /* If we didn't get an aggregate type, or we don't have ::~,
4249 then something has gone wrong. Since the only caller of this
4250 function is looking for something after `.' or `->' after a
4251 scalar type, most likely the program is trying to get a
4252 member of a non-aggregate type. */
4253 if (*scope == error_mark_node
4254 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4255 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4256 {
4257 cp_parser_error (parser, "request for member of non-aggregate type");
4258 *type = error_mark_node;
4259 return;
4260 }
4261
4262 /* Look for the `::' token. */
4263 cp_parser_require (parser, CPP_SCOPE, "`::'");
4264 }
4265 else
4266 *scope = NULL_TREE;
4267
4268 /* Look for the `~'. */
4269 cp_parser_require (parser, CPP_COMPL, "`~'");
4270 /* Look for the type-name again. We are not responsible for
4271 checking that it matches the first type-name. */
4272 *type = cp_parser_type_name (parser);
4273 }
4274
4275 /* Parse a unary-expression.
4276
4277 unary-expression:
4278 postfix-expression
4279 ++ cast-expression
4280 -- cast-expression
4281 unary-operator cast-expression
4282 sizeof unary-expression
4283 sizeof ( type-id )
4284 new-expression
4285 delete-expression
4286
4287 GNU Extensions:
4288
4289 unary-expression:
4290 __extension__ cast-expression
4291 __alignof__ unary-expression
4292 __alignof__ ( type-id )
4293 __real__ cast-expression
4294 __imag__ cast-expression
4295 && identifier
4296
4297 ADDRESS_P is true iff the unary-expression is appearing as the
4298 operand of the `&' operator.
4299
4300 Returns a representation of the expression. */
4301
4302 static tree
4303 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4304 {
4305 cp_token *token;
4306 enum tree_code unary_operator;
4307
4308 /* Peek at the next token. */
4309 token = cp_lexer_peek_token (parser->lexer);
4310 /* Some keywords give away the kind of expression. */
4311 if (token->type == CPP_KEYWORD)
4312 {
4313 enum rid keyword = token->keyword;
4314
4315 switch (keyword)
4316 {
4317 case RID_ALIGNOF:
4318 case RID_SIZEOF:
4319 {
4320 tree operand;
4321 enum tree_code op;
4322
4323 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4324 /* Consume the token. */
4325 cp_lexer_consume_token (parser->lexer);
4326 /* Parse the operand. */
4327 operand = cp_parser_sizeof_operand (parser, keyword);
4328
4329 if (TYPE_P (operand))
4330 return cxx_sizeof_or_alignof_type (operand, op, true);
4331 else
4332 return cxx_sizeof_or_alignof_expr (operand, op);
4333 }
4334
4335 case RID_NEW:
4336 return cp_parser_new_expression (parser);
4337
4338 case RID_DELETE:
4339 return cp_parser_delete_expression (parser);
4340
4341 case RID_EXTENSION:
4342 {
4343 /* The saved value of the PEDANTIC flag. */
4344 int saved_pedantic;
4345 tree expr;
4346
4347 /* Save away the PEDANTIC flag. */
4348 cp_parser_extension_opt (parser, &saved_pedantic);
4349 /* Parse the cast-expression. */
4350 expr = cp_parser_simple_cast_expression (parser);
4351 /* Restore the PEDANTIC flag. */
4352 pedantic = saved_pedantic;
4353
4354 return expr;
4355 }
4356
4357 case RID_REALPART:
4358 case RID_IMAGPART:
4359 {
4360 tree expression;
4361
4362 /* Consume the `__real__' or `__imag__' token. */
4363 cp_lexer_consume_token (parser->lexer);
4364 /* Parse the cast-expression. */
4365 expression = cp_parser_simple_cast_expression (parser);
4366 /* Create the complete representation. */
4367 return build_x_unary_op ((keyword == RID_REALPART
4368 ? REALPART_EXPR : IMAGPART_EXPR),
4369 expression);
4370 }
4371 break;
4372
4373 default:
4374 break;
4375 }
4376 }
4377
4378 /* Look for the `:: new' and `:: delete', which also signal the
4379 beginning of a new-expression, or delete-expression,
4380 respectively. If the next token is `::', then it might be one of
4381 these. */
4382 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4383 {
4384 enum rid keyword;
4385
4386 /* See if the token after the `::' is one of the keywords in
4387 which we're interested. */
4388 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4389 /* If it's `new', we have a new-expression. */
4390 if (keyword == RID_NEW)
4391 return cp_parser_new_expression (parser);
4392 /* Similarly, for `delete'. */
4393 else if (keyword == RID_DELETE)
4394 return cp_parser_delete_expression (parser);
4395 }
4396
4397 /* Look for a unary operator. */
4398 unary_operator = cp_parser_unary_operator (token);
4399 /* The `++' and `--' operators can be handled similarly, even though
4400 they are not technically unary-operators in the grammar. */
4401 if (unary_operator == ERROR_MARK)
4402 {
4403 if (token->type == CPP_PLUS_PLUS)
4404 unary_operator = PREINCREMENT_EXPR;
4405 else if (token->type == CPP_MINUS_MINUS)
4406 unary_operator = PREDECREMENT_EXPR;
4407 /* Handle the GNU address-of-label extension. */
4408 else if (cp_parser_allow_gnu_extensions_p (parser)
4409 && token->type == CPP_AND_AND)
4410 {
4411 tree identifier;
4412
4413 /* Consume the '&&' token. */
4414 cp_lexer_consume_token (parser->lexer);
4415 /* Look for the identifier. */
4416 identifier = cp_parser_identifier (parser);
4417 /* Create an expression representing the address. */
4418 return finish_label_address_expr (identifier);
4419 }
4420 }
4421 if (unary_operator != ERROR_MARK)
4422 {
4423 tree cast_expression;
4424 tree expression = error_mark_node;
4425 const char *non_constant_p = NULL;
4426
4427 /* Consume the operator token. */
4428 token = cp_lexer_consume_token (parser->lexer);
4429 /* Parse the cast-expression. */
4430 cast_expression
4431 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4432 /* Now, build an appropriate representation. */
4433 switch (unary_operator)
4434 {
4435 case INDIRECT_REF:
4436 non_constant_p = "`*'";
4437 expression = build_x_indirect_ref (cast_expression, "unary *");
4438 break;
4439
4440 case ADDR_EXPR:
4441 non_constant_p = "`&'";
4442 /* Fall through. */
4443 case BIT_NOT_EXPR:
4444 expression = build_x_unary_op (unary_operator, cast_expression);
4445 break;
4446
4447 case PREINCREMENT_EXPR:
4448 case PREDECREMENT_EXPR:
4449 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4450 ? "`++'" : "`--'");
4451 /* Fall through. */
4452 case CONVERT_EXPR:
4453 case NEGATE_EXPR:
4454 case TRUTH_NOT_EXPR:
4455 expression = finish_unary_op_expr (unary_operator, cast_expression);
4456 break;
4457
4458 default:
4459 abort ();
4460 }
4461
4462 if (non_constant_p
4463 && cp_parser_non_integral_constant_expression (parser,
4464 non_constant_p))
4465 expression = error_mark_node;
4466
4467 return expression;
4468 }
4469
4470 return cp_parser_postfix_expression (parser, address_p);
4471 }
4472
4473 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4474 unary-operator, the corresponding tree code is returned. */
4475
4476 static enum tree_code
4477 cp_parser_unary_operator (cp_token* token)
4478 {
4479 switch (token->type)
4480 {
4481 case CPP_MULT:
4482 return INDIRECT_REF;
4483
4484 case CPP_AND:
4485 return ADDR_EXPR;
4486
4487 case CPP_PLUS:
4488 return CONVERT_EXPR;
4489
4490 case CPP_MINUS:
4491 return NEGATE_EXPR;
4492
4493 case CPP_NOT:
4494 return TRUTH_NOT_EXPR;
4495
4496 case CPP_COMPL:
4497 return BIT_NOT_EXPR;
4498
4499 default:
4500 return ERROR_MARK;
4501 }
4502 }
4503
4504 /* Parse a new-expression.
4505
4506 new-expression:
4507 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4508 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4509
4510 Returns a representation of the expression. */
4511
4512 static tree
4513 cp_parser_new_expression (cp_parser* parser)
4514 {
4515 bool global_scope_p;
4516 tree placement;
4517 tree type;
4518 tree initializer;
4519
4520 /* Look for the optional `::' operator. */
4521 global_scope_p
4522 = (cp_parser_global_scope_opt (parser,
4523 /*current_scope_valid_p=*/false)
4524 != NULL_TREE);
4525 /* Look for the `new' operator. */
4526 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4527 /* There's no easy way to tell a new-placement from the
4528 `( type-id )' construct. */
4529 cp_parser_parse_tentatively (parser);
4530 /* Look for a new-placement. */
4531 placement = cp_parser_new_placement (parser);
4532 /* If that didn't work out, there's no new-placement. */
4533 if (!cp_parser_parse_definitely (parser))
4534 placement = NULL_TREE;
4535
4536 /* If the next token is a `(', then we have a parenthesized
4537 type-id. */
4538 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4539 {
4540 /* Consume the `('. */
4541 cp_lexer_consume_token (parser->lexer);
4542 /* Parse the type-id. */
4543 type = cp_parser_type_id (parser);
4544 /* Look for the closing `)'. */
4545 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4546 /* There should not be a direct-new-declarator in this production,
4547 but GCC used to allowed this, so we check and emit a sensible error
4548 message for this case. */
4549 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4550 {
4551 error ("array bound forbidden after parenthesized type-id");
4552 inform ("try removing the parentheses around the type-id");
4553 cp_parser_direct_new_declarator (parser);
4554 }
4555 }
4556 /* Otherwise, there must be a new-type-id. */
4557 else
4558 type = cp_parser_new_type_id (parser);
4559
4560 /* If the next token is a `(', then we have a new-initializer. */
4561 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4562 initializer = cp_parser_new_initializer (parser);
4563 else
4564 initializer = NULL_TREE;
4565
4566 /* A new-expression may not appear in an integral constant
4567 expression. */
4568 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4569 return error_mark_node;
4570
4571 /* Create a representation of the new-expression. */
4572 return build_new (placement, type, initializer, global_scope_p);
4573 }
4574
4575 /* Parse a new-placement.
4576
4577 new-placement:
4578 ( expression-list )
4579
4580 Returns the same representation as for an expression-list. */
4581
4582 static tree
4583 cp_parser_new_placement (cp_parser* parser)
4584 {
4585 tree expression_list;
4586
4587 /* Parse the expression-list. */
4588 expression_list = (cp_parser_parenthesized_expression_list
4589 (parser, false, /*non_constant_p=*/NULL));
4590
4591 return expression_list;
4592 }
4593
4594 /* Parse a new-type-id.
4595
4596 new-type-id:
4597 type-specifier-seq new-declarator [opt]
4598
4599 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4600 and whose TREE_VALUE is the new-declarator. */
4601
4602 static tree
4603 cp_parser_new_type_id (cp_parser* parser)
4604 {
4605 tree type_specifier_seq;
4606 tree declarator;
4607 const char *saved_message;
4608
4609 /* The type-specifier sequence must not contain type definitions.
4610 (It cannot contain declarations of new types either, but if they
4611 are not definitions we will catch that because they are not
4612 complete.) */
4613 saved_message = parser->type_definition_forbidden_message;
4614 parser->type_definition_forbidden_message
4615 = "types may not be defined in a new-type-id";
4616 /* Parse the type-specifier-seq. */
4617 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4618 /* Restore the old message. */
4619 parser->type_definition_forbidden_message = saved_message;
4620 /* Parse the new-declarator. */
4621 declarator = cp_parser_new_declarator_opt (parser);
4622
4623 return build_tree_list (type_specifier_seq, declarator);
4624 }
4625
4626 /* Parse an (optional) new-declarator.
4627
4628 new-declarator:
4629 ptr-operator new-declarator [opt]
4630 direct-new-declarator
4631
4632 Returns a representation of the declarator. See
4633 cp_parser_declarator for the representations used. */
4634
4635 static tree
4636 cp_parser_new_declarator_opt (cp_parser* parser)
4637 {
4638 enum tree_code code;
4639 tree type;
4640 tree cv_qualifier_seq;
4641
4642 /* We don't know if there's a ptr-operator next, or not. */
4643 cp_parser_parse_tentatively (parser);
4644 /* Look for a ptr-operator. */
4645 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4646 /* If that worked, look for more new-declarators. */
4647 if (cp_parser_parse_definitely (parser))
4648 {
4649 tree declarator;
4650
4651 /* Parse another optional declarator. */
4652 declarator = cp_parser_new_declarator_opt (parser);
4653
4654 /* Create the representation of the declarator. */
4655 if (code == INDIRECT_REF)
4656 declarator = make_pointer_declarator (cv_qualifier_seq,
4657 declarator);
4658 else
4659 declarator = make_reference_declarator (cv_qualifier_seq,
4660 declarator);
4661
4662 /* Handle the pointer-to-member case. */
4663 if (type)
4664 declarator = build_nt (SCOPE_REF, type, declarator);
4665
4666 return declarator;
4667 }
4668
4669 /* If the next token is a `[', there is a direct-new-declarator. */
4670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4671 return cp_parser_direct_new_declarator (parser);
4672
4673 return NULL_TREE;
4674 }
4675
4676 /* Parse a direct-new-declarator.
4677
4678 direct-new-declarator:
4679 [ expression ]
4680 direct-new-declarator [constant-expression]
4681
4682 Returns an ARRAY_REF, following the same conventions as are
4683 documented for cp_parser_direct_declarator. */
4684
4685 static tree
4686 cp_parser_direct_new_declarator (cp_parser* parser)
4687 {
4688 tree declarator = NULL_TREE;
4689
4690 while (true)
4691 {
4692 tree expression;
4693
4694 /* Look for the opening `['. */
4695 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4696 /* The first expression is not required to be constant. */
4697 if (!declarator)
4698 {
4699 expression = cp_parser_expression (parser);
4700 /* The standard requires that the expression have integral
4701 type. DR 74 adds enumeration types. We believe that the
4702 real intent is that these expressions be handled like the
4703 expression in a `switch' condition, which also allows
4704 classes with a single conversion to integral or
4705 enumeration type. */
4706 if (!processing_template_decl)
4707 {
4708 expression
4709 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4710 expression,
4711 /*complain=*/true);
4712 if (!expression)
4713 {
4714 error ("expression in new-declarator must have integral or enumeration type");
4715 expression = error_mark_node;
4716 }
4717 }
4718 }
4719 /* But all the other expressions must be. */
4720 else
4721 expression
4722 = cp_parser_constant_expression (parser,
4723 /*allow_non_constant=*/false,
4724 NULL);
4725 /* Look for the closing `]'. */
4726 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4727
4728 /* Add this bound to the declarator. */
4729 declarator = build_nt (ARRAY_REF, declarator, expression);
4730
4731 /* If the next token is not a `[', then there are no more
4732 bounds. */
4733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4734 break;
4735 }
4736
4737 return declarator;
4738 }
4739
4740 /* Parse a new-initializer.
4741
4742 new-initializer:
4743 ( expression-list [opt] )
4744
4745 Returns a representation of the expression-list. If there is no
4746 expression-list, VOID_ZERO_NODE is returned. */
4747
4748 static tree
4749 cp_parser_new_initializer (cp_parser* parser)
4750 {
4751 tree expression_list;
4752
4753 expression_list = (cp_parser_parenthesized_expression_list
4754 (parser, false, /*non_constant_p=*/NULL));
4755 if (!expression_list)
4756 expression_list = void_zero_node;
4757
4758 return expression_list;
4759 }
4760
4761 /* Parse a delete-expression.
4762
4763 delete-expression:
4764 :: [opt] delete cast-expression
4765 :: [opt] delete [ ] cast-expression
4766
4767 Returns a representation of the expression. */
4768
4769 static tree
4770 cp_parser_delete_expression (cp_parser* parser)
4771 {
4772 bool global_scope_p;
4773 bool array_p;
4774 tree expression;
4775
4776 /* Look for the optional `::' operator. */
4777 global_scope_p
4778 = (cp_parser_global_scope_opt (parser,
4779 /*current_scope_valid_p=*/false)
4780 != NULL_TREE);
4781 /* Look for the `delete' keyword. */
4782 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4783 /* See if the array syntax is in use. */
4784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4785 {
4786 /* Consume the `[' token. */
4787 cp_lexer_consume_token (parser->lexer);
4788 /* Look for the `]' token. */
4789 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4790 /* Remember that this is the `[]' construct. */
4791 array_p = true;
4792 }
4793 else
4794 array_p = false;
4795
4796 /* Parse the cast-expression. */
4797 expression = cp_parser_simple_cast_expression (parser);
4798
4799 /* A delete-expression may not appear in an integral constant
4800 expression. */
4801 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4802 return error_mark_node;
4803
4804 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4805 }
4806
4807 /* Parse a cast-expression.
4808
4809 cast-expression:
4810 unary-expression
4811 ( type-id ) cast-expression
4812
4813 Returns a representation of the expression. */
4814
4815 static tree
4816 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4817 {
4818 /* If it's a `(', then we might be looking at a cast. */
4819 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4820 {
4821 tree type = NULL_TREE;
4822 tree expr = NULL_TREE;
4823 bool compound_literal_p;
4824 const char *saved_message;
4825
4826 /* There's no way to know yet whether or not this is a cast.
4827 For example, `(int (3))' is a unary-expression, while `(int)
4828 3' is a cast. So, we resort to parsing tentatively. */
4829 cp_parser_parse_tentatively (parser);
4830 /* Types may not be defined in a cast. */
4831 saved_message = parser->type_definition_forbidden_message;
4832 parser->type_definition_forbidden_message
4833 = "types may not be defined in casts";
4834 /* Consume the `('. */
4835 cp_lexer_consume_token (parser->lexer);
4836 /* A very tricky bit is that `(struct S) { 3 }' is a
4837 compound-literal (which we permit in C++ as an extension).
4838 But, that construct is not a cast-expression -- it is a
4839 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4840 is legal; if the compound-literal were a cast-expression,
4841 you'd need an extra set of parentheses.) But, if we parse
4842 the type-id, and it happens to be a class-specifier, then we
4843 will commit to the parse at that point, because we cannot
4844 undo the action that is done when creating a new class. So,
4845 then we cannot back up and do a postfix-expression.
4846
4847 Therefore, we scan ahead to the closing `)', and check to see
4848 if the token after the `)' is a `{'. If so, we are not
4849 looking at a cast-expression.
4850
4851 Save tokens so that we can put them back. */
4852 cp_lexer_save_tokens (parser->lexer);
4853 /* Skip tokens until the next token is a closing parenthesis.
4854 If we find the closing `)', and the next token is a `{', then
4855 we are looking at a compound-literal. */
4856 compound_literal_p
4857 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4858 /*consume_paren=*/true)
4859 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4860 /* Roll back the tokens we skipped. */
4861 cp_lexer_rollback_tokens (parser->lexer);
4862 /* If we were looking at a compound-literal, simulate an error
4863 so that the call to cp_parser_parse_definitely below will
4864 fail. */
4865 if (compound_literal_p)
4866 cp_parser_simulate_error (parser);
4867 else
4868 {
4869 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4870 parser->in_type_id_in_expr_p = true;
4871 /* Look for the type-id. */
4872 type = cp_parser_type_id (parser);
4873 /* Look for the closing `)'. */
4874 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4875 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4876 }
4877
4878 /* Restore the saved message. */
4879 parser->type_definition_forbidden_message = saved_message;
4880
4881 /* If ok so far, parse the dependent expression. We cannot be
4882 sure it is a cast. Consider `(T ())'. It is a parenthesized
4883 ctor of T, but looks like a cast to function returning T
4884 without a dependent expression. */
4885 if (!cp_parser_error_occurred (parser))
4886 expr = cp_parser_simple_cast_expression (parser);
4887
4888 if (cp_parser_parse_definitely (parser))
4889 {
4890 /* Warn about old-style casts, if so requested. */
4891 if (warn_old_style_cast
4892 && !in_system_header
4893 && !VOID_TYPE_P (type)
4894 && current_lang_name != lang_name_c)
4895 warning ("use of old-style cast");
4896
4897 /* Only type conversions to integral or enumeration types
4898 can be used in constant-expressions. */
4899 if (parser->integral_constant_expression_p
4900 && !dependent_type_p (type)
4901 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4902 && (cp_parser_non_integral_constant_expression
4903 (parser,
4904 "a cast to a type other than an integral or "
4905 "enumeration type")))
4906 return error_mark_node;
4907
4908 /* Perform the cast. */
4909 expr = build_c_cast (type, expr);
4910 return expr;
4911 }
4912 }
4913
4914 /* If we get here, then it's not a cast, so it must be a
4915 unary-expression. */
4916 return cp_parser_unary_expression (parser, address_p);
4917 }
4918
4919 /* Parse a pm-expression.
4920
4921 pm-expression:
4922 cast-expression
4923 pm-expression .* cast-expression
4924 pm-expression ->* cast-expression
4925
4926 Returns a representation of the expression. */
4927
4928 static tree
4929 cp_parser_pm_expression (cp_parser* parser)
4930 {
4931 static const cp_parser_token_tree_map map = {
4932 { CPP_DEREF_STAR, MEMBER_REF },
4933 { CPP_DOT_STAR, DOTSTAR_EXPR },
4934 { CPP_EOF, ERROR_MARK }
4935 };
4936
4937 return cp_parser_binary_expression (parser, map,
4938 cp_parser_simple_cast_expression);
4939 }
4940
4941 /* Parse a multiplicative-expression.
4942
4943 multiplicative-expression:
4944 pm-expression
4945 multiplicative-expression * pm-expression
4946 multiplicative-expression / pm-expression
4947 multiplicative-expression % pm-expression
4948
4949 Returns a representation of the expression. */
4950
4951 static tree
4952 cp_parser_multiplicative_expression (cp_parser* parser)
4953 {
4954 static const cp_parser_token_tree_map map = {
4955 { CPP_MULT, MULT_EXPR },
4956 { CPP_DIV, TRUNC_DIV_EXPR },
4957 { CPP_MOD, TRUNC_MOD_EXPR },
4958 { CPP_EOF, ERROR_MARK }
4959 };
4960
4961 return cp_parser_binary_expression (parser,
4962 map,
4963 cp_parser_pm_expression);
4964 }
4965
4966 /* Parse an additive-expression.
4967
4968 additive-expression:
4969 multiplicative-expression
4970 additive-expression + multiplicative-expression
4971 additive-expression - multiplicative-expression
4972
4973 Returns a representation of the expression. */
4974
4975 static tree
4976 cp_parser_additive_expression (cp_parser* parser)
4977 {
4978 static const cp_parser_token_tree_map map = {
4979 { CPP_PLUS, PLUS_EXPR },
4980 { CPP_MINUS, MINUS_EXPR },
4981 { CPP_EOF, ERROR_MARK }
4982 };
4983
4984 return cp_parser_binary_expression (parser,
4985 map,
4986 cp_parser_multiplicative_expression);
4987 }
4988
4989 /* Parse a shift-expression.
4990
4991 shift-expression:
4992 additive-expression
4993 shift-expression << additive-expression
4994 shift-expression >> additive-expression
4995
4996 Returns a representation of the expression. */
4997
4998 static tree
4999 cp_parser_shift_expression (cp_parser* parser)
5000 {
5001 static const cp_parser_token_tree_map map = {
5002 { CPP_LSHIFT, LSHIFT_EXPR },
5003 { CPP_RSHIFT, RSHIFT_EXPR },
5004 { CPP_EOF, ERROR_MARK }
5005 };
5006
5007 return cp_parser_binary_expression (parser,
5008 map,
5009 cp_parser_additive_expression);
5010 }
5011
5012 /* Parse a relational-expression.
5013
5014 relational-expression:
5015 shift-expression
5016 relational-expression < shift-expression
5017 relational-expression > shift-expression
5018 relational-expression <= shift-expression
5019 relational-expression >= shift-expression
5020
5021 GNU Extension:
5022
5023 relational-expression:
5024 relational-expression <? shift-expression
5025 relational-expression >? shift-expression
5026
5027 Returns a representation of the expression. */
5028
5029 static tree
5030 cp_parser_relational_expression (cp_parser* parser)
5031 {
5032 static const cp_parser_token_tree_map map = {
5033 { CPP_LESS, LT_EXPR },
5034 { CPP_GREATER, GT_EXPR },
5035 { CPP_LESS_EQ, LE_EXPR },
5036 { CPP_GREATER_EQ, GE_EXPR },
5037 { CPP_MIN, MIN_EXPR },
5038 { CPP_MAX, MAX_EXPR },
5039 { CPP_EOF, ERROR_MARK }
5040 };
5041
5042 return cp_parser_binary_expression (parser,
5043 map,
5044 cp_parser_shift_expression);
5045 }
5046
5047 /* Parse an equality-expression.
5048
5049 equality-expression:
5050 relational-expression
5051 equality-expression == relational-expression
5052 equality-expression != relational-expression
5053
5054 Returns a representation of the expression. */
5055
5056 static tree
5057 cp_parser_equality_expression (cp_parser* parser)
5058 {
5059 static const cp_parser_token_tree_map map = {
5060 { CPP_EQ_EQ, EQ_EXPR },
5061 { CPP_NOT_EQ, NE_EXPR },
5062 { CPP_EOF, ERROR_MARK }
5063 };
5064
5065 return cp_parser_binary_expression (parser,
5066 map,
5067 cp_parser_relational_expression);
5068 }
5069
5070 /* Parse an and-expression.
5071
5072 and-expression:
5073 equality-expression
5074 and-expression & equality-expression
5075
5076 Returns a representation of the expression. */
5077
5078 static tree
5079 cp_parser_and_expression (cp_parser* parser)
5080 {
5081 static const cp_parser_token_tree_map map = {
5082 { CPP_AND, BIT_AND_EXPR },
5083 { CPP_EOF, ERROR_MARK }
5084 };
5085
5086 return cp_parser_binary_expression (parser,
5087 map,
5088 cp_parser_equality_expression);
5089 }
5090
5091 /* Parse an exclusive-or-expression.
5092
5093 exclusive-or-expression:
5094 and-expression
5095 exclusive-or-expression ^ and-expression
5096
5097 Returns a representation of the expression. */
5098
5099 static tree
5100 cp_parser_exclusive_or_expression (cp_parser* parser)
5101 {
5102 static const cp_parser_token_tree_map map = {
5103 { CPP_XOR, BIT_XOR_EXPR },
5104 { CPP_EOF, ERROR_MARK }
5105 };
5106
5107 return cp_parser_binary_expression (parser,
5108 map,
5109 cp_parser_and_expression);
5110 }
5111
5112
5113 /* Parse an inclusive-or-expression.
5114
5115 inclusive-or-expression:
5116 exclusive-or-expression
5117 inclusive-or-expression | exclusive-or-expression
5118
5119 Returns a representation of the expression. */
5120
5121 static tree
5122 cp_parser_inclusive_or_expression (cp_parser* parser)
5123 {
5124 static const cp_parser_token_tree_map map = {
5125 { CPP_OR, BIT_IOR_EXPR },
5126 { CPP_EOF, ERROR_MARK }
5127 };
5128
5129 return cp_parser_binary_expression (parser,
5130 map,
5131 cp_parser_exclusive_or_expression);
5132 }
5133
5134 /* Parse a logical-and-expression.
5135
5136 logical-and-expression:
5137 inclusive-or-expression
5138 logical-and-expression && inclusive-or-expression
5139
5140 Returns a representation of the expression. */
5141
5142 static tree
5143 cp_parser_logical_and_expression (cp_parser* parser)
5144 {
5145 static const cp_parser_token_tree_map map = {
5146 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5147 { CPP_EOF, ERROR_MARK }
5148 };
5149
5150 return cp_parser_binary_expression (parser,
5151 map,
5152 cp_parser_inclusive_or_expression);
5153 }
5154
5155 /* Parse a logical-or-expression.
5156
5157 logical-or-expression:
5158 logical-and-expression
5159 logical-or-expression || logical-and-expression
5160
5161 Returns a representation of the expression. */
5162
5163 static tree
5164 cp_parser_logical_or_expression (cp_parser* parser)
5165 {
5166 static const cp_parser_token_tree_map map = {
5167 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5168 { CPP_EOF, ERROR_MARK }
5169 };
5170
5171 return cp_parser_binary_expression (parser,
5172 map,
5173 cp_parser_logical_and_expression);
5174 }
5175
5176 /* Parse the `? expression : assignment-expression' part of a
5177 conditional-expression. The LOGICAL_OR_EXPR is the
5178 logical-or-expression that started the conditional-expression.
5179 Returns a representation of the entire conditional-expression.
5180
5181 This routine is used by cp_parser_assignment_expression.
5182
5183 ? expression : assignment-expression
5184
5185 GNU Extensions:
5186
5187 ? : assignment-expression */
5188
5189 static tree
5190 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5191 {
5192 tree expr;
5193 tree assignment_expr;
5194
5195 /* Consume the `?' token. */
5196 cp_lexer_consume_token (parser->lexer);
5197 if (cp_parser_allow_gnu_extensions_p (parser)
5198 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5199 /* Implicit true clause. */
5200 expr = NULL_TREE;
5201 else
5202 /* Parse the expression. */
5203 expr = cp_parser_expression (parser);
5204
5205 /* The next token should be a `:'. */
5206 cp_parser_require (parser, CPP_COLON, "`:'");
5207 /* Parse the assignment-expression. */
5208 assignment_expr = cp_parser_assignment_expression (parser);
5209
5210 /* Build the conditional-expression. */
5211 return build_x_conditional_expr (logical_or_expr,
5212 expr,
5213 assignment_expr);
5214 }
5215
5216 /* Parse an assignment-expression.
5217
5218 assignment-expression:
5219 conditional-expression
5220 logical-or-expression assignment-operator assignment_expression
5221 throw-expression
5222
5223 Returns a representation for the expression. */
5224
5225 static tree
5226 cp_parser_assignment_expression (cp_parser* parser)
5227 {
5228 tree expr;
5229
5230 /* If the next token is the `throw' keyword, then we're looking at
5231 a throw-expression. */
5232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5233 expr = cp_parser_throw_expression (parser);
5234 /* Otherwise, it must be that we are looking at a
5235 logical-or-expression. */
5236 else
5237 {
5238 /* Parse the logical-or-expression. */
5239 expr = cp_parser_logical_or_expression (parser);
5240 /* If the next token is a `?' then we're actually looking at a
5241 conditional-expression. */
5242 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5243 return cp_parser_question_colon_clause (parser, expr);
5244 else
5245 {
5246 enum tree_code assignment_operator;
5247
5248 /* If it's an assignment-operator, we're using the second
5249 production. */
5250 assignment_operator
5251 = cp_parser_assignment_operator_opt (parser);
5252 if (assignment_operator != ERROR_MARK)
5253 {
5254 tree rhs;
5255
5256 /* Parse the right-hand side of the assignment. */
5257 rhs = cp_parser_assignment_expression (parser);
5258 /* An assignment may not appear in a
5259 constant-expression. */
5260 if (cp_parser_non_integral_constant_expression (parser,
5261 "an assignment"))
5262 return error_mark_node;
5263 /* Build the assignment expression. */
5264 expr = build_x_modify_expr (expr,
5265 assignment_operator,
5266 rhs);
5267 }
5268 }
5269 }
5270
5271 return expr;
5272 }
5273
5274 /* Parse an (optional) assignment-operator.
5275
5276 assignment-operator: one of
5277 = *= /= %= += -= >>= <<= &= ^= |=
5278
5279 GNU Extension:
5280
5281 assignment-operator: one of
5282 <?= >?=
5283
5284 If the next token is an assignment operator, the corresponding tree
5285 code is returned, and the token is consumed. For example, for
5286 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5287 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5288 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5289 operator, ERROR_MARK is returned. */
5290
5291 static enum tree_code
5292 cp_parser_assignment_operator_opt (cp_parser* parser)
5293 {
5294 enum tree_code op;
5295 cp_token *token;
5296
5297 /* Peek at the next toen. */
5298 token = cp_lexer_peek_token (parser->lexer);
5299
5300 switch (token->type)
5301 {
5302 case CPP_EQ:
5303 op = NOP_EXPR;
5304 break;
5305
5306 case CPP_MULT_EQ:
5307 op = MULT_EXPR;
5308 break;
5309
5310 case CPP_DIV_EQ:
5311 op = TRUNC_DIV_EXPR;
5312 break;
5313
5314 case CPP_MOD_EQ:
5315 op = TRUNC_MOD_EXPR;
5316 break;
5317
5318 case CPP_PLUS_EQ:
5319 op = PLUS_EXPR;
5320 break;
5321
5322 case CPP_MINUS_EQ:
5323 op = MINUS_EXPR;
5324 break;
5325
5326 case CPP_RSHIFT_EQ:
5327 op = RSHIFT_EXPR;
5328 break;
5329
5330 case CPP_LSHIFT_EQ:
5331 op = LSHIFT_EXPR;
5332 break;
5333
5334 case CPP_AND_EQ:
5335 op = BIT_AND_EXPR;
5336 break;
5337
5338 case CPP_XOR_EQ:
5339 op = BIT_XOR_EXPR;
5340 break;
5341
5342 case CPP_OR_EQ:
5343 op = BIT_IOR_EXPR;
5344 break;
5345
5346 case CPP_MIN_EQ:
5347 op = MIN_EXPR;
5348 break;
5349
5350 case CPP_MAX_EQ:
5351 op = MAX_EXPR;
5352 break;
5353
5354 default:
5355 /* Nothing else is an assignment operator. */
5356 op = ERROR_MARK;
5357 }
5358
5359 /* If it was an assignment operator, consume it. */
5360 if (op != ERROR_MARK)
5361 cp_lexer_consume_token (parser->lexer);
5362
5363 return op;
5364 }
5365
5366 /* Parse an expression.
5367
5368 expression:
5369 assignment-expression
5370 expression , assignment-expression
5371
5372 Returns a representation of the expression. */
5373
5374 static tree
5375 cp_parser_expression (cp_parser* parser)
5376 {
5377 tree expression = NULL_TREE;
5378
5379 while (true)
5380 {
5381 tree assignment_expression;
5382
5383 /* Parse the next assignment-expression. */
5384 assignment_expression
5385 = cp_parser_assignment_expression (parser);
5386 /* If this is the first assignment-expression, we can just
5387 save it away. */
5388 if (!expression)
5389 expression = assignment_expression;
5390 else
5391 expression = build_x_compound_expr (expression,
5392 assignment_expression);
5393 /* If the next token is not a comma, then we are done with the
5394 expression. */
5395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5396 break;
5397 /* Consume the `,'. */
5398 cp_lexer_consume_token (parser->lexer);
5399 /* A comma operator cannot appear in a constant-expression. */
5400 if (cp_parser_non_integral_constant_expression (parser,
5401 "a comma operator"))
5402 expression = error_mark_node;
5403 }
5404
5405 return expression;
5406 }
5407
5408 /* Parse a constant-expression.
5409
5410 constant-expression:
5411 conditional-expression
5412
5413 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5414 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5415 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5416 is false, NON_CONSTANT_P should be NULL. */
5417
5418 static tree
5419 cp_parser_constant_expression (cp_parser* parser,
5420 bool allow_non_constant_p,
5421 bool *non_constant_p)
5422 {
5423 bool saved_integral_constant_expression_p;
5424 bool saved_allow_non_integral_constant_expression_p;
5425 bool saved_non_integral_constant_expression_p;
5426 tree expression;
5427
5428 /* It might seem that we could simply parse the
5429 conditional-expression, and then check to see if it were
5430 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5431 one that the compiler can figure out is constant, possibly after
5432 doing some simplifications or optimizations. The standard has a
5433 precise definition of constant-expression, and we must honor
5434 that, even though it is somewhat more restrictive.
5435
5436 For example:
5437
5438 int i[(2, 3)];
5439
5440 is not a legal declaration, because `(2, 3)' is not a
5441 constant-expression. The `,' operator is forbidden in a
5442 constant-expression. However, GCC's constant-folding machinery
5443 will fold this operation to an INTEGER_CST for `3'. */
5444
5445 /* Save the old settings. */
5446 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5447 saved_allow_non_integral_constant_expression_p
5448 = parser->allow_non_integral_constant_expression_p;
5449 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5450 /* We are now parsing a constant-expression. */
5451 parser->integral_constant_expression_p = true;
5452 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5453 parser->non_integral_constant_expression_p = false;
5454 /* Although the grammar says "conditional-expression", we parse an
5455 "assignment-expression", which also permits "throw-expression"
5456 and the use of assignment operators. In the case that
5457 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5458 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5459 actually essential that we look for an assignment-expression.
5460 For example, cp_parser_initializer_clauses uses this function to
5461 determine whether a particular assignment-expression is in fact
5462 constant. */
5463 expression = cp_parser_assignment_expression (parser);
5464 /* Restore the old settings. */
5465 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5466 parser->allow_non_integral_constant_expression_p
5467 = saved_allow_non_integral_constant_expression_p;
5468 if (allow_non_constant_p)
5469 *non_constant_p = parser->non_integral_constant_expression_p;
5470 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5471
5472 return expression;
5473 }
5474
5475 /* Parse __builtin_offsetof.
5476
5477 offsetof-expression:
5478 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5479
5480 offsetof-member-designator:
5481 id-expression
5482 | offsetof-member-designator "." id-expression
5483 | offsetof-member-designator "[" expression "]"
5484 */
5485
5486 static tree
5487 cp_parser_builtin_offsetof (cp_parser *parser)
5488 {
5489 int save_ice_p, save_non_ice_p;
5490 tree type, expr;
5491 cp_id_kind dummy;
5492
5493 /* We're about to accept non-integral-constant things, but will
5494 definitely yield an integral constant expression. Save and
5495 restore these values around our local parsing. */
5496 save_ice_p = parser->integral_constant_expression_p;
5497 save_non_ice_p = parser->non_integral_constant_expression_p;
5498
5499 /* Consume the "__builtin_offsetof" token. */
5500 cp_lexer_consume_token (parser->lexer);
5501 /* Consume the opening `('. */
5502 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5503 /* Parse the type-id. */
5504 type = cp_parser_type_id (parser);
5505 /* Look for the `,'. */
5506 cp_parser_require (parser, CPP_COMMA, "`,'");
5507
5508 /* Build the (type *)null that begins the traditional offsetof macro. */
5509 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5510
5511 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5512 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5513 true, &dummy);
5514 while (true)
5515 {
5516 cp_token *token = cp_lexer_peek_token (parser->lexer);
5517 switch (token->type)
5518 {
5519 case CPP_OPEN_SQUARE:
5520 /* offsetof-member-designator "[" expression "]" */
5521 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5522 break;
5523
5524 case CPP_DOT:
5525 /* offsetof-member-designator "." identifier */
5526 cp_lexer_consume_token (parser->lexer);
5527 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5528 true, &dummy);
5529 break;
5530
5531 case CPP_CLOSE_PAREN:
5532 /* Consume the ")" token. */
5533 cp_lexer_consume_token (parser->lexer);
5534 goto success;
5535
5536 default:
5537 /* Error. We know the following require will fail, but
5538 that gives the proper error message. */
5539 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5540 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5541 expr = error_mark_node;
5542 goto failure;
5543 }
5544 }
5545
5546 success:
5547 /* We've finished the parsing, now finish with the semantics. At present
5548 we're just mirroring the traditional macro implementation. Better
5549 would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5550 here rather than in build_x_unary_op. */
5551 expr = build_reinterpret_cast (build_reference_type (char_type_node), expr);
5552 expr = build_x_unary_op (ADDR_EXPR, expr);
5553 expr = build_reinterpret_cast (size_type_node, expr);
5554
5555 failure:
5556 parser->integral_constant_expression_p = save_ice_p;
5557 parser->non_integral_constant_expression_p = save_non_ice_p;
5558
5559 return expr;
5560 }
5561
5562 /* Statements [gram.stmt.stmt] */
5563
5564 /* Parse a statement.
5565
5566 statement:
5567 labeled-statement
5568 expression-statement
5569 compound-statement
5570 selection-statement
5571 iteration-statement
5572 jump-statement
5573 declaration-statement
5574 try-block */
5575
5576 static void
5577 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5578 {
5579 tree statement;
5580 cp_token *token;
5581 location_t statement_locus;
5582
5583 /* There is no statement yet. */
5584 statement = NULL_TREE;
5585 /* Peek at the next token. */
5586 token = cp_lexer_peek_token (parser->lexer);
5587 /* Remember the location of the first token in the statement. */
5588 statement_locus = token->location;
5589 /* If this is a keyword, then that will often determine what kind of
5590 statement we have. */
5591 if (token->type == CPP_KEYWORD)
5592 {
5593 enum rid keyword = token->keyword;
5594
5595 switch (keyword)
5596 {
5597 case RID_CASE:
5598 case RID_DEFAULT:
5599 statement = cp_parser_labeled_statement (parser,
5600 in_statement_expr_p);
5601 break;
5602
5603 case RID_IF:
5604 case RID_SWITCH:
5605 statement = cp_parser_selection_statement (parser);
5606 break;
5607
5608 case RID_WHILE:
5609 case RID_DO:
5610 case RID_FOR:
5611 statement = cp_parser_iteration_statement (parser);
5612 break;
5613
5614 case RID_BREAK:
5615 case RID_CONTINUE:
5616 case RID_RETURN:
5617 case RID_GOTO:
5618 statement = cp_parser_jump_statement (parser);
5619 break;
5620
5621 case RID_TRY:
5622 statement = cp_parser_try_block (parser);
5623 break;
5624
5625 default:
5626 /* It might be a keyword like `int' that can start a
5627 declaration-statement. */
5628 break;
5629 }
5630 }
5631 else if (token->type == CPP_NAME)
5632 {
5633 /* If the next token is a `:', then we are looking at a
5634 labeled-statement. */
5635 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5636 if (token->type == CPP_COLON)
5637 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5638 }
5639 /* Anything that starts with a `{' must be a compound-statement. */
5640 else if (token->type == CPP_OPEN_BRACE)
5641 statement = cp_parser_compound_statement (parser, false);
5642
5643 /* Everything else must be a declaration-statement or an
5644 expression-statement. Try for the declaration-statement
5645 first, unless we are looking at a `;', in which case we know that
5646 we have an expression-statement. */
5647 if (!statement)
5648 {
5649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5650 {
5651 cp_parser_parse_tentatively (parser);
5652 /* Try to parse the declaration-statement. */
5653 cp_parser_declaration_statement (parser);
5654 /* If that worked, we're done. */
5655 if (cp_parser_parse_definitely (parser))
5656 return;
5657 }
5658 /* Look for an expression-statement instead. */
5659 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5660 }
5661
5662 /* Set the line number for the statement. */
5663 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5664 {
5665 SET_EXPR_LOCUS (statement, NULL);
5666 annotate_with_locus (statement, statement_locus);
5667 }
5668 }
5669
5670 /* Parse a labeled-statement.
5671
5672 labeled-statement:
5673 identifier : statement
5674 case constant-expression : statement
5675 default : statement
5676
5677 GNU Extension:
5678
5679 labeled-statement:
5680 case constant-expression ... constant-expression : statement
5681
5682 Returns the new CASE_LABEL, for a `case' or `default' label. For
5683 an ordinary label, returns a LABEL_STMT. */
5684
5685 static tree
5686 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5687 {
5688 cp_token *token;
5689 tree statement = error_mark_node;
5690
5691 /* The next token should be an identifier. */
5692 token = cp_lexer_peek_token (parser->lexer);
5693 if (token->type != CPP_NAME
5694 && token->type != CPP_KEYWORD)
5695 {
5696 cp_parser_error (parser, "expected labeled-statement");
5697 return error_mark_node;
5698 }
5699
5700 switch (token->keyword)
5701 {
5702 case RID_CASE:
5703 {
5704 tree expr, expr_hi;
5705 cp_token *ellipsis;
5706
5707 /* Consume the `case' token. */
5708 cp_lexer_consume_token (parser->lexer);
5709 /* Parse the constant-expression. */
5710 expr = cp_parser_constant_expression (parser,
5711 /*allow_non_constant_p=*/false,
5712 NULL);
5713
5714 ellipsis = cp_lexer_peek_token (parser->lexer);
5715 if (ellipsis->type == CPP_ELLIPSIS)
5716 {
5717 /* Consume the `...' token. */
5718 cp_lexer_consume_token (parser->lexer);
5719 expr_hi =
5720 cp_parser_constant_expression (parser,
5721 /*allow_non_constant_p=*/false,
5722 NULL);
5723 /* We don't need to emit warnings here, as the common code
5724 will do this for us. */
5725 }
5726 else
5727 expr_hi = NULL_TREE;
5728
5729 if (!parser->in_switch_statement_p)
5730 error ("case label `%E' not within a switch statement", expr);
5731 else
5732 statement = finish_case_label (expr, expr_hi);
5733 }
5734 break;
5735
5736 case RID_DEFAULT:
5737 /* Consume the `default' token. */
5738 cp_lexer_consume_token (parser->lexer);
5739 if (!parser->in_switch_statement_p)
5740 error ("case label not within a switch statement");
5741 else
5742 statement = finish_case_label (NULL_TREE, NULL_TREE);
5743 break;
5744
5745 default:
5746 /* Anything else must be an ordinary label. */
5747 statement = finish_label_stmt (cp_parser_identifier (parser));
5748 break;
5749 }
5750
5751 /* Require the `:' token. */
5752 cp_parser_require (parser, CPP_COLON, "`:'");
5753 /* Parse the labeled statement. */
5754 cp_parser_statement (parser, in_statement_expr_p);
5755
5756 /* Return the label, in the case of a `case' or `default' label. */
5757 return statement;
5758 }
5759
5760 /* Parse an expression-statement.
5761
5762 expression-statement:
5763 expression [opt] ;
5764
5765 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5766 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5767 indicates whether this expression-statement is part of an
5768 expression statement. */
5769
5770 static tree
5771 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5772 {
5773 tree statement = NULL_TREE;
5774
5775 /* If the next token is a ';', then there is no expression
5776 statement. */
5777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5778 statement = cp_parser_expression (parser);
5779
5780 /* Consume the final `;'. */
5781 cp_parser_consume_semicolon_at_end_of_statement (parser);
5782
5783 if (in_statement_expr_p
5784 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5785 {
5786 /* This is the final expression statement of a statement
5787 expression. */
5788 statement = finish_stmt_expr_expr (statement);
5789 }
5790 else if (statement)
5791 statement = finish_expr_stmt (statement);
5792 else
5793 finish_stmt ();
5794
5795 return statement;
5796 }
5797
5798 /* Parse a compound-statement.
5799
5800 compound-statement:
5801 { statement-seq [opt] }
5802
5803 Returns a COMPOUND_STMT representing the statement. */
5804
5805 static tree
5806 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5807 {
5808 tree compound_stmt;
5809
5810 /* Consume the `{'. */
5811 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5812 return error_mark_node;
5813 /* Begin the compound-statement. */
5814 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5815 /* Parse an (optional) statement-seq. */
5816 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5817 /* Finish the compound-statement. */
5818 finish_compound_stmt (compound_stmt);
5819 /* Consume the `}'. */
5820 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5821
5822 return compound_stmt;
5823 }
5824
5825 /* Parse an (optional) statement-seq.
5826
5827 statement-seq:
5828 statement
5829 statement-seq [opt] statement */
5830
5831 static void
5832 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5833 {
5834 /* Scan statements until there aren't any more. */
5835 while (true)
5836 {
5837 /* If we're looking at a `}', then we've run out of statements. */
5838 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5839 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5840 break;
5841
5842 /* Parse the statement. */
5843 cp_parser_statement (parser, in_statement_expr_p);
5844 }
5845 }
5846
5847 /* Parse a selection-statement.
5848
5849 selection-statement:
5850 if ( condition ) statement
5851 if ( condition ) statement else statement
5852 switch ( condition ) statement
5853
5854 Returns the new IF_STMT or SWITCH_STMT. */
5855
5856 static tree
5857 cp_parser_selection_statement (cp_parser* parser)
5858 {
5859 cp_token *token;
5860 enum rid keyword;
5861
5862 /* Peek at the next token. */
5863 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5864
5865 /* See what kind of keyword it is. */
5866 keyword = token->keyword;
5867 switch (keyword)
5868 {
5869 case RID_IF:
5870 case RID_SWITCH:
5871 {
5872 tree statement;
5873 tree condition;
5874
5875 /* Look for the `('. */
5876 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5877 {
5878 cp_parser_skip_to_end_of_statement (parser);
5879 return error_mark_node;
5880 }
5881
5882 /* Begin the selection-statement. */
5883 if (keyword == RID_IF)
5884 statement = begin_if_stmt ();
5885 else
5886 statement = begin_switch_stmt ();
5887
5888 /* Parse the condition. */
5889 condition = cp_parser_condition (parser);
5890 /* Look for the `)'. */
5891 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5892 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5893 /*consume_paren=*/true);
5894
5895 if (keyword == RID_IF)
5896 {
5897 tree then_stmt;
5898
5899 /* Add the condition. */
5900 finish_if_stmt_cond (condition, statement);
5901
5902 /* Parse the then-clause. */
5903 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5904 finish_then_clause (statement);
5905
5906 /* If the next token is `else', parse the else-clause. */
5907 if (cp_lexer_next_token_is_keyword (parser->lexer,
5908 RID_ELSE))
5909 {
5910 tree else_stmt;
5911
5912 /* Consume the `else' keyword. */
5913 cp_lexer_consume_token (parser->lexer);
5914 /* Parse the else-clause. */
5915 else_stmt
5916 = cp_parser_implicitly_scoped_statement (parser);
5917 finish_else_clause (statement);
5918 }
5919
5920 /* Now we're all done with the if-statement. */
5921 finish_if_stmt ();
5922 }
5923 else
5924 {
5925 tree body;
5926 bool in_switch_statement_p;
5927
5928 /* Add the condition. */
5929 finish_switch_cond (condition, statement);
5930
5931 /* Parse the body of the switch-statement. */
5932 in_switch_statement_p = parser->in_switch_statement_p;
5933 parser->in_switch_statement_p = true;
5934 body = cp_parser_implicitly_scoped_statement (parser);
5935 parser->in_switch_statement_p = in_switch_statement_p;
5936
5937 /* Now we're all done with the switch-statement. */
5938 finish_switch_stmt (statement);
5939 }
5940
5941 return statement;
5942 }
5943 break;
5944
5945 default:
5946 cp_parser_error (parser, "expected selection-statement");
5947 return error_mark_node;
5948 }
5949 }
5950
5951 /* Parse a condition.
5952
5953 condition:
5954 expression
5955 type-specifier-seq declarator = assignment-expression
5956
5957 GNU Extension:
5958
5959 condition:
5960 type-specifier-seq declarator asm-specification [opt]
5961 attributes [opt] = assignment-expression
5962
5963 Returns the expression that should be tested. */
5964
5965 static tree
5966 cp_parser_condition (cp_parser* parser)
5967 {
5968 tree type_specifiers;
5969 const char *saved_message;
5970
5971 /* Try the declaration first. */
5972 cp_parser_parse_tentatively (parser);
5973 /* New types are not allowed in the type-specifier-seq for a
5974 condition. */
5975 saved_message = parser->type_definition_forbidden_message;
5976 parser->type_definition_forbidden_message
5977 = "types may not be defined in conditions";
5978 /* Parse the type-specifier-seq. */
5979 type_specifiers = cp_parser_type_specifier_seq (parser);
5980 /* Restore the saved message. */
5981 parser->type_definition_forbidden_message = saved_message;
5982 /* If all is well, we might be looking at a declaration. */
5983 if (!cp_parser_error_occurred (parser))
5984 {
5985 tree decl;
5986 tree asm_specification;
5987 tree attributes;
5988 tree declarator;
5989 tree initializer = NULL_TREE;
5990
5991 /* Parse the declarator. */
5992 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5993 /*ctor_dtor_or_conv_p=*/NULL,
5994 /*parenthesized_p=*/NULL);
5995 /* Parse the attributes. */
5996 attributes = cp_parser_attributes_opt (parser);
5997 /* Parse the asm-specification. */
5998 asm_specification = cp_parser_asm_specification_opt (parser);
5999 /* If the next token is not an `=', then we might still be
6000 looking at an expression. For example:
6001
6002 if (A(a).x)
6003
6004 looks like a decl-specifier-seq and a declarator -- but then
6005 there is no `=', so this is an expression. */
6006 cp_parser_require (parser, CPP_EQ, "`='");
6007 /* If we did see an `=', then we are looking at a declaration
6008 for sure. */
6009 if (cp_parser_parse_definitely (parser))
6010 {
6011 /* Create the declaration. */
6012 decl = start_decl (declarator, type_specifiers,
6013 /*initialized_p=*/true,
6014 attributes, /*prefix_attributes=*/NULL_TREE);
6015 /* Parse the assignment-expression. */
6016 initializer = cp_parser_assignment_expression (parser);
6017
6018 /* Process the initializer. */
6019 cp_finish_decl (decl,
6020 initializer,
6021 asm_specification,
6022 LOOKUP_ONLYCONVERTING);
6023
6024 return convert_from_reference (decl);
6025 }
6026 }
6027 /* If we didn't even get past the declarator successfully, we are
6028 definitely not looking at a declaration. */
6029 else
6030 cp_parser_abort_tentative_parse (parser);
6031
6032 /* Otherwise, we are looking at an expression. */
6033 return cp_parser_expression (parser);
6034 }
6035
6036 /* Parse an iteration-statement.
6037
6038 iteration-statement:
6039 while ( condition ) statement
6040 do statement while ( expression ) ;
6041 for ( for-init-statement condition [opt] ; expression [opt] )
6042 statement
6043
6044 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6045
6046 static tree
6047 cp_parser_iteration_statement (cp_parser* parser)
6048 {
6049 cp_token *token;
6050 enum rid keyword;
6051 tree statement;
6052 bool in_iteration_statement_p;
6053
6054
6055 /* Peek at the next token. */
6056 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6057 if (!token)
6058 return error_mark_node;
6059
6060 /* Remember whether or not we are already within an iteration
6061 statement. */
6062 in_iteration_statement_p = parser->in_iteration_statement_p;
6063
6064 /* See what kind of keyword it is. */
6065 keyword = token->keyword;
6066 switch (keyword)
6067 {
6068 case RID_WHILE:
6069 {
6070 tree condition;
6071
6072 /* Begin the while-statement. */
6073 statement = begin_while_stmt ();
6074 /* Look for the `('. */
6075 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6076 /* Parse the condition. */
6077 condition = cp_parser_condition (parser);
6078 finish_while_stmt_cond (condition, statement);
6079 /* Look for the `)'. */
6080 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6081 /* Parse the dependent statement. */
6082 parser->in_iteration_statement_p = true;
6083 cp_parser_already_scoped_statement (parser);
6084 parser->in_iteration_statement_p = in_iteration_statement_p;
6085 /* We're done with the while-statement. */
6086 finish_while_stmt (statement);
6087 }
6088 break;
6089
6090 case RID_DO:
6091 {
6092 tree expression;
6093
6094 /* Begin the do-statement. */
6095 statement = begin_do_stmt ();
6096 /* Parse the body of the do-statement. */
6097 parser->in_iteration_statement_p = true;
6098 cp_parser_implicitly_scoped_statement (parser);
6099 parser->in_iteration_statement_p = in_iteration_statement_p;
6100 finish_do_body (statement);
6101 /* Look for the `while' keyword. */
6102 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6103 /* Look for the `('. */
6104 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6105 /* Parse the expression. */
6106 expression = cp_parser_expression (parser);
6107 /* We're done with the do-statement. */
6108 finish_do_stmt (expression, statement);
6109 /* Look for the `)'. */
6110 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6111 /* Look for the `;'. */
6112 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6113 }
6114 break;
6115
6116 case RID_FOR:
6117 {
6118 tree condition = NULL_TREE;
6119 tree expression = NULL_TREE;
6120
6121 /* Begin the for-statement. */
6122 statement = begin_for_stmt ();
6123 /* Look for the `('. */
6124 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6125 /* Parse the initialization. */
6126 cp_parser_for_init_statement (parser);
6127 finish_for_init_stmt (statement);
6128
6129 /* If there's a condition, process it. */
6130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6131 condition = cp_parser_condition (parser);
6132 finish_for_cond (condition, statement);
6133 /* Look for the `;'. */
6134 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6135
6136 /* If there's an expression, process it. */
6137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6138 expression = cp_parser_expression (parser);
6139 finish_for_expr (expression, statement);
6140 /* Look for the `)'. */
6141 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6142
6143 /* Parse the body of the for-statement. */
6144 parser->in_iteration_statement_p = true;
6145 cp_parser_already_scoped_statement (parser);
6146 parser->in_iteration_statement_p = in_iteration_statement_p;
6147
6148 /* We're done with the for-statement. */
6149 finish_for_stmt (statement);
6150 }
6151 break;
6152
6153 default:
6154 cp_parser_error (parser, "expected iteration-statement");
6155 statement = error_mark_node;
6156 break;
6157 }
6158
6159 return statement;
6160 }
6161
6162 /* Parse a for-init-statement.
6163
6164 for-init-statement:
6165 expression-statement
6166 simple-declaration */
6167
6168 static void
6169 cp_parser_for_init_statement (cp_parser* parser)
6170 {
6171 /* If the next token is a `;', then we have an empty
6172 expression-statement. Grammatically, this is also a
6173 simple-declaration, but an invalid one, because it does not
6174 declare anything. Therefore, if we did not handle this case
6175 specially, we would issue an error message about an invalid
6176 declaration. */
6177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6178 {
6179 /* We're going to speculatively look for a declaration, falling back
6180 to an expression, if necessary. */
6181 cp_parser_parse_tentatively (parser);
6182 /* Parse the declaration. */
6183 cp_parser_simple_declaration (parser,
6184 /*function_definition_allowed_p=*/false);
6185 /* If the tentative parse failed, then we shall need to look for an
6186 expression-statement. */
6187 if (cp_parser_parse_definitely (parser))
6188 return;
6189 }
6190
6191 cp_parser_expression_statement (parser, false);
6192 }
6193
6194 /* Parse a jump-statement.
6195
6196 jump-statement:
6197 break ;
6198 continue ;
6199 return expression [opt] ;
6200 goto identifier ;
6201
6202 GNU extension:
6203
6204 jump-statement:
6205 goto * expression ;
6206
6207 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6208 GOTO_STMT. */
6209
6210 static tree
6211 cp_parser_jump_statement (cp_parser* parser)
6212 {
6213 tree statement = error_mark_node;
6214 cp_token *token;
6215 enum rid keyword;
6216
6217 /* Peek at the next token. */
6218 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6219 if (!token)
6220 return error_mark_node;
6221
6222 /* See what kind of keyword it is. */
6223 keyword = token->keyword;
6224 switch (keyword)
6225 {
6226 case RID_BREAK:
6227 if (!parser->in_switch_statement_p
6228 && !parser->in_iteration_statement_p)
6229 {
6230 error ("break statement not within loop or switch");
6231 statement = error_mark_node;
6232 }
6233 else
6234 statement = finish_break_stmt ();
6235 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6236 break;
6237
6238 case RID_CONTINUE:
6239 if (!parser->in_iteration_statement_p)
6240 {
6241 error ("continue statement not within a loop");
6242 statement = error_mark_node;
6243 }
6244 else
6245 statement = finish_continue_stmt ();
6246 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6247 break;
6248
6249 case RID_RETURN:
6250 {
6251 tree expr;
6252
6253 /* If the next token is a `;', then there is no
6254 expression. */
6255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6256 expr = cp_parser_expression (parser);
6257 else
6258 expr = NULL_TREE;
6259 /* Build the return-statement. */
6260 statement = finish_return_stmt (expr);
6261 /* Look for the final `;'. */
6262 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6263 }
6264 break;
6265
6266 case RID_GOTO:
6267 /* Create the goto-statement. */
6268 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6269 {
6270 /* Issue a warning about this use of a GNU extension. */
6271 if (pedantic)
6272 pedwarn ("ISO C++ forbids computed gotos");
6273 /* Consume the '*' token. */
6274 cp_lexer_consume_token (parser->lexer);
6275 /* Parse the dependent expression. */
6276 finish_goto_stmt (cp_parser_expression (parser));
6277 }
6278 else
6279 finish_goto_stmt (cp_parser_identifier (parser));
6280 /* Look for the final `;'. */
6281 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6282 break;
6283
6284 default:
6285 cp_parser_error (parser, "expected jump-statement");
6286 break;
6287 }
6288
6289 return statement;
6290 }
6291
6292 /* Parse a declaration-statement.
6293
6294 declaration-statement:
6295 block-declaration */
6296
6297 static void
6298 cp_parser_declaration_statement (cp_parser* parser)
6299 {
6300 /* Parse the block-declaration. */
6301 cp_parser_block_declaration (parser, /*statement_p=*/true);
6302
6303 /* Finish off the statement. */
6304 finish_stmt ();
6305 }
6306
6307 /* Some dependent statements (like `if (cond) statement'), are
6308 implicitly in their own scope. In other words, if the statement is
6309 a single statement (as opposed to a compound-statement), it is
6310 none-the-less treated as if it were enclosed in braces. Any
6311 declarations appearing in the dependent statement are out of scope
6312 after control passes that point. This function parses a statement,
6313 but ensures that is in its own scope, even if it is not a
6314 compound-statement.
6315
6316 Returns the new statement. */
6317
6318 static tree
6319 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6320 {
6321 tree statement;
6322
6323 /* If the token is not a `{', then we must take special action. */
6324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6325 {
6326 /* Create a compound-statement. */
6327 statement = begin_compound_stmt (/*has_no_scope=*/false);
6328 /* Parse the dependent-statement. */
6329 cp_parser_statement (parser, false);
6330 /* Finish the dummy compound-statement. */
6331 finish_compound_stmt (statement);
6332 }
6333 /* Otherwise, we simply parse the statement directly. */
6334 else
6335 statement = cp_parser_compound_statement (parser, false);
6336
6337 /* Return the statement. */
6338 return statement;
6339 }
6340
6341 /* For some dependent statements (like `while (cond) statement'), we
6342 have already created a scope. Therefore, even if the dependent
6343 statement is a compound-statement, we do not want to create another
6344 scope. */
6345
6346 static void
6347 cp_parser_already_scoped_statement (cp_parser* parser)
6348 {
6349 /* If the token is not a `{', then we must take special action. */
6350 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6351 {
6352 tree statement;
6353
6354 /* Create a compound-statement. */
6355 statement = begin_compound_stmt (/*has_no_scope=*/true);
6356 /* Parse the dependent-statement. */
6357 cp_parser_statement (parser, false);
6358 /* Finish the dummy compound-statement. */
6359 finish_compound_stmt (statement);
6360 }
6361 /* Otherwise, we simply parse the statement directly. */
6362 else
6363 cp_parser_statement (parser, false);
6364 }
6365
6366 /* Declarations [gram.dcl.dcl] */
6367
6368 /* Parse an optional declaration-sequence.
6369
6370 declaration-seq:
6371 declaration
6372 declaration-seq declaration */
6373
6374 static void
6375 cp_parser_declaration_seq_opt (cp_parser* parser)
6376 {
6377 while (true)
6378 {
6379 cp_token *token;
6380
6381 token = cp_lexer_peek_token (parser->lexer);
6382
6383 if (token->type == CPP_CLOSE_BRACE
6384 || token->type == CPP_EOF)
6385 break;
6386
6387 if (token->type == CPP_SEMICOLON)
6388 {
6389 /* A declaration consisting of a single semicolon is
6390 invalid. Allow it unless we're being pedantic. */
6391 if (pedantic && !in_system_header)
6392 pedwarn ("extra `;'");
6393 cp_lexer_consume_token (parser->lexer);
6394 continue;
6395 }
6396
6397 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6398 parser to enter or exit implicit `extern "C"' blocks. */
6399 while (pending_lang_change > 0)
6400 {
6401 push_lang_context (lang_name_c);
6402 --pending_lang_change;
6403 }
6404 while (pending_lang_change < 0)
6405 {
6406 pop_lang_context ();
6407 ++pending_lang_change;
6408 }
6409
6410 /* Parse the declaration itself. */
6411 cp_parser_declaration (parser);
6412 }
6413 }
6414
6415 /* Parse a declaration.
6416
6417 declaration:
6418 block-declaration
6419 function-definition
6420 template-declaration
6421 explicit-instantiation
6422 explicit-specialization
6423 linkage-specification
6424 namespace-definition
6425
6426 GNU extension:
6427
6428 declaration:
6429 __extension__ declaration */
6430
6431 static void
6432 cp_parser_declaration (cp_parser* parser)
6433 {
6434 cp_token token1;
6435 cp_token token2;
6436 int saved_pedantic;
6437
6438 /* Set this here since we can be called after
6439 pushing the linkage specification. */
6440 c_lex_string_translate = true;
6441
6442 /* Check for the `__extension__' keyword. */
6443 if (cp_parser_extension_opt (parser, &saved_pedantic))
6444 {
6445 /* Parse the qualified declaration. */
6446 cp_parser_declaration (parser);
6447 /* Restore the PEDANTIC flag. */
6448 pedantic = saved_pedantic;
6449
6450 return;
6451 }
6452
6453 /* Try to figure out what kind of declaration is present. */
6454 token1 = *cp_lexer_peek_token (parser->lexer);
6455
6456 /* Don't translate the CPP_STRING in extern "C". */
6457 if (token1.keyword == RID_EXTERN)
6458 c_lex_string_translate = false;
6459
6460 if (token1.type != CPP_EOF)
6461 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6462
6463 c_lex_string_translate = true;
6464
6465 /* If the next token is `extern' and the following token is a string
6466 literal, then we have a linkage specification. */
6467 if (token1.keyword == RID_EXTERN
6468 && cp_parser_is_string_literal (&token2))
6469 cp_parser_linkage_specification (parser);
6470 /* If the next token is `template', then we have either a template
6471 declaration, an explicit instantiation, or an explicit
6472 specialization. */
6473 else if (token1.keyword == RID_TEMPLATE)
6474 {
6475 /* `template <>' indicates a template specialization. */
6476 if (token2.type == CPP_LESS
6477 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6478 cp_parser_explicit_specialization (parser);
6479 /* `template <' indicates a template declaration. */
6480 else if (token2.type == CPP_LESS)
6481 cp_parser_template_declaration (parser, /*member_p=*/false);
6482 /* Anything else must be an explicit instantiation. */
6483 else
6484 cp_parser_explicit_instantiation (parser);
6485 }
6486 /* If the next token is `export', then we have a template
6487 declaration. */
6488 else if (token1.keyword == RID_EXPORT)
6489 cp_parser_template_declaration (parser, /*member_p=*/false);
6490 /* If the next token is `extern', 'static' or 'inline' and the one
6491 after that is `template', we have a GNU extended explicit
6492 instantiation directive. */
6493 else if (cp_parser_allow_gnu_extensions_p (parser)
6494 && (token1.keyword == RID_EXTERN
6495 || token1.keyword == RID_STATIC
6496 || token1.keyword == RID_INLINE)
6497 && token2.keyword == RID_TEMPLATE)
6498 cp_parser_explicit_instantiation (parser);
6499 /* If the next token is `namespace', check for a named or unnamed
6500 namespace definition. */
6501 else if (token1.keyword == RID_NAMESPACE
6502 && (/* A named namespace definition. */
6503 (token2.type == CPP_NAME
6504 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6505 == CPP_OPEN_BRACE))
6506 /* An unnamed namespace definition. */
6507 || token2.type == CPP_OPEN_BRACE))
6508 cp_parser_namespace_definition (parser);
6509 /* We must have either a block declaration or a function
6510 definition. */
6511 else
6512 /* Try to parse a block-declaration, or a function-definition. */
6513 cp_parser_block_declaration (parser, /*statement_p=*/false);
6514 }
6515
6516 /* Parse a block-declaration.
6517
6518 block-declaration:
6519 simple-declaration
6520 asm-definition
6521 namespace-alias-definition
6522 using-declaration
6523 using-directive
6524
6525 GNU Extension:
6526
6527 block-declaration:
6528 __extension__ block-declaration
6529 label-declaration
6530
6531 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6532 part of a declaration-statement. */
6533
6534 static void
6535 cp_parser_block_declaration (cp_parser *parser,
6536 bool statement_p)
6537 {
6538 cp_token *token1;
6539 int saved_pedantic;
6540
6541 /* Check for the `__extension__' keyword. */
6542 if (cp_parser_extension_opt (parser, &saved_pedantic))
6543 {
6544 /* Parse the qualified declaration. */
6545 cp_parser_block_declaration (parser, statement_p);
6546 /* Restore the PEDANTIC flag. */
6547 pedantic = saved_pedantic;
6548
6549 return;
6550 }
6551
6552 /* Peek at the next token to figure out which kind of declaration is
6553 present. */
6554 token1 = cp_lexer_peek_token (parser->lexer);
6555
6556 /* If the next keyword is `asm', we have an asm-definition. */
6557 if (token1->keyword == RID_ASM)
6558 {
6559 if (statement_p)
6560 cp_parser_commit_to_tentative_parse (parser);
6561 cp_parser_asm_definition (parser);
6562 }
6563 /* If the next keyword is `namespace', we have a
6564 namespace-alias-definition. */
6565 else if (token1->keyword == RID_NAMESPACE)
6566 cp_parser_namespace_alias_definition (parser);
6567 /* If the next keyword is `using', we have either a
6568 using-declaration or a using-directive. */
6569 else if (token1->keyword == RID_USING)
6570 {
6571 cp_token *token2;
6572
6573 if (statement_p)
6574 cp_parser_commit_to_tentative_parse (parser);
6575 /* If the token after `using' is `namespace', then we have a
6576 using-directive. */
6577 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6578 if (token2->keyword == RID_NAMESPACE)
6579 cp_parser_using_directive (parser);
6580 /* Otherwise, it's a using-declaration. */
6581 else
6582 cp_parser_using_declaration (parser);
6583 }
6584 /* If the next keyword is `__label__' we have a label declaration. */
6585 else if (token1->keyword == RID_LABEL)
6586 {
6587 if (statement_p)
6588 cp_parser_commit_to_tentative_parse (parser);
6589 cp_parser_label_declaration (parser);
6590 }
6591 /* Anything else must be a simple-declaration. */
6592 else
6593 cp_parser_simple_declaration (parser, !statement_p);
6594 }
6595
6596 /* Parse a simple-declaration.
6597
6598 simple-declaration:
6599 decl-specifier-seq [opt] init-declarator-list [opt] ;
6600
6601 init-declarator-list:
6602 init-declarator
6603 init-declarator-list , init-declarator
6604
6605 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6606 function-definition as a simple-declaration. */
6607
6608 static void
6609 cp_parser_simple_declaration (cp_parser* parser,
6610 bool function_definition_allowed_p)
6611 {
6612 tree decl_specifiers;
6613 tree attributes;
6614 int declares_class_or_enum;
6615 bool saw_declarator;
6616
6617 /* Defer access checks until we know what is being declared; the
6618 checks for names appearing in the decl-specifier-seq should be
6619 done as if we were in the scope of the thing being declared. */
6620 push_deferring_access_checks (dk_deferred);
6621
6622 /* Parse the decl-specifier-seq. We have to keep track of whether
6623 or not the decl-specifier-seq declares a named class or
6624 enumeration type, since that is the only case in which the
6625 init-declarator-list is allowed to be empty.
6626
6627 [dcl.dcl]
6628
6629 In a simple-declaration, the optional init-declarator-list can be
6630 omitted only when declaring a class or enumeration, that is when
6631 the decl-specifier-seq contains either a class-specifier, an
6632 elaborated-type-specifier, or an enum-specifier. */
6633 decl_specifiers
6634 = cp_parser_decl_specifier_seq (parser,
6635 CP_PARSER_FLAGS_OPTIONAL,
6636 &attributes,
6637 &declares_class_or_enum);
6638 /* We no longer need to defer access checks. */
6639 stop_deferring_access_checks ();
6640
6641 /* In a block scope, a valid declaration must always have a
6642 decl-specifier-seq. By not trying to parse declarators, we can
6643 resolve the declaration/expression ambiguity more quickly. */
6644 if (!function_definition_allowed_p && !decl_specifiers)
6645 {
6646 cp_parser_error (parser, "expected declaration");
6647 goto done;
6648 }
6649
6650 /* If the next two tokens are both identifiers, the code is
6651 erroneous. The usual cause of this situation is code like:
6652
6653 T t;
6654
6655 where "T" should name a type -- but does not. */
6656 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6657 {
6658 /* If parsing tentatively, we should commit; we really are
6659 looking at a declaration. */
6660 cp_parser_commit_to_tentative_parse (parser);
6661 /* Give up. */
6662 goto done;
6663 }
6664
6665 /* Keep going until we hit the `;' at the end of the simple
6666 declaration. */
6667 saw_declarator = false;
6668 while (cp_lexer_next_token_is_not (parser->lexer,
6669 CPP_SEMICOLON))
6670 {
6671 cp_token *token;
6672 bool function_definition_p;
6673 tree decl;
6674
6675 saw_declarator = true;
6676 /* Parse the init-declarator. */
6677 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6678 function_definition_allowed_p,
6679 /*member_p=*/false,
6680 declares_class_or_enum,
6681 &function_definition_p);
6682 /* If an error occurred while parsing tentatively, exit quickly.
6683 (That usually happens when in the body of a function; each
6684 statement is treated as a declaration-statement until proven
6685 otherwise.) */
6686 if (cp_parser_error_occurred (parser))
6687 goto done;
6688 /* Handle function definitions specially. */
6689 if (function_definition_p)
6690 {
6691 /* If the next token is a `,', then we are probably
6692 processing something like:
6693
6694 void f() {}, *p;
6695
6696 which is erroneous. */
6697 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6698 error ("mixing declarations and function-definitions is forbidden");
6699 /* Otherwise, we're done with the list of declarators. */
6700 else
6701 {
6702 pop_deferring_access_checks ();
6703 return;
6704 }
6705 }
6706 /* The next token should be either a `,' or a `;'. */
6707 token = cp_lexer_peek_token (parser->lexer);
6708 /* If it's a `,', there are more declarators to come. */
6709 if (token->type == CPP_COMMA)
6710 cp_lexer_consume_token (parser->lexer);
6711 /* If it's a `;', we are done. */
6712 else if (token->type == CPP_SEMICOLON)
6713 break;
6714 /* Anything else is an error. */
6715 else
6716 {
6717 cp_parser_error (parser, "expected `,' or `;'");
6718 /* Skip tokens until we reach the end of the statement. */
6719 cp_parser_skip_to_end_of_statement (parser);
6720 /* If the next token is now a `;', consume it. */
6721 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6722 cp_lexer_consume_token (parser->lexer);
6723 goto done;
6724 }
6725 /* After the first time around, a function-definition is not
6726 allowed -- even if it was OK at first. For example:
6727
6728 int i, f() {}
6729
6730 is not valid. */
6731 function_definition_allowed_p = false;
6732 }
6733
6734 /* Issue an error message if no declarators are present, and the
6735 decl-specifier-seq does not itself declare a class or
6736 enumeration. */
6737 if (!saw_declarator)
6738 {
6739 if (cp_parser_declares_only_class_p (parser))
6740 shadow_tag (decl_specifiers);
6741 /* Perform any deferred access checks. */
6742 perform_deferred_access_checks ();
6743 }
6744
6745 /* Consume the `;'. */
6746 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6747
6748 done:
6749 pop_deferring_access_checks ();
6750 }
6751
6752 /* Parse a decl-specifier-seq.
6753
6754 decl-specifier-seq:
6755 decl-specifier-seq [opt] decl-specifier
6756
6757 decl-specifier:
6758 storage-class-specifier
6759 type-specifier
6760 function-specifier
6761 friend
6762 typedef
6763
6764 GNU Extension:
6765
6766 decl-specifier:
6767 attributes
6768
6769 Returns a TREE_LIST, giving the decl-specifiers in the order they
6770 appear in the source code. The TREE_VALUE of each node is the
6771 decl-specifier. For a keyword (such as `auto' or `friend'), the
6772 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6773 representation of a type-specifier, see cp_parser_type_specifier.
6774
6775 If there are attributes, they will be stored in *ATTRIBUTES,
6776 represented as described above cp_parser_attributes.
6777
6778 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6779 appears, and the entity that will be a friend is not going to be a
6780 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6781 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6782 friendship is granted might not be a class.
6783
6784 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6785 flags:
6786
6787 1: one of the decl-specifiers is an elaborated-type-specifier
6788 (i.e., a type declaration)
6789 2: one of the decl-specifiers is an enum-specifier or a
6790 class-specifier (i.e., a type definition)
6791
6792 */
6793
6794 static tree
6795 cp_parser_decl_specifier_seq (cp_parser* parser,
6796 cp_parser_flags flags,
6797 tree* attributes,
6798 int* declares_class_or_enum)
6799 {
6800 tree decl_specs = NULL_TREE;
6801 bool friend_p = false;
6802 bool constructor_possible_p = !parser->in_declarator_p;
6803
6804 /* Assume no class or enumeration type is declared. */
6805 *declares_class_or_enum = 0;
6806
6807 /* Assume there are no attributes. */
6808 *attributes = NULL_TREE;
6809
6810 /* Keep reading specifiers until there are no more to read. */
6811 while (true)
6812 {
6813 tree decl_spec = NULL_TREE;
6814 bool constructor_p;
6815 cp_token *token;
6816
6817 /* Peek at the next token. */
6818 token = cp_lexer_peek_token (parser->lexer);
6819 /* Handle attributes. */
6820 if (token->keyword == RID_ATTRIBUTE)
6821 {
6822 /* Parse the attributes. */
6823 decl_spec = cp_parser_attributes_opt (parser);
6824 /* Add them to the list. */
6825 *attributes = chainon (*attributes, decl_spec);
6826 continue;
6827 }
6828 /* If the next token is an appropriate keyword, we can simply
6829 add it to the list. */
6830 switch (token->keyword)
6831 {
6832 case RID_FRIEND:
6833 /* decl-specifier:
6834 friend */
6835 if (friend_p)
6836 error ("duplicate `friend'");
6837 else
6838 friend_p = true;
6839 /* The representation of the specifier is simply the
6840 appropriate TREE_IDENTIFIER node. */
6841 decl_spec = token->value;
6842 /* Consume the token. */
6843 cp_lexer_consume_token (parser->lexer);
6844 break;
6845
6846 /* function-specifier:
6847 inline
6848 virtual
6849 explicit */
6850 case RID_INLINE:
6851 case RID_VIRTUAL:
6852 case RID_EXPLICIT:
6853 decl_spec = cp_parser_function_specifier_opt (parser);
6854 break;
6855
6856 /* decl-specifier:
6857 typedef */
6858 case RID_TYPEDEF:
6859 /* The representation of the specifier is simply the
6860 appropriate TREE_IDENTIFIER node. */
6861 decl_spec = token->value;
6862 /* Consume the token. */
6863 cp_lexer_consume_token (parser->lexer);
6864 /* A constructor declarator cannot appear in a typedef. */
6865 constructor_possible_p = false;
6866 /* The "typedef" keyword can only occur in a declaration; we
6867 may as well commit at this point. */
6868 cp_parser_commit_to_tentative_parse (parser);
6869 break;
6870
6871 /* storage-class-specifier:
6872 auto
6873 register
6874 static
6875 extern
6876 mutable
6877
6878 GNU Extension:
6879 thread */
6880 case RID_AUTO:
6881 case RID_REGISTER:
6882 case RID_STATIC:
6883 case RID_EXTERN:
6884 case RID_MUTABLE:
6885 case RID_THREAD:
6886 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6887 break;
6888
6889 default:
6890 break;
6891 }
6892
6893 /* Constructors are a special case. The `S' in `S()' is not a
6894 decl-specifier; it is the beginning of the declarator. */
6895 constructor_p = (!decl_spec
6896 && constructor_possible_p
6897 && cp_parser_constructor_declarator_p (parser,
6898 friend_p));
6899
6900 /* If we don't have a DECL_SPEC yet, then we must be looking at
6901 a type-specifier. */
6902 if (!decl_spec && !constructor_p)
6903 {
6904 int decl_spec_declares_class_or_enum;
6905 bool is_cv_qualifier;
6906
6907 decl_spec
6908 = cp_parser_type_specifier (parser, flags,
6909 friend_p,
6910 /*is_declaration=*/true,
6911 &decl_spec_declares_class_or_enum,
6912 &is_cv_qualifier);
6913
6914 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6915
6916 /* If this type-specifier referenced a user-defined type
6917 (a typedef, class-name, etc.), then we can't allow any
6918 more such type-specifiers henceforth.
6919
6920 [dcl.spec]
6921
6922 The longest sequence of decl-specifiers that could
6923 possibly be a type name is taken as the
6924 decl-specifier-seq of a declaration. The sequence shall
6925 be self-consistent as described below.
6926
6927 [dcl.type]
6928
6929 As a general rule, at most one type-specifier is allowed
6930 in the complete decl-specifier-seq of a declaration. The
6931 only exceptions are the following:
6932
6933 -- const or volatile can be combined with any other
6934 type-specifier.
6935
6936 -- signed or unsigned can be combined with char, long,
6937 short, or int.
6938
6939 -- ..
6940
6941 Example:
6942
6943 typedef char* Pc;
6944 void g (const int Pc);
6945
6946 Here, Pc is *not* part of the decl-specifier seq; it's
6947 the declarator. Therefore, once we see a type-specifier
6948 (other than a cv-qualifier), we forbid any additional
6949 user-defined types. We *do* still allow things like `int
6950 int' to be considered a decl-specifier-seq, and issue the
6951 error message later. */
6952 if (decl_spec && !is_cv_qualifier)
6953 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6954 /* A constructor declarator cannot follow a type-specifier. */
6955 if (decl_spec)
6956 constructor_possible_p = false;
6957 }
6958
6959 /* If we still do not have a DECL_SPEC, then there are no more
6960 decl-specifiers. */
6961 if (!decl_spec)
6962 {
6963 /* Issue an error message, unless the entire construct was
6964 optional. */
6965 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6966 {
6967 cp_parser_error (parser, "expected decl specifier");
6968 return error_mark_node;
6969 }
6970
6971 break;
6972 }
6973
6974 /* Add the DECL_SPEC to the list of specifiers. */
6975 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6976 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6977
6978 /* After we see one decl-specifier, further decl-specifiers are
6979 always optional. */
6980 flags |= CP_PARSER_FLAGS_OPTIONAL;
6981 }
6982
6983 /* Don't allow a friend specifier with a class definition. */
6984 if (friend_p && (*declares_class_or_enum & 2))
6985 error ("class definition may not be declared a friend");
6986
6987 /* We have built up the DECL_SPECS in reverse order. Return them in
6988 the correct order. */
6989 return nreverse (decl_specs);
6990 }
6991
6992 /* Parse an (optional) storage-class-specifier.
6993
6994 storage-class-specifier:
6995 auto
6996 register
6997 static
6998 extern
6999 mutable
7000
7001 GNU Extension:
7002
7003 storage-class-specifier:
7004 thread
7005
7006 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7007
7008 static tree
7009 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7010 {
7011 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7012 {
7013 case RID_AUTO:
7014 case RID_REGISTER:
7015 case RID_STATIC:
7016 case RID_EXTERN:
7017 case RID_MUTABLE:
7018 case RID_THREAD:
7019 /* Consume the token. */
7020 return cp_lexer_consume_token (parser->lexer)->value;
7021
7022 default:
7023 return NULL_TREE;
7024 }
7025 }
7026
7027 /* Parse an (optional) function-specifier.
7028
7029 function-specifier:
7030 inline
7031 virtual
7032 explicit
7033
7034 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7035
7036 static tree
7037 cp_parser_function_specifier_opt (cp_parser* parser)
7038 {
7039 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7040 {
7041 case RID_INLINE:
7042 case RID_VIRTUAL:
7043 case RID_EXPLICIT:
7044 /* Consume the token. */
7045 return cp_lexer_consume_token (parser->lexer)->value;
7046
7047 default:
7048 return NULL_TREE;
7049 }
7050 }
7051
7052 /* Parse a linkage-specification.
7053
7054 linkage-specification:
7055 extern string-literal { declaration-seq [opt] }
7056 extern string-literal declaration */
7057
7058 static void
7059 cp_parser_linkage_specification (cp_parser* parser)
7060 {
7061 cp_token *token;
7062 tree linkage;
7063
7064 /* Look for the `extern' keyword. */
7065 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7066
7067 /* Peek at the next token. */
7068 token = cp_lexer_peek_token (parser->lexer);
7069 /* If it's not a string-literal, then there's a problem. */
7070 if (!cp_parser_is_string_literal (token))
7071 {
7072 cp_parser_error (parser, "expected language-name");
7073 return;
7074 }
7075 /* Consume the token. */
7076 cp_lexer_consume_token (parser->lexer);
7077
7078 /* Transform the literal into an identifier. If the literal is a
7079 wide-character string, or contains embedded NULs, then we can't
7080 handle it as the user wants. */
7081 if (token->type == CPP_WSTRING
7082 || (strlen (TREE_STRING_POINTER (token->value))
7083 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7084 {
7085 cp_parser_error (parser, "invalid linkage-specification");
7086 /* Assume C++ linkage. */
7087 linkage = get_identifier ("c++");
7088 }
7089 /* If it's a simple string constant, things are easier. */
7090 else
7091 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7092
7093 /* We're now using the new linkage. */
7094 push_lang_context (linkage);
7095
7096 /* If the next token is a `{', then we're using the first
7097 production. */
7098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7099 {
7100 /* Consume the `{' token. */
7101 cp_lexer_consume_token (parser->lexer);
7102 /* Parse the declarations. */
7103 cp_parser_declaration_seq_opt (parser);
7104 /* Look for the closing `}'. */
7105 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7106 }
7107 /* Otherwise, there's just one declaration. */
7108 else
7109 {
7110 bool saved_in_unbraced_linkage_specification_p;
7111
7112 saved_in_unbraced_linkage_specification_p
7113 = parser->in_unbraced_linkage_specification_p;
7114 parser->in_unbraced_linkage_specification_p = true;
7115 have_extern_spec = true;
7116 cp_parser_declaration (parser);
7117 have_extern_spec = false;
7118 parser->in_unbraced_linkage_specification_p
7119 = saved_in_unbraced_linkage_specification_p;
7120 }
7121
7122 /* We're done with the linkage-specification. */
7123 pop_lang_context ();
7124 }
7125
7126 /* Special member functions [gram.special] */
7127
7128 /* Parse a conversion-function-id.
7129
7130 conversion-function-id:
7131 operator conversion-type-id
7132
7133 Returns an IDENTIFIER_NODE representing the operator. */
7134
7135 static tree
7136 cp_parser_conversion_function_id (cp_parser* parser)
7137 {
7138 tree type;
7139 tree saved_scope;
7140 tree saved_qualifying_scope;
7141 tree saved_object_scope;
7142 bool pop_p = false;
7143
7144 /* Look for the `operator' token. */
7145 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7146 return error_mark_node;
7147 /* When we parse the conversion-type-id, the current scope will be
7148 reset. However, we need that information in able to look up the
7149 conversion function later, so we save it here. */
7150 saved_scope = parser->scope;
7151 saved_qualifying_scope = parser->qualifying_scope;
7152 saved_object_scope = parser->object_scope;
7153 /* We must enter the scope of the class so that the names of
7154 entities declared within the class are available in the
7155 conversion-type-id. For example, consider:
7156
7157 struct S {
7158 typedef int I;
7159 operator I();
7160 };
7161
7162 S::operator I() { ... }
7163
7164 In order to see that `I' is a type-name in the definition, we
7165 must be in the scope of `S'. */
7166 if (saved_scope)
7167 pop_p = push_scope (saved_scope);
7168 /* Parse the conversion-type-id. */
7169 type = cp_parser_conversion_type_id (parser);
7170 /* Leave the scope of the class, if any. */
7171 if (pop_p)
7172 pop_scope (saved_scope);
7173 /* Restore the saved scope. */
7174 parser->scope = saved_scope;
7175 parser->qualifying_scope = saved_qualifying_scope;
7176 parser->object_scope = saved_object_scope;
7177 /* If the TYPE is invalid, indicate failure. */
7178 if (type == error_mark_node)
7179 return error_mark_node;
7180 return mangle_conv_op_name_for_type (type);
7181 }
7182
7183 /* Parse a conversion-type-id:
7184
7185 conversion-type-id:
7186 type-specifier-seq conversion-declarator [opt]
7187
7188 Returns the TYPE specified. */
7189
7190 static tree
7191 cp_parser_conversion_type_id (cp_parser* parser)
7192 {
7193 tree attributes;
7194 tree type_specifiers;
7195 tree declarator;
7196
7197 /* Parse the attributes. */
7198 attributes = cp_parser_attributes_opt (parser);
7199 /* Parse the type-specifiers. */
7200 type_specifiers = cp_parser_type_specifier_seq (parser);
7201 /* If that didn't work, stop. */
7202 if (type_specifiers == error_mark_node)
7203 return error_mark_node;
7204 /* Parse the conversion-declarator. */
7205 declarator = cp_parser_conversion_declarator_opt (parser);
7206
7207 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7208 /*initialized=*/0, &attributes);
7209 }
7210
7211 /* Parse an (optional) conversion-declarator.
7212
7213 conversion-declarator:
7214 ptr-operator conversion-declarator [opt]
7215
7216 Returns a representation of the declarator. See
7217 cp_parser_declarator for details. */
7218
7219 static tree
7220 cp_parser_conversion_declarator_opt (cp_parser* parser)
7221 {
7222 enum tree_code code;
7223 tree class_type;
7224 tree cv_qualifier_seq;
7225
7226 /* We don't know if there's a ptr-operator next, or not. */
7227 cp_parser_parse_tentatively (parser);
7228 /* Try the ptr-operator. */
7229 code = cp_parser_ptr_operator (parser, &class_type,
7230 &cv_qualifier_seq);
7231 /* If it worked, look for more conversion-declarators. */
7232 if (cp_parser_parse_definitely (parser))
7233 {
7234 tree declarator;
7235
7236 /* Parse another optional declarator. */
7237 declarator = cp_parser_conversion_declarator_opt (parser);
7238
7239 /* Create the representation of the declarator. */
7240 if (code == INDIRECT_REF)
7241 declarator = make_pointer_declarator (cv_qualifier_seq,
7242 declarator);
7243 else
7244 declarator = make_reference_declarator (cv_qualifier_seq,
7245 declarator);
7246
7247 /* Handle the pointer-to-member case. */
7248 if (class_type)
7249 declarator = build_nt (SCOPE_REF, class_type, declarator);
7250
7251 return declarator;
7252 }
7253
7254 return NULL_TREE;
7255 }
7256
7257 /* Parse an (optional) ctor-initializer.
7258
7259 ctor-initializer:
7260 : mem-initializer-list
7261
7262 Returns TRUE iff the ctor-initializer was actually present. */
7263
7264 static bool
7265 cp_parser_ctor_initializer_opt (cp_parser* parser)
7266 {
7267 /* If the next token is not a `:', then there is no
7268 ctor-initializer. */
7269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7270 {
7271 /* Do default initialization of any bases and members. */
7272 if (DECL_CONSTRUCTOR_P (current_function_decl))
7273 finish_mem_initializers (NULL_TREE);
7274
7275 return false;
7276 }
7277
7278 /* Consume the `:' token. */
7279 cp_lexer_consume_token (parser->lexer);
7280 /* And the mem-initializer-list. */
7281 cp_parser_mem_initializer_list (parser);
7282
7283 return true;
7284 }
7285
7286 /* Parse a mem-initializer-list.
7287
7288 mem-initializer-list:
7289 mem-initializer
7290 mem-initializer , mem-initializer-list */
7291
7292 static void
7293 cp_parser_mem_initializer_list (cp_parser* parser)
7294 {
7295 tree mem_initializer_list = NULL_TREE;
7296
7297 /* Let the semantic analysis code know that we are starting the
7298 mem-initializer-list. */
7299 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7300 error ("only constructors take base initializers");
7301
7302 /* Loop through the list. */
7303 while (true)
7304 {
7305 tree mem_initializer;
7306
7307 /* Parse the mem-initializer. */
7308 mem_initializer = cp_parser_mem_initializer (parser);
7309 /* Add it to the list, unless it was erroneous. */
7310 if (mem_initializer)
7311 {
7312 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7313 mem_initializer_list = mem_initializer;
7314 }
7315 /* If the next token is not a `,', we're done. */
7316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7317 break;
7318 /* Consume the `,' token. */
7319 cp_lexer_consume_token (parser->lexer);
7320 }
7321
7322 /* Perform semantic analysis. */
7323 if (DECL_CONSTRUCTOR_P (current_function_decl))
7324 finish_mem_initializers (mem_initializer_list);
7325 }
7326
7327 /* Parse a mem-initializer.
7328
7329 mem-initializer:
7330 mem-initializer-id ( expression-list [opt] )
7331
7332 GNU extension:
7333
7334 mem-initializer:
7335 ( expression-list [opt] )
7336
7337 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7338 class) or FIELD_DECL (for a non-static data member) to initialize;
7339 the TREE_VALUE is the expression-list. */
7340
7341 static tree
7342 cp_parser_mem_initializer (cp_parser* parser)
7343 {
7344 tree mem_initializer_id;
7345 tree expression_list;
7346 tree member;
7347
7348 /* Find out what is being initialized. */
7349 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7350 {
7351 pedwarn ("anachronistic old-style base class initializer");
7352 mem_initializer_id = NULL_TREE;
7353 }
7354 else
7355 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7356 member = expand_member_init (mem_initializer_id);
7357 if (member && !DECL_P (member))
7358 in_base_initializer = 1;
7359
7360 expression_list
7361 = cp_parser_parenthesized_expression_list (parser, false,
7362 /*non_constant_p=*/NULL);
7363 if (!expression_list)
7364 expression_list = void_type_node;
7365
7366 in_base_initializer = 0;
7367
7368 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7369 }
7370
7371 /* Parse a mem-initializer-id.
7372
7373 mem-initializer-id:
7374 :: [opt] nested-name-specifier [opt] class-name
7375 identifier
7376
7377 Returns a TYPE indicating the class to be initializer for the first
7378 production. Returns an IDENTIFIER_NODE indicating the data member
7379 to be initialized for the second production. */
7380
7381 static tree
7382 cp_parser_mem_initializer_id (cp_parser* parser)
7383 {
7384 bool global_scope_p;
7385 bool nested_name_specifier_p;
7386 bool template_p = false;
7387 tree id;
7388
7389 /* `typename' is not allowed in this context ([temp.res]). */
7390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7391 {
7392 error ("keyword `typename' not allowed in this context (a qualified "
7393 "member initializer is implicitly a type)");
7394 cp_lexer_consume_token (parser->lexer);
7395 }
7396 /* Look for the optional `::' operator. */
7397 global_scope_p
7398 = (cp_parser_global_scope_opt (parser,
7399 /*current_scope_valid_p=*/false)
7400 != NULL_TREE);
7401 /* Look for the optional nested-name-specifier. The simplest way to
7402 implement:
7403
7404 [temp.res]
7405
7406 The keyword `typename' is not permitted in a base-specifier or
7407 mem-initializer; in these contexts a qualified name that
7408 depends on a template-parameter is implicitly assumed to be a
7409 type name.
7410
7411 is to assume that we have seen the `typename' keyword at this
7412 point. */
7413 nested_name_specifier_p
7414 = (cp_parser_nested_name_specifier_opt (parser,
7415 /*typename_keyword_p=*/true,
7416 /*check_dependency_p=*/true,
7417 /*type_p=*/true,
7418 /*is_declaration=*/true)
7419 != NULL_TREE);
7420 if (nested_name_specifier_p)
7421 template_p = cp_parser_optional_template_keyword (parser);
7422 /* If there is a `::' operator or a nested-name-specifier, then we
7423 are definitely looking for a class-name. */
7424 if (global_scope_p || nested_name_specifier_p)
7425 return cp_parser_class_name (parser,
7426 /*typename_keyword_p=*/true,
7427 /*template_keyword_p=*/template_p,
7428 /*type_p=*/false,
7429 /*check_dependency_p=*/true,
7430 /*class_head_p=*/false,
7431 /*is_declaration=*/true);
7432 /* Otherwise, we could also be looking for an ordinary identifier. */
7433 cp_parser_parse_tentatively (parser);
7434 /* Try a class-name. */
7435 id = cp_parser_class_name (parser,
7436 /*typename_keyword_p=*/true,
7437 /*template_keyword_p=*/false,
7438 /*type_p=*/false,
7439 /*check_dependency_p=*/true,
7440 /*class_head_p=*/false,
7441 /*is_declaration=*/true);
7442 /* If we found one, we're done. */
7443 if (cp_parser_parse_definitely (parser))
7444 return id;
7445 /* Otherwise, look for an ordinary identifier. */
7446 return cp_parser_identifier (parser);
7447 }
7448
7449 /* Overloading [gram.over] */
7450
7451 /* Parse an operator-function-id.
7452
7453 operator-function-id:
7454 operator operator
7455
7456 Returns an IDENTIFIER_NODE for the operator which is a
7457 human-readable spelling of the identifier, e.g., `operator +'. */
7458
7459 static tree
7460 cp_parser_operator_function_id (cp_parser* parser)
7461 {
7462 /* Look for the `operator' keyword. */
7463 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7464 return error_mark_node;
7465 /* And then the name of the operator itself. */
7466 return cp_parser_operator (parser);
7467 }
7468
7469 /* Parse an operator.
7470
7471 operator:
7472 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7473 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7474 || ++ -- , ->* -> () []
7475
7476 GNU Extensions:
7477
7478 operator:
7479 <? >? <?= >?=
7480
7481 Returns an IDENTIFIER_NODE for the operator which is a
7482 human-readable spelling of the identifier, e.g., `operator +'. */
7483
7484 static tree
7485 cp_parser_operator (cp_parser* parser)
7486 {
7487 tree id = NULL_TREE;
7488 cp_token *token;
7489
7490 /* Peek at the next token. */
7491 token = cp_lexer_peek_token (parser->lexer);
7492 /* Figure out which operator we have. */
7493 switch (token->type)
7494 {
7495 case CPP_KEYWORD:
7496 {
7497 enum tree_code op;
7498
7499 /* The keyword should be either `new' or `delete'. */
7500 if (token->keyword == RID_NEW)
7501 op = NEW_EXPR;
7502 else if (token->keyword == RID_DELETE)
7503 op = DELETE_EXPR;
7504 else
7505 break;
7506
7507 /* Consume the `new' or `delete' token. */
7508 cp_lexer_consume_token (parser->lexer);
7509
7510 /* Peek at the next token. */
7511 token = cp_lexer_peek_token (parser->lexer);
7512 /* If it's a `[' token then this is the array variant of the
7513 operator. */
7514 if (token->type == CPP_OPEN_SQUARE)
7515 {
7516 /* Consume the `[' token. */
7517 cp_lexer_consume_token (parser->lexer);
7518 /* Look for the `]' token. */
7519 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7520 id = ansi_opname (op == NEW_EXPR
7521 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7522 }
7523 /* Otherwise, we have the non-array variant. */
7524 else
7525 id = ansi_opname (op);
7526
7527 return id;
7528 }
7529
7530 case CPP_PLUS:
7531 id = ansi_opname (PLUS_EXPR);
7532 break;
7533
7534 case CPP_MINUS:
7535 id = ansi_opname (MINUS_EXPR);
7536 break;
7537
7538 case CPP_MULT:
7539 id = ansi_opname (MULT_EXPR);
7540 break;
7541
7542 case CPP_DIV:
7543 id = ansi_opname (TRUNC_DIV_EXPR);
7544 break;
7545
7546 case CPP_MOD:
7547 id = ansi_opname (TRUNC_MOD_EXPR);
7548 break;
7549
7550 case CPP_XOR:
7551 id = ansi_opname (BIT_XOR_EXPR);
7552 break;
7553
7554 case CPP_AND:
7555 id = ansi_opname (BIT_AND_EXPR);
7556 break;
7557
7558 case CPP_OR:
7559 id = ansi_opname (BIT_IOR_EXPR);
7560 break;
7561
7562 case CPP_COMPL:
7563 id = ansi_opname (BIT_NOT_EXPR);
7564 break;
7565
7566 case CPP_NOT:
7567 id = ansi_opname (TRUTH_NOT_EXPR);
7568 break;
7569
7570 case CPP_EQ:
7571 id = ansi_assopname (NOP_EXPR);
7572 break;
7573
7574 case CPP_LESS:
7575 id = ansi_opname (LT_EXPR);
7576 break;
7577
7578 case CPP_GREATER:
7579 id = ansi_opname (GT_EXPR);
7580 break;
7581
7582 case CPP_PLUS_EQ:
7583 id = ansi_assopname (PLUS_EXPR);
7584 break;
7585
7586 case CPP_MINUS_EQ:
7587 id = ansi_assopname (MINUS_EXPR);
7588 break;
7589
7590 case CPP_MULT_EQ:
7591 id = ansi_assopname (MULT_EXPR);
7592 break;
7593
7594 case CPP_DIV_EQ:
7595 id = ansi_assopname (TRUNC_DIV_EXPR);
7596 break;
7597
7598 case CPP_MOD_EQ:
7599 id = ansi_assopname (TRUNC_MOD_EXPR);
7600 break;
7601
7602 case CPP_XOR_EQ:
7603 id = ansi_assopname (BIT_XOR_EXPR);
7604 break;
7605
7606 case CPP_AND_EQ:
7607 id = ansi_assopname (BIT_AND_EXPR);
7608 break;
7609
7610 case CPP_OR_EQ:
7611 id = ansi_assopname (BIT_IOR_EXPR);
7612 break;
7613
7614 case CPP_LSHIFT:
7615 id = ansi_opname (LSHIFT_EXPR);
7616 break;
7617
7618 case CPP_RSHIFT:
7619 id = ansi_opname (RSHIFT_EXPR);
7620 break;
7621
7622 case CPP_LSHIFT_EQ:
7623 id = ansi_assopname (LSHIFT_EXPR);
7624 break;
7625
7626 case CPP_RSHIFT_EQ:
7627 id = ansi_assopname (RSHIFT_EXPR);
7628 break;
7629
7630 case CPP_EQ_EQ:
7631 id = ansi_opname (EQ_EXPR);
7632 break;
7633
7634 case CPP_NOT_EQ:
7635 id = ansi_opname (NE_EXPR);
7636 break;
7637
7638 case CPP_LESS_EQ:
7639 id = ansi_opname (LE_EXPR);
7640 break;
7641
7642 case CPP_GREATER_EQ:
7643 id = ansi_opname (GE_EXPR);
7644 break;
7645
7646 case CPP_AND_AND:
7647 id = ansi_opname (TRUTH_ANDIF_EXPR);
7648 break;
7649
7650 case CPP_OR_OR:
7651 id = ansi_opname (TRUTH_ORIF_EXPR);
7652 break;
7653
7654 case CPP_PLUS_PLUS:
7655 id = ansi_opname (POSTINCREMENT_EXPR);
7656 break;
7657
7658 case CPP_MINUS_MINUS:
7659 id = ansi_opname (PREDECREMENT_EXPR);
7660 break;
7661
7662 case CPP_COMMA:
7663 id = ansi_opname (COMPOUND_EXPR);
7664 break;
7665
7666 case CPP_DEREF_STAR:
7667 id = ansi_opname (MEMBER_REF);
7668 break;
7669
7670 case CPP_DEREF:
7671 id = ansi_opname (COMPONENT_REF);
7672 break;
7673
7674 case CPP_OPEN_PAREN:
7675 /* Consume the `('. */
7676 cp_lexer_consume_token (parser->lexer);
7677 /* Look for the matching `)'. */
7678 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7679 return ansi_opname (CALL_EXPR);
7680
7681 case CPP_OPEN_SQUARE:
7682 /* Consume the `['. */
7683 cp_lexer_consume_token (parser->lexer);
7684 /* Look for the matching `]'. */
7685 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7686 return ansi_opname (ARRAY_REF);
7687
7688 /* Extensions. */
7689 case CPP_MIN:
7690 id = ansi_opname (MIN_EXPR);
7691 break;
7692
7693 case CPP_MAX:
7694 id = ansi_opname (MAX_EXPR);
7695 break;
7696
7697 case CPP_MIN_EQ:
7698 id = ansi_assopname (MIN_EXPR);
7699 break;
7700
7701 case CPP_MAX_EQ:
7702 id = ansi_assopname (MAX_EXPR);
7703 break;
7704
7705 default:
7706 /* Anything else is an error. */
7707 break;
7708 }
7709
7710 /* If we have selected an identifier, we need to consume the
7711 operator token. */
7712 if (id)
7713 cp_lexer_consume_token (parser->lexer);
7714 /* Otherwise, no valid operator name was present. */
7715 else
7716 {
7717 cp_parser_error (parser, "expected operator");
7718 id = error_mark_node;
7719 }
7720
7721 return id;
7722 }
7723
7724 /* Parse a template-declaration.
7725
7726 template-declaration:
7727 export [opt] template < template-parameter-list > declaration
7728
7729 If MEMBER_P is TRUE, this template-declaration occurs within a
7730 class-specifier.
7731
7732 The grammar rule given by the standard isn't correct. What
7733 is really meant is:
7734
7735 template-declaration:
7736 export [opt] template-parameter-list-seq
7737 decl-specifier-seq [opt] init-declarator [opt] ;
7738 export [opt] template-parameter-list-seq
7739 function-definition
7740
7741 template-parameter-list-seq:
7742 template-parameter-list-seq [opt]
7743 template < template-parameter-list > */
7744
7745 static void
7746 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7747 {
7748 /* Check for `export'. */
7749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7750 {
7751 /* Consume the `export' token. */
7752 cp_lexer_consume_token (parser->lexer);
7753 /* Warn that we do not support `export'. */
7754 warning ("keyword `export' not implemented, and will be ignored");
7755 }
7756
7757 cp_parser_template_declaration_after_export (parser, member_p);
7758 }
7759
7760 /* Parse a template-parameter-list.
7761
7762 template-parameter-list:
7763 template-parameter
7764 template-parameter-list , template-parameter
7765
7766 Returns a TREE_LIST. Each node represents a template parameter.
7767 The nodes are connected via their TREE_CHAINs. */
7768
7769 static tree
7770 cp_parser_template_parameter_list (cp_parser* parser)
7771 {
7772 tree parameter_list = NULL_TREE;
7773
7774 while (true)
7775 {
7776 tree parameter;
7777 cp_token *token;
7778
7779 /* Parse the template-parameter. */
7780 parameter = cp_parser_template_parameter (parser);
7781 /* Add it to the list. */
7782 parameter_list = process_template_parm (parameter_list,
7783 parameter);
7784
7785 /* Peek at the next token. */
7786 token = cp_lexer_peek_token (parser->lexer);
7787 /* If it's not a `,', we're done. */
7788 if (token->type != CPP_COMMA)
7789 break;
7790 /* Otherwise, consume the `,' token. */
7791 cp_lexer_consume_token (parser->lexer);
7792 }
7793
7794 return parameter_list;
7795 }
7796
7797 /* Parse a template-parameter.
7798
7799 template-parameter:
7800 type-parameter
7801 parameter-declaration
7802
7803 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7804 TREE_PURPOSE is the default value, if any. */
7805
7806 static tree
7807 cp_parser_template_parameter (cp_parser* parser)
7808 {
7809 cp_token *token;
7810
7811 /* Peek at the next token. */
7812 token = cp_lexer_peek_token (parser->lexer);
7813 /* If it is `class' or `template', we have a type-parameter. */
7814 if (token->keyword == RID_TEMPLATE)
7815 return cp_parser_type_parameter (parser);
7816 /* If it is `class' or `typename' we do not know yet whether it is a
7817 type parameter or a non-type parameter. Consider:
7818
7819 template <typename T, typename T::X X> ...
7820
7821 or:
7822
7823 template <class C, class D*> ...
7824
7825 Here, the first parameter is a type parameter, and the second is
7826 a non-type parameter. We can tell by looking at the token after
7827 the identifier -- if it is a `,', `=', or `>' then we have a type
7828 parameter. */
7829 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7830 {
7831 /* Peek at the token after `class' or `typename'. */
7832 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7833 /* If it's an identifier, skip it. */
7834 if (token->type == CPP_NAME)
7835 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7836 /* Now, see if the token looks like the end of a template
7837 parameter. */
7838 if (token->type == CPP_COMMA
7839 || token->type == CPP_EQ
7840 || token->type == CPP_GREATER)
7841 return cp_parser_type_parameter (parser);
7842 }
7843
7844 /* Otherwise, it is a non-type parameter.
7845
7846 [temp.param]
7847
7848 When parsing a default template-argument for a non-type
7849 template-parameter, the first non-nested `>' is taken as the end
7850 of the template parameter-list rather than a greater-than
7851 operator. */
7852 return
7853 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7854 /*parenthesized_p=*/NULL);
7855 }
7856
7857 /* Parse a type-parameter.
7858
7859 type-parameter:
7860 class identifier [opt]
7861 class identifier [opt] = type-id
7862 typename identifier [opt]
7863 typename identifier [opt] = type-id
7864 template < template-parameter-list > class identifier [opt]
7865 template < template-parameter-list > class identifier [opt]
7866 = id-expression
7867
7868 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7869 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7870 the declaration of the parameter. */
7871
7872 static tree
7873 cp_parser_type_parameter (cp_parser* parser)
7874 {
7875 cp_token *token;
7876 tree parameter;
7877
7878 /* Look for a keyword to tell us what kind of parameter this is. */
7879 token = cp_parser_require (parser, CPP_KEYWORD,
7880 "`class', `typename', or `template'");
7881 if (!token)
7882 return error_mark_node;
7883
7884 switch (token->keyword)
7885 {
7886 case RID_CLASS:
7887 case RID_TYPENAME:
7888 {
7889 tree identifier;
7890 tree default_argument;
7891
7892 /* If the next token is an identifier, then it names the
7893 parameter. */
7894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7895 identifier = cp_parser_identifier (parser);
7896 else
7897 identifier = NULL_TREE;
7898
7899 /* Create the parameter. */
7900 parameter = finish_template_type_parm (class_type_node, identifier);
7901
7902 /* If the next token is an `=', we have a default argument. */
7903 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7904 {
7905 /* Consume the `=' token. */
7906 cp_lexer_consume_token (parser->lexer);
7907 /* Parse the default-argument. */
7908 default_argument = cp_parser_type_id (parser);
7909 }
7910 else
7911 default_argument = NULL_TREE;
7912
7913 /* Create the combined representation of the parameter and the
7914 default argument. */
7915 parameter = build_tree_list (default_argument, parameter);
7916 }
7917 break;
7918
7919 case RID_TEMPLATE:
7920 {
7921 tree parameter_list;
7922 tree identifier;
7923 tree default_argument;
7924
7925 /* Look for the `<'. */
7926 cp_parser_require (parser, CPP_LESS, "`<'");
7927 /* Parse the template-parameter-list. */
7928 begin_template_parm_list ();
7929 parameter_list
7930 = cp_parser_template_parameter_list (parser);
7931 parameter_list = end_template_parm_list (parameter_list);
7932 /* Look for the `>'. */
7933 cp_parser_require (parser, CPP_GREATER, "`>'");
7934 /* Look for the `class' keyword. */
7935 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7936 /* If the next token is an `=', then there is a
7937 default-argument. If the next token is a `>', we are at
7938 the end of the parameter-list. If the next token is a `,',
7939 then we are at the end of this parameter. */
7940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7941 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7942 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7943 identifier = cp_parser_identifier (parser);
7944 else
7945 identifier = NULL_TREE;
7946 /* Create the template parameter. */
7947 parameter = finish_template_template_parm (class_type_node,
7948 identifier);
7949
7950 /* If the next token is an `=', then there is a
7951 default-argument. */
7952 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7953 {
7954 bool is_template;
7955
7956 /* Consume the `='. */
7957 cp_lexer_consume_token (parser->lexer);
7958 /* Parse the id-expression. */
7959 default_argument
7960 = cp_parser_id_expression (parser,
7961 /*template_keyword_p=*/false,
7962 /*check_dependency_p=*/true,
7963 /*template_p=*/&is_template,
7964 /*declarator_p=*/false);
7965 if (TREE_CODE (default_argument) == TYPE_DECL)
7966 /* If the id-expression was a template-id that refers to
7967 a template-class, we already have the declaration here,
7968 so no further lookup is needed. */
7969 ;
7970 else
7971 /* Look up the name. */
7972 default_argument
7973 = cp_parser_lookup_name (parser, default_argument,
7974 /*is_type=*/false,
7975 /*is_template=*/is_template,
7976 /*is_namespace=*/false,
7977 /*check_dependency=*/true);
7978 /* See if the default argument is valid. */
7979 default_argument
7980 = check_template_template_default_arg (default_argument);
7981 }
7982 else
7983 default_argument = NULL_TREE;
7984
7985 /* Create the combined representation of the parameter and the
7986 default argument. */
7987 parameter = build_tree_list (default_argument, parameter);
7988 }
7989 break;
7990
7991 default:
7992 /* Anything else is an error. */
7993 cp_parser_error (parser,
7994 "expected `class', `typename', or `template'");
7995 parameter = error_mark_node;
7996 }
7997
7998 return parameter;
7999 }
8000
8001 /* Parse a template-id.
8002
8003 template-id:
8004 template-name < template-argument-list [opt] >
8005
8006 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8007 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8008 returned. Otherwise, if the template-name names a function, or set
8009 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8010 names a class, returns a TYPE_DECL for the specialization.
8011
8012 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8013 uninstantiated templates. */
8014
8015 static tree
8016 cp_parser_template_id (cp_parser *parser,
8017 bool template_keyword_p,
8018 bool check_dependency_p,
8019 bool is_declaration)
8020 {
8021 tree template;
8022 tree arguments;
8023 tree template_id;
8024 ptrdiff_t start_of_id;
8025 tree access_check = NULL_TREE;
8026 cp_token *next_token, *next_token_2;
8027 bool is_identifier;
8028
8029 /* If the next token corresponds to a template-id, there is no need
8030 to reparse it. */
8031 next_token = cp_lexer_peek_token (parser->lexer);
8032 if (next_token->type == CPP_TEMPLATE_ID)
8033 {
8034 tree value;
8035 tree check;
8036
8037 /* Get the stored value. */
8038 value = cp_lexer_consume_token (parser->lexer)->value;
8039 /* Perform any access checks that were deferred. */
8040 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8041 perform_or_defer_access_check (TREE_PURPOSE (check),
8042 TREE_VALUE (check));
8043 /* Return the stored value. */
8044 return TREE_VALUE (value);
8045 }
8046
8047 /* Avoid performing name lookup if there is no possibility of
8048 finding a template-id. */
8049 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8050 || (next_token->type == CPP_NAME
8051 && !cp_parser_nth_token_starts_template_argument_list_p
8052 (parser, 2)))
8053 {
8054 cp_parser_error (parser, "expected template-id");
8055 return error_mark_node;
8056 }
8057
8058 /* Remember where the template-id starts. */
8059 if (cp_parser_parsing_tentatively (parser)
8060 && !cp_parser_committed_to_tentative_parse (parser))
8061 {
8062 next_token = cp_lexer_peek_token (parser->lexer);
8063 start_of_id = cp_lexer_token_difference (parser->lexer,
8064 parser->lexer->first_token,
8065 next_token);
8066 }
8067 else
8068 start_of_id = -1;
8069
8070 push_deferring_access_checks (dk_deferred);
8071
8072 /* Parse the template-name. */
8073 is_identifier = false;
8074 template = cp_parser_template_name (parser, template_keyword_p,
8075 check_dependency_p,
8076 is_declaration,
8077 &is_identifier);
8078 if (template == error_mark_node || is_identifier)
8079 {
8080 pop_deferring_access_checks ();
8081 return template;
8082 }
8083
8084 /* If we find the sequence `[:' after a template-name, it's probably
8085 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8086 parse correctly the argument list. */
8087 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8088 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8089 if (next_token->type == CPP_OPEN_SQUARE
8090 && next_token->flags & DIGRAPH
8091 && next_token_2->type == CPP_COLON
8092 && !(next_token_2->flags & PREV_WHITE))
8093 {
8094 cp_parser_parse_tentatively (parser);
8095 /* Change `:' into `::'. */
8096 next_token_2->type = CPP_SCOPE;
8097 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8098 CPP_LESS. */
8099 cp_lexer_consume_token (parser->lexer);
8100 /* Parse the arguments. */
8101 arguments = cp_parser_enclosed_template_argument_list (parser);
8102 if (!cp_parser_parse_definitely (parser))
8103 {
8104 /* If we couldn't parse an argument list, then we revert our changes
8105 and return simply an error. Maybe this is not a template-id
8106 after all. */
8107 next_token_2->type = CPP_COLON;
8108 cp_parser_error (parser, "expected `<'");
8109 pop_deferring_access_checks ();
8110 return error_mark_node;
8111 }
8112 /* Otherwise, emit an error about the invalid digraph, but continue
8113 parsing because we got our argument list. */
8114 pedwarn ("`<::' cannot begin a template-argument list");
8115 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8116 "between `<' and `::'");
8117 if (!flag_permissive)
8118 {
8119 static bool hint;
8120 if (!hint)
8121 {
8122 inform ("(if you use `-fpermissive' G++ will accept your code)");
8123 hint = true;
8124 }
8125 }
8126 }
8127 else
8128 {
8129 /* Look for the `<' that starts the template-argument-list. */
8130 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8131 {
8132 pop_deferring_access_checks ();
8133 return error_mark_node;
8134 }
8135 /* Parse the arguments. */
8136 arguments = cp_parser_enclosed_template_argument_list (parser);
8137 }
8138
8139 /* Build a representation of the specialization. */
8140 if (TREE_CODE (template) == IDENTIFIER_NODE)
8141 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8142 else if (DECL_CLASS_TEMPLATE_P (template)
8143 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8144 template_id
8145 = finish_template_type (template, arguments,
8146 cp_lexer_next_token_is (parser->lexer,
8147 CPP_SCOPE));
8148 else
8149 {
8150 /* If it's not a class-template or a template-template, it should be
8151 a function-template. */
8152 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8153 || TREE_CODE (template) == OVERLOAD
8154 || BASELINK_P (template)),
8155 20010716);
8156
8157 template_id = lookup_template_function (template, arguments);
8158 }
8159
8160 /* Retrieve any deferred checks. Do not pop this access checks yet
8161 so the memory will not be reclaimed during token replacing below. */
8162 access_check = get_deferred_access_checks ();
8163
8164 /* If parsing tentatively, replace the sequence of tokens that makes
8165 up the template-id with a CPP_TEMPLATE_ID token. That way,
8166 should we re-parse the token stream, we will not have to repeat
8167 the effort required to do the parse, nor will we issue duplicate
8168 error messages about problems during instantiation of the
8169 template. */
8170 if (start_of_id >= 0)
8171 {
8172 cp_token *token;
8173
8174 /* Find the token that corresponds to the start of the
8175 template-id. */
8176 token = cp_lexer_advance_token (parser->lexer,
8177 parser->lexer->first_token,
8178 start_of_id);
8179
8180 /* Reset the contents of the START_OF_ID token. */
8181 token->type = CPP_TEMPLATE_ID;
8182 token->value = build_tree_list (access_check, template_id);
8183 token->keyword = RID_MAX;
8184 /* Purge all subsequent tokens. */
8185 cp_lexer_purge_tokens_after (parser->lexer, token);
8186 }
8187
8188 pop_deferring_access_checks ();
8189 return template_id;
8190 }
8191
8192 /* Parse a template-name.
8193
8194 template-name:
8195 identifier
8196
8197 The standard should actually say:
8198
8199 template-name:
8200 identifier
8201 operator-function-id
8202
8203 A defect report has been filed about this issue.
8204
8205 A conversion-function-id cannot be a template name because they cannot
8206 be part of a template-id. In fact, looking at this code:
8207
8208 a.operator K<int>()
8209
8210 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8211 It is impossible to call a templated conversion-function-id with an
8212 explicit argument list, since the only allowed template parameter is
8213 the type to which it is converting.
8214
8215 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8216 `template' keyword, in a construction like:
8217
8218 T::template f<3>()
8219
8220 In that case `f' is taken to be a template-name, even though there
8221 is no way of knowing for sure.
8222
8223 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8224 name refers to a set of overloaded functions, at least one of which
8225 is a template, or an IDENTIFIER_NODE with the name of the template,
8226 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8227 names are looked up inside uninstantiated templates. */
8228
8229 static tree
8230 cp_parser_template_name (cp_parser* parser,
8231 bool template_keyword_p,
8232 bool check_dependency_p,
8233 bool is_declaration,
8234 bool *is_identifier)
8235 {
8236 tree identifier;
8237 tree decl;
8238 tree fns;
8239
8240 /* If the next token is `operator', then we have either an
8241 operator-function-id or a conversion-function-id. */
8242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8243 {
8244 /* We don't know whether we're looking at an
8245 operator-function-id or a conversion-function-id. */
8246 cp_parser_parse_tentatively (parser);
8247 /* Try an operator-function-id. */
8248 identifier = cp_parser_operator_function_id (parser);
8249 /* If that didn't work, try a conversion-function-id. */
8250 if (!cp_parser_parse_definitely (parser))
8251 {
8252 cp_parser_error (parser, "expected template-name");
8253 return error_mark_node;
8254 }
8255 }
8256 /* Look for the identifier. */
8257 else
8258 identifier = cp_parser_identifier (parser);
8259
8260 /* If we didn't find an identifier, we don't have a template-id. */
8261 if (identifier == error_mark_node)
8262 return error_mark_node;
8263
8264 /* If the name immediately followed the `template' keyword, then it
8265 is a template-name. However, if the next token is not `<', then
8266 we do not treat it as a template-name, since it is not being used
8267 as part of a template-id. This enables us to handle constructs
8268 like:
8269
8270 template <typename T> struct S { S(); };
8271 template <typename T> S<T>::S();
8272
8273 correctly. We would treat `S' as a template -- if it were `S<T>'
8274 -- but we do not if there is no `<'. */
8275
8276 if (processing_template_decl
8277 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8278 {
8279 /* In a declaration, in a dependent context, we pretend that the
8280 "template" keyword was present in order to improve error
8281 recovery. For example, given:
8282
8283 template <typename T> void f(T::X<int>);
8284
8285 we want to treat "X<int>" as a template-id. */
8286 if (is_declaration
8287 && !template_keyword_p
8288 && parser->scope && TYPE_P (parser->scope)
8289 && dependent_type_p (parser->scope))
8290 {
8291 ptrdiff_t start;
8292 cp_token* token;
8293 /* Explain what went wrong. */
8294 error ("non-template `%D' used as template", identifier);
8295 error ("(use `%T::template %D' to indicate that it is a template)",
8296 parser->scope, identifier);
8297 /* If parsing tentatively, find the location of the "<"
8298 token. */
8299 if (cp_parser_parsing_tentatively (parser)
8300 && !cp_parser_committed_to_tentative_parse (parser))
8301 {
8302 cp_parser_simulate_error (parser);
8303 token = cp_lexer_peek_token (parser->lexer);
8304 token = cp_lexer_prev_token (parser->lexer, token);
8305 start = cp_lexer_token_difference (parser->lexer,
8306 parser->lexer->first_token,
8307 token);
8308 }
8309 else
8310 start = -1;
8311 /* Parse the template arguments so that we can issue error
8312 messages about them. */
8313 cp_lexer_consume_token (parser->lexer);
8314 cp_parser_enclosed_template_argument_list (parser);
8315 /* Skip tokens until we find a good place from which to
8316 continue parsing. */
8317 cp_parser_skip_to_closing_parenthesis (parser,
8318 /*recovering=*/true,
8319 /*or_comma=*/true,
8320 /*consume_paren=*/false);
8321 /* If parsing tentatively, permanently remove the
8322 template argument list. That will prevent duplicate
8323 error messages from being issued about the missing
8324 "template" keyword. */
8325 if (start >= 0)
8326 {
8327 token = cp_lexer_advance_token (parser->lexer,
8328 parser->lexer->first_token,
8329 start);
8330 cp_lexer_purge_tokens_after (parser->lexer, token);
8331 }
8332 if (is_identifier)
8333 *is_identifier = true;
8334 return identifier;
8335 }
8336
8337 /* If the "template" keyword is present, then there is generally
8338 no point in doing name-lookup, so we just return IDENTIFIER.
8339 But, if the qualifying scope is non-dependent then we can
8340 (and must) do name-lookup normally. */
8341 if (template_keyword_p
8342 && (!parser->scope
8343 || (TYPE_P (parser->scope)
8344 && dependent_type_p (parser->scope))))
8345 return identifier;
8346 }
8347
8348 /* Look up the name. */
8349 decl = cp_parser_lookup_name (parser, identifier,
8350 /*is_type=*/false,
8351 /*is_template=*/false,
8352 /*is_namespace=*/false,
8353 check_dependency_p);
8354 decl = maybe_get_template_decl_from_type_decl (decl);
8355
8356 /* If DECL is a template, then the name was a template-name. */
8357 if (TREE_CODE (decl) == TEMPLATE_DECL)
8358 ;
8359 else
8360 {
8361 /* The standard does not explicitly indicate whether a name that
8362 names a set of overloaded declarations, some of which are
8363 templates, is a template-name. However, such a name should
8364 be a template-name; otherwise, there is no way to form a
8365 template-id for the overloaded templates. */
8366 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8367 if (TREE_CODE (fns) == OVERLOAD)
8368 {
8369 tree fn;
8370
8371 for (fn = fns; fn; fn = OVL_NEXT (fn))
8372 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8373 break;
8374 }
8375 else
8376 {
8377 /* Otherwise, the name does not name a template. */
8378 cp_parser_error (parser, "expected template-name");
8379 return error_mark_node;
8380 }
8381 }
8382
8383 /* If DECL is dependent, and refers to a function, then just return
8384 its name; we will look it up again during template instantiation. */
8385 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8386 {
8387 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8388 if (TYPE_P (scope) && dependent_type_p (scope))
8389 return identifier;
8390 }
8391
8392 return decl;
8393 }
8394
8395 /* Parse a template-argument-list.
8396
8397 template-argument-list:
8398 template-argument
8399 template-argument-list , template-argument
8400
8401 Returns a TREE_VEC containing the arguments. */
8402
8403 static tree
8404 cp_parser_template_argument_list (cp_parser* parser)
8405 {
8406 tree fixed_args[10];
8407 unsigned n_args = 0;
8408 unsigned alloced = 10;
8409 tree *arg_ary = fixed_args;
8410 tree vec;
8411 bool saved_in_template_argument_list_p;
8412
8413 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8414 parser->in_template_argument_list_p = true;
8415 do
8416 {
8417 tree argument;
8418
8419 if (n_args)
8420 /* Consume the comma. */
8421 cp_lexer_consume_token (parser->lexer);
8422
8423 /* Parse the template-argument. */
8424 argument = cp_parser_template_argument (parser);
8425 if (n_args == alloced)
8426 {
8427 alloced *= 2;
8428
8429 if (arg_ary == fixed_args)
8430 {
8431 arg_ary = xmalloc (sizeof (tree) * alloced);
8432 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8433 }
8434 else
8435 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8436 }
8437 arg_ary[n_args++] = argument;
8438 }
8439 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8440
8441 vec = make_tree_vec (n_args);
8442
8443 while (n_args--)
8444 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8445
8446 if (arg_ary != fixed_args)
8447 free (arg_ary);
8448 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8449 return vec;
8450 }
8451
8452 /* Parse a template-argument.
8453
8454 template-argument:
8455 assignment-expression
8456 type-id
8457 id-expression
8458
8459 The representation is that of an assignment-expression, type-id, or
8460 id-expression -- except that the qualified id-expression is
8461 evaluated, so that the value returned is either a DECL or an
8462 OVERLOAD.
8463
8464 Although the standard says "assignment-expression", it forbids
8465 throw-expressions or assignments in the template argument.
8466 Therefore, we use "conditional-expression" instead. */
8467
8468 static tree
8469 cp_parser_template_argument (cp_parser* parser)
8470 {
8471 tree argument;
8472 bool template_p;
8473 bool address_p;
8474 bool maybe_type_id = false;
8475 cp_token *token;
8476 cp_id_kind idk;
8477 tree qualifying_class;
8478
8479 /* There's really no way to know what we're looking at, so we just
8480 try each alternative in order.
8481
8482 [temp.arg]
8483
8484 In a template-argument, an ambiguity between a type-id and an
8485 expression is resolved to a type-id, regardless of the form of
8486 the corresponding template-parameter.
8487
8488 Therefore, we try a type-id first. */
8489 cp_parser_parse_tentatively (parser);
8490 argument = cp_parser_type_id (parser);
8491 /* If there was no error parsing the type-id but the next token is a '>>',
8492 we probably found a typo for '> >'. But there are type-id which are
8493 also valid expressions. For instance:
8494
8495 struct X { int operator >> (int); };
8496 template <int V> struct Foo {};
8497 Foo<X () >> 5> r;
8498
8499 Here 'X()' is a valid type-id of a function type, but the user just
8500 wanted to write the expression "X() >> 5". Thus, we remember that we
8501 found a valid type-id, but we still try to parse the argument as an
8502 expression to see what happens. */
8503 if (!cp_parser_error_occurred (parser)
8504 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8505 {
8506 maybe_type_id = true;
8507 cp_parser_abort_tentative_parse (parser);
8508 }
8509 else
8510 {
8511 /* If the next token isn't a `,' or a `>', then this argument wasn't
8512 really finished. This means that the argument is not a valid
8513 type-id. */
8514 if (!cp_parser_next_token_ends_template_argument_p (parser))
8515 cp_parser_error (parser, "expected template-argument");
8516 /* If that worked, we're done. */
8517 if (cp_parser_parse_definitely (parser))
8518 return argument;
8519 }
8520 /* We're still not sure what the argument will be. */
8521 cp_parser_parse_tentatively (parser);
8522 /* Try a template. */
8523 argument = cp_parser_id_expression (parser,
8524 /*template_keyword_p=*/false,
8525 /*check_dependency_p=*/true,
8526 &template_p,
8527 /*declarator_p=*/false);
8528 /* If the next token isn't a `,' or a `>', then this argument wasn't
8529 really finished. */
8530 if (!cp_parser_next_token_ends_template_argument_p (parser))
8531 cp_parser_error (parser, "expected template-argument");
8532 if (!cp_parser_error_occurred (parser))
8533 {
8534 /* Figure out what is being referred to. If the id-expression
8535 was for a class template specialization, then we will have a
8536 TYPE_DECL at this point. There is no need to do name lookup
8537 at this point in that case. */
8538 if (TREE_CODE (argument) != TYPE_DECL)
8539 argument = cp_parser_lookup_name (parser, argument,
8540 /*is_type=*/false,
8541 /*is_template=*/template_p,
8542 /*is_namespace=*/false,
8543 /*check_dependency=*/true);
8544 if (TREE_CODE (argument) != TEMPLATE_DECL
8545 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8546 cp_parser_error (parser, "expected template-name");
8547 }
8548 if (cp_parser_parse_definitely (parser))
8549 return argument;
8550 /* It must be a non-type argument. There permitted cases are given
8551 in [temp.arg.nontype]:
8552
8553 -- an integral constant-expression of integral or enumeration
8554 type; or
8555
8556 -- the name of a non-type template-parameter; or
8557
8558 -- the name of an object or function with external linkage...
8559
8560 -- the address of an object or function with external linkage...
8561
8562 -- a pointer to member... */
8563 /* Look for a non-type template parameter. */
8564 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8565 {
8566 cp_parser_parse_tentatively (parser);
8567 argument = cp_parser_primary_expression (parser,
8568 &idk,
8569 &qualifying_class);
8570 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8571 || !cp_parser_next_token_ends_template_argument_p (parser))
8572 cp_parser_simulate_error (parser);
8573 if (cp_parser_parse_definitely (parser))
8574 return argument;
8575 }
8576 /* If the next token is "&", the argument must be the address of an
8577 object or function with external linkage. */
8578 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8579 if (address_p)
8580 cp_lexer_consume_token (parser->lexer);
8581 /* See if we might have an id-expression. */
8582 token = cp_lexer_peek_token (parser->lexer);
8583 if (token->type == CPP_NAME
8584 || token->keyword == RID_OPERATOR
8585 || token->type == CPP_SCOPE
8586 || token->type == CPP_TEMPLATE_ID
8587 || token->type == CPP_NESTED_NAME_SPECIFIER)
8588 {
8589 cp_parser_parse_tentatively (parser);
8590 argument = cp_parser_primary_expression (parser,
8591 &idk,
8592 &qualifying_class);
8593 if (cp_parser_error_occurred (parser)
8594 || !cp_parser_next_token_ends_template_argument_p (parser))
8595 cp_parser_abort_tentative_parse (parser);
8596 else
8597 {
8598 if (qualifying_class)
8599 argument = finish_qualified_id_expr (qualifying_class,
8600 argument,
8601 /*done=*/true,
8602 address_p);
8603 if (TREE_CODE (argument) == VAR_DECL)
8604 {
8605 /* A variable without external linkage might still be a
8606 valid constant-expression, so no error is issued here
8607 if the external-linkage check fails. */
8608 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8609 cp_parser_simulate_error (parser);
8610 }
8611 else if (is_overloaded_fn (argument))
8612 /* All overloaded functions are allowed; if the external
8613 linkage test does not pass, an error will be issued
8614 later. */
8615 ;
8616 else if (address_p
8617 && (TREE_CODE (argument) == OFFSET_REF
8618 || TREE_CODE (argument) == SCOPE_REF))
8619 /* A pointer-to-member. */
8620 ;
8621 else
8622 cp_parser_simulate_error (parser);
8623
8624 if (cp_parser_parse_definitely (parser))
8625 {
8626 if (address_p)
8627 argument = build_x_unary_op (ADDR_EXPR, argument);
8628 return argument;
8629 }
8630 }
8631 }
8632 /* If the argument started with "&", there are no other valid
8633 alternatives at this point. */
8634 if (address_p)
8635 {
8636 cp_parser_error (parser, "invalid non-type template argument");
8637 return error_mark_node;
8638 }
8639 /* If the argument wasn't successfully parsed as a type-id followed
8640 by '>>', the argument can only be a constant expression now.
8641 Otherwise, we try parsing the constant-expression tentatively,
8642 because the argument could really be a type-id. */
8643 if (maybe_type_id)
8644 cp_parser_parse_tentatively (parser);
8645 argument = cp_parser_constant_expression (parser,
8646 /*allow_non_constant_p=*/false,
8647 /*non_constant_p=*/NULL);
8648 argument = fold_non_dependent_expr (argument);
8649 if (!maybe_type_id)
8650 return argument;
8651 if (!cp_parser_next_token_ends_template_argument_p (parser))
8652 cp_parser_error (parser, "expected template-argument");
8653 if (cp_parser_parse_definitely (parser))
8654 return argument;
8655 /* We did our best to parse the argument as a non type-id, but that
8656 was the only alternative that matched (albeit with a '>' after
8657 it). We can assume it's just a typo from the user, and a
8658 diagnostic will then be issued. */
8659 return cp_parser_type_id (parser);
8660 }
8661
8662 /* Parse an explicit-instantiation.
8663
8664 explicit-instantiation:
8665 template declaration
8666
8667 Although the standard says `declaration', what it really means is:
8668
8669 explicit-instantiation:
8670 template decl-specifier-seq [opt] declarator [opt] ;
8671
8672 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8673 supposed to be allowed. A defect report has been filed about this
8674 issue.
8675
8676 GNU Extension:
8677
8678 explicit-instantiation:
8679 storage-class-specifier template
8680 decl-specifier-seq [opt] declarator [opt] ;
8681 function-specifier template
8682 decl-specifier-seq [opt] declarator [opt] ; */
8683
8684 static void
8685 cp_parser_explicit_instantiation (cp_parser* parser)
8686 {
8687 int declares_class_or_enum;
8688 tree decl_specifiers;
8689 tree attributes;
8690 tree extension_specifier = NULL_TREE;
8691
8692 /* Look for an (optional) storage-class-specifier or
8693 function-specifier. */
8694 if (cp_parser_allow_gnu_extensions_p (parser))
8695 {
8696 extension_specifier
8697 = cp_parser_storage_class_specifier_opt (parser);
8698 if (!extension_specifier)
8699 extension_specifier = cp_parser_function_specifier_opt (parser);
8700 }
8701
8702 /* Look for the `template' keyword. */
8703 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8704 /* Let the front end know that we are processing an explicit
8705 instantiation. */
8706 begin_explicit_instantiation ();
8707 /* [temp.explicit] says that we are supposed to ignore access
8708 control while processing explicit instantiation directives. */
8709 push_deferring_access_checks (dk_no_check);
8710 /* Parse a decl-specifier-seq. */
8711 decl_specifiers
8712 = cp_parser_decl_specifier_seq (parser,
8713 CP_PARSER_FLAGS_OPTIONAL,
8714 &attributes,
8715 &declares_class_or_enum);
8716 /* If there was exactly one decl-specifier, and it declared a class,
8717 and there's no declarator, then we have an explicit type
8718 instantiation. */
8719 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8720 {
8721 tree type;
8722
8723 type = check_tag_decl (decl_specifiers);
8724 /* Turn access control back on for names used during
8725 template instantiation. */
8726 pop_deferring_access_checks ();
8727 if (type)
8728 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8729 }
8730 else
8731 {
8732 tree declarator;
8733 tree decl;
8734
8735 /* Parse the declarator. */
8736 declarator
8737 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8738 /*ctor_dtor_or_conv_p=*/NULL,
8739 /*parenthesized_p=*/NULL);
8740 cp_parser_check_for_definition_in_return_type (declarator,
8741 declares_class_or_enum);
8742 if (declarator != error_mark_node)
8743 {
8744 decl = grokdeclarator (declarator, decl_specifiers,
8745 NORMAL, 0, NULL);
8746 /* Turn access control back on for names used during
8747 template instantiation. */
8748 pop_deferring_access_checks ();
8749 /* Do the explicit instantiation. */
8750 do_decl_instantiation (decl, extension_specifier);
8751 }
8752 else
8753 {
8754 pop_deferring_access_checks ();
8755 /* Skip the body of the explicit instantiation. */
8756 cp_parser_skip_to_end_of_statement (parser);
8757 }
8758 }
8759 /* We're done with the instantiation. */
8760 end_explicit_instantiation ();
8761
8762 cp_parser_consume_semicolon_at_end_of_statement (parser);
8763 }
8764
8765 /* Parse an explicit-specialization.
8766
8767 explicit-specialization:
8768 template < > declaration
8769
8770 Although the standard says `declaration', what it really means is:
8771
8772 explicit-specialization:
8773 template <> decl-specifier [opt] init-declarator [opt] ;
8774 template <> function-definition
8775 template <> explicit-specialization
8776 template <> template-declaration */
8777
8778 static void
8779 cp_parser_explicit_specialization (cp_parser* parser)
8780 {
8781 /* Look for the `template' keyword. */
8782 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8783 /* Look for the `<'. */
8784 cp_parser_require (parser, CPP_LESS, "`<'");
8785 /* Look for the `>'. */
8786 cp_parser_require (parser, CPP_GREATER, "`>'");
8787 /* We have processed another parameter list. */
8788 ++parser->num_template_parameter_lists;
8789 /* Let the front end know that we are beginning a specialization. */
8790 begin_specialization ();
8791
8792 /* If the next keyword is `template', we need to figure out whether
8793 or not we're looking a template-declaration. */
8794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8795 {
8796 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8797 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8798 cp_parser_template_declaration_after_export (parser,
8799 /*member_p=*/false);
8800 else
8801 cp_parser_explicit_specialization (parser);
8802 }
8803 else
8804 /* Parse the dependent declaration. */
8805 cp_parser_single_declaration (parser,
8806 /*member_p=*/false,
8807 /*friend_p=*/NULL);
8808
8809 /* We're done with the specialization. */
8810 end_specialization ();
8811 /* We're done with this parameter list. */
8812 --parser->num_template_parameter_lists;
8813 }
8814
8815 /* Parse a type-specifier.
8816
8817 type-specifier:
8818 simple-type-specifier
8819 class-specifier
8820 enum-specifier
8821 elaborated-type-specifier
8822 cv-qualifier
8823
8824 GNU Extension:
8825
8826 type-specifier:
8827 __complex__
8828
8829 Returns a representation of the type-specifier. If the
8830 type-specifier is a keyword (like `int' or `const', or
8831 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8832 For a class-specifier, enum-specifier, or elaborated-type-specifier
8833 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8834
8835 If IS_FRIEND is TRUE then this type-specifier is being declared a
8836 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8837 appearing in a decl-specifier-seq.
8838
8839 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8840 class-specifier, enum-specifier, or elaborated-type-specifier, then
8841 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8842 if a type is declared; 2 if it is defined. Otherwise, it is set to
8843 zero.
8844
8845 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8846 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8847 is set to FALSE. */
8848
8849 static tree
8850 cp_parser_type_specifier (cp_parser* parser,
8851 cp_parser_flags flags,
8852 bool is_friend,
8853 bool is_declaration,
8854 int* declares_class_or_enum,
8855 bool* is_cv_qualifier)
8856 {
8857 tree type_spec = NULL_TREE;
8858 cp_token *token;
8859 enum rid keyword;
8860
8861 /* Assume this type-specifier does not declare a new type. */
8862 if (declares_class_or_enum)
8863 *declares_class_or_enum = 0;
8864 /* And that it does not specify a cv-qualifier. */
8865 if (is_cv_qualifier)
8866 *is_cv_qualifier = false;
8867 /* Peek at the next token. */
8868 token = cp_lexer_peek_token (parser->lexer);
8869
8870 /* If we're looking at a keyword, we can use that to guide the
8871 production we choose. */
8872 keyword = token->keyword;
8873 switch (keyword)
8874 {
8875 /* Any of these indicate either a class-specifier, or an
8876 elaborated-type-specifier. */
8877 case RID_CLASS:
8878 case RID_STRUCT:
8879 case RID_UNION:
8880 case RID_ENUM:
8881 /* Parse tentatively so that we can back up if we don't find a
8882 class-specifier or enum-specifier. */
8883 cp_parser_parse_tentatively (parser);
8884 /* Look for the class-specifier or enum-specifier. */
8885 if (keyword == RID_ENUM)
8886 type_spec = cp_parser_enum_specifier (parser);
8887 else
8888 type_spec = cp_parser_class_specifier (parser);
8889
8890 /* If that worked, we're done. */
8891 if (cp_parser_parse_definitely (parser))
8892 {
8893 if (declares_class_or_enum)
8894 *declares_class_or_enum = 2;
8895 return type_spec;
8896 }
8897
8898 /* Fall through. */
8899
8900 case RID_TYPENAME:
8901 /* Look for an elaborated-type-specifier. */
8902 type_spec = cp_parser_elaborated_type_specifier (parser,
8903 is_friend,
8904 is_declaration);
8905 /* We're declaring a class or enum -- unless we're using
8906 `typename'. */
8907 if (declares_class_or_enum && keyword != RID_TYPENAME)
8908 *declares_class_or_enum = 1;
8909 return type_spec;
8910
8911 case RID_CONST:
8912 case RID_VOLATILE:
8913 case RID_RESTRICT:
8914 type_spec = cp_parser_cv_qualifier_opt (parser);
8915 /* Even though we call a routine that looks for an optional
8916 qualifier, we know that there should be one. */
8917 my_friendly_assert (type_spec != NULL, 20000328);
8918 /* This type-specifier was a cv-qualified. */
8919 if (is_cv_qualifier)
8920 *is_cv_qualifier = true;
8921
8922 return type_spec;
8923
8924 case RID_COMPLEX:
8925 /* The `__complex__' keyword is a GNU extension. */
8926 return cp_lexer_consume_token (parser->lexer)->value;
8927
8928 default:
8929 break;
8930 }
8931
8932 /* If we do not already have a type-specifier, assume we are looking
8933 at a simple-type-specifier. */
8934 type_spec = cp_parser_simple_type_specifier (parser, flags,
8935 /*identifier_p=*/true);
8936
8937 /* If we didn't find a type-specifier, and a type-specifier was not
8938 optional in this context, issue an error message. */
8939 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8940 {
8941 cp_parser_error (parser, "expected type specifier");
8942 return error_mark_node;
8943 }
8944
8945 return type_spec;
8946 }
8947
8948 /* Parse a simple-type-specifier.
8949
8950 simple-type-specifier:
8951 :: [opt] nested-name-specifier [opt] type-name
8952 :: [opt] nested-name-specifier template template-id
8953 char
8954 wchar_t
8955 bool
8956 short
8957 int
8958 long
8959 signed
8960 unsigned
8961 float
8962 double
8963 void
8964
8965 GNU Extension:
8966
8967 simple-type-specifier:
8968 __typeof__ unary-expression
8969 __typeof__ ( type-id )
8970
8971 For the various keywords, the value returned is simply the
8972 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8973 For the first two productions, and if IDENTIFIER_P is false, the
8974 value returned is the indicated TYPE_DECL. */
8975
8976 static tree
8977 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8978 bool identifier_p)
8979 {
8980 tree type = NULL_TREE;
8981 cp_token *token;
8982
8983 /* Peek at the next token. */
8984 token = cp_lexer_peek_token (parser->lexer);
8985
8986 /* If we're looking at a keyword, things are easy. */
8987 switch (token->keyword)
8988 {
8989 case RID_CHAR:
8990 type = char_type_node;
8991 break;
8992 case RID_WCHAR:
8993 type = wchar_type_node;
8994 break;
8995 case RID_BOOL:
8996 type = boolean_type_node;
8997 break;
8998 case RID_SHORT:
8999 type = short_integer_type_node;
9000 break;
9001 case RID_INT:
9002 type = integer_type_node;
9003 break;
9004 case RID_LONG:
9005 type = long_integer_type_node;
9006 break;
9007 case RID_SIGNED:
9008 type = integer_type_node;
9009 break;
9010 case RID_UNSIGNED:
9011 type = unsigned_type_node;
9012 break;
9013 case RID_FLOAT:
9014 type = float_type_node;
9015 break;
9016 case RID_DOUBLE:
9017 type = double_type_node;
9018 break;
9019 case RID_VOID:
9020 type = void_type_node;
9021 break;
9022
9023 case RID_TYPEOF:
9024 {
9025 tree operand;
9026
9027 /* Consume the `typeof' token. */
9028 cp_lexer_consume_token (parser->lexer);
9029 /* Parse the operand to `typeof'. */
9030 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9031 /* If it is not already a TYPE, take its type. */
9032 if (!TYPE_P (operand))
9033 operand = finish_typeof (operand);
9034
9035 return operand;
9036 }
9037
9038 default:
9039 break;
9040 }
9041
9042 /* If the type-specifier was for a built-in type, we're done. */
9043 if (type)
9044 {
9045 tree id;
9046
9047 /* Consume the token. */
9048 id = cp_lexer_consume_token (parser->lexer)->value;
9049
9050 /* There is no valid C++ program where a non-template type is
9051 followed by a "<". That usually indicates that the user thought
9052 that the type was a template. */
9053 cp_parser_check_for_invalid_template_id (parser, type);
9054
9055 return identifier_p ? id : TYPE_NAME (type);
9056 }
9057
9058 /* The type-specifier must be a user-defined type. */
9059 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9060 {
9061 bool qualified_p;
9062
9063 /* Don't gobble tokens or issue error messages if this is an
9064 optional type-specifier. */
9065 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9066 cp_parser_parse_tentatively (parser);
9067
9068 /* Look for the optional `::' operator. */
9069 cp_parser_global_scope_opt (parser,
9070 /*current_scope_valid_p=*/false);
9071 /* Look for the nested-name specifier. */
9072 qualified_p
9073 = (cp_parser_nested_name_specifier_opt (parser,
9074 /*typename_keyword_p=*/false,
9075 /*check_dependency_p=*/true,
9076 /*type_p=*/false,
9077 /*is_declaration=*/false)
9078 != NULL_TREE);
9079 /* If we have seen a nested-name-specifier, and the next token
9080 is `template', then we are using the template-id production. */
9081 if (parser->scope
9082 && cp_parser_optional_template_keyword (parser))
9083 {
9084 /* Look for the template-id. */
9085 type = cp_parser_template_id (parser,
9086 /*template_keyword_p=*/true,
9087 /*check_dependency_p=*/true,
9088 /*is_declaration=*/false);
9089 /* If the template-id did not name a type, we are out of
9090 luck. */
9091 if (TREE_CODE (type) != TYPE_DECL)
9092 {
9093 cp_parser_error (parser, "expected template-id for type");
9094 type = NULL_TREE;
9095 }
9096 }
9097 /* Otherwise, look for a type-name. */
9098 else
9099 type = cp_parser_type_name (parser);
9100 /* Keep track of all name-lookups performed in class scopes. */
9101 if (type
9102 && !qualified_p
9103 && TREE_CODE (type) == TYPE_DECL
9104 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9105 maybe_note_name_used_in_class (DECL_NAME (type), type);
9106 /* If it didn't work out, we don't have a TYPE. */
9107 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9108 && !cp_parser_parse_definitely (parser))
9109 type = NULL_TREE;
9110 }
9111
9112 /* If we didn't get a type-name, issue an error message. */
9113 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9114 {
9115 cp_parser_error (parser, "expected type-name");
9116 return error_mark_node;
9117 }
9118
9119 /* There is no valid C++ program where a non-template type is
9120 followed by a "<". That usually indicates that the user thought
9121 that the type was a template. */
9122 if (type && type != error_mark_node)
9123 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9124
9125 return type;
9126 }
9127
9128 /* Parse a type-name.
9129
9130 type-name:
9131 class-name
9132 enum-name
9133 typedef-name
9134
9135 enum-name:
9136 identifier
9137
9138 typedef-name:
9139 identifier
9140
9141 Returns a TYPE_DECL for the the type. */
9142
9143 static tree
9144 cp_parser_type_name (cp_parser* parser)
9145 {
9146 tree type_decl;
9147 tree identifier;
9148
9149 /* We can't know yet whether it is a class-name or not. */
9150 cp_parser_parse_tentatively (parser);
9151 /* Try a class-name. */
9152 type_decl = cp_parser_class_name (parser,
9153 /*typename_keyword_p=*/false,
9154 /*template_keyword_p=*/false,
9155 /*type_p=*/false,
9156 /*check_dependency_p=*/true,
9157 /*class_head_p=*/false,
9158 /*is_declaration=*/false);
9159 /* If it's not a class-name, keep looking. */
9160 if (!cp_parser_parse_definitely (parser))
9161 {
9162 /* It must be a typedef-name or an enum-name. */
9163 identifier = cp_parser_identifier (parser);
9164 if (identifier == error_mark_node)
9165 return error_mark_node;
9166
9167 /* Look up the type-name. */
9168 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9169 /* Issue an error if we did not find a type-name. */
9170 if (TREE_CODE (type_decl) != TYPE_DECL)
9171 {
9172 if (!cp_parser_simulate_error (parser))
9173 cp_parser_name_lookup_error (parser, identifier, type_decl,
9174 "is not a type");
9175 type_decl = error_mark_node;
9176 }
9177 /* Remember that the name was used in the definition of the
9178 current class so that we can check later to see if the
9179 meaning would have been different after the class was
9180 entirely defined. */
9181 else if (type_decl != error_mark_node
9182 && !parser->scope)
9183 maybe_note_name_used_in_class (identifier, type_decl);
9184 }
9185
9186 return type_decl;
9187 }
9188
9189
9190 /* Parse an elaborated-type-specifier. Note that the grammar given
9191 here incorporates the resolution to DR68.
9192
9193 elaborated-type-specifier:
9194 class-key :: [opt] nested-name-specifier [opt] identifier
9195 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9196 enum :: [opt] nested-name-specifier [opt] identifier
9197 typename :: [opt] nested-name-specifier identifier
9198 typename :: [opt] nested-name-specifier template [opt]
9199 template-id
9200
9201 GNU extension:
9202
9203 elaborated-type-specifier:
9204 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9205 class-key attributes :: [opt] nested-name-specifier [opt]
9206 template [opt] template-id
9207 enum attributes :: [opt] nested-name-specifier [opt] identifier
9208
9209 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9210 declared `friend'. If IS_DECLARATION is TRUE, then this
9211 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9212 something is being declared.
9213
9214 Returns the TYPE specified. */
9215
9216 static tree
9217 cp_parser_elaborated_type_specifier (cp_parser* parser,
9218 bool is_friend,
9219 bool is_declaration)
9220 {
9221 enum tag_types tag_type;
9222 tree identifier;
9223 tree type = NULL_TREE;
9224 tree attributes = NULL_TREE;
9225
9226 /* See if we're looking at the `enum' keyword. */
9227 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9228 {
9229 /* Consume the `enum' token. */
9230 cp_lexer_consume_token (parser->lexer);
9231 /* Remember that it's an enumeration type. */
9232 tag_type = enum_type;
9233 /* Parse the attributes. */
9234 attributes = cp_parser_attributes_opt (parser);
9235 }
9236 /* Or, it might be `typename'. */
9237 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9238 RID_TYPENAME))
9239 {
9240 /* Consume the `typename' token. */
9241 cp_lexer_consume_token (parser->lexer);
9242 /* Remember that it's a `typename' type. */
9243 tag_type = typename_type;
9244 /* The `typename' keyword is only allowed in templates. */
9245 if (!processing_template_decl)
9246 pedwarn ("using `typename' outside of template");
9247 }
9248 /* Otherwise it must be a class-key. */
9249 else
9250 {
9251 tag_type = cp_parser_class_key (parser);
9252 if (tag_type == none_type)
9253 return error_mark_node;
9254 /* Parse the attributes. */
9255 attributes = cp_parser_attributes_opt (parser);
9256 }
9257
9258 /* Look for the `::' operator. */
9259 cp_parser_global_scope_opt (parser,
9260 /*current_scope_valid_p=*/false);
9261 /* Look for the nested-name-specifier. */
9262 if (tag_type == typename_type)
9263 {
9264 if (cp_parser_nested_name_specifier (parser,
9265 /*typename_keyword_p=*/true,
9266 /*check_dependency_p=*/true,
9267 /*type_p=*/true,
9268 is_declaration)
9269 == error_mark_node)
9270 return error_mark_node;
9271 }
9272 else
9273 /* Even though `typename' is not present, the proposed resolution
9274 to Core Issue 180 says that in `class A<T>::B', `B' should be
9275 considered a type-name, even if `A<T>' is dependent. */
9276 cp_parser_nested_name_specifier_opt (parser,
9277 /*typename_keyword_p=*/true,
9278 /*check_dependency_p=*/true,
9279 /*type_p=*/true,
9280 is_declaration);
9281 /* For everything but enumeration types, consider a template-id. */
9282 if (tag_type != enum_type)
9283 {
9284 bool template_p = false;
9285 tree decl;
9286
9287 /* Allow the `template' keyword. */
9288 template_p = cp_parser_optional_template_keyword (parser);
9289 /* If we didn't see `template', we don't know if there's a
9290 template-id or not. */
9291 if (!template_p)
9292 cp_parser_parse_tentatively (parser);
9293 /* Parse the template-id. */
9294 decl = cp_parser_template_id (parser, template_p,
9295 /*check_dependency_p=*/true,
9296 is_declaration);
9297 /* If we didn't find a template-id, look for an ordinary
9298 identifier. */
9299 if (!template_p && !cp_parser_parse_definitely (parser))
9300 ;
9301 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9302 in effect, then we must assume that, upon instantiation, the
9303 template will correspond to a class. */
9304 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9305 && tag_type == typename_type)
9306 type = make_typename_type (parser->scope, decl,
9307 /*complain=*/1);
9308 else
9309 type = TREE_TYPE (decl);
9310 }
9311
9312 /* For an enumeration type, consider only a plain identifier. */
9313 if (!type)
9314 {
9315 identifier = cp_parser_identifier (parser);
9316
9317 if (identifier == error_mark_node)
9318 {
9319 parser->scope = NULL_TREE;
9320 return error_mark_node;
9321 }
9322
9323 /* For a `typename', we needn't call xref_tag. */
9324 if (tag_type == typename_type)
9325 return cp_parser_make_typename_type (parser, parser->scope,
9326 identifier);
9327 /* Look up a qualified name in the usual way. */
9328 if (parser->scope)
9329 {
9330 tree decl;
9331
9332 /* In an elaborated-type-specifier, names are assumed to name
9333 types, so we set IS_TYPE to TRUE when calling
9334 cp_parser_lookup_name. */
9335 decl = cp_parser_lookup_name (parser, identifier,
9336 /*is_type=*/true,
9337 /*is_template=*/false,
9338 /*is_namespace=*/false,
9339 /*check_dependency=*/true);
9340
9341 /* If we are parsing friend declaration, DECL may be a
9342 TEMPLATE_DECL tree node here. However, we need to check
9343 whether this TEMPLATE_DECL results in valid code. Consider
9344 the following example:
9345
9346 namespace N {
9347 template <class T> class C {};
9348 }
9349 class X {
9350 template <class T> friend class N::C; // #1, valid code
9351 };
9352 template <class T> class Y {
9353 friend class N::C; // #2, invalid code
9354 };
9355
9356 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9357 name lookup of `N::C'. We see that friend declaration must
9358 be template for the code to be valid. Note that
9359 processing_template_decl does not work here since it is
9360 always 1 for the above two cases. */
9361
9362 decl = (cp_parser_maybe_treat_template_as_class
9363 (decl, /*tag_name_p=*/is_friend
9364 && parser->num_template_parameter_lists));
9365
9366 if (TREE_CODE (decl) != TYPE_DECL)
9367 {
9368 error ("expected type-name");
9369 return error_mark_node;
9370 }
9371
9372 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9373 check_elaborated_type_specifier
9374 (tag_type, decl,
9375 (parser->num_template_parameter_lists
9376 || DECL_SELF_REFERENCE_P (decl)));
9377
9378 type = TREE_TYPE (decl);
9379 }
9380 else
9381 {
9382 /* An elaborated-type-specifier sometimes introduces a new type and
9383 sometimes names an existing type. Normally, the rule is that it
9384 introduces a new type only if there is not an existing type of
9385 the same name already in scope. For example, given:
9386
9387 struct S {};
9388 void f() { struct S s; }
9389
9390 the `struct S' in the body of `f' is the same `struct S' as in
9391 the global scope; the existing definition is used. However, if
9392 there were no global declaration, this would introduce a new
9393 local class named `S'.
9394
9395 An exception to this rule applies to the following code:
9396
9397 namespace N { struct S; }
9398
9399 Here, the elaborated-type-specifier names a new type
9400 unconditionally; even if there is already an `S' in the
9401 containing scope this declaration names a new type.
9402 This exception only applies if the elaborated-type-specifier
9403 forms the complete declaration:
9404
9405 [class.name]
9406
9407 A declaration consisting solely of `class-key identifier ;' is
9408 either a redeclaration of the name in the current scope or a
9409 forward declaration of the identifier as a class name. It
9410 introduces the name into the current scope.
9411
9412 We are in this situation precisely when the next token is a `;'.
9413
9414 An exception to the exception is that a `friend' declaration does
9415 *not* name a new type; i.e., given:
9416
9417 struct S { friend struct T; };
9418
9419 `T' is not a new type in the scope of `S'.
9420
9421 Also, `new struct S' or `sizeof (struct S)' never results in the
9422 definition of a new type; a new type can only be declared in a
9423 declaration context. */
9424
9425 /* Warn about attributes. They are ignored. */
9426 if (attributes)
9427 warning ("type attributes are honored only at type definition");
9428
9429 type = xref_tag (tag_type, identifier,
9430 (is_friend
9431 || !is_declaration
9432 || cp_lexer_next_token_is_not (parser->lexer,
9433 CPP_SEMICOLON)),
9434 parser->num_template_parameter_lists);
9435 }
9436 }
9437 if (tag_type != enum_type)
9438 cp_parser_check_class_key (tag_type, type);
9439
9440 /* A "<" cannot follow an elaborated type specifier. If that
9441 happens, the user was probably trying to form a template-id. */
9442 cp_parser_check_for_invalid_template_id (parser, type);
9443
9444 return type;
9445 }
9446
9447 /* Parse an enum-specifier.
9448
9449 enum-specifier:
9450 enum identifier [opt] { enumerator-list [opt] }
9451
9452 Returns an ENUM_TYPE representing the enumeration. */
9453
9454 static tree
9455 cp_parser_enum_specifier (cp_parser* parser)
9456 {
9457 cp_token *token;
9458 tree identifier = NULL_TREE;
9459 tree type;
9460
9461 /* Look for the `enum' keyword. */
9462 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9463 return error_mark_node;
9464 /* Peek at the next token. */
9465 token = cp_lexer_peek_token (parser->lexer);
9466
9467 /* See if it is an identifier. */
9468 if (token->type == CPP_NAME)
9469 identifier = cp_parser_identifier (parser);
9470
9471 /* Look for the `{'. */
9472 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9473 return error_mark_node;
9474
9475 /* At this point, we're going ahead with the enum-specifier, even
9476 if some other problem occurs. */
9477 cp_parser_commit_to_tentative_parse (parser);
9478
9479 /* Issue an error message if type-definitions are forbidden here. */
9480 cp_parser_check_type_definition (parser);
9481
9482 /* Create the new type. */
9483 type = start_enum (identifier ? identifier : make_anon_name ());
9484
9485 /* Peek at the next token. */
9486 token = cp_lexer_peek_token (parser->lexer);
9487 /* If it's not a `}', then there are some enumerators. */
9488 if (token->type != CPP_CLOSE_BRACE)
9489 cp_parser_enumerator_list (parser, type);
9490 /* Look for the `}'. */
9491 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9492
9493 /* Finish up the enumeration. */
9494 finish_enum (type);
9495
9496 return type;
9497 }
9498
9499 /* Parse an enumerator-list. The enumerators all have the indicated
9500 TYPE.
9501
9502 enumerator-list:
9503 enumerator-definition
9504 enumerator-list , enumerator-definition */
9505
9506 static void
9507 cp_parser_enumerator_list (cp_parser* parser, tree type)
9508 {
9509 while (true)
9510 {
9511 cp_token *token;
9512
9513 /* Parse an enumerator-definition. */
9514 cp_parser_enumerator_definition (parser, type);
9515 /* Peek at the next token. */
9516 token = cp_lexer_peek_token (parser->lexer);
9517 /* If it's not a `,', then we've reached the end of the
9518 list. */
9519 if (token->type != CPP_COMMA)
9520 break;
9521 /* Otherwise, consume the `,' and keep going. */
9522 cp_lexer_consume_token (parser->lexer);
9523 /* If the next token is a `}', there is a trailing comma. */
9524 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9525 {
9526 if (pedantic && !in_system_header)
9527 pedwarn ("comma at end of enumerator list");
9528 break;
9529 }
9530 }
9531 }
9532
9533 /* Parse an enumerator-definition. The enumerator has the indicated
9534 TYPE.
9535
9536 enumerator-definition:
9537 enumerator
9538 enumerator = constant-expression
9539
9540 enumerator:
9541 identifier */
9542
9543 static void
9544 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9545 {
9546 cp_token *token;
9547 tree identifier;
9548 tree value;
9549
9550 /* Look for the identifier. */
9551 identifier = cp_parser_identifier (parser);
9552 if (identifier == error_mark_node)
9553 return;
9554
9555 /* Peek at the next token. */
9556 token = cp_lexer_peek_token (parser->lexer);
9557 /* If it's an `=', then there's an explicit value. */
9558 if (token->type == CPP_EQ)
9559 {
9560 /* Consume the `=' token. */
9561 cp_lexer_consume_token (parser->lexer);
9562 /* Parse the value. */
9563 value = cp_parser_constant_expression (parser,
9564 /*allow_non_constant_p=*/false,
9565 NULL);
9566 }
9567 else
9568 value = NULL_TREE;
9569
9570 /* Create the enumerator. */
9571 build_enumerator (identifier, value, type);
9572 }
9573
9574 /* Parse a namespace-name.
9575
9576 namespace-name:
9577 original-namespace-name
9578 namespace-alias
9579
9580 Returns the NAMESPACE_DECL for the namespace. */
9581
9582 static tree
9583 cp_parser_namespace_name (cp_parser* parser)
9584 {
9585 tree identifier;
9586 tree namespace_decl;
9587
9588 /* Get the name of the namespace. */
9589 identifier = cp_parser_identifier (parser);
9590 if (identifier == error_mark_node)
9591 return error_mark_node;
9592
9593 /* Look up the identifier in the currently active scope. Look only
9594 for namespaces, due to:
9595
9596 [basic.lookup.udir]
9597
9598 When looking up a namespace-name in a using-directive or alias
9599 definition, only namespace names are considered.
9600
9601 And:
9602
9603 [basic.lookup.qual]
9604
9605 During the lookup of a name preceding the :: scope resolution
9606 operator, object, function, and enumerator names are ignored.
9607
9608 (Note that cp_parser_class_or_namespace_name only calls this
9609 function if the token after the name is the scope resolution
9610 operator.) */
9611 namespace_decl = cp_parser_lookup_name (parser, identifier,
9612 /*is_type=*/false,
9613 /*is_template=*/false,
9614 /*is_namespace=*/true,
9615 /*check_dependency=*/true);
9616 /* If it's not a namespace, issue an error. */
9617 if (namespace_decl == error_mark_node
9618 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9619 {
9620 cp_parser_error (parser, "expected namespace-name");
9621 namespace_decl = error_mark_node;
9622 }
9623
9624 return namespace_decl;
9625 }
9626
9627 /* Parse a namespace-definition.
9628
9629 namespace-definition:
9630 named-namespace-definition
9631 unnamed-namespace-definition
9632
9633 named-namespace-definition:
9634 original-namespace-definition
9635 extension-namespace-definition
9636
9637 original-namespace-definition:
9638 namespace identifier { namespace-body }
9639
9640 extension-namespace-definition:
9641 namespace original-namespace-name { namespace-body }
9642
9643 unnamed-namespace-definition:
9644 namespace { namespace-body } */
9645
9646 static void
9647 cp_parser_namespace_definition (cp_parser* parser)
9648 {
9649 tree identifier;
9650
9651 /* Look for the `namespace' keyword. */
9652 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9653
9654 /* Get the name of the namespace. We do not attempt to distinguish
9655 between an original-namespace-definition and an
9656 extension-namespace-definition at this point. The semantic
9657 analysis routines are responsible for that. */
9658 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9659 identifier = cp_parser_identifier (parser);
9660 else
9661 identifier = NULL_TREE;
9662
9663 /* Look for the `{' to start the namespace. */
9664 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9665 /* Start the namespace. */
9666 push_namespace (identifier);
9667 /* Parse the body of the namespace. */
9668 cp_parser_namespace_body (parser);
9669 /* Finish the namespace. */
9670 pop_namespace ();
9671 /* Look for the final `}'. */
9672 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9673 }
9674
9675 /* Parse a namespace-body.
9676
9677 namespace-body:
9678 declaration-seq [opt] */
9679
9680 static void
9681 cp_parser_namespace_body (cp_parser* parser)
9682 {
9683 cp_parser_declaration_seq_opt (parser);
9684 }
9685
9686 /* Parse a namespace-alias-definition.
9687
9688 namespace-alias-definition:
9689 namespace identifier = qualified-namespace-specifier ; */
9690
9691 static void
9692 cp_parser_namespace_alias_definition (cp_parser* parser)
9693 {
9694 tree identifier;
9695 tree namespace_specifier;
9696
9697 /* Look for the `namespace' keyword. */
9698 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9699 /* Look for the identifier. */
9700 identifier = cp_parser_identifier (parser);
9701 if (identifier == error_mark_node)
9702 return;
9703 /* Look for the `=' token. */
9704 cp_parser_require (parser, CPP_EQ, "`='");
9705 /* Look for the qualified-namespace-specifier. */
9706 namespace_specifier
9707 = cp_parser_qualified_namespace_specifier (parser);
9708 /* Look for the `;' token. */
9709 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9710
9711 /* Register the alias in the symbol table. */
9712 do_namespace_alias (identifier, namespace_specifier);
9713 }
9714
9715 /* Parse a qualified-namespace-specifier.
9716
9717 qualified-namespace-specifier:
9718 :: [opt] nested-name-specifier [opt] namespace-name
9719
9720 Returns a NAMESPACE_DECL corresponding to the specified
9721 namespace. */
9722
9723 static tree
9724 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9725 {
9726 /* Look for the optional `::'. */
9727 cp_parser_global_scope_opt (parser,
9728 /*current_scope_valid_p=*/false);
9729
9730 /* Look for the optional nested-name-specifier. */
9731 cp_parser_nested_name_specifier_opt (parser,
9732 /*typename_keyword_p=*/false,
9733 /*check_dependency_p=*/true,
9734 /*type_p=*/false,
9735 /*is_declaration=*/true);
9736
9737 return cp_parser_namespace_name (parser);
9738 }
9739
9740 /* Parse a using-declaration.
9741
9742 using-declaration:
9743 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9744 using :: unqualified-id ; */
9745
9746 static void
9747 cp_parser_using_declaration (cp_parser* parser)
9748 {
9749 cp_token *token;
9750 bool typename_p = false;
9751 bool global_scope_p;
9752 tree decl;
9753 tree identifier;
9754 tree scope;
9755 tree qscope;
9756
9757 /* Look for the `using' keyword. */
9758 cp_parser_require_keyword (parser, RID_USING, "`using'");
9759
9760 /* Peek at the next token. */
9761 token = cp_lexer_peek_token (parser->lexer);
9762 /* See if it's `typename'. */
9763 if (token->keyword == RID_TYPENAME)
9764 {
9765 /* Remember that we've seen it. */
9766 typename_p = true;
9767 /* Consume the `typename' token. */
9768 cp_lexer_consume_token (parser->lexer);
9769 }
9770
9771 /* Look for the optional global scope qualification. */
9772 global_scope_p
9773 = (cp_parser_global_scope_opt (parser,
9774 /*current_scope_valid_p=*/false)
9775 != NULL_TREE);
9776
9777 /* If we saw `typename', or didn't see `::', then there must be a
9778 nested-name-specifier present. */
9779 if (typename_p || !global_scope_p)
9780 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9781 /*check_dependency_p=*/true,
9782 /*type_p=*/false,
9783 /*is_declaration=*/true);
9784 /* Otherwise, we could be in either of the two productions. In that
9785 case, treat the nested-name-specifier as optional. */
9786 else
9787 qscope = cp_parser_nested_name_specifier_opt (parser,
9788 /*typename_keyword_p=*/false,
9789 /*check_dependency_p=*/true,
9790 /*type_p=*/false,
9791 /*is_declaration=*/true);
9792 if (!qscope)
9793 qscope = global_namespace;
9794
9795 /* Parse the unqualified-id. */
9796 identifier = cp_parser_unqualified_id (parser,
9797 /*template_keyword_p=*/false,
9798 /*check_dependency_p=*/true,
9799 /*declarator_p=*/true);
9800
9801 /* The function we call to handle a using-declaration is different
9802 depending on what scope we are in. */
9803 if (identifier == error_mark_node)
9804 ;
9805 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9806 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9807 /* [namespace.udecl]
9808
9809 A using declaration shall not name a template-id. */
9810 error ("a template-id may not appear in a using-declaration");
9811 else
9812 {
9813 scope = current_scope ();
9814 if (scope && TYPE_P (scope))
9815 {
9816 /* Create the USING_DECL. */
9817 decl = do_class_using_decl (build_nt (SCOPE_REF,
9818 parser->scope,
9819 identifier));
9820 /* Add it to the list of members in this class. */
9821 finish_member_declaration (decl);
9822 }
9823 else
9824 {
9825 decl = cp_parser_lookup_name_simple (parser, identifier);
9826 if (decl == error_mark_node)
9827 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9828 else if (scope)
9829 do_local_using_decl (decl, qscope, identifier);
9830 else
9831 do_toplevel_using_decl (decl, qscope, identifier);
9832 }
9833 }
9834
9835 /* Look for the final `;'. */
9836 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9837 }
9838
9839 /* Parse a using-directive.
9840
9841 using-directive:
9842 using namespace :: [opt] nested-name-specifier [opt]
9843 namespace-name ; */
9844
9845 static void
9846 cp_parser_using_directive (cp_parser* parser)
9847 {
9848 tree namespace_decl;
9849 tree attribs;
9850
9851 /* Look for the `using' keyword. */
9852 cp_parser_require_keyword (parser, RID_USING, "`using'");
9853 /* And the `namespace' keyword. */
9854 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9855 /* Look for the optional `::' operator. */
9856 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9857 /* And the optional nested-name-specifier. */
9858 cp_parser_nested_name_specifier_opt (parser,
9859 /*typename_keyword_p=*/false,
9860 /*check_dependency_p=*/true,
9861 /*type_p=*/false,
9862 /*is_declaration=*/true);
9863 /* Get the namespace being used. */
9864 namespace_decl = cp_parser_namespace_name (parser);
9865 /* And any specified attributes. */
9866 attribs = cp_parser_attributes_opt (parser);
9867 /* Update the symbol table. */
9868 parse_using_directive (namespace_decl, attribs);
9869 /* Look for the final `;'. */
9870 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9871 }
9872
9873 /* Parse an asm-definition.
9874
9875 asm-definition:
9876 asm ( string-literal ) ;
9877
9878 GNU Extension:
9879
9880 asm-definition:
9881 asm volatile [opt] ( string-literal ) ;
9882 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9883 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9884 : asm-operand-list [opt] ) ;
9885 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9886 : asm-operand-list [opt]
9887 : asm-operand-list [opt] ) ; */
9888
9889 static void
9890 cp_parser_asm_definition (cp_parser* parser)
9891 {
9892 cp_token *token;
9893 tree string;
9894 tree outputs = NULL_TREE;
9895 tree inputs = NULL_TREE;
9896 tree clobbers = NULL_TREE;
9897 tree asm_stmt;
9898 bool volatile_p = false;
9899 bool extended_p = false;
9900
9901 /* Look for the `asm' keyword. */
9902 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9903 /* See if the next token is `volatile'. */
9904 if (cp_parser_allow_gnu_extensions_p (parser)
9905 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9906 {
9907 /* Remember that we saw the `volatile' keyword. */
9908 volatile_p = true;
9909 /* Consume the token. */
9910 cp_lexer_consume_token (parser->lexer);
9911 }
9912 /* Look for the opening `('. */
9913 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9914 /* Look for the string. */
9915 c_lex_string_translate = false;
9916 token = cp_parser_require (parser, CPP_STRING, "asm body");
9917 if (!token)
9918 goto finish;
9919 string = token->value;
9920 /* If we're allowing GNU extensions, check for the extended assembly
9921 syntax. Unfortunately, the `:' tokens need not be separated by
9922 a space in C, and so, for compatibility, we tolerate that here
9923 too. Doing that means that we have to treat the `::' operator as
9924 two `:' tokens. */
9925 if (cp_parser_allow_gnu_extensions_p (parser)
9926 && at_function_scope_p ()
9927 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9928 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9929 {
9930 bool inputs_p = false;
9931 bool clobbers_p = false;
9932
9933 /* The extended syntax was used. */
9934 extended_p = true;
9935
9936 /* Look for outputs. */
9937 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9938 {
9939 /* Consume the `:'. */
9940 cp_lexer_consume_token (parser->lexer);
9941 /* Parse the output-operands. */
9942 if (cp_lexer_next_token_is_not (parser->lexer,
9943 CPP_COLON)
9944 && cp_lexer_next_token_is_not (parser->lexer,
9945 CPP_SCOPE)
9946 && cp_lexer_next_token_is_not (parser->lexer,
9947 CPP_CLOSE_PAREN))
9948 outputs = cp_parser_asm_operand_list (parser);
9949 }
9950 /* If the next token is `::', there are no outputs, and the
9951 next token is the beginning of the inputs. */
9952 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9953 {
9954 /* Consume the `::' token. */
9955 cp_lexer_consume_token (parser->lexer);
9956 /* The inputs are coming next. */
9957 inputs_p = true;
9958 }
9959
9960 /* Look for inputs. */
9961 if (inputs_p
9962 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9963 {
9964 if (!inputs_p)
9965 /* Consume the `:'. */
9966 cp_lexer_consume_token (parser->lexer);
9967 /* Parse the output-operands. */
9968 if (cp_lexer_next_token_is_not (parser->lexer,
9969 CPP_COLON)
9970 && cp_lexer_next_token_is_not (parser->lexer,
9971 CPP_SCOPE)
9972 && cp_lexer_next_token_is_not (parser->lexer,
9973 CPP_CLOSE_PAREN))
9974 inputs = cp_parser_asm_operand_list (parser);
9975 }
9976 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9977 /* The clobbers are coming next. */
9978 clobbers_p = true;
9979
9980 /* Look for clobbers. */
9981 if (clobbers_p
9982 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9983 {
9984 if (!clobbers_p)
9985 /* Consume the `:'. */
9986 cp_lexer_consume_token (parser->lexer);
9987 /* Parse the clobbers. */
9988 if (cp_lexer_next_token_is_not (parser->lexer,
9989 CPP_CLOSE_PAREN))
9990 clobbers = cp_parser_asm_clobber_list (parser);
9991 }
9992 }
9993 /* Look for the closing `)'. */
9994 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9995 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9996 /*consume_paren=*/true);
9997 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9998
9999 /* Create the ASM_STMT. */
10000 if (at_function_scope_p ())
10001 {
10002 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10003 inputs, clobbers);
10004 /* If the extended syntax was not used, mark the ASM_STMT. */
10005 if (!extended_p)
10006 ASM_INPUT_P (asm_stmt) = 1;
10007 }
10008 else
10009 assemble_asm (string);
10010
10011 finish:
10012 c_lex_string_translate = true;
10013 }
10014
10015 /* Declarators [gram.dcl.decl] */
10016
10017 /* Parse an init-declarator.
10018
10019 init-declarator:
10020 declarator initializer [opt]
10021
10022 GNU Extension:
10023
10024 init-declarator:
10025 declarator asm-specification [opt] attributes [opt] initializer [opt]
10026
10027 function-definition:
10028 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10029 function-body
10030 decl-specifier-seq [opt] declarator function-try-block
10031
10032 GNU Extension:
10033
10034 function-definition:
10035 __extension__ function-definition
10036
10037 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10038 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10039 then this declarator appears in a class scope. The new DECL created
10040 by this declarator is returned.
10041
10042 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10043 for a function-definition here as well. If the declarator is a
10044 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10045 be TRUE upon return. By that point, the function-definition will
10046 have been completely parsed.
10047
10048 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10049 is FALSE. */
10050
10051 static tree
10052 cp_parser_init_declarator (cp_parser* parser,
10053 tree decl_specifiers,
10054 tree prefix_attributes,
10055 bool function_definition_allowed_p,
10056 bool member_p,
10057 int declares_class_or_enum,
10058 bool* function_definition_p)
10059 {
10060 cp_token *token;
10061 tree declarator;
10062 tree attributes;
10063 tree asm_specification;
10064 tree initializer;
10065 tree decl = NULL_TREE;
10066 tree scope;
10067 bool is_initialized;
10068 bool is_parenthesized_init;
10069 bool is_non_constant_init;
10070 int ctor_dtor_or_conv_p;
10071 bool friend_p;
10072 bool pop_p = false;
10073
10074 /* Assume that this is not the declarator for a function
10075 definition. */
10076 if (function_definition_p)
10077 *function_definition_p = false;
10078
10079 /* Defer access checks while parsing the declarator; we cannot know
10080 what names are accessible until we know what is being
10081 declared. */
10082 resume_deferring_access_checks ();
10083
10084 /* Parse the declarator. */
10085 declarator
10086 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10087 &ctor_dtor_or_conv_p,
10088 /*parenthesized_p=*/NULL);
10089 /* Gather up the deferred checks. */
10090 stop_deferring_access_checks ();
10091
10092 /* If the DECLARATOR was erroneous, there's no need to go
10093 further. */
10094 if (declarator == error_mark_node)
10095 return error_mark_node;
10096
10097 cp_parser_check_for_definition_in_return_type (declarator,
10098 declares_class_or_enum);
10099
10100 /* Figure out what scope the entity declared by the DECLARATOR is
10101 located in. `grokdeclarator' sometimes changes the scope, so
10102 we compute it now. */
10103 scope = get_scope_of_declarator (declarator);
10104
10105 /* If we're allowing GNU extensions, look for an asm-specification
10106 and attributes. */
10107 if (cp_parser_allow_gnu_extensions_p (parser))
10108 {
10109 /* Look for an asm-specification. */
10110 asm_specification = cp_parser_asm_specification_opt (parser);
10111 /* And attributes. */
10112 attributes = cp_parser_attributes_opt (parser);
10113 }
10114 else
10115 {
10116 asm_specification = NULL_TREE;
10117 attributes = NULL_TREE;
10118 }
10119
10120 /* Peek at the next token. */
10121 token = cp_lexer_peek_token (parser->lexer);
10122 /* Check to see if the token indicates the start of a
10123 function-definition. */
10124 if (cp_parser_token_starts_function_definition_p (token))
10125 {
10126 if (!function_definition_allowed_p)
10127 {
10128 /* If a function-definition should not appear here, issue an
10129 error message. */
10130 cp_parser_error (parser,
10131 "a function-definition is not allowed here");
10132 return error_mark_node;
10133 }
10134 else
10135 {
10136 /* Neither attributes nor an asm-specification are allowed
10137 on a function-definition. */
10138 if (asm_specification)
10139 error ("an asm-specification is not allowed on a function-definition");
10140 if (attributes)
10141 error ("attributes are not allowed on a function-definition");
10142 /* This is a function-definition. */
10143 *function_definition_p = true;
10144
10145 /* Parse the function definition. */
10146 if (member_p)
10147 decl = cp_parser_save_member_function_body (parser,
10148 decl_specifiers,
10149 declarator,
10150 prefix_attributes);
10151 else
10152 decl
10153 = (cp_parser_function_definition_from_specifiers_and_declarator
10154 (parser, decl_specifiers, prefix_attributes, declarator));
10155
10156 return decl;
10157 }
10158 }
10159
10160 /* [dcl.dcl]
10161
10162 Only in function declarations for constructors, destructors, and
10163 type conversions can the decl-specifier-seq be omitted.
10164
10165 We explicitly postpone this check past the point where we handle
10166 function-definitions because we tolerate function-definitions
10167 that are missing their return types in some modes. */
10168 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10169 {
10170 cp_parser_error (parser,
10171 "expected constructor, destructor, or type conversion");
10172 return error_mark_node;
10173 }
10174
10175 /* An `=' or an `(' indicates an initializer. */
10176 is_initialized = (token->type == CPP_EQ
10177 || token->type == CPP_OPEN_PAREN);
10178 /* If the init-declarator isn't initialized and isn't followed by a
10179 `,' or `;', it's not a valid init-declarator. */
10180 if (!is_initialized
10181 && token->type != CPP_COMMA
10182 && token->type != CPP_SEMICOLON)
10183 {
10184 cp_parser_error (parser, "expected init-declarator");
10185 return error_mark_node;
10186 }
10187
10188 /* Because start_decl has side-effects, we should only call it if we
10189 know we're going ahead. By this point, we know that we cannot
10190 possibly be looking at any other construct. */
10191 cp_parser_commit_to_tentative_parse (parser);
10192
10193 /* If the decl specifiers were bad, issue an error now that we're
10194 sure this was intended to be a declarator. Then continue
10195 declaring the variable(s), as int, to try to cut down on further
10196 errors. */
10197 if (decl_specifiers != NULL
10198 && TREE_VALUE (decl_specifiers) == error_mark_node)
10199 {
10200 cp_parser_error (parser, "invalid type in declaration");
10201 TREE_VALUE (decl_specifiers) = integer_type_node;
10202 }
10203
10204 /* Check to see whether or not this declaration is a friend. */
10205 friend_p = cp_parser_friend_p (decl_specifiers);
10206
10207 /* Check that the number of template-parameter-lists is OK. */
10208 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10209 return error_mark_node;
10210
10211 /* Enter the newly declared entry in the symbol table. If we're
10212 processing a declaration in a class-specifier, we wait until
10213 after processing the initializer. */
10214 if (!member_p)
10215 {
10216 if (parser->in_unbraced_linkage_specification_p)
10217 {
10218 decl_specifiers = tree_cons (error_mark_node,
10219 get_identifier ("extern"),
10220 decl_specifiers);
10221 have_extern_spec = false;
10222 }
10223 decl = start_decl (declarator, decl_specifiers,
10224 is_initialized, attributes, prefix_attributes);
10225 }
10226
10227 /* Enter the SCOPE. That way unqualified names appearing in the
10228 initializer will be looked up in SCOPE. */
10229 if (scope)
10230 pop_p = push_scope (scope);
10231
10232 /* Perform deferred access control checks, now that we know in which
10233 SCOPE the declared entity resides. */
10234 if (!member_p && decl)
10235 {
10236 tree saved_current_function_decl = NULL_TREE;
10237
10238 /* If the entity being declared is a function, pretend that we
10239 are in its scope. If it is a `friend', it may have access to
10240 things that would not otherwise be accessible. */
10241 if (TREE_CODE (decl) == FUNCTION_DECL)
10242 {
10243 saved_current_function_decl = current_function_decl;
10244 current_function_decl = decl;
10245 }
10246
10247 /* Perform the access control checks for the declarator and the
10248 the decl-specifiers. */
10249 perform_deferred_access_checks ();
10250
10251 /* Restore the saved value. */
10252 if (TREE_CODE (decl) == FUNCTION_DECL)
10253 current_function_decl = saved_current_function_decl;
10254 }
10255
10256 /* Parse the initializer. */
10257 if (is_initialized)
10258 initializer = cp_parser_initializer (parser,
10259 &is_parenthesized_init,
10260 &is_non_constant_init);
10261 else
10262 {
10263 initializer = NULL_TREE;
10264 is_parenthesized_init = false;
10265 is_non_constant_init = true;
10266 }
10267
10268 /* The old parser allows attributes to appear after a parenthesized
10269 initializer. Mark Mitchell proposed removing this functionality
10270 on the GCC mailing lists on 2002-08-13. This parser accepts the
10271 attributes -- but ignores them. */
10272 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10273 if (cp_parser_attributes_opt (parser))
10274 warning ("attributes after parenthesized initializer ignored");
10275
10276 /* Leave the SCOPE, now that we have processed the initializer. It
10277 is important to do this before calling cp_finish_decl because it
10278 makes decisions about whether to create DECL_STMTs or not based
10279 on the current scope. */
10280 if (pop_p)
10281 pop_scope (scope);
10282
10283 /* For an in-class declaration, use `grokfield' to create the
10284 declaration. */
10285 if (member_p)
10286 {
10287 decl = grokfield (declarator, decl_specifiers,
10288 initializer, /*asmspec=*/NULL_TREE,
10289 /*attributes=*/NULL_TREE);
10290 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10291 cp_parser_save_default_args (parser, decl);
10292 }
10293
10294 /* Finish processing the declaration. But, skip friend
10295 declarations. */
10296 if (!friend_p && decl)
10297 cp_finish_decl (decl,
10298 initializer,
10299 asm_specification,
10300 /* If the initializer is in parentheses, then this is
10301 a direct-initialization, which means that an
10302 `explicit' constructor is OK. Otherwise, an
10303 `explicit' constructor cannot be used. */
10304 ((is_parenthesized_init || !is_initialized)
10305 ? 0 : LOOKUP_ONLYCONVERTING));
10306
10307 /* Remember whether or not variables were initialized by
10308 constant-expressions. */
10309 if (decl && TREE_CODE (decl) == VAR_DECL
10310 && is_initialized && !is_non_constant_init)
10311 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10312
10313 return decl;
10314 }
10315
10316 /* Parse a declarator.
10317
10318 declarator:
10319 direct-declarator
10320 ptr-operator declarator
10321
10322 abstract-declarator:
10323 ptr-operator abstract-declarator [opt]
10324 direct-abstract-declarator
10325
10326 GNU Extensions:
10327
10328 declarator:
10329 attributes [opt] direct-declarator
10330 attributes [opt] ptr-operator declarator
10331
10332 abstract-declarator:
10333 attributes [opt] ptr-operator abstract-declarator [opt]
10334 attributes [opt] direct-abstract-declarator
10335
10336 Returns a representation of the declarator. If the declarator has
10337 the form `* declarator', then an INDIRECT_REF is returned, whose
10338 only operand is the sub-declarator. Analogously, `& declarator' is
10339 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10340 used. The first operand is the TYPE for `X'. The second operand
10341 is an INDIRECT_REF whose operand is the sub-declarator.
10342
10343 Otherwise, the representation is as for a direct-declarator.
10344
10345 (It would be better to define a structure type to represent
10346 declarators, rather than abusing `tree' nodes to represent
10347 declarators. That would be much clearer and save some memory.
10348 There is no reason for declarators to be garbage-collected, for
10349 example; they are created during parser and no longer needed after
10350 `grokdeclarator' has been called.)
10351
10352 For a ptr-operator that has the optional cv-qualifier-seq,
10353 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10354 node.
10355
10356 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10357 detect constructor, destructor or conversion operators. It is set
10358 to -1 if the declarator is a name, and +1 if it is a
10359 function. Otherwise it is set to zero. Usually you just want to
10360 test for >0, but internally the negative value is used.
10361
10362 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10363 a decl-specifier-seq unless it declares a constructor, destructor,
10364 or conversion. It might seem that we could check this condition in
10365 semantic analysis, rather than parsing, but that makes it difficult
10366 to handle something like `f()'. We want to notice that there are
10367 no decl-specifiers, and therefore realize that this is an
10368 expression, not a declaration.)
10369
10370 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10371 the declarator is a direct-declarator of the form "(...)". */
10372
10373 static tree
10374 cp_parser_declarator (cp_parser* parser,
10375 cp_parser_declarator_kind dcl_kind,
10376 int* ctor_dtor_or_conv_p,
10377 bool* parenthesized_p)
10378 {
10379 cp_token *token;
10380 tree declarator;
10381 enum tree_code code;
10382 tree cv_qualifier_seq;
10383 tree class_type;
10384 tree attributes = NULL_TREE;
10385
10386 /* Assume this is not a constructor, destructor, or type-conversion
10387 operator. */
10388 if (ctor_dtor_or_conv_p)
10389 *ctor_dtor_or_conv_p = 0;
10390
10391 if (cp_parser_allow_gnu_extensions_p (parser))
10392 attributes = cp_parser_attributes_opt (parser);
10393
10394 /* Peek at the next token. */
10395 token = cp_lexer_peek_token (parser->lexer);
10396
10397 /* Check for the ptr-operator production. */
10398 cp_parser_parse_tentatively (parser);
10399 /* Parse the ptr-operator. */
10400 code = cp_parser_ptr_operator (parser,
10401 &class_type,
10402 &cv_qualifier_seq);
10403 /* If that worked, then we have a ptr-operator. */
10404 if (cp_parser_parse_definitely (parser))
10405 {
10406 /* If a ptr-operator was found, then this declarator was not
10407 parenthesized. */
10408 if (parenthesized_p)
10409 *parenthesized_p = true;
10410 /* The dependent declarator is optional if we are parsing an
10411 abstract-declarator. */
10412 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10413 cp_parser_parse_tentatively (parser);
10414
10415 /* Parse the dependent declarator. */
10416 declarator = cp_parser_declarator (parser, dcl_kind,
10417 /*ctor_dtor_or_conv_p=*/NULL,
10418 /*parenthesized_p=*/NULL);
10419
10420 /* If we are parsing an abstract-declarator, we must handle the
10421 case where the dependent declarator is absent. */
10422 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10423 && !cp_parser_parse_definitely (parser))
10424 declarator = NULL_TREE;
10425
10426 /* Build the representation of the ptr-operator. */
10427 if (code == INDIRECT_REF)
10428 declarator = make_pointer_declarator (cv_qualifier_seq,
10429 declarator);
10430 else
10431 declarator = make_reference_declarator (cv_qualifier_seq,
10432 declarator);
10433 /* Handle the pointer-to-member case. */
10434 if (class_type)
10435 declarator = build_nt (SCOPE_REF, class_type, declarator);
10436 }
10437 /* Everything else is a direct-declarator. */
10438 else
10439 {
10440 if (parenthesized_p)
10441 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10442 CPP_OPEN_PAREN);
10443 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10444 ctor_dtor_or_conv_p);
10445 }
10446
10447 if (attributes && declarator != error_mark_node)
10448 declarator = tree_cons (attributes, declarator, NULL_TREE);
10449
10450 return declarator;
10451 }
10452
10453 /* Parse a direct-declarator or direct-abstract-declarator.
10454
10455 direct-declarator:
10456 declarator-id
10457 direct-declarator ( parameter-declaration-clause )
10458 cv-qualifier-seq [opt]
10459 exception-specification [opt]
10460 direct-declarator [ constant-expression [opt] ]
10461 ( declarator )
10462
10463 direct-abstract-declarator:
10464 direct-abstract-declarator [opt]
10465 ( parameter-declaration-clause )
10466 cv-qualifier-seq [opt]
10467 exception-specification [opt]
10468 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10469 ( abstract-declarator )
10470
10471 Returns a representation of the declarator. DCL_KIND is
10472 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10473 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10474 we are parsing a direct-declarator. It is
10475 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10476 of ambiguity we prefer an abstract declarator, as per
10477 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10478 cp_parser_declarator.
10479
10480 For the declarator-id production, the representation is as for an
10481 id-expression, except that a qualified name is represented as a
10482 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10483 see the documentation of the FUNCTION_DECLARATOR_* macros for
10484 information about how to find the various declarator components.
10485 An array-declarator is represented as an ARRAY_REF. The
10486 direct-declarator is the first operand; the constant-expression
10487 indicating the size of the array is the second operand. */
10488
10489 static tree
10490 cp_parser_direct_declarator (cp_parser* parser,
10491 cp_parser_declarator_kind dcl_kind,
10492 int* ctor_dtor_or_conv_p)
10493 {
10494 cp_token *token;
10495 tree declarator = NULL_TREE;
10496 tree scope = NULL_TREE;
10497 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10498 bool saved_in_declarator_p = parser->in_declarator_p;
10499 bool first = true;
10500 bool pop_p = false;
10501
10502 while (true)
10503 {
10504 /* Peek at the next token. */
10505 token = cp_lexer_peek_token (parser->lexer);
10506 if (token->type == CPP_OPEN_PAREN)
10507 {
10508 /* This is either a parameter-declaration-clause, or a
10509 parenthesized declarator. When we know we are parsing a
10510 named declarator, it must be a parenthesized declarator
10511 if FIRST is true. For instance, `(int)' is a
10512 parameter-declaration-clause, with an omitted
10513 direct-abstract-declarator. But `((*))', is a
10514 parenthesized abstract declarator. Finally, when T is a
10515 template parameter `(T)' is a
10516 parameter-declaration-clause, and not a parenthesized
10517 named declarator.
10518
10519 We first try and parse a parameter-declaration-clause,
10520 and then try a nested declarator (if FIRST is true).
10521
10522 It is not an error for it not to be a
10523 parameter-declaration-clause, even when FIRST is
10524 false. Consider,
10525
10526 int i (int);
10527 int i (3);
10528
10529 The first is the declaration of a function while the
10530 second is a the definition of a variable, including its
10531 initializer.
10532
10533 Having seen only the parenthesis, we cannot know which of
10534 these two alternatives should be selected. Even more
10535 complex are examples like:
10536
10537 int i (int (a));
10538 int i (int (3));
10539
10540 The former is a function-declaration; the latter is a
10541 variable initialization.
10542
10543 Thus again, we try a parameter-declaration-clause, and if
10544 that fails, we back out and return. */
10545
10546 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10547 {
10548 tree params;
10549 unsigned saved_num_template_parameter_lists;
10550
10551 cp_parser_parse_tentatively (parser);
10552
10553 /* Consume the `('. */
10554 cp_lexer_consume_token (parser->lexer);
10555 if (first)
10556 {
10557 /* If this is going to be an abstract declarator, we're
10558 in a declarator and we can't have default args. */
10559 parser->default_arg_ok_p = false;
10560 parser->in_declarator_p = true;
10561 }
10562
10563 /* Inside the function parameter list, surrounding
10564 template-parameter-lists do not apply. */
10565 saved_num_template_parameter_lists
10566 = parser->num_template_parameter_lists;
10567 parser->num_template_parameter_lists = 0;
10568
10569 /* Parse the parameter-declaration-clause. */
10570 params = cp_parser_parameter_declaration_clause (parser);
10571
10572 parser->num_template_parameter_lists
10573 = saved_num_template_parameter_lists;
10574
10575 /* If all went well, parse the cv-qualifier-seq and the
10576 exception-specification. */
10577 if (cp_parser_parse_definitely (parser))
10578 {
10579 tree cv_qualifiers;
10580 tree exception_specification;
10581
10582 if (ctor_dtor_or_conv_p)
10583 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10584 first = false;
10585 /* Consume the `)'. */
10586 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10587
10588 /* Parse the cv-qualifier-seq. */
10589 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10590 /* And the exception-specification. */
10591 exception_specification
10592 = cp_parser_exception_specification_opt (parser);
10593
10594 /* Create the function-declarator. */
10595 declarator = make_call_declarator (declarator,
10596 params,
10597 cv_qualifiers,
10598 exception_specification);
10599 /* Any subsequent parameter lists are to do with
10600 return type, so are not those of the declared
10601 function. */
10602 parser->default_arg_ok_p = false;
10603
10604 /* Repeat the main loop. */
10605 continue;
10606 }
10607 }
10608
10609 /* If this is the first, we can try a parenthesized
10610 declarator. */
10611 if (first)
10612 {
10613 bool saved_in_type_id_in_expr_p;
10614
10615 parser->default_arg_ok_p = saved_default_arg_ok_p;
10616 parser->in_declarator_p = saved_in_declarator_p;
10617
10618 /* Consume the `('. */
10619 cp_lexer_consume_token (parser->lexer);
10620 /* Parse the nested declarator. */
10621 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10622 parser->in_type_id_in_expr_p = true;
10623 declarator
10624 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10625 /*parenthesized_p=*/NULL);
10626 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10627 first = false;
10628 /* Expect a `)'. */
10629 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10630 declarator = error_mark_node;
10631 if (declarator == error_mark_node)
10632 break;
10633
10634 goto handle_declarator;
10635 }
10636 /* Otherwise, we must be done. */
10637 else
10638 break;
10639 }
10640 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10641 && token->type == CPP_OPEN_SQUARE)
10642 {
10643 /* Parse an array-declarator. */
10644 tree bounds;
10645
10646 if (ctor_dtor_or_conv_p)
10647 *ctor_dtor_or_conv_p = 0;
10648
10649 first = false;
10650 parser->default_arg_ok_p = false;
10651 parser->in_declarator_p = true;
10652 /* Consume the `['. */
10653 cp_lexer_consume_token (parser->lexer);
10654 /* Peek at the next token. */
10655 token = cp_lexer_peek_token (parser->lexer);
10656 /* If the next token is `]', then there is no
10657 constant-expression. */
10658 if (token->type != CPP_CLOSE_SQUARE)
10659 {
10660 bool non_constant_p;
10661
10662 bounds
10663 = cp_parser_constant_expression (parser,
10664 /*allow_non_constant=*/true,
10665 &non_constant_p);
10666 if (!non_constant_p)
10667 bounds = fold_non_dependent_expr (bounds);
10668 }
10669 else
10670 bounds = NULL_TREE;
10671 /* Look for the closing `]'. */
10672 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10673 {
10674 declarator = error_mark_node;
10675 break;
10676 }
10677
10678 declarator = build_nt (ARRAY_REF, declarator, bounds);
10679 }
10680 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10681 {
10682 /* Parse a declarator-id */
10683 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10684 cp_parser_parse_tentatively (parser);
10685 declarator = cp_parser_declarator_id (parser);
10686 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10687 {
10688 if (!cp_parser_parse_definitely (parser))
10689 declarator = error_mark_node;
10690 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10691 {
10692 cp_parser_error (parser, "expected unqualified-id");
10693 declarator = error_mark_node;
10694 }
10695 }
10696
10697 if (declarator == error_mark_node)
10698 break;
10699
10700 if (TREE_CODE (declarator) == SCOPE_REF
10701 && !current_scope ())
10702 {
10703 tree scope = TREE_OPERAND (declarator, 0);
10704
10705 /* In the declaration of a member of a template class
10706 outside of the class itself, the SCOPE will sometimes
10707 be a TYPENAME_TYPE. For example, given:
10708
10709 template <typename T>
10710 int S<T>::R::i = 3;
10711
10712 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10713 this context, we must resolve S<T>::R to an ordinary
10714 type, rather than a typename type.
10715
10716 The reason we normally avoid resolving TYPENAME_TYPEs
10717 is that a specialization of `S' might render
10718 `S<T>::R' not a type. However, if `S' is
10719 specialized, then this `i' will not be used, so there
10720 is no harm in resolving the types here. */
10721 if (TREE_CODE (scope) == TYPENAME_TYPE)
10722 {
10723 tree type;
10724
10725 /* Resolve the TYPENAME_TYPE. */
10726 type = resolve_typename_type (scope,
10727 /*only_current_p=*/false);
10728 /* If that failed, the declarator is invalid. */
10729 if (type == error_mark_node)
10730 error ("`%T::%D' is not a type",
10731 TYPE_CONTEXT (scope),
10732 TYPE_IDENTIFIER (scope));
10733 /* Build a new DECLARATOR. */
10734 declarator = build_nt (SCOPE_REF,
10735 type,
10736 TREE_OPERAND (declarator, 1));
10737 }
10738 }
10739
10740 /* Check to see whether the declarator-id names a constructor,
10741 destructor, or conversion. */
10742 if (declarator && ctor_dtor_or_conv_p
10743 && ((TREE_CODE (declarator) == SCOPE_REF
10744 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10745 || (TREE_CODE (declarator) != SCOPE_REF
10746 && at_class_scope_p ())))
10747 {
10748 tree unqualified_name;
10749 tree class_type;
10750
10751 /* Get the unqualified part of the name. */
10752 if (TREE_CODE (declarator) == SCOPE_REF)
10753 {
10754 class_type = TREE_OPERAND (declarator, 0);
10755 unqualified_name = TREE_OPERAND (declarator, 1);
10756 }
10757 else
10758 {
10759 class_type = current_class_type;
10760 unqualified_name = declarator;
10761 }
10762
10763 /* See if it names ctor, dtor or conv. */
10764 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10765 || IDENTIFIER_TYPENAME_P (unqualified_name)
10766 || constructor_name_p (unqualified_name, class_type)
10767 || (TREE_CODE (unqualified_name) == TYPE_DECL
10768 && same_type_p (TREE_TYPE (unqualified_name),
10769 class_type)))
10770 *ctor_dtor_or_conv_p = -1;
10771 if (TREE_CODE (declarator) == SCOPE_REF
10772 && TREE_CODE (unqualified_name) == TYPE_DECL
10773 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
10774 {
10775 error ("invalid use of constructor as a template");
10776 inform ("use `%T::%D' instead of `%T::%T' to name the "
10777 "constructor in a qualified name", class_type,
10778 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
10779 class_type, class_type);
10780 }
10781 }
10782
10783 handle_declarator:;
10784 scope = get_scope_of_declarator (declarator);
10785 if (scope)
10786 /* Any names that appear after the declarator-id for a
10787 member are looked up in the containing scope. */
10788 pop_p = push_scope (scope);
10789 parser->in_declarator_p = true;
10790 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10791 || (declarator
10792 && (TREE_CODE (declarator) == SCOPE_REF
10793 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10794 /* Default args are only allowed on function
10795 declarations. */
10796 parser->default_arg_ok_p = saved_default_arg_ok_p;
10797 else
10798 parser->default_arg_ok_p = false;
10799
10800 first = false;
10801 }
10802 /* We're done. */
10803 else
10804 break;
10805 }
10806
10807 /* For an abstract declarator, we might wind up with nothing at this
10808 point. That's an error; the declarator is not optional. */
10809 if (!declarator)
10810 cp_parser_error (parser, "expected declarator");
10811
10812 /* If we entered a scope, we must exit it now. */
10813 if (pop_p)
10814 pop_scope (scope);
10815
10816 parser->default_arg_ok_p = saved_default_arg_ok_p;
10817 parser->in_declarator_p = saved_in_declarator_p;
10818
10819 return declarator;
10820 }
10821
10822 /* Parse a ptr-operator.
10823
10824 ptr-operator:
10825 * cv-qualifier-seq [opt]
10826 &
10827 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10828
10829 GNU Extension:
10830
10831 ptr-operator:
10832 & cv-qualifier-seq [opt]
10833
10834 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10835 used. Returns ADDR_EXPR if a reference was used. In the
10836 case of a pointer-to-member, *TYPE is filled in with the
10837 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10838 with the cv-qualifier-seq, or NULL_TREE, if there are no
10839 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10840
10841 static enum tree_code
10842 cp_parser_ptr_operator (cp_parser* parser,
10843 tree* type,
10844 tree* cv_qualifier_seq)
10845 {
10846 enum tree_code code = ERROR_MARK;
10847 cp_token *token;
10848
10849 /* Assume that it's not a pointer-to-member. */
10850 *type = NULL_TREE;
10851 /* And that there are no cv-qualifiers. */
10852 *cv_qualifier_seq = NULL_TREE;
10853
10854 /* Peek at the next token. */
10855 token = cp_lexer_peek_token (parser->lexer);
10856 /* If it's a `*' or `&' we have a pointer or reference. */
10857 if (token->type == CPP_MULT || token->type == CPP_AND)
10858 {
10859 /* Remember which ptr-operator we were processing. */
10860 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10861
10862 /* Consume the `*' or `&'. */
10863 cp_lexer_consume_token (parser->lexer);
10864
10865 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10866 `&', if we are allowing GNU extensions. (The only qualifier
10867 that can legally appear after `&' is `restrict', but that is
10868 enforced during semantic analysis. */
10869 if (code == INDIRECT_REF
10870 || cp_parser_allow_gnu_extensions_p (parser))
10871 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10872 }
10873 else
10874 {
10875 /* Try the pointer-to-member case. */
10876 cp_parser_parse_tentatively (parser);
10877 /* Look for the optional `::' operator. */
10878 cp_parser_global_scope_opt (parser,
10879 /*current_scope_valid_p=*/false);
10880 /* Look for the nested-name specifier. */
10881 cp_parser_nested_name_specifier (parser,
10882 /*typename_keyword_p=*/false,
10883 /*check_dependency_p=*/true,
10884 /*type_p=*/false,
10885 /*is_declaration=*/false);
10886 /* If we found it, and the next token is a `*', then we are
10887 indeed looking at a pointer-to-member operator. */
10888 if (!cp_parser_error_occurred (parser)
10889 && cp_parser_require (parser, CPP_MULT, "`*'"))
10890 {
10891 /* The type of which the member is a member is given by the
10892 current SCOPE. */
10893 *type = parser->scope;
10894 /* The next name will not be qualified. */
10895 parser->scope = NULL_TREE;
10896 parser->qualifying_scope = NULL_TREE;
10897 parser->object_scope = NULL_TREE;
10898 /* Indicate that the `*' operator was used. */
10899 code = INDIRECT_REF;
10900 /* Look for the optional cv-qualifier-seq. */
10901 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10902 }
10903 /* If that didn't work we don't have a ptr-operator. */
10904 if (!cp_parser_parse_definitely (parser))
10905 cp_parser_error (parser, "expected ptr-operator");
10906 }
10907
10908 return code;
10909 }
10910
10911 /* Parse an (optional) cv-qualifier-seq.
10912
10913 cv-qualifier-seq:
10914 cv-qualifier cv-qualifier-seq [opt]
10915
10916 Returns a TREE_LIST. The TREE_VALUE of each node is the
10917 representation of a cv-qualifier. */
10918
10919 static tree
10920 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10921 {
10922 tree cv_qualifiers = NULL_TREE;
10923
10924 while (true)
10925 {
10926 tree cv_qualifier;
10927
10928 /* Look for the next cv-qualifier. */
10929 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10930 /* If we didn't find one, we're done. */
10931 if (!cv_qualifier)
10932 break;
10933
10934 /* Add this cv-qualifier to the list. */
10935 cv_qualifiers
10936 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10937 }
10938
10939 /* We built up the list in reverse order. */
10940 return nreverse (cv_qualifiers);
10941 }
10942
10943 /* Parse an (optional) cv-qualifier.
10944
10945 cv-qualifier:
10946 const
10947 volatile
10948
10949 GNU Extension:
10950
10951 cv-qualifier:
10952 __restrict__ */
10953
10954 static tree
10955 cp_parser_cv_qualifier_opt (cp_parser* parser)
10956 {
10957 cp_token *token;
10958 tree cv_qualifier = NULL_TREE;
10959
10960 /* Peek at the next token. */
10961 token = cp_lexer_peek_token (parser->lexer);
10962 /* See if it's a cv-qualifier. */
10963 switch (token->keyword)
10964 {
10965 case RID_CONST:
10966 case RID_VOLATILE:
10967 case RID_RESTRICT:
10968 /* Save the value of the token. */
10969 cv_qualifier = token->value;
10970 /* Consume the token. */
10971 cp_lexer_consume_token (parser->lexer);
10972 break;
10973
10974 default:
10975 break;
10976 }
10977
10978 return cv_qualifier;
10979 }
10980
10981 /* Parse a declarator-id.
10982
10983 declarator-id:
10984 id-expression
10985 :: [opt] nested-name-specifier [opt] type-name
10986
10987 In the `id-expression' case, the value returned is as for
10988 cp_parser_id_expression if the id-expression was an unqualified-id.
10989 If the id-expression was a qualified-id, then a SCOPE_REF is
10990 returned. The first operand is the scope (either a NAMESPACE_DECL
10991 or TREE_TYPE), but the second is still just a representation of an
10992 unqualified-id. */
10993
10994 static tree
10995 cp_parser_declarator_id (cp_parser* parser)
10996 {
10997 tree id_expression;
10998
10999 /* The expression must be an id-expression. Assume that qualified
11000 names are the names of types so that:
11001
11002 template <class T>
11003 int S<T>::R::i = 3;
11004
11005 will work; we must treat `S<T>::R' as the name of a type.
11006 Similarly, assume that qualified names are templates, where
11007 required, so that:
11008
11009 template <class T>
11010 int S<T>::R<T>::i = 3;
11011
11012 will work, too. */
11013 id_expression = cp_parser_id_expression (parser,
11014 /*template_keyword_p=*/false,
11015 /*check_dependency_p=*/false,
11016 /*template_p=*/NULL,
11017 /*declarator_p=*/true);
11018 /* If the name was qualified, create a SCOPE_REF to represent
11019 that. */
11020 if (parser->scope)
11021 {
11022 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11023 parser->scope = NULL_TREE;
11024 }
11025
11026 return id_expression;
11027 }
11028
11029 /* Parse a type-id.
11030
11031 type-id:
11032 type-specifier-seq abstract-declarator [opt]
11033
11034 Returns the TYPE specified. */
11035
11036 static tree
11037 cp_parser_type_id (cp_parser* parser)
11038 {
11039 tree type_specifier_seq;
11040 tree abstract_declarator;
11041
11042 /* Parse the type-specifier-seq. */
11043 type_specifier_seq
11044 = cp_parser_type_specifier_seq (parser);
11045 if (type_specifier_seq == error_mark_node)
11046 return error_mark_node;
11047
11048 /* There might or might not be an abstract declarator. */
11049 cp_parser_parse_tentatively (parser);
11050 /* Look for the declarator. */
11051 abstract_declarator
11052 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11053 /*parenthesized_p=*/NULL);
11054 /* Check to see if there really was a declarator. */
11055 if (!cp_parser_parse_definitely (parser))
11056 abstract_declarator = NULL_TREE;
11057
11058 return groktypename (build_tree_list (type_specifier_seq,
11059 abstract_declarator));
11060 }
11061
11062 /* Parse a type-specifier-seq.
11063
11064 type-specifier-seq:
11065 type-specifier type-specifier-seq [opt]
11066
11067 GNU extension:
11068
11069 type-specifier-seq:
11070 attributes type-specifier-seq [opt]
11071
11072 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
11073 type-specifier, or the TREE_PURPOSE is a list of attributes. */
11074
11075 static tree
11076 cp_parser_type_specifier_seq (cp_parser* parser)
11077 {
11078 bool seen_type_specifier = false;
11079 tree type_specifier_seq = NULL_TREE;
11080
11081 /* Parse the type-specifiers and attributes. */
11082 while (true)
11083 {
11084 tree type_specifier;
11085
11086 /* Check for attributes first. */
11087 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11088 {
11089 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
11090 NULL_TREE,
11091 type_specifier_seq);
11092 continue;
11093 }
11094
11095 /* After the first type-specifier, others are optional. */
11096 if (seen_type_specifier)
11097 cp_parser_parse_tentatively (parser);
11098 /* Look for the type-specifier. */
11099 type_specifier = cp_parser_type_specifier (parser,
11100 CP_PARSER_FLAGS_NONE,
11101 /*is_friend=*/false,
11102 /*is_declaration=*/false,
11103 NULL,
11104 NULL);
11105 /* If the first type-specifier could not be found, this is not a
11106 type-specifier-seq at all. */
11107 if (!seen_type_specifier && type_specifier == error_mark_node)
11108 return error_mark_node;
11109 /* If subsequent type-specifiers could not be found, the
11110 type-specifier-seq is complete. */
11111 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11112 break;
11113
11114 /* Add the new type-specifier to the list. */
11115 type_specifier_seq
11116 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11117 seen_type_specifier = true;
11118 }
11119
11120 /* We built up the list in reverse order. */
11121 return nreverse (type_specifier_seq);
11122 }
11123
11124 /* Parse a parameter-declaration-clause.
11125
11126 parameter-declaration-clause:
11127 parameter-declaration-list [opt] ... [opt]
11128 parameter-declaration-list , ...
11129
11130 Returns a representation for the parameter declarations. Each node
11131 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
11132 representation.) If the parameter-declaration-clause ends with an
11133 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11134 list. A return value of NULL_TREE indicates a
11135 parameter-declaration-clause consisting only of an ellipsis. */
11136
11137 static tree
11138 cp_parser_parameter_declaration_clause (cp_parser* parser)
11139 {
11140 tree parameters;
11141 cp_token *token;
11142 bool ellipsis_p;
11143
11144 /* Peek at the next token. */
11145 token = cp_lexer_peek_token (parser->lexer);
11146 /* Check for trivial parameter-declaration-clauses. */
11147 if (token->type == CPP_ELLIPSIS)
11148 {
11149 /* Consume the `...' token. */
11150 cp_lexer_consume_token (parser->lexer);
11151 return NULL_TREE;
11152 }
11153 else if (token->type == CPP_CLOSE_PAREN)
11154 /* There are no parameters. */
11155 {
11156 #ifndef NO_IMPLICIT_EXTERN_C
11157 if (in_system_header && current_class_type == NULL
11158 && current_lang_name == lang_name_c)
11159 return NULL_TREE;
11160 else
11161 #endif
11162 return void_list_node;
11163 }
11164 /* Check for `(void)', too, which is a special case. */
11165 else if (token->keyword == RID_VOID
11166 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11167 == CPP_CLOSE_PAREN))
11168 {
11169 /* Consume the `void' token. */
11170 cp_lexer_consume_token (parser->lexer);
11171 /* There are no parameters. */
11172 return void_list_node;
11173 }
11174
11175 /* Parse the parameter-declaration-list. */
11176 parameters = cp_parser_parameter_declaration_list (parser);
11177 /* If a parse error occurred while parsing the
11178 parameter-declaration-list, then the entire
11179 parameter-declaration-clause is erroneous. */
11180 if (parameters == error_mark_node)
11181 return error_mark_node;
11182
11183 /* Peek at the next token. */
11184 token = cp_lexer_peek_token (parser->lexer);
11185 /* If it's a `,', the clause should terminate with an ellipsis. */
11186 if (token->type == CPP_COMMA)
11187 {
11188 /* Consume the `,'. */
11189 cp_lexer_consume_token (parser->lexer);
11190 /* Expect an ellipsis. */
11191 ellipsis_p
11192 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11193 }
11194 /* It might also be `...' if the optional trailing `,' was
11195 omitted. */
11196 else if (token->type == CPP_ELLIPSIS)
11197 {
11198 /* Consume the `...' token. */
11199 cp_lexer_consume_token (parser->lexer);
11200 /* And remember that we saw it. */
11201 ellipsis_p = true;
11202 }
11203 else
11204 ellipsis_p = false;
11205
11206 /* Finish the parameter list. */
11207 return finish_parmlist (parameters, ellipsis_p);
11208 }
11209
11210 /* Parse a parameter-declaration-list.
11211
11212 parameter-declaration-list:
11213 parameter-declaration
11214 parameter-declaration-list , parameter-declaration
11215
11216 Returns a representation of the parameter-declaration-list, as for
11217 cp_parser_parameter_declaration_clause. However, the
11218 `void_list_node' is never appended to the list. */
11219
11220 static tree
11221 cp_parser_parameter_declaration_list (cp_parser* parser)
11222 {
11223 tree parameters = NULL_TREE;
11224
11225 /* Look for more parameters. */
11226 while (true)
11227 {
11228 tree parameter;
11229 bool parenthesized_p;
11230 /* Parse the parameter. */
11231 parameter
11232 = cp_parser_parameter_declaration (parser,
11233 /*template_parm_p=*/false,
11234 &parenthesized_p);
11235
11236 /* If a parse error occurred parsing the parameter declaration,
11237 then the entire parameter-declaration-list is erroneous. */
11238 if (parameter == error_mark_node)
11239 {
11240 parameters = error_mark_node;
11241 break;
11242 }
11243 /* Add the new parameter to the list. */
11244 TREE_CHAIN (parameter) = parameters;
11245 parameters = parameter;
11246
11247 /* Peek at the next token. */
11248 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11249 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11250 /* The parameter-declaration-list is complete. */
11251 break;
11252 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11253 {
11254 cp_token *token;
11255
11256 /* Peek at the next token. */
11257 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11258 /* If it's an ellipsis, then the list is complete. */
11259 if (token->type == CPP_ELLIPSIS)
11260 break;
11261 /* Otherwise, there must be more parameters. Consume the
11262 `,'. */
11263 cp_lexer_consume_token (parser->lexer);
11264 /* When parsing something like:
11265
11266 int i(float f, double d)
11267
11268 we can tell after seeing the declaration for "f" that we
11269 are not looking at an initialization of a variable "i",
11270 but rather at the declaration of a function "i".
11271
11272 Due to the fact that the parsing of template arguments
11273 (as specified to a template-id) requires backtracking we
11274 cannot use this technique when inside a template argument
11275 list. */
11276 if (!parser->in_template_argument_list_p
11277 && !parser->in_type_id_in_expr_p
11278 && cp_parser_parsing_tentatively (parser)
11279 && !cp_parser_committed_to_tentative_parse (parser)
11280 /* However, a parameter-declaration of the form
11281 "foat(f)" (which is a valid declaration of a
11282 parameter "f") can also be interpreted as an
11283 expression (the conversion of "f" to "float"). */
11284 && !parenthesized_p)
11285 cp_parser_commit_to_tentative_parse (parser);
11286 }
11287 else
11288 {
11289 cp_parser_error (parser, "expected `,' or `...'");
11290 if (!cp_parser_parsing_tentatively (parser)
11291 || cp_parser_committed_to_tentative_parse (parser))
11292 cp_parser_skip_to_closing_parenthesis (parser,
11293 /*recovering=*/true,
11294 /*or_comma=*/false,
11295 /*consume_paren=*/false);
11296 break;
11297 }
11298 }
11299
11300 /* We built up the list in reverse order; straighten it out now. */
11301 return nreverse (parameters);
11302 }
11303
11304 /* Parse a parameter declaration.
11305
11306 parameter-declaration:
11307 decl-specifier-seq declarator
11308 decl-specifier-seq declarator = assignment-expression
11309 decl-specifier-seq abstract-declarator [opt]
11310 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11311
11312 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11313 declares a template parameter. (In that case, a non-nested `>'
11314 token encountered during the parsing of the assignment-expression
11315 is not interpreted as a greater-than operator.)
11316
11317 Returns a TREE_LIST representing the parameter-declaration. The
11318 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11319 there is no default argument. The TREE_VALUE is a representation
11320 of the decl-specifier-seq and declarator. In particular, the
11321 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11322 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11323 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11324 the declarator is of the form "(p)". */
11325
11326 static tree
11327 cp_parser_parameter_declaration (cp_parser *parser,
11328 bool template_parm_p,
11329 bool *parenthesized_p)
11330 {
11331 int declares_class_or_enum;
11332 bool greater_than_is_operator_p;
11333 tree decl_specifiers;
11334 tree attributes;
11335 tree declarator;
11336 tree default_argument;
11337 tree parameter;
11338 cp_token *token;
11339 const char *saved_message;
11340
11341 /* In a template parameter, `>' is not an operator.
11342
11343 [temp.param]
11344
11345 When parsing a default template-argument for a non-type
11346 template-parameter, the first non-nested `>' is taken as the end
11347 of the template parameter-list rather than a greater-than
11348 operator. */
11349 greater_than_is_operator_p = !template_parm_p;
11350
11351 /* Type definitions may not appear in parameter types. */
11352 saved_message = parser->type_definition_forbidden_message;
11353 parser->type_definition_forbidden_message
11354 = "types may not be defined in parameter types";
11355
11356 /* Parse the declaration-specifiers. */
11357 decl_specifiers
11358 = cp_parser_decl_specifier_seq (parser,
11359 CP_PARSER_FLAGS_NONE,
11360 &attributes,
11361 &declares_class_or_enum);
11362 /* If an error occurred, there's no reason to attempt to parse the
11363 rest of the declaration. */
11364 if (cp_parser_error_occurred (parser))
11365 {
11366 parser->type_definition_forbidden_message = saved_message;
11367 return error_mark_node;
11368 }
11369
11370 /* Peek at the next token. */
11371 token = cp_lexer_peek_token (parser->lexer);
11372 /* If the next token is a `)', `,', `=', `>', or `...', then there
11373 is no declarator. */
11374 if (token->type == CPP_CLOSE_PAREN
11375 || token->type == CPP_COMMA
11376 || token->type == CPP_EQ
11377 || token->type == CPP_ELLIPSIS
11378 || token->type == CPP_GREATER)
11379 {
11380 declarator = NULL_TREE;
11381 if (parenthesized_p)
11382 *parenthesized_p = false;
11383 }
11384 /* Otherwise, there should be a declarator. */
11385 else
11386 {
11387 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11388 parser->default_arg_ok_p = false;
11389
11390 /* After seeing a decl-specifier-seq, if the next token is not a
11391 "(", there is no possibility that the code is a valid
11392 expression. Therefore, if parsing tentatively, we commit at
11393 this point. */
11394 if (!parser->in_template_argument_list_p
11395 /* In an expression context, having seen:
11396
11397 (int((char ...
11398
11399 we cannot be sure whether we are looking at a
11400 function-type (taking a "char" as a parameter) or a cast
11401 of some object of type "char" to "int". */
11402 && !parser->in_type_id_in_expr_p
11403 && cp_parser_parsing_tentatively (parser)
11404 && !cp_parser_committed_to_tentative_parse (parser)
11405 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11406 cp_parser_commit_to_tentative_parse (parser);
11407 /* Parse the declarator. */
11408 declarator = cp_parser_declarator (parser,
11409 CP_PARSER_DECLARATOR_EITHER,
11410 /*ctor_dtor_or_conv_p=*/NULL,
11411 parenthesized_p);
11412 parser->default_arg_ok_p = saved_default_arg_ok_p;
11413 /* After the declarator, allow more attributes. */
11414 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11415 }
11416
11417 /* The restriction on defining new types applies only to the type
11418 of the parameter, not to the default argument. */
11419 parser->type_definition_forbidden_message = saved_message;
11420
11421 /* If the next token is `=', then process a default argument. */
11422 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11423 {
11424 bool saved_greater_than_is_operator_p;
11425 /* Consume the `='. */
11426 cp_lexer_consume_token (parser->lexer);
11427
11428 /* If we are defining a class, then the tokens that make up the
11429 default argument must be saved and processed later. */
11430 if (!template_parm_p && at_class_scope_p ()
11431 && TYPE_BEING_DEFINED (current_class_type))
11432 {
11433 unsigned depth = 0;
11434
11435 /* Create a DEFAULT_ARG to represented the unparsed default
11436 argument. */
11437 default_argument = make_node (DEFAULT_ARG);
11438 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11439
11440 /* Add tokens until we have processed the entire default
11441 argument. */
11442 while (true)
11443 {
11444 bool done = false;
11445 cp_token *token;
11446
11447 /* Peek at the next token. */
11448 token = cp_lexer_peek_token (parser->lexer);
11449 /* What we do depends on what token we have. */
11450 switch (token->type)
11451 {
11452 /* In valid code, a default argument must be
11453 immediately followed by a `,' `)', or `...'. */
11454 case CPP_COMMA:
11455 case CPP_CLOSE_PAREN:
11456 case CPP_ELLIPSIS:
11457 /* If we run into a non-nested `;', `}', or `]',
11458 then the code is invalid -- but the default
11459 argument is certainly over. */
11460 case CPP_SEMICOLON:
11461 case CPP_CLOSE_BRACE:
11462 case CPP_CLOSE_SQUARE:
11463 if (depth == 0)
11464 done = true;
11465 /* Update DEPTH, if necessary. */
11466 else if (token->type == CPP_CLOSE_PAREN
11467 || token->type == CPP_CLOSE_BRACE
11468 || token->type == CPP_CLOSE_SQUARE)
11469 --depth;
11470 break;
11471
11472 case CPP_OPEN_PAREN:
11473 case CPP_OPEN_SQUARE:
11474 case CPP_OPEN_BRACE:
11475 ++depth;
11476 break;
11477
11478 case CPP_GREATER:
11479 /* If we see a non-nested `>', and `>' is not an
11480 operator, then it marks the end of the default
11481 argument. */
11482 if (!depth && !greater_than_is_operator_p)
11483 done = true;
11484 break;
11485
11486 /* If we run out of tokens, issue an error message. */
11487 case CPP_EOF:
11488 error ("file ends in default argument");
11489 done = true;
11490 break;
11491
11492 case CPP_NAME:
11493 case CPP_SCOPE:
11494 /* In these cases, we should look for template-ids.
11495 For example, if the default argument is
11496 `X<int, double>()', we need to do name lookup to
11497 figure out whether or not `X' is a template; if
11498 so, the `,' does not end the default argument.
11499
11500 That is not yet done. */
11501 break;
11502
11503 default:
11504 break;
11505 }
11506
11507 /* If we've reached the end, stop. */
11508 if (done)
11509 break;
11510
11511 /* Add the token to the token block. */
11512 token = cp_lexer_consume_token (parser->lexer);
11513 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11514 token);
11515 }
11516 }
11517 /* Outside of a class definition, we can just parse the
11518 assignment-expression. */
11519 else
11520 {
11521 bool saved_local_variables_forbidden_p;
11522
11523 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11524 set correctly. */
11525 saved_greater_than_is_operator_p
11526 = parser->greater_than_is_operator_p;
11527 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11528 /* Local variable names (and the `this' keyword) may not
11529 appear in a default argument. */
11530 saved_local_variables_forbidden_p
11531 = parser->local_variables_forbidden_p;
11532 parser->local_variables_forbidden_p = true;
11533 /* Parse the assignment-expression. */
11534 default_argument = cp_parser_assignment_expression (parser);
11535 /* Restore saved state. */
11536 parser->greater_than_is_operator_p
11537 = saved_greater_than_is_operator_p;
11538 parser->local_variables_forbidden_p
11539 = saved_local_variables_forbidden_p;
11540 }
11541 if (!parser->default_arg_ok_p)
11542 {
11543 if (!flag_pedantic_errors)
11544 warning ("deprecated use of default argument for parameter of non-function");
11545 else
11546 {
11547 error ("default arguments are only permitted for function parameters");
11548 default_argument = NULL_TREE;
11549 }
11550 }
11551 }
11552 else
11553 default_argument = NULL_TREE;
11554
11555 /* Create the representation of the parameter. */
11556 if (attributes)
11557 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11558 parameter = build_tree_list (default_argument,
11559 build_tree_list (decl_specifiers,
11560 declarator));
11561
11562 return parameter;
11563 }
11564
11565 /* Parse a function-body.
11566
11567 function-body:
11568 compound_statement */
11569
11570 static void
11571 cp_parser_function_body (cp_parser *parser)
11572 {
11573 cp_parser_compound_statement (parser, false);
11574 }
11575
11576 /* Parse a ctor-initializer-opt followed by a function-body. Return
11577 true if a ctor-initializer was present. */
11578
11579 static bool
11580 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11581 {
11582 tree body;
11583 bool ctor_initializer_p;
11584
11585 /* Begin the function body. */
11586 body = begin_function_body ();
11587 /* Parse the optional ctor-initializer. */
11588 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11589 /* Parse the function-body. */
11590 cp_parser_function_body (parser);
11591 /* Finish the function body. */
11592 finish_function_body (body);
11593
11594 return ctor_initializer_p;
11595 }
11596
11597 /* Parse an initializer.
11598
11599 initializer:
11600 = initializer-clause
11601 ( expression-list )
11602
11603 Returns a expression representing the initializer. If no
11604 initializer is present, NULL_TREE is returned.
11605
11606 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11607 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11608 set to FALSE if there is no initializer present. If there is an
11609 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11610 is set to true; otherwise it is set to false. */
11611
11612 static tree
11613 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11614 bool* non_constant_p)
11615 {
11616 cp_token *token;
11617 tree init;
11618
11619 /* Peek at the next token. */
11620 token = cp_lexer_peek_token (parser->lexer);
11621
11622 /* Let our caller know whether or not this initializer was
11623 parenthesized. */
11624 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11625 /* Assume that the initializer is constant. */
11626 *non_constant_p = false;
11627
11628 if (token->type == CPP_EQ)
11629 {
11630 /* Consume the `='. */
11631 cp_lexer_consume_token (parser->lexer);
11632 /* Parse the initializer-clause. */
11633 init = cp_parser_initializer_clause (parser, non_constant_p);
11634 }
11635 else if (token->type == CPP_OPEN_PAREN)
11636 init = cp_parser_parenthesized_expression_list (parser, false,
11637 non_constant_p);
11638 else
11639 {
11640 /* Anything else is an error. */
11641 cp_parser_error (parser, "expected initializer");
11642 init = error_mark_node;
11643 }
11644
11645 return init;
11646 }
11647
11648 /* Parse an initializer-clause.
11649
11650 initializer-clause:
11651 assignment-expression
11652 { initializer-list , [opt] }
11653 { }
11654
11655 Returns an expression representing the initializer.
11656
11657 If the `assignment-expression' production is used the value
11658 returned is simply a representation for the expression.
11659
11660 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11661 the elements of the initializer-list (or NULL_TREE, if the last
11662 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11663 NULL_TREE. There is no way to detect whether or not the optional
11664 trailing `,' was provided. NON_CONSTANT_P is as for
11665 cp_parser_initializer. */
11666
11667 static tree
11668 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11669 {
11670 tree initializer;
11671
11672 /* If it is not a `{', then we are looking at an
11673 assignment-expression. */
11674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11675 {
11676 initializer
11677 = cp_parser_constant_expression (parser,
11678 /*allow_non_constant_p=*/true,
11679 non_constant_p);
11680 if (!*non_constant_p)
11681 initializer = fold_non_dependent_expr (initializer);
11682 }
11683 else
11684 {
11685 /* Consume the `{' token. */
11686 cp_lexer_consume_token (parser->lexer);
11687 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11688 initializer = make_node (CONSTRUCTOR);
11689 /* If it's not a `}', then there is a non-trivial initializer. */
11690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11691 {
11692 /* Parse the initializer list. */
11693 CONSTRUCTOR_ELTS (initializer)
11694 = cp_parser_initializer_list (parser, non_constant_p);
11695 /* A trailing `,' token is allowed. */
11696 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11697 cp_lexer_consume_token (parser->lexer);
11698 }
11699 /* Now, there should be a trailing `}'. */
11700 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11701 }
11702
11703 return initializer;
11704 }
11705
11706 /* Parse an initializer-list.
11707
11708 initializer-list:
11709 initializer-clause
11710 initializer-list , initializer-clause
11711
11712 GNU Extension:
11713
11714 initializer-list:
11715 identifier : initializer-clause
11716 initializer-list, identifier : initializer-clause
11717
11718 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11719 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11720 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11721 as for cp_parser_initializer. */
11722
11723 static tree
11724 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11725 {
11726 tree initializers = NULL_TREE;
11727
11728 /* Assume all of the expressions are constant. */
11729 *non_constant_p = false;
11730
11731 /* Parse the rest of the list. */
11732 while (true)
11733 {
11734 cp_token *token;
11735 tree identifier;
11736 tree initializer;
11737 bool clause_non_constant_p;
11738
11739 /* If the next token is an identifier and the following one is a
11740 colon, we are looking at the GNU designated-initializer
11741 syntax. */
11742 if (cp_parser_allow_gnu_extensions_p (parser)
11743 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11744 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11745 {
11746 /* Consume the identifier. */
11747 identifier = cp_lexer_consume_token (parser->lexer)->value;
11748 /* Consume the `:'. */
11749 cp_lexer_consume_token (parser->lexer);
11750 }
11751 else
11752 identifier = NULL_TREE;
11753
11754 /* Parse the initializer. */
11755 initializer = cp_parser_initializer_clause (parser,
11756 &clause_non_constant_p);
11757 /* If any clause is non-constant, so is the entire initializer. */
11758 if (clause_non_constant_p)
11759 *non_constant_p = true;
11760 /* Add it to the list. */
11761 initializers = tree_cons (identifier, initializer, initializers);
11762
11763 /* If the next token is not a comma, we have reached the end of
11764 the list. */
11765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11766 break;
11767
11768 /* Peek at the next token. */
11769 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11770 /* If the next token is a `}', then we're still done. An
11771 initializer-clause can have a trailing `,' after the
11772 initializer-list and before the closing `}'. */
11773 if (token->type == CPP_CLOSE_BRACE)
11774 break;
11775
11776 /* Consume the `,' token. */
11777 cp_lexer_consume_token (parser->lexer);
11778 }
11779
11780 /* The initializers were built up in reverse order, so we need to
11781 reverse them now. */
11782 return nreverse (initializers);
11783 }
11784
11785 /* Classes [gram.class] */
11786
11787 /* Parse a class-name.
11788
11789 class-name:
11790 identifier
11791 template-id
11792
11793 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11794 to indicate that names looked up in dependent types should be
11795 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11796 keyword has been used to indicate that the name that appears next
11797 is a template. TYPE_P is true iff the next name should be treated
11798 as class-name, even if it is declared to be some other kind of name
11799 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11800 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11801 being defined in a class-head.
11802
11803 Returns the TYPE_DECL representing the class. */
11804
11805 static tree
11806 cp_parser_class_name (cp_parser *parser,
11807 bool typename_keyword_p,
11808 bool template_keyword_p,
11809 bool type_p,
11810 bool check_dependency_p,
11811 bool class_head_p,
11812 bool is_declaration)
11813 {
11814 tree decl;
11815 tree scope;
11816 bool typename_p;
11817 cp_token *token;
11818
11819 /* All class-names start with an identifier. */
11820 token = cp_lexer_peek_token (parser->lexer);
11821 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11822 {
11823 cp_parser_error (parser, "expected class-name");
11824 return error_mark_node;
11825 }
11826
11827 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11828 to a template-id, so we save it here. */
11829 scope = parser->scope;
11830 if (scope == error_mark_node)
11831 return error_mark_node;
11832
11833 /* Any name names a type if we're following the `typename' keyword
11834 in a qualified name where the enclosing scope is type-dependent. */
11835 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11836 && dependent_type_p (scope));
11837 /* Handle the common case (an identifier, but not a template-id)
11838 efficiently. */
11839 if (token->type == CPP_NAME
11840 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11841 {
11842 tree identifier;
11843
11844 /* Look for the identifier. */
11845 identifier = cp_parser_identifier (parser);
11846 /* If the next token isn't an identifier, we are certainly not
11847 looking at a class-name. */
11848 if (identifier == error_mark_node)
11849 decl = error_mark_node;
11850 /* If we know this is a type-name, there's no need to look it
11851 up. */
11852 else if (typename_p)
11853 decl = identifier;
11854 else
11855 {
11856 /* If the next token is a `::', then the name must be a type
11857 name.
11858
11859 [basic.lookup.qual]
11860
11861 During the lookup for a name preceding the :: scope
11862 resolution operator, object, function, and enumerator
11863 names are ignored. */
11864 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11865 type_p = true;
11866 /* Look up the name. */
11867 decl = cp_parser_lookup_name (parser, identifier,
11868 type_p,
11869 /*is_template=*/false,
11870 /*is_namespace=*/false,
11871 check_dependency_p);
11872 }
11873 }
11874 else
11875 {
11876 /* Try a template-id. */
11877 decl = cp_parser_template_id (parser, template_keyword_p,
11878 check_dependency_p,
11879 is_declaration);
11880 if (decl == error_mark_node)
11881 return error_mark_node;
11882 }
11883
11884 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11885
11886 /* If this is a typename, create a TYPENAME_TYPE. */
11887 if (typename_p && decl != error_mark_node)
11888 {
11889 decl = make_typename_type (scope, decl, /*complain=*/1);
11890 if (decl != error_mark_node)
11891 decl = TYPE_NAME (decl);
11892 }
11893
11894 /* Check to see that it is really the name of a class. */
11895 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11896 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11897 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11898 /* Situations like this:
11899
11900 template <typename T> struct A {
11901 typename T::template X<int>::I i;
11902 };
11903
11904 are problematic. Is `T::template X<int>' a class-name? The
11905 standard does not seem to be definitive, but there is no other
11906 valid interpretation of the following `::'. Therefore, those
11907 names are considered class-names. */
11908 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11909 else if (decl == error_mark_node
11910 || TREE_CODE (decl) != TYPE_DECL
11911 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11912 {
11913 cp_parser_error (parser, "expected class-name");
11914 return error_mark_node;
11915 }
11916
11917 return decl;
11918 }
11919
11920 /* Parse a class-specifier.
11921
11922 class-specifier:
11923 class-head { member-specification [opt] }
11924
11925 Returns the TREE_TYPE representing the class. */
11926
11927 static tree
11928 cp_parser_class_specifier (cp_parser* parser)
11929 {
11930 cp_token *token;
11931 tree type;
11932 tree attributes = NULL_TREE;
11933 int has_trailing_semicolon;
11934 bool nested_name_specifier_p;
11935 unsigned saved_num_template_parameter_lists;
11936 bool pop_p = false;
11937
11938 push_deferring_access_checks (dk_no_deferred);
11939
11940 /* Parse the class-head. */
11941 type = cp_parser_class_head (parser,
11942 &nested_name_specifier_p,
11943 &attributes);
11944 /* If the class-head was a semantic disaster, skip the entire body
11945 of the class. */
11946 if (!type)
11947 {
11948 cp_parser_skip_to_end_of_block_or_statement (parser);
11949 pop_deferring_access_checks ();
11950 return error_mark_node;
11951 }
11952
11953 /* Look for the `{'. */
11954 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11955 {
11956 pop_deferring_access_checks ();
11957 return error_mark_node;
11958 }
11959
11960 /* Issue an error message if type-definitions are forbidden here. */
11961 cp_parser_check_type_definition (parser);
11962 /* Remember that we are defining one more class. */
11963 ++parser->num_classes_being_defined;
11964 /* Inside the class, surrounding template-parameter-lists do not
11965 apply. */
11966 saved_num_template_parameter_lists
11967 = parser->num_template_parameter_lists;
11968 parser->num_template_parameter_lists = 0;
11969
11970 /* Start the class. */
11971 if (nested_name_specifier_p)
11972 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11973 type = begin_class_definition (type);
11974 if (type == error_mark_node)
11975 /* If the type is erroneous, skip the entire body of the class. */
11976 cp_parser_skip_to_closing_brace (parser);
11977 else
11978 /* Parse the member-specification. */
11979 cp_parser_member_specification_opt (parser);
11980 /* Look for the trailing `}'. */
11981 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11982 /* We get better error messages by noticing a common problem: a
11983 missing trailing `;'. */
11984 token = cp_lexer_peek_token (parser->lexer);
11985 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11986 /* Look for trailing attributes to apply to this class. */
11987 if (cp_parser_allow_gnu_extensions_p (parser))
11988 {
11989 tree sub_attr = cp_parser_attributes_opt (parser);
11990 attributes = chainon (attributes, sub_attr);
11991 }
11992 if (type != error_mark_node)
11993 type = finish_struct (type, attributes);
11994 if (pop_p)
11995 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11996 /* If this class is not itself within the scope of another class,
11997 then we need to parse the bodies of all of the queued function
11998 definitions. Note that the queued functions defined in a class
11999 are not always processed immediately following the
12000 class-specifier for that class. Consider:
12001
12002 struct A {
12003 struct B { void f() { sizeof (A); } };
12004 };
12005
12006 If `f' were processed before the processing of `A' were
12007 completed, there would be no way to compute the size of `A'.
12008 Note that the nesting we are interested in here is lexical --
12009 not the semantic nesting given by TYPE_CONTEXT. In particular,
12010 for:
12011
12012 struct A { struct B; };
12013 struct A::B { void f() { } };
12014
12015 there is no need to delay the parsing of `A::B::f'. */
12016 if (--parser->num_classes_being_defined == 0)
12017 {
12018 tree queue_entry;
12019 tree fn;
12020
12021 /* In a first pass, parse default arguments to the functions.
12022 Then, in a second pass, parse the bodies of the functions.
12023 This two-phased approach handles cases like:
12024
12025 struct S {
12026 void f() { g(); }
12027 void g(int i = 3);
12028 };
12029
12030 */
12031 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12032 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12033 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12034 TREE_PURPOSE (parser->unparsed_functions_queues)
12035 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12036 {
12037 fn = TREE_VALUE (queue_entry);
12038 /* Make sure that any template parameters are in scope. */
12039 maybe_begin_member_template_processing (fn);
12040 /* If there are default arguments that have not yet been processed,
12041 take care of them now. */
12042 cp_parser_late_parsing_default_args (parser, fn);
12043 /* Remove any template parameters from the symbol table. */
12044 maybe_end_member_template_processing ();
12045 }
12046 /* Now parse the body of the functions. */
12047 for (TREE_VALUE (parser->unparsed_functions_queues)
12048 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12049 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12050 TREE_VALUE (parser->unparsed_functions_queues)
12051 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12052 {
12053 /* Figure out which function we need to process. */
12054 fn = TREE_VALUE (queue_entry);
12055
12056 /* A hack to prevent garbage collection. */
12057 function_depth++;
12058
12059 /* Parse the function. */
12060 cp_parser_late_parsing_for_member (parser, fn);
12061 function_depth--;
12062 }
12063
12064 }
12065
12066 /* Put back any saved access checks. */
12067 pop_deferring_access_checks ();
12068
12069 /* Restore the count of active template-parameter-lists. */
12070 parser->num_template_parameter_lists
12071 = saved_num_template_parameter_lists;
12072
12073 return type;
12074 }
12075
12076 /* Parse a class-head.
12077
12078 class-head:
12079 class-key identifier [opt] base-clause [opt]
12080 class-key nested-name-specifier identifier base-clause [opt]
12081 class-key nested-name-specifier [opt] template-id
12082 base-clause [opt]
12083
12084 GNU Extensions:
12085 class-key attributes identifier [opt] base-clause [opt]
12086 class-key attributes nested-name-specifier identifier base-clause [opt]
12087 class-key attributes nested-name-specifier [opt] template-id
12088 base-clause [opt]
12089
12090 Returns the TYPE of the indicated class. Sets
12091 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12092 involving a nested-name-specifier was used, and FALSE otherwise.
12093
12094 Returns NULL_TREE if the class-head is syntactically valid, but
12095 semantically invalid in a way that means we should skip the entire
12096 body of the class. */
12097
12098 static tree
12099 cp_parser_class_head (cp_parser* parser,
12100 bool* nested_name_specifier_p,
12101 tree *attributes_p)
12102 {
12103 cp_token *token;
12104 tree nested_name_specifier;
12105 enum tag_types class_key;
12106 tree id = NULL_TREE;
12107 tree type = NULL_TREE;
12108 tree attributes;
12109 bool template_id_p = false;
12110 bool qualified_p = false;
12111 bool invalid_nested_name_p = false;
12112 bool invalid_explicit_specialization_p = false;
12113 bool pop_p = false;
12114 unsigned num_templates;
12115
12116 /* Assume no nested-name-specifier will be present. */
12117 *nested_name_specifier_p = false;
12118 /* Assume no template parameter lists will be used in defining the
12119 type. */
12120 num_templates = 0;
12121
12122 /* Look for the class-key. */
12123 class_key = cp_parser_class_key (parser);
12124 if (class_key == none_type)
12125 return error_mark_node;
12126
12127 /* Parse the attributes. */
12128 attributes = cp_parser_attributes_opt (parser);
12129
12130 /* If the next token is `::', that is invalid -- but sometimes
12131 people do try to write:
12132
12133 struct ::S {};
12134
12135 Handle this gracefully by accepting the extra qualifier, and then
12136 issuing an error about it later if this really is a
12137 class-head. If it turns out just to be an elaborated type
12138 specifier, remain silent. */
12139 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12140 qualified_p = true;
12141
12142 push_deferring_access_checks (dk_no_check);
12143
12144 /* Determine the name of the class. Begin by looking for an
12145 optional nested-name-specifier. */
12146 nested_name_specifier
12147 = cp_parser_nested_name_specifier_opt (parser,
12148 /*typename_keyword_p=*/false,
12149 /*check_dependency_p=*/false,
12150 /*type_p=*/false,
12151 /*is_declaration=*/false);
12152 /* If there was a nested-name-specifier, then there *must* be an
12153 identifier. */
12154 if (nested_name_specifier)
12155 {
12156 /* Although the grammar says `identifier', it really means
12157 `class-name' or `template-name'. You are only allowed to
12158 define a class that has already been declared with this
12159 syntax.
12160
12161 The proposed resolution for Core Issue 180 says that whever
12162 you see `class T::X' you should treat `X' as a type-name.
12163
12164 It is OK to define an inaccessible class; for example:
12165
12166 class A { class B; };
12167 class A::B {};
12168
12169 We do not know if we will see a class-name, or a
12170 template-name. We look for a class-name first, in case the
12171 class-name is a template-id; if we looked for the
12172 template-name first we would stop after the template-name. */
12173 cp_parser_parse_tentatively (parser);
12174 type = cp_parser_class_name (parser,
12175 /*typename_keyword_p=*/false,
12176 /*template_keyword_p=*/false,
12177 /*type_p=*/true,
12178 /*check_dependency_p=*/false,
12179 /*class_head_p=*/true,
12180 /*is_declaration=*/false);
12181 /* If that didn't work, ignore the nested-name-specifier. */
12182 if (!cp_parser_parse_definitely (parser))
12183 {
12184 invalid_nested_name_p = true;
12185 id = cp_parser_identifier (parser);
12186 if (id == error_mark_node)
12187 id = NULL_TREE;
12188 }
12189 /* If we could not find a corresponding TYPE, treat this
12190 declaration like an unqualified declaration. */
12191 if (type == error_mark_node)
12192 nested_name_specifier = NULL_TREE;
12193 /* Otherwise, count the number of templates used in TYPE and its
12194 containing scopes. */
12195 else
12196 {
12197 tree scope;
12198
12199 for (scope = TREE_TYPE (type);
12200 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12201 scope = (TYPE_P (scope)
12202 ? TYPE_CONTEXT (scope)
12203 : DECL_CONTEXT (scope)))
12204 if (TYPE_P (scope)
12205 && CLASS_TYPE_P (scope)
12206 && CLASSTYPE_TEMPLATE_INFO (scope)
12207 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12208 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12209 ++num_templates;
12210 }
12211 }
12212 /* Otherwise, the identifier is optional. */
12213 else
12214 {
12215 /* We don't know whether what comes next is a template-id,
12216 an identifier, or nothing at all. */
12217 cp_parser_parse_tentatively (parser);
12218 /* Check for a template-id. */
12219 id = cp_parser_template_id (parser,
12220 /*template_keyword_p=*/false,
12221 /*check_dependency_p=*/true,
12222 /*is_declaration=*/true);
12223 /* If that didn't work, it could still be an identifier. */
12224 if (!cp_parser_parse_definitely (parser))
12225 {
12226 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12227 id = cp_parser_identifier (parser);
12228 else
12229 id = NULL_TREE;
12230 }
12231 else
12232 {
12233 template_id_p = true;
12234 ++num_templates;
12235 }
12236 }
12237
12238 pop_deferring_access_checks ();
12239
12240 if (id)
12241 cp_parser_check_for_invalid_template_id (parser, id);
12242
12243 /* If it's not a `:' or a `{' then we can't really be looking at a
12244 class-head, since a class-head only appears as part of a
12245 class-specifier. We have to detect this situation before calling
12246 xref_tag, since that has irreversible side-effects. */
12247 if (!cp_parser_next_token_starts_class_definition_p (parser))
12248 {
12249 cp_parser_error (parser, "expected `{' or `:'");
12250 return error_mark_node;
12251 }
12252
12253 /* At this point, we're going ahead with the class-specifier, even
12254 if some other problem occurs. */
12255 cp_parser_commit_to_tentative_parse (parser);
12256 /* Issue the error about the overly-qualified name now. */
12257 if (qualified_p)
12258 cp_parser_error (parser,
12259 "global qualification of class name is invalid");
12260 else if (invalid_nested_name_p)
12261 cp_parser_error (parser,
12262 "qualified name does not name a class");
12263 else if (nested_name_specifier)
12264 {
12265 tree scope;
12266 /* Figure out in what scope the declaration is being placed. */
12267 scope = current_scope ();
12268 if (!scope)
12269 scope = current_namespace;
12270 /* If that scope does not contain the scope in which the
12271 class was originally declared, the program is invalid. */
12272 if (scope && !is_ancestor (scope, nested_name_specifier))
12273 {
12274 error ("declaration of `%D' in `%D' which does not "
12275 "enclose `%D'", type, scope, nested_name_specifier);
12276 type = NULL_TREE;
12277 goto done;
12278 }
12279 /* [dcl.meaning]
12280
12281 A declarator-id shall not be qualified exception of the
12282 definition of a ... nested class outside of its class
12283 ... [or] a the definition or explicit instantiation of a
12284 class member of a namespace outside of its namespace. */
12285 if (scope == nested_name_specifier)
12286 {
12287 pedwarn ("extra qualification ignored");
12288 nested_name_specifier = NULL_TREE;
12289 num_templates = 0;
12290 }
12291 }
12292 /* An explicit-specialization must be preceded by "template <>". If
12293 it is not, try to recover gracefully. */
12294 if (at_namespace_scope_p ()
12295 && parser->num_template_parameter_lists == 0
12296 && template_id_p)
12297 {
12298 error ("an explicit specialization must be preceded by 'template <>'");
12299 invalid_explicit_specialization_p = true;
12300 /* Take the same action that would have been taken by
12301 cp_parser_explicit_specialization. */
12302 ++parser->num_template_parameter_lists;
12303 begin_specialization ();
12304 }
12305 /* There must be no "return" statements between this point and the
12306 end of this function; set "type "to the correct return value and
12307 use "goto done;" to return. */
12308 /* Make sure that the right number of template parameters were
12309 present. */
12310 if (!cp_parser_check_template_parameters (parser, num_templates))
12311 {
12312 /* If something went wrong, there is no point in even trying to
12313 process the class-definition. */
12314 type = NULL_TREE;
12315 goto done;
12316 }
12317
12318 /* Look up the type. */
12319 if (template_id_p)
12320 {
12321 type = TREE_TYPE (id);
12322 maybe_process_partial_specialization (type);
12323 }
12324 else if (!nested_name_specifier)
12325 {
12326 /* If the class was unnamed, create a dummy name. */
12327 if (!id)
12328 id = make_anon_name ();
12329 type = xref_tag (class_key, id, /*globalize=*/false,
12330 parser->num_template_parameter_lists);
12331 }
12332 else
12333 {
12334 tree class_type;
12335 bool pop_p = false;
12336
12337 /* Given:
12338
12339 template <typename T> struct S { struct T };
12340 template <typename T> struct S<T>::T { };
12341
12342 we will get a TYPENAME_TYPE when processing the definition of
12343 `S::T'. We need to resolve it to the actual type before we
12344 try to define it. */
12345 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12346 {
12347 class_type = resolve_typename_type (TREE_TYPE (type),
12348 /*only_current_p=*/false);
12349 if (class_type != error_mark_node)
12350 type = TYPE_NAME (class_type);
12351 else
12352 {
12353 cp_parser_error (parser, "could not resolve typename type");
12354 type = error_mark_node;
12355 }
12356 }
12357
12358 maybe_process_partial_specialization (TREE_TYPE (type));
12359 class_type = current_class_type;
12360 /* Enter the scope indicated by the nested-name-specifier. */
12361 if (nested_name_specifier)
12362 pop_p = push_scope (nested_name_specifier);
12363 /* Get the canonical version of this type. */
12364 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12365 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12366 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12367 type = push_template_decl (type);
12368 type = TREE_TYPE (type);
12369 if (nested_name_specifier)
12370 {
12371 *nested_name_specifier_p = true;
12372 if (pop_p)
12373 pop_scope (nested_name_specifier);
12374 }
12375 }
12376 /* Indicate whether this class was declared as a `class' or as a
12377 `struct'. */
12378 if (TREE_CODE (type) == RECORD_TYPE)
12379 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12380 cp_parser_check_class_key (class_key, type);
12381
12382 /* Enter the scope containing the class; the names of base classes
12383 should be looked up in that context. For example, given:
12384
12385 struct A { struct B {}; struct C; };
12386 struct A::C : B {};
12387
12388 is valid. */
12389 if (nested_name_specifier)
12390 pop_p = push_scope (nested_name_specifier);
12391 /* Now, look for the base-clause. */
12392 token = cp_lexer_peek_token (parser->lexer);
12393 if (token->type == CPP_COLON)
12394 {
12395 tree bases;
12396
12397 /* Get the list of base-classes. */
12398 bases = cp_parser_base_clause (parser);
12399 /* Process them. */
12400 xref_basetypes (type, bases);
12401 }
12402 /* Leave the scope given by the nested-name-specifier. We will
12403 enter the class scope itself while processing the members. */
12404 if (pop_p)
12405 pop_scope (nested_name_specifier);
12406
12407 done:
12408 if (invalid_explicit_specialization_p)
12409 {
12410 end_specialization ();
12411 --parser->num_template_parameter_lists;
12412 }
12413 *attributes_p = attributes;
12414 return type;
12415 }
12416
12417 /* Parse a class-key.
12418
12419 class-key:
12420 class
12421 struct
12422 union
12423
12424 Returns the kind of class-key specified, or none_type to indicate
12425 error. */
12426
12427 static enum tag_types
12428 cp_parser_class_key (cp_parser* parser)
12429 {
12430 cp_token *token;
12431 enum tag_types tag_type;
12432
12433 /* Look for the class-key. */
12434 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12435 if (!token)
12436 return none_type;
12437
12438 /* Check to see if the TOKEN is a class-key. */
12439 tag_type = cp_parser_token_is_class_key (token);
12440 if (!tag_type)
12441 cp_parser_error (parser, "expected class-key");
12442 return tag_type;
12443 }
12444
12445 /* Parse an (optional) member-specification.
12446
12447 member-specification:
12448 member-declaration member-specification [opt]
12449 access-specifier : member-specification [opt] */
12450
12451 static void
12452 cp_parser_member_specification_opt (cp_parser* parser)
12453 {
12454 while (true)
12455 {
12456 cp_token *token;
12457 enum rid keyword;
12458
12459 /* Peek at the next token. */
12460 token = cp_lexer_peek_token (parser->lexer);
12461 /* If it's a `}', or EOF then we've seen all the members. */
12462 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12463 break;
12464
12465 /* See if this token is a keyword. */
12466 keyword = token->keyword;
12467 switch (keyword)
12468 {
12469 case RID_PUBLIC:
12470 case RID_PROTECTED:
12471 case RID_PRIVATE:
12472 /* Consume the access-specifier. */
12473 cp_lexer_consume_token (parser->lexer);
12474 /* Remember which access-specifier is active. */
12475 current_access_specifier = token->value;
12476 /* Look for the `:'. */
12477 cp_parser_require (parser, CPP_COLON, "`:'");
12478 break;
12479
12480 default:
12481 /* Otherwise, the next construction must be a
12482 member-declaration. */
12483 cp_parser_member_declaration (parser);
12484 }
12485 }
12486 }
12487
12488 /* Parse a member-declaration.
12489
12490 member-declaration:
12491 decl-specifier-seq [opt] member-declarator-list [opt] ;
12492 function-definition ; [opt]
12493 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12494 using-declaration
12495 template-declaration
12496
12497 member-declarator-list:
12498 member-declarator
12499 member-declarator-list , member-declarator
12500
12501 member-declarator:
12502 declarator pure-specifier [opt]
12503 declarator constant-initializer [opt]
12504 identifier [opt] : constant-expression
12505
12506 GNU Extensions:
12507
12508 member-declaration:
12509 __extension__ member-declaration
12510
12511 member-declarator:
12512 declarator attributes [opt] pure-specifier [opt]
12513 declarator attributes [opt] constant-initializer [opt]
12514 identifier [opt] attributes [opt] : constant-expression */
12515
12516 static void
12517 cp_parser_member_declaration (cp_parser* parser)
12518 {
12519 tree decl_specifiers;
12520 tree prefix_attributes;
12521 tree decl;
12522 int declares_class_or_enum;
12523 bool friend_p;
12524 cp_token *token;
12525 int saved_pedantic;
12526
12527 /* Check for the `__extension__' keyword. */
12528 if (cp_parser_extension_opt (parser, &saved_pedantic))
12529 {
12530 /* Recurse. */
12531 cp_parser_member_declaration (parser);
12532 /* Restore the old value of the PEDANTIC flag. */
12533 pedantic = saved_pedantic;
12534
12535 return;
12536 }
12537
12538 /* Check for a template-declaration. */
12539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12540 {
12541 /* Parse the template-declaration. */
12542 cp_parser_template_declaration (parser, /*member_p=*/true);
12543
12544 return;
12545 }
12546
12547 /* Check for a using-declaration. */
12548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12549 {
12550 /* Parse the using-declaration. */
12551 cp_parser_using_declaration (parser);
12552
12553 return;
12554 }
12555
12556 /* Parse the decl-specifier-seq. */
12557 decl_specifiers
12558 = cp_parser_decl_specifier_seq (parser,
12559 CP_PARSER_FLAGS_OPTIONAL,
12560 &prefix_attributes,
12561 &declares_class_or_enum);
12562 /* Check for an invalid type-name. */
12563 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12564 return;
12565 /* If there is no declarator, then the decl-specifier-seq should
12566 specify a type. */
12567 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12568 {
12569 /* If there was no decl-specifier-seq, and the next token is a
12570 `;', then we have something like:
12571
12572 struct S { ; };
12573
12574 [class.mem]
12575
12576 Each member-declaration shall declare at least one member
12577 name of the class. */
12578 if (!decl_specifiers)
12579 {
12580 if (pedantic)
12581 pedwarn ("extra semicolon");
12582 }
12583 else
12584 {
12585 tree type;
12586
12587 /* See if this declaration is a friend. */
12588 friend_p = cp_parser_friend_p (decl_specifiers);
12589 /* If there were decl-specifiers, check to see if there was
12590 a class-declaration. */
12591 type = check_tag_decl (decl_specifiers);
12592 /* Nested classes have already been added to the class, but
12593 a `friend' needs to be explicitly registered. */
12594 if (friend_p)
12595 {
12596 /* If the `friend' keyword was present, the friend must
12597 be introduced with a class-key. */
12598 if (!declares_class_or_enum)
12599 error ("a class-key must be used when declaring a friend");
12600 /* In this case:
12601
12602 template <typename T> struct A {
12603 friend struct A<T>::B;
12604 };
12605
12606 A<T>::B will be represented by a TYPENAME_TYPE, and
12607 therefore not recognized by check_tag_decl. */
12608 if (!type)
12609 {
12610 tree specifier;
12611
12612 for (specifier = decl_specifiers;
12613 specifier;
12614 specifier = TREE_CHAIN (specifier))
12615 {
12616 tree s = TREE_VALUE (specifier);
12617
12618 if (TREE_CODE (s) == IDENTIFIER_NODE)
12619 get_global_value_if_present (s, &type);
12620 if (TREE_CODE (s) == TYPE_DECL)
12621 s = TREE_TYPE (s);
12622 if (TYPE_P (s))
12623 {
12624 type = s;
12625 break;
12626 }
12627 }
12628 }
12629 if (!type || !TYPE_P (type))
12630 error ("friend declaration does not name a class or "
12631 "function");
12632 else
12633 make_friend_class (current_class_type, type,
12634 /*complain=*/true);
12635 }
12636 /* If there is no TYPE, an error message will already have
12637 been issued. */
12638 else if (!type)
12639 ;
12640 /* An anonymous aggregate has to be handled specially; such
12641 a declaration really declares a data member (with a
12642 particular type), as opposed to a nested class. */
12643 else if (ANON_AGGR_TYPE_P (type))
12644 {
12645 /* Remove constructors and such from TYPE, now that we
12646 know it is an anonymous aggregate. */
12647 fixup_anonymous_aggr (type);
12648 /* And make the corresponding data member. */
12649 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12650 /* Add it to the class. */
12651 finish_member_declaration (decl);
12652 }
12653 else
12654 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12655 }
12656 }
12657 else
12658 {
12659 /* See if these declarations will be friends. */
12660 friend_p = cp_parser_friend_p (decl_specifiers);
12661
12662 /* Keep going until we hit the `;' at the end of the
12663 declaration. */
12664 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12665 {
12666 tree attributes = NULL_TREE;
12667 tree first_attribute;
12668
12669 /* Peek at the next token. */
12670 token = cp_lexer_peek_token (parser->lexer);
12671
12672 /* Check for a bitfield declaration. */
12673 if (token->type == CPP_COLON
12674 || (token->type == CPP_NAME
12675 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12676 == CPP_COLON))
12677 {
12678 tree identifier;
12679 tree width;
12680
12681 /* Get the name of the bitfield. Note that we cannot just
12682 check TOKEN here because it may have been invalidated by
12683 the call to cp_lexer_peek_nth_token above. */
12684 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12685 identifier = cp_parser_identifier (parser);
12686 else
12687 identifier = NULL_TREE;
12688
12689 /* Consume the `:' token. */
12690 cp_lexer_consume_token (parser->lexer);
12691 /* Get the width of the bitfield. */
12692 width
12693 = cp_parser_constant_expression (parser,
12694 /*allow_non_constant=*/false,
12695 NULL);
12696
12697 /* Look for attributes that apply to the bitfield. */
12698 attributes = cp_parser_attributes_opt (parser);
12699 /* Remember which attributes are prefix attributes and
12700 which are not. */
12701 first_attribute = attributes;
12702 /* Combine the attributes. */
12703 attributes = chainon (prefix_attributes, attributes);
12704
12705 /* Create the bitfield declaration. */
12706 decl = grokbitfield (identifier,
12707 decl_specifiers,
12708 width);
12709 /* Apply the attributes. */
12710 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12711 }
12712 else
12713 {
12714 tree declarator;
12715 tree initializer;
12716 tree asm_specification;
12717 int ctor_dtor_or_conv_p;
12718
12719 /* Parse the declarator. */
12720 declarator
12721 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12722 &ctor_dtor_or_conv_p,
12723 /*parenthesized_p=*/NULL);
12724
12725 /* If something went wrong parsing the declarator, make sure
12726 that we at least consume some tokens. */
12727 if (declarator == error_mark_node)
12728 {
12729 /* Skip to the end of the statement. */
12730 cp_parser_skip_to_end_of_statement (parser);
12731 /* If the next token is not a semicolon, that is
12732 probably because we just skipped over the body of
12733 a function. So, we consume a semicolon if
12734 present, but do not issue an error message if it
12735 is not present. */
12736 if (cp_lexer_next_token_is (parser->lexer,
12737 CPP_SEMICOLON))
12738 cp_lexer_consume_token (parser->lexer);
12739 return;
12740 }
12741
12742 cp_parser_check_for_definition_in_return_type
12743 (declarator, declares_class_or_enum);
12744
12745 /* Look for an asm-specification. */
12746 asm_specification = cp_parser_asm_specification_opt (parser);
12747 /* Look for attributes that apply to the declaration. */
12748 attributes = cp_parser_attributes_opt (parser);
12749 /* Remember which attributes are prefix attributes and
12750 which are not. */
12751 first_attribute = attributes;
12752 /* Combine the attributes. */
12753 attributes = chainon (prefix_attributes, attributes);
12754
12755 /* If it's an `=', then we have a constant-initializer or a
12756 pure-specifier. It is not correct to parse the
12757 initializer before registering the member declaration
12758 since the member declaration should be in scope while
12759 its initializer is processed. However, the rest of the
12760 front end does not yet provide an interface that allows
12761 us to handle this correctly. */
12762 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12763 {
12764 /* In [class.mem]:
12765
12766 A pure-specifier shall be used only in the declaration of
12767 a virtual function.
12768
12769 A member-declarator can contain a constant-initializer
12770 only if it declares a static member of integral or
12771 enumeration type.
12772
12773 Therefore, if the DECLARATOR is for a function, we look
12774 for a pure-specifier; otherwise, we look for a
12775 constant-initializer. When we call `grokfield', it will
12776 perform more stringent semantics checks. */
12777 if (TREE_CODE (declarator) == CALL_EXPR)
12778 initializer = cp_parser_pure_specifier (parser);
12779 else
12780 /* Parse the initializer. */
12781 initializer = cp_parser_constant_initializer (parser);
12782 }
12783 /* Otherwise, there is no initializer. */
12784 else
12785 initializer = NULL_TREE;
12786
12787 /* See if we are probably looking at a function
12788 definition. We are certainly not looking at at a
12789 member-declarator. Calling `grokfield' has
12790 side-effects, so we must not do it unless we are sure
12791 that we are looking at a member-declarator. */
12792 if (cp_parser_token_starts_function_definition_p
12793 (cp_lexer_peek_token (parser->lexer)))
12794 {
12795 /* The grammar does not allow a pure-specifier to be
12796 used when a member function is defined. (It is
12797 possible that this fact is an oversight in the
12798 standard, since a pure function may be defined
12799 outside of the class-specifier. */
12800 if (initializer)
12801 error ("pure-specifier on function-definition");
12802 decl = cp_parser_save_member_function_body (parser,
12803 decl_specifiers,
12804 declarator,
12805 attributes);
12806 /* If the member was not a friend, declare it here. */
12807 if (!friend_p)
12808 finish_member_declaration (decl);
12809 /* Peek at the next token. */
12810 token = cp_lexer_peek_token (parser->lexer);
12811 /* If the next token is a semicolon, consume it. */
12812 if (token->type == CPP_SEMICOLON)
12813 cp_lexer_consume_token (parser->lexer);
12814 return;
12815 }
12816 else
12817 {
12818 /* Create the declaration. */
12819 decl = grokfield (declarator, decl_specifiers,
12820 initializer, asm_specification,
12821 attributes);
12822 /* Any initialization must have been from a
12823 constant-expression. */
12824 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12825 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12826 }
12827 }
12828
12829 /* Reset PREFIX_ATTRIBUTES. */
12830 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12831 attributes = TREE_CHAIN (attributes);
12832 if (attributes)
12833 TREE_CHAIN (attributes) = NULL_TREE;
12834
12835 /* If there is any qualification still in effect, clear it
12836 now; we will be starting fresh with the next declarator. */
12837 parser->scope = NULL_TREE;
12838 parser->qualifying_scope = NULL_TREE;
12839 parser->object_scope = NULL_TREE;
12840 /* If it's a `,', then there are more declarators. */
12841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12842 cp_lexer_consume_token (parser->lexer);
12843 /* If the next token isn't a `;', then we have a parse error. */
12844 else if (cp_lexer_next_token_is_not (parser->lexer,
12845 CPP_SEMICOLON))
12846 {
12847 cp_parser_error (parser, "expected `;'");
12848 /* Skip tokens until we find a `;'. */
12849 cp_parser_skip_to_end_of_statement (parser);
12850
12851 break;
12852 }
12853
12854 if (decl)
12855 {
12856 /* Add DECL to the list of members. */
12857 if (!friend_p)
12858 finish_member_declaration (decl);
12859
12860 if (TREE_CODE (decl) == FUNCTION_DECL)
12861 cp_parser_save_default_args (parser, decl);
12862 }
12863 }
12864 }
12865
12866 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12867 }
12868
12869 /* Parse a pure-specifier.
12870
12871 pure-specifier:
12872 = 0
12873
12874 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12875 Otherwise, ERROR_MARK_NODE is returned. */
12876
12877 static tree
12878 cp_parser_pure_specifier (cp_parser* parser)
12879 {
12880 cp_token *token;
12881
12882 /* Look for the `=' token. */
12883 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12884 return error_mark_node;
12885 /* Look for the `0' token. */
12886 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12887 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12888 to get information from the lexer about how the number was
12889 spelled in order to fix this problem. */
12890 if (!token || !integer_zerop (token->value))
12891 return error_mark_node;
12892
12893 return integer_zero_node;
12894 }
12895
12896 /* Parse a constant-initializer.
12897
12898 constant-initializer:
12899 = constant-expression
12900
12901 Returns a representation of the constant-expression. */
12902
12903 static tree
12904 cp_parser_constant_initializer (cp_parser* parser)
12905 {
12906 /* Look for the `=' token. */
12907 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12908 return error_mark_node;
12909
12910 /* It is invalid to write:
12911
12912 struct S { static const int i = { 7 }; };
12913
12914 */
12915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12916 {
12917 cp_parser_error (parser,
12918 "a brace-enclosed initializer is not allowed here");
12919 /* Consume the opening brace. */
12920 cp_lexer_consume_token (parser->lexer);
12921 /* Skip the initializer. */
12922 cp_parser_skip_to_closing_brace (parser);
12923 /* Look for the trailing `}'. */
12924 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12925
12926 return error_mark_node;
12927 }
12928
12929 return cp_parser_constant_expression (parser,
12930 /*allow_non_constant=*/false,
12931 NULL);
12932 }
12933
12934 /* Derived classes [gram.class.derived] */
12935
12936 /* Parse a base-clause.
12937
12938 base-clause:
12939 : base-specifier-list
12940
12941 base-specifier-list:
12942 base-specifier
12943 base-specifier-list , base-specifier
12944
12945 Returns a TREE_LIST representing the base-classes, in the order in
12946 which they were declared. The representation of each node is as
12947 described by cp_parser_base_specifier.
12948
12949 In the case that no bases are specified, this function will return
12950 NULL_TREE, not ERROR_MARK_NODE. */
12951
12952 static tree
12953 cp_parser_base_clause (cp_parser* parser)
12954 {
12955 tree bases = NULL_TREE;
12956
12957 /* Look for the `:' that begins the list. */
12958 cp_parser_require (parser, CPP_COLON, "`:'");
12959
12960 /* Scan the base-specifier-list. */
12961 while (true)
12962 {
12963 cp_token *token;
12964 tree base;
12965
12966 /* Look for the base-specifier. */
12967 base = cp_parser_base_specifier (parser);
12968 /* Add BASE to the front of the list. */
12969 if (base != error_mark_node)
12970 {
12971 TREE_CHAIN (base) = bases;
12972 bases = base;
12973 }
12974 /* Peek at the next token. */
12975 token = cp_lexer_peek_token (parser->lexer);
12976 /* If it's not a comma, then the list is complete. */
12977 if (token->type != CPP_COMMA)
12978 break;
12979 /* Consume the `,'. */
12980 cp_lexer_consume_token (parser->lexer);
12981 }
12982
12983 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12984 base class had a qualified name. However, the next name that
12985 appears is certainly not qualified. */
12986 parser->scope = NULL_TREE;
12987 parser->qualifying_scope = NULL_TREE;
12988 parser->object_scope = NULL_TREE;
12989
12990 return nreverse (bases);
12991 }
12992
12993 /* Parse a base-specifier.
12994
12995 base-specifier:
12996 :: [opt] nested-name-specifier [opt] class-name
12997 virtual access-specifier [opt] :: [opt] nested-name-specifier
12998 [opt] class-name
12999 access-specifier virtual [opt] :: [opt] nested-name-specifier
13000 [opt] class-name
13001
13002 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13003 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13004 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13005 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13006
13007 static tree
13008 cp_parser_base_specifier (cp_parser* parser)
13009 {
13010 cp_token *token;
13011 bool done = false;
13012 bool virtual_p = false;
13013 bool duplicate_virtual_error_issued_p = false;
13014 bool duplicate_access_error_issued_p = false;
13015 bool class_scope_p, template_p;
13016 tree access = access_default_node;
13017 tree type;
13018
13019 /* Process the optional `virtual' and `access-specifier'. */
13020 while (!done)
13021 {
13022 /* Peek at the next token. */
13023 token = cp_lexer_peek_token (parser->lexer);
13024 /* Process `virtual'. */
13025 switch (token->keyword)
13026 {
13027 case RID_VIRTUAL:
13028 /* If `virtual' appears more than once, issue an error. */
13029 if (virtual_p && !duplicate_virtual_error_issued_p)
13030 {
13031 cp_parser_error (parser,
13032 "`virtual' specified more than once in base-specified");
13033 duplicate_virtual_error_issued_p = true;
13034 }
13035
13036 virtual_p = true;
13037
13038 /* Consume the `virtual' token. */
13039 cp_lexer_consume_token (parser->lexer);
13040
13041 break;
13042
13043 case RID_PUBLIC:
13044 case RID_PROTECTED:
13045 case RID_PRIVATE:
13046 /* If more than one access specifier appears, issue an
13047 error. */
13048 if (access != access_default_node
13049 && !duplicate_access_error_issued_p)
13050 {
13051 cp_parser_error (parser,
13052 "more than one access specifier in base-specified");
13053 duplicate_access_error_issued_p = true;
13054 }
13055
13056 access = ridpointers[(int) token->keyword];
13057
13058 /* Consume the access-specifier. */
13059 cp_lexer_consume_token (parser->lexer);
13060
13061 break;
13062
13063 default:
13064 done = true;
13065 break;
13066 }
13067 }
13068 /* It is not uncommon to see programs mechanically, erroneously, use
13069 the 'typename' keyword to denote (dependent) qualified types
13070 as base classes. */
13071 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13072 {
13073 if (!processing_template_decl)
13074 error ("keyword `typename' not allowed outside of templates");
13075 else
13076 error ("keyword `typename' not allowed in this context "
13077 "(the base class is implicitly a type)");
13078 cp_lexer_consume_token (parser->lexer);
13079 }
13080
13081 /* Look for the optional `::' operator. */
13082 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13083 /* Look for the nested-name-specifier. The simplest way to
13084 implement:
13085
13086 [temp.res]
13087
13088 The keyword `typename' is not permitted in a base-specifier or
13089 mem-initializer; in these contexts a qualified name that
13090 depends on a template-parameter is implicitly assumed to be a
13091 type name.
13092
13093 is to pretend that we have seen the `typename' keyword at this
13094 point. */
13095 cp_parser_nested_name_specifier_opt (parser,
13096 /*typename_keyword_p=*/true,
13097 /*check_dependency_p=*/true,
13098 /*type_p=*/true,
13099 /*is_declaration=*/true);
13100 /* If the base class is given by a qualified name, assume that names
13101 we see are type names or templates, as appropriate. */
13102 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13103 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13104
13105 /* Finally, look for the class-name. */
13106 type = cp_parser_class_name (parser,
13107 class_scope_p,
13108 template_p,
13109 /*type_p=*/true,
13110 /*check_dependency_p=*/true,
13111 /*class_head_p=*/false,
13112 /*is_declaration=*/true);
13113
13114 if (type == error_mark_node)
13115 return error_mark_node;
13116
13117 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13118 }
13119
13120 /* Exception handling [gram.exception] */
13121
13122 /* Parse an (optional) exception-specification.
13123
13124 exception-specification:
13125 throw ( type-id-list [opt] )
13126
13127 Returns a TREE_LIST representing the exception-specification. The
13128 TREE_VALUE of each node is a type. */
13129
13130 static tree
13131 cp_parser_exception_specification_opt (cp_parser* parser)
13132 {
13133 cp_token *token;
13134 tree type_id_list;
13135
13136 /* Peek at the next token. */
13137 token = cp_lexer_peek_token (parser->lexer);
13138 /* If it's not `throw', then there's no exception-specification. */
13139 if (!cp_parser_is_keyword (token, RID_THROW))
13140 return NULL_TREE;
13141
13142 /* Consume the `throw'. */
13143 cp_lexer_consume_token (parser->lexer);
13144
13145 /* Look for the `('. */
13146 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13147
13148 /* Peek at the next token. */
13149 token = cp_lexer_peek_token (parser->lexer);
13150 /* If it's not a `)', then there is a type-id-list. */
13151 if (token->type != CPP_CLOSE_PAREN)
13152 {
13153 const char *saved_message;
13154
13155 /* Types may not be defined in an exception-specification. */
13156 saved_message = parser->type_definition_forbidden_message;
13157 parser->type_definition_forbidden_message
13158 = "types may not be defined in an exception-specification";
13159 /* Parse the type-id-list. */
13160 type_id_list = cp_parser_type_id_list (parser);
13161 /* Restore the saved message. */
13162 parser->type_definition_forbidden_message = saved_message;
13163 }
13164 else
13165 type_id_list = empty_except_spec;
13166
13167 /* Look for the `)'. */
13168 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13169
13170 return type_id_list;
13171 }
13172
13173 /* Parse an (optional) type-id-list.
13174
13175 type-id-list:
13176 type-id
13177 type-id-list , type-id
13178
13179 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13180 in the order that the types were presented. */
13181
13182 static tree
13183 cp_parser_type_id_list (cp_parser* parser)
13184 {
13185 tree types = NULL_TREE;
13186
13187 while (true)
13188 {
13189 cp_token *token;
13190 tree type;
13191
13192 /* Get the next type-id. */
13193 type = cp_parser_type_id (parser);
13194 /* Add it to the list. */
13195 types = add_exception_specifier (types, type, /*complain=*/1);
13196 /* Peek at the next token. */
13197 token = cp_lexer_peek_token (parser->lexer);
13198 /* If it is not a `,', we are done. */
13199 if (token->type != CPP_COMMA)
13200 break;
13201 /* Consume the `,'. */
13202 cp_lexer_consume_token (parser->lexer);
13203 }
13204
13205 return nreverse (types);
13206 }
13207
13208 /* Parse a try-block.
13209
13210 try-block:
13211 try compound-statement handler-seq */
13212
13213 static tree
13214 cp_parser_try_block (cp_parser* parser)
13215 {
13216 tree try_block;
13217
13218 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13219 try_block = begin_try_block ();
13220 cp_parser_compound_statement (parser, false);
13221 finish_try_block (try_block);
13222 cp_parser_handler_seq (parser);
13223 finish_handler_sequence (try_block);
13224
13225 return try_block;
13226 }
13227
13228 /* Parse a function-try-block.
13229
13230 function-try-block:
13231 try ctor-initializer [opt] function-body handler-seq */
13232
13233 static bool
13234 cp_parser_function_try_block (cp_parser* parser)
13235 {
13236 tree try_block;
13237 bool ctor_initializer_p;
13238
13239 /* Look for the `try' keyword. */
13240 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13241 return false;
13242 /* Let the rest of the front-end know where we are. */
13243 try_block = begin_function_try_block ();
13244 /* Parse the function-body. */
13245 ctor_initializer_p
13246 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13247 /* We're done with the `try' part. */
13248 finish_function_try_block (try_block);
13249 /* Parse the handlers. */
13250 cp_parser_handler_seq (parser);
13251 /* We're done with the handlers. */
13252 finish_function_handler_sequence (try_block);
13253
13254 return ctor_initializer_p;
13255 }
13256
13257 /* Parse a handler-seq.
13258
13259 handler-seq:
13260 handler handler-seq [opt] */
13261
13262 static void
13263 cp_parser_handler_seq (cp_parser* parser)
13264 {
13265 while (true)
13266 {
13267 cp_token *token;
13268
13269 /* Parse the handler. */
13270 cp_parser_handler (parser);
13271 /* Peek at the next token. */
13272 token = cp_lexer_peek_token (parser->lexer);
13273 /* If it's not `catch' then there are no more handlers. */
13274 if (!cp_parser_is_keyword (token, RID_CATCH))
13275 break;
13276 }
13277 }
13278
13279 /* Parse a handler.
13280
13281 handler:
13282 catch ( exception-declaration ) compound-statement */
13283
13284 static void
13285 cp_parser_handler (cp_parser* parser)
13286 {
13287 tree handler;
13288 tree declaration;
13289
13290 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13291 handler = begin_handler ();
13292 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13293 declaration = cp_parser_exception_declaration (parser);
13294 finish_handler_parms (declaration, handler);
13295 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13296 cp_parser_compound_statement (parser, false);
13297 finish_handler (handler);
13298 }
13299
13300 /* Parse an exception-declaration.
13301
13302 exception-declaration:
13303 type-specifier-seq declarator
13304 type-specifier-seq abstract-declarator
13305 type-specifier-seq
13306 ...
13307
13308 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13309 ellipsis variant is used. */
13310
13311 static tree
13312 cp_parser_exception_declaration (cp_parser* parser)
13313 {
13314 tree type_specifiers;
13315 tree declarator;
13316 const char *saved_message;
13317
13318 /* If it's an ellipsis, it's easy to handle. */
13319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13320 {
13321 /* Consume the `...' token. */
13322 cp_lexer_consume_token (parser->lexer);
13323 return NULL_TREE;
13324 }
13325
13326 /* Types may not be defined in exception-declarations. */
13327 saved_message = parser->type_definition_forbidden_message;
13328 parser->type_definition_forbidden_message
13329 = "types may not be defined in exception-declarations";
13330
13331 /* Parse the type-specifier-seq. */
13332 type_specifiers = cp_parser_type_specifier_seq (parser);
13333 /* If it's a `)', then there is no declarator. */
13334 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13335 declarator = NULL_TREE;
13336 else
13337 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13338 /*ctor_dtor_or_conv_p=*/NULL,
13339 /*parenthesized_p=*/NULL);
13340
13341 /* Restore the saved message. */
13342 parser->type_definition_forbidden_message = saved_message;
13343
13344 return start_handler_parms (type_specifiers, declarator);
13345 }
13346
13347 /* Parse a throw-expression.
13348
13349 throw-expression:
13350 throw assignment-expression [opt]
13351
13352 Returns a THROW_EXPR representing the throw-expression. */
13353
13354 static tree
13355 cp_parser_throw_expression (cp_parser* parser)
13356 {
13357 tree expression;
13358 cp_token* token;
13359
13360 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13361 token = cp_lexer_peek_token (parser->lexer);
13362 /* Figure out whether or not there is an assignment-expression
13363 following the "throw" keyword. */
13364 if (token->type == CPP_COMMA
13365 || token->type == CPP_SEMICOLON
13366 || token->type == CPP_CLOSE_PAREN
13367 || token->type == CPP_CLOSE_SQUARE
13368 || token->type == CPP_CLOSE_BRACE
13369 || token->type == CPP_COLON)
13370 expression = NULL_TREE;
13371 else
13372 expression = cp_parser_assignment_expression (parser);
13373
13374 return build_throw (expression);
13375 }
13376
13377 /* GNU Extensions */
13378
13379 /* Parse an (optional) asm-specification.
13380
13381 asm-specification:
13382 asm ( string-literal )
13383
13384 If the asm-specification is present, returns a STRING_CST
13385 corresponding to the string-literal. Otherwise, returns
13386 NULL_TREE. */
13387
13388 static tree
13389 cp_parser_asm_specification_opt (cp_parser* parser)
13390 {
13391 cp_token *token;
13392 tree asm_specification;
13393
13394 /* Peek at the next token. */
13395 token = cp_lexer_peek_token (parser->lexer);
13396 /* If the next token isn't the `asm' keyword, then there's no
13397 asm-specification. */
13398 if (!cp_parser_is_keyword (token, RID_ASM))
13399 return NULL_TREE;
13400
13401 /* Consume the `asm' token. */
13402 cp_lexer_consume_token (parser->lexer);
13403 /* Look for the `('. */
13404 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13405
13406 /* Look for the string-literal. */
13407 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13408 if (token)
13409 asm_specification = token->value;
13410 else
13411 asm_specification = NULL_TREE;
13412
13413 /* Look for the `)'. */
13414 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13415
13416 return asm_specification;
13417 }
13418
13419 /* Parse an asm-operand-list.
13420
13421 asm-operand-list:
13422 asm-operand
13423 asm-operand-list , asm-operand
13424
13425 asm-operand:
13426 string-literal ( expression )
13427 [ string-literal ] string-literal ( expression )
13428
13429 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13430 each node is the expression. The TREE_PURPOSE is itself a
13431 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13432 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13433 is a STRING_CST for the string literal before the parenthesis. */
13434
13435 static tree
13436 cp_parser_asm_operand_list (cp_parser* parser)
13437 {
13438 tree asm_operands = NULL_TREE;
13439
13440 while (true)
13441 {
13442 tree string_literal;
13443 tree expression;
13444 tree name;
13445 cp_token *token;
13446
13447 c_lex_string_translate = false;
13448
13449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13450 {
13451 /* Consume the `[' token. */
13452 cp_lexer_consume_token (parser->lexer);
13453 /* Read the operand name. */
13454 name = cp_parser_identifier (parser);
13455 if (name != error_mark_node)
13456 name = build_string (IDENTIFIER_LENGTH (name),
13457 IDENTIFIER_POINTER (name));
13458 /* Look for the closing `]'. */
13459 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13460 }
13461 else
13462 name = NULL_TREE;
13463 /* Look for the string-literal. */
13464 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13465 string_literal = token ? token->value : error_mark_node;
13466 c_lex_string_translate = true;
13467 /* Look for the `('. */
13468 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13469 /* Parse the expression. */
13470 expression = cp_parser_expression (parser);
13471 /* Look for the `)'. */
13472 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13473 c_lex_string_translate = false;
13474 /* Add this operand to the list. */
13475 asm_operands = tree_cons (build_tree_list (name, string_literal),
13476 expression,
13477 asm_operands);
13478 /* If the next token is not a `,', there are no more
13479 operands. */
13480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13481 break;
13482 /* Consume the `,'. */
13483 cp_lexer_consume_token (parser->lexer);
13484 }
13485
13486 return nreverse (asm_operands);
13487 }
13488
13489 /* Parse an asm-clobber-list.
13490
13491 asm-clobber-list:
13492 string-literal
13493 asm-clobber-list , string-literal
13494
13495 Returns a TREE_LIST, indicating the clobbers in the order that they
13496 appeared. The TREE_VALUE of each node is a STRING_CST. */
13497
13498 static tree
13499 cp_parser_asm_clobber_list (cp_parser* parser)
13500 {
13501 tree clobbers = NULL_TREE;
13502
13503 while (true)
13504 {
13505 cp_token *token;
13506 tree string_literal;
13507
13508 /* Look for the string literal. */
13509 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13510 string_literal = token ? token->value : error_mark_node;
13511 /* Add it to the list. */
13512 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13513 /* If the next token is not a `,', then the list is
13514 complete. */
13515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13516 break;
13517 /* Consume the `,' token. */
13518 cp_lexer_consume_token (parser->lexer);
13519 }
13520
13521 return clobbers;
13522 }
13523
13524 /* Parse an (optional) series of attributes.
13525
13526 attributes:
13527 attributes attribute
13528
13529 attribute:
13530 __attribute__ (( attribute-list [opt] ))
13531
13532 The return value is as for cp_parser_attribute_list. */
13533
13534 static tree
13535 cp_parser_attributes_opt (cp_parser* parser)
13536 {
13537 tree attributes = NULL_TREE;
13538
13539 while (true)
13540 {
13541 cp_token *token;
13542 tree attribute_list;
13543
13544 /* Peek at the next token. */
13545 token = cp_lexer_peek_token (parser->lexer);
13546 /* If it's not `__attribute__', then we're done. */
13547 if (token->keyword != RID_ATTRIBUTE)
13548 break;
13549
13550 /* Consume the `__attribute__' keyword. */
13551 cp_lexer_consume_token (parser->lexer);
13552 /* Look for the two `(' tokens. */
13553 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13554 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13555
13556 /* Peek at the next token. */
13557 token = cp_lexer_peek_token (parser->lexer);
13558 if (token->type != CPP_CLOSE_PAREN)
13559 /* Parse the attribute-list. */
13560 attribute_list = cp_parser_attribute_list (parser);
13561 else
13562 /* If the next token is a `)', then there is no attribute
13563 list. */
13564 attribute_list = NULL;
13565
13566 /* Look for the two `)' tokens. */
13567 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13568 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13569
13570 /* Add these new attributes to the list. */
13571 attributes = chainon (attributes, attribute_list);
13572 }
13573
13574 return attributes;
13575 }
13576
13577 /* Parse an attribute-list.
13578
13579 attribute-list:
13580 attribute
13581 attribute-list , attribute
13582
13583 attribute:
13584 identifier
13585 identifier ( identifier )
13586 identifier ( identifier , expression-list )
13587 identifier ( expression-list )
13588
13589 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13590 TREE_PURPOSE of each node is the identifier indicating which
13591 attribute is in use. The TREE_VALUE represents the arguments, if
13592 any. */
13593
13594 static tree
13595 cp_parser_attribute_list (cp_parser* parser)
13596 {
13597 tree attribute_list = NULL_TREE;
13598
13599 c_lex_string_translate = false;
13600 while (true)
13601 {
13602 cp_token *token;
13603 tree identifier;
13604 tree attribute;
13605
13606 /* Look for the identifier. We also allow keywords here; for
13607 example `__attribute__ ((const))' is legal. */
13608 token = cp_lexer_peek_token (parser->lexer);
13609 if (token->type != CPP_NAME
13610 && token->type != CPP_KEYWORD)
13611 return error_mark_node;
13612 /* Consume the token. */
13613 token = cp_lexer_consume_token (parser->lexer);
13614
13615 /* Save away the identifier that indicates which attribute this is. */
13616 identifier = token->value;
13617 attribute = build_tree_list (identifier, NULL_TREE);
13618
13619 /* Peek at the next token. */
13620 token = cp_lexer_peek_token (parser->lexer);
13621 /* If it's an `(', then parse the attribute arguments. */
13622 if (token->type == CPP_OPEN_PAREN)
13623 {
13624 tree arguments;
13625
13626 arguments = (cp_parser_parenthesized_expression_list
13627 (parser, true, /*non_constant_p=*/NULL));
13628 /* Save the identifier and arguments away. */
13629 TREE_VALUE (attribute) = arguments;
13630 }
13631
13632 /* Add this attribute to the list. */
13633 TREE_CHAIN (attribute) = attribute_list;
13634 attribute_list = attribute;
13635
13636 /* Now, look for more attributes. */
13637 token = cp_lexer_peek_token (parser->lexer);
13638 /* If the next token isn't a `,', we're done. */
13639 if (token->type != CPP_COMMA)
13640 break;
13641
13642 /* Consume the comma and keep going. */
13643 cp_lexer_consume_token (parser->lexer);
13644 }
13645 c_lex_string_translate = true;
13646
13647 /* We built up the list in reverse order. */
13648 return nreverse (attribute_list);
13649 }
13650
13651 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13652 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13653 current value of the PEDANTIC flag, regardless of whether or not
13654 the `__extension__' keyword is present. The caller is responsible
13655 for restoring the value of the PEDANTIC flag. */
13656
13657 static bool
13658 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13659 {
13660 /* Save the old value of the PEDANTIC flag. */
13661 *saved_pedantic = pedantic;
13662
13663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13664 {
13665 /* Consume the `__extension__' token. */
13666 cp_lexer_consume_token (parser->lexer);
13667 /* We're not being pedantic while the `__extension__' keyword is
13668 in effect. */
13669 pedantic = 0;
13670
13671 return true;
13672 }
13673
13674 return false;
13675 }
13676
13677 /* Parse a label declaration.
13678
13679 label-declaration:
13680 __label__ label-declarator-seq ;
13681
13682 label-declarator-seq:
13683 identifier , label-declarator-seq
13684 identifier */
13685
13686 static void
13687 cp_parser_label_declaration (cp_parser* parser)
13688 {
13689 /* Look for the `__label__' keyword. */
13690 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13691
13692 while (true)
13693 {
13694 tree identifier;
13695
13696 /* Look for an identifier. */
13697 identifier = cp_parser_identifier (parser);
13698 /* Declare it as a lobel. */
13699 finish_label_decl (identifier);
13700 /* If the next token is a `;', stop. */
13701 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13702 break;
13703 /* Look for the `,' separating the label declarations. */
13704 cp_parser_require (parser, CPP_COMMA, "`,'");
13705 }
13706
13707 /* Look for the final `;'. */
13708 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13709 }
13710
13711 /* Support Functions */
13712
13713 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13714 NAME should have one of the representations used for an
13715 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13716 is returned. If PARSER->SCOPE is a dependent type, then a
13717 SCOPE_REF is returned.
13718
13719 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13720 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13721 was formed. Abstractly, such entities should not be passed to this
13722 function, because they do not need to be looked up, but it is
13723 simpler to check for this special case here, rather than at the
13724 call-sites.
13725
13726 In cases not explicitly covered above, this function returns a
13727 DECL, OVERLOAD, or baselink representing the result of the lookup.
13728 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13729 is returned.
13730
13731 If IS_TYPE is TRUE, bindings that do not refer to types are
13732 ignored.
13733
13734 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13735 ignored.
13736
13737 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13738 are ignored.
13739
13740 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13741 types. */
13742
13743 static tree
13744 cp_parser_lookup_name (cp_parser *parser, tree name,
13745 bool is_type, bool is_template, bool is_namespace,
13746 bool check_dependency)
13747 {
13748 tree decl;
13749 tree object_type = parser->context->object_type;
13750
13751 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13752 no longer valid. Note that if we are parsing tentatively, and
13753 the parse fails, OBJECT_TYPE will be automatically restored. */
13754 parser->context->object_type = NULL_TREE;
13755
13756 if (name == error_mark_node)
13757 return error_mark_node;
13758
13759 /* A template-id has already been resolved; there is no lookup to
13760 do. */
13761 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13762 return name;
13763 if (BASELINK_P (name))
13764 {
13765 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13766 == TEMPLATE_ID_EXPR),
13767 20020909);
13768 return name;
13769 }
13770
13771 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13772 it should already have been checked to make sure that the name
13773 used matches the type being destroyed. */
13774 if (TREE_CODE (name) == BIT_NOT_EXPR)
13775 {
13776 tree type;
13777
13778 /* Figure out to which type this destructor applies. */
13779 if (parser->scope)
13780 type = parser->scope;
13781 else if (object_type)
13782 type = object_type;
13783 else
13784 type = current_class_type;
13785 /* If that's not a class type, there is no destructor. */
13786 if (!type || !CLASS_TYPE_P (type))
13787 return error_mark_node;
13788 if (!CLASSTYPE_DESTRUCTORS (type))
13789 return error_mark_node;
13790 /* If it was a class type, return the destructor. */
13791 return CLASSTYPE_DESTRUCTORS (type);
13792 }
13793
13794 /* By this point, the NAME should be an ordinary identifier. If
13795 the id-expression was a qualified name, the qualifying scope is
13796 stored in PARSER->SCOPE at this point. */
13797 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13798 20000619);
13799
13800 /* Perform the lookup. */
13801 if (parser->scope)
13802 {
13803 bool dependent_p;
13804
13805 if (parser->scope == error_mark_node)
13806 return error_mark_node;
13807
13808 /* If the SCOPE is dependent, the lookup must be deferred until
13809 the template is instantiated -- unless we are explicitly
13810 looking up names in uninstantiated templates. Even then, we
13811 cannot look up the name if the scope is not a class type; it
13812 might, for example, be a template type parameter. */
13813 dependent_p = (TYPE_P (parser->scope)
13814 && !(parser->in_declarator_p
13815 && currently_open_class (parser->scope))
13816 && dependent_type_p (parser->scope));
13817 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13818 && dependent_p)
13819 {
13820 if (is_type)
13821 /* The resolution to Core Issue 180 says that `struct A::B'
13822 should be considered a type-name, even if `A' is
13823 dependent. */
13824 decl = TYPE_NAME (make_typename_type (parser->scope,
13825 name,
13826 /*complain=*/1));
13827 else if (is_template)
13828 decl = make_unbound_class_template (parser->scope,
13829 name,
13830 /*complain=*/1);
13831 else
13832 decl = build_nt (SCOPE_REF, parser->scope, name);
13833 }
13834 else
13835 {
13836 bool pop_p = false;
13837
13838 /* If PARSER->SCOPE is a dependent type, then it must be a
13839 class type, and we must not be checking dependencies;
13840 otherwise, we would have processed this lookup above. So
13841 that PARSER->SCOPE is not considered a dependent base by
13842 lookup_member, we must enter the scope here. */
13843 if (dependent_p)
13844 pop_p = push_scope (parser->scope);
13845 /* If the PARSER->SCOPE is a a template specialization, it
13846 may be instantiated during name lookup. In that case,
13847 errors may be issued. Even if we rollback the current
13848 tentative parse, those errors are valid. */
13849 decl = lookup_qualified_name (parser->scope, name, is_type,
13850 /*complain=*/true);
13851 if (pop_p)
13852 pop_scope (parser->scope);
13853 }
13854 parser->qualifying_scope = parser->scope;
13855 parser->object_scope = NULL_TREE;
13856 }
13857 else if (object_type)
13858 {
13859 tree object_decl = NULL_TREE;
13860 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13861 OBJECT_TYPE is not a class. */
13862 if (CLASS_TYPE_P (object_type))
13863 /* If the OBJECT_TYPE is a template specialization, it may
13864 be instantiated during name lookup. In that case, errors
13865 may be issued. Even if we rollback the current tentative
13866 parse, those errors are valid. */
13867 object_decl = lookup_member (object_type,
13868 name,
13869 /*protect=*/0, is_type);
13870 /* Look it up in the enclosing context, too. */
13871 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13872 is_namespace,
13873 /*flags=*/0);
13874 parser->object_scope = object_type;
13875 parser->qualifying_scope = NULL_TREE;
13876 if (object_decl)
13877 decl = object_decl;
13878 }
13879 else
13880 {
13881 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13882 is_namespace,
13883 /*flags=*/0);
13884 parser->qualifying_scope = NULL_TREE;
13885 parser->object_scope = NULL_TREE;
13886 }
13887
13888 /* If the lookup failed, let our caller know. */
13889 if (!decl
13890 || decl == error_mark_node
13891 || (TREE_CODE (decl) == FUNCTION_DECL
13892 && DECL_ANTICIPATED (decl)))
13893 return error_mark_node;
13894
13895 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13896 if (TREE_CODE (decl) == TREE_LIST)
13897 {
13898 /* The error message we have to print is too complicated for
13899 cp_parser_error, so we incorporate its actions directly. */
13900 if (!cp_parser_simulate_error (parser))
13901 {
13902 error ("reference to `%D' is ambiguous", name);
13903 print_candidates (decl);
13904 }
13905 return error_mark_node;
13906 }
13907
13908 my_friendly_assert (DECL_P (decl)
13909 || TREE_CODE (decl) == OVERLOAD
13910 || TREE_CODE (decl) == SCOPE_REF
13911 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13912 || BASELINK_P (decl),
13913 20000619);
13914
13915 /* If we have resolved the name of a member declaration, check to
13916 see if the declaration is accessible. When the name resolves to
13917 set of overloaded functions, accessibility is checked when
13918 overload resolution is done.
13919
13920 During an explicit instantiation, access is not checked at all,
13921 as per [temp.explicit]. */
13922 if (DECL_P (decl))
13923 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13924
13925 return decl;
13926 }
13927
13928 /* Like cp_parser_lookup_name, but for use in the typical case where
13929 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13930 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13931
13932 static tree
13933 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13934 {
13935 return cp_parser_lookup_name (parser, name,
13936 /*is_type=*/false,
13937 /*is_template=*/false,
13938 /*is_namespace=*/false,
13939 /*check_dependency=*/true);
13940 }
13941
13942 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13943 the current context, return the TYPE_DECL. If TAG_NAME_P is
13944 true, the DECL indicates the class being defined in a class-head,
13945 or declared in an elaborated-type-specifier.
13946
13947 Otherwise, return DECL. */
13948
13949 static tree
13950 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13951 {
13952 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13953 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13954
13955 struct A {
13956 template <typename T> struct B;
13957 };
13958
13959 template <typename T> struct A::B {};
13960
13961 Similarly, in a elaborated-type-specifier:
13962
13963 namespace N { struct X{}; }
13964
13965 struct A {
13966 template <typename T> friend struct N::X;
13967 };
13968
13969 However, if the DECL refers to a class type, and we are in
13970 the scope of the class, then the name lookup automatically
13971 finds the TYPE_DECL created by build_self_reference rather
13972 than a TEMPLATE_DECL. For example, in:
13973
13974 template <class T> struct S {
13975 S s;
13976 };
13977
13978 there is no need to handle such case. */
13979
13980 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13981 return DECL_TEMPLATE_RESULT (decl);
13982
13983 return decl;
13984 }
13985
13986 /* If too many, or too few, template-parameter lists apply to the
13987 declarator, issue an error message. Returns TRUE if all went well,
13988 and FALSE otherwise. */
13989
13990 static bool
13991 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13992 tree declarator)
13993 {
13994 unsigned num_templates;
13995
13996 /* We haven't seen any classes that involve template parameters yet. */
13997 num_templates = 0;
13998
13999 switch (TREE_CODE (declarator))
14000 {
14001 case CALL_EXPR:
14002 case ARRAY_REF:
14003 case INDIRECT_REF:
14004 case ADDR_EXPR:
14005 {
14006 tree main_declarator = TREE_OPERAND (declarator, 0);
14007 return
14008 cp_parser_check_declarator_template_parameters (parser,
14009 main_declarator);
14010 }
14011
14012 case SCOPE_REF:
14013 {
14014 tree scope;
14015 tree member;
14016
14017 scope = TREE_OPERAND (declarator, 0);
14018 member = TREE_OPERAND (declarator, 1);
14019
14020 /* If this is a pointer-to-member, then we are not interested
14021 in the SCOPE, because it does not qualify the thing that is
14022 being declared. */
14023 if (TREE_CODE (member) == INDIRECT_REF)
14024 return (cp_parser_check_declarator_template_parameters
14025 (parser, member));
14026
14027 while (scope && CLASS_TYPE_P (scope))
14028 {
14029 /* You're supposed to have one `template <...>'
14030 for every template class, but you don't need one
14031 for a full specialization. For example:
14032
14033 template <class T> struct S{};
14034 template <> struct S<int> { void f(); };
14035 void S<int>::f () {}
14036
14037 is correct; there shouldn't be a `template <>' for
14038 the definition of `S<int>::f'. */
14039 if (CLASSTYPE_TEMPLATE_INFO (scope)
14040 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14041 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14042 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14043 ++num_templates;
14044
14045 scope = TYPE_CONTEXT (scope);
14046 }
14047 }
14048
14049 /* Fall through. */
14050
14051 default:
14052 /* If the DECLARATOR has the form `X<y>' then it uses one
14053 additional level of template parameters. */
14054 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
14055 ++num_templates;
14056
14057 return cp_parser_check_template_parameters (parser,
14058 num_templates);
14059 }
14060 }
14061
14062 /* NUM_TEMPLATES were used in the current declaration. If that is
14063 invalid, return FALSE and issue an error messages. Otherwise,
14064 return TRUE. */
14065
14066 static bool
14067 cp_parser_check_template_parameters (cp_parser* parser,
14068 unsigned num_templates)
14069 {
14070 /* If there are more template classes than parameter lists, we have
14071 something like:
14072
14073 template <class T> void S<T>::R<T>::f (); */
14074 if (parser->num_template_parameter_lists < num_templates)
14075 {
14076 error ("too few template-parameter-lists");
14077 return false;
14078 }
14079 /* If there are the same number of template classes and parameter
14080 lists, that's OK. */
14081 if (parser->num_template_parameter_lists == num_templates)
14082 return true;
14083 /* If there are more, but only one more, then we are referring to a
14084 member template. That's OK too. */
14085 if (parser->num_template_parameter_lists == num_templates + 1)
14086 return true;
14087 /* Otherwise, there are too many template parameter lists. We have
14088 something like:
14089
14090 template <class T> template <class U> void S::f(); */
14091 error ("too many template-parameter-lists");
14092 return false;
14093 }
14094
14095 /* Parse a binary-expression of the general form:
14096
14097 binary-expression:
14098 <expr>
14099 binary-expression <token> <expr>
14100
14101 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14102 to parser the <expr>s. If the first production is used, then the
14103 value returned by FN is returned directly. Otherwise, a node with
14104 the indicated EXPR_TYPE is returned, with operands corresponding to
14105 the two sub-expressions. */
14106
14107 static tree
14108 cp_parser_binary_expression (cp_parser* parser,
14109 const cp_parser_token_tree_map token_tree_map,
14110 cp_parser_expression_fn fn)
14111 {
14112 tree lhs;
14113
14114 /* Parse the first expression. */
14115 lhs = (*fn) (parser);
14116 /* Now, look for more expressions. */
14117 while (true)
14118 {
14119 cp_token *token;
14120 const cp_parser_token_tree_map_node *map_node;
14121 tree rhs;
14122
14123 /* Peek at the next token. */
14124 token = cp_lexer_peek_token (parser->lexer);
14125 /* If the token is `>', and that's not an operator at the
14126 moment, then we're done. */
14127 if (token->type == CPP_GREATER
14128 && !parser->greater_than_is_operator_p)
14129 break;
14130 /* If we find one of the tokens we want, build the corresponding
14131 tree representation. */
14132 for (map_node = token_tree_map;
14133 map_node->token_type != CPP_EOF;
14134 ++map_node)
14135 if (map_node->token_type == token->type)
14136 {
14137 /* Assume that an overloaded operator will not be used. */
14138 bool overloaded_p = false;
14139
14140 /* Consume the operator token. */
14141 cp_lexer_consume_token (parser->lexer);
14142 /* Parse the right-hand side of the expression. */
14143 rhs = (*fn) (parser);
14144 /* Build the binary tree node. */
14145 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14146 &overloaded_p);
14147 /* If the binary operator required the use of an
14148 overloaded operator, then this expression cannot be an
14149 integral constant-expression. An overloaded operator
14150 can be used even if both operands are otherwise
14151 permissible in an integral constant-expression if at
14152 least one of the operands is of enumeration type. */
14153 if (overloaded_p
14154 && (cp_parser_non_integral_constant_expression
14155 (parser, "calls to overloaded operators")))
14156 lhs = error_mark_node;
14157 break;
14158 }
14159
14160 /* If the token wasn't one of the ones we want, we're done. */
14161 if (map_node->token_type == CPP_EOF)
14162 break;
14163 }
14164
14165 return lhs;
14166 }
14167
14168 /* Parse an optional `::' token indicating that the following name is
14169 from the global namespace. If so, PARSER->SCOPE is set to the
14170 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14171 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14172 Returns the new value of PARSER->SCOPE, if the `::' token is
14173 present, and NULL_TREE otherwise. */
14174
14175 static tree
14176 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14177 {
14178 cp_token *token;
14179
14180 /* Peek at the next token. */
14181 token = cp_lexer_peek_token (parser->lexer);
14182 /* If we're looking at a `::' token then we're starting from the
14183 global namespace, not our current location. */
14184 if (token->type == CPP_SCOPE)
14185 {
14186 /* Consume the `::' token. */
14187 cp_lexer_consume_token (parser->lexer);
14188 /* Set the SCOPE so that we know where to start the lookup. */
14189 parser->scope = global_namespace;
14190 parser->qualifying_scope = global_namespace;
14191 parser->object_scope = NULL_TREE;
14192
14193 return parser->scope;
14194 }
14195 else if (!current_scope_valid_p)
14196 {
14197 parser->scope = NULL_TREE;
14198 parser->qualifying_scope = NULL_TREE;
14199 parser->object_scope = NULL_TREE;
14200 }
14201
14202 return NULL_TREE;
14203 }
14204
14205 /* Returns TRUE if the upcoming token sequence is the start of a
14206 constructor declarator. If FRIEND_P is true, the declarator is
14207 preceded by the `friend' specifier. */
14208
14209 static bool
14210 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14211 {
14212 bool constructor_p;
14213 tree type_decl = NULL_TREE;
14214 bool nested_name_p;
14215 cp_token *next_token;
14216
14217 /* The common case is that this is not a constructor declarator, so
14218 try to avoid doing lots of work if at all possible. It's not
14219 valid declare a constructor at function scope. */
14220 if (at_function_scope_p ())
14221 return false;
14222 /* And only certain tokens can begin a constructor declarator. */
14223 next_token = cp_lexer_peek_token (parser->lexer);
14224 if (next_token->type != CPP_NAME
14225 && next_token->type != CPP_SCOPE
14226 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14227 && next_token->type != CPP_TEMPLATE_ID)
14228 return false;
14229
14230 /* Parse tentatively; we are going to roll back all of the tokens
14231 consumed here. */
14232 cp_parser_parse_tentatively (parser);
14233 /* Assume that we are looking at a constructor declarator. */
14234 constructor_p = true;
14235
14236 /* Look for the optional `::' operator. */
14237 cp_parser_global_scope_opt (parser,
14238 /*current_scope_valid_p=*/false);
14239 /* Look for the nested-name-specifier. */
14240 nested_name_p
14241 = (cp_parser_nested_name_specifier_opt (parser,
14242 /*typename_keyword_p=*/false,
14243 /*check_dependency_p=*/false,
14244 /*type_p=*/false,
14245 /*is_declaration=*/false)
14246 != NULL_TREE);
14247 /* Outside of a class-specifier, there must be a
14248 nested-name-specifier. */
14249 if (!nested_name_p &&
14250 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14251 || friend_p))
14252 constructor_p = false;
14253 /* If we still think that this might be a constructor-declarator,
14254 look for a class-name. */
14255 if (constructor_p)
14256 {
14257 /* If we have:
14258
14259 template <typename T> struct S { S(); };
14260 template <typename T> S<T>::S ();
14261
14262 we must recognize that the nested `S' names a class.
14263 Similarly, for:
14264
14265 template <typename T> S<T>::S<T> ();
14266
14267 we must recognize that the nested `S' names a template. */
14268 type_decl = cp_parser_class_name (parser,
14269 /*typename_keyword_p=*/false,
14270 /*template_keyword_p=*/false,
14271 /*type_p=*/false,
14272 /*check_dependency_p=*/false,
14273 /*class_head_p=*/false,
14274 /*is_declaration=*/false);
14275 /* If there was no class-name, then this is not a constructor. */
14276 constructor_p = !cp_parser_error_occurred (parser);
14277 }
14278
14279 /* If we're still considering a constructor, we have to see a `(',
14280 to begin the parameter-declaration-clause, followed by either a
14281 `)', an `...', or a decl-specifier. We need to check for a
14282 type-specifier to avoid being fooled into thinking that:
14283
14284 S::S (f) (int);
14285
14286 is a constructor. (It is actually a function named `f' that
14287 takes one parameter (of type `int') and returns a value of type
14288 `S::S'. */
14289 if (constructor_p
14290 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14291 {
14292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14293 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14294 /* A parameter declaration begins with a decl-specifier,
14295 which is either the "attribute" keyword, a storage class
14296 specifier, or (usually) a type-specifier. */
14297 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14298 && !cp_parser_storage_class_specifier_opt (parser))
14299 {
14300 tree type;
14301 bool pop_p = false;
14302 unsigned saved_num_template_parameter_lists;
14303
14304 /* Names appearing in the type-specifier should be looked up
14305 in the scope of the class. */
14306 if (current_class_type)
14307 type = NULL_TREE;
14308 else
14309 {
14310 type = TREE_TYPE (type_decl);
14311 if (TREE_CODE (type) == TYPENAME_TYPE)
14312 {
14313 type = resolve_typename_type (type,
14314 /*only_current_p=*/false);
14315 if (type == error_mark_node)
14316 {
14317 cp_parser_abort_tentative_parse (parser);
14318 return false;
14319 }
14320 }
14321 pop_p = push_scope (type);
14322 }
14323
14324 /* Inside the constructor parameter list, surrounding
14325 template-parameter-lists do not apply. */
14326 saved_num_template_parameter_lists
14327 = parser->num_template_parameter_lists;
14328 parser->num_template_parameter_lists = 0;
14329
14330 /* Look for the type-specifier. */
14331 cp_parser_type_specifier (parser,
14332 CP_PARSER_FLAGS_NONE,
14333 /*is_friend=*/false,
14334 /*is_declarator=*/true,
14335 /*declares_class_or_enum=*/NULL,
14336 /*is_cv_qualifier=*/NULL);
14337
14338 parser->num_template_parameter_lists
14339 = saved_num_template_parameter_lists;
14340
14341 /* Leave the scope of the class. */
14342 if (pop_p)
14343 pop_scope (type);
14344
14345 constructor_p = !cp_parser_error_occurred (parser);
14346 }
14347 }
14348 else
14349 constructor_p = false;
14350 /* We did not really want to consume any tokens. */
14351 cp_parser_abort_tentative_parse (parser);
14352
14353 return constructor_p;
14354 }
14355
14356 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14357 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14358 they must be performed once we are in the scope of the function.
14359
14360 Returns the function defined. */
14361
14362 static tree
14363 cp_parser_function_definition_from_specifiers_and_declarator
14364 (cp_parser* parser,
14365 tree decl_specifiers,
14366 tree attributes,
14367 tree declarator)
14368 {
14369 tree fn;
14370 bool success_p;
14371
14372 /* Begin the function-definition. */
14373 success_p = begin_function_definition (decl_specifiers,
14374 attributes,
14375 declarator);
14376
14377 /* If there were names looked up in the decl-specifier-seq that we
14378 did not check, check them now. We must wait until we are in the
14379 scope of the function to perform the checks, since the function
14380 might be a friend. */
14381 perform_deferred_access_checks ();
14382
14383 if (!success_p)
14384 {
14385 /* If begin_function_definition didn't like the definition, skip
14386 the entire function. */
14387 error ("invalid function declaration");
14388 cp_parser_skip_to_end_of_block_or_statement (parser);
14389 fn = error_mark_node;
14390 }
14391 else
14392 fn = cp_parser_function_definition_after_declarator (parser,
14393 /*inline_p=*/false);
14394
14395 return fn;
14396 }
14397
14398 /* Parse the part of a function-definition that follows the
14399 declarator. INLINE_P is TRUE iff this function is an inline
14400 function defined with a class-specifier.
14401
14402 Returns the function defined. */
14403
14404 static tree
14405 cp_parser_function_definition_after_declarator (cp_parser* parser,
14406 bool inline_p)
14407 {
14408 tree fn;
14409 bool ctor_initializer_p = false;
14410 bool saved_in_unbraced_linkage_specification_p;
14411 unsigned saved_num_template_parameter_lists;
14412
14413 /* If the next token is `return', then the code may be trying to
14414 make use of the "named return value" extension that G++ used to
14415 support. */
14416 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14417 {
14418 /* Consume the `return' keyword. */
14419 cp_lexer_consume_token (parser->lexer);
14420 /* Look for the identifier that indicates what value is to be
14421 returned. */
14422 cp_parser_identifier (parser);
14423 /* Issue an error message. */
14424 error ("named return values are no longer supported");
14425 /* Skip tokens until we reach the start of the function body. */
14426 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14427 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14428 cp_lexer_consume_token (parser->lexer);
14429 }
14430 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14431 anything declared inside `f'. */
14432 saved_in_unbraced_linkage_specification_p
14433 = parser->in_unbraced_linkage_specification_p;
14434 parser->in_unbraced_linkage_specification_p = false;
14435 /* Inside the function, surrounding template-parameter-lists do not
14436 apply. */
14437 saved_num_template_parameter_lists
14438 = parser->num_template_parameter_lists;
14439 parser->num_template_parameter_lists = 0;
14440 /* If the next token is `try', then we are looking at a
14441 function-try-block. */
14442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14443 ctor_initializer_p = cp_parser_function_try_block (parser);
14444 /* A function-try-block includes the function-body, so we only do
14445 this next part if we're not processing a function-try-block. */
14446 else
14447 ctor_initializer_p
14448 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14449
14450 /* Finish the function. */
14451 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14452 (inline_p ? 2 : 0));
14453 /* Generate code for it, if necessary. */
14454 expand_or_defer_fn (fn);
14455 /* Restore the saved values. */
14456 parser->in_unbraced_linkage_specification_p
14457 = saved_in_unbraced_linkage_specification_p;
14458 parser->num_template_parameter_lists
14459 = saved_num_template_parameter_lists;
14460
14461 return fn;
14462 }
14463
14464 /* Parse a template-declaration, assuming that the `export' (and
14465 `extern') keywords, if present, has already been scanned. MEMBER_P
14466 is as for cp_parser_template_declaration. */
14467
14468 static void
14469 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14470 {
14471 tree decl = NULL_TREE;
14472 tree parameter_list;
14473 bool friend_p = false;
14474
14475 /* Look for the `template' keyword. */
14476 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14477 return;
14478
14479 /* And the `<'. */
14480 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14481 return;
14482
14483 /* If the next token is `>', then we have an invalid
14484 specialization. Rather than complain about an invalid template
14485 parameter, issue an error message here. */
14486 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14487 {
14488 cp_parser_error (parser, "invalid explicit specialization");
14489 begin_specialization ();
14490 parameter_list = NULL_TREE;
14491 }
14492 else
14493 {
14494 /* Parse the template parameters. */
14495 begin_template_parm_list ();
14496 parameter_list = cp_parser_template_parameter_list (parser);
14497 parameter_list = end_template_parm_list (parameter_list);
14498 }
14499
14500 /* Look for the `>'. */
14501 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14502 /* We just processed one more parameter list. */
14503 ++parser->num_template_parameter_lists;
14504 /* If the next token is `template', there are more template
14505 parameters. */
14506 if (cp_lexer_next_token_is_keyword (parser->lexer,
14507 RID_TEMPLATE))
14508 cp_parser_template_declaration_after_export (parser, member_p);
14509 else
14510 {
14511 decl = cp_parser_single_declaration (parser,
14512 member_p,
14513 &friend_p);
14514
14515 /* If this is a member template declaration, let the front
14516 end know. */
14517 if (member_p && !friend_p && decl)
14518 {
14519 if (TREE_CODE (decl) == TYPE_DECL)
14520 cp_parser_check_access_in_redeclaration (decl);
14521
14522 decl = finish_member_template_decl (decl);
14523 }
14524 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14525 make_friend_class (current_class_type, TREE_TYPE (decl),
14526 /*complain=*/true);
14527 }
14528 /* We are done with the current parameter list. */
14529 --parser->num_template_parameter_lists;
14530
14531 /* Finish up. */
14532 finish_template_decl (parameter_list);
14533
14534 /* Register member declarations. */
14535 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14536 finish_member_declaration (decl);
14537
14538 /* If DECL is a function template, we must return to parse it later.
14539 (Even though there is no definition, there might be default
14540 arguments that need handling.) */
14541 if (member_p && decl
14542 && (TREE_CODE (decl) == FUNCTION_DECL
14543 || DECL_FUNCTION_TEMPLATE_P (decl)))
14544 TREE_VALUE (parser->unparsed_functions_queues)
14545 = tree_cons (NULL_TREE, decl,
14546 TREE_VALUE (parser->unparsed_functions_queues));
14547 }
14548
14549 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14550 `function-definition' sequence. MEMBER_P is true, this declaration
14551 appears in a class scope.
14552
14553 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14554 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14555
14556 static tree
14557 cp_parser_single_declaration (cp_parser* parser,
14558 bool member_p,
14559 bool* friend_p)
14560 {
14561 int declares_class_or_enum;
14562 tree decl = NULL_TREE;
14563 tree decl_specifiers;
14564 tree attributes;
14565 bool function_definition_p = false;
14566
14567 /* Defer access checks until we know what is being declared. */
14568 push_deferring_access_checks (dk_deferred);
14569
14570 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14571 alternative. */
14572 decl_specifiers
14573 = cp_parser_decl_specifier_seq (parser,
14574 CP_PARSER_FLAGS_OPTIONAL,
14575 &attributes,
14576 &declares_class_or_enum);
14577 if (friend_p)
14578 *friend_p = cp_parser_friend_p (decl_specifiers);
14579 /* Gather up the access checks that occurred the
14580 decl-specifier-seq. */
14581 stop_deferring_access_checks ();
14582
14583 /* Check for the declaration of a template class. */
14584 if (declares_class_or_enum)
14585 {
14586 if (cp_parser_declares_only_class_p (parser))
14587 {
14588 decl = shadow_tag (decl_specifiers);
14589 if (decl)
14590 decl = TYPE_NAME (decl);
14591 else
14592 decl = error_mark_node;
14593 }
14594 }
14595 else
14596 decl = NULL_TREE;
14597 /* If it's not a template class, try for a template function. If
14598 the next token is a `;', then this declaration does not declare
14599 anything. But, if there were errors in the decl-specifiers, then
14600 the error might well have come from an attempted class-specifier.
14601 In that case, there's no need to warn about a missing declarator. */
14602 if (!decl
14603 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14604 || !value_member (error_mark_node, decl_specifiers)))
14605 decl = cp_parser_init_declarator (parser,
14606 decl_specifiers,
14607 attributes,
14608 /*function_definition_allowed_p=*/true,
14609 member_p,
14610 declares_class_or_enum,
14611 &function_definition_p);
14612
14613 pop_deferring_access_checks ();
14614
14615 /* Clear any current qualification; whatever comes next is the start
14616 of something new. */
14617 parser->scope = NULL_TREE;
14618 parser->qualifying_scope = NULL_TREE;
14619 parser->object_scope = NULL_TREE;
14620 /* Look for a trailing `;' after the declaration. */
14621 if (!function_definition_p
14622 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14623 cp_parser_skip_to_end_of_block_or_statement (parser);
14624
14625 return decl;
14626 }
14627
14628 /* Parse a cast-expression that is not the operand of a unary "&". */
14629
14630 static tree
14631 cp_parser_simple_cast_expression (cp_parser *parser)
14632 {
14633 return cp_parser_cast_expression (parser, /*address_p=*/false);
14634 }
14635
14636 /* Parse a functional cast to TYPE. Returns an expression
14637 representing the cast. */
14638
14639 static tree
14640 cp_parser_functional_cast (cp_parser* parser, tree type)
14641 {
14642 tree expression_list;
14643 tree cast;
14644
14645 expression_list
14646 = cp_parser_parenthesized_expression_list (parser, false,
14647 /*non_constant_p=*/NULL);
14648
14649 cast = build_functional_cast (type, expression_list);
14650 /* [expr.const]/1: In an integral constant expression "only type
14651 conversions to integral or enumeration type can be used". */
14652 if (cast != error_mark_node && !type_dependent_expression_p (type)
14653 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14654 {
14655 if (cp_parser_non_integral_constant_expression
14656 (parser, "a call to a constructor"))
14657 return error_mark_node;
14658 }
14659 return cast;
14660 }
14661
14662 /* Save the tokens that make up the body of a member function defined
14663 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14664 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14665 specifiers applied to the declaration. Returns the FUNCTION_DECL
14666 for the member function. */
14667
14668 static tree
14669 cp_parser_save_member_function_body (cp_parser* parser,
14670 tree decl_specifiers,
14671 tree declarator,
14672 tree attributes)
14673 {
14674 cp_token_cache *cache;
14675 tree fn;
14676
14677 /* Create the function-declaration. */
14678 fn = start_method (decl_specifiers, declarator, attributes);
14679 /* If something went badly wrong, bail out now. */
14680 if (fn == error_mark_node)
14681 {
14682 /* If there's a function-body, skip it. */
14683 if (cp_parser_token_starts_function_definition_p
14684 (cp_lexer_peek_token (parser->lexer)))
14685 cp_parser_skip_to_end_of_block_or_statement (parser);
14686 return error_mark_node;
14687 }
14688
14689 /* Remember it, if there default args to post process. */
14690 cp_parser_save_default_args (parser, fn);
14691
14692 /* Create a token cache. */
14693 cache = cp_token_cache_new ();
14694 /* Save away the tokens that make up the body of the
14695 function. */
14696 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14697 /* Handle function try blocks. */
14698 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14699 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14700
14701 /* Save away the inline definition; we will process it when the
14702 class is complete. */
14703 DECL_PENDING_INLINE_INFO (fn) = cache;
14704 DECL_PENDING_INLINE_P (fn) = 1;
14705
14706 /* We need to know that this was defined in the class, so that
14707 friend templates are handled correctly. */
14708 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14709
14710 /* We're done with the inline definition. */
14711 finish_method (fn);
14712
14713 /* Add FN to the queue of functions to be parsed later. */
14714 TREE_VALUE (parser->unparsed_functions_queues)
14715 = tree_cons (NULL_TREE, fn,
14716 TREE_VALUE (parser->unparsed_functions_queues));
14717
14718 return fn;
14719 }
14720
14721 /* Parse a template-argument-list, as well as the trailing ">" (but
14722 not the opening ">"). See cp_parser_template_argument_list for the
14723 return value. */
14724
14725 static tree
14726 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14727 {
14728 tree arguments;
14729 tree saved_scope;
14730 tree saved_qualifying_scope;
14731 tree saved_object_scope;
14732 bool saved_greater_than_is_operator_p;
14733
14734 /* [temp.names]
14735
14736 When parsing a template-id, the first non-nested `>' is taken as
14737 the end of the template-argument-list rather than a greater-than
14738 operator. */
14739 saved_greater_than_is_operator_p
14740 = parser->greater_than_is_operator_p;
14741 parser->greater_than_is_operator_p = false;
14742 /* Parsing the argument list may modify SCOPE, so we save it
14743 here. */
14744 saved_scope = parser->scope;
14745 saved_qualifying_scope = parser->qualifying_scope;
14746 saved_object_scope = parser->object_scope;
14747 /* Parse the template-argument-list itself. */
14748 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14749 arguments = NULL_TREE;
14750 else
14751 arguments = cp_parser_template_argument_list (parser);
14752 /* Look for the `>' that ends the template-argument-list. If we find
14753 a '>>' instead, it's probably just a typo. */
14754 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14755 {
14756 if (!saved_greater_than_is_operator_p)
14757 {
14758 /* If we're in a nested template argument list, the '>>' has to be
14759 a typo for '> >'. We emit the error message, but we continue
14760 parsing and we push a '>' as next token, so that the argument
14761 list will be parsed correctly.. */
14762 cp_token* token;
14763 error ("`>>' should be `> >' within a nested template argument list");
14764 token = cp_lexer_peek_token (parser->lexer);
14765 token->type = CPP_GREATER;
14766 }
14767 else
14768 {
14769 /* If this is not a nested template argument list, the '>>' is
14770 a typo for '>'. Emit an error message and continue. */
14771 error ("spurious `>>', use `>' to terminate a template argument list");
14772 cp_lexer_consume_token (parser->lexer);
14773 }
14774 }
14775 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14776 error ("missing `>' to terminate the template argument list");
14777 /* The `>' token might be a greater-than operator again now. */
14778 parser->greater_than_is_operator_p
14779 = saved_greater_than_is_operator_p;
14780 /* Restore the SAVED_SCOPE. */
14781 parser->scope = saved_scope;
14782 parser->qualifying_scope = saved_qualifying_scope;
14783 parser->object_scope = saved_object_scope;
14784
14785 return arguments;
14786 }
14787
14788 /* MEMBER_FUNCTION is a member function, or a friend. If default
14789 arguments, or the body of the function have not yet been parsed,
14790 parse them now. */
14791
14792 static void
14793 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14794 {
14795 cp_lexer *saved_lexer;
14796
14797 /* If this member is a template, get the underlying
14798 FUNCTION_DECL. */
14799 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14800 member_function = DECL_TEMPLATE_RESULT (member_function);
14801
14802 /* There should not be any class definitions in progress at this
14803 point; the bodies of members are only parsed outside of all class
14804 definitions. */
14805 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14806 /* While we're parsing the member functions we might encounter more
14807 classes. We want to handle them right away, but we don't want
14808 them getting mixed up with functions that are currently in the
14809 queue. */
14810 parser->unparsed_functions_queues
14811 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14812
14813 /* Make sure that any template parameters are in scope. */
14814 maybe_begin_member_template_processing (member_function);
14815
14816 /* If the body of the function has not yet been parsed, parse it
14817 now. */
14818 if (DECL_PENDING_INLINE_P (member_function))
14819 {
14820 tree function_scope;
14821 cp_token_cache *tokens;
14822
14823 /* The function is no longer pending; we are processing it. */
14824 tokens = DECL_PENDING_INLINE_INFO (member_function);
14825 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14826 DECL_PENDING_INLINE_P (member_function) = 0;
14827 /* If this was an inline function in a local class, enter the scope
14828 of the containing function. */
14829 function_scope = decl_function_context (member_function);
14830 if (function_scope)
14831 push_function_context_to (function_scope);
14832
14833 /* Save away the current lexer. */
14834 saved_lexer = parser->lexer;
14835 /* Make a new lexer to feed us the tokens saved for this function. */
14836 parser->lexer = cp_lexer_new_from_tokens (tokens);
14837 parser->lexer->next = saved_lexer;
14838
14839 /* Set the current source position to be the location of the first
14840 token in the saved inline body. */
14841 cp_lexer_peek_token (parser->lexer);
14842
14843 /* Let the front end know that we going to be defining this
14844 function. */
14845 start_function (NULL_TREE, member_function, NULL_TREE,
14846 SF_PRE_PARSED | SF_INCLASS_INLINE);
14847
14848 /* Now, parse the body of the function. */
14849 cp_parser_function_definition_after_declarator (parser,
14850 /*inline_p=*/true);
14851
14852 /* Leave the scope of the containing function. */
14853 if (function_scope)
14854 pop_function_context_from (function_scope);
14855 /* Restore the lexer. */
14856 parser->lexer = saved_lexer;
14857 }
14858
14859 /* Remove any template parameters from the symbol table. */
14860 maybe_end_member_template_processing ();
14861
14862 /* Restore the queue. */
14863 parser->unparsed_functions_queues
14864 = TREE_CHAIN (parser->unparsed_functions_queues);
14865 }
14866
14867 /* If DECL contains any default args, remember it on the unparsed
14868 functions queue. */
14869
14870 static void
14871 cp_parser_save_default_args (cp_parser* parser, tree decl)
14872 {
14873 tree probe;
14874
14875 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14876 probe;
14877 probe = TREE_CHAIN (probe))
14878 if (TREE_PURPOSE (probe))
14879 {
14880 TREE_PURPOSE (parser->unparsed_functions_queues)
14881 = tree_cons (NULL_TREE, decl,
14882 TREE_PURPOSE (parser->unparsed_functions_queues));
14883 break;
14884 }
14885 return;
14886 }
14887
14888 /* FN is a FUNCTION_DECL which may contains a parameter with an
14889 unparsed DEFAULT_ARG. Parse the default args now. */
14890
14891 static void
14892 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14893 {
14894 cp_lexer *saved_lexer;
14895 cp_token_cache *tokens;
14896 bool saved_local_variables_forbidden_p;
14897 tree parameters;
14898
14899 /* While we're parsing the default args, we might (due to the
14900 statement expression extension) encounter more classes. We want
14901 to handle them right away, but we don't want them getting mixed
14902 up with default args that are currently in the queue. */
14903 parser->unparsed_functions_queues
14904 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14905
14906 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14907 parameters;
14908 parameters = TREE_CHAIN (parameters))
14909 {
14910 if (!TREE_PURPOSE (parameters)
14911 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14912 continue;
14913
14914 /* Save away the current lexer. */
14915 saved_lexer = parser->lexer;
14916 /* Create a new one, using the tokens we have saved. */
14917 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14918 parser->lexer = cp_lexer_new_from_tokens (tokens);
14919
14920 /* Set the current source position to be the location of the
14921 first token in the default argument. */
14922 cp_lexer_peek_token (parser->lexer);
14923
14924 /* Local variable names (and the `this' keyword) may not appear
14925 in a default argument. */
14926 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14927 parser->local_variables_forbidden_p = true;
14928 /* Parse the assignment-expression. */
14929 if (DECL_CLASS_SCOPE_P (fn))
14930 push_nested_class (DECL_CONTEXT (fn));
14931 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14932 if (DECL_CLASS_SCOPE_P (fn))
14933 pop_nested_class ();
14934
14935 /* If the token stream has not been completely used up, then
14936 there was extra junk after the end of the default
14937 argument. */
14938 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14939 cp_parser_error (parser, "expected `,'");
14940
14941 /* Restore saved state. */
14942 parser->lexer = saved_lexer;
14943 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14944 }
14945
14946 /* Restore the queue. */
14947 parser->unparsed_functions_queues
14948 = TREE_CHAIN (parser->unparsed_functions_queues);
14949 }
14950
14951 /* Parse the operand of `sizeof' (or a similar operator). Returns
14952 either a TYPE or an expression, depending on the form of the
14953 input. The KEYWORD indicates which kind of expression we have
14954 encountered. */
14955
14956 static tree
14957 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14958 {
14959 static const char *format;
14960 tree expr = NULL_TREE;
14961 const char *saved_message;
14962 bool saved_integral_constant_expression_p;
14963
14964 /* Initialize FORMAT the first time we get here. */
14965 if (!format)
14966 format = "types may not be defined in `%s' expressions";
14967
14968 /* Types cannot be defined in a `sizeof' expression. Save away the
14969 old message. */
14970 saved_message = parser->type_definition_forbidden_message;
14971 /* And create the new one. */
14972 parser->type_definition_forbidden_message
14973 = xmalloc (strlen (format)
14974 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14975 + 1 /* `\0' */);
14976 sprintf ((char *) parser->type_definition_forbidden_message,
14977 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14978
14979 /* The restrictions on constant-expressions do not apply inside
14980 sizeof expressions. */
14981 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14982 parser->integral_constant_expression_p = false;
14983
14984 /* Do not actually evaluate the expression. */
14985 ++skip_evaluation;
14986 /* If it's a `(', then we might be looking at the type-id
14987 construction. */
14988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14989 {
14990 tree type;
14991 bool saved_in_type_id_in_expr_p;
14992
14993 /* We can't be sure yet whether we're looking at a type-id or an
14994 expression. */
14995 cp_parser_parse_tentatively (parser);
14996 /* Consume the `('. */
14997 cp_lexer_consume_token (parser->lexer);
14998 /* Parse the type-id. */
14999 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15000 parser->in_type_id_in_expr_p = true;
15001 type = cp_parser_type_id (parser);
15002 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15003 /* Now, look for the trailing `)'. */
15004 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15005 /* If all went well, then we're done. */
15006 if (cp_parser_parse_definitely (parser))
15007 {
15008 /* Build a list of decl-specifiers; right now, we have only
15009 a single type-specifier. */
15010 type = build_tree_list (NULL_TREE,
15011 type);
15012
15013 /* Call grokdeclarator to figure out what type this is. */
15014 expr = grokdeclarator (NULL_TREE,
15015 type,
15016 TYPENAME,
15017 /*initialized=*/0,
15018 /*attrlist=*/NULL);
15019 }
15020 }
15021
15022 /* If the type-id production did not work out, then we must be
15023 looking at the unary-expression production. */
15024 if (!expr)
15025 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15026 /* Go back to evaluating expressions. */
15027 --skip_evaluation;
15028
15029 /* Free the message we created. */
15030 free ((char *) parser->type_definition_forbidden_message);
15031 /* And restore the old one. */
15032 parser->type_definition_forbidden_message = saved_message;
15033 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15034
15035 return expr;
15036 }
15037
15038 /* If the current declaration has no declarator, return true. */
15039
15040 static bool
15041 cp_parser_declares_only_class_p (cp_parser *parser)
15042 {
15043 /* If the next token is a `;' or a `,' then there is no
15044 declarator. */
15045 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15046 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15047 }
15048
15049 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15050 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15051
15052 static bool
15053 cp_parser_friend_p (tree decl_specifiers)
15054 {
15055 while (decl_specifiers)
15056 {
15057 /* See if this decl-specifier is `friend'. */
15058 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15059 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15060 return true;
15061
15062 /* Go on to the next decl-specifier. */
15063 decl_specifiers = TREE_CHAIN (decl_specifiers);
15064 }
15065
15066 return false;
15067 }
15068
15069 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15070 issue an error message indicating that TOKEN_DESC was expected.
15071
15072 Returns the token consumed, if the token had the appropriate type.
15073 Otherwise, returns NULL. */
15074
15075 static cp_token *
15076 cp_parser_require (cp_parser* parser,
15077 enum cpp_ttype type,
15078 const char* token_desc)
15079 {
15080 if (cp_lexer_next_token_is (parser->lexer, type))
15081 return cp_lexer_consume_token (parser->lexer);
15082 else
15083 {
15084 /* Output the MESSAGE -- unless we're parsing tentatively. */
15085 if (!cp_parser_simulate_error (parser))
15086 {
15087 char *message = concat ("expected ", token_desc, NULL);
15088 cp_parser_error (parser, message);
15089 free (message);
15090 }
15091 return NULL;
15092 }
15093 }
15094
15095 /* Like cp_parser_require, except that tokens will be skipped until
15096 the desired token is found. An error message is still produced if
15097 the next token is not as expected. */
15098
15099 static void
15100 cp_parser_skip_until_found (cp_parser* parser,
15101 enum cpp_ttype type,
15102 const char* token_desc)
15103 {
15104 cp_token *token;
15105 unsigned nesting_depth = 0;
15106
15107 if (cp_parser_require (parser, type, token_desc))
15108 return;
15109
15110 /* Skip tokens until the desired token is found. */
15111 while (true)
15112 {
15113 /* Peek at the next token. */
15114 token = cp_lexer_peek_token (parser->lexer);
15115 /* If we've reached the token we want, consume it and
15116 stop. */
15117 if (token->type == type && !nesting_depth)
15118 {
15119 cp_lexer_consume_token (parser->lexer);
15120 return;
15121 }
15122 /* If we've run out of tokens, stop. */
15123 if (token->type == CPP_EOF)
15124 return;
15125 if (token->type == CPP_OPEN_BRACE
15126 || token->type == CPP_OPEN_PAREN
15127 || token->type == CPP_OPEN_SQUARE)
15128 ++nesting_depth;
15129 else if (token->type == CPP_CLOSE_BRACE
15130 || token->type == CPP_CLOSE_PAREN
15131 || token->type == CPP_CLOSE_SQUARE)
15132 {
15133 if (nesting_depth-- == 0)
15134 return;
15135 }
15136 /* Consume this token. */
15137 cp_lexer_consume_token (parser->lexer);
15138 }
15139 }
15140
15141 /* If the next token is the indicated keyword, consume it. Otherwise,
15142 issue an error message indicating that TOKEN_DESC was expected.
15143
15144 Returns the token consumed, if the token had the appropriate type.
15145 Otherwise, returns NULL. */
15146
15147 static cp_token *
15148 cp_parser_require_keyword (cp_parser* parser,
15149 enum rid keyword,
15150 const char* token_desc)
15151 {
15152 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15153
15154 if (token && token->keyword != keyword)
15155 {
15156 dyn_string_t error_msg;
15157
15158 /* Format the error message. */
15159 error_msg = dyn_string_new (0);
15160 dyn_string_append_cstr (error_msg, "expected ");
15161 dyn_string_append_cstr (error_msg, token_desc);
15162 cp_parser_error (parser, error_msg->s);
15163 dyn_string_delete (error_msg);
15164 return NULL;
15165 }
15166
15167 return token;
15168 }
15169
15170 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15171 function-definition. */
15172
15173 static bool
15174 cp_parser_token_starts_function_definition_p (cp_token* token)
15175 {
15176 return (/* An ordinary function-body begins with an `{'. */
15177 token->type == CPP_OPEN_BRACE
15178 /* A ctor-initializer begins with a `:'. */
15179 || token->type == CPP_COLON
15180 /* A function-try-block begins with `try'. */
15181 || token->keyword == RID_TRY
15182 /* The named return value extension begins with `return'. */
15183 || token->keyword == RID_RETURN);
15184 }
15185
15186 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15187 definition. */
15188
15189 static bool
15190 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15191 {
15192 cp_token *token;
15193
15194 token = cp_lexer_peek_token (parser->lexer);
15195 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15196 }
15197
15198 /* Returns TRUE iff the next token is the "," or ">" ending a
15199 template-argument. ">>" is also accepted (after the full
15200 argument was parsed) because it's probably a typo for "> >",
15201 and there is a specific diagnostic for this. */
15202
15203 static bool
15204 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15205 {
15206 cp_token *token;
15207
15208 token = cp_lexer_peek_token (parser->lexer);
15209 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15210 || token->type == CPP_RSHIFT);
15211 }
15212
15213 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15214 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15215
15216 static bool
15217 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15218 size_t n)
15219 {
15220 cp_token *token;
15221
15222 token = cp_lexer_peek_nth_token (parser->lexer, n);
15223 if (token->type == CPP_LESS)
15224 return true;
15225 /* Check for the sequence `<::' in the original code. It would be lexed as
15226 `[:', where `[' is a digraph, and there is no whitespace before
15227 `:'. */
15228 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15229 {
15230 cp_token *token2;
15231 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15232 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15233 return true;
15234 }
15235 return false;
15236 }
15237
15238 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15239 or none_type otherwise. */
15240
15241 static enum tag_types
15242 cp_parser_token_is_class_key (cp_token* token)
15243 {
15244 switch (token->keyword)
15245 {
15246 case RID_CLASS:
15247 return class_type;
15248 case RID_STRUCT:
15249 return record_type;
15250 case RID_UNION:
15251 return union_type;
15252
15253 default:
15254 return none_type;
15255 }
15256 }
15257
15258 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15259
15260 static void
15261 cp_parser_check_class_key (enum tag_types class_key, tree type)
15262 {
15263 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15264 pedwarn ("`%s' tag used in naming `%#T'",
15265 class_key == union_type ? "union"
15266 : class_key == record_type ? "struct" : "class",
15267 type);
15268 }
15269
15270 /* Issue an error message if DECL is redeclared with different
15271 access than its original declaration [class.access.spec/3].
15272 This applies to nested classes and nested class templates.
15273 [class.mem/1]. */
15274
15275 static void cp_parser_check_access_in_redeclaration (tree decl)
15276 {
15277 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15278 return;
15279
15280 if ((TREE_PRIVATE (decl)
15281 != (current_access_specifier == access_private_node))
15282 || (TREE_PROTECTED (decl)
15283 != (current_access_specifier == access_protected_node)))
15284 error ("%D redeclared with different access", decl);
15285 }
15286
15287 /* Look for the `template' keyword, as a syntactic disambiguator.
15288 Return TRUE iff it is present, in which case it will be
15289 consumed. */
15290
15291 static bool
15292 cp_parser_optional_template_keyword (cp_parser *parser)
15293 {
15294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15295 {
15296 /* The `template' keyword can only be used within templates;
15297 outside templates the parser can always figure out what is a
15298 template and what is not. */
15299 if (!processing_template_decl)
15300 {
15301 error ("`template' (as a disambiguator) is only allowed "
15302 "within templates");
15303 /* If this part of the token stream is rescanned, the same
15304 error message would be generated. So, we purge the token
15305 from the stream. */
15306 cp_lexer_purge_token (parser->lexer);
15307 return false;
15308 }
15309 else
15310 {
15311 /* Consume the `template' keyword. */
15312 cp_lexer_consume_token (parser->lexer);
15313 return true;
15314 }
15315 }
15316
15317 return false;
15318 }
15319
15320 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15321 set PARSER->SCOPE, and perform other related actions. */
15322
15323 static void
15324 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15325 {
15326 tree value;
15327 tree check;
15328
15329 /* Get the stored value. */
15330 value = cp_lexer_consume_token (parser->lexer)->value;
15331 /* Perform any access checks that were deferred. */
15332 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15333 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15334 /* Set the scope from the stored value. */
15335 parser->scope = TREE_VALUE (value);
15336 parser->qualifying_scope = TREE_TYPE (value);
15337 parser->object_scope = NULL_TREE;
15338 }
15339
15340 /* Add tokens to CACHE until a non-nested END token appears. */
15341
15342 static void
15343 cp_parser_cache_group (cp_parser *parser,
15344 cp_token_cache *cache,
15345 enum cpp_ttype end,
15346 unsigned depth)
15347 {
15348 while (true)
15349 {
15350 cp_token *token;
15351
15352 /* Abort a parenthesized expression if we encounter a brace. */
15353 if ((end == CPP_CLOSE_PAREN || depth == 0)
15354 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15355 return;
15356 /* If we've reached the end of the file, stop. */
15357 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15358 return;
15359 /* Consume the next token. */
15360 token = cp_lexer_consume_token (parser->lexer);
15361 /* Add this token to the tokens we are saving. */
15362 cp_token_cache_push_token (cache, token);
15363 /* See if it starts a new group. */
15364 if (token->type == CPP_OPEN_BRACE)
15365 {
15366 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15367 if (depth == 0)
15368 return;
15369 }
15370 else if (token->type == CPP_OPEN_PAREN)
15371 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15372 else if (token->type == end)
15373 return;
15374 }
15375 }
15376
15377 /* Begin parsing tentatively. We always save tokens while parsing
15378 tentatively so that if the tentative parsing fails we can restore the
15379 tokens. */
15380
15381 static void
15382 cp_parser_parse_tentatively (cp_parser* parser)
15383 {
15384 /* Enter a new parsing context. */
15385 parser->context = cp_parser_context_new (parser->context);
15386 /* Begin saving tokens. */
15387 cp_lexer_save_tokens (parser->lexer);
15388 /* In order to avoid repetitive access control error messages,
15389 access checks are queued up until we are no longer parsing
15390 tentatively. */
15391 push_deferring_access_checks (dk_deferred);
15392 }
15393
15394 /* Commit to the currently active tentative parse. */
15395
15396 static void
15397 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15398 {
15399 cp_parser_context *context;
15400 cp_lexer *lexer;
15401
15402 /* Mark all of the levels as committed. */
15403 lexer = parser->lexer;
15404 for (context = parser->context; context->next; context = context->next)
15405 {
15406 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15407 break;
15408 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15409 while (!cp_lexer_saving_tokens (lexer))
15410 lexer = lexer->next;
15411 cp_lexer_commit_tokens (lexer);
15412 }
15413 }
15414
15415 /* Abort the currently active tentative parse. All consumed tokens
15416 will be rolled back, and no diagnostics will be issued. */
15417
15418 static void
15419 cp_parser_abort_tentative_parse (cp_parser* parser)
15420 {
15421 cp_parser_simulate_error (parser);
15422 /* Now, pretend that we want to see if the construct was
15423 successfully parsed. */
15424 cp_parser_parse_definitely (parser);
15425 }
15426
15427 /* Stop parsing tentatively. If a parse error has occurred, restore the
15428 token stream. Otherwise, commit to the tokens we have consumed.
15429 Returns true if no error occurred; false otherwise. */
15430
15431 static bool
15432 cp_parser_parse_definitely (cp_parser* parser)
15433 {
15434 bool error_occurred;
15435 cp_parser_context *context;
15436
15437 /* Remember whether or not an error occurred, since we are about to
15438 destroy that information. */
15439 error_occurred = cp_parser_error_occurred (parser);
15440 /* Remove the topmost context from the stack. */
15441 context = parser->context;
15442 parser->context = context->next;
15443 /* If no parse errors occurred, commit to the tentative parse. */
15444 if (!error_occurred)
15445 {
15446 /* Commit to the tokens read tentatively, unless that was
15447 already done. */
15448 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15449 cp_lexer_commit_tokens (parser->lexer);
15450
15451 pop_to_parent_deferring_access_checks ();
15452 }
15453 /* Otherwise, if errors occurred, roll back our state so that things
15454 are just as they were before we began the tentative parse. */
15455 else
15456 {
15457 cp_lexer_rollback_tokens (parser->lexer);
15458 pop_deferring_access_checks ();
15459 }
15460 /* Add the context to the front of the free list. */
15461 context->next = cp_parser_context_free_list;
15462 cp_parser_context_free_list = context;
15463
15464 return !error_occurred;
15465 }
15466
15467 /* Returns true if we are parsing tentatively -- but have decided that
15468 we will stick with this tentative parse, even if errors occur. */
15469
15470 static bool
15471 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15472 {
15473 return (cp_parser_parsing_tentatively (parser)
15474 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15475 }
15476
15477 /* Returns nonzero iff an error has occurred during the most recent
15478 tentative parse. */
15479
15480 static bool
15481 cp_parser_error_occurred (cp_parser* parser)
15482 {
15483 return (cp_parser_parsing_tentatively (parser)
15484 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15485 }
15486
15487 /* Returns nonzero if GNU extensions are allowed. */
15488
15489 static bool
15490 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15491 {
15492 return parser->allow_gnu_extensions_p;
15493 }
15494
15495 \f
15496 /* The parser. */
15497
15498 static GTY (()) cp_parser *the_parser;
15499
15500 /* External interface. */
15501
15502 /* Parse one entire translation unit. */
15503
15504 void
15505 c_parse_file (void)
15506 {
15507 bool error_occurred;
15508 static bool already_called = false;
15509
15510 if (already_called)
15511 {
15512 sorry ("inter-module optimizations not implemented for C++");
15513 return;
15514 }
15515 already_called = true;
15516
15517 the_parser = cp_parser_new ();
15518 push_deferring_access_checks (flag_access_control
15519 ? dk_no_deferred : dk_no_check);
15520 error_occurred = cp_parser_translation_unit (the_parser);
15521 the_parser = NULL;
15522 }
15523
15524 /* This variable must be provided by every front end. */
15525
15526 int yydebug;
15527
15528 #include "gt-cp-parser.h"